[pyngl-talk] writing a numpy array to a file
David Brown
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
mismatches.
Here is a script that does what you want:
import Nio
import numpy as np
import os
os.system('rm -f test.nc')
ofile = 'test.nc'
outfile = Nio.open_file(ofile, 'w')
outfile.create_dimension('time',None)
outfile.create_dimension('lat', 50)
outfile.create_dimension('lon',50)
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[1] = np.ones( (50,50), dtype = 'float32')
outfile.close()
-dave
On Wed, Sep 3, 2014 at 12:06 PM, whannah <whannah at rsmas.miami.edu> wrote:
> Hi,
>
> 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,:,:]?
>
>
> thanks,
> Walter
>
> _______________________________________________
> pyngl-talk mailing list
> List instructions, subscriber options, unsubscribe:
> http://mailman.ucar.edu/mailman/listinfo/pyngl-talk
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ucar.edu/pipermail/pyngl-talk/attachments/20140903/eab37a83/attachment.html
More information about the pyngl-talk
mailing list