def averageRasters(filelist=None, projection=None, shape=None, lfeedback=True): ''' load a list of rasters into an array and average over a shape; return timeseries ''' # read rasters data, geotransform = readRasterArray(file_pattern='{FILENAME}', lfeedback=lfeedback, lgeotransform=True, lskipMissing=False, lna=False, axes=('FILENAME', ), FILENAME=filelist) if lfeedback: print((data.shape, geotransform)) # create GridDef to rasterize shape griddef = GridDefinition(name='raster', projection=projection, geotransform=geotransform, size=(data.shape[-1], data.shape[-2]), convention='proj4') if lfeedback: print(griddef) # create mask mask = shape.rasterize(griddef=griddef, invert=False) assert mask.shape == data.shape[1:], griddef mask = mask.reshape((1, ) + mask.shape).repeat(data.shape[0], axis=0) assert mask.shape == data.shape, mask #print(mask) # extract and average data #print(mask[1,:].sum(),) data.mask = mask #print(data[1,:]) #print(data[1,:].sum(),data[1,:].mean()) ts_data = data.mean(axis=-1).mean(axis=-1) assert len(filelist) == data.shape[0], data.shape # return timeseries of shape averages return ts_data
# NARR projection projdict = dict(proj = 'lcc', # Lambert Conformal Conic lat_1 = 50., # Latitude of first standard parallel lat_2 = 50., # Latitude of second standard parallel lat_0 = 50., # Latitude of natural origin lon_0 = -107.) # Longitude of natural origin # # x_0 = 5632642.22547, # False Origin Easting # y_0 = 4612545.65137) # False Origin Northing # NARR grid definition projection = getProjFromDict(projdict) geotransform = (-5648873.5, 32463.0, 0.0, -4628776.5, 0.0, 32463.0) size = (349, 277) # (x,y) map size of NARR grid # make GridDefinition instance NARR_grid = GridDefinition(name=dataset_name, projection=projdict, geotransform=geotransform, size=size) # variable attributes and name varatts = dict(air = dict(name='T2', units='K'), # 2m Temperature prate = dict(name='precip', units='kg/m^2/s'), # total precipitation rate (kg/m^2/s) # LTM-only variables (currently...) prmsl = dict(name='pmsl', units='Pa'), # sea-level pressure pevap = dict(name='pet', units='kg/m^2'), # monthly accumulated PET (kg/m^2) pr_wtr = dict(name='pwtr', units='kg/m^2'), # total precipitable water (kg/m^2) # axes (don't have their own file; listed in axes) lon = dict(name='lon2D', units='deg E'), # geographic longitude field lat = dict(name='lat2D', units='deg N'), # geographic latitude field time = dict(name='time', units='days', offset=-1569072, scalefactor=1./24.), # time coordinate # N.B.: the time coordinate is only used for the monthly time-series data, not the LTM # the time offset is chose such that 1979 begins with the origin (time=0) x = dict(name='x', units='m', offset=-5632642), # projected west-east coordinate
# PRISM grid definition dlat = dlon = 1. / 120. # 0.0083333333 dlat2 = dlon2 = 1. / 240. # half step nlat = 1680 # slat = 14 deg nlon = 3241 # slon = 27 deg # N.B.: coordinates refer to grid points (CF convention), commented values refer to box edges (GDAL convention) llclat = 48. # 48.0000000000553 # llclat = 48.0000000000553 # 48. llclon = -140. # -140.0 geotransform = (llclon - dlon2, dlon, 0.0, llclat - dlat2, 0.0, dlat) size = (nlon, nlat) # (x,y) map size of PRISM grid # make GridDefinition instance PCIC_grid = GridDefinition(name=dataset_name, projection=None, geotransform=geotransform, size=size) ## Functions that handle access to the original PCIC NetCDF files # variable attributes and names in original PCIC files ltmvaratts = dict( tmin=dict(name='Tmin', units='K', atts=dict(long_name='Minimum 2m Temperature'), offset=273.15), # 2m minimum temperature tmax=dict(name='Tmax', units='K', atts=dict(long_name='Maximum 2m Temperature'), offset=273.15), # 2m maximum temperature pr=dict(name='precip',
dataset_name = 'CFSR' # CFSR grid definition # geotransform_031 = (-180.15625, 0.3125, 0.0, 89.915802001953125, 0.0, -0.30960083) geotransform_031 = (-0.15625, 0.3125, 0.0, 89.915802001953125, 0.0, -0.30960083) size_031 = (1152, 576) # (x,y) map size geotransform_05 = (-180.0, 0.5, 0.0, -90.0, 0.0, 0.5) geotransform_05 = (-0.25, 0.5, 0.0, 90.25, 0.0, -0.5 ) # this grid actually has a grid point at the poles! size_05 = (720, 361) # (x,y) map size # make GridDefinition instance CFSR_031_grid = GridDefinition(name='CFSR_031', projection=None, geotransform=geotransform_031, size=size_031, lwrap360=True) CFSR_05_grid = GridDefinition(name='CFSR_05', projection=None, geotransform=geotransform_05, size=size_05, lwrap360=True) CFSR_grid = CFSR_031_grid # default # variable attributes and name varatts = dict( TMP_L103_Avg=dict(name='T2', units='K'), # 2m average temperature TMP_L1=dict(name='Ts', units='K'), # average skin temperature PRATE_L1=dict(name='precip', units='kg/m^2/s'), # total precipitation PRES_L1=dict(name='ps', units='Pa'), # surface pressure
dataset_name = 'GPCC' # GPCC grid definition geotransform_025 = (-180.0, 0.25, 0.0, -90.0, 0.0, 0.25) size_025 = (1440, 720) # (x,y) map size geotransform_05 = (-180.0, 0.5, 0.0, -90.0, 0.0, 0.5) size_05 = (720, 360) # (x,y) map size geotransform_10 = (-180.0, 1.0, 0.0, -90.0, 0.0, 1.0) size_10 = (360, 180) # (x,y) map size geotransform_25 = (-180.0, 2.5, 0.0, -90.0, 0.0, 2.5) size_25 = (144, 72) # (x,y) map size # make GridDefinition instance GPCC_025_grid = GridDefinition(name='GPCC_025', projection=None, geotransform=geotransform_025, size=size_025) GPCC_05_grid = GridDefinition(name='GPCC_05', projection=None, geotransform=geotransform_05, size=size_05) GPCC_10_grid = GridDefinition(name='GPCC_10', projection=None, geotransform=geotransform_10, size=size_10) GPCC_25_grid = GridDefinition(name='GPCC_25', projection=None, geotransform=geotransform_25, size=size_25) # variable attributes and name