      SUBROUTINE TMPCTT(NCH1,NCH2,RADT,BTT,TROTMP,CLDTMP)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- TMPCTT
C         ADAPTED FROM CLDTEM
C
C   LANGUAGE- FORTRAN     TYPE- SUBROUTINE
C
C   VERSION- 1.0    DATE- 04/12/07   PROGRAMMER- FRANK TILLEY (IMSG)
C
C   FUNCTION- THIS SUBROUTINE CALCULATES CLOUD TOP TEMPERATURE FOR CLOUDS
C             WITH CLOUD TOPS ABOVE THE TROPOPAUSE.
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      TROTMP                 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      DELX        REAL     DELTA BETWEEN THE SLOPE FOR A
C                           TEMPERATURE VALUE AND THE SLOPE
C                           FOR THAT TEMPERATURE VALUE MINUS 1.
C      DEL         REAL     THE INTERIM SLOPE MINUS THE FIXED SLOPE
C                           DIVIDED BY DELX.
C      MITER        INT     COUNTER FOR TRACKING ITERATIONS.
C
C   SUBROUTINES CALLED- NONE.
C
C   FUNCTIONS USED- PLKRAD
C
C**********************************************************************
C
C
      INTEGER  NCH1,NCH2

      REAL   CLDTMP,RADT(4),BTT(4),PLKRAD,TROTMP
      REAL   S, TN, CLOPE, TNO, DELX, DEL, DELTA, THRES

      INTEGER MITER
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
ccft  S(TN) = (RADT(4)-PLKRAD(TN,NCH2))/(RADT(3)-PLKRAD(TN,NCH1))
      S(TN) = (RADT(4)-PLKRAD(TN,NCH2))/(PLKRAD(BTT(3),NCH1)-PLKRAD(TN,NCH1))
C
C   START BY SETTING THE CLOUD TOP TEMPERATURE TO THE TROPOPAUSE
C   TEMPERATURE.
C
      THRES = 0.0001
      TN=TROTMP
      IF(ABS( PLKRAD(BTT(3),NCH1)-PLKRAD(BTT(1),NCH1)) .LT. THRES) THEN
        go to 700
      END IF
C
C   IF THE PREDICTED CLEAR RADIANCE AT THE UPPER PEAKING CHANNEL
C   IS WITHIN 0.025 WATTS PER CENTIMETER SQUARED OF THE RADIANCE
C   FOR THE LOWER-PEAKING CHANNEL BRIGHTNESS TEMPERATURE COMPUTED
C   USING THE UPPER PEAKING CHANNEL FREQUENCY, RETURN THE CLOUD
C   TOP TEMPERATURE AS THE MEASURED BRIGHTNESS TEMPERATURE AT THE
C   LOWER PEAKING CHANNEL. DO THE SAME THING IF THE PREDICTED
C   CLEAR RADIANCE AT THE UPPER PEAKING CHANNEL IS LESS THAN
C   0.025 WATTS PER CENTIMETER SQUARED OF THE MEASURED RADIANCE
C   AT THE UPPER PEAKING CHANNEL.
C
ccft  IF( RADT(3)-PLKRAD(TN,NCH1) .LT. 0.025 ) GOTO 700
 200  CONTINUE
      IF(ABS( PLKRAD(BTT(3),NCH1)-PLKRAD(TN,NCH1)) .LT. 0.025) THEN
        TN = TN+1.0
        GO TO 200
      END IF
ccft  IF( RADT(3)-RADT(1) .LT. 0.025 )  GOTO 700
C
C   DEFINE THE FIXED SLOPE AS THE PREDICTED CLEAR MINUS MEASURED
C   RADIANCE AT THE LOWER PEAKING CHANNEL DIVIDED BY THE
C   PREDICTED CLEAR MINUS MEASURED RADIANCE AT THE UPPER
C   PEAKING CHANNEL.
C
      CLOPE = (RADT(4)-RADT(2))/(PLKRAD(BTT(3),NCH1)-PLKRAD(BTT(1),NCH1))
C
C   IF THE MEASURED BRIGHTNESS TEMPERATURE AT THE UPPER
C   PEAKING CHANNEL IS GREATER THAN OR EQUAL TO THE BRIGHTNESS
C   TEMPERATURE AT THE LOWER PEAKING CHANNEL, RETURN THE CLOUD
C   TOP TEMPERATURE AS THE BRIGHTNESS TEMPERATURE AT THE LOWER
C   PEAKING CHANNEL.
C
ccft  IF(BTT(1).GE.BTT(2) ) GOTO 700
C
C   INITIALIZE THE INTERIM CLOUD TOP TEMPERATURE VARIABLE AND
C   THE ITERATION COUNTER.
C
      TNO=0.0
      MITER=0
      DELTA = 1.0
C
C   ITERATION LOOP
C
  300 CONTINUE
C
C   IF THE RADIANCE OF THE INTERIM CLOUD TOP TEMPERATURE
C   COMPUTED USING THE UPPER PEAKING CHANNEL FREQUENCY IS
C   WITHIN 0.025 WATTS PER CENTIMETER SQUARED OF THE
C   PREDICTED CLEAR RADIANCE AT THE UPPER PEAKING CHANNEL,
C   RETURN THE INTERIM CLOUD TOP TEMPERATURE AS THE CLOUD
C   TOP TEMPERATURE.
C
ccft  IF(RADT(3)-PLKRAD(TN,NCH1).LT.0.025) GO TO 700
      IF(ABS(PLKRAD(BTT(3),NCH1)-PLKRAD(TN,NCH1)).LT.THRES) THEN
        go to 700
      END IF
C
C   IF THE SLOPE FUNCTION VALUES FOR THE INTERIM CLOUD TOP TEMPERATURE
C   VALUE AND THAT VALUE MINUS 1.0 ARE WITHIN 0.01 OF EACH OTHER,
C   RETURN THE INTERIM CLOUD TOP TEMPERATURE AS THE CLOUD TOP TEMPERATURE.
C
      DELX = (S(TN) - S(TN+DELTA)) / DELTA
      IF(ABS(DELX).LE.0.01) GO TO 700
C
C   IF THE INTERIM SLOPE MINUS FIXED SLOPE VALUE, DIVIDED BY THE
C   "DELX" VALUE EXCEEDS 15.0, RETURN THE INTERIM CLOUD TOP TEMPERATURE
C   AS THE CLOUD TOP TEMPERATURE.
C
      DEL = (S(TN)-CLOPE)/DELX
C
C   SUBTRACT THE DELTA COMPUTED EARLIER FROM THE INTERIM CLOUD TOP
C   TEMPERATURE AND INCREMENT THE ITERATION COUNTER. IF THE COUNTER
C   EXCEEDS 25 ITERATIONS, RETURN THE LATEST INTERIM CLOUD TOP
C   TEMPERATURE VALUE AS THE CLOUD TOP TEMPERATURE.
C
C
      IF (ABS(DEL).LE.0.05) GO TO 700
      IF (ABS(DEL).GE.10.0) THEN
         TN=TN+DELTA
      ELSE
         TN = TN+DEL
      END IF
      MITER=MITER+1
      IF(MITER.GT.25) GOTO 700
C
C   IF THE ABSOLUTE VALUE OF THE COMPUTED DELTA IS LESS THAN OR EQUAL
C   TO 0.05, OR THE INTERIM CLOUD TOP TEMPERATURE VALUE IS 190.0
C   DEGREES KELVIN OR LESS OR THE INTERIM CLOUD TOP TEMPERATURE IS
C   GREATER THAN OR EQUAL TO THE PREDICTED CLEAR TEMPERATURE AT THE
C   LOWER PEAKING CHANNEL, TEST THE INTERIM CLOUD TOP TEMPERATURE
C   AGAINST THE CLOUD TOP TEMPERATURE FROM A PREVIOUS ITERATION.
C   OTHERWISE, JUMP DIRECTLY TO THE NEXT ITERATION.
C
ccft  IF(ABS(DEL).LE.0.05 .OR. TN.LE.190.0 .OR. TN.GE.BTT(3)) GOTO 600
ccft  IF(ABS(DEL).LE.0.05 .OR. TN.LE.170.0 .OR. TN.GE.BTT(4)) GOTO 600
c     IF(ABS(DEL).LE.0.05 .OR. TN.LE.BTT(4) .OR. TN.GE.BTT(1)) GOTO 600
c     GOTO 300
C
c 600 CONTINUE
C
C  IF THE SAVED INTERIM CLOUD TOP TEMPERATURE IS WITHIN 0.01 DEGREES
C  KELVIN OF THE CURRENT INTERIM CLOUD TOP TEMPERATURE, RETURN THE
C  CURRENT CLOUD TOP TEMPERATURE AS THE CLOUD TOP TEMPERATURE. 
C  OTHERWISE, SAVE THE CURRENT INTERIM CLOUD TOP TEMPERATURE AND
C  JUMP TO THE NEXT ITERATION.
C
c     IF(ABS(TN-TNO).LT.0.01) GOTO 700
      IF(ABS(TN-TNO).LT.0.1) GOTO 700
      TNO=TN
      GOTO 300
C
  700 CONTINUE
C
C  RETURN THE INTERIM CLOUD TOP TEMPERATURE AS THE CLOUD TOP
C  TEMPERATURE.
C
      CLDTMP=TN
      RETURN
      END
