x_comp_read_fields Subroutine

private subroutine x_comp_read_fields(xdatacfg, xstate, rc)

Arguments

Type IntentOptional Attributes Name
type(ESMF_HConfig) :: xdatacfg
type(xdata_state), intent(inout), pointer :: xstate
integer, intent(out) :: rc

Source Code

  subroutine x_comp_read_fields(xdatacfg, xstate, rc)
    ! arguments
    type(ESMF_HConfig)                        :: xdatacfg
    type(xdata_state), pointer, intent(inout) :: xstate
    integer, intent(out)                      :: rc
    ! local variables
    logical                    :: isPresent
    integer                    :: stat
    logical                    :: check
    type(ESMF_HConfig)         :: flistcfg
    type(ESMF_HConfig)         :: fieldcfg
    type(ESMF_HConfigIter)     :: flistcur
    type(ESMF_HConfigIter)     :: flistbeg
    type(ESMF_HConfigIter)     :: flistend
    character(:), allocatable  :: fname
    type(xdata_field), pointer :: xfield
    character(:), allocatable  :: badKey

    rc = ESMF_SUCCESS

    if (.not. associated(xstate)) then
      call ESMF_LogSetError(ESMF_RC_PTR_NOTALLOC, &
        msg='XDATA: xstate has not been associated', &
        line=__LINE__, file=__FILE__, rcToReturn=rc)
      return
    endif

    ! read import field configuration
    isPresent = ESMF_HConfigIsDefined(xdatacfg, &
      keyString="importFields", rc=rc)
    if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
      line=__LINE__, file=__FILE__)) return
    if (isPresent) then
      ! access flistcfg(import)
      flistcfg = ESMF_HConfigCreateAt(xdatacfg, &
        keyString="importFields", rc=rc)
      if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
        line=__LINE__, file=__FILE__)) return
      flistbeg = ESMF_HConfigIterBegin(flistcfg, rc=rc)
      flistend = ESMF_HConfigIterEnd(flistcfg, rc=rc)
      flistcur = flistbeg
      do while (ESMF_HConfigIterLoop(flistcur, flistbeg, flistend, rc=rc))
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        fname = ESMF_HConfigAsStringMapKey(flistcur, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        ! access fieldcfg
        fieldcfg = ESMF_HConfigCreateAt(flistcfg, keyString=fname, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        check = ESMF_HConfigValidateMapKeys(fieldcfg, &
          vocabulary=["dim", &
                      "min", &
                      "max"  &
                     ], badKey=badKey, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        if (.not. check) then
          call ESMF_LogSetError(ESMF_RC_NOT_VALID, &
            msg=trim(xstate%cname)//": ("//fname//")"// &
                " unknown importFields option key - "//badKey, &
          line=__LINE__,file=__FILE__, rcToReturn=rc)
          return
        endif
        nullify(xfield)
        allocate(xfield, stat=stat)
        if (ESMF_LogFoundAllocError(statusToCheck=stat, &
          msg=trim(xstate%cname)//': Memory allocation failed.', &
          line=__LINE__, &
          file=__FILE__, &
          rcToReturn=rc)) return
        xfield%stdn = fname
        xfield%fdim = x_comp_hconfig_i4(fieldcfg, "dim", &
          defaultValue=2, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        xfield%okay = .false.
        xfield%lsum = (/filv, 0.0_ESMF_KIND_R8/)
        xfield%lmin = filv
        xfield%lmax = filv
        xfield%gsum = (/filv, 0.0_ESMF_KIND_R8/)
        xfield%gmin = filv
        xfield%gmax = filv
        xfield%gavg = filv
        xfield%minv = x_comp_hconfig_r8(fieldcfg, "min", &
          defaultValue=0.0_ESMF_KIND_R8, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        xfield%maxv = x_comp_hconfig_r8(fieldcfg, "max", &
          defaultValue=0.0_ESMF_KIND_R8, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        xfield%dflt = filv
        xfield%nfld => null()
        if (.not. associated(xstate%imp_flds_head)) then
          xstate%imp_flds_head => xfield
          xstate%imp_flds_tail => xfield
        else
          xstate%imp_flds_tail%nfld => xfield
          xstate%imp_flds_tail => xfield
        endif
        call ESMF_HConfigDestroy(fieldcfg, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
      enddo ! fieldcfg
      call ESMF_HConfigDestroy(flistcfg, rc=rc)
      if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
        line=__LINE__, file=__FILE__)) return
    endif ! flistcfg

    ! read export field configuration
    isPresent = ESMF_HConfigIsDefined(xdatacfg, &
      keyString="exportFields", rc=rc)
    if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
      line=__LINE__, file=__FILE__)) return
    if (isPresent) then
      ! access flistcfg(export)
      flistcfg = ESMF_HConfigCreateAt(xdatacfg, &
        keyString="exportFields", rc=rc)
      if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
        line=__LINE__, file=__FILE__)) return
      flistbeg = ESMF_HConfigIterBegin(flistcfg, rc=rc)
      flistend = ESMF_HConfigIterEnd(flistcfg, rc=rc)
      flistcur = flistbeg
      do while (ESMF_HConfigIterLoop(flistcur, flistbeg, flistend, rc=rc))
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        fname = ESMF_HConfigAsStringMapKey(flistcur, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        ! access fieldcfg
        fieldcfg = ESMF_HConfigCreateAt(flistcfg, keyString=fname, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        check = ESMF_HConfigValidateMapKeys(fieldcfg, &
          vocabulary=["dim", &
                      "val"  &
                     ], badKey=badKey, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        if (.not. check) then
          call ESMF_LogSetError(ESMF_RC_NOT_VALID, &
            msg=trim(xstate%cname)//": ("//fname//")"// &
                " unknown exportFields option key - "//badKey, &
          line=__LINE__,file=__FILE__, rcToReturn=rc)
          return
        endif
        nullify(xfield)
        allocate(xfield, stat=stat)
        if (ESMF_LogFoundAllocError(statusToCheck=stat, &
          msg=trim(xstate%cname)//': Memory allocation failed.', &
          line=__LINE__, &
          file=__FILE__, &
          rcToReturn=rc)) return
        xfield%stdn = fname
        xfield%fdim = x_comp_hconfig_i4(fieldcfg, "dim", &
          defaultValue=2, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        xfield%okay = .false.
        xfield%lsum = (/filv, 0.0_ESMF_KIND_R8/)
        xfield%lmin = filv
        xfield%lmax = filv
        xfield%gsum = (/filv, 0.0_ESMF_KIND_R8/)
        xfield%gmin = filv
        xfield%gmax = filv
        xfield%gavg = filv
        xfield%minv = 0.0_ESMF_KIND_R8
        xfield%maxv = 0.0_ESMF_KIND_R8
        xfield%dflt = x_comp_hconfig_r8(fieldcfg, "val", &
          defaultValue=0.0_ESMF_KIND_R8, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
        xfield%nfld => null()
        if (.not. associated(xstate%exp_flds_head)) then
          xstate%exp_flds_head => xfield
          xstate%exp_flds_tail => xfield
        else
          xstate%exp_flds_tail%nfld => xfield
          xstate%exp_flds_tail => xfield
        endif
        call ESMF_HConfigDestroy(fieldcfg, rc=rc)
        if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
          line=__LINE__, file=__FILE__)) return
      enddo ! fieldcfg
      call ESMF_HConfigDestroy(flistcfg, rc=rc)
      if (ESMF_LogFoundError(rcToCheck=rc, msg=ESMF_LOGERR_PASSTHRU, &
        line=__LINE__, file=__FILE__)) return
    endif ! flistcfg

  endsubroutine x_comp_read_fields