C     @(#)rech.f	1.1     02/19/98
C
      real*4 function rech (string, ier, ip)

c Interpret a character variable as a real number

c Assumes a character variable in string is to be converted
c       to a real number
c Leading blanks are treated as zeros.
c Embedded blanks are not allowed.
c A blank character other than a leading blank terminates the string.
c Plus or minus signs, when used, must prefix the digits with
c       no embedded blanks.

c IER returns   0 if no digits were found
c              +n number terminated at byte n, by space or end of string
c              -n error found at byte n

c IP returns -1 if no decimal point found
c               else number of places after decimal point

      character*(*) string
      real*8 dech
      logical digits

      rech = 0.
      itch = 0
      ier = 0
      ip = -1
      last = len(string)
      sign = +1.
      nat = 0
      digits = .false.
1     if (nat .eq. last) return
      nat = nat + 1
      if (string(nat:nat) .eq. ' ') go to 1
      if (string(nat:nat) .eq. '+') go to 10
      if (string(nat:nat) .ne. '-') go to 3
      sign = -1.
10    if (nat .eq. last) go to 20
      nat = nat + 1
      go to 4

3     sign = 0.
4     if (string(nat:nat) .eq. '.') go to 12
      if (string(nat:nat) .eq. ' ') go to 20
      if (string(nat:nat) .lt. '0'
     2       .or. string(nat:nat) .gt. '9') go to 77
      digits = .true.
      itch = itch*10 + ichar(string(nat:nat)) - ichar('0')
      if (ip .ne. -1) ip = ip + 1
      go to 10

12    if (ip .ne. -1) go to 77
      ip = 0
      go to 10

77    ier = -nat
      go to 25

20    ier = nat
25    if (.not.digits) ier = 0
      dech = dfloat(itch)
      if (ip .ge. 1) dech = dech*10.d0**(-ip)
      if (sign .eq. 0.) sign = 1.
      rech = sign * dech
      end
