subroutine user_initP2(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
type(ESMF_Grid) :: srcGrid, dstGrid
type(ESMF_DistGrid) :: dstDistGrid
! Initialize return code
rc = ESMF_SUCCESS
print *, "User Coupler Init phase=2 starting"
call ESMF_LogWrite (msg='User Coupler Init phase=2 starting', &
logmsgFlag=ESMF_LOGMSG_TRACE)
! Need to re-reconcile import and export states
call ESMF_StateReconcile(importState, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_StateReconcile(exportState, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Access source Field and Grid
call ESMF_StateGet(importState, "srcField", srcField, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_FieldGet(srcField, grid=srcGrid, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Access destination Field, Grid, and DistGrid
call ESMF_StateGet(exportState, "dstField", dstField, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_FieldGet(dstField, grid=dstGrid, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_GridGet(dstGrid, distgrid=dstDistGrid, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Create a new dstGrid from srcGrid, considering dstDistGrid
! Now that dstDistGrid is reconciled, it can be used without the need
! for passing in the dstVM argument. The resulting dstGrid will function
! correctly in the CplComp context, as well as the Comp2 context.
dstGrid = ESMF_GridCreate(srcGrid, dstDistGrid,rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Set the new dstGrid in the dstField to make it available to Comp2
call ESMF_FieldEmptySet(dstField, grid=dstGrid, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_LogWrite (msg='User Coupler Init phase=2 complete', &
logmsgFlag=ESMF_LOGMSG_TRACE)
print *, "User Coupler Init phase=2 returning"
end subroutine user_initP2