ESMF_UtilString2Real Function

public function ESMF_UtilString2Real(string, keywordEnforcer, rc)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: string
type(ESMF_KeywordEnforcer), optional :: keywordEnforcer
integer, intent(out), optional :: rc

Return Value real


Source Code

  function ESMF_UtilString2Real(string, keywordEnforcer, rc)
! !RETURN VALUE:
    real :: ESMF_UtilString2Real
! !ARGUMENTS:
    character(len=*), intent(in)            :: string
type(ESMF_KeywordEnforcer), optional:: keywordEnforcer ! must use keywords below
    integer,          intent(out), optional :: rc
! !DESCRIPTION:
!   Return the numerical real value represented by the {\tt string}.
!
!   Leading and trailing blanks in {\tt string} are ignored when directly
!   converting into integers.
!
!   This procedure may fail when used in an expression in a {\tt write} statement
!   with some older, pre-Fortran 2003, compiler environments that do not support
!   re-entrant I/O calls.
!
!   The arguments are:
!   \begin{description}
!   \item[string]
!     The string to be converted
!   \item[{[rc]}]
!     Return code; equals {\tt ESMF\_SUCCESS} if there are no errors.
!   \end{description}
!
!EOPI
  !-----------------------------------------------------------------------------
    ! local variables
    integer                   :: ioerr
    character(:), allocatable :: tempString

    if (present(rc)) rc = ESMF_SUCCESS

    ESMF_UtilString2Real = 0 ! initialize

    tempString = trim(adjustl(string))! remove leading and trailing white spaces

    if (verify(tempString,".-+0123456789e") == 0) then
      ! should convert to real just fine
      read (tempString, *, iostat=ioerr) ESMF_UtilString2Real
      if (ioerr /= 0) then
        call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
            msg="The string '"//tempString//"' could not be converted to real.", &
            line=__LINE__, &
            file=ESMF_FILENAME, &
            rcToReturn=rc)
        return ! bail out
      end if
    else
      ! the string contains characters besides numbers
      call ESMF_LogSetError(ESMF_RC_ARG_BAD, &
        msg="The string '"//tempString//"' contains characters besides "// &
          "numbers, cannot convert to real.", &
        line=__LINE__, &
        file=ESMF_FILENAME, &
        rcToReturn=rc)
      return ! bail out
    endif

  end function ESMF_UtilString2Real