C LAST UPDATE: 
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
C
C***********************
C**     LDM_INTFP     **
C***********************
C
CD-Routine: Decode Character String into a Number - LDM_INTFP
C This routine will decode a character string into an integer or floating
C point number. Fixed point or scientific notation numbers are both valid.
CD-end
CD-Fortran:
       FUNCTION LDM_INTFP (CHSTR, FP, IV, ND) 
CD-end
C Purpose:    Decode a character string into a number (floating and integer)
C
C Author:     John W. Campbell
CD-Doc:
C      Return:     = 0  Syntax error in the number
C                  = 1  integer  (no decimal point present)
C                  = 2  floating point no.
C                  =-1  blank string
C
CD-end
C                                             
C Arguments:
C
CD-Parameters:
C CHSTR   c (R)     Character string containing the number to be decoded
C                   (Leading spaces ignored; further space terminates
C                   the number string interpreted)
C FP      r (W)     Returns the value as a floating point value (0.0 if
C                   syntax error)
C IV      i (W)     Returns the value as an integer (the nearest integer
C                   if value contained a decimal point) (0 if syntax error)
C ND      i (W)     No. of digits after the decimal point. (-ve if E format
C                   number)
CD-end
C Declarations
C ============
C
C LOCALS:
C
      CHARACTER*(*) CHSTR
      CHARACTER*15 DIG
      LOGICAL END, START
      DOUBLE PRECISION FPVAL, X, S
      DATA DIG /'eE0123456789.+-'/
C
C-------------------------------------------------------------------------------
C
C
C====== Initialisations
C
      FPVAL = 0.0
      FP = 0.0
      IV = 0
      ND = 0
      END = .FALSE.
      START = .FALSE.
      IEXP = 0
      M1 = 3
      M2 = 15
      I = 0
      IMAX = LEN (CHSTR) 
      IF (CHSTR.EQ.' ') THEN
         LDM_INTFP = -1
         RETURN
      ENDIF
C
C====== Interpret Number
C
100   S = 1.0
      ID = 0
      IFDEC = 0
      X = 0.0
      KV = 0
120   I = I + 1
      IF (I.GT.IMAX) THEN
         END = .TRUE.
         IF (IEXP.EQ.1) GO TO 600
         GO TO 500
      ENDIF
      IF (CHSTR(I:I).EQ.' ') THEN
         IF (START) I = IMAX
         GO TO 120
      ENDIF
      J = INDEX(DIG(M1:M2),CHSTR(I:I))
      IF (J.EQ.0) GO TO 800
      START = .TRUE.
      J = J + M1 - 1
      M1 = 1 + 2*IEXP
C
C====== Digit
C
      IF (J.GE.3.AND.J.LE.12) THEN
         ID = ID + IFDEC
         X = 10.0 * X + J - 3
         IF (IFDEC.EQ.0) KV = 10*KV + J - 3
         IF (M2.GT.13) M2 = 13
         GO TO 120
C
C====== Decimal point
C
      ELSE IF (J.EQ.13) THEN
         IF (IEXP.EQ.1) GO TO 800
         IFDEC = 1
         M2 = 12
         GO TO 120
C
C====== Sign
C
      ELSE IF (J.GE.14) THEN
         IF (J.EQ.15) S = -1.0
         M2 = 13
         GO TO 120
C
C====== Exponent
C
      ELSE
         IEXP = 1
         M1 = 3
         M2 = 15
      ENDIF
C
C====== Evaluate number
C
500   FPVAL = S * X / (10**ID)
      IF (END) THEN
         FP = FPVAL
         IF (IFDEC.EQ.0) THEN
            IV = NINT(S)*KV
         ELSE
            IV = NINT (FP)
         END IF
         LDM_INTFP = 1
         IF (IFDEC.GT.0) THEN
            LDM_INTFP = 2
            ND = ID
         END IF
         RETURN
      ENDIF
      GO TO 100
C
C====== Evaluate and apply exponent
C
600   IEX = NINT (S*X)
      FPVAL = FPVAL * 10.0**IEX
      FP = FPVAL
      IV = NINT (FPVAL)
      LDM_INTFP = 2
      ND = -ND
      RETURN
C
C====== Error Condition
C
800   LDM_INTFP = 0
      RETURN
      END
