GridSpecReadCoords Subroutine

public subroutine GridSpecReadCoords(filename, coordsname, lonarray, latarray, rc)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: coordsname
real(kind=ESMF_KIND_R8), TARGET :: lonarray(:,:)
real(kind=ESMF_KIND_R8), TARGET :: latarray(:,:)
integer :: rc

Source Code

  subroutine GridSpecReadCoords(filename, coordsname, lonarray, latarray, rc)

  character(len=*), intent(in) :: filename
  character(len=*), intent(in) :: coordsname
  real(ESMF_KIND_R8), TARGET  :: lonarray(:,:)
  real(ESMF_KIND_R8), TARGET  :: latarray(:,:)
  integer                      :: rc

    character(len=128) :: errmsg
    integer, parameter :: nf90_noerror = 0
    integer :: ncStatus
    integer ::  gridid, varid, tempids(1), varids(2), meshid, len
    character(len=128) :: attvalue, locallocstr, varnames(2)
    integer :: ndims, dimids(2), dims(2)
    integer :: i, j, nvars, pos
    real(ESMF_KIND_R8), allocatable :: buffer(:)
    real(ESMF_KIND_R8), parameter :: d2r = 3.141592653589793238/180

#ifdef ESMF_NETCDF
    ncStatus = nf90_open (path=filename, mode=nf90_nowrite, ncid=gridid)
    errmsg = 'Fail to open '//trim(filename)
    if (CDFCheckError (ncStatus, &
            ESMF_METHOD, &
            ESMF_SRCLINE,&
            errmsg, &
            rc)) return
    pos = INDEX(coordsname, ' ')
    varnames(1)=coordsname(1:pos-1)
    varnames(2)=coordsname(pos+1:)
    do i=1,2
       ncStatus = nf90_inq_varid(gridid, varnames(i), varid)
       errmsg = 'Coordinate variable '//trim(varnames(i))//' does not exist'
       if (CDFCheckError (ncStatus, &
            ESMF_METHOD, &
            ESMF_SRCLINE,&
            errmsg, &
            rc)) return
       ncStatus = nf90_inquire_variable(gridid, varid, ndims=ndims, dimids=dimids)
       errmsg = 'nf90_inquire_variable failed '//trim(varnames(i))
       if (CDFCheckError (ncStatus, &
               ESMF_METHOD, &
               ESMF_SRCLINE,&
               errmsg, &
               rc)) return
       do j=1, ndims
           ncStatus = nf90_inquire_dimension(gridid, dimids(j), len=dims(j))
           errmsg = 'nf90_inquire_dimension failed '//trim(filename)
           if (CDFCheckError (ncStatus, &
               ESMF_METHOD, &
               ESMF_SRCLINE,&
               errmsg, &
               rc)) return
       enddo
       if (ndims == 1) then 
           allocate(buffer(dims(1)))
           ncStatus = nf90_get_var(gridid, varid, buffer)
           errmsg = 'Read variable failed: '//trim(varnames(i))
           if (CDFCheckError (ncStatus, &
               ESMF_METHOD, &
               ESMF_SRCLINE,&
               errmsg, &
               rc)) return
           if (i==1) then !longitude
             do j=1,size(lonarray, 2)
               lonarray(:,j)=buffer
             enddo
           else
             do j=1,size(latarray, 1)
               latarray(j,:)=buffer
             enddo
           endif
           deallocate(buffer)
       else
           if (i==1) then
               ncStatus = nf90_get_var(gridid, varid, lonarray)
           else
               ncStatus = nf90_get_var(gridid, varid, latarray)
           endif
           errmsg = 'Read variable failed: '//trim(varnames(i))
           if (CDFCheckError (ncStatus, &
               ESMF_METHOD, &
               ESMF_SRCLINE,&
               errmsg, &
               rc)) return
       endif
    enddo  
    latarray = latarray*d2r
    lonarray = lonarray*d2r
    rc = ESMF_SUCCESS
    return
#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 GridSpecReadCoords