subroutine user_init(comp, importState, exportState, clock, rc)
type(ESMF_CplComp) :: comp
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! Local variables
integer :: itemcount
type(ESMF_Field) :: F_lnd, F_atm
type(ESMF_VM) :: vm
type(ESMF_Grid) :: lnd_grid, atm_grid
type(ESMF_XGrid) :: xgrid
type(ESMF_Field) :: flux
type(ESMF_RouteHandle) :: rh1, rh2, rh3
real(ESMF_KIND_R8), pointer :: xfptr(:)
integer :: localPet
integer :: cnt
! Initialize return code
rc = ESMF_SUCCESS
!call ESMF_StateGet(importState, itemcount=itemcount, rc=rc)
!if (rc/=ESMF_SUCCESS) return ! bail out
!print *, "Import State contains ", itemcount, " items."
call ESMF_CplCompGet(comp, vm=vm, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_VMGet (vm, localPet=localPet)
print *, "User Coupler Init starting, localPet =", localPet
! Need to reconcile import and export states
call ESMF_StateReconcile(importState, vm, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_StateReconcile(exportState, vm, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_StatePrint(importState, nestedFlag=.true., rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Get destination Field out of export state
call ESMF_StateGet(exportState, "F_atm", F_atm, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Get source Fields out of import state
call ESMF_StateGet(importState, itemName="F_lnd", field=F_lnd, &
rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Create an XGrid
! - grab grids
call ESMF_FieldGet(F_lnd, grid=lnd_grid, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_FieldGet(F_atm, grid=atm_grid, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Finally ready to do an flux exchange from A side to B side
xgrid = ESMF_XGridCreate(sideAGrid=(/lnd_grid/), sideBGrid=(/atm_grid/), &
sideAMaskValues=(/2,3,4/), &
rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
flux = ESMF_FieldCreate(xgrid, typekind=ESMF_TYPEKIND_R8, name='xgrid_flux', rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_FieldGet(flux, farrayPtr=xfptr, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
xfptr = 0.0
! Precompute and store an FieldRegrid operation
call ESMF_FieldRegridStore(xgrid, F_lnd, flux, &
routehandle=rh1, rc=rc)
call ESMF_RoutehandleSet(rh1, name='lnd2xgrid', rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_FieldRegridStore(xgrid, flux, F_atm, &
routehandle=rh3, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_RoutehandleSet(rh3, name='xgrid2atm', rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_StateAdd(exportState, (/rh1,rh3/), rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_StateAdd(exportState, (/flux/), rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
print *, "User Coupler Init returning"
end subroutine user_init