def testYears(self): f = cdms2.open( os.path.join(cdat_info.get_sampledata_path(), 'th_yr.nc')) th = f('th', time=slice(-3, None, 1)) t = th.getTime() cdutil.setTimeBoundsYearly(t) self.assertEqual(th.shape, (3, 64, 128)) self.assertTrue( numpy.equal(th.getTime().getBounds()[0], [1997., 1998.]).all()) dep = cdutil.YEAR.departures(th, statusbar=None) self.assertEqual(dep.shape, (3, 64, 128))
def setTimeBounds( self, var ): time_axis = var.getTime() if time_axis._bounds_ == None: try: time_unit = time_axis.units.split(' since ')[0].strip() if time_unit == 'hours': values = time_axis.getValue() freq = 24/( values[1]-values[0] ) cdutil.setTimeBoundsDaily( time_axis, freq ) elif time_unit == 'days': cdutil.setTimeBoundsDaily( time_axis ) elif time_unit == 'months': cdutil.setTimeBoundsMonthly( time_axis ) elif time_unit == 'years': cdutil.setTimeBoundsYearly( time_axis ) except Exception, err: wpsLog.debug( "Exception in setTimeBounds:\n " + traceback.format_exc() )
#!/usr/bin/env python # Adapted for numpy/ma/cdms2 by convertcdms.py import cdms2,cdutil,sys,os,numpy cdms2.setAutoBounds('on') f = cdms2.open(os.path.join(sys.prefix,'sample_data','th_yr.nc')) th=f('th',time=slice(-3,None,1)) t=th.getTime() cdutil.setTimeBoundsYearly(t) assert(th.shape==(3,64,128)) assert(numpy.equal(th.getTime().getBounds()[0],[1997.,1998.]).all()) dep=cdutil.YEAR.departures(th,statusbar=None) assert(dep.shape==(3, 64, 128))
#!/usr/bin/env python # Adapted for numpy/ma/cdms2 by convertcdms.py import cdms2,cdutil,sys,os,numpy,cdat_info cdms2.setAutoBounds('on') f = cdms2.open(os.path.join(cdat_info.get_sampledata_path(),'th_yr.nc')) th=f('th',time=slice(-3,None,1)) t=th.getTime() cdutil.setTimeBoundsYearly(t) assert(th.shape==(3,64,128)) assert(numpy.equal(th.getTime().getBounds()[0],[1997.,1998.]).all()) dep=cdutil.YEAR.departures(th,statusbar=None) assert(dep.shape==(3, 64, 128))
def pdsi_time_series(D,start_time,stop_time,SM=None,best_fit=True,aerosols=False,use_tree=True): """ plot time series of PDSI data between start_time and stop_time""" if aerosols: aerosol_start = cdtime.comptime(1950,1,1) aerosol_stop = cdtime.comptime(1975,12,31) cru_p=D.ALL.project_cru_on_solver(start=start_time,solver=truncated_solver(D,aerosol_start,aerosol_stop,include_cru=True)) dai_p=D.ALL.project_dai_on_solver(start=start_time,solver=truncated_solver(D,aerosol_start,aerosol_stop,include_dai=True)) cdutil.setTimeBoundsYearly(dai_p) tree_p=D.ALL.get_tree_ring_projection(solver=truncated_solver(D,aerosol_start,aerosol_stop)) else: cru_p=D.ALL.project_cru_on_solver(start=start_time) dai_p=D.ALL.project_dai_on_solver(start=start_time) cdutil.setTimeBoundsYearly(dai_p) tree_p=D.ALL.projection x=np.arange(start_time.year,stop_time.year+1) if use_tree: cdutil.setTimeBoundsYearly(tree_p) ytree=tree_p(time=(start_time,stop_time,"oob")).anom() Plotting.time_plot(ytree,color=get_dataset_color("tree"),label="Tree ring reconstructions",lw=1) xtree=x[:len(ytree)] if best_fit: p=np.polyfit(xtree,ytree.asma(),1) plt.plot(xtree,np.polyval(p,xtree),"--",color=get_dataset_color("tree"),lw=1) cdutil.setTimeBoundsYearly(cru_p) if start_time.year>=1900: ycru=cru_p(time=(start_time,stop_time,"oob")).anom() Plotting.time_plot(ycru,color=get_dataset_color("cru"),label ="CRU",lw=1) xcru=x[:len(ycru)] if best_fit: p=np.polyfit(xcru,ycru.asma(),1) print "CRU slope is"+str(p[0]*10.) plt.plot(xcru,np.polyval(p,xcru),"--",color=get_dataset_color("cru"),lw=1) ydai=dai_p(time=(start_time,stop_time,"oob")).anom() Plotting.time_plot(ydai,color=get_dataset_color("dai"),label="Dai",lw=1) xdai=x[:len(ydai)] if best_fit: p=np.polyfit(xdai,ydai.asma(),1) print "DAI slope is"+str(p[0]*10.) plt.plot(xdai,np.polyval(p,xdai),"--",color=get_dataset_color("dai"),lw=1) #Plotting.time_plot(dai_p(time=(start_time,stop_time)).anom()) plt.xlabel("Time") plt.ylabel("Projection") if SM is not None: SM.project_soilmoisture("MERRA2") SM.project_soilmoisture("GLEAM") for depth in ["30cm","2m"]: for dataset in ["GLEAM","MERRA2"]: ymoist=SM.OBS_PROJECTIONS[dataset][depth](time=(start_time,stop_time)) xmoist=x[:len(ymoist)] Plotting.time_plot(ymoist,color=get_dataset_color(dataset,depth),lw=1,label=dataset+": "+sm_label(depth)) if best_fit: p=np.polyfit(xmoist,ymoist.asma(),1) plt.plot(xmoist,np.polyval(p,xmoist),"--",color=get_dataset_color(dataset,depth),lw=1) if aerosols: return ytree,ycru,ydai