def Xtest4_mvESMFRegrid_esmfSlabs(self): print('\ntest4') srcDims, srcXYZCenter, srcData, srcBounds = makeGrid(5, 4, 3) dstDims, dstXYZCenter, dstData, dstBounds = makeGrid(5, 4, 3) ro = ESMFRegrid(srcXYZCenter[0].shape, dstXYZCenter[0].shape, srcData.dtype, 'conserve', 'center', 0, 'cart', staggerloc='center', hasSrcBounds=True, hasDstBounds=True) ro.setCoords(srcXYZCenter, dstXYZCenter, srcBounds=srcBounds, dstBounds=dstBounds, globalIndexing=True) ro.computeWeights() ro.apply(srcData, dstData, rootPe=self.rootPe, globalIndexing=True) print(('src', srcData.min(), srcData.max(), srcData.shape)) print(('dst', dstData.min(), dstData.max(), dstData.shape)) sA = ro.getSrcAreas(rootPe=self.rootPe) dA = ro.getDstAreas(rootPe=self.rootPe) sF = ro.getSrcAreaFractions(rootPe=self.rootPe) dF = ro.getDstAreaFractions(rootPe=self.rootPe)
def xtest3_mvESMFRegrid_pregenSlabs(self): print('\ntest3') srcDims, srcXYZCenter, srcData, srcBounds = makeGrid(5, 4, 3) dstDims, dstXYZCenter, dstData, dstBounds = makeGrid(5, 4, 3) ro = ESMFRegrid(srcXYZCenter[0].shape, dstXYZCenter[0].shape, srcData.dtype, 'conserve', 'center', 0, 'cart', staggerloc='center', hasSrcBounds=True, hasDstBounds=True) srcSlab = ro.getSrcLocalSlab('center') dstSlab = ro.getDstLocalSlab('center') srcCds = [coord[srcSlab] for coord in srcXYZCenter] dstCds = [coord[dstSlab] for coord in dstXYZCenter] srcSlab = ro.getSrcLocalSlab('vface') dstSlab = ro.getDstLocalSlab('vface') srcBnd = [bound[srcSlab] for bound in srcBounds] dstBnd = [bound[dstSlab] for bound in dstBounds] ro.setCoords(srcCds, dstCds, srcBounds=srcBnd, dstBounds=dstBnd) ro.computeWeights() srcDataSec = numpy.array(srcData[srcSlab], srcData.dtype) dstDataSec = numpy.array(dstData[dstSlab], dstData.dtype) ro.apply(srcDataSec, dstDataSec, rootPe=None) print(('src', srcDataSec.min(), srcDataSec.max(), srcDataSec.shape)) print(('dst', dstDataSec.min(), dstDataSec.max(), dstDataSec.shape)) sA = ro.getSrcAreas(rootPe=self.rootPe) dA = ro.getDstAreas(rootPe=self.rootPe) sF = ro.getSrcAreaFractions(rootPe=self.rootPe) dF = ro.getDstAreaFractions(rootPe=self.rootPe)
def Xtest4_mvESMFRegrid_esmfSlabs(self): print '\ntest4' srcDims, srcXYZCenter, srcData, srcBounds = makeGrid(5, 4, 3) dstDims, dstXYZCenter, dstData, dstBounds = makeGrid(5, 4, 3) ro = ESMFRegrid(srcXYZCenter[0].shape, dstXYZCenter[0].shape, srcData.dtype, 'conserve','center', 0,'cart', staggerloc = 'center', hasSrcBounds = True, hasDstBounds = True) ro.setCoords(srcXYZCenter, dstXYZCenter, srcBounds = srcBounds, dstBounds = dstBounds, globalIndexing = True) ro.computeWeights() ro.apply(srcData, dstData, rootPe = self.rootPe, globalIndexing = True) print 'src', srcData.min(), srcData.max(), srcData.shape print 'dst', dstData.min(), dstData.max(), dstData.shape sA = ro.getSrcAreas(rootPe = self.rootPe) dA = ro.getDstAreas(rootPe = self.rootPe) sF = ro.getSrcAreaFractions(rootPe = self.rootPe) dF = ro.getDstAreaFractions(rootPe = self.rootPe)
def test1_ESMFRegrid(self): if self.pe == 0: print # # 1. input # coordSys = ESMP.ESMP_COORDSYS_SPH_DEG # ESMP.ESMP_COORDSYS_CART fails inFile = sys.prefix + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc' # dtype of a numpy 'float64' in scipy is '>f4' ... # switching to cdms2 startBegin = time.time() srcF = cdms2.open(inFile) # Source Grid missing_value = 1.e20 srcGrd = [srcF.variables['lat'], srcF.variables['lon']] srcBounds = _getCorners([srcF.variables['lat_vertices'], srcF.variables['lon_vertices']]) srcGrdMask = srcF['so'][0, 0, ...].mask # Destination Grid lat1dBounds = numpy.arange(-90.0, 90.001, 5.0) lon1dBounds = numpy.arange(-180.0, 180.001, 5.0) lat1dCenter = 0.5*(lat1dBounds[:-1] + lat1dBounds[1:]) lon1dCenter = 0.5*(lon1dBounds[:-1] + lon1dBounds[1:]) lat2dBounds = numpy.outer(lat1dBounds, numpy.ones((len(lon1dBounds),), lon1dBounds.dtype )) lon2dBounds = numpy.outer(numpy.ones((len(lat1dBounds),), lat1dBounds.dtype ), lon1dBounds ) lat2dCenter = numpy.outer(lat1dCenter, numpy.ones((len(lon1dCenter),), lon1dCenter.dtype )) lon2dCenter = numpy.outer(numpy.ones((len(lat1dCenter),), lat1dCenter.dtype ), lon1dCenter ) dstGrd = [lat2dCenter, lon2dCenter] dstBounds = [lat2dBounds, lon2dBounds] # # 4. Regrid # srcGrdShape = srcGrd[0][:].shape dstGrdShape = dstGrd[0][:].shape startRegrid = time.time() ro = ESMFRegrid(srcGrdShape, dstGrdShape, numpy.float32, 'conserve', 'center', 0, 'degrees', srcGridMask = srcGrdMask, hasSrcBounds = True, hasDstBounds = True) # Get the slab and local part of the grid and bounds dstSlabCtr = ro.getDstLocalSlab("center") srcSlabCtr = ro.getSrcLocalSlab("center") # srcGrdLcl = [coord[srcSlabCtr].data for coord in srcGrd] dstGrdLcl = [coord[dstSlabCtr] for coord in dstGrd] dstSlabCnr = ro.getDstLocalSlab("corner") srcSlabCnr = ro.getSrcLocalSlab("corner") srcBndLcl = [bound[srcSlabCnr] for bound in srcBounds] dstBndLcl = [bound[dstSlabCnr] for bound in dstBounds] ro.setCoords(srcGrdLcl, dstGrdLcl, srcGridMask = srcGrdMask, srcBounds = srcBndLcl, dstBounds = dstBndLcl, globalIndexing = False) ro.computeWeights() # Local dimensions localDstData = numpy.ones(dstGrd[0].shape, numpy.float64)[dstSlabCtr[0], dstSlabCtr[1]] localSrcData = srcF.variables['so'][0, 0, srcSlabCtr[0], srcSlabCtr[1]].data ro.apply(localSrcData, localDstData, rootPe = None, globalIndexing = False) endRegrid = time.time() # # 5. Check # # conservation diag = {'srcAreaFractions':None, 'srcAreas':None, 'dstAreaFractions':None, 'dstAreas':None} ro.fillInDiagnosticData(diag, rootPe = None) localSrcAreaFractions = diag['srcAreaFractions'] localSrcAreas = diag['srcAreas'] localDstAreaFractions = diag['dstAreaFractions'] localDstAreas = diag['dstAreas'] localSrcMass = (localSrcData * localSrcAreas * localSrcAreaFractions).sum() localDstMass = (localDstData * localDstAreas).sum() localLackConserve = localSrcMass - localDstMass # nans if numpy.isnan(localSrcAreaFractions).sum() > 0: a = self.pe b = numpy.isnan(localSrcAreaFractions).sum() #print '[%d] *** %d Nans found in localSrcAreaFractions!!' % (a, b) # (self.pe, str(numpy.isnan().sum(localSrcAreaFractions))) if numpy.isnan(localDstAreaFractions).sum() > 0: c = self.pe d = numpy.isnan(localDstAreaFractions).sum() #print '[%d] *** %d Nans found in localDstAreaFractions!!' % (c, d) # (self.pe, numpy.isnan().sum(localDstAreaFractions)) #print '[%d] checksum of localSrc: %g checksum of localDst: %g' % \ (self.pe, localSrcMass, localDstMass) #print '[%d] localSrc total area integral: %g localDst total area integral: %g diff: %g\n' % \ (self.pe, localSrcMass, localDstMass, localLackConserve) if HAS_MPI: lackConserv = MPI.COMM_WORLD.reduce(localLackConserve, op=MPI.SUM, root=0) else: lackConserv = localLackConserve if self.pe == 0: #print 'ROOT: total lack of conservation (should be small): %f' % lackConserv print 'Time for total including file opens:', time.time() - startBegin print 'Time for regridding ops:', endRegrid - startRegrid
def Xtest3_mvESMFRegrid_pregenSlabs(self): print '\ntest3' srcDims, srcXYZCenter, srcData, srcBounds = makeGrid(5, 4, 3) dstDims, dstXYZCenter, dstData, dstBounds = makeGrid(5, 4, 3) ro = ESMFRegrid(srcXYZCenter[0].shape, dstXYZCenter[0].shape, srcData.dtype, 'conserve','center', 0,'cart', staggerloc = 'center', hasSrcBounds = True, hasDstBounds = True) srcSlab = ro.getSrcLocalSlab('center') dstSlab = ro.getDstLocalSlab('center') srcCds = [coord[srcSlab] for coord in srcXYZCenter] dstCds = [coord[dstSlab] for coord in dstXYZCenter] srcSlab = ro.getSrcLocalSlab('vface') dstSlab = ro.getDstLocalSlab('vface') srcBnd = [bound[srcSlab] for bound in srcBounds] dstBnd = [bound[dstSlab] for bound in dstBounds] ro.setCoords(srcCds, dstCds, srcBounds = srcBnd, dstBounds = dstBnd) ro.computeWeights() srcDataSec = numpy.array(srcData[srcSlab], srcData.dtype) dstDataSec = numpy.array(dstData[dstSlab], dstData.dtype) ro.apply(srcDataSec, dstDataSec, rootPe = None) print 'src', srcDataSec.min(), srcDataSec.max(), srcDataSec.shape print 'dst', dstDataSec.min(), dstDataSec.max(), dstDataSec.shape sA = ro.getSrcAreas(rootPe = self.rootPe) dA = ro.getDstAreas(rootPe = self.rootPe) sF = ro.getSrcAreaFractions(rootPe = self.rootPe) dF = ro.getDstAreaFractions(rootPe = self.rootPe)