本文整理匯總了Python中geodata.netcdf.DatasetNetCDF.mask方法的典型用法代碼示例。如果您正苦於以下問題:Python DatasetNetCDF.mask方法的具體用法?Python DatasetNetCDF.mask怎麽用?Python DatasetNetCDF.mask使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類geodata.netcdf.DatasetNetCDF
的用法示例。
在下文中一共展示了DatasetNetCDF.mask方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: loadObservations
# 需要導入模塊: from geodata.netcdf import DatasetNetCDF [as 別名]
# 或者: from geodata.netcdf.DatasetNetCDF import mask [as 別名]
def loadObservations(name=None, folder=None, period=None, grid=None, station=None, shape=None, lencl=False,
varlist=None, varatts=None, filepattern=None, filelist=None, resolution=None,
projection=None, geotransform=None, axes=None, lautoregrid=None, mode='climatology'):
''' A function to load standardized observational datasets. '''
# prepare input
if mode.lower() == 'climatology': # post-processed climatology files
# transform period
if period is None or period == '':
if name not in ('PCIC','PRISM','GPCC','NARR'):
raise ValueError("A period is required to load observational climatologies.")
elif isinstance(period,basestring):
period = tuple([int(prd) for prd in period.split('-')])
elif not isinstance(period,(int,np.integer)) and ( not isinstance(period,tuple) and len(period) == 2 ):
raise TypeError(period)
elif mode.lower() in ('time-series','timeseries'): # concatenated time-series files
period = None # to indicate time-series (but for safety, the input must be more explicit)
if lautoregrid is None: lautoregrid = False # this can take very long!
# cast/copy varlist
if isinstance(varlist,basestring): varlist = [varlist] # cast as list
elif varlist is not None: varlist = list(varlist) # make copy to avoid interference
# figure out station and shape options
if station and shape: raise ArgumentError()
elif station or shape:
if grid is not None: raise NotImplementedError('Currently observational station data can only be loaded from the native grid.')
if lautoregrid: raise GDALError('Station data can not be regridded, since it is not map data.')
lstation = bool(station); lshape = bool(shape)
grid = station if lstation else shape
# add station/shape parameters
if varlist:
params = stn_params if lstation else shp_params
for param in params:
if param not in varlist: varlist.append(param)
else:
lstation = False; lshape = False
# varlist (varlist = None means all variables)
if varatts is None: varatts = default_varatts.copy()
if varlist is not None: varlist = translateVarNames(varlist, varatts)
# filelist
if filelist is None:
filename = getFileName(name=name, resolution=resolution, period=period, grid=grid, filepattern=filepattern)
# check existance
filepath = '{:s}/{:s}'.format(folder,filename)
if not os.path.exists(filepath):
nativename = getFileName(name=name, resolution=resolution, period=period, grid=None, filepattern=filepattern)
nativepath = '{:s}/{:s}'.format(folder,nativename)
if os.path.exists(nativepath):
if lautoregrid:
from processing.regrid import performRegridding # causes circular reference if imported earlier
griddef = loadPickledGridDef(grid=grid, res=None, folder=grid_folder)
dataargs = dict(period=period, resolution=resolution)
performRegridding(name, 'climatology',griddef, dataargs) # default kwargs
else: raise IOError("The dataset '{:s}' for the selected grid ('{:s}') is not available - use the regrid module to generate it.".format(filename,grid) )
else: raise IOError("The dataset file '{:s}' does not exits!\n('{:s}')".format(filename,filepath))
# load dataset
dataset = DatasetNetCDF(name=name, folder=folder, filelist=[filename], varlist=varlist, varatts=varatts,
axes=axes, multifile=False, ncformat='NETCDF4')
# mask all shapes that are incomplete in dataset
if shape and lencl and 'shp_encl' in dataset:
dataset.load() # need to load data before masking; is cheap for shape averages, anyway
dataset.mask(mask='shp_encl', invert=True, skiplist=shp_params)
# correct ordinal number of shape (should start at 1, not 0)
if lshape:
if dataset.hasAxis('shapes'): raise AxisError("Axis 'shapes' should be renamed to 'shape'!")
if not dataset.hasAxis('shape'):
raise AxisError()
if dataset.shape.coord[0] == 0: dataset.shape.coord += 1
# figure out grid
if not lstation and not lshape:
if grid is None or grid == name:
dataset = addGDALtoDataset(dataset, projection=projection, geotransform=geotransform, gridfolder=grid_folder)
elif isinstance(grid,basestring): # load from pickle file
# griddef = loadPickledGridDef(grid=grid, res=None, filename=None, folder=grid_folder)
# add GDAL functionality to dataset
dataset = addGDALtoDataset(dataset, griddef=grid, gridfolder=grid_folder)
else: raise TypeError(dataset)
# N.B.: projection should be auto-detected, if geographic (lat/lon)
return dataset
示例2: CentralProcessingUnit
# 需要導入模塊: from geodata.netcdf import DatasetNetCDF [as 別名]
# 或者: from geodata.netcdf.DatasetNetCDF import mask [as 別名]
offset = source.time.getIndex(period[0]-1979)/12 # origin of monthly time-series is at January 1979
# initialize processing
CPU = CentralProcessingUnit(source, sink, tmp=True)
# start processing climatology
CPU.Climatology(period=period[1]-period[0], offset=offset, flush=False)
# shift longitude axis by 180 degrees left (i.e. 0 - 360 -> -180 - 180)
CPU.Shift(lon=-180, flush=False)
# sync temporary storage with output (sink variable; do not flush!)
CPU.sync(flush=False)
# make new masks
if sink.hasVariable('landmask'):
sink.mask(sink.landmask, maskSelf=False, varlist=['snow','snowh','zs'], invert=True, merge=False)
# add names and length of months
sink.axisAnnotation('name_of_month', name_of_month, 'time',
atts=dict(name='name_of_month', units='', long_name='Name of the Month'))
#print ' === month === '
# sink += VarNC(sink.dataset, name='length_of_month', units='days', axes=(sink.time,), data=days_per_month,
# atts=dict(name='length_of_month',units='days',long_name='Length of Month'))
# close...
sink.sync()
sink.close()
# print dataset
print('')
print(sink)
示例3: print
# 需要導入模塊: from geodata.netcdf import DatasetNetCDF [as 別名]
# 或者: from geodata.netcdf.DatasetNetCDF import mask [as 別名]
# sync temporary storage with output
CPU.sync(flush=False)
print('\n')
# add landmask
print ' === landmask === '
tmpatts = dict(name='landmask', units='', long_name='Landmask for Climatology Fields',
description='where this mask is non-zero, no data is available')
# find a masked variable
for var in sink.variables.itervalues():
if var.masked and var.gdal:
mask = var.getMapMask(); break
# add variable to dataset
sink.addVariable(Variable(name='landmask', units='', axes=(sink.lat,sink.lon),
data=mask, atts=tmpatts), asNC=True)
sink.mask(sink.landmask)
# add names and length of months
sink.axisAnnotation('name_of_month', name_of_month, 'time',
atts=dict(name='name_of_month', units='', long_name='Name of the Month'))
#print ' === month === '
sink.addVariable(Variable(name='length_of_month', units='days', axes=(sink.time,), data=days_per_month,
atts=dict(name='length_of_month',units='days',long_name='Length of Month')), asNC=True)
# close...
sink.sync()
sink.close()
# print dataset
print('')
print(sink)
示例4: __init__
# 需要導入模塊: from geodata.netcdf import DatasetNetCDF [as 別名]
# 或者: from geodata.netcdf.DatasetNetCDF import mask [as 別名]
#.........這裏部分代碼省略.........
if experiment: title = experiment.title
else: title = name
dataset = DatasetNetCDF(name=name, folder=folder, filelist=filenames, varlist=varlist, axes=None,
varatts=atts, title=title, multifile=False, ignore_list=ignore_list,
ncformat='NETCDF4', squeeze=True, mode=ncmode, check_vars=check_vars)
# replace time axis
if lreplaceTime:
if lts or lcvdp:
# check time axis and center at 1979-01 (zero-based)
if experiment is None: ys = period[0]; ms = 1
else: ys,ms,ds = [int(t) for t in experiment.begindate.split('-')]; assert ds == 1
if dataset.hasAxis('time'):
ts = (ys-1979)*12 + (ms-1); te = ts+len(dataset.time) # month since 1979 (Jan 1979 = 0)
atts = dict(long_name='Month since 1979-01')
timeAxis = Axis(name='time', units='month', coord=np.arange(ts,te,1, dtype='int16'), atts=atts)
dataset.replaceAxis(dataset.time, timeAxis, asNC=False, deepcopy=False)
if dataset.hasAxis('year'):
ts = ys-1979; te = ts+len(dataset.year) # month since 1979 (Jan 1979 = 0)
atts = dict(long_name='Years since 1979-01')
yearAxis = Axis(name='year', units='year', coord=np.arange(ts,te,1, dtype='int16'), atts=atts)
dataset.replaceAxis(dataset.year, yearAxis, asNC=False, deepcopy=False)
elif lclim:
if dataset.hasAxis('time') and not dataset.time.units.lower() in monthlyUnitsList:
atts = dict(long_name='Month of the Year')
timeAxis = Axis(name='time', units='month', coord=np.arange(1,13, dtype='int16'), atts=atts)
assert len(dataset.time) == len(timeAxis), dataset.time
dataset.replaceAxis(dataset.time, timeAxis, asNC=False, deepcopy=False)
elif dataset.hasAxis('year'): raise NotImplementedError, dataset
# rename SST
if lSST: dataset['SST'] = dataset.Ts
# correct ordinal number of shape (should start at 1, not 0)
if lshape:
# mask all shapes that are incomplete in dataset
if lencl and 'shp_encl' in dataset: dataset.mask(mask='shp_encl', invert=True)
if dataset.hasAxis('shapes'): raise AxisError, "Axis 'shapes' should be renamed to 'shape'!"
if not dataset.hasAxis('shape'): raise AxisError
if dataset.shape.coord[0] == 0: dataset.shape.coord += 1
# check
if len(dataset) == 0: raise DatasetError, 'Dataset is empty - check source file or variable list!'
# add projection, if applicable
if not ( lstation or lshape ):
dataset = addGDALtoDataset(dataset, griddef=griddef, gridfolder=grid_folder, lwrap360=True, geolocator=True)
# return formatted dataset
return dataset
## Dataset API
dataset_name = 'CMIP5' # dataset name
root_folder # root folder of the dataset
avgfolder # root folder for monthly averages
outfolder # root folder for direct WRF output
ts_file_pattern = 'cmip5{0:s}{1:s}_monthly.nc' # filename pattern: filetype, grid
clim_file_pattern = 'cmip5{0:s}{1:s}_clim{2:s}.nc' # filename pattern: filetype, grid, period
data_folder = root_folder # folder for user data
grid_def = {'':None} # there are too many...
grid_res = {'':1.} # approximate grid resolution at 45 degrees latitude
default_grid = None
# functions to access specific datasets
loadLongTermMean = None # WRF doesn't have that...
loadClimatology = loadCESM # pre-processed, standardized climatology
loadTimeSeries = loadCESM_TS # time-series data
#loadStationClimatology = loadCESM_Stn # pre-processed, standardized climatology at stations
#loadStationTimeSeries = loadCESM_StnTS # time-series data at stations
#loadShapeClimatology = loadCESM_Shp # climatologies without associated grid (e.g. provinces or basins)
#loadShapeTimeSeries = loadCESM_ShpTS # time-series without associated grid (e.g. provinces or basins)