      SUBROUTINE TSTSPT(NSPOTS,NLINES,NDATEL,NANC,HIRDAT,HIRANC,
     1                  SPOT,LINE,RESULT,CLOUDY,ERSTAT)
      IMPLICIT NONE
C***********************************************************************
C
C   NAME-  TSTSPT
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0     DATE- 2/11/93    PROGRAMMER- CARLA NEIGH, SMSRC
C   VERSION- 3.0    DATE- 05/29/96   PROGRAMMER- LEAH WILSON (HSTX)
C        SYNTAX MODS FOR ATOVS BASELINE ON CRAY.
C   VERSION- 3.1    DATE- 09/23/97   PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.2    DATE- 11/19/97   PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.3    DATE- 11/18/98   PROGRAMMER- MP FERGUSON (RITSS)
C   VERSION- 3.4    DATE- 11/30/98   PROGRAMMER- MP FERGUSON (RITSS)
C   VERSION- 3.5    DATE- 09/10/99   PROGRAMMER- MP FERGUSON (RITSS)
C
C   FUNCTION-  THIS SUBROUTINE IS THE DRIVER FOR THE CLOUD TESTS ON A
C              SINGLE HIRS SPOT.  THE TESTS PERFORMED ARE THE COLD
C              SEA TEST, THE ALBEDO TEST, THE WINDOW CHANNEL TEST,
C              THE SURFACE TEMPERATURE TEST, THE NEW AVHRR TEST, AND
C              THE INTERCHANNEL REGRESSION TESTS.  IF ANY OF THESE
C              TESTS FIND THE HIRS SPOT TO BE CLOUDY, THE LOGICAL
C              FLAG CLOUDY FOR THE SPOT IS SET TO TRUE.
C   VERSION- 3.1 RTOVS UPGRADES TO CALLS TO WINCHN AND SFCTMP.
C   VERSION- 3.2 RTOVS UPGRADES TO MODIFY HIRS8 FOR WINCHN.
C   VERSION- 3.3 ARGUMENT TS8LM2 ADDED TO CALL OF WINCHN.
C   VERSION- 3.4 ARGUMENTS TS8LM3, SNWFRE, AND TS8LM4 ADDED TO CALL OF WINCHN.
C   VERSION- 3.5 ADDED TSTLM5 IN CALL TO WINCHN.
C
C   CALLING SEQUENCE PARAMETERS-
C   NAME    TYPE   I/O   DESCRIPTION
C   ----    ----   ---   -----------
C   NSPOTS   INT    I    TOTAL NUMBER OF SPOTS IN A HIRS SCAN LINE.
C   NLINES   INT    I    TOTAL NUMBER OF SCAN LINES IN THE HIRS DATA
C                        BUFFER.
C   NDATEL   INT    I    TOTAL NUMBER OF DATA ELEMENTS FOR EACH SPOT IN
C                        THE HIRS DATA BUFFER.
C   NANC     INT    I    TOTAL NUMBER OF ANCILLARY ELEMENTS IN THE
C                        HIRS ANCILLARY BUFFER.
C   HIRDAT   REAL   I    3D ARRAY (DIM NSPOTS X NLINES X NDATEL)
C                        CONTAINING THE HIRS DATA BUFFER FOR AN EXTENDED
C                        DATA FRAME.
C   HIRANC   REAL   I    3D ARRAY (DIM NSPOTS X NLINES X NDATE)
C                        CONTAINING THE HIRS ANCILLARY BUFFER FOR AN
C                        EXTENDED DATA FRAME.
C   SPOT     INT    I    SPOT NUMBER IN THE HIRS SCAN LINE FOR WHICH THE
C                        CLOUD TESTS ARE TO BE PERFORMED.
C   LINE     INT    I    SCAN LINE NUMBER WITHIN THE HIRS DATA BUFFER
C                        THAT CONTAINS THE SPOT.
C   RESULT   INT   I/O   ARRAY (DIM 20) CONTAINING THE RESULTS FOR
C                        EACH OF THE CLOUD TESTS.  A VALUE OF 0 MEANS
C                        THE TEST DETERMINED THE SPOT TO BE CLOUDY,
C                        A VALUE OF 1 MEANS THE TEST DID NOT DETERMINE
C                        THE SPOT TO BE CLOUDY, A VALUE OF 2 MEANS THE
C                        TEST COULD NOT BE PERFORMED ON THE SPOT.
C   CLOUDY LOGICAL  O    LOGICAL FLAG SET TO TRUE IF ANY CLOUD TEST
C                        DETERMINED A SPOT TO BE CLOUDY, OTHERWISE
C                        SET TO FALSE.
C   ERSTAT   INT    O    ERROR STATUS CODE
C                         = 0 IF NO ERROR OCCURRED,
C                         = 99 IF FATAL ERROR OCCURRED.
C
C   COMMON AREA PARAMETERS- REGCOE, CLDPRC, CLDDAT
C
C   LOCAL PARAMETERS-
C   NAME    TYPE   DESCRIPTION
C   ----    ----   -----------
C   CHNAVL  REAL   SPOT CONFIDENCE FROM HIRS ANCILLARY BUFFER.
C   HIRS20  REAL   HIRS CHANNEL 20 PERCENT ALBEDO.
C   ICHAVL  INT    INTEGER EQUIVALENT OF CHNAVL.
C   ITEST   INT    DO LOOP COUNTER.
C   STATUS  INT    STATUS CODE RETURNED FROM CALLED ROUTINES.
C
C   SUBPROGRAMS CALLED- CLDSEA, ALBEDO, WINCHN, SFCTMP, NEWAVR, CHREG
C
C   EXIT STATES-
C      ERSTAT = 99 : FATAL ERROR RETURNED FROM A CALLED SUBROUTINE.
C
C   COMMENTS-
C     IT IS ASSUMED THAT THE ELEMENTS OF THE ARRAY RESULT HAVE BEEN
C     INITIALIZED TO 2 BEFORE THIS ROUTINE IS CALLED.
C
C***********************************************************************
C
      LOGICAL CLOUDY
      INTEGER NSPOTS, NLINES, NDATEL, NANC, SPOT, LINE, ERSTAT
      REAL HIRDAT(NSPOTS,NLINES,NDATEL), HIRANC(NSPOTS,NLINES,NANC)
      INTEGER RESULT(20)
C
      INTEGER STATUS, ITEST, ICHAVL
      REAL HIRS20, CHNAVL, HIRS8
C
      EQUIVALENCE(CHNAVL, ICHAVL)
C
      INCLUDE 'REGCOE'
      INCLUDE 'CLDPRC'
      INCLUDE 'CLDDAT'
C
      CLOUDY = .FALSE.
      ERSTAT = 0
C
      CHNAVL = HIRANC(SPOT,LINE,7)
      HIRS20 = HIRDAT(SPOT,LINE,20)
C
C   PERFORM THE COLD SEA TEST IF THE PROCESSING FLAG IS TRUE.
C
      IF (PRCLSE) THEN
         CALL CLDSEA(OVRLND,ICEFRE,TSFEST,CSTLIM,RESULT(1),STATUS)
         IF (STATUS .EQ. 99) THEN
            ERSTAT = 99
            RETURN
         ENDIF
      ENDIF
C
C   PERFORM THE ALBEDO TEST IF THE PROCESSING FLAG IS TRUE.
C
      IF (PRALBE) THEN
C
         CALL ALBEDO(NIGHT,SNWFRE,OVRLND,ALLNDL,ALH2OL,HIRS20,RESULT(2),
     1               STATUS)
         IF (STATUS .EQ. 99) THEN
            ERSTAT = 99
            RETURN
         ENDIF
      ENDIF
C
C   PERFORM THE WINDOW CHANNEL TEST IF THE PROCESSING FLAG IS TRUE.
C
      IF (PRWCHN) THEN
         HIRS8=HIRDAT(SPOT,LINE,8)
         CALL WINCHN(NIGHT,OVRLND,TSFC8,TSFC18,TSFC19,HIRS20,WDDYLM,
     1               WDYLM2,WDNTL1,WDNTL2,WDNTL3,WDNTL4,HIRS8,TS8LIM,
     2               TS8LM2,TS8LM3,TS8LM4,TS8LM5,TROPDF,TROPLT,ICEFRE,
     3               SNWFRE,SPOT,LINE,RESULT(3),STATUS)
         IF (STATUS .EQ. 99) THEN
            ERSTAT = 99
            RETURN
         ENDIF
      ENDIF
C
C   PERFORM THE SURFACE TEMPERATURE TEST IF THE PROCESSING FLAG IS TRUE.
C
      IF (PRSFCT) THEN
         CALL SFCTMP(OVRLND,NIGHT,FORCST,TSFEST,RESULT(4),STATUS)
         IF (STATUS .EQ. 99) THEN
            ERSTAT = 99
            RETURN
         ENDIF
      ENDIF
C
C   PERFORM THE NEW AVHRR TEST IF THE PROCESSING FLAG IS TRUE.
C
      IF (PRNAVR) THEN
         CALL NEWAVR(NSPOTS,NLINES,NDATEL,HIRDAT,SPOT,LINE,NIGHT,
     1               SNWFRE,ICEFRE,OVRLND,NAVCMP,RESULT(5),STATUS)
         IF (STATUS .EQ. 99) THEN
            ERSTAT = 99
            RETURN
         ENDIF
      ENDIF
C
C   PERFORM THE INTERCHANNEL REGRESSION TEST IF THE PROCESSING FLAG IS
C   TRUE.
C
      IF (PRICHR) THEN
         CALL ICHREG2(NSPOTS,NLINES,NDATEL,SPOT,LINE,ICHAVL,
     1               HIRANC(SPOT,LINE,8),NIGHT,ICHCMP,RESULT(13),STATUS)
         IF (STATUS .EQ. 99) THEN
            ERSTAT = 99
            RETURN
         ENDIF
       ENDIF
C
C   IF ANY TEST DETERMINES THAT THE SPOT IS CLOUDY, SET THE LOGICAL
C   FLAG CLOUDY TO TRUE.
C
      DO 10 ITEST = 1, 20
         IF (RESULT(ITEST).EQ.0) THEN
             CLOUDY = .TRUE.
             RETURN
         ENDIF
   10 CONTINUE
C
      RETURN
      END
