[pyngl-talk] writing a numpy array to a file
whannah at rsmas.miami.edu
Wed Sep 3 13:14:46 MDT 2014
Great, that makes perfect sense. I figured it was a type problem, but I
didn't know how to check the type that the file variable was expecting.
On 9/3/14, 3:09 PM, David Brown wrote:
> 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
> 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 <http://test.nc>')
> ofile = 'test.nc <http://test.nc>'
> outfile = Nio.open_file(ofile, 'w')
> outfile.create_dimension('lat', 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 = np.ones( (50,50), dtype = 'float32')
> On Wed, Sep 3, 2014 at 12:06 PM, whannah <whannah at rsmas.miami.edu
> <mailto: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
> pyngl-talk mailing list
> List instructions, subscriber options, unsubscribe:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the pyngl-talk