def get_latlon_array_descriptor(self): configPath = str(self.datadir.join('config.analysis')) config = MpasAnalysisConfigParser() config.read(configPath) lat = numpy.array(config.getExpression('interpolate', 'lat', usenumpyfunc=True)) lon = numpy.array(config.getExpression('interpolate', 'lon', usenumpyfunc=True)) descriptor = LatLonGridDescriptor() descriptor.create(lat, lon, units='degrees') return descriptor
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 # ------- # Xylar Asay-Davis, Luke Van Roekel # Load Argo observational Data dsObs = self.build_observational_dataset(fileName) # create a descriptor of the observation grid using Lat/Lon # coordinates obsDescriptor = LatLonGridDescriptor.read(ds=dsObs, latVarName='latCoord', lonVarName='lonCoord') dsObs.close() return obsDescriptor # }}}
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 ''' # create a descriptor of the observation grid using the lat/lon # coordinates obsDescriptor = LatLonGridDescriptor.read(fileName=fileName, latVarName='lat', lonVarName='lon') return obsDescriptor # }}}
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 # ------- # Riley X. Brady, Xylar Asay-Davis # create a descriptor of the observation grid using the lat/lon # coordinates dsObs = self.build_observational_dataset(fileName) obsDescriptor = LatLonGridDescriptor.read(ds=dsObs, latVarName='lat', lonVarName='lon') return obsDescriptor # }}}
def remap_v(prefix, inGridName, inGridFileName, inDir, inVPrefix): cacheVFileName = '{}_merid_vel_{}.nc'.format(prefix, inGridName) config = MpasAnalysisConfigParser() config.read('mpas_analysis/config.default') matGrid = loadmat(inGridFileName) # lat/lon is a tensor grid so we can use 1-D arrays lon = matGrid['XC'][:, 0] lat = matGrid['YG'][0, :] z = matGrid['RC'][:, 0] cellFraction = matGrid['hFacS'] botIndices = get_bottom_indices(cellFraction) with sose_v_to_nc('{}/{}'.format(inDir, inVPrefix), cacheVFileName, lon, lat, z, cellFraction, botIndices) \ as dsV: inDescriptor = LatLonGridDescriptor() inDescriptor = LatLonGridDescriptor.read(cacheVFileName, latVarName='lat', lonVarName='lon') outDescriptor = get_comparison_descriptor(config, 'antarctic') outGridName = outDescriptor.meshName outVFileName = '{}_merid_vel_{}.nc'.format(prefix, outGridName) mappingFileName = '{}/map_V_{}_to_{}.nc'.format( inDir, inGridName, outGridName) remapper = Remapper(inDescriptor, outDescriptor, mappingFileName) remapper.build_mapping_file(method='bilinear') if not os.path.exists(outVFileName): print('Remapping meridional velocity...') with remapper.remap(dsV, renormalizationThreshold=0.01) \ as remappedV: print('Done.') remappedV.attrs['history'] = ' '.join(sys.argv) write_netcdf(remappedV, outVFileName)
def setup_obs_remapper(self, config, fieldName): gridFileName = '{}/obsGrid.nc'.format(self.datadir) comparisonDescriptor = \ get_lat_lon_comparison_descriptor(config) obsDescriptor = LatLonGridDescriptor() obsDescriptor.read(fileName=gridFileName, latVarName='lat', lonVarName='lon') remapper = \ get_remapper( config=config, sourceDescriptor=obsDescriptor, comparisonDescriptor=comparisonDescriptor, mappingFileSection='oceanObservations', mappingFileOption='sstClimatologyMappingFile', mappingFilePrefix='map_obs_{}'.format(fieldName), method=config.get('oceanObservations', 'interpolationMethod')) return remapper
def remap(inDir, outDir): inGridName = 'SouthernOcean_0.25x0.125degree' inFileName = '{}/Schmidtko_et_al_2014_bottom_PT_S_PD_{}.nc'.format( inDir, inGridName) config = MpasAnalysisConfigParser() config.read('mpas_analysis/config.default') inDescriptor = LatLonGridDescriptor() inDescriptor = LatLonGridDescriptor.read(inFileName, latVarName='lat', lonVarName='lon') outDescriptor = get_comparison_descriptor(config, 'antarctic') outGridName = outDescriptor.meshName outFileName = '{}/Schmidtko_et_al_2014_bottom_PT_S_PD_{}.nc'.format( outDir, outGridName) mappingFileName = '{}/map_{}_to_{}.nc'.format(inDir, inGridName, outGridName) remapper = Remapper(inDescriptor, outDescriptor, mappingFileName) remapper.build_mapping_file(method='bilinear') if not os.path.exists(outFileName): print('Remapping...') with xarray.open_dataset(inFileName) as dsIn: with remapper.remap(dsIn, renormalizationThreshold=0.01) \ as remappedMLD: print('Done.') remappedMLD.attrs['history'] = ' '.join(sys.argv) write_netcdf(remappedMLD, outFileName)
def remap(ds, outDescriptor, mappingFileName, inDir, outFileName): tempFileName1 = '{}/temp_transpose.nc'.format(inDir) tempFileName2 = '{}/temp_remap.nc'.format(inDir) if 'z' in ds: print(' transposing and fixing periodicity...') ds = ds.chunk({'Time': 4}) ds = ds.transpose('Time', 'z', 'lat', 'lon') ds = add_periodic_lon(ds) write_netcdf(ds, tempFileName1) ds.close() inDescriptor = LatLonGridDescriptor.read(tempFileName1, latVarName='lat', lonVarName='lon') remapper = Remapper(inDescriptor, outDescriptor, mappingFileName) remapper.build_mapping_file(method='bilinear') remapper.remap_file(inFileName=tempFileName1, outFileName=tempFileName2, overwrite=True, renormalize=0.01) ds = xarray.open_dataset(tempFileName2) if 'z' in ds: print(' transposing back...') ds = ds.chunk({'Time': 4}) ds = ds.transpose('Time', 'x', 'y', 'z', 'nvertices') ds.attrs['meshName'] = outDescriptor.meshName for coord in ['x', 'y']: ds.coords[coord] = xarray.DataArray.from_dict( outDescriptor.coords[coord]) ds = ds.set_coords(names=['month', 'year']) write_netcdf(ds, outFileName) ds.close()
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 # }}}
# Copyright (c) 2018 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 mpas_analysis.shared.interpolation import Remapper from mpas_analysis.shared.grid import LatLonGridDescriptor 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_pt_s(prefix, inGridName, inGridFileName, inDir, inTPrefix, inSPrefix, inGammaNPrefix): cacheTFileName = '{}_pot_temp_{}.nc'.format(prefix, inGridName) cacheSFileName = '{}_salinity_{}.nc'.format(prefix, inGridName) cacheGammaNFileName = '{}_neut_den_{}.nc'.format(prefix, inGridName) config = MpasAnalysisConfigParser() config.read('mpas_analysis/config.default') matGrid = loadmat(inGridFileName) # lat/lon is a tensor grid so we can use 1-D arrays lon = matGrid['XC'][:, 0] lat = matGrid['YC'][0, :] z = matGrid['RC'][:, 0] cellFraction = matGrid['hFacC'] botIndices = get_bottom_indices(cellFraction) with sose_pt_to_nc('{}/{}'.format(inDir, inTPrefix), cacheTFileName, lon, lat, z, cellFraction, botIndices) \ as dsT: inDescriptor = LatLonGridDescriptor() inDescriptor = LatLonGridDescriptor.read(cacheTFileName, latVarName='lat', lonVarName='lon') outDescriptor = get_comparison_descriptor(config, 'antarctic') outGridName = outDescriptor.meshName outTFileName = '{}_pot_temp_{}.nc'.format(prefix, outGridName) outSFileName = '{}_salinity_{}.nc'.format(prefix, outGridName) outGammaNFileName = '{}_neut_den_{}.nc'.format(prefix, outGridName) mappingFileName = '{}/map_C_{}_to_{}.nc'.format( inDir, inGridName, outGridName) remapper = Remapper(inDescriptor, outDescriptor, mappingFileName) remapper.build_mapping_file(method='bilinear') if not os.path.exists(outTFileName): dsT.reset_coords(names='zBot', inplace=True) print('Remapping potential temperature...') with remapper.remap(dsT, renormalizationThreshold=0.01) \ as remappedT: print('Done.') remappedT.attrs['history'] = ' '.join(sys.argv) remappedT.set_coords(names='zBot', inplace=True) write_netcdf(remappedT, outTFileName) with sose_s_to_nc('{}/{}'.format(inDir, inSPrefix), cacheSFileName, lon, lat, z, cellFraction, botIndices) \ as dsS: if not os.path.exists(outSFileName): dsS.reset_coords(names='zBot', inplace=True) print('Remapping salinity...') with remapper.remap(dsS, renormalizationThreshold=0.01) \ as remappedS: print('Done.') remappedS.attrs['history'] = ' '.join(sys.argv) remappedS.set_coords(names='zBot', inplace=True) write_netcdf(remappedS, outSFileName) with sose_gammaN_to_nc('{}/{}'.format(inDir, inGammaNPrefix), cacheGammaNFileName, lon, lat, z, cellFraction, botIndices) \ as dsGammaN: if not os.path.exists(outGammaNFileName): dsGammaN.reset_coords(names='zBot', inplace=True) print('Remapping neutral density...') with remapper.remap(dsGammaN, renormalizationThreshold=0.01) \ as remappedGammaN: print('Done.') remappedGammaN.attrs['history'] = ' '.join(sys.argv) remappedGammaN.set_coords(names='zBot', inplace=True) write_netcdf(remappedGammaN, outGammaNFileName)
'SouthernOcean/SOSE/monthly/SALT_mnthlyBar.0000000100' inGridFileName = '/media/xylar/extra_data/data_overflow/observations/' \ 'SouthernOcean/SOSE/grid.mat' prefix = 'SOSE_2005-2010_monthly_' cacheTFileName = '{}_pot_temp_{}.nc'.format(prefix, inGridName) cacheSFileName = '{}_salinity_{}.nc'.format(prefix, inGridName) outTFileName = '{}_pot_temp_{}.nc'.format(prefix, outGridName) outSFileName = '{}_salinity_{}.nc'.format(prefix, outGridName) config = MpasAnalysisConfigParser() config.read('config.default') inDescriptor = LatLonGridDescriptor() if not os.path.exists(cacheTFileName) or not os.path.exists(cacheSFileName): matGrid = loadmat(inGridFileName) # lat/lon is a tensor grid so we can use 1-D arrays lon = matGrid['XC'][:, 0] lat = matGrid['YC'][0, :] z = matGrid['RC'][:, 0] cellFraction = matGrid['hFacC'] botIndices = get_bottom_indices(cellFraction) if os.path.exists(cacheTFileName): dsT = xarray.open_dataset(cacheTFileName) else: field, botField = get_monthly_average(inTFileName)