def calculate_reservoir_rate(var, surf_mask, surf_type=None): """Inputs are two maps (var and surf_mask). Whereas there may be scripts that calculate the masked means, this calculates the volume flow rate (ie, not the flux density, but the total flux. This is currently written to only apply to water variables. """ #If the units are not in mm/day, convert units target_units_prect = 'mm/day' if var.units == 'kg m-2 s-1' or var.units == 'kg/m2/s' or var.units == 'kg/m^2/s' or var.units == 'kg m^-2 s^-1' or var.units == 'kg/s/m^2': var.units = 'mm/s' a_prect = udunits(1., var.units) s_prect, i_prect = a_prect.how(target_units_prect) var = s_prect * var + i_prect var.units = target_units_prect #Mask the variable according to the surf_mask, using script in the same file var_masked = surface_maskvariable(var, surf_mask, surf_type=None) #Check to see if the variable has the time axis var_axislist = var_masked.getAxisList() if 'time' in var_axislist: scalar = genutil.averager( var_masked, axis='yxt') #Calculate the spatial+temporal mean over the mask area_frac = genutil.averager(area_frac, axis='t') area_frac = 1. - genutil.averager( surf_mask, axis='yx' ) #Calculate the fractional area over which the numbers are calculated ie, where it's NOT masked else: scalar = genutil.averager( var_masked, axis='yx') #Calculate the spatial mean over the mask area_frac = 1. - genutil.averager( surf_mask, axis='yx' ) #Calculate the fractional area over which the numbers are calculated ie, where it's NOT masked #Calculate the fraction of surface area that surf_mask covers #Here we hard-wire the coefficient necessary to move from mm/day * m^2 to km3/year coefficient = float(365. / 1000. / 1000000000.) #to convert mm/day to km^3/yr r_earth = metrics.packages.amwg.derivations.atmconst.AtmConst.Rad_earth #obtain radius of Earth A_earth = 4. * numpy.math.pi * r_earth**( 2) #Calulate the approximate surface area of Earth m^2 reservoir_total = A_earth * scalar * coefficient * area_frac #Calculate the volume rate #define the units of the reservoir total reservoir_total.units = 'km3/year' #reservoir_total.units=reservoir_total_units #apply units to the output return reservoir_total
def reduce_prs_tau(mv, vid=None): """Input is a transient variable. Dimensions isccp_prs, isccp_tau are reduced - but not by averaging as in most reduction functions. The unweighted sum is applied instead.""" if vid is None: vid = mv.id axes = mv.getAxisList() axis_names = [ a.id for a in axes if a.id == 'isccp_prs' or a.id == 'isccp_tau' or a.id == 'cosp_prs' or a.id == 'cosp_tau' or a.id == 'modis_prs' or a.id == 'modis_tau' or a.id == 'cosp_tau_modis' or a.id == 'cosp_htmisr' or a.id == 'misr_cth' or a.id == 'misr_tau' ] if len(axis_names) <= 0: return mv else: axes_string = '(' + ')('.join(axis_names) + ')' for axis in mv.getAxisList(): if axis.getBounds() is None: axis._bounds_ = axis.genGenericBounds() avmv = averager(mv, axis=axes_string, action='sum', weights=['unweighted', 'unweighted']) avmv.id = vid if hasattr(mv, 'units'): avmv.units = mv.units return avmv
def test_MeanValue(self): filename = 'tas_Amon_IPSL-CM5A-LR_1pctCO2_r1i1p1_185001-198912.nc' if not os.path.exists(filename): r = requests.get( "https://cdat.llnl.gov/cdat/sample_data/notebooks/{}".format(filename), stream=True) with open(filename, "wb") as f: for chunk in r.iter_content(chunk_size=1024): if chunk: # filter local_filename keep-alive new chunks f.write(chunk) # Open data file and extract variable f = cdms2.open(filename) data = f("tas") # Mask the data datamskd = MV2.masked_greater(data, data.max()-7) # extract the departures of the masked data. datamskd_departures = cdutil.times.ANNUALCYCLE.departures(datamskd) # create time series of the masked data departures. datamskd_departures_ts = genutil.averager(datamskd_departures, axis='xy', weights=[ 'weighted', 'weighted'], combinewts=1) datamskd_departures_ts_corrected = cdutil.times.ANNUALCYCLE.departures( datamskd_departures_ts) # Graphics and plot steps template = vcs.createtemplate() self.x = vcs.init(bg=True, geometry=(1200, 900)) # Plot image and check against reference self.x.clear() self.x.plot(datamskd_departures_ts_corrected, template) self.checkImage("test_vcs_plot_mean_value.png")
def calculate_reservoir_rate(var, surf_mask, surf_type=None): """Inputs are two maps (var and surf_mask). Whereas there may be scripts that calculate the masked means, this calculates the volume flow rate (ie, not the flux density, but the total flux. This is currently written to only apply to water variables. """ #If the units are not in mm/day, convert units target_units_prect='mm/day' if var.units=='kg m-2 s-1' or var.units=='kg/m2/s' or var.units=='kg/m^2/s' or var.units=='kg m^-2 s^-1' or var.units=='kg/s/m^2': var.units='mm/s' a_prect=udunits(1.,var.units) s_prect,i_prect=a_prect.how(target_units_prect) var=s_prect*var + i_prect var.units=target_units_prect #Mask the variable according to the surf_mask, using script in the same file var_masked=surface_maskvariable(var, surf_mask, surf_type=None) #Check to see if the variable has the time axis var_axislist=var_masked.getAxisList() if 'time' in var_axislist: scalar=genutil.averager(var_masked,axis='yxt') #Calculate the spatial+temporal mean over the mask area_frac=genutil.averager(area_frac,axis='t') area_frac=1.-genutil.averager(surf_mask,axis='yx') #Calculate the fractional area over which the numbers are calculated ie, where it's NOT masked else: scalar=genutil.averager(var_masked,axis='yx') #Calculate the spatial mean over the mask area_frac=1.-genutil.averager(surf_mask,axis='yx') #Calculate the fractional area over which the numbers are calculated ie, where it's NOT masked #Calculate the fraction of surface area that surf_mask covers #Here we hard-wire the coefficient necessary to move from mm/day * m^2 to km3/year coefficient=float(365./1000./1000000000.) #to convert mm/day to km^3/yr r_earth=metrics.packages.amwg.derivations.atmconst.AtmConst.Rad_earth #obtain radius of Earth A_earth=4. * numpy.math.pi * r_earth ** (2) #Calulate the approximate surface area of Earth m^2 reservoir_total=A_earth * scalar * coefficient * area_frac #Calculate the volume rate #define the units of the reservoir total reservoir_total.units='km3/year' #reservoir_total.units=reservoir_total_units #apply units to the output return reservoir_total
def reduce_prs_tau( mv, vid=None ): """Input is a transient variable. Dimensions isccp_prs, isccp_tau are reduced - but not by averaging as in most reduction functions. The unweighted sum is applied instead.""" if vid is None: vid = mv.id axes = mv.getAxisList() axis_names = [ a.id for a in axes if a.id=='isccp_prs' or a.id=='isccp_tau' or a.id=='cosp_prs' or a.id=='cosp_tau' or a.id=='modis_prs' or a.id=='modis_tau' or a.id=='cosp_tau_modis' or a.id=='cosp_htmisr' or a.id=='misr_cth' or a.id=='misr_tau'] if len(axis_names)<=0: return mv else: axes_string = '('+')('.join(axis_names)+')' for axis in mv.getAxisList(): if axis.getBounds() is None: axis._bounds_ = axis.genGenericBounds() avmv = averager( mv, axis=axes_string, action='sum', weights=['unweighted','unweighted'] ) avmv.id = vid if hasattr(mv,'units'): avmv.units = mv.units return avmv