      SUBROUTINE STABLT2(CONST,HI8NMC,HI8TOV,DELTAS,DELTAL,DELTAH)
      IMPLICIT NONE
C
C
C********************************************************************
C
C   NAME- STABLT2
C
C   LANGUAGE- FORTRAN-77       TYPE- SUBROUTINE      COMPUTER- CRAY
C
C   VERSION- 1.0    DATE- 01/19/95   PROGRAMMER- LEAH CASEY (HUGHES STX)
C   VERSION- 2.0    DATE- 07/20/95   PROGRAMMER- ALEX POZDNYAK (SMSRC)
C   VERSION- 3.0    DATE- 05/14/96   PROGRAMMER- ALEX POZDNYAK, FDC
C      FOR REHOSTING TO THE CRAY, CHANGED ALL R*4 DECLARATIONS TO
C      REAL.
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function
C
C   FUNCTION- THIS ROUTINE COMPUTES THE STABILITY INDICATOR BETWEEN A
C             RETRIEVAL LOCATION AND THE CLOSEST FORECAST.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE   TYPE  I/O  DEFINITION
C     ---------  ----  ---  ----------
C     CONST      REAL   I   ARRAY (DIM 2) OF CONSTANTS THAT RELATE INPUT
C                           PRESSURE LEVELS.
C     HI8NMC     REAL   I   ARRAY (DIM 4) OF NMC GEOPOTENTIAL HEIGHTS
C                           INTERPOLATED TO THE RETRIEVAL LOCATION.
C     HI8TOV     REAL   I   ARRAY (DIM 4) OF GEOPOTENTIAL HEIGHTS FOR
C                           4 TOVS PRESSURE LEVELS.
C     DELTAS     REAL   O   VARIABLE CONTAINING STABILITY DEPARTURE.
C     DELTAL     REAL   O   VARIABLE CONTAINING LOWER DEPARTURE.
C     DELTAH     REAL   O   VARIABLE CONTAINING UPPER DEPARTURE.
C
C
C   COMMON AREA PARAMETERS- SPVAL
C
C
C   LOCAL PARAMETERS-
C     VARIABLE  TYPE  DESCRIPTION
C     --------  ----  -----------
C     Z         REAL  GEOPOTENTIAL HEIGHT DIFFERENCE BETWEEN TWO
C                     LEVELS.
C     ZLOWS     REAL  MEAN LAYER VIRTUAL TEMPERATURE BETWEEN TWO
C                     LOWER LEVELS.
C     ZHIGHS    REAL  MEAN LAYER VIRTUAL TEMPERATURE BETWEEN TWO
C                     HIGHER LEVELS.
C     ZLOWN     REAL  MEAN LAYER VIRTUAL TEMPERATURE BETWEEN TWO
C                     LOWER LEVELS COMPUTED FROM NMC FORECAST.
C     ZHIGHN    REAL  MEAN LAYER VIRTUAL TEMPERATURE BETWEEN TWO
C                     HIGHER LEVELS COMPUTED FROM NMC FORECAST.
C     SSAT      REAL  STABILITY VALUE COMPUTED FROM RETRIEVAL.
C     SNMC      REAL  STABILITY VALUE COMPUTED FROM NMC FORECAST.
C
C   SUBROUTINES CALLED- NONE
C
C   FUNCTIONS USED- NONE
C
C   EXIT STATES- NONE
C
C********************************************************************
C
      REAL CONST(2), HI8NMC(4), HI8TOV(4), DELTAS, DELTAL, DELTAH
C
      REAL Z, ZLOWS, ZHIGHS, ZLOWN, ZHIGHN, SSAT, SNMC
      LOGICAL   ISR4SPV
C
      INCLUDE 'SPVAL'
C
C     INITIALIZE VARIABLES TO A MISSING VALUE.
C
      DELTAS = R4SPV
      DELTAL = R4SPV
      DELTAH = R4SPV

      print *,'STABLT2'
      print *,'HI8NMC(1) ',HI8NMC(1),' HI8NMC(2) ',HI8NMC(2),' HI8NMC(3) ',
     +         HI8NMC(3),' HI8NMC(4) ',HI8NMC(4)
      print *,'HI8TOV(1) ',HI8TOV(1),' HI8TOV(2) ',HI8TOV(2),' HI8TOV(3) ',
     +         HI8TOV(3),' HI8TOV(4) ',HI8TOV(4)
C
C     CHECK FOR MISSING VALUES IN THE ARRAYS HI8NMC AND HI8TOV.
C
      IF(.NOT. ISR4SPV(HI8NMC(1)) .AND..NOT. ISR4SPV(HI8NMC(2)) .AND.
     1   .NOT. ISR4SPV(HI8NMC(3)) .AND..NOT. ISR4SPV(HI8NMC(4)) .AND.
     2   .NOT. ISR4SPV(HI8TOV(1)) .AND..NOT. ISR4SPV(HI8TOV(2)) .AND.
     3   .NOT. ISR4SPV(HI8TOV(3)) .AND..NOT. ISR4SPV(HI8TOV(4))) THEN
C
C        COMPUTE THE GEOPOTENTIAL HEIGHT DIFFERENCE BETWEEN TWO LOWER
C        LEVELS.
C
         Z = HI8TOV(4) - HI8TOV(3)
C
C        COMPUTE MEAN LAYER VIRTUAL TEMPERATURE BETWEEN TWO LOWER LEVELS
C        FROM RETRIEVAL.
C
         ZLOWS = Z * CONST(2)
C
C        COMPUTE THE GEOPOTENTIAL HEIGHT DIFFERENCE BETWEEN TWO HIGHER
C        LEVELS.
C
         Z = HI8TOV(2) - HI8TOV(1)
C
C        COMPUTE MEAN LAYER VIRTUAL TEMPERATURE BETWEEN TWO HIGHER LEVEL
C        FROM RETRIEVAL.
C
         ZHIGHS = Z * CONST(1)
C
C        COMPUTE STABILITY VALUE FROM RETRIEVAL.
C
         SSAT = ZLOWS - ZHIGHS
C
C        COMPUTE THE GEOPOTENTIAL HEIGHT DIFFERENCE BETWEEN TWO LOWER
C        LEVELS.
C
         Z = HI8NMC(4) - HI8NMC(3)
C
C        COMPUTE MEAN LAYER VIRTUAL TEMPERATURE BETWEEN TWO LOWER LEVELS
C        FROM NMC FORECAST.
C
         ZLOWN = Z * CONST(2)
C
C        COMPUTE THE GEOPOTENTIAL HEIGHT DIFFERENCE BETWEEN TWO HIGHER
C        LEVELS FROM NMC FORECAST.
C
         Z = HI8NMC(2) - HI8NMC(1)
C
C        COMPUTE MEAN LAYER VIRTUAL TEMPERATURE BETWEEN TWO HIGHER LEVEL
C        FROM NMC FORECAST.
C
         ZHIGHN = Z * CONST(1)
C
C        COMPUTE STABILITY VALUE FROM NMC FORECAST.
C
         SNMC = ZLOWN - ZHIGHN
C
C        COMPUTE STABILITY DEPARTURE VALUE.
C
         DELTAS = SSAT - SNMC
C
C        COMPUTE LOWER DEPARTURE VALUE.
C
         DELTAL = ZLOWS - ZLOWN
C
C        COMPUTE UPPER DEPARTURE VALUE.
C
         DELTAH = ZHIGHS - ZHIGHN
      ENDIF
C
C     RETURN TO CALLING ROUTINE.
C
      RETURN
      END
