      SUBROUTINE CLDTEMP(NCH1,NCH2,RADT,BTT,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   UPPER PEAKING CHANNEL OF CHANNEL PAIR.
C      NCH2                    INT  I   LOWER PEAKING 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      CLDTMP                 REAL  O   CLOUD TOP TEMPERATURE AT THE
C                                       SPOT.
C
C   COMMON AREAS- SPVAL
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      TNOLD         REAL     INTERIM CLOUD TOP TEMPERATURE VALUE.
C
C   SUBROUTINES CALLED- NONE.
C
C   FUNCTIONS USED- PLKRAD
C
C**********************************************************************
C
C
      INCLUDE 'SPVAL'
C-----
      INTEGER  NCH1,NCH2, ITRCNT, MAXITER

      REAL   CLDTMP,RADT(4),BTT(4),PLKRAD
      REAL   S, TN, CLOPE, TNOLD, DELTA, SLOPE1, SLOPE2, SLOPE3
      REAL   DIFF0, DIFF1, IBT, P3DELTA, SDELTA, TNSAVED, TNOLDS

      LOGICAL REVERSED

      PARAMETER (MAXITER=9)

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 interim cloud top temperature using the lower
C        peaking channel frequency) divided by:
C       (HIRS channel 18 radiance for the upper peaking predicted clear
C        minus the HIRS channel 18 radiance for the temperature)
C
      S(TN) = (RADT(4)-PLKRAD(TN,NCH2))/(PLKRAD(BTT(3),18)-PLKRAD(TN,18))

      SLOPE1 = R4SPV
      SLOPE2 = R4SPV
      SLOPE3 = R4SPV
      SDELTA = 9999999

      CLDTMP = R4SPV

      REVERSED = .FALSE.

C   CALCULATE THE TARGET SLOPE USING THE PARAMETERS PASSED IN AS ARGUMENTS.
C
C     RADT(4) is the radiance for the predicted clear for the lower peaking
C             channel at the lower peaking channel frequency
C     RADT(2) is the radiance for the observed brightness temperature for
C             the lower peaking channel at the lower peaking channel frequency
C     BTT(3)  is the radiance for the upper peaking predicted clear
C     BTT(1)  is the observed brightness temperature for the upper peaking channel

      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. THE TEMPERATURE AT THE
C   LOWER PEAKING CHANNEL IS EXPECTED TO BE WARMER THAN THE TEMPERATURE AT THE
C   UPPER PEAKING CHANNEL UNLESS THE UPPER PEAKING CHANNEL IS 2, WHICH PEAKS
C   ABOVE THE TROPOPAUSE. TN IS EXPECTED TO BE THE WARMER OF THE TWO OBSERVED
C   TEMPERATURES.
C
c     IF (CLOPE.LT.0.0) THEN
c        TN = BTT(1)
c     ELSE
c        TN = BTT(2)
c     END IF

      TN = BTT(2)

C   SAVE THE INITIAL TEMPERATURE TO BE USED AS A FALLBACK IF NECESSARY

      IBT = TN
      IF (IBT.LE.0.0) THEN
         PRINT *,'CLDTEMP: NCH2 ',NCH2,' IBT ',IBT
      END IF

C   IF THE INITIAL SLOPE IS NEGATIVE FOR THE 4/8 OR 7/8 CHANNEL COMBINATION
C   SET THE CLOUD TOP TEMPERATURE TO THE HIRS CHANNEL 8 TEMPERATURE.

      IF (CLOPE.LT.0.0) THEN
         IF (NCH2.EQ.8) THEN
            IF (NCH1.EQ.4.OR.NCH1.EQ.7) THEN
               CLDTMP = IBT
               GO TO 700
            END IF
         END IF
      END IF

C   INITIALIZE THE ITERATION COUNT TO ZERO AND SET THE VALUE OF "DELTA" TO
C   MINUS ONE.  "DELTA" IS USED TO ADJUST THE INTERIM TEMPERATURE UP OR DOWN.
C
      ITRCNT = 0
      IF (CLOPE.GT.0.0) THEN
         DELTA = -1.0
      ELSE
         DELTA = 1
      END IF

C   IF THE TEST SLOPE VALUE IS ZERO, SET THE CLOUD TOP TEMPERATURE TO THE
C   INITIAL TEMPERATURE AND RETURN TO THE CALLING ROUTINE.
C
      IF (CLOPE.EQ.0.0) THEN
         CLDTMP = IBT
         GO TO 700
      END IF
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
C
      IF (ABS(S(TN)-CLOPE).LE.0.01) THEN
         CLDTMP = TN
         GO TO 700
      END IF
      
C   
      TNOLD = TN
C
C   COMPARE SLOPES TO DETERMINE WHETHER THE INTERIM CLOUD TOP
C   TEMPERATURE NEEDS TO BE WARMED OR COOLED.
C
   5  CONTINUE
C
C   SAVE THE TEMPERATURE WHERE THE DIFFERENCE BETWEEN THE ORIGINAL
C   SLOPE AND THE CALCULATED SLOPE IS THE SMALLEST.
C
      TNSAVED = TNOLD
      TNOLD = TN
C
C   ADJUST THE TEMPERATURE UP OR DOWN 

      TN = TN + DELTA

  20  CONTINUE

C     SAVE THE TEMPERATURE WITH THE SMALLEST DIFFERENCE IN CALCULATED
C     SLOPES.

      IF (ABS(CLOPE - S(TN)).LT.SDELTA) THEN
         TNSAVED = TN
         TNOLDS = TNOLD
         SDELTA = ABS(CLOPE - S(TN))
      END IF
      IF (ITRCNT.GT.MAXITER) GO TO 500
      IF (ITRCNT.LE.2) THEN
         SLOPE1 = S(TNOLD)
         SLOPE2 = S(TN)
      ELSE
         SLOPE1 = SLOPE2
         SLOPE2 = S(TNOLD)
         SLOPE3 = S(TN)
      END IF

      DIFF0 = S(TNOLD) - CLOPE
      DIFF1 = S(TN) - CLOPE
c     IF (DIFF0.EQ.DIFF1) then
c        GO TO 400
c     END IF
      IF (NCH1.EQ.2) GO TO 25
      IF (ITRCNT.LE.2) THEN
         IF (DIFF0.LT.0.0) THEN
            IF (DIFF1.LT.0.0) THEN 
               IF (DIFF0 .LT. DIFF1) GO TO 25
               IF (.NOT.(REVERSED)) THEN
                  DELTA = DELTA* (-1.0)
                  REVERSED = .TRUE.
               END IF
               TN = TN + (2.0 * DELTA)
               ITRCNT = ITRCNT+1
               if (ITRCNT.EQ.2) then
                  CLDTMP = IBT
                  GO TO 700
               end if
               GO TO 20
            ELSE
               GO TO 500
            END IF
         ELSE
            IF (DIFF1.GT.0.0) THEN
               IF (DIFF0 .GT. DIFF1) GO TO 25
               IF (.NOT.(REVERSED)) THEN
                  DELTA = DELTA * (-1.0)
                  REVERSED = .TRUE.
               END IF
               TN = TN + (2.0 * DELTA)
               ITRCNT = ITRCNT+1
               if (ITRCNT.EQ.2) then
                  CLDTMP = IBT
                  GO TO 700
               end if
               GO TO 20
            ELSE
               GO TO 500
            END IF
         END IF
      END IF

  25  CONTINUE
      ITRCNT = ITRCNT + 1

C
C     FOR UPPER PEAKING CHANNELS OF 3 OR GREATER, IF THE ADJUSTED
C     TEMPERATURE IS COOLER THAN THE OBSERVED TEMPERATURE FOR THE UPPER
C     PEAKING CHANNEL OR IS WARMER THAN THE OBSERVED TEMPERATURE AT THE
C     LOWER PEAKING CHANNEL, REVERSE THE SIGN OF THE "DELTA" PARAMETER.
C     WHEN THE UPPER PEAKING CHANNEL IS 2, REVERSE THE SIGN OF THE "DELTA"
C     PARAMETER IF THE ADJUSTED TEMPERATURE IS WARMER THAN THE OBSERVED
C     CHANNEL 2 TEMPERATURE OR IS COOLER THAN THE OBSERVED TEMPERATURE AT
C     THE LOWER PEAKING CHANNEL (WHICH IS ASSUMED TO BE 3).

      IF (NCH1.GE.3) THEN
         IF (TN.LE.BTT(1).OR.TN.GT.BTT(2)) THEN
            IF (.NOT.(REVERSED)) THEN
               DELTA = DELTA * (-0.5)
               REVERSED = .TRUE.
               GO TO 5
            END IF
         END IF
      ELSE
         IF (TN.LE.BTT(2).OR.TN.GT.BTT(1)) THEN
            IF (.NOT.(REVERSED)) THEN
               DELTA = DELTA * (-0.5)
               REVERSED = .TRUE.
               GO TO 5
            END IF
         END IF
      END IF

      IF (SLOPE1.NE.R4SPV.AND.SLOPE2.NE.R4SPV.AND.SLOPE3.NE.R4SPV) THEN
         IF ((SLOPE2.LT.SLOPE1.AND.SLOPE2.LT.SLOPE3) .OR.
     +       (SLOPE2.GT.SLOPE1.AND.SLOPE2.GT.SLOPE3)) THEN
             CLDTMP = TNOLD
             GO TO 700
         END IF
      END IF

C
C     IF THE ADJUSTED TEMPERATURE DIFFERS FROM THE OBSERVED TEMPERATURE OF
C     THE LOWER PEAKING CHANNEL BY +50 DEGREES OR MORE, SET THE
C     CLOUD TOP TEMPERATURE TO THE INITIAL TEMPERATURE AND RETURN TO THE
C     CALLING ROUTINE.

      if (TN.GE.(BTT(2)+50.0)) THEN
         CLDTMP = IBT
         GO TO 700
      END IF
c     if (TN.LE.(BTT(2)-50.0)) THEN
c        CLDTMP = IBT
c        GO TO 700
c     END IF

c-----
C     IF THE UPPER PEAKING CHANNEL IS 2 AND THE ADJUSTED TEMPERATURE IS WARMER
C     THAN THE OBSERVED CHANNEL 2 BRIGHTNESS TEMPERATURE, SET THE CLOUD TOP
C     TEMPERATURE TO THE INITIAL TEMPERATURE ND RETURN TO THE CALLING ROUTINE.

      IF (NCH1.EQ.2.AND.TN.GE.BTT(1)) THEN
         CLDTMP = IBT
         GO TO 700
      END IF

C     IF THE LOWER PEAKING CHANNEL IS 8 AND THE ADJUSTED TEMPERATURE IS WARMER
C     THAN THE OBSERVED CHANNEL 8 BRIGHTNESS TEMPERATURE, SET THE CLOUD TOP
C     TEMPERATURE TO THE INITIAL TEMPERATURE ND RETURN TO THE CALLING ROUTINE.

      IF (NCH2.EQ.8.AND.TN.GE.BTT(2)) THEN
         CLDTMP = BTT(2)
         GO TO 700
      END IF
c-----
C     IF (THE SLOPE FOR THE ADJUSTED TEMPERATURE IS WITHIN 0.01 OF THE TARGET
C     SLOPE, SET THE CLOUD TOP TEMPERATURE TO THE ADJUSTED TEMPERATURE AND
C     RETURN TO THE CALLING ROUTINE.
C
      IF (ABS(S(TN) - CLOPE).LE.0.01) THEN
         CLDTMP = TN
         GO TO 700
      END IF
c-----

C     CALCULATE THE DIFFERENCE(DIFF0) IN THE SLOPE FROM THE SAVED TEMPERATURE FROM
C     THE PREVIOUS ITERATION MINUS THE TARGET SLOPE. CALCULATE THE DIFFERENCE (DIFF1)
C     IN THE SLOPE FROM THE ADJUSTED TEMPERATURE MINUS THE TARGET SLOPE.

      DIFF0 = S(TNOLD) - CLOPE
      DIFF1 = S(TN) - CLOPE

      IF (DIFF0.GT.0.0) THEN
C
C        IF THE SLOPE DIFFERENCE FROM THE PREVIOUS ITERATION IS POSITIVE AND 
C        THE SLOPE DIFFERENCE FROM THE CURRENT ITERATION IS POSITIVE, IF
C        THE SLOPE DIFFERENCE FROM THE PREVIOUS ITERATION IS GREATER THAN
C        THE SLOPE DIFFERENCE FROM THE CURRENT ITERATION, CONTINUE ITERATING.
C        IF THE SLOPE DIFFERENCE FROM THE CURRENT ITERATION
C        IS GREATER THAN THE SLOPE DIFFERENCE FROM THE PREVIOUS ITERATION,
C        SET THE CLOUD TOP TEMPERATURE TO THE SAVED TEMPERATURE FROM THE PREVIOUS
C        ITERATION AND RETURN TO THE CALLING ROUTINE.
C
         IF (DIFF1.GT.0.0) THEN
            IF (DIFF0.GT.DIFF1) THEN
               GO TO 5
            ELSE
               CLDTMP = TNOLD
               GO TO 700
            END IF
         ELSE
C
C        IF THE SLOPE DIFFERENCE FROM THE PREVIOUS ITERATION IS POSITIVE AND
C        THE SLOPE DIFFERENCE FROM THE CURRENT ITERATION IS NEGATIVE, EITHER
C        SET THE CLOUD TOP TEMPERATURE TO THE LAST SAVED TEMPERATURE OR
C        PERFORM AN INTERPOLATION TO SET THE CLOUD TOP TEMPERATURE.
C
            GO TO 500
         END IF
      ELSE
C
C        IF THE SLOPE DIFFERENCE FROM THE PREVIOUS ITERATION IS NEGATIVE
C        AND THE SLOPE DIFFERENCE FROM THE CURRENT ITERATION IS NEGATIVE,
C        IF THE SLOPE DIFFERENCE FROM THE PREVIOUS ITERATION IS LESS THAN
C        THE SLOPE DIFFERENCE FROM THE CURRENT ITERATION, CONTINUE ITERATING.

         IF(DIFF1.LT.0.0) THEN
            IF (DIFF0.LT.DIFF1) THEN
               GO TO 5
            ELSE
C
C        IF THE SLOPE DIFFERENCE FROM THE PREVIOUS ITERATION IS NEGATIVE AND
C        THE SLOPE DIFFERENCE FROM THE CURRENT ITERATION IS LESS THAN
C        THE SLOPE DIFFERENCE FROM THE PREVIOUS ITERATION, SET THE CLOUD TOP
C        TEMPERATURE TO THE TEMPERATURE FROM THE PREVIOUS ITERATION AND
C        RETURN TO THE CALLING ROUTINE.
C 
               CLDTMP = TNOLD
               GO TO 700
            END IF
         ELSE
C
C        IF THE SLOPE DIFFERENCE FROM THE PREVIOUS ITERATION IS NEGATIVE AND
C        THE SLOPE DIFFERENCE FROM THE CURRENT ITERATION IS POSITIVE, EITHER
C        SET THE CLOUD TOP TEMPERATURE TO THE LAST SAVED TEMPERATURE OR
C        PERFORM AN INTERPOLATION TO SET THE CLOUD TOP TEMPERATURE.
C
            GO TO 500
         END IF
      END IF

 400  CONTINUE
C      
C     IN THE UNLIKELY CIRCUMSTANCE WHERE THE SLOPE DIFFERENCE FROM THE
C     PREVIOUS ITERATION AND THE SLOPE DIFFERENCE FROM THE CURRENT
C     ITERATION ARE IDENTICAL, SET THE CLOUD TOP TEMPERATURE TO ZERO AND
C     RETURN TO THE CALLING ROUTINE.
C
c     CLDTMP = 0.0
c     GO TO 700

 500  CONTINUE
C
C     IF THE ITERATION COUNT IS GREATER THAN THE MAXIMUM ITERATION LIMIT
C     SET THE CLOUD TOP TEMPERATURE TO THE LAST SAVED INTERIM TEMPERATURE.
C     OTHERWISE, PERFORM AN INTERPOLATION TO SET THE CLOUD TOP TEMPERATURE
C     BASED ON THE TEMPERATURE FROM THE PREVIOUS ITERATION.

      IF (ITRCNT.GT.MAXITER) THEN
         CLDTMP = TNSAVED
         GO TO 700
      END IF
      CLDTMP = TNOLD + (DELTA*((S(TNOLD)-CLOPE)/(S(TNOLD)-S(TN))))
      GO TO 700
C
C
  700 CONTINUE
C
C  RETURN THE CLOUD TOP TEMPERATURE. IF THE CALCULATED CLOUD TOP TEMPERATURE
C  IS HOTTER THAN THE INITIAL TEMPERATURE, SET THE CLOUD TOP TEMPERATURE TO
C  THE INITIAL TEMPERATURE.
C

      IF (CLDTMP.LE.0.0) CLDTMP = IBT
      IF (CLDTMP.GT.IBT) CLDTMP = IBT
      RETURN
      END
