def __init__(self, maxIndex): self.grid = ESMP.ESMP_GridCreateNoPeriDim(maxIndex, coordSys=COORDSYS) ESMP.ESMP_GridAddCoord(self.grid, staggerloc=CENTER) self.field = ESMP.ESMP_FieldCreateGrid(self.grid, 'srcFeld1', staggerloc=CENTER, typekind=R4)
def create_ESMPfieldgrid(grid, name): ''' PRECONDITIONS: An ESMP_Grid has been created, and 'name' is a string that will be used to initialize the name of a new ESMP_Field. POSTCONDITIONS: An ESMP_Field has been created. ''' # defaults to center staggerloc field = ESMP.ESMP_FieldCreateGrid(grid, name) return field
def __init__(self, esmfGrid, name, datatype, staggerloc = CENTER): """ Creator for structured ESMF Field @param esmfGrid instance of an ESMP_Grid @param name field name (must be unique) @param datatype data type, one of 'float64', 'float32', 'int64', or 'int32' (or equivalent numpy dtype) @param staggerloc ESMP_STAGGERLOC_CENTER ESMP_STAGGERLOC_CORNER """ # field object self.field = None # the local processor rank self.pe = 0 # the number of processors self.nprocs = 1 # associated grid self.grid = esmfGrid # staggering self.staggerloc = staggerloc # communicator self.comm = None try: from mpi4py import MPI self.comm = MPI.COMM_WORLD except: pass vm = ESMP.ESMP_VMGetGlobal() self.pe, self.nprocs = ESMP.ESMP_VMGet(vm) etype = None sdatatype = str(datatype) # in case user passes a numpy dtype if re.search('float64', sdatatype): etype = R8 elif re.search('float32', sdatatype): etype = R4 elif re.search('int64', sdatatype): etype = I8 elif re.search('int32', sdatatype): etype = I4 else: msg = 'esmf.EsmfStructField.__init__: ERROR invalid type %s' % datatype raise RegridError, msg self.field = ESMP.ESMP_FieldCreateGrid(esmfGrid.grid, name, staggerloc = staggerloc, typekind = etype)
def XXtest0_ESMP(self): import scipy.io.netcdf srcF = scipy.io.netcdf.netcdf_file( 'so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = srcF.variables['so'][0, 0, ...] missing_value = 1.e20 srcGrd = [srcF.variables['lat'][:], srcF.variables['lon'][:]] srcBounds = _getCorners([ srcF.variables['lat_vertices'][:], srcF.variables['lon_vertices'][:] ]) 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] coordSys = ESMP.ESMP_COORDSYS_SPH_DEG # ESMP.ESMP_COORDSYS_CART fails srcDims = srcGrd[0].shape dstDims = dstGrd[0].shape # do we need to revert the order here? srcMaxIndex = numpy.array(srcDims[::-1], numpy.int32) # number of cells dstMaxIndex = numpy.array(dstDims[::-1], numpy.int32) # number of cells # grids srcGrid = ESMP.ESMP_GridCreateNoPeriDim(srcMaxIndex, coordSys=coordSys) dstGrid = ESMP.ESMP_GridCreateNoPeriDim(dstMaxIndex, coordSys=coordSys) # it's a good idea to always add the nodal coordinates ESMP.ESMP_GridAddCoord(srcGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) # masks ESMP.ESMP_GridAddItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) srcGridMaskPtr = ESMP.ESMP_GridGetItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) ESMP.ESMP_GridAddItem(dstGrid, item=ESMP.ESMP_GRIDITEM_MASK) dstGridMaskPtr = ESMP.ESMP_GridGetItem(dstGrid, item=ESMP.ESMP_GRIDITEM_MASK) # get pointer to coordinates array and dimensions # src srcXCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CORNER) srcYCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CORNER) srcLoCorner, srcHiCorner = ESMP.ESMP_GridGetCoord( srcGrid, ESMP.ESMP_STAGGERLOC_CORNER) srcXCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) srcYCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) srcLoCenter, srcHiCenter = ESMP.ESMP_GridGetCoord( srcGrid, ESMP.ESMP_STAGGERLOC_CENTER) # dst dstXCorner = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, ESMP.ESMP_STAGGERLOC_CORNER) dstYCorner = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, ESMP.ESMP_STAGGERLOC_CORNER) dstLoCorner, dstHiCorner = ESMP.ESMP_GridGetCoord( dstGrid, ESMP.ESMP_STAGGERLOC_CORNER) dstXCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) dstYCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) dstLoCenter, dstHiCenter = ESMP.ESMP_GridGetCoord( dstGrid, ESMP.ESMP_STAGGERLOC_CENTER) # fields srcFld = ESMP.ESMP_FieldCreateGrid( srcGrid, 'srcFld', typekind=ESMP.ESMP_TYPEKIND_R8, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) dstFld = ESMP.ESMP_FieldCreateGrid( dstGrid, 'dstFld', typekind=ESMP.ESMP_TYPEKIND_R8, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) srcAreaField = ESMP.ESMP_FieldCreateGrid( srcGrid, 'srcAreas', typekind=ESMP.ESMP_TYPEKIND_R8, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) dstAreaField = ESMP.ESMP_FieldCreateGrid( dstGrid, 'dstAreas', typekind=ESMP.ESMP_TYPEKIND_R8, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) srcFracField = ESMP.ESMP_FieldCreateGrid( srcGrid, 'srcFracAreas', typekind=ESMP.ESMP_TYPEKIND_R8, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) dstFracField = ESMP.ESMP_FieldCreateGrid( dstGrid, 'dstFracAreas', typekind=ESMP.ESMP_TYPEKIND_R8, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) srcFieldPtr = ESMP.ESMP_FieldGetPtr(srcFld) srcFracPtr = ESMP.ESMP_FieldGetPtr(srcFracField) dstFieldPtr = ESMP.ESMP_FieldGetPtr(dstFld) dstFracPtr = ESMP.ESMP_FieldGetPtr(dstFracField) # set the coordinates and field values. In ESMP, arrays are column major! # src-corners srcNx1 = srcHiCorner[0] - srcLoCorner[0] srcNy1 = srcHiCorner[1] - srcLoCorner[1] srcYCorner[:] = srcBounds[0][srcLoCorner[1]:srcHiCorner[1], srcLoCorner[0]:srcHiCorner[0]].flat srcXCorner[:] = srcBounds[1][srcLoCorner[1]:srcHiCorner[1], srcLoCorner[0]:srcHiCorner[0]].flat # src-center coordinates, field, and mask srcNx = srcHiCenter[0] - srcLoCenter[0] srcNy = srcHiCenter[1] - srcLoCenter[1] yc = numpy.array(srcGrd[0][srcLoCenter[1]:srcHiCenter[1], srcLoCenter[0]:srcHiCenter[0]]) xc = numpy.array(srcGrd[1][srcLoCenter[1]:srcHiCenter[1], srcLoCenter[0]:srcHiCenter[0]]) srcYCenter[:] = yc.flat srcXCenter[:] = xc.flat msk = numpy.array(so[srcLoCenter[1]:srcHiCenter[1], srcLoCenter[0]:srcHiCenter[0]] \ == missing_value, numpy.int32) srcGridMaskPtr[:] = msk.flat fld = numpy.array( so[srcLoCenter[1]:srcHiCenter[1], srcLoCenter[0]:srcHiCenter[0]], so.dtype) # set to zero where masked fld *= (1 - msk) srcFieldPtr[:] = fld.flat srcFracPtr[:] = 1.0 # dst-corners dstNx1 = dstHiCorner[0] - dstLoCorner[0] dstNy1 = dstHiCorner[1] - dstLoCorner[1] dstYCorner[:] = dstBounds[0][dstLoCorner[1]:dstHiCorner[1], dstLoCorner[0]:dstHiCorner[0]].flat dstXCorner[:] = dstBounds[1][dstLoCorner[1]:dstHiCorner[1], dstLoCorner[0]:dstHiCorner[0]].flat # dst-center coordinates, field, and mask dstNx = dstHiCenter[0] - dstLoCenter[0] dstNy = dstHiCenter[1] - dstLoCenter[1] yc = numpy.array(dstGrd[0][dstLoCenter[1]:dstHiCenter[1], dstLoCenter[0]:dstHiCenter[0]]) xc = numpy.array(dstGrd[1][dstLoCenter[1]:dstHiCenter[1], dstLoCenter[0]:dstHiCenter[0]]) dstYCenter[:] = yc.flat dstXCenter[:] = xc.flat dstGridMaskPtr[:] = 0 dstFieldPtr[:] = missing_value dstFracPtr[:] = 1.0 # interpolation maskVals = numpy.array([1], numpy.int32) # values defining mask regrid = ESMP.ESMP_FieldRegridStore( srcFld, dstFld, srcMaskValues=maskVals, dstMaskValues=maskVals, regridmethod=ESMP.ESMP_REGRIDMETHOD_CONSERVE, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE, srcFracField=srcFracField, dstFracField=dstFracField) ESMP.ESMP_FieldRegrid(srcFld, dstFld, regrid) # get the cell areas ESMP.ESMP_FieldRegridGetArea(srcAreaField) ESMP.ESMP_FieldRegridGetArea(dstAreaField) srcAreasPtr = ESMP.ESMP_FieldGetPtr(srcAreaField) dstAreasPtr = ESMP.ESMP_FieldGetPtr(dstAreaField) srcFracPtr = ESMP.ESMP_FieldGetPtr(srcFracField) dstFracPtr = ESMP.ESMP_FieldGetPtr(dstFracField) # check conservation srcFldSum, dstFldSum = srcFieldPtr.sum(), dstFieldPtr.sum() srcFldIntegral = (srcFieldPtr * srcAreasPtr * srcFracPtr).sum() dstFldIntegral = (dstFieldPtr * dstAreasPtr * dstFracPtr).sum() lackConservLocal = srcFldIntegral - dstFldIntegral # check for nans if numpy.isnan(srcFracPtr).sum() > 0: print '[%d] *** %d Nans found in srcFracPtr!!' % ( self.pe, numpy.isnan().sum(srcFracPtr)) if numpy.isnan(dstFracPtr).sum() > 0: print '[%d] *** %d Nans found in dstFracPtr!!' % ( self.pe, numpy.isnan().sum(dstFracPtr)) print '[%d] checksum of src: %g checksum of dst: %g' % ( self.pe, srcFldSum, dstFldSum) print '[%d] src total area integral: %g dst total area integral: %g diff: %g\n' % \ (self.pe, srcFldIntegral, dstFldIntegral, lackConservLocal) if HAS_MPI: lackConserv = MPI.COMM_WORLD.reduce(lackConservLocal, op=MPI.SUM, root=0) else: lackConserv = lackConservLocal if self.pe == 0: print 'ROOT: total lack of conservation (should be small): %f' % lackConserv # cleanup ESMP.ESMP_FieldRegridRelease(regrid) ESMP.ESMP_FieldDestroy(srcAreaField) ESMP.ESMP_FieldDestroy(dstAreaField) ESMP.ESMP_FieldDestroy(srcFracField) ESMP.ESMP_FieldDestroy(dstFracField) ESMP.ESMP_FieldDestroy(srcFld) ESMP.ESMP_FieldDestroy(dstFld) ESMP.ESMP_GridDestroy(srcGrid) ESMP.ESMP_GridDestroy(dstGrid)
def test_2d_esmf_native(self): print 'running test_2d_esmf_native...' f = cdms2.open(sys.prefix + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = f('so')[0, 0, :, :] clt = cdms2.open(sys.prefix + '/sample_data/clt.nc')('clt')[0, :, :] cltBounds = clt.getGrid().getBounds() tic = time.time() ny, nx = clt.shape yb = numpy.zeros((ny + 1, ), numpy.float32) yb[:ny] = cltBounds[0][:, 0] yb[ny] = cltBounds[0][ny - 1, 1] xb = numpy.zeros((nx + 1, ), numpy.float32) xb[:nx] = cltBounds[1][:, 0] xb[nx] = cltBounds[1][nx - 1, 1] # make curvilinear dstLatCorner = numpy.outer(yb, numpy.ones((nx + 1, ), numpy.float32)) dstLonCorner = numpy.outer(numpy.ones((ny + 1, ), numpy.float32), xb) ny, nx = so.shape soBounds = so.getGrid().getBounds() srcLatCorner = numpy.zeros((ny + 1, nx + 1), numpy.float32) srcLatCorner[:ny, :nx] = soBounds[0][:, :, 0] srcLatCorner[:ny, nx] = soBounds[0][:ny, nx - 1, 1] srcLatCorner[ny, nx] = soBounds[0][ny - 1, nx - 1, 2] srcLatCorner[ny, :nx] = soBounds[0][ny - 1, :nx, 3] srcLonCorner = numpy.zeros((ny + 1, nx + 1), numpy.float32) srcLonCorner[:ny, :nx] = soBounds[1][:, :, 0] srcLonCorner[:ny, nx] = soBounds[1][:ny, nx - 1, 1] srcLonCorner[ny, nx] = soBounds[1][ny - 1, nx - 1, 2] srcLonCorner[ny, :nx] = soBounds[1][ny - 1, :nx, 3] # create grid srcMaxIndex = numpy.array(so.shape[::-1], dtype=numpy.int32) srcGrid = ESMP.ESMP_GridCreateNoPeriDim( srcMaxIndex, coordSys=ESMP.ESMP_COORDSYS_SPH_DEG) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER) srcDimsCenter = ESMP.ESMP_GridGetCoord(srcGrid, ESMP.ESMP_STAGGERLOC_CENTER) srcDimsCorner = ESMP.ESMP_GridGetCoord(srcGrid, ESMP.ESMP_STAGGERLOC_CORNER) srcXCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) srcYCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) srcXCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CORNER) srcYCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CORNER) dstMaxIndex = numpy.array(clt.shape[::-1], dtype=numpy.int32) dstGrid = ESMP.ESMP_GridCreateNoPeriDim( dstMaxIndex, coordSys=ESMP.ESMP_COORDSYS_SPH_DEG) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER) dstDimsCenter = ESMP.ESMP_GridGetCoord(dstGrid, ESMP.ESMP_STAGGERLOC_CENTER) dstDimsCorner = ESMP.ESMP_GridGetCoord(dstGrid, ESMP.ESMP_STAGGERLOC_CORNER) dstXCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) dstYCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) dstXCorner = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, ESMP.ESMP_STAGGERLOC_CORNER) dstYCorner = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, ESMP.ESMP_STAGGERLOC_CORNER) # mask ESMP.ESMP_GridAddItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) srcMask = ESMP.ESMP_GridGetItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) # create field srcFld = ESMP.ESMP_FieldCreateGrid( srcGrid, 'srcFld', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) srcFldPtr = ESMP.ESMP_FieldGetPtr(srcFld) srcFld2 = ESMP.ESMP_FieldCreateGrid( srcGrid, 'srcFld2', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) srcFldPtr2 = ESMP.ESMP_FieldGetPtr(srcFld2) dstFld = ESMP.ESMP_FieldCreateGrid( dstGrid, 'dstFld', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) dstFldPtr = ESMP.ESMP_FieldGetPtr(dstFld) # set coords, mask, and field values for src and dst srcNtotCenter = reduce(operator.mul, [srcDimsCenter[1][i] - srcDimsCenter[0][i] \ for i in range(2)]) srcNtotCorner = reduce(operator.mul, [srcDimsCorner[1][i] - srcDimsCorner[0][i] \ for i in range(2)]) srcJCenterBeg = srcDimsCenter[0][1] srcJCenterEnd = srcDimsCenter[1][1] srcJCornerBeg = srcDimsCorner[0][1] srcJCornerEnd = srcDimsCorner[1][1] srcICenterBeg = srcDimsCenter[0][0] srcICenterEnd = srcDimsCenter[1][0] srcICornerBeg = srcDimsCorner[0][0] srcICornerEnd = srcDimsCorner[1][0] srcLonCenter = so.getLongitude() srcLatCenter = so.getLatitude() srcXCenter[:] = numpy.reshape( srcLonCenter[srcJCenterBeg:srcJCenterEnd, srcICenterBeg:srcICenterEnd], (srcNtotCenter, )) srcYCenter[:] = numpy.reshape( srcLatCenter[srcJCenterBeg:srcJCenterEnd, srcICenterBeg:srcICenterEnd], (srcNtotCenter, )) srcXCorner[:] = numpy.reshape( srcLonCorner[srcJCornerBeg:srcJCornerEnd, srcICornerBeg:srcICornerEnd], (srcNtotCorner, )) srcYCorner[:] = numpy.reshape( srcLatCorner[srcJCornerBeg:srcJCornerEnd, srcICornerBeg:srcICornerEnd], (srcNtotCorner, )) srcFldPtr[:] = numpy.reshape( so[srcJCenterBeg:srcJCenterEnd, srcICenterBeg:srcICenterEnd], (srcNtotCenter, )) srcMask[:] = (srcFldPtr == so.missing_value) dstNtotCenter = reduce( operator.mul, [dstDimsCenter[1][i] - dstDimsCenter[0][i] for i in range(2)]) dstNtotCorner = reduce( operator.mul, [dstDimsCorner[1][i] - dstDimsCorner[0][i] for i in range(2)]) # clt grid is rectilinear, transform to curvilinear lons = clt.getGrid().getLongitude() lats = clt.getGrid().getLatitude() ny, nx = dstDimsCenter[1][1] - dstDimsCenter[0][1], dstDimsCenter[1][ 0] - dstDimsCenter[0][0] yy = numpy.outer(lats[dstDimsCenter[0][1]:dstDimsCenter[1][1]], numpy.ones((nx, ), dtype=numpy.float32)) xx = numpy.outer(numpy.ones((ny, ), dtype=numpy.float32), lons[dstDimsCenter[0][0]:dstDimsCenter[1][0]]) y = yy.reshape((dstNtotCenter, )) x = xx.reshape((dstNtotCenter, )) dstXCenter[:] = x[:] dstYCenter[:] = y[:] dstXCorner[:] = numpy.reshape( dstLonCorner[dstDimsCorner[0][1]:dstDimsCorner[1][1], dstDimsCorner[0][0]:dstDimsCorner[1][0]], (dstNtotCorner, )) dstYCorner[:] = numpy.reshape( dstLatCorner[dstDimsCorner[0][1]:dstDimsCorner[1][1], dstDimsCorner[0][0]:dstDimsCorner[1][0]], (dstNtotCorner, )) dstFldPtr[:] = 0 # regrid forward and backward maskVals = numpy.array([1], numpy.int32) # values defining mask regrid1 = ESMP.ESMP_FieldRegridStore( srcFld, dstFld, srcMaskValues=maskVals, dstMaskValues=None, regridmethod=ESMP.ESMP_REGRIDMETHOD_CONSERVE, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE, srcFracField=None, dstFracField=None) ESMP.ESMP_FieldRegrid(srcFld, dstFld, regrid1) jbeg, jend = dstDimsCenter[0][1], dstDimsCenter[1][1] ibeg, iend = dstDimsCenter[0][0], dstDimsCenter[1][0] soInterp = numpy.reshape(dstFldPtr, (jend - jbeg, iend - ibeg)) regrid2 = ESMP.ESMP_FieldRegridStore( dstFld, srcFld2, srcMaskValues=None, dstMaskValues=None, regridmethod=ESMP.ESMP_REGRIDMETHOD_CONSERVE, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE, srcFracField=None, dstFracField=None) ESMP.ESMP_FieldRegrid(dstFld, srcFld2, regrid2) jbeg, jend = srcDimsCenter[0][1], srcDimsCenter[1][1] ibeg, iend = srcDimsCenter[0][0], srcDimsCenter[1][0] soInterpInterp = numpy.reshape(srcFldPtr2, (jend - jbeg, iend - ibeg)) toc = time.time() avgdiff = numpy.sum(so[jbeg:jend, ibeg:iend] - soInterpInterp) / float(srcNtotCenter) print 'avgdiff = ', avgdiff self.assertLess(abs(avgdiff), 3.0) if PLOT: pylab.figure(1) pylab.subplot(2, 2, 1) pylab.pcolor(so, vmin=20.0, vmax=40.0) pylab.colorbar() pylab.title('ESMF conserve native: so') pylab.subplot(2, 2, 2) pylab.pcolor(soInterp, vmin=20.0, vmax=40.0) pylab.colorbar() pylab.title('ESMF conserve native: soInterp') pylab.subplot(2, 2, 3) pylab.pcolor(soInterpInterp, vmin=20.0, vmax=40.0) pylab.colorbar() pylab.title('ESMF conserve native: soInterpInterp') pylab.subplot(2, 2, 4) pylab.pcolor(so[jbeg:jend, ibeg:iend] - soInterpInterp, vmin=-0.5, vmax=0.5) pylab.colorbar() pylab.title('ESMF conserve native: error') # clean up ESMP.ESMP_FieldRegridRelease(regrid2) ESMP.ESMP_FieldRegridRelease(regrid1) ESMP.ESMP_FieldDestroy(dstFld) ESMP.ESMP_GridDestroy(dstGrid) ESMP.ESMP_FieldDestroy(srcFld) ESMP.ESMP_FieldDestroy(srcFld2) ESMP.ESMP_GridDestroy(srcGrid)
def test_2d_esmf_native(self): #print 'running test_2d_esmf_native...' f = cdms2.open(sys.prefix + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = f('so')[0, 0, :, :] clt = cdms2.open(sys.prefix + '/sample_data/clt.nc')('clt')[0, :, :] tic = time.time() # create grid srcMaxIndex = numpy.array(so.shape[::-1], dtype=numpy.int32) srcGrid = ESMP.ESMP_GridCreateNoPeriDim(srcMaxIndex, coordSys = ESMP.ESMP_COORDSYS_SPH_DEG) #srcGrid = ESMP.ESMP_GridCreate1PeriDim(srcMaxIndex, # coordSys = ESMP.ESMP_COORDSYS_SPH_DEG) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc = ESMP.ESMP_STAGGERLOC_CENTER) srcDimsCenter = ESMP.ESMP_GridGetCoord(srcGrid, ESMP.ESMP_STAGGERLOC_CENTER) srcXCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) srcYCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) dstMaxIndex = numpy.array(clt.shape[::-1], dtype=numpy.int32) dstGrid = ESMP.ESMP_GridCreateNoPeriDim(dstMaxIndex, coordSys = ESMP.ESMP_COORDSYS_SPH_DEG) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc = ESMP.ESMP_STAGGERLOC_CENTER) dstDimsCenter = ESMP.ESMP_GridGetCoord(dstGrid, ESMP.ESMP_STAGGERLOC_CENTER) dstXCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) dstYCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) # mask ESMP.ESMP_GridAddItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) srcMask = ESMP.ESMP_GridGetItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) # create field srcFld = ESMP.ESMP_FieldCreateGrid(srcGrid, 'srcFld', typekind = ESMP.ESMP_TYPEKIND_R4, staggerloc = ESMP.ESMP_STAGGERLOC_CENTER) srcFldPtr = ESMP.ESMP_FieldGetPtr(srcFld) srcFld2 = ESMP.ESMP_FieldCreateGrid(srcGrid, 'srcFld2', typekind = ESMP.ESMP_TYPEKIND_R4, staggerloc = ESMP.ESMP_STAGGERLOC_CENTER) srcFldPtr2 = ESMP.ESMP_FieldGetPtr(srcFld2) dstFld = ESMP.ESMP_FieldCreateGrid(dstGrid, 'dstFld', typekind = ESMP.ESMP_TYPEKIND_R4, staggerloc = ESMP.ESMP_STAGGERLOC_CENTER) dstFldPtr = ESMP.ESMP_FieldGetPtr(dstFld) # set coords, mask, and field values for src and dst srcNtot = reduce(operator.mul, [srcDimsCenter[1][i] - srcDimsCenter[0][i] for i in range(2)]) srcXCenter[:] = numpy.reshape(so.getGrid().getLongitude()[srcDimsCenter[0][1]:srcDimsCenter[1][1], srcDimsCenter[0][0]:srcDimsCenter[1][0]], (srcNtot,)) srcYCenter[:] = numpy.reshape(so.getGrid().getLatitude()[srcDimsCenter[0][1]:srcDimsCenter[1][1], srcDimsCenter[0][0]:srcDimsCenter[1][0]], (srcNtot,)) srcFldPtr[:] = numpy.reshape(so[srcDimsCenter[0][1]:srcDimsCenter[1][1], srcDimsCenter[0][0]:srcDimsCenter[1][0]], (srcNtot,)) srcMask[:] = (srcFldPtr == so.missing_value) dstNtot = reduce(operator.mul, [dstDimsCenter[1][i] - dstDimsCenter[0][i] for i in range(2)]) # clt grid is rectilinear, transform to curvilinear lons = clt.getGrid().getLongitude() lats = clt.getGrid().getLatitude() ny, nx = dstDimsCenter[1][1]-dstDimsCenter[0][1], dstDimsCenter[1][0]-dstDimsCenter[0][0] xx = numpy.outer(numpy.ones((ny,), dtype=numpy.float32), lons) yy = numpy.outer(lats, numpy.ones((nx,), dtype=numpy.float32)) x = xx.reshape( (dstNtot,) ) y = yy.reshape( (dstNtot,) ) dstXCenter[:] = x[:] dstYCenter[:] = y[:] dstFldPtr[:] = 0 # regrid forward and backward maskVals = numpy.array([1], numpy.int32) # values defining mask regrid1 = ESMP.ESMP_FieldRegridStore(srcFld, dstFld, srcMaskValues=maskVals, dstMaskValues=None, regridmethod=ESMP.ESMP_REGRIDMETHOD_BILINEAR, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE, srcFracField=None, dstFracField=None) ESMP.ESMP_FieldRegrid(srcFld, dstFld, regrid1) jbeg, jend = dstDimsCenter[0][1], dstDimsCenter[1][1] ibeg, iend = dstDimsCenter[0][0], dstDimsCenter[1][0] soInterp = numpy.reshape(dstFldPtr, (jend-jbeg, iend-ibeg)) regrid2 = ESMP.ESMP_FieldRegridStore(dstFld, srcFld2, srcMaskValues=None, dstMaskValues=None, regridmethod=ESMP.ESMP_REGRIDMETHOD_BILINEAR, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE, srcFracField=None, dstFracField=None) ESMP.ESMP_FieldRegrid(dstFld, srcFld2, regrid2) jbeg, jend = srcDimsCenter[0][1], srcDimsCenter[1][1] ibeg, iend = srcDimsCenter[0][0], srcDimsCenter[1][0] soInterpInterp = numpy.reshape(srcFldPtr2, (jend-jbeg, iend-ibeg)) toc = time.time() #print 'time to interpolate (ESMF linear native) forward/backward: ', toc - tic ntot = reduce(operator.mul, so.shape) avgdiff = numpy.sum(so - soInterpInterp) / float(ntot) #print 'avgdiff = ', avgdiff self.assertLess(abs(avgdiff), 3.0) if PLOT: pylab.figure(3) pylab.subplot(2, 2, 1) pylab.pcolor(so, vmin=20.0, vmax=40.0) pylab.colorbar() pylab.title('ESMF linear native: so') pylab.subplot(2, 2, 2) pylab.pcolor(soInterp, vmin=20.0, vmax=40.0) pylab.colorbar() pylab.title('ESMF linear native: soInterp') pylab.subplot(2, 2, 3) pylab.pcolor(soInterpInterp, vmin=20.0, vmax=40.0) pylab.colorbar() pylab.title('ESMF linear native: soInterpInterp') pylab.subplot(2, 2, 4) pylab.pcolor(so - soInterpInterp, vmin=-0.5, vmax=0.5) pylab.colorbar() pylab.title('ESMF linear native: error') # clean up ESMP.ESMP_FieldRegridRelease(regrid2) ESMP.ESMP_FieldRegridRelease(regrid1) ESMP.ESMP_FieldDestroy(dstFld) ESMP.ESMP_GridDestroy(dstGrid) ESMP.ESMP_FieldDestroy(srcFld) ESMP.ESMP_FieldDestroy(srcFld2) ESMP.ESMP_GridDestroy(srcGrid)
def Xtest0_ESMP(self): import scipy.io.netcdf # # 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' srcF = scipy.io.netcdf.netcdf_file(inFile) #so = srcF.variables['so'][0, 0,...] missing_value = 1.e20 srcGrd = [srcF.variables['lat'][:], srcF.variables['lon'][:]] srcBounds = _getCorners([srcF.variables['lat_vertices'][:], srcF.variables['lon_vertices'][:]]) 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] srcDims = srcGrd[0].shape dstDims = dstGrd[0].shape srcMaxIndex = numpy.array(srcDims[::-1], numpy.int32) # number of cells dstMaxIndex = numpy.array(dstDims[::-1], numpy.int32) # number of cells # # 2. create grid objects # srcGrid = ESMP.ESMP_GridCreateNoPeriDim(srcMaxIndex, coordSys = coordSys) dstGrid = ESMP.ESMP_GridCreateNoPeriDim(dstMaxIndex, coordSys = coordSys) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc = CORNER) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc = CENTER) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc = CORNER) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc = CENTER) # add masks ESMP.ESMP_GridAddItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) srcGridMaskPtr = ESMP.ESMP_GridGetItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) ESMP.ESMP_GridAddItem(dstGrid, item=ESMP.ESMP_GRIDITEM_MASK) dstGridMaskPtr = ESMP.ESMP_GridGetItem(dstGrid, item=ESMP.ESMP_GRIDITEM_MASK) srcLoCorner, srcHiCorner = ESMP.ESMP_GridGetCoord(srcGrid, CORNER) srcLoCenter, srcHiCenter = ESMP.ESMP_GridGetCoord(srcGrid, CENTER) dstLoCorner, dstHiCorner = ESMP.ESMP_GridGetCoord(dstGrid, CORNER) dstLoCenter, dstHiCenter = ESMP.ESMP_GridGetCoord(dstGrid, CENTER) srcNx1 = srcHiCorner[0] - srcLoCorner[0] srcNy1 = srcHiCorner[1] - srcLoCorner[1] srcNx = srcHiCenter[0] - srcLoCenter[0] srcNy = srcHiCenter[1] - srcLoCenter[1] dstNx1 = dstHiCorner[0] - dstLoCorner[0] dstNy1 = dstHiCorner[1] - dstLoCorner[1] dstNx = dstHiCenter[0] - dstLoCenter[0] dstNy = dstHiCenter[1] - dstLoCenter[1] srcXCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, CORNER) srcYCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, CORNER) srcXCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, CENTER) srcYCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, CENTER) dstXCorner = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, CORNER) dstYCorner = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, CORNER) dstXCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, CENTER) dstYCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, CENTER) # # 3. create fields # srcFld = ESMP.ESMP_FieldCreateGrid(srcGrid, 'srcFld', typekind = R8, staggerloc = CENTER) dstFld = ESMP.ESMP_FieldCreateGrid(dstGrid, 'dstFld', typekind = R8, staggerloc = CENTER) srcAreaField = ESMP.ESMP_FieldCreateGrid(srcGrid, 'srcAreas', typekind = R8, staggerloc = CENTER) dstAreaField = ESMP.ESMP_FieldCreateGrid(dstGrid, 'dstAreas', typekind = R8, staggerloc = CENTER) srcFracField = ESMP.ESMP_FieldCreateGrid(srcGrid, 'srcFracAreas', typekind = R8, staggerloc = CENTER) dstFracField = ESMP.ESMP_FieldCreateGrid(dstGrid, 'dstFracAreas', typekind = R8, staggerloc = CENTER) srcFieldPtr = ESMP.ESMP_FieldGetPtr(srcFld) srcFracPtr = ESMP.ESMP_FieldGetPtr(srcFracField) dstFieldPtr = ESMP.ESMP_FieldGetPtr(dstFld) dstFracPtr = ESMP.ESMP_FieldGetPtr(dstFracField) # # 4. set the coordinates and the fields # srcYCorner[:] = srcBounds[0][srcLoCorner[1]:srcHiCorner[1], srcLoCorner[0]:srcHiCorner[0]].flat srcXCorner[:] = srcBounds[1][srcLoCorner[1]:srcHiCorner[1], srcLoCorner[0]:srcHiCorner[0]].flat # src-center coordinates, field, and mask yc = numpy.array(srcGrd[0][srcLoCenter[1]:srcHiCenter[1], srcLoCenter[0]:srcHiCenter[0]]) xc = numpy.array(srcGrd[1][srcLoCenter[1]:srcHiCenter[1], srcLoCenter[0]:srcHiCenter[0]]) srcYCenter[:] = yc.flat srcXCenter[:] = xc.flat # read the local data soLocalToPe = srcF.variables['so'][0, 0, srcLoCenter[1]:srcHiCenter[1], srcLoCenter[0]:srcHiCenter[0]] msk = numpy.array(soLocalToPe == missing_value, numpy.int32) srcGridMaskPtr[:] = msk.flat fld = numpy.array(soLocalToPe, soLocalToPe.dtype) # set to zero where masked fld *= (1 - msk) srcFieldPtr[:] = fld.flat srcFracPtr[:] = 1.0 dstYCorner[:] = dstBounds[0][dstLoCorner[1]:dstHiCorner[1], dstLoCorner[0]:dstHiCorner[0]].flat dstXCorner[:] = dstBounds[1][dstLoCorner[1]:dstHiCorner[1], dstLoCorner[0]:dstHiCorner[0]].flat # dst-center coordinates, field, and mask yc = numpy.array(dstGrd[0][dstLoCenter[1]:dstHiCenter[1], dstLoCenter[0]:dstHiCenter[0]]) xc = numpy.array(dstGrd[1][dstLoCenter[1]:dstHiCenter[1], dstLoCenter[0]:dstHiCenter[0]]) dstYCenter[:] = yc.flat dstXCenter[:] = xc.flat dstGridMaskPtr[:] = 0 dstFieldPtr[:] = missing_value dstFracPtr[:] = 1.0 # # 5. interpolate # maskVals = numpy.array([1], numpy.int32) # values defining mask regrid = ESMP.ESMP_FieldRegridStore(srcFld, dstFld, srcMaskValues=maskVals, dstMaskValues=maskVals, regridmethod=ESMP.ESMP_REGRIDMETHOD_CONSERVE, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE, srcFracField=srcFracField, dstFracField=dstFracField) ESMP.ESMP_FieldRegrid(srcFld, dstFld, regrid) # # 6. check # ESMP.ESMP_FieldRegridGetArea(srcAreaField) ESMP.ESMP_FieldRegridGetArea(dstAreaField) srcAreasPtr = ESMP.ESMP_FieldGetPtr(srcAreaField) dstAreasPtr = ESMP.ESMP_FieldGetPtr(dstAreaField) srcFracPtr = ESMP.ESMP_FieldGetPtr(srcFracField) dstFracPtr = ESMP.ESMP_FieldGetPtr(dstFracField) # conservation srcFldSum, dstFldSum = srcFieldPtr.sum(), dstFieldPtr.sum() srcFldIntegral = (srcFieldPtr * srcAreasPtr * srcFracPtr).sum() dstFldIntegral = (dstFieldPtr * dstAreasPtr * dstFracPtr).sum() lackConservLocal = srcFldIntegral - dstFldIntegral # nans if numpy.isnan(srcFracPtr).sum() > 0: print '[%d] *** %d Nans found in srcFracPtr!!' % \ (self.pe, numpy.isnan().sum(srcFracPtr)) if numpy.isnan(dstFracPtr).sum() > 0: print '[%d] *** %d Nans found in dstFracPtr!!' % \ (self.pe, numpy.isnan().sum(dstFracPtr)) print '[%d] checksum of src: %g checksum of dst: %g' % \ (self.pe, srcFldSum, dstFldSum) print '[%d] src total area integral: %g dst total area integral: %g diff: %g\n' % \ (self.pe, srcFldIntegral, dstFldIntegral, lackConservLocal) if HAS_MPI: lackConserv = MPI.COMM_WORLD.reduce(lackConservLocal, op=MPI.SUM, root=0) else: lackConserv = lackConservLocal if self.pe == 0: print 'ROOT: total lack of conservation (should be small): %f' % lackConserv # # 7. clean up # ESMP.ESMP_FieldRegridRelease(regrid) ESMP.ESMP_FieldDestroy(srcAreaField) ESMP.ESMP_FieldDestroy(dstAreaField) ESMP.ESMP_FieldDestroy(srcFracField) ESMP.ESMP_FieldDestroy(dstFracField) ESMP.ESMP_FieldDestroy(srcFld) ESMP.ESMP_FieldDestroy(dstFld) ESMP.ESMP_GridDestroy(srcGrid) ESMP.ESMP_GridDestroy(dstGrid)
def test1_EDGE1_linear_cart_nopd_native(self): # Cells maxIndexdstGrid = numpy.array(self.datadstGrid.shape[::-1], numpy.int32) grid = ESMP.ESMP_GridCreateNoPeriDim(maxIndexdstGrid, coordSys=ESMP.ESMP_COORDSYS_CART) ESMP.ESMP_GridAddCoord(grid, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) cGrdXPtr = ESMP.ESMP_GridGetCoordPtr(grid, 0, ESMP.ESMP_STAGGERLOC_CENTER) cGrdYPtr = ESMP.ESMP_GridGetCoordPtr(grid, 1, ESMP.ESMP_STAGGERLOC_CENTER) # Destination u and v fields in the cell centers cuFld = ESMP.ESMP_FieldCreateGrid( grid, 'cell_U_Fld', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) cvFld = ESMP.ESMP_FieldCreateGrid( grid, 'cell_V_Fld', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) cuFldPtr = ESMP.ESMP_FieldGetPtr(cuFld) cvFldPtr = ESMP.ESMP_FieldGetPtr(cvFld) # u Grid ESMP.ESMP_GridAddCoord(grid, staggerloc=ESMP.ESMP_STAGGERLOC_EDGE1) uGrdXPtr = ESMP.ESMP_GridGetCoordPtr(grid, 0, ESMP.ESMP_STAGGERLOC_EDGE1) uGrdYPtr = ESMP.ESMP_GridGetCoordPtr(grid, 1, ESMP.ESMP_STAGGERLOC_EDGE1) uFld = ESMP.ESMP_FieldCreateGrid(grid, 'srcEdge1', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_EDGE1) uFldPtr = ESMP.ESMP_FieldGetPtr(uFld) # v Grid ESMP.ESMP_GridAddCoord(grid, staggerloc=ESMP.ESMP_STAGGERLOC_EDGE2) vGrdXPtr = ESMP.ESMP_GridGetCoordPtr(grid, 0, ESMP.ESMP_STAGGERLOC_EDGE2) vGrdYPtr = ESMP.ESMP_GridGetCoordPtr(grid, 1, ESMP.ESMP_STAGGERLOC_EDGE2) vFld = ESMP.ESMP_FieldCreateGrid(grid, 'srcEdge2', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_EDGE2) vFldPtr = ESMP.ESMP_FieldGetPtr(vFld) # Get the indices ccLo, ccHi = ESMP.ESMP_GridGetCoord(grid, ESMP.ESMP_STAGGERLOC_CENTER) e1Lo, e1Hi = ESMP.ESMP_GridGetCoord(grid, ESMP.ESMP_STAGGERLOC_EDGE1) e2Lo, e2Hi = ESMP.ESMP_GridGetCoord(grid, ESMP.ESMP_STAGGERLOC_EDGE2) # Set the data cGrdXPtr[:] = self.dstGrid[1][ccLo[1]:ccHi[1], ccLo[0]:ccHi[0]].flat cGrdYPtr[:] = self.dstGrid[0][ccLo[1]:ccHi[1], ccLo[0]:ccHi[0]].flat uGrdXPtr[:] = self.dEdge2[1][e1Lo[1]:e1Hi[1], e1Lo[0]:e1Hi[0]].flat uGrdYPtr[:] = self.dEdge2[0][e1Lo[1]:e1Hi[1], e1Lo[0]:e1Hi[0]].flat vGrdXPtr[:] = self.dEdge1[1][e2Lo[1]:e2Hi[1], e2Lo[0]:e2Hi[0]].flat vGrdYPtr[:] = self.dEdge1[0][e2Lo[1]:e2Hi[1], e2Lo[0]:e2Hi[0]].flat cuFldPtr[:] = self.datadstGrid[ccLo[1]:ccHi[1], ccLo[0]:ccHi[0]].flat cvFldPtr[:] = self.datadstGrid[ccLo[1]:ccHi[1], ccLo[0]:ccHi[0]].flat uFldPtr[:] = self.dataEdge2[e1Lo[1]:e1Hi[1], e1Lo[0]:e1Hi[0]].flat vFldPtr[:] = self.dataEdge1[e2Lo[1]:e2Hi[1], e2Lo[0]:e2Hi[0]].flat # Regrid regridObj1 = ESMP.ESMP_FieldRegridStore( uFld, cuFld, srcMaskValues=None, dstMaskValues=None, regridmethod=ESMP.ESMP_REGRIDMETHOD_BILINEAR, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE, srcFracField=None, dstFracField=None) ESMP.ESMP_FieldRegrid(uFld, cuFld, regridObj1) regridObj2 = ESMP.ESMP_FieldRegridStore( vFld, cvFld, srcMaskValues=None, dstMaskValues=None, regridmethod=ESMP.ESMP_REGRIDMETHOD_BILINEAR, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE, srcFracField=None, dstFracField=None) ESMP.ESMP_FieldRegrid(vFld, cvFld, regridObj2) if self.pe == 0: print cuFldPtr.shape, self.datadstGrid.shape cuFldArr = numpy.reshape(cuFldPtr, self.datadstGrid.shape) cvFldArr = numpy.reshape(cvFldPtr, self.datadstGrid.shape) print print "U's" print self.dataEdge2 print cuFldArr print print self.dataEdge1 print cvFldArr
def test_2d(self): mype = 0 if HAS_MPI: mype = MPI.COMM_WORLD.Get_rank() 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, :, :] # ESMF interface, assume so and clt are cell centered srcGrid = regrid2.esmf.EsmfStructGrid( so[0, 0, ...].shape, coordSys=ESMP.ESMP_COORDSYS_SPH_DEG, periodicity=0) dstGrid = regrid2.esmf.EsmfStructGrid( clt.shape, coordSys=ESMP.ESMP_COORDSYS_SPH_DEG, periodicity=0) grid = [so.getGrid().getLatitude(), so.getGrid().getLongitude()] srcGrid.setCoords([numpy.array(g[:]) for g in grid], staggerloc=ESMP.ESMP_STAGGERLOC_CENTER, globalIndexing=True) # convert to curvilinear ny, nx = clt.shape y = clt.getGrid().getLatitude() x = clt.getGrid().getLongitude() yy = numpy.outer(y, numpy.ones((nx, ), numpy.float32)) xx = numpy.outer(numpy.ones((ny, ), numpy.float32), x) dstGrid.setCoords([yy, xx], staggerloc=ESMP.ESMP_STAGGERLOC_CENTER, globalIndexing=True) mask = numpy.zeros(so[0, 0, ...].shape, numpy.int32) mask[:] = (so[0, 0, ...] == so.missing_value) srcGrid.setMask(mask) srcFld = regrid2.esmf.EsmfStructField( srcGrid, 'srcFld', datatype=so[:].dtype, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) srcSlab = srcGrid.getLocalSlab(ESMP.ESMP_STAGGERLOC_CENTER) dstSlab = dstGrid.getLocalSlab(ESMP.ESMP_STAGGERLOC_CENTER) srcFld.setLocalData(numpy.array(so[0, 0, srcSlab[0], srcSlab[1]]), staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) dstFld = regrid2.esmf.EsmfStructField( dstGrid, 'dstFld', datatype=so.dtype, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) dstData = numpy.ones(clt.shape, numpy.float32)[dstSlab[0], dstSlab[1]] dstFld.setLocalData(so.missing_value * dstData, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) rgrd1 = regrid2.esmf.EsmfRegrid( srcFld, dstFld, srcFrac=None, dstFrac=None, srcMaskValues=numpy.array([1], numpy.int32), dstMaskValues=numpy.array([1], numpy.int32), regridMethod=ESMP.ESMP_REGRIDMETHOD_BILINEAR, unMappedAction=ESMP.ESMP_UNMAPPEDACTION_IGNORE) # now interpolate rgrd1(srcFld, dstFld) # get the data on this proc soInterpEsmfInterface = dstFld.getData(rootPe=None) # gather the data on proc 0 soInterpEsmfInterfaceRoot = dstFld.getData(rootPe=0) print '[%d] esmfInterface chksum = %f' % (mype, soInterpEsmfInterface.sum()) if mype == 0: print 'ROOT esmfInterface chksum = %f' % soInterpEsmfInterfaceRoot.sum( ) # Native ESMP srcMaxIndex = numpy.array(so[0, 0, ...].shape[::-1], dtype=numpy.int32) srcGrid = ESMP.ESMP_GridCreateNoPeriDim( srcMaxIndex, coordSys=ESMP.ESMP_COORDSYS_SPH_DEG) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) srcDimsCenter = ESMP.ESMP_GridGetCoord(srcGrid, ESMP.ESMP_STAGGERLOC_CENTER) srcXCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) srcYCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) dstMaxIndex = numpy.array(clt.shape[::-1], dtype=numpy.int32) dstGrid = ESMP.ESMP_GridCreateNoPeriDim( dstMaxIndex, coordSys=ESMP.ESMP_COORDSYS_SPH_DEG) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) dstDimsCenter = ESMP.ESMP_GridGetCoord(dstGrid, ESMP.ESMP_STAGGERLOC_CENTER) dstXCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) dstYCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) # mask ESMP.ESMP_GridAddItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) srcMask = ESMP.ESMP_GridGetItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) # create field srcFld = ESMP.ESMP_FieldCreateGrid( srcGrid, 'srcFld', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) srcFldPtr = ESMP.ESMP_FieldGetPtr(srcFld) dstFld = ESMP.ESMP_FieldCreateGrid( dstGrid, 'dstFld', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) dstFldPtr = ESMP.ESMP_FieldGetPtr(dstFld) # set coords, mask, and field values for src and dst srcNtot = reduce( operator.mul, [srcDimsCenter[1][i] - srcDimsCenter[0][i] for i in range(2)]) srcXCenter[:] = numpy.reshape( so.getGrid().getLongitude() [srcDimsCenter[0][1]:srcDimsCenter[1][1], srcDimsCenter[0][0]:srcDimsCenter[1][0]], (srcNtot, )) srcYCenter[:] = numpy.reshape( so.getGrid().getLatitude() [srcDimsCenter[0][1]:srcDimsCenter[1][1], srcDimsCenter[0][0]:srcDimsCenter[1][0]], (srcNtot, )) srcFldPtr[:] = numpy.reshape( so[0, 0, srcDimsCenter[0][1]:srcDimsCenter[1][1], srcDimsCenter[0][0]:srcDimsCenter[1][0]], (srcNtot, )) srcMask[:] = (srcFldPtr == so.missing_value) dstNtot = reduce( operator.mul, [dstDimsCenter[1][i] - dstDimsCenter[0][i] for i in range(2)]) # clt grid is rectilinear, transform to curvilinear lons = clt.getGrid().getLongitude() lats = clt.getGrid().getLatitude() ny, nx = dstDimsCenter[1][1] - dstDimsCenter[0][1], dstDimsCenter[1][ 0] - dstDimsCenter[0][0] localLons = lons[dstDimsCenter[0][0]:dstDimsCenter[1][0]] localLats = lats[dstDimsCenter[0][1]:dstDimsCenter[1][1]] xx = numpy.outer(numpy.ones((ny, ), dtype=numpy.float32), localLons) yy = numpy.outer(localLats, numpy.ones((nx, ), dtype=numpy.float32)) dstXCenter[:] = xx.reshape((dstNtot, )) dstYCenter[:] = yy.reshape((dstNtot, )) dstFldPtr[:] = so.missing_value # regrid forward and backward maskVals = numpy.array([1], numpy.int32) # values defining mask regrid1 = ESMP.ESMP_FieldRegridStore( srcFld, dstFld, srcMaskValues=maskVals, dstMaskValues=None, regridmethod=ESMP.ESMP_REGRIDMETHOD_BILINEAR, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE, srcFracField=None, dstFracField=None) ESMP.ESMP_FieldRegrid(srcFld, dstFld, regrid1, zeroregion=ESMP.ESMP_REGION_SELECT) jbeg, jend = dstDimsCenter[0][1], dstDimsCenter[1][1] ibeg, iend = dstDimsCenter[0][0], dstDimsCenter[1][0] soInterpESMP = numpy.reshape(dstFldPtr, (jend - jbeg, iend - ibeg)) # check local diffs ntot = reduce(operator.mul, soInterpESMP.shape) avgdiff = numpy.sum(soInterpEsmfInterface - soInterpESMP) / float(ntot) self.assertLess(abs(avgdiff), 1.e-7) # check gather chksumESMP = numpy.sum(soInterpESMP) chksumEsmfInterface = numpy.sum(soInterpEsmfInterface) if HAS_MPI: chksumsESMP = MPI.COMM_WORLD.gather(chksumESMP, root=0) else: chksumsESMP = chksumESMP print '[%d] ESMP chksum = %f' % (mype, chksumESMP) if mype == 0: print 'ROOT ESMP chksum = %f' % numpy.sum(chksumsESMP) if mype == 0: chksumESMPRoot = numpy.sum(chksumsESMP) chksumESMFInterfaceRoot = numpy.sum(soInterpEsmfInterfaceRoot) self.assertLess(abs(chksumESMFInterfaceRoot - chksumESMPRoot), 1.e-5 * chksumESMPRoot) if PLOT: pylab.subplot(2, 2, 1) pylab.pcolor(so, vmin=20.0, vmax=40.0) pylab.colorbar() pylab.title('so') pylab.subplot(2, 2, 2) pylab.pcolor(soInterpEsmfInterface - soInterpESMP, vmin=-0.5, vmax=0.5) pylab.colorbar() pylab.title('[%d] EsmfInterface - ESMP' % mype) pylab.subplot(2, 2, 3) pylab.pcolor(soInterpEsmfInterface, vmin=20.0, vmax=40.0) pylab.colorbar() pylab.title('[%d] ESMFInterface' % mype) pylab.subplot(2, 2, 4) pylab.pcolor(soInterpESMP, vmin=20, vmax=40) pylab.colorbar() pylab.title('[%d] ESMP' % mype) # clean up ESMP.ESMP_FieldRegridRelease(regrid1) ESMP.ESMP_FieldDestroy(dstFld) ESMP.ESMP_GridDestroy(dstGrid) ESMP.ESMP_FieldDestroy(srcFld) ESMP.ESMP_GridDestroy(srcGrid)
def test1_ESMP(self): rk = 0 if HAS_MPI: rk = MPI.COMM_WORLD.Get_rank() coordSys = ESMP.ESMP_COORDSYS_CART nres = 4 self.srcDims = (4 * nres, 3 * nres) self.dstDims = (4 * nres, 3 * nres) srcMaxIndex = numpy.array(self.srcDims, numpy.int32) # number of cells dstMaxIndex = numpy.array(self.dstDims, numpy.int32) # number of cells # grids srcGrid = ESMP.ESMP_GridCreateNoPeriDim(srcMaxIndex, coordSys=coordSys) dstGrid = ESMP.ESMP_GridCreateNoPeriDim(dstMaxIndex, coordSys=coordSys) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) # masks ESMP.ESMP_GridAddItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) srcGridMaskPtr = ESMP.ESMP_GridGetItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) ESMP.ESMP_GridAddItem(dstGrid, item=ESMP.ESMP_GRIDITEM_MASK) dstGridMaskPtr = ESMP.ESMP_GridGetItem(dstGrid, item=ESMP.ESMP_GRIDITEM_MASK) # coordinates srcXCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CORNER) srcYCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CORNER) srcLoCorner, srcHiCorner = ESMP.ESMP_GridGetCoord( srcGrid, ESMP.ESMP_STAGGERLOC_CORNER) srcXCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) srcYCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) srcLoCenter, srcHiCenter = ESMP.ESMP_GridGetCoord( srcGrid, ESMP.ESMP_STAGGERLOC_CENTER) dstXCorner = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, ESMP.ESMP_STAGGERLOC_CORNER) dstYCorner = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, ESMP.ESMP_STAGGERLOC_CORNER) dstLoCorner, dstHiCorner = ESMP.ESMP_GridGetCoord( dstGrid, ESMP.ESMP_STAGGERLOC_CORNER) dstXCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) dstYCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) dstLoCenter, dstHiCenter = ESMP.ESMP_GridGetCoord( dstGrid, ESMP.ESMP_STAGGERLOC_CENTER) # fields srcFld = ESMP.ESMP_FieldCreateGrid( srcGrid, 'srcFld', typekind=ESMP.ESMP_TYPEKIND_R8, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) dstFld = ESMP.ESMP_FieldCreateGrid( dstGrid, 'dstFld', typekind=ESMP.ESMP_TYPEKIND_R8, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) srcFieldPtr = ESMP.ESMP_FieldGetPtr(srcFld) dstFieldPtr = ESMP.ESMP_FieldGetPtr(dstFld) # set the coordinates and field values # src-corners srcNx1 = srcHiCorner[0] - srcLoCorner[0] srcNy1 = self.srcDims[1] + 1 self.dims = self.srcDims for iy in range(srcNy1): iyp = iy + srcLoCorner[0] for ix in range(srcNx1): ixp = ix + srcLoCorner[1] srcXCorner[ix + iy * srcNx1] = self.xFunct(iyp, ixp) srcYCorner[ix + iy * srcNx1] = self.yFunct(iyp, ixp) # src-centers and mask srcNx = srcHiCenter[0] - srcLoCenter[0] srcNy = srcHiCenter[1] - srcLoCenter[1] self.dims = self.srcDims for iy in range(srcNy): iyp = iy + srcLoCenter[0] for ix in range(srcNx): ixp = ix + srcLoCenter[1] x = self.xFunct(iyp + 0.5, ixp + 0.5) y = self.yFunct(iyp + 0.5, ixp + 0.5) srcXCenter[ix + iy * srcNx] = x srcYCenter[ix + iy * srcNx] = y mask = 0 if ((iyp * ixp) % 3) == 1: mask = 1 srcGridMaskPtr[ix + iy * srcNx] = mask # valid srcFieldPtr[ix + iy * srcNx] = self.func(ixp, iyp) # dst-corners dstNx1 = dstHiCorner[0] - dstLoCorner[0] dstNy1 = dstHiCorner[1] - dstLoCorner[1] self.dims = self.dstDims for iy in range(dstNy1): iyp = iy + dstLoCorner[0] for ix in range(dstNx1): ixp = ix + dstLoCorner[1] dstXCorner[ix + iy * dstNx1] = self.xFunct(iyp, ixp) dstYCorner[ix + iy * dstNx1] = self.yFunct(iyp, ixp) # dst-centers and mask dstNx = dstHiCenter[0] - dstLoCenter[0] dstNy = dstHiCenter[1] - dstLoCenter[1] self.dims = self.dstDims for iy in range(dstNy): iyp = iy + dstLoCenter[0] for ix in range(dstNx): ixp = ix + dstLoCenter[1] x = self.xFunct(iyp + 0.5, ixp + 0.5) y = self.yFunct(iyp + 0.5, ixp + 0.5) dstXCenter[ix + iy * dstNx] = x dstYCenter[ix + iy * dstNx] = y dstGridMaskPtr[ix + iy * dstNx] = 0 # valid dstFieldPtr[ix + iy * dstNx] = -20 # fill value srcAreaField = ESMP.ESMP_FieldCreateGrid(srcGrid, 'srcArea') dstAreaField = ESMP.ESMP_FieldCreateGrid(dstGrid, 'dstArea') srcFracField = ESMP.ESMP_FieldCreateGrid(srcGrid, 'srcFrac') dstFracField = ESMP.ESMP_FieldCreateGrid(dstGrid, 'dstFrac') # IF you want to set your own area. These lines are required. # Otherwise, let ESMF do it. # ESMP.ESMP_GridAddItem(srcGrid, item = ESMP.ESMP_GRIDITEM_AREA) # srcAreas = ESMP.ESMP_GridGetItem(srcGrid, item = ESMP.ESMP_GRIDITEM_AREA) # ESMP.ESMP_GridAddItem(dstGrid, item = ESMP.ESMP_GRIDITEM_AREA) # dstAreas = ESMP.ESMP_GridGetItem(dstGrid, item = ESMP.ESMP_GRIDITEM_AREA) # srcAreas[:] = 0.02080333333 # dstAreas[:] = 0.08333333333 # interpolation maskVals = numpy.array([1], numpy.int32) # values defining mask regrid = ESMP.ESMP_FieldRegridStore( srcFld, dstFld, srcMaskValues=maskVals, dstMaskValues=None, regridmethod=ESMP.ESMP_REGRIDMETHOD_CONSERVE, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE, srcFracField=srcFracField, dstFracField=None) ESMP.ESMP_FieldRegrid(srcFld, dstFld, regrid) # get the cell areas ESMP.ESMP_FieldRegridGetArea(srcAreaField) ESMP.ESMP_FieldRegridGetArea(dstAreaField) srcarea = ESMP.ESMP_FieldGetPtr(srcAreaField) dstarea = ESMP.ESMP_FieldGetPtr(dstAreaField) srcFracPtr = ESMP.ESMP_FieldGetPtr(srcFracField) # check conservation marr = numpy.array(mask == 0, dtype=numpy.int32) srcFldSum, dstFldSum = srcFieldPtr.sum(), dstFieldPtr.sum() srcFldIntegral = (srcFieldPtr * srcarea * srcFracPtr).sum() dstFldIntegral = (dstFieldPtr * dstarea).sum() lackConservLocal = srcFldIntegral - dstFldIntegral print '[%d] src corner lo = %s hi = %s dst corner lo = %s hi = %s' % ( rk, str(srcLoCorner), str(srcHiCorner), str(dstLoCorner), str(dstHiCorner)) print '[%d] src center lo = %s hi = %s dst center lo = %s hi = %s' % ( rk, str(srcLoCenter), str(srcHiCenter), str(dstLoCenter), str(dstHiCenter)) print '[%d] checksum of src: %f checksum of dst: %f' % (rk, srcFldSum, dstFldSum) print '[%d] src total area integral: %g dst total area integral: %g diff: %g\n' % \ (rk, srcFldIntegral, dstFldIntegral, lackConservLocal) if HAS_MPI: lackConserv = MPI.COMM_WORLD.reduce(lackConservLocal, op=MPI.SUM, root=0) else: lackConserv = lackConservLocal if rk == 0: print '[0] total lack of conservation (should be small): %f' % lackConserv assert (abs(lackConserv) < 1.e-6) # cleanup ESMP.ESMP_FieldRegridRelease(regrid) ESMP.ESMP_FieldDestroy(srcFld) ESMP.ESMP_FieldDestroy(dstFld) ESMP.ESMP_GridDestroy(srcGrid) ESMP.ESMP_GridDestroy(dstGrid)
def test_2d_esmf_native(self): print 'running test_2d_esmf_native...' f = cdms2.open(sys.prefix + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = f('so')[0, 0, :, :] ny, nx = so.shape soBounds = so.getGrid().getBounds() srcLatCorner = numpy.zeros( (ny+1, nx+1), numpy.float32 ) srcLatCorner[:ny, :nx] = soBounds[0][:, :, 0] srcLatCorner[:ny, nx] = soBounds[0][:ny, nx-1, 1] srcLatCorner[ny, nx] = soBounds[0][ny-1, nx-1, 2] srcLatCorner[ny, :nx] = soBounds[0][ny-1, :nx, 3] srcLonCorner = numpy.zeros( (ny+1, nx+1), numpy.float32 ) srcLonCorner[:ny, :nx] = soBounds[1][:, :, 0] srcLonCorner[:ny, nx] = soBounds[1][:ny, nx-1, 1] srcLonCorner[ny, nx] = soBounds[1][ny-1, nx-1, 2] srcLonCorner[ny, :nx] = soBounds[1][ny-1, :nx, 3] # create grid srcMaxIndex = numpy.array(so.shape[::-1], dtype=numpy.int32) srcGrid = ESMP.ESMP_GridCreateNoPeriDim(srcMaxIndex, coordSys = ESMP.ESMP_COORDSYS_SPH_DEG) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc = ESMP.ESMP_STAGGERLOC_CENTER) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc = ESMP.ESMP_STAGGERLOC_CORNER) srcDimsCenter = ESMP.ESMP_GridGetCoord(srcGrid, ESMP.ESMP_STAGGERLOC_CENTER) srcDimsCorner = ESMP.ESMP_GridGetCoord(srcGrid, ESMP.ESMP_STAGGERLOC_CORNER) srcXCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) srcYCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) srcXCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CORNER) srcYCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CORNER) # mask ESMP.ESMP_GridAddItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) srcMask = ESMP.ESMP_GridGetItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) # create field srcFld = ESMP.ESMP_FieldCreateGrid(srcGrid, 'srcFld', typekind = ESMP.ESMP_TYPEKIND_R4, staggerloc = ESMP.ESMP_STAGGERLOC_CENTER) srcFldPtr = ESMP.ESMP_FieldGetPtr(srcFld) # local field # set coords, mask, and field values for src srcNtotCenter = reduce(operator.mul, [srcDimsCenter[1][i] - srcDimsCenter[0][i] \ for i in range(2)]) srcNtotCorner = reduce(operator.mul, [srcDimsCorner[1][i] - srcDimsCorner[0][i] \ for i in range(2)]) srcJCenterBeg = srcDimsCenter[0][1] srcJCenterEnd = srcDimsCenter[1][1] srcJCornerBeg = srcDimsCorner[0][1] srcJCornerEnd = srcDimsCorner[1][1] srcICenterBeg = srcDimsCenter[0][0] srcICenterEnd = srcDimsCenter[1][0] srcICornerBeg = srcDimsCorner[0][0] srcICornerEnd = srcDimsCorner[1][0] srcLonCenter = so.getLongitude() srcLatCenter = so.getLatitude() srcXCenter[:] = numpy.reshape(srcLonCenter[srcJCenterBeg:srcJCenterEnd, srcICenterBeg:srcICenterEnd], (srcNtotCenter,)) srcYCenter[:] = numpy.reshape(srcLatCenter[srcJCenterBeg:srcJCenterEnd, srcICenterBeg:srcICenterEnd], (srcNtotCenter,)) srcXCorner[:] = numpy.reshape(srcLonCorner[srcJCornerBeg:srcJCornerEnd, srcICornerBeg:srcICornerEnd], (srcNtotCorner,)) srcYCorner[:] = numpy.reshape(srcLatCorner[srcJCornerBeg:srcJCornerEnd, srcICornerBeg:srcICornerEnd], (srcNtotCorner,)) srcFldPtr[:] = numpy.reshape(so[srcJCenterBeg:srcJCenterEnd, srcICenterBeg:srcICenterEnd], (srcNtotCenter,)) srcMask[:] = (srcFldPtr == so.missing_value) # pe local start/end indices jbeg, jend = srcDimsCenter[0][1], srcDimsCenter[1][1] ibeg, iend = srcDimsCenter[0][0], srcDimsCenter[1][0] lo, hi = ESMP.ESMP_GridGetCoord(srcGrid) ptr = ESMP.ESMP_FieldGetPtr(srcFld) chksum = numpy.sum(ptr) # gather the result on proc 0 from mpi4py import MPI comm = MPI.COMM_WORLD chksums = comm.gather(chksum, root = 0) ptrs = comm.gather(ptr, root = 0) mype = comm.Get_rank() nprocs = comm.Get_size() if mype == 0: for i in range(nprocs): chksum = numpy.sum(ptrs[i]) print '[%d] ptrs[i].shape = %s sum(ptrs[i]) = %f' % (i, str(ptrs[i].shape), chksum) self.assertEqual(chksum, chksums[i]) # clean up ESMP.ESMP_FieldDestroy(srcFld) ESMP.ESMP_GridDestroy(srcGrid)
def Xtest1_3D_Native_Bilinear(self): print 'test1' srcDims, srcXYZCenter, srcData, srcBounds = makeGrid(5, 4, 3) dstDims, dstXYZCenter, dstData, dstBounds = makeGrid(5, 4, 3) # Initialize the grids **without** coordinates maxIndex = numpy.array(dstDims[::-1], dtype = numpy.int32) dstGrid3D = ESMP.ESMP_GridCreateNoPeriDim(maxIndex, coordSys = ESMP.ESMP_COORDSYS_CART) maxIndex = numpy.array(srcDims[::-1], dtype = numpy.int32) srcGrid3D = ESMP.ESMP_GridCreateNoPeriDim(maxIndex, coordSys = ESMP.ESMP_COORDSYS_CART) # Populate the Grids dstXYZCtrPtr, dstIJKbe, dstCtrShape = esmfGrid(dstGrid3D, dstXYZCenter, ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) dstXYZCnrPtr, dstIJKbe, dstCnrShape = esmfGrid(dstGrid3D, dstBounds, ESMP.ESMP_STAGGERLOC_CORNER_VFACE) srcXYZCtrPtr, srcIJKbe, srcCtrShape = esmfGrid(srcGrid3D, srcXYZCenter, ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) srcXYZCnrPtr, srcIJKbe, srcCnrShape = esmfGrid(srcGrid3D, srcBounds, ESMP.ESMP_STAGGERLOC_CORNER_VFACE) # initialize the fields **without** data, after ESMP_GridAddCoord dstField = ESMP.ESMP_FieldCreateGrid(dstGrid3D, 'dstDataCtr', staggerloc = ESMP.ESMP_STAGGERLOC_CENTER_VCENTER, typekind = ESMP.ESMP_TYPEKIND_R4) srcField = ESMP.ESMP_FieldCreateGrid(srcGrid3D, 'srcDataCtr', staggerloc = ESMP.ESMP_STAGGERLOC_CENTER_VCENTER, typekind = ESMP.ESMP_TYPEKIND_R4) srcIntFd = ESMP.ESMP_FieldCreateGrid(srcGrid3D, 'srcDataCtr', staggerloc = ESMP.ESMP_STAGGERLOC_CENTER_VCENTER, typekind = ESMP.ESMP_TYPEKIND_R4) # Populate the fields dstFieldPtr = ESMP.ESMP_FieldGetPtr(dstField) dstFieldPtr[:] = 0 srcFieldPtr = ESMP.ESMP_FieldGetPtr(srcField) srcFieldPtr[:] = srcData[srcIJKbe].flat srcIntFdPtr = ESMP.ESMP_FieldGetPtr(srcIntFd) srcIntFdPtr[:] = -1 srcMaskValues = numpy.array([1], numpy.int32) # Regrid regridOut = ESMP.ESMP_FieldRegridStore(srcField, dstField, srcMaskValues = srcMaskValues, dstMaskValues = None, srcFracField = None, dstFracField = None, regridmethod = ESMP.ESMP_REGRIDMETHOD_BILINEAR, unmappedaction = ESMP.ESMP_UNMAPPEDACTION_IGNORE) ESMP.ESMP_FieldRegrid(srcField, dstField, regridOut) regridBck = ESMP.ESMP_FieldRegridStore(dstField, srcIntFd, srcMaskValues = None, dstMaskValues = None, srcFracField = None, dstFracField = None, regridmethod = ESMP.ESMP_REGRIDMETHOD_BILINEAR, unmappedaction = ESMP.ESMP_UNMAPPEDACTION_IGNORE) ESMP.ESMP_FieldRegrid(dstField, srcIntFd, regridBck) minlsd, maxlsd = srcData.min(), srcData.max() minlsi, maxlsi = dstFieldPtr.min(), dstFieldPtr.max() minlii, maxlii = srcIntFdPtr.min(), srcIntFdPtr.max() minsd = MPI.COMM_WORLD.reduce(minlsd, op = MPI.MIN, root = self.rootPe) maxsd = MPI.COMM_WORLD.reduce(maxlsd, op = MPI.MAX, root = self.rootPe) minsi = MPI.COMM_WORLD.reduce(minlsi, op = MPI.MIN, root = self.rootPe) maxsi = MPI.COMM_WORLD.reduce(maxlsi, op = MPI.MAX, root = self.rootPe) minii = MPI.COMM_WORLD.reduce(minlii, op = MPI.MIN, root = self.rootPe) maxii = MPI.COMM_WORLD.reduce(maxlii, op = MPI.MAX, root = self.rootPe) if self.pe == self.rootPe: print(minsd, minsi) print(minsd, minii) print(maxsd, maxsi) print(maxsd, maxii) self.assertEqual(minsd, minsi) self.assertEqual(minsd, minii) self.assertEqual(maxsd, maxsi) self.assertEqual(maxsd, maxii)
def test_3D_Native(self): print 'running test_3d_esmf_native...' f = cdms2.open(cdat_info.get_prefix() + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = f('so')[0, ...] # Coords soLats = so.getLatitude()[:] soLons = so.getLongitude()[:] soLevs = so.getLevel()[:] soX1 = numpy.ones(soLats.shape) soZ1 = numpy.ones(soLevs.shape) soLevs = numpy.outer(soLevs, soX1).reshape(so.shape) soLats = numpy.outer(soZ1, soLats).reshape(so.shape) soLons = numpy.outer(soZ1, soLons).reshape(so.shape) shape1 = tuple([i + 1 for i in so.shape]) shape = so.shape print 'Source Center' srcXYZCenter = convertToXYZ(soLons, soLats, soLevs) # Bounds soLtBd0 = so.getLatitude().getBounds() soLnBd0 = so.getLongitude().getBounds() soLvBd0 = so.getLevel().getBounds() soLtBd1 = numpy.zeros(shape1, dtype=numpy.float32) soLnBd1 = numpy.zeros(shape1, dtype=numpy.float32) soLvBd1 = numpy.zeros(shape1, dtype=numpy.float32) soLtBd1[:, :shape[1], :shape[2]] = soLtBd0[:, :, 0] soLtBd1[:, shape[1], :shape[2]] = soLtBd0[shape[1] - 1, :, 3] soLtBd1[:, :shape[1], shape[2]] = soLtBd0[:, shape[2] - 1, 1] soLtBd1[:, shape[1], shape[2]] = soLtBd0[shape[1] - 1, shape[2] - 1, 2] soLnBd1[:, :shape[1], :shape[2]] = soLnBd0[:, :, 0] soLnBd1[:, shape[1], :shape[2]] = soLnBd0[shape[1] - 1, :, 3] soLnBd1[:, :shape[1], shape[2]] = soLnBd0[:, shape[2] - 1, 1] soLnBd1[:, shape[1], shape[2]] = soLnBd0[shape[1] - 1, shape[2] - 1, 2] soLvBd2 = numpy.zeros(shape1[0], dtype=numpy.float32) soLvBd2[:-1] = soLvBd0[:, 0] soLvBd2[-1] = soLvBd0[-1, 1] soLvBd1 = numpy.outer(soLvBd2, numpy.ones(shape1[1:])).reshape(shape1) print 'Source Corner' srcXYZCorner = convertToXYZ(soLtBd1, soLnBd1, soLvBd1) clt = cdms2.open(cdat_info.get_prefix() + '/sample_data/clt.nc')('clt')[0, :, :] cltBounds = clt.getGrid().getBounds() # Destination grid dimensions nz = 10 ny, nx = clt.shape shape = (nz, ny, nx) nz1, ny1, nx1 = nz + 1, ny + 1, nx + 1 shape1 = (nz1, ny1, nx1) # Corners (Nodes) - Destination zb = numpy.linspace(0., 5000., nz1) yb = numpy.zeros((ny1, ), numpy.float32) yb[:ny] = cltBounds[0][:, 0] yb[ny] = cltBounds[0][ny - 1, 1] xb = numpy.zeros((nx1, ), numpy.float32) xb[:nx] = cltBounds[1][:, 0] xb[nx] = cltBounds[1][nx - 1, 1] # Centers (Cells) - Destination zint = (zb[1] - zb[0]) / 2 z = numpy.linspace(zint, 5000 - zint, nz) y = clt.getLatitude() x = clt.getLongitude() z1 = numpy.ones((nz1, ), numpy.float32) y1 = numpy.ones((ny1, ), numpy.float32) x1 = numpy.ones((nx1, ), numpy.float32) dstLevCorner = numpy.outer(zb, numpy.outer(y1, x1)).reshape((shape1)) dstLatCorner = numpy.outer(z1, numpy.outer(yb, x1)).reshape((shape1)) dstLonCorner = numpy.outer(z1, numpy.outer(y1, xb)).reshape((shape1)) print 'Desintation Corner' dstXYZCorner = convertToXYZ(dstLonCorner, dstLatCorner, dstLevCorner) # make Centers curvilinear z1 = numpy.ones((nz, ), numpy.float32) y1 = numpy.ones((ny, ), numpy.float32) x1 = numpy.ones((nx, ), numpy.float32) dstLevCenter = numpy.outer(z, numpy.outer(y1, x1)).reshape((shape)) dstLatCenter = numpy.outer(z1, numpy.outer(y, x1)).reshape((shape)) dstLonCenter = numpy.outer(z1, numpy.outer(y1, x)).reshape((shape)) print 'Desintation Center' dstXYZCenter = convertToXYZ(dstLonCenter, dstLatCenter, dstLevCenter) tic = time.time() dstShapeCorner = dstXYZCorner[0].shape dstShapeCenter = dstXYZCenter[0].shape srcShapeCorner = srcXYZCorner[0].shape srcShapeCenter = srcXYZCenter[0].shape # Establish the destination grid maxIndex = numpy.array(dstShapeCenter[::-1], dtype=numpy.int32) dstGrid3D = ESMP.ESMP_GridCreateNoPeriDim( maxIndex, coordSys=ESMP.ESMP_COORDSYS_CART) # Destination Corners ESMP.ESMP_GridAddCoord(dstGrid3D, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER_VFACE) dstDimsCorner = ESMP.ESMP_GridGetCoord( dstGrid3D, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER_VFACE) dstXCorner = ESMP.ESMP_GridGetCoordPtr( dstGrid3D, 0, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER_VFACE) dstYCorner = ESMP.ESMP_GridGetCoordPtr( dstGrid3D, 1, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER_VFACE) dstZCorner = ESMP.ESMP_GridGetCoordPtr( dstGrid3D, 2, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER_VFACE) dstNtotCorner = reduce(operator.mul, [dstDimsCorner[1][i] - dstDimsCorner[0][i] \ for i in range(3)]) dstICornerBE = slice(dstDimsCorner[0][0], dstDimsCorner[1][0]) dstJCornerBE = slice(dstDimsCorner[0][1], dstDimsCorner[1][1]) dstKCornerBE = slice(dstDimsCorner[0][2], dstDimsCorner[1][2]) dstXCorner[:] = dstXYZCorner[0][dstKCornerBE, dstJCornerBE, dstICornerBE].reshape(dstNtotCorner) dstYCorner[:] = dstXYZCorner[1][dstKCornerBE, dstJCornerBE, dstICornerBE].reshape(dstNtotCorner) dstZCorner[:] = dstXYZCorner[2][dstKCornerBE, dstJCornerBE, dstICornerBE].reshape(dstNtotCorner) # Destination Centers ESMP.ESMP_GridAddCoord(dstGrid3D, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) dstDimsCenter = ESMP.ESMP_GridGetCoord( dstGrid3D, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) dstXCenter = ESMP.ESMP_GridGetCoordPtr( dstGrid3D, 0, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) dstYCenter = ESMP.ESMP_GridGetCoordPtr( dstGrid3D, 1, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) dstZCenter = ESMP.ESMP_GridGetCoordPtr( dstGrid3D, 2, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) # Dimensions for local processor dstNtotCenter = reduce(operator.mul, [dstDimsCenter[1][i] - dstDimsCenter[0][i] \ for i in range(3)]) dstICenterBE = slice(dstDimsCenter[0][0], dstDimsCenter[1][0]) dstJCenterBE = slice(dstDimsCenter[0][1], dstDimsCenter[1][1]) dstKCenterBE = slice(dstDimsCenter[0][2], dstDimsCenter[1][2]) dstKJIShape = ((dstDimsCenter[1][2] - dstDimsCenter[0][2]), (dstDimsCenter[1][1] - dstDimsCenter[0][1]), (dstDimsCenter[1][0] - dstDimsCenter[0][0])) # Populate Destination Grid dstXCenter[:] = dstXYZCenter[0][dstKCenterBE, dstJCenterBE, dstICenterBE].flat dstYCenter[:] = dstXYZCenter[1][dstKCenterBE, dstJCenterBE, dstICenterBE].flat dstZCenter[:] = dstXYZCenter[2][dstKCenterBE, dstJCenterBE, dstICenterBE].flat # Create and Populate Destination Field dstField = ESMP.ESMP_FieldCreateGrid( dstGrid3D, 'dst_salinity', staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER, typekind=ESMP.ESMP_TYPEKIND_R4) dstFieldPtr = ESMP.ESMP_FieldGetPtr(dstField) dstFieldPtr[:] = 1. # Establish the source grid maxIndex = numpy.array(so.shape[::-1], dtype=numpy.int32) srcGrid3D = ESMP.ESMP_GridCreateNoPeriDim( maxIndex, coordSys=ESMP.ESMP_COORDSYS_CART) # Source Corners ESMP.ESMP_GridAddCoord(srcGrid3D, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER_VFACE) srcDimsCorner = ESMP.ESMP_GridGetCoord( srcGrid3D, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER_VFACE) srcXCorner = ESMP.ESMP_GridGetCoordPtr( srcGrid3D, 0, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER_VFACE) srcYCorner = ESMP.ESMP_GridGetCoordPtr( srcGrid3D, 1, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER_VFACE) srcZCorner = ESMP.ESMP_GridGetCoordPtr( srcGrid3D, 2, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER_VFACE) # Dimensions for local processor srcNtotCorner = reduce(operator.mul, [srcDimsCorner[1][i] - srcDimsCorner[0][i] \ for i in range(3)]) srcICornerBE = slice(srcDimsCorner[0][0], srcDimsCorner[1][0]) srcJCornerBE = slice(srcDimsCorner[0][1], srcDimsCorner[1][1]) srcKCornerBE = slice(srcDimsCorner[0][2], srcDimsCorner[1][2]) srcKJIShape = ((srcDimsCorner[1][2] - srcDimsCorner[0][2]), (srcDimsCorner[1][1] - srcDimsCorner[0][1]), (srcDimsCorner[1][0] - srcDimsCorner[0][0])) # Populate the Source Grid Corners srcXCorner[:] = numpy.reshape( srcXYZCorner[0][srcKCornerBE, srcJCornerBE, srcICornerBE], srcNtotCorner) srcYCorner[:] = numpy.reshape( srcXYZCorner[1][srcKCornerBE, srcJCornerBE, srcICornerBE], srcNtotCorner) srcZCorner[:] = numpy.reshape( srcXYZCorner[2][srcKCornerBE, srcJCornerBE, srcICornerBE], srcNtotCorner) # Source Centers ESMP.ESMP_GridAddCoord(srcGrid3D, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) srcDimsCenter = ESMP.ESMP_GridGetCoord( srcGrid3D, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) srcXCenter = ESMP.ESMP_GridGetCoordPtr( srcGrid3D, 0, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) srcYCenter = ESMP.ESMP_GridGetCoordPtr( srcGrid3D, 1, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) srcZCenter = ESMP.ESMP_GridGetCoordPtr( srcGrid3D, 2, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER) # Dimensions for local processor srcNtotCenter = reduce(operator.mul, [srcDimsCenter[1][i] - srcDimsCenter[0][i] \ for i in range(3)]) srcICenterBE = slice(srcDimsCenter[0][0], srcDimsCenter[1][0]) srcJCenterBE = slice(srcDimsCenter[0][1], srcDimsCenter[1][1]) srcKCenterBE = slice(srcDimsCenter[0][2], srcDimsCenter[1][2]) srcKJIShape = ((srcDimsCenter[1][2] - srcDimsCenter[0][2]) - 1, (srcDimsCenter[1][1] - srcDimsCenter[0][1]) - 1, (srcDimsCenter[1][0] - srcDimsCenter[0][0]) - 1) # Populate the Source Grid Centers srcXCenter[:] = numpy.reshape( srcXYZCenter[0][srcKCenterBE, srcJCenterBE, srcICenterBE], srcNtotCenter) srcYCenter[:] = numpy.reshape( srcXYZCenter[1][srcKCenterBE, srcJCenterBE, srcICenterBE], srcNtotCenter) srcZCenter[:] = numpy.reshape( srcXYZCenter[2][srcKCenterBE, srcJCenterBE, srcICenterBE], srcNtotCenter) # Source Mask ESMP.ESMP_GridAddItem(srcGrid3D, item=ESMP.ESMP_GRIDITEM_MASK) srcMaskPtr = ESMP.ESMP_GridGetItem(srcGrid3D, item=ESMP.ESMP_GRIDITEM_MASK) srcMaskPtr[:] = numpy.reshape( so[srcKCornerBE, srcJCornerBE, srcICornerBE].mask, srcNtotCenter) # Create and Populate Source Field srcField = ESMP.ESMP_FieldCreateGrid( srcGrid3D, 'src_salinity', staggerloc=ESMP.ESMP_STAGGERLOC_CENTER_VCENTER, typekind=ESMP.ESMP_TYPEKIND_R4) srcFieldPtr = ESMP.ESMP_FieldGetPtr(srcField) aa = numpy.array(so) srcFieldPtr[:] = numpy.reshape( aa[srcKCornerBE, srcJCornerBE, srcICornerBE], srcNtotCenter) srcMaskValues = numpy.array([1], numpy.int32) # Regrid regridOut = ESMP.ESMP_FieldRegridStore( srcField, dstField, srcMaskValues=srcMaskValues, dstMaskValues=None, srcFracField=None, dstFracField=None, regridmethod=ESMP.ESMP_REGRIDMETHOD_BILINEAR, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_ERROR) ESMP.ESMP_FieldRegrid(srcField, dstField, regridOut) soInterp = numpy.reshape(dstFieldPtr, dstKJIShape) regridBck = ESMP.ESMP_FieldRegridStore( dstField, srcField, srcMaskValues=None, dstMaskValues=None, srcFracField=None, dstFracField=None, regridmethod=ESMP.ESMP_REGRIDMETHOD_BILINEAR, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE) ESMP.ESMP_FieldRegrid(dstField, srcField, regridBck) soInterpInterp = numpy.reshape(srcFieldPtr, dstKJIShape) so.toVisit('so.vsh5') soInterp.toVisit('soInterp.vsh5') soInterpInterp.toVisit('soInterpInterp.vsh5')
def getDataPointer(self): return ESMP.ESMP_FieldGetPtr(self.field) #srcCrds, srcBnds = makeGrid2D(12, 24) #dstCrds, dstBnds = makeGrid2D(12, 24) sDims, srcCrds, srcData, srcBnds = makeGrid3D(6, 12, 24) dDims, dstCrds, dstData, dstBnds = makeGrid3D(6, 12, 24) maxIndex = numpy.array(srcCrds[0].shape[::-1], numpy.int32) srcGrid1 = ESMP.ESMP_GridCreateNoPeriDim(maxIndex, coordSys=COORDSYS) dstGrid1 = ESMP.ESMP_GridCreateNoPeriDim(maxIndex, coordSys=COORDSYS) srcFeld1 = ESMP.ESMP_FieldCreateGrid(srcGrid1, 'srcFeld1', staggerloc=CENTER, typekind=R4) dstFeld1 = ESMP.ESMP_FieldCreateGrid(dstGrid1, 'dstFeld', staggerloc=CENTER, typekind=R4) srcPtr = ESMP.ESMP_FieldGetPtr(srcFeld1) if pe == rootPe: print '1. Create Grid, Field then GridAddCoord()' if pe == rootPe: print '2. GridCreate, GridAddCoord, FieldCreate' if pe == rootPe: print '3. Use a class to create the grid addCoords then the field' print '1. ', srcPtr.size, srcCrds[0].size ESMP.ESMP_GridAddCoord(srcGrid1, staggerloc=CENTER)
def test1_2d_esmf_native_tripolar_fraction(self): mype = MPI.COMM_WORLD.Get_rank() f = cdms2.open(sys.prefix + \ '/sample_data/so_Omon_ACCESS1-0_historical_r1i1p1_185001-185412_2timesteps.nc') so = f('so')[0, 0, :, :] h = cdms2.open(sys.prefix + \ '/sample_data/so_Omon_HadGEM2-CC_historical_r1i1p1_185912-186911_2timesteps.nc') hadGEM2Model = h('so')[0, 0, ...] ny, nx = so.shape soBounds = so.getGrid().getBounds() srcLatCorner = numpy.zeros((ny + 1, nx + 1), numpy.float32) srcLatCorner[:ny, :nx] = soBounds[0][:, :, 0] srcLatCorner[:ny, nx] = soBounds[0][:ny, nx - 1, 1] srcLatCorner[ny, nx] = soBounds[0][ny - 1, nx - 1, 2] srcLatCorner[ny, :nx] = soBounds[0][ny - 1, :nx, 3] srcLonCorner = numpy.zeros((ny + 1, nx + 1), numpy.float32) srcLonCorner[:ny, :nx] = soBounds[1][:, :, 0] srcLonCorner[:ny, nx] = soBounds[1][:ny, nx - 1, 1] srcLonCorner[ny, nx] = soBounds[1][ny - 1, nx - 1, 2] srcLonCorner[ny, :nx] = soBounds[1][ny - 1, :nx, 3] srcCells = [so.getLatitude(), so.getLongitude()] srcNodes = [srcLatCorner, srcLonCorner] clt = cdms2.open(sys.prefix + 'sample_data/clt.nc')('clt')[0, :, :] cltBounds = clt.getGrid().getBounds() ny, nx = clt.shape # clt grid is rectilinear, transform to curvilinear CLGrid = clt.getGrid().toCurveGrid() #lats = CLGrid.getLatitude()[:].data lons = CLGrid.getLongitude()[:].data # Make the bounds go from -90, 90 with uniform spacing and the # Cell Centers go from -88.something to 88.something yb = numpy.linspace(-90, 90, ny + 1) interval = abs(yb[0] - yb[1]) y = numpy.linspace(-90 + interval / 2., 90 - interval / 2., ny) lats = numpy.outer(y, numpy.ones((nx), numpy.float32)) ny, nx = clt.shape #yb = numpy.zeros((ny+1,), numpy.float32) #yb[:ny] = cltBounds[0][:, 0] #yb[ny] = cltBounds[0][ny-1, 1] xb = numpy.zeros((nx + 1, ), numpy.float32) xb[:nx] = cltBounds[1][:, 0] xb[nx] = cltBounds[1][nx - 1, 1] # make curvilinear dstLatCorner = numpy.outer(yb, numpy.ones((nx + 1, ), numpy.float32)) dstLonCorner = numpy.outer(numpy.ones((ny + 1, ), numpy.float32), xb) dstCells = [lats, lons] dstNodes = [dstLatCorner, dstLonCorner] print 'running test2_2d_esmf_native_tripolar_fraction...' tic = time.time() # create grid srcMaxIndex = numpy.array(so.shape[::-1], dtype=numpy.int32) srcGrid = ESMP.ESMP_GridCreate1PeriDim( srcMaxIndex, coordSys=ESMP.ESMP_COORDSYS_SPH_DEG) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) ESMP.ESMP_GridAddCoord(srcGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER) srcDimsCenter = ESMP.ESMP_GridGetCoord(srcGrid, ESMP.ESMP_STAGGERLOC_CENTER) srcDimsCorner = ESMP.ESMP_GridGetCoord(srcGrid, ESMP.ESMP_STAGGERLOC_CORNER) srcXCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) srcYCenter = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) srcXCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 0, ESMP.ESMP_STAGGERLOC_CORNER) srcYCorner = ESMP.ESMP_GridGetCoordPtr(srcGrid, 1, ESMP.ESMP_STAGGERLOC_CORNER) dstMaxIndex = numpy.array(clt.shape[::-1], dtype=numpy.int32) dstGrid = ESMP.ESMP_GridCreate1PeriDim( dstMaxIndex, coordSys=ESMP.ESMP_COORDSYS_SPH_DEG) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) ESMP.ESMP_GridAddCoord(dstGrid, staggerloc=ESMP.ESMP_STAGGERLOC_CORNER) dstDimsCenter = ESMP.ESMP_GridGetCoord(dstGrid, ESMP.ESMP_STAGGERLOC_CENTER) dstDimsCorner = ESMP.ESMP_GridGetCoord(dstGrid, ESMP.ESMP_STAGGERLOC_CORNER) dstXCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, ESMP.ESMP_STAGGERLOC_CENTER) dstYCenter = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, ESMP.ESMP_STAGGERLOC_CENTER) dstXCorner = ESMP.ESMP_GridGetCoordPtr(dstGrid, 0, ESMP.ESMP_STAGGERLOC_CORNER) dstYCorner = ESMP.ESMP_GridGetCoordPtr(dstGrid, 1, ESMP.ESMP_STAGGERLOC_CORNER) # mask ESMP.ESMP_GridAddItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) srcMask = ESMP.ESMP_GridGetItem(srcGrid, item=ESMP.ESMP_GRIDITEM_MASK) # create field srcFld = ESMP.ESMP_FieldCreateGrid( srcGrid, 'srcFld', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) srcFldPtr = ESMP.ESMP_FieldGetPtr(srcFld) dstFld = ESMP.ESMP_FieldCreateGrid( dstGrid, 'dstFld', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) dstFldPtr = ESMP.ESMP_FieldGetPtr(dstFld) # Create the field for the fractional areas srcFracFld = ESMP.ESMP_FieldCreateGrid( srcGrid, 'srcFrac', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) srcFracPtr = ESMP.ESMP_FieldGetPtr(srcFracFld) dstFracFld = ESMP.ESMP_FieldCreateGrid( dstGrid, 'dstFrac', typekind=ESMP.ESMP_TYPEKIND_R4, staggerloc=ESMP.ESMP_STAGGERLOC_CENTER) dstFracPtr = ESMP.ESMP_FieldGetPtr(dstFracFld) # set coords, mask, and field values for src and dst srcNtotCenter = reduce(operator.mul, [srcDimsCenter[1][i] - srcDimsCenter[0][i] \ for i in range(2)]) srcNtotCorner = reduce(operator.mul, [srcDimsCorner[1][i] - srcDimsCorner[0][i] \ for i in range(2)]) srcJCenterBeg = srcDimsCenter[0][1] srcJCenterEnd = srcDimsCenter[1][1] srcJCornerBeg = srcDimsCorner[0][1] srcJCornerEnd = srcDimsCorner[1][1] srcICenterBeg = srcDimsCenter[0][0] srcICenterEnd = srcDimsCenter[1][0] srcICornerBeg = srcDimsCorner[0][0] srcICornerEnd = srcDimsCorner[1][0] srcXCenter[:] = numpy.reshape( srcCells[1][srcJCenterBeg:srcJCenterEnd, srcICenterBeg:srcICenterEnd], (srcNtotCenter, )) srcYCenter[:] = numpy.reshape( srcCells[0][srcJCenterBeg:srcJCenterEnd, srcICenterBeg:srcICenterEnd], (srcNtotCenter, )) srcXCorner[:] = numpy.reshape( srcNodes[1][srcJCornerBeg:srcJCornerEnd, srcICornerBeg:srcICornerEnd], (srcNtotCorner, )) srcYCorner[:] = numpy.reshape( srcNodes[0][srcJCornerBeg:srcJCornerEnd, srcICornerBeg:srcICornerEnd], (srcNtotCorner, )) srcFldPtr[:] = numpy.reshape( so[srcJCenterBeg:srcJCenterEnd, srcICenterBeg:srcICenterEnd], (srcNtotCenter, )) srcMask[:] = (srcFldPtr == so.missing_value) srcFracPtr[:] = -999 dstFracPtr[:] = -999 dstNtotCenter = reduce( operator.mul, [dstDimsCenter[1][i] - dstDimsCenter[0][i] for i in range(2)]) dstNtotCorner = reduce( operator.mul, [dstDimsCorner[1][i] - dstDimsCorner[0][i] for i in range(2)]) dstXCenter[:] = numpy.reshape( dstCells[1][dstDimsCenter[0][1]:dstDimsCenter[1][1], dstDimsCenter[0][0]:dstDimsCenter[1][0]], (dstNtotCenter)) dstXCenter[:] = numpy.reshape( dstCells[0][dstDimsCenter[0][1]:dstDimsCenter[1][1], dstDimsCenter[0][0]:dstDimsCenter[1][0]], (dstNtotCenter)) dstXCorner[:] = numpy.reshape( dstNodes[1][dstDimsCorner[0][1]:dstDimsCorner[1][1], dstDimsCorner[0][0]:dstDimsCorner[1][0]], (dstNtotCorner, )) dstYCorner[:] = numpy.reshape( dstNodes[0][dstDimsCorner[0][1]:dstDimsCorner[1][1], dstDimsCorner[0][0]:dstDimsCorner[1][0]], (dstNtotCorner, )) dstFldPtr[:] = 0 srcAreaFld = ESMP.ESMP_FieldCreateGrid(srcGrid, 'srcArea') dstAreaFld = ESMP.ESMP_FieldCreateGrid(dstGrid, 'dstArea') # regrid forward and backward maskVals = numpy.array([1], numpy.int32) # values defining mask regrid1 = ESMP.ESMP_FieldRegridStore( srcFld, dstFld, srcMaskValues=maskVals, dstMaskValues=None, regridmethod=ESMP.ESMP_REGRIDMETHOD_CONSERVE, unmappedaction=ESMP.ESMP_UNMAPPEDACTION_IGNORE, srcFracField=srcFracFld, dstFracField=dstFracFld) ESMP.ESMP_FieldRegrid(srcFld, dstFld, regrid1) srcAreas = ESMP.ESMP_FieldRegridGetArea(srcAreaFld) dstAreas = ESMP.ESMP_FieldRegridGetArea(dstAreaFld) srcAreaPtr = ESMP.ESMP_FieldGetPtr(srcAreaFld) dstAreaPtr = ESMP.ESMP_FieldGetPtr(dstAreaFld) if mype == 0: srcHasNan = numpy.any(numpy.isnan(srcFracPtr)) dstHasNan = numpy.any(numpy.isnan(dstFracPtr)) aa = numpy.isnan(srcFracPtr) bb = numpy.isnan(dstFracPtr) cc = srcFldPtr == 0 dd = dstFldPtr == 0 if PLOT: pl.figure(1) pl.subplot(2, 1, 1) pl.pcolor(numpy.reshape(aa, so.shape)) pl.colorbar() pl.title('source') pl.subplot(2, 1, 2) pl.pcolor(numpy.reshape(bb, clt.shape)) pl.colorbar() pl.title('destination') pl.suptitle("Red == location of nan's") print srcHasNan, dstHasNan # Do they have nans? self.assertFalse(srcHasNan, True) self.assertFalse(dstHasNan, True) jbeg, jend = dstDimsCenter[0][1], dstDimsCenter[1][1] ibeg, iend = dstDimsCenter[0][0], dstDimsCenter[1][0] soInterp = numpy.reshape(dstFldPtr, (jend - jbeg, iend - ibeg)) toc = time.time() # clean up ESMP.ESMP_FieldRegridRelease(regrid1) ESMP.ESMP_FieldDestroy(dstFld) ESMP.ESMP_GridDestroy(dstGrid) ESMP.ESMP_FieldDestroy(srcFld) ESMP.ESMP_GridDestroy(srcGrid)