!+ Subroutine to obtain the value of a SAMSON field at a point.
!
! Method:
!	The field to be evaluated is passed to the routine, together
!	with its second derivatives if a splined fit is used. Its
!	value at the pressure supplied is evaluated according to the
!	type of interpolation prescribed. The result is returned.
!
! Current Owner of Code: J. M. Edwards
!
! History:
!	Version		Date			Comment
!	1.0		12-04-95		First Version under RCS
!						(J. M. Edwards)
!
! Description of Code:
!   FORTRAN 77  with extensions listed in documentation.
!
!- ---------------------------------------------------------------------
      SUBROUTINE INTERPOLATE_P(IERR, N, P, A, X, Y, Y2, WORK, PP, AA
     &   , I_MODE, L_SPLINED)
!
!
!
      IMPLICIT NONE
!
!
!     INCLUDE HEADER FILES.
      INCLUDE 'incl/interp_mode.pcf'
      INCLUDE 'incl/def_std_io.icf'
      INCLUDE 'incl/error.pcf'
!
!     DUMMY ARGUMENTS.
      INTEGER	!, INTENT(OUT)
     &     IERR
!             ERROR FLAG
      INTEGER	!, INTENT(IN)
     &     N
!             NUMBER OF LEVELS
     &   , I_MODE
!             MODE OF INTERPOLATION
      LOGICAL	!, INTENT(IN)
     &     L_SPLINED
!             TRUE IF FIELD ALREADY SPLINED
      REAL*8	!, INTENT(IN)
     &     P(N)
!             PRESSURE LEVELS
     &   , A(N)
!             FIELD TO BE INTERPOLATED
     &   , PP
!             INTERPOLATING PRESSURE
      REAL*8	!, INTENT(INOUT)
     &     X(N)
!             CONVERTED ABSCISSA
     &   , Y(N)
!             CONVERTED ORDINATE
     &   , Y2(N)
!             SECOND DERIVATIVE OF ORDINATE
     &   , WORK(N)
!             WORKING AARAY
      REAL*8	!, INTENT(OUT)
     &     AA
!             INTERPOLATED VALUE
!
!     LOCAL VARIABLES.
      INTEGER
     &     I
!             LOOP VARIABLE
      REAL*8
     &    XX
!             CONVERTED EVALAUTION POINT
     &  , YY
!             RAW INTERPOLATE
!
!     SUBROUTINES CALLED:
      EXTERNAL
     &     SPLINE_FIT, SPLINE_EVALUATE
!
!
!
!     PERFORM THE INITIAL SPLINE FIT ON THE FIRST CALL.
      IF (.NOT.L_SPLINED) THEN
         IF ( (I_MODE.EQ.IP_1_LIN_LIN)
     &      .OR.(I_MODE.EQ.IP_3_LIN_LIN) ) THEN
!           LINEAR-LINEAR FIT.
            DO I=1, N
               X(I)=P(I)
               Y(I)=A(I)
            ENDDO
         ELSE IF ( (I_MODE.EQ.IP_1_LOG_LIN)
     &      .OR.(I_MODE.EQ.IP_3_LOG_LIN) ) THEN
!           LOGARITHMIC-LINEAR FIT.
            DO I=1, N
               X(I)=LOG(P(I))
               Y(I)=A(I)
            ENDDO
         ELSE IF ( (I_MODE.EQ.IP_1_LIN_LOG)
     &      .OR.(I_MODE.EQ.IP_3_LIN_LOG) ) THEN
!           LINEAR-LOGARITHMIC FIT.
            DO I=1, N
               X(I)=P(I)
               Y(I)=LOG(A(I))
            ENDDO
         ELSE IF ( (I_MODE.EQ.IP_1_LOG_LOG)
     &      .OR.(I_MODE.EQ.IP_3_LOG_LOG) ) THEN
!           LOGARITHMIC-LOGARITHMIC FIT.
            DO I=1, N
               X(I)=LOG(P(I))
               Y(I)=LOG(A(I))
            ENDDO
         ELSE
            WRITE(IU_ERR, '(/A)')
     &         '***ERROR: UNRECOGNIZED FITTING MODE.'
            IERR=I_ERR_FATAL
            RETURN
         ENDIF
!
         IF ( (I_MODE.EQ.IP_1_LIN_LIN).OR.
     &        (I_MODE.EQ.IP_1_LOG_LIN).OR.
     &        (I_MODE.EQ.IP_1_LIN_LOG).OR.
     &        (I_MODE.EQ.IP_1_LOG_LOG) ) THEN
!           THIS IS A SPLINE FIT WITHOUT SECOND DERIVATIVES.
            DO I=1, N
               Y2(I)=0.0D+00
            ENDDO
         ELSE IF ( (I_MODE.EQ.IP_3_LIN_LIN).OR.
     &        (I_MODE.EQ.IP_3_LOG_LIN).OR.
     &        (I_MODE.EQ.IP_3_LIN_LOG).OR.
     &        (I_MODE.EQ.IP_3_LOG_LOG) ) THEN
!           CUBIC SPLINES.
            CALL SPLINE_FIT(N, X, Y, Y2, WORK)
         ENDIF
         L_SPLINED=.TRUE.
!
      ENDIF
!
!
!     CALCULATE THE SPLINING POINT FROM PP.
      IF ( (I_MODE.EQ.IP_1_LIN_LIN)
     &   .OR.(I_MODE.EQ.IP_3_LIN_LIN) ) THEN
         XX=PP
      ELSE IF ( (I_MODE.EQ.IP_1_LOG_LIN)
     &   .OR.(I_MODE.EQ.IP_3_LOG_LIN) ) THEN
         XX=LOG(PP)
      ELSE IF ( (I_MODE.EQ.IP_1_LIN_LOG)
     &   .OR.(I_MODE.EQ.IP_3_LIN_LOG) ) THEN
         XX=PP
      ELSE IF ( (I_MODE.EQ.IP_1_LOG_LOG)
     &   .OR.(I_MODE.EQ.IP_3_LOG_LOG) ) THEN
         XX=LOG(PP)
      ELSE
         WRITE(IU_ERR, '(/A)')
     &      '***ERROR: UNRECOGNIZED FITTING MODE.'
         IERR=I_ERR_FATAL
         RETURN
      ENDIF
!
      CALL SPLINE_EVALUATE(IERR, N, X, Y, Y2, XX, YY)
      IF (IERR.NE.I_NORMAL) THEN
         IF (IERR.EQ.I_ERR_RANGE) THEN
            IF (XX.LT.X(1)) THEN
               YY=Y(1)
            ELSE IF (XX.GT.X(N)) THEN
               YY=Y(N)
            ENDIF
!           RECOVER FORM THE ERROR
            IERR=I_NORMAL
         ELSE
            IERR=I_ERR_FATAL
            RETURN
         ENDIF
      ENDIF
!
!     CONVERT BACK TO THE ACTUAL DATA FIELD.
      IF ( (I_MODE.EQ.IP_1_LIN_LIN)
     &   .OR.(I_MODE.EQ.IP_3_LIN_LIN) ) THEN
         AA=YY
      ELSE IF ( (I_MODE.EQ.IP_1_LOG_LIN)
     &   .OR.(I_MODE.EQ.IP_3_LOG_LIN) ) THEN
         AA=YY
      ELSE IF ( (I_MODE.EQ.IP_1_LIN_LOG)
     &   .OR.(I_MODE.EQ.IP_3_LIN_LOG) ) THEN
         AA=EXP(YY)
      ELSE IF ( (I_MODE.EQ.IP_1_LOG_LOG)
     &   .OR.(I_MODE.EQ.IP_3_LOG_LOG) ) THEN
         AA=EXP(YY)
      ENDIF
!
!
      RETURN
      END
