subroutine user_initP3(comp, importState, exportState, clock, rc)
type(ESMF_CplComp) :: comp
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! Local variables
type(ESMF_Field) :: srcField, dstField, finalField
type(ESMF_RouteHandle) :: rh
! Initialize return code
rc = ESMF_SUCCESS
print *, "User Coupler Init phase=3 starting"
! Re-reconcile import and export states, just in case something changed
call ESMF_StateReconcile(importState, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
call ESMF_StateReconcile(exportState, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
! Access source, final, and destination Fields
call ESMF_StateGet(importState, "srcField", srcField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
call ESMF_StateGet(importState, "finalField", finalField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
call ESMF_StateGet(exportState, "dstField", dstField, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
! Precompute the Redist operation, give it a name and keep it in importState
call ESMF_FieldRedistStore(srcField, dstField, routehandle=rh, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
call ESMF_RouteHandleSet(rh, name="Redist", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
call ESMF_StateAdd(importState, (/rh/), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
! Precompute the Regrid operation, give it a name and keep it in importState
call ESMF_FieldRegridStore(dstField, finalField, routehandle=rh, rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
call ESMF_RouteHandleSet(rh, name="Regrid", rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
call ESMF_StateAdd(importState, (/rh/), rc=rc)
if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
line=__LINE__, &
file=__FILE__)) return ! bail out
print *, "User Coupler Init phase=3 returning"
end subroutine user_initP3