[pyngl-talk] writing a numpy array to a file

whannah 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.

Thanks!

Walter

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 
> '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 <http://test.nc>')
> ofile = 'test.nc <http://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 
> <mailto: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/12e5aedb/attachment.html 


More information about the pyngl-talk mailing list