      SUBROUTINE CLDPRS(TMPCLD,TPROFI,TROPRS,TROTMP,SURPRS,TROP1,
     +                  ISISO,MINPRESS,DPRESS)
      IMPLICIT NONE
C*******************************************************************
C
C   LANGUAGE- FORTRAN     TYPE- MAIN
C
C   VERSION- 1.0    DATE- 02/06/91   PROGRAMMER- ZHOU SISONG
C   VERSION- 1.1    DATE- 08/13/91   PROGRAMMER- TIM GARDNER (HSTX)
C   VERSION- 1.2    DATE- 09/29/95   PROGRAMMER- LEAH CASEY (HSTX)
C   VERSION- 1.3    DATE- 06/03/96   PROGRAMMER- LEAH CASEY (HSTX)
C   VERSION- 3.0    DATE- 11/13/97   PROGRAMMER- BRIAN BARIL (HSTX)
C   VERSION- 3.1    DATE- 04/05/01   PROGRAMMER- A. ALLEGRINO (RITSS)
C   VERSION- 3.2    DATE- 06/08/06   PROGRAMMER- FRANK TILLEY (IMSG)
C   VERSION- 3.3    DATE- 04/09/07   PROGRAMMER- FRANK TILLEY (IMSG)
C   VERSION- 3.4    DATE- 07/30/08   PROGRAMMER- FRANK TILLEY (IMSG)
C   VERSION- 3.5    DATE- 01/21/10   PROGRAMMER- FRANK TILLEY (IMSG)
C   VERSION- 3.6    DATE- 08/20/10   PROGRAMMER- FRANK TILLEY (IMSG)
C
C  FUNCTION - CALCULATES CLOUD TOP PRESSURE FOR EACH SPOT
C             IN A MINIBOX
C  VERSION- 1.2 MODIFICATIONS FOR INCLUSION INTO RTOVS.
C  VERSION- 1.3 MODS FOR ATTENUATION ABOVE THE CLOUDS.
C  VERSION- 3.0 MODIFICATIONS FOR INCLUSION INTO ATOVS.
C  VERSION- 3.1 INCLUDED COMMON RETPARM AND INCREASED THE SIZE OF
C               THE TPROFI ARRAY TO 42 (PRFTLV)PRFTLV) (WR#150)
C  VERSION- 3.2 ADDED SURFACE PRESSURE TO THE CALLING ARGUMENTS AND
C               CONSTRAINED THE CLOUD TOP PRESSURE TO BE NO GREATER
C               THAN THE SURFACE PRESSURE. ADDED TROPOPAUSE PRESSURE
C               TO THE CALLING ARGUMENTS AND CONSTRAINED CLOUD TOP
C               PRESSURE TO BE BELOW THE TROPOPAUSE.
C  VERSION- 3.3 ADD LOGICAL TROP1 TO CALLING ARGUMENTS. WHEN TROP1
C               IS TRUE THE CLOUD TOP IS ABOVE THE TROPOPAUSE.
C  VERSION- 3.4 CONSTRAINED THE CLOUD TOP PRESSURE TO BE BETWEEN 60
C               AND 1000 MB. IF THE CALCULATED CLOUD TOP PRESSURE
C               IS LESS THAN 60 IT GETS SET TO 60 AND THE CLOUD TOP
C               TEMPERATURE GETS SET TO THE RETRIEVED TEMPERATURE
C               AT 60 MB. IF THE CLOUD TOP PRESSURE GETS SET TO
C               1000 MB THE CLOUD TOP TEMPERATURE GETS SET TO THE
C               RETRIEVED TEMPERATURE AT 1000 MB
C  VERSION- 3.5 ADDED LOGIC TO SET THE CLOUD TOP TEMPERATURE TO THE
C               TROPOPAUSE TEMPERATURE AND THE CLOUD TOP PRESSURE TO
C               THE TROPOPAUSE PRESSURE IF THE CLOUD TOP TEMPERATURE
C               IS LESS THAN OR EQUAL TO THE TROPOPAUSE TEMPERATURE.
C  VERSION- 3.6 ADDED CALLING ARGUMENT 'MINPRESS' AND MODIFIED CODE
C               TO ENSURE THE PRESSURE AT THE CLOUD TOP IS NEVER
C               LESS THAN THIS MINIMUM PRESSURE VALUE.
C
C   PARAMETERS-
C      VARIABLE                TYPE I/O FUNCTION
C      --------                ---- --- --------
C      TMPCLD                  REAL I/O  CLOUD TOP TEMPERATURE AT SPOT.
C      TPROFI(42)              REAL  I   ATMOSPHERIC PROFILE AT TOVS 42
C                                        LEVELS.
C      TROPRS                  REAL  I   TROPOPAUSE PRESSURE.
C      TROTMP                  REAL  I   TROPOPAUSE TEMPERATURE.
C      SURPRS                  REAL  I   CALCULATED SURFACE PRESSURE. 
C      TROP1                    LOG  I   WHEN TRUE THE CLOUD TOP IS ABOVE
C                                        THE TROPOPAUSE.
C      ISISO                    LOG  I   FLAG SET TO TRUE WHEN THE TEMPERATURE
C                                        PROFILE IMMEDIATELY ABOVE THE
C                                        TROPOPAUSE DOES NOT INCREASE WITH
C                                        HEIGHT.
C      MINPRESS                REAL  I   MINIMUM ALLOWABLE PRESSURE FOR CLOUD
C                                        TOP PRESSURE.
C      DPRESS                  REAL  O   CLOUD TOP PRESSURE AT THE SPOT.
C
C*******************************************************************

      INCLUDE 'RETPARM'
      INCLUDE 'RTPLVL'

      INTEGER L,J,IU,IV,K,IL, MINLEV
      REAL TMPCLD,DPRESS,TPROFI(PRFTLV),TROPRS,SURPRS,TROTMP 
      REAL TMPUP,TMPDN,TMPDF,PRSUP,PRSDF,PRSGD,TMPDFX,PRSCLD,PRSDN
      REAL YPROFI(30),YPRESS(30),MINPRESS,PRDIFF
      LOGICAL TROP1, ISISO
C
C  LOOK UP TABLE FOR ATMOSPHERIC LAYERS
C
      DO L = 1, 30
         YPRESS(L) = PROFLV(L+10)
      END DO
C
C  IF THE CLOUD TOP TEMPERATURE IS LESS THAN OR EQUAL TO THE TROPOPAUSE
C  TEMPERATURE, SET THE CLOUD TOP TEMPERATURE TO THE TROPOPAUSE TEMPERATURE
C  AND THE CLOUD TOP PRESSURE TO THE TROPOPAUSE PRESSURE.
C
      IF (TMPCLD.LE.TROTMP) THEN
         TMPCLD = TROTMP
         DPRESS = TROPRS
         GO TO 300
      END IF
C        
      DO L=1,30
         YPROFI(L) = TPROFI(10+L)
      END DO
C
C  FIND THE PRESSURE LEVEL THAT IS CLOSEST TO THE MINIMUM PRESSURE LEVEL.
C  THIS INFORMATION WILL BE USED IF THE CLOUD TOP PRESSURE GETS SET TO THE
C  MINIMUM PRESSURE.
C
      MINLEV = 0
      PRDIFF = 9999.9
      DO L = 1, 30
         IF (ABS(MINPRESS - YPRESS(L)) .LT. PRDIFF) THEN
            PRDIFF = ABS(MINPRESS - YPRESS(L))
            MINLEV = L
         END IF
      END DO
      
C
C  IF THE CLOUD TOP IS ABOVE THE TROPOPAUSE, CHECK FOR CLOUD TOP PRESSURE
C  BETWEEN 60 MB AND THE TROPOPAUSE PRESSURE.
C

      IF (TROP1) THEN
         DO 40 J = 30,1, -1
            IF (YPRESS(J) .GT. TROPRS) GO TO 40
C           IF (YPROFI(J).GT.TMPCLD) GO TO 40
            IF (YPROFI(J).LT.TMPCLD) GO TO 40
            IU = J+1
            GO TO 150
   40    CONTINUE
         PRSCLD = 50.0
         TMPCLD = YPROFI(6)
         GO TO 110
      ELSE
            
C
C  CALCULATE CLOUD TOP PRESSURE FOR EACH SPOT STARTING AT OR BELOW
C  THE TROPOPAUSE.
C
         DO 100 J=1,30
            IF ( YPRESS(J) .LT. TROPRS) GO TO 100
            IF( TMPCLD.GT.YPROFI(J) ) GOTO 100
C
            IU=J
            GOTO 102
  100    CONTINUE
         GOTO 160
      END IF
C
  102 CONTINUE
C.....  CHECK IF THERE IS A INVERSION LAYER
      IV=IU+1
      DO 120 K=IV,30
         IF(TMPCLD.LT.YPROFI(K)) GOTO 120
         IU=K
         GOTO 103
  120 CONTINUE
C.....  INVERSION DOES NOT EXIST
      GOTO 150
C
  103 CONTINUE
C....... INVERSION EXISTS, FIND THE LOWER INTERSECTION
      IL=IU+1
      DO 130 L=IL,30
         IF(TMPCLD.GT.YPROFI(L)) GOTO 130
         IU=L
         GOTO 150
  130 CONTINUE
C
  150 CONTINUE
C.....  CALCULATE CLOUD TOP PRESSURE
C
      IF( IU.EQ.1 ) GOTO 101
C
      TMPUP = YPROFI(IU-1)
      TMPDN = YPROFI(IU)
      TMPDF = ABS( TMPDN-TMPUP )
      PRSUP = YPRESS(IU-1)
      PRSDN = YPRESS(IU)
      PRSDF = PRSDN-PRSUP
      PRSGD = PRSDF/TMPDF
C*    TMPDFX= TMPCLD-TMPUP
      IF (TROP1.AND.(.NOT.(ISISO))) THEN
         IF (PRSDN.GT.TROPRS) THEN
            PRSDN = TROPRS
            TMPDN = TROTMP
            TMPDF = ABS(TMPDN-TMPUP)
            PRSDF = PRSDN - PRSUP
            PRSGD = PRSDF/TMPDF
         END IF

         IF (PRSUP.LT.YPRESS(6)) THEN
            PRSUP = YPRESS(6)
            PRSDN = YPRESS(7)
            TMPDN = YPROFI(7)
            TMPDF = ABS(TMPDN-TMPUP)
            PRSDF = PRSDN - PRSUP
            PRSGD = PRSDF/TMPDF
            TMPCLD = (YPROFI(6) + YPROFI(7)) * 0.5
         END IF

         TMPDFX = ABS (TMPCLD-TMPDN)
         PRSCLD = PRSDN - PRSGD*TMPDFX
         IF (PRSCLD.LT.PRSUP) THEN
            PRSCLD = PRSDN
         END IF
         IF (PRSCLD.GT.TROPRS) THEN
            PRSCLD = TROPRS
         ELSE
            IF (ISISO) PRSCLD = TROPRS
         END IF
      ELSE
         IF (PRSUP.LT.TROPRS) THEN
            PRSUP = TROPRS
            TMPUP = TROTMP
            TMPDF = ABS (TMPDN-TMPUP)
            PRSDF = PRSDN-PRSUP
            PRSGD = PRSDF/TMPDF
         END IF
         TMPDFX= ABS( TMPCLD-TMPUP )
         PRSCLD= PRSUP + PRSGD*TMPDFX
      END IF
      GOTO 110
C
  101 CONTINUE
C....  SETS CLOUD TOP PRESSURE TO 115 MB WHEN IT IS LESS THAN 115 MB
C      (COMMENTED OUT FOR TESTING 9/3/2005  F. TILLEY)
C*    PRSCLD=115.0
      GOTO 110
C
  160 CONTINUE
C.... SET CLOUD TOP PRESSURE TO 1000 MB WHEN CLOUD TOP TEMPERATURE 
C     IS WARMER THAN THE TEMPERATURE AT 1000 MB LEVEL
      PRSCLD=1000.0
      TMPCLD = YPROFI(30)
C
  110 CONTINUE
      IF (PRSCLD.GE.SURPRS) THEN
         PRSCLD = SURPRS
      END IF

C-- IF THE CLOUD TOP PRESSURE IS LESS THAN THE MINIMIM PRESSURE, SET THE
C   CLOUD TOP PRESSURE TO THE MINIMUM AND SET THE CLOUD TOP TEMPERATURE
C   ACCORDINGLY..

      IF (PRSCLD.LT.MINPRESS) THEN
         PRSCLD = MINPRESS
         TMPCLD = YPROFI(MINLEV)
      END IF

      DPRESS = PRSCLD
ccft
      IF (DPRESS.GT.1000.0) THEN
         DPRESS = 1000.0
         TMPCLD = YPROFI(30)
      END IF

  300 CONTINUE
      RETURN
      END
