      SUBROUTINE ADJTST(SPOT,LINE,HNSPOT,HTOTLN,HNANC,HIRANC,TEST,
     +        ADH2OL,ADLNDL,CLOUDY,CTRSLT,ADJCMP,AVLBLE,ERSTAT)
C
C********************************************************************
C
C   NAME- ADJTST
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- HDS-9000
C
C   VERSION- 1.0    DATE- 09/11/91   PROGRAMMER- JEFF MANNING (SMSRC)
C   VERSION- 3.0    DATE- 05/21/96   PROGRAMMER- LEAH CASEY (HSTX)
C         INITIALIZE DIFF ARRAY FOR CRAY BASELINE.  MOVE INCREMENTING
C         OF ADJSPT TO AFTER I=2.AND.J=2 TEST TO PREVENT DIFF ARRAY
C         FROM OVERFLOWING.
C   VERSION- 1.2    DATE- 02/07/02   PROGRAMMER- RICKEY ROLLINS (CSC)
C         DECLARE ADJCMP AS L*4
C   VERSION- 1.3    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function
C
C   FUNCTION- THIS ROUTINE PERFORMS THE ADJACENT SPOT TESTS ON THE
C             3D ARRAY PASSED IN.  THERE ARE THREE COMPONENTS OR TESTS
C             INVOLVED.  THE FIRST TEST CHECKS TO SEE IF ALL THE
C             ADJACENT SPOTS ARE CLOUDY.  IF THEY ARE ALL FOUND TO BE
C             CLOUDY THEN THE CENTER SPOT IS MARKED AS CLOUDY.  THE
C             SECOND TEST MARKS THE CENTER SPOT AS CLOUDY IF THE
C             FOLLOWING CONDITION IS TRUE FOR AT LEAST ONE ADJACENT
C             SPOT:
C
C             TSE(ADJ) - TSE(CENTER) > TEST LIMIT
C             (TSE BEING THE SURFACE TEMPERATURE ESTIMATE)
C
C             THE THIRD TEST MARKS THE CENTER SPOT BE MARKED AS
C             CLOUDY IF THE FOLLOWING CONDITION IS TRUE FOR ALL OF THE
C             ADJACENT SPOTS:
C
C             ABS(TSE(ADJ) - TSE(CENTER)) >= TEST LIMIT
C
C             THE TEST LIMIT USED FOR TESTS TWO AND THREE IS DEPENDENT
C             UPON WHETHER THE SPOTS BEING TESTED ARE OVER LAND OR
C             WATER.  IF THE CENTER SPOT IS OF THE TERRAIN TYPE LAND OR
C             COAST, THEN THE TEST LIMIT FOR OVER LAND SHOULD BE USED.
C             IF THE CENTER SPOT IS OF THE TERRAIN TYPE SEA, THEN THE
C             TEST LIMIT FOR OVER SEA SHOULD BE USED.
C
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE     TYPE   I/O       DESCRIPTION
C     --------     ----   ---       -----------
C     SPOT         INT     I        CENTER SPOT NUMBER.
C     LINE         INT     I        CENTER SPOT LINE NUMBER.
C     HNSPOT       INT     I        NUMBER OF SPOTS IN A HIRS SCAN LINE.
C     HTOTLN       INT     I        NUMBER OF LINES IN A HIRS BUFFER.
C     HNANC        INT     I        NUMBER OF HIRS ANCILLARY ELEMENTS IN
C                                   THE HIRS ANCILLARY BUFFER.
C     HIRANC       REAL    I        HIRS ANCILLARY BUFFER FOR AN
C                                   EXTENDED DATA FRAME.
C                                   (DIMENSION HNSPOT X HTOTLN X HNANC)
C     TEST         REAL    I        THE 3X3 ARRAY OF SURFACE TEMPERATURE
C                                   ESTIMATES TO BE USED IN THE ADJACENT
C                                   SPOT TEST.
C                                   (DIMENSION 3 X 3)
C     ADH2OL       REAL    I        ADJACENT SPOT TEST LIMIT FOR WATER
C                                   AREAS.
C     ADLNDL       REAL    I        ADJACENT SPOT TEST LIMIT FOR LAND
C                                   AREAS.
C     CLOUDY     LOGICAL  I/O       FLAG INDICATING CLOUDY OR CLEAR.
C                                   A VALUE OF TRUE INDICATES CLOUDY
C                                   AND FALSE INDICATES CLEAR.
C                                   (DIMENSION HNSPOT X HTOTLN)
C     CTRSLT       INT    I/O       ARRAY CONTAINING THE CLOUD TEST
C                                   RESULTS FOR EACH TEST.  A ONE
C                                   INDICATES THAT THE SPOT WAS
C                                   DETERMINED TO BE NOT CLOUDY, A ZERO
C                                   INDICATES THAT IT WAS DETERMINED
C                                   CLOUDY, AND TWO INDICATES THE TEST
C                                   WAS NOT PERFORMED.
C                                   (DIMENSION 20 X HNSPOT X HTOTLN)
C     ADJCMP     LOGICAL   I        LOGICAL ARRAY CONTAINING THE
C                                   PROCESSING FLAGS FOR THE COMPONENTS
C                                   OF THE ADJACENT SPOT TEST.  A VALUE
C                                   OF TRUE INDICATES THAT THE COMPONENT
C                                   OF THE TEST IS TO BE PERFORMED AND
C                                   FALSE INDICATES THAT IT IS NOT TO BE
C                                   PERFORMED.
C                                   (DIMENSION 3)
C     AVLBLE       INT     I        COUNTER OF ADJACENT SPOTS THAN CAN
C                                   BE USED IN THE ADJACENT SPOT TESTS.
C     ERSTAT       INT     O        ERROR STATUS CODE. A VALUE OF 99
C                                   REPRESENTS A FATAL ERROR.
C
C   LOCAL PARAMTERS-
C     VARIABLE      TYPE      DEFINITION
C     --------      ----      ----------
C     ADJCNT        INT       ADJACENT SPOT COUNTER.
C     I,J           INT       DO LOOP COUNTERS.
C     ABSCNT        INT       ABSOLUTE DIFFERENCE COUNTER FOR COMPONENT
C                             THREE.
C     SPTCNT        INT       COUNTER FOR SPOTS WITH A CLOUDY FLAG OF
C                             TRUE.  USED FOR COMPONENT ONE.
C     DIFF          REAL      DIFFERENCE BETWEEN SPOT SUM OF ADJACENT
C                             SPOT AND THE SPOT SUM OF THE CENTER SPOT.
C     ABSDIF        REAL      ABSOLUTE DIFFERENCE BETWEEN THE ADJACENT
C                             SPOT AND THE SPOT THE CENTER SPOT.
C     STATUS        INT       STATUS RETURNED FROM ADJTS$.
C     TSTLIM        REAL      SET TO THE TEST LIMIT DEPENDING ON WHETHER
C                             THE LIMIT IS FOR LAND OR WATER.
C
C   FUNCTIONS USED- ABS
C
C   SUBROUTINES CALLED- ADJTS$
C
C   EXIT STATES- ERSTAT = 10:  A NON-FATAL ERROR.  A LIMIT FOR ONE OF
C                              THE ADJACENT SPOT TESTS WAS MISSING.
C
C                ERSTAT = 99:  A FATAL ERROR WAS RETURNED FROM ADJTS$.
C
C   REFERENCE- CHEDIN ET AL, "RECENT PROGRESS IN THE DETERMINATION OF
C              METEOROLOGICAL PARAMETERS FROM THE SATELLITES OF THE
C              TIROS-N SERIES".  TECHNICAL PROCEEDINGS THIRD ITOVS
C              STUDY CONFERENCE, 13-19 AUGUST 1986, MADISON, WI.
C
C********************************************************************
C
      INTEGER I, J, HNSPOT, HTOTLN, HNANC, AVLBLE, STATUS, ERSTAT
      INTEGER CTRSLT(20,HNSPOT,HTOTLN)
      INTEGER SPOT, LINE, SPTCNT, ADJSPT, ABSCNT
      REAL    TEST(3,3), ADH2OL, ADLNDL, TSTLIM, DIFF(8), ABSDIF
      REAL    HIRANC(HNSPOT,HTOTLN,HNANC)
C     LOGICAL CLOUDY(HNSPOT,HTOTLN),ADJCMP(3)
      LOGICAL CLOUDY(HNSPOT,HTOTLN)
      LOGICAL*4 ADJCMP(3)
      LOGICAL ISR4SPV
C
      INCLUDE 'SPVAL'
C
C     INITIALIZE VARIABLES.
C
      ERSTAT = 0
      SPTCNT = 0
      ADJSPT = 0
      ABSCNT = 0
      DO 5 I=1,8
         DIFF(I) = 0.0
    5 CONTINUE
C
C     CHECK THE COMPONENT FLAG TO SEE IF THE FIRST TEST SHOULD BE
C     PERFORMED.
C
      IF(ADJCMP(1)) THEN
C
C         LOOP THROUGH THE 3X3 ARRAY OF ADJACENT SPOTS.
C
          DO 20, J = LINE-1,LINE+1
              DO 10, I = SPOT-1,SPOT+1
C
C                 SKIP THE CENTER SPOT.
C
                  IF((I.EQ.SPOT).AND.(J.EQ.LINE)) GO TO 10
C
C                 CHECK TO SEE IF THE SPOT IS CLOUDY.
C
                  IF(CLOUDY(I,J)) SPTCNT=SPTCNT+1
   10         CONTINUE
   20     CONTINUE
C
C         CHECK THE CLOUDY SPOT COUNTER.
C
          IF(SPTCNT.EQ.AVLBLE) THEN
              CLOUDY(SPOT,LINE) = .TRUE.
              CTRSLT(18,SPOT,LINE) = 0
C
C             RETURN TO ADJCNT.
C
              RETURN
          ELSE
              CTRSLT(18,SPOT,LINE) = 1
          END IF
      END IF
C
C     CHECK THE CENTER SPOT'S TERRAIN TYPE.
C
      IF((HIRANC(SPOT,LINE,9).EQ.1).OR.(HIRANC(SPOT,LINE,9).EQ.2).OR.
     +   (HIRANC(SPOT,LINE,9).EQ.11)) THEN
C
C         SET THE TEST LIMIT TO THE LAND LIMIT.
C
C         CHECK TO SEE IF THE TEST LIMIT IS MISSING.
C
          IF(ISR4SPV(ADLNDL)) THEN
             CALL ADJTS$(101,STATUS)
             ERSTAT = 10
             IF(STATUS.EQ.99) ERSTAT=99
             RETURN
          ELSE
             TSTLIM = ADLNDL
          END IF
      ELSE
C
C         SET THE TEST LIMIT TO THE WATER LIMIT.
C
C         CHECK TO SEE IF THE TEST LIMIT IS MISSING.
C
          IF(ISR4SPV(ADH2OL)) THEN
             CALL ADJTS$(102,STATUS)
             ERSTAT = 10
             IF(STATUS.EQ.99) ERSTAT=99
             RETURN
          ELSE
             TSTLIM = ADH2OL
          END IF
      END IF
C
C     LOOP THROUGH THE ADJACENT SPOTS.
C
      DO 40, J=1,3
          DO 30, I=1,3
C
C             SKIP THE CENTER SPOT.
C
              IF((I.EQ.2).AND.(J.EQ.2)) GO TO 30
C
C             INCREMENT THE ADJACENT SPOT COUNTER.
C
              ADJSPT = ADJSPT + 1
C
C             CHECK TO SEE IF THE ADJACENT SPOT IS A MISSING VALUE.
C
              IF(ISR4SPV(TEST(I,J))) GO TO 30
C
C             GET THE DIFFERENCE BETWEEN THE ADJACENT SPOT AND THE
C             CENTER SPOT.
C
              DIFF(ADJSPT) = TEST(I,J) - TEST(2,2)
C
C             CHECK THE COMPONENT FLAG TO SEE IF THE SECOND TEST SHOULD
C             BE PERFORMED.
C
              IF(ADJCMP(2)) THEN
C
C                 IF THE DIFFERENCE IS GREATER THAN THE TEST LIMIT THEN
C                 SET THE CLOUDY FLAG TO TRUE.
C
                  IF(DIFF(ADJSPT).GT.TSTLIM) THEN
                      CLOUDY(SPOT,LINE) = .TRUE.
                      CTRSLT(19,SPOT,LINE) = 0
C
C                     RETURN TO ADJCNT
C
                      RETURN
                  ELSE
                      CTRSLT(19,SPOT,LINE) = 1
                  END IF
C
              END IF
C
C             CHECK THE ABSOLUTE DIFFERENCE.
C
              ABSDIF = ABS(DIFF(ADJSPT))
              IF(ABSDIF.GE.TSTLIM) ABSCNT = ABSCNT + 1
   30     CONTINUE
   40 CONTINUE
C
C     CHECK THE PROCESSING FLAG TO SEE IF THE THIRD TEST SHOULD BE
C     PERFORMED.
C
      IF(ADJCMP(3)) THEN
C
C         CHECK THE TEST COUNTER TO SEE IF IT EQUALS THE AVAILABLE
C         SPOTS (THAT ALL ADJACENT SPOTS MET THE ABOVE CONDITION).
C
          IF(ABSCNT.EQ.AVLBLE) THEN
              CLOUDY(SPOT,LINE) = .TRUE.
              CTRSLT(20,SPOT,LINE) = 0
          ELSE
              CTRSLT(20,SPOT,LINE) = 1
          END IF
      END IF
C
C     RETURN TO ADJCNT
C
      RETURN
      END
C**********************************************************************
      SUBROUTINE ADJTS$(MSGCOD,STAT)
C
C**********************************************************************
C
C   NAME- ADJTS$
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916 
C
C   VERSION- 1.0    DATE- 02/08/93    PROGRAMMER- JEFF MANNING (SMSRC)
C   VERSION- 3.0    DATE- 09/24/98    PROGRAMMER- LEAH WILSON, RAYTHEON
C
C   FUNCTION- THIS SUBROUTINE WRITES OUT ERROR MESSAGES TO THE
C             MAINTENANCE MESSAGE LOG FILE FOR THE SUBROUTINE ADJTST.
C   VERSION- 3.0 CONVERT FROM MMLF TO KIDS.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O   UNIT  FUNCTIONAL DESCRIPTION
C     ------------------------ ---   ----  ----------------------
C     KIDS                      O   KUNIT  KRAY I/A DOCUMENTATION SYSTEM
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  FUNCTION
C     --------          ---- ---  --------
C     MSGCOD             INT  I   MESSAGE CODE OF ERROR DETECTED.
C     STAT               INT  O   ERROR STATUS CODE
C                                  = 0 IF NO ERROR OCCURRED,
C                                 <> 0 IF ERROR OCCURRED.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'SPGUNT'
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  FUNCTION
C     --------          ----  --------
C     I                  INT  DO LOOP CONTROL INDEX.
C     COUNT              INT  COUNTER FOR NUMBER OF TIMES $-ROUTINE
C                             HAS BEEN INVOKED FOR EACH MESSAGE.
C
C   SUBPROGRAMS CALLED-  KIDS 
C
C   EXIT STATES-
C       STAT =  1; INDICATING A NON-FATAL ERROR WAS DETECTED.
C       STAT = 99; INDICATING A FATAL ERROR WAS DETECTED.
C
C***********************************************************************
C
      INTEGER   STAT, I, MSGCOD, COUNT(2)
      CHARACTER*4 C4TEMP
      CHARACTER*100 KMSG
C
      DATA COUNT /0,0/
      SAVE COUNT
C
C   SET THE SUBROUTINE NAME.
C
      SUBNAM = 'SPGDVR'
      STAT = 0
C
C     CHECK FOR VALID MESSAGE CODES.
C
      IF (MSGCOD .EQ. 101) THEN
         COUNT(1) = COUNT(1) + 1
         IF (COUNT(1).GT.1) RETURN
         KMSG(1:50)='* WARNING * Adjacent Spot Tests could not be perf'
         KMSG(51:100)='ormed over land due to missing test threshold.'
         CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSEIF (MSGCOD .EQ. 102) THEN
C
         COUNT(2) = COUNT(2) + 1
         IF (COUNT(2).GT.1) RETURN
         KMSG(1:50)='* WARNING * Adjacent Spot Tests could not be perf'
         KMSG(51:100)='ormed over Sea due to missing test threshold.'
         CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE
C
C NOT A VALID MSGCOD
C
        STAT=0
        KMSG = ' Invalid error message code = '
        WRITE(C4TEMP,'(I4)') MSGCOD
        KMSG(31:34) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      ENDIF
      RETURN
      END


