      SUBROUTINE RTVHGT2(RETTMP,PRESS,NLVLS,RETWTV,NWVLVL,GPTHGT,GMTHGT,
     1                  LYRTHK)
      IMPLICIT NONE
C***********************************************************************
C
C   NAME- RTVHGT
C
C   LANGUAGE- VS FORTRAN    TYPE- SUBROUTINE      COMPUTER- AIX
C
C   VERSION- 2.0    DATE- 1/9/95    PROGRAMMER- CARLA NEIGH, SMSRC
C   VERSION- 3.0    DATE- 05/14/96   PROGRAMMER- ALEX POZDNYAK, FDC
C      FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C      INTEGER AND ALL R*4 DECLARATIONS TO REAL.
C   VERSION- 3.1    DATE- 02/24/99  PROGRAMMER- BRIAN BARIL (ITSS)
C      FIXED AN ERROR THAT WAS CAUSING INCORRECT SCALING ABOVE 100MB.
C   VERSION- 3.2    DATE- 10/16/06  PROGRAMMER- FRANK TILLEY (IMSG)
C      REPLACED CALLS TO FUNCTIONS W AND DWPT WITH TDEW2MR AND
C      DEWPOINT, RESPECTIVELY. REPLACED FUNCTION Z WITH FUNCTION
C      THICKNESS.
C  
C   FUNCTION-  THIS SUBROUTINE COMPUTES THE GEOPOTENTIAL HEIGHTS,
C              GEOMETRIC HEIGHTS AND LAYER THICKNESSES FROM THE
C              RETRIEVED VALUES OF TEMPERATURE AND WATER VAPOR
C              MIXING RATIO.  VALUES FOR GEOPOTENTIAL HEIGHTS AT
C              PRESSURE LEVELS FROM 10MB AND BELOW AND VALUES FOR
C              LAYER THICKNESSES AT ALL PRESSURE LEVELS ARE RETURNED
C              IN METERS.  VALUES FOR GEOPOTENTIAL HEIGHTS AT PRESSURE
C              LEVELS ABOVE 10MB ARE RETURNED IN DECAMETERS.  GEOMETRIC
C              HEIGHTS ARE RETURNED IN KILOMETERS.
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function
C
C   CALLING SEQUENCE PARAMETERS-
C   NAME    TYPE   I/O   DESCRIPTION
C   ----    ----   ---   -----------
C   RETTMP  REAL    I    ARRAY (DIM NLVLS) CONTAINING TEMPERATURES
C                        IN KELVIN.
C   PRESS   REAL    I    ARRAY (DIM NLVLS) CONTAINING PRESSURE VALUES
C                        (IN MB) CORRESPONDING TO EACH LEVEL.
C   NLVLS    INT    I    NUMBER OF LEVELS OF TEMPERATURE AND PRESSURE.
C   RETWTV  REAL    I    ARRAY (DIM NWVLVL) CONTAINING WATER VAPOR
C                        MIXING RATIO VALUES (G/KG).  IT IS ASSUMED THAT
C                        NWVLVL IS <= NLVLS AND THE WATER VAPOR MIXING
C                        RATIO VALUES CORRESPOND TO THE BOTTOM NWVLVL
C                        LEVELS IN THE PRESS ARRAY.
C   NWVLVL   INT    I    NUMBER OF LEVELS OF WATER VAPOR MIXING RATIO.
C   GPTHGT   REAL   O    ARRAY (DIM NLVLS) OF GEOPOTENTIAL HEIGHTS.
C   GMTHGT   REAL   O    ARRAY (DIM NLVLS) OF GEOMETRIC HEIGHTS.
C   LYRTHK   REAL   O    ARRAY (DIM NLVLS) OF LAYER THICKNESSES.
C
C   COMMON AREA PARAMETERS-  SPVAL
C
C   LOCAL PARAMETERS-
C   NAME    TYPE   DESCRIPTION
C   ----    ----   -----------
C   A       REAL   CONSTANT EQUAL TO RADIUS OF THE EARTH (M).
C   C2      REAL   CONSTANT EQUAL TO R/(2.*G) WHERE R IS THE GAS
C                  CONSTANT FOR DRY AIR (287 KG/JOULE/DEG K) AND G
C                  IS THE ACCELERATION DUE TO GRAVITY (9.8 M/S**2).
C   LVL     INT    DO LOOP COUNTER FOR LEVELS.
C   BOTTOM  INT    LOWEST LEVEL CONTAINING A NON-MISSING VALUE FOR
C                  TEMPERATURE.
C   TEMPC   REAL   ARRAY (DIM 2) CONTAINING TEMPERATURE IN CELSIUS AT
C                  THE TOP AND BOTTOM OF THE LAYER.
C   SMXTOP  REAL   SATURATION MIXING RATIO AT BOTTOM OF THE LAYER.
C   SMXBOT  REAL   SATURATION MIXING RATIO AT TOP OF THE LAYER.
C   RHTOP   REAL   RELATIVE HUMIDITY AT TOP OF THE LAYER.
C   RHBOT   REAL   RELATIVE HUMIDITY AT BOTTOM OF THE LAYER.
C   DEWPT   REAL   ARRAY (DIM 2) CONTAINING THE DEWPOINT TEMPERATURE AT
C                  THE TOP AND BOTTOM OF THE LAYER.
C   PRSARY  REAL   ARRAY (DIM 2) CONTAINING PRESSURE AT THE TOP AND
C                  BOTTOM OF THE LAYER.
C
C   FUNCTIONS USED-  DEWPOINT, TDEW2MR, ALOG, THICKNESS
C
C   EXIT STATES- NONE
C
C***********************************************************************
C
      INCLUDE 'SPVAL'
C
      INTEGER   NLVLS, NWVLVL
      REAL      RETTMP(NLVLS), PRESS(NLVLS), RETWTV(NWVLVL)
      REAL      GPTHGT(NLVLS), GMTHGT(NLVLS), LYRTHK(NLVLS)
C
      INTEGER   LVL, BOTTOM
      REAL      TEMPC(2), SMXTOP, SMXBOT, RHTOP, RHBOT, DEWPT(2), C2, A
      REAL      PRSARY(2), TDEW2MR, DEWPOINT, THICKNESS
      LOGICAL   ISR4SPV
C
      PARAMETER (C2=14.64285)
      PARAMETER (A = 6356.776E3)
C
C  INITIALIZE OUTPUT TO MISSING VALUES.
C

      DO 10 LVL = 1, NLVLS
         GPTHGT(LVL) = R4SPV
         GMTHGT(LVL) = R4SPV
         LYRTHK(LVL) = R4SPV
   10 CONTINUE
C
C  FIND THE LOWEST LEVEL CONTAINING A NON-MISSING TEMPERATURE.
C
      BOTTOM = I4SPV
      DO 20 LVL = NLVLS, 1, -1
         IF (.NOT. ISR4SPV(RETTMP(LVL))) THEN
            BOTTOM = LVL
            GOTO 30
         ENDIF
   20 CONTINUE
C
   30 IF (BOTTOM .EQ. I4SPV) RETURN

      if (BOTTOM.GT.40) BOTTOM = 40
C
C  BOTTOM LEVEL HEIGHTS ARE ASSUMED TO BE ZERO.
C
      GPTHGT(BOTTOM) = 0.0
      GMTHGT(BOTTOM) = 0.0
      LYRTHK(BOTTOM) = 0.0
C
C  COMPUTE HEIGHTS FOR REMAINING LEVELS.
C
      DO 60 LVL = BOTTOM, 2, -1
C
C     FIRST, THICKNESS WILL BE COMPUTED FOR THE LAYER.  LAYER THICKNESS
C     IS COMPUTED DIFFERENTLY FOR LEVELS WITH WATER VAPOR VALUES AND
C     LEVELS WITHOUT WATER VAPOR VALUES.
C
         TEMPC(1) = RETTMP(LVL)
         TEMPC(2) = RETTMP(LVL-1)
         IF ((ISR4SPV(TEMPC(1))).OR.(ISR4SPV(TEMPC(2)))) GOTO 60
C
         IF (LVL .GT. ((NLVLS-NWVLVL)+1)) THEN
C
            TEMPC(1) = TEMPC(1) - 273.16
            TEMPC(2) = TEMPC(2) - 273.16
C
C        FUNCTION TDEW2MR IS USED TO COMPUTE THE SATURATION MIXING RATIO.
C
            SMXBOT = TDEW2MR(PRESS(LVL),TEMPC(1))
            SMXTOP = TDEW2MR(PRESS(LVL-1),TEMPC(2))
C
            RHBOT =  (RETWTV((LVL-NLVLS)+NWVLVL)/SMXBOT) * 100.0
            RHTOP =  (RETWTV((LVL-NLVLS)+NWVLVL-1)/SMXTOP) * 100.0
C
C        FUNCTION DEWPOINT IS USED TO COMPUTE THE DEWPOINT TEMPERATURE.
C
            DEWPT(1) = DEWPOINT(TEMPC(1),RHBOT)
            DEWPT(2) = DEWPOINT(TEMPC(2),RHTOP)
C
C        FUNCTION THICKNESS COMPUTES THE LAYER THICKNESS.
C
            PRSARY(1) = PRESS(LVL)
            PRSARY(2) = PRESS(LVL-1)
C
            LYRTHK(LVL-1) = THICKNESS(PRSARY(2),PRSARY,TEMPC,DEWPT,2)
C
         ELSE
C
            LYRTHK(LVL-1) = C2 * (RETTMP(LVL) + RETTMP(LVL-1)) *
     1                      ALOG(PRESS(LVL)/PRESS(LVL-1))
C
         ENDIF
C
C     GEOPOTENTIAL AND GEOMETRIC HEIGHT CAN BE COMPUTED FROM THE
C     LAYER THICKNESS.  GEOPOTENTIAL HEIGHT VALUES ABOVE 100 MB ARE
C     REPRESENTED IN DECAMETERS, WHILE OTHER ALL OTHER LEVELS ARE
C     REPRESENTED IN METERS.  GEOMETRIC HEIGHT VALUES ARE IN KM.
C
         IF (PRESS(LVL-1) .GT. 100.0) THEN
            GPTHGT(LVL-1) = GPTHGT(LVL) + LYRTHK(LVL-1)
            GMTHGT(LVL-1) = ((GPTHGT(LVL-1)*A)/(A-GPTHGT(LVL-1)))/1000.0
         ELSE
            IF (PRESS(LVL-1) .EQ. 100.0) THEN
               GPTHGT(LVL-1) = (GPTHGT(LVL) + LYRTHK(LVL-1)) / 10.0
            ELSE
               GPTHGT(LVL-1) = GPTHGT(LVL) + (LYRTHK(LVL-1) / 10.0)
            ENDIF
            GMTHGT(LVL-1) = ((GPTHGT(LVL-1)*10.0*A)/
     1                      (A-10.0*GPTHGT(LVL-1)))/1000.0
         ENDIF
   60 CONTINUE
C
      RETURN
      END
