      SUBROUTINE INPCTL(ARRAY,RNGBUF,RNGGST,RNGITT,RNGABF,RNGPTR,
     1                  SPTIME,CALTIM,LINBAD,FEOF,AVAUSE,FIRST2)
      IMPLICIT NONE
C***********************************************************************
C
C   NAME-INPCTL
C
C   LANGUAGE- FORTRAN 77     TYPE-SUBROUTINE      COMPUTER- IBM SP
C
C   VERSION-1.0      DATE- 06/24/91     PROGRAMMER- DAVE MOREL (SMSRC)
C   VERSION-2.0      DATE- 07/20/95     PROGRAMMER- DIANE BEITZELL (SMSRC)
C          ATOVS-146 - CHANGE REQUEST TO READ AVHRR GAC 1B* DATA SET.
C   VERSION 2.1      DATE- 01/30/96     PROGRAMMER- ROB FELDMAN (SMSRC)
C          ATOVS-302  CHANGED SELECT_STATUS_3A_3B TO CHANNEL3_INDICATOR
C   VERSION 2.2      DATE- 02/26/96     PROGRAMMER- DAVE MOREL (SMSRC)
C          ATOVS-336  CHANGED RNGITT FROM I*2 TO R*4, ALSO ADDED IT TO
C                   THE CALL TO AVG1RD.  REMOVED THE VARIABLE TARGET AND
C                   THE EXTRACTION OF INTERNAL TARGET DATA AS IT IS
C                   NOW DONE IN AVG1RD.
C   VERSION-3.0      DATE- 05/14/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 - 03/23/98     PROGRAMMER- LEAH WILSON (RSTX)
C     FOR Y2K, CHANGE YEAR CHECK FROM NOT BETWEEN 0-99 TO NOT .LT.0
C     ALSO ADD /MONSTA/ AND COUNTERS FOR WHY GAC 1B* SCAN LINES ARE NOT
C     USED.
C   VERSION- 3.2    DATE - 06/12/98     PROGRAMMER- LEAH WILSON (RSTX)
C     CORRECT RNGABF - SHOULD BE 0=3B, 1=3A, -1=UNKNOWN. (ORIGINAL CODE
C     HAD 0 AND 1 VICE-VERSA)
C   VERSION- 3.3    DATE-  08/06/98     PROGRAMMER- LEAH WILSON (RSTX)
C     ADD ITEMID TO AVG1RD CALL FOR PROPER ERROR MESSAGING.
C   VERSION- 3.4    DATE-  03/10/00     PROGRAMMER- LEAH WILSON (RAYTHEON)
C     COMMENT OUT READING AND CHECKING ALL 1B* SCAN LINE FLAGS EXCEPT
C     THE DO_NOT_USE FLAG IN ORDER TO SPEED UP PROCESSING, AND TO BE  
C     CONSISTENT WITH THE HIRS AND AMSU-A PROCESSING.
C   VERSION- 4.0    DATE-  01/10/05     PROGRAMMER- F. TILLEY (RAYTHEON)
C     UNDER WR #230 (NOAA-N READINESS) CHANGED THE CALL TO AVSGT1 TO
C     AVDGT1 FOR DIRECT ACCESS TO THE GAC 1B* AND ADDED A SCAN LINE
C     POINTER THAT IS SAVED LOCALLY. CHANGED 'SHAPE' AND 'JSHAPE' TO
C     INTEGER*4. SUBSTITUTED I*4 'LENGTH' FOR THE NUMERICAL DATA
C     LENGTH IN 1B* UTILITY CALLS. CHANGED 'STATUS' TO I*4 'RETCOD'
C     IN 1B* UTILITY CALLS. REPLACED COMMON 'B1ERR' WITH COMMON
C     'MSG1BS' IN INPCT$ AND ADDED A CALL TO B1SERR TO DUMP THE
C     1B* MESSAGE BUFFER TO THE KIDS FILE.
C   VERSION- 4.1    DATE-  03/17/10     PROGRAMMER- FRANK TILLEY (IMSG)
C     MODIFIED CHECK OF SCAN LINE START TIMES TO USE '.GE.' AND '.LE.'
C     INSTEAD OF '.GT.' AND '.LT.' TO AVOID FALSE DETECTION OF DATA
C     GAPS.
C
C   FUNCTION- THIS SUBROUTINE ACTS AS THE INPUT CONTROL OF GAC SCAN
C   LINES FOR THE SUBROUTINE GACSEL. IT MAKES CALLS TO AVSGT1 WHEN A
C   NEW SCAN LINE IS TO BE INPUT.  THE 5 CHANNEL RADIANCES ARE STORED
C   IN A CIRCULAR ARRAY CALLED RNGBUF(RING BUFFER) ALONG WITH CORRESPOND-
C   ING ARRAYS OF TIMES, TEMPS, AND AB FLAGS.  WHEN A GAC LINE CORRESPOND-
C   ING TO THE CENTER OF THE HIRS SPOT IS FOUND, A POINTER IS SET TO THAT
C   LINE IN THE RING BUFFER.  THE TIME OF THAT LINE IS PLACED IN THE
C   CENTER VALUE OF ARRAY, AND OTHER INFORMATION IS PULLED FROM THE
C   SCAN LINE.  THE NEXT TWO GAC SCAN LINES ARE THEN READ
C   INTO THE RING BUFFER.  THE TWO PRECEDING AND SUCCEEDING GAC
C   SCAN LINES ARE CHECKED FOR ANY GAPS IN THE SCAN LINE TIMES.
C   IF A GAP EXISTS, A FLAG IS SET FOR THAT LINE. OTHERWISE THE
C   SCAN LINE TIME AND ALL OTHER NEEDED DATA ARE EXTRACTED.  THE
C   TIMES FOR ALL FIVE GAC SCAN LINES (PROVIDED THEY EXIST) ARE PUT
C   INTO ARRAY.
C
C   CALLING SEQUENCE PARAMETERS-
C
C    NAME        TYPE   I/O  DESCRIPTION
C    ----        ----   ---  -----------
C    ARRAY        INT    O   ARRAY HOLDING THE FIVE GAC SCAN LINE TIMES
C                            THAT CORRESPOND TO THE HIRS SPOT.
C    RNGBUF      REAL    O   THREE DIMENSIONAL RING BUFFER THAT HOLDS
C                            NUMCHN CHANNEL RADIANCES FOR NUMSPT SPOTS
C                            ON BUFSIZ GAC SCAN LINES.
C    RNGGST       INT    O   TWO DIMENSIONAL RING BUFFER THAT HOLDS THE 
C                            CENTURY DAY/MILLISECOND START TIME OF THE
C                            BUFSIZ GAC SCAN LINES IN RNGBUF .
C    RNGITT      REAL    O   TWO DIMENSIONAL RING BUFFER OF 3 INTERNAL RNGITT
C                            TEMPERATURES FOR BUFSIZ GAC SCAN LINES IN RNGBUF.
C    RNGABF       INT    O   ONE DIMENSIONAL RING BUFFER WHICH INDICATES
C                            WHETHER CHANNEL 3A OR CHANNEL 3B IS BEING
C                            USED.  (0=3B, 1=3A, -1=UNKNOWN)
C    RNGPTR       INT    O   POINTER TO THE CENTER GAC LINE CLOSEST IN TIME
C                            TO THE HIRS SPOT IN THE RING BUFFERS.
C    SPTIME       INT    I   START TIME OF THE HIRS SCAN LINE IN CENTURY
C                            DAY/MILLISECOND TIME.
C    CALTIM       INT    I   CALCULATED GAC SCAN LINE START TIME.
C    LINBAD       INT    O   AN ARRAY WHOSE VALUES ARE 0 IF NO GAC LINES
C                            ARE MISSING AND WHOSE VALUES ARE SET TO THE
C                            LINE NUMBER IF THE GAC LINE IS MISSING.
C    FEOF         LOG    O   FLAG TO INDICATE THE END OF FILE WAS FOUND
C                            OR THAT 1B* UTILITIES UNABLE TO READ FILE.
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE 'COLLOC'
      INCLUDE 'INPCDB'
      INCLUDE 'MONSTA'
      INCLUDE 'SPVAL'
C
C   LOCAL PARAMETERS-
C
C    NAME         TYPE    DESCRIPTION
C    ----         ----    -----------
C    BUFSIZ       INT     NUMBER OF GAC SCAN LINES IN RING BUFFERS.
C                         SET TO 14 (# GAC LINES PER ONE HIRS LINE)
C                         ALTHOUGH ONLY 5-6 ARE USED AT ONE TIME,
C                         BECAUSE CURRENT PROCESSING DOES ONE HIRS
C                         SPOT AT A TIME.  IN FUTURE, PROCESSING COULD POSSIBLY
C                         CHANGE TO ONE HIRS LINE AT A TIME AND A BUFSIZ OF 14
C                         LINES WOULD BE NECESSARY.
C    CDAY         INT     CENTURY DAY FOR THE GAC LINE.
C    DAY          INT     DAY OF YEAR(I.E., 1-366) FOR THE GAC LINE.
C    EOF          INT     UPPER LIMIT OF A DO LOOP.  IT CHANGES FROM
C                         1 TO 3 TO CONTROL THE NUMBER OF NEEDED INPUT
C                         GAC LINES.
C    ERRCOD       INT     INTERNAL ERROR CODE.
C    FIRST        LOG     A FLAG SET TO TRUE UNTIL THE RING BUFFER
C                         IS FILLED FOR THE FIRST TIME.
C    FLAG         INT     1B* QUALITY FLAG.
C    FUTURE       INT     POINTS TO THE NEXT GAC SCAN LINE IN RNGBUF
C                         WHICH IS GREATER THAN 250 MILLISECONDS OF CALTIM
C                         INDICATING A GAP WAS DETECTED.
C    GSC2SC       INT     GAC SCAN-TO-SCAN TIME IN MILLISECONDS.
C    I            INT     POINTER TO BEGINNING SCAN LINES IN RNGBUF WHEN
C                         RNGBUF IS BEING FIRST BEING FILLED.
C    INT1VL       I*1     VARIABLE FOR EXTRACTING I*1 DATA FROM THE 1B*
C    INT2VL       I*2     VARIABLE FOR EXTRACTING I*2 DATA FROM THE 1B*
C    INT4VL       I*4     VARIABLE FOR EXTRACTING I*4 DATA FROM THE 1B*
C    ITEMID       CH*32   THE DESIRED DATA ITEM NAME HELD IN THE AVHRR
C                         1B* DATA SET RECORD.
C    J,K,L,M      INT     DO LOOP INDEXES.
C    JDAY         INT     JULIAN DAY (DAY OF YEAR) OF GAC SCAN LINE
C                         EQUIVALENCED TO JDAY4.
C    JDAY4        INT     JULIAN DAY (DAY OF YEAR) OF GAC SCAN LINE
C                         EQUIVALENCED TO JDAY.
C    LENGTH       I*4     DATA LENGTH OF 1B* DATA.
C    MILSEC       INT     TIME OF DAY OF GAC SCAN LINE IN MILLISECONDS.
C    MONTH        INT     MONTH (I.E., 1-12) OF GAC SCAN LINE.
C    NOREAD       LOG     INITIALLY FALSE, INDICATING A GAC RECORD IS
C                         TO BE READ.  ONLY SET TO TRUE WHEN THE MOST RECENT
C                         GOOD GAC LINE READ IN IS FURTHER IN THE FUTURE THAN
C                         THE 5 LINES SURROUNDING THE HIRS SPOT, INDICATING A
C                         GAP WAS FOUND, SO NO READ IS PERFORMED UNTIL THE
C                         HIRS SPOT CATCHES UP IN TIME.
C    ONCE         LOG     SET TRUE INITIALLY TO PROPERLY FILL RNGBUF FOR THE
C                         VERY FIRST TIME. SET FALSE WHEN RNGBUF HAS BEEN
C                         FILLED ONE TIME.
C    PNTER        INT     POINTER TO THE CENTER POSITION(3) OF ARRAY.
C    PT           INT     RANGES FROM 0 TO 2 TO PROPERLY POSITION
C                         PNTER WITHIN ARRAY FOR READING IN NEW GAC LINES.
C    RECCNT       INT     COUNT OF LOGICAL RECORDS(SCAN LINES). ONLY USED
C                         FOR ERROR MESSAGES.
C    RETCOD       I*4     RETURN CODE FROM 1B* UTILITIES.
C    SCNAVH       I*4     AVHRR SCAN NUMBER.
C    SOF          INT     INTEGER FLAG THAT CHANGES VALUE WHEN THE
C                         MATCHING GAC SCAN LINE IS THE START OF THE
C                         RING BUFFER.
C    START        INT     POINTER TO THE NEXT LINE OF THE RING BUFFERS WHICH
C                         WILL BE WRITTEN TO.
C    STATUS       INT     EXIT STATUS FROM SUBROUTINES.
C    SAVETM       INT     CONTAINS THE TIME OF THE LAST RECORD READ.
C    SAVPT        INT     IN THE EVENT OF A GAP THIS VARIABLE ENSURES
C                         RNGPTR POINTS TO THE CORRECT SCAN LINE.
C    SHAPE        I*4     VECTOR OF LENGTH 8 TO  HOLD THE SHAPE OF
C                         THE UTILITY ARRAY. (DIMENSION 0:7).  THE ZERO'TH
C                         ELEMENT IS THE DIMENSION OF THE ARRAY.  THE
C                         NEXT ELEMENT IS THE EXTENT OF THE FIRST
C                         DIMENSION.  THE NEXT ELEMENT IS THE EXTENT OF
C                         THE SECOND DIMENSION, ETC.   CAUTION:
C                         CURRENTLY INITIALIZED FOR ONLY ONE DIMENSION.
C    VAL1,VAL2    INT     VALUES USED IN ERROR MESSAGES.
C    YEAR         INT     YEAR OF CENTURY (00-99) OF GAC SCAN LINE
C                         EQUIVALENCED TO YEAR4.
C    YEAR4        INT     YEAR OF CENTURY (00-99) OF GAC SCAN LINE
C                         EQUIVALENCED TO YEAR.
C
C    SUBPROGRAMS CALLED - AVDGT1,JULDAY,CENDAY,AVG1RD,INPCT$,
C                         AVG1I1,AVG1I2,AVG1I4
C
C    EXIT STATES-  LOGICAL FEOF IS SET TO TRUE WHEN END OF FILE IS
C                  FOUND OR IF 1B* UTILITIES UNABLE TO READ FILE.
C
C    COMMENTS- RNGBUF CONTAINS A MAXIMUM 14 GOOD SEQUENTIAL GAC LINES OF
C        RADIANCES.  ARRAY CONTAINS THE SCAN LINE TIMES OF THE 5 DESIRED
C        SCAN LINES WHICH CONTAIN THE 17 CHOSEN SPOTS.  IF A GAP
C        EXISTS IT IS CONTROLLED BY ARRAY AND RNGBUF REMAINS WITH
C        GOOD SEQUENTIAL LINES.  RNGPTR POINTS TO THE SCAN LINE IN
C        RNGBUF WHICH HAS ITS SCAN LINE TIME IN ARRAY(3), THE CENTER
C        OF THE 5 DESIRED LINES.
C
C*********************************************************************
C
      INTEGER    NUMCHN,NUMSPT,BUFSIZ
      PARAMETER  (NUMCHN=5,NUMSPT=409,BUFSIZ=14)
      INTEGER    RECCNT,SAVETM,SOF,VAL1,VAL2
      INTEGER    I,J,K,L,M,START,RNGPTR,SPTIME(2),PNTER,PT
      INTEGER    ARRAY(5),STATUS,CALTIM,LINBAD(5),MONTH,DAY
      INTEGER    CDAY,MILSEC,GSC2SC,EOF,FUTURE,IDX
      INTEGER    SAVPT,RNGGST(2,BUFSIZ),YEAR4,JDAY4,ERRCOD
      INTEGER*4  SHAPE(0:7), JSHAPE(0:7), LENGTH, SCNAVH, INT4VL
      INTEGER*4  RETCOD
      INTEGER*2  INT2VL
      INTEGER*1  INT1VL
      INTEGER    YEAR,JDAY,CH3IND,CALIB(6,3)
      INTEGER    FLAG,RNGABF(BUFSIZ)
      LOGICAL    FEOF,FIRST,ONCE,NOREAD,AVAUSE,FIRST2
      CHARACTER*32 ITEMID
      REAL       RNGBUF(NUMCHN,NUMSPT,BUFSIZ),RNGITT(3,BUFSIZ)
      DATA RECCNT /0/, SAVETM /0/, SOF /1/, VAL1 /0/, VAL2 /0/
      DATA FIRST /.TRUE./, ONCE /.TRUE./, NOREAD /.FALSE./
      DATA SCNAVH /0/,START/1/
C
      SAVE FIRST,START,RECCNT,SAVETM,FUTURE,NOREAD,ONCE,SCNAVH
C
C  INITIALIZE VALUES.
C
      PNTER = 3
      STATUS = 0
      FIRST2 = FIRST
      GSC2SC = GACTIM(3)/1000
      PT = 0
      EOF = 3
      SAVPT = 0
      DO J = 0,7
         SHAPE(J) = 0
         JSHAPE(J) = 0
      ENDDO
C
      IF (FIRST) GOTO 50
C
C  IF THE RING BUFFER POINTER SHOWS ELEMENT 10 OR GREATER,
C  SHIFT THE VALUES IN THE RING BUFFER AND CORRESPONDING
C  SCAN LINE START TIME ARRAYS BACK 5 POSITIONS.
C
C
C  CHECK IF CENTER GAC LINE IS ALREADY IN RNGBUF.
C
      DO J=4,5
         IF ((CALTIM .GE. (ARRAY(J)-250)  .AND. CALTIM .LE.
     1           (ARRAY(J)+250)) .OR. (LINBAD(J) .EQ. J)) THEN
            DO M=5,3,-1
               IF(LINBAD(M) .EQ. 0) SAVPT = M
            END DO  
C
C  IF CENTER GAC LINE IN 4TH ARRAY POSITION, REASSIGN VALUES AND
C          READ IN ONE MORE GAC LINE.
C
            IF (J .EQ. 4) THEN
               EOF=1
               PT = 2
               DO K=1,4
                  ARRAY(K)=ARRAY(K+1)
                  IF(LINBAD(K+1).GT.0)LINBAD(K)=K
                  IF(LINBAD(K+1).EQ.0)LINBAD(K)=0
               END DO  
               LINBAD(5)=0
               IF(SAVPT .LT. 4) RNGPTR=RNGPTR+1
            ELSE
C
C  IF CENTER GAC LINE IN 5TH ARRAY POSITION, REASSIGN VALUES AND
C  READ IN TWO MORE GAC LINES.
C
               DO K=1,3
                  ARRAY(K)=ARRAY(K+2)
                  IF(LINBAD(K+2).GT.0)LINBAD(K)=K
                  IF(LINBAD(K+2).EQ.0)LINBAD(K)=0
               END DO
               LINBAD(4)=0
               LINBAD(5)=0
               IF(SAVPT .LT. 4) RNGPTR=RNGPTR+2
               EOF = 2
               PT = 1
            ENDIF
            IF(RNGPTR .EQ. 15) RNGPTR = 1
            IF(RNGPTR .GT. 15) RNGPTR = 2
            GOTO 50
         ENDIF
      END DO
C
C  COULDN'T FIND GAC LINE TIME IN RNGBUF, SO START WITH 5 NEW LINES
C  IN RNGBUF (FIRST=TRUE) AND RE-INITIALIZE THE POINTER.
C
      FIRST = .TRUE.
      RNGPTR = 0
C
   50 DO 90 L = 1, EOF
        IF(NOREAD) THEN
          CDAY = RNGGST(1,FUTURE)
          MILSEC = RNGGST(2,FUTURE)
          GOTO 80
        ENDIF
   60   CONTINUE
        SCNAVH = SCNAVH+1
        CALL AVDGT1(SCNAVH,RETCOD)
        STATUS = RETCOD
C
C IF END OF FILE FOUND OR UNABLE TO READ GAC DATA,
C SET FLAG AND RETURN TO CALLING ROUTINE.
C
        IF (STATUS .NE. 0) GOTO 910
        RECCNT = RECCNT + 1
        TOTGAC = TOTGAC + 1
C
C  CHECK GAC SCAN LINE FLAGS.
C  SET SHAPE FOR 1 BYTE FLAG CALLS.
C
        SHAPE(0) = 1
        SHAPE(1) = 1
C
C  IF LINE IS UNUSABLE GET THE NEXT SCAN LINE.
C
        ITEMID = 'DO_NOT_USE_SCAN                 '
        LENGTH = 1
        CALL AVG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
        STATUS = RETCOD
        IF(STATUS .NE. 0) GOTO 910
        FLAG = INT1VL
        IF(FLAG .NE. 0) THEN
           GACDO_NOT = GACDO_NOT + 1
           GOTO 60
        ENDIF
C
C    - COMMENT OUT THESE PER VERSION 3.4
C   
C       ITEMID = 'TIME_ERROR                      '
C       CALL AVG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
C       STATUS = RETCOD
C       IF(STATUS .NE. 0) GOTO 910
C       FLAG = INT1VL
C       IF(FLAG .NE. 0) THEN
C          GACTIMER = GACTIMER + 1
C          GOTO 60
C       ENDIF
C
C       ITEMID = 'CALIBRATION_ERROR               '
C       CALL AVG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
C       STATUS = RETCOD
C       IF(STATUS .NE. 0) GOTO 910
C       FLAG = INT1VL
C       IF(FLAG .NE. 0) THEN
C
C  4/23/98 - For NOAA-K data, this flag is set more often than
C            it should be, so double-check it against other
C            flags to see if it should truly be set
C
C          FLAG = 0
C          ITEMID = 'CHANNEL3_INDICATOR              '
C          CALL AVG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
C          IF(STATUS.NE.0) GO TO 910
C          CH3IND = INT1VL
C          JSHAPE(0)=2             
C          JSHAPE(1)=6             
C          JSHAPE(2)=3             
C          ITEMID = 'CALIB_CHAN_PROBLEM_INDICATOR    '
C          LENGTH = 18
C          CALL AVG1I1(ITEMID,CALIB,LENGTH,JSHAPE,STATUS)
C          IF(STATUS.NE.0) GO TO 910
C          DO IY=1,3
C             IF((CALIB(1,IY).NE.0).AND.((CH3IND.NE.1).OR.(IY.NE.1))) 
C    1           FLAG = 1
C             IF(CALIB(3,IY).EQ.1) FLAG = 1
C             IF(CALIB(4,IY).EQ.1) FLAG = 1
C          ENDDO
C          IF(FLAG.EQ.1) THEN
C             GACCALER = GACCALER + 1
C             GO TO 60
C          ENDIF
C       ENDIF
C
C  UNPACK YEAR, JULIAN DAY, AND MILLISECOND OF DAY FROM 1B*.
C
   70   ITEMID = 'YEAR_OF_SCAN                    '
        LENGTH = 1
        CALL AVG1I2(ITEMID,INT2VL,LENGTH,SHAPE,RETCOD)
        STATUS = RETCOD
        IF(STATUS .NE. 0) GOTO 910
        YEAR = INT2VL
        IF(YEAR .LE. 0 ) GOTO 920
C
        ITEMID = 'DAY_OF_YEAR_OF_SCAN             '
        CALL AVG1I2(ITEMID,INT2VL,LENGTH,SHAPE,RETCOD)
        STATUS = RETCOD
        IF(STATUS .NE. 0) GOTO 910
        JDAY = INT2VL
        IF(JDAY .LT. 0 .OR. JDAY .GT.366) GOTO 930
C
        ITEMID = 'TIME_OF_DAY_OF_SCAN             '
        CALL AVG1I4(ITEMID,INT4VL,LENGTH,SHAPE,RETCOD)
        STATUS = RETCOD
        IF(STATUS .NE. 0) GOTO 910
        MILSEC = INT4VL
C
C  CONVERT THE GAC SCAN LINE TIME TO MILLISECONDS.
C
        JDAY4 = JDAY
        YEAR4 = YEAR
        CALL JULDAY(JDAY4,YEAR4,MONTH,DAY)
        CALL CENDAY(YEAR4,MONTH,DAY,CDAY)
C
C   PUT GAC_LINE RADIANCES, TIME, RNGITT TEMPS, AND 3A/3B FLAG
C   INTO RING BUFFERS.
C
        CALL AVG1RD(RNGBUF(1,1,START),RNGITT(1,START),NUMCHN,
     1              NUMSPT,ITEMID,STATUS)
        IF(STATUS .NE. 0) GOTO 940
C
        RNGGST(1,START) = CDAY
        RNGGST(2,START) = MILSEC
C
        SHAPE(0) = 1
        SHAPE(1) = 1
        SHAPE(2) = 0
        LENGTH = 1
        ITEMID = 'CHANNEL3_INDICATOR              '
        CALL AVG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
        STATUS = RETCOD
        IF(STATUS .NE. 0) GOTO 910
        RNGABF(START) = INT1VL
        IF(RNGABF(START).EQ.1)AVAUSE=.TRUE.
        IF(RNGABF(START).EQ.0)AVAUSE=.FALSE.
C
C  INCREMENT POINTER TO RING BUFFER.
C
        START = START + 1
        IF (START .GT. BUFSIZ) THEN
          START = 1
          ONCE = .FALSE.
        ENDIF
C
C  CHECK IF HIRS AND GAC DAYS AGREE.
C
   80   IF (SPTIME(1) .GT. CDAY) THEN
C
C         GAC LINE AT LEAST A DAY PREVIOUS TO HIRS LINE
C         GET ANOTHER GAC SCAN LINE.
C
          SAVETM=MILSEC
          GOTO 60
        ELSEIF (CDAY .GT. SPTIME(1)) THEN
C
C         GAC LINE AT LEAST A DAY AFTER HIRS LINE
C
          SAVETM=MILSEC
          NOREAD=.TRUE.
          ARRAY(PNTER+PT)=I4SPV
          LINBAD(PNTER+PT)=PNTER+PT
          PT=PT+1
          FUTURE=START-1
          IF(FUTURE .EQ. 0) FUTURE=BUFSIZ
          GOTO 90
        ENDIF
C
C  GAC LINE SAME DAY AS HIRS LINE.
C  IF EXTRACTED TIME OF GAC LINE IS WITHIN 250 MILLISEC. OF CALCULATED
C  TIME, THAT SCAN LINE IS THE CLOSEST TO THE CENTER OF THE HIRS SPOT.
C
        IF(MILSEC .GE. (CALTIM+(PT*GSC2SC) - 250) .AND. MILSEC .LE.
     1        (CALTIM+(PT*GSC2SC) + 250)) THEN
C
C         FOUND CLOSEST GAC LINE.
C         SET PROPER POSITION OF ARRAY EQUAL TO GAC SCAN TIME.
C
          NOREAD=.FALSE.
          SAVETM=MILSEC
          ARRAY(PNTER+PT)=MILSEC
          LINBAD(PNTER+PT)=0
          IF(RNGPTR .EQ. 0) RNGPTR = START - 1
          IF (RNGPTR .EQ. 0) RNGPTR=BUFSIZ
          PT = PT + 1
          GOTO 90
        ELSE
C
C            GAC LINE LATER THAN HIRS LINE.
C
          IF(MILSEC .GT. (CALTIM +(PT*GSC2SC) + 250)) THEN
             NOREAD=.TRUE.
             SAVETM=MILSEC
             ARRAY(PNTER+PT)=I4SPV
             LINBAD(PNTER+PT)=PNTER+PT
             PT=PT+1
             FUTURE=START-1
             IF (FUTURE .EQ. 0) FUTURE=BUFSIZ
             GOTO 90
           ENDIF
        ENDIF
C
C CHECK FOR A TIME SEQUENCE ERROR.
C
        IF(MILSEC .LT. SAVETM) THEN
           ERRCOD=101
           VAL1=MILSEC
           VAL2=SAVETM
           GOTO 1000
        ENDIF
        GOTO 60
C
   90 CONTINUE
      IF(FIRST) THEN
        FIRST = .FALSE.
        IF(ONCE) THEN
C
C         IF CENTER LINE IS FIRST SCAN LINE IN FILE,
C         DO NOT TRY TO GET THE TWO PREVIOUS SCAN LINES.
C
          ONCE=.FALSE.
          IF (RNGPTR .EQ. 1 .OR. FUTURE .EQ. 1) THEN
             LINBAD(1)=1
             LINBAD(2)=2
             GOTO 110
C
C         IF CENTER LINE IS 2ND GAC SCAN LINE IN FILE,
C         RETRIEVE ONLY ONE PREVIOUS SCAN LINE.
C
          ELSEIF (RNGPTR .EQ. 2) THEN
             LINBAD(1)=1
             SOF = 2
          ENDIF
        ENDIF
C
C
C   FILL IN TOP OF ARRAY WITH THE CORRECT GAC SCAN LINE TIMES.
C
        I = RNGPTR - 1
        IF (I.LE.0) I=BUFSIZ
C
        DO 100 J=2,SOF,-1
C
           IF ((RNGGST(2,I) .GE. (CALTIM-GSC2SC)-250) .AND.
     1         (RNGGST(2,I) .LE. (CALTIM-GSC2SC)+250)) THEN
C
C            GAC SCAN LINE IS PRESENT AND ARRAY TIME IS FILLED IN.
C
             ARRAY(J) = RNGGST(2,I)
             LINBAD(J) = 0
C
C          OTHERWISE THE ARRAY VALUE WILL BE SET AS MISSING.
C
           ELSE
             ARRAY(J)=I4SPV
             LINBAD(J)=J
           ENDIF
C
           I = I - 1
           IF (I.EQ.0) I=BUFSIZ
           GSC2SC = GSC2SC + (GACTIM(3)/1000)
  100   CONTINUE
      ENDIF
C
  110 CONTINUE
C
      GOTO 1010
  910 ERRCOD = 105
      VAL1 = STATUS
      GOTO 1000
C
  920 ERRCOD = 104
      VAL1 = YEAR
      GOTO 1000
C
  930 ERRCOD = 103
      VAL1 = JDAY
      GOTO 1000
C
  940 ERRCOD = 102
      VAL1 = START
 1000 CALL INPCT$(ERRCOD,RECCNT,ITEMID,VAL1,VAL2,STATUS)
      FEOF = .TRUE.
 1010 CONTINUE
      RETURN
      END
C**********************************************************************
      SUBROUTINE INPCT$(MSGCOD,RECCNT,ITEMID,VAL1,VAL2,STAT)
C
C**********************************************************************
C
C   NAME- INPCT$
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 1.0    DATE- 08/19/91   PROGRAMMER- DAVE MOREL (SMSRC)
C   VERSION- 2.0    DATE- 08/23/95   PROGRAMMER- DIANE BEITZELL (SMSRC)
C   VERSION- 3.0    DATE- 10/07/97   PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.1    DATE- 04/23/98   PROGRAMMER- LEAH WILSON (RSTX)
C
C   FUNCTION-THIS SUBROUTINE WRITES OUT ERROR MESSAGES DETECTED IN
C            INPCTL AND ANY B1* UTILITY ERROR MESSAGES.
C   VERSION- 3.0 WRITE ERROR MESSAGES TO THE KIDS FILE.  INCLUDE /SPGUNT/.
C   VERSION- 3.1 ADD ITEMID TO CALL.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O   UNIT  FUNCTIONAL DESCRIPTION
C     ------------------------ ---   ----  ----------------------
C     ...KIDS                   O    KUNIT KRAY INTERACTIVE DOCUMENTATION
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE          TYPE I/O  DESCRIPTION
C     --------          ---- ---  --------
C     MSGCOD             INT  I   MESSAGE CODE OF ERROR DETECTED
C     RECCNT             INT  I   COUNT OF LOGICAL RECORDS PROCESSED
C     ITEMID           CH*32  I   ITEM BEING EXTRACTED FROM 1B*
C     VAL1               INT  I   VALUE TO BE WRITTEN OUT IN ERROR MESSAGE
C     VAL2               INT  I   VALUE TO BE WRITTEN OUT IN ERROR MESSAGE
C     STAT               INT  O   NORMALLY SET TO 0 BUT IF AN ERROR
C                                 OCCURS AN ERROR STATUS CODE IS SET
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
      INCLUDE 'MSG1BS'
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    FUNCTION
C     --------                ----    --------
C     I                        INT    DO LOOP CONTROL INDEX
C     KMSG                   CH*100   KIDS MESSAGE BUFFER
C     C4TEMP                  CH*4    HOLDING BUFFER FOR WRITING TO KIDS
C     C5TEMP                  CH*5    HOLDING BUFFER FOR WRITING TO KIDS
C     C9TEMP                  CH*9    HOLDING BUFFER FOR WRITING TO KIDS
C     STAT1B                   I*4    RETURN CODE FOR THE CALL TO B1SERR
C
C   SUBROUTINES CALLED- KIDS
C
C   EXIT STATES-
C       STAT =  0; INDICATING A NO OR NON-FATAL ERROR WAS DETECTED
C       STAT = 99; INDICATING A FATAL ERROR WAS DETECTED
C
C***********************************************************************
C
      CHARACTER*4   C4TEMP
      CHARACTER*5   C5TEMP
      CHARACTER*9   C9TEMP
      CHARACTER*32 ITEMID
      CHARACTER*100 KMSG
      INTEGER     STAT, I, MSGCOD, RECCNT, VAL1, VAL2
      INTEGER*4   STAT1B
C
C  SET ERINFO:SUBNAM
C
      SUBNAM = 'SPGDVR'
      STAT = 0
C
C   CHECK MESSAGE CODES.
C
      IF(MSGCOD.EQ.101) THEN
        STAT = 99
        KMSG(1:50)=' * G A C ERROR * GAC time out of sequence in frame'
        KMSG(51:100)='    , logical record #      '
        WRITE(C4TEMP,'(I4)') FRMNBR 
        KMSG(51:54) = C4TEMP
        WRITE(C4TEMP,'(I4)') RECCNT 
        KMSG(74:77) = C4TEMP
        print *, KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        KMSG(1:50)='Previous logical record time in msecs =          .'
        KMSG(51:100)=' Last logical record time in msecs =          '
        WRITE(C9TEMP,'(I9)') VAL2
        KMSG(41:49) = C9TEMP
        WRITE(C9TEMP,'(I9)') VAL1
        KMSG(86:94) = C9TEMP
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE IF(MSGCOD.EQ.102) THEN
        KMSG(1:50)=' * G A C ERROR * In GAC 1b* utility for frame     '
        KMSG(51:100)=' and logical record #     , status      '
        WRITE(C4TEMP,'(I4)') FRMNBR 
        KMSG(47:50) = C4TEMP
        WRITE(C4TEMP,'(I4)') RECCNT 
        KMSG(73:76) = C4TEMP
        WRITE(C4TEMP,'(I4)') VAL1   
        KMSG(86:89) = C4TEMP
        STAT = 99
        print *, KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        KMSG=' Item being accessed is                           '
        KMSG(25:56) = ITEMID
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        DO IDX=1,NOUT
           KMSG(1:60) = ERRBUF(IDX)
           PRINT *,KMSG
           CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        END DO
C
      ELSE IF(MSGCOD.EQ.103) THEN
        KMSG(1:50)=' * G A C ERROR * Jday      is not between 0 & 366.'
        KMSG(51:100)='Loc: logical record #     '
        WRITE(C4TEMP,'(I4)') VAL1   
        KMSG(23:26) = C4TEMP
        WRITE(C4TEMP,'(I4)') RECCNT 
        KMSG(73:76) = C4TEMP
        STAT = 99
        print *, KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        KMSG=' Item being accessed is                           '
        KMSG(25:56) = ITEMID
        print *, KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE IF(MSGCOD.EQ.104) THEN
        KMSG(1:50)=' * G A C ERROR * Year       is less than or = 0.  '
        KMSG(51:100)='Loc: logical record #     '
        WRITE(C5TEMP,'(I5)') VAL1   
        KMSG(23:27) = C5TEMP
        WRITE(C4TEMP,'(I4)') RECCNT 
        KMSG(73:76) = C4TEMP
        STAT = 99
        print *, KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        KMSG=' Item being accessed is                           '
        KMSG(25:56) = ITEMID
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
      ELSE IF(MSGCOD.EQ.105) THEN
        KMSG='End of file or unable to read further GAC data.'
        STAT = 99
        print *, KMSG
        CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
C
      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

