C     ------------------------------------------
      REAL*8 FUNCTION TABLE(NT,XT,YT,X1,X2,IGET)
C     ------------------------------------------
C
C     SUBROUTINE AUTHOR:
C     TIM DURBIN
C     TIMOTHY J. DURBIN, INC.
C     5330 PRIMROSE DRIVE, SUITE 228
C     FAIR OAKS, CA 95628
C     (916) 536-2314
C
C     PURPOSE:
C     THIS SUBROUTINE IS USED TO INTERPOLATE VALUES FOR A TIME AS
C     DEFINED IN THE MODEL INPUT TABLES.
C     THE FIRST ENTRY POINT IS CALLED ONCE BYTHE MAIN PROGRAM.
C     THE SECOND ENTRY POINT IS CALLED ONCE FOR EACH TIMESTEP ITERATION.
C
      IMPLICIT REAL*8 (A-H,O-Z)
      IMPLICIT INTEGER*4 (I-N)
C
C     INTERNAL SCALARS AND ARRAYS.
C     INTERP      INTERPETED VALUE FROM TABLE.
C     NT          NUMBER OF POINT IN TABLE.
C     XT()        X COORDINATE OF POINT IN TABLE.
C     YT()        Y COORDINATE OF POINT IN TABLE.
C     X1          LOWER RANGE FOR AVERAGING.
C     X2          UPPER RANGE FOR AVERAGING.
C     IGET        SWITCH FOR INTERPOLATION MEATHOD.
C
C     INTERNAL ARRAYS.
      REAL*8 INTERP
      REAL*8 XT(*),YT(*)
C
C     GET INSTANTANIOUS VALUE FROM TABLE.
      IF (IGET.EQ.1) THEN
          TABLE=INTERP(NT,XT,YT,X2,IPT)
          RETURN
      ENDIF
C
C     GET AVERAGE VALUE FROM TABLE.
      IF (IGET.EQ.2) THEN
          Y1=INTERP(NT,XT,YT,X1,IPT1)
          Y2=INTERP(NT,XT,YT,X2,IPT2)
C
C         INTERVAL X1-X2 TO LEFT OF FIRST POINT.
          IF (IPT2.EQ.1) THEN
              TABLE=Y2
              RETURN
          ENDIF
C
C         INTERVAL X1-X2 TO RIGHT OF LAST POINT.
          IF (IPT1.EQ.NT+1) THEN
              TABLE=Y1
              RETURN
          ENDIF
C
C         INTERVAL X1-X2 IS WITHIN TABLE.
          TABLE=0.0
          DO IPT=IPT1,IPT2
              IF (IPT1.EQ.IPT2) THEN
C
C                 X1 AND X2 ARE BETWEEN ADJACENT TABLE POINTS.
                  DELX=X2-X1
                  TABLE=TABLE+(Y1+Y2)*DELX*0.5
                  EXIT
              ENDIF
C
C             X1 AND X2 SPAN ONE OR MODE TABLE POINTS.
              IF (IPT.EQ.IPT1) THEN
C
C                 INTERVAL IS FROM X1 TO TABLE POINT NEXT RIGHT.
                  DELX=XT(IPT)-X1
                  TABLE=TABLE+(Y1+YT(IPT))*DELX*0.5
                  CYCLE
              ELSEIF (IPT.EQ.IPT2) THEN
C
C                 INTEVAL FROM TABLE POINT NEXT LEFT TO X2.
                  DELX=X2-XT(IPT-1)
                  TABLE=TABLE+(Y2+YT(IPT-1))*DELX*0.5
                  EXIT
              ELSE
C
C                 INTERVAL BETWEEN ADJACENT TABLE POINTS.
                  DELX=XT(IPT)-XT(IPT-1)
                  TABLE=TABLE+(YT(IPT)+YT(IPT-1))*DELX*0.5
                  CYCLE
              ENDIF
          END DO
          TABLE=TABLE/(X2-X1)
          RETURN
      ENDIF
      END FUNCTION

C     --------------------------------------
      REAL*8 FUNCTION INTERP(NT,XT,YT,X,IPT)
C     --------------------------------------
C
      REAL*8 X
      REAL*8 XT(*),YT(*)
C
C     X LESS THAN OR EQUAL TO FIRST POINT IN TABLE.
      IF (X.LE.XT(1)) THEN
          INTERP=YT(1)
          IPT=1
          RETURN
      ENDIF
C
C     X GREATER THAN LAST POINT IN TABLE.
      IF (X.GT.XT(NT)) THEN
          INTERP=YT(NT)
          IPT=NT+1
          RETURN
      ENDIF
C
C     X GREATER THAN FIRST POINT AND LESS THAN OR EQUAL TO LAST POINT.
      DO II=2,NT
          IF (X.LE.XT(II)) THEN
             SLOPE=(YT(II)-YT(II-1))/(XT(II)-XT(II-1))
             INTERP=YT(II-1)+(X-XT(II-1))*SLOPE
             IPT=II
             RETURN
          ENDIF
      END DO
      RETURN
      END FUNCTION
