def convert_units(var, target_units): ''' Converts units of var to target_units. var is a cdms.TransientVariable. ''' if not hasattr(var, 'units') and var.id == 'SST': var.units = target_units elif not hasattr(var, 'units') and var.id == 'ICEFRAC': var.units = target_units var = 100.0 * var elif var.units == 'fraction': var = 100.0 * var var.units = target_units elif var.units == 'mb': var.units = target_units elif var.units == 'gpm': # geopotential meter var = var / 9.8 / 100 # convert to hecto meter var.units = target_units elif var.units == 'Pa/s': var = var / 100.0 * 24 * 3600 var.units = target_units elif var.units == 'mb/day': var = var var.units = target_units else: temp = udunits(1.0, var.units) coeff, offset = temp.how(target_units) var = coeff * var + offset var.units = target_units return var
def level_units_to_Pa( filen ): f=cdms2.open(filen,'r+') ta=f['ta'] lev=ta.getLevel() tmp = udunits(1.0,'lev.units') s,i = tmp.how('Pa') lev[:] = s*lev[:] + i lev.units = 'Pa' f.close()
def verticalize( T, hyam, hybm, ps ): """ For data T with CAM's hybrid level coordinates, converts to pressure levels and may (depending on how the final if clause is set) interpolate to more standard (cdutil default) pressure level coordinates. This function returns a temperature variable with pressure levels. The input arguments hyam, hybm, ps are the usual CAM veriables by that name. Order of dimensions must be (lev,lat,lon). """ if hyam is None or hybm is None or ps is None: raise Exception("In verticalize, missing one of hyam,hybm,ps: %s,%s,%s"% ( getattr(hyam,'id',None), getattr(hybm,'id',None), getattr(ps,'id',None) )) p0 = 1000. # mb # Convert p0 to match ps. Later, we'll convert back to mb. This is faster than # converting ps to millibars. if ps.units=='mb': ps.units = 'mbar' # udunits uses mb for something else tmp = udunits(1.0,'mbar') s,i = tmp.how(ps.units) p0 = s*p0 + i axhyam = hyam.getDomain()[0][0] axhybm = hybm.getDomain()[0][0] if not hasattr(axhyam,'axis'): axhyam.axis = 'Z' if not hasattr(axhybm,'axis'): axhybm.axis = 'Z' levels_orig = cdutil.vertical.reconstructPressureFromHybrid( ps, hyam, hybm, p0 ) # At this point levels_orig has the same units as ps. Convert to to mbar tmp = udunits(1.0,ps.units) s,i = tmp.how('mbar') levels_orig = s*levels_orig + i levels_orig.units = 'mbar' newT = cdutil.vertical.logLinearInterpolation( T, levels_orig, levels_std ) return newT
def convert_units(var, target_units): """Converts units of var to target_units. var is a cdms.TransientVariable.""" if not hasattr(var, "units") and var.id == "SST": var.units = target_units elif not hasattr(var, "units") and var.id == "ICEFRAC": var.units = target_units var = 100.0 * var elif not hasattr(var, "units") and var.id == "AODVIS": var.units = target_units elif var.id == "AOD_550_ann": var.units = target_units elif var.units == "C" and target_units == "DegC": var.units = target_units elif var.units == "N/m2" and target_units == "N/m^2": var.units = target_units elif var.id == "AODVIS" or var.id == "AOD_550_ann": var.units = target_units elif var.units == "fraction": var = 100.0 * var var.units = target_units elif var.units == "mb": var.units = target_units elif var.units == "gpm": # geopotential meter var = var / 9.8 / 100 # convert to hecto meter var.units = target_units elif var.units == "Pa/s": var = var / 100.0 * 24 * 3600 var.units = target_units elif var.units == "mb/day": var = var var.units = target_units elif var.id == "prw" and var.units == "cm": var = var * 10.0 # convert from 'cm' to 'kg/m2' or 'mm' var.units = target_units else: temp = udunits(1.0, var.units) coeff, offset = temp.how(target_units) var = coeff * var + offset var.units = target_units return var
dm.shape, ' ', do.shape, ' ', ref) # # Basic checks # if dm.shape != do.shape: raise RuntimeError( "Obs and Model -%s- have different" % model_version + "shapes %s vs %s" % (do.shape, dm.shape)) # Ok possible issue with units if hasattr(dm, "units") and do.units != dm.units: u = genutil.udunits(1, dm.units) try: scaling, offset = u.how(do.units) dm = dm * scaling + offset wrn = "Model and observation units differed, converted model" +\ "(%s) to observation unit (%s) scaling: %g offset: %g" % ( dm.units, do.units, scaling, offset) warnings.warn(wrn) except: raise RuntimeError( "Could not convert model units (%s) " % dm.units + "to obs units: (%s)" % (do.units)) # # OBS INFO FOR JSON/ASCII FILES #
else: OUT.level = "-%i" % (int(level/100.)) dm = MODEL.get(var,varInFile=var,level=level) except Exception,err: success = False dup('Failed to get variable %s for version: %s, error:\n%s' % ( var, model_version, err)) break dup(var,' ', model_version,' ', dm.shape,' ', do.shape,' ', ref) ########################################################################### #### Basic checks ########################################################################### if dm.shape!=do.shape: raise RuntimeError, "Obs and Model -%s- have different shapes %s vs %s" % (model_version,do.shape,dm.shape) if do.units!=dm.units: # Ok possible issue with units u = genutil.udunits(1,dm.units) try: scaling,offset = u.how(do.units) dm = dm*scaling + offset warnings.warn("Model and observation units differed, converted model (%s) to observation unit (%s)" % (dm.units,do.units)) except: raise RuntimeError, "Could not convert model units (%s) to obs units: (%s)" % (dm.units,do.units) ########################################################################### #### OBS INFO FOR JSON/ASCII FILES ########################################################################### onm = obs_dic[var][ref] ########################################################################### #### METRICS CALCULATIONS