NCFileInquire Subroutine

public subroutine NCFileInquire(weightFile, title, normType, src_dim, dst_dim, regridmethod, rc)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: weightFile
character(len=*), intent(out) :: title
type(ESMF_NormType_Flag), intent(out) :: normType
integer, intent(out) :: src_dim
integer, intent(out) :: dst_dim
type(ESMF_RegridMethod_Flag), intent(out) :: regridmethod
integer, intent(out), optional :: rc

Source Code

  subroutine NCFileInquire (weightFile, title, normType, src_dim, dst_dim, regridmethod, rc)

    character(len=*), intent(in)   :: weightFile
    character(len=*), intent(out)  :: title
    integer, intent(out)           :: src_dim
    integer, intent(out)           :: dst_dim
    type(ESMF_NormType_Flag), intent(out) :: normType
    type(ESMF_RegridMethod_Flag), intent(out) :: regridmethod
    integer, intent(out), optional :: rc

    integer :: ncstat,  nc_file_id,  nc_srcdim_id, nc_dstdim_id
    integer :: titleLen, normLen, methodLen

    character(ESMF_MAXPATHLEN) :: msg, normStr, methodStr

#ifdef ESMF_NETCDF
    !-----------------------------------------------------------------
    ! open netcdf file
    !-----------------------------------------------------------------

    ncstat = nf90_open(weightFile, NF90_NOWRITE, nc_file_id)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_open: '//weightFile,  &
        ESMF_CONTEXT, rcToReturn=rc)) return

    !-----------------------------------------------------------------
    ! Title
    !-----------------------------------------------------------------
    ncstat = nf90_inquire_attribute(nc_file_id, nf90_global, 'title', len=titleLen)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_inquire_attribute',  &
        ESMF_CONTEXT, rcToReturn=rc)) return
    if(len(title) < titleLen) then
      print *, "Not enough space to put title."
      return
    end if

    ncstat = nf90_get_att(nc_file_id, nf90_global, "title", title)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_get_att',  &
        ESMF_CONTEXT, rcToReturn=rc)) return

    !-----------------------------------------------------------------
    ! Regridmethod
    !-----------------------------------------------------------------
    ncstat = nf90_inquire_attribute(nc_file_id, nf90_global, 'map_method', &
             len=methodLen)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_inquire_attribute',  &
        ESMF_CONTEXT, rcToReturn=rc)) return
    if(len(methodStr) < methodLen) then
      print *, "Not enough space to put regridmethod string."
      return
    end if

    ncstat = nf90_get_att(nc_file_id, nf90_global, "map_method", methodStr)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_get_att',  &
        ESMF_CONTEXT, rcToReturn=rc)) return

    regridmethod = ESMF_REGRIDMETHOD_BILINEAR
    if (trim(methodStr) .eq. "Conservative remapping")  then
       regridmethod = ESMF_REGRIDMETHOD_CONSERVE
    endif

    !-----------------------------------------------------------------
    ! Normalization
    !-----------------------------------------------------------------
    ncstat = nf90_inquire_attribute(nc_file_id, nf90_global, 'normalization', &
             len=normLen)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_inquire_attribute',  &
        ESMF_CONTEXT, rcToReturn=rc)) return
    if(len(normStr) < normLen) then
      print *, "Not enough space to put normalization string."
      return
    end if

    ncstat = nf90_get_att(nc_file_id, nf90_global, "normalization", normStr)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_get_att error',  &
        ESMF_CONTEXT, rcToReturn=rc)) return

    ! translate normalization into normtype
    normType=ESMF_NORMTYPE_DSTAREA
    if (trim(normStr) .eq. "fracarea") then
       normType=ESMF_NORMTYPE_FRACAREA
    endif

    !-----------------------------------------------------------------
    ! source grid dimensions
    !-----------------------------------------------------------------

    ncstat = nf90_inq_dimid(nc_file_id, 'n_a', nc_srcdim_id)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_inq_dimid',  &
        ESMF_CONTEXT, rcToReturn=rc)) return

    ncstat = nf90_inquire_dimension(nc_file_id, nc_srcdim_id, len=src_dim)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_inquire_variable',  &
        ESMF_CONTEXT, rcToReturn=rc)) return

    !-----------------------------------------------------------------
    ! destination grid dimensions
    !-----------------------------------------------------------------

    ncstat = nf90_inq_dimid(nc_file_id, 'n_b', nc_dstdim_id)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_inq_dimid',  &
        ESMF_CONTEXT, rcToReturn=rc)) return

    ncstat = nf90_inquire_dimension(nc_file_id, nc_dstdim_id, len=dst_dim)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_inquire_variable',  &
        ESMF_CONTEXT, rcToReturn=rc)) return

    !------------------------------------------------------------------------
    !     close input file
    !------------------------------------------------------------------------

    ncstat = nf90_close(nc_file_id)
    if (ESMF_LogFoundNetCDFError (ncstat, msg='nf90_close',  &
        ESMF_CONTEXT, rcToReturn=rc)) return

    if(present(rc)) rc = ESMF_SUCCESS

#else
    call ESMF_LogSetError(rcToCheck=ESMF_RC_LIB_NOT_PRESENT, &
      msg="- ESMF_NETCDF not defined when lib was compiled", &
      ESMF_CONTEXT, rcToReturn=rc)
    return
#endif

  end subroutine NCFileInquire