def main(): fileName = sys.argv[1] ncFile = NetCDFFile(fileName, 'r') description = ET.Element('pointDataDescription') variableKeys = ncFile.variables.keys() variableKeys.sort() for variableName in variableKeys: if variableName in excludeVars: continue variableObject = ncFile.variables[variableName] varElement = ET.SubElement(description, 'parameter') varElement.set('name', variableName) typeCode = variableObject.typecode() numDims = len(variableObject.shape) if numDims > 1 and typeCode is 'c': typeCode = 's' numDims = numDims - 1 varElement.set('type', typeMap[typeCode]) varElement.set('numDims', str(numDims)) if 'units' in variableObject._attributes: unit = variableObject._attributes['units'] varElement.set('unit', unit) if '_FillValue' in variableObject._attributes: fillValue = variableObject._attributes['_FillValue'] # Disabled for now: # if fillValue: # varElement.set('fillValue',str(long(fillValue))) indentElement(description) print ET.tostring(description, 'UTF-8')
def __init__(self, fn, frame=0, double=False, store_force=False, mode='r', format='NETCDF4'): self._fn = fn self._data = None try: if __have_netcdf4__: self._data = Dataset(fn, mode, format=format) else: if mode == 'ws': mode = 'w' self._data = NetCDFFile(fn, mode) except RuntimeError as e: raise RuntimeError('Error opening file "{0}": {1}'.format(fn, e)) if double: self._float_str = 'f8' else: self._float_str = 'f4' self._store_force = store_force self._is_amber = False if mode[0] == 'w': self._data.program = 'PyCo' self._data.programVersion = 'N/A' self._is_defined = False else: if 'nx' in self._data.dimensions and 'ny' in self._data.dimensions: try: self._shape = (len(self._data.dimensions['nx']), len(self._data.dimensions['ny'])) except TypeError: self._shape = (self._data.dimensions['nx'], self._data.dimensions['ny']) self._shape2 = self._shape elif 'atom' in self._data.dimensions: n = self._data.dimensions['atom'] nx = int(sqrt(n)) assert nx * nx == n self._shape = (nx, nx) self._shape2 = (nx, nx) self._is_amber = True else: raise RuntimeError('Unknown NetCDF convention used for file ' '%s.' % fn) self._is_defined = True if frame < 0: self._cur_frame = len(self) + frame else: self._cur_frame = frame
""" from pupynere import NetCDFFile import pylab as pl from matplotlib import rcParams try: import numpy rcParams['numerix'] = 'numpy' except: raise ImportError('this example requires numpy') import matplotlib.numerix.ma as MA import matplotlib.numerix as N from matplotlib.toolkits.basemap import Basemap # read in data from netCDF file. infile = 'ccsm_popgrid.nc' fpin = NetCDFFile(infile) tlat = fpin.variables['TLAT'][:] tlon = fpin.variables['TLONG'][:] temp = fpin.variables['TEMP'][:] fillvalue = fpin.variables['TEMP'].attributes['_FillValue'] fpin.close() # make longitudes monotonically increasing. tlon = N.where(N.greater_equal(tlon, min(tlon[:, 0])), tlon - 360, tlon) # create a masked array with temperature data (continents masked). temp = MA.masked_values(temp, fillvalue) # stack grids side-by-side (in longitiudinal direction), so # any range of longitudes may be plotted on a world map. tlon = N.concatenate((tlon, tlon + 360), 1)
from pupynere import NetCDFFile from matplotlib.toolkits.basemap import Basemap, cm import pylab, copy from matplotlib import rcParams # make tick labels smaller rcParams['xtick.labelsize'] = 9 rcParams['ytick.labelsize'] = 9 # plot rainfall from NWS using special precipitation # colormap used by the NWS, and included in basemap. nc = NetCDFFile('nws_precip_conus_20061222.nc') # data from http://www.srh.noaa.gov/rfcshare/precip_analysis_new.php prcpvar = nc.variables['amountofprecip'] data = 0.01 * prcpvar[:] data = pylab.clip(data, 0, 10000) latcorners = nc.variables['lat'][:] loncorners = -nc.variables['lon'][:] plottitle = prcpvar.long_name + ' for period ending ' + prcpvar.dateofdata print data.min(), data.max() print latcorners print loncorners print plottitle print data.shape lon_0 = -nc.variables['true_lon'].getValue() lat_0 = nc.variables['true_lat'].getValue() # create polar stereographic Basemap instance. m = Basemap(projection='stere',lon_0=lon_0,lat_0=90.,lat_ts=lat_0,\ llcrnrlat=latcorners[0],urcrnrlat=latcorners[2],\ llcrnrlon=loncorners[0],urcrnrlon=loncorners[2],\