      SUBROUTINE GACSEL(HIRDAT,LINENO,SPTIME,FEOF,STATUS)
      IMPLICIT NONE
C***********************************************************************
C
C   NAME-GACSEL
C
C   LANGUAGE-FORTRAN 77    TYPE-SUBROUTINE    COMPUTER-CRAY   
C
C   VERSION-1.0      DATE- 05/09/91    PROGRAMMER- TERESA SIMMS (SMSRC)
C   VERSION-2.0      DATE- 07/18/95    PROGRAMMER- DIANE BEITZELL (SMSRC)
C      ATOVS-145 - CHANGED FOR ATOVS TO INPUT A 1B* FILE.
C   VERSION-2.1      DATE- 02/26/96    PROGRAMMER- DAVE MOREL (SMSRC)
C      ATOVS-335 - CHANGED RNGITT FROM I*2 TO R*4. ALSO BTNLN NO LONGER
C               GETS CALLED FOR CHANNEL 3B.
C   VERSION-3.0      DATE- 05/13/96    PROGRAMMER- ALEX POZDNYAK (FDC)  
C     FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C      INTEGER AND ALL R*4 DECLARATIONS TO REAL.
C   VERSION-3.1      DATE- 07/06/96    PROGRAMMER- TIM GARDNER (HSTX)
C      ADDED THE CENTER POSITION POINTER TO THE SAVE STATEMENT.
C   VERSION-3.2      DATE- 06/12/98    PROGRAMMER- LEAH WILSON (RSTX)
C      CORRECT RNGABF - SHOULD BE 0=3B, 1=3A.  ORIGINAL CODE WAS VICE-
C      VERSA.
C   VERSION- 3.3     DATE- 02/05/99    PROGRAMMER- LEAH WILSON, RAYTHEON
C      REMOVE GACFOV AND GACTM AND REPLACE BY IN-LINING.  THIS ALSO
C      RENDERS OBSOLETE THE FUNCTIONS HIRFOV AND HIRSTM.
C   VERSION- 3.4     DATE- 04/25/01    PROGRAMMER- M.P. FERGUSON, RITSS 
C      REMOVE THE NON-LINEARITY CORRECTION FOR AVHRR CHANNELS 4 AND 5   
C      BY OMITTING CALL TO BTNLN, AS THIS IS ALREADY BEING DONE IN THE  
C      PRE-PROCESSING.                                                  
C   VERSION- 1,2     DATE- 02/08/02    PROGRAMMER- RICKEY ROLLINS, CSC
C      PORTED TO IBM SP - CHANGED RNGABF FROM I*1 to I*4
C
C   FUNCTION- THIS ROUTINE IS CALLED ONCE PER HIRS SCAN LINE.  IT
C      CALLS INPCTL TO READ IN GAC SCAN LINES AND TO SELECT THE FIVE
C      THAT COVER THE HIRS SPOT.  SEL17 IS CALLED TO PULL OUT THE COUNT
C      VALUES FOR THE 17 COLLOCATED GAC SPOTS FOR EACH HIRS SPOT ON A
C      SCAN LINE.  THE COUNT VALUES ARE CONVERTED TO RADIANCES AND TO
C      BRIGHTNESS TEMPERATURES IN CALLED ROUTINES CALSCL AND RAD2BT, AND
C      A NONLINEARITY CORRECTION IS THEN PERFORMED BY ROUTINE BTNLN.
C      THE FINAL VALUES ARE WRITTEN TO THE HIRDAT BUFFER BY CALLED
C      ROUTINE FLLBUF.
C
C   CALLING SEQUENCE PARAMETERS-
C
C    NAME              TYPE  I/O  DESCRIPTION
C    ----              ----  ---  -----------
C    HIRDAT            REAL  I/O  HIRS EXTENDED DATA BUFFER.
C
C    LINENO             INT   I   HIRS SCAN LINE NUMBER IN BUFFER.
C
C    SPTIME             INT   I   TIME OF 1ST HIRS SPOT IN SCAN LINE
C                                 (CENTURY DAY, MILLISECOND OF DAY).
C
C    FEOF               LOG   O   FLAG SET IF END-OF-FILE FOUND WHILE
C                                 READING IN GAC SCAN LINES.
C
C    STATUS             INT   O   ERROR RETURN CODE.
C                                 = 0 IF NO ERROR OR END OF FILE FOUND,
C                                 = 99 IF ERROR OCCURRED IN BTNLN.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'COLLOC'
      INCLUDE 'SPVAL'
      INCLUDE 'INSPARM'
      INCLUDE 'INPARM'
      INCLUDE 'OVRLAP'
      INCLUDE 'AVRCON'
C
C   LOCAL PARAMETERS-
C
C    NAME       TYPE    DESCRIPTION
C    ----       ----    -----------
C    ARRAY      INT     ARRAY HOLDING THE FIVE GAC SCAN LINE TIMES THAT
C                       CORRESPOND TO THE HIRS SPOT.
C    BRTEMP     REAL    THE BRIGHTNESS TEMPERATURES FOR CHANNELS 3B,4,5.
C    CALTIM     INT     CALCULATED GAC SCAN LINE TIME.
C    CHN        INT     COUNTER FOR GAC CHANNEL NUMBER.
C    GACSPT     INT     GAC SPOT NUMBER COLLOCATED TO THE HIRS SPOT.
C    GACSCN     INT     GAC SCAN LINE NUMBER WHERE GACSPT IS CONTAINED.
C    GTIME1     REAL    GAC SPOT-TO-SPOT TIME IN MILLISECONDS.
C    HIRSPT     INT     DO LOOP INDEX FOR EACH HIRS SPOT IN A LINE.
C    I,K,N      INT     DO LOOP INDEXES.
C    ITT        REAL    INTERNAL TARGET TEMPERATURES FOR GAC CHANNELS
C                       3B,4, AND 5.
C    LINBAD     INT     A VECTOR WHOSE VALUE IS 0 IF THE CORRESPONDING
C                       ARRAY LINE IS GOOD OR THE LINE NUMBER IF THE
C                       LINE IS BAD.
C    MISSIN     LOG     FLAG SWITCHED TO FALSE IF DATA FOUND IN ARRAY.
C    PNTER      INT     POINTER TO THE CENTER POSITION(3) OF ARRAY.
C    RAD17      REAL    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    RNGABF     INT     ONE DIMENSIONAL RING BUFFER THAT INDICATES
C                       WHETHER CHANNEL 3A OR CHANNEL 3B IS BEING USED.
C                       (1=3A, 0=3B, -1=UNKNOWN)
C    RNGBUF     REAL    THREE DIMENSIONAL RING BUFFER THAT HOLDS
C                       NUMCHN CHANNEL RADIENCES FOR NUMSPT SPOTS
C                       ON RNGSIZ GAC SCAN LINES.
C    RNGGST     INT     TWO DIMENSIONAL RING BUFFER THAT HOLDS THE
C                       CENTURY DAY/MILLISECOND START TIME OF THE
C                       RNGSIZ GAC SCAN LINES IN RNGBUF .
C    RNGITT     REAL    TWO DIMENSIONAL RING BUFFER OF 3 INTERNAL TARGET
C                       TEMPERATURES FOR RNGSIZ GAC SCAN LINES IN RNGBUF.
C    RNGPTR     INT     POINTER THAT KEEPS TRACK OF POSITION OF THE
C                       DESIRED SCAN LINES IN THE RING BUFFER.
C    GSC2SC     INT     GAC SCAN TO SCAN TIME IN MILLISECONDS.
C
C   SUBPROGRAMS CALLED - INPCTL, SEL17, RAD2BT, BTNLN, FLLBUF
C
C   FUNCTIONS USED- NINT, REAL
C
C   EXIT STATES - ROUTINE WILL RETURN WITH 
C                      STATUS = 0  - IF PROGRAM RAN CORRECTLY OR
C                                    END OF FILE CONDITION FOUND.
C                      STATUS = 99 - IF ENCOUNTERS ERROR IN BTNLN.
C
C***********************************************************************
C
      INTEGER   NUMCHN,NUMSPT,RNGSIZ
C
      PARAMETER (NUMCHN=5,NUMSPT=409,RNGSIZ=14)
C
C     INTEGER*1 RNGABF(RNGSIZ)
      INTEGER   RNGABF(RNGSIZ)
      INTEGER   LINENO, SPTIME(2), RNGGST(2,RNGSIZ),
     1          I,K,N, RNGPTR, PNTER, HIRSPT, CHN, LINBAD(5),
     2          ARRAY(5), CALTIM, GACSCN, GACSPT, GSC2SC, STATUS
      INTEGER   HIRSCN, III, IIII
C
      REAL      HIRDAT(HNSPOT,HTOTLN,HDATEL),BRTEMP(3*17),
     1          RNGBUF(NUMCHN,NUMSPT,RNGSIZ), RAD17(6*17),
     2          GTIME1,ITT(3),RNGITT(3,RNGSIZ)
      REAL      TIME, GTIME(4), HIRNAD
C
      LOGICAL   FEOF, MISSIN, AVAUSE, FIRST2
C
      SAVE RNGBUF,RNGGST,RNGITT,RNGABF,RNGPTR
      SAVE ARRAY,LINBAD,PNTER
C
      DATA RNGPTR /0/, PNTER/3/, (LINBAD(I),I=1,5)/0,0,0,0,0/
      DATA (ARRAY(I),I=1,5)/Z'80000000',Z'80000000',Z'80000000',
     1                      Z'80000000',Z'80000000'/
      DATA FIRST2 /.TRUE./
C
C     INITIALIZE VARIABLES. 
C
      MISSIN = .TRUE.
C
      IF(FEOF) RETURN
      GSC2SC=GACTIM(3)/1000
      GTIME1=REAL(GACTIM(2))/1000
C
C  LOOP OVER ALL HIRS SPOTS.
C
      DO 130 HIRSPT = 1, HNSPOT
C
C  INITIALIZE LOCAL ARRAY.
C
      DO 10 I=1,51
         BRTEMP(I)=0.0
   10 CONTINUE
C
C  CALCULATE GAC SPOT NUMBER.
C
      HIRNAD = HIRANG(1) + (HIRANG(2) * (HIRSPT - 1)) + HIRANG(3)
      GACSPT=1 + NINT((HIRNAD - GACANG(1) - GACANG(3)) / GACANG(2))
      HIRSCN=LINENO-HTOTBF/2
C
      TIME= HIRTIM(2) * (HIRSPT - 1) + HIRTIM(3) * (HIRSCN - 1)
     1        + HIRTIM(4)
C
       DO IIII = 1,4
          GTIME(IIII) = REAL(GACTIM(IIII)/1000)
       END DO   
C
       GACSCN = 1 + INT((TIME - GTIME(1) - GTIME(2) * (GACSPT - 1)
     1                                      - GTIME(4)) / GTIME(3))
C
C
C  COMPUTE START TIME OF GAC SCAN LINE THAT IS CLOSEST TO THE CENTER
C  OF THE HIRS SPOT.
C
      CALTIM=SPTIME(2)-NINT(GACSPT*GTIME1)
C
C  COMPUTE TIME OF NEXT HIRS SPOT.
C
      SPTIME(2) = SPTIME(2) + HIRTIM(2)
C
C  CHECK IF CENTER GAC LINE HAS ALREADY BEEN READ IN.
C
      IF(LINBAD(PNTER) .EQ. PNTER) THEN
         IF(LINBAD(PNTER+1) .EQ. (PNTER+1)) THEN
            IF((CALTIM+(2*GSC2SC)) .GT. (ARRAY(PNTER+2)-250) .AND.
     1        (CALTIM+(2*GSC2SC)) .LT. (ARRAY(PNTER+2)+250)) GOTO 100
         ELSE
            IF((CALTIM+GSC2SC) .GT. (ARRAY(PNTER+1)-250) .AND.
     1         (CALTIM+GSC2SC) .LT. (ARRAY(PNTER+1)+250)) GOTO 100
         ENDIF
      ENDIF
C
      IF(CALTIM .GT. (ARRAY(PNTER)-250)  .AND. CALTIM .LT.
     1           (ARRAY(PNTER)+250)) GOTO 100
C
C  CALL THE INPUT CONTROL ROUTINE TO FILL THE RING BUFFERS
C     AND ARRAY, CONTAINING THE GAC LINE TIMES.
C
      IF(FIRST2) THEN
         DO 79 III = 1,50
            AVAUSN(III) = .TRUE.
 79      CONTINUE
         print *, 'avausn initialized in gacsel'
      ENDIF           
      CALL INPCTL(ARRAY,RNGBUF,RNGGST,RNGITT,RNGABF,RNGPTR,
     1            SPTIME,CALTIM,LINBAD,FEOF,AVAUSN(LINENO),
     2            FIRST2)
C
      IF(FEOF) RETURN
C
C  IF ALL 5 SCAN LINES IN ARRAY ARE MISSING GO TO NEXT HIRS SPOT.
      DO 80 N=1,5
           IF (ARRAY(N) .NE. I4SPV) THEN
               MISSIN = .FALSE.
               GOTO 100
           ENDIF
   80  CONTINUE
   90  IF (MISSIN) GOTO 130
C
C  PULL OUT RADIANCES FOR THE 17 COLLOCATED GAC SPOTS.
C
  100    CALL SEL17(RNGBUF,RNGITT,RNGABF,RNGPTR,GACSPT,LINBAD,
     1              RAD17,ITT)
C
C  CONVERT RADIANCE TO BRIGHTNESS TEMPERATURE FOR CHANNELS 3B, 4 & 5.
C     (CDB VALUES ARE STORED IN CHANNEL POSITIONS 4, 5 & 6.)
C  PERFORM NONLINEARITY CORRECTION FOR BRIGHTNESS TEMPERATURES ON
C  CHANNELS 4 & 5.
C
         CHN=3
         DO 120 K=52,86,17
            CALL RAD2BT(4,CHN+1,BRTEMP(K-51),RAD17(K),17)
CCMPF       IF(CHN .GT. 3) CALL BTNLN(4,CHN,BRTEMP(K-51),17,
CCMPF1                   ITT(CHN-2),STATUS)                 
CCMPF       IF (STATUS .EQ. 99) GOTO 140                    
            CHN=CHN+1
  120   CONTINUE
C
C  WRITE COMPUTED VALUES FOR ALL CHANNELS TO HIRDAT.
C
        CALL FLLBUF(HIRDAT,HNSPOT,HTOTLN,HDATEL,HDATOV,HTOTBF,
     1              LINENO,HIRSPT,BRTEMP,RAD17)
  130 CONTINUE
C
  140 RETURN
      END
