      SUBROUTINE CLDTOP(OBSHIR,SATID,OLR,LC1,LC2,LC3,LC4,R1T1ST,
     2                   CLD1ST,PRFLAG,ERSTAT,AEROCNT,EXTCNT,METOP,
     +                   CTPEXIT,CPELT5PCT,AEROQC2,FAILCOUNT,
     +                   COUNTQC3,MINUS1,COUNTQC0,COUNTQC1,COUNTQC4,
     +                   COUNTQC5)
      IMPLICIT NONE
C
C
C  THIS VERSION TESTS M.CHALFANT'S CODE.
C
C***********************************************************************
C    NAME- CLDTOP
C
C    LANGUAGE- VS FORTRAN     TYPE- SUBROUTINE    COMPUTER- CEMSCS
C
C    VERSION- 1.0     DATE- 11/30/93   PROGRAMMER- DAVE MOREL (SMSRC)
C    VERSION- 2.0     DATE- 09/27/95   PROGRAMMER- LEAH CASEY (HSTX)
C    VERSION- 2.1     DATE- 06/03/96   PROGRAMMER- LEAH CASEY (HSTX)
C    VERSION- 2.2     DATE- 11/06/96   PROGRAMMER- A. ALLEGRINO (HSTX)
C    VERSION- 2.3     DATE- 02/21/97   PROGRAMMER- LEAH WILSON (HSTX)
C    VERSION- 2.4     DATE- 05/01/97   PROGRAMMER- LEAH WILSON (HSTX)
C    VERSION- 2.5     DATE- 06/14/97   PROGRAMMER- A. ALLEGRINO (HSTX)
C    VERSION- 2.6     DATE- 07/25/97   PROGRAMMER- A. ALLEGRINO (HSTX)
C    VERSION- 2.7     DATE- 08/06/97   PROGRAMMER- A. ALLEGRINO (HSTX)
C    VERSION- 2.8     DATE- 09/15/97   PROGRAMMER- A. ALLEGRINO (HSTX)
C    VERSION- 2.9     DATE- 10/01/97   PROGRAMMER- A. ALLEGRINO (HSTX)
C    VERSION- 3.0     DATE- 11/13/97   PROGRAMMER- BRIAN BARIL (HSTX)
C    VERSION- 3.1     DATE- 01/02/98   PROGRAMMER- BRIAN BARIL (HSTX)
C    VERSION- 3.2     DATE- 03/02/98   PROGRAMMER- LEAH WILSON (RSTX)
C    VERSION- 3.3     DATE- 03/27/98   PROGRAMMER- BRIAN BARIL (RSTX)
C    VERSION- 3.4     DATE- 04/06/98   PROGRAMMER- BRIAN BARIL (RSTX)
C    VERSION- 3.5     DATE- 05/12/98   PROGRAMMER- BRIAN BARIL (RSTX)
C    VERSION- 3.6     DATE- 06/02/98   PROGRAMMER- BRIAN BARIL (RSTX)
C    VERSION- 3.7     DATE- 06/29/98   PROGRAMMER- LEAH WILSON (RSTX)
C    VERSION- 3.8     DATE- 03/03/99   PROGRAMMER- BRIAN BARIL (RSTX)
C    VERSION- 3.9     DATE- 06/07/99   PROGRAMMER- A.ALLEGRINO (RSTX)
C    VERSION- 3.9.1   DATE- 07/29/99   PROGRAMMER- BRIAN BARIL (RSTX)
C    VERSION- 3.10    DATE- 10/22/99   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.11    DATE- 10/25/99   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.12    DATE- 10/26/99   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.13    DATE- 11/29/99   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.14    DATE- 12/02/99   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.15    DATE- 12/06/99   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.16    DATE- 12/14/99   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.17    DATE- 01/07/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.18    DATE- 01/31/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.19    DATE- 02/09/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.20    DATE- 02/11/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.21    DATE- 02/23/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.22    DATE- 03/06/00   PROGRAMMER- LEAH WILSON  (RITSS)
C    VERSION- 3.23    DATE- 03/17/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.24    DATE- 03/31/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.25    DATE- 04/11/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.26    DATE- 04/17/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.27    DATE- 04/18/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C**********************************************************************
C    THIS VERSION WILL NOT BE DELIVERED
C    VERSION- 3.28    DATE- 05/08/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C**********************************************************************
C    VERSION- 3.29    DATE- 07/05/00   PROGRAMMER- L. WILSON (RAYTHEON)
C    VERSION- 3.30    DATE- 08/24/00   PROGRAMMER- L. WILSON (RAYTHEON)
C    VERSION- 3.31    DATE- 09/28/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.32    DATE- 10/03/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.33    DATE- 11/01/00   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.34    DATE- 02/07/01   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.35    DATE- 04/05/01   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.36    DATE- 05/30/01   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.37    DATE- 07/19/01   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.38    DATE- 09/21/01   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.39    DATE- 10/16/01   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.40    DATE- 10/22/01   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.41    DATE- 12/07/01   PROGRAMMER- A. ALLEGRINO (RITSS)
C    VERSION- 3.43    DATE- 09/08/03   PROGRAMMER- A. ALLEGRINO (RITSS)
C                                           AND  FRANK TILLEY (RAYTHEON)
C    VERSION- 3.44    DATE- 02/27/03   PROGRAMMER- RICKEY ROLLINS (CSC)
C    VERSION- 3.45    DATE- 05/20/04   PROGRAMMER- F. TILLEY (RAYTHEON)
C    VERSION- 3.46    DATE- 08/12/05   PROGRAMMER- F. TILLEY (RAYTHEON)
C    VERSION- 4.00    DATE- 07/10/13   PROGRAMMER- F. TILLEY (IMSG)
C
C    FUNCTION-  THIS SUBROUTINE IS USED TO OBTAIN THE CLOUD AMOUNT,
C               CLOUD TOP PRESSURE AND CLOUD TOP TEMPERATURE AND PUT
C               THEM IN THE HIRS ANCILLARY BUFFER.
C    VERSION- 2.0 INSTALL TOVS CLOUD ALGORITHM.
C    VERSION- 2.1 INSTALL ATTENUATION ABOVE THE CLOUDS.
C    VERSION- 2.2 ADDED CALCULATION OF TROPOPAUSE TEMPERATURE AND
C                 LEVEL AND CORRECTED MISLOCATION OF CERTAIN SPOTS
C                 TO SPOT #1 BY USING THE CORRECT INDEX IN THE DO 172
C                 LOOP
C    VERSION- 2.3 STORE OLR AND 4 LAYER COOLING RATES FOR EACH SPOT AND
C                 LINE.  ADD /CLIMATE/.  PASS ICTBAD TO WRTR1T.
C    VERSION- 2.4 INCREASE 3RD DIMENSION OF OBSHIR FROM 19 TO 20.
C                 ADD ESTH8C TO CALL TO GET SKIN TEMPERATURE TO REPLACE
C                 USE OF HIRS CHANNEL 8.
C    VERSION- 2.5 COMMENTED OUT USING SKIN TEMPERATURE TO REPLACE
C                 HIRS CHANNEL 8. REPLACED IT WITH HIRS CHANNEL 8
C                 WHICH ACCOUNTS FOR WATER VAPOR
C    VERSION- 2.6 TARM PATH (IE, CLEAR,NSTAR OR CLOUDY RETRIEVALS) TEST
C                 IS USED ONLY OVER LAND. OVER SEA, THE CLOUD
C                 COEFFICIENTS ARE USED AT ALL RETRIEVAL LOCATIONS
C    VERSION- 2.7 IF CLEAR, THEN THE CLOUD TOP TEMPERATURE IS SET TO
C                 ABSOLUTE ZERO (0K)
C    VERSION- 2.8 A CLOUD TOP TEMPERATURE OF ZERO WOULD INCORRECTLY
C                 SET ALL OF THE CLOUD PRODUCTES TO MISSING. THIS WAS
C                 RECTIFIED IN THIS VERSION.
C    VERSION- 2.9 USED POLAR REDUNDANCY TO SCREEN THE RETRIEVALS.
C                 IF POLAR REDUNDANT THEN SKIP PROCESSING.
C    VERSION- 3.0 MODIFICATIONS FOR INCLUSION INTO ATOVS.  REPLACED
C                 TRANPV WITH OPTRAN.  AMSU-A CHANNELS.
C                 ADDED ARGUMENTS FOR THE CALL FROM SPGDVR.
C    VERSION- 3.1 CDB CONVERSIONS. REPLACED CALL TO RGTCDB WITH REALCOEF.
C    VERSION- 3.2 MODIFICATIONS FOR MERGING WITH LATEST SPGDVR.  NEWCH8
C                 ARRAY WAS MADE LOCAL, SINCE ATTENUATED HIRS-8 NOW
C                 RESIDES IN HIRANC(-,-,23).  ADD PRFLAG FOR POLAR
C                 REDUNDANCY FLAGS.
C    VERSION- 3.3 IMPLIMENTED AN UPDATED PRECIPITABLE WATER ROUTINE.
C                 MOVED THE CALL TO TO2PRF SO IT WILL BE CALLED ONLY
C                 AFTER THE FIRST CALL TO CLDTOP.  HARDCODED THE
C                 TOTAL OZONE (TTLOZ) SENT TO TO2PRF.  MOVED INTO3
C                 INTO THE INCLUDE STATEMENT RTPLVL.  SET THE NUMBER
C                 OF CHANNELS SENT TO OPTRAN TO 23.
C    VERSION- 3.4 REPLACED THE CALL TO TO2PRF WITH TO3PPMV.
C                 INITIALIZED THE WPROFI ARRAY TO AVOID CALCULATIONS
C                 WITH BOGUS VALUES.  CHANGED THE CONDITION VALUE
C                 WHERE WPROFI IS FILLED FROM JT.LE.33 TO JT.LE.43
C                 TO ACCOUNT FOR THE LOWER LEVELS.
C    VERSION-3.5  ADDED CODE THAT RUNS THE INTEGRATED OZONE PROFILE
C                 (PPMV) THROUGH THE PRECIPITABLE WATER ROUTINE.
C    VERSION-3.6  CHANGED FROM PROCESSING 9 SPOTS TO 4 SPOTS.  BOX
C                 WAS REDIMENSIONED.  IDICIES OF 1 TO 3 WERE CHANGED
C                 TO 1 TO 2.  INSTEAD OF LOOPING THROUGH 40 LINES OF
C                 THE RETRIEVAL BUFFER, NOW LOOPS THROUGH 20.  THIS
C                 BEING THE RESULT OF PROCESSING EVERY OTHER LINE
C                 RATHER THAN EVEY LINE AS WAS DONE FOR THE 2X1.
C                 TCLEAR8 WAS BEING INCORRECTLY SET TO AMSU 2,3,4
C                 INSTEAD OF 5,7,9 WHICH CORRESPONDS TO MSU 2,3,4
C                 REMOVED ESTH8C
C    VERSION- 3.7 CHANGE TEMPERATURE ERROR MESSAGE TO APPEAR IN KIDS
C                 FILE, WITH A CAP OF 1 MESSAGE DISPLAYED.
C    VERSION- 3.8 REMOVED RADT8M AND RADT8C SINCE THEY WERE NOT
C                 ASSIGNED VALUES FOR THE CLOUD AMOUNT CALCULATION.
C                 THEY WERE REPLACED BY RADT2 AND RADT4 WHICH ARE
C                 RADIANCE VALUES BASED ON THE CHANNEL COMBINATIONS.
C                 ALSO REMOVED THE HARDCODED 7 AND 8 FROM THE THIRD
C                 ITERATION OF CLOUD AMOUNT.
C    VERSION- 3.9 REPLACED THE OBSHIR ARRAY WITH HIRDAT SINCE OBSHIR
C                 NO LONGER CONTAINS THE LIMB CORRECTED DATA.
C    VERSION- 3.9.1  ADDED A SEPARATE CALCULATION FOR A SURFACE ESTIMATED
C                    CHANNEL 8 CLOUD AMOUNT FOR THE SECOND AND THIRD ITERATION.
C                    ALSO ADDED A NEW VALUE TO THE ICTBAD FLAG TO INDICATE WHERE
C                    THE AMOUNT WAS SET TO 0.01 BECAUSE THE SFC AMOUNT SAID
C                    CLEAR BUT THE HIRS 8 AMOUNT SAID CLOUDY.
C    VERSION- 3.10 ADDED A TEST TO DETERMINE WHETHER OR NOT TO SET CLOUD AMT
C                  TO ZERO IF OVER HIGH TERRAIN BY DIFFERING THE HIRS CHANNEL
C                  8 AND THE HIRS CHANNEL 8 SURFACE TEMPERATURE ESTIMATE.
C    VERSION- 3.11 ADDED A CHECK FOR ANTARCTICA SOUTH OF 70S SO THAT ALL
C                  CLOUD AMOUNTS =100% OR LESS THAN 3% ARE CLEAR.
C    VERSION- 3.12 REMOVED ALL CLOUDS IF OVER ANTARCTICA ABOVE 3000 METERS
C                  AND TERRAIN IS SNOW COVERED.
C    VERSION- 3.13 CHANGED THE SETTING OF THE CLOUD PRODUCTS FROM CLEAR TO
C                  MISSING OVER ANTARCTICA FOR TERRAIN HEIGHTS > 3000M AND
C                  SNOW COVER.
C    VERSION- 3.14 CHANGED THE HIGH TERRAIN (> 1500M) TEMPERATURE DIFFENCE
C                  LIMIT TO BE SOFT CODED AND READ FROM THE SOFTFLAGS.A2
C                  FILE.
C    VERSION- 3.15 ADDED THE VERY HIGH TERRAIN (> 2900M) TEMPERATURE DIFFENCE
C                  LIMIT AND IT WILL  BE SOFT CODED AND READ FROM THE
C                  SOFTFLAGS.A2 FILE.
C    VERSION- 3.16 CHANGED THE VERY HIGH TERRAIN (> 2900M) TEST TO
C                  JUST A HIGH TERRAIN (>1500) TEST FOR AREAS OF THE
C                  ANTARCTIC SOUTH OF 70S.
C    VERSION- 3.17 ADDED A CALCULATION OF THE HIGH TERRAIN TEMPERATURE
C                  DIFFERENCE THRESHOLD TO VARY THE THRESHOLD WITH TERRAIN
C                  HEIGHT AND CHANGED THE LATITUDE LIMIT FROM 70S TO 66S.
C    VERSION- 3.18 REMOVED THE CLOUD AMOUNT TESTS OVER ANTARTICA WHICH TEST
C                  FOR AMOUNTS, FROM THE SURFACE TEMPERATURE ESTIMATE USING
C                  HIRS CHANNEL 8 OR FROM THE HIRS CHANNEL 8 ITSELF, GREATER
C                  THAN OR EQUAL TO 1 OR LESS THAN OR EQUAL TO 0.03. IF THE
C                  CRITERIA WAS MET THEN THE AMOUNT WAS SET TO MISSING.
C    VERSION- 3.19 ADDED ANOTHER QUALITY ASSURANCE FLAG INDICATING WHERE THE
C                  CLOUD PRODUCTS WERE ARTIFICALLY SET DUE TO THE RESTRAHLEN
C                  EFFECT OVER HOT TERRAIN. ( SEE ICTBAD. THIS IS SET TO 3 FOR
C                  THIS CONDITION.)
C    VERSION- 3.20 SAME AS VERSION 3.19 BUT IT WILL NOW INCLUDE HIGH TERRAIN.
C    VERSION- 3.21 COMMENTED OUT THE ADJUSTMENT OF THE CLOUD AMOUNT TO 0.03
C                  WHEN THE COMPUTED CLOUD AMOUNT, FROM THE SURFACE TEMPERATURE
C                  ESTIMATE, IS LESS THAN ZERO.
C    VERSION- 3.22 COMMENTED OUT THE SETTING OF CLOUD AMOUNT TO ZERO IF THE
C                  COMPUTED CLOUD AMOUNT IS < 0.0, CHANGED SCREENING TESTS
C                  FOR HOT/HIGH TERRAIN TO BE APPLIED ONLY IF THERE IS CLOUD
C                  COVER AND CORRECTED A TYPO IN THE CHECK FOR ZERO CLOUD
C                  AMOUNTS AND CLOUD TOP PRESSURES NOT EQUAL TO 1250MB.
C    VERSION- 3.23 ASSIGNED THE ICTBAD VALUE OF 2 TO ALL CLEAR SPOTS SET TO
C                  CLEAR AS A RESULT OF COMPARING THE TEMPERATURE DIFFERENCE
C                  BETWEEN THE COMPUTED CLEAR HIRS CHANNEL 8 AND THE OBSERVED
C                  WITH THE DIFFERENCE THRESHOLD OF 1.5K
C    VERSION- 3.24 ADDED A SOFT CODED SWITCH TO DECIDE HOW TO HANDLE INSTANCES
C                  WHERE THE CO2 SLICING PRODUCES A HIGH CLOUD AMOUNT VALUE
C                  IN PLACES WHERE IT IS BELIEVED TO BE MUCH LOWER, POSSIBLY
C                  CLEAR. THIS IS RELATED TO ARTIFICALLY SETTING THE CLOUD
C                  AMOUNT TO 0.03 WHICH WAS DONE IN AN EARLIER VERSION.
C                  THE SWITCH WILL HAVE THE FOLLOWING VALUES AND DEFINITIONS:
C
C                      0 = NOTHING IS TO BE DONE
C                      1 = SET ALL 3 CLOUD PARAMETERS TO MISSING
C                      2 = SET ALL 3 CLOUD PARAMETERS TO CLEAR
C
C                  ALSO CHANGED THE MEANING OF ICTBAD=2. THE CORRECT MEANING
C                  IS THAT IT CHECKS THE TEMPERATURE DIFFERENCE OF THE VALUES
C                  USED TO COMPUTE THE DENOMINATOR OF THE CLOUD AMOUNT FRACTION
C                  IF THE ABSOLUTE VALUE IS LESS THAN 1.5K THEN IT IS FLAGGED.
C    VERSION- 3.25 USED THE SOFT CODED SWITCH, CLDSWH, TO DECIDE WHETHER OR NOT
C                  TO TEST THE TEMPERATURE DIFFERENCE NORTH OF 66S.
C    VERSION- 3.26 ADDED ANOTHER TEST FOR HIGH TERRAIN WHICH SETS THE CLOUD
C                  PRODUCTS TO MISSING IF THE TERRAIN HEIGHT IS BETWEEN 2700
C                  AND 2900 METERS AND THE CLOUD TOP PRESSURE IS LESS THAN
C                  440MB.
C    VERSION- 3.27 ADDED THE SOFT CODED THRESHOLD CLRTHRES TO BE USED IN
C                  DETERMINING CLEAR SKY OLR AND LC1-4 IN SUBROUTINE WRTR1T.
C***************************************************************************
C    THIS VERSION WILL NOT BE DELIVERED
C    VERSION- 3.28 ADDED ANOTHER VALUE TO THE ICTBAD ARRAY. ICTBAD WILL BE
C                  SET TO 5 IF BOTH THE NEGATIVE CLOUD AMOUNT AND THE
C                  TEMPERATURE DIFFERENCE, FROM THE DENOMINATOR USED TO COMPUTE
C                  CLOUD AMOUNT, IS < 1.5K. ALSO CORRECTED TYPOS WITHIN THE DO
C                  170 LOOP, REFERENCES TO JSPOT WERE CHANGED TO JT.
C***************************************************************************
C    VERSION- 3.29 ADDED READ OF LIMTER FROM FT70 TO PASS INTO WRTR1T.
C    VERSION- 3.30 EXPANDED LIMTER FROM 3 TO 5 ELEMENTS FOR WR #109.
C    VERSION- 3.31 CHANGED CODE TO USE T. REALE'S CLOUD DETECTION RESULTS
C                  TO DETERMINE WHETHER OR NOT TO USE THE PREDICTED TEMPERATURE
C                  FROM THE CLOUD COEFFICIENTS OVER SEA AS WELL AS OVER LAND.
C                  (WR#115)
C    VERSION- 3.32 CHANGED CODE TO APPLY CLOUD COEFFICIENTS TO DATA SOUTH OF
C                  60S (WR#117)
C    VERSION- 3.33 ADDED CODE TO SKIP OVER ALL NOHIRS RETRIEVALS (WR #121).
C    VERSION- 3.34 ADDED CODE TO INSURE THAT IF THE CLOUD AMOUNT IN THE THIRD
C                  ITERATION IS LESS THAN ZERO THEN THE CLOUD PRODUCTS ARE SET
C                  TO MISSING VALUES. (WR #120).
C    VERSION- 3.35 CHANGED ARRAY SIZES SO THAT AN ATOVS LEVEL '41' COULD
C                  PROCESS IN SUBROUTINE SIMCBT.F. THIS INVOLVED INCREASING
C                  ARRAYS FROM 40 ELEMENTS TO 42 ELEMENTS (42 = VARIABLE PRFTLV)
C                  CHANGES WERE MADE TO ARRAYS TAUIR, TAUHRS, & INVPRS. THE DO
C                  108 LOOP WAS ALSO INCREASED FROM 40 TO 42 ITERATIONS AND
C                  THE CALL TO OPTRAN WAS MODIFIED FROM PRFTLV-2 TO PRFTLV.
C                  (WR#150)
C    VERSION- 3.36 ADDED CODE TO CHECK FOR MISSING CLOUD TOP TEMPERATURE VALUES
C                  IN DETERMINING WHETHER OR NOT TO CALL A CLOUD PRODUCT
C                  QUESTIONABLE IN THE CLOUD PRODUCT QC FLAG. BEFORE, IF
C                  THE CLOUD AMOUNT WAS NEGATIVE, THE QC FLAG WOULD NOT
C                  INDICATE A NEGATIVE CLOUD AMOUNT BUT IT WOULD BE
C                  QUESTIONABLE. (WR#154)
C    VERSION- 3.37 ADDED CODE TO CHECK FOR HOT AND HIGH TERRAIN IF THE
C                  COMPUTED CLOUD AMOUNT IS NEGATIVE. IF TRUE, THEN
C                  SET THE CLOUD PRODUCTS TO CLEAR AND RESET THE QC FLAG
C                  TO SIGNIFY HOT/HIGH TERRAIN. ALSO ADDED COASTLINES IN THE
C                  CHECK OF HOT TERRAIN ON JULY 2. (WR#165)
C    VERSION- 3.38 REMOVED VERSION 3.31 FROM THE CODE (WR#115). TONY REALE'S
C                  CLOUD MASK IS NOW USED ONLY OVER LAND. (WR#175)
C    VERSION- 3.39 CHANGED THE FIRST TEST OF WHETHER OR NOT THE SPOT IS CLEAR.
C                  APPARENTLY THIS WAS A TEST OF THE RESTRAHLEN EFFECT AND WAS
C                  ONLY MEANT TO BE USED OVER LAND. HENCE, THE IF STATEMENT
C                  ALSO INCLUDES A CHECK FOR LAND.  ANOTHER CHANGE HAD TO BE
C                  MADE TO ACCOUNT FOR THE MANY INSTANCES OF THE RADIANCE OF
C                  THE CLOUD BEING GREATER THAN THE RADIANCE OF THE PREDICTED
C                  CLEAR RADIANCE. THIS CONDITION DEFINES CLEAR SKIES HENCE
C                  THE CLOUD PRODUCTS ARE DEFINED AS CLEAR.
C                  ALSO ADDED A NEW QC FLAG WHICH SHOWS THE SPOTS SELECTED
C                  AS CLEAR AS A RESULT OF THE RESTRAHLEN EFFECT. THE ICTBAD
C                  VALUE IS SET TO 2. (WR#178)
C    VERSION- 3.40 ADDED CODE TO READ IN THE UPPER AND LOWER LATITUDE BOUNDARIES
C                  FOR THE INTERPOLATION OF THE PREDICTED CLEAR TEMPERATURES
C                  WITHIN THESE LATITUDES.
C                  THESE ARRAYS ARE NOW PASSED INTO THE ROUTINE PRDHIR.
C                  THE LATITUDE BOUNDARYS ARE READ FROM THE SOFTFLAGS FILE.
C                  (WR#176)
C    VERSION- 3.41 ADDED CODE TO CHECK THE ESTIMATED SURFACE TEMPERATURE
C                  ESTIMATE OF HIRS CH8 MINUS THE HIRS CH 8 AGAINST A SOFT
C                  CODED THRESHOLD. IF THE DIFFERENCE IS LESS THAN THE THRESHOLD
C                  THEN DO THE RESTRAHLEN TEST OTHERWISE LET IT PROCEED THRU
C                  THE ALGORITHM. (WR#182)
C    VERSION- 3.43 CHANGED THE ALGORITHM SELECTION BASED ON THE SIGN OF
C                  THE NUMERATOR AND DENOMINATOR FOR CALCULATING CLOUD
C                  AMOUNT. ADDED ARRAYS RDT8CA, RET8MA, AND RCLODA TO
C                  STORE RADIANCE VERSIONS OF THE PREDICTED CLEAR SURFACE
C                  TEMPERATURE, HIRS 8 SURFACE TEMPERATURE ESTIMATE, AND
C                  CLOUD TOP TEMPERATURE AND ADDED CODE TO WRITE THEM TO THE
C                  ROTATING 1T FILE (WR#205/210)
C                  CHANGED THE AMSU-A CHANNELS USED IN MAKING THE PREDICTED
C                  CLEAR TEMPERATURE TO 4-5-6, 5-6-7, OR 6-7-8 DEPENDING
C                  ON TERRAIN TYPE AND ELEVATION.
C    VERSION- 3.44 REPLACED R4SPV EQUIVALENCE TEST WITH ISR4SPV FUNCTION.
C    VERSION- 3.45 UNDER WR-257 REMOVED SLOPES AND INTERCEPTS FROM CLDTOP
C                  CALLING ARGUMENTS AND FROM THE CALLS TO PRDHIR.
C    VERSION- 3.46 UNDER WR-286 INCLUDED COMMON /HIRUSE/ TO PICK UP THE
C                  'H1REPL' FLAG. ADDED CODE TO REPLACE HIRS CHANNEL 1
C                  WITH THE MEAN OF AMSU-A CHANNELS 10-14 WHEN 'H1REPL'
C                  IS TRUE.
C    VERSION- 4.00 CHANGED CALL TO WRTR1T2000 TO WRTR1T2800.
C
C
C
C    CALLING ARGUMENTS-
C      VARIABLE      TYPE   I/O   DESCRIPTION
C      --------      ----   ---   -----------
C      OBSHIR         REAL   I    OBSERVED HIRS DATA BUFFER DIMENSIONED
C                                 (HNSPOT,HTOTLN,35) (20TH ELT = NODE)
C      OLR,LC1-4      REAL   O    OUTGOING LONGWAVE RADIATION AND 4
C                                 LAYER COOLING RATES, EACH DIMENSIONED
C                                 (HNSPOT X HTOTLN)
C      R1T1ST         LOG    I    SET TO TRUE THE FIRST TIME WRTR1T IS
C                                 CALLED.
C      CLD1ST         LOG    I    SET TO TRUE THE FIRST TIME CLDTOP IS
C                                 CALLED.
C      PRFLAG         LOG    I    ARRAY OF POLAR REDUNDANCY FLAG, DIM
C                                 RNGRDP X RTOTLN.
C      SATID          C*8    I    SATELLITE ID
C      ERSTAT         INT    O    ERROR STATUS, SEE EXIT STATES.
C
C    COMMON AREA PARAMETERS- RETPARM, TRANSM, ATTENU8, RTPLVL
C                            STBGEO, CLIMATE, HIRUSE, CLOUDMSK
C
C    LOCAL VARIABLES-
C      VARIABLE      TYPE   DESCRIPTION
C      --------      ----   -----------
C      I,J           INT    DO LOOP INDEX.
C      II,JJ         INT    ARRAY INDICES.
C      REALSP        REAL   REAL EQUIVALENT OF THE HIRS SPOT NUMBER.
C                           7 & 8 CLEARED HIRS BRIGHTNESS TEMPERATURES.
C      IHIRSP        INT    THE HIRS SPOT NUMBER FROM THE RETANC BUFFER
C                           AS AN INTEGER.
C      BOX           INT    ARRAY (DIMENSION 2 X 2 X 2) HOLDING THE
C                           HIRS SPOT AND LINE NUMBERS OF THE 2 BY 2
C                           BOX USED FOR THE RETRIEVAL.
C     RDATCY         REAL   I/O VECTOR OF DIMENSION 62, CONTAINING
C                           42 TOVS TEMPERATURE PROFILE(K), 19
C                           WATER VAPOR MIXING RATIOS ALOG(G/KG)
C                           AND 1 SURFACE TEMPERATURE (K).
C     A              REAL   RADIUS OF THE EARTH (M).
C     CLRPCT         REAL   CLEAR FRACTION (INVERSE OF CLOUD AMOUNT)
C     DEWPT          REAL   VECTOR OF LENGTH 2 OF DEW POINTS (C).
C     RELHUM         REAL   VECTOR OF LENGTH 2 CONTAINING THE
C                           RELATIVE HUMIDITY (%).
C     TKTRP          REAL   TROPOPAUSE TEMPERATURE (K).
C     TROPRS         REAL   TROPOPAUSE PRESSURE (MB).
C     TEMPC          REAL   VECTOR OF 2 CONTAINING THE TEMP IN (C).
C     LEVS           REAL   VECTOR OF 2 HOLDING THE PRESSURE LEVELS.
C     GOHGKM         REAL   VECTOR OF 40 GEOMETRIC HEIGHTS (KM).
C     LAYERS         REAL   VECTOR OF LENGTH 40 CONTAINING LAYER
C                           THICKNESS AT THE 40 TOVS LEVELS.
C     N12WVC         REAL   TOVS WATER VAPOR ATTENUATION COEFFICIENTS
C                           FOR NOAA-12
C     N14WVC         REAL   TOVS WATER VAPOR ATTENUATION COEFFICIENTS
C                           FOR NOAA-14
C     SFCTYP         INT    TERRAIN TYPE (0=SEA,1=LAND,2=COAST,10=SEA ICE,
C                                         11=SNOW)
C     PWATER         REAL   ARRAY (RETPARM:PRFTLV) OF PRECIPITAL WATER
C     APRES          REAL   ARRAY (RETPARM:PRFTLV) OF PRESSURE LEVELS
C     WPROFI         REAL   ARRAY (RETPARM:PRFTLV) CONTAINING ATM.
C                           MIXING RATIO PROFILE.
C     INTO3          REAL   ARRAY (RETPARM:PRFTLV) OF INTEGRATED OZONE
C                           PROFILE
C     PROFLV         REAL   VECTOR CONTAINING THE PRESSURE VALUES (MB)
C                           AT EACH OF THE RETRIEVAL INTERPOLATED PROFILE
C                           LEVELS (DIM RETPARM:PRFTLV).
C     CHAN           INT    DO LOOP INDEX FOR CHANNEL
C     CHNBUF         REAL   NUMBER OF CHANNELS TO COMPUTE TRANSMIT
C                           ARRAY (NUMCHN)
C     NEWCH8         REAL   WAS WATER VAPOR ATTENUATED HIRS CHANNEL 8
C                           NOW IT JUST HIRS CHANNEL 8
C     DIFLMT         REAL   THE HIGH TERRAIN (> 1500M) TEMPERATURE
C                           DIFFERENCE LIMIT
C     DIFLMT2        REAL   THE VERY HIGH TERRAIN (> 2900M) TEMPERATURE
C                           DIFFERENCE LIMIT
C     CLDSWH         REAL   A THRESHOLD USED AFTER THE ORIGINAL CLOUD AMOUNT
C                           ALGORITHM IS APPLIED TO DETERMINE WHETHER OR
C                           NOT TO USE THE INVERTED CLOUD ALGORITHM.
C     CLRTHRES       REAL   A VALUE WHICH DETERMINES HOW MUCH CLOUD
C                           CONSTITUTES CLEAR SKY FOR THE CLEAR SKY
C                           RADIATION PARAMETERS.
C     LIMTER         REAL   ARRAY (DIM 5) CONTAINING TERRAIN HEIGHT
C                           LIMITS FOR COMPUTING LAYER COOLING RATES 1-3.
C                           ELEMENTS 4 AND 5 ARE THE LC3/4 PRESSURE
C                           SCREENING LIMITS.
C     UPRLAT(4)      REAL   THE NORTHERNMOST  LATITUDE BOUNDARIES FOR THE
C                           INTERPOALTION OF THE PREDICTED CLEAR TEMPERATURE.
C     LOWLAT(4)      REAL   THE SOUTHERNMOST  LATITUDE BOUNDARIES FOR THE
C                           INTERPOALTION OF THE PREDICTED CLEAR TEMPERATURE.
C     SFC8MC8        REAL   SURFACE TEMPERATURE ESTIMATE FOR HIRS CHANNEL 8
C                           MINUS HIRS CHANNEL 8.
C     RESTHRES       REAL   THE THRESHOLD FOR THE RESTRAHLEN TEST.
C     A10U8(4)       REAL   THE ATTENUATION ABOVE THE CLOUDS FOR CLOUD TOP
C                           TEMPERATURE.
C     PREDTEMP(4)    REAL   THE PREDICTED CLEAR HIRS TEMPERATURE
C     SURFDIFF       REAL   DIFFERENCE BETWEEN SURFACE TEMPERATURE ESTIMATE
C                           AND HIRS CHANNEL 8
C     CLRDIF         REAL   SOFTFLAG THRESHOLD FOR SETTING CLOUD AMOUNT TO
C                           MISSING.
C     SEACLR         REAL   SOFTFLAG VALUE FOR TESTING FOR CLEARS OVER SEA
C     NSEACK         REAL   SOFTFLAG VALUE FOR TESTING FOR AEROSOLS OVER
C                           SEA
C     NLNDCK         REAL   SOFTFLAG VALUE FOR TESTING FOR AEROSOLS OVER
C                           LAND
C     HICHECK        LOG    FLAG THAT CONTROLS WHETHER OR NOT SPECIAL
C                           TESTS FOR HIGH TERRAIN ARE PERFORMED.
C     HIRCHNS        INT    NUMBER OF HIRS CHANNELS FOR WHICH RADIANCES
C                           ARE TO BE COMPUTED BY OPTRAN.
C     PEAKPR        PEAK   TOVS PRESSURE CLOSEST TO WHERE HIRS CHANNELS
C                           1 THROUGH 19 PEAK.
C
C
C    SUBPROGRAMS CALLED- INTPAMA, INTPAMA2, PRDHIR, GRD2X2, CLDTEM, CLDPRS, WRTR1T,
C                        TROPLV, OPTRAN, TO3PPMV, REALCOEF
C
C    FUNCTIONS USED- MOD, NINT, REAL, PLKRAD, THICKNESS, TDEW2MR, DEWPOINT, ECACPE
C
C
C    EXIT STATES-
C
C***********************************************************************
C
      INCLUDE 'RETPARM'
      INCLUDE 'RTPLVL'
      INCLUDE 'RTVPARM'
      INCLUDE 'RTVGRD'
      INCLUDE 'SPVAL'
      INCLUDE 'SPGUNT'
      INCLUDE 'TRANSM'
      INCLUDE 'ATTENU8'
      INCLUDE 'CLIMATE'
      INCLUDE 'INSPARM'
      INCLUDE 'INSCOM'
      INCLUDE 'STBGEO'
      INCLUDE 'CLOUDMSK'
      INCLUDE 'HIRUSE'
      INCLUDE 'FRMINF'
      INCLUDE 'HIRSTAT'
C
      INTEGER   QCFLAG(4),AEROCNT,EXTCNT,CTPEXIT(15),AEROQC2,COUNTQC3
      INTEGER   MINUS1, COUNTQC0, COUNTQC1, COUNTQC4, COUNTQC5
      INTEGER   ERSTAT, STATUS, CHNBUF(NUMCHN),IERR, NCH1GUESS(4)
      INTEGER   BOX(2,2,2), I, J, K, L, II, JJ, IHIRSP
      INTEGER   NCH1,NCH2,SPOT,CPELT5PCT
      INTEGER   JCH,LV,JC,IDUM, TERHGT,HEIGHT(4)
      INTEGER   LVL,CHAN,WRTCNT,ITER,CTPITER(4)
      INTEGER   ILEVEL,TERTYP(4),SFCTYP
      INTEGER   HIRCHNS, D1, D2, D3, SFCLVL, TROLEV, PASSFAIL(4)
      INTEGER   SFCLEV(4), UPCHAN(4), DNCHAN(4)
      INTEGER   CLDAMT, HIRSPT
      INTEGER   FAILCOUNT
      REAL   OLR(HNSPOT,HTOTLN), LC1(HNSPOT,HTOTLN),RADT8C,RADT8M,RADT8CU(4)
      REAL   LC2(HNSPOT,HTOTLN), LC3(HNSPOT,HTOTLN),TDEW2MR, DEWPOINT,TKTRP
      REAL   TKTRP1, TROPRS1, TKTRP2, TROPRS2, HIR8OBS
      REAL   THICKNESS,DELPRS8(4,19),DELPRS(19)
      REAL   DELPRS1(4),DELPRS2(4,19),DELPRS3(4,19),DELPRS4(4,19)
      REAL   DELPR1,DELPR2(19),DELPR3(19),DELPR4(19)
      REAL   LC4(HNSPOT,HTOTLN),PRESSX,RADTEMP
      REAL   OBSHIR(HNSPOT,HTOTLN,35), RADT(4)
      REAL   TPROFI(PRFTLV),TCLEAR(28), BTT(4)
      REAL   CLTTMP(4),CLAMTS(4),PCLOUD(4),XTBX(4,8)
      REAL   TO8OBS(8),TO8CLR(8),CPRESS
      REAL   TAUIR(PRFTLV,11),TAUHRS(PRFTLV,11),DELTAT(8),PLKRAD,IPLKRD
      REAL   PSFC(4), CTTEMP, SFCPRS, PRSSFC, CTT46, CTT48, CTT57, CTT78
      REAL   CTT58
      REAL   AVGRADIUS,RDATCY(62)
      REAL   TROPRS, TEMPC(2), DEWPT(2), C2, TCLR8(28),TCLR84(4,28)
      REAL   RELHUM(2), LEVS(2), GOHGKM(40), LAYERS(40)
      REAL   NEWCH8(HNSPOT,HTOTLN),TTLOZ,RCLOUD,RNUMER,RDENOM,RADCTT(4)
      REAL   APRES(PRFTLV),PWATER(PRFTLV),WPROFI(PRFTLV),QO3(PRFTLV)
      REAL   PTRANS(PRFTLV,35),CGRAV,PATH,DELP,TEMPLMT,AT8THRS
      REAL   DIFLMT2,BTSIMX
      REAL   DELSFCT,TSFCOB(4),RSFCOB,SAMOUNT,DIFLMT
      REAL   CLRTHRES,LIMTER(5),ANG,CLDSWH
      REAL   RESTHRES,A10U8(4),PREDTEMP(4), PCLRBIAS(4)
      REAL   BCORSTE(4),BIASPCLR(4),BIASPCL8(4), DELTAU
      REAL   RDT8CA(4),RDT8MA(4),RCLODA(4), AROSOL(4)
      REAL   H8RAD(4),OH8RAD(4), RADTC8,RDTC8A(4),PREDTEM8(4)
      REAL   CLRDIF,SEACLR,NSEACK,H8TEMP(4),QCLAND,NLNDCK
      REAL   SOLZEN(4), UPRLAT(4), LOWLAT(4), TCLOUD(4), ECAORIG(4)
      REAL   TCLEAR4(4,28), MHSCH3(4), MHSCH5(4), MAXPRS
      REAL   T45678(4,7,5), RADT8C8(4),DELTAT8
      REAL   PRD567(4,7,5),PRD678(4,7,5),PRD789(4,7,5),LATS(4),LONS(4)
      REAL   PCLRCF(4,7,5), PCLRCF4(4,4,7,5), H8STE(56,6)
      REAL   H8STER(4), PEAKPR(19), FOVLAT, FOVLON
      REAL   HIRRAD(4,19), RTEMP
      REAL   CCHRAD(4,19),BTHIRS(4,19), CONPCT(4,19), RCCCTT(4),RCC8STE(4)
      REAL   RCCPC8(4),CPERROR(4), PRSTRO(4),HIROBS(19),CC8STE, STECH8(4)
      REAL   TMPTRO(4), CHNERRORIG(4,19), CHNERRFIN(4,19)
      REAL   PRSTRO1(4), RADSTE(4)
      REAL   AMSU4(4),AMSU5(4),AMSU6(4), AMSU1(4), AMSU15(4), CLW(4)
      REAL   RCBIAS(4), DENOMTH
      REAL   LOGSFC(4), LOGCTP(4), LOGPEAK(4,19)
      REAL   PRSTRO2(4), TMPTRO2(4)
      REAL   HBTCCL(4,19), TMPTRO1(4)
      REAL   LPINDX(4)
      REAL   PLSAV, PLNEW, MINPRESS, DELTAPRS, PCTROP, DELPRSX, DELPRESX(4)
      REAL   MINECA, XLAT, FACTOR, ECACPE
      REAL   OPDEPTH(4), ATNPCLR8(4),BCATPCLR8(4),RADBCATPC8(4)
      REAL   PREDTEM8COR(4), BIASCOR, RADCLR(4), ALBEDO(4),T_VALUE(4)
      PARAMETER (MINECA = 0.005)
      integer target, counter



      CHARACTER*8 SATID, SUBNAM
      CHARACTER*2 C2MSG
      CHARACTER*3 C3MSG
      CHARACTER*6 C6MSG
      CHARACTER*12 C12MSG
      CHARACTER*100 KMSG
      LOGICAL   R1T1ST, CLD1ST, PRFLAG(RNGRDP, RTOTLN),METOP
      LOGICAL   TROP1, TROPO, ISISO, ADJUST, H20ADJ(4)
      LOGICAL   ISR4SPV, ISAEROSOL, NIGHT(4), AEROSOL(4), DAY(4)
      LOGICAL   TRANS1ST, QC0FAIL(4), ECAFLAG
      DATA TRANS1ST/.TRUE./
      DATA IERR,ANG/0,0.0/
      DATA C2/14.64285/
C-- SET CHNBUF TO HIRS CHANNELS 1-19 FOR OPTRAN
      DATA CHNBUF /1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,16*0/
      data counter/0/
      parameter (target=150000)
      PARAMETER   (AVGRADIUS = 6356.766E3)
      PARAMETER (CGRAV=5.098581E-4)
      PARAMETER (DENOMTH = 15.0)
      SAVE TEMPLMT,AT8THRS,DIFLMT,DIFLMT2,CLDSWH,CLRTHRES,LIMTER,
     >UPRLAT,LOWLAT,RESTHRES,CLRDIF,SEACLR,NSEACK,PRD567,PRD678,PRD789,
     >NLNDCK,T45678,H8STE,TRANS1ST, counter
C
      GOHGKM(40) = 0
      LAYERS(40) = 0
      WRTCNT = 0
      HIRCHNS = 19
      SUBNAM = 'SPGDVR  '

      PRINT *,'CLDTOP: METOP FLAG = ',METOP
C
C  READ COEFFICIENTS FOR HIRS CH.4,5,6,7 & 8 RADIANCES PREDICTION
C
       IF (CLD1ST) THEN
C
         CALL OPCOEF(ERSTAT)
C
C  GET COEFFICIENTS FOR PREDICTING HIRS CHANNELS 4-8 FROM THE CDB.
C
         CALL REALCOEF(CDBUNT,'RETRIEVE','CLDPARAM','PRD45678',4,7,5,0,
     1                 T45678,'GET',STATUS)
         IF (STATUS.NE.0) PRINT *,'T45678 STATUS: ',STATUS
         CALL REALCOEF(CDBUNT,'RETRIEVE','CLDPARAM','PRDAM567',4,7,5,0,
     1                 PRD567,'GET',STATUS)
         IF (STATUS.NE.0) PRINT *,'PRD567 STATUS: ',STATUS
         CALL REALCOEF(CDBUNT,'RETRIEVE','CLDPARAM','PRDAM678',4,7,5,0,
     1                 PRD678,'GET',STATUS)
         IF (STATUS.NE.0) PRINT *,'PRD678 STATUS: ',STATUS
         CALL REALCOEF(CDBUNT,'RETRIEVE','CLDPARAM','PRDAM789',4,7,5,0,
     1                 PRD789,'GET',STATUS)
         IF (STATUS.NE.0) PRINT *,'PRD789 STATUS: ',STATUS
C
C   GET MIN/MAX LIMITS FOR GROSS ERROR CHECKING OF BRIGHTNESS
C   TEMPERATURES FROM THE CDB.
C
         CALL REALCOEF(CDBUNT,'RETRIEVE','CLDPARAM','CLDMXTMP',7,0,0,0,
     1                 CCMXTP,'GET',STATUS)


         CALL REALCOEF(CDBUNT,'RETRIEVE','CLDPARAM','CLDMNTMP',7,0,0,0,
     1                 CCMNTP,'GET',STATUS)
C
C   GET COEFFICIENTS USED IN MAKING A HIRS 8 SURFACE TEMPERATURE ESTIMATE.
C
         CALL REALCOEF(CDBUNT,'CLOUDCLR','REGCOEF ','H8STE   ',56,6,0,0,
     1                 H8STE,'GET',STATUS)

C
C   CALL TO2PRF TO OBTAIN AN ARRAY OF INTEGRATED OZONE PROFILE
C   (INTO3) FROM THE TOTAL OZONE (TTLOZ)
C
           TTLOZ=320.0

           CALL TO3PPMV(TTLOZ,INTO3)
C
C  READ WATER ATTENUATED HIRS CH. 8 TEMPERATURE LIMIT,
C  THE INITIAL CLEAR/CLOUDY THRESHOLD LIMIT AND THE HIGH TERRAIN
C  TEMPERATURE DIFFERENCE LIMIT.
C
           READ(70,*) TEMPLMT,AT8THRS,DIFLMT,DIFLMT2,CLDSWH,CLRTHRES,
     >                RESTHRES
C
C  READ THE TERRAIN HEIGHT LIMITS FOR COMPUTING LAYER COOLING RATES 1-3.
C
           READ(70,*) LIMTER
C
C  READ THE UPPER LATITUDE LIMITS FOR APPLYING THE INTERPOLATED COEFFICIENTS
C
           READ(70,*) (UPRLAT(I),I=1,4)
C
C  READ THE LOWER LATITUDE LIMITS FOR APPLYING THE INTERPOLATED COEFFICIENTS
C
           READ(70,*) (LOWLAT(I),I=1,4)
C
C  READ THE TEMPERATURE DIFFERENCE THRESHOLD, THE SEA CLEAR BRIGHTNESS
C  TEMPERATURE THRESHOLD, AND THE PRIMARY AND SECONDARY NON-SEA CLEAR
C  BRIGHTNESS TEMPERATURE THRESHOLDS.
C
           READ(70,*) CLRDIF, SEACLR, NSEACK, QCLAND, NLNDCK
C
C  READ IN THE BIAS CORRECTIONS FOR DOWNWARD LONGWAVE RADIATION CALCULATION
C  AND STORE THE VALUES IN COMMON /CLIMATE/.
C
           DO I = 1, 56
              READ(301,*) J, DLRBIAS(J)
           END DO

           CLD1ST = .FALSE.
        ENDIF
C
C  ADJUST AMSU-A CHANNELS 4-7 FOR PRECIPITATION CONTAMINATION, IF
C  NECESSARY
C
      CALL INTPAMA(HIRANC,HIRDAT)
C
C  ADJUST AMSU-A CHANNELS 6-8 OVER LAND AND COAST FOR PRECIPITATION
C  CONTAMINATION.
C
      CALL INTPAMA2(HIRANC,HIRDAT)
C
C  REPLACE THE WATER VAPOR ATTENUATED HIRS CH8 WITH JUST HIRS CHANNEL 8
C
      DO I=1,HNSPOT
         DO J=1,HTOTLN
            NEWCH8(I,J) = HIRDAT(I,J,8)
            IF(NEWCH8(I,J).LE.150.0) NEWCH8(I,J)=R4SPV
         END DO
      END DO

C INITIALIZE WATER VAPOR MIXING RATIO (WPROFI) ARRAY

      DO I = 1, PRFTLV
         WPROFI(I)=.003
      END DO
C
C  LOOP OVER EACH GRID POINT, COMPUTING CLOUD PARAMETERS FOR THE
C  SURROUNDING 2X2 OF EACH GRID POINT.
C
      DO 190 J = 1, RTOTLN
         DO 180 I = 1, RNGRDP


C  DETERMINE THE HIRS SPOT AND LINE FROM THE SPOT NUMBER
C  IN THE FIRST ELEMENT OF THE RETRIEVAL ANCILLARY BUFFER.
C  IF THIS IS MISSING, COMPLETELY SKIP THIS SPOT.
C
            IF(ISR4SPV(RETANC(I,J,1))) THEN
              GO TO 180
            END IF
C
C  DETERMINE THE HIRS SPOT LOCATION FROM THE RETRIEVAL SPOT
C  LOCATION.
C
            IHIRSP = NINT(RETANC(I,J,1))
            IF(IHIRSP.EQ.0) GO TO 180
C
C  DETERMINE THE 2X2 ABOUT THE RETRIEVAL GRID POINT.
C
            CALL GRD2X2(HNSPOT,IHIRSP,BOX)
C
C  INITIALIZE THE CLOUD PARAMETERS IN THE HIRS ANCILLARY
C  BUFFER TO MISSING VALUES.
C
            DO K = 1, 2
               DO L = 1, 2
                  HIRANC(BOX(K,L,1),BOX(K,L,2),15) = R4SPV
                  HIRANC(BOX(K,L,1),BOX(K,L,2),18) = R4SPV
                  HIRANC(BOX(K,L,1),BOX(K,L,2),19) = R4SPV
                  HIRANC(BOX(K,L,1),BOX(K,L,2),26) = R4SPV
               END DO
            END DO
C
C  CHECK FOR POLAR REDUNDANCY. IF POLAR REDUNDANCY FLAG IS FALSE
C  THEN THE DATA IS OK. IF TRUE, THEN THE DATA IS POLAR
C  REDUNDANT AND IT SHOULD BE SKIPPED.
C
C           IF(PRFLAG(I,J)) GO TO 180
C
C  SKIP OVER ANY HOHIRS RETRIEVALS.
C
            IF (BADRET(I,J)) GO TO 180
C
C    COMPUTE THE LINE NUMBER
C
            JJ = (IHIRSP / HNSPOT) + 1
C
C    COMPUTE THE SPOT NUMBER ON THE LINE
C
            II = MOD(IHIRSP,HNSPOT)
C
C    IF THE HIRS SPOT NUMBER IN THE FRAME IS EVENLY DIVISIBLE
C    BY THE NUMBER OF SPOTS ON A HIRS LINE, SET THE SPOT
C    NUMBER TO THE MAXIMUM NUMBER OF SPOTS ON A HIRS LINE.
C
            IF(II .EQ. 0) THEN
               JJ = JJ - 1
               II = HNSPOT
            ENDIF
C
C
C    GET ATMOSPHERIC PROFILE, CLEAR BRIGHTNESS TEMPERATURES, AND
C    CLEAR RADIANCES FOR HIRS CHANNELS 3-8.  IF ANY OF THESE VALUES
C    IS MISSING, SKIP THIS GRID POINT.  SAVE WARMEST TEMPERATURE OF
C    THE BOTTOM 8 LEVELS.
C
            DO LVL=1,PRFTLV
C
C  IF THERE IS A MISSING TEMPERATURE IN THE RETRIEVAL PROFILE,
C  SKIP THIS RETRIEVAL.
C
                IF (LVL.LT.41) THEN
                   IF(ISR4SPV(RETDAT(I,J,LVL))) GO TO 180
                   TPROFI(LVL) = RETDAT(I,J,LVL)
                   RDATCY(LVL) = RETDAT(I,J,LVL)
                ELSE
                   IF (LVL.EQ.41) TPROFI(LVL) = 209.0
                   IF (LVL.EQ.42) TPROFI(LVL) = 210.0
                END IF
C
C  FILL THE WATER VAPOR MIXING RATIO ARRAY (WPROFI).
C
                IF(LVL.GT.23.AND.(.NOT. ISR4SPV(RETDAT(I,J,LVL+19)))) THEN
                   WPROFI(LVL) = RETDAT(I,J,LVL+19)
                ELSE
                   WPROFI(LVL) = 0.003
                ENDIF
            END DO
C
C PUT THE RETRIEVED WVMR PROFILE AND RETRIEVED SURFACE TEMPERATURE
C INTO THE RDATCY ARRAY.
C
            DO LVL=43,62
               RDATCY(LVL)= RETDAT(I,J,LVL)
            ENDDO
C
C    PREPARE PRESSURE VALUES FOR USE IN OPTRAN
C
C    APRES = ATMOSPHERIC PRESSURE LEVELS.
C
            DO LVL = 1, PRFTLV
               APRES(LVL) = PROFLV(LVL)
            END DO
C
C   COMPUTE PRECIPITABLE WATER (PWATER) FOR TRANSMITTANCE CALCULATIONS
C
C  IF ANGLE (ANG) IS CHANGED TO ANYTHING BUT ZERO MAKE SURE IT IS
C  IN RADIANS


            PATH = CGRAV/COS(ANG)
            PWATER(1) = 0.0
            DO LVL = 2, PRFTLV
               IF((.NOT.ISR4SPV(APRES(LVL))).AND.
     +            (.NOT.ISR4SPV(APRES(LVL-1))).AND.
     +            (.NOT.ISR4SPV(WPROFI(LVL))).AND.
     +            (.NOT.ISR4SPV(WPROFI(LVL-1)))) THEN
                  DELP = ABS(APRES(LVL) - APRES(LVL-1))

                  PWATER(LVL)=PWATER(LVL-1) + PATH*(WPROFI(LVL) +
     +                        WPROFI(LVL-1))*DELP
              ENDIF
           END DO
C
C    RUN O3(PPMV) THROUGH THE PW ROUTINE
C
C    QO3 = ADJUSTED OZONE PROFILE.
C
           QO3(1) = 0.0
           DO LVL=2,PRFTLV
              DELP = ABS(APRES(LVL) - APRES(LVL-1))
              QO3(LVL) = QO3(LVL-1) + PATH*
     +                  (INTO3(LVL) + INTO3(LVL-1))*DELP
           ENDDO

C
C    CALCULATE TRANSMITTANCES FOR HIRS CH. 1-8 AT 42 LEVELS.  IF
C    OPTRAN RETURNS ANY ERROR, SKIP THIS GRID POINT.
C
           CALL OPTRAN(PRFTLV,APRES,TPROFI,WPROFI,PWATER,QO3,
     1                 ANG,HIRCHNS,CHNBUF,PTRANS,IERR)
           IF(IERR.NE.0) GO TO 180
C
C    TAUIR = TRANSMITTANCE VALUES FOR HIRS CHS 1-11, FOR
C            40 LEVELS(42 IF AVAILABLE).
C
           DO JCH=1,11
              DO LV=1,PRFTLV
                 TAUIR(LV,JCH) = PTRANS(LV,JCH)
              END DO
           END DO

C-- SAVE PEAK PRESSURES FOR HIRS CHANNELS 1-19

           counter = counter + 1
           DO JCH = 1, 19
              PLSAV = 0.0
              DO LV =2, 40
                 DELTAU = PTRANS(LV-1,JCH)-PTRANS(LV,JCH)
                 DELP = (ALOG(PROFLV(LV))) - (ALOG(PROFLV(LV-1)))
                 PLNEW = DELTAU/DELP
                 IF (PLNEW.GT.PLSAV) THEN
                    PEAKPR(JCH) = PROFLV(LV)
                    PLSAV = PLNEW
                 END IF
              END DO
           END DO

C
C    INITIALIZE BUFFERS
C
            DO SPOT=1,4
               LOGSFC(SPOT) = R4SPV
               LOGCTP(SPOT) = R4SPV
               RADT8C8(SPOT) = R4SPV
               HEIGHT(SPOT) = I2SPV
               QCFLAG(SPOT) = I2SPV
               CLTTMP(SPOT) = R4SPV
               CLAMTS(SPOT)= R4SPV
               AROSOL(SPOT) = R4SPV
               PCLOUD(SPOT) = R4SPV
               TCLOUD(SPOT) = R4SPV
               RCCCTT(SPOT) = R4SPV
               RCC8STE(SPOT) = R4SPV
               RCCPC8(SPOT) = R4SPV
               CPERROR(SPOT) = R4SPV
               ECAORIG(SPOT) = R4SPV
               DO JCH = 1, 19
                  CHNERRORIG(SPOT,JCH) = R4SPV
                  CHNERRFIN(SPOT,JCH) = R4SPV
               END DO
               SFCLEV(SPOT)  = 0
               PSFC(SPOT) = R4SPV
               TSFCOB(SPOT) = R4SPV
               DO CHAN = 1, 19
                  CCHRAD(SPOT,CHAN) = R4SPV
                  LOGPEAK(SPOT,CHAN) = R4SPV
               END DO
               STECH8(SPOT) = R4SPV
               UPCHAN(SPOT) = I2SPV
               DNCHAN(SPOT) = I2SPV
            END DO
C
C****************************************************************
C    DO LOOPS 130,140, AND 150 PERFORM THE FIRST ESTIMATES OF
C    CLOUD TOP TEMPERATURE, CLOUD TOP PRESSURE AND CLOUD AMOUNT
C    USING HIRS CHANNELS 7 & 8.
C****************************************************************
C
C    PICK UP MEASURED BRIGHTNESS TEMPERATURES FOR HIRS AND AMSUA, AND
C    CONVERT TO RADIANCE.  THE FIRST ELEMENT OF THE XTBX
C    ARRAY CORRESPOND TO HIRS-1.
C
            SPOT = 0
            DO 140 K = 1, 2
               DO 130 L = 1, 2

                  SPOT = SPOT + 1
                  AMSU4(SPOT) = R4SPV
                  AMSU5(SPOT) = R4SPV
                  AMSU6(SPOT) = R4SPV
                  AMSU1(SPOT) = R4SPV
                  AMSU15(SPOT) = R4SPV
                  DO CHAN = 1, 19
                     HBTCCL(SPOT,CHAN) = HIRDAT(BOX(K,L,1),BOX(K,L,2),CHAN)
                  END DO
                  ALBEDO(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),20)
                  DO CHAN = 1, 28
                     TCLEAR4(SPOT,CHAN) = R4SPV
                     TCLR84(SPOT,CHAN) = R4SPV
                  END DO
                  H8RAD(SPOT) = R4SPV
                  OH8RAD(SPOT) = R4SPV
                  H8STER(SPOT) = R4SPV
                  MHSCH3(SPOT) = R4SPV
                  MHSCH5(SPOT) = R4SPV
C
C    BUILD THE TCLEAR BT ARRAY FOR EACH HIRS SPOT IN THE 2X2 BOX
C    IF ANY HIRS OR AMSU-A BRIGHTNESS TEMPERATURE IS MISSING,
C    SKIP THE SPOT.
C
                  IF (.NOT.(H1REPL)) THEN
                     TCLEAR(1) = HIRDAT(BOX(K,L,1),BOX(K,L,2),1)
                     IF (ISR4SPV(TCLEAR(1))) GO TO 130
                  ELSE

C-- SKIP THIS SPOT IF H1REPL IS TRUE AND ANY OF AMSU-A
C   CHANNELS 10-14 IS MISSING.
C
                     IF (ISR4SPV(HIRDAT(BOX(K,L,1),BOX(K,L,2),30)) .OR.
     +                   ISR4SPV(HIRDAT(BOX(K,L,1),BOX(K,L,2),31)) .OR.
     +                   ISR4SPV(HIRDAT(BOX(K,L,1),BOX(K,L,2),32)) .OR.
     +                   ISR4SPV(HIRDAT(BOX(K,L,1),BOX(K,L,2),33)) .OR.
     +                   ISR4SPV(HIRDAT(BOX(K,L,1),BOX(K,L,2),34)))
     +               THEN
                         GO TO 130
                     ELSE

C-- REPLACE HIRS CHANNEL 1 WITH THE MEAN OF AMSU-A CHANNELS 10-14 WHEN
C   THE H1REPL FLAG IS TRUE.

                         TCLEAR(1) = (HIRDAT(BOX(K,L,1),BOX(K,L,2),30) +
     +                                HIRDAT(BOX(K,L,1),BOX(K,L,2),31) +
     +                                HIRDAT(BOX(K,L,1),BOX(K,L,2),32) +
     +                                HIRDAT(BOX(K,L,1),BOX(K,L,2),33) +
     +                                HIRDAT(BOX(K,L,1),BOX(K,L,2),34)) / 5.0


                     END IF
                  END IF
                  BTHIRS(SPOT,1) = TCLEAR(1)
                  DO CHAN = 2, 21
                     TCLEAR(CHAN) = HIRDAT(BOX(K,L,1),BOX(K,L,2),CHAN)
                     IF (CHAN.EQ.12) THEN
                        IF (ISR4SPV(TCLEAR(CHAN))) TCLEAR(CHAN) = 140.0
                     END IF
                     IF (CHAN.LE.19) THEN
                        BTHIRS(SPOT,CHAN) = TCLEAR(CHAN)
                        CCHRAD(SPOT,CHAN) = PLKRAD(TCLEAR(CHAN),CHAN)
                        HBTCCL(SPOT,CHAN) = BTHIRS(SPOT,CHAN)
                     END IF
                  END DO

                  OH8RAD(SPOT) = PLKRAD(TCLEAR(8),8)
C
C    GET THE MHS LIMB CORRECTED CHANNEL 2 AND CHANNEL 5
C    BRIGHTNESS TEMPERATURES FOR THIS SPOT.
C
                  MHSCH3(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),165)
                  MHSCH5(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),167)
C
C    GET THE SURFACE TYPE FOR THIS HIRS SPOT. 04/15/03
C
                  SFCTYP = NINT(HIRANC(BOX(K,L,1),BOX(K,L,2),9))
C
C    IF THE HIRS 8 TEMPERATURE IS LESS THAN 150, SKIP THIS SPOT.
C    OTHERWISE COMPUTE THE HIRS 8 RADIANCE.

                  IF (TCLEAR(8).LT.150.0) THEN
                     GO TO 130
                  ELSE
                     H8RAD(SPOT) = PLKRAD(TCLEAR(8),8)
                  END IF
C
C    ADD AMSU-A BT'S FOR CHANNELS 5, 7, AND 9. IF ANY CHANNEL
C    BT IS MISSING, SKIP OUT OF THE LOOP.
C    9/15/03 USE AMSU-A CHANNELS 4, 5, 6, 7, AND 8
C    2/2/04 ADD AMSU-A CHANNEL 9
C    6/27/06 USE PRECIPITATION ADJUSTED AMSU-A CHANNELS 4-7
C
C   -- AMSU-A 4
                  TCLEAR(22)=HIRDAT(BOX(K,L,1),BOX(K,L,2),154)
                  IF (ISR4SPV(TCLEAR(22))) THEN
                     GO TO 130
                  END IF
C   -- AMSU-A 5
                  TCLEAR(23)=HIRDAT(BOX(K,L,1),BOX(K,L,2),155)
                  IF (ISR4SPV(TCLEAR(23))) THEN
                     PRINT *,'TCLEAR(23) ',TCLEAR(23)
                     GO TO 130
                  END IF
C   -- AMSU-A 6
                  TCLEAR(24)=HIRDAT(BOX(K,L,1),BOX(K,L,2),156)
                  IF (ISR4SPV(TCLEAR(24))) THEN
                     PRINT *,'TCLEAR(24) ',TCLEAR(24)
                     GO TO 130
                  END IF
C   -- AMSU-A 7
                  TCLEAR(25)=HIRDAT(BOX(K,L,1),BOX(K,L,2),157)
                  IF (ISR4SPV(TCLEAR(25))) THEN
                     PRINT *,'TCLEAR(25) ',TCLEAR(25)
                     GO TO 130
                  END IF
C   -- AMSU-A 8
                  TCLEAR(26)=HIRDAT(BOX(K,L,1),BOX(K,L,2),158)
                  IF (ISR4SPV(TCLEAR(26))) THEN
                     PRINT *,'TCLEAR(26) ',TCLEAR(26)
                     GO TO 130
                  END IF
C   -- AMSU-A 9
                  TCLEAR(27)=HIRDAT(BOX(K,L,1),BOX(K,L,2),29)
                  IF (ISR4SPV(TCLEAR(27))) THEN
                     PRINT *,'TCLEAR(27) ',TCLEAR(27)
                     GO TO 130
                  END IF
C   -- MHS 3
                  TCLEAR(28)=HIRDAT(BOX(K,L,1),BOX(K,L,2),173)
                  IF (ISR4SPV(TCLEAR(28))) THEN
                     PRINT *,'TCLEAR(28) ',TCLEAR(28)
                     GO TO 130
                  END IF
C
C   MAKE A COPY OF THE TCLEAR ARRAY, THEN REPLACE PRECIP ADJUSTED
C   AMSU-A CHANNELS 4-8 WITH OBSERVED VALUES.
C
                  DO JCH = 1, 28
                     TCLR8(JCH) = TCLEAR(JCH)
                  END DO
                  TCLR8(22)=HIRDAT(BOX(K,L,1),BOX(K,L,2),24)
                  TCLR8(23)=HIRDAT(BOX(K,L,1),BOX(K,L,2),25)
                  TCLR8(24)=HIRDAT(BOX(K,L,1),BOX(K,L,2),26)
                  TCLR8(25)=HIRDAT(BOX(K,L,1),BOX(K,L,2),27)
                  TCLR8(26)=HIRDAT(BOX(K,L,1),BOX(K,L,2),28)
                  TCLR8(28)=HIRDAT(BOX(K,L,1),BOX(K,L,2),165)

                  LATS(SPOT) = HIRANC(BOX(K,L,1),BOX(K,L,2),1)
                  LONS(SPOT) = HIRANC(BOX(K,L,1),BOX(K,L,2),2)
C
C  FOR SPOTS POLEWARD OF +/- 46 DEGREES) USE EITHER THE MEASURED AMSU-A
C  CHANNEL BT OR THE CLW ADJUSTED AMSU-A CHANNEL BT, WHICHEVER IS SMALLER,
C  TO COMPUTE THE PREDICTED CLEAR HIRS CHANNEL BTS.
C
                  TERTYP(SPOT) = SFCTYP
                  IF (ABS(LATS(SPOT)).GT.42.0) THEN
                     IF (TCLEAR(22).GT.TCLR8(22)) THEN
                        TCLEAR(22) = TCLR8(22)
                        HIRDAT(BOX(K,L,1),BOX(K,L,2),154) = TCLEAR(22)
                     END IF
                     IF (TCLEAR(23).GT.TCLR8(23)) THEN
                        TCLEAR(23) = TCLR8(23)
                        HIRDAT(BOX(K,L,1),BOX(K,L,2),155) = TCLEAR(23)
                     END IF
                     IF (TCLEAR(24).GT.TCLR8(24)) THEN
                        TCLEAR(24) = TCLR8(24)
                        HIRDAT(BOX(K,L,1),BOX(K,L,2),156) = TCLEAR(24)
                     END IF
                     IF (TCLEAR(25).GT.TCLR8(25)) THEN
                        TCLEAR(25) = TCLR8(25)
                        HIRDAT(BOX(K,L,1),BOX(K,L,2),157) = TCLEAR(25)
                     END IF
                     IF (TCLEAR(26).GT.TCLR8(26)) THEN
                        TCLEAR(26) = TCLR8(26)
                        HIRDAT(BOX(K,L,1),BOX(K,L,2),158) = TCLEAR(26)
                     END IF
                     IF (TCLEAR(28).GT.TCLR8(28)) THEN
                        TCLEAR(28) = TCLR8(28)
                        HIRDAT(BOX(K,L,1),BOX(K,L,2),173) = TCLEAR(28)
                     END IF
                  END IF
C
C   GET THE SOLAR ZENITH ANGLE FOR THIS SPOT. IF THE SOLAR
C   ZENITH ANGLE IS GREATER THAN 95 DEGREES, SET THE 'NIGHT'
C   FLAG TO TRUE. OTHERWISE SET THE 'NIGHT' FLAG TO FALSE.
C
                  SOLZEN(SPOT) = HIRANC(BOX(K,L,1),BOX(K,L,2),3)
                  IF (SOLZEN(SPOT).GT.95.0) THEN
                     NIGHT(SPOT) = .TRUE.
                     DAY(SPOT) = .FALSE.
                     ALBEDO(SPOT) = 0.0
                  ELSE
                     NIGHT(SPOT) = .FALSE.
                     DAY(SPOT) = .TRUE.
                  END IF

C
C  CALCULATE A HIRS 8 SURFACE TEMPERATURE ESTIMATE USING COEFFICIENTS
C  DERIVED FROM RECORDS IN THE SHORT RRODF. IF THE SURFACE TEMPERATURE
C  ESTIMATE IS COOLER THAN HIRS CHANNEL 8 SET THE SURFACE TEMPERATURE
C  ESTIMATE TO HIRS CHANNEL 8 BRIGHTNESS TEMPERATURE.
C
                  DO JCH = 1, 19
                     HIROBS(JCH) = BTHIRS(SPOT,JCH)
                     HBTCCL(SPOT,JCH) = BTHIRS(SPOT,JCH)
                  END DO

                  STECH8(SPOT) = CC8STE(HIROBS,H8STE)
                  IF (STECH8(SPOT).LT.HIROBS(8)) THEN
                     STECH8(SPOT) = HIROBS(8)
                  END IF
                  IF (STECH8(SPOT).GT.0.0) THEN
                     H8STER(SPOT) = PLKRAD (STECH8(SPOT),8)
                  ELSE
                     H8STER(SPOT) = R4SPV
                  END IF

                  RADSTE(SPOT) = H8STER(SPOT)
                  RCCCTT(SPOT) = H8STER(SPOT)

                  DO CHAN=1,8
                     XTBX(SPOT,CHAN)=HIRDAT(BOX(K,L,1),BOX(K,L,2),CHAN)
C
C  IF HIRS CH 8 USE THE WATER VAPOR ATTENUATED HIRS CH 8.
C

                     IF(CHAN.EQ.3) JC = 1
                     IF(CHAN.EQ.4) JC = 2
                     IF(CHAN.EQ.6.OR.CHAN.EQ.7) JC = 3
                     IF(CHAN.EQ.8) JC = 4
                     IF (CHAN.GT.2) THEN
                        IF(XTBX(SPOT,CHAN).GE.CCMNTP(JC).AND.
     1                     XTBX(SPOT,CHAN).LE.CCMXTP(JC)) THEN
                        ELSE
                           IF(XTBX(SPOT,CHAN).NE.R4SPV) THEN
                              WRTCNT = WRTCNT + 1
                              IF(WRTCNT.EQ.1) THEN
        KMSG(1:50)='* WARNING * TEMP OUT OF RANGE FOR SPOT    (BOX   ,'
        KMSG(51:100)=',  ) CH.   =              (RANGE    -   )'
                                 WRITE(C2MSG,'(I2)') SPOT
                                 KMSG(40:41) = C2MSG
                                 WRITE(C2MSG,'(I2)') BOX(K,L,1)
                                 KMSG(48:49) = C2MSG
                                 WRITE(C2MSG,'(I2)') BOX(K,L,2)
                                 KMSG(52:53) = C2MSG
                                 WRITE(C2MSG,'(I2)') JC
                                 KMSG(59:60) = C2MSG
                                 WRITE(C12MSG,'(F12.2)') XTBX(SPOT,CHAN)
                                 KMSG(64:75) = C12MSG
                                 IDUM=INT(CCMNTP(JC))
                                 WRITE(C3MSG,'(I3)') IDUM
                                 KMSG(84:86) = C3MSG
                                 IDUM=INT(CCMXTP(JC))
                                 WRITE(C3MSG,'(I3)') IDUM
                                 KMSG(88:90) = C3MSG
                                 PRINT *,KMSG
                                 CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
                              ENDIF
                           ENDIF
                           GO TO 130
                        ENDIF
                     ENDIF
                  END DO
C
C  FOR NON-SEA SPOTS THE TARM PATH CALLS CLOUDY, GET THE PREDICTED HIRS
C  TEMPERATURE. USE SEPARATE SETS OF COEFFICIENTS BASED ON TERRAIN
C  HEIGHT.
C
                  TERHGT=NINT(HIRANC(BOX(K,L,1),BOX(K,L,2),8))
                  HEIGHT(SPOT) = TERHGT
C
C  EXTRACT THE FORECAST SURFACE PRESSURE FOR THIS SPOT.
C
                  PSFC(SPOT) = HIRANC(BOX(K,L,1),BOX(K,L,2),12)

                  IF (TERTYP(SPOT).NE.0) THEN
C
C SELECT THE COEFFICIENTS TO BE USED BASED ON TERRAIN HEIGHT.
C
                     IF (TERHGT.LT.1600) THEN
                        DO D1 = 1, 4
                           DO D2 = 1, 7
                              DO D3 = 1, 5
CCFT                             PCLRCF(D1,D2,D3) = PRD567(D1,D2,D3)
C CHANGE LOW TERRAIN FROM PRD567 TO PRD678 - 15 AUG 2006
                                 PCLRCF(D1,D2,D3) = PRD678(D1,D2,D3)
                                 PCLRCF4(SPOT,D1,D2,D3) = PCLRCF(D1,D2,D3)
                              END DO
                           END DO
                        END DO
                     END IF
                     IF (TERHGT.GE.1600.AND.TERHGT.LE.2900) THEN
                        DO D1 = 1, 4
                           DO D2 = 1, 7
                              DO D3 = 1, 5
                                 PCLRCF(D1,D2,D3) = PRD678(D1,D2,D3)
                                 PCLRCF4(SPOT,D1,D2,D3) = PCLRCF(D1,D2,D3)
                              END DO
                           END DO
                        END DO
                     END IF
                     IF (TERHGT.GT.2900) THEN
                        DO D1 = 1, 4
                           DO D2 = 1, 7
                              DO D3 = 1, 5
                                 PCLRCF(D1,D2,D3) = PRD789(D1,D2,D3)
                                 PCLRCF4(SPOT,D1,D2,D3) = PCLRCF(D1,D2,D3)
                              END DO
                           END DO
                        END DO
                     END IF
                  ELSE
C
C DO THE SAME FOR ALL SEA SPOTS, WHETHER CLEAR OR CLOUDY.
C
                     DO D1 = 1, 4
                        DO D2 = 1, 7
                           DO D3 = 1, 5
                              PCLRCF(D1,D2,D3) = T45678(D1,D2,D3)
                              PCLRCF4(SPOT,D1,D2,D3) = PCLRCF(D1,D2,D3)
                           END DO
                        END DO
                     END DO
                  END IF


                  HIRSPT = BOX(K,L,1)
C
C
C GET PREDICTED CLEAR CHANNEL BTS USING OBSERVED AMSU-A CHANNELS
C
                  AMSU4(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),24)
                  AMSU5(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),25)
                  AMSU6(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),26)

                  CALL PRDHIR(TERTYP(SPOT),TERHGT,TCLR8,
     +                 LATS(SPOT),PCLRCF,UPRLAT,LOWLAT,AMSU4(SPOT),AMSU5(SPOT),
     +                 AMSU6(SPOT),HIRSPT,METOP)
                  DO D1 = 4, 8
                     IF (TCLR8(D1).EQ.R4SPV) THEN
                        PRINT *,'TCLR8(',D1,') IS MISSING. SPOT SKIPPED'
                        GO TO 180
                     END IF
                  END DO

C CALCULATE PREDICTED CLEAR BRIGHTNESS TEMPERATURES FOR HIRS CHANNELS
C 2 THROUGH 8 USING PRECIP ADJUSTED AMSU-A CHANNELS.
C
                  AMSU4(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),154)
                  AMSU5(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),155)
                  AMSU6(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),156)
C
                  CALL PRDHIR(TERTYP(SPOT),TERHGT,TCLEAR,
     +                 LATS(SPOT),PCLRCF,UPRLAT,LOWLAT,AMSU4(SPOT),AMSU5(SPOT),
     +                 AMSU6(SPOT),HIRSPT,METOP)
                  DO D1 = 4, 8
                     IF (TCLEAR(D1).EQ.R4SPV) THEN
                        PRINT *,'TCLEAR(',D1,') IS MISSING. SPOT SKIPPED'
                        GO TO 180
                     END IF
                  END DO
C
C-- FOR HIRS CHANNELS 2 THROUGH 8, IF THE OBSERVED BRIGHTNESS TEMPERATURE IS
C   WARMER THAN THE PREDICTED CLEAR BRIGHTNESS TEMPERATURE, REPLACE THE
C   PREDICTED CLEAR BRIGHTNESS TEMPERATURE WITH THE OBSERVED BRIGHTNESS
C   TEMPERATURE
C
C                 DO CHAN = 2, 8
C                    IF (XTBX(SPOT,CHAN) .GT. TCLEAR(CHAN)) THEN
C                       TCLEAR(CHAN) = XTBX(SPOT,CHAN)
C                    END IF
C                    IF (XTBX(SPOT,CHAN) .GT. TCLR8(CHAN)) THEN
C                       TCLR8(CHAN) = XTBX(SPOT,CHAN)
C                    END IF
C                 END DO
C
C   SAVE THE HIRS AND AMSU-A CHANNEL DATA FOR THIS SPOT.
C
                  DO CHAN = 1, 28
                     TCLEAR4(SPOT,CHAN) = TCLEAR(CHAN)
                     TCLR84(SPOT,CHAN)  = TCLR8(CHAN)
                     IF (CHAN.LE.19) THEN
                        CONPCT(SPOT,CHAN) = 0.0
                     END IF
                  END DO
C
C   SAVE THE LIMB-CORRECTED, BUT NOT CLW ADJUSTED, AMSU-A
C   CHANNEL 1 AND 15 TEMPERATURES FOR THIS SPOT.
C
                  AMSU1(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),21)
                  AMSU15(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),35)
C
C   SAVE THE CLOUD LIQUID WATER VALUE FOR EACH SPOT
C
                  CLW(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),149)
C
C SET THE CLOUD TOP PRESSURE TO MISSING
C
                  PCLOUD(SPOT) = R4SPV
C
C ============================================================
C LOOP ONE
C ============================================================
                  TKTRP = R4SPV
C
C   USE OBSERVED HIRS 8 TEMPERATURE AS FIRST GUESS CLOUD TOP
C   TEMPERATURE
                  CLTTMP(SPOT) = XTBX(SPOT,8)
C
C   MAKE A RADIANCE FROM THE FIRST GUESS FOR CLOUD TOP TEMPERATURE
C
                  RADCTT(SPOT) = PLKRAD(CLTTMP(SPOT),8)
C
  130          CONTINUE
  140       CONTINUE
C
            DO 150 SPOT=1,4
               IF ((CLTTMP(SPOT).LT.CCMNTP(7)).OR.CLTTMP(SPOT).GT.
     +             CCMXTP(7)) GO TO 150
C
C   SKIP SPOTS THAT DO NOT HAVE A VALID HIRS CHANNEL 8 SURFACE TEMPERATURE
C   ESTIMATE.
C
               IF (STECH8(SPOT).LE.0.0) GO TO 150
C
C  COMPUTE THE TROPOPAUSE TEMPERATURE FOR EACH RETRIEVAL GRID POINT
C
               GEOHGT(40) = 0.0
               DO 60 K = 40, 2, -1
C
C CONVERT TEMPERATURES FROM KELVIN TO CELSIUS
C
                  TEMPC(1) = RDATCY(K) - 273.16
                  TEMPC(2) = RDATCY(K-1) - 273.16
C
C RELATIVE HUMIDITY NEEDS TO BE OBTAINED UP TO THE
C THE 200 MB LEVEL (TOVS LEVEL 24).
C
                  IF(K .GT. 24) THEN
                     RELHUM(1) = TDEW2MR(PROFLV(K),TEMPC(1))
                     RELHUM(2) = TDEW2MR(PROFLV(K-1),TEMPC(2))
                     RELHUM(1) = (RDATCY(K+19)/RELHUM(1)) * 100
                     RELHUM(2) = (RDATCY(K+18)/RELHUM(2)) * 100
                     DEWPT(1) = DEWPOINT(TEMPC(1),RELHUM(1))
                     DEWPT(2) = DEWPOINT(TEMPC(2),RELHUM(2))
                     LEVS(1) = PROFLV(K)
                     LEVS(2) = PROFLV(K-1)
                     LAYERS(K-1)=THICKNESS(LEVS(2),LEVS,TEMPC,DEWPT,2)
                  ELSE
C
C ABOVE 200 MB COMPUTE THICKNESS WITHOUT DEW POINT
C TEMPERATURES.
C
                     LAYERS(K-1)=C2*(RDATCY(K)+RDATCY(K-1)) *
     1                           ALOG(PROFLV(K)/PROFLV(K-1))
                  ENDIF
C
C UP TO 10 MB THE HEIGHT CAN BE REPRESENTED IN METERS ABOVE THAT
C IN DECAMETERS.
C
                  IF(K-1 .GT. 10) THEN
                        GEOHGT(K-1) = GEOHGT(K) + LAYERS(K-1)
                  ELSE
                     IF(K-1 .EQ. 10) THEN
                        GEOHGT(K-1) = (GEOHGT(K) + LAYERS(K-1)) / 10.0
                     ELSE
                        GEOHGT(K-1) = GEOHGT(K) + (LAYERS(K-1) / 10.0)
                     ENDIF
                  ENDIF
                  GOHGKM(K-1) = (GEOHGT(K-1)*AVGRADIUS)/(AVGRADIUS-GEOHGT(K-1))
                  GOHGKM(K-1) = GOHGKM(K-1)/1000.0
   60          CONTINUE
C
C  CALCULATE TROPOPAUSE LEVEL (TROPRS)
C
C
C-- "FRANK" APPROACH
               CALL TROPLV(PROFLV,GOHGKM,RDATCY,PRFTLV,TKTRP1,TROPRS1)
C-- "CARL" APPROACH
               CALL TROPLV2(PROFLV,GOHGKM,RDATCY,PRFTLV,TKTRP2,TROPRS2)
               TROPRS = TROPRS1
               TKTRP = TKTRP1
               IF (TROPRS.EQ.R4SPV) THEN
                  PRINT *,'NO TROPOPAUSE FOUND: SPOT SKIPPED'
                  DO K = 12, 31
                     PRINT *, (NINT(PROFLV(K))),(NINT(RDATCY(K)))
                  END DO

                  GO TO 180
               END IF
C
C   CALCULATE CLOUD TOP PRESSURE. EQUATORWARD OF +/- 50 DEGREES, USE THE
C   CARL COX METHOD EVERYWHERE. POLEWARD OF +/- 50 DEGREES, IF THE CARL
C   COX METHOD RETURNS A TROPOPAUSE PRESSURE OF LESS THAN 130 MB, USE
C   THE ALTERNATE METHOD OF CALCULATING TROPOPAUSE PRESSURE, PROVIDED THAT
C   THE NEW CALCULATION RESULTS IN A TROPOPAUSE PRESSURE BETWEEN 340 MB
C   AND 140 MB.
C
               PRSTRO2(SPOT) = TROPRS2
               TMPTRO2(SPOT) = TKTRP2
C
C-- PUT THE "CARL" TROPOPAUSE PRESSURE AND TEMPERATURE INTO PRSTRO AND TMPTRO
C   FOR WRITING TO THE R1T

               PRSTRO(SPOT) = TROPRS1
               TMPTRO(SPOT) = TKTRP1
               IF (ABS(LATS(SPOT)).GT.52.0.AND.TROPRS.LT.140.0) THEN
                  IF (TROPRS1.GE.140.0.AND.TROPRS1.LE.340.0) THEN
                     PRSTRO(SPOT) = TROPRS1
                     TMPTRO(SPOT) = TKTRP1
                  ELSE
                     CALL TROPLV3(PROFLV,GOHGKM,RDATCY,PRFTLV,TKTRP1,TROPRS1)
                     PRSTRO(SPOT) = TROPRS1
                     TMPTRO(SPOT) = TKTRP1
                  END IF
                  TROPRS = PRSTRO(SPOT)
                  TKTRP = TMPTRO(SPOT)
               END IF
               PRSTRO1(SPOT) = TROPRS
               TMPTRO1(SPOT) = TKTRP
               CTTEMP = CLTTMP(SPOT)
               SFCPRS = PSFC(SPOT)
               IF (SFCPRS.GT.1000.0) THEN
                  PRSSFC = 1000.0
               ELSE
                  PRSSFC = SFCPRS
               END IF
               TROP1 = .FALSE.
               ISISO = .FALSE.
C-- GET THE CLOUD TOP PRESSURE CORRESPONDING TO HIRS 8 BRIGHTNESS TEMPERATURE.

               MINPRESS = TROPRS
               CALL CLDPRS(CTTEMP,TPROFI,TROPRS,TKTRP,PRSSFC,TROP1,ISISO,
     +                     MINPRESS,CPRESS)
               PCLOUD(SPOT) = CPRESS
C
C-- SIMULATE PREDICTED CLEAR TEMPERATURES FOR HIRS CHANNELS 10 AND 11 FROM
C   PRECIP ADJUSTED PREDICTED CLEAR HIRS CHANNEL 8. SIMULATE PREDICTED CLEAR
C   TEMPERATURE FOR HIRS CHANNEL 12 FROM PRECIP ADJUSTED MHS CHANNEL 3
C
               IF (PRSTRO(SPOT). GT. 188.0) THEN
                  TCLEAR4(SPOT,10) = TCLEAR4(SPOT,8) -7.0
                  TCLEAR4(SPOT,11) = TCLEAR4(SPOT,8) -17.0
                  TCLEAR4(SPOT,12) = TCLEAR4(SPOT,28) - 8.0
               ELSE
                  TCLEAR4(SPOT,10) = TCLEAR4(SPOT,8) - 7.0
                  TCLEAR4(SPOT,11) = TCLEAR4(SPOT,8) - 28.0
                  TCLEAR4(SPOT,12) = TCLEAR4(SPOT,28) - 8.0
               END IF

  150       CONTINUE

C***********************************************************
C   PERFORM ATTENUATION CORRECTION FOR HIRS CH. 7 AND 8
C   AND RECOMPUTE THE CLOUD PRODUCTS ( DO LOOPS 166 & 164)
C***********************************************************
C
            SPOT = 0
            DO 166 K=1,2
               DO 164 L=1,2
                  SPOT = SPOT + 1
                  IF (STECH8(SPOT).LE.0.0) GO TO 164
                  H8TEMP(SPOT) = HIRDAT(BOX(K,L,1),BOX(K,L,2),8)
                  IF(CLTTMP(SPOT).LE. 0.0) GO TO 164
C
C   FIND CLOUD TOP POSITION IN ATMOSPHERE AND COMPUTE TRANSMITTANCE
C
C   TAUHRS IS THE OUTPUTTED TRANSMITTANCE FUNCTIONS.
C
                  TROPRS = PRSTRO(SPOT)
                  CALL TOPINT(CLTTMP(SPOT),TPROFI,TAUIR,TROPRS,
     1                        ILEVEL,TAUHRS)

C
C   CALCULATE AND PERFORM THE ATTENUATION CORRECTION.
C
C   SUBROUTINE SIMCBT CALCULATES THE SIMULATED BRIGHTNESS TEMPERATURE
C   FOR CALCULATION OF THE ATTENUATION ABOVE THE CLOUD TOP. IT USES
C   RADIANCES.
C-- USE THE HIRS 4/8 COMBO INSTEAD OF THE HIRS 7/8 COMBO 11 AUGUST 2010

                  IF(ISR4SPV(XTBX(SPOT,4))) THEN
                    TO8OBS(4) = R4SPV
                  ELSE
                    TO8OBS(4) = XTBX(SPOT,4)
                  END IF
                  IF(ISR4SPV(XTBX(SPOT,6))) THEN
                    TO8OBS(6) = R4SPV
                  ELSE
                    TO8OBS(6) = XTBX(SPOT,6)
                  END IF
                  IF(ISR4SPV(XTBX(SPOT,8))) THEN
                    TO8OBS(8) = R4SPV
                  ELSE
                    TO8OBS(8) = XTBX(SPOT,8)
                  END IF
                  IF (ISR4SPV(XTBX(SPOT,7))) THEN
                     TO8OBS(7) = R4SPV
                  ELSE
                     TO8OBS(7) = XTBX(SPOT,7)
                  END IF
C-- ATTENUATE THE PREDICTED CLEAR HIRS 4 AT THE CLOUD TOP
C
                  CALL SIMCBT(TPROFI,TAUHRS,CLTTMP(SPOT),ILEVEL,
     1                              4,BTSIMX)
                  IF (BTSIMX.EQ.R4SPV) THEN
                     DELTAT(4) = 0.0
                  ELSE
                     DELTAT(4) = CLTTMP(SPOT) - BTSIMX
                     IF (DELTAT(4).LT.0.0) DELTAT(4) = 0.0
                  END IF
C-- GET THE ATTENUATION FOR PREDICTED CLEAR HIRS 5 AT THE CLOUD TOP
C
                  CALL SIMCBT(TPROFI,TAUHRS,CLTTMP(SPOT),ILEVEL,
     1                              5,BTSIMX)
                  IF (BTSIMX.EQ.R4SPV) THEN
                     DELTAT(5) = 0.0
                  ELSE
                     DELTAT(5) = CLTTMP(SPOT) - BTSIMX
                     IF (DELTAT(5).LT.0.0) DELTAT(5) = 0.0
                  END IF
                 
                  TO8CLR(4) = TCLEAR4(SPOT,4)
                  TO8CLR(6) = TCLR84(SPOT,6)
                  TO8CLR(8) = TCLR84(SPOT,8)
                  TO8CLR(7) = TCLEAR4(SPOT,7)
                  CALL SIMCBT(TPROFI,TAUHRS,CLTTMP(SPOT),ILEVEL,
     +                             7,BTSIMX)
                  IF (BTSIMX.EQ.R4SPV) THEN
                     DELTAT(7) = 0.0
                  ELSE
                     DELTAT(7) = CLTTMP(SPOT) - BTSIMX
                     IF (DELTAT(7).LT.0.0) DELTAT(7) = 0.0
                  end if
                  BTT(1) = TO8OBS(7)
                  BTT(2) = TO8OBS(8)
                  BTT(3) = TO8CLR(7) + DELTAT(7)
                  BTT(4) = TO8CLR(8)
                  RADT(1) = PLKRAD(BTT(1),7)
                  RADT(2) = PLKRAD(BTT(2),8)
                  RADT(3) = PLKRAD(BTT(3),7)
                  RADT(4) = PLKRAD(BTT(4),8)

                  CALL CLDTEMP(7,8,RADT,BTT,CTT78)
C
C   CALCULATE THE FIRST ESTIMATE OF CLOUD TOP TEMPERATURE USING the HIRS 4/6 CHANNEL
C   COMBINATION.
C
                  BTT(1) = TO8OBS(4)
                  BTT(2) = TO8OBS(6)
                  IF(ISR4SPV(BTT(1)).OR.ISR4SPV(BTT(2))) GO TO 164
                  BTT(3) = TO8CLR(4) + DELTAT(4)
                  BTT(4) = TCLR84(SPOT,6)
                  IF(ISR4SPV(BTT(3)).OR.ISR4SPV(BTT(4))) GO TO 164
                  RADT(1) = PLKRAD(BTT(1),4)
                  RADT(2) = PLKRAD(BTT(2),6)
                  RADT(3) = PLKRAD(BTT(3),4)
                  RADT(4) = PLKRAD(BTT(4),6)
                  CALL CLDTEMP(4,6,RADT,BTT,CTT46)

C
C   CALCULATE THE FIRST ESTIMATE OF CLOUD TOP TEMPERATURE USING the HIRS 4/8 CHANNEL
C   COMBINATION.
C
                  BTT(1) = TO8OBS(4)
                  BTT(2) = TO8OBS(8)
                  IF(ISR4SPV(BTT(1)).OR.ISR4SPV(BTT(2))) GO TO 164
                  BTT(3) = TO8CLR(4) + DELTAT(4)
                  BTT(4) = TO8CLR(8)
                  IF(ISR4SPV(BTT(3)).OR.ISR4SPV(BTT(4))) GO TO 164
                  RADT(1) = PLKRAD(BTT(1),4)
                  RADT(2) = PLKRAD(BTT(2),8)
                  RADT(3) = PLKRAD(BTT(3),4)
                  RADT(4) = PLKRAD(BTT(4),8)
                  CALL CLDTEMP(4,8,RADT,BTT,CTT48)
C
C   CALCULATE THE FIRST ESTIMATE OF CLOUD TOP TEMPERATURE USING the HIRS 5/7 CHANNEL
C   COMBINATION.
C
                  BTT(1) = TO8OBS(5)
                  BTT(2) = TO8OBS(7)
                  IF(ISR4SPV(BTT(1)).OR.ISR4SPV(BTT(2))) GO TO 164
                  BTT(3) = TO8CLR(5) + DELTAT(5)
                  BTT(4) = TO8CLR(7)
                  IF(ISR4SPV(BTT(3)).OR.ISR4SPV(BTT(4))) GO TO 164
                  RADT(1) = PLKRAD(BTT(1),5)
                  RADT(2) = PLKRAD(BTT(2),7)
                  RADT(3) = PLKRAD(BTT(3),5)
                  RADT(4) = PLKRAD(BTT(4),7)
                  CALL CLDTEMP(5,7,RADT,BTT,CTT57)
C
C   CALCULATE THE FIRST ESTIMATE OF CLOUD TOP TEMPERATURE USING the HIRS 5/8 CHANNEL
C   COMBINATION.
C
                  BTT(1) = TO8OBS(5)
                  BTT(2) = TO8OBS(8)
                  IF(ISR4SPV(BTT(1)).OR.ISR4SPV(BTT(2))) GO TO 164
                  BTT(3) = TO8CLR(5) + DELTAT(5)
                  BTT(4) = TO8CLR(8)
                  IF(ISR4SPV(BTT(3)).OR.ISR4SPV(BTT(4))) GO TO 164
                  RADT(1) = PLKRAD(BTT(1),5)
                  RADT(2) = PLKRAD(BTT(2),8)
                  RADT(3) = PLKRAD(BTT(3),5)
                  RADT(4) = PLKRAD(BTT(4),8)
                  CALL CLDTEMP(5,8,RADT,BTT,CTT58)
C
C
C =============================================================
C LOOP TWO
C =============================================================

C   IF A CLOUD TOP TEMPERATURE IS CALCULATED FOR THE 5/8 OR 7/8
C   CHANNEL PAIR, USE THE COLDER OF THE TWO CALCULATED TEMPERATURES.
C   IF CLDTEMP CANNOT CALLCULATE A CLOUD TOP TEMPERATURE, IT
C   RETURNS (IN THIS CASE) THE OBSERVED HIRS CHANNEL 8 TEMPERATURE FOR
C   THE 5/8 CHANNEL PAIR AND THE 7/8 CHANNEL PAIR.
C   WHEN THIS HAPPENS FOR BOTH THE 5/8 AND 7/8 CHANNEL PAIRS, SET
C   THE NCH1GUESS FLAG TO 1 TO REFLECT THE USE OF HIRS CHANNEL 8 AS 
C   THE FIRST GUESS CTT.
C
                  NCH1GUESS(SPOT) = I2SPV
                  IF (CTT78 .EQ. TO8OBS(8) .AND. CTT58 .EQ. TO8OBS(8)) THEN
                     CLTTMP(SPOT) = TO8OBS(8)
                     NCH1GUESS(SPOT) = 1
                  END IF
                  IF (CTT58 .NE. TO8OBS(8)) THEN
                     IF (CTT78 .NE. TO8OBS(8)) THEN
                        IF (CTT78.LT.CTT58) THEN
                           CLTTMP(SPOT) = CTT78
                           NCH1GUESS(SPOT) = 7
                        ELSE
                           CLTTMP(SPOT) = CTT58
                           NCH1GUESS(SPOT) = 5
                        END IF
                     ELSE
                        CLTTMP(SPOT) = CTT58
                        NCH1GUESS(SPOT) = 5
                     END IF
                  END IF
                  IF (CTT78 .NE. TO8OBS(8)) THEN
                     IF (CTT58 .EQ. TO8OBS(8)) THEN
                        CLTTMP(SPOT) = CTT78
                        NCH1GUESS(SPOT) = 7
                     ELSE
                        IF (CTT58 .LE. CTT78) THEN
                           CLTTMP(SPOT) = CTT58
                           NCH1GUESS(SPOT) = 5
                        ELSE
                           CLTTMP(SPOT) = CTT78
                           NCH1GUESS(SPOT) = 7
                        END IF
                    END IF
                  END IF
                        

                  IF(CLTTMP(SPOT).LE.0.0) GO TO 164

                  RADCTT(SPOT) = PLKRAD(CLTTMP(SPOT),8)
C
                  SFCPRS = PSFC(SPOT)
                  IF (SFCPRS.GT.1000.0) THEN
                     PRSSFC = 1000.0
                  ELSE
                     PRSSFC = SFCPRS
                  END IF
                  TROP1 = .FALSE.
                  ISISO = .FALSE.
                  MINPRESS = TROPRS
                  CALL CLDPRS(CLTTMP(SPOT),TPROFI,TROPRS,TKTRP,PRSSFC,TROP1,ISISO,
     +                        MINPRESS,CPRESS)

                  PCLOUD(SPOT) = CPRESS
C
  164          CONTINUE
  166       CONTINUE
C
C-- CHECK THE TEMPERATURE PROFILE FOR NEARLY ISOTHERMAL OR DECREASING
C   TEMPERATURES ABOVE THE TROPOPAUSE AND SET THE ISISO FLAG ACCORDINGLY.
C
            TROLEV = 0
            DO LV = 31, 15, -1
               IF (TROLEV.EQ.0) THEN
                  IF (TROPRS.GE.PROFLV(LV)) THEN
                     TROLEV = LV
                  END IF
               END IF
            END DO
            IF (TROLEV.EQ.0) THEN
               PRINT *,'TROLEV IS 0 WITH PRESSURE OF ',TROPRS
            END IF

            IF (TROLEV.NE.0) THEN
               IF ((TPROFI(TROLEV-1)- TKTRP).GE.1.0) THEN
                  ISISO = .FALSE.
               ELSE
                  ISISO = .TRUE.
               END IF
            END IF

C
C*******************************************************************
C   NOW RECALCULATE THE CLOUD PARAMETERS WITH A SELECTED PAIR OF
C   CHANNELS.  FIRST SELECT THE PROPER PAIR WITH WHICH TO REFINE
C   CLOUD TOP TEMPERATURE.  THE CHANNEL SELECTION AND RECALCULATION
C   IS DONE TWICE. (THE DO 171 & 170 LOOPS)
C*******************************************************************
C
C  PRESSX = CLOUD TOP PRESSURE
C
C           DO 171 ITER=1,2
               ITER = 2
               DO 170 SPOT=1,4
                  SFCTYP = TERTYP(SPOT)
                  PRESSX = PCLOUD(SPOT)
                  IF (TRANS1ST) THEN
                     IF (SPOT.EQ.4) TRANS1ST = .FALSE.
                  END IF
                  TROP1 = .FALSE.
                  FOVLAT = LATS(SPOT)
                  FOVLON = LONS(SPOT)

C                 IF(ITER .EQ. 1) THEN
                     ATNPCLR8(SPOT) = R4SPV
                     PREDTEM8COR(SPOT) = R4SPV
                     BCATPCLR8(SPOT) = R4SPV
                     RADBCATPC8(SPOT) = R4SPV
                     DELPRESX(SPOT) = 0.0
                     RADT8C8(SPOT) = R4SPV
                     A10U8(SPOT) = R4SPV
                     BCORSTE(SPOT) = R4SPV
                     BIASPCLR(SPOT) = R4SPV
                     BIASPCL8(SPOT) = R4SPV
                     PREDTEMP(SPOT) = R4SPV
                     PREDTEM8(SPOT) = R4SPV
                     PCLRBIAS(SPOT) = R4SPV
                     RDTC8A(SPOT) = R4SPV
                     RDT8CA(SPOT) = R4SPV
                     RDT8MA(SPOT) = R4SPV
                     RCLODA(SPOT) = R4SPV
                     NCH1 = 0
                     NCH2 = 0
                     CTPITER(SPOT) = -1
                     RCBIAS(SPOT) = 0
                     DELPRS1(SPOT) = R4SPV
                     CPERROR(SPOT) = R4SPV
                     LPINDX(SPOT) = R4SPV
                     DO CHAN = 1, 19
                        DELPRS8(SPOT,CHAN) = R4SPV
                     END DO
                     DO CHAN = 1, 19
                        DELPRS2(SPOT,CHAN) = R4SPV
                        DELPRS3(SPOT,CHAN) = R4SPV
                        DELPRS4(SPOT,CHAN) = R4SPV
                     END DO
                     PASSFAIL(SPOT) = 0
                     QC0FAIL(SPOT) = .FALSE.
                     OPDEPTH(SPOT) = R4SPV
                     T_VALUE(SPOT) = R4SPV
C                 ENDIF
C
C  SKIP THIS SPOT IF THERE IS NO VALID HIRS 8 SURFACE TEMPERATURE
C  ESTIMATE AVAILABLE.
C
                  IF (STECH8(SPOT).LE.0.0) THEN
                     GO TO 170
                  END IF
C
C  SKIP THIS SPOT IF THE FORECAST SURFACE PRESSURE IS MISSING.
C
                  SFCPRS = PSFC(SPOT)
                  IF (SFCPRS.EQ.R4SPV) THEN
                     GO TO 170
                  END IF

C
C  IF THE SPOT HAS ALREADY BEEN MARKED AS CLEAR, SKIP IT.
C
                  IF (PRESSX.EQ.0.0) THEN
                      GO TO 170
                  END IF
C
C  CHECK THE CLOUD TOP PRESSURE FROM THE PREVIOUS ITERATION (DO LOOPS 164
C  AND 166) AND IF IS OUT OF BOUNDS SET THE PRODUCTS TO MISSING.
C  THIS WILL ALSO PICK UP SPOTS WHERE NO PREDICTED CLEAR TEMPERATURE
C  WAS CALCULATED (NO CLOUD TOP TEMPERATURE).
C
                  IF(PRESSX.GT.0.0) THEN
                     IF(CLTTMP(SPOT).GT.CCMNTP(7).AND.CLTTMP(SPOT)
     1                  .LT.CCMXTP(7)) THEN
C
C  CALCULATE AND SAVE THE LAND PRECIPITATION INDEX WHERE IT COULD HAVE BEEN USED
C  TO ADJUST AMSU-A CHANNELS 4-8.
C
                        IF ((AMSU1(SPOT) - AMSU15(SPOT) .GT. 0.0) .AND.
     +                       MHSCH5(SPOT) .LT. 269.0) THEN
                           IF (LATS(SPOT).GE.(-60.0) .AND. LATS(SPOT) .LE. 76.0) THEN
                              IF (TERTYP(SPOT) .EQ. 1 .OR. TERTYP(SPOT) .EQ. 2) THEN
                                 IF (HEIGHT(SPOT) .LE. 1200) THEN
                                    LPINDX(SPOT) = AMSU1(SPOT) - AMSU15(SPOT)
                                 END IF
                              END IF
                           END IF
                        END IF

                        HIR8OBS = XTBX(SPOT,8)
                        TROPRS = PRSTRO(SPOT)
                        CALL TOPINT(CLTTMP(SPOT),TPROFI,TAUIR,TROPRS,ILEVEL,TAUHRS)
C
C   CALCULATE CLOUD TOP TEMPERATURE ATTENUATION CORRECTION FOR CHANNELS
C   3 THROUGH 6
C
                        DELTAT(2) = 0.0

                        DO CHAN = 3, 6
                           CALL SIMCBT(TPROFI,TAUHRS,CLTTMP(SPOT),ILEVEL,
     1                                 CHAN,BTSIMX)
                           IF (BTSIMX.EQ.R4SPV) THEN
                              DELTAT(CHAN) = 0.0
                           ELSE
                              DELTAT(CHAN) = CLTTMP(SPOT) - BTSIMX
                              IF (DELTAT(CHAN).LT.0.0) DELTAT(CHAN) = 0.0
                           END IF
                        END DO
C
C
C GROUP1
C
                        MINPRESS = TROPRS
                        IF (AMSU6(SPOT).GT.230.0.AND.XTBX(SPOT,8).LT.223.0.AND.TROPRS
     +                      .LT.115.0) THEN
                           IF ((TCLEAR4(SPOT,2) - XTBX(SPOT,2)) .GT. 0.0) THEN
                              NCH1 = 2
                              NCH2 = 3
                              TROP1 = .TRUE.
                              DELTAPRS=((TCLR84(SPOT,2) - XTBX(SPOT,2))) * 3.2
                              MINPRESS = TROPRS-DELTAPRS
                              GO TO 161
                           END IF
                        END IF

C
C GROUP2
C
                        IF (AMSU6(SPOT).GT.236.0.AND.TCLEAR4(SPOT,3).GE.XTBX(SPOT,3))
     +                      THEN
                           IF (XTBX(SPOT,8).LT.241.0) THEN
                              NCH1 = 3
                              NCH2 = 4
                              GO TO 161
                           END IF
                        END IF
C
C GROUP3
C
                        IF (PEAKPR(7).EQ.1000.0) THEN
                           XLAT = (ABS(LATS(SPOT)) / 70.0)
                           IF (XLAT.GT.1.0) XLAT = 1.0
                           FACTOR = 1.727 + XLAT
                           PEAKPR(7) = PEAKPR(8) - ((PEAKPR(8) - PEAKPR(6)) / FACTOR)
                           DO LV = 31, 40
                              IF (PEAKPR(7).GT.PROFLV(LV-1).AND.PEAKPR(7).LT.PROFLV(LV)) THEN
                                 IF ((PEAKPR(7) - PROFLV(LV-1)).LT.(PROFLV(LV) - PEAKPR(7))) THEN
                                    PEAKPR(7) = PROFLV(LV-1)
                                 ELSE
                                    PEAKPR(7) = PROFLV(LV)
                                 END IF
                              END IF
                           END DO
                        END IF

                        IF (PEAKPR(3).LE.PRESSX.AND.PEAKPR(4).GT.PRESSX) THEN
                           NCH1 = 3
                           NCH2 = 4
                        ELSE IF (PEAKPR(4).LE.PRESSX.AND.PEAKPR(5).GT.PRESSX) THEN
                           NCH1 = 4
                           NCH2 = 5
                        ELSE IF (PEAKPR(5).LE.PRESSX.AND.PEAKPR(6).GT.PRESSX) THEN
                           IF (HEIGHT(SPOT).GT.2280) THEN
                              NCH1 = 4
                              NCH2 = 5
                           ELSE
                              NCH1 = 5
                              NCH2 = 6
                           END IF
                        ELSE IF (PEAKPR(6).LE.PRESSX.AND.PEAKPR(7).GT.PRESSX) THEN
                           IF (HEIGHT(SPOT).GT.1600) THEN
                              NCH1 = 5
                              NCH2 = 6
                           ELSE
                              NCH1 = 6
                              NCH2 = 7
                           END IF
                        ELSE
                           NCH1 = 6
                           NCH2 = 8
                        END IF
C
 161                    CONTINUE
C
                        A10U8(SPOT) = DELTAT(NCH1)
                        TO8OBS(NCH1) = XTBX(SPOT,NCH1)
                        TO8OBS(NCH2) = XTBX(SPOT,NCH2)
                        IF(TO8OBS(NCH1).LT.0.)
     +                     TO8OBS(NCH1)=XTBX(SPOT,NCH1)
                        IF(TO8OBS(NCH2).LT.0.)
     +                     TO8OBS(NCH2)=XTBX(SPOT,NCH2)
C
C  APPLY BIAS CORRECTION TO THE CLW ADJUSTED PREDICTED CLEAR FOR NCH1
C  FOR CALCULATING CTT.
C
                        TO8CLR(NCH1) = TCLEAR4(SPOT,NCH1) + DELTAT(NCH1)
                        TO8CLR(NCH2) = TCLR84(SPOT,NCH2)

C
C-- SAVE THE TROPOPAUSE TEMPERATURE AND PRESSURE
C
                        TKTRP = TMPTRO(SPOT)
                        TROPRS = PRSTRO(SPOT)
C
C IF THE 3-4 CHANNEL COMBINATION IS USED, CALCULATE A PREDICTED
C CLEAR TEMPERATURE FOR THE TROPOPAUSE AND USE THE TROPOPAUSE TEMPERATURE
C AND TROPOPAUSE PREDICTED CLEAR TEMPERATURE IN THE CALCULATION OF
C CLOUD TOP TEMPERATURE.

                        IF (NCH2.NE.4) GO TO 110
C
C CALCULATE THE PREDICTED CLEAR TROPOPAUSE TEMPERATURE FOR THE 2-3 OR
C 3-4 CHANNEL COMBINATION.
C

                        IF (ALOG(TROPRS).EQ.ALOG(PEAKPR(3))) THEN
                           PCTROP = TCLEAR4(SPOT,3)
                           GO TO 100
                        END IF
                        IF (NCH1.EQ.2) THEN
                           IF (TROPRS.GT.PEAKPR(3)) THEN
                              GO TO 110
                           ELSE
                              PCTROP = (TCLEAR4(SPOT,3) + ((TCLEAR4(SPOT,2) -
     +                                  TCLEAR4(SPOT,3))) * ((ALOG(PEAKPR(3))-
     +                                  ALOG(TROPRS)) / (ALOG(PEAKPR(3)) -
     +                                  ALOG(PEAKPR(2)))))
                           END IF
                        END IF
                        IF (NCH1.EQ.3) THEN
                           IF (TROPRS.LT.PEAKPR(3).OR.TROPRS.GT.PEAKPR(4)) THEN
                              GO TO 110
                           ELSE
                              PCTROP = (TCLEAR4(SPOT,3) + ((TCLEAR4(SPOT,4) -
     +                                  TCLEAR4(SPOT,3))) * ((ALOG(TROPRS) -
     +                                  ALOG(PEAKPR(3)))))
                           END IF
                        END IF
C
  100 CONTINUE
C
C NOW SET UP THE PARAMETERS USED IN CLOUD TOP TEMPERATURE CALCULATION USING
C THE PREVIOUSLY SELECTED CHANNEL PAIRS AND THE TROPOPAUSE LEVEL DATA IF
C THE TROPOPAUSE FALLS BETWEEN THE PEAK PRESSURES FOR THE SELECTED CHANNELS.
C
                        IF (NCH2.EQ.3) THEN

C
C ASSUME THAT THE SELECTED CHANNELS STRADDLE THE CLOUD TOP PRESSURE.
C
C                          IF THE TROPOPAUSE PRESSURE IS LOWER IN THE
C                          ATMOSPHERE THAN THE PEAK PRESSURE FOR CHANNEL 3,
C                          THERE IS NO NEED TO INCLUDE TROPOPAUSE DATA IN
C                          THE CLOUD TOP TEMPERATURE CALCULATION.
C
                           IF (ALOG(TROPRS).GT.ALOG(PEAKPR(3))) THEN
                              GO TO 110
                           END IF
C
C                          USE CHANNEL 2 AND THE TROPOPAUSE DATA IF THE
C                          CLOUD TOP PRESSURE IS ABOVE THE TROPOPAUSE.
C
                           IF (ALOG(PRESSX).LE.ALOG(TROPRS)) THEN
                              BTT(1) = TO8OBS(NCH1)
                              BTT(2) = TKTRP
                              BTT(3) = TO8CLR(NCH1)
                              BTT(4) = PCTROP
                              RADT(1) = PLKRAD(BTT(1),NCH1)
                              RADT(2) = PLKRAD(BTT(2),NCH2)
                              RADT(3) = PLKRAD(BTT(3),NCH1)
                              RADT(4) = PLKRAD(BTT(4),NCH2)
                              GO TO 120
                           ELSE
C
C                             USE THE TROPOPAUSE AND CHANNEL 3 IF THE
C                             CLOUD TOP PRESSURE IS BELOW THE TROPOPAUSE.
C
                              BTT(1) = TKTRP
                              BTT(2) = TO8OBS(NCH2)
                              BTT(3) = PCTROP
                              BTT(4) = TO8CLR(NCH2)
                              RADT(1) = PLKRAD(BTT(1),NCH2)
                              RADT(2) = PLKRAD(BTT(2),NCH2)
                              RADT(3) = PLKRAD(BTT(3),NCH2)
                              RADT(4) = PLKRAD(BTT(4),NCH2)
                              GO TO 120
                           END IF
                        END IF
C
                        IF (NCH2.EQ.4) THEN
C
C                          IF THE TROPOPAUSE PRESSURE IS HIGHER IN THE
C                          ATMOSPHERE THAN THE PEAK PRESSURE FOR CHANNEL 3,
C                          THERE IS NO NEED TO INCLUDE TROPOPAUSE DATA IN
C                          THE CLOUD TOP TEMPERATURE CALCULATION.
C
                           IF (ALOG(TROPRS).LT.ALOG(PEAKPR(3))) THEN
                              GO TO 110
                           END IF
C
C                          IF THE CLOUD TOP PRESSURE IS ABOVE THE TROPOPAUSE
C                          USE CHANNEL 3 AND THE TROPOPAUSE.
C
                           IF (ALOG(PRESSX).LE.ALOG(TROPRS)) THEN
                              BTT(1) = TO8OBS(NCH1)
                              BTT(2) = TKTRP
                              BTT(3) = TO8CLR(NCH1)
                              BTT(4) = PCTROP
                              RADT(1) = PLKRAD(BTT(1),NCH1)
                              RADT(2) = PLKRAD(BTT(2),NCH1)
                              RADT(3) = PLKRAD(BTT(3),NCH1)
                              RADT(4) = PLKRAD(BTT(4),NCH1)
                              GO TO 120
                           ELSE
C
C                          IF THE CLOUD TOP PRESSURE IS BELOW THE TROPOPAUSE
C                          USE THE TROPOPAUSE AND CHANNEL 4.
C
                              BTT(1) = TKTRP
                              BTT(2) = TO8OBS(NCH2)
                              BTT(3) = PCTROP
                              BTT(4) = TO8CLR(NCH2)
                              RADT(1) = PLKRAD(BTT(1),NCH1)
                              RADT(2) = PLKRAD(BTT(2),NCH2)
                              RADT(3) = PLKRAD(BTT(3),NCH1)
                              RADT(4) = PLKRAD(BTT(4),NCH2)
                              GO TO 120
                           END IF
                        END IF

C
  110 CONTINUE
C
C                       ASSIGN VALUES USED IN CLOUD TOP TEMPERATURE CALCULATION
C                       WHERE THE TROPOPAUSE PRESSURE IS NOT A FACTOR.
C
                        BTT(1) = TO8OBS(NCH1)
                        BTT(2) = TO8OBS(NCH2)
                        BTT(3) = TO8CLR(NCH1)
                        BTT(4) = TO8CLR(NCH2)
C
C                       IF THE OBSERVED TEMPERATURE FOR NCH1 AND THE PREDICTED CLEAR
C                       TEMPERATURE FOR NCH1 ARE IDENTICAL, MOVE NCH1 AND NCH2 DOWN
C                       ONE CHANNEL IN THE ATMOSPHERE. IF NCH2 IS 8, ONLY MOVE NCH1
C                       DOWN ONE CHANNEL IF NCH1 IS 6. DO NOT INCREMENT NCH2 IF IT
C                       IS ALREADY SET TO 8.
C
                        IF (BTT(1).EQ.BTT(3)) THEN
                           if (NCH2.EQ.8) THEN
                              IF (NCH1.EQ.6) THEN
                                 NCH1 = NCH1 + 1
                              END IF
                           ELSE
                              NCH1 = NCH1 + 1
                              NCH2 = NCH2 + 1
                              BTT(1) = TO8OBS(NCH1)
                              BTT(2) = TO8OBS(NCH2)
                              BTT(3) = TO8CLR(NCH1)
                              BTT(4) = TO8CLR(NCH2)
                           END IF
                        END IF


                        RADT(1) = PLKRAD(BTT(1),NCH1)
                        RADT(2) = PLKRAD(BTT(2),NCH2)
                        RADT(3) = PLKRAD(BTT(3),NCH1)
                        RADT(4) = PLKRAD(BTT(4),NCH2)

  120 CONTINUE

                        IF (NCH1.EQ.8) THEN
                           print *,'NCH1 ',NCH1
                           print *,'BTT(1) ',BTT(1)
                           print *,'BTT(2) ',BTT(2)
                           print *,'BTT(3) ',BTT(3)
                           print *,'BTT(4) ',BTT(4)
                        END IF
C
                        SFCPRS = PSFC(SPOT)
C
C
C ======================================================================
C
C   DON'T TEST IN THE FIRST ITERATION, SAVE ALL TESTING FOR THE SECOND.
C
C                          IF(ITER .EQ. 1) GO TO 170
C
C =---------------------------------------------------------------------------------
C
                        CALL LEVTOV(SFCPRS,SFCLVL)

                        IF (SFCLVL.GT.40) SFCLVL = 40
                        IF (SFCLVL.EQ.0) GO TO 170
                        SFCLEV(SPOT) = SFCLVL
C
C   USE THE FORECAST SURFACE TEMPERATURE TO GET A TOVS LEVEL, THEN
C   SIMULATE A HIRS 8 BRIGHTNESS TEMPERATURE AT THAT LEVEL FROM THE
C   HIRS 8 SURFACE TEMPERATURE ESTIMATE, IN ORDER TO GENERATE THE BIAS
C   CORRECTION FOR THE HIRS 8 SURFACE TEMPERATURE ESTIMATE.
C
                        CALL SIMCBT(TPROFI,TAUIR,STECH8(SPOT),SFCLVL,8,BTSIMX)
                        IF (BTSIMX.EQ.R4SPV) THEN
                           DELSFCT = 0.0
                        ELSE
                           DELSFCT = STECH8(SPOT) - BTSIMX
                           IF (DELSFCT.GT.25.0) DELSFCT = 25.0
                           IF (DELSFCT.LT.-60.0) DELSFCT = -60.0
                        END IF
C
C  SAVE THE BIAS CORRECTION FOR THE HIRS 8 SURFACE TEMPERATURE ESTIMATE
C  TO BE WRITTEN TO THE R1T.
C
                        BCORSTE(SPOT) = DELSFCT
                        TSFCOB(SPOT) = STECH8(SPOT) + DELSFCT
C
C   IF THE BIAS CORRECTED HIRS 8 SURFACE TEMPERATURE ESTIMATE IS ZERO OR
C   LESS, USE THE ORIGINAL HIRS 8 SURFACE TEMPERATURE ESTIMATE
C
                        IF (TSFCOB(SPOT).LE.0.0) THEN
                           TSFCOB(SPOT)=STECH8(SPOT)
                        END IF
C
C   GET THE RADIANCE OF THE BIAS CORRECTED H8STE AT THE HIRS 8 FREQUENCY
C
                        RSFCOB = PLKRAD(TSFCOB(SPOT),8)
                        RADT8M = RSFCOB

C  NOW BIAS CORRECT THE ATTENUATION CORRECTED CLW CORRECTED PREDICTED CLEAR

                        PREDTEMP(SPOT) = TCLEAR4(SPOT,8)
                        PREDTEM8(SPOT) = TCLR84(SPOT,8)


C  CALCULATE THE ATTENUATION CORRECTION FOR PREDICTED CLEAR HIRS 8 AT THE CLOUD TOP
C  * ATTENUATION CORRECTION REMOVED 6 SEPTEMBER 2013).

                        CALL SIMCBT(TPROFI,TAUHRS,TCLEAR4(SPOT,8),ILEVEL,
     1                                 8,BTSIMX)
                        IF (BTSIMX.EQ.R4SPV) THEN
                           DELTAT(8) = 0.0
                        ELSE
C                          DELTAT(8) = TCLEAR4(SPOT,8) - BTSIMX
                           DELTAT(8) = 0.0
                        END IF
                        ATNPCLR8(SPOT) = TCLEAR4(SPOT,8) + DELTAT(8)
C
C  MIKE WANTS A COMMENT ON THIS ONE (SO WE KNOW IT IS BIAS ADJUSTMENT)

                        CALL SIMCBT(TPROFI,TAUIR,ATNPCLR8(SPOT),SFCLVL,
     1                              8,BTSIMX)
                        IF (BTSIMX.EQ.R4SPV) THEN
                           DELTAT(8) = 0.0
                        ELSE
                           DELTAT(8) = PREDTEMP(SPOT) - BTSIMX
                           IF (DELTAT(8).GT.22.0) DELTAT(8) = 22.0
                           IF (DELTAT(8).LT.0.0) DELTAT(8) = 0.0
                        END IF
                        BCATPCLR8(SPOT) = ATNPCLR8(SPOT) + DELTAT(8)
                        RADBCATPC8(SPOT) = PLKRAD(BCATPCLR8(SPOT),8)

C
C-- SIMULATE A BRIGHTNESS TEMPERATURE FOR CHANNEL 8 FOR THE CLW ADJUSTED
C   PREDICTED CLEAR TEMPERATURE AND APPLY A BIAS CORRECTION.
C
                        CALL SIMCBT(TPROFI,TAUIR,PREDTEMP(SPOT),SFCLVL,
     1                              8,BTSIMX)
                        IF (BTSIMX.EQ.R4SPV) THEN
                           DELTAT(8) = 0.0
                        ELSE
                           DELTAT(8) = PREDTEMP(SPOT) - BTSIMX
                           IF (DELTAT(8).GT.22.0) DELTAT(8) = 22.0
                           IF (DELTAT(8).LT.0.0) DELTAT(8) = 0.0
                        END IF
                        BIASPCLR(SPOT) = DELTAT(8)
C
C-- APPLY A BIAS CORRECTION TO THE CLW ADJUSTED PREDICTED CLEAR USED IN THE
C   NUMERATOR OF THE ECA CALCULATION.
C
                        PCLRBIAS(SPOT) = PREDTEMP(SPOT) + DELTAT(8)
                        RADT8C = PLKRAD(PCLRBIAS(SPOT),8)
C
                        RADTC8 = PLKRAD(PREDTEM8(SPOT),8)
                        RADT8C8(SPOT) = RADTC8

                        IF ((RADT8C - RADTC8).GT.70.0) THEN
                           RADT8C = RADTC8 + 70.0
                        END IF

C
C-- CALCULATE THE RADIANCE FOR THE CLW ADJUSTED, BUT NOT BIAS CORRECTED,
C   PREDICTED CLEAR FOR USE IN GETERR.
C
                        RADT8CU(SPOT) = PLKRAD(PREDTEMP(SPOT),8)
C
C-- CALCULATE A BIAS CORRECTION FOR THE PREDICTED CLEAR WITHOUT CLW ADJUSTMENT.

                        CALL SIMCBT(TPROFI,TAUIR,PREDTEM8(SPOT),SFCLVL,
     +                              8,BTSIMX)
                        IF (BTSIMX.EQ.R4SPV) THEN
                           RADCLR(SPOT) = PLKRAD(PREDTEM8(SPOT),8)
                           PREDTEM8COR(SPOT) = PREDTEM8(SPOT)
                        ELSE
                           BIASCOR = PREDTEM8(SPOT) - BTSIMX
                           RADCLR(SPOT) = PLKRAD((PREDTEM8(SPOT)+BIASCOR),8)
                           PREDTEM8COR(SPOT) = PREDTEM8(SPOT) + BIASCOR
                        END IF

C
C OVER LAND OR COAST WHERE THE 6/8 CHANNEL COMBINATION WAS SPECIFIED AND
C THE RADIANCE FOR THE HIRS 8 SURFACE TEMPERATURE ESTIMATE EXCEEDS THE
C RADIANCE FOR THE CLW ADJUSTED PREDICTED CLEAR TEMPERATURE, SET THE
C CLOUD TOP TEMPERATURE TO THE OBSERVED PREDICTED CLEAR THAT WAS NOT
C ADJUSTED FOR CLW. FOR ALL OTHER CONDITIONS, CALL A SUBROUTINE TO CALCULATE
C THE CLOUD TOP TEMPERATURE.
C

                        IF ((SFCTYP.EQ.1.OR.SFCTYP.EQ.2).AND.NCH1.EQ.6.AND.
     +                      PRESSX.GT.860.0.AND.(RADT8M.GT.RADT8C)) NCH2=8
                        CALL CLDTEMP(NCH1,NCH2,RADT,BTT,CLTTMP(SPOT))
      

                        IF(CLTTMP(SPOT).LE.0.0) THEN
                          PRINT *,'CTT ',CLTTMP(SPOT),' LAT ',FOVLAT,' LON ',FOVLON
                          GO TO 330
                        END IF
C
C  USE CTT TO MAKE RCLOUD
C
                        RCLOUD = PLKRAD(CLTTMP(SPOT),8)

                        RCBIAS(SPOT) = RCLOUD
C
C  MAKE SURE THE RADIANCE FROM THE CTT IS POSITIVE BEFORE USING IT.
C
                        IF (RCLOUD.LE.0.0) THEN
                           PRINT *,'RCLOUD ',RCLOUD,' LAT ',FOVLAT,' LON ',FOVLON
                           GO TO 330
                        END IF

C
C  NOW CALCULATE THE CLOUD TOP PRESSURE.
C
                        TROPO = TROP1
                        TKTRP = TMPTRO(SPOT)
                        IF (SFCPRS.GT.1000.0) THEN
                           PRSSFC = 1000.0
                        ELSE
                           PRSSFC = SFCPRS
                        END IF
                        CALL CLDPRS(CLTTMP(SPOT),TPROFI,TROPRS,TKTRP,PRSSFC,
     +                       TROPO,ISISO,MINPRESS,CPRESS)

                        PCLOUD(SPOT) = CPRESS
C
C
C-- SAVE THE ADJUSTED CHANNEL PAIR
C
                        UPCHAN(SPOT) = NCH1
                        DNCHAN(SPOT) = NCH2

C
C-- SAVE THE BIAS CORRECTION FOR THE CLW ADJUSTED PREDICTED CLEAR
C   TEMPERATURE TO BE WRITTEN TO THE R1T.
C
C
                        BIASPCL8(SPOT) = DELTAT8
C
C SAVE RADIANCE VALUES FOR EACH SPOT FOR STORING ON THE R1T
C (WR #210)
C
C      PREDICTED CLEAR HIRS 8 RADIANCE
C
                        RDT8CA(SPOT) = RADT8C
C
C -- SAVE THE NON CLW-ADJUSTED, NON BIAS-CORRECTED PREDICTED CLEAR
C    HIRS CHANNEL 8 RADIANCE.
C
                        RDTC8A(SPOT) = RADTC8
C
C      HIRS 8 SURFACE TEMPERATURE ESTIMATE RADIANCE.
C
                        RDT8MA(SPOT) = RADT8M
C
C      CLOUD TOP TEMPERATURE RADIANCE
C
                        RCLODA(SPOT) = RCLOUD


C  SFC. HIRS.8 CALC OF CLOUD AMOUNT
C
                        IF (RADT8C.EQ.R4SPV.OR.RADT8M.EQ.R4SPV) THEN
                           PRINT *,'RADT8C ',RADT8C,' RADT8M ',RADT8M,
     +                             ' LAT ',FOVLAT,' LON ',FOVLON
                           GO TO 330
                        END IF

                        IF (RADTC8.EQ.R4SPV.OR.RCLOUD.EQ.R4SPV) THEN
                           PRINT *,' RADTC8 ',RADTC8,' RCLOUD ',RCLOUD,
     +                             ' LAT ',FOVLAT,' LON ',FOVLON
                           GO TO 330
                        END IF
C
C IF OVER SEA AND THE CLOUD TOP PRESSURE IS GREATER THAN THE PREDICTED
C SURFACE PRESSURE OR GREATER THAN 1000 MB, MARK THE SPOT AS CLEAR.
C
                        IF (SFCPRS.GE.1000.0) THEN
                           MAXPRS = 1000.0
                        ELSE
                           MAXPRS = SFCPRS
                        END IF
C
C   DETERMINE IF THE CLOUD IN THE SPOT IS AN AEROSOL
C
                        AEROSOL(SPOT) = .FALSE.
                        H20ADJ(SPOT) = .FALSE.
                        IF (ISAEROSOL
     +                       (MHSCH3(SPOT),MHSCH5(SPOT),AMSU6(SPOT),
     +                       PCLOUD(SPOT),HEIGHT(SPOT),HIR8OBS,SFCTYP,
     +                       CLTTMP(SPOT),NIGHT(SPOT)) )
     +                  THEN
                           AEROSOL(SPOT) = .TRUE.
                           IF (DAY(SPOT).AND.(SFCTYP.EQ.1.OR.SFCTYP.EQ.2.OR.SFCTYP.EQ.11) .AND.
     +                         AMSU6(SPOT) .GT. 230.0) THEN
                              H20ADJ(SPOT) = .TRUE.
                           END IF
                        END IF
C
C   IF DAYTIME AND OVER LAND OR COAST, IF THE ALBEDO IS GREATER THAN A SPECIFIED THRESHOLD,
C   SET AEROSOL AND H20ADJ TO FALSE.
C
                        IF (DAY(SPOT).AND.(SFCTYP.EQ.1.OR.SFCTYP.EQ.2.OR.SFCTYP.EQ.11)) THEN
                           IF (ALBEDO(SPOT) .GE. 39.5) THEN
                              AEROSOL(SPOT) = .FALSE.
                              H20ADJ(SPOT) = .FALSE.
                           END IF
                        END IF
C
C   IF A HIRS CHANNEL 20 ALBEDO ADJUSTMENT IS TO BE APPLIED, ADJUST THE CLOUD TOP RADIANCE
C   AND CREATE A NEW CLOUD TOP TEMPERATURE AND CORRESPONDING CLOUD TOP PRESSURE.

                        IF (H20ADJ(SPOT)) THEN
                           RCLOUD = RCLOUD - (((ALBEDO(SPOT)*0.01)**1) * RCLOUD)
                           RCLODA(SPOT) = RCLOUD
                           CLTTMP(SPOT) = IPLKRD(RCLOUD,8)
                           CALL CLDPRS(CLTTMP(SPOT),TPROFI,TROPRS,TKTRP,PRSSFC,
     +                          TROPO,ISISO,MINPRESS,CPRESS)
                           PCLOUD(SPOT) = CPRESS
                        END IF
C
C  CALCULATE THE CLOUD AMOUNT
C
                        IF (AEROSOL(SPOT)) THEN
                           IF (H20ADJ(SPOT)) THEN
                              QCFLAG(SPOT) = 5
                              RADTC8 = RADTC8 + (RADTC8 * ((0.01 * ALBEDO(SPOT))**2.6))
                              PREDTEM8(SPOT) = IPLKRD(RADTC8,8)
                              RDTC8A(SPOT) = RADTC8
                              RADCLR(SPOT) = RADCLR(SPOT) + (RADCLR(SPOT) * ((0.01 * ALBEDO(SPOT))**2.8))
                              PREDTEM8COR(SPOT) = IPLKRD(RADCLR(SPOT),8)
                           ELSE
                              QCFLAG(SPOT) = 2
                           END IF
                        ELSE
                           QCFLAG(SPOT) = 0
                        END IF

                        ECAFLAG = .TRUE.
                        SAMOUNT = ECACPE(QCFLAG(SPOT),RADTC8,RCLOUD,RADSTE(SPOT),
     +                                   ECAFLAG,RNUMER,RDENOM,RADCLR(SPOT),
     +                                   SFCTYP,MINECA,T_VALUE(SPOT))
                        IF (QCFLAG(SPOT).EQ.1.OR.QCFLAG(SPOT).EQ.3.OR.QCFLAG(SPOT).EQ.6) THEN
                           GO TO 165
                        END IF
                        IF ((QCFLAG(SPOT).EQ.2.OR.QCFLAG(SPOT).EQ.5).AND.SAMOUNT.GT.0.85) THEN
                           GO TO 165
                        END IF
                        IF (SAMOUNT.GT.1.0 .AND. (RDENOM).LT.1.0) THEN
                           GO TO 165
                        END IF
                        IF (QCFLAG(SPOT).EQ.(-1).OR.QCFLAG(SPOT).EQ.7) THEN
                           GO TO 320
                        END IF


                        IF (AEROSOL(SPOT)) THEN
                           AROSOL(SPOT) = SAMOUNT
                        END IF

C
C
C QC FOR ORIGINAL ALGORITHM MAY REQUIRE MARINE STRATUS ALGORITHM.
C ADDED UNDER WR #257. THE TEST USES THE DIFFERENCE IN THE RADIANCE
C VALUES OF THE HIRS 8 SURFACE TEMPERATURE ESTIMATE AND THE
C CLOUD TOP TEMPERATURE.
C
C CHECK FOR THE PRESENCE OF AEROSOLS. USE THE RELATIONSHIP BETWEEN
C MHS LIMB CORRECTED CHANNEL 3 AND CHANNEL 5 BRIGHTNESS
C TEMPERATURES TO CHECK FOR THE PRESENCE OF WATER VAPOR CLOUDS.
C CHECK THE HIRS CHANNEL 8 TEMPERATURE AGAINST
C THE THRESHOLDS, THEN LOOK FOR SMALL DIFFERENCES BETWEEN THE
C HIRS CHANNEL 8 SURFACE TEMPERATURE ESTIMATE AND THE CLOUD
C TOP TEMPERATURE.
C
                        TCLOUD(SPOT) = SAMOUNT
                        RADT8C8(SPOT) = RADTC8

                        IF (QCFLAG(SPOT).EQ.-1.OR.QCFLAG(SPOT).EQ.7) GO TO 320

                        IF (AEROSOL(SPOT)) THEN
                           OPDEPTH(SPOT) = ((RADT8C - RCLOUD) / (RADT8M + RCLOUD)) *
     +                                     (RADT8C / (RADT8M + RCLOUD))
                        END IF

C
C CALCULATE CLOUD-CLEARED HIRS BRIGHTNESS TEMPERATURES, PERCENT
C CONTAMINATION PER CHANNEL, CLOUD-CLEARED CLOUD TOP TEMPERATURE,
C AND CLOUD PRODUCT ERROR.
C
C
                        CALL GETERR(SPOT,QCFLAG,SAMOUNT,PTRANS,PCLOUD,BTHIRS,
     +                              TROPRS,TCLEAR4,HEIGHT,RADT8C,CCHRAD,RCCCTT,
     +                              RCC8STE,RCCPC8,CONPCT,CPERROR,MAXPRS,RADTC8,
     +                              H8STE,NCH1,NCH2,DELPRS,AMSU6,PEAKPR,DELPR1,
     +                              DELPR2,DELPR3,DELPR4,HBTCCL,DENOMTH,SFCTYP,
     +                              FOVLAT,RCLOUD,8,DELPRSX,RNUMER,RDENOM,
     +                              RADCLR(SPOT),MINECA,T_VALUE(SPOT))

                        IF (QCFLAG(SPOT).EQ.6.OR.QCFLAG(SPOT).EQ.1.OR.QCFLAG(SPOT).EQ.3) GO TO 165
                        IF (QCFLAG(SPOT).EQ.(-1).OR.QCFLAG(SPOT).EQ.7) THEN
                           GO TO 320
                        END IF


                        IF (QCFLAG(SPOT).EQ.0) THEN
                           IF (CPERROR(SPOT).GT.1.0.AND.(RNUMER) .LT. 5.0 .AND.
     +                         ABS(RDENOM) .LT. 10.0) THEN
                               GO TO 165
                           END IF
                        END IF

                        DELPRESX(SPOT) = DELPRSX

                        IF (CPERROR(SPOT).EQ.0.0) CTPEXIT(1) = CTPEXIT(1) + 1
                        IF (QCFLAG(SPOT).EQ.0.AND.RDENOM.LT.DENOMTH.AND.SAMOUNT.GT.0.2) THEN
                           QC0FAIL(SPOT) = .TRUE.
                           GO TO 170
                        END IF
C
C-- IF THE QC FLAG IS 2 FOR AN AEROSOL AND THE CLOUD PRODUCT ERROR IS GREATER THAN
C   50 PERCENT AND THE DENOMINATOR IS LESS THAN 22.0, CALCULATE THE ECA USING THE
C   EXTINCTION FUNCTION AND RECALCULATE THE CLOUD PRODUCT ERROR USING QC FLAG 6.
C
                        DO CHAN = 1, 19
                           HIRRAD(SPOT,CHAN) = PLKRAD(BTHIRS(SPOT,CHAN),CHAN)
                           CCHRAD(SPOT,CHAN) = PLKRAD(HBTCCL(SPOT,CHAN),CHAN)
                        END DO
C
                        DELPRS1(SPOT) = DELPR1
                        DO CHAN = 1, 19
                           DELPRS2(SPOT,CHAN) = DELPR2(CHAN)
                           DELPRS3(SPOT,CHAN) = DELPR3(CHAN)
                           DELPRS4(SPOT,CHAN) = DELPR4(CHAN)
                        END DO
                        DO CHAN = 1, 19
                           DELPRS8(SPOT,CHAN) = DELPRS(CHAN)
                        END DO
                        ECAORIG(SPOT) = TCLOUD(SPOT)
                        DO CHAN = 2, 12
                           CHNERRORIG(SPOT,CHAN) = 0.0
                           IF (TCLOUD(SPOT).LT.MINECA) THEN
                              CHNERRORIG(SPOT,CHAN) = R4SPV
                           END IF
                           IF (TCLOUD(SPOT).GE.MINECA.AND.CONPCT(SPOT,CHAN) .GT. 0.0
     +                        .AND. DELPRS8(SPOT,CHAN).GT. 0.0) THEN
                              RTEMP = CCHRAD(SPOT,CHAN)
                              RADTEMP = PLKRAD(TCLEAR4(SPOT,CHAN),CHAN)
                              CHNERRORIG(SPOT,CHAN) = RADTEMP - RTEMP
                              IF (CHNERRORIG(SPOT,CHAN).GT.327.67) THEN
                                 CHNERRORIG(SPOT,CHAN) = 327.67
                              END IF
                              IF (CHNERRORIG(SPOT,CHAN).LT.-327.66) THEN
                                 CHNERRORIG(SPOT,CHAN) = -327.66
                              END IF
                           END IF
                        END DO
C
C ATTEMPT TO ADJUST THE CLOUD TOP PRESSURE AND CLOUD TOP
C TEMPERATURE BASED ON THE CLOUD PRODUCT ERROR.
C
                        ADJUST = .TRUE.
c                       ADJUST = .FALSE.
                        IF (ABS(CPERROR(SPOT)).LE.0.03) ADJUST = .FALSE.
                        IF (ADJUST) THEN
                           CALL CTPADJUST(SPOT,QCFLAG,SAMOUNT,PTRANS,PCLOUD,BTHIRS,
     +                                TPROFI,CLTTMP,TROPRS,TKTRP,
     +                                TCLEAR4,HEIGHT,RADT8C,CCHRAD,RCCCTT,
     +                                RCC8STE,RCCPC8,CONPCT,CPERROR,MAXPRS,
     +                                RADTC8,H8STE,NCH1,NCH2,
     +                                DELPRS,TCLOUD,AEROSOL,
     +                                AMSU6,PEAKPR,DELPR1,
     +                                DELPR2,DELPR3,DELPR4,HBTCCL,
     +                                DENOMTH,SFCTYP,
     +                                FOVLAT,RNUMER,RDENOM,MINPRESS,CTPEXIT,
     +                                CTPITER,RCLOUD,
     +                                MINECA,8,DELPRSX,
     +                                RADSTE(SPOT),RADCLR(SPOT),T_VALUE(SPOT))
                           DELPRESX(SPOT) = DELPRSX
                           RCLODA(SPOT) = RCLOUD
                           UPCHAN(SPOT) = NCH1
                           DNCHAN(SPOT) = NCH2
                           IF (QCFLAG(SPOT).EQ.6.OR.QCFLAG(SPOT).EQ.1.OR.QCFLAG(SPOT).EQ.3) GO TO 165
                           IF (QCFLAG(SPOT).EQ.(-1).OR.
     +                         QCFLAG(SPOT).EQ.7) THEN
                              GO TO 320
                           END IF
                           DELPRS1(SPOT) = DELPR1
                           DO CHAN = 1, 19
                              DELPRS2(SPOT,CHAN) = DELPR2(CHAN)
                              DELPRS3(SPOT,CHAN) = DELPR3(CHAN)
                              DELPRS4(SPOT,CHAN) = DELPR4(CHAN)
                           END DO
                           DO CHAN = 1, 19
                              DELPRS8(SPOT,CHAN) = DELPRS(CHAN)
                           END DO
                           IF (AEROSOL(SPOT)) THEN
                              AROSOL(SPOT) = SAMOUNT
                           END IF
                        END IF
                        IF (SFCTYP.EQ.0) THEN
                           IF (PCLOUD(SPOT).GE.MAXPRS) THEN
                              QCFLAG(SPOT) = 4
                              GO TO 320
                           END IF
                        END IF
                        GO TO 310
C
  165                   CONTINUE

C
C USE THE EXTINCTION FUNCTION FOR CASES WHERE BOTH THE NUMERATOR AND DENOMINATOR ARE
C SMALL
C
                        IF (AEROSOL(SPOT)) THEN
                           IF (H20ADJ(SPOT)) THEN
                              QCFLAG(SPOT) = 3
                           ELSE
                              QCFLAG(SPOT) = 6
                           END IF
                        ELSE
                           QCFLAG(SPOT) = 1
                        END IF

                        ECAFLAG = .TRUE.
                        SAMOUNT = ECACPE(QCFLAG(SPOT),RADTC8,RCLOUD,RADSTE(SPOT),
     +                                   ECAFLAG,RNUMER,RDENOM,RADCLR(SPOT),
     +                                   SFCTYP,MINECA,T_VALUE(SPOT))

                        IF (QCFLAG(SPOT).EQ.7) GO TO 320

                        TCLOUD(SPOT) = SAMOUNT
C
                        IF (QCFLAG(SPOT).EQ.-1.OR.QCFLAG(SPOT).EQ.7) GO TO 320

                        IF (AEROSOL(SPOT)) THEN
                           AROSOL(SPOT) = SAMOUNT
                        END IF

C
C CALCULATE CLOUD-CLEARED HIRS BRIGHTNESS TEMPERATURES, PERCENT
C CONTAMINATION PER CHANNEL, CLOUD-CLEARED CLOUD TOP TEMPERATURE,
C AND CLOUD PRODUCT ERROR.
C
                        CALL GETERR(SPOT,QCFLAG,SAMOUNT,PTRANS,PCLOUD,BTHIRS,
     +                              TROPRS,TCLEAR4,HEIGHT,RADT8C,CCHRAD,RCCCTT,
     +                              RCC8STE,RCCPC8,CONPCT,CPERROR,MAXPRS,RADTC8,
     +                              H8STE,NCH1,NCH2,DELPRS,AMSU6,PEAKPR,DELPR1,
     +                              DELPR2,DELPR3,DELPR4,HBTCCL,DENOMTH,SFCTYP,
     +                              FOVLAT,RCLOUD,8,DELPRSX,RNUMER,RDENOM,
     +                              RADCLR(SPOT),MINECA,T_VALUE(SPOT))

                        DELPRESX(SPOT) = DELPRSX
                        IF (CPERROR(SPOT).EQ.0.0) CTPEXIT(1) = CTPEXIT(1) + 1
                        DELPRS1(SPOT) = DELPR1
                        DO CHAN = 1, 19
                           DELPRS2(SPOT,CHAN) = DELPR2(CHAN)
                           DELPRS3(SPOT,CHAN) = DELPR3(CHAN)
                           DELPRS4(SPOT,CHAN) = DELPR4(CHAN)
                        END DO
                        DO CHAN = 1, 19
                           HIRRAD(SPOT,CHAN) = PLKRAD(BTHIRS(SPOT,CHAN),CHAN)
                           CCHRAD(SPOT,CHAN) = PLKRAD(HBTCCL(SPOT,CHAN),CHAN)
                        END DO
                        DO CHAN = 2, 19
                           DELPRS8(SPOT,CHAN) = DELPRS(CHAN)
                        END DO
                        ECAORIG(SPOT) = TCLOUD(SPOT)
                        DO CHAN = 2, 12
                           CHNERRORIG(SPOT,CHAN) = 0.0
                           IF (TCLOUD(SPOT).LT.MINECA) THEN
                              CHNERRORIG(SPOT,CHAN) = R4SPV
                           END IF
                           IF (TCLOUD(SPOT).GE.MINECA.AND.CONPCT(SPOT,CHAN) .GT. 0.0
     +                        .AND. DELPRS8(SPOT,CHAN).GT. 0.0) THEN
                              RTEMP = CCHRAD(SPOT,CHAN)
                              RADTEMP = PLKRAD(TCLEAR4(SPOT,CHAN),CHAN)
                              CHNERRORIG(SPOT,CHAN) = RADTEMP - RTEMP
                              IF (CHNERRORIG(SPOT,CHAN).GT.327.67) THEN
                                 CHNERRORIG(SPOT,CHAN) = 327.67
                              END IF
                              IF (CHNERRORIG(SPOT,CHAN).LT.-327.66) THEN
                                 CHNERRORIG(SPOT,CHAN) = -327.66
                              END IF
                           END IF
                        END DO
                        ADJUST = .TRUE.
c                       ADJUST = .FALSE.
                        IF (ABS(CPERROR(SPOT)).LE.0.03) ADJUST = .FALSE.
                        IF (ADJUST) THEN
                           CALL CTPADJUST(SPOT,QCFLAG,SAMOUNT,PTRANS,PCLOUD,BTHIRS,
     +                                TPROFI,CLTTMP,TROPRS,TKTRP,
     +                                TCLEAR4,HEIGHT,RADT8C,CCHRAD,RCCCTT,
     +                                RCC8STE,RCCPC8,CONPCT,CPERROR,MAXPRS,
     +                                RADTC8,H8STE,NCH1,NCH2,
     +                                DELPRS,TCLOUD,AEROSOL,
     +                                AMSU6,PEAKPR,DELPR1,
     +                                DELPR2,DELPR3,DELPR4,HBTCCL,
     +                                DENOMTH,SFCTYP,
     +                                FOVLAT,RNUMER,RDENOM,MINPRESS,CTPEXIT,
     +                                CTPITER,RCLOUD,
     +                                MINECA,8,DELPRSX,
     +                                RADSTE(SPOT),RADCLR(SPOT),T_VALUE(SPOT))
                           DELPRESX(SPOT) = DELPRSX
                           RCLODA(SPOT) = RCLOUD
                           UPCHAN(SPOT) = NCH1
                           DNCHAN(SPOT) = NCH2
                           IF (QCFLAG(SPOT).EQ.(-1).OR.
     +                         QCFLAG(SPOT).EQ.4.OR.
     +                         QCFLAG(SPOT).EQ.7) THEN
                              GO TO 320
                           END IF

                           DELPRS1(SPOT) = DELPR1
                           DO CHAN = 1, 19
                              DELPRS2(SPOT,CHAN) = DELPR2(CHAN)
                              DELPRS3(SPOT,CHAN) = DELPR3(CHAN)
                              DELPRS4(SPOT,CHAN) = DELPR4(CHAN)
                           END DO
                           DO CHAN = 1, 19
                              DELPRS8(SPOT,CHAN) = DELPRS(CHAN)
                           END DO
                           IF (AEROSOL(SPOT)) THEN
                              AROSOL(SPOT) = SAMOUNT
                           END IF
                        END IF
                        GO TO 310
C
C TEST CLOUD AMOUNT
C
  310                   CONTINUE
C
                        IF (SFCTYP.EQ.0) THEN
                           IF (PCLOUD(SPOT).GE.MAXPRS) THEN
                              QCFLAG(SPOT) = 4
                              GO TO 320
                           END IF
                        END IF
                        IF (SAMOUNT.LT.MINECA) THEN
                           IF (QCFLAG(SPOT).EQ.0) THEN
                              QCFLAG(SPOT) = -1
                              GO TO 320
                           END IF
                           IF (QCFLAG(SPOT).EQ.2) THEN
                              QCFLAG(SPOT) = -1
                              GO TO 320
                           END IF
                           IF (QCFLAG(SPOT).EQ.6) THEN
                              QCFLAG(SPOT) = 7
                           END IF
                           GO TO 320
                        ELSE
                           CLAMTS(SPOT) = SAMOUNT
                           GO TO 170
                        END IF
C
C SET TO CLEAR
C
  320                   CONTINUE


                        DO CHAN = 1, 19
                           DELPRS8(SPOT,CHAN) = 0.0
                        END DO

                        CLAMTS(SPOT) = 0.0
                        AROSOL(SPOT) = 0.0
                        CLTTMP(SPOT) = 0.0
                        TCLOUD(SPOT) = 0.0
                        PCLOUD(SPOT) = 0.0
                        CPERROR(SPOT) = R4SPV
                        OPDEPTH(SPOT) = R4SPV
                        GO TO 170
C
C SET TO MISSING
C
  330                   CONTINUE
                        PRINT *,'QC ',QCFLAG(SPOT),' VALUES SET TO MISSING. LAT ',FOVLAT,
     +                          ' LON ',FOVLON
                        PRINT *,' '
                        QCFLAG(SPOT) = I2SPV
                        CLAMTS(SPOT) = R4SPV
                        AROSOL(SPOT) = R4SPV
                        CLTTMP(SPOT) = R4SPV
                        RCLODA(SPOT) = R4SPV
                        TCLOUD(SPOT) = R4SPV
                        PCLOUD(SPOT) = R4SPV
                        CPERROR(SPOT) = R4SPV
                        CTPITER(SPOT) = I2SPV
                        OPDEPTH(SPOT) = R4SPV
                        GO TO 170
C
                     ELSE
C
C  COME HERE IF THE CLOUD TOP TEMPERATURE FALLS OUTSIDE THE LIMITS
C  FOR CLOUD AMOUNT CALCULATION.
C  IF CLEAR (I.E., CLOUD TOP TEMPERATURE IS ZERO), DO NOT SET CLOUD
C  PRODUCTS TO MISSING.
C
                        IF(CLTTMP(SPOT) .EQ. 0.0) THEN
                           CLAMTS(SPOT) = 0.0
                           TCLOUD(SPOT) = 0.0
                           AROSOL(SPOT) = 0.0
C                          RCLODA(SPOT) = R4SPV
                           PCLOUD(SPOT) = 0.0
                           QCFLAG(SPOT) = -1
                           CPERROR(SPOT) = R4SPV
                           OPDEPTH(SPOT) = R4SPV
                        ELSE
                           QCFLAG(SPOT) = I2SPV
                           CLTTMP(SPOT) = R4SPV
                           RCLODA(SPOT) = R4SPV
                           CLAMTS(SPOT) = R4SPV
                           TCLOUD(SPOT) = R4SPV
                           AROSOL(SPOT) = R4SPV
                           PCLOUD(SPOT) = R4SPV
                           CPERROR(SPOT) = R4SPV
                           OPDEPTH(SPOT) = R4SPV
                        ENDIF
                     ENDIF
C
C  ABOVE ENDIF IS THE ENDIF OF CLOUD TOP TEMPERATURE CHECK.
C
                  ELSE
C
C  COME HERE IF CLOUD TOP PRESSURE IS OUT OF BOUNDS.
C  IF CLEAR,  DON'T CHANGE, SKIP TO NEXT SPOT.
C
                     IF(PCLOUD(SPOT) .EQ. 0.0) GO TO 170
                     PRINT *,'CTP OUT OF BOUNDS. VALUE = ',PCLOUD(SPOT)
                     QCFLAG(SPOT) = I2SPV
                     CLTTMP(SPOT) = R4SPV
                     RCLODA(SPOT) = R4SPV
                     CLAMTS(SPOT) = R4SPV
                     TCLOUD(SPOT) = R4SPV
                     AROSOL(SPOT) = R4SPV
                     PCLOUD(SPOT) = R4SPV
                     CPERROR(SPOT) = R4SPV
                     CTPITER(SPOT) = I2SPV
                     OPDEPTH(SPOT) = R4SPV
                  ENDIF
C
C  ABOVE END IF IS THE ENDIF OF CLOUD TOP PRESSURE CHECK.
C
  170         CONTINUE
C 171      CONTINUE

C
C   OUTPUT CLOUD PARAMETERS TO THE HIRANC AND HIRDAT BUFFERS.
C
           SPOT = 0
           DO K=1,2
              DO L=1,2
                 SPOT = SPOT + 1
                 IF (QCFLAG(SPOT).EQ.(-1)) THEN
                    MINUS1 = MINUS1 + 1
                 END IF
                 IF (QCFLAG(SPOT).EQ.0) THEN
                    COUNTQC0 = COUNTQC0 + 1
                 END IF
                 IF (QCFLAG(SPOT).EQ.1) THEN
                    COUNTQC1 = COUNTQC1 + 1
                 END IF
                 IF (QCFLAG(SPOT).EQ.4) THEN
                    COUNTQC4 = COUNTQC4 + 1
                 END IF
                 IF (QCFLAG(SPOT).EQ.5) THEN
                    COUNTQC5 = COUNTQC5 + 1
                 END IF
                 IF (QCFLAG(SPOT).EQ.6) THEN
                     EXTCNT = EXTCNT + 1
                     IF (AROSOL(SPOT).GT.0.0) THEN
                        AEROCNT = AEROCNT + 1
                     END IF
                 END IF
                 IF (QCFLAG(SPOT).EQ.2) THEN
                    AEROQC2 = AEROQC2 + 1
                 END IF
                 IF (QCFLAG(SPOT).EQ.3) THEN
                    COUNTQC3 = COUNTQC3 + 1
                 END IF
                 IF (QCFLAG(SPOT).EQ.1.OR.QCFLAG(SPOT).EQ.3.OR.
     +               QCFLAG(SPOT).EQ.6) THEN
                     RDTC8A(SPOT) = RADCLR(SPOT)
                     PREDTEM8(SPOT) = IPLKRD(RDTC8A(SPOT),8)
                 END IF

                 IF (QCFLAG(SPOT).EQ.3.AND.(.NOT.(DAY(SPOT)))) THEN
                     PRINT *,'QCFLAG ',QCFLAG(SPOT),' DAY ',DAY
                 END IF

C
C SET CLOUD PARAMETERS TO MISSING WHERE THE QC FLAG IS MISSING
C
                IF (QCFLAG(SPOT).EQ.I2SPV) THEN
                   PCLOUD(SPOT) = R4SPV
                   CLTTMP(SPOT) = R4SPV
                   CLAMTS(SPOT) = R4SPV
                   TCLOUD(SPOT) = R4SPV
                   RCLODA(SPOT) = R4SPV
                   AROSOL(SPOT) = R4SPV
                   CPERROR(SPOT) = R4SPV
                   CTPITER(SPOT) = I2SPV
                   OPDEPTH(SPOT) = R4SPV
                END IF
C
C IF SCALING A CLOUD AMOUNT RESULTS IN A ZERO, SET THE CLOUD PARAMETERS
C TO CLEAR AND RESET THE QC FLAGS ACCORDINGLY.
C

                IF (TCLOUD(SPOT).GE.0.0) THEN
                   CLDAMT = (TCLOUD(SPOT) * 1000.0)
                   RTEMP = FLOAT(CLDAMT) / 1000.0
                   IF (RTEMP.EQ.0.0) THEN
                      IF (QCFLAG(SPOT).EQ.0) QCFLAG(SPOT) = -1
                      IF (QCFLAG(SPOT).EQ.6) QCFLAG(SPOT) = 7
                      CLAMTS(SPOT) = 0.0
                      AROSOL(SPOT) = 0.0
                      CLTTMP(SPOT) = 0.0
                      PCLOUD(SPOT) = 0.0
                      TCLOUD(SPOT) = 0.0
                      CPERROR(SPOT) = R4SPV
                      OPDEPTH(SPOT) = R4SPV
                   END IF
                END IF

C CLOUD-CLEARED HIRS CHANNELS

                DO CHAN = 1, 19
                   IF (CLAMTS(SPOT).LE.0.0) THEN
                      HIRDAT(BOX(K,L,1),BOX(K,L,2),(173+CHAN)) =
     +                HIRDAT(BOX(K,L,1),BOX(K,L,2),CHAN)
                   ELSE
                      HIRDAT(BOX(K,L,1),BOX(K,L,2),(173+CHAN)) = HBTCCL(SPOT,CHAN)
                   END IF
                END DO
                HIRDAT(BOX(K,L,1),BOX(K,L,2),193) =
     +             HIRDAT(BOX(K,L,1),BOX(K,L,2),20)

C CLW-ADJUSTED AMSU-A CHANNELS. DO NOT USE ADJUSTED AMSU-A CHANNEL 1 TEMPERATURES
C OVER NON-SEA AREAS.

                 IF (NINT(HIRANC(BOX(K,L,1),BOX(K,L,2),9)) .NE. 0) THEN
                    HIRDAT(BOX(K,L,1),BOX(K,L,2),194) =
     +                 HIRDAT(BOX(K,L,1),BOX(K,L,2),21)
                 END IF

C COPY THE OBSERVED AMSU-A CHANNEL BRIGHTNESS TEMPERATURES TO NEW LOCATIONS
C FOR CHANNELS 2 THROUGH 15. CLW ADJUSTED AMSU-A CHANNEL 1 IS ALREADY IN
C HIRDAT(SPOT,LINE,194).

                DO CHAN = 1, 15
                   HIRDAT(BOX(K,L,1),BOX(K,L,2),(193+CHAN)) =
     +                HIRDAT(BOX(K,L,1),BOX(K,L,2),(20+CHAN))
                END DO


C COPY THE CLW-ADJUSTED AMSU-A CHANNEL BRIGHTNESS TEMPERATURES TO NEW LOCATIONS ONLY
C IF THE SPOT IS NOT CLEAR.

C               IF (CLAMTS(SPOT).GT.0.0) THEN
C                  DO CHAN = 4, 8
C                     HIRDAT(BOX(K,L,1),BOX(K,L,2),(193+CHAN)) =
C    +                   HIRDAT(BOX(K,L,1),BOX(K,L,2),(150+CHAN))
C                  END DO
C               END IF

                HIRANC(BOX(K,L,1),BOX(K,L,2),15) = PCLOUD(SPOT)
                HIRANC(BOX(K,L,1),BOX(K,L,2),18) = CLTTMP(SPOT)
                IF (CLTTMP(SPOT).LT.0.0.AND.CLTTMP(SPOT).NE.R4SPV) THEN
                   PRINT *,'CLTTMP = ',CLTTMP(SPOT)
                END IF
C               HIRANC(BOX(K,L,1),BOX(K,L,2),19) = CLAMTS(SPOT)
CCFT REPLACED CLOUD AMOUNT WITH EFFECTIVE CLOUD AMOUNT IN HIRANC BUFFER 23 AUG 2010
                HIRANC(BOX(K,L,1),BOX(K,L,2),19) = TCLOUD(SPOT)
                HIRANC(BOX(K,L,1),BOX(K,L,2),26) = AROSOL(SPOT)
                LOGSFC(SPOT) = ALOG(PSFC(SPOT))
                LOGCTP(SPOT) = ALOG(PCLOUD(SPOT))
                DO CHAN = 1, 19
                   LOGPEAK(SPOT,CHAN) = ALOG(PEAKPR(CHAN))
                END DO

                DO CHAN = 1, 19
                   HIRRAD(SPOT,CHAN) = PLKRAD(BTHIRS(SPOT,CHAN),CHAN)
                   CCHRAD(SPOT,CHAN) = PLKRAD(HBTCCL(SPOT,CHAN),CHAN)
                END DO
C
C-- CALCULATE THE RADIANCE ERROR FOR HIRS CHANNELS 2 THROUGH 12
C
                DO CHAN = 2, 12
                   CHNERRFIN(SPOT,CHAN) = 0.0
                END DO
                DO CHAN = 2, 8
                   IF (TCLOUD(SPOT).LE.0.0) THEN
                      CHNERRFIN(SPOT,CHAN) = R4SPV
                   END IF
                   IF (TCLOUD(SPOT).GT.0.0.AND.CONPCT(SPOT,CHAN) .GT. 0.0
     +                .AND. DELPRS8(SPOT,CHAN).GT. 0.0) THEN
                      RTEMP = CCHRAD(SPOT,CHAN)
                      RADTEMP = PLKRAD(TCLEAR4(SPOT,CHAN),CHAN)
                      if (chan.eq.8) then
                         if (QCFLAG(SPOT).EQ.1.OR.QCFLAG(SPOT).EQ.3.OR.QCFLAG(SPOT).EQ.6) then
                            RADTEMP = RADCLR(SPOT)
                         else
                            RADTEMP = RADTC8
                         end if
                      end if
                      CHNERRFIN(SPOT,CHAN) = RADTEMP - RTEMP
                      IF (CHNERRFIN(SPOT,CHAN).GT.327.67) THEN
                          CHNERRFIN(SPOT,CHAN) = 327.67
                      END IF
                      IF (CHNERRFIN(SPOT,CHAN).LT.-327.66) THEN
                         CHNERRFIN(SPOT,CHAN) = -327.66
                      END IF
                   END IF
                END DO
                
                DO CHAN = 10, 12
                   IF (TCLOUD(SPOT).LE.0.0) THEN
                      CHNERRFIN(SPOT,CHAN) = R4SPV
                   END IF
                   IF (TCLOUD(SPOT).GT.0.0.AND.CONPCT(SPOT,CHAN) .GT. 0.0
     +                .AND. DELPRS8(SPOT,CHAN).GT. 0.0) THEN
                      RTEMP = CCHRAD(SPOT,CHAN)
                      RADTEMP = PLKRAD(TCLEAR4(SPOT,CHAN),CHAN)
                      CHNERRFIN(SPOT,CHAN) = RADTEMP - RTEMP
                      IF (CHNERRFIN(SPOT,CHAN).GT.327.67) THEN
                          CHNERRFIN(SPOT,CHAN) = 327.67
                      END IF
                      IF (CHNERRFIN(SPOT,CHAN).LT.-327.66) THEN
                         CHNERRFIN(SPOT,CHAN) = -327.66
                      END IF
                   END IF
                END DO
                IF (CPERROR(SPOT).NE.R4SPV) THEN
                   IF ((ABS(CPERROR(SPOT)).GT.0.6) .OR.
     +                 (TCLOUD(SPOT).GT.1.10) .OR.
     +                 QC0FAIL(SPOT).OR.
     +                 (ABS(CHNERRFIN(SPOT,4)).GT.10.0) .OR.
     +                 (ABS(CHNERRFIN(SPOT,5)).GT.20.0) .OR.
     +                 (ABS(CHNERRFIN(SPOT,6)).GT.35.0) .OR.
     +                 (ABS(CHNERRFIN(SPOT,7)).GT.45.0) .OR.
     +                 (ABS(CHNERRFIN(SPOT,8)).GT.55.0) .OR.
     +                 (ABS(CHNERRFIN(SPOT,10)).GT.55.0) .OR.
     +                 AROSOL(SPOT).GT.0.85) THEN

                       PASSFAIL(SPOT) = 1
                       IF (AROSOL(SPOT).GT.0.85) PASSFAIL(SPOT) = 2
                       FAILCOUNT = FAILCOUNT + 1
                       IF (AROSOL(SPOT).EQ.R4SPV) THEN
                          AEROSOL = .FALSE.
                       ELSE
                          AEROSOL = .TRUE.
                       END IF
                   END IF
                END IF
                IF (PASSFAIL(SPOT).EQ.0) THEN
                   IF (CPERROR(SPOT).NE.R4SPV.AND.ABS(CPERROR(SPOT)).LE.0.05) THEN
                      CPELT5PCT = CPELT5PCT + 1
                   END IF
                END IF
              END DO
           END DO
C
C
C   OUTPUT CLOUD PARAMETERS FOR THE 2X2 SURROUNDING THIS GRID POINT
C   TO THE DSDR1T.
C
            CALL WRTR1T2800(OBSHIR,BOX,I,J,II,JJ,R1T1ST
     2                 ,OLR,LC1,LC2,LC3,LC4,QCFLAG,NEWCH8,PROFLV,
     3                 LIMTER,A10U8,PREDTEMP,STECH8,RDT8CA,
     4                 RDT8MA,RCLODA,AROSOL,H8RAD,H8STER,CCHRAD,TCLOUD,
     5                 HBTCCL,CONPCT,RCCCTT,RCC8STE,RCCPC8,CPERROR,TCLEAR4,
     6                 PREDTEM8,RDTC8A,TCLR84,PCLRBIAS,BCORSTE,BIASPCLR,
     7                 BIASPCL8,PRSTRO1,
     8                 TMPTRO1,DNCHAN,RADT8C8,RCBIAS,DELPRS8,
     9                 LOGSFC,LOGCTP,LOGPEAK,DELPRS1,DELPRS2,DELPRS3,DELPRS4,
     +                 PRSTRO2,TMPTRO2,CHNERRORIG,TSFCOB,CTPITER,CHNERRFIN,
     +                 ECAORIG,PASSFAIL,OPDEPTH,RADSTE,
     +                 RADT8CU,ATNPCLR8,BCATPCLR8,RADBCATPC8,PREDTEM8COR,
     +                 LPINDX,RADCLR,DELPRESX,NCH1GUESS,T_VALUE)
C
C
  180    CONTINUE
  190 CONTINUE
C
      IF(WRTCNT.GT.1) THEN
         KMSG(1:48)='       TEMPS EXCEEDED LIMITS IN CLOUD ALGORITHM '
         WRITE(C6MSG,'(I6)') WRTCNT
         KMSG(1:6) = C6MSG
         KMSG(49:57) = 'IN FRAME '
         WRITE(C2MSG,'(I2)') FRAMNO
         KMSG(58:59) = C2MSG
         PRINT *,KMSG
         CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
C
      RETURN
      END

