def get(self): value=self.grid if value is None: if not self.latitude.n is None: # Ok we seem to have grid definition if self.latitude.type == 1: # Gaussian if self.longitude.first is None: value=cdms2.createGaussianGrid(self.latitude.n) else: value=cdms2.createGaussianGrid(self.latitude.n,self.longitude.first) elif self.latitude.type == 0: # Uniform value=cdms2.createUniformGrid(self.latitude.first, self.latitude.n, self.latitude.delta, self.longitude.first, self.longitude.n, self.longitude.delta) elif self.latitude.type== 2: # Equalarea lat=cdms2.createEqualAreaAxis(self.latitude.n) lon=cdms2.createUniformLongitude(self.longitude.first, self.longitude.n, self.longitude.delta) value=cdms2.createGenericGrid(lat[:],lon[:],lat.getBounds(),lon.getBounds()) elif not self.file is None: f=cdms2.open(self.file) value=f[self.var].getGrid() ## Ok temporary stuff to try to be able to close the file lat=value.getLatitude() lon=value.getLongitude() value=cdms2.createRectGrid(lat,lon) f.close() return value
def get(self): value = self.grid if value is None: if not self.latitude.n is None: # Ok we seem to have grid definition if self.latitude.type == 1: # Gaussian if self.longitude.first is None: value = cdms2.createGaussianGrid(self.latitude.n) else: value = cdms2.createGaussianGrid( self.latitude.n, self.longitude.first) elif self.latitude.type == 0: # Uniform value = cdms2.createUniformGrid(self.latitude.first, self.latitude.n, self.latitude.delta, self.longitude.first, self.longitude.n, self.longitude.delta) elif self.latitude.type == 2: # Equalarea lat = cdms2.createEqualAreaAxis(self.latitude.n) lon = cdms2.createUniformLongitude(self.longitude.first, self.longitude.n, self.longitude.delta) value = cdms2.createGenericGrid(lat[:], lon[:], lat.getBounds(), lon.getBounds()) elif not self.file is None: f = cdms2.open(self.file) value = f[self.var].getGrid() ## Ok temporary stuff to try to be able to close the file lat = value.getLatitude() lon = value.getLongitude() value = cdms2.createRectGrid(lat, lon) f.close() return value
def testRegridZonal(self): f = self.getFile( os.path.join(cdat_info.get_sampledata_path(), "clt.nc")) s = f("clt", slice(0, 1)) g = cdms2.createGaussianGrid(64) gl = cdms2.createZonalGrid(g) regridded = s.regrid(gl)
def testContiguousRegridNANIssue(self): a = MV2.reshape(MV2.sin(MV2.arange(20000)), (2, 1, 100, 100)) lon = cdms2.createAxis(MV2.arange(100) * 3.6) lon.designateLongitude() lon.units = "degrees_east" lon.id = "longitude" lat = cdms2.createAxis(MV2.arange(100) * 1.8 - 90.) lat.id = "latitude" lat.designateLatitude() lat.units = "degrees_north" lev = cdms2.createAxis([1000.]) lev.id = "plev" lev.designateLevel() lev.units = "hPa" t = cdms2.createAxis([0, 31.]) t.id = "time" t.designateTime() t.units = "days since 2014" cdutil.setTimeBoundsMonthly(t) a.setAxisList((t, lev, lat, lon)) a = MV2.masked_less(a, .5) grd = cdms2.createGaussianGrid(64) a = a.ascontiguous() a = a.regrid(grd, regridTool="regrid2") a = cdutil.averager(a, axis='txy') self.assertEqual(a[0], 0.7921019540305255)
def generate_grid(self, gridder, spatial, chunk): try: grid_type, grid_param = gridder.grid.split('~') except ValueError: raise WPSError('Error generating grid "{name}"', name=gridder.grid) if grid_type.lower() == 'uniform': result = re.match('^(.*)x(.*)$', grid_param) if result is None: raise WPSError('Failed to parse uniform configuration from {value}', value=grid_param) try: start_lat, nlat, delta_lat = self.parse_uniform_arg(result.group(1), -90.0, 180.0) except WPSError: raise try: start_lon, nlon, delta_lon = self.parse_uniform_arg(result.group(2), 0.0, 360.0) except WPSError: raise grid = cdms2.createUniformGrid(start_lat, nlat, delta_lat, start_lon, nlon, delta_lon) logger.info('Created target uniform grid {} from lat {}:{}:{} lon {}:{}:{}'.format( grid.shape, start_lat, delta_lat, nlat, start_lon, delta_lon, nlon)) else: try: nlats = int(grid_param) except ValueError: raise WPSError('Error converting gaussian parameter to an int') grid = cdms2.createGaussianGrid(nlats) logger.info('Created target gaussian grid {}'.format(grid.shape)) target = cdms2.MV2.ones(grid.shape) target.setAxisList(grid.getAxisList()) lat = chunk.getLatitude() lon = chunk.getLongitude() try: lat_spec = spatial[lat.id] lon_spec = spatial[lon.id] except KeyError as e: logger.debug('Skipping subsetting the target grid') else: target = target(latitude=lat_spec, longitude=lon_spec) return target.getGrid()
def test0(self): """ One way interpolation """ fnm = sys.prefix + '/sample_data/clt.nc' f = cdms2.open(fnm) s = f("clt") grdori = s.getGrid() nLon = s.shape[-1] nLat = s.shape[-2] xx = numpy.outer(numpy.ones((nLat, ), numpy.float32), s.getLongitude()[:]) yy = numpy.outer(s.getLatitude(), numpy.ones((nLon, ), numpy.float32)) print s.shape, xx.shape, yy.shape #s[0,...] = 50.0*(1.0 + numpy.sin(4*numpy.pi * xx / 180.0) * numpy.cos(2*numpy.pi * yy / 180.0)) grid = cdms2.createGaussianGrid(32, 64) sInterps = {} sInterps['regrid2'] = s.regrid(grid, regridTool='regrid2') sInterps['esmf linear'] = s.regrid(grid, regridTool='esmf', regridMethod='linear') diag = {} sInterps['libcf'] = s.regrid(grid, regridTool='libcf', diag=diag) print diag diag = {} sInterps['esmf conserve'] = s.regrid(grid, regridTool='esmf', regridMethod='conserve', diag=diag) print diag diff = abs(sInterps['esmf linear'] - sInterps['regrid2']).max() self.assertLess(diff, 18.0) diff = abs(sInterps['esmf conserve'] - sInterps['regrid2']).max() self.assertLess(diff, 86.0) diff = abs(sInterps['libcf'] - sInterps['regrid2']).max() self.assertLess(diff, 18.0) if PLOT: row = 0 for mth in sInterps: row += 1 pylab.subplot(2, 2, row) pylab.pcolor(sInterps[mth][0, ...] - sInterps['regrid2'][0, ...], vmin=-10, vmax=10) pylab.colorbar() pylab.title(mth + ' - regrid2')
def generate_user_defined_grid(self, gridder): try: grid_type, grid_param = gridder.grid.split('~') except AttributeError: return None except ValueError: raise WPSError('Error generating grid "{name}"', name=gridder.grid) logger.info('Generating grid %r %r', grid_type, grid_param) if grid_type.lower() == 'uniform': result = re.match('^(.*)x(.*)$', grid_param) if result is None: raise WPSError( 'Failed to parse uniform configuration from {value}', value=grid_param) try: start_lat, nlat, delta_lat = self.parse_uniform_arg( result.group(1), -90.0, 180.0) except WPSError: raise try: start_lon, nlon, delta_lon = self.parse_uniform_arg( result.group(2), 0.0, 360.0) except WPSError: raise grid = cdms2.createUniformGrid(start_lat, nlat, delta_lat, start_lon, nlon, delta_lon) logger.info( 'Created target uniform grid {} from lat {}:{}:{} lon {}:{}:{}' .format(grid.shape, start_lat, delta_lat, nlat, start_lon, delta_lon, nlon)) elif grid_type.lower() == 'gaussian': try: nlats = int(grid_param) except ValueError: raise WPSError('Error converting gaussian parameter to an int') grid = cdms2.createGaussianGrid(nlats) logger.info('Created target gaussian grid {}'.format(grid.shape)) else: raise WPSError('Unknown grid type for regridding: {}', grid_type) return grid
def interp2commonGrid(d, dlat, debug=False): """ input - d: cdms array - dlat: resolution (i.e. grid distance) in degree output - d2: d interpolated to dlat resolution grid """ nlat = int(180/dlat) grid = cdms2.createGaussianGrid(nlat, xorigin=0.0, order="yx") d2 = d.regrid(grid, regridTool='regrid2', mkCyclic=True) d2 = d2(latitude=(-10, 10)) if debug: print('debug: d2.shape:', d2.shape) return d2
def Xtest1(self): """ Forward/backward interpolation """ fnm=vcs.sample_data+'/clt.nc' f=cdms2.open(fnm) s=f("clt") grdori = s.getGrid() nLon = s.shape[-1] nLat = s.shape[-2] xx = numpy.outer(numpy.ones( (nLat,), numpy.float32 ), s.getLongitude()[:]) yy = numpy.outer(s.getLatitude(), numpy.ones( (nLon,), numpy.float32) ) print s.shape, xx.shape, yy.shape #s[0,...] = 50.0*(1.0 + numpy.sin(4*numpy.pi * xx / 180.0) * numpy.cos(2*numpy.pi * yy / 180.0)) grid = cdms2.createGaussianGrid(64,128) sInterps = {} sInterps['regrid2'] = s.regrid(grid, regridTool='regrid2').regrid(grdori, regridTool='regrid2') sInterps['esmf linear'] = s.regrid(grid, regridTool='esmf', regridMethod = 'linear').regrid(grdori, regridTool='esmf', regridMethod = 'linear') diag = {} sInterps['libcf'] = s.regrid(grid, regridTool='libcf', diag=diag).regrid(grdori, regridTool='libcf', diag=diag) print diag diag = {} sInterps['esmf conserve'] = s.regrid(grid, regridTool='esmf', regridMethod = 'conserve', diag=diag).regrid(grdori, regridTool='esmf', regridMethod = 'conserve') print diag diff = abs(sInterps['regrid2'] - s).max() self.assertLess(diff, 63.0) diff = abs(sInterps['esmf linear'] - s).max() self.assertLess(diff, 45.0) diff = abs(sInterps['esmf conserve'] - s).max() self.assertLess(diff, 103.534) diff = abs(sInterps['libcf'] - s).max() self.assertLess(diff, 45.0) if PLOT: row = 0 for mth in sInterps: row += 1 pylab.subplot(2, 2, row) pylab.pcolor(sInterps[mth][0,...] - s[0,...], vmin = -10, vmax = 10) pylab.colorbar() pylab.title(mth + ' - original')
def executeOperations(self, task, _inputs): cdms2.setAutoBounds(2) t0 = time.time() self.logger.info( " Execute REGRID Task with metadata: " + str( task.metadata ) ) crsSpec = task.metadata.get("crs","") if( len(crsSpec) and (crsSpec[0] == '~') ): crsId = crsSpec[1:] grid_input = _inputs.get( crsId, None ) if not grid_input: raise Exception( "Can't find grid variable uid: " + crsId + ", variable uids = " + str( _inputs.keys() ) ) toGrid = grid_input.getGrid() else: crsToks = crsSpec.split('~') if( len(crsToks) > 1 ): if crsToks[0] == "gaussian": resolution = int(crsToks[1]) toGrid = cdms2.createGaussianGrid( resolution ) else: raise Exception( "Unrecognized grid type: " + crsToks[0]) else: gridSpec = task.metadata.get("gridSpec","") if not gridSpec: raise Exception( "Can't find crs spec in regrid kernel: " + str(crsToks)) toGrid = getGrid( gridSpec ) results = [] for input_id in task.inputs: _input = _inputs.get( input_id.split('-')[0] ) variable = _input.getVariable() ingrid = _input.getGrid() inlatBounds, inlonBounds = ingrid.getBounds() self.logger.info( " >> in LAT Bounds shape: " + str(inlatBounds.shape) ) self.logger.info( " >> in LON Bounds shape: " + str(inlonBounds.shape) ) outlatBounds, outlonBounds = toGrid.getBounds() self.logger.info( " >> out LAT Bounds shape: " + str(outlatBounds.shape) ) self.logger.info( " >> out LON Bounds shape: " + str(outlonBounds.shape) ) if( not ingrid == toGrid ): self.logger.info( " Regridding Variable {0} using grid {1} ".format( variable.id, str(toGrid) ) ) if self._debug: self.logger.info( " >> Input Data Sample: [ {0} ]".format( ', '.join( [ str( variable.data.flat[i] ) for i in range(20,90) ] ) ) ) self.logger.info( " >> Input Variable Shape: {0}, Grid Shape: {1} ".format( str(variable.shape), str([len(ingrid.getLatitude()),len(ingrid.getLongitude())] ))) result_var = variable.regrid( toGrid, regridTool="esmf", regridMethod="linear" ) self.logger.info( " >> Gridded Data Sample: [ {0} ]".format( ', '.join( [ str( result_var.data.flat[i] ) for i in range(20,90) ] ) ) ) results.append( self.createResult( result_var, _input, task ) ) t1 = time.time() self.logger.info(" @RRR@ Completed regrid operation for input variables: {0} in time {1}".format( str(_inputs.keys), (t1 - t0))) return results
def execOp( self, op, context, inputs ): t0 = time.time() opToks = op.split(".") module = opToks[0] opIdToks = opToks[1].split("-") opName = opIdToks[0] opId = opIdToks[1] crsToks = context.get("crs","gaussian~128").split("~") regridder = context.get("regridder","regrid2") crs = crsToks[0] resolution = int(crsToks[1]) if len(crsToks) > 1 else 128 if opName.lower() == "regrid": if crs == "gaussian": t42 = cdms2.createGaussianGrid( resolution ) results = [ input.regrid( t42 , regridTool=regridder ) for input in inputs ] self.logger.info( " >> Regridded variables in time {0}, nresults = {1}".format( (time.time()-t0), len(results) ) ) return results return []
def generate_user_defined_grid(cls, grid_type, grid_param, lat_bounds=None, lon_bounds=None): logger.info('Generating grid %r %r', grid_type, grid_param) if grid_type.lower() == 'uniform': result = re.match('^(.*)x(.*)$', grid_param) if result is None: raise Exception( f'Failed to parse uniform configuration from {grid_param}') start_lat, nlat, delta_lat = cls.parse_uniform_arg( result.group(1), -90.0, 180.0, lat_bounds) start_lon, nlon, delta_lon = cls.parse_uniform_arg( result.group(2), 0.0, 360.0, lon_bounds) grid = cdms2.createUniformGrid(start_lat, nlat, delta_lat, start_lon, nlon, delta_lon) logger.info( 'Created target uniform grid {} from lat {}:{}:{} lon {}:{}:{}' .format(grid.shape, start_lat, delta_lat, nlat, start_lon, delta_lon, nlon)) elif grid_type.lower() == 'gaussian': try: nlats = int(grid_param) except ValueError: raise Exception( 'Error converting gaussian parameter to an int') grid = cdms2.createGaussianGrid(nlats) logger.info(f'Created target gaussian grid {grid.shape}') else: raise Exception(f'Unknown grid type for regridding: {grid_type}') return grid
#!/usr/bin/env python import cdms2, numpy.ma, regrid2 as regrid, os, sys from regrid2 import Regridder from markError import clearError, markError, reportError from markError import get_sample_data_dir clearError() print 'Test 8: Regridding ...', ## lat = cdms2.createGaussianAxis(32) ## lon = cdms2.createUniformLongitudeAxis(0.0,64,360.0/64.) ## outgrid = cdms2.createRectGrid(lat,lon,'yx','gaussian') outgrid = cdms2.createGaussianGrid(32) f = cdms2.openDataset(os.path.join(get_sample_data_dir(), 'readonly.nc')) u = f.variables['u'] ingrid = u.getGrid() try: sh = ingrid.shape except: markError('Grid shape') regridf = Regridder(ingrid, outgrid) newu = regridf(u) if (abs(newu[0, 0, -1] - 488.4763488) > 1.e-3): markError('regrid', newu[0, 0, -1]) newu = u.regrid(outgrid)
import cdms2 import MV2 import sys f = cdms2.open(sys.prefix + "/sample_data/clt.nc") s = f('clt') S2 = MV2.masked_greater(s, 87) G = cdms2.createGaussianGrid(22) S3 = S2.regrid(G) assert (S3.max() < 87.1)
import cdms2 inputPath = "http://aims3.llnl.gov/thredds/dodsC/cmip5_css02_data/cmip5/output1/CMCC/CMCC-CESM/historical/mon/atmos/Amon/r1i1p1/tas/1/tas_Amon_CMCC-CESM_historical_r1i1p1_198001-198412.nc" dataset = cdms2.open(inputPath) resolution = 128 regridder = "regrid2" input = dataset("tas") t42 = cdms2.createGaussianGrid(resolution) result = input.regrid(t42, regridTool=regridder) axes = result.getAxisList() grid = result.getGrid() newDataset = cdms2.createDataset("/tmp/test") for axis in axes: newDataset.copyAxis(axis) newDataset.copyGrid(grid) newDataset.createVariableCopy(result) newDataset.close() print "."
def testGenmask(self): f = cdms2.open(os.path.join(egg_path, "navy_land.nc")) navy_frac_one = f('sftlf') g = navy_frac_one.getGrid() print(("SOURCE GRID:", g)) print(("LON:", g.getLongitude())) print(("LAT:", g.getLatitude())) navy_frac = navy_frac_one / 100. g = navy_frac.getGrid() print(("SOURCE GRID 2:", g)) print(("LON:", g.getLongitude())) print(("LAT:", g.getLatitude())) target = cdms2.open( os.path.join(cdat_info.get_sampledata_path(), 'clt.nc'))("clt", slice(0, 1)).getGrid() print(("TARGET:", navy_frac.getGrid())) mask = cdutil.generateLandSeaMask(target, navy_frac) target = cdms2.open( os.path.join(cdat_info.get_sampledata_path(), 'clt.nc'))("clt", slice(0, 1)) mask = cdutil.generateLandSeaMask(target, navy_frac) target = cdms2.createGaussianGrid(64) mask = cdutil.generateLandSeaMask(target) target = cdms2.open( os.path.join(cdat_info.get_sampledata_path(), 'clt.nc'))("clt", slice(0, 1), latitude=(15, 85), longitude=(-175, -65)).getGrid() mask = cdutil.generateLandSeaMask(target) self.assertEqual(mask.shape, (17, 23)) good = numpy.array([ [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 ], [ 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 ], [ 1.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 1.0 ], [ 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0 ], [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0 ] ]) #good = numpy.ma.masked_equal(good,1.0) self.assertTrue(numpy.ma.allclose(mask.filled(3.) - good, 0))
import cdms2 import cdat_info import os f = cdms2.open(os.path.join(cdat_info.get_sampledata_path(), "clt.nc")) s = f("clt", slice(0, 1)) g = cdms2.createGaussianGrid(64) gl = cdms2.createZonalGrid(g) regridded = s.regrid(gl)
import cdms2,sys,cdutil,os,cdat_info f=cdms2.open(os.path.join(cdat_info.get_prefix(),"sample_data","navy_land.nc")) navy_frac = f("sftlf")/100. target = cdms2.open(os.path.join(cdat_info.get_prefix(),'sample_data','clt.nc'))("clt",slice(0,1)).getGrid() mask = cdutil.generateLandSeaMask(target,navy_frac) target = cdms2.open(os.path.join(cdat_info.get_prefix(),'sample_data','clt.nc'))("clt",slice(0,1)) mask = cdutil.generateLandSeaMask(target,navy_frac) target=cdms2.createGaussianGrid(64) mask = cdutil.generateLandSeaMask(target) target = cdms2.open(os.path.join(cdat_info.get_prefix(),'sample_data','clt.nc'))("clt",slice(0,1),latitude=(15,85),longitude=(-175,-65)).getGrid() mask = cdutil.generateLandSeaMask(target) #import vcs #x=vcs.init() #x.plot(mask) #raw_input()
lon=cdms2.createAxis(MV2.arange(100)*3.6) lon.designateLongitude() lon.units="degrees_east" lon.id="longitude" lat = cdms2.createAxis(MV2.arange(100)*1.8-90.) lat.id="latitude" lat.designateLatitude() lat.units="degrees_north" lev = cdms2.createAxis([1000.]) lev.id="plev" lev.designateLevel() lev.units="hPa" t=cdms2.createAxis([0,31.]) t.id="time" t.designateTime() t.units="days since 2014" cdutil.setTimeBoundsMonthly(t) a.setAxisList((t,lev,lat,lon)) a=MV2.masked_less(a,.5) grd=cdms2.createGaussianGrid(64) a=a.ascontiguous() a=a.regrid(grd,regridTool="regrid2") a=cdutil.averager(a,axis='txy') assert a[0]==0.7921019540305255
## Automatically adapted for numpy.oldnumeric Aug 01, 2007 by #!/usr/bin/env python import cdms2,numpy.ma, regrid2 as regrid, os, sys from regrid2 import Regridder from markError import clearError,markError,reportError clearError() print 'Test 8: Regridding ...', ## lat = cdms2.createGaussianAxis(32) ## lon = cdms2.createUniformLongitudeAxis(0.0,64,360.0/64.) ## outgrid = cdms2.createRectGrid(lat,lon,'yx','gaussian') outgrid = cdms2.createGaussianGrid(32) pth = os.path.dirname(os.path.abspath(__file__)) f = cdms2.open(os.path.join(pth,'readonly.nc')) u = f.variables['u'] ingrid = u.getGrid() try: sh = ingrid.shape except: markError('Grid shape') regridf = Regridder(ingrid, outgrid) newu = regridf(u) if (abs(newu[0,0,-1]-488.4763488) > 1.e-3): markError('regrid',newu[0,0,-1]) newu = u.regrid(outgrid,regridTool='regrid2') if (abs(newu[0,0,-1]-488.4763488) > 1.e-3): markError('regrid',newu[0,0,-1])
import cdms2,cdat_info import MV2 import sys f = cdms2.open(cdat_info.get_prefix() + "/sample_data/clt.nc") s = f('clt') S2 = MV2.masked_greater(s, 87) G = cdms2.createGaussianGrid(22) S3 = S2.regrid(G) assert(S3.max() < 87.1)
import cdms2, sys, cdutil, os, cdat_info f = cdms2.open(os.path.join(cdat_info.get_sampledata_path(), "navy_land.nc")) navy_frac = f("sftlf") / 100. target = cdms2.open(os.path.join(cdat_info.get_sampledata_path(), 'clt.nc'))("clt", slice(0, 1)).getGrid() mask = cdutil.generateLandSeaMask(target, navy_frac) target = cdms2.open(os.path.join(cdat_info.get_sampledata_path(), 'clt.nc'))("clt", slice(0, 1)) mask = cdutil.generateLandSeaMask(target, navy_frac) target = cdms2.createGaussianGrid(64) mask = cdutil.generateLandSeaMask(target) target = cdms2.open(os.path.join(cdat_info.get_sampledata_path(), 'clt.nc'))("clt", slice(0, 1), latitude=(15, 85), longitude=(-175, -65)).getGrid() mask = cdutil.generateLandSeaMask(target) #import vcs #x=vcs.init() #x.plot(mask) #raw_input()
def testRegrid2(self): outgrid = cdms2.createGaussianGrid(32) pth = os.path.dirname(os.path.abspath(__file__)) f = self.getDataFile('readonly.nc') u = f.variables['u'] ingrid = u.getGrid() sh = ingrid.shape regridf = Horizontal(ingrid, outgrid) newu = regridf(u) self.assertLess(abs(newu[0, 0, -1] - 488.4763488), 1.e-3) newu = u.regrid(outgrid, regridTool='regrid2') self.assertLess(abs(newu[0, 0, -1] - 488.4763488), 1.e-3) # Regrid TV tv = u.subSlice(0) newtv = regridf(tv) self.assertLess(abs(newtv[0, 0, -1] - 488.4763488), 1.e-3) newtv = tv.regrid(outgrid, regridTool='regrid2') self.assertLess(abs(newtv[0, 0, -1] - 488.4763488), 1.e-3) # Regrid numpy.ma ma = u[0] newma = regridf(ma) # Force slice result to be a scalar self.assertLess(abs(newma[0][-1] - 488.4763488), 1.e-3) # Regrid numpy numar = numpy.ma.filled(u[0]) newar = regridf(numar) self.assertLess(abs(newar[0][-1] - 488.4763488), 1.e-3) # Regrid masked Variable umasked = f.variables['umasked'] newum = regridf(umasked) self.assertLess(abs(newum[0, 0, -1] - 488.4763488), 1.e-3) # Set explicit missing variable numar = numpy.ma.filled(umasked[0]) newar = regridf(numar, missing=-99.9) self.assertLess(abs(newar[0][-1] - 488.4763488), 1.e-3) # Set explicit mask mask = umasked.subRegion().mask[0] newar = regridf(numar, mask=mask) self.assertLess(abs(newar[0][-1] - 488.4763488), 1.e-3) # Set the input grid mask ingrid.setMask(mask) regridf2 = Horizontal(ingrid, outgrid) newar = regridf2(numar) self.assertLess(abs(newar[0][-1] - 488.4763488), 1.e-3) # Dataset g = self.getDataFile('test.xml') u = g.variables['u'] outgrid = cdms2.createGaussianGrid(24) regridf3 = Horizontal(u.getGrid(), outgrid) try: unew = regridf3(u) except BaseException: markError('regrid dataset variable') lon2 = numpy.ma.array([ 90., 101.25, 112.5, 123.75, 135., 146.25, 157.5, 168.75, 180., 191.25, 202.5, 213.75, 225., 236.25, 247.5, 258.75, ]) lat2 = numpy.ma.array([ -42., -30., -18., -6., 6., 18., 30., 42., ]) grid2 = cdms2.createGenericGrid(lat2, lon2) b1, b2 = grid2.getBounds() grid2.setBounds(b1, b2) latw, lonw = grid2.getWeights() g = cdms2.createGaussianGrid(16) levs = numpy.array([1.0, 3.0, 5.0]) lev = cdms2.createAxis(levs, id='level') levsout = numpy.array([2.0, 4.0]) levout = cdms2.createAxis(levsout, id='level') dat = numpy.zeros((3, 16, 32), numpy.float32) dat2 = numpy.zeros((2, 16, 32), numpy.float32) dat[0] = 2.0 dat[1] = 4.0 dat[2] = 6.0 var = cdms2.createVariable(dat, axes=(lev, g), attributes={'units': 'N/A'}, id='test') result = var.pressureRegrid(levout) self.assertLess(abs(result[0, 0, 0] - 3.26185), 1.e-4) # Test cross-section regridder -------------------------------- latin = cdms2.createGaussianAxis(16) latout = cdms2.createGaussianAxis(24) levsin = numpy.array([1.0, 3.0, 5.0]) lev = cdms2.createAxis(levsin, id='level') levsout = numpy.array([2.0, 4.0]) levout = cdms2.createAxis(levsout, id='level') dat = numpy.zeros((3, 16), numpy.float32) dat[0] = 2.0 dat[1] = 4.0 dat[2] = 6.0 var = cdms2.createVariable(dat, axes=(lev, latin), attributes={'units': 'N/A'}, id='test') dat2 = var.crossSectionRegrid(levout, latout) self.assertLess(abs(dat2[0, 0] - 3.26185), 1.e-4)