def set_variable(obj, varname, format) : """ Change to VARNAME the variable name for OBJ, which FORMAT maybe 'file' or 'MaskedArray'. Also set the variable long_name using CF convention (TBD) """ if obj is None : return None long_name=CFlongname(varname) if (format=='file') : oldvarname=varOfFile(obj) if (oldvarname != varname) : command="ncrename -v %s,%s %s >/dev/null 2>&1"%(oldvarname,varname,obj) if ( os.system(command) != 0 ) : clogger.error("Issue with changing varname to %s in %s"%(varname,obj)) return None clogger.debug("Varname changed to %s in %s"%(varname,obj)) command="ncatted -a long_name,%s,o,c,%s %s"%(varname,long_name,obj) if ( os.system(command) != 0 ) : clogger.error("Issue with changing long_name for var %s in %s"% (varname,obj)) return None return True elif (format=='MaskedArray') : clogger.warning('TBD - Cannot yet set the varname for MaskedArray') else : clogger.error('Cannot handle format %s'%format)
def cread(datafile,varname=None): import re if not datafile : return(None) if re.findall(".png$",datafile) : subprocess.Popen(["display",datafile,"&"]) elif re.findall(".nc$",datafile) : clogger.debug("reading NetCDF file %s"%datafile) if varname is None: varname=varOfFile(datafile) if varname is None: return(None) from Scientific.IO.NetCDF import NetCDFFile as ncf fileobj=ncf(datafile) #import netCDF4 #fileobj=netCDF4.Dataset(datafile) # Note taken from the CDOpy developper : .data is not backwards # compatible to old scipy versions, [:] is data=fileobj.variables[varname][:] fillv=fileobj.variables[varname]._FillValue import numpy.ma rep= numpy.ma.array(data,mask = data==fillv) fileobj.close() return(rep) else : clogger.error("cannot yet handle %s"%datafile) return None