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 :: i, itemcount
integer :: petCount, xprocs, yprocs
character(len=ESMF_MAXSTR) :: name,stateItemNames(4)
type(ESMF_Array) :: srcArray, dstArray
type(ESMF_State) :: state
type(ESMF_VM) :: vm
type(ESMF_ArraySpec) :: arrayspec
type(ESMF_DistGrid) :: distgrid
! Initialize return code
rc = ESMF_SUCCESS
! get vm to be used by stateReconcile
call ESMF_CplCompGet(comp, vm=vm, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_VMGet(vm, petCount = petCount, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_StateGet(importState, name=name, itemNameList=stateItemNames, itemcount=itemcount, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! each item in the import state contains one export state from the ensemble, let's get them one by one
! and call stateReconcile to reconcile them if they are concurrent components
! Also call ArrayRedistStore to regrid them to the destination array and add them to the export state
do i=1,itemcount
call ESMF_StateGet(importState, stateItemNames(i), state, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_StateReconcile(state, vm=vm, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Get source Array out of import state
call ESMF_StateGet(state, "array data", srcArray, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Create the destination Array
call ESMF_ArraySpecSet(arrayspec, typekind=ESMF_TYPEKIND_R8, rank=2, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! create a destArray of equal size to do the redist
xprocs = 2
yprocs = petCount/2
distgrid = ESMF_DistGridCreate(minIndex=(/1,1/), maxIndex=(/100,150/), &
regDecomp=(/xprocs,yprocs/), rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
dstArray = ESMF_ArrayCreate(arrayspec=arrayspec, distgrid=distgrid, &
indexflag=ESMF_INDEX_GLOBAL, rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
call ESMF_ArraySet(dstArray, name=stateItemNames(i), rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Precompute and store an ArrayRedist routehandle for each import array
call ESMF_ArrayRedistStore(srcArray=srcArray, dstArray=dstArray, &
routehandle=rhandle(i), rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
! Add the dstArray into the exportState using the component export state name
call ESMF_StateAdd(exportState, (/dstArray/), rc=rc)
if (rc/=ESMF_SUCCESS) return ! bail out
end do
end subroutine user_init