def XXtest5_regrid(self): srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') dstData = so.regrid(clt.getGrid(), regridTool='esmf', regridMethod='conserve') if self.pe == 0: dstDataMask = (dstData == so.missing_value) dstDataFltd = dstData * (1 - dstDataMask) zeroValCnt = (dstData == 0).sum() if so.missing_value > 0: dstDataMin = dstData.min() dstDataMax = dstDataFltd.max() else: dstDataMin = dstDataFltd.min() dstDataMax = dstData.max() zeroValCnt = (dstData == 0).sum() print 'Number of zero valued cells', zeroValCnt print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax) self.assertLess(dstDataMax, so.max()) if PLOT: pylab.figure(1) pylab.pcolor(so, vmin=20, vmax=40) pylab.colorbar() pylab.title('so') pylab.figure(2) pylab.pcolor(dstData, vmin=20, vmax=40) pylab.colorbar() pylab.title('dstData')
def test_2d_esmf_conserv(self): print 'running test_2d_esmf_conserv...' f = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = f('so')[0, 0, :, :] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt')[0, :, :] tic = time.time() soInterp = so.regrid(clt.getGrid(), regridTool='ESMF', regridMethod='Conservative') soInterpInterp = soInterp.regrid(so.getGrid(), regridTool='ESMF', regridMethod='Conservative') toc = time.time() print 'time to interpolate (ESMF conservative) forward/backward: ', toc - tic ntot = reduce(operator.mul, so.shape) avgdiff = numpy.sum(so - soInterpInterp) / float(ntot) print 'avgdiff = ', avgdiff if PLOT: pylab.figure(2) pylab.subplot(2, 2, 1) pylab.pcolor(so, vmin=20.0, vmax=40.0) pylab.colorbar() pylab.title('ESMF conserve regrid: so') pylab.subplot(2, 2, 2) pylab.pcolor(soInterp, vmin=20.0, vmax=40.0) pylab.colorbar() pylab.title('ESMF conserve regrid: soInterp') pylab.subplot(2, 2, 3) pylab.pcolor(soInterpInterp, vmin=20.0, vmax=40.0) pylab.colorbar() pylab.title('ESMF conserve regrid: soInterpInterp') pylab.subplot(2, 2, 4) pylab.pcolor(so - soInterpInterp, vmin=-0.5, vmax=0.5) pylab.colorbar() pylab.title('ESMF conserve regrid: error')
def setUp(self): """ Set up the grids to pass to mvGenericRegrid """ self.doPlots = False self.clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt')[0, ...] # Convert to curvilinear cds, cds_forBounds = [], [] cds.append(self.clt.getLatitude()) cds.append(self.clt.getLongitude()) cds_forBounds.append(self.clt.getLatitude()) cds_forBounds.append(self.clt.getLongitude()) self.cltGrid, nDims = regrid2.gsRegrid.makeCurvilinear(cds) self.cltInterp = numpy.array(self.clt) * 0.0 + self.clt.missing_value self.cltInterpInterp = numpy.array( self.clt) * 0.0 + self.clt.missing_value # Salinity check f = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') self.so = f('so') soGrid = [] soGrid.append(self.so.getLatitude()) soGrid.append(self.so.getLongitude()) self.soGrid = soGrid self.soInterp = numpy.array(self.clt) * 0.0 + self.so.missing_value self.tol = 1e2 self.rank = 0 self.size = 1 if HAS_MPI: self.comm = MPI.COMM_WORLD self.rank = self.comm.Get_rank() self.size = self.comm.Get_size()
def XXtest5_regrid(self): srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') dstData = so.regrid(clt.getGrid(), regridTool = 'esmf', regridMethod='conserve') if self.pe == 0: dstDataMask = (dstData == so.missing_value) dstDataFltd = dstData * (1 - dstDataMask) zeroValCnt = (dstData == 0).sum() if so.missing_value > 0: dstDataMin = dstData.min() dstDataMax = dstDataFltd.max() else: dstDataMin = dstDataFltd.min() dstDataMax = dstData.max() zeroValCnt = (dstData == 0).sum() print 'Number of zero valued cells', zeroValCnt print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax) self.assertLess(dstDataMax, so.max()) if PLOT: pylab.figure(1) pylab.pcolor(so, vmin=20, vmax=40) pylab.colorbar() pylab.title('so') pylab.figure(2) pylab.pcolor(dstData, vmin=20, vmax=40) pylab.colorbar() pylab.title('dstData')
def Xtest2_libCFRegrid(self): srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') srcGrd = [so.getGrid().getLatitude(), so.getGrid().getLongitude()] dstGrd = [clt.getGrid().getLatitude(), clt.getGrid().getLongitude()] # create regrid object r = regrid2.mvLibCFRegrid.LibCFRegrid( srcGrd, dstGrd, srcGridMask=(so == so.missing_value), srcBounds=None, mkCyclic=False, handleCut=False) # compute weights r.computeWeights(nitermax=20, tolpos=0.01) # create dst data container dstShp = r.getDstGrid()[0].shape dstData = numpy.ones(dstShp, so.dtype) * so.missing_value # interpolate r.apply(so, dstData) # checks dstDataMask = (dstData == so.missing_value) dstDataFltd = dstData * (1 - dstDataMask) if so.missing_value > 0: dstDataMin = dstData.min() dstDataMax = dstDataFltd.max() else: dstDataMin = dstDataFltd.min() dstDataMax = dstData.max() print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax) self.assertGreater(dstDataMin, so.min()) self.assertLess(dstDataMax, so.max())
def Xtest3_genericRegrid(self): srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') srcGrd = [so.getGrid().getLatitude(), so.getGrid().getLongitude()] dstGrd = [clt.getGrid().getLatitude(), clt.getGrid().getLongitude()] # create regrid object r = regrid2.mvGenericRegrid.GenericRegrid(srcGrd, dstGrd, so.dtype, regridMethod='linear', regridTool='libcf', srcGridMask=(so == so.missing_value), srcBounds=None, srcGridAreas=None, dstGridMask=None, dstBounds=None, dstGridAreas=None, mkCyclic=False, handleCut=False) # compute weights r.computeWeights(nitermax=20, tolpos=0.01) # create dst data container dstShp = r.getDstGrid()[0].shape dstData = numpy.ones(dstShp, so.dtype) * so.missing_value # interpolate r.apply(so, dstData) # checks dstDataMask = (dstData == so.missing_value) dstDataFltd = dstData * (1 - dstDataMask) if so.missing_value > 0: dstDataMin = dstData.min() dstDataMax = dstDataFltd.max() else: dstDataMin = dstDataFltd.min() dstDataMax = dstData.max() print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax) self.assertGreater(dstDataMin, so.min()) self.assertLess(dstDataMax, so.max())
def test4_cdmsRegrid(self): srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') # create regrid object r = cdms2.CdmsRegrid(so.getGrid(), clt.getGrid(), so.dtype, regridMethod='linear', regridTool='libcf', srcGridMask=(so == so.missing_value), srcGridAreas=None, dstGridMask=None, dstGridAreas=None, mkCyclic=False, handleCut=False) dstData = r(so) # checks dstDataMask = (dstData == so.missing_value) dstDataFltd = dstData * (1 - dstDataMask) if so.missing_value > 0: dstDataMin = dstData.min() dstDataMax = dstDataFltd.max() else: dstDataMin = dstDataFltd.min() dstDataMax = dstData.max() print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax) self.assertGreater(dstDataMin, so.min()) self.assertLess(dstDataMax, so.max())
def test2_varRegrid(self): print print 'test2_varRegrid' srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') diag = {'srcAreas': None, 'dstAreas': None, 'srcAreaFractions': None, 'dstAreaFractions': None} soInterp = so.regrid(clt.getGrid(), regridTool = 'esmf', regridMethod='conserve', diag = diag) if self.pe == 0: totSrcArea = diag['srcAreas'].sum() totDstArea = diag['dstAreas'].sum() totSrcFrac = diag['srcAreaFractions'].sum() self.assertEqual(numpy.isnan(totSrcFrac).sum(), 0) self.assertLess(abs(totSrcArea - 4*pi)/(4*pi), 0.02) self.assertLess(abs(totDstArea - 4*pi)/(4*pi), 0.01) soMass = (so*diag['srcAreas']).sum() inMass = (soInterp*diag['dstAreas']).sum() print soMass, inMass diff = abs(soMass - inMass)/soMass self.assertLess(diff, 7.e-7) if PLOT: pylab.subplot(1, 2, 1) pylab.pcolor(so, vmin = 20, vmax = 40) pylab.colorbar() pylab.title('so') pylab.subplot(1, 2, 2) pylab.pcolor(soInterp, vmin = 20, vmax = 40) pylab.colorbar() pylab.title('soInterp')
def Xtest1_gsRegrid(self): srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') srcGrd = [so.getGrid().getLatitude(), so.getGrid().getLongitude()] dstGrd = [clt.getGrid().getLatitude(), clt.getGrid().getLongitude()] # create regrid object r = regrid2.gsRegrid.Regrid(srcGrd, dstGrd, src_bounds=None, mkCyclic=False, handleCut=False) # set mask r.setMask( (so == so.missing_value) ) # compute interpolation weights r.computeWeights(nitermax=20, tolpos=0.01) # make sure most of the weights have ben found print 'ratio of valid over number of nodes: %f' % (r.getNumValid()/float(r.getNumDstPoints())) self.assertGreater(r.getNumValid(), 0) # create dst data container dstShp = r.getDstGrid()[0].shape print 'dst data shape: ', dstShp dstData = numpy.ones(dstShp, so.dtype) * so.missing_value # interpolate r.apply(so, dstData) # checks dstDataMask = (dstData == so.missing_value) dstDataFltd = dstData * (1 - dstDataMask) if so.missing_value > 0: dstDataMin = dstData.min() dstDataMax = dstDataFltd.max() else: dstDataMin = dstDataFltd.min() dstDataMax = dstData.max() print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax) self.assertGreater(dstDataMin, so.min()) self.assertLess(dstDataMax, so.max())
def test4_cdmsRegrid(self): srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] srcGridMask = numpy.array((so == so.missing_value), numpy.int32) clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') # create regrid object r = cdms2.CdmsRegrid(so.getGrid(), clt.getGrid(), dtype=so.dtype, regridMethod='conserve', regridTool='esmf', srcGridMask=srcGridMask, srcGridAreas=None, dstGridMask=None, dstGridAreas=None) dstData = r(so) # checks if self.pe == 0: dstDataMask = (dstData == so.missing_value) print 'number of masked values = ', dstDataMask.sum() self.assertTrue(str(type(dstData)), str(type(clt))) dstData.mask = (dstData == so.missing_value) dstDataMin = dstData.min() dstDataMax = dstData.max() zeroValCnt = (dstData == 0).sum() print 'Number of zero valued cells', zeroValCnt print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax) self.assertLess(dstDataMax, so.max())
def setUp(self): """ Set up the grids to pass to mvGenericRegrid """ self.doPlots = False self.clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt')[0, ...] # Convert to curvilinear cds, cds_forBounds = [], [] cds.append(self.clt.getLatitude()) cds.append(self.clt.getLongitude()) cds_forBounds.append(self.clt.getLatitude()) cds_forBounds.append(self.clt.getLongitude()) self.cltGrid, nDims = regrid2.gsRegrid.makeCurvilinear(cds) self.cltInterp = numpy.array(self.clt) * 0.0 + self.clt.missing_value self.cltInterpInterp = numpy.array(self.clt) * 0.0 + self.clt.missing_value # Salinity check f = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') self.so = f('so') soGrid = [] soGrid.append(self.so.getLatitude()) soGrid.append(self.so.getLongitude()) self.soGrid = soGrid self.soInterp = numpy.array(self.clt) * 0.0 + self.so.missing_value self.tol = 1e2 self.rank = 0 self.size = 1 if HAS_MPI: self.comm = MPI.COMM_WORLD self.rank = self.comm.Get_rank() self.size = self.comm.Get_size()
def test4_cdmsRegrid(self): srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] srcGridMask = numpy.array((so == so.missing_value) , numpy.int32) clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') # create regrid object r = cdms2.CdmsRegrid(so.getGrid(), clt.getGrid(), dtype=so.dtype, regridMethod='conserve', regridTool='esmf', srcGridMask=srcGridMask, srcGridAreas=None, dstGridMask=None, dstGridAreas=None) dstData = r(so) # checks if self.pe == 0: dstDataMask = (dstData == so.missing_value) print 'number of masked values = ', dstDataMask.sum() self.assertTrue(str(type(dstData)), str(type(clt))) dstData.mask = (dstData == so.missing_value) dstDataMin = dstData.min() dstDataMax = dstData.max() zeroValCnt = (dstData == 0).sum() print 'Number of zero valued cells', zeroValCnt print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax) self.assertLess(dstDataMax, so.max())
def test_2d_esmf(self): #print 'running test_2d_esmf...' f = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = f('so')[0, 0, :, :] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt')[0, :, :] tic = time.time() soInterp = so.regrid(clt.getGrid(), regridTool='ESMF') #, periodicity=1) soInterpInterp = soInterp.regrid(so.getGrid(), regridTool='ESMF') toc = time.time() #print 'time to interpolate (ESMF linear) forward/backward: ', toc - tic mype = MPI.COMM_WORLD.Get_rank() if mype == 0: ntot = reduce(operator.mul, so.shape) avgdiff = numpy.sum(so - soInterpInterp) / float(ntot) #print 'avgdiff = ', avgdiff self.assertLess(abs(avgdiff), 5.2e18) if PLOT: pylab.figure(2) pylab.pcolor(abs(so - soInterpInterp), vmin=0.0, vmax=1.0) pylab.colorbar() pylab.title('ESMF linear')
def buildDate(): """ Returns the date python was built, and the date VCDAT was built """ fnm = os.path.join(cdat_info.get_prefix(),'bin','python') a=os.stat(fnm)[8] p=time.asctime(time.localtime(a)) fnm = os.path.join(cdat_info.get_prefix(),'bin','cdat') a=os.stat(fnm)[8] return p, time.asctime(time.localtime(a))
def setUp(self): self.so = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc')('so') self.tasGood = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/tas_Amon_HadGEM2-A_amip_r1i2p1_197809-200811_2timesteps.nc')('tas') self.tasBad = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/tas_Amon_ACCESS1-0_historical_r1i1p1_185001-189912_2timesteps.nc')('tas')
def buildDate(): """ Returns the date python was built, and the date VCDAT was built """ fnm = os.path.join(cdat_info.get_prefix(), 'bin', 'python') a = os.stat(fnm)[8] p = time.asctime(time.localtime(a)) fnm = os.path.join(cdat_info.get_prefix(), 'bin', 'cdat') a = os.stat(fnm)[8] return p, time.asctime(time.localtime(a))
def setUp(self): fso = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') self.soLevel = fso('so')[0, 0:5, ...] self.soTime = fso('so')[:, 0, ...] self.so = fso('so')[:, 0:5, ...] fclt = cdms2.open(cdat_info.get_prefix() + "/sample_data/clt.nc") self.clt = fclt('clt')
def setUp(self): fso = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') self.soLevel = fso('so')[0,0:5,...] self.soTime = fso('so')[:,0,...] self.so = fso('so')[:,0:5,...] fclt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc') self.clt = fclt('clt')
def setUp(self): """ Set up the grids to pass to mvGenericRegrid """ f = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') self.so = f('so') g = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc') self.clt = g('clt')
def test_test2(self): """ 2D ESMP """ u = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('u')[0, 0,...] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt')[0, ...] ctlOnUGrid = clt.regrid( u.getGrid(), regridTool = "ESMP" ) #print 'ctlOnUGrid.getGrid() = ', type(ctlOnUGrid.getGrid()) self.assertRegexpMatches(str(type(ctlOnUGrid.getGrid())), "cdms2.grid.TransientRectGrid")
def setUp(self): access = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') self.access = access('so')[0, 0, ...] self.tasGood = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/tas_Amon_HadGEM2-A_amip_r1i2p1_197809-200811_2timesteps.nc')('tas') giss = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_GISS-E2-R_historicalNat_r5i1p1_185001-187512_2timesteps.nc') self.giss = giss('so')[0,0,...]
def test_test2(self): """ 2D ESMP """ u = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('u')[0, 0, ...] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt')[0, ...] ctlOnUGrid = clt.regrid(u.getGrid(), regridTool="ESMP") #print 'ctlOnUGrid.getGrid() = ', type(ctlOnUGrid.getGrid()) self.assertRegexpMatches(str(type(ctlOnUGrid.getGrid())), "cdms2.grid.TransientRectGrid")
def Xtest3(self): """ Test from Kate Marvel As the following code snippet demonstrates, regridding a cdms2.tvariable.TransientVariable instance using regridTool='regrid2' results in a new array that is masked everywhere. regridTool='esmf' and regridTool='libcf' both work as expected. This is similar to the original test but we construct our own uniform grid. This should passes. """ import cdms2 as cdms import numpy as np filename = cdat_info.get_prefix() + '/sample_data/clt.nc' a=cdms.open(filename) data=a('clt')[0,...] print data.mask #verify this data is not masked GRID = cdms.grid.createUniformGrid(-90.0, 23, 8.0, -180.0, 36, 10.0, order="yx", mask=None) test_data=data.regrid(GRID,regridTool='regrid2') # check that the mask does not extend everywhere... self.assertNotEqual(test_data.mask.sum(), test_data.size) if PLOT: pylab.subplot(2, 1, 1) pylab.pcolor(data[...]) pylab.title('data') pylab.subplot(2, 1, 2) pylab.pcolor(test_data[...]) pylab.title('test_data (interpolated data)') pylab.show()
def Xtest2(self): """ Test from Kate Marvel As the following code snippet demonstrates, regridding a cdms2.tvariable.TransientVariable instance using regridTool='regrid2' results in a new array that is masked everywhere. regridTool='esmf' and regridTool='libcf' both work as expected. This passes. """ import cdms2 as cdms import numpy as np filename = cdat_info.get_prefix() + '/sample_data/clt.nc' a=cdms.open(filename) data=a('clt')[0,...] print data.mask #verify this data is not masked GRID= data.getGrid() # input = output grid, passes test_data=data.regrid(GRID,regridTool='regrid2') # check that the mask does not extend everywhere... self.assertNotEqual(test_data.mask.sum(), test_data.size) if PLOT: pylab.subplot(2, 1, 1) pylab.pcolor(data[...]) pylab.title('data') pylab.subplot(2, 1, 2) pylab.pcolor(test_data[...]) pylab.title('test_data (interpolated data)') pylab.show()
def Xtest2(self): """ Test from Kate Marvel As the following code snippet demonstrates, regridding a cdms2.tvariable.TransientVariable instance using regridTool='regrid2' results in a new array that is masked everywhere. regridTool='esmf' and regridTool='libcf' both work as expected. This passes. """ import cdms2 as cdms import numpy as np filename = cdat_info.get_prefix() + '/sample_data/clt.nc' a = cdms.open(filename) data = a('clt')[0, ...] print data.mask #verify this data is not masked GRID = data.getGrid() # input = output grid, passes test_data = data.regrid(GRID, regridTool='regrid2') # check that the mask does not extend everywhere... self.assertNotEqual(test_data.mask.sum(), test_data.size) if PLOT: pylab.subplot(2, 1, 1) pylab.pcolor(data[...]) pylab.title('data') pylab.subplot(2, 1, 2) pylab.pcolor(test_data[...]) pylab.title('test_data (interpolated data)') pylab.show()
def generateLandSeaMask(target,source=None,threshold_1 = .2, threshold_2 = .3,regridTool='regrid2'): """ Generates a best guess mask on any rectilinear grid, using the method described in PCMDI's report #58 see: http://www-pcmdi.llnl.gov/publications/ab58.html Input: target: either a MV2 object with a grid, or a cdms2 grid (rectilinear grid only) source: A fractional (0 to 1.) land sea mask, where 1 means all land threshold_1 (optional): criteria 1 for detecting cells with possible increment see report for detail difference threshold threshold_2 (optional): criteria 2 for detecting cells with possible increment see report for detail water/land content threshold regridTool: which cdms2 regridder tool to use, default is regrid2 Output: landsea maks on target grid """ cdat_info.pingPCMDIdb("cdat","cdutil.generateLandSeaMask") if cdms2.isVariable(target): target = target.getGrid() if target is None: raise Exception,"Error target data passed do not have a grid" if not isinstance(target,cdms2.grid.TransientRectGrid): raise Exception, "Error: target grid must be rectilinear" if source is None: source = cdms2.open(os.path.join(cdat_info.get_prefix(),'share','cdutil','navy_land.nc'))('sftlf') try: navy_frac_t = source.regrid(target,regridTool='regrid2') except Exception,err: raise "error, cannot regrid source data to target, got error message: %s" % err
def test1_regrid(self): clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt')[0,...] ta = cdms2.open(cdat_info.get_prefix() + '/sample_data/ta_ncep_87-6-88-4.nc')('ta')[0, 0,...] diag = {} cltInterp = clt.regrid( ta.getGrid(), regridTool = 'libcf', mkCyclic = True, verbose = True, diag = diag ) print cltInterp.sum() n = reduce(lambda x,y: x*y, cltInterp.shape) self.assertLess(abs(cltInterp.sum() - 696921.0)/n, 0.3) if PLOT: pylab.pcolor(ta.getLongitude()[:], ta.getLatitude()[:], cltInterp) pylab.colorbar() pylab.title('cltInterp') self.assertEqual(True, True)
def testSingleTimeSingleElev(self): """ Interpolate over one level/time """ f = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc') clt = f('clt') v = f('v')[0,0,...] srcGrid = v.getGrid() dstGrid = clt.getGrid() ro = CdmsRegrid(srcGrid = srcGrid, dstGrid = dstGrid, dtype = v.dtype) vInterp = ro(v) print 'min/max of v: %f %f' % (v.min(), v.max()) print 'min/max of vInterp: %f %f' % (vInterp.min(), vInterp.max()) if PLOT: pl.figure() pl.pcolor(vInterp, vmin=-20, vmax=20) pl.title('testSingleTimeSingleElev: vInterp') pl.colorbar()
def Xtest_test0(self): """ Test size, rank etc. """ da = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc', 'r')('clt') rk = da.getMPIRank() sz = da.getMPISize() # make the data rank dependent da[:] = rk # create the halo regions da.exposeHalo(ghostWidth=2) # ellipsis for one side slce = da.getHaloEllipsis(side=(1, 0, 0)) # access remote data chunk = da.fetchHaloData((rk - 1) % sz, side=(1, 0, 0)) # check diff = float('inf') if rk != 0: diff = abs(numpy.sum(da[slce] - 1 - chunk)) else: diff = abs(numpy.sum(da[slce] + sz - 1 - chunk)) success = False if diff < 1.e-10: success = True self.assertEqual(success, True) # clean up da.freeHalo()
def Xtest_test0(self): """ Test size, rank etc. """ da = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc', 'r')('clt') rk = da.getMPIRank() sz = da.getMPISize() # make the data rank dependent da[:] = rk # create the halo regions da.exposeHalo(ghostWidth = 2) # ellipsis for one side slce = da.getHaloEllipsis(side=(1, 0, 0)) # access remote data chunk = da.fetchHaloData((rk-1)%sz, side=(1, 0, 0)) # check diff = float('inf') if rk != 0: diff = abs(numpy.sum(da[slce] - 1 - chunk)) else: diff = abs(numpy.sum(da[slce] + sz - 1 - chunk)) success = False if diff < 1.e-10: success = True self.assertEqual(success, True) # clean up da.freeHalo()
def test5_regrid(self): srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') dstData = so.regrid(clt.getGrid()) dstDataMask = (dstData == so.missing_value) dstDataFltd = dstData * (1 - dstDataMask) if so.missing_value > 0: dstDataMin = dstData.min() dstDataMax = dstDataFltd.max() else: dstDataMin = dstDataFltd.min() dstDataMax = dstData.max() print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax) self.assertGreater(dstDataMin, so.min()) self.assertLess(dstDataMax, so.max())
def test_3(self): f = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc') clt = f('clt')[:, :, :] u = f('u')[:, :, :] cltInterp = clt.regrid(u.getGrid(), regridTool='regrid2') avgClt = clt.sum()/float(reduce(operator.mul, clt.shape)) avgCltInterp = cltInterp.sum()/float(reduce(operator.mul, cltInterp.shape)) self.assertLess(abs(avgClt - avgCltInterp), 0.3)
def XXtest3_genericRegrid(self): srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] srcGridMask = numpy.array((so == so.missing_value), numpy.int32) clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') srcGrd = [so.getGrid().getLatitude(), so.getGrid().getLongitude()] srcBounds = cdms2.mvCdmsRegrid.getBoundList(srcGrd) dG = clt.getGrid().toCurveGrid() dstGrd = [dG.getLatitude(), dG.getLongitude()] dstBounds = cdms2.mvCdmsRegrid.getBoundList(dstGrd) # create regrid object r = regrid2.mvGenericRegrid.GenericRegrid( [numpy.array(coord) for coord in srcGrd], [numpy.array(coord) for coord in dstGrd], dtype=so.dtype, regridMethod='conserve', regridTool='esmf', srcGridMask=srcGridMask, srcBounds=srcBounds, srcGridAreas=None, dstGridMask=None, dstBounds=dstBounds, dstGridAreas=None) # compute weights r.computeWeights() # create dst data container dstShp = dG.getLatitude().shape dstData = numpy.ones(dstShp, so.dtype) * so.missing_value # interpolate r.apply(numpy.array(so), dstData, rootPe=0) if self.pe == 0: # checks dstDataMask = (dstData == so.missing_value) print 'number of masked values = ', dstDataMask.sum() dstDataFltd = dstData * (1 - dstDataMask) if so.missing_value > 0: dstDataMin = dstData.min() dstDataMax = dstDataFltd.max() else: dstDataMin = dstDataFltd.min() dstDataMax = dstData.max() print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax) self.assertLess(dstDataMax, so.max())
def test_3(self): f = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc') clt = f('clt')[:, :, :] u = f('u')[:, :, :] cltInterp = clt.regrid(u.getGrid(), regridTool='regrid2') avgClt = clt.sum() / float(reduce(operator.mul, clt.shape)) avgCltInterp = cltInterp.sum() / float( reduce(operator.mul, cltInterp.shape)) self.assertLess(abs(avgClt - avgCltInterp), 0.3)
def test3(self): """ 2D + level """ print 'This is a known failure for now. ESMF are looking into the error' u = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('u')[0, :,...] uCart = u.regrid( u.getGrid(), regridTool='esmf', regridMethod='linear', coordSys = 'cart', periodicity = 1) uDegr = u.regrid( u.getGrid(), regridTool='esmf', regridMethod='linear', coordSys = 'deg', periodicity = 1) n = reduce(lambda x,y: x*y, uCart.shape) mask = (u == u.missing_value) if PLOT: import matplotlib.pylab as pl fig = pl.figure() fig.add_subplot(2,1,1) pl.pcolor(u.getLongitude()[:], u.getLatitude()[:], uCart[1,...], vmin = 0) pl.colorbar() pl.title('Cartiesian') fig.add_subplot(2,1,2) pl.pcolor(u.getLongitude()[:], u.getLatitude()[:], uDegr[1,...], vmin = 0) pl.colorbar() pl.title('Degrees') pl.show() if PRINT: print print 'Level 0, u.min() = %11.3f, uInterp.min() = %11.3f' % \ (u[0,...].min(), uInterp[0,...].min()) print 'Level 1, u.min() = %11.3f, uInterp.min() = %11.3f' % \ (u[1,...].min(), uInterp[1,...].min()) print '\nFor indices 52,59 and 60, 68' print 'm means missing' print 'd means uInterp < u' print '. means uInterp == u' for i in range(52, 59): string = "" for j in range(60, 68): if uInterp.mask[1,i,j] == True: string = string+"m" elif (uInterp[1,i,j] < u[1,i,j]): string = string+"d" else: string = string + "." print string print diff = abs(numpy.sum(u*(1-mask) - uCart)/float(n)) self.assertLess(diff, 1.e-3) diff = abs(numpy.sum(u*(1-mask) - uDegr)/float(n)) self.assertLess(diff, 1.e-3)
def XXtest3_genericRegrid(self): srcF = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF('so')[0, 0, ...] srcGridMask = numpy.array((so == so.missing_value) , numpy.int32) clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt') srcGrd = [so.getGrid().getLatitude(), so.getGrid().getLongitude()] srcBounds = cdms2.mvCdmsRegrid.getBoundList(srcGrd) dG = clt.getGrid().toCurveGrid() dstGrd = [dG.getLatitude(), dG.getLongitude()] dstBounds = cdms2.mvCdmsRegrid.getBoundList(dstGrd) # create regrid object r = regrid2.mvGenericRegrid.GenericRegrid([numpy.array(coord) for coord in srcGrd], [numpy.array(coord) for coord in dstGrd], dtype=so.dtype, regridMethod='conserve', regridTool='esmf', srcGridMask=srcGridMask, srcBounds=srcBounds, srcGridAreas=None, dstGridMask=None, dstBounds=dstBounds, dstGridAreas=None) # compute weights r.computeWeights() # create dst data container dstShp = dG.getLatitude().shape dstData = numpy.ones(dstShp, so.dtype) * so.missing_value # interpolate r.apply(numpy.array(so), dstData, rootPe = 0) if self.pe == 0: # checks dstDataMask = (dstData == so.missing_value) print 'number of masked values = ', dstDataMask.sum() dstDataFltd = dstData * (1 - dstDataMask) if so.missing_value > 0: dstDataMin = dstData.min() dstDataMax = dstDataFltd.max() else: dstDataMin = dstDataFltd.min() dstDataMax = dstData.max() print 'min/max value of dstData: %f %f' % (dstDataMin, dstDataMax) self.assertLess(dstDataMax, so.max())
def test1(self): """ Test cdutil.generateLandSeaMask, using a smaller dataset """ f = cdms2.open(cdat_info.get_prefix() + "/sample_data/clt.nc") s = f("clt") print s.shape print dir(cdutil.create_landsea_mask) # this will call the regrid method m = cdutil.generateLandSeaMask(s)
def test1_regrid(self): clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt')[0, ...] ta = cdms2.open(cdat_info.get_prefix() + '/sample_data/ta_ncep_87-6-88-4.nc')('ta')[0, 0, ...] diag = {} cltInterp = clt.regrid(ta.getGrid(), regridTool='libcf', mkCyclic=True, verbose=True, diag=diag) print cltInterp.sum() n = reduce(lambda x, y: x * y, cltInterp.shape) self.assertLess(abs(cltInterp.sum() - 696921.0) / n, 0.3) if PLOT: pylab.pcolor(ta.getLongitude()[:], ta.getLatitude()[:], cltInterp) pylab.colorbar() pylab.title('cltInterp') self.assertEqual(True, True)
def Xtest6_ESMF_Conserve_LevelTime_clt(self): """ Interpolate over level/time in addition to lat-lon """ f = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc') clt = f('clt') v = f('v') # mask srcGridMask = numpy.array(v[0, 0, ...] == v.missing_value, numpy.int32) # v onto the ctl grid srcGrd, srcNDims = regrid2.gsRegrid.makeCurvilinear( [v.getLatitude(), v.getLongitude()]) dstGrd, dstNDims = regrid2.gsRegrid.makeCurvilinear( [clt.getLatitude(), clt.getLongitude()]) ro = regrid2.GenericRegrid(srcGrd, dstGrd, regridMethod='conserve', regridTool='esmp', periodicity=1, srcGridMask=srcGridMask) ro.computeWeights() vInterp = numpy.ones( list(v.shape[:-2]) + list(clt.shape[-2:]), v.dtype) * v.missing_value ro.apply(numpy.array(v), vInterp, rootPe=0) print 'min/max of v: %f %f' % (v.min(), v.max()) print 'min/max of vInterp: %f %f' % (vInterp.min(), vInterp.max()) if PLOT: nTimes = v.shape[0] nLevels = v.shape[1] for el in range(nTimes): for k in range(nLevels): pl.figure() pl.subplot(1, 2, 1) pl.pcolor(srcGrd[1], srcGrd[0], v[el, k, ...], vmin=-20, vmax=20) pl.title('test6: v[%d, %d,...]' % (el, k)) pl.colorbar() pl.subplot(1, 2, 2) pl.pcolor(dstGrd[1], dstGrd[0], vInterp[el, k, ...], vmin=-20, vmax=20) pl.title('test6: vInterp[%d, %d,...]' % (el, k)) pl.colorbar()
def Xtest1(self): """ Test from Kate Marvel As the following code snippet demonstrates, regridding a cdms2.tvariable.TransientVariable instance using regridTool='regrid2' results in a new array that is masked everywhere. regridTool='esmf' and regridTool='libcf' both work as expected. This is similar to test0 but we only onterpolate over a single elevation, also fails. Likely GRID is not constructed correctly. """ import cdms2 as cdms import numpy as np filename = cdat_info.get_prefix() + '/sample_data/clt.nc' a = cdms.open(filename) data = a('clt')[0, ...] print data.mask #verify this data is not masked ## Now create a new grid using every other latitude and every # other longitude LATS = data.getLatitude()[::2] ub = data.getLatitude().getBounds()[1::2][:, 0] lb = data.getLatitude().getBounds()[::2][:, 1] latbounds = np.array(zip(ub, lb)) self.assertEqual((ub != lb).all(), True) LONS = data.getLongitude()[::2] ub = data.getLongitude().getBounds()[1::2][:, 0] lb = data.getLongitude().getBounds()[::2][:, 1] lonbounds = np.array(zip(ub, lb)) self.assertEqual((ub != lb).all(), True) GRID = cdms.createGenericGrid(LATS, LONS, latBounds=latbounds, lonBounds=lonbounds) test_data = data.regrid(GRID, regridTool='regrid2') # check that the mask does not extend everywhere... self.assertNotEqual(test_data.mask.sum(), test_data.size) if PLOT: pylab.subplot(2, 1, 1) pylab.pcolor(data[...]) pylab.title('data') pylab.subplot(2, 1, 2) pylab.pcolor(test_data[...]) pylab.title('test_data (interpolated data)') pylab.show()
def setUp(self): dir = cdat_info.get_prefix() + "/sample_data/" filename = dir + "so_Omon_MPI-ESM-LR_1pctCO2_r1i1p1_185001-185912_2timesteps.nc" g = cdms2.open(filename) self.so = g('so')[0,0,:,:254] gLat = cdms2.createGaussianAxis(64) deltaLon = (360/128.) gLon = cdms2.createUniformLongitudeAxis(0, 128, deltaLon) self.gaussGrid = cdms2.grid.createGenericGrid(gLat[:], gLon[:], gLat.getBounds(), gLon.getBounds())
def test_test3(self): """ 2D + level """ tas = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/tas_ccsr-95a_1979.01-1979.12.nc')('tas')[0, :,...] tasInterp = tas.regrid( tas.getGrid() ) if not numpy.all(tasInterp.mask): n = reduce(lambda x,y: x*y, tasInterp.shape) diff = abs(numpy.sum(tas - tasInterp))/float(n) self.assertLess(diff, 3.e-5)
def Xtest0(self): """ Test cdutil.generateLandSeaMask """ f = cdms2.open(cdat_info.get_prefix() + \ "/sample_data/so_Omon_GISS-E2-R_historicalNat_r5i1p1_185001-187512_2timesteps.nc") s = f("so") print s.shape print dir(cdutil.create_landsea_mask) # this will call the regrid method m = cdutil.generateLandSeaMask(s)
def Xtest1(self): """ Test from Kate Marvel As the following code snippet demonstrates, regridding a cdms2.tvariable.TransientVariable instance using regridTool='regrid2' results in a new array that is masked everywhere. regridTool='esmf' and regridTool='libcf' both work as expected. This is similar to test0 but we only onterpolate over a single elevation, also fails. Likely GRID is not constructed correctly. """ import cdms2 as cdms import numpy as np filename = cdat_info.get_prefix() + '/sample_data/clt.nc' a=cdms.open(filename) data=a('clt')[0,...] print data.mask #verify this data is not masked ## Now create a new grid using every other latitude and every # other longitude LATS=data.getLatitude()[::2] ub = data.getLatitude().getBounds()[1::2][:,0] lb = data.getLatitude().getBounds()[::2][:,1] latbounds = np.array(zip(ub,lb)) self.assertEqual((ub != lb).all(), True) LONS=data.getLongitude()[::2] ub=data.getLongitude().getBounds()[1::2][:,0] lb=data.getLongitude().getBounds()[::2][:,1] lonbounds=np.array(zip(ub,lb)) self.assertEqual((ub != lb).all(), True) GRID=cdms.createGenericGrid(LATS,LONS, latBounds=latbounds, lonBounds=lonbounds) test_data=data.regrid(GRID,regridTool='regrid2') # check that the mask does not extend everywhere... self.assertNotEqual(test_data.mask.sum(), test_data.size) if PLOT: pylab.subplot(2, 1, 1) pylab.pcolor(data[...]) pylab.title('data') pylab.subplot(2, 1, 2) pylab.pcolor(test_data[...]) pylab.title('test_data (interpolated data)') pylab.show()
def setUp(self): filename = cdat_info.get_prefix() + \ "/sample_data/so_Omon_CNRM-CM5_decadal2004_r9i1p1_200501-201412_2timesteps.nc" g = cdms2.open(filename) self.so = g('so')[0,0,...] self.so.toVisit('soCNRM.vsh5', 'Vs') gLat = cdms2.createGaussianAxis(64) deltaLon = (360/128.) gLon = cdms2.createUniformLongitudeAxis(0, 128, deltaLon) self.gaussGrid = cdms2.grid.createGenericGrid(gLat[:], gLon[:], gLat.getBounds(), gLon.getBounds())
def test_2d_esmf(self): #print 'running test_2d_esmf...' f = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = f('so')[0, 0, :, :] clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt')[0, :, :] tic = time.time() soInterp = so.regrid(clt.getGrid(), regridTool='ESMF') #, periodicity=1) soInterpInterp = soInterp.regrid(so.getGrid(), regridTool='ESMF') toc = time.time() #print 'time to interpolate (ESMF linear) forward/backward: ', toc - tic ntot = reduce(operator.mul, so.shape) avgdiff = numpy.sum(so - soInterpInterp) / float(ntot) #print 'avgdiff = ', avgdiff self.assertLess(abs(avgdiff), 5.2e18) if PLOT: pylab.figure(2) pylab.pcolor(abs(so - soInterpInterp), vmin=0.0, vmax=1.0) pylab.colorbar() pylab.title('ESMF linear')
def setUp(self): filename = cdat_info.get_prefix() + \ "/sample_data/so_Omon_CNRM-CM5_decadal2004_r9i1p1_200501-201412_2timesteps.nc" g = cdms2.open(filename) self.so = g('so')[0, 0, ...] self.so.toVisit('soCNRM.vsh5', 'Vs') gLat = cdms2.createGaussianAxis(64) deltaLon = (360 / 128.) gLon = cdms2.createUniformLongitudeAxis(0, 128, deltaLon) self.gaussGrid = cdms2.grid.createGenericGrid(gLat[:], gLon[:], gLat.getBounds(), gLon.getBounds())
def Xtest2(self): """ 2D + time """ u = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('u')[:, 0,...] uInterp = u.regrid( u.getGrid() ) self.assertEqual(numpy.isnan(u).sum(), 0) self.assertEqual(numpy.isnan(uInterp).sum(), 0) n = reduce(lambda x,y: x*y, uInterp.shape) diff = abs(numpy.sum(u - uInterp))/float(n) success = False self.assertLess(diff, self.tolerance)
def Xtest4(self): """ 2D + level + time """ u = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('u')[:, :,...] uInterp = u.regrid( u.getGrid(), regridTool='esmf', regridMethod='linear', coordSys = 'cart', periodicity = 1) n = reduce(lambda x,y: x*y, uInterp.shape) mask = (u == u.missing_value) diff = abs(numpy.sum(u*(1-mask) - uInterp)/float(n)) self.assertLess(diff, 1.e-3)
def Xtest1(self): """ 2D """ u = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('u')[0, 0,...] uInterp = u.regrid( u.getGrid() ) n = reduce(lambda x,y: x*y, uInterp.shape) diff = abs(numpy.sum(u - uInterp))/float(n) nans = numpy.isnan(uInterp).sum() self.assertEqual(numpy.isnan(u).sum(), 0) self.assertEqual(numpy.isnan(uInterp).sum(), 0) self.assertLess((uInterp.mean()-u.mean()), self.tolerance) self.assertLess(diff, self.tolerance)