      SUBROUTINE GETERR(SPOT,QCFLAG,CLDAMT,PTRANS,PCLOUD,BTHIRS,
     +                  TROPRS,TCLEAR4,HEIGHT,RADT8C,CCHRAD,
     +                  RCCCTT,RCC8STE,RCCPC8,CONPCT,CPERROR,SFCPRS,
     +                  RADTC8,H8STE,NCH1,NCH2,DELPRS8,AMSU6,PEAKPR,
     +                  DELPR1,DELPR2,DELPR3,DELPR4,HBTCCL,DENOMTH,
     +                  SFCTYP,FOVLAT,RCLOUD,IBIAS,DELPRSX,
     +                  RNUMER,RDENOM,RADCLR,MINECA,T_VALUE)
      IMPLICIT NONE
C
C***********************************************************************
C
C   NAME- GETERR
C
C   LANGUAGE- FORTRAN         TYPE- SUBROUTINE      COMPUTER- AIX
C
C   VERSION- 1.0     DATE- 04/24/06      PROGRAMMER-  FRANK TILLEY (IMSG)
C
C   FUNCTION- THIS SUBROUTINE USES CLOUD-CLEARED HIRS CHANNEL BRIGHTNESS
C             TEMPERATURES TO RE-COMPUTE THE CLOUD TOP TEMPERATURE, HIRS
C             CHANNEL 8 SURFACE TEMPERATURE ESTIMATE, AND PREDICTED CLEAR
C             HIRS CHANNEL 8 TEMPERATURE TO CALCULATE A CLOUD PRODUCT
C             ERROR VALUE USING THE ALGORITHM THAT WAS USED INITIALLY TO
C             CALCULATE A CLOUD AMOUNT. 
C
C   CALLING ARGUMENTS-
C   NAME    TYPE  I/O    DESCRIPTION
C   ----    ----  ---    -----------
C   SPOT      INT  I     HIRS SPOT NUMBER (1-4) IN THE 2X2 BOX FROM WHICH
C                        A RETRIEVAL WAS MADE.
C   QCFLAG    INT  I     INTEGER VALUE INDICATING THE CLOUD AMOUNT ALGORITHM
C                        USED TO GET THE ORIGINAL CLOUD AMOUNT.
C   CLDAMT   REAL  I     ORIGINAL CALCULATED CLOUD AMOUNT.
C   PTRANS   REAL  I     ARRAY OF HIRS CHANNEL TRANSMITTANCES AT
C                        RETRIEVAL LEVELS.
C   PCLOUD   REAL  I     CALCULATED CLOUD TOP PRESSURE.
C   BTHIRS   REAL  I     HIRS BRIGHTNESS TEMPERATURES (IN) (HIRS CHANNEL
C                        20 IS NOT USED.
C   TROPRS   REAL  I     TROPOPAUSE PRESSURE.
C   TCLEAR4  REAL  I     ARRAY OF OBSERVED HIRS AND AMSU-A BRIGHTNESS TEMPERATURES.
C                        HIRS CHANNEL 2 THROUGH 8 ARE PREDICTED CLEAR.
C   HEIGHT    INT  I     TERRAIN HEIGHT AT THE HIRS SPOTS.
C   RADT8C   REAL  I     ORIGINAL PREDICTED CLEAR HIRS CHANNEL 8 RADIANCE.
C   CCHRAD   REAL I/O    CLOUD-CLEARED HIRS CHANNEL RADIANCES FOR CHANNELS
C                        1-19.
C   RCCCTT   REAL I/O    CLOUD-CLEARED CLOUD TOP TEMPERATURE RADIANCES.
C   RCC8STE  REAL I/O    CLOUD-CLEARED HIRS 8 SURFACE TEMPERATURE ESTIMATE
C                        RADIANCES.
C   RCCPC8   REAL I/O    CLOUD-CLEARED PREDICTED CLEAR HIRS CHANNEL 8 RADIANCES.
C   CONPCT   REAL  O     PERCENT OF CLOUD CONTAMINATION FOR EACH HIRS
C                        CHANNEL.
C   CPERROR  REAL  O     CLOUD PRODUCT ERROR (CLOUD AMOUNT CALCULATED AFTER CLOUD
C                        CLEARING.
C   NCH1     INT   I     UPPER PEAKING CHANNEL USED TO GENERATE ORIGINAL CLOUD TOP
C                        TEMPERATURE.
C   NCH2     INT   I     LOWER PEAKING CHANNEL USED TO GENERATE ORIGINAL CLOUD TOP
C                        TEMPERATURE.
C   PEAKPR  REAL  I     PRESSURES WHERE HIRS CHANNEL SENSITIVITY PEAKS FOR THIS
C                        TEMPERATURE PROFILE.
C   HBTCCL   REAL  O     CLOUD-CLEARED HIRS BRIGHTNESS TEMPERATURES (OUT)
C                        (HIRS CHANNEL 20 IS NOT USED)
C
C   COMMOM AREA PARAMETERS - SPVAL, RETPARM, PLCOEF
C
C   LOCAL VARIABLES
C   NAME      TYPE       DESCRIPTION
C   ----      ----       -----------
C
C   FUNCTIONS USED- PLKRAD, CC8STE, ECACPE
C
C***********************************************************************
C
      INCLUDE 'SPVAL'
      INCLUDE 'RETPARM'
      INCLUDE 'PLCOEF'
C
      INTEGER SPOT, QCFLAG(4), HEIGHT(4), NCH1, NCH2
      INTEGER SFCTYP, IBIAS
C
      LOGICAL ECAFLAG
C
      REAL    AMSU6(4),PEAKPR(19),DELPR1,DELPR2(19),DELPR3(19),DELPR4(19)
      REAL    CLDAMT, PTRANS(PRFTLV,35), PCLOUD(4), BTHIRS(4,19)
      REAL    TCLEAR4(4,27), CCHRAD(4,19), RCCCTT(4), RCC8STE(4)
      REAL    RCCPC8(4), CONPCT(4,19), CPERROR(4), T_VALUE
      REAL    TROPRS,SFCPRS, RADT8C, RADTC8
      REAL    H8STE(56,6),DELPRS8(19)
      REAL    HBTCCL(4,19), DENOMTH, DELPRSX
      REAL    FOVLAT
      REAL    ECACPE, RADCLR, MINECA
C
      INTEGER CHAN
C
      REAL    PLKRAD, CC8STE
      REAL    HIRSBTS(19), HIRRAD(19), PCTCON(19), CH8STE
      REAL    TSFCOB, RADT8M, TCLEAR(27), RCLOUD
      REAL    RNUMER, RDENOM
      REAL    MINRAD, MAXRAD, RADSTE
      PARAMETER(MINRAD = 0.1, MAXRAD = 197.0)

C

      ECAFLAG = .FALSE.

C-- INITIALIZE CLOUD PRODUCT ERROR TO A MISSING VALUE.
C
      CPERROR(SPOT) = R4SPV
      DO CHAN = 1, 19
         CONPCT(SPOT,CHAN) = R4SPV
      END DO
C
C-- IF THE CLOUD AMOUNT IS MISSING, DO NOT ATTEMPT CLOUD CLEARING.
C
      IF (CLDAMT.LE.0.0) THEN
         RETURN     
      END IF
C
C-- SET UP THE ARRAYS USED IN CLOUD-CLEARING THE HIRS CHANNELS
C
      DO CHAN = 1, 19
         HIRSBTS(CHAN) = BTHIRS(SPOT,CHAN)
      END DO
C
C-- CLOUD-CLEAR THE HIRS CHANNELS IF THEY ARE TO BE CLEARED.
C
      DO CHAN = 1, 19
         DELPRS8(CHAN) = R4SPV
      END DO
      
      CALL CCHIRS(CLDAMT,PTRANS,PCLOUD,SFCPRS,HIRSBTS,HIRRAD,PCTCON,
     +            DELPRS8,NCH1,SPOT,TCLEAR4,AMSU6,PEAKPR,
     +            TROPRS,DELPR1,DELPR2,DELPR3,DELPR4,SFCTYP,HEIGHT,
     +            DELPRSX,QCFLAG,FOVLAT)

c
C
C-- SAVE THE CLOUD-CLEARED HIRS BRIGHTNESS TEMPERATURES, RADIANCES,
C   AND PERCENT CONTAMINATION TO BE WRITTEN TO THE ROTATING 1T FILE.
C
      DO CHAN = 1, 19
         HBTCCL(SPOT,CHAN) = HIRSBTS(CHAN)
         CCHRAD(SPOT,CHAN) = HIRRAD(CHAN)
         CONPCT(SPOT,CHAN) = PCTCON(CHAN)
      END DO

C
C-- MAKE A LOCAL COPY OF THE HIRS AND AMSU-A BRIGHTNESS TEMPERATURE ARRAY
C   THAT CONTAINS PREDICTED CLEAR HIRS CHANNELS 2-8 BRIGHTNESS TEMPERATURES.
C
      DO CHAN = 1, 27
         TCLEAR(CHAN) = TCLEAR4(SPOT,CHAN)
      END DO
      
      RCCPC8(SPOT) = RADT8C
C
C-- CALCULATE A HIRS 8 SURFACE TEMPERATURE ESTIMATE USING THE CLOUD-CLEARED
C   HIRS BRIGHTNESS TEMPERATURES.
C
      CH8STE = CC8STE(HIRSBTS,H8STE)
C
      TSFCOB = CH8STE

      RADT8M = PLKRAD(TSFCOB,IBIAS)
      RADSTE = RADT8M

      IF (RADT8M.LT.MINRAD) THEN
         RADT8M = MINRAD
      END IF
      IF (RADT8M.GT.MAXRAD) THEN
         RADT8M = MAXRAD
      END IF
      RADSTE = RADT8M
C
C-- STORE THE CALCULATED, NOT BIAS-CORRECTED, HIRS 8 SURFACE TEMPERATURE
C   ESTIMATE RADIANCE IN THE ARRAY ORIGINALLY DESIGNED FOR THE CLOUD-CLEARED
C   CLOUD TOP TEMPERATURE RADIANCE.
C
      RCCCTT(SPOT) = RADT8M
      RCC8STE(SPOT) = RADT8M
C
C-- USE THE QC FLAG VALUE TO DETERMINE WHICH ALGORITHM TO USE
C   TO CALCULATE CLOUD PRODUCT ERROR.
C
      IF (QCFLAG(SPOT).EQ.0) THEN
         RDENOM = RADTC8 - RCLOUD
         IF ((RDENOM.LE.DENOMTH) .AND.
     +       (BTHIRS(SPOT,8).GT.200.0) .AND.
     +       (AMSU6(SPOT).LT.238.0)) THEN
            QCFLAG(SPOT) = 1
            GO TO 200
         END IF

         ECAFLAG = .FALSE.
         CPERROR(SPOT) = ECACPE(QCFLAG(SPOT),RADTC8,RCLOUD,
     +                      RADSTE,ECAFLAG,RNUMER,RDENOM,
     +                      RADCLR,SFCTYP,MINECA,T_VALUE)
         GO TO 1000
      END IF
C
C CALCULATE CPE FOR AEROSOL USING THE ORIGINAL ALGORITHM
C
      IF (QCFLAG(SPOT).EQ.2.OR.QCFLAG(SPOT).EQ.5) THEN

C

         ECAFLAG = .FALSE.
         CPERROR(SPOT) = ECACPE(QCFLAG(SPOT),RADTC8,RCLOUD,
     +                   RADSTE,ECAFLAG,RNUMER,RDENOM,
     +                   RADCLR,SFCTYP,MINECA,T_VALUE)
         GO TO 1000
      END IF
C
 200  CONTINUE
      IF (QCFLAG(SPOT).EQ.6.OR.QCFLAG(SPOT).EQ.1.OR.QCFLAG(SPOT).EQ.3) THEN
         
         ECAFLAG = .FALSE.
         CPERROR(SPOT) = ECACPE(QCFLAG(SPOT),RADTC8,RCLOUD,
     +                   RADSTE,ECAFLAG,RNUMER,RDENOM,
     +                   RADCLR,SFCTYP,MINECA,T_VALUE)
      END IF

C
 1000 CONTINUE


      RETURN
      END
