def get_observation_descriptor(self, fileName): # {{{ ''' get a MeshDescriptor for the observation grid Parameters ---------- fileName : str observation file name describing the source grid Returns ------- obsDescriptor : ``MeshDescriptor`` The descriptor for the observation grid ''' # Authors # ------- # Xylar Asay-Davis # Load MLD observational data dsObs = self.build_observational_dataset(fileName) # create a descriptor of the observation grid using the lat/lon # coordinates obsDescriptor = LatLonGridDescriptor.read(ds=dsObs, latVarName='lat', lonVarName='lon') dsObs.close() return obsDescriptor # }}}
def get_latlon_file_descriptor(self): latLonGridFileName = str(self.datadir.join('SST_annual_1870-1900.nc')) descriptor = LatLonGridDescriptor.read(latLonGridFileName, latVarName='lat', lonVarName='lon') return (descriptor, latLonGridFileName)
def get_observation_descriptor(self, fileName): # {{{ ''' get a MeshDescriptor for the observation grid Parameters ---------- fileName : str observation file name describing the source grid Returns ------- obsDescriptor : ``MeshDescriptor`` The descriptor for the observation grid ''' # Authors # ------- # Kevin Rosa # create a descriptor of the observation grid using the lat/lon # coordinates obsDescriptor = LatLonGridDescriptor.read(fileName=fileName, latVarName='Lat', lonVarName='Lon') return obsDescriptor # }}}
def setup_obs_remapper(self, config, fieldName): gridFileName = '{}/obsGrid.nc'.format(self.datadir) comparisonDescriptor = \ get_comparison_descriptor(config, comparisonGridName='latlon') obsDescriptor = LatLonGridDescriptor.read(fileName=gridFileName, latVarName='lat', lonVarName='lon') remapper = \ get_remapper( config=config, sourceDescriptor=obsDescriptor, comparisonDescriptor=comparisonDescriptor, mappingFilePrefix='map_obs_{}'.format(fieldName), method=config.get('oceanObservations', 'interpolationMethod')) return remapper
def get_observation_descriptor(self, fileName): # {{{ """ get a MeshDescriptor for the observation grid Parameters ---------- fileName : str observation file name describing the source grid Returns ------- obsDescriptor : ``MeshDescriptor`` The descriptor for the observation grid """ # Authors # ------- # Darin Comeau, Xylar Asay-Davis # create a descriptor of the observation grid using the lat/lon # coordinates obsDescriptor = LatLonGridDescriptor.read(fileName=fileName, latVarName='latitude', lonVarName='longitude') return obsDescriptor # }}}
def _get_lat_lon_comparison_descriptor(config): # {{{ """ Get a descriptor of the lat/lon comparison grid, used for remapping and determining the grid name Parameters ---------- config : instance of ``MpasAnalysisConfigParser`` Contains configuration options Returns ------- descriptor : ``LatLonGridDescriptor`` object A descriptor of the lat/lon grid """ # Authors # ------- # Xylar Asay-Davis climSection = 'climatology' comparisonLatRes = config.getWithDefault(climSection, 'comparisonLatResolution', constants.dLatitude) comparisonLonRes = config.getWithDefault(climSection, 'comparisonLatResolution', constants.dLongitude) nLat = int((constants.latmax - constants.latmin) / comparisonLatRes) + 1 nLon = int((constants.lonmax - constants.lonmin) / comparisonLonRes) + 1 lat = numpy.linspace(constants.latmin, constants.latmax, nLat) lon = numpy.linspace(constants.lonmin, constants.lonmax, nLon) descriptor = LatLonGridDescriptor.create(lat, lon, units='degrees') return descriptor # }}}
def get_latlon_array_descriptor(self): lat = numpy.linspace(-90., 90., 91) lon = numpy.linspace(-180., 180., 181) descriptor = LatLonGridDescriptor.create(lat, lon, units='degrees') return descriptor
# reserved. # Copyright (c) 2020 UT-Battelle, LLC. All rights reserved. # # Additional copyright and license information can be found in the LICENSE file # distributed with this code, or at # https://raw.githubusercontent.com/MPAS-Dev/MPAS-Analysis/master/LICENSE import numpy from pyremap import LatLonGridDescriptor, Remapper from mpas_analysis.shared.constants import constants inputFileName = '/media/xylar/extra_data/analysis/output/GMPAS-QU240/' \ 'remap_obs/clim/obs/mld_1.0x1.0degree.nc' obsDescriptor = LatLonGridDescriptor.read(fileName=inputFileName, latVarName='lat', lonVarName='lon') comparisonLatRes = 4. comparisonLonRes = 4. nLat = int((constants.latmax - constants.latmin) / comparisonLatRes) + 1 nLon = int((constants.lonmax - constants.lonmin) / comparisonLonRes) + 1 lat = numpy.linspace(constants.latmin, constants.latmax, nLat) lon = numpy.linspace(constants.lonmin, constants.lonmax, nLon) comparisonDescriptor = LatLonGridDescriptor.create(lat, lon, units='degrees') remapper = Remapper(obsDescriptor, comparisonDescriptor, mappingFileName='map.nc')
def _remap(config, modelFolder): res = get_res(config) hres = get_horiz_res(config) modelName = config.get('model', 'name') inFileNames = {} outFileNames = {} bothExist = True for fieldName in ['temperature', 'salinity']: inFileNames[fieldName] = \ '{}/{}_{}_interp_z.nc'.format(modelFolder, modelName, fieldName) outFileNames[fieldName] = \ '{}/{}_{}_{}.nc'.format(modelFolder, modelName, fieldName, res) if not os.path.exists(outFileNames[fieldName]): bothExist = False if bothExist: return print(' Remapping to {} grid...'.format(res)) for fieldName in inFileNames: inFileName = inFileNames[fieldName] outFileName = outFileNames[fieldName] if os.path.exists(outFileName): continue outGridFileName = 'ismip6/{}_grid.nc'.format(hres) print(' {}'.format(outFileName)) progressDir = '{}/progress_remap_{}'.format(modelFolder, fieldName) try: os.makedirs(progressDir) except OSError: pass ds = xarray.open_dataset(inFileName) if len(ds.lon.dims) == 1: inDescriptor = LatLonGridDescriptor.read(inFileName, latVarName='lat', lonVarName='lon') else: assert (len(ds.lon.dims) == 2) inDescriptor = LatLon2DGridDescriptor.read(inFileName, latVarName='lat', lonVarName='lon') inDescriptor.regional = True outDescriptor = get_polar_descriptor_from_file(outGridFileName, projection='antarctic') mappingFileName = '{}/map_{}_to_{}.nc'.format(modelName.lower(), inDescriptor.meshName, outDescriptor.meshName) remapper = Remapper(inDescriptor, outDescriptor, mappingFileName) remapper.build_mapping_file(method='bilinear') ds = ds.drop_vars(['lat', 'lon']) nt = ds.sizes['time'] widgets = [ ' ', progressbar.Percentage(), ' ', progressbar.Bar(), ' ', progressbar.ETA() ] print(f' remapping: {fieldName}') bar = progressbar.ProgressBar(widgets=widgets, maxval=nt).start() for tIndex in range(nt): progressFileName = '{}/{}_t_{}.nc'.format(progressDir, modelName, tIndex) if os.path.exists(progressFileName): bar.update(tIndex + 1) continue dsIn = ds.isel(time=tIndex) dsOut = remapper.remap(dsIn, renormalizationThreshold=0.1) dsOut = dsOut.transpose('z', 'y', 'x') for attrName in ['units', 'standard_name', 'long_name']: if attrName in ds[fieldName].attrs: dsOut[fieldName].attrs[attrName] = \ ds[fieldName].attrs[attrName] dsOut.z.attrs = ds.z.attrs dsOut.to_netcdf(progressFileName) bar.update(tIndex + 1) bar.finish() dsOut = xarray.open_mfdataset('{}/{}_t_*.nc'.format( progressDir, modelName), combine='nested', concat_dim='time') dsOut['z_bnds'] = ds.z_bnds dsOut.to_netcdf(outFileName)