def plan_computation( self, model, obs, varid, seasonid, region=None, aux=slice(0,None), names={}, plotparms=None ): """Set up for a lat-lon polar contour plot. Data is averaged over all other axes. """ filetable1, filetable2 = self.getfts(model, obs) ft1src = filetable1.source() if region is not None: regname = str(region) else: regname = None try: ft2src = filetable2.source() except: ft2src = '' reduced_varlis = [ reduced_variable( variableid=varid, filetable=filetable1, season=self.season, region=regname, reduction_function=(lambda x,vid, region=regname,aux1=aux: reduce2latlon_seasonal( x(latitude=aux1, longitude=(0, 360)), self.season, region, vid=vid ) ) ), reduced_variable( variableid=varid, filetable=filetable2, season=self.season, region=regname, reduction_function=(lambda x,vid, region=regname,aux1=aux: reduce2latlon_seasonal( x(latitude=aux1, longitude=(0, 360)), self.season, region, vid=vid ) ) ) ] self.reduced_variables = { v.id():v for v in reduced_varlis } vid1 = rv.dict_id( varid, seasonid, filetable1, region=regname ) vid2 = rv.dict_id( varid, seasonid, filetable2, region=regname ) self.derived_variables = {} self.single_plotspecs = { self.plot1_id: plotspec( vid = ps.dict_idid(vid1), zvars = [vid1], zfunc = (lambda z: z), plottype = self.plottype, source = names.get('model',ft1src), file_descr = 'model', plotparms = plotparms[src2modobs(ft1src)] ), self.plot2_id: plotspec( vid = ps.dict_idid(vid2), zvars = [vid2], zfunc = (lambda z: z), plottype = self.plottype, source = names.get('obs',ft2src), file_descr = 'obs', plotparms = plotparms[src2obsmod(ft2src)] ), self.plot3_id: plotspec( vid = ps.dict_id(varid,'diff',seasonid,filetable1,filetable2), zvars = [vid1,vid2], zfunc = aminusb_2ax, plottype = self.plottype, source = ', '.join([names.get('model',ft1src),names.get('obs',ft2src)]), file_descr = 'diff', plotparms = plotparms['diff'] ) } self.composite_plotspecs = { self.plotall_id: [ self.plot1_id, self.plot2_id, self.plot3_id] } self.computation_planned = True
def plan_computation( self, filetable1, filetable2, varid, seasonid, aux=None, vlist=None ): if vlist != None: default_list = filetable1._varindex.keys() derived_list = ['PREC', 'P-E', 'LHEAT', 'TOTRUNOFF', 'EVAPFRAC'] vars = default_list + derived_list vars.sort() self.varlist = vars print self.varlist print 'DOEN with preplan' else: print 'plan compute called' self.reduced_variables = {} self.reduced_variables[varid+'_1'] = reduced_variable(variableid = varid, filetable=filetable1, reduced_var_id=varid+'_1', reduction_function=(lambda x, vid: reduce2latlon_seasonal(x, self.season, vid))) if(filetable2 != None): self.reduced_variables[varid+'_2'] = reduced_variable(variableid = varid, filetable=filetable2, reduced_var_id=varid+'_2', reduction_function=(lambda x, vid: reduce2latlon_seasonal(x, self.season, vid))) self.derived_variables = {} self.derived_variables['PREC_1'] = derived_var(vid='PREC_1', inputs=['RAIN_1', 'SNOW_1'], func=aplusb) if(filetable2 != None): self.derived_variables['PREC_2'] = derived_var(vid='PREC_2', inputs=['RAIN_2', 'SNOW_2'], func=aplusb) self.single_plotspecs = {} self.composite_plotspecs = {} self.single_plotspecs[self.plot1_id] = plotspec( vid = varid+'_1', zvars = [varid+'_1'], zfunc = (lambda z: z), plottype = self.plottype) self.composite_plotspecs[self.plotall_id] = [self.plot1_id] if(filetable2 != None): self.single_plotspecs[self.plot2_id] = plotspec( vid = varid+'_2', zvars = [varid+'_2'], zfunc = (lambda z: z), plottype = self.plottype) self.single_plotspecs[self.plot3_id] = plotspec( vid = varid+' diff', zvars = [varid+'_1', varid+'_2'], zfunc = aminusb_2ax, plottype = self.plottype) self.composite_plotspecs[self.plotall_id].append(self.plot2_id) self.composite_plotspecs[self.plotall_id].append(self.plot3_id) self.computation_planned = True
def plan_computation( self, model, obs, varnom, seasonid, plotparms ): filetable1, filetable2 = self.getfts(model, obs) ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' self.reduced_variables = {} vidAll = {} for FT in [filetable1, filetable2]: VIDs = [] for i in range(1, 13): month = cdutil.times.getMonthString(i) #pdb.set_trace() #create identifiers VID = rv.dict_id(varnom, month, FT) #cdutil.times.getMonthIndex(VID[2])[0]-1 RF = (lambda x, vid=id2str(VID), month = VID[2]:reduce2scalar_seasonal_zonal(x, seasons=cdutil.times.Seasons(month), vid=vid)) RV = reduced_variable(variableid = varnom, filetable = FT, season = cdutil.times.Seasons(month), reduction_function = RF) VID = id2str(VID) self.reduced_variables[VID] = RV VIDs += [VID] vidAll[FT] = VIDs #create the identifiers vidModel = dv.dict_id(varnom, 'model', "", filetable1) vidObs = dv.dict_id(varnom, 'obs', "", filetable2) self.vidModel = id2str(vidModel) self.vidObs = id2str(vidObs) #create the derived variables which is the composite of the months #pdb.set_trace() model = derived_var(vid=self.vidModel, inputs=vidAll[filetable1], func=join_1d_data) obs = derived_var(vid=self.vidObs, inputs=vidAll[filetable2], func=join_1d_data) self.derived_variables = {self.vidModel: model, self.vidObs: obs} #create the plot spec self.single_plotspecs = {} self.single_plotspecs[self.plot_id] = plotspec(self.plot_id, zvars = [self.vidModel], zfunc = (lambda y: y), z2vars = [self.vidObs ], z2func = (lambda z: z), plottype = self.plottype, source = ', '.join([ft1src,ft2src]), file_descr = '', plotparms=plotparms[src2modobs(ft1src)] ) self.computation_planned = True
def plan_computation(self, filetable1, filetable2, varid, seasonid, aux=None, vlist=None): if vlist != None: print 'filling in vlist hopefully' # we should at least have a ft1 in this case. default_list = filetable1._varindex.keys() # Where should we define these? They need to be insync with the actual plan_compute list derived_list = ['PREC', 'E-T', 'LHEAT', 'SHEAT', 'EVAPFRAC'] vars = default_list + derived_list vars.sort() self.varlist = vars print self.varlist print 'DONE with pre-plan' else: print 'PLAN COMPUTATION CALLED args:', filetable1, filetable2, varid, seasonid self.reduced_variables = { varid+'_1':reduced_variable( variableid = varid, filetable=filetable1, reduced_var_id=varid+'_1', reduction_function=(lambda x, vid: reduceAnnTrend(x, vid))), varid+'_2':reduced_variable( variableid = varid, filetable=filetable2, reduced_var_id=varid+'_2', reduction_function=(lambda x, vid: reduceAnnTrend(x, vid))) } self.derived_variables = { 'PREC_1': derived_var(vid='PREC_1', inputs=['RAIN_1', 'SNOW_1'], func=aplusb), 'PREC_2': derived_var(vid='PREC_2', inputs=['RAIN_2', 'SNOW_2'], func=aplusb) } self.single_plotspecs = { self.plot1_id: plotspec( vid=varid+'_1', zvars = [varid+'_1'], zfunc=(lambda z: z), plottype = self.plottype) } #, # self.plot2_id: plotspec( # vid=varid+'_2', # zvars = [varid+'_2'], zfunc=(lambda z: z), # plottype = self.plottype) } # self.plot3_id: plotspec( # vid=varid+'_1', # zvars = [varid+'_1', varid+'_2'], zfunc=aminusb, # plottype = self.plottype) } # } self.composite_plotspecs = { # self.plotall_id: [self.plot1_id, self.plot2_id, self.plot3_id] self.plotall_id: [self.plot1_id] } self.computation_planned = True
def plan_computation( self, model, obs, varnom, seasonid, region, plotparms ): filetable1, filetable2 = self.getfts(model, obs) ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' region = interpret_region( region ) if varnom in filetable1.list_variables_incl_axes(): vid1 = self.var_from_data( filetable1, varnom, seasonid, region ) elif varnom in self.common_derived_variables.keys(): vid1 = self.var_from_cdv( filetable1, varnom, seasonid, region ) else: logger.error( "variable %s cannot be read or computed from data in the filetable %s", varnom, filetable1 ) return None if filetable2 is None: vid2 = None elif varnom in filetable2.list_variables_incl_axes(): vid2 = self.var_from_data( filetable2, varnom, seasonid, region ) elif varnom in self.common_derived_variables.keys(): vid2 = self.var_from_cdv( filetable2, varnom, seasonid, region ) else: vid2 = None ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' #vid1 = rv.dict_id( varnom,seasonid, filetable1, region=region) #vid2 = rv.dict_id( varnom,seasonid, filetable2, region=region) self.single_plotspecs = { self.plot1_id: plotspec( vid = ps.dict_idid(vid1), zvars=[vid1],\ zfunc=(lambda z: standardize_and_check_cloud_variable(z)), plottype = self.plottype, title = ' '.join([varnom,seasonid,str(region),'(1)']), source = ft1src, file_descr = 'model', plotparms = plotparms[src2modobs(ft1src)] ), self.plot2_id: plotspec( vid = ps.dict_idid(vid2), zvars=[vid2],\ zfunc=(lambda z: standardize_and_check_cloud_variable(z)), plottype = self.plottype, title = ' '.join([varnom,seasonid,str(region),'(2)']), source = ft2src, file_descr = 'obs', plotparms = plotparms[src2obsmod(ft2src)] ), self.plot3_id: plotspec( vid = ps.dict_id(varnom,'diff',seasonid,filetable1,filetable2,region=region), zvars=[vid1,vid2], zfunc=aminusb_2ax, plottype = self.plottype, title = ' '.join([varnom,seasonid,str(region),'(1)-(2)']), source = ', '.join([ft1src,ft2src]), file_descr = 'diff', plotparms = plotparms['diff'] ) } self.composite_plotspecs = { self.plotall_id: [self.plot1_id, self.plot2_id, self.plot3_id ] } self.computation_planned = True
def plan_computation_level_surface( self, model, obs, varnom, seasonid, aux=None, names={}, plotparms=None ): filetable1, filetable2 = self.getfts(model, obs) """Set up for a lat-lon contour plot, averaged in other directions - except that if the variable to be plotted depend on level, it is not averaged over level. Instead, the value at a single specified pressure level, aux, is used. The units of aux are millbars.""" # In calling reduce_time_seasonal, I am assuming that no variable has axes other than # (time, lev,lat,lon). # If there were another axis, then we'd need a new function which reduces it as well. if not isinstance(aux,Number): return None pselect = udunits(aux,'mbar') pselect.value=int(pselect.value) PSELECT = str(pselect) reduced_varlis = [ reduced_variable( # var=var(time,lev,lat,lon) variableid=varnom, filetable=filetable1, season=self.season, reduction_function=(lambda x,vid: reduce_time_seasonal( x, self.season, self.region, vid ) ) ), reduced_variable( # hyam=hyam(lev) variableid='hyam', filetable=filetable1, season=self.season, reduction_function=(lambda x,vid=None: select_region( x, self.region)) ), reduced_variable( # hybm=hybm(lev) variableid='hybm', filetable=filetable1, season=self.season, reduction_function=(lambda x,vid=None: select_region( x, self.region)) ), reduced_variable( # ps=ps(time,lat,lon) variableid='PS', filetable=filetable1, season=self.season, reduction_function=(lambda x,vid: reduce_time_seasonal( x, self.season, self.region, vid ) ) ) ] # vid1 = varnom+'_p_1' # vidl1 = varnom+'_lp_1' vid1 = dv.dict_id( varnom, 'p', seasonid, filetable1) vidl1 = dv.dict_id(varnom, 'lp', seasonid, filetable1) self.derived_variables = { vid1: derived_var( vid=vid1, inputs = [rv.dict_id(varnom,seasonid,filetable1), rv.dict_id('hyam',seasonid,filetable1), rv.dict_id('hybm',seasonid,filetable1), rv.dict_id('PS',seasonid,filetable1) ], #was vid1: derived_var( vid=vid1, inputs=[ varnom+'_1', 'hyam_1', 'hybm_1', 'PS_1' ], func=verticalize ), vidl1: derived_var( vid=vidl1, inputs=[vid1], func=(lambda z,psl=pselect: select_lev(z,psl))) } ft1src = filetable1.source() #note: the title in the following is parced in customizeTemplate. This is a garbage kludge! Mea culpa. varstr = varnom + '(' + PSELECT + ')' self.single_plotspecs = { self.plot1_id: plotspec( # was vid = varnom+'_1', # was zvars = [vid1], zfunc = (lambda z: select_lev( z, pselect ) ), vid = ps.dict_idid(vidl1), zvars = [vidl1], zfunc = (lambda z: z), plottype = self.plottype, title = ' '.join([varstr, seasonid, 'model', names['model']]), title1 = ' '.join([varstr, seasonid, 'model', names['model']]), title2 = 'model', file_descr = 'model', source = names['model'], plotparms = plotparms[src2modobs(ft1src)] ) } if filetable2 is None: self.reduced_variables = { v.id():v for v in reduced_varlis } self.composite_plotspecs = { self.plotall_id: [ self.plot1_id ] } self.computation_planned = True return if 'hyam' in filetable2.list_variables() and 'hybm' in filetable2.list_variables(): # hybrid levels in use, convert to pressure levels reduced_varlis += [ reduced_variable( # var=var(time,lev,lat,lon) variableid=varnom, filetable=filetable2, season=self.season, reduction_function=(lambda x,vid: reduce_time_seasonal( x, self.season, self.region, vid ) ) ), reduced_variable( # hyam=hyam(lev) variableid='hyam', filetable=filetable2, season=self.season, reduction_function=(lambda x,vid=None: select_region( x, self.region)) ), reduced_variable( # hybm=hybm(lev) variableid='hybm', filetable=filetable2, season=self.season, reduction_function=(lambda x,vid=None: select_region( x, self.region)) ), reduced_variable( # ps=ps(time,lat,lon) variableid='PS', filetable=filetable2, season=self.season, reduction_function=(lambda x,vid: reduce_time_seasonal( x, self.season, self.region, vid ) ) ) ] #vid2 = varnom+'_p_2' #vidl2 = varnom+'_lp_2' vid2 = dv.dict_id( varnom, 'p', seasonid, filetable2 ) vid2 = dv.dict_id( vards, 'lp', seasonid, filetable2 ) self.derived_variables[vid2] = derived_var( vid=vid2, inputs=[ rv.dict_id(varnom,seasonid,filetable2), rv.dict_id('hyam',seasonid,filetable2), rv.dict_id('hybm',seasonid,filetable2), rv.dict_id('PS',seasonid,filetable2) ], func=verticalize ) self.derived_variables[vidl2] = derived_var( vid=vidl2, inputs=[vid2], func=(lambda z,psl=pselect: select_lev(z,psl) ) ) else: # no hybrid levels, assume pressure levels. #vid2 = varnom+'_2' #vidl2 = varnom+'_lp_2' vid2 = rv.dict_id(varnom,seasonid,filetable2) vidl2 = dv.dict_id( varnom, 'lp', seasonid, filetable2 ) reduced_varlis += [ reduced_variable( # var=var(time,lev,lat,lon) variableid=varnom, filetable=filetable2, season=self.season, reduction_function=(lambda x,vid: reduce_time_seasonal( x, self.season, self.region, vid ) ) ) ] self.derived_variables[vidl2] = derived_var( vid=vidl2, inputs=[vid2], func=(lambda z,psl=pselect: select_lev(z,psl) ) ) self.reduced_variables = { v.id():v for v in reduced_varlis } try: ft2src = filetable2.source() except: ft2src = '' filterid = ft2src.split('_')[-1] #recover the filter id: kludge self.single_plotspecs[self.plot2_id] = plotspec( #was vid = varnom+'_2', vid = ps.dict_idid(vidl2), zvars = [vidl2], zfunc = (lambda z: z), plottype = self.plottype, title = ' '.join([varstr,seasonid,'observation',names['obs']]), title1 = ' '.join([varstr, seasonid, 'observation',names['obs']]), title2 = 'observation', file_descr = 'obs', source = names['obs'], plotparms = plotparms[src2obsmod(ft2src)] ) self.single_plotspecs[self.plot3_id] = plotspec( #was vid = varnom+'_diff', vid = ps.dict_id(varnom,'diff',seasonid,filetable1,filetable2), zvars = [vidl1,vidl2], zfunc = aminusb_2ax, plottype = self.plottype, title = ' '.join([varstr,seasonid,'difference']), title1 = ' '.join([varstr, seasonid, 'difference']), title2 = 'difference', file_descr = 'diff', plotparms = plotparms['diff'] ) # zerocontour=-1 ) self.composite_plotspecs = { self.plotall_id: [ self.plot1_id, self.plot2_id, self.plot3_id ] } self.computation_planned = True
def plan_computation_normal_contours( self, model, obs, varnom, seasonid, aux=None, names={}, plotparms=None ): filetable1, filetable2 = self.getfts(model, obs) """Set up for a lat-lon contour plot, as in plot set 5. Data is averaged over all other axes.""" if varnom in filetable1.list_variables(): vid1,vid1var = self.vars_normal_contours( filetable1, varnom, seasonid, aux=None ) elif varnom in self.common_derived_variables.keys(): vid1,vid1var = self.vars_commdervar_normal_contours( filetable1, varnom, seasonid, aux=None ) else: logger.error("variable %s not found in and cannot be computed from %s",varnom, filetable1) return None if filetable2 is not None and varnom in filetable2.list_variables(): vid2,vid2var = self.vars_normal_contours( filetable2, varnom, seasonid, aux=None ) elif varnom in self.common_derived_variables.keys(): vid2,vid2var = self.vars_commdervar_normal_contours( filetable2, varnom, seasonid, aux=None ) else: vid2,vid2var = None,None self.single_plotspecs = {} ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' all_plotnames = [] if filetable1 is not None: if vid1 is not None: self.single_plotspecs[self.plot1_id] = plotspec( vid = ps.dict_idid(vid1), zvars = [vid1], zfunc = (lambda z: z), plottype = self.plottype, title = ' '.join([varnom, seasonid, 'model']), title1 = ' '.join([varnom, seasonid, 'model']), title2 = 'model', file_descr = 'model', source = names['model'], plotparms = plotparms[src2modobs(ft1src)]) all_plotnames.append(self.plot1_id) if vid1var is not None: self.single_plotspecs[self.plot1var_id] = plotspec( vid = ps.dict_idid(vid1var), zvars = [vid1var], zfunc = (lambda z: z), plottype = self.plottype, title = ' '.join([varnom, seasonid, 'model variance']), title1 = ' '.join([varnom, seasonid, 'model variance']), title2 = 'model variance', file_descr = 'model variance', source = names['model'], plotparms = plotparms[src2modobs(ft1src)] ) all_plotnames.append(self.plot1var_id) if filetable2 is not None and vid2 is not None: self.single_plotspecs[self.plot2_id] = plotspec( vid = ps.dict_idid(vid2), zvars = [vid2], zfunc = (lambda z: z), plottype = self.plottype, title = ' '.join([varnom, seasonid, 'obs']), title1 = ' '.join([varnom, seasonid, 'obs']), title2 = "observation", file_descr = "obs", source = names['obs'], plotparms = plotparms[src2obsmod(ft2src)] ) all_plotnames.append(self.plot2_id) if filetable1 is not None and filetable2 is not None and vid1 is not None and vid2 is not None: self.single_plotspecs[self.plot3_id] = plotspec( vid = ps.dict_id(varnom,'diff',seasonid,filetable1,filetable2), zvars = [vid1,vid2], zfunc = aminusb_2ax, plottype = self.plottype, title = ' '.join([varnom, seasonid, 'difference']), title1 = ' '.join([varnom, seasonid, 'difference']), title2 = 'difference', file_descr = 'diff', plotparms = plotparms['diff'] ) all_plotnames.append(self.plot3_id) if len(all_plotnames)>0: self.composite_plotspecs = { self.plotall_id: all_plotnames } else: self.composite_plotspecs = {} self.computation_planned = True
def plan_computation( self, model, obs, varid, seasonid, plotparms ): filetable1, filetable2 = self.getfts(model, obs) ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' self.computation_planned = False #setup the reduced variables self.reduced_variables = {} vidAll = {} for FT in self.filetables: #pdb.set_trace() VIDs = [] for i in range(1, 13): month = cdutil.times.getMonthString(i) #pdb.set_trace() #create identifiers VID = rv.dict_id(varid, month, FT) RF = (lambda x, vid=id2str(VID), month=VID[2]: reduce2lat_seasonal(x, seasons=cdutil.times.Seasons(month), region=self.region, vid=vid)) RV = reduced_variable(variableid = varid, filetable = FT, season = cdutil.times.Seasons(VID[2]), reduction_function = RF) self.reduced_variables[RV.id()] = RV VIDs += [VID] vidAll[FT] = VIDs vidModel = dv.dict_id(varid, 'ZonalMean model', self._seasonid, filetable1) if self.FT2: vidObs = dv.dict_id(varid, 'ZonalMean obs', self._seasonid, filetable2) vidDiff = dv.dict_id(varid, 'ZonalMean difference', self._seasonid, filetable1) else: vidObs = None vidDiff = None self.derived_variables = {} #create the derived variables which is the composite of the months self.derived_variables[vidModel] = derived_var(vid=id2str(vidModel), inputs=vidAll[filetable1], func=join_data) if self.FT2: self.derived_variables[vidObs] = derived_var(vid=id2str(vidObs), inputs=vidAll[filetable2], func=join_data) #create the derived variable which is the difference of the composites self.derived_variables[vidDiff] = derived_var(vid=id2str(vidDiff), inputs=[vidModel, vidObs], func=aminusb_ax2) #create composite plots np.transpose zfunc = (lambda x: x), zfunc = (lambda z:z), self.single_plotspecs = { self.plot1_id: plotspec(vid = ps.dict_idid(vidModel), zvars = [vidModel], zfunc = (lambda x: MV2.transpose(x)), plottype = self.plottype, source = ft1src, title1 = ' '.join([vidModel.var, vidModel.season, vidModel.region]), title2 = ' '.join([vidModel.var, vidModel.season, vidModel.region]), file_descr = 'model', plotparms = plotparms['model'] )} if self.FT2: self.single_plotspecs[self.plot2_id] = \ plotspec(vid = ps.dict_idid(vidObs), zvars=[vidObs], zfunc = (lambda x: MV2.transpose(x)), plottype = self.plottype, source = ft2src, title1 = ' '.join([vidObs.var, vidObs.season, vidObs.region]), title2 = 'observations', file_descr = 'obs', plotparms = plotparms['obs'] ) self.single_plotspecs[self.plot3_id] = \ plotspec(vid = ps.dict_idid(vidDiff), zvars = [vidDiff], zfunc = (lambda x: MV2.transpose(x)), plottype = self.plottype, source = ', '.join([ft1src,ft2src]), title1 = ' '.join([vidDiff.var, vidDiff.season, vidDiff.region]), title2 = 'difference', file_descr = 'diff', plotparms = plotparms['diff'] ) self.composite_plotspecs = { self.plotall_id: [ self.plot1_id, self.plot2_id, self.plot3_id ] } #... was self.composite_plotspecs = { self.plotall_id: self.single_plotspecs.keys() } self.computation_planned = True
def plan_computation(self, model, obs, varid, seasonid, aux, plotparms): def join_data(*args): """ This function joins the results of several reduced variables into a single derived variable. It is used in plot set 14. """ import cdms2, cdutil, numpy alldata = [] allbias = [] IDs = [] i = 0 #pdb.set_trace() for arg in args: if i == 0: data = [arg.tolist()] IDs += [arg.id] i += 1 elif i == 1: data += [arg.tolist()] alldata += [data] i += 1 elif i == 2: allbias += [arg.tolist()] i = 0 data = MV2.array(alldata) #create attributes on the fly data.bias = allbias data.IDs = IDs #pdb.set_trace() return data from genutil.statistics import correlation #filetable1, filetable2 = self.getfts(model, obs) self.computation_planned = False #check if there is data to process ft1_valid = False ft2_valid = False #if filetable1 is not None and filetable2 is not None: # ft1 = filetable1.find_files(self.vars[0]) # ft2 = filetable2.find_files(self.vars[1]) # ft1_valid = ft1 is not None and ft1!=[] # true iff filetable1 uses hybrid level coordinates # ft2_valid = ft2 is not None and ft2!=[] # true iff filetable2 uses hybrid level coordinates #else: # print "ERROR: user must specify 2 data files" # return None #if not ft1_valid or not ft2_valid: # return None FTs = {'model': model, 'obs': obs} self.reduced_variables = {} RVs = {} for dt in self.datatype: for ft in FTs[dt]: for var in self.vars: #rv for the data VID_data = rv.dict_id(var, 'data', ft) VID_data = id2str(VID_data) RV = reduced_variable( variableid=var, filetable=ft, season=cdutil.times.Seasons(seasonid), reduction_function=(lambda x, vid=VID_data: x)) self.reduced_variables[VID_data] = RV #create identifier, unused fro now FN = RV.get_variable_file(var) L = FN.split('/') DATAID = var + '_' + L[-1:][0] #rv for the mean VID_mean = rv.dict_id(var, 'mean', ft) VID_mean = id2str(VID_mean) RV = reduced_variable( variableid=var, filetable=ft, season=cdutil.times.Seasons(seasonid), reduction_function=(lambda x, vid=VID_mean, ID=DATAID: MV2.array(reduce2scalar(x)))) #numpy mean() isn't working here reduction_function=( lambda x, vid=VID_mean, ID=DATAID: MV2.array(x.mean()) ) ) self.reduced_variables[VID_mean] = RV #rv for its std VID_std = rv.dict_id(var, 'std', ft) VID_std = id2str(VID_std) RV = reduced_variable( variableid=var, filetable=ft, season=cdutil.times.Seasons(seasonid), reduction_function=(lambda x, vid=VID_std, ID=DATAID: MV2.array(x.std()))) self.reduced_variables[VID_std] = RV #pdb.set_trace() RVs[(dt, ft, var)] = (VID_data, VID_mean, VID_std) self.derived_variables = {} #compute bias of the means bias = [] BVs = {} for var in self.vars: for ft in model: Vobs = RVs['obs', obs[0], var][1] Vmodel = RVs['model', ft, var][1] BV = rv.dict_id(var, 'bias', ft) BV = id2str(BV) bias += [BV] BVs[var, ft, 'bias'] = BV #FUNC = ( lambda x,y, vid=BV: x-y ) self.derived_variables[BV] = derived_var(vid=BV, inputs=[Vmodel, Vobs], func=adivb) #generate derived variables for correlation between each model data and obs data nvars = len(self.vars) CVs = {} for var in self.vars: for ft in model: Vobs = RVs['obs', obs[0], var][0] #this assumes only one obs data Vmodel = RVs['model', ft, var][0] CV = rv.dict_id(var, 'model_correlation', ft) CV = id2str(CV) CVs[var, ft, 'correlation'] = CV FUNC = (lambda x, y, vid=CV, aux=aux: correlateData(x, y, aux)) self.derived_variables[CV] = derived_var(vid=CV, inputs=[Vobs, Vmodel], func=FUNC) #generate the normalized stds NVs = {} for ft in model: for var in self.vars: Vobs = RVs['obs', obs[0], var][2] Vmodel = RVs['model', ft, var][2] NV = rv.dict_id(var, '_normalized_std', ft) NV = id2str(NV) NVs[var, ft, 'normalized_std'] = NV self.derived_variables[NV] = derived_var(vid=NV, inputs=[Vmodel, Vobs], func=adivb) #get the pairs of std,correlation and bias for each variable and datatype triples = [] for ft in model: for var in self.vars: triple = (NVs[var, ft, 'normalized_std'], CVs[var, ft, 'correlation'], BVs[var, ft, 'bias']) triples += triple #correlation coefficient self.derived_variables['TaylorData'] = derived_var(vid='TaylorData', inputs=triples, func=join_data) #self.derived_variables['TaylorBias'] = derived_var(vid='TaylorBias', inputs=bias, func=join_scalar_data) self.single_plotspecs = {} title = "" #"Taylor diagram" self.single_plotspecs['Taylor'] = plotspec( vid='Taylor', zvars=['TaylorData'], zfunc=(lambda x: x), plottype=self.plottype, title='', plotparms=plotparms['model']) self.computation_planned = True
def plan_computation_normal_contours(self, model, obs, varid, seasonid, aux, plotparms): """Set up for a lat-lon contour plot, as in plot set 5. Data is averaged over all other axes.""" filetable1, filetable2 = self.getfts(model, obs) self.derived_variables = {} vars_vec1 = {} vars_vec2 = {} try: if varid == 'STRESS' or varid == 'SURF_STRESS' or varid == 'MOISTURE_TRANSPORT': vars1,rvars1,dvars1,var_cont1,vars_vec1,vid_cont1 =\ self.STRESS_setup( filetable1, varid, seasonid ) vars2,rvars2,dvars2,var_cont2,vars_vec2,vid_cont2 =\ self.STRESS_setup( filetable2, varid, seasonid ) if vars1 is None and vars2 is None: raise DiagError("cannot find set6 variables in data 2") else: logger.error("AMWG plot set 6 does not yet support %s", varid) return None except Exception as e: logger.error( "cannot find suitable set6_variables in data for varid= %s", varid) logger.exception(" %s ", e) return None reduced_varlis = [] vardict1 = {'': 'nameless_variable'} vardict2 = {'': 'nameless_variable'} new_reducedvars, needed_derivedvars = self.STRESS_rvs( filetable1, rvars1, seasonid, vardict1) reduced_varlis += new_reducedvars self.reduced_variables = {v.id(): v for v in reduced_varlis} self.STRESS_dvs(filetable1, needed_derivedvars, seasonid, vardict1, vid_cont1, vars_vec1) new_reducedvars, needed_derivedvars = self.STRESS_rvs( filetable2, rvars2, seasonid, vardict2) reduced_varlis += new_reducedvars self.STRESS_dvs(filetable2, needed_derivedvars, seasonid, vardict2, vid_cont2, vars_vec2) self.reduced_variables = {v.id(): v for v in reduced_varlis} self.single_plotspecs = {} ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' vid_vec1 = vardict1[','.join([vars_vec1[0], vars_vec1[1]])] vid_vec11 = vardict1[vars_vec1[0]] vid_vec12 = vardict1[vars_vec1[1]] vid_vec2 = vardict2[','.join([vars_vec2[0], vars_vec2[1]])] vid_vec21 = vardict2[vars_vec2[0]] vid_vec22 = vardict2[vars_vec2[1]] plot_type_temp = [ 'Isofill', 'Vector' ] # can't use self.plottype yet because don't support it elsewhere as a list or tuple <<<<< if vars1 is not None: # Draw two plots, contour and vector, over one another to get a single plot. # Only one needs to plot title,source; but both need source to get the filenames right. title = ' '.join([varid, seasonid, '(1)']) contplot = plotspec(vid=ps.dict_idid(vid_cont1), zvars=[vid_cont1], zfunc=(lambda z: z), plottype=plot_type_temp[0], title=title, source=ft1src, plotparms=plotparms[src2modobs(ft1src)]) vecplot = plotspec(vid=ps.dict_idid(vid_vec1), zvars=[vid_vec11, vid_vec12], zfunc=(lambda z, w: (z, w)), plottype=plot_type_temp[1], title1='', title2='', source=ft1src, plotparms=plotparms[src2modobs(ft1src)]) #self.single_plotspecs[self.plot1_id] = [contplot,vecplot] self.single_plotspecs[self.plot1_id + 'c'] = contplot self.single_plotspecs[self.plot1_id + 'v'] = vecplot if vars2 is not None: # Draw two plots, contour and vector, over one another to get a single plot. # Only one needs title,source. title = ' '.join([varid, seasonid, '(2)']) contplot = plotspec(vid=ps.dict_idid(vid_cont2), zvars=[vid_cont2], zfunc=(lambda z: z), plottype=plot_type_temp[0], title=title, source=ft2src, plotparms=plotparms[src2obsmod(ft2src)]) vecplot = plotspec(vid=ps.dict_idid(vid_vec2), zvars=[vid_vec21, vid_vec22], zfunc=(lambda z, w: (z, w)), plottype=plot_type_temp[1], title1='', title2='', source=ft2src, plotparms=plotparms[src2obsmod(ft2src)]) self.single_plotspecs[self.plot2_id + 'c'] = contplot self.single_plotspecs[self.plot2_id + 'v'] = vecplot if vars1 is not None and vars2 is not None: # First, we need some more derived variables in order to do the contours as a magnitude # of the difference vector. diff1_vid = dv.dict_id(vid_vec11[1], 'DIFF1', seasonid, filetable1, filetable2) diff1 = derived_var(vid=diff1_vid, inputs=[vid_vec11, vid_vec21], func=aminusb_2ax) self.derived_variables[diff1_vid] = diff1 diff2_vid = dv.dict_id(vid_vec12[1], 'DIFF1', seasonid, filetable1, filetable2) diff2 = derived_var(vid=diff2_vid, inputs=[vid_vec12, vid_vec22], func=aminusb_2ax) self.derived_variables[diff2_vid] = diff2 title = ' '.join([varid, seasonid, 'diff,mag.diff']) #source = underscore_join([ft1src,ft2src]) source = '' contplot = plotspec( vid=ps.dict_id(var_cont1, 'mag.of.diff', seasonid, filetable1, filetable2), zvars=[diff1_vid, diff2_vid], zfunc=abnorm, # This is magnitude of difference of vectors plottype=plot_type_temp[0], title=title, source=source, plotparms=plotparms['diff']) #contplot = plotspec( # vid = ps.dict_id(var_cont1,'diff.of.mags',seasonid,filetable1,filetable2), # zvars = [vid_cont1,vid_cont2], zfunc = aminusb_2ax, # This is difference of magnitudes. # plottype = plot_type_temp[0], title=title, source=source ) # This could be done in terms of diff1,diff2, but I'll leave it alone for now... vecplot = plotspec( vid=ps.dict_id(vid_vec2, 'diff', seasonid, filetable1, filetable2), zvars=[vid_vec11, vid_vec12, vid_vec21, vid_vec22], zfunc=(lambda z1, w1, z2, w2: (aminusb_2ax(z1, z2), aminusb_2ax(w1, w2))), plottype=plot_type_temp[1], title1='', title2='', source=source, plotparms=plotparms['diff']) self.single_plotspecs[self.plot3_id + 'c'] = contplot self.single_plotspecs[self.plot3_id + 'v'] = vecplot # initially we're not plotting the contour part of the plots.... self.composite_plotspecs = { self.plot1_id: (self.plot1_id + 'c', self.plot1_id + 'v'), #self.plot1_id: [ self.plot1_id+'v' ], self.plot2_id: (self.plot2_id + 'c', self.plot2_id + 'v'), self.plot3_id: (self.plot3_id + 'c', self.plot3_id + 'v'), self.plotall_id: [self.plot1_id, self.plot2_id, self.plot3_id] } self.computation_planned = True
def plan_computation( self, model, obs, varid, seasonid, aux, plotparms ): def join_data(*args ): """ This function joins the results of several reduced variables into a single derived variable. It is used in plot set 14. """ import cdms2, cdutil, numpy alldata = [] allbias = [] IDs = [] i=0 #pdb.set_trace() for arg in args: if i == 0: data = [arg.tolist()] IDs += [arg.id] i += 1 elif i == 1: data += [arg.tolist()] alldata += [data] i += 1 elif i == 2: allbias += [arg.tolist()] i = 0 data = MV2.array(alldata) #create attributes on the fly data.bias = allbias data.IDs = IDs #pdb.set_trace() return data from genutil.statistics import correlation #filetable1, filetable2 = self.getfts(model, obs) self.computation_planned = False #check if there is data to process ft1_valid = False ft2_valid = False #if filetable1 is not None and filetable2 is not None: # ft1 = filetable1.find_files(self.vars[0]) # ft2 = filetable2.find_files(self.vars[1]) # ft1_valid = ft1 is not None and ft1!=[] # true iff filetable1 uses hybrid level coordinates # ft2_valid = ft2 is not None and ft2!=[] # true iff filetable2 uses hybrid level coordinates #else: # print "ERROR: user must specify 2 data files" # return None #if not ft1_valid or not ft2_valid: # return None FTs = {'model': model, 'obs': obs} self.reduced_variables = {} RVs = {} for dt in self.datatype: for ft in FTs[dt]: for var in self.vars: #rv for the data VID_data = rv.dict_id(var, 'data', ft) VID_data = id2str(VID_data) RV = reduced_variable( variableid=var, filetable=ft, season=cdutil.times.Seasons(seasonid), reduction_function=( lambda x, vid=VID_data:x ) ) self.reduced_variables[VID_data] = RV #create identifier, unused fro now FN = RV.get_variable_file(var) L = FN.split('/') DATAID = var + '_' + L[-1:][0] #rv for the mean VID_mean = rv.dict_id(var, 'mean', ft) VID_mean = id2str(VID_mean) RV = reduced_variable( variableid=var, filetable=ft, season=cdutil.times.Seasons(seasonid), reduction_function=( lambda x, vid=VID_mean, ID=DATAID: MV2.array(reduce2scalar(x)) ) ) #numpy mean() isn't working here reduction_function=( lambda x, vid=VID_mean, ID=DATAID: MV2.array(x.mean()) ) ) self.reduced_variables[VID_mean] = RV #rv for its std VID_std = rv.dict_id(var, 'std', ft) VID_std = id2str(VID_std) RV = reduced_variable( variableid=var, filetable=ft, season=cdutil.times.Seasons(seasonid), reduction_function=( lambda x, vid=VID_std, ID=DATAID: MV2.array(x.std()) ) ) self.reduced_variables[VID_std] = RV #pdb.set_trace() RVs[(dt, ft, var)] = (VID_data, VID_mean, VID_std) self.derived_variables = {} #compute bias of the means bias = [] BVs = {} for var in self.vars: for ft in model: Vobs = RVs['obs', obs[0], var][1] Vmodel = RVs['model', ft, var][1] BV = rv.dict_id(var, 'bias', ft) BV = id2str(BV) bias += [BV] BVs[var, ft, 'bias'] = BV #FUNC = ( lambda x,y, vid=BV: x-y ) self.derived_variables[BV] = derived_var(vid=BV, inputs=[Vmodel, Vobs], func=adivb) #generate derived variables for correlation between each model data and obs data nvars = len(self.vars) CVs = {} for var in self.vars: for ft in model: Vobs = RVs['obs', obs[0], var][0] #this assumes only one obs data Vmodel = RVs['model', ft, var][0] CV = rv.dict_id(var, 'model_correlation', ft) CV = id2str(CV) CVs[var, ft, 'correlation'] = CV FUNC = ( lambda x,y, vid=CV, aux=aux: correlateData(x, y, aux) ) self.derived_variables[CV] = derived_var(vid=CV, inputs=[Vobs, Vmodel], func=FUNC) #generate the normalized stds NVs = {} for ft in model: for var in self.vars: Vobs = RVs['obs', obs[0], var][2] Vmodel = RVs['model', ft, var][2] NV = rv.dict_id(var,'_normalized_std', ft) NV = id2str(NV) NVs[var, ft, 'normalized_std'] = NV self.derived_variables[NV] = derived_var(vid=NV, inputs=[Vmodel, Vobs], func=adivb) #get the pairs of std,correlation and bias for each variable and datatype triples = [] for ft in model: for var in self.vars: triple = (NVs[var, ft, 'normalized_std'], CVs[var, ft, 'correlation'], BVs[var, ft, 'bias']) triples += triple #correlation coefficient self.derived_variables['TaylorData'] = derived_var(vid='TaylorData', inputs=triples, func=join_data) #self.derived_variables['TaylorBias'] = derived_var(vid='TaylorBias', inputs=bias, func=join_scalar_data) self.single_plotspecs = {} title = "" #"Taylor diagram" self.single_plotspecs['Taylor'] = plotspec(vid = 'Taylor', zvars = ['TaylorData'], zfunc = (lambda x: x), plottype = self.plottype, title = '', plotparms=plotparms['model'] ) self.computation_planned = True
def plan_computation( self, model, obs, varid, station, plotparms ): filetable1, filetable2 = self.getfts(model, obs) self.computation_planned = False #check if there is data to process #ft1_valid = False #ft2_valid = False #if filetable1 is not None and filetable2 is not None: # ft1 = filetable1.find_files(varid) # ft2 = filetable2.find_files(varid) # ft1_valid = ft1 is not None and ft1!=[] # true iff filetable1 uses hybrid level coordinates # ft2_valid = ft2 is not None and ft2!=[] # true iff filetable2 uses hybrid level coordinates #else: # print "ERROR: user must specify 2 data files" # return None #if not ft1_valid or not ft2_valid: # return None self.reduced_variables = {} VIDs = {} #setup the model reduced variables for monthIndex, month in enumerate(self.months): VID = rv.dict_id(varid, month, filetable1) RF = (lambda x, monthIndex=monthIndex, lat=self.lat, lon=self.lon, vid=VID: getSection(x, monthIndex=monthIndex, lat=lat, lon=lon, vid=vid) ) RV = reduced_variable( variableid=varid, filetable=filetable1, season=cdutil.times.Seasons(month), reduction_function=RF ) VID = id2str(VID) self.reduced_variables[VID] = RV VIDs['model', month] = VID #setup the observational data as reduced variables for monthi, month in enumerate(self.months): sd = self.StationData.getData(varid, station, monthi) if not self.IDsandUnits: self.saveIds(sd) VIDobs = rv.dict_id(varid, month, filetable2) RF = (lambda x, stationdata=sd, vid=VID: stationdata ) RVobs = reduced_variable( variableid=varid, filetable=filetable2, season=cdutil.times.Seasons(month), reduction_function=RF ) VIDobs = id2str(VIDobs) self.reduced_variables[VIDobs] = RVobs VIDs['obs', month] = VIDobs #setup the graphical output self.single_plotspecs = {} title = 'P vs ' + varid for plot_id in self.plot_ids: month = plot_id VIDmodel = VIDs['model', month] VIDobs = VIDs['obs', month] self.plot_id_model = plot_id+'_model' self.single_plotspecs[plot_id+'_model'] = plotspec(vid = plot_id+'_model', zvars = [VIDmodel], zfunc = (lambda z: z), zrangevars={'xrange':[1000., 0.]}, zlinetype='solid', plottype = "Yxvsx", title = month) VIDobs= VIDs['obs', month] self.single_plotspecs[plot_id+'_obs'] = plotspec(vid = plot_id+'_obs', zvars = [VIDobs], zfunc = (lambda z: z), zrangevars={'xrange':[1000., 0.]}, zlinetype='dot', plottype="Yxvsx", title="") self.composite_plotspecs = {} plotall_id = [] for plot_id in self.plot_ids: self.composite_plotspecs[plot_id] = ( plot_id+'_model', plot_id+'_obs' ) plotall_id += [plot_id] self.composite_plotspecs[self.plotall_id] = plotall_id self.computation_planned = True
def plan_computation( self, model, obs, varid, seasonid, plotparms ): filetable1, filetable2 = self.getfts(model, obs) ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' self.computation_planned = False #check if there is data to process ft1_valid = False ft2_valid = False if filetable1 is not None and filetable2 is not None: ft1 = filetable1.find_files(varid) ft2 = filetable2.find_files(varid) ft1_valid = ft1 is not None and ft1!=[] # true iff filetable1 uses hybrid level coordinates ft2_valid = ft2 is not None and ft2!=[] # true iff filetable2 uses hybrid level coordinates else: logger.error("user must specify 2 data files") return None if not ft1_valid or not ft2_valid: return None #generate identifiers vid1 = rv.dict_id(varid, self._s1, filetable1) vid2 = rv.dict_id(varid, self._s2, filetable2) vid3 = dv.dict_id(varid, 'SeasonalDifference', self._seasonid, filetable1)#, ft2=filetable2) #setup the reduced variables vid1_season = cdutil.times.Seasons(self._s1) if vid1_season is None: vid1_season = seasonsyr vid2_season = cdutil.times.Seasons(self._s2) if vid2_season is None: vid2_season = seasonsyr rv_1 = reduced_variable(variableid=varid, filetable=filetable1, season=vid1_season, reduction_function=( lambda x, vid=vid1: reduce2latlon_seasonal(x, vid1_season, self.region, vid=vid)) ) rv_2 = reduced_variable(variableid=varid, filetable=filetable2, season=vid2_season, reduction_function=( lambda x, vid=vid2: reduce2latlon_seasonal(x, vid2_season, self.region, vid=vid)) ) self.reduced_variables = {rv_1.id(): rv_1, rv_2.id(): rv_2} #create the derived variable which is the difference self.derived_variables = {} self.derived_variables[vid3] = derived_var(vid=vid3, inputs=[vid1, vid2], func=aminusb_2ax) self.single_plotspecs = { self.plot1_id: plotspec( vid = ps.dict_idid(vid1), zvars=[vid1], zfunc = (lambda z: z), plottype = self.plottype, source = ft1src, file_descr = 'model', plotparms = plotparms[src2modobs(ft1src)] ), self.plot2_id: plotspec( vid = ps.dict_idid(vid2), zvars=[vid2], zfunc = (lambda z: z), plottype = self.plottype, source = ft2src, file_descr = 'obs', plotparms = plotparms[src2obsmod(ft2src)] ), self.plot3_id: plotspec( vid = ps.dict_idid(vid3), zvars = [vid3], zfunc = (lambda x: x), plottype = self.plottype, source = ', '.join([ft1src,ft2src]), file_descr = 'diff', plotparms = plotparms['diff'] ) } self.composite_plotspecs = { self.plotall_id: [ self.plot1_id, self.plot2_id, self.plot3_id ] } # ...was self.composite_plotspecs = { self.plotall_id: self.single_plotspecs.keys() } self.computation_planned = True
def plan_computation(self, model, obs, varid, seasonid, names, plotparms): filetable1, filetable2 = self.getfts(model, obs) ft1_hyam = filetable1.find_files('hyam') if filetable2 is None: ft2_hyam = None else: ft2_hyam = filetable2.find_files('hyam') hybrid1 = ft1_hyam is not None and ft1_hyam != [ ] # true iff filetable1 uses hybrid level coordinates hybrid2 = ft2_hyam is not None and ft2_hyam != [ ] # true iff filetable2 uses hybrid level coordinates #print hybrid1, hybrid2 #retrieve the 1d and 2d reduction function; either lat & levlat or lon & levlon RF_1d, RF_2d = self.reduction_functions[self.number] rf_id = self.rf_ids[self.number] #print RF_1d #print RF_2d if hybrid1: reduced_variables_1 = self.reduced_variables_hybrid_lev( filetable1, varid, seasonid, RF1=identity, RF2=identity) else: reduced_variables_1 = self.reduced_variables_press_lev(filetable1, varid, seasonid, RF2=RF_2d) if hybrid2: reduced_variables_2 = self.reduced_variables_hybrid_lev( filetable2, varid, seasonid, RF1=identity, RF2=identity) else: reduced_variables_2 = self.reduced_variables_press_lev(filetable2, varid, seasonid, RF2=RF_2d) reduced_variables_1.update(reduced_variables_2) self.reduced_variables = reduced_variables_1 self.derived_variables = {} if hybrid1: # >>>> actually last arg of the derived var should identify the coarsest level, not nec. 2 # Note also that it is dangerous to use input data from two filetables (though necessary here) - it # can badly mess things up if derive() assigns to the new variable a filetable which is not the # main source of the data, meaning its axes. It gets the filetable from the first input listed here. vid1 = dv.dict_id(varid, rf_id, seasonid, filetable1) self.derived_variables[vid1] = derived_var( vid=vid1, inputs=[ rv.dict_id(varid, seasonid, filetable1), rv.dict_id('hyam', seasonid, filetable1), rv.dict_id('hybm', seasonid, filetable1), rv.dict_id('PS', seasonid, filetable1), rv.dict_id(varid, seasonid, filetable2) ], func=(lambda T, hyam, hybm, ps, level_src, seasonid= seasonid, varid=varid: RF_2d(verticalize( T, hyam, hybm, ps, level_src), season=seasonid, vid=varid))) else: vid1 = rv.dict_id(varid, seasonid, filetable1) if hybrid2: # >>>> actually last arg of the derived var should identify the coarsest level, not nec. 2 vid2 = dv.dict_id(varid, rf_id, seasonid, filetable2) self.derived_variables[vid2] = derived_var( vid=vid2, inputs=[ rv.dict_id(varid, seasonid, filetable2), rv.dict_id('hyam', seasonid, filetable2), rv.dict_id('hybm', seasonid, filetable2), rv.dict_id('PS', seasonid, filetable2), rv.dict_id(varid, seasonid, filetable2) ], func=(lambda T, hyam, hybm, ps, level_src, seasonid= seasonid, varid=varid: RF_2d(verticalize( T, hyam, hybm, ps, level_src), season=seasonid, vid=varid))) else: vid2 = rv.dict_id(varid, seasonid, filetable2) ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' self.single_plotspecs = { self.plot1_id: plotspec(vid=ps.dict_idid(vid1), zvars=[vid1], zfunc=(lambda z: z), plottype=self.plottype, title=' '.join([varid, seasonid, '(1)']), file_descr='model', source=names['model'], plotparms=plotparms[src2modobs(ft1src)]), self.plot2_id: plotspec(vid=ps.dict_idid(vid2), zvars=[vid2], zfunc=(lambda z: z), plottype=self.plottype, title=' '.join([varid, seasonid, '(2)']), file_descr='obs', source=names['obs'], plotparms=plotparms[src2obsmod(ft2src)]), self.plot3_id: plotspec(vid=ps.dict_id(varid, 'diff', seasonid, filetable1, filetable2), zvars=[vid1, vid2], zfunc=aminusb_2ax, plottype=self.plottype, title=' '.join([varid, seasonid, '(1)-(2)']), file_descr='diff', source=', '.join([names['model'], names['obs']]), plotparms=plotparms['diff']) } self.composite_plotspecs = { self.plotall_id: [self.plot1_id, self.plot2_id, self.plot3_id] } self.computation_planned = True
def plan_computation_level_surface(self, model, obs, varnom, seasonid, aux=None, names={}, plotparms=None): filetable1, filetable2 = self.getfts(model, obs) """Set up for a lat-lon contour plot, averaged in other directions - except that if the variable to be plotted depend on level, it is not averaged over level. Instead, the value at a single specified pressure level, aux, is used. The units of aux are millbars.""" # In calling reduce_time_seasonal, I am assuming that no variable has axes other than # (time, lev,lat,lon). # If there were another axis, then we'd need a new function which reduces it as well. if not isinstance(aux, Number): return None pselect = udunits(aux, 'mbar') pselect.value = int(pselect.value) PSELECT = str(pselect) reduced_varlis = [ reduced_variable( # var=var(time,lev,lat,lon) variableid=varnom, filetable=filetable1, season=self.season, reduction_function=(lambda x, vid: reduce_time_seasonal( x, self.season, self.region, vid))), reduced_variable( # hyam=hyam(lev) variableid='hyam', filetable=filetable1, season=self.season, reduction_function=( lambda x, vid=None: select_region(x, self.region))), reduced_variable( # hybm=hybm(lev) variableid='hybm', filetable=filetable1, season=self.season, reduction_function=( lambda x, vid=None: select_region(x, self.region))), reduced_variable( # ps=ps(time,lat,lon) variableid='PS', filetable=filetable1, season=self.season, reduction_function=(lambda x, vid: reduce_time_seasonal( x, self.season, self.region, vid))) ] # vid1 = varnom+'_p_1' # vidl1 = varnom+'_lp_1' vid1 = dv.dict_id(varnom, 'p', seasonid, filetable1) vidl1 = dv.dict_id(varnom, 'lp', seasonid, filetable1) self.derived_variables = { vid1: derived_var( vid=vid1, inputs=[ rv.dict_id(varnom, seasonid, filetable1), rv.dict_id('hyam', seasonid, filetable1), rv.dict_id('hybm', seasonid, filetable1), rv.dict_id('PS', seasonid, filetable1) ], #was vid1: derived_var( vid=vid1, inputs=[ varnom+'_1', 'hyam_1', 'hybm_1', 'PS_1' ], func=verticalize), vidl1: derived_var(vid=vidl1, inputs=[vid1], func=(lambda z, psl=pselect: select_lev(z, psl))) } ft1src = filetable1.source() #note: the title in the following is parced in customizeTemplate. This is a garbage kludge! Mea culpa. varstr = varnom + '(' + PSELECT + ')' self.single_plotspecs = { self.plot1_id: plotspec( # was vid = varnom+'_1', # was zvars = [vid1], zfunc = (lambda z: select_lev( z, pselect ) ), vid=ps.dict_idid(vidl1), zvars=[vidl1], zfunc=(lambda z: z), plottype=self.plottype, title=' '.join([varstr, seasonid, 'model', names['model']]), title1=' '.join([varstr, seasonid, 'model', names['model']]), title2='model', file_descr='model', source=names['model'], plotparms=plotparms[src2modobs(ft1src)]) } if filetable2 is None: self.reduced_variables = {v.id(): v for v in reduced_varlis} self.composite_plotspecs = {self.plotall_id: [self.plot1_id]} self.computation_planned = True return if 'hyam' in filetable2.list_variables( ) and 'hybm' in filetable2.list_variables(): # hybrid levels in use, convert to pressure levels reduced_varlis += [ reduced_variable( # var=var(time,lev,lat,lon) variableid=varnom, filetable=filetable2, season=self.season, reduction_function=(lambda x, vid: reduce_time_seasonal( x, self.season, self.region, vid))), reduced_variable( # hyam=hyam(lev) variableid='hyam', filetable=filetable2, season=self.season, reduction_function=( lambda x, vid=None: select_region(x, self.region))), reduced_variable( # hybm=hybm(lev) variableid='hybm', filetable=filetable2, season=self.season, reduction_function=( lambda x, vid=None: select_region(x, self.region))), reduced_variable( # ps=ps(time,lat,lon) variableid='PS', filetable=filetable2, season=self.season, reduction_function=(lambda x, vid: reduce_time_seasonal( x, self.season, self.region, vid))) ] #vid2 = varnom+'_p_2' #vidl2 = varnom+'_lp_2' vid2 = dv.dict_id(varnom, 'p', seasonid, filetable2) vid2 = dv.dict_id(vards, 'lp', seasonid, filetable2) self.derived_variables[vid2] = derived_var( vid=vid2, inputs=[ rv.dict_id(varnom, seasonid, filetable2), rv.dict_id('hyam', seasonid, filetable2), rv.dict_id('hybm', seasonid, filetable2), rv.dict_id('PS', seasonid, filetable2) ], func=verticalize) self.derived_variables[vidl2] = derived_var( vid=vidl2, inputs=[vid2], func=(lambda z, psl=pselect: select_lev(z, psl))) else: # no hybrid levels, assume pressure levels. #vid2 = varnom+'_2' #vidl2 = varnom+'_lp_2' vid2 = rv.dict_id(varnom, seasonid, filetable2) vidl2 = dv.dict_id(varnom, 'lp', seasonid, filetable2) reduced_varlis += [ reduced_variable( # var=var(time,lev,lat,lon) variableid=varnom, filetable=filetable2, season=self.season, reduction_function=(lambda x, vid: reduce_time_seasonal( x, self.season, self.region, vid))) ] self.derived_variables[vidl2] = derived_var( vid=vidl2, inputs=[vid2], func=(lambda z, psl=pselect: select_lev(z, psl))) self.reduced_variables = {v.id(): v for v in reduced_varlis} try: ft2src = filetable2.source() except: ft2src = '' filterid = ft2src.split('_')[-1] #recover the filter id: kludge self.single_plotspecs[self.plot2_id] = plotspec( #was vid = varnom+'_2', vid=ps.dict_idid(vidl2), zvars=[vidl2], zfunc=(lambda z: z), plottype=self.plottype, title=' '.join([varstr, seasonid, 'observation', names['obs']]), title1=' '.join([varstr, seasonid, 'observation', names['obs']]), title2='observation', file_descr='obs', source=names['obs'], plotparms=plotparms[src2obsmod(ft2src)]) self.single_plotspecs[self.plot3_id] = plotspec( #was vid = varnom+'_diff', vid=ps.dict_id(varnom, 'diff', seasonid, filetable1, filetable2), zvars=[vidl1, vidl2], zfunc=aminusb_2ax, plottype=self.plottype, title=' '.join([varstr, seasonid, 'difference']), title1=' '.join([varstr, seasonid, 'difference']), title2='difference', file_descr='diff', plotparms=plotparms['diff']) # zerocontour=-1 ) self.composite_plotspecs = { self.plotall_id: [self.plot1_id, self.plot2_id, self.plot3_id] } self.computation_planned = True
def plan_computation_normal_contours(self, model, obs, varnom, seasonid, aux=None, names={}, plotparms=None): filetable1, filetable2 = self.getfts(model, obs) """Set up for a lat-lon contour plot, as in plot set 5. Data is averaged over all other axes.""" if varnom in filetable1.list_variables(): vid1, vid1var = self.vars_normal_contours(filetable1, varnom, seasonid, aux=None) elif varnom in self.common_derived_variables.keys(): vid1, vid1var = self.vars_commdervar_normal_contours(filetable1, varnom, seasonid, aux=None) else: logger.error( "variable %s not found in and cannot be computed from %s", varnom, filetable1) return None if filetable2 is not None and varnom in filetable2.list_variables(): vid2, vid2var = self.vars_normal_contours(filetable2, varnom, seasonid, aux=None) elif varnom in self.common_derived_variables.keys(): vid2, vid2var = self.vars_commdervar_normal_contours(filetable2, varnom, seasonid, aux=None) else: vid2, vid2var = None, None self.single_plotspecs = {} ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' all_plotnames = [] if filetable1 is not None: if vid1 is not None: self.single_plotspecs[self.plot1_id] = plotspec( vid=ps.dict_idid(vid1), zvars=[vid1], zfunc=(lambda z: z), plottype=self.plottype, title=' '.join([varnom, seasonid, 'model']), title1=' '.join([varnom, seasonid, 'model']), title2='model', file_descr='model', source=names['model'], plotparms=plotparms[src2modobs(ft1src)]) all_plotnames.append(self.plot1_id) if vid1var is not None: self.single_plotspecs[self.plot1var_id] = plotspec( vid=ps.dict_idid(vid1var), zvars=[vid1var], zfunc=(lambda z: z), plottype=self.plottype, title=' '.join([varnom, seasonid, 'model variance']), title1=' '.join([varnom, seasonid, 'model variance']), title2='model variance', file_descr='model variance', source=names['model'], plotparms=plotparms[src2modobs(ft1src)]) all_plotnames.append(self.plot1var_id) if filetable2 is not None and vid2 is not None: self.single_plotspecs[self.plot2_id] = plotspec( vid=ps.dict_idid(vid2), zvars=[vid2], zfunc=(lambda z: z), plottype=self.plottype, title=' '.join([varnom, seasonid, 'obs']), title1=' '.join([varnom, seasonid, 'obs']), title2="observation", file_descr="obs", source=names['obs'], plotparms=plotparms[src2obsmod(ft2src)]) all_plotnames.append(self.plot2_id) if filetable1 is not None and filetable2 is not None and vid1 is not None and vid2 is not None: self.single_plotspecs[self.plot3_id] = plotspec( vid=ps.dict_id(varnom, 'diff', seasonid, filetable1, filetable2), zvars=[vid1, vid2], zfunc=aminusb_2ax, plottype=self.plottype, title=' '.join([varnom, seasonid, 'difference']), title1=' '.join([varnom, seasonid, 'difference']), title2='difference', file_descr='diff', plotparms=plotparms['diff']) all_plotnames.append(self.plot3_id) if len(all_plotnames) > 0: self.composite_plotspecs = {self.plotall_id: all_plotnames} else: self.composite_plotspecs = {} self.computation_planned = True
def plan_computation( self, model, obs, varid, seasonid, plotparms ): filetable1, filetable2 = self.getfts(model, obs) ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' self.computation_planned = False #check if there is data to process ft1_valid = False ft2_valid = False if filetable1 is not None and filetable2 is not None: #the filetables must contain the variables LWCF and SWCF ft10 = filetable1.find_files(self.vars[0]) ft11 = filetable1.find_files(self.vars[1]) ft20 = filetable2.find_files(self.vars[0]) ft21 = filetable2.find_files(self.vars[1]) ft1_valid = ft10 is not None and ft10!=[] and ft11 is not None and ft11!=[] ft2_valid = ft20 is not None and ft20!=[] and ft21 is not None and ft21!=[] else: logger.error("user must specify 2 data files") return None if not ft1_valid or not ft2_valid: return None VIDs = {} for season in self.seasons: for dt, ft in zip(self.datatype, self.filetables): pair = [] for var in self.vars: VID = rv.dict_id(var, season, ft) VID = id2str(VID) pair += [VID] if ft == filetable1: RF = ( lambda x, vid=VID:x) else: RF = ( lambda x, vid=VID:x[0]) RV = reduced_variable( variableid=var, filetable=ft, season=cdutil.times.Seasons(season), reduction_function=RF) self.reduced_variables[VID] = RV ID = dt + '_' + season VIDs[ID] = pair #create a line as a derived variable self.derived_variables = {} xline = cdms2.createVariable([0., 120.]) xline.id = 'LWCF' yline = cdms2.createVariable([0., -120.]) yline.id = 'SWCF' yline.units = 'Wm-2' self.derived_variables['LINE'] = derived_var(vid='LINE', func=create_yvsx(xline, yline, stride=1)) #inputs=[xline, yline], self.single_plotspecs = {} self.composite_plotspecs[self.plotall_id] = [] self.compositeTitle = self.vars[0] + ' vs ' + self.vars[1] SLICE = slice(0, None, 10) #return every 10th datum for plot_id in self.plot_ids: title = plot_id xVID, yVID = VIDs[plot_id] self.single_plotspecs[plot_id] = plotspec(vid = plot_id, zvars=[xVID], zfunc = (lambda x: x.flatten()[SLICE]), zrangevars={'xrange':[0., 120.]}, z2vars = [yVID], z2func = (lambda x: x.flatten()[SLICE]), z2rangevars={'yrange':[-120., 0.]}, plottype = 'Scatter', title = title, overplotline = False, source = ', '.join([ft1src,ft2src]), plotparms=plotparms[src2modobs(ft1src)] ) self.single_plotspecs['DIAGONAL_LINE'] = plotspec(vid = 'LINE_PS', zvars=['LINE'], zfunc = (lambda x: x), plottype = 'Yxvsx', zlinecolor = 242, title='', overplotline = False) self.composite_plotspecs = {} plotall_id = [] for plot_id in self.plot_ids: ID = plot_id + '_with_line' self.composite_plotspecs[ID] = (plot_id, 'DIAGONAL_LINE' ) plotall_id += [ID] self.composite_plotspecs[self.plotall_id] = plotall_id self.computation_planned = True
def plan_computation(self, model, obs, varid, seasonid, plotparms): filetable1, filetable2 = self.getfts(model, obs) ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' self.computation_planned = False #setup the reduced variables self.reduced_variables = {} vidAll = {} for FT in self.filetables: #pdb.set_trace() VIDs = [] for i in range(1, 13): month = cdutil.times.getMonthString(i) #pdb.set_trace() #create identifiers VID = rv.dict_id(varid, month, FT) RF = (lambda x, vid=id2str(VID), month=VID[2]: reduce2lat_seasonal(x, seasons=cdutil.times.Seasons(month), region=self.region, vid=vid)) RV = reduced_variable(variableid=varid, filetable=FT, season=cdutil.times.Seasons(VID[2]), reduction_function=RF) self.reduced_variables[RV.id()] = RV VIDs += [VID] vidAll[FT] = VIDs vidModel = dv.dict_id(varid, 'ZonalMean model', self._seasonid, filetable1) if self.FT2: vidObs = dv.dict_id(varid, 'ZonalMean obs', self._seasonid, filetable2) vidDiff = dv.dict_id(varid, 'ZonalMean difference', self._seasonid, filetable1) else: vidObs = None vidDiff = None self.derived_variables = {} #create the derived variables which is the composite of the months self.derived_variables[vidModel] = derived_var( vid=id2str(vidModel), inputs=vidAll[filetable1], func=join_data) if self.FT2: self.derived_variables[vidObs] = derived_var( vid=id2str(vidObs), inputs=vidAll[filetable2], func=join_data) #create the derived variable which is the difference of the composites self.derived_variables[vidDiff] = derived_var( vid=id2str(vidDiff), inputs=[vidModel, vidObs], func=aminusb_ax2) #create composite plots np.transpose zfunc = (lambda x: x), zfunc = (lambda z:z), self.single_plotspecs = { self.plot1_id: plotspec(vid=ps.dict_idid(vidModel), zvars=[vidModel], zfunc=(lambda x: MV2.transpose(x)), plottype=self.plottype, source=ft1src, title1=' '.join( [vidModel.var, vidModel.season, vidModel.region]), title2=' '.join( [vidModel.var, vidModel.season, vidModel.region]), file_descr='model', plotparms=plotparms['model']) } if self.FT2: self.single_plotspecs[self.plot2_id] = \ plotspec(vid = ps.dict_idid(vidObs), zvars=[vidObs], zfunc = (lambda x: MV2.transpose(x)), plottype = self.plottype, source = ft2src, title1 = ' '.join([vidObs.var, vidObs.season, vidObs.region]), title2 = 'observations', file_descr = 'obs', plotparms = plotparms['obs'] ) self.single_plotspecs[self.plot3_id] = \ plotspec(vid = ps.dict_idid(vidDiff), zvars = [vidDiff], zfunc = (lambda x: MV2.transpose(x)), plottype = self.plottype, source = ', '.join([ft1src,ft2src]), title1 = ' '.join([vidDiff.var, vidDiff.season, vidDiff.region]), title2 = 'difference', file_descr = 'diff', plotparms = plotparms['diff'] ) self.composite_plotspecs = { self.plotall_id: [self.plot1_id, self.plot2_id, self.plot3_id] } #... was self.composite_plotspecs = { self.plotall_id: self.single_plotspecs.keys() } self.computation_planned = True
def plan_computation_normal_contours( self, model, obs, varid, seasonid, aux, plotparms ): """Set up for a lat-lon contour plot, as in plot set 5. Data is averaged over all other axes.""" filetable1, filetable2 = self.getfts(model, obs) self.derived_variables = {} vars_vec1 = {} vars_vec2 = {} try: if varid=='STRESS' or varid=='SURF_STRESS' or varid=='MOISTURE_TRANSPORT': vars1,rvars1,dvars1,var_cont1,vars_vec1,vid_cont1 =\ self.STRESS_setup( filetable1, varid, seasonid ) vars2,rvars2,dvars2,var_cont2,vars_vec2,vid_cont2 =\ self.STRESS_setup( filetable2, varid, seasonid ) if vars1 is None and vars2 is None: raise DiagError("cannot find set6 variables in data 2") else: logger.error("AMWG plot set 6 does not yet support %s",varid) return None except Exception as e: logger.error("cannot find suitable set6_variables in data for varid= %s",varid) logger.exception(" %s ", e) return None reduced_varlis = [] vardict1 = {'':'nameless_variable'} vardict2 = {'':'nameless_variable'} new_reducedvars, needed_derivedvars = self.STRESS_rvs( filetable1, rvars1, seasonid, vardict1 ) reduced_varlis += new_reducedvars self.reduced_variables = { v.id():v for v in reduced_varlis } self.STRESS_dvs( filetable1, needed_derivedvars, seasonid, vardict1, vid_cont1, vars_vec1 ) new_reducedvars, needed_derivedvars = self.STRESS_rvs( filetable2, rvars2, seasonid, vardict2 ) reduced_varlis += new_reducedvars self.STRESS_dvs( filetable2, needed_derivedvars, seasonid, vardict2, vid_cont2, vars_vec2 ) self.reduced_variables = { v.id():v for v in reduced_varlis } self.single_plotspecs = {} ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' vid_vec1 = vardict1[','.join([vars_vec1[0],vars_vec1[1]])] vid_vec11 = vardict1[vars_vec1[0]] vid_vec12 = vardict1[vars_vec1[1]] vid_vec2 = vardict2[','.join([vars_vec2[0],vars_vec2[1]])] vid_vec21 = vardict2[vars_vec2[0]] vid_vec22 = vardict2[vars_vec2[1]] plot_type_temp = ['Isofill','Vector'] # can't use self.plottype yet because don't support it elsewhere as a list or tuple <<<<< if vars1 is not None: # Draw two plots, contour and vector, over one another to get a single plot. # Only one needs to plot title,source; but both need source to get the filenames right. title = ' '.join([varid,seasonid,'(1)']) contplot = plotspec( vid = ps.dict_idid(vid_cont1), zvars = [vid_cont1], zfunc = (lambda z: z), plottype = plot_type_temp[0], title = title, source=ft1src, plotparms = plotparms[src2modobs(ft1src)] ) vecplot = plotspec( vid = ps.dict_idid(vid_vec1), zvars=[vid_vec11,vid_vec12], zfunc = (lambda z,w: (z,w)), plottype = plot_type_temp[1], title1='', title2='', source=ft1src, plotparms = plotparms[src2modobs(ft1src)] ) #self.single_plotspecs[self.plot1_id] = [contplot,vecplot] self.single_plotspecs[self.plot1_id+'c'] = contplot self.single_plotspecs[self.plot1_id+'v'] = vecplot if vars2 is not None: # Draw two plots, contour and vector, over one another to get a single plot. # Only one needs title,source. title = ' '.join([varid,seasonid,'(2)']) contplot = plotspec( vid = ps.dict_idid(vid_cont2), zvars = [vid_cont2], zfunc = (lambda z: z), plottype = plot_type_temp[0], title = title, source=ft2src, plotparms = plotparms[src2obsmod(ft2src)] ) vecplot = plotspec( vid = ps.dict_idid(vid_vec2), zvars=[vid_vec21,vid_vec22], zfunc = (lambda z,w: (z,w)), plottype = plot_type_temp[1], title1='', title2='', source=ft2src, plotparms = plotparms[src2obsmod(ft2src)] ) self.single_plotspecs[self.plot2_id+'c'] = contplot self.single_plotspecs[self.plot2_id+'v'] = vecplot if vars1 is not None and vars2 is not None: # First, we need some more derived variables in order to do the contours as a magnitude # of the difference vector. diff1_vid = dv.dict_id( vid_vec11[1], 'DIFF1', seasonid, filetable1, filetable2 ) diff1 = derived_var( vid=diff1_vid, inputs=[vid_vec11,vid_vec21], func=aminusb_2ax ) self.derived_variables[diff1_vid] = diff1 diff2_vid = dv.dict_id( vid_vec12[1], 'DIFF1', seasonid, filetable1, filetable2 ) diff2 = derived_var( vid=diff2_vid, inputs=[vid_vec12,vid_vec22], func=aminusb_2ax ) self.derived_variables[diff2_vid] = diff2 title = ' '.join([varid,seasonid,'diff,mag.diff']) #source = underscore_join([ft1src,ft2src]) source = '' contplot = plotspec( vid = ps.dict_id(var_cont1,'mag.of.diff',seasonid,filetable1,filetable2), zvars = [diff1_vid,diff2_vid], zfunc = abnorm, # This is magnitude of difference of vectors plottype = plot_type_temp[0], title=title, source=source, plotparms = plotparms['diff'] ) #contplot = plotspec( # vid = ps.dict_id(var_cont1,'diff.of.mags',seasonid,filetable1,filetable2), # zvars = [vid_cont1,vid_cont2], zfunc = aminusb_2ax, # This is difference of magnitudes. # plottype = plot_type_temp[0], title=title, source=source ) # This could be done in terms of diff1,diff2, but I'll leave it alone for now... vecplot = plotspec( vid = ps.dict_id(vid_vec2,'diff',seasonid,filetable1,filetable2), zvars = [vid_vec11,vid_vec12,vid_vec21,vid_vec22], zfunc = (lambda z1,w1,z2,w2: (aminusb_2ax(z1,z2),aminusb_2ax(w1,w2))), plottype = plot_type_temp[1], title1='', title2='', source=source, plotparms = plotparms['diff'] ) self.single_plotspecs[self.plot3_id+'c'] = contplot self.single_plotspecs[self.plot3_id+'v'] = vecplot # initially we're not plotting the contour part of the plots.... self.composite_plotspecs = { self.plot1_id: ( self.plot1_id+'c', self.plot1_id+'v' ), #self.plot1_id: [ self.plot1_id+'v' ], self.plot2_id: ( self.plot2_id+'c', self.plot2_id+'v' ), self.plot3_id: ( self.plot3_id+'c', self.plot3_id+'v' ), self.plotall_id: [self.plot1_id, self.plot2_id, self.plot3_id] } self.computation_planned = True
def plan_computation( self, model, obs, varid, seasonid, names, plotparms ): filetable1, filetable2 = self.getfts(model, obs) ft1_hyam = filetable1.find_files('hyam') if filetable2 is None: ft2_hyam = None else: ft2_hyam = filetable2.find_files('hyam') hybrid1 = ft1_hyam is not None and ft1_hyam!=[] # true iff filetable1 uses hybrid level coordinates hybrid2 = ft2_hyam is not None and ft2_hyam!=[] # true iff filetable2 uses hybrid level coordinates #print hybrid1, hybrid2 #retrieve the 1d and 2d reduction function; either lat & levlat or lon & levlon RF_1d, RF_2d = self.reduction_functions[self.number] rf_id = self.rf_ids[self.number] #print RF_1d #print RF_2d if hybrid1: reduced_variables_1 = self.reduced_variables_hybrid_lev( filetable1, varid, seasonid, RF1=identity, RF2=identity ) else: reduced_variables_1 = self.reduced_variables_press_lev( filetable1, varid, seasonid, RF2=RF_2d) if hybrid2: reduced_variables_2 = self.reduced_variables_hybrid_lev( filetable2, varid, seasonid, RF1=identity, RF2=identity ) else: reduced_variables_2 = self.reduced_variables_press_lev( filetable2, varid, seasonid, RF2=RF_2d ) reduced_variables_1.update( reduced_variables_2 ) self.reduced_variables = reduced_variables_1 self.derived_variables = {} if hybrid1: # >>>> actually last arg of the derived var should identify the coarsest level, not nec. 2 # Note also that it is dangerous to use input data from two filetables (though necessary here) - it # can badly mess things up if derive() assigns to the new variable a filetable which is not the # main source of the data, meaning its axes. It gets the filetable from the first input listed here. vid1=dv.dict_id(varid,rf_id,seasonid,filetable1) self.derived_variables[vid1] = derived_var( vid=vid1, inputs=[rv.dict_id(varid,seasonid,filetable1), rv.dict_id('hyam',seasonid,filetable1), rv.dict_id('hybm',seasonid,filetable1), rv.dict_id('PS',seasonid,filetable1), rv.dict_id(varid,seasonid,filetable2) ], func=(lambda T, hyam, hybm, ps, level_src, seasonid=seasonid, varid=varid: RF_2d( verticalize(T,hyam,hybm,ps,level_src), season=seasonid, vid=varid ) ) ) else: vid1 = rv.dict_id(varid,seasonid,filetable1) if hybrid2: # >>>> actually last arg of the derived var should identify the coarsest level, not nec. 2 vid2=dv.dict_id(varid,rf_id,seasonid,filetable2) self.derived_variables[vid2] = derived_var( vid=vid2, inputs=[rv.dict_id(varid,seasonid,filetable2), rv.dict_id('hyam',seasonid,filetable2), rv.dict_id('hybm',seasonid,filetable2), rv.dict_id('PS',seasonid,filetable2), rv.dict_id(varid,seasonid,filetable2) ], func=(lambda T, hyam, hybm, ps, level_src, seasonid=seasonid, varid=varid: RF_2d( verticalize(T,hyam,hybm,ps,level_src), season=seasonid, vid=varid ) ) ) else: vid2 = rv.dict_id(varid,seasonid,filetable2) ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' self.single_plotspecs = { self.plot1_id: plotspec( vid = ps.dict_idid(vid1), zvars=[vid1], zfunc=(lambda z: z), plottype = self.plottype, title = ' '.join([varid,seasonid,'(1)']), file_descr = 'model', source = names['model'], plotparms = plotparms[src2modobs(ft1src)] ), self.plot2_id: plotspec( vid = ps.dict_idid(vid2), zvars=[vid2], zfunc=(lambda z: z), plottype = self.plottype, title = ' '.join([varid,seasonid,'(2)']), file_descr = 'obs', source = names['obs'], plotparms = plotparms[src2obsmod(ft2src)] ), self.plot3_id: plotspec( vid = ps.dict_id(varid,'diff',seasonid,filetable1,filetable2), zvars=[vid1,vid2], zfunc=aminusb_2ax, plottype = self.plottype, title = ' '.join([varid,seasonid,'(1)-(2)']), file_descr = 'diff', source = ', '.join([names['model'],names['obs']]), plotparms = plotparms['diff'] ) } self.composite_plotspecs = { self.plotall_id: [self.plot1_id, self.plot2_id, self.plot3_id ] } self.computation_planned = True
def plan_computation(self, model, obs, varnom, seasonid, plotparms): filetable1, filetable2 = self.getfts(model, obs) ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' self.reduced_variables = {} vidAll = {} for FT in [filetable1, filetable2]: VIDs = [] for i in range(1, 13): month = cdutil.times.getMonthString(i) #pdb.set_trace() #create identifiers VID = rv.dict_id(varnom, month, FT) #cdutil.times.getMonthIndex(VID[2])[0]-1 RF = (lambda x, vid=id2str(VID), month=VID[2]: reduce2scalar_seasonal_zonal( x, seasons=cdutil.times.Seasons(month), vid=vid)) RV = reduced_variable(variableid=varnom, filetable=FT, season=cdutil.times.Seasons(month), reduction_function=RF) VID = id2str(VID) self.reduced_variables[VID] = RV VIDs += [VID] vidAll[FT] = VIDs #create the identifiers vidModel = dv.dict_id(varnom, 'model', "", filetable1) vidObs = dv.dict_id(varnom, 'obs', "", filetable2) self.vidModel = id2str(vidModel) self.vidObs = id2str(vidObs) #create the derived variables which is the composite of the months #pdb.set_trace() model = derived_var(vid=self.vidModel, inputs=vidAll[filetable1], func=join_1d_data) obs = derived_var(vid=self.vidObs, inputs=vidAll[filetable2], func=join_1d_data) self.derived_variables = {self.vidModel: model, self.vidObs: obs} #create the plot spec self.single_plotspecs = {} self.single_plotspecs[self.plot_id] = plotspec( self.plot_id, zvars=[self.vidModel], zfunc=(lambda y: y), z2vars=[self.vidObs], z2func=(lambda z: z), plottype=self.plottype, source=', '.join([ft1src, ft2src]), file_descr='', plotparms=plotparms[src2modobs(ft1src)]) self.computation_planned = True
def test_driver( path1, path2=None, filt2=None ): """ Test driver for setting up data for plots""" # First, find and index the data files. datafiles1 = dirtree_datafiles( path1 ) logger.info("jfp datafiles1=%s",datafiles1) datafiles2 = dirtree_datafiles( path2, filt2 ) logger.info("jfp datafiles2=%s",datafiles2) filetable1 = basic_filetable( datafiles1 ) filetable2 = basic_filetable( datafiles2 ) # Next we'll compute reduced variables. They have generally been reduced by averaging in time, # and often more axes as well. Reducing the data first is the fastest way to compute, important # if we need to be interactive. And it is correct if whatever we plot is linear in the # variables, as is almost always the case. But if we want to plot a highly nonlinear function # of the data variables, the averaging will have to wait until later. # The reduced_variables dict names and contains all the reduced variables which we have defined. # They will be used in defining instances of plotspec. reduced_variables = { 'hyam_1': reduced_variable( variableid='hyam', filetable=filetable1, reduction_function=(lambda x,vid=None: x) ), 'hybm_1': reduced_variable( variableid='hybm', filetable=filetable1, reduction_function=(lambda x,vid=None: x) ), 'PS_ANN_1': reduced_variable( variableid='PS', filetable=filetable1, reduction_function=reduce2lat ), 'T_CAM_ANN_1': reduced_variable( variableid='T', filetable=filetable1, reduction_function=reduce2levlat ), 'T_CAM_ANN_2': reduced_variable( variableid='T', filetable=filetable2, reduction_function=reduce2levlat ), 'TREFHT_ANN_latlon_Npole_1': reduced_variable( variableid='TREFHT', filetable=filetable1, reduction_function=(lambda x,vid=None: restrict_lat(reduce2latlon(x,vid=vid),50,90)) ), 'TREFHT_ANN_latlon_Npole_2': reduced_variable( variableid='TREFHT', filetable=filetable2, reduction_function=(lambda x,vid=None: restrict_lat(reduce2latlon(x,vid=vid),50,90)) ), 'TREFHT_ANN_lat_1': reduced_variable( variableid='TREFHT', filetable=filetable1, reduction_function=reduce2lat ), 'TREFHT_DJF_lat_1': reduced_variable( variableid='TREFHT', filetable=filetable1, reduction_function=(lambda x,vid=None: reduce2lat_seasonal(x,seasonsDJF,vid=vid)) ), 'TREFHT_DJF_lat_2': reduced_variable( variableid='TREFHT', filetable=filetable2, reduction_function=(lambda x,vid=None: reduce2lat_seasonal(x,seasonsDJF,vid=vid)) ), 'TREFHT_DJF_latlon_1': reduced_variable( variableid='TREFHT', filetable=filetable1, reduction_function=(lambda x,vid=None: reduce2latlon_seasonal(x,seasonsDJF,vid=vid)) ), 'TREFHT_DJF_latlon_2': reduced_variable( variableid='TREFHT', filetable=filetable2, reduction_function=(lambda x,vid=None: reduce2latlon_seasonal(x,seasonsDJF,vid=vid)) ), 'TREFHT_JJA': reduced_variable( variableid='TREFHT', filetable=filetable1, reduction_function=(lambda x,vid=None: reduce2lat_seasonal(x,seasonsJJA,vid=vid)) ), 'PRECT_JJA_lat_1': reduced_variable( variableid='PRECT', filetable=filetable1, reduction_function=(lambda x,vid=None: reduce2lat_seasonal(x,seasonsJJA,vid=vid)) ), 'PRECT_JJA_lat_2': reduced_variable( variableid='PRECT', filetable=filetable2, reduction_function=(lambda x,vid=None: reduce2lat_seasonal(x,seasonsJJA,vid=vid)) ), # CAM variables needed for heat transport: # FSNS, FLNS, FLUT, FSNTOA, FLNT, FSNT, SHFLX, LHFLX, 'FSNS_1': reduced_variable( variableid='FSNS',filetable=filetable1,reduction_function=(lambda x,vid:x) ), 'FSNS_ANN_latlon_1': reduced_variable( variableid='FSNS', filetable=filetable1, reduction_function=reduce2latlon ), 'FLNS_1': reduced_variable( variableid='FLNS',filetable=filetable1,reduction_function=(lambda x,vid:x) ), 'FLNS_ANN_latlon_1': reduced_variable( variableid='FLNS', filetable=filetable1, reduction_function=reduce2latlon ), 'FLUT_ANN_latlon_1': reduced_variable( variableid='FLUT', filetable=filetable1, reduction_function=reduce2latlon ), 'FSNTOA_ANN_latlon_1': reduced_variable( variableid='FSNTOA', filetable=filetable1, reduction_function=reduce2latlon ), 'FLNT_1': reduced_variable( variableid='FLNT',filetable=filetable1,reduction_function=(lambda x,vid:x) ), 'FLNT_ANN_latlon_1': reduced_variable( variableid='FLNT', filetable=filetable1, reduction_function=reduce2latlon ), 'FSNT_1': reduced_variable( variableid='FSNT',filetable=filetable1,reduction_function=(lambda x,vid:x) ), 'FSNT_ANN_latlon_1': reduced_variable( variableid='FSNT', filetable=filetable1, reduction_function=reduce2latlon ), 'QFLX_1': reduced_variable( variableid='QFLX',filetable=filetable1,reduction_function=(lambda x,vid:x) ), 'SHFLX_1': reduced_variable( variableid='SHFLX',filetable=filetable1,reduction_function=(lambda x,vid:x) ), 'SHFLX_ANN_latlon_1': reduced_variable( variableid='SHFLX', filetable=filetable1, reduction_function=reduce2latlon ), 'LHFLX_ANN_latlon_1': reduced_variable( variableid='LHFLX', filetable=filetable1, reduction_function=reduce2latlon ), 'ORO_ANN_latlon_1': reduced_variable( variableid='ORO', filetable=filetable1, reduction_function=reduce2latlon ), 'OCNFRAC_ANN_latlon_1': reduced_variable( variableid='OCNFRAC', filetable=filetable1, reduction_function=reduce2latlon ), 'ts_lat_old': reduced_variable( variableid='surface_temperature', # normally a CF standard_name, even for non-CF data. filetable=filetable1, reduction_function=reduce2lat_old ), 'ts_lat_new': reduced_variable( variableid='surface_temperature', # normally a CF standard_name, even for non-CF data. filetable=filetable1, reduction_function=reduce2lat # The reduction function will take just one argument, a variable (MV). But it might # be expressed here as a lambda wrapping a more general function. # Often there will be ranges in time, space, etc. specified here. No range means # everything. ), 'ts_scalar_tropical_o': reduced_variable( variableid = 'surface_temperature', filetable=filetable1, reduction_function=(lambda mv,vid=None: reduce2scalar_zonal_old(mv,-20,20,vid=vid)) ), 'ts_scalar_tropical_n': reduced_variable( variableid = 'surface_temperature', filetable=filetable1, reduction_function=(lambda mv,vid=None: reduce2scalar_zonal(mv,-20,20,vid=vid)) ) } # Derived variables have to be treated separately from reduced variables # because derived variables generally depend on reduced variables. # But N.B.: the dicts reduced_variables and derived_variables # must never use the same key! derived_variables = { 'CAM_HEAT_TRANSPORT_ALL_1': derived_var( vid='CAM_HEAT_TRANSPORT_ALL_1', inputs=['FSNS_ANN_latlon_1', 'FLNS_ANN_latlon_1', 'FLUT_ANN_latlon_1', 'FSNTOA_ANN_latlon_1', 'FLNT_ANN_latlon_1', 'FSNT_ANN_latlon_1', 'SHFLX_ANN_latlon_1', 'LHFLX_ANN_latlon_1', 'OCNFRAC_ANN_latlon_1' ], outputs=['atlantic_heat_transport','pacific_heat_transport', 'indian_heat_transport', 'global_heat_transport' ], func=oceanic_heat_transport ), 'NCEP_OBS_HEAT_TRANSPORT_ALL_2': derived_var( vid='NCEP_OBS_HEAT_TRANSPORT_ALL_2', inputs=[], outputs=('latitude', ['atlantic_heat_transport','pacific_heat_transport', 'indian_heat_transport', 'global_heat_transport' ]), func=(lambda: ncep_ocean_heat_transport(path2) ) ), 'T_ANN_1': derived_var( vid='T_ANN_1', inputs=['T_CAM_ANN_1', 'hyam_1', 'hybm_1', 'PS_ANN_1', 'T_CAM_ANN_2'], outputs=('temperature'), func=verticalize ) } plotvars = dict( reduced_variables.items() + derived_variables.items() ) # The plotvspecs dict names and contains all plotspec objects which we have defined. # The plotspeckeys variable, below, names the ones for which we will generate output. # A dict value can be a plotspec object, or a list of such objects. A list of # plotspec instances specifies a page containing multiple plots in separate panes. plotspecs = { 'TREFHT_ANN_Npole_ALL': ['TREFHT_ANN_Npole_1', 'TREFHT_ANN_Npole_2', 'TREFHT_ANN_Npole_diff'], 'TREFHT_ANN_Npole_1': plotspec( vid='TREFHT_ANN_Npole_1', xvars=['TREFHT_ANN_latlon_Npole_1'], xfunc = lonvar, yvars=['TREFHT_ANN_latlon_Npole_1'], yfunc = latvar, zvars=['TREFHT_ANN_latlon_Npole_1'], zfunc = (lambda z: z), plottype='polar contour plot' ), 'TREFHT_ANN_Npole_2': plotspec( vid='TREFHT_ANN_Npole_2', xvars=['TREFHT_ANN_latlon_Npole_2'], xfunc = lonvar, yvars=['TREFHT_ANN_latlon_Npole_2'], yfunc = latvar, zvars=['TREFHT_ANN_latlon_Npole_2'], zfunc = (lambda z: z), plottype='polar contour plot' ), 'TREFHT_ANN_Npole_diff': plotspec( vid='TREFHT_ANN_Npole_diff', xvars=['TREFHT_ANN_latlon_Npole_1','TREFHT_ANN_latlon_Npole_2'], xfunc = lonvar_min, yvars=['TREFHT_ANN_latlon_Npole_1','TREFHT_ANN_latlon_Npole_2'], yfunc = latvar_min, zvars=['TREFHT_ANN_latlon_Npole_1','TREFHT_ANN_latlon_Npole_2'], zfunc = aminusb_2ax, plottype='polar contour plot' ), 'TREFHT_DJF_laton_ALL': ['TREFHT_DJF_latlon_1', 'TREFHT_DJF_latlon_2', 'TREFHT_DJF_latlon_diff'], 'TREFHT_DJF_latlon_1': plotspec( vid='TREFHT_DJF_latlon_1', xvars=['TREFHT_DJF_latlon_1'], xfunc = lonvar, yvars=['TREFHT_DJF_latlon_1'], yfunc = latvar, zvars=['TREFHT_DJF_latlon_1'], zfunc = (lambda z: z), plottype='contour plot' ), 'TREFHT_DJF_latlon_2': plotspec( vid='TREFHT_DJF_latlon_2', xvars=['TREFHT_DJF_latlon_2'], xfunc = lonvar, yvars=['TREFHT_DJF_latlon_2'], yfunc = latvar, zvars=['TREFHT_DJF_latlon_2'], zfunc = (lambda z: z), plottype='contour plot' ), 'TREFHT_DJF_latlon_diff': plotspec( vid='TREFHT_DJF_latlon_diff', xvars=['TREFHT_DJF_latlon_1','TREFHT_DJF_latlon_2'], xfunc=lonvar_min, yvars=['TREFHT_DJF_latlon_1','TREFHT_DJF_latlon_2'], yfunc=latvar_min, zvars=['TREFHT_DJF_latlon_1','TREFHT_DJF_latlon_2'], zfunc= aminusb_2ax, plottype='contour_plot' ), 'T_ANN_VERT_CAM_OBS_ALL': ['T_VERT_ANN_1', 'T_VERT_ANN_2', 'T_VERT_difference' ], 'T_VERT_difference': plotspec( vid='T_VERT_difference', xvars=['T_ANN_1','T_CAM_ANN_2'], xfunc = latvar_min, yvars=['T_ANN_1','T_CAM_ANN_2'], yfunc = levvar_min, ya1vars=['T_ANN_1','T_CAM_ANN_2'], ya1func = (lambda y1,y2: heightvar(levvar_min(y1,y2))), zvars=['T_ANN_1','T_CAM_ANN_2'], zfunc=aminusb_ax2, plottype="contour plot" ), 'T_VERT_ANN_2': plotspec( vid='T_ANN_2', xvars=['T_CAM_ANN_2'], xfunc=latvar, yvars=['T_CAM_ANN_2'], yfunc=levvar, ya1vars=['T_CAM_ANN_2'], ya1func=heightvar, zvars=['T_CAM_ANN_2'], plottype='contour plot', zrangevars=['T_ANN_1','T_CAM_ANN_2'], zrangefunc=minmin_maxmax ), 'T_VERT_ANN_1': plotspec( vid='T_ANN_1', xvars=['T_ANN_1'], xfunc=latvar, yvars=['T_ANN_1'], yfunc=levvar, ya1vars=['T_ANN_1'], ya1func=heightvar, zvars=['T_ANN_1'], plottype='contour plot', zrangevars=['T_ANN_1','T_CAM_ANN_2'], zrangefunc=minmin_maxmax ), 'NCEP_OBS_HEAT_TRANSPORT_GLOBAL_2': plotspec( vid='NCEP_OBS_HEAT_TRANSPORT_GLOBAL_2', xvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], xfunc=(lambda x: x[0]), yvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2' ], yfunc=(lambda y: y[1][3]), plottype='line plot'), 'NCEP_OBS_HEAT_TRANSPORT_PACIFIC_2': plotspec( vid='NCEP_OBS_HEAT_TRANSPORT_PACIFIC_2', xvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], xfunc=(lambda x: x[0]), yvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2' ], yfunc=(lambda y: y[1][0]), plottype='line plot'), 'NCEP_OBS_HEAT_TRANSPORT_ATLANTIC_2': plotspec( vid='NCEP_OBS_HEAT_TRANSPORT_ATLANTIC_2', xvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], xfunc=(lambda x: x[0]), yvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2' ], yfunc=(lambda y: y[1][1]), plottype='line plot'), 'NCEP_OBS_HEAT_TRANSPORT_INDIAN_2': plotspec( vid='NCEP_OBS_HEAT_TRANSPORT_INDIAN_2', xvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], xfunc=(lambda x: x[0]), yvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2' ], yfunc=(lambda y: y[1][2]), plottype='line plot'), 'CAM_HEAT_TRANSPORT_GLOBAL_1': plotspec( vid='CAM_HEAT_TRANSPORT_GLOBAL_1', xvars=['FSNS_ANN_latlon_1'], xfunc=latvar, yvars=['CAM_HEAT_TRANSPORT_ALL_1' ], yfunc=(lambda y: y[3]), plottype='line plot'), 'CAM_HEAT_TRANSPORT_PACIFIC_1': plotspec( vid='CAM_HEAT_TRANSPORT_PACIFIC_1', xvars=['FSNS_ANN_latlon_1'], xfunc=latvar, yvars=['CAM_HEAT_TRANSPORT_ALL_1' ], yfunc=(lambda y: y[0]), plottype='line plot'), 'CAM_HEAT_TRANSPORT_ATLANTIC_1': plotspec( vid='CAM_HEAT_TRANSPORT_ATLANTIC_1', xvars=['FSNS_ANN_latlon_1'], xfunc=latvar, yvars=['CAM_HEAT_TRANSPORT_ALL_1' ], yfunc=(lambda y: y[1]), plottype='line plot'), 'CAM_HEAT_TRANSPORT_INDIAN_1': plotspec( vid='CAM_HEAT_TRANSPORT_INDIAN_1', xvars=['FSNS_ANN_latlon_1'], xfunc=latvar, yvars=['CAM_HEAT_TRANSPORT_ALL_1' ], yfunc=(lambda y: y[2]), plottype='line plot'), 'CAM_HEAT_TRANSPORT_ALL_1': ['CAM_HEAT_TRANSPORT_GLOBAL_1','CAM_HEAT_TRANSPORT_PACIFIC_1', 'CAM_HEAT_TRANSPORT_ATLANTIC_1','CAM_HEAT_TRANSPORT_INDIAN_1'], 'CAM_NCEP_HEAT_TRANSPORT_GLOBAL': plotspec( vid='CAM_NCEP_HEAT_TRANSPORT_GLOBAL', x1vars=['FSNS_ANN_latlon_1'], x1func=latvar, y1vars=['CAM_HEAT_TRANSPORT_ALL_1' ], y1func=(lambda y: y[3]), x2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], x2func=(lambda x: x[0]), y2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2' ], y2func=(lambda y: y[1][3]), plottype='2 line plot' ), 'CAM_NCEP_HEAT_TRANSPORT_PACIFIC': plotspec( vid='CAM_NCEP_HEAT_TRANSPORT_PACIFIC', x1vars=['FSNS_ANN_latlon_1'], x1func=latvar, y1vars=['CAM_HEAT_TRANSPORT_ALL_1' ], y1func=(lambda y: y[0]), x2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], x2func=(lambda x: x[0]), y2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2' ], y2func=(lambda y: y[1][0]), plottype='2 line plot' ), 'CAM_NCEP_HEAT_TRANSPORT_ATLANTIC': plotspec( vid='CAM_NCEP_HEAT_TRANSPORT_ATLANTIC', x1vars=['FSNS_ANN_latlon_1'], x1func=latvar, y1vars=['CAM_HEAT_TRANSPORT_ALL_1' ], y1func=(lambda y: y[0]), x2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], x2func=(lambda x: x[0]), y2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2' ], y2func=(lambda y: y[1][1]), plottype='2 line plot' ), 'CAM_NCEP_HEAT_TRANSPORT_INDIAN': plotspec( vid='CAM_NCEP_HEAT_TRANSPORT_INDIAN', x1vars=['FSNS_ANN_latlon_1'], x1func=latvar, y1vars=['CAM_HEAT_TRANSPORT_ALL_1' ], y1func=(lambda y: y[0]), x2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], x2func=(lambda x: x[0]), y2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2' ], y2func=(lambda y: y[1][2]), plottype='2 line plot' ), 'CAM_NCEP_HEAT_TRANSPORT_ALL': ['CAM_NCEP_HEAT_TRANSPORT_GLOBAL','CAM_NCEP_HEAT_TRANSPORT_PACIFIC', 'CAM_NCEP_HEAT_TRANSPORT_ATLANTIC','CAM_NCEP_HEAT_TRANSPORT_INDIAN'], 'past_CAM_HEAT_TRANSPORT_GLOBAL_1': plotspec( vid='CAM_HEAT_TRANSPORT_GLOBAL_1', xvars=['FSNS_ANN_latlon_1'], xfunc=latvar, yvars=['FSNS_ANN_latlon_1', 'FLNS_ANN_latlon_1', 'FLUT_ANN_latlon_1', 'FSNTOA_ANN_latlon_1', 'FLNT_ANN_latlon_1', 'FSNT_ANN_latlon_1', 'SHFLX_ANN_latlon_1', 'LHFLX_ANN_latlon_1', 'OCNFRAC_ANN_latlon_1' ], yfunc=oceanic_heat_transport, plottype='line plot'), 'PRECT_JJA': ['PRECT_JJA_2line','PRECT_JJA_diff'], 'PRECT_JJA_2line': plotspec( vid='PRECT_JJA_2line', x1vars=['PRECT_JJA_lat_1'], x1func = latvar, x2vars=['PRECT_JJA_lat_2'], x2func = latvar, y1vars=['PRECT_JJA_lat_1'], y1func=(lambda y: y), y2vars=['PRECT_JJA_lat_2'], y2func=(lambda y: y), plottype='2-line plot'), 'PRECT_JJA_diff': plotspec( vid='PRECT_JJA_difference', xvars=['PRECT_JJA_lat_1','PRECT_JJA_lat_2'], xfunc = latvar_min, yvars=['PRECT_JJA_lat_1','PRECT_JJA_lat_2'], yfunc=aminusb_1ax, # aminusb_1ax(y1,y2)=y1-y2; each y has 1 axis, use min axis plottype='line plot'), 'TREFHT_ANN': plotspec( vid='TREFHT_ANN',xvars=['TREFHT_ANN_lat_1'], xfunc = latvar, yvars=['TREFHT_ANN_lat_1'], yfunc=(lambda y: y), plottype='line plot'), 'TREFHT_DJF': ['TREFHT_DJF_2line','TREFHT_DJF_diff'], 'TREFHT_DJF_2line': plotspec( vid='TREFHT_DJF_2line', x1vars=['TREFHT_DJF_lat_1'], x1func = latvar, x2vars=['TREFHT_DJF_lat_2'], x2func = latvar, y1vars=['TREFHT_DJF_lat_1'], y1func=(lambda y: y), y2vars=['TREFHT_DJF_lat_2'], y2func=(lambda y: y), plottype='2-line plot'), 'TREFHT_DJF_diff': plotspec( vid='TREFHT_DJF_diff', xvars=['TREFHT_DJF_lat_1','TREFHT_DJF_lat_2'], xfunc = latvar_min, yvars=['TREFHT_DJF_lat_1','TREFHT_DJF_lat_2'], yfunc=aminusb_1ax, # aminusb_1ax(y1,y2)=y1-y2; each y has 1 axis, use min axis plottype='line plot'), 'TREFHT_DJF_line': plotspec( vid='TREFHT_DJF_line', xvars=['TREFHT_DJF_lat_1'], xfunc = latvar, yvars=['TREFHT_DJF_lat_1'], plottype='line plot'), 'TREFHT_DJF_contour': plotspec( vid='TREFHT_DJF_contour', xvars=['TREFHT_DJF_latlon_1'], xfunc = (lambda x: x), plottype='line plot'), #plotspec( vid='TREFHT_JJA',xvars=['TREFHT_JJA'], xfiletable=filetable1, xfunc = latvar, # yvars=['TREFHT_JJA'], yfunc=(lambda y: y), plottype='line plot'), #plotspec( # vid='ts_by_lat_old', # suitable for filenames # xfiletable=filetable1, # xfunc = latvar, # function to return x axis values # xvars = ['ts_lat_old'], # names of variables or axes, args of xfunc # yfiletable=filetable1, # can differ from xfiletable, e.g. comparing 2 runs # yfunc = (lambda y: y), # function to return y axis values # yvars = ['ts_lat_old'], # names of variables or axes, args of xfunc # zfiletable=filetable1, # zfunc = (lambda: None), # zvars = [], # would be needed for countour or 3D plot # # ... the ?vars variable will be converted (using the filetable and # # plotvars) to actual variables which become the arguments for a call # # of ?func, which returns the data we write out for plotting use. # plottype='line plot' ), 'ts_by_lat': plotspec( vid='ts_by_lat', # suitable for filenames xfunc = latvar, # function to return x axis values xvars = ['ts_lat_new'], # names of variables or axes, args of xfunc yfunc = (lambda y: y), # function to return y axis values yvars = ['ts_lat_new'], # names of variables or axes, args of xfunc zfunc = (lambda: None), zvars = [], # would be needed for countour or 3D plot # ... the ?vars variable will be converted (using the filetable and # plotvars) to actual variables which become the arguments for a call # of ?func, which returns the data we write out for plotting use. plottype='line plot' ), #plotspec( vid="ts_global_old",xvars=['ts_scalar_tropical_o'], xfiletable=filetable1 ), 'ts_global': plotspec( vid="ts_global",xvars=['ts_scalar_tropical_n'] ), } # Plotspeckeys specifies what plot data we will compute and write out. # In the future we may add a command line option, or provide other ways to # define plotspeckeys. #plotspeckeys = [['TREFHT_DJF_2line','TREFHT_DJF_difference']] #plotspeckeys = ['TREFHT_DJF_2line'] #plotspeckeys = ['NCEP_OBS_HEAT_TRANSPORT_GLOBAL_2','CAM_HEAT_TRANSPORT_ALL_1'] #plotspeckeys = ['CAM_NCEP_HEAT_TRANSPORT_GLOBAL'] #plotspeckeys = ['CAM_NCEP_HEAT_TRANSPORT_ALL'] #plotspeckeys = ['T_ANN_VERT_CAM_OBS_ALL'] #plotspeckeys = ['TREFHT_DJF_laton_ALL'] #plotspeckeys = ['TREFHT_ANN_Npole_ALL'] plotspeckeys = ['TREFHT_DJF'] #plotspeckeys = ['GLOBAL_AVERAGES'] # Find the variable names required by the plotspecs. varkeys = [] for psk in plotspeckeys: if type(psk) is str and type(plotspecs[psk]) is list: psk = plotspecs[psk] if type(psk) is str: write_xml = False psl = [ plotspecs[psk] ] else: write_xml = True psl = [ plotspecs[k] for k in psk ] xml_name = '_'.join( [ ps._strid for ps in psl ] ) +'.xml' h = open( xml_name, 'w' ) h.write("<plotdata>\n") for ps in psl: varkeys = varkeys+ps.xvars+ps.x1vars+ps.x2vars+ps.x3vars varkeys = varkeys+ps.yvars+ps.y1vars+ps.y2vars+ps.y3vars varkeys = varkeys + ps.zvars + ps.zrangevars for key in varkeys: if key in derived_variables.keys(): varkeys = varkeys + derived_variables[key]._inputs varkeys = list( set(varkeys) ) # Compute the value of every variable we need. varvals = {} # First compute all the reduced variables for key in varkeys: if key in reduced_variables.keys(): varvals[key] = reduced_variables[key].reduce() # Then use the reduced variables to compute the derived variables # Note that the derive() method is allowed to return a tuple. This way # we can use one function to compute what's really several variables. for key in varkeys: if key in derived_variables.keys(): varvals[key] = derived_variables[key].derive(varvals) # Now use the reduced and derived variables to compute the plot data. for psk in plotspeckeys: if type(psk) is str and type(plotspecs[psk]) is list: psk = plotspecs[psk] if type(psk) is str: write_xml = False psl = [ plotspecs[psk] ] else: write_xml = True psl = [ plotspecs[k] for k in psk ] xml_name = '_'.join( [ ps._strid for ps in psl ] ) +'.xml' h = open( xml_name, 'w' ) h.write("<plotdata>\n") varkeys = [] for ps in psl: logger.info("jfp preparing data for %s", ps._strid) xrv = [ varvals[k] for k in ps.xvars ] x1rv = [ varvals[k] for k in ps.x1vars ] x2rv = [ varvals[k] for k in ps.x2vars ] x3rv = [ varvals[k] for k in ps.x3vars ] yrv = [ varvals[k] for k in ps.yvars ] y1rv = [ varvals[k] for k in ps.y1vars ] y2rv = [ varvals[k] for k in ps.y2vars ] y3rv = [ varvals[k] for k in ps.y3vars ] yarv = [ varvals[k] for k in ps.yavars ] ya1rv = [ varvals[k] for k in ps.ya1vars ] zrv = [ varvals[k] for k in ps.zvars ] zrrv = [ varvals[k] for k in ps.zrangevars ] xax = apply( ps.xfunc, xrv ) x1ax = apply( ps.x1func, x1rv ) x2ax = apply( ps.x2func, x2rv ) x3ax = apply( ps.x3func, x3rv ) yax = apply( ps.yfunc, yrv ) y1ax = apply( ps.y1func, y1rv ) y2ax = apply( ps.y2func, y2rv ) y3ax = apply( ps.y3func, y3rv ) yaax = apply( ps.yafunc, yarv ) ya1ax = apply( ps.ya1func, ya1rv ) zax = apply( ps.zfunc, zrv ) zr = apply( ps.zrangefunc, zrrv ) if (xax is None or len(xrv)==0) and (x1ax is None or len(x1rv)==0)\ and (x2ax is None or len(x2rv)==0) and (x3ax is None or len(x3rv)==0)\ and (yax is None or len(yrv)==0) and (y1ax is None or len(y1rv)==0)\ and (y2ax is None or len(y2rv)==0) and (y3ax is None or len(y3rv)==0)\ and (zax is None or len(zrv)==0): continue filename = ps._strid+"_test.nc" value=0 cdms2.setNetcdfShuffleFlag(value) ## where value is either 0 or 1 cdms2.setNetcdfDeflateFlag(value) ## where value is either 0 or 1 cdms2.setNetcdfDeflateLevelFlag(value) ## where value is a integer between 0 and 9 included g = cdms2.open( filename, 'w' ) # later, choose a better name and a path! store_provenance(g) # Much more belongs in g, e.g. axis and graph names. if xax is not None and len(xrv)>0: xax.id = 'X' g.write(xax) if x1ax is not None and len(x1rv)>0: x1ax.id = 'X1' g.write(x1ax) if x2ax is not None and len(x2rv)>0: x2ax.id = 'X2' g.write(x2ax) if x3ax is not None and len(x3rv)>0: x3ax.id = 'X3' g.write(x3ax) if yax is not None and len(yrv)>0: yax.id = 'Y' g.write(yax) if y1ax is not None and len(y1rv)>0: y1ax.id = 'Y1' g.write(y1ax) if y2ax is not None and len(y2rv)>0: y2ax.id = 'Y2' g.write(y2ax) if y3ax is not None and len(y3rv)>0: y3ax.id = 'Y3' g.write(y3ax) if yaax is not None and len(yarv)>0: yaax.id = 'YA' g.write(yaax) if ya1ax is not None and len(ya1rv)>0: ya1ax.id = 'YA1' g.write(ya1ax) if zax is not None and len(zrv)>0: zax.id = 'Z' g.write(zax) if zr is not None: zr.id = 'Zrange' g.write(zr) g.presentation = ps.plottype # Note: For table output, it would be convenient to use a string-valued variable X # to specify string parts of the table. Butcdms2 doesn't support them usefully. # Instead we'll manage with a convention that a table row plotspec's id is the name of # the row, thus available to be printed in, e.g., the first column. if ps.plottype=="table row": g.rowid = ps._strid g.close() if write_xml: h.write( "<ncfile>"+filename+"</ncfile>\n" ) if write_xml: h.write( "</plotdata>\n" ) h.close()
def plan_computation(self, model, obs, varid, seasonid, plotparms): filetable1, filetable2 = self.getfts(model, obs) ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' self.computation_planned = False #check if there is data to process ft1_valid = False ft2_valid = False if filetable1 is not None and filetable2 is not None: #the filetables must contain the variables LWCF and SWCF ft10 = filetable1.find_files(self.vars[0]) ft11 = filetable1.find_files(self.vars[1]) ft20 = filetable2.find_files(self.vars[0]) ft21 = filetable2.find_files(self.vars[1]) ft1_valid = ft10 is not None and ft10!=[] and ft11 is not None and ft11!=[] ft2_valid = ft20 is not None and ft20!=[] and ft21 is not None and ft21!=[] else: logger.error("user must specify 2 data files") return None if not ft1_valid or not ft2_valid: return None VIDs = {} for season in self.seasons: for dt, ft in zip(self.datatype, self.filetables): pair = [] for var in self.vars: VID = rv.dict_id(var, season, ft) VID = id2str(VID) pair += [VID] if ft == filetable1: RF = (lambda x, vid=VID: x) else: RF = (lambda x, vid=VID: x[0]) RV = reduced_variable(variableid=var, filetable=ft, season=cdutil.times.Seasons(season), reduction_function=RF) self.reduced_variables[VID] = RV ID = dt + '_' + season VIDs[ID] = pair #create a line as a derived variable self.derived_variables = {} xline = cdms2.createVariable([0., 120.]) xline.id = 'LWCF' yline = cdms2.createVariable([0., -120.]) yline.id = 'SWCF' yline.units = 'Wm-2' self.derived_variables['LINE'] = derived_var( vid='LINE', func=create_yvsx(xline, yline, stride=1)) #inputs=[xline, yline], self.single_plotspecs = {} self.composite_plotspecs[self.plotall_id] = [] self.compositeTitle = self.vars[0] + ' vs ' + self.vars[1] SLICE = slice(0, None, 10) #return every 10th datum for plot_id in self.plot_ids: title = plot_id xVID, yVID = VIDs[plot_id] self.single_plotspecs[plot_id] = plotspec( vid=plot_id, zvars=[xVID], zfunc=(lambda x: x.flatten()[SLICE]), zrangevars={'xrange': [0., 120.]}, z2vars=[yVID], z2func=(lambda x: x.flatten()[SLICE]), z2rangevars={'yrange': [-120., 0.]}, plottype='Scatter', title=title, overplotline=False, source=', '.join([ft1src, ft2src]), plotparms=plotparms[src2modobs(ft1src)]) self.single_plotspecs['DIAGONAL_LINE'] = plotspec(vid='LINE_PS', zvars=['LINE'], zfunc=(lambda x: x), plottype='Yxvsx', zlinecolor=242, title='', overplotline=False) self.composite_plotspecs = {} plotall_id = [] for plot_id in self.plot_ids: ID = plot_id + '_with_line' self.composite_plotspecs[ID] = (plot_id, 'DIAGONAL_LINE') plotall_id += [ID] self.composite_plotspecs[self.plotall_id] = plotall_id self.computation_planned = True
def test_driver(path1, path2=None, filt2=None): """ Test driver for setting up data for plots""" # First, find and index the data files. datafiles1 = dirtree_datafiles(path1) print "jfp datafiles1=", datafiles1 datafiles2 = dirtree_datafiles(path2, filt2) print "jfp datafiles2=", datafiles2 filetable1 = basic_filetable(datafiles1) filetable2 = basic_filetable(datafiles2) # Next we'll compute reduced variables. They have generally been reduced by averaging in time, # and often more axes as well. Reducing the data first is the fastest way to compute, important # if we need to be interactive. And it is correct if whatever we plot is linear in the # variables, as is almost always the case. But if we want to plot a highly nonlinear function # of the data variables, the averaging will have to wait until later. # The reduced_variables dict names and contains all the reduced variables which we have defined. # They will be used in defining instances of plotspec. reduced_variables = { 'hyam_1': reduced_variable(variableid='hyam', filetable=filetable1, reduction_function=(lambda x, vid=None: x)), 'hybm_1': reduced_variable(variableid='hybm', filetable=filetable1, reduction_function=(lambda x, vid=None: x)), 'PS_ANN_1': reduced_variable(variableid='PS', filetable=filetable1, reduction_function=reduce2lat), 'T_CAM_ANN_1': reduced_variable(variableid='T', filetable=filetable1, reduction_function=reduce2levlat), 'T_CAM_ANN_2': reduced_variable(variableid='T', filetable=filetable2, reduction_function=reduce2levlat), 'TREFHT_ANN_latlon_Npole_1': reduced_variable(variableid='TREFHT', filetable=filetable1, reduction_function=(lambda x, vid=None: restrict_lat( reduce2latlon(x, vid=vid), 50, 90))), 'TREFHT_ANN_latlon_Npole_2': reduced_variable(variableid='TREFHT', filetable=filetable2, reduction_function=(lambda x, vid=None: restrict_lat( reduce2latlon(x, vid=vid), 50, 90))), 'TREFHT_ANN_lat_1': reduced_variable(variableid='TREFHT', filetable=filetable1, reduction_function=reduce2lat), 'TREFHT_DJF_lat_1': reduced_variable( variableid='TREFHT', filetable=filetable1, reduction_function=(lambda x, vid=None: reduce2lat_seasonal( x, seasonsDJF, vid=vid))), 'TREFHT_DJF_lat_2': reduced_variable( variableid='TREFHT', filetable=filetable2, reduction_function=(lambda x, vid=None: reduce2lat_seasonal( x, seasonsDJF, vid=vid))), 'TREFHT_DJF_latlon_1': reduced_variable( variableid='TREFHT', filetable=filetable1, reduction_function=(lambda x, vid=None: reduce2latlon_seasonal( x, seasonsDJF, vid=vid))), 'TREFHT_DJF_latlon_2': reduced_variable( variableid='TREFHT', filetable=filetable2, reduction_function=(lambda x, vid=None: reduce2latlon_seasonal( x, seasonsDJF, vid=vid))), 'TREFHT_JJA': reduced_variable( variableid='TREFHT', filetable=filetable1, reduction_function=(lambda x, vid=None: reduce2lat_seasonal( x, seasonsJJA, vid=vid))), 'PRECT_JJA_lat_1': reduced_variable( variableid='PRECT', filetable=filetable1, reduction_function=(lambda x, vid=None: reduce2lat_seasonal( x, seasonsJJA, vid=vid))), 'PRECT_JJA_lat_2': reduced_variable( variableid='PRECT', filetable=filetable2, reduction_function=(lambda x, vid=None: reduce2lat_seasonal( x, seasonsJJA, vid=vid))), # CAM variables needed for heat transport: # FSNS, FLNS, FLUT, FSNTOA, FLNT, FSNT, SHFLX, LHFLX, 'FSNS_1': reduced_variable(variableid='FSNS', filetable=filetable1, reduction_function=(lambda x, vid: x)), 'FSNS_ANN_latlon_1': reduced_variable(variableid='FSNS', filetable=filetable1, reduction_function=reduce2latlon), 'FLNS_1': reduced_variable(variableid='FLNS', filetable=filetable1, reduction_function=(lambda x, vid: x)), 'FLNS_ANN_latlon_1': reduced_variable(variableid='FLNS', filetable=filetable1, reduction_function=reduce2latlon), 'FLUT_ANN_latlon_1': reduced_variable(variableid='FLUT', filetable=filetable1, reduction_function=reduce2latlon), 'FSNTOA_ANN_latlon_1': reduced_variable(variableid='FSNTOA', filetable=filetable1, reduction_function=reduce2latlon), 'FLNT_1': reduced_variable(variableid='FLNT', filetable=filetable1, reduction_function=(lambda x, vid: x)), 'FLNT_ANN_latlon_1': reduced_variable(variableid='FLNT', filetable=filetable1, reduction_function=reduce2latlon), 'FSNT_1': reduced_variable(variableid='FSNT', filetable=filetable1, reduction_function=(lambda x, vid: x)), 'FSNT_ANN_latlon_1': reduced_variable(variableid='FSNT', filetable=filetable1, reduction_function=reduce2latlon), 'QFLX_1': reduced_variable(variableid='QFLX', filetable=filetable1, reduction_function=(lambda x, vid: x)), 'SHFLX_1': reduced_variable(variableid='SHFLX', filetable=filetable1, reduction_function=(lambda x, vid: x)), 'SHFLX_ANN_latlon_1': reduced_variable(variableid='SHFLX', filetable=filetable1, reduction_function=reduce2latlon), 'LHFLX_ANN_latlon_1': reduced_variable(variableid='LHFLX', filetable=filetable1, reduction_function=reduce2latlon), 'ORO_ANN_latlon_1': reduced_variable(variableid='ORO', filetable=filetable1, reduction_function=reduce2latlon), 'OCNFRAC_ANN_latlon_1': reduced_variable(variableid='OCNFRAC', filetable=filetable1, reduction_function=reduce2latlon), 'ts_lat_old': reduced_variable( variableid= 'surface_temperature', # normally a CF standard_name, even for non-CF data. filetable=filetable1, reduction_function=reduce2lat_old), 'ts_lat_new': reduced_variable( variableid= 'surface_temperature', # normally a CF standard_name, even for non-CF data. filetable=filetable1, reduction_function=reduce2lat # The reduction function will take just one argument, a variable (MV). But it might # be expressed here as a lambda wrapping a more general function. # Often there will be ranges in time, space, etc. specified here. No range means # everything. ), 'ts_scalar_tropical_o': reduced_variable( variableid='surface_temperature', filetable=filetable1, reduction_function=(lambda mv, vid=None: reduce2scalar_zonal_old( mv, -20, 20, vid=vid))), 'ts_scalar_tropical_n': reduced_variable( variableid='surface_temperature', filetable=filetable1, reduction_function=(lambda mv, vid=None: reduce2scalar_zonal( mv, -20, 20, vid=vid))) } # Derived variables have to be treated separately from reduced variables # because derived variables generally depend on reduced variables. # But N.B.: the dicts reduced_variables and derived_variables # must never use the same key! derived_variables = { 'CAM_HEAT_TRANSPORT_ALL_1': derived_var(vid='CAM_HEAT_TRANSPORT_ALL_1', inputs=[ 'FSNS_ANN_latlon_1', 'FLNS_ANN_latlon_1', 'FLUT_ANN_latlon_1', 'FSNTOA_ANN_latlon_1', 'FLNT_ANN_latlon_1', 'FSNT_ANN_latlon_1', 'SHFLX_ANN_latlon_1', 'LHFLX_ANN_latlon_1', 'OCNFRAC_ANN_latlon_1' ], outputs=[ 'atlantic_heat_transport', 'pacific_heat_transport', 'indian_heat_transport', 'global_heat_transport' ], func=oceanic_heat_transport), 'NCEP_OBS_HEAT_TRANSPORT_ALL_2': derived_var(vid='NCEP_OBS_HEAT_TRANSPORT_ALL_2', inputs=[], outputs=('latitude', [ 'atlantic_heat_transport', 'pacific_heat_transport', 'indian_heat_transport', 'global_heat_transport' ]), func=(lambda: ncep_ocean_heat_transport(path2))), 'T_ANN_1': derived_var(vid='T_ANN_1', inputs=[ 'T_CAM_ANN_1', 'hyam_1', 'hybm_1', 'PS_ANN_1', 'T_CAM_ANN_2' ], outputs=('temperature'), func=verticalize) } plotvars = dict(reduced_variables.items() + derived_variables.items()) # The plotvspecs dict names and contains all plotspec objects which we have defined. # The plotspeckeys variable, below, names the ones for which we will generate output. # A dict value can be a plotspec object, or a list of such objects. A list of # plotspec instances specifies a page containing multiple plots in separate panes. plotspecs = { 'TREFHT_ANN_Npole_ALL': ['TREFHT_ANN_Npole_1', 'TREFHT_ANN_Npole_2', 'TREFHT_ANN_Npole_diff'], 'TREFHT_ANN_Npole_1': plotspec(vid='TREFHT_ANN_Npole_1', xvars=['TREFHT_ANN_latlon_Npole_1'], xfunc=lonvar, yvars=['TREFHT_ANN_latlon_Npole_1'], yfunc=latvar, zvars=['TREFHT_ANN_latlon_Npole_1'], zfunc=(lambda z: z), plottype='polar contour plot'), 'TREFHT_ANN_Npole_2': plotspec(vid='TREFHT_ANN_Npole_2', xvars=['TREFHT_ANN_latlon_Npole_2'], xfunc=lonvar, yvars=['TREFHT_ANN_latlon_Npole_2'], yfunc=latvar, zvars=['TREFHT_ANN_latlon_Npole_2'], zfunc=(lambda z: z), plottype='polar contour plot'), 'TREFHT_ANN_Npole_diff': plotspec( vid='TREFHT_ANN_Npole_diff', xvars=['TREFHT_ANN_latlon_Npole_1', 'TREFHT_ANN_latlon_Npole_2'], xfunc=lonvar_min, yvars=['TREFHT_ANN_latlon_Npole_1', 'TREFHT_ANN_latlon_Npole_2'], yfunc=latvar_min, zvars=['TREFHT_ANN_latlon_Npole_1', 'TREFHT_ANN_latlon_Npole_2'], zfunc=aminusb_2ax, plottype='polar contour plot'), 'TREFHT_DJF_laton_ALL': [ 'TREFHT_DJF_latlon_1', 'TREFHT_DJF_latlon_2', 'TREFHT_DJF_latlon_diff' ], 'TREFHT_DJF_latlon_1': plotspec(vid='TREFHT_DJF_latlon_1', xvars=['TREFHT_DJF_latlon_1'], xfunc=lonvar, yvars=['TREFHT_DJF_latlon_1'], yfunc=latvar, zvars=['TREFHT_DJF_latlon_1'], zfunc=(lambda z: z), plottype='contour plot'), 'TREFHT_DJF_latlon_2': plotspec(vid='TREFHT_DJF_latlon_2', xvars=['TREFHT_DJF_latlon_2'], xfunc=lonvar, yvars=['TREFHT_DJF_latlon_2'], yfunc=latvar, zvars=['TREFHT_DJF_latlon_2'], zfunc=(lambda z: z), plottype='contour plot'), 'TREFHT_DJF_latlon_diff': plotspec(vid='TREFHT_DJF_latlon_diff', xvars=['TREFHT_DJF_latlon_1', 'TREFHT_DJF_latlon_2'], xfunc=lonvar_min, yvars=['TREFHT_DJF_latlon_1', 'TREFHT_DJF_latlon_2'], yfunc=latvar_min, zvars=['TREFHT_DJF_latlon_1', 'TREFHT_DJF_latlon_2'], zfunc=aminusb_2ax, plottype='contour_plot'), 'T_ANN_VERT_CAM_OBS_ALL': ['T_VERT_ANN_1', 'T_VERT_ANN_2', 'T_VERT_difference'], 'T_VERT_difference': plotspec(vid='T_VERT_difference', xvars=['T_ANN_1', 'T_CAM_ANN_2'], xfunc=latvar_min, yvars=['T_ANN_1', 'T_CAM_ANN_2'], yfunc=levvar_min, ya1vars=['T_ANN_1', 'T_CAM_ANN_2'], ya1func=(lambda y1, y2: heightvar(levvar_min(y1, y2))), zvars=['T_ANN_1', 'T_CAM_ANN_2'], zfunc=aminusb_ax2, plottype="contour plot"), 'T_VERT_ANN_2': plotspec(vid='T_ANN_2', xvars=['T_CAM_ANN_2'], xfunc=latvar, yvars=['T_CAM_ANN_2'], yfunc=levvar, ya1vars=['T_CAM_ANN_2'], ya1func=heightvar, zvars=['T_CAM_ANN_2'], plottype='contour plot', zrangevars=['T_ANN_1', 'T_CAM_ANN_2'], zrangefunc=minmin_maxmax), 'T_VERT_ANN_1': plotspec(vid='T_ANN_1', xvars=['T_ANN_1'], xfunc=latvar, yvars=['T_ANN_1'], yfunc=levvar, ya1vars=['T_ANN_1'], ya1func=heightvar, zvars=['T_ANN_1'], plottype='contour plot', zrangevars=['T_ANN_1', 'T_CAM_ANN_2'], zrangefunc=minmin_maxmax), 'NCEP_OBS_HEAT_TRANSPORT_GLOBAL_2': plotspec(vid='NCEP_OBS_HEAT_TRANSPORT_GLOBAL_2', xvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], xfunc=(lambda x: x[0]), yvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], yfunc=(lambda y: y[1][3]), plottype='line plot'), 'NCEP_OBS_HEAT_TRANSPORT_PACIFIC_2': plotspec(vid='NCEP_OBS_HEAT_TRANSPORT_PACIFIC_2', xvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], xfunc=(lambda x: x[0]), yvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], yfunc=(lambda y: y[1][0]), plottype='line plot'), 'NCEP_OBS_HEAT_TRANSPORT_ATLANTIC_2': plotspec(vid='NCEP_OBS_HEAT_TRANSPORT_ATLANTIC_2', xvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], xfunc=(lambda x: x[0]), yvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], yfunc=(lambda y: y[1][1]), plottype='line plot'), 'NCEP_OBS_HEAT_TRANSPORT_INDIAN_2': plotspec(vid='NCEP_OBS_HEAT_TRANSPORT_INDIAN_2', xvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], xfunc=(lambda x: x[0]), yvars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], yfunc=(lambda y: y[1][2]), plottype='line plot'), 'CAM_HEAT_TRANSPORT_GLOBAL_1': plotspec(vid='CAM_HEAT_TRANSPORT_GLOBAL_1', xvars=['FSNS_ANN_latlon_1'], xfunc=latvar, yvars=['CAM_HEAT_TRANSPORT_ALL_1'], yfunc=(lambda y: y[3]), plottype='line plot'), 'CAM_HEAT_TRANSPORT_PACIFIC_1': plotspec(vid='CAM_HEAT_TRANSPORT_PACIFIC_1', xvars=['FSNS_ANN_latlon_1'], xfunc=latvar, yvars=['CAM_HEAT_TRANSPORT_ALL_1'], yfunc=(lambda y: y[0]), plottype='line plot'), 'CAM_HEAT_TRANSPORT_ATLANTIC_1': plotspec(vid='CAM_HEAT_TRANSPORT_ATLANTIC_1', xvars=['FSNS_ANN_latlon_1'], xfunc=latvar, yvars=['CAM_HEAT_TRANSPORT_ALL_1'], yfunc=(lambda y: y[1]), plottype='line plot'), 'CAM_HEAT_TRANSPORT_INDIAN_1': plotspec(vid='CAM_HEAT_TRANSPORT_INDIAN_1', xvars=['FSNS_ANN_latlon_1'], xfunc=latvar, yvars=['CAM_HEAT_TRANSPORT_ALL_1'], yfunc=(lambda y: y[2]), plottype='line plot'), 'CAM_HEAT_TRANSPORT_ALL_1': [ 'CAM_HEAT_TRANSPORT_GLOBAL_1', 'CAM_HEAT_TRANSPORT_PACIFIC_1', 'CAM_HEAT_TRANSPORT_ATLANTIC_1', 'CAM_HEAT_TRANSPORT_INDIAN_1' ], 'CAM_NCEP_HEAT_TRANSPORT_GLOBAL': plotspec(vid='CAM_NCEP_HEAT_TRANSPORT_GLOBAL', x1vars=['FSNS_ANN_latlon_1'], x1func=latvar, y1vars=['CAM_HEAT_TRANSPORT_ALL_1'], y1func=(lambda y: y[3]), x2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], x2func=(lambda x: x[0]), y2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], y2func=(lambda y: y[1][3]), plottype='2 line plot'), 'CAM_NCEP_HEAT_TRANSPORT_PACIFIC': plotspec(vid='CAM_NCEP_HEAT_TRANSPORT_PACIFIC', x1vars=['FSNS_ANN_latlon_1'], x1func=latvar, y1vars=['CAM_HEAT_TRANSPORT_ALL_1'], y1func=(lambda y: y[0]), x2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], x2func=(lambda x: x[0]), y2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], y2func=(lambda y: y[1][0]), plottype='2 line plot'), 'CAM_NCEP_HEAT_TRANSPORT_ATLANTIC': plotspec(vid='CAM_NCEP_HEAT_TRANSPORT_ATLANTIC', x1vars=['FSNS_ANN_latlon_1'], x1func=latvar, y1vars=['CAM_HEAT_TRANSPORT_ALL_1'], y1func=(lambda y: y[0]), x2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], x2func=(lambda x: x[0]), y2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], y2func=(lambda y: y[1][1]), plottype='2 line plot'), 'CAM_NCEP_HEAT_TRANSPORT_INDIAN': plotspec(vid='CAM_NCEP_HEAT_TRANSPORT_INDIAN', x1vars=['FSNS_ANN_latlon_1'], x1func=latvar, y1vars=['CAM_HEAT_TRANSPORT_ALL_1'], y1func=(lambda y: y[0]), x2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], x2func=(lambda x: x[0]), y2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], y2func=(lambda y: y[1][2]), plottype='2 line plot'), 'CAM_NCEP_HEAT_TRANSPORT_ALL': [ 'CAM_NCEP_HEAT_TRANSPORT_GLOBAL', 'CAM_NCEP_HEAT_TRANSPORT_PACIFIC', 'CAM_NCEP_HEAT_TRANSPORT_ATLANTIC', 'CAM_NCEP_HEAT_TRANSPORT_INDIAN' ], 'past_CAM_HEAT_TRANSPORT_GLOBAL_1': plotspec(vid='CAM_HEAT_TRANSPORT_GLOBAL_1', xvars=['FSNS_ANN_latlon_1'], xfunc=latvar, yvars=[ 'FSNS_ANN_latlon_1', 'FLNS_ANN_latlon_1', 'FLUT_ANN_latlon_1', 'FSNTOA_ANN_latlon_1', 'FLNT_ANN_latlon_1', 'FSNT_ANN_latlon_1', 'SHFLX_ANN_latlon_1', 'LHFLX_ANN_latlon_1', 'OCNFRAC_ANN_latlon_1' ], yfunc=oceanic_heat_transport, plottype='line plot'), 'PRECT_JJA': ['PRECT_JJA_2line', 'PRECT_JJA_diff'], 'PRECT_JJA_2line': plotspec(vid='PRECT_JJA_2line', x1vars=['PRECT_JJA_lat_1'], x1func=latvar, x2vars=['PRECT_JJA_lat_2'], x2func=latvar, y1vars=['PRECT_JJA_lat_1'], y1func=(lambda y: y), y2vars=['PRECT_JJA_lat_2'], y2func=(lambda y: y), plottype='2-line plot'), 'PRECT_JJA_diff': plotspec( vid='PRECT_JJA_difference', xvars=['PRECT_JJA_lat_1', 'PRECT_JJA_lat_2'], xfunc=latvar_min, yvars=['PRECT_JJA_lat_1', 'PRECT_JJA_lat_2'], yfunc= aminusb_1ax, # aminusb_1ax(y1,y2)=y1-y2; each y has 1 axis, use min axis plottype='line plot'), 'TREFHT_ANN': plotspec(vid='TREFHT_ANN', xvars=['TREFHT_ANN_lat_1'], xfunc=latvar, yvars=['TREFHT_ANN_lat_1'], yfunc=(lambda y: y), plottype='line plot'), 'TREFHT_DJF': ['TREFHT_DJF_2line', 'TREFHT_DJF_diff'], 'TREFHT_DJF_2line': plotspec(vid='TREFHT_DJF_2line', x1vars=['TREFHT_DJF_lat_1'], x1func=latvar, x2vars=['TREFHT_DJF_lat_2'], x2func=latvar, y1vars=['TREFHT_DJF_lat_1'], y1func=(lambda y: y), y2vars=['TREFHT_DJF_lat_2'], y2func=(lambda y: y), plottype='2-line plot'), 'TREFHT_DJF_diff': plotspec( vid='TREFHT_DJF_diff', xvars=['TREFHT_DJF_lat_1', 'TREFHT_DJF_lat_2'], xfunc=latvar_min, yvars=['TREFHT_DJF_lat_1', 'TREFHT_DJF_lat_2'], yfunc= aminusb_1ax, # aminusb_1ax(y1,y2)=y1-y2; each y has 1 axis, use min axis plottype='line plot'), 'TREFHT_DJF_line': plotspec(vid='TREFHT_DJF_line', xvars=['TREFHT_DJF_lat_1'], xfunc=latvar, yvars=['TREFHT_DJF_lat_1'], plottype='line plot'), 'TREFHT_DJF_contour': plotspec(vid='TREFHT_DJF_contour', xvars=['TREFHT_DJF_latlon_1'], xfunc=(lambda x: x), plottype='line plot'), #plotspec( vid='TREFHT_JJA',xvars=['TREFHT_JJA'], xfiletable=filetable1, xfunc = latvar, # yvars=['TREFHT_JJA'], yfunc=(lambda y: y), plottype='line plot'), #plotspec( # vid='ts_by_lat_old', # suitable for filenames # xfiletable=filetable1, # xfunc = latvar, # function to return x axis values # xvars = ['ts_lat_old'], # names of variables or axes, args of xfunc # yfiletable=filetable1, # can differ from xfiletable, e.g. comparing 2 runs # yfunc = (lambda y: y), # function to return y axis values # yvars = ['ts_lat_old'], # names of variables or axes, args of xfunc # zfiletable=filetable1, # zfunc = (lambda: None), # zvars = [], # would be needed for countour or 3D plot # # ... the ?vars variable will be converted (using the filetable and # # plotvars) to actual variables which become the arguments for a call # # of ?func, which returns the data we write out for plotting use. # plottype='line plot' ), 'ts_by_lat': plotspec( vid='ts_by_lat', # suitable for filenames xfunc=latvar, # function to return x axis values xvars=['ts_lat_new'], # names of variables or axes, args of xfunc yfunc=(lambda y: y), # function to return y axis values yvars=['ts_lat_new'], # names of variables or axes, args of xfunc zfunc=(lambda: None), zvars=[], # would be needed for countour or 3D plot # ... the ?vars variable will be converted (using the filetable and # plotvars) to actual variables which become the arguments for a call # of ?func, which returns the data we write out for plotting use. plottype='line plot'), #plotspec( vid="ts_global_old",xvars=['ts_scalar_tropical_o'], xfiletable=filetable1 ), 'ts_global': plotspec(vid="ts_global", xvars=['ts_scalar_tropical_n']), } # Plotspeckeys specifies what plot data we will compute and write out. # In the future we may add a command line option, or provide other ways to # define plotspeckeys. #plotspeckeys = [['TREFHT_DJF_2line','TREFHT_DJF_difference']] #plotspeckeys = ['TREFHT_DJF_2line'] #plotspeckeys = ['NCEP_OBS_HEAT_TRANSPORT_GLOBAL_2','CAM_HEAT_TRANSPORT_ALL_1'] #plotspeckeys = ['CAM_NCEP_HEAT_TRANSPORT_GLOBAL'] #plotspeckeys = ['CAM_NCEP_HEAT_TRANSPORT_ALL'] #plotspeckeys = ['T_ANN_VERT_CAM_OBS_ALL'] #plotspeckeys = ['TREFHT_DJF_laton_ALL'] #plotspeckeys = ['TREFHT_ANN_Npole_ALL'] plotspeckeys = ['TREFHT_DJF'] #plotspeckeys = ['GLOBAL_AVERAGES'] # Find the variable names required by the plotspecs. varkeys = [] for psk in plotspeckeys: if type(psk) is str and type(plotspecs[psk]) is list: psk = plotspecs[psk] if type(psk) is str: write_xml = False psl = [plotspecs[psk]] else: write_xml = True psl = [plotspecs[k] for k in psk] xml_name = '_'.join([ps._strid for ps in psl]) + '.xml' h = open(xml_name, 'w') h.write("<plotdata>\n") for ps in psl: varkeys = varkeys + ps.xvars + ps.x1vars + ps.x2vars + ps.x3vars varkeys = varkeys + ps.yvars + ps.y1vars + ps.y2vars + ps.y3vars varkeys = varkeys + ps.zvars + ps.zrangevars for key in varkeys: if key in derived_variables.keys(): varkeys = varkeys + derived_variables[key]._inputs varkeys = list(set(varkeys)) # Compute the value of every variable we need. varvals = {} # First compute all the reduced variables for key in varkeys: if key in reduced_variables.keys(): varvals[key] = reduced_variables[key].reduce() # Then use the reduced variables to compute the derived variables # Note that the derive() method is allowed to return a tuple. This way # we can use one function to compute what's really several variables. for key in varkeys: if key in derived_variables.keys(): varvals[key] = derived_variables[key].derive(varvals) # Now use the reduced and derived variables to compute the plot data. for psk in plotspeckeys: if type(psk) is str and type(plotspecs[psk]) is list: psk = plotspecs[psk] if type(psk) is str: write_xml = False psl = [plotspecs[psk]] else: write_xml = True psl = [plotspecs[k] for k in psk] xml_name = '_'.join([ps._strid for ps in psl]) + '.xml' h = open(xml_name, 'w') h.write("<plotdata>\n") varkeys = [] for ps in psl: print "jfp preparing data for", ps._strid xrv = [varvals[k] for k in ps.xvars] x1rv = [varvals[k] for k in ps.x1vars] x2rv = [varvals[k] for k in ps.x2vars] x3rv = [varvals[k] for k in ps.x3vars] yrv = [varvals[k] for k in ps.yvars] y1rv = [varvals[k] for k in ps.y1vars] y2rv = [varvals[k] for k in ps.y2vars] y3rv = [varvals[k] for k in ps.y3vars] yarv = [varvals[k] for k in ps.yavars] ya1rv = [varvals[k] for k in ps.ya1vars] zrv = [varvals[k] for k in ps.zvars] zrrv = [varvals[k] for k in ps.zrangevars] xax = apply(ps.xfunc, xrv) x1ax = apply(ps.x1func, x1rv) x2ax = apply(ps.x2func, x2rv) x3ax = apply(ps.x3func, x3rv) yax = apply(ps.yfunc, yrv) y1ax = apply(ps.y1func, y1rv) y2ax = apply(ps.y2func, y2rv) y3ax = apply(ps.y3func, y3rv) yaax = apply(ps.yafunc, yarv) ya1ax = apply(ps.ya1func, ya1rv) zax = apply(ps.zfunc, zrv) zr = apply(ps.zrangefunc, zrrv) if (xax is None or len(xrv)==0) and (x1ax is None or len(x1rv)==0)\ and (x2ax is None or len(x2rv)==0) and (x3ax is None or len(x3rv)==0)\ and (yax is None or len(yrv)==0) and (y1ax is None or len(y1rv)==0)\ and (y2ax is None or len(y2rv)==0) and (y3ax is None or len(y3rv)==0)\ and (zax is None or len(zrv)==0): continue filename = ps._strid + "_test.nc" g = cdms2.open(filename, 'w') # later, choose a better name and a path! # Much more belongs in g, e.g. axis and graph names. if xax is not None and len(xrv) > 0: xax.id = 'X' g.write(xax) if x1ax is not None and len(x1rv) > 0: x1ax.id = 'X1' g.write(x1ax) if x2ax is not None and len(x2rv) > 0: x2ax.id = 'X2' g.write(x2ax) if x3ax is not None and len(x3rv) > 0: x3ax.id = 'X3' g.write(x3ax) if yax is not None and len(yrv) > 0: yax.id = 'Y' g.write(yax) if y1ax is not None and len(y1rv) > 0: y1ax.id = 'Y1' g.write(y1ax) if y2ax is not None and len(y2rv) > 0: y2ax.id = 'Y2' g.write(y2ax) if y3ax is not None and len(y3rv) > 0: y3ax.id = 'Y3' g.write(y3ax) if yaax is not None and len(yarv) > 0: yaax.id = 'YA' g.write(yaax) if ya1ax is not None and len(ya1rv) > 0: ya1ax.id = 'YA1' g.write(ya1ax) if zax is not None and len(zrv) > 0: zax.id = 'Z' g.write(zax) if zr is not None: zr.id = 'Zrange' g.write(zr) g.presentation = ps.plottype # Note: For table output, it would be convenient to use a string-valued variable X # to specify string parts of the table. Butcdms2 doesn't support them usefully. # Instead we'll manage with a convention that a table row plotspec's id is the name of # the row, thus available to be printed in, e.g., the first column. if ps.plottype == "table row": g.rowid = ps._strid g.close() if write_xml: h.write("<ncfile>" + filename + "</ncfile>\n") if write_xml: h.write("</plotdata>\n") h.close()
def plan_computation(self, model, obs, varid, seasonid, region=None, aux=slice(0, None), names={}, plotparms=None): """Set up for a lat-lon polar contour plot. Data is averaged over all other axes. """ filetable1, filetable2 = self.getfts(model, obs) ft1src = filetable1.source() if region is not None: regname = str(region) else: regname = None try: ft2src = filetable2.source() except: ft2src = '' reduced_varlis = [ reduced_variable( variableid=varid, filetable=filetable1, season=self.season, region=regname, reduction_function=(lambda x, vid, region=regname, aux1=aux: reduce2latlon_seasonal(x( latitude=aux1, longitude=(0, 360)), self.season, region, vid=vid))), reduced_variable( variableid=varid, filetable=filetable2, season=self.season, region=regname, reduction_function=(lambda x, vid, region=regname, aux1=aux: reduce2latlon_seasonal(x( latitude=aux1, longitude=(0, 360)), self.season, region, vid=vid))) ] self.reduced_variables = {v.id(): v for v in reduced_varlis} vid1 = rv.dict_id(varid, seasonid, filetable1, region=regname) vid2 = rv.dict_id(varid, seasonid, filetable2, region=regname) self.derived_variables = {} self.single_plotspecs = { self.plot1_id: plotspec(vid=ps.dict_idid(vid1), zvars=[vid1], zfunc=(lambda z: z), plottype=self.plottype, source=names.get('model', ft1src), file_descr='model', plotparms=plotparms[src2modobs(ft1src)]), self.plot2_id: plotspec(vid=ps.dict_idid(vid2), zvars=[vid2], zfunc=(lambda z: z), plottype=self.plottype, source=names.get('obs', ft2src), file_descr='obs', plotparms=plotparms[src2obsmod(ft2src)]), self.plot3_id: plotspec(vid=ps.dict_id(varid, 'diff', seasonid, filetable1, filetable2), zvars=[vid1, vid2], zfunc=aminusb_2ax, plottype=self.plottype, source=', '.join([ names.get('model', ft1src), names.get('obs', ft2src) ]), file_descr='diff', plotparms=plotparms['diff']) } self.composite_plotspecs = { self.plotall_id: [self.plot1_id, self.plot2_id, self.plot3_id] } self.computation_planned = True
def plan_computation(self, model, obs, varid, seasonid, plotparms): filetable1, filetable2 = self.getfts(model, obs) ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' self.computation_planned = False #check if there is data to process ft1_valid = False ft2_valid = False if filetable1 is not None and filetable2 is not None: ft1 = filetable1.find_files(varid) ft2 = filetable2.find_files(varid) ft1_valid = ft1 is not None and ft1 != [ ] # true iff filetable1 uses hybrid level coordinates ft2_valid = ft2 is not None and ft2 != [ ] # true iff filetable2 uses hybrid level coordinates else: logger.error("user must specify 2 data files") return None if not ft1_valid or not ft2_valid: return None #generate identifiers vid1 = rv.dict_id(varid, self._s1, filetable1) vid2 = rv.dict_id(varid, self._s2, filetable2) vid3 = dv.dict_id(varid, 'SeasonalDifference', self._seasonid, filetable1) #, ft2=filetable2) #setup the reduced variables vid1_season = cdutil.times.Seasons(self._s1) if vid1_season is None: vid1_season = seasonsyr vid2_season = cdutil.times.Seasons(self._s2) if vid2_season is None: vid2_season = seasonsyr rv_1 = reduced_variable( variableid=varid, filetable=filetable1, season=vid1_season, reduction_function=(lambda x, vid=vid1: reduce2latlon_seasonal( x, vid1_season, self.region, vid=vid))) rv_2 = reduced_variable( variableid=varid, filetable=filetable2, season=vid2_season, reduction_function=(lambda x, vid=vid2: reduce2latlon_seasonal( x, vid2_season, self.region, vid=vid))) self.reduced_variables = {rv_1.id(): rv_1, rv_2.id(): rv_2} #create the derived variable which is the difference self.derived_variables = {} self.derived_variables[vid3] = derived_var(vid=vid3, inputs=[vid1, vid2], func=aminusb_2ax) self.single_plotspecs = { self.plot1_id: plotspec(vid=ps.dict_idid(vid1), zvars=[vid1], zfunc=(lambda z: z), plottype=self.plottype, source=ft1src, file_descr='model', plotparms=plotparms[src2modobs(ft1src)]), self.plot2_id: plotspec(vid=ps.dict_idid(vid2), zvars=[vid2], zfunc=(lambda z: z), plottype=self.plottype, source=ft2src, file_descr='obs', plotparms=plotparms[src2obsmod(ft2src)]), self.plot3_id: plotspec(vid=ps.dict_idid(vid3), zvars=[vid3], zfunc=(lambda x: x), plottype=self.plottype, source=', '.join([ft1src, ft2src]), file_descr='diff', plotparms=plotparms['diff']) } self.composite_plotspecs = { self.plotall_id: [self.plot1_id, self.plot2_id, self.plot3_id] } # ...was self.composite_plotspecs = { self.plotall_id: self.single_plotspecs.keys() } self.computation_planned = True
def plan_computation(self, model, obs, varid, seasonid, plotparms): filetable1, filetable2 = self.getfts(model, obs) # CAM variables needed for heat transport: (SOME ARE SUPERFLUOUS <<<<<<) # FSNS, FLNS, FLUT, FSNTOA, FLNT, FSNT, SHFLX, LHFLX, self.reduced_variables = { 'FSNS_1': reduced_variable(variableid='FSNS', filetable=filetable1, season=self.season, reduction_function=(lambda x, vid: x)), 'FSNS_ANN_latlon_1': reduced_variable(variableid='FSNS', filetable=filetable1, season=self.season, reduction_function=reduce2latlon), 'FLNS_1': reduced_variable(variableid='FLNS', filetable=filetable1, season=self.season, reduction_function=(lambda x, vid: x)), 'FLNS_ANN_latlon_1': reduced_variable(variableid='FLNS', filetable=filetable1, season=self.season, reduction_function=reduce2latlon), 'FLUT_ANN_latlon_1': reduced_variable(variableid='FLUT', filetable=filetable1, season=self.season, reduction_function=reduce2latlon), 'FSNTOA_ANN_latlon_1': reduced_variable(variableid='FSNTOA', filetable=filetable1, season=self.season, reduction_function=reduce2latlon), 'FLNT_1': reduced_variable(variableid='FLNT', filetable=filetable1, reduction_function=(lambda x, vid: x)), 'FLNT_ANN_latlon_1': reduced_variable(variableid='FLNT', filetable=filetable1, season=self.season, reduction_function=reduce2latlon), 'FSNT_1': reduced_variable(variableid='FSNT', filetable=filetable1, season=self.season, reduction_function=(lambda x, vid: x)), 'FSNT_ANN_latlon_1': reduced_variable(variableid='FSNT', filetable=filetable1, season=self.season, reduction_function=reduce2latlon), 'QFLX_1': reduced_variable(variableid='QFLX', filetable=filetable1, reduction_function=(lambda x, vid: x)), 'SHFLX_1': reduced_variable(variableid='SHFLX', filetable=filetable1, season=self.season, reduction_function=(lambda x, vid: x)), 'SHFLX_ANN_latlon_1': reduced_variable(variableid='SHFLX', filetable=filetable1, season=self.season, reduction_function=reduce2latlon), 'LHFLX_ANN_latlon_1': reduced_variable(variableid='LHFLX', filetable=filetable1, season=self.season, reduction_function=reduce2latlon), 'OCNFRAC_ANN_latlon_1': reduced_variable(variableid='OCNFRAC', filetable=filetable1, season=self.season, reduction_function=reduce2latlon) } self.derived_variables = { 'CAM_HEAT_TRANSPORT_ALL_1': derived_var(vid='CAM_HEAT_TRANSPORT_ALL_1', inputs=[ 'FSNS_ANN_latlon_1', 'FLNS_ANN_latlon_1', 'FLUT_ANN_latlon_1', 'FSNTOA_ANN_latlon_1', 'FLNT_ANN_latlon_1', 'FSNT_ANN_latlon_1', 'SHFLX_ANN_latlon_1', 'LHFLX_ANN_latlon_1', 'OCNFRAC_ANN_latlon_1' ], outputs=[ 'atlantic_heat_transport', 'pacific_heat_transport', 'indian_heat_transport', 'global_heat_transport' ], func=oceanic_heat_transport), 'NCEP_OBS_HEAT_TRANSPORT_ALL_2': derived_var(vid='NCEP_OBS_HEAT_TRANSPORT_ALL_2', inputs=[], outputs=('latitude', [ 'atlantic_heat_transport', 'pacific_heat_transport', 'indian_heat_transport', 'global_heat_transport' ]), func=(lambda: ncep_ocean_heat_transport(filetable2))) } ft1src = filetable1.source() try: ft2src = filetable2.source() except: ft2src = '' self.single_plotspecs = { 'CAM_NCEP_HEAT_TRANSPORT_GLOBAL': plotspec(vid='CAM_NCEP_HEAT_TRANSPORT_GLOBAL', zvars=['CAM_HEAT_TRANSPORT_ALL_1'], zfunc=(lambda y: y[3]), z2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], z2func=(lambda z: z[1][3]), plottype=self.plottype, title='CAM and NCEP HEAT_TRANSPORT GLOBAL', file_descr='', source=ft1src, more_id='Global', plotparms=plotparms[src2modobs(ft1src)]), 'CAM_NCEP_HEAT_TRANSPORT_PACIFIC': plotspec(vid='CAM_NCEP_HEAT_TRANSPORT_PACIFIC', zvars=['CAM_HEAT_TRANSPORT_ALL_1'], zfunc=(lambda y: y[0]), z2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], z2func=(lambda y: y[1][0]), plottype=self.plottype, title='CAM and NCEP HEAT_TRANSPORT PACIFIC', file_descr='', source=ft1src, more_id='Pacific', plotparms=plotparms[src2modobs(ft1src)]), 'CAM_NCEP_HEAT_TRANSPORT_ATLANTIC': plotspec(vid='CAM_NCEP_HEAT_TRANSPORT_ATLANTIC', zvars=['CAM_HEAT_TRANSPORT_ALL_1'], zfunc=(lambda y: y[1]), z2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], z2func=(lambda y: y[1][1]), plottype=self.plottype, title='CAM and NCEP HEAT_TRANSPORT ATLANTIC', file_descr='', source=ft1src, more_id='Atlantic', plotparms=plotparms[src2modobs(ft1src)]), 'CAM_NCEP_HEAT_TRANSPORT_INDIAN': plotspec(vid='CAM_NCEP_HEAT_TRANSPORT_INDIAN', zvars=['CAM_HEAT_TRANSPORT_ALL_1'], zfunc=(lambda y: y[2]), z2vars=['NCEP_OBS_HEAT_TRANSPORT_ALL_2'], z2func=(lambda y: y[1][2]), plottype=self.plottype, title='CAM and NCEP HEAT_TRANSPORT INDIAN', file_descr='', source=ft1src, more_id='Indian', plotparms=plotparms[src2modobs(ft1src)]), } self.composite_plotspecs = { 'CAM_NCEP_HEAT_TRANSPORT_ALL': [ 'CAM_NCEP_HEAT_TRANSPORT_GLOBAL', 'CAM_NCEP_HEAT_TRANSPORT_PACIFIC', 'CAM_NCEP_HEAT_TRANSPORT_ATLANTIC', 'CAM_NCEP_HEAT_TRANSPORT_INDIAN' ] } self.computation_planned = True