      REAL FUNCTION ECACPE(QCFLAG,RADTC8,RCLOUD,RADSTE,ECAFLAG,RNUMER,
     +                     RDENOM,RADCLR,SFCTYP,MINECA,T_VALUE)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- ECACPE
C
C   LANGUAGE- FORTRAN      TYPE- LOGICAL FUNCTION  COMPUTER- IBM SP
C
C   VERSION- 1.0    DATE-  9/06/12    PROGRAMMER- FRANK TILLEY (IMSG)
C
C   FUNCTION - THIS FUNCTION CALCULATES EITHER THE EFFECTIVE CLOUD
C              AMOUNT (ECA) OR THE CLOUD PRODUCT ERROR (CPE) FOR
C              A CLOUDY SPOT, BASED ON THE QCFLAG ASSIGNED TO THAT
C              SPOT.
C
C   CALLING ARGUMENTS-
C      VARIABLE          TYPE I/O FUNCTION
C      --------          ---- --- --------
C      QCFLAG            INT  I/O QCFLAG INDICATING WHICH ALGORITHM TO
C                                 USE
C      RADTC8            REAL  I  RADIANCE FOR UNADJUSTED PREDICTED
C                                 CLEAR TEMPERATURE.
C      RCLOUD            REAL  I  RADIANCE FOR CLOUD TOP TEMPERATURE.
C
C      RADSTE            REAL  I  RADIANCE FOR HIRS SURFACE TEMPERATURE
C                                 WITHOUT BIAS ADJUSTMENT.
C      ECAFLAG           LOG   I  FLAG SET TO .TRUE. IF ECA IS BEING
C                                 CALCULATED.
C      RNUMER            REAL  O  RADIANCE NUMERATOR USED IN ECA/CPE CALCULATION
C      RDENOM            REAL  O  RADIANCE NUMERATOR USED IN ECA/CPE CALCULATION
C      RADCLR            REAL  I  RADIANCE FOR BIAS-CORRECTED UNADJUSTED
C                                 PREDICTED CLEAR TEMPERATURE.
C      SFCTYP            INT   I  SURFACE TERRAIN TYPE.
C      MINECA            REAL  I  ECA THRESHOLD BELOW WHICH A SPOT IS MARKED
C                                 AS CLEAR.
C   SUBPROGRAMS CALLED- NONE
C
C   COMMON AREAS- SPVAL 
C
C**********************************************************************
C
      INCLUDE 'SPVAL'

      INTEGER QCFLAG, SFCTYP

      REAL RADTC8, RCLOUD, RADSTE
      REAL RNUM1, RNUM2, RNUMER, RDENOM, DENOMTH
      REAL RADCLR, RDEN1, MINECA, T_VALUE

      PARAMETER (DENOMTH = 15.0)

      LOGICAL ECAFLAG

      ECACPE = R4SPV
      T_VALUE = R4SPV

C
C-- ORIGINAL ALGORITHM FOR WATER VAPOR CLOUDS

      IF (QCFLAG.EQ.0) THEN
         RNUMER = RADTC8 - RADSTE
         RDENOM = RADTC8 - RCLOUD
         RNUM1 = RADSTE - RADTC8
         RDEN1 = RCLOUD - RADTC8

         IF (ECAFLAG) THEN
            IF (RNUMER .LE. 0.0 .OR. RDENOM .LE. DENOMTH) THEN
               QCFLAG = 1
               GO TO 100
            END IF
         ELSE  
            IF (RDENOM.EQ.0.0) THEN
               ECACPE = 0.0
               GO TO 200
            END IF
         END IF
            
         ECACPE = RNUM1 / RDEN1

         IF (ECAFLAG) THEN
            IF (ECACPE.LE.0.0) THEN
               QCFLAG = 1
               GO TO 100
            END IF
         END IF

      END IF
   
C
C-- ALGORITHM FOR RELATIVELY LARGE AEROSOL CLOUDS
C
      IF (QCFLAG.EQ.2.OR.QCFLAG.EQ.5) THEN
         RNUMER = RADTC8 - RADSTE
         RDENOM = RADTC8 - RCLOUD
         RNUM1 = RADSTE - RADTC8
         RDEN1 = RCLOUD - RADTC8
         
         IF (ECAFLAG) THEN

c           DO NOT ALLOW THE ECA TO BE NEGATIVE

            IF (RNUMER .LE. 0.0 .OR. RDENOM .LE. DENOMTH) THEN
               IF (QCFLAG.EQ.2) QCFLAG = 6
               IF (QCFLAG.EQ.5) QCFLAG = 3
               GO TO 100
            END IF
         ELSE
            IF (RDENOM.EQ.0.0) THEN
               ECACPE = 0.0
               GO TO 200
            END IF
         END IF

         ECACPE = RNUM1  / RDEN1

         IF (ECAFLAG) THEN
            IF (ECACPE.LE.0.0) THEN
               IF (QCFLAG.EQ.2) QCFLAG = 6
               IF (QCFLAG.EQ.5) QCFLAG = 3
               GO TO 100
            END IF
         END IF
            
      END IF


C-- ALGORITHM FOR AEROSOLS OR WATER VAPOR CLOUDS USING THE
C   BOUNDARY LAYER FUNCTION

 100  CONTINUE 
      IF (QCFLAG.EQ.6.OR.QCFLAG.EQ.3.OR.QCFLAG.EQ.1) THEN
         RNUM1 = RADCLR - RADSTE
         RNUM2 = RADCLR - RCLOUD
         RNUMER = RNUM2
         IF (RNUM1.LE.0.0) RNUM1 = 0.0
         RDENOM = RADCLR - (RCLOUD - RADSTE)
         IF (ECAFLAG) THEN
            IF (RNUM2 .LE. 0.0. OR. RDENOM .LE. DENOMTH) THEN
                QCFLAG = 7
                ECACPE = 0.0
                GO TO 200
            END IF
         END IF
         
         T_VALUE = ((RCLOUD-RADCLR)*(RCLOUD-RADCLR)) /
     +       ((RCLOUD-RADCLR-RADSTE)*(RADSTE-RADCLR))

         ECACPE = (RNUM2 / RDENOM)

         IF (ECAFLAG) THEN
            IF (ECACPE.LE.0.0) THEN
               QCFLAG = 7
               ECACPE = 0.0
            END IF
            IF (ECACPE.GT.0.0.AND.ECACPE.LT.0.005) THEN
               QCFLAG = -1
               ECACPE = 0.0
            END IF
         END IF

      END IF

 200  CONTINUE
      RETURN
 
      END
