Problem installing the netCDF4 module with Python 2.7

This is the place for queries that don't fit in any of the other categories.

Problem installing the netCDF4 module with Python 2.7

Postby Albert » Tue Nov 26, 2013 5:20 am

Hello. I am starting a new part-time job, and I have to program in Python (I have done a lot of previous scientific programming, mainly in C and Matlab, but I am not a computer scientist). My employer wants me to use version 2 so I have installed Python 2.7. I also need to get the netCDF4 module working. I have some large data files to analyze. I can open these and analyze them in Matlab with no problems at all, so the data files themselves are all OK. I am using my home laptop with Windows 7 installed.

So: what I did is this. I downloaded and installed the 32 bit numpy and netCDF4 packages for Python 2.7 (specifically, numpy-1.8.0-win32-superpack-python2.7 and netCDF4-1.0.7.win32-py2.7). The installation seemed to be successful, and if I type "import numpy" and "import netCDF4" in python it doesn't complain about anything. However, when I run the following short piece of code, I get an error, not in my code, but in the netCDF4.pyx code (I'll post the error below). Here is my code:

Code: Select all
import numpy
from netCDF4 import Dataset

def main():

    dataset = Dataset(infilename)
    kelvin = dataset.variables['tos'][0]
    sst = kelvin - 273.15
    time = dataset.variables['time']
    lat = dataset.variables['latitude']
    lon = dataset.variables['longitude']
    num_months = len(time)
    print 'Loaded %d months of sst' % num_months


Here is the error I get:

Code: Select all
Traceback (most recent call last):
  File "C:\Albert\Python\", line 22, in <module>
  File "C:\Albert\Python\", line 19, in main
    num_months = len(time)
  File "netCDF4.pyx", line 3070, in netCDF4.Variable.__len__ (netCDF4.c:38796)
  File "netCDF4.pyx", line 2485, in netCDF4.Variable.shape.__get__ (netCDF4.c:30870)
  File "netCDF4.pyx", line 2456, in netCDF4.Variable._getdims (netCDF4.c:30508)
RuntimeError: NetCDF: Not a valid ID

I had a read of the docs, and the only thing I could find that might be the problem is that I might need to install the netCDF4 C libraries. So I got the binaries file netCDF4.3.0-NC4-DAP-32 and installed it. It seems to have installed perfectly; but I still get the same error message. I have looked on the internet as best I can and cannot find any help. If anyone out there can help me, I would greatly appreciate it!

Last edited by micseydel on Tue Nov 26, 2013 7:30 am, edited 1 time in total.
Reason: Code tags, locked.
Posts: 5
Joined: Tue Nov 26, 2013 5:04 am

Re: Problem installing the netCDF4 module with Python 2.7

Postby stranac » Tue Nov 26, 2013 10:34 am

Based on some google searching, I think that the problem is with your trying to get len(time) after dataset was closed.
Try putting that line before dataset.close()
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

Re: Problem installing the netCDF4 module with Python 2.7

Postby Albert » Wed Nov 27, 2013 12:43 am

Thanks stranac! That worked perfectly! I really appreciate your help. I'm completely new to Python, so my apologies for my ignorance! :D
Posts: 5
Joined: Tue Nov 26, 2013 5:04 am

Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], Yahoo [Bot] and 13 guests