subroutine CheckVarMask(filename, varname, haveMask, missingval, rc)
! ARGUMENTS:
character(len=*), intent(in) :: filename
character(len=*), intent(in) :: varname
logical, intent(out) :: haveMask
real(ESMF_KIND_R8) :: missingval
integer, optional :: rc
integer :: ncStatus
integer :: gridid, varid
character(len=128) :: errmsg
integer, parameter :: nf90_noerror = 0
#ifdef ESMF_NETCDF
rc = ESMF_FAILURE
! check if varname exist for GRIDSPEC and UGRID
! varname could be a list of variables separated by comma, need to check all of
! them
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
ncStatus = nf90_inq_varid(gridid, varname, varid)
errmsg = 'Variable does not exist '//trim(varname)
if (CDFCheckError (ncStatus, &
ESMF_METHOD, &
ESMF_SRCLINE,&
errmsg, &
rc)) return
! Get missing value attribute
ncStatus = nf90_get_att(gridid, varid, "_FillValue", missingval)
if (ncStatus /= nf90_noerror) then
ncStatus = nf90_get_att(gridid, varid, "missing_value", missingval)
if (ncStatus == nf90_noerror) then
haveMask = .TRUE.
else
haveMask = .FALSE.
endif
else
haveMask = .TRUE.
endif
if (.not. haveMask) missingval = 0.0
ncStatus = nf90_close(gridid)
if (CDFCheckError (ncStatus, &
ESMF_METHOD, &
ESMF_SRCLINE,&
errmsg, &
rc)) return
rc = ESMF_SUCCESS
return
#else
call ESMF_LogSetError(ESMF_RC_LIB_NOT_PRESENT, &
msg="- ESMF_NETCDF not defined when lib was compiled", &
ESMF_CONTEXT, rcToReturn=rc)
return
#endif
end subroutine CheckVarMask