0

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:

  • latitudes (94)
  • longitudes (192)
  • temperature data corresponding to that matrix formed above

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.

4
  • 1
    Which line gets that TypeError? We don't like guessing. Is lons your numpy array? Why the lons=longs[:] line? Commented Oct 24, 2015 at 6:51
  • 1
    You couldn't figure out how to use netcdf4? Because that seems to be the logical choice to use if you want to write netcdf files, especially since you apparently can't use scipy for netcdf either. Commented Oct 24, 2015 at 6:52
  • @Evert So the problem I was having with the netcdf4 package was that I would get a 'RunTimeError: Permission Denied' errror. Here's my code for that attempt: pastebin.com/Du0sTdGn Commented Oct 24, 2015 at 23:37
  • @Evert I googled it and the only helpful information I found was to use the full path of the file (which I then did) but even with that it still gives the error, and I was lost, so I decided to try Scipy. Commented Oct 24, 2015 at 23:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your reply! I didn't quite catch onto the lists of dimension part. I went ahead and made the corrections you suggested but then later when I tried to read the file back in and print it I had some trouble. Also I noticed that the file is only 2 KB, whereas most of the other .nc files I have (downloaded online) are about 70 KB. Code is in the next comment because of the character limit.
EDIT: The formatting didnt quite work out, here's the link on pastebin to the code I used to create the NC file itself. link: pastebin.com/PmKW0fMu
Also here's the code that contains the reading of the same file I wrote to, just to test that it was working. However, when I tried running it I got "Key Error: 'lati'" pastebin.com/PjCxHb9F I used the netCDF4 package to do the reading, and the code is the exact same as it was when I read the two other .nc files I used to make this new one. pastebin.com/PjCxHb9F
I'm not sure I'm getting what you're trying to say in your three comments: does the above work? Does it now fail somewhere else? If the latter, that is a new question, not a comment with a pastebin.
It does technically "write" to the file, but for some reason when I read that file back it won't read the data in. It seems like the data is written incorrectly to the file since it's only 2 KB whereas other .nc files are about 70 KB.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.