      SUBROUTINE LAPRTE(HEIGHT,TEMP,N,LPSRTE)
      IMPLICIT NONE
C
C***********************************************************************
C
C   NAME- LAPRTE
C
C   LANGUAGE- FORTRAN         TYPE- SUBROUTINE      COMPUTER- AIX
C
C   VERSION- 1.0      DATE- 11/14/07     PROGRAMMER-  FRANK TILLEY (IMSG)
C
C   FUNCTION- THIS SUBROUTINE PRODUCES THE LAPSE RATES AT THE NUMBER OF
C   GEOMETRIC HEIGHTS SPECIFIED IN THE CALLING ARGUMENT N.
C
C   CALLING SEQUENCE PARAMETERS-
C
C   NAME    TYPE  I/O    DESCRIPTION
C   ----    ----  ---    -----------
C   HEIGHT  REAL   I     VECTOR OF N GEOMETRIC HEIGHTS (KM).
C   TEMP    REAL   I     VECTOR OF N TEMPERATURE VALUES (K).
C   N       INT    I     NUMBER OF PRESSURE LEVELS.
C   LPSRTE  REAL   O     VECTOR OF N TO HOLD THE LAPSE RATES.  LAPSE
C                        RATE FOR A LEVEL WILL BE A MISSING VALUE IF
C                        IT COULD NOT BE COMPUTED DUE TO MISSING HEIGHT
C                        OR TEMPERATURE.
C
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'SPVAL'
C
C   LOCAL VARIABLES
C   NAME      TYPE  DESCRIPTION
C   ----      ----  -----------
C   TOP        INT  HIGHEST LEVEL CONTAINING NON-MISSING VALUES.
C   BOTTOM     INT  LOWEST LEVEL CONTAINING NON-MISSING VALUES.
C   I          INT  DO LOOP INDEX.
C   NUMER1    REAL  FIRST NUMERATOR IN THE EQUATION.
C   NUMER2    REAL  SECOND NUMERATOR IN THE EQUATION.
C   DENOM1    REAL  FIRST DENOMINATOR IN THE EQUATION.
C   DENOM2    REAL  SECOND DENOMINATOR IN THE EQUATION.
C   EQUAT     REAL  VECTOR OF THE THREE PARTS OF THE FORMULA.
C
C   SUBPROGRAMS CALLED-  NONE
C
C   EXIT STATES- NONE
C
C   ASSUMPTIONS- MISSING VALUES CAN EXIST IN THE HEIGHT OR TEMPERATURE
C                ARRAYS ONLY AT TOP OR BOTTOM LEVELS.  ALL VALUES
C                BETWEEN THE LOWEST AND HIGHEST NON-MISSING VALUES WILL
C                BE NON-MISSING.
C
C   COMMENTS- THE REFERENCE FOR THE DIFFERENTIATION FORMULA USED IS
C             ELEMENTARY THEORY AND APPLICATION OF NUMERICAL ANALYSIS
C             BY MOURSUND & DURIS COPYRIGHT 1967.
C
C***********************************************************************
      INTEGER     N
      REAL        HEIGHT(N), LPSRTE(N), TEMP(N)
C
      INTEGER  I, TOP, BOTTOM
C
C     INITIALIZE LAPSE RATE TO MISSING VALUES.
C
      DO I = 1, N
         LPSRTE(I) = R4SPV
      END DO
C
C     FIND THE HIGHEST AND LOWEST LEVELS OF NON-MISSING VALUES OF
C     TEMPERATURE AND HEIGHT.
C
      TOP = I4SPV
      BOTTOM = I4SPV
C
      DO I = 1, N
         IF (HEIGHT(I).NE.R4SPV.AND.TEMP(I).NE.R4SPV) THEN
            TOP = I
            GOTO 30
         ENDIF
      END DO
C
   30 IF (TOP .EQ. I4SPV) RETURN
C
      DO I = N, 1, -1
         IF (HEIGHT(I).NE.R4SPV.AND.TEMP(I).NE.R4SPV) THEN
            BOTTOM = I
            GOTO 50
         ENDIF
      END DO
C
C     THREE LEVELS OF NON-MISSING VALUES ARE NEEDED TO COMPUTE THE
C     LAPSE RATES.
C
   50 IF ((BOTTOM - TOP) .LT. 2) RETURN
C
      DO I = TOP, BOTTOM-1
         LPSRTE(I) = -(TEMP(I)-TEMP(I+1)) / (HEIGHT(I)-HEIGHT(I+1))
      END DO
      
      RETURN
      END
