C$$$  SUBPROGRAM DOCUMENTATION BLOCK
C                .      .    .                                       .
C SUBPROGRAM:    M_INTP      INTERPOLATE RAOB PRESSURE
C   PRGMMR: M. BARTH         ORG: FSL        DATE: 00-02-10
C
C ABSTRACT: USE A LOG(P) INTERPOLATION TO COMPUTE MISSING PRESSURES.
C   ONLY SURFACE, MANDATORY, OR SIGT LEVELS CAN BE USED.
C
C PROGRAM HISTORY LOG:
C   00-02-10  M. BARTH    ORIGINAL VERSION
C   08-06-13  L. BENJAMIN CHANGED "MADISCON" INCLUDE TO 
C                         "INTQC1CON1".  
C
C USAGE:    CALL M_INTP (I,NLEVELS,LTYPE,HT,P)
C
C   INPUT ARGUMENT LIST:
C     I        - I       INDEX OF THE LEVEL TO BE INTERPOLATED
C     NLEVELS  - I       NUMBER OF LEVELS IN DATA ARRAYS
C     LTYPE    - IA      LEVEL TYPES (E.G., MANDATORY, SIGT)
C     HT       - RA      HEIGHTS
C
C   INPUT/OUTPUT ARGUMENT LIST:
C     P        - RA      PRESSURES
C
C SUBPROGRAMS CALLED:
C     NONE
C
C REMARKS:
C     THE INTERPOLATION ALGORITHM USED IS FROM THE FSL RADIOSONDE
C     DATABASE PROJECT.
C
C ATTRIBUTES:
C   LANGUAGE: FORTRAN-77 + EXTENSIONS
C   MACHINE:  UNIX
C$$$

      SUBROUTINE M_INTP(I,NLEVELS,LTYPE,HT,P)

C     ------------------------------------------------------------------
C     INCLUDES AND COMMONS
C     ------------------------------------------------------------------
      INCLUDE 'IMPLICIT'
      INCLUDE 'INTQC1CON1'

C     ------------------------------------------------------------------
C     ARGUMENTS
C     ------------------------------------------------------------------
      INTEGER I,NLEVELS
      INTEGER LTYPE(*)
      REAL HT(*)
      REAL P(*)

C     ------------------------------------------------------------------
C     LOCAL
C     ------------------------------------------------------------------
      INTEGER PNT1,PNT2,J,LOW,HIGH
      REAL A,B,C,D


C     *****************************************************************
C     ******************* BEGIN OF M_INTP *****************************
C     *****************************************************************

C     ------------------------------------------------------------------
C     INITIALIZE THE DIFFERENCE VARIABLES
C     ------------------------------------------------------------------
      PNT1 = MISS_P
      PNT2 = MISS_P

C     ------------------------------------------------------------------
C     FIND THE CLOSEST HEIGHT MEASURE TO TARGET.
C     ------------------------------------------------------------------
      DO 20 J = 1, NLEVELS

         IF(J.EQ.I)GO TO 20

         IF(P(J).NE.MISS_P.AND.LTYPE(J).LT.4)THEN

	    IF(HT(I)-HT(J).GE.0.AND.HT(I)-HT(J).LT.PNT1)THEN

               LOW = J
               PNT1 = HT(I) - HT(J)

	    ENDIF

	    IF (HT(J)-HT(I).GE.0.AND.HT(J).NE.MISS_P.AND.
     1          HT(J)-HT(I).LT.PNT2)THEN

               PNT2 = HT(J) - HT(I)
               HIGH = J

	    ENDIF

         ENDIF

 20   CONTINUE

C     ------------------------------------------------------------------
C     IF THERE WAS NO UPPER LEVEL OR LOWER LEVEL BOUNDING THE
C     MISSING PRESSURE LEVEL THEN SKIP IT.
C     ------------------------------------------------------------------
      IF(PNT2.EQ.MISS_P.OR.PNT1.EQ.MISS_P)GO TO 900

C     ------------------------------------------------------------------
C     IF HEIGHTS ARE THE SAME THEN ASSIGN EQUIVALENT PRESSURES.
C     ------------------------------------------------------------------
      IF(PNT2.EQ.0.OR.PNT1.EQ.0)THEN

         IF(PNT2.EQ.0)THEN

	    P(I) = P(HIGH)

         ELSE

	    P(I) = P(LOW)

         ENDIF

         GOTO 900

      ENDIF

C     ------------------------------------------------------------------
C     ASSIGN INTERMEDIATE VALUES TO SOME OF THE LOG(P) INTERPOLATION
C     FORMULA AND PERFORM THE INTERPOLATION.
C     ------------------------------------------------------------------
      A = HT(I) - HT(LOW)
      B = P(LOW)/P(HIGH)
      C =  HT(HIGH) - HT(LOW)
      D = EXP(A * LOG(B)/C)
      P(I) = P(LOW)/D

      IF(P(I).EQ.0)P(I) = MISS_P

 900  RETURN
      END


C**---------------------------------------------------------------------
C**REQUIRED STANDARD FSL DISCLAIMER (NOVEMBER 2000)
C**---------------------------------------------------------------------
C**  OPEN SOURCE LICENSE/DISCLAIMER,  FORECAST SYSTEMS LABORATORY
C**  NOAA/OAR/FSL, 325 BROADWAY BOULDER, CO 80305
C**
C**  THIS SOFTWARE IS DISTRIBUTED UNDER THE OPEN SOURCE DEFINITION, 
C**  WHICH MAY BE FOUND AT http://www.opensource.org/osd.html.
C**
C**  IN PARTICULAR, REDISTRIBUTION AND USE IN SOURCE AND BINARY FORMS,
C**  WITH OR WITHOUT MODIFICATION, ARE PERMITTED PROVIDED THAT THE 
C**  FOLLOWING CONDITIONS ARE MET:
C**
C**  - REDISTRIBUTIONS OF SOURCE CODE MUST RETAIN THIS NOTICE, THIS LIST
C**    OF CONDITIONS AND THE FOLLOWING DISCLAIMER.
C**
C**  - REDISTRIBUTIONS IN BINARY FORM MUST PROVIDE ACCESS TO THIS 
C**    NOTICE, THIS LIST OF CONDITIONS AND THE FOLLOWING DISCLAIMER, AND
C**    THE UNDERLYING SOURCE CODE.
C**
C**  - ALL MODIFICATIONS TO THIS SOFTWARE MUST BE CLEARLY DOCUMENTED, 
C**    AND ARE SOLELY THE RESPONSIBILITY OF THE AGENT MAKING THE 
C**    MODIFICATIONS.
C**
C**  - IF SIGNIFICANT MODIFICATIONS OR ENHANCEMENTS ARE MADE TO THIS
C**    SOFTWARE, THE FSL SOFTWARE POLICY MANAGER 
C**    (softwaremgr.fsl@noaa.gov) BE NOTIFIED.
C**
C**  THIS SOFTWARE AND ITS DOCUMENTATION ARE IN THE PUBLIC DOMAIN AND 
C**  ARE FURNISHED "AS IS."  THE AUTHORS, THE UNITED STATES GOVERNMENT,
C**  ITS INSTRUMENTALITIES, OFFICERS, EMPLOYEES, AND AGENTS MAKE NO 
C**  WARRANTY, EXPRESS OR IMPLIED, AS TO THE USEFULNESS OF THE SOFTWARE
C**  AND DOCUMENTATION FOR ANY PURPOSE.  THEY ASSUME NO RESPONSIBILITY 
C**  (1) FOR THE USE OF THE SOFTWARE AND DOCUMENTATION; OR (2) TO 
C**  PROVIDE TECHNICAL SUPPORT TO USERS.
C**---------------------------------------------------------------------
