      SUBROUTINE RTPRFL(TQKNWN,PKNWN,TLKNWN,QLKNWN,PINTRP,TLINTP,
     1                  QLINTP,TQINTP,ERSTAT)
      IMPLICIT NONE
C**********************************************************************
C
C   NAME- RTPRFL
C
C   LANGUAGE- VS FORTRAN    TYPE- SUBROUTINE   COMPUTER- CRAY J916
C
C   VERSION- 1.0       DATE- 12/18/91    PROGRAMMER- CARLA NEIGH
C            2.0       DATE-  5/19/94    PROGRAMMER-TERESA SIMMS, SMSRC
C        SATOV-8: SET VALUES FOR INTERPOLATED LEVELS OUTSIDE THE RANGE
C                 OF THE KNOWN LEVELS TO MISSING VALUES. REMOVE CALL
C                 TO RTPRF$.
C            3.0       DATE-   6/02/96   PROGRAMMER-TIM GARDNER (HSTX)
C            3.1       DATE-   6/19/96   PROGRAMMER-MIKE PETTEY (HSTX)
C
C   FUNCTION- THIS SUBROUTINE INTERPOLATES TEMPERATURE AND WATER
C             VAPOR MIXING RATIO VALUES TO A SET OF PRESSURE LEVELS
C             FROM KNOWN DATA USING A SPLINE INTERPOLATION.
C
C   VERSION 3.0- DOCUMENTATION AND SYNTAX CHANGES FOR ATOVS BASELINE ON
C                THE CRAY.
C   VERSION 3.1- ADDED A LINE TO INITIALIZE ERSTAT TO 0
C
C
C   CALLING SEQUENCE PARAMETERS-
C   NAME   TYPE   I/O   DESCRIPTION
C   ----   ----   ---   -----------
C   TQKNWN REAL    I    VECTOR (DIM TLKNWN+QLKNWN+1) CONTAINING THE
C                       KNOWN TEMPERATURE AND WATER VAPOR MIXING
C                       RATIO VALUES.
C   PKNWN  REAL    I    VECTOR (DIM TLKNWN) CONTAINING THE PRESSURE
C                       LEVELS CORRESPONDING TO THE KNOWN VALUES
C                       (IN MB).
C   TLKNWN INT     I    NUMBER OF KNOWN LEVELS OF TEMPERATURE (NOT
C                       INCLUDING SURFACE).
C   QLKNWN INT     I    NUMBER OF KNOWN LEVELS OF WATER VAPOR MIXING
C                       RATIO.
C   PINTRP REAL    I    VECTOR (DIM TLINTP) CONTAINING VALUES OF
C                       PRESSURE TO BE INTERPOLATED TO (IN MB).
C   TLINTP INT     I    NUMBER OF LEVELS OF TEMPERATURE (NOT INCLUDING
C                       SURFACE) TO BE INTERPOLATED.
C   QLINTP INT     I    NUMBER OF LEVELS OF WATER VAPOR MIXING RATIO
C                       TO BE INTERPOLATED.
C   TQINTP REAL    O    VECTOR (DIM TLINTP+QLINTP+1) CONTAINING
C                       INTERPOLATED TEMPERATURE AND WATER VAPOR
C                       MIXING RATIO VALUES.
C   ERSTAT INT     O    ERROR RETURN CODE FROM RTPRFL
C                        = 0 IF NO ERROR OCCURRED
C                       <> 0 IF ERROR OCCURRED.
C
C   (NOTE:  ALL TEMPERATURE VALUES ARE IN K; ALL WATER VAPOR MIXING
C    RATIO VALUES ARE IN G/KG.)
C
C   LOCAL VARIABLES-
C   NAME    TYPE  DESCRIPTION
C   ----    ----  -----------
C   INTLVL  INT   DO LOOP COUNTER FOR INTERPOLATED LEVELS.
C   KWNLVL  INT   DO LOOP COUNTER FOR KNOWN LEVELS.
C   BOTTOM  INT   INTERPOLATED PRESSURE LEVEL WITHIN THE RANGE OF
C                 THE KNOWN PRESSURE LEVELS LOWEST IN ALTITUDE.
C   TOP     INT   INTERPOLATED PRESSURE LEVEL WITHIN THE RANGE OF
C                 THE KNOWN PRESSURE LEVELS HIGHEST IN ALTITUDE.
C   TOPFLG  LOG   FLAG FOR WHETHER THE INTERPOLATED PRESSURE LEVEL
C                 HIGHEST IN ALTITUDE HAS BEEN FOUND.
C   NUMLVL  INT   THE NUMBER OF INTERPOLATED LEVELS WITHIN THE RANGE
C                 OF THE KNOWN PRESSURE LEVELS.
C   TBLIND  INT   ARRAY (DIM 100) CONTAINING INDEX OF KNOWN
C                 LEVEL THAT IS THE LOWER END OF THE INTERVAL
C                 CONTAINING THE INTERPOLATED LEVEL.
C   DX      REAL  ARRAY (DIM 100) CONTAINING DIFFERENCE BETWEEN
C                 INTERPOLATED PRESSURE LEVEL VALUES AND THEIR
C                 CORRESPONDING KNOWN PRESSURE LEVEL VALUES
C                 INDEXED BY TBLIND.
C   DX2DV2  REAL  ARRAY (DIM 100) CONTAINING DX SQUARED DIVIDED BY 2.
C   DX3DV6  REAL  ARRAY (DIM 100) CONTAINING DX CUBED DIVIDED BY 6.
C   KLVLDX  REAL  ARRAY (DIM 100) CONTAINING DIFFERENCES IN PRESSURE
C                 VALUES BETWEEN ADJACENT KNOWN LEVELS.
C   P27     REAL  STATEMENT FUNCTION USED TO CONVERT PRESSURE TO
C                 PRESSURE IN P**(2.0/7.0).
C   P       REAL  VALUE FOR PRESSURE IN STATEMENT FUNCTION P27.
C
C   NOTE:  THESE LOCAL ARRAYS ARE CURRENTLY DIMENSIONED AT 100.  THE
C          DIMENSION MUST BE INCREASED IF THE NUMBER OF INTERPOLATED
C          TEMPERATURE LEVELS (OR KNOWN TEMPERATURE LEVELS IN THE
C          CASE OF KLVLDX) IS GREATER THAN 100.
C
C   SUBROUTINES CALLED- SPLNCF
C
C   FUNCTIONS USED- SPLNVL, INTRVL
C
C   COMMON AREA PARAMETERS- SPVAL
C
C   EXIT STATES- NONE
C
C   COMMENTS-
C     SEE THE FOLLOWING REFERENCES:
C       DE BOOR, CARL,1978: A PRACTICAL GUIDE TO SPLINES, SPRINGER-
C          VERLAG, 392 PP.
C       HYMAN, JAMES M.,1983: ACCURATE MONOTONICITY PRESERVING CUBIC
C          INTERPOLATION, SIAM J.SCI.STAT.COMPUT., 4, 645-654.
C
C**********************************************************************
C
      INTEGER TLKNWN, QLKNWN, TLINTP, QLINTP, ERSTAT
      REAL TQKNWN(TLKNWN+QLKNWN+1), TQINTP(TLINTP+QLINTP+1)
      REAL PKNWN(TLKNWN), PINTRP(TLINTP), P27, P, SPLNVL
C
      LOGICAL LVLFLG, INTRVL, TOPFLG
      INTEGER TBLIND(100), INTLVL, KWNLVL, TOP, BOTTOM, NUMLVL
      REAL DX(100),DX2DV2(100),DX3DV6(100),COEFF(4,100),KLVLDX(100)
C
      INCLUDE 'SPVAL'


C  DEFINE STATEMENT FUNCTION P27

      P27(P) = P**(2.0/7.0)

      TOPFLG = .FALSE.
      ERSTAT = 0
C
C  INITIALIZE OUTPUT ARRAY TO MISSING VALUES.
C
      DO 10 INTLVL = 1, TLINTP+QLINTP+1
         TQINTP(INTLVL) = R4SPV
   10 CONTINUE
C
C  SET UP AN ARRAY OF INDICES
C  FOR LOWER BOUNDS OF THE INTERVALS IN THE KNOWN LEVELS CONTAINING
C  THE INTERPOLATED LEVELS, AND ARRAYS CONTAINING THE DIFFERENCES IN
C  THE PRESSURE VALUES AT THESE LEVELS, THE DIFFERENCES SQUARED DIVIDED
C  BY 2, AND THE DIFFERENCES CUBED DIVIDED BY 6.  ALSO SET UP THE ARRAY
C  OF DIFFERENCES BETWEEN ADJACENT KNOWN LEVELS.  FOR ALL DIFFERENCE
C  CALCULATIONS, CONVERT PRESSURE TO PRESSURE**(2.0/7.0).
C
        DO 20 INTLVL = 1, TLINTP
          LVLFLG = INTRVL(PKNWN,TLKNWN,PINTRP(INTLVL),TBLIND(INTLVL))
C
          IF(LVLFLG) THEN
C  INTERPOLATED LEVEL IS INSIDE THE RANGE OF THE KNOWN PRESSURE LEVELS.
            BOTTOM=INTLVL
C
            IF (.NOT. TOPFLG) THEN
              TOPFLG = .TRUE.
              TOP=INTLVL
            ENDIF
C
          ELSE
C
C  INTERPOLATED LEVEL IS OUTSIDE THE RANGE OF THE KNOWN PRESSURE LEVELS.
            GOTO 20
          ENDIF
C
          DX(INTLVL) = P27(PINTRP(INTLVL)) - P27(PKNWN(TBLIND(INTLVL)))
          DX2DV2(INTLVL) = DX(INTLVL)**2 / 2.0
          DX3DV6(INTLVL) = DX(INTLVL)**3 / 6.0
   20   CONTINUE
C
        NUMLVL = ABS(BOTTOM - TOP) + 1
C
C  CHECK IF ALL INTERPOLATED PRESSURE LEVELS ARE MISSING.
        IF (NUMLVL .LE. 1) THEN
          ERSTAT = 99
          RETURN
        ENDIF
C
        KLVLDX(1) = 0.0
        DO 30 KWNLVL = 2, TLKNWN
          KLVLDX(KWNLVL) = P27(PKNWN(KWNLVL)) - P27(PKNWN(KWNLVL-1))
   30   CONTINUE
C
C  SET VALUE OF FIRST SPLINE COEFFICIENT FOR EACH KNOWN TEMPERATURE
C  DATA POINT.  SET VALUE OF THIRD SPLINE COEFFICIENT TO THE DIFFERENCE
C  BETWEEN PRESSURE VALUES AT ADJACENT LEVELS.
C
      DO 40 KWNLVL = 1, TLKNWN
        COEFF(1,KWNLVL) = TQKNWN(KWNLVL)
        COEFF(3,KWNLVL) = KLVLDX(KWNLVL)
   40 CONTINUE
C
C  COMPUTE INTERPOLATED TEMPERATURE VALUES.
C
      CALL SPLNCF(COEFF,TLKNWN,.TRUE.)
C
      DO 50 INTLVL = TOP, BOTTOM
        TQINTP(INTLVL) = SPLNVL(COEFF(1,TBLIND(INTLVL)),DX(INTLVL),
     1                 DX2DV2(INTLVL),DX3DV6(INTLVL))
   50 CONTINUE
C
C  TRANSFER SURFACE TEMPERATURE VALUE FROM KNOWN TO INTERPOLATED
C  PROFILE.
C
      TQINTP(TLINTP+1) = TQKNWN(TLKNWN+1)
C
C  SET VALUE OF FIRST SPLINE COEFFICIENT FOR EACH KNOWN WATER
C  VAPOR MIXING RATIO DATA POINT.
C
      DO 60 KWNLVL = TLKNWN-QLKNWN+1, TLKNWN
        COEFF(1,KWNLVL) = TQKNWN(KWNLVL+QLKNWN+1)
        COEFF(3,KWNLVL) = KLVLDX(KWNLVL)
   60 CONTINUE
C
C  COMPUTE INTERPOLATED MIXING RATIO VALUES.
C
      CALL SPLNCF(COEFF(1,TLKNWN-QLKNWN+1),QLKNWN,.TRUE.)
C
      DO 70 INTLVL = TLINTP-QLINTP+1, BOTTOM
        TQINTP(INTLVL+QLINTP+1) = SPLNVL(COEFF(1,TBLIND(INTLVL)),
     1                DX(INTLVL),DX2DV2(INTLVL),DX3DV6(INTLVL))
   70 CONTINUE
C
      RETURN
      END
C*********************************************************************
      SUBROUTINE RTPRF$(MSGCOD,PLVL,STAT)
C*********************************************************************
C   NAME- RTPRF$
C
C   LANGUAGE- VS FORTRAN   TYPE- SUBROUTINE        COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 01/06/92    PROGRAMMER- CARLA NEIGH(SMSRC)
C   VERSION- 3.0    DATE- 06/03/96    PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 3.1    DATE- 10/10/97    PROGRAMMER- LEAH WILSON (HSTX)
C
C   FILES-
C    DSN                       I/O  UNIT   FUNCTIONAL DESCRIPTION
C    ---                       ---  ----   -----------------------
C     ...KIDS                 O    KUNIT   KRAY INTERACTIVE DOCUMENTATION
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE RTPRFL
C
C   VERSION 3.0- DOCUMENTATION AND SYNTAX CHAGES FOR ATOVS BASELINE ON
C                THE CRAY.
C   VERSION- 3.1 WRITE ERROR MESSAGES TO THE KIDS FILE.  INCLUDE /SPGUNT/.
C
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE      TYPE  I/O  FUNCTION
C     --------      ----  ---  --------
C     MSGCOD        INT   I   MESSAGE CODE USED TO IDENTIFY THE ERROR
C                             PATTERN.
C     PLVL          REAL  I   PRESSURE LEVEL WHERE ERROR OCCURRED.
C     STAT          INT   O   STATUS CODE IF AN ERROR OCCURS DURING I/O.
C
      INTEGER   MSGCOD, STAT
      REAL PLVL
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     I                       INT     DO LOOP COUNTER
C     KMSG                   CH*100   KIDS MESSAGE BUFFER
C     C4TEMP                  CH*4    HOLDING BUFFER FOR WRITING TO KIDS
C     C12TMP                  CH*12   HOLDING BUFFER FOR WRITING TO KIDS
C
C   SUBROUTINES CALLED- KIDS
C
C   EXIT STATES-
C     STAT =  0; INDICATING NO OR A NON-FATAL ERROR HAS OCCURRED
C     STAT = 99; INDICATING A FATAL ERROR HAS OCCURRED
C
C*********************************************************************
C
      INTEGER     I
      CHARACTER*4   C4TEMP
      CHARACTER*12  C12TMP
      CHARACTER*100 KMSG
C
C  SET ERINFO:SUBNAM.
C
      SUBNAM = 'SPGDVR'
C
C DETERMINE WHICH ERROR MESSAGE TO WRITE, IF MSGCOD = 110
C
      IF (MSGCOD .EQ. 110) THEN
        KMSG(1:50)=' * FATAL ERROR * Interpolated pressure level value'
        KMSG(51:100)='              does not fall w/in bounds.'
        WRITE(C12TMP,'(F12.4)') PLVL
        KMSG(52:63) = C12TMP
        STAT = 99
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE
C
C NOT A VALID MSGCOD
C
        STAT=0
        KMSG = ' Invalid error message code = '
        WRITE(C4TEMP,'(I4)') MSGCOD
        KMSG(31:34) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
      RETURN
      END


