Adil:
I am out of the office this week with very limited email, so please excuse the
delay.
Bill's suggestion to use "load()" is a great way to start. After you do the
"a=load()", you might want to use the "dumpType(a)" method (in JpythonMethods)
to look at the structure of the VisAD Data object that is created.
Furthermore, if you find you need to read the netcdf file "directly", here is
a simple class we use. While it parallels some of the more standard NetCDF
APIs, it provides a simpler way, at times, to inspect the file and its
attributes. It is also useful for extremely large, complex files.
tom
import jarray
import ucar
from java.net import URL
# netcdfhelper -- generic class for helping read NetCDF files
# using the version 1 API. This could / should be broken out.
class netcdfhelper:
def __init__(self, fname):
"""
fname is the NetCDF file name
"""
try:
self.file = ucar.nc2.NetcdfFile(fname)
except:
print "local file: ",fname," not found"
print "trying to open as a remote DODS file..."
try:
self.file = ucar.nc2.dods.DODSNetcdfFile(fname)
except:
print "dods file: ",fname," not found"
print "tyring to open as URL: ",fname
url = URL(fname)
self.file = ucar.nc2.NetcdfFile(url)
def close(self):
self.file.close()
def getDimensions(self):
"""
Return a dictionary of the dimenions for each variable
"""
self.di = self.file.getDimensionIterator()
self.dim = {}
while (self.di.hasNext()):
self.dit = self.di.next()
self.dim[self.dit.getName()] = self.dit.getLength()
return self.dim
def getAttributes(self):
"""
Return a list of the attributes
"""
self.ai = self.file.getGlobalAttributeIterator()
self.att = []
while (self.ai.hasNext()):
self.att.append(self.ai.next())
return self.att
def getAttribute(self,name):
"""
Get a single, named attribute
"""
return self.file.findGlobalAttribute(name)
def getVariable(self, name):
"""
Get a single, named variable
"""
return self.file.findVariable(name)
def getVariables(self):
"""
Return a list of all the variables
"""
self.vi = self.file.getVariableIterator()
self.var = []
while (self.vi.hasNext()):
self.var.append(self.vi.next())
return self.var
def getFloat(self, variable, start):
"""
Get the value of the variable at the index (may be more than
"""
size = []
for i in xrange(len(start)):
size.append(1)
array = variable.read(start, size)
ja = array.copyTo1DJavaArray()
return float(ja[0])
def getValues(self, variable, start=None, size=None):
if (start == None and size == None):
ncdf_array = variable.read()
array = ncdf_array.copyTo1DJavaArray()
return array
else:
slice = []
section = []
for i in xrange(len(start)):
if (size[i] == 1):
slice.append(i)
else:
section.append(i)
mas = variable
if len(slice) > 0:
for i in xrange(len(slice)):
mas = ucar.ma2.MultiArraySlice(mas, slice[(len(slice)-1)-i],
start[slice[(len(slice)-1)-i]])
mas = ucar.ma2.MultiArraySection(mas, [start[section[i]] for i in
xrange(len(section))], [size[section[i]] for i in xrange(len(section))])
ncdf_array = mas.read()
array = ncdf_array.copyTo1DJavaArray()
return array
--
Tom Whittaker (tomw@xxxxxxxxxxxxx)
University of Wisconsin-Madison
Space Science and Engineering Center
Cooperative Institute for Meteorological Satellite Studies
Phone/VoiceMail: 608.262.2759