      SUBROUTINE SEL17(RNGBUF,RNGITT,RNGABF,RNGPTR,SPOT,LINBAD,
     1                 RAD17,ITT)
      IMPLICIT NONE
C***********************************************************************
C
C   NAME- SEL17
C
C   LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE      COMPUTER- CRAY     
C
C   VERSION- 1.0    DATE- 06/14/91   PROGRAMMER- TERESA SIMMS (SMSRC)
C   VERSION- 2.0    DATE- 07/26/95   PROGRAMMER- DIANE BEITZELL (SMSRC)
C      ATOVS-147 - CHANGED FOR ATOVS TO INPUT A 1B* FILE.
C   VERSION- 2.1    DATE- 02/26/96   PROGRAMMER- DAVE MOREL (SMSRC)
C      ATOVS-338 - CHANGED RNGITT FROM I*2 TO R*4 AND DELETED THE
C                INTRINSIC FLOAT.
C   VERSION- 3.0    DATE- 05/14/96   PROGRAMMER- ALEX POZDNYAK (FDC)  
C      MODIFIED TO RUN ON THE CRAY.  REPLACED I*2/4 WITH INT AND
C      R*4 WITH REAL.
C   VERSION- 3.1    DATE- 06/12/98   PROGRAMMER- LEAH WILSON (RSTX)
C      CORRECTED 3A/3B FLAG TO BE 0=3B, 1=3A.  ORIGINAL CODE WAS VICE-
C      VERSA.
C   VERSION- 3.2    DATE- 08/07/03   PROGRAMMER- M.P.FERGUSON (RITSS)
C      INCLUDED COMMON 'HIRUSE' TO PICK UP THE VALUE OF 'HIRTYP',
C      WHICH INDICATES WHETHER THE HIRS/3 OR HIRS/4 INSTRUMENT IS
C      BEING USED. FOR THE HIRS/3 SEL17 SELECTS 17 GAC SPOTS. FOR
C      THE HIRS/4 SEL17 SELECTS 9 SPOTS, SINCE THE HIRS/4 FOV SIZE
C      IS SMALLER.
C 
C   FUNCTION- THIS SUBROUTINE PULLS OUT GAC RADIANCES FOR THE CHOSEN
C      17 GAC SPOTS FROM RNGBUF FOR FIVE CHANNELS TO FILL UP A VECTOR,
C      RAD17, OF LENGTH 102, WRITING 3A AND 3B AS SEPARATE CHANNELS.
C      IT ALSO OBTAINS THE MOST RECENT SET OF INTERNAL TARGET TEMPERATURES.
C      FOR THE HIRS/4 INSTRUMENT THIS SUBROUTINE SELECTS 9 GAC SPOTS.
C 
C   CALLING SEQUENCE PARAMETERS- 
C
C   VARIABLE   TYPE    I/O  DESCRIPTION 
C   --------   ----    ---  ----------- 
C   RNGBUF     REAL     I   THREE DIMENSIONAL RING BUFFER THAT HOLDS 
C                           NUMCHN CHANNEL RADIENCES FOR NUMSPT SPOTS
C                           ON BUFSIZ GAC SCAN LINES.
C
C   RNGITT      INT     I   TWO DIMENSIONAL RING BUFFER OF 3 INTERNAL TARGET
C                           TEMPERATURES FOR BUFSIZ GAC SCAN LINES IN RNGBUF.
C
C   RNGABF      INT     I   ONE DIMENSIONAL RING BUFFER THAT INDICATES
C                           WHETHER CHANNEL 3A OR CHANNEL 3B IS BEING USED.
C                           (0=3B, 1=3A, -1=UNKNOWN)
C
C   RNGPTR      INT     I   POINTER TO THE SCAN LINE IN THE
C                           RNGBUF THAT CONTAINS THE CENTER SPOT.
C
C   SPOT        INT     I   NUMBER OF CENTER GAC SPOT.
C
C   LINBAD      INT     I   A VECTOR WHOSE VALUES ARE ZERO IF THE
C                           CORRESPONDING ARRAY LINE IS GOOD OR BY
C                           THE CORRESPONDING LINE NUMBER (1-5) IF
C                           THE ARRAY LINE IS BAD.
C
C   RAD17      REAL     O   VECTOR OF RADIANCES FOR THE 17 GAC SPOTS.
C                           CHANNEL 1  DATA IS STORED IN THE  1ST 17 PLACES.
C                           CHANNEL 2  DATA IS STORED IN THE  2ND 17 PLACES.
C                           CHANNEL 3A DATA IS STORED IN THE  3RD 17 PLACES.
C                           CHANNEL 3B DATA IS STORED IN THE  4TH 17 PLACES.
C                           CHANNEL 4  DATA IS STORED IN THE  5TH 17 PLACES.
C                           CHANNEL 5  DATA IS STORED IN THE LAST 17 PLACES.
C
C   ITT        REAL     O   THE INTERNAL TARGET TEMPERATURES FOR THE
C                           LAST GOOD GAC SCAN LINE.  IT WILL BE ASSUMED
C                           THAT THE TARGET TEMPERATURES WILL NOT VARY
C                           MUCH FROM SCAN LINE TO SCAN LINE.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'SPVAL'
      INCLUDE 'HIRUSE'
C
C   LOCAL PARAMETERS-
C
C   NAME            TYPE    DESCRIPTION
C   ----            ----    -----------
C   I,J,K            INT    LOOP CONTROL COUNTERS.
C   ISPOT            INT    NUMBER OF CENTRAL GAC SPOT MINUS 3.
C                           THIS NUMBER MAKES IT EASIER TO FILL IN
C                           THE CORRECT 17 GAC SPOTS.
C   ISTART           INT    POINTER TO THE CURRENT GAC SCAN LINE
C                           BEING PROCESSED IN THE RING BUFFERS.
C   NXTNUM           INT    NUMBER OF GAC TEMPLATE SPOT WHOSE DATA
C                           NEEDS TO BE PULLED OUT.
C   SPTNUM           INT    NUMBER OF GAC TEMPLATE SPOT WHOSE DATA
C                           NEEDS TO BE PULLED OUT.
C
C   EXIT STATES- NONE
C
C   COMMENTS- THE GAC SPOTS SELECTED BY THIS ROUTINE TO CORRESPOND
C             TO ONE HIRS SPOT ARE FOUND ON FIVE SUCCESSIVE SCAN LINES
C             IN THE FOLLOWING PATTERN:
C
C              11   X   12   X   13                 (1)
C               X   3    4   5    X                 (2)
C              10   2    1   6   14                 (3)
C               X   9    8   7    X                 (4)
C              17   X   16   X   15                 (5)
C
C   X REPRESENTS A GAC SPOT THAT IS NOT USED, AND THE NUMBER TO THE
C   RIGHT INDICATES THE SCAN LINE NUMBER ASSIGNED FOR THE PURPOSES
C   OF THIS ROUTINE.   THIS ROUTINE ALSO TAKES INTO ACCOUNT THAT
C   THE AVHRR SCANS IN THE OPPOSITE DIRECTION TO THE TOVS INSTRUMENTS,
C   FROM RIGHT TO LEFT ALONG THE SATELLITE TRACK.
C
C**********************************************************************
C
      INTEGER     NUMCHN,NUMSPT,BUFSIZ
      PARAMETER   (NUMCHN=5,NUMSPT=409,BUFSIZ=14)
      INTEGER     RNGABF(BUFSIZ), MM
      REAL        RNGITT(3,BUFSIZ)
C
      INTEGER     I,J,K, RNGPTR, SPOT, ISTART
      INTEGER     ISPOT, NXTNUM, SPTNUM, LINBAD(5)
      REAL        RAD17(6*17),RNGBUF(NUMCHN,NUMSPT,BUFSIZ),ITT(3)
C
C     INITIALIZE RAD17 AND ITT TO MISSING VALUES.
C
      DO 10 I=1,6*17
         RAD17(I) = R4SPV
         IF (I .LE. 3) ITT(I) = R4SPV
   10 CONTINUE
C
C     POINT TO FIRST COLUMN OF SPOTS IN THE 17 SPOT GRID,
C     WHERE SPOT POINTS TO THE CENTER SPOT IN GRID.
C
      ISPOT=SPOT-3
C
C     START PROCESSING WITH THE TWO GAC SCAN LINES BEFORE THE CENTRAL ONE
C     UNLESS THERE IS A GAP IN THE SCAN LINE TIMES. REMEMBER THE RNGBUF
C     HAS GOOD SCAN LINES ONLY-LINBAD SHOWS IF THERE ARE ANY GAPS.
C
      ISTART = RNGPTR - 2
      IF (ISTART .EQ. 0) ISTART = 14
      IF (ISTART .LT. 0) ISTART = 13
C
C     LOOP OVER 5 SCAN LINES THAT CONTAIN THE NEEDED GAC DATA.
C
      DO 160 I=1,5
        IF (I .EQ. LINBAD(I)) THEN
           ISTART = ISTART + 1
           IF(ISTART .EQ. 15) ISTART = 1
           GOTO 160
        ENDIF
C
C       STORE INTERNAL TARGET TEMPERATURES.
C
        DO 20 K = 1,3
           ITT(K) = RNGITT(K,ISTART)
   20   CONTINUE
C
C       FOR FIRST AND LAST SCAN LINES OF ARRAY PULL OUT EVERY OTHER SPOT.
C
         IF (I.EQ.1 .OR. I.EQ.5) THEN
          IF(HIRTYP.EQ.3)THEN
            SPTNUM=13
            NXTNUM=15
            DO 70 J=1,5,2
              IF (I .EQ. 1) THEN
C
C               STUFF BUFFER WITH RADIANCES FOR SPOTS 13,12, AND 11.
C
                DO 30 K=1,3
                   IF(K.LT.3 .OR. RNGABF(ISTART).EQ.1)
     1             RAD17(SPTNUM+(K-1)*17) = RNGBUF(K,ISPOT+J,ISTART)
   30           CONTINUE
                DO 40 K=3,5
                   IF(K.GT.3 .OR. RNGABF(ISTART).EQ.0)
     1             RAD17(SPTNUM+(K)*17) = RNGBUF(K,ISPOT+J,ISTART)
   40           CONTINUE
                SPTNUM=SPTNUM-1
              ELSE
C
C               STUFF BUFFER WITH RADIANCES FOR SPOTS 15,16, AND 17.
C
                DO 50 K=1,3
                   IF(K.LT.3 .OR. RNGABF(ISTART).EQ.1)
     1             RAD17(NXTNUM+(K-1)*17) = RNGBUF(K,ISPOT+J,ISTART)
   50           CONTINUE
                DO 60 K=3,5
                   IF(K.GT.3 .OR. RNGABF(ISTART).EQ.0)
     1             RAD17(NXTNUM+(K)*17) = RNGBUF(K,ISPOT+J,ISTART)
   60           CONTINUE
                NXTNUM=NXTNUM+1
              ENDIF
   70       CONTINUE
C
C           FOR 2ND AND 4TH LINES OF ARRAY PULL OUT 2ND THROUGH 4TH SPOTS.
C
          ENDIF
         ELSEIF (I.EQ.2 .OR. I.EQ.4) THEN
            SPTNUM=5
            NXTNUM=7
            DO 120 J=2,4
              IF (I .EQ. 2) THEN
C
C               STUFF BUFFER WITH RADIANCES FOR SPOTS 5,4, AND 3.
C
                DO 80 K=1,3
                   IF(K.LT.3 .OR. RNGABF(ISTART).EQ.1)
     1             RAD17(SPTNUM+(K-1)*17) = RNGBUF(K,ISPOT+J,ISTART)
   80           CONTINUE
                DO 90 K=3,5
                   IF(K.GT.3 .OR. RNGABF(ISTART).EQ.0)
     1             RAD17(SPTNUM+(K)*17) = RNGBUF(K,ISPOT+J,ISTART)
   90           CONTINUE
                SPTNUM=SPTNUM-1
              ELSE
C
C               STUFF BUFFER WITH RADIANCES FOR SPOTS 7,8, AND 9.
C
                DO 100 K=1,3
                   IF(K.LT.3 .OR. RNGABF(ISTART).EQ.1)
     1             RAD17(NXTNUM+(K-1)*17) = RNGBUF(K,ISPOT+J,ISTART)
  100           CONTINUE
                DO 110 K=3,5
                   IF(K.GT.3 .OR. RNGABF(ISTART).EQ.0)
     1             RAD17(NXTNUM+(K)*17) = RNGBUF(K,ISPOT+J,ISTART)
  110           CONTINUE
                NXTNUM=NXTNUM+1
              ENDIF
  120       CONTINUE
         ELSE
C
C           FOR CENTER LINE OF ARRAY PULL OUT ALL 5 SPOTS.
C
            DO 150 J=1,5
C
C               ASSIGN TEMPLATE SPOT NUMBER TO CORRESPONDING GAC SPOT.
C
                IF(HIRTYP .NE. 3 .AND. J .EQ. 1) GO TO 150
                IF(HIRTYP .NE. 3 .AND. J .EQ. 5) GO TO 150
                IF (J .EQ. 1) SPTNUM=14
                IF (J .EQ. 2) SPTNUM=6
                IF (J .EQ. 3) SPTNUM=1
                IF (J .EQ. 4) SPTNUM=2
                IF (J .EQ. 5) SPTNUM=10
C
C               STUFF BUFFER WITH RADIANCES FOR THE SELECTED SPOT.
C
                DO 130 K=1,3
                   IF(K.LT.3 .OR. RNGABF(ISTART).EQ.1)
     1             RAD17(SPTNUM+(K-1)*17) = RNGBUF(K,ISPOT+J,ISTART)
  130           CONTINUE
                DO 140 K=3,5
                   IF(K.GT.3 .OR. RNGABF(ISTART).EQ.0)
     1             RAD17(SPTNUM+(K)*17) = RNGBUF(K,ISPOT+J,ISTART)
  140           CONTINUE
  150       CONTINUE
         ENDIF
C
C        INCREMENT RING POINTER AND CHECK IF IT NEEDS TO POINT TO
C        BEGINNING OF RING BUFFER.
C
         ISTART = ISTART + 1
         IF(ISTART .GT. 14) ISTART = 1
  160 CONTINUE
C
CCMPF PRINT *, 'FOR GAC SPOT = ',SPOT
CCMPF PRINT *, '17 AVHRR 1 RADS ARE ',(RAD17(MM),MM=1,17)
CCMPF PRINT *, '17 AVHRR 2 RADS ARE ',(RAD17(MM),MM=18,34)
CCMPF PRINT *, '17 AVHRR 3a RADS ARE ',(RAD17(MM),MM=35,51)
CCMPF PRINT *, '17 AVHRR 3b RADS ARE ',(RAD17(MM),MM=52,68)
CCMPF PRINT *, '17 AVHRR 4 RADS ARE ',(RAD17(MM),MM=69,85)
CCMPF PRINT *, '17 AVHRR 5 RADS ARE ',(RAD17(MM),MM=86,102)
      RETURN
      END
