subroutine user_init(comp, importState, exportState, clock, rc)
type(ESMF_GridComp) :: comp
type(ESMF_State) :: importState, exportState
type(ESMF_Clock) :: clock
integer, intent(out) :: rc
! ! Local variables
type(ESMF_Field) :: humidity
type(ESMF_VM) :: vm
type(ESMF_grid) :: grid1
type(ESMF_ArraySpec) :: arrayspec
real(ESMF_KIND_R8), dimension(:,:), pointer :: idata
real(ESMF_KIND_R8), dimension(:,:), pointer :: coordX, coordY
real(ESMF_KIND_R8) :: min(2), max(2), dx, dy
integer :: counts(2)
integer :: npets, de_id, i, j, tlb(2), tub(2)
rc = ESMF_SUCCESS
! Initially import state contains a field with a grid but no data.
! Query component for VM and create a layout with the right breakdown
call ESMF_GridCompGet(comp, vm=vm, rc=rc)
if(rc/=ESMF_SUCCESS) return
call ESMF_VMGet(vm, localPet=de_id, petCount=npets, rc=rc)
if(rc/=ESMF_SUCCESS) return
print *, de_id, "User Comp 2 Init starting"
! Add a "humidity" field to the import state.
min(1) = 0.0
min(2) = 0.0
counts(1) = 200
counts(2) = 100
min(1) = 0.0
max(1) = 60.0
min(2) = 0.0
max(2) = 50.0
dx = (max(1)-min(1))/(counts(1)-1)
dy = (max(2)-min(2))/(counts(2)-1)
grid1 = ESMF_GridCreateNoPeriDim(minIndex=(/1,1/), maxIndex=counts, &
gridEdgeLWidth=(/0,0/), gridEdgeUWidth=(/0,0/), &
indexflag=ESMF_INDEX_GLOBAL, &
regDecomp=(/ npets/2, 2/), name="dest grid", rc=rc)
if(rc/=ESMF_SUCCESS) return
call ESMF_GridAddCoord(grid1, rc=rc)
if(rc/=ESMF_SUCCESS) return
call ESMF_GridGetCoord(grid1, localDE=0, coordDim=1, &
farrayPtr=coordX, computationalLBound=tlb, computationalUBound=tub, rc=rc)
if(rc/=ESMF_SUCCESS) return
call ESMF_GridGetCoord(grid1, localDE=0, coordDim=2, &
farrayPtr=coordY, rc=rc)
if(rc/=ESMF_SUCCESS) return
do j = tlb(2), tub(2)
do i = tlb(1), tub(1)
coordX(i,j) = (i-1)*dx
coordY(i,j) = (j-1)*dy
enddo
enddo
! Set up a 2D real array
call ESMF_ArraySpecSet(arrayspec, rank=2, &
typekind=ESMF_TYPEKIND_R8, rc=rc)
if(rc/=ESMF_SUCCESS) return
! Create the field and have it create the array internally
humidity = ESMF_FieldCreate(grid1, arrayspec, &
totalLWidth=(/0,0/), totalUWidth=(/0,0/), &
name="humidity", rc=rc)
if(rc/=ESMF_SUCCESS) return
! Get the allocated array back and get an F90 array pointer
call ESMF_FieldGet(humidity, farrayPtr=idata, rc=rc)
if(rc/=ESMF_SUCCESS) return
call ESMF_StateAdd(importState, (/humidity/), rc=rc)
if(rc/=ESMF_SUCCESS) return
! call ESMF_StatePrint(importState, rc=rc)
print *, de_id, "User Comp 2 Init returning"
end subroutine user_init