C******************** START FILE ECINTR.FOR ; GROUP ECINTR ******************
C-------------------------------------------------------------------
C  ECINTR
C
C  INTERPOLATION UTILITY
C  DEFINE LINEAR INTERPOLATION IN ONE DIMENSION
C
      SUBROUTINE ECINTR(TIM0,T,NT,IT0,IT0P,XI,XIC,IEX)
C
      REAL T(NT)
C
C  INPUT
C   TIM0     TARGET OF INTERPOLATION
C   T(NT)  DOMAIN OF DATA ON WHICH INTERPOLATION IS TO BE PERFORMED
C
C  OUTPUT
C   IT0    INDEX TO GREATEST T PT .LT. TIM0
C   IT0P   IT0+1
C   XI     INTERPOLATION FACTOR (T0-T(IT0))/(T(IT0P)-T(IT0))
C          I.E. --> 0.0 IF TARGET IS NEAR T(IT0)
C          AND --> 1.0 IF TARGET IS NEAR T(IT0P)
C   XIC    COMPLIMENTARY FACTOR XIC=1.0-XI
C   IEX    FLAG 0=NORMAL, 1=TARGET OUT OF RANGE, EXTRAPOLATION EXPECTED
C
C  INTERPOLATION FORMULA WILL BE
C   F0=F(IT0)*XIC + F(IT0P)*XI
C
C  IF F(NT) DEFINED AT T(NT) IS THE FUNCTION BEING INTERPOLATED
C
C  IF CALLER KEEPS INTERPOLATION INDEX IT0 FROM PRIOR CALL, SEARCH FOR
C  NEW INTERPOLATION INDEX WILL PROCEED FROM THAT POINT AFTER A LIMIT
C  CHECK
C
C---------------------------------------------------------------------
C  START
C
      IEX=0
C
      IT0=MIN0(NT,MAX0(1,IT0))
C  CHECK FOR EXTRAPOLATION
      IF(TIM0.LE.T(1)) THEN
C  LESS THAN MINIMUM
        IT0=1
        XI=0.0
        IF(TIM0.LT.T(1)) IEX=1
      ELSE IF(TIM0.GE.T(NT)) THEN
C  GREATER THAN MAXIMUM
        IT0=NT
        XI=0.0
        IF(TIM0.GT.T(NT)) IEX=1
      ELSE
C  INBETWEEN
C   DETERMINE DIRECTION OF SEARCH
        IF(T(IT0).LT.TIM0) THEN
C   SEARCH FORWARD
          I=IT0+1
          DO WHILE (T(I).LT.TIM0)
            I=I+1
          ENDDO
          IT0=I-1
          XI=(TIM0-T(IT0))/(T(I)-T(IT0))
        ELSE IF(T(IT0).GT.TIM0) THEN
C   SEARCH BACK
          I=IT0-1
          DO WHILE (T(I).GT.TIM0)
            I=I-1
          ENDDO
          IT0=I
          I=I+1
          XI=(TIM0-T(IT0))/(T(I)-T(IT0))
        ELSE
C   BULL'S EYE
          XI=0.0
        ENDIF
      ENDIF
C
C  FINISH UP
C
      IT0P=MIN0(NT,(IT0+1))
      XIC=1.0-XI
C
      RETURN
      END
C******************** END FILE ECINTR.FOR ; GROUP ECINTR ******************
