      SUBROUTINE CONDVR(CLOUDY,AMASPT,DELSPT,AMALIN,DELSCN,
     1                  CNFLIM,CLWTHR,LPITHR,LATLMT,PRECIPFG,
     2                  NDIM,CHKPREC,TERRPERC,CH4LIM,CH5LIM,AVLLIM,
     3                  ALTERLIM,N16HIR,ERSTAT) 
      IMPLICIT NONE
C
C*********************************************************************
C
C   NAME- CONDVR
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916 
C
C   VERSION- 1.0  DATE- 07/13/92  PROGRAMMER- JEFFREY R. JONES (SMSRC)
C   VERSION- 2.0  DATE- 05/06/94   PROGRAMMER- FRANK TILLEY (SMSRC)
C                 XATOV-3 REPLACE COMMON BLOCK /BUFFER/ WITH
C                 PARAMETER FILE INSPARM AND COMMON BLOCK /INSCOM/.
C   VERSION- 2.1  DATE- 11/9/94   PROGRAMMER- CARLA NEIGH (SMSRC)
C     ** XATOV-71: REPLACE MSU WITH AMSU-A.  REMOVE ARGUMENT ASSOCM.
C   VERSION- 2.2  DATE- 07/11/95   PROGRAMMER- LI LU (SMSRC)
C     ** ATOVS-137: DELSPT AND DELSCN ARE ADDED AS INPUT VALUES. A CALL 
C		    TO AMABLI IS ADDED TO COLLOCATE THE AMSU-A TO HIRS
C                   USING BILINEAR INTERPOLATION. THE CODE FOR PUTTING
C		    AMSU-A VALUES IN THE HIRS DATA BUFFER IS REMOVED.
C   VERSION- 2.3  DATE- 10/04/95   PROGRAMMER- LI LU (SMSRC)
C     ** ATOVS-222: ADD CNFLIM AS A PASSING PARAMETER TO AMABLI.
C   VERSION- 2.4  DATE- 12/8/95    PROGRAMMER- CARLA NEIGH (SMSRC)
C     ** ATOVS-282:  ADDED INCLUDE FOR MONSTA AND ACCUMULATION OF
C                    STATS VARIABLES NCTDON AND NCTFAL.
C   VERSION- 3.0  DATE- 5/29/96    PROGRAMMER- LEAH WILSON (HSTX)
C     SYNTAX MODS FOR ATOVS BASELINE ON CRAY.
C   VERSION- 3.1  DATE- 6/19/97    PROGRAMMER- LEAH WILSON (HSTX)
C     DIAGNOSTIC PRINTOUT INCLUDED TO DISPLAY RESULTS OF CLOUD
C     DETECTION AND RESTORAL TESTS.  INCLUDES PRINTS TO KIDS FILE.
C     REMOVE HIRDAT/HIRANC AND ASSOCIATED VARIABLES FROM CALL TO 
C     FILDAT.
C   VERSION- 3.2  DATE- 8/18/98    PROGRAMMER- MP FERGUSON (RSTX)
C     TEMPORARY CHANGE MADE TO ALLOW CALCULATION OF SURFACE TEMPERA-
C     TURE ESTIMATES AND PERFORMANCE OF CLOUD TESTS DESPITE LOW SPOT
C     CONFIDENCE OR PRECIPITATION FAILURE.
C   VERSION- 3.3  DATE- 10/08/98   PROGRAMMER- A. ALLEGRINO (RITSS)
C     ADDED THE LAND/SEA FLAG TO THE CALLING STATEMENTS OF CONDVR 
C     AND PRECIP SO THAT THE CORRECT LIMB CORRECTION SOFTWARE COULD BE
C     USED.
C   VERSION- 3.4  DATE-  4/14/99   PROGRAMMER- A. ALLEGRINO (RITSS)
C     REMOVED THE LAND/SEA FLAG FROM THE CALLING STATEMENTS OF CONDVR
C     AND PRECIP SINCE LIMB CORRECTION IS NO LONGER DONE IN PRECIP.
C   VERSION- 3.5  DATE-  6/17/99   PROGRAMMER- A. ALLEGRINO (RITSS)
C     ADDED A CLOUD LIQUID WATER THRESHOLD TO BE PASSED INTO THE 
C     PRECIP SUBROUTINE.
C   VERSION- 3.6  DATE-  6/21/99   PROGRAMMER- A. ALLEGRINO (RITSS)
C     ADDED A LAND PRECIPITATION THRESHOLD TO BE PASSED INTO THE 
C     PRECIP SUBROUTINE.
C   VERSION- 3.7  DATE- 10/25/99   PROGRAMMER- A. ALLEGRINO (RITSS)
C     ADDED A LATITUDE THRESHOLD TO BE PASSED INTO THE 
C     PRECIP SUBROUTINE.
C   VERSION- 3.8  DATE- 02/15/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C     ADDED A CHANNEL SELECTION INDICATOR TO BE PASSED INTO THE
C     PRECIP SUBROUTINE. THIS IS USED FOR THE MEDIAN FILTER.
C   VERSION- 3.9  DATE- 03/29/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C     ADDED A LOGICAL ARRAY PRECIPFG TO EVENTUALLY DISPLAY
C     PRECIPITATION CONTAMINATION AT THE HIRS FOV.
C   VERSION- 3.10 DATE- 06/14/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C     REMOVED THE CHANNEL SELECTION INDICATOR AND ADDED THE DIMENSION
C     OF THE SQUARE, A PRECIPITATION CONTAMINATION CHECK FLAG AND A
C     TERRAIN PRECENTAGE LIMIT FOR USE BY THE MEDIAN FILTER, IN THE
C     CALL TO PRECIP AND THIS FUNCTION. (WR#74)
C   VERSION- 3.11 DATE- 08/02/00  PROGRAMMER- M.P. FERGUSON (RITSS)
C     CODE ADDED TO ENABLE MANUAL SWITCHING OFF OF CLOUD TESTS VIA
C     FLAG HIROFF OR AUTOMATED SWITCHING OFF IF THE QUALITY OF THE
C     HIRS DATA IS POOR (AS INDICATED BY FLAG BADRET) (WR # 105).
C   VERSION- 3.12 DATE- 08/02/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C     ADDED SEVERAL MEDIAN FILTER LIMITS TO PASS THRU TO THE SUBROUTINE
C     PRECIP. (WR#74)
C   VERSION- 3.13 DATE- 08/17/00   PROGRAMMER- A. ALLEGRINO (RITSS)
C     ADDED ANOTHER MEDIAN FILTER LIMIT TO PASS THRU TO THE SUBROUTINE
C     PRECIP. (WR#74)
C   VERSION- 3.14 DATE- 12/11/01   PROGRAMMER- M. FERGUSON (RITSS)
C     CALL TO RESTOR CHANGED TO PASS HIRS 8 AND 18 FOR USE IN THE NIGHT
C     VERSION OF THE TEST (WR # 183).
C   VERSION- 3.15 DATE- 07/31/03   PROGRAMMER- F. TILLEY (RAYTHEON)
C     ADDED CALLING ARGUMENT 'N16HIR' AND MODIFIED THE SELECTION OF
C     THE FIRST SPOT FOR THE BEGINNING LAT/LON FOR THE DATA FRAME
C     TO STOP PRINTING OUT MISSING VALUES.
C
C   FUNCTION-  THIS IS THE DRIVER FOR THE DATA CONTAMINATION SECTION
C              OF THE SOUNDINGS PRODUCT GENERATION SUBSYSTEM.
C              PRECIPITATION EDITING, AND CLOUD TESTS ARE PERFORMED
C              ON THE ENTIRE EXTENDED DATA FRAME.
C    
C   CALLING SEQUENCE PARAMETERS-
C
C     NAME      TYPE  I/O                   DESCRIPTION
C     --------  ----  ---   ------------------------------------------
C     CLOUDY   LOGICAL O    ARRAY (DIM INSPARM:HNSPOTS X INSPARM:HTOTLN)
C                           CONTAINING LOGICAL FLAGS FOR EACH OF THE
C                           HIRS SPOTS INDICATING IF THE SPOT IS CLOUD
C                           CONTAMINATED (TRUE = CLOUD CONTAMINATED,
C                           FALSE = NOT CLOUD CONTAMINATED).
C     AMASPT    INT    I    ARRAY (DIM INSPARM:HNSPOT X INSPARM:HTOTLN)
C                           CONTAINING THE SPOT NUMBERS FOR THE AMSU-A
C                           SPOTS COLLOCATED TO EACH HIRS SPOT.
C     DELSPT    REAL   I    ARRAY (DIM INSPARM:HNSPOT X INSPARM:HTOTLN)
C                           CONTAINING THE SPOT DISPLACEMENT OF EACH
C			    HIRS SPOT TO THE CLOSEST AMSU-A SPOT.
C     AMALIN    INT    I    ARRAY (DIM INSPARM:HNSPOT X INSPARM:HTOTLN)
C                           CONTAINING THE LINE NUMBERS FOR THE AMSU-A
C                           SPOTS COLLOCATED TO EACH HIRS SPOT.
C     DELSCN    REAL   I    ARRAY (DIM INSPARM:HNSPOT X INSPARM:HTOTLN)
C                           CONTAINING THE LINE DISPLACEMENT OF EACH
C			    HIRS SPOT TO THE CLOSEST AMSU-A SPOT.
C     CNFLIM    INT    I    THE SPOT CONFIDENCE LIMIT THAT MUST BE MET
C                           FOR CLOUD TESTS TO BE PERFORMED.
C     CLWTHR    REAL   I    CLOUD LIQUID WATER THRESHOLD VALUE FROM THE
C                           SOFTFLAGS DATASET
C     LATLMT    REAL   I    LATITUDE THRESHOLD VALUE FROM THE
C                           SOFTFLAGS DATASET
C     PRECIPFG  LOG    O    A LOGICAL ARRAY SET TO TRUE IF THE MEDIAN
C                           FILTER AND/OR THE CLOUD LIQUID WATER TEST
C                           INDICATE PRECIPITATION, AT THE HIRS FOV.
C     NDIM      INT    I    THE DIMENSIONS OF THE SQUARE AREA OF
C                           SPOTS USED IN THE MEDIAN FILTER
C     CHKPREC   INT    I    USED TO DETERMINE IF PREVIOUSLY DEFINED
C                           PRECIPITATION CONTAMINATED SPOTS WILL BE
C                           USED TO DETERMINE THE MEDIAN FILTER.
C                           (=1, NO THEY WON'T, =0, YES THEY WILL)
C     TERRPERC  INT    I    MINIMUM PERCENTAGE OF SPOTS NECESSARY
C                           TO DECLARE AN AREA OPEN OCEAN.
C     CH4LIM    REAL   I    MEDIAN FILTER LIMIT FOR AMSU CH4
C     CH5LIM    REAL   I    MEDIAN FILTER LIMIT FOR AMSU CH5
C     AVLLIM    REAL   I    THE MINIMUM PERCENTAGE OF AVAILABLE SPOTS
C                           NECESSARY FOR THE MEDIAN FILTER
C     ALTERLIM  REAL   I    MEDIAN FILTER LIMIT FOR TERRAIN TYPES: COAST
C                           ICE, SNOW, ISOLATED LAND AND ISOLATED SEA
C     N16HIR    INT    I    FLAG INDICATING WHETHER OR NOT THE HIRS
C                           DATA HAVE BEEN SHIFTED ONE SPOT TO THE
C                           RIGHT. (0 = NO SHIFT)
C     ERSTAT    INT    O    ERROR STATUS CODE-
C                            = 0 IF NO ERROR OCCURRED,
C                            = 99 IF A FATAL ERROR OCCURRED.
C
C   COMMON AREA PARAMETERS- INSPARM, INSCOM, REGCOE, FRMINF, INPARM,
C                           CLDRES, CLDDAT, CLDPRC, SPVAL, SPGUNT, HIRSTAT
C
C   LOCAL PARAMETERS-
C
C     NAME      TYPE                   DESCRIPTION
C     --------  ----   ------------------------------------------
C     BIT       INT    POINTER TO THE BIT TO BE SET IN THE HIRS
C                      ANCILLARY BUFFER ELEMENT.
C     ENLAT     REAL   ENDING LATITUDE
C     ENLON     REAL   ENDING LONGITUDE
C     FIRST   LOGICAL  HOLDS TRUE IF THIS IS THE FIRST TIME THIS
C                      ROUTINE HAS BEEN EXECUTED.
C     FLAG      INT    POINTER TO AN ELEMENT OF THE I4STBT ARRAY.
C     I,J,K     INT    DO LOOP INDEX.
C     I4STBT    INT    ARRAY (DIM 2) USED TO SET THE BITS OF THE
C                      HIRS ANCILLARY BUFFER ELEMENTS.
C     LINE      INT    DO LOOP INDEX SPECIFYING THE LINE BEING
C                      PROCESSED.
C     R4STBT    REAL   ARRAY (DIM 2) EQUIVALANCED TO I4STBT ARRAY.
C     SPOT      INT    DO LOOP INDEX SPECIFYING THE SPOT BEING
C                      PROCESSED.
C     STLAT     REAL   STARTING LATITUDE
C     STLON     REAL   ENDING LONGITUDE
C     STATUS    INT    HOLDS THE RETURN CODE FROM THE VARIOUS LOWER
C                      ROUTINES.
C     TMPEST    REAL   ARRAY (DIM INSPARM:HNSPOT X INSPARM:HTOTLN)
C                      HOLDING SURFACE TEMPERATURE ESTIMATES FOR
C                      ADJACENT SPOT TEST.
C     C5TEMP    CH*5   TEMPORARY CHARACTER BUFFER FOR WRITING TO 
C                      KIDS FILE.
C     KMSG     CH*100  KIDS MESSAGE BUFFER.
C
C   SUBROUTINES CALLED-  GTCOEF, AMABLI, PRECIP, FILDAT, TSTSPT, RESTOR,
C			 ADJCNT, KIDS
C
C   EXIT STATES-
C     ERSTAT = 99 - A FATAL ERROR OCCURRED IN A CALLED SUBROUTINE.
C
C   ASSUMPTIONS - THIS ROUTINE ASSUMES KIDS HAS BEEN CALLED PREVIOUSLY
C                 TO OPEN THE KIDS FILE.
C
C**********************************************************************
C
      INCLUDE 'INSPARM'
      INCLUDE 'INSCOM'
      INCLUDE 'SPGUNT'
C
      INTEGER AMASPT(HNSPOT,HTOTLN), AMALIN(HNSPOT,HTOTLN)
      REAL    DELSPT(HNSPOT,HTOTLN), DELSCN(HNSPOT,HTOTLN), CLWTHR
      REAL LPITHR, LATLMT,CH4LIM,CH5LIM,ALTERLIM,AVLLIM
      INTEGER ERSTAT, CNFLIM,NDIM,CHKPREC,TERRPERC, N16HIR
      LOGICAL CLOUDY(HNSPOT,HTOTLN),PRECIPFG(HNSPOT,HTOTLN)
C
      REAL    R4STBT(2), TMPEST(HNSPOT,HTOTLN), STLAT, STLON
      REAL    ENLAT, ENLON
      INTEGER BIT, I, J, K, FLAG, SPOT, LINE, STATUS,I4STBT(2) 
      INTEGER NCLOUDY,NCLEAR,ITCOUNT(20),IRCOUNT,ICCOUNT(6)
      INTEGER TCLEAR,TCLOUDY,I2COUNT(20),I2RCONT,I2CCONT(6)
      INTEGER I1COUNT(20),I1RCONT,I1CCONT(6),TTCOUNT(20)
      INTEGER TRCOUNT,TCCOUNT(6),T2COUNT(20),T2RCONT,T2CCONT(6)
      INTEGER T1COUNT(20),T1RCONT,T1CCONT(6),I11,I50
      LOGICAL FIRST
      CHARACTER*5 C5TEMP
      CHARACTER*8 SUBNAM
      CHARACTER*100 KMSG
C
      DATA FIRST/.TRUE./
      SAVE FIRST,TTCOUNT,T2COUNT,T1COUNT,TRCOUNT,T2RCONT,T1RCONT,
     1     TCCOUNT,T2CCONT,T1CCONT,TCLEAR,TCLOUDY   
C
      EQUIVALENCE(I4STBT,R4STBT)
C
      INCLUDE 'REGCOE'
      INCLUDE 'FRMINF'
      INCLUDE 'INPARM'
      INCLUDE 'CLDRES'
      INCLUDE 'CLDDAT'
      INCLUDE 'CLDPRC'
      INCLUDE 'HIRSTAT'
      INCLUDE 'MONSTA'
      INCLUDE 'SPVAL'
C
C   INITIALIZE VALUES FOR CLOUD TEST RESULTS.
C
      NCLOUDY=0
      NCLEAR=0
      IRCOUNT=0
      I2RCONT=0
      I1RCONT=0
      IF(FIRST) THEN
         TCLEAR=0
         TCLOUDY=0
         TRCOUNT=0
         T1RCONT=0
         T2RCONT=0
      ENDIF
      DO 30 I = 1,HNSPOT
         IF(I.LE.6) THEN
            ICCOUNT(I)=0
            I2CCONT(I)=0
            I1CCONT(I)=0
            IF(FIRST) THEN
               TCCOUNT(I)=0
               T2CCONT(I)=0
               T1CCONT(I)=0
            ENDIF
         ENDIF
         IF(I.LE.20) THEN
            ITCOUNT(I)=0
            I2COUNT(I)=0
            I1COUNT(I)=0
            IF(FIRST) THEN
               TTCOUNT(I)=0
               T1COUNT(I)=0
               T2COUNT(I)=0
            ENDIF
         ENDIF
         DO 20 J = 1,HTOTLN
            CLOUDY(I,J) = .FALSE.
            PRECIPFG(I,J) = .FALSE.
            RTRSLT(I,J) = 2
            TMPEST(I,J) = R4SPV
            DO 10 K = 1,20
               CTRSLT(K,I,J) = 2
               IF (K .LT. 6) CNRSLT(K,I,J) = 2
   10       CONTINUE
   20    CONTINUE
   30 CONTINUE
C
C   CHECK TO SEE IF THIS IS THE FIRST TIME CONDVR HAS BEEN CALLED.
C
      IF (FIRST) THEN
C
C       RETRIEVE THE REGRESSION COEFFICIENTS FROM THE CDB.
C
         CALL GTCOEF(STATUS)
   40    IF (STATUS .NE. 0) GOTO 110
         FIRST = .FALSE.
      END IF
C
C   PERFORM MICROWAVE PRECIPITATION EDITING ON THE ENTIRE DATA FRAME.
C
      CALL PRECIP(CLOUDY,CNFLIM,MEDLIM,HGTLIM,AMASPT,AMALIN,CLWTHR,
     >LPITHR,LATLMT,NDIM,CHKPREC,TERRPERC,CH4LIM,CH5LIM,AVLLIM,ALTERLIM)
C
C   COPY THE CONTENTS OF THE CLOUDY ARRAY TO THE PRECIPFG ARRAY.
C
      DO I=1,HNSPOT
       DO J=1,HTOTLN
        PRECIPFG(I,J) = CLOUDY(I,J)
       ENDDO
      ENDDO
C
C   COLLOCATE AMSU-A TO THE HIRS.
C
      CALL AMABLI(AMASPT,DELSPT,AMALIN,DELSCN,CNFLIM,STATUS)
      IF (STATUS .NE. 0) GOTO 110
C
C   LOOP THROUGH EACH SPOT IN THE HIRS DATA BUFFER AND PERFORM THE
C   CLOUD TESTS.
C
      DO 70 LINE = 1, HTOTLN
C
C      CHECK FOR A USABLE SCAN LINE.
C
         IF (HGOODL(LINE)) THEN
C
            DO 60 SPOT = 1,HNSPOT
C
C             IF THE SPOT WAS DETERMINED TO BE CLOUD CONTAMINATED FROM
C             THE PRECIPITATION EDITING CHECK OR THE SPOT CONFIDENCE
C             IS LESS THAN THE LIMIT, DO NOT PERFORM ANY CLOUD TESTS
C             ON THE SPOT.
C
          IF (HIROFF .OR. BADRET(SPOT,LINE)) THEN
          CLOUDY(SPOT,LINE) = .TRUE.
          IF ((HIRANC(SPOT,LINE,6) .LT. CNFLIM) .OR.
     1              (CLOUDY(SPOT,LINE))) GOTO 60
          ENDIF
C
C             PUT THE DATA NEEDED FOR THE CLOUD TESTS IN THE COMMON
C             BLOCK /CLDDAT/.
C
               CALL FILDAT(SPOT,LINE)
C
C             PUT THE SURFACE TEMPERATURE ESTIMATE IN BUFFER FOR
C             ADJACENT SPOT TEST.
C
               TMPEST(SPOT,LINE) = TSFEST
C
C             PERFORM THE CLOUD TESTS FOR A SINGLE SPOT.
C
               CALL TSTSPT(HNSPOT,HTOTLN,HDATEL,HNANC,HIRDAT,HIRANC,
     1           SPOT,LINE,CTRSLT(1,SPOT,LINE),CLOUDY(SPOT,LINE),STATUS)
               IF (STATUS.NE.0) GOTO 110
C
C             CALL RESTOR TO PERFORM THE RESTORAL TESTS ON THE
C             SPOTS MARKED CLOUDY.
C
               IF (CLOUDY(SPOT,LINE)) THEN
                 CALL RESTOR(CLOUDY(SPOT,LINE),RTRSLT(SPOT,LINE),
     1           HIRDAT(SPOT,LINE,8),HIRDAT(SPOT,LINE,18),STATUS)
                 IF (STATUS.NE.0) GOTO 110
               ENDIF
C
   60       CONTINUE
C
         END IF
C
   70 CONTINUE
C
C   PERFORM ADJACENT SPOT TEST ON THE ENTIRE DATA FRAME.
C
      IF (PRADJS) THEN
        CALL ADJCNT(HNSPOT,HTOTLN,HNANC,HIRANC,ADJTHR,CTRSLT,CLOUDY,
     1        ADJCMP,ADH2OL,ADLNDL,TMPEST,STATUS)
        IF (STATUS.EQ.99) GO TO 110
      ENDIF
C
C   LOOP THROUGH EACH ELEMENT OF THE HIRS SPOT TO SET THE BIT
C    CORRESPONDING TO THE VALUE OF THE CLOUD TEST RESULT.
C
      DO 100 LINE = 1, HTOTLN
          DO 90 SPOT = 1, HNSPOT
C
C    INITIALIZE I4STBT.
C
              I4STBT(1) = 0
              I4STBT(2) = 0
C
C    COUNT THE NUMBER OF CLOUDY RESULTING FROM THESE TESTS. (AND CLEAR).
C
              IF (CLOUDY(SPOT,LINE)) THEN
                 NCLOUDY = NCLOUDY + 1
              ELSE
                 NCLEAR = NCLEAR + 1
              ENDIF
C
              IF (CLOUDY(SPOT,LINE)) I4STBT(2) = IBSET(I4STBT(2),0)
C
C    COUNT NUMBER OF TEST FAILURES, SUCCESSES, LACK OF APPLICATIONS,
C    FOR EACH TEST.
C
              DO 79 I11=1,20
                 IF(CTRSLT(I11,SPOT,LINE).EQ.0) THEN
                    ITCOUNT(I11)=ITCOUNT(I11) + 1
                 ELSE IF(CTRSLT(I11,SPOT,LINE).EQ.1) THEN
                    I1COUNT(I11)=I1COUNT(I11) + 1
                 ELSE IF(CTRSLT(I11,SPOT,LINE).EQ.2) THEN
                    I2COUNT(I11)=I2COUNT(I11)+1
                 ENDIF
   79         CONTINUE
              IF(RTRSLT(SPOT,LINE).EQ.0) THEN
                 IRCOUNT=IRCOUNT+1
              ELSEIF(RTRSLT(SPOT,LINE).EQ.1) THEN
                 I1RCONT=I1RCONT+1
              ELSEIF(RTRSLT(SPOT,LINE).EQ.2) THEN
                 I2RCONT=I2RCONT+1
              ENDIF 
C
              DO 80 I = 1, 20
                  IF (I .LE. 16) THEN
                      BIT = 2*I
                      FLAG = 1
                  ELSE
                      BIT = 2*(I - 16)
                      FLAG = 2
                  ENDIF
                  IF (CTRSLT(I,SPOT,LINE) .EQ. 2) THEN
                      I4STBT(FLAG) = IBSET(I4STBT(FLAG),32 - BIT + 1)
                  ELSE
                      NCTDON(I) = NCTDON(I) + 1
                      IF (CTRSLT(I,SPOT,LINE) .EQ. 1) THEN
                          I4STBT(FLAG) = IBSET(I4STBT(FLAG),32 - BIT) 
                      ELSE
                          NCTFAL(I) = NCTFAL(I) + 1
                      ENDIF
                  ENDIF
   80         CONTINUE
C
C   SET THE APPROPRIATE BITS OF THE HIRS ANCILLARY BUFFER ELEMENTS.
C
              HIRANC(SPOT,LINE,13) = R4STBT(1)
              HIRANC(SPOT,LINE,14) = R4STBT(2)
   90     CONTINUE
  100  CONTINUE
C
C TOTAL UP RESULTS FOR ENTIRE ORBIT.
C
       TCLOUDY=TCLOUDY+NCLOUDY
       TCLEAR=TCLEAR+NCLEAR
       DO 123 I11=1,20
          TTCOUNT(I11)=TTCOUNT(I11)+ITCOUNT(I11)
          T1COUNT(I11)=T1COUNT(I11)+I1COUNT(I11)
          T2COUNT(I11)=T2COUNT(I11)+I2COUNT(I11)
123    CONTINUE
       TRCOUNT=TRCOUNT+IRCOUNT
       T1RCONT=T1RCONT+I1RCONT
       T2RCONT=T2RCONT+I2RCONT
C
C FIND THE BEGINNING AND ENDING LAT/LON FOR THE DATA FRAME
C
       STLAT = R4SPV
       DO I = 10, 46
          IF (N16HIR.GT.0) THEN
             IF (STLAT.EQ.R4SPV) THEN
                STLAT = HIRANC(2,I,1)
                STLON = HIRANC(2,I,2)
             END IF
          ELSE
             IF (STLAT.EQ.R4SPV) THEN
                STLAT = HIRANC(1,I,1)
                STLON = HIRANC(1,I,2)
             END IF
          END IF
          IF (HIRANC(56,I,1).NE.R4SPV) THEN
             ENLAT = HIRANC(56,I,1)
             ENLON = HIRANC(56,I,2)
          END IF
       END DO
       PRINT *, 'CLOUD DETECTION TESTS FOR FRAME EXTENDING FROM '
       PRINT 121, 'LAT/LON ',STLAT, STLON,' TO ',ENLAT,ENLON
 121   FORMAT(A,F6.2,1X,F7.2,A,F6.2,1X,F7.2)
       PRINT *, '              '
       PRINT *, '               RESULTS FOR THE 20 FOV TESTS'
       PRINT *, '             '
       WRITE(6,124)
       WRITE(6,125)
       DO 136 I50=1,20
       WRITE(6,127)I50,ITCOUNT(I50),I1COUNT(I50),I2COUNT(I50),
     + TTCOUNT(I50),T1COUNT(I50),T2COUNT(I50)
       ITCOUNT(I50)=0
       I1COUNT(I50)=0
       I2COUNT(I50)=0
136    CONTINUE
       PRINT *, '          '
       PRINT *, '                RESULTS FOR THE RESTORAL TEST '
       PRINT *, '             '
       WRITE(6,124)
       WRITE(6,125)
       I50 = 1
       WRITE(6,127)I50,IRCOUNT,I1RCONT,I2RCONT,
     + TRCOUNT,T1RCONT,T2RCONT
       IRCOUNT=0
       I1RCONT=0
       I2RCONT=0
       PRINT *, '             '
       PRINT *, 'NUMBER OF CLOUDIES THIS FRAME IS ',NCLOUDY
       PRINT *, 'NUMBER OF CLEARS THIS FRAME IS ',NCLEAR
       PRINT *, 'NUMBER OF CLOUDIES THIS ORBIT THUS FAR IS ',TCLOUDY
       PRINT *, 'NUMBER OF CLEARS THIS ORBIT THUS FAR IS ',TCLEAR
C
C  NOTE: FOR KIDS FILES, GIVE THE MAIN SPGDVR AS THE SUBNAM - THE KIDS
C  SUBROUTINE GETS THE SUBROUTINE NAME (I.E. CONDVR) ITSELF.
C
       SUBNAM='SPGDVR'
       KMSG(1:50)='CLOUD DETECTION CUMULATIVE COUNTS:       CLOUDY & '
       KMSG(51:100)='      CLEAR HIRS SPOTS.'
       WRITE(C5TEMP,'(I5)') TCLOUDY
       KMSG(36:40) = C5TEMP
       WRITE(C5TEMP,'(I5)') TCLEAR
       KMSG(51:55) = C5TEMP
       CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
C
124    FORMAT(1X,'TEST #','        THIS FRAME       ',
     + '      ORBIT THUS FAR      ')
125    FORMAT(1X,'      ',' FAILED  PASSED  NOT DONE',
     + '  FAILED  PASSED  NOT DONE')
127    FORMAT(2X,I2,3X,I6,3X,I5,4X,I5,2X,I8,2X,I6,3X,I6)
      RETURN
  110 ERSTAT = 99
      RETURN
      END
