      SUBROUTINE SET3X3(HNSPOT,HTOTLN,HNANC,TMPEST,HIRANC,SPOT,LINE,
     +                  AVLBLE,TEST)
C
C********************************************************************
C
C   NAME- SET3X3
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
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- 3.1    DATE- 09/23/97   PROGRAMMER- LEAH WILSON (HSTX)
C
C   FUNCTION- THIS ROUTINE SETS UP THE 3X3 ARRAY FOR THE ADJACENT SPOT
C             TESTS.  THERE ARE TWO CONDITIONS NECESSARY TO COMPARE
C             TWO ADJACENT SPOTS.  THE FIRST CONDITION REQUIRES
C             THAT THE CENTER SPOT AND THE ADJACENT SPOT HAVE THE SAME
C             TERRAIN TYPE (UNLESS THE TERRAIN TYPE FOR ONE OF THE
C             SPOTS IS COAST).  THE SECOND CONDITION REQUIRES THAT THE
C             DIFFERENCE BETWEEN THE CENTER SPOT AND THE ADJACENT SPOT
C             BE LESS THAN 250 METERS.
C   VERSION- 3.1 RTOVS UPGRADE TO PREVENT OVERFLOW FROM CHANGES IN ADJCNT
C                FOR THE ADJACENT SPOT TEST ON THE CORNERS AND ENDS.
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function 
C
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE      TYPE   I/O      DESCRIPTION
C     --------      ----   ---      -----------
C     HNSPOT        INT     I       NUMBER OF SPOTS IN A SCAN LINE.
C     HTOTLN        INT     I       NUMBER OF LINES IN THE HIRS DATA
C                                   BUFFER.
C     HNANC         INT     I       NUMBER OF ELEMENTS IN THE HIRS
C                                   ANCILLARY BUFFER.
C     TMPEST        REAL    I       THE ARRAY OF SURFACE TEMPERATURE
C                                   ESTIMATES.
C                                   (DIMENSION  HNSPOT X HTOTLN)
C     HIRANC        REAL    I       THE HIRS ANCILLARY BUFFER FOR AN
C                                   EXTENDED DATA FRAME.
C                                   (DIMENSION HNSPOT X HTOTLN X HNANC)
C     SPOT          INT     I       THE CENTER SPOT OF THE 3X3 ARRAY TO
C                                   BE TESTED BY THE ADJACENT SPOT TEST.
C     LINE          INT     I       THE LINE CONTAINING THE CENTER SPOT
C                                   3X3 ARRAY TO BE TESTED BY THE
C                                   ADJACENT SPOT TESTS.
C     AVLBLE        INT     O       COUNTER OF ADJACENT SPOTS THAT CAN
C                                   BE USED IN THE ADJACENT SPOT TESTS.
C     TEST          INT     O       THE 3X3 ARRAY OF SURFACE TEMPERATURE
C                                   ESTIMATES TO BE USED IN THE ADJACENT
C                                   SPOT TEST.
C
C   LOCAL PARAMTERS-
C     VARIABLE      TYPE      DEFINITION
C     --------      ----      ----------
C     I             INT       DO LOOP COUNTER.
C     J             INT       DO LOOP COUNTER.
C     IA            INT       LINE COUNTER.
C     JA            INT       SPOT COUNTER.
C     DIFF          REAL      HOLDS THE ABSOLUTE VALUE OF THE TERRAIN
C                             HEIGHT DIFFERENCE BETWEEN THE CENTER
C                             SPOT AND AN ADJACENT SPOT.
C     ADJTER        INT       HOLDS THE ADJACENT SPOT TERRAIN TYPE.
C     CNTTER        INT       HOLDS THE CENTER SPOT TERRAIN TYPE.
C
C   INCLUDES- SPVAL
C
C   FUNCTIONS USED- MOD, NINT, ABS
C
C   EXIT STATES- NONE
C
C********************************************************************
C
      INTEGER SPOT, LINE, I, J, IA, JA, HNSPOT, HTOTLN, HNANC
      INTEGER AVLBLE, ADJTER, CNTTER
      REAL    TMPEST(HNSPOT,HTOTLN), TEST(3,3)
      REAL    HIRANC(HNSPOT,HTOTLN,HNANC), DIFF
      LOGICAL   ISR4SPV
C
      INCLUDE 'SPVAL'
C
C     INITIALIZE THE AVAILABLE SPOT COUNTER, SPOT COUNTER AND LINE
C     COUNTER TO ZERO.
C
      AVLBLE = 0
      IA = 0
      JA = 0
C
C     LOOP THROUGH THE PRECEDING LINE TO THE SUCCEEDING LINE.
C
      DO 20, I=LINE-1,LINE+1
C
C     INCREMENT THE LINE COUNTER BY ONE.
C
           IA = IA+1
C
C     LOOP THROUGH THE PRECEDING SPOT TO THE SUCCEEDING SPOT.
C
           DO 10, J=SPOT-1,SPOT+1
C
C     INCREMENT THE SPOT COUNTER BY ONE.
                JA = JA+1
C
C     INTERESTED IN THE ADJACENT SPOTS SO SKIP THE CENTER SPOT.
C
                IF((J.EQ.SPOT) .AND. (I.EQ.LINE)) GO TO 10
C
C     CHECK TO SEE IF THE SPOT IS A MISSING VALUE.  IF THE SPOT IS
C     MISSING THEN GO TO THE NEXT SPOT.
C
                IF(ISR4SPV(TMPEST(J,I))) THEN
C
C     SET THE 3X3 ELEMENT TO A MISSING VALUE.
C
                    TEST(JA,IA) = R4SPV
                    GO TO 10
                END IF
C
C     CHECK TO SEE IF THE CENTER SPOT AND THE ADJACENT SPOT ARE OF THE
C     SAME TERRAIN TYPE (OR IF EITHER SPOT IS DETERMINED TO BE A
C     TERRAIN TYPE OF COAST).  IF THE TERRAIN TYPES DO NOT MATCH, THEN
C     GO TO THE NEXT SPOT.
C
               ADJTER = NINT(HIRANC(J,I,9))
               CNTTER = NINT(HIRANC(SPOT,LINE,9))
               ADJTER = MOD(ADJTER,10)
               CNTTER = MOD(CNTTER,10)
C
               IF((ADJTER.EQ.2).OR.(CNTTER.EQ.2).OR.
     +            (ADJTER.EQ.CNTTER)) THEN
C
C     CHECK TO SEE IF THE CENTER SPOT AND THE ADJACENT SPOT ARE WITHIN
C     250 METERS OF EACH OTHER.  IF THEY ARE NOT THEN GO TO THE NEXT
C     SPOT.  IF THEY ARE WITHIN 250 METERS THEN PUT THE SURFACE
C     TEMPERATURE ESTIMATE INTO THE ARRAY OF ADJACENT SPOTS AND
C     INCREMENT THE AVAILABLE SPOT COUNTER BY ONE.
C
                  IF(I.EQ.0.OR.I.EQ.HTOTLN+1.OR.J.EQ.0.OR.
     +               J.EQ.HNSPOT+1)THEN 
                     TEST(JA,IA) = R4SPV
                     GO TO 10
                  ENDIF
                  DIFF = ABS(HIRANC(SPOT,LINE,8) - HIRANC(J,I,8))
                  IF(DIFF.LT.250.0) THEN
                      TEST(JA,IA) = TMPEST(J,I)
                      AVLBLE = AVLBLE + 1
                  ELSE
                      TEST(JA,IA) = R4SPV
                  END IF
               ELSE
                    TEST(JA,IA) = R4SPV
               END IF
C
   10      CONTINUE
C
C          RESET THE SPOT COUNTER TO ZERO.
C
           JA = 0
C
   20 CONTINUE
C
C     SET THE CENTER VALUE.
C
      TEST(2,2) = TMPEST(SPOT,LINE)
C
C     RETURN TO ADJCNT
C
      RETURN
      END
