function b_or_p_GridToMesh(grid,staggerloc,maskValues, turnedOnMeshNodeMask, rc)
type (ESMF_Grid), intent(in) :: grid
type (ESMF_StaggerLoc), intent(in) :: staggerloc
integer(ESMF_KIND_I4), intent(in), optional :: maskValues(:)
logical, intent(out), optional :: turnedOnMeshNodeMask
integer, intent(out), optional :: rc
type (ESMF_Mesh) :: b_or_p_GridToMesh
logical :: moabOn
integer :: tileCount, gridDimCount
type (ESMF_Mesh) :: tmpMesh
integer :: localrc
if (present(rc)) rc = ESMF_RC_NOT_IMPL
! Init variables
if (present(turnedOnMeshNodeMask)) turnedOnMeshNodeMask=.false.
! check grid
call checkGrid(grid,staggerloc,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
! ESMF_REGION_ENTER("gridToMesh", localrc)
if (moabOn) then
! Convert Grid to Mesh depending on staggerloc
if (staggerloc .eq. ESMF_STAGGERLOC_CORNER) then
b_or_p_GridToMesh=ESMF_MeshCreate(grid, rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
else if (staggerloc .eq. ESMF_STAGGERLOC_CENTER) then
! Convert Grid to Mesh
tmpMesh=ESMF_MeshCreate(grid, rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Create Dual of Mesh (to move centers to corners)
b_or_p_GridToMesh=ESMF_MeshCreateDual(tmpMesh, rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Get rid of temporary Mesh
call ESMF_MeshDestroy(tmpMesh, noGarbage=.true., rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
else
call ESMF_LogSetError(rcToCheck=ESMF_RC_ARG_BAD, &
msg=" when using MOAB, bilinear or patch regridding is currently not supported on this stagger location.", &
ESMF_CONTEXT, rcToReturn=rc)
return
endif
! Turn on masking
if (present(maskValues)) then
call ESMF_MeshTurnOnNodeMask(b_or_p_GridToMesh, maskValues=maskValues, rc=localrc);
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Record that we turned on masking
if (present(turnedOnMeshNodeMask)) turnedOnMeshNodeMask=.true.
endif
else
b_or_p_GridToMesh = ESMF_GridToMesh(grid, staggerLoc, 0, .false., &
maskValues=maskValues, regridConserve=ESMF_REGRID_CONSERVE_OFF, &
rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
! Record that we turned on masking
! (The GToM call above turns on masking if the maskValues are present)
if (present(maskValues)) then
if (present(turnedOnMeshNodeMask)) turnedOnMeshNodeMask=.true.
endif
endif
! ESMF_REGION_EXIT("gridToMesh", localrc)
if(present(rc)) rc = ESMF_SUCCESS
end function b_or_p_GridToMesh