def maskNcVar(inNcFile, outNcFile, outVars=[], writeMode='include', conditionVar=None, filterMin=None, filterMax=None, varToMask=None, maskValue=-9999.): nc = NC(inNcFile) if conditionVar is not None: try: condVar = nc.variables[conditionVar] except: die('maskNcVar: Variable %s not in ncFile %s' % (conditionVar, inNcFile)) if varToMask is None: die('maskNcVar: Must specify variable to mask with condition.') try: var = nc.variables[varToMask] except: die('maskNcVar: Variable %s not in ncFile %s' % (varToMask, inNcFile)) if filterMin is not None or filterMax is not None: if filterMin is not None and filterMax is None: cond = N.greater(var, float(filterMin)) elif filterMin is None and filterMax is not None: cond = N.less(var, float(filterMax)) else: cond = N.logical_and(N.greater(var, float(filterMin)), N.less(var, float(filterMax))) var = N.putmask(var, cond, float(maskValue)) outVars = list( set(outVars).add(conditionVar).add(varToMask) ) return subsetNcFile(inNcFile, outVars, outNcFile)
def imageSlice(dataFile, lonSlice, latSlice, varSlice, title, vmin=None, vmax=None, imageWidth=None, imageHeight=None, imageFile=None, projection='cyl', colormap=M.cm.jet): # if dataFile == []: dataFile = 'http://laits.gmu.edu/NWGISS_Temp_Data/WCSfCays5.hdf' if imageFile is None: imageFile = dataFile + '.image.png' print(dataFile) print(imageFile) tmpFile, headers = urlretrieve(dataFile) # tmpFile = 'WCSfCays5.hdf' try: from Scientific.IO.NetCDF import NetCDFFile as NC nc = NC(tmpFile) fileType = 'nc' except: try: import hdfeos grids = hdfeos.grids(tmpFile) fileType = 'hdf_grid' grid = grids[0] except: try: swaths = hdfeos.swaths(tmpFile) fileType = 'hdf_swath' swath = swaths[0] except: raise RuntimeError('imageSlice: Can only slice & image netCDF or HDF grid/swath files.') if fileType == 'nc': lons = evalSlice(nc, lonSlice) lats = evalSlice(nc, latSlice) vals = evalSlice(nc, varSlice) elif fileType == 'hdf_grid': print(grids) fields = hdfeos.grid_fields(tmpFile, grid) print(fields) field = fields[0] dims = hdfeos.grid_field_dims(tmpFile, grid, field) print(dims) lons = hdfeos.grid_field_read(tmpFile, grid, lonSlice) lats = hdfeos.grid_field_read(tmpFile, grid, latSlice) vals = hdfeos.grid_field_read(tmpFile, grid, field) elif fileType == 'hdf_swath': print(swaths) print(hdfeos.swath_geo_fields(tmpFile, swath)) print(hdfeos.swath_data_fields(tmpFile, swath)) lons = hdfeos.get_swath_field(tmpFile, swath, 'Longitude') # assume HDFEOS conventions lats = hdfeos.get_swath_field(tmpFile, swath, 'Latitude') vals = hdfeos.get_swath_field(tmpFile, swath, varSlice) imageMap(lons, lats, vals, vmin, vmax, imageWidth, imageHeight, imageFile, title=title, projection=projection, cmap=colormap) return imageFile
def subsetNcFile(inNcFile, outNcFile, outVars, writeMode='include'): if writeMode == '' or writeMode == 'auto': writeMode = 'include' inf = NC(inNcFile) outf = NC(outNcFile, 'w') return outNcFile