      SUBROUTINE CHKSUP2 (LSTLVL,PRESS,TEMP,FSUPER,SUPLVL,SMODLV)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- CHKSUP2
C
C   LANGUAGE- FORTRAN  TYPE- SUBROUTINE
C
C   VERSION- 1.0    DATE-  1/10/89   PROGRAMMER- SOO O. LEE (SMSRC)
C   VERSION- 2.0    DATE-  1/30/94   PROGRAMMER- ALEX POZDNYAK (SMSRC)
C   VERSION- 2.1    DATE-  8/29/95   PROGRAMMER- FRANK TILLEY (SMSRC)
C   VERSION- 2.2    DATE-  5/14/96   PROGRAMMER- ALEX POZDNYAK (FDC)
C   VERSION- 3.0    DATE-  7/15/97   PROGRAMMER- MIKE PETTEY (HSTX)
C   VERSION- 3.1    DATE-  9/22/98   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C   VERSION- 3.2    DATE- 11/16/98   PROGRAMMER- A. ALLEGRINO (RAYTHEON)
C   VERSION- 3.3    DATE-  9/18/02   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 3.4    DATE- 12/03/02   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 3.5    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C   VERSION- 3.6    DATE- 10/13/04   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 4.0    DATE- 07/22/13   PROGRAMMER- FRANK TILLEY (IMSG)
C
C   FUNCTION- THIS ROUTINE CHECKS FOR SUPERADIABATIC LAYERS IN GIVEN
C             PRESSURE AND TEMPERATURE DATA. THE SUPERADIABATIC
C             FLAG IS SET IF POTENTIAL TEMPERATURE DOES NOT INCREASE
C             MONOTONICALLY WITH HEIGHT AND THE SUPERADIABATIC
C             CONDITION EXCEEDS THE FAILURE THRESHOLD. THIS ROUTINE
C             WORKS WITH TEMPERATURES IN DEGREES KELVIN.
C
C      VERSION 2.0: ADDED INCLUDE MEMBER SPVAL.  FOR COMPUTATION OF
C                   POT0, COMPUTED FOR FIRST NON-MISSING LEVEL OF
C                   TEMPERATURE. DO LOOP 10 LOOPS FROM FIRST NON-
C                   MISSING "LEVEL + 1" TO "LSTLVL". ADDED A CHECK
C                   FOR MISSING TEMPERATURE WHEN COMPUTING "TPOT"
C                   AND SKIP LEVEL IF MISSING VALUE IS FOUND.
C
C      VERSION 2.1: UPDATED FOR UNIFIED RAOBS.  ADDED CHECK FOR
C                   MISSING PRESSURE IN DETERMINING FIRST GOOD LEVEL.
C
C      VERSION 2.2: FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 TO
C                   INTEGER, ALL L*4 TO LOGICAL, AND R*4 TO REAL
C
C      VERSION 3.0: CODE PUT IN TO REQUIRE A PRESSURE DIFFERENCE OF
C                   25 MB OR MORE BETWEEN TWO PRESSURE LEVELS BEFORE
C                   A FAILURE OF THE SUPERADIABATIC TEST IS OBTAINED.
C                   ALSO, SOFT-CODED THE ADDITION TO TPOT.  THE
C                   VARIABLE SUPADD IS STORED IN THE COMMON AREA
C                   FLGCOM AND DEFAULTS TO 1.0 IF IT IS NOT SET.
C
C      VERSION 3.1: CAPTURED THE UPPER LEVEL TO PASS BACK TO RETOD1
C                   TO PLACE ON THE ODF AND THE REQUIRED PRESSURE
C                   DIFFERENCE WAS CHANGED FROM 25 MB TO 15 MB
C      VERSION 3.2: SET THE VALUE OF SUPADD TO 0.5                
C      VERSION 3.3: CHANGED INPUT TEMPERATURE ARRAY FROM CELSIUS
C                   TO KELVIN AND PRESSURE LEVEL ORDER TO ATOVS
C                   PRESSURE LEVEL ORDER. FUNCTION THETA, WHICH
C                   CALCULATES POTENTIAL TEMPERATURE USING A  
C                   REFERENCE PRESSURE OF 1000 MB, WAS REPLACED
C                   WITH CODE THAT CALCULATES POTENTIAL TEMPERATURE
C                   BETWEEN ADJACENT LAYERS. (PR#64)
C      VERSION 3.4  ADDED A CHECK FOR MISSING PRESSURES AND FOR
C                   A MINIMUM PRESSURE DELTA OF 15 MB BEFORE
C                   CALCULATING POTENTIAL TEMPERATURE TO AVOID
C                   FLOATING POINT ERRORS. ADDED AN
C                   ARGUMENT (SMODLV) TO THE CALLING ARGUMENTS
C                   FOR SURFACE MODEL LEVEL. REWROTE THE SUBROUTINE
C                   TO CORRECT MULTIPLE LAYERS BETWEEN THE SURFACE
C                   AND 70 MB.  (WR #215)
C      VERSION 3.5  Replaced R4SPV equivalence test with ISR4SPV 
C                   function
C      VERSION 3.6  REMOVED ALL TEMPERATURE CORRECTIONS (WR #215).
C      VERSION 4.0  ADAPTED FROM CHKSUP TO USE CLOUD-CLEARED RETRIEVALS.
C
C
C   CALLING PARAMETERS-
C     NAME    TYPE   I/O   DESCRIPTION
C     ----    ----   ---   -----------
C     LSTLVL   INT    I    THE NUMBER OF LEVELS
C
C     PRESS   REAL    I    ATMOSPHERIC PRESSURES (MB)
C
C     TEMP    REAL    I    ATMOSPHERIC TEMPERATURES (K)
C
C     FSUPER   LOG    O    FLAG SET TO TRUE IF A SUPERADIABATIC
C                          LAYER IS FOUND
C
C     SUPLVL   INT    O    SUPERADIABATIC LEVEL
C    
C     SMODLV   INT    I    SURFACE MODEL LEVEL
C
C
C   COMMON AREAS-
      INCLUDE 'RMPHSCON'
      INCLUDE 'FLGCOM'
      INCLUDE 'SPVAL'
C
C
C   LOCAL VARIABLES-
C     NAME    TYPE   DESCRIPTION
C     ----    ----   -----------
C     TPOT    REAL   POTENTIAL TEMPERATURE AT THE LEVEL BEING
C                    CHECKED.
C
C     LVL      INT   COUNTER FOR DO LOOP
C
C     I        INT   COUNTER FOR DO LOOP
C
C     NONMIS   INT   THE INDEX OF THE SURFACE-MOST LEVEL
C                    NON-MISSING PRESSURE AND TEMPERATURE
C
C     TDIFF   REAL   THE DIFFERENCE BETWEEN THE POTENTIAL
C                    TEMPERATURE AT A GIVEN LEVEL AND THE
C                    RETRIEVED TEMPERATURE AT THE LEVEL JUST
C                    LOWER IN THE ATMOSPHERE.
C 
C     TLOW    REAL   TEMPERATURE IN KELVIN AT THE 
C                    PREVIOUS PRESSURE LEVEL.
C
C
C   FUNCTIONS USED- NONE
C
C
C   EXIT STATES- NONE
C
C   COMMENTS-  1) THE FLAG, FSUPER, IS SET TRUE WHEN THE POTENTIAL
C                 TEMPERATURE DOES NOT INCREASE MONOTONICALLY WITH
C                 HEIGHT BETWEEN ADJACENT LAYERS.
C              3) THIS ROUTINE EXPECTS THE TEMPERATURE AND PRESSURE
C                 ARRAYS TO BE ORDERED FROM THE POINT OF VIEW OF A
C                 SATELLITE LOOKING DOWN THROUGH THE ATMOSPHERE
C
C**********************************************************************
C
      INTEGER    LSTLVL,SUPLVL,SMODLV
      REAL       PRESS(*),TEMP(*)
      LOGICAL    FSUPER

      REAL       TPOT, TLOW, TDIFF
      INTEGER    LVL, I, NONMIS
      LOGICAL    ISR4SPV

      FSUPER = .FALSE.
      SUPLVL = I4SPV
      
C-- FIND THE FIRST LEVEL WHERE BOTH TEMPERATURE AND PRESSURE
C   ARE PRESENT. DO NOT CHECK BELOW TOVS LEVEL 40 (1000 MB).
C   START THE CHECK AT THE SURFACE AND WORK UPWARD. CALCULATE
C   THE POTENTIAL TEMPERATURE AT THE SELECTED LEVEL.

      NONMIS = 0
      LVL = SMODLV
      IF (SMODLV.GT.40) LVL = 40
      DO I = LVL, 1, -1
         IF ((.NOT.(ISR4SPV(TEMP(I+63)))) .AND.
     +       (.NOT.(ISR4SPV(PRESS(I))))) THEN
            NONMIS = I
            TLOW = TEMP(I+63)
            GOTO 20
         END IF
      END DO

   20 CONTINUE

C-- IF NO TEMPERATURE IS FOUND, RETURN TO THE CALLING ROUTINE

      IF (NONMIS.EQ.0) RETURN

C-- IF THE PRESSURE FOR THE FIRST AVAILABLE TEMPERATURE IS LESS
C   THAN 85 MB (TOVS LEVEL 19), RETURN TO THE CALLING ROUTINE

      IF (NONMIS.LT.19) GO TO 1000

C-----------
C STEP 1
C-----------      

C-- CHECK THE SURFACE LAYER FOR SUPER ADIABATIC CONDITIONS.
C   IF THE LAYER IS AT ALL SUPER ADIABATIC, SET THE SUPER
C   ADIABATIC LEVEL TO THE SURFACE LEVEL. IF THE LAYER
C   IS SUPER ADIABATIC BY MORE THAN "SUPADD", FLAG THE
C   RETRIEVAL AS SUPER ADIABATIC AND RETURN TO THE CALLING
C   ROUTINE.
C   
    
      TPOT = TEMP((NONMIS-1)+63) * (PRESS(NONMIS)/PRESS(NONMIS-1))**(R/CP)
      TDIFF = TLOW - TPOT
      IF (TDIFF.GT.0.0) THEN
         SUPLVL = NONMIS
         IF (TDIFF.GT.SUPADD) THEN
            FSUPER = .TRUE.
            GO TO 1000
         END IF
      END IF  
      TLOW = TEMP((NONMIS -1)+63)

C-----------
C STEP 2
C-----------      

C-- CHECK THE LAYERS BETWEEN THE SURFACE LAYER AND 70 MB FOR
C   SUPER ADIABATIC CONDITIONS. IF A SUPER ADIABATIC CONDITION
C   IS FOUND AND THE SUPER ADIABATIC LEVEL VALUE IS STILL
C   A MISSING VALUE, SET THE SUPER ADIABATIC LEVEL INDICATOR
C   THE LEVEL AT THE LOWER BOUND OF THE SUPER ADIABATIC LAYER.
C   IF THE LAYER IS SUPER ADIABATIC BY MORE THAN "SUPADD", FLAG
C   THE RETRIEVAL AS SUPER ADIABATIC AND RETURN TO THE CALLING
C   ROUTINE.

      DO LVL = (NONMIS-2), 18, -1
         IF (ISR4SPV(PRESS(LVL)).OR. ISR4SPV(PRESS(LVL+1))) RETURN
         IF (ISR4SPV(TEMP(LVL+63)).OR. ISR4SPV(TLOW)) RETURN

C-- COMPUTE THE POTENTIAL TEMPERATURE FOR THE UPPER BOUNDARY LEVEL
C   OF THE LAYER.

         TPOT = TEMP(LVL+63) * (PRESS(LVL+1)/PRESS(LVL))**(R/CP)
         TDIFF = TLOW - TPOT
         IF (TDIFF.GT.0.0) THEN
            IF (SUPLVL.EQ.I4SPV) THEN
               SUPLVL = LVL+1
            END IF
            IF (TDIFF.GT.SUPADD) THEN
               FSUPER = .TRUE.
               GO TO 1000
            END IF
         END IF
         TLOW = TEMP(LVL+63)
      END DO

 1000 CONTINUE

      RETURN
      END
