      SUBROUTINE CLDTEM(NCH1,NCH2,RADT,BTT,TMPTRO,CLDTMP)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- CLDTEM
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- 06/03/96   PROGRAMMER- LEAH W. CASEY (HSTX)
C   VERSION- 3.0    DATE- 11/13/97   PROGRAMMER- BRIAN BARIL (HSTX)
C   VERSION- 3.1    DATE- 12/02/04   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 3.2    DATE- 04/21/06   PROGRAMMER- FRANK TILLEY (IMSG)
C
C   FUNCTION-*$ED23TG* PROGRAM CALCULATES CLOUD TOP TEMP. FOR A
C        MINIBOX SPOT.
C   VERSION- 1.2 MODS FOR ATTENUATION ABOVE THE CLOUDS UPGRADE.
C   VERSION- 3.0 MODIFICATIONS FOR INCLUSION INTO ATOVS.
C   VERSION- 3.1 EXPLICITLY DECLARED VARIABLES.
C   VERSION- 3.2 REMOVED UNUSED CALLING ARGUMENT 'DELR'
C
C   CALLING ARGUMENTS-
C      NAME                    TYPE I/O FUNCTION
C      ----                    ---- --- --------
C      NCH1                    INT  I   FIRST CHANNEL OF CHANNEL PAIR.
C      NCH2                    INT  I   SECOND CHANNEL OF CHANNEL PAIR.
C      RADT(4)                REAL  I   MEASURED AND PREDICTED CLEAR
C                                       RADIANCES FOR THE CHANNEL PAIR.
C      BTT(4)                 REAL  I   MEASURED AND PREDICTED CLEAR
C                                       BRIGHTNESS TEMPERATURES FOR
C                                       THE CHANNEL PAIR.
C      TMPTRO                 REAL  I   TROPOPAUSE TEMPERATURE.
C      CLDTMP                 REAL  O   CLOUD TOP TEMPERATURE AT THE
C                                       SPOT.
C
C   COMMON AREAS- NONE.
C
C   LOCAL VARIABLES-
C      NAME        TYPE     FUNCTION
C      ----        ----     --------
C      S           REAL     FUNCTION TO COMPUTE A SLOPE FOR AN
C                           INTERIM CLOUD TOP TEMPERATURE.
C      TN          REAL     BRIGHTNESS TEMPERATURE WHICH EVENTUALLY
C                           BECOMES THE CALCULATED CLOUD TOP
C                           TEMPERATURE.
C      CLOPE       REAL     FIXED SLOPE DEFINED AS:
C                           PREDICTED MINUS MEASURED RADIANCE (NCH2)
C                                    DIVIDED BY
C                           PREDICTED MINUS MEASURED RADIANCE (NCH1)                           
C      TNO         REAL     INTERIM CLOUD TOP TEMPERATURE VALUE.
C
C   SUBROUTINES CALLED- NONE.
C
C   FUNCTIONS USED- PLKRAD
C
C**********************************************************************
C
C
      INTEGER  NCH1,NCH2

      REAL   CLDTMP,RADT(4),BTT(4),PLKRAD,TMPTRO
      REAL   S, TN, CLOPE, TNO

C
C   DECLARE FUNCTION S, WHICH COMPUTES A SLOPE USING THE
C   PREDICTED CLEAR RADIANCES AT THE LOWER AND UPPER PEAKING
C   CHANNELS AND THE RADIANCES FOR AN INTERIM CLOUD TOP
C   TEMPERATURE COMPUTED USING THE CHANNEL FREQUENCIES OF THE
C   LOWER AND UPPER PEAKING CHANNELS, RESPECTIVELY.
C
c   The Slope for the temperature is:
C       (Radiance for the lower peaking predicted clear minus
C        the radiance for the temperature using the lower peaking
C        channel frequency) divided by:
C       (Radiance for the upper peaking predicted clear minus
C        the radiance for the temperature using the upper peaking
C        channel frequency)
C
      S(TN) = (RADT(4)-PLKRAD(TN,NCH2))/(PLKRAD(BTT(3),18)-PLKRAD(TN,18))

C   CALCULATE THE SLOPE USING THE PARAMETERS PASSED IN AS ARGUMENTS.
C
      CLOPE = (RADT(4)-RADT(2))/(PLKRAD(BTT(3),18)-PLKRAD(BTT(1),18))
C
C   START BY SETTING THE CLOUD TOP TEMPERATURE TO THE MEASURED
C   BRIGHTNESS TEMPERATURE AT THE LOWER PEAKING CHANNEL.
C
      TN = BTT(2)
C
C   IF THE SLOPE USING THE LOWER PEAKING OBSERVED BRIGHTNESS TEMPERATURE
C   MATCHES THE ORIGINAL SLOPE, NO CHANGE TO THE CLOUD TOP TEMPERATURE
C   IS NEEDED.
C
      IF (ABS(S(TN)-CLOPE).EQ.0.0) THEN
         CLDTMP = TN
         GO TO 700
      END IF
C
C   COMPARE SLOPES TO DETERMINE WHETHER THE INTERIM CLOUD TOP
C   TEMPERATURE NEEDS TO BE WARMED OR COOLED.
C
      IF (S(TN).GT.CLOPE) THEN
   5     CONTINUE
         TNO = TN
         TN = TN - 1.0
         IF (TN.LT.TMPTRO) THEN
            CLDTMP = TMPTRO
            GO TO 700
         END IF 

         IF (S(TN).GT.CLOPE) GO TO 5

         CLDTMP = TNO - (S(TNO)-CLOPE)/(S(TNO)-S(TN))
         GO TO 700
      ELSE
   10    CONTINUE
         TNO = TN
         TN = TN + 1.0
         IF (NCH1.EQ.2) THEN
            IF (TN.GT.BTT(1)) THEN
               CLDTMP = BTT(1)
               GO TO 700
            END IF
         ELSE
            IF (TN.GT.BTT(4)) THEN
               CLDTMP = BTT(4)
               GO TO 700
            END IF
         END IF

         IF (S(TN).LT.CLOPE) GO TO 10

         CLDTMP = TNO+(S(TNO)-CLOPE)/(S(TNO)-S(TN))
         GO TO 700
      END IF 
C
  700 CONTINUE
C
C  RETURN THE CLOUD TOP TEMPERATURE
C
      RETURN
      END
