Re: I need a simple example about visad+jython+netcdf


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.


import jarray
import ucar
from 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
      self.file = ucar.nc2.NetcdfFile(fname)
      print "local file: ",fname," not found"
      print "trying to open as a remote DODS file..."
        self.file = ucar.nc2.dods.DODSNetcdfFile(fname)
        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):

  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.dim[self.dit.getName()] = self.dit.getLength()
    return self.dim

  def getAttributes(self):
    Return a list of the attributes
    """ = self.file.getGlobalAttributeIterator()
    self.att = []
    while (
    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.file.getVariableIterator()
    self.var = []
    while (
    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)):
    array =, size)
    ja = array.copyTo1DJavaArray()
    return float(ja[0])

  def getValues(self, variable, start=None, size=None):
    if (start == None and size == None):
      ncdf_array =
      array = ncdf_array.copyTo1DJavaArray()
      return array
      slice   = []
      section = []
      for i in xrange(len(start)):
        if (size[i] == 1):

      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 =
      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

  • 2004 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the visad archives: