function ESMF_GridMatch(grid1, grid2, keywordEnforcer, globalflag, rc)
!
! !RETURN VALUE:
type(ESMF_GridMatch_Flag) :: ESMF_GridMatch
! !ARGUMENTS:
type(ESMF_Grid), intent(in) :: grid1
type(ESMF_Grid), intent(in) :: grid2
type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
logical, intent(in), optional :: globalflag
integer, intent(out), optional :: rc
!
!
! !DESCRIPTION:
! Check if {\tt grid1} and {\tt grid2} match. Returns a range of values of type
! ESMF\_GridMatch indicating how closely the Grids match. For a description of
! the possible return values, please see~\ref{const:gridmatch}.
! Please also note that by default this call is not collective and only
! returns the match for the piece of the Grids on the local PET. In this case,
! it is possible for this call to return a different match on different PETs
! for the same Grids. To do a global match operation set the {\tt globalflag}
! argument to .true.. In this case, the call becomes collective across the
! current VM, ensuring the same result is returned on all PETs.
!
! The arguments are:
! \begin{description}
! \item[grid1]
! {\tt ESMF\_Grid} object.
! \item[grid2]
! {\tt ESMF\_Grid} object.
! \item[{[globalflag]}]
! By default this flag is set to false. When it's set to true, the
! function performs the match check globally. In this case,
! the method becomes collective across the current VM.
! \item[{[rc]}]
! Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
! \end{description}
!
!EOP
!------------------------------------------------------------------------------
integer :: localrc ! local return code
integer :: matchResult
integer(ESMF_KIND_I4) :: localResult(1), globalResult(1)
logical :: l_global
integer :: npet
type(ESMF_VM) :: vm
! initialize return code; assume routine not implemented
localrc = ESMF_RC_NOT_IMPL
if (present(rc)) rc = ESMF_RC_NOT_IMPL
! init to one setting in case of error
ESMF_GridMatch = ESMF_GRIDMATCH_INVALID
! Check init status of arguments
ESMF_INIT_CHECK_DEEP(ESMF_GridGetInit, grid1, rc)
ESMF_INIT_CHECK_DEEP(ESMF_GridGetInit, grid2, rc)
! Call into the C++ interface, which will sort out optional arguments.
call c_ESMC_GridMatch(grid1, grid2, matchResult, localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
if (matchResult == 1) then
ESMF_GridMatch = ESMF_GRIDMATCH_EXACT
else
ESMF_GridMatch = ESMF_GRIDMATCH_NONE
endif
l_global = .false.
if(present(globalflag)) l_global = globalflag
if(l_global) then
call ESMF_VMGetCurrent(vm, rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
call ESMF_VMGet(vm, petCount=npet, rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
localResult(1) = matchResult
globalResult(1) = 0
call ESMF_VMAllReduce(vm, localResult, globalResult, &
1, ESMF_REDUCE_SUM, rc=localrc)
if (ESMF_LogFoundError(localrc, ESMF_ERR_PASSTHRU, &
ESMF_CONTEXT, rcToReturn=rc)) return
if(globalResult(1) == npet) then
ESMF_GridMatch = ESMF_GRIDMATCH_EXACT
else
ESMF_GridMatch = ESMF_GRIDMATCH_NONE
endif
endif
! return successfully
if (present(rc)) rc = ESMF_SUCCESS
end function ESMF_GridMatch