      SUBROUTINE ADJCNT(HNSPOT,HTOTLN,HNANC,HIRANC,ADJTHR,CTRSLT,CLOUDY,
     +                  ADJCMP,ADH2OL,ADLNDL,TMPEST,ERSTAT)
C
C********************************************************************
C
C   NAME- ADJCNT
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/29/96   PROGRAMMER- LEAH WILSON (HSTX)
C        SYNTAX MODS FOR ATOVS BASELINE ON CRAY.
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 SETS UP THE 3 X 3 ARRAYS AND CALLS THE
C             THE ROUTINE THAT PERFORMS THE ADJACENT SPOT TESTS.
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE      TYPE   I/O      DESCRIPTION
C     --------      ----   ---      -----------
C     HNSPOT        INT     I       TOTAL NUMBER OF FIELDS OF VIEW
C                                   IN A HIRS SCAN LINE.
C     HTOTLN        INT     I       TOTAL NUMBER OF HIRS LINES IN THE
C                                   HIRS BUFFER.
C     HNANC         INT     I       TOTAL NUMBER OF ANCILLARY ELEMENTS
C                                   IN THE HIRS ANCILLARY BUFFER.
C     HIRANC        REAL    I       HIRS ANCILLARY BUFFER.
C                                   (DIMENSION  HNSPOT X HTOTLN X HNANC)
C     ADJTHR        INT     I       THRESHOLD FOR THE REQUIRED NUMBER OF
C                                   ADJACENT SPOTS NEEDED TO PERFORM THE
C                                   ADJACENT SPOT TESTS.
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, ZERO
C                                   INDICATES THAT THE SPOT WAS
C                                   DETERMINED CLOUDY.  ALL ELEMENTS
C                                   OF THIS ARRAY HAVE BEEN FILLED PRIOR
C                                   TO THIS ROUTINE EXCEPT FOR THE SPOTS
C                                   RELATED TO THE LAST THREE TESTS,
C                                   WHICH WILL BE FILLED BY THE ADJACENT
C                                   SPOT TESTS.
C                                   DIMENSION (20 X HNSPOT X HTOTLN)
C     CLOUDY      LOGICAL  I/O      FLAG INDICATING WHETHER THE SPOT WAS
C                                   DETERMINED CLOUDY.  A VALUE OF TRUE
C                                   INDICATES THE SPOT WAS DETERMINED
C                                   CLOUDY AND FALSE INDICATES CLEAR.
C                                   (DIMENSION HNSPOT X HTOTLN)
C     ADJCMP        LOGICAL I       THE PROCESSING FLAGS FOR THE
C                                   COMPONENTS OF THE ADJACENT SPOT
C                                   TEST.  A VALUE OF TRUE INDICATES
C                                   THAT THE TEST IS TO BE PERFORMED
C                                   AND FALSE INDICATES THAT IT IS NOT
C                                   TO BE PERFORMED.
C                                   (DIMENSION 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     TMPEST        REAL    I       ARRAY CONTAINING THE SURFACE
C                                   TEMPERATURE ESTIMATES FOR THE
C                                   ADJACENT SPOT TESTS.
C                                   (DIMENSION HNSPOT X HTOTLN)
C     ERSTAT        INT     O       ERROR STATUS CODE.
C
C   LOCAL PARAMTERS-
C     VARIABLE      TYPE      DESCRIPTION
C     --------      ----      -----------
C     SPOT          INT       DO LOOP COUNTER.
C     LINE          INT       DO LOOP COUNTER.
C     AVLBLE        INT       COUNTER FOR THE NUMBER OF AVAILABLE SPOTS.
C     TEST          REAL      3 X 3 ARRAY OF TEMPERATURE ESTIMATES FOR
C                             THE ADJACENT SPOT TESTS.
C     STATUS        INT       STATUS RETURNED FROM ADJCN$.
C
C   SUBROUTINES CALLED- SET3X3, ADJTST, ADJCN$
C
C   EXIT STATES- ERSTAT = 10:  NON-FATAL ERROR.  A LIMIT FOR ONE OF
C                              THE ADJACENT SPOT TESTS WAS MISSING.
C
C                ERSTAT = 99:  FATAL ERROR WAS RETURNED FROM ADJCN$
C                              OR ADJTST.
C
C********************************************************************
C
      INTEGER SPOT, LINE, HNSPOT, HTOTLN, HNANC, AVLBLE, ADJTHR
      INTEGER CTRSLT(20,HNSPOT,HTOTLN), ERSTAT, STATUS
      REAL    TMPEST(HNSPOT,HTOTLN), TEST(3,3), ADH2OL, ADLNDL
      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     LOOP THROUGH THE NUMBER OF LINES IN THE DATA FRAME, SKIPPING THE
C     THE FIRST AND LAST LINES BECAUSE THEY WILL NOT HAVE THE REQUIRED
C     SPOTS TO PERFORM THE TESTS.
C
      DO 20 LINE=2,HTOTLN-1
C
C     LOOP THROUGH THE NUMBER OF SPOTS IN A SCAN LINE (SKIPPING THE
C     FIRST AND LAST SPOT BECAUSE THEY WILL NOT HAVE THE REQUIRED SPOTS
C     TO PERFORM THE TESTS).
C
           DO 10 SPOT=2,55
C
C     CHECK THE TEMPERATURE ESTIMATE TO SEE IF THE SPOT IS A MISSING
C     VALUE.
C
              IF(ISR4SPV(TMPEST(SPOT,LINE))) GO TO 10
C
C     CHECK THE CLOUDY FLAG TO SEE IF THE SPOT IS CLEAR (THE ADJACENT
C     SPOT TEST IS ONLY PERFORMED ON THOSE SPOTS WHICH HAVE BEEN
C     DETERMINED TO BE CLEAR BY ALL THE PREVIOUS CLOUD TESTS).
C
              IF(CLOUDY(SPOT,LINE)) GO TO 10
C
C     SET UP THE 3 X 3 ARRAY WITH A CALL TO SET3X3.
C
              CALL SET3X3(HNSPOT,HTOTLN,HNANC,TMPEST,HIRANC,SPOT,LINE,
     +                    AVLBLE,TEST)
C
C     IF THE ADJACENT SPOT THRESHOLD IS MISSING, WRITE A WARNING
C     MESSAGE TO THE KIDS AND RETURN TO CONDVR.
C
              IF(ADJTHR.EQ.I4SPV) THEN
                  CALL ADJCN$(100,STATUS)
                  ERSTAT = 10
                  IF(STATUS.EQ.99) ERSTAT=99
                  RETURN
              END IF
C
C     CHECK THE AVAILABLE SPOTS TO SEE IF THE 3X3 ARRAY CAN BE USED FOR
C     THE ADJACENT SPOT TESTS.  IF THE NUMBER OF AVAILABLE SPOTS IS
C     GREATER THAN OR EQUAL TO THE THRESHOLD THEN PERFORM THE TESTS.
C
              IF(AVLBLE.GE.ADJTHR) THEN
C
C     PERFORM ADJACENT SPOT TESTS WITH A CALL TO ADJTST.
C
                  CALL ADJTST(SPOT,LINE,HNSPOT,HTOTLN,HNANC,HIRANC,TEST,
     +                        ADH2OL,ADLNDL,CLOUDY,CTRSLT,ADJCMP,AVLBLE,
     +                        ERSTAT)
C
C     IF AN ERROR STATUS OF 99 IS ENCOUNTERED THEN RETURN TO CONDVR.
C
                  IF(ERSTAT.EQ.99) THEN
                      RETURN
                  END IF
C
              END IF
C
   10      CONTINUE
C
   20 CONTINUE
C
C     RETURN TO CONDVR
C
      RETURN
      END
C**********************************************************************
      SUBROUTINE ADJCN$(MSGCOD,STAT)
C
C**********************************************************************
C
C   NAME- ADJCN$
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 ADJCNT.
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 INTERACTIVE DOC. 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
      CHARACTER*4 C4TEMP
      CHARACTER*100 KMSG
C
      DATA COUNT /0/
      SAVE COUNT
      SUBNAM = 'SPGDVR'
      STAT = 0
C
C     CHECK FOR VALID MESSAGE CODES.
C
      IF (MSGCOD .EQ. 100) THEN
         COUNT = COUNT + 1
         IF (COUNT.GT.1) RETURN
         MSGID = 100
         KMSG(1:50)='* WARNING * Adjacent Spot Tests not performed bec'
         KMSG(51:100)='ause the threshold is missing.'
         CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
      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


