      SUBROUTINE PIKSPT(SPOT,LINE,CHNCMB,HNSPOT,HTOTLN,HNANC,HIRANC,
     +                  ACCSPT,ACCLNE,CLEAR,NOHIRS,HIRDAT,
     +                  HDATEL)
      IMPLICIT NONE
C
C********************************************************************
C
C   NAME- PIKSPT
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 09/12/00   PROGRAMMER- M.P. FERGUSON (RITSS)
C   VERSION- 1.1    DATE- 07/02/01   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 1.2    DATE- 09/06/01   PROGRAMMER- M.P. FERGUSON (RITSS)
C   VERSION- 1.3    DATE- 09/08/01   PROGRAMMER- M.P.FERGUSON (RITSS)
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C
C   FUNCTION- THIS ROUTINE LOCATES THE MOST ACCEPTABLE SPOT FOR
C             EACH RETRIEVAL GRID POINT IF ONE IS AVAILABLE.
C
C   VERSION 1.0  SUBROUTINE REVAMPED TO MAKE LOGIC EASIER TO FOLLOW AND ELI-
C                MINATE SOME PROBLEMS IN SELECTION PROCESS.  NOW SPOT SELECT-
C                ED BY MEANS OF A SIMPLE SCORING SYSTEM, AND THE SPOT PICKED
C                IS EITHER CLEAR OR CLOUDY (PR # 24).
C   VERSION 1.1  ADDED CHECK FOR MISSING LATITUDE FOR A SPOT TO EXCLUDE THAT
C                SPOT FROM THE SELECTION PROCESS.  THIS IS ESPECIALLY
C                IMPORTANT FOR THE NOAA-16 HIRS WHERE THE DATA ARE SHIFTED
C                ONE SPOT TO THE RIGHT. (WR #163)
C   VERSION 1.2  SUBROUTINE MODIFIED TO CHANGE WEIGHING OF "NON-COAST" DESIG-
C                NATION AS WELL AS CREATE MORE DIVISION  FOR THE "TERRAIN
C                AGREEMENT" CATEGORY, NOW DESIGNATED AS ESTABLISHING "HIGH 
C                QUALITY SEA, NON-SEA/NON-COAST, OR COAST" (PR # 50).
C   VERSION 1.3  SUBROUTINE MODIFIED AGAIN TO REMOVE ALL BIAS AGAINST COAST
C                FOV SELECTION, AND TO BEGIN USING AMSU-A CH. 2 INSTEAD OF
C                HIRS 8 AS THE LOWEST ORDER CRITERIA. THE COOLEST AMSU-A 2
C                OVER THE OCEAN AND THE WARMEST IN THE NON-SEA BOX IS 
C                PREFERRED.
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function 
C
C
C                 
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE     TYPE    I/O      DESCRIPTION
C     --------     ----    ---      -----------
C     SPOT          INT     I       CURRENT SPOT.
C     LINE          INT     I       CURRENT LINE.
C     CHNCMB        INT     I       CLEAR CHANNEL MASK COMBINATION FOR
C                                   THE INSTRUMENTS (DIM 2X4X2).
C     HNSPOT        INT     I       TOTAL NUMBER OF HIRS SPOTS.
C     HTOTLN        INT     I       TOTAL NUMBER OF HIRS LINES.
C     HNANC         INT     I       TOTAL NUMBER OF HIRS ANCILLARY
C                                   ELEMENTS.
C     HIRANC        REAL    I       HIRS ANCILLARY BUFFER
C                                   (HNSPOT X HTOTLN X HNANC).
C     ACCSPT        INT     O       SPOT NUMBER OF THE ACCEPTABLE
C                                   CLEAR SPOT.
C     ACCLNE        INT     O       LINE NUMBER OF THE ACCEPTABLE
C                                   CLEAR SPOT.
C     CLEAR         LOG     O       FLAG INDICATING A CLEAR SPOT WAS
C                                   FOUND (TRUE IF CLEAR, FALSE IF NOT).
C     NOHIRS        LOG     O       FLAG INDICATING THAT THE HIRS IS
C                                   UNAVAILABLE FOR THIS SPOT.
C     HIRDAT        REAL    I       HIRS DATA BUFFER.
C     HDATEL        INT     I       NUMBER OF DATA ELEMENTS IN HIRDAT.
C
C   LOCAL PARAMTERS-
C     VARIABLE      TYPE      DESCRIPTION
C     --------      ----      -----------
C     I              INT      LOOP COUNTERS.
C     NOHCTR         INT      NO-HIRS SPOT COUNTER.
C     NCLCTR         INT      NON-CLEAR SPOT COUNTER.
C     CHRSLT         INT      VARIABLE USED TO COMPARE CHANNEL
C                             AVAILABILITY AGAINST THE CLEAR CHANNEL
C                             MASK.
C     NSPOTS         INT      NUMBER OF GOOD SPOTS IN THE 2X2 BOX.
C     RTEMP          REAL     TEMPORARY VARIABLE (REAL TYPE) FOR THE
C                             CHANNEL AVAILABILITY OF THE HIRS AND
C                             CLOUD TEST RESULTS.
C     ITEMP          INT      TEMPORARY VARIABLE (INTEGER TYPE) FOR THE
C                             CHANNEL AVAILABILITY OF THE HIRS AND CLOUD
C                             TEST RESULTS.
C
C   SUBROUTINE USED- NONE
C
C   FUNCTIONS USED- REAL, IAND. BTEST
C
C   EXIT STATES- NONE
C
C********************************************************************
C
      INTEGER SPOT, LINE, HNSPOT, HTOTLN, ACCSPT, ACCLNE
      INTEGER FOVSCR(HNSPOT,HTOTLN),LIM,HOLD
      INTEGER NOHCTR, NCLRCT, I, CHNCMB(2,4,2), HNANC, CHRSLT
      INTEGER ITEMP, HDATEL, NSEA, ILNDSEA, NCOAST, ITERRR
      INTEGER BADSPT(HNSPOT,HTOTLN),TOTCAS(6),ICLEAR,ICLEAR1
      INTEGER NCSTRETS, J, K, L, ICASE, NSPOTS, NCLNSE, NCLSEA
CCMPF INTEGER IINIT,NCSTRETS,NCLR2CLD
      REAL   HIRANC(HNSPOT,HTOTLN,HNANC), HIRDAT(HNSPOT,HTOTLN,HDATEL)
      REAL   RTEMP,  RCLEAR, RCLEAR1, PROPO1
      LOGICAL CLEAR, NOHIRS, LSEA
      LOGICAL   ISR4SPV
C
      EQUIVALENCE(ITEMP,RTEMP)
      EQUIVALENCE(ICLEAR,RCLEAR)
      EQUIVALENCE(ICLEAR1,RCLEAR1)
C
      INCLUDE 'SPVAL'
      INCLUDE 'BOXPRM'
C
C     INITIALIZE VARIABLES.
C
      NSEA = 0
      PROPO1 = 0.0
      NOHCTR = 0
      NCLNSE = 0
      NCLSEA = 0
      ACCSPT = I4SPV
      ACCLNE = I4SPV
      HOLD = 0
      LIM = 0
      CLEAR = .TRUE.
      NOHIRS = .FALSE.
      NSPOTS = 0

C
C         LOOP THROUGH THE LINE BOUNDARIES
C
          DO 20, J = LINE, LINE+1
C
C         LOOP THROUGH THE SPOT BOUNDARIES.
C
           DO 10, I=SPOT,SPOT+1
                ILNDSEA = 1
                FOVSCR(I,J) = 0
C         IF THE SPOT LATITUDE IS MISSING, SKIP THIS SPOT
C
                IF (ISR4SPV(HIRANC(I,J,1))) GOTO 10
                NSPOTS = NSPOTS + 1   
                IF(HIRANC(I,J,9).NE.0)ILNDSEA = 2
                BADSPT(I,J) = 1
                LSEA = .FALSE.
                IF (HIRANC(I,J,9) .EQ. 0.) THEN
                   LSEA = .TRUE.
                   NSEA = NSEA + 1
                ENDIF
C
C HIRS FOV IS NOT A COAST, ADD 10 TO FOVSCR.
C
                IF (HIRANC(I,J,9) .EQ. 2) NCOAST = NCOAST + 1
C
C               CHECK THE LAST BIT OF SECOND WORD OF CLOUD TEST RESULTS
C               EQUAL TO ZERO (DENOTING CLEAR).
C
                RTEMP = HIRANC(I,J,14)
                IF ( .NOT. BTEST(ITEMP,0) ) THEN
C
C                  CHECK THE CHANNEL AVAILABILITY AGAINST
C                  THE CLEAR CHANNEL MASK.
C
                   RTEMP = HIRANC(I,J,7)
                   CHRSLT = IAND(ITEMP,CHNCMB(1,1,ILNDSEA))
C
                   IF(CHRSLT.NE.CHNCMB(1,1,ILNDSEA)) THEN
                      NOHCTR = NOHCTR + 1
                      BADSPT(I,J) = -1
                   ELSE
C
C HIRS FOV HAS PASSED CLOUD DETECTION TESTS AND ALL CHANNELS IN THE CLEAR
C CHANNEL COMBINATION ARE PRESENT, IT IS FIT TO MAKE A CLEAR SOUNDING, ADD
C 10000 TO FOVSCR.
C
                      FOVSCR(I,J) = FOVSCR(I,J) + 10000
                      IF(LSEA)NCLSEA = NCLSEA + 1
                      IF(.NOT.LSEA)NCLNSE = NCLNSE + 1
                   ENDIF
                ELSE
                   BADSPT(I,J) = -1
                END IF
C
   10      CONTINUE
 20        CONTINUE
C
C     CHECK TO SEE IF NO CLEAR SPOTS WERE FOUND.
C
      IF (NSPOTS.GT.0) THEN
         PROPO1 = (FLOAT(NSEA)) / REAL(NSPOTS)
      END IF
C
C     CHECK TO SEE IF NO-HIRS SPOTS WERE FOUND, OR, ALL OF THE SPOTS ARE BAD.
C
      IF(NOHCTR.EQ.NSPOTS) NOHIRS = .TRUE.
C
C USE ABOVE PROPORTIONS TO DETERMINE WHICH CASE TYPE OF RETRIEVAL IS TO BE 
C GIVEN PREFERENCE FOR THIS BOX, THEN SCORE EACH FOV FOR SEA TERRAIN AGREEMENT 
C (1000) AND HIGH QUALITY CLEARNESS (100), AS WELL AS HIRS 8 TEMPERATURE (0-4).
C
      IF (PROPO1 .GE. PCTSEA) THEN
         IF (NCLSEA .GE. NUMCLEAR) THEN
            ICASE = 1
         ELSE 
            CLEAR = .FALSE.
            ICASE = 2
         ENDIF 
      ELSE
         IF (NCLNSE .GE. NUMCLEAR) THEN
           ICASE = 3
         ELSE 
           CLEAR = .FALSE.
           ICASE = 4
         ENDIF 
      ENDIF
      DO 30 J = LINE, LINE+1
         DO 30 I = SPOT, SPOT+1
            IF (ICASE .EQ. 1 .AND. HIRANC(I,J,9) .EQ. 0) THEN
               IF(BADSPT(I,J).EQ.1)FOVSCR(I,J) = FOVSCR(I,J) + 100
            ENDIF
            IF (ICASE .LE. 2 .AND. HIRANC(I,J,9) .EQ. 0) THEN
               FOVSCR(I,J) = FOVSCR(I,J) + 1000
            ENDIF
            IF (ICASE .EQ. 3 .AND. HIRANC(I,J,9) .NE. 0) THEN
               IF(BADSPT(I,J).EQ.1)FOVSCR(I,J) = FOVSCR(I,J) + 100
            ENDIF
            DO 32 L = LINE, LINE + 1
               DO 32 K = SPOT, SPOT + 1
                IF (ICASE.LE.2)THEN
                  IF (HIRDAT(I,J,22).GT.0. .AND. HIRDAT(I,J,22).LE.
     +               HIRDAT(K,L,22)) FOVSCR(I,J) = FOVSCR(I,J) + 1
                ELSE
                  IF (HIRDAT(I,J,22).GT.0. .AND. HIRDAT(I,J,22).GE.
     +               HIRDAT(K,L,22)) FOVSCR(I,J) = FOVSCR(I,J) + 1
                ENDIF
 32         CONTINUE
C
C 10100 IS THE MINIMUM SCORE FOR AN ACCEPATABLE CLEAR SPOT; THE SCORE DESIG-
C NATES A FOV WHOSE CHANNEL COMBINATION HAS ALL THE CLEAR CHANNELS AND WHOSE
C BRIGHTNESS TEMPERATURES PASSED ALL THE CLOUD DETECTION TESTS, AND WHICH
C RESIDES IN A BOX FOR WHICH THERE IS A CERTAIN NUMBER OF CLEARS (NUMCLEAR)       
C OF ONE TYPE (SEA OR NON-SEA), DESIGNATE AS A 'HIGH QUALITY' CLEAR SPOT.
C IF THE LATITUDE FOR A SPOT IS MISSING, SET THE FOV SCORE TO ZERO.  IF THE
C SPOT IS TO BE A SEA CLEAR, IT MUST BE IN A SEA BOX, AND THIS BOX MUST HAVE A
C CERTAIN PERCENTAGE OF SEA SPOTS (PCTSEA) TO BE A SEA BOX.  IF THE BOX IS NOT
C A SEA BOX, IT IS BY DEFAULT A NON-SEA BOX (CONTAINING AT LEAST [1-PCTCLR]%
C NON-SEA [LAND,ICE,SNOW, OR COAST] FOVS).  
C 
            IF (ISR4SPV(HIRANC(I,J,1))) FOVSCR(I,J) = 0
            IF(FOVSCR(I,J).GE.10100)THEN
               IF(FOVSCR(I,J).GT.HOLD)THEN
                  ACCSPT=I
                  ACCLNE=J
                  HOLD=FOVSCR(I,J)
               ENDIF
            ENDIF
 30   CONTINUE
C
C AN ACCEPTABLE CLEAR FOV WAS NOT FOUND, SO LOOK FOR AN ACCEPTABLE CLOUDY SPOT.
C
      IF(ACCSPT.EQ.I4SPV)THEN    
         ITERRR=22
      ELSE
         ITERRR=HIRANC(ACCSPT,ACCLNE,9)
      ENDIF
      IF(.NOT.CLEAR)THEN
C                      
C
C         LOOP THROUGH THE LINE BOUNDARIES
C
          DO 21, J = LINE, LINE+1
C
C         LOOP THROUGH THE SPOT BOUNDARIES.
C
           DO 11, I=SPOT,SPOT+1
                ILNDSEA = 1
                FOVSCR(I,J) = 0                 
                IF(HIRANC(I,J,9).NE.0)ILNDSEA = 2
                BADSPT(I,J) = 1
C
C                  CHECK THE CHANNEL AVAILABILITY AGAINST
C                  THE CLOUDY CHANNEL MASK.
C
                   RTEMP = HIRANC(I,J,7)
                   CHRSLT = IAND(ITEMP,CHNCMB(1,2,ILNDSEA))
                   IF(CHRSLT.NE.CHNCMB(1,2,ILNDSEA)) GO TO 11
C
C SPOT HAS THE MINIMUM CHANNEL CONFIGURATION TO PERFORM A CLOUDY
C RETRIEVAL, ADD 10000 TO THE FOV SCORE.
C
                   FOVSCR(I,J) = FOVSCR(I,J) + 10000
            IF (ICASE .EQ. 2 .AND. HIRANC(I,J,9) .EQ. 0) THEN
               IF(BADSPT(I,J).EQ.1)FOVSCR(I,J) = FOVSCR(I,J) + 100
            ENDIF
            IF (ICASE .LE. 2 .AND. HIRANC(I,J,9) .EQ. 0) THEN
               FOVSCR(I,J) = FOVSCR(I,J) + 1000
            ENDIF
            IF (ICASE .EQ. 4 .AND. HIRANC(I,J,9) .NE. 0) THEN
               IF(BADSPT(I,J).EQ.1)FOVSCR(I,J) = FOVSCR(I,J) + 100
            ENDIF
            DO 33 L = LINE, LINE + 1
               DO 33 K = SPOT, SPOT + 1
                IF (ICASE.LE.2)THEN
                  IF (HIRDAT(I,J,22).GT.0. .AND. HIRDAT(I,J,22).LE.
     +               HIRDAT(K,L,22)) FOVSCR(I,J) = FOVSCR(I,J) + 1
                ELSE
                  IF (HIRDAT(I,J,22).GT.0. .AND. HIRDAT(I,J,22).GE.
     +               HIRDAT(K,L,22)) FOVSCR(I,J) = FOVSCR(I,J) + 1
                ENDIF
 33         CONTINUE     
                   IF (ISR4SPV(HIRANC(I,J,1))) FOVSCR(I,J) = 0
                   IF(FOVSCR(I,J).GT.LIM)THEN
                      LIM=FOVSCR(I,J)
                      IF(LIM.GT.10000)THEN
                         CLEAR=.FALSE.
                         ACCSPT=I
                         ACCLNE=J
                      ENDIF
                   ENDIF
C
   11      CONTINUE
 21       CONTINUE
       ENDIF
      RETURN
      END
