[pyngl-talk] writing a numpy array to a file
dbrown at ucar.edu
Wed Sep 3 13:09:44 MDT 2014
The error message says "type or dimensional mismatch" and in this case it's
a type mismatch. This is an easy thing to forget and a cause of much wasted
effort. The typecode 'f' specifies a 32 bit float, but in Python (not just
numpy) a float is 64 bits, equivalent to the typecode 'd'.
Maybe in the future we can have separate messages for type and dimensional
Here is a script that does what you want:
import numpy as np
os.system('rm -f test.nc')
ofile = 'test.nc'
outfile = Nio.open_file(ofile, 'w')
v = outfile.create_variable('tvar','f',('time','lat','lon'))
v[0,:,:] = np.ones( (50,50), dtype = 'float32')
# but you only really need to specify the leftmost dimension
v = np.ones( (50,50), dtype = 'float32')
On Wed, Sep 3, 2014 at 12:06 PM, whannah <whannah at rsmas.miami.edu> wrote:
> I'm trying to develop some code that will write new data to a file once a
> day. The "time" dimension is unlimited.
> Here are some snippets of code that I thought should work
> outfile = nio.open_file(ofile, "w")
> fvar = outfile.create_variable('test', 'f', ('time','lat','lon'))
> fvar[0,:,:] = np.ones( (nlat,nlon) , dtype=float )
> The last line throws this error:
> NIOError: type or dimensional mismatch writing to variable (test)
> if I use a constant value
> fvar[0,:,:] = 1.
> or I don't specify the indices of "fvar":
> fvar = np.ones( (nlat,nlon) , dtype=float )
> I don't have any problems. But I really need to specify the time index.
> How do I write different numpy arrays to fvar[0,:,:] and fvar[1,:,:]?
> pyngl-talk mailing list
> List instructions, subscriber options, unsubscribe:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the pyngl-talk