function conserve_GridToMesh(grid, maskValues, turnedOnMeshElemMask, rc)
type (ESMF_Grid), intent(in) :: grid
integer(ESMF_KIND_I4), intent(in), optional :: maskValues(:)
logical, intent(out), optional :: turnedOnMeshElemMask
integer, intent(out), optional :: rc
type (ESMF_Mesh) :: conserve_GridToMesh
logical :: moabOn
integer :: tileCount, gridDimCount
integer :: localrc
type (ESMF_StaggerLoc) :: staggerlocG2M
if (present(rc)) rc = ESMF_RC_NOT_IMPL
! Init variables
if (present(turnedOnMeshElemMask)) turnedOnMeshElemMask=.false.
! Create the mesh from corner stagger to better represent the
! control volumes
call ESMF_GridGet(grid=grid, &
dimCount=gridDimCount, tileCount=tileCount, rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Figure out staggerloc based on dimension
if (gridDimCount .eq. 2) then
staggerlocG2M=ESMF_STAGGERLOC_CORNER
else if (gridDimCount .eq. 3) then
staggerlocG2M=ESMF_STAGGERLOC_CORNER_VFACE
else
call ESMF_LogSetError(rcToCheck=ESMF_RC_ARG_BAD, &
msg="- can currently only do conservative regridding on 2D or 3D grids", &
ESMF_CONTEXT, rcToReturn=rc)
return
endif
! check grid
call checkGrid(grid,staggerlocG2M,rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Check if Moab is on, if so do things a bit differently below
call ESMF_MeshGetMOAB(moabOn, rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Convert Grid to Mesh
if (moabOn) then
! Convert Grid to Mesh
conserve_GridToMesh=ESMF_MeshCreate(grid, rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Turn on masking
if (present(maskValues)) then
call ESMF_MeshTurnOnCellMask(conserve_GridToMesh, maskValues=maskValues, rc=localrc);
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Record that we turned on the elem masking
if (present(turnedOnMeshElemMask)) turnedOnMeshElemMask=.true.
endif
else
if (tileCount .eq. 1) then
conserve_GridToMesh = ESMF_GridToMesh(grid, staggerLocG2M, 0, .false., &
maskValues=maskValues, regridConserve=ESMF_REGRID_CONSERVE_ON, rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Record that we turned on the elem masking
! (If maskValues are present, then the GToM call above turns on masking.)
if (present(maskValues)) then
if (present(turnedOnMeshElemMask)) turnedOnMeshElemMask=.true.
endif
else
conserve_GridToMesh = ESMF_GridToMeshCell(grid, rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Turn on masking
if (present(maskValues)) then
call ESMF_MeshTurnOnCellMask(conserve_GridToMesh, maskValues=maskValues, rc=localrc);
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Record that we turned on the elem masking
if (present(turnedOnMeshElemMask)) turnedOnMeshElemMask=.true.
endif
endif
endif
if(present(rc)) rc = ESMF_SUCCESS
end function conserve_GridToMesh