Raleigh L.
Raleigh L.

Reputation: 923

Creating netcdf file with Scipy?

My job today is to take an array in Numpy and dump it into a net CDF file (.nc) using Scipy. Scipy has a specific sub module for this http://docs.scipy.org/doc/scipy-0.16.0/reference/generated/scipy.io.netcdf.netcdf_file.html, but there isn't many examples there for creating an actual netcdf file.

Here's what data needs to go into the file:

I already have all 3 of those saved in Numpy, I just now have to use Scipy (I also have the NETCDF4 python package, but I couldn't figure it out using that) to put that data into the .nc file.

Here's my code so far:

#testing code about difference
f = netcdf.netcdf_file('2m air temperature difference.nc', 'w')    
f.history = 'describes the difference in 2m air temperature between the period (1992-2011) to (1961-1981)'
f.createDimension('lons', 192)    
f.createDimension('lats', 94)
print 'lons.size', lons.size
print 'lats.size', lats.size
longi = f.createVariable('lons',  'i', 192)
lati = f.createVariable('lats', 'i',  94)
f.createDimension('diff', difference.size)
lons = lons[:]
lats = lats[:]
differ = difference[:]
f.close()

I got from using the default example that was provided in the scipy documentation, and I only replaced the parts that were necessary to make it for my data, everything else is the same as is shown in th eexample.

However,

the only problem is that for some reason I'm getting the error that says "Type Error: 'int' object is not iterable". However, the example given uses i as the data type, and it doesnt provide any other examples of what could be used as a data type in the "createVariable()" method...?

Any help is appreciated.

Upvotes: 0

Views: 1047

Answers (1)

user707650
user707650

Reputation:

The documentation for createVariable says for the third parameter:

List of the dimension names used by the variable, in the desired order.

So you need to provide a list of dimension names, not an integer like 192 or 94. For example,

longi = f.createVariable('lons',  'i', ['lons'])

seems to be correct.

Upvotes: 1

Related Questions