def teestTimes2(self): a=MV2.masked_array(MV2.array([0,0,0,0,0,0,0,0,0,0,0,0]),[0,1,1,1,1,1,1,1,1,1,1,0]) bounds=numpy.ma.array( [[0,31], [31,59], [59,90], [90,120], [120,151], [151,181], [181,212], [212,243], [243,273], [273,304], [304,334], [334,365]] ) ax=a.getAxis(0) ax.setBounds(bounds) #print cdutil.times.centroid(a,[-10,30]) print(('Centroid Normal:',cdutil.times.centroid(a,[0,365]))) print(('Centroid Cyclic:',cdutil.times.cyclicalcentroid(a,[0,365]))) djf=cdutil.DJF(self.tas_mo) djf=cdutil.DJF(self.tas_mo,criteriaarg=[.8,0.0001]) djf=cdutil.ANNUALCYCLE.climatology(self.tas_mo) djf=cdutil.YEAR.departures(self.tas_mo)
def testAnnualSeasonalAverage(self): f = cdms2.open(self.filename, "r") # Read in the raw data EXCLUDING a leap year obs_timeseries1 = f('obs', time=slice(0, 48)) # 1900.1. to 1903.12. # Read in the raw data INCLUDING a leap year obs_timeseries2 = f('obs', time=slice( 0, 60)) # 1900.1. to 1904.12., 1904 is year lear ### Truncate first Jan, Feb and last Dec before get Annual cycle anomaly ... (to have fair DJF seasonal mean later) obs_timeseries1 = obs_timeseries1[2:-1] obs_timeseries2 = obs_timeseries2[2:-1] ### Set monthly time bounds ... cdutil.setTimeBoundsMonthly(obs_timeseries1) cdutil.setTimeBoundsMonthly(obs_timeseries2) #### Removing Annual cycle ... obs_timeseries_ano1 = cdutil.ANNUALCYCLE.departures(obs_timeseries1) obs_timeseries_ano2 = cdutil.ANNUALCYCLE.departures(obs_timeseries2) #### Calculate time average ... obs_timeseries_ano_timeave1 = cdutil.averager( obs_timeseries_ano1, axis='t') ## This should be zero and it does obs_timeseries_ano_timeave2 = cdutil.averager( obs_timeseries_ano2, axis='t') ## This should be zero BUT it does NOT #### SEASONAL MEAN TEST #### obs_timeseries_ano1_DJF = cdutil.DJF(obs_timeseries_ano1, criteriaarg=[0.95, None]) obs_timeseries_ano2_DJF = cdutil.DJF(obs_timeseries_ano2, criteriaarg=[0.95, None]) obs_timeseries_ano1_JJA = cdutil.JJA(obs_timeseries_ano1, criteriaarg=[0.95, None]) obs_timeseries_ano2_JJA = cdutil.JJA(obs_timeseries_ano2, criteriaarg=[0.95, None]) #### Calculate time average ... obs_timeseries_ano1_DJF_timeave = cdutil.averager( obs_timeseries_ano1_DJF, axis='t') ## This should be zero and it does obs_timeseries_ano2_DJF_timeave = cdutil.averager( obs_timeseries_ano2_DJF, axis='t') ## This should be zero BUT it does NOT obs_timeseries_ano1_JJA_timeave = cdutil.averager( obs_timeseries_ano1_JJA, axis='t') ## This should be zero and it does obs_timeseries_ano2_JJA_timeave = cdutil.averager( obs_timeseries_ano2_JJA, axis='t') ## This should be zero and it does numpy.testing.assert_almost_equal(obs_timeseries_ano_timeave2, obs_timeseries_ano_timeave1, 10) numpy.testing.assert_almost_equal(obs_timeseries_ano1_JJA_timeave, obs_timeseries_ano2_JJA_timeave, 10) numpy.testing.assert_almost_equal(obs_timeseries_ano1_DJF_timeave, obs_timeseries_ano2_DJF_timeave, 10)
def write_merra2(surf=None, root=None): if surf is None: surf, root = merra2() cdutil.setTimeBoundsMonthly(surf) cdutil.setTimeBoundsMonthly(root) fw = cdms.open( "../DROUGHT_ATLAS/OBSERVATIONS/MERRA2_soilmoisture_summerseason.nc", "w") djf_surf = cdutil.DJF(surf, criteriaarg=(1, None))[1:] jja_surf = cdutil.DJF(surf, criteriaarg=(1, None))[1:] ss_surf = summerseason_GLEAM(jja_surf, djf_surf) ss_surf.id = "smsurf" fw.write(ss_surf) djf_root = cdutil.DJF(root, criteriaarg=(1, None))[1:] jja_root = cdutil.DJF(root, criteriaarg=(1, None))[1:] ss_root = summerseason_GLEAM(jja_root, djf_root) ss_root.id = "smroot" fw.write(ss_root) fw.close()
# Adapted for numpy/ma/cdms2 by convertcdms.py import cdms2, cdutil, numpy.ma, MV2, os, sys, cdat_info cdms2.setAutoBounds('on') # centroid test a = MV2.masked_array(MV2.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]) bounds = numpy.ma.array([[0, 31], [31, 59], [59, 90], [90, 120], [120, 151], [151, 181], [181, 212], [212, 243], [243, 273], [273, 304], [304, 334], [334, 365]]) ax = a.getAxis(0) ax.setBounds(bounds) #print cdutil.times.centroid(a,[-10,30]) print 'Centroid Normal:', cdutil.times.centroid(a, [0, 365]) print 'Centroid Cyclic:', cdutil.times.cyclicalcentroid(a, [0, 365]) f = cdms2.open(os.path.join(cdat_info.get_sampledata_path(), 'tas_mo.nc')) s = f('tas') cdutil.setTimeBoundsMonthly(s) djf = cdutil.DJF(s) djf = cdutil.DJF(s, criteriaarg=[.8, 0.0001]) djf = cdutil.ANNUALCYCLE.climatology(s) djf = cdutil.YEAR.departures(s)
# Set time period --- start_year = 1980 end_year = 2000 start_time = cdtime.comptime(start_year) end_time = cdtime.comptime(end_year) # Load variable --- d = f('psl', time=(start_time, end_time), longitude=(-180, 180), latitude=(20, 90)) # Provide proper variable name d = MV2.divide(d, 100.) # Pa to hPa d.units = 'hPa' # Get DJF seasonal mean time series --- d_DJF = cdutil.DJF(d) # EOF (take only first variance mode...) --- solver = Eof(d_DJF, weights='area') eof = solver.eofsAsCovariance(neofs=1) pc = solver.pcs(npcs=1, pcscaling=1) # pcscaling=1: scaled to unit variance # (divided by the square-root of their eigenvalue) frac = solver.varianceFraction() # Sign control if needed --- eof = eof * -1 pc = pc * -1 #=========================================================================================================== # Plot #-----------------------------------------------------------------------------------------------------------