You're getting the error because you haven't defined a variable named soundings
. If you define this variable by
soundings = f.dimensions[u'soundings']
then you should be able to find the length of soundings
using print len(soundings)
.
Alternatively, you can access the length of the 'soundings' dimension directly by using
print len(f.dimensions[u'soundings'])
I have to admit, I've not used netCDF4, so I read the netCDF4 documentation briefly. In the section 'Dimensions in a netCDF file' it contains the following example of displaying the dimensions of a netCDF4 dataset:
>>> print rootgrp.dimensions
OrderedDict([('level', <netCDF4.Dimension object at 0x1b48030>),
('time', <netCDF4.Dimension object at 0x1b481c0>),
('lat', <netCDF4.Dimension object at 0x1b480f8>),
('lon', <netCDF4.Dimension object at 0x1b48a08>)])
(For brevity, I've omitted the details about where rootgrp
comes from.)
The next line of code in the following code fragment is this:
>>> print len(lon)
What you might have missed is that the variable lon
was declared further up, as
>>> lon = rootgrp.createDimension('lon', 144)
The above section of tutorial deals with creating new dimensions in a netCDF file, whereas you are reading existing dimensions from a netCDF file. You must therefore fetch the dimensions out of the netCDF file.