      SUBROUTINE INGAC(HIRDAT, FIRST, FEOF, HIRSKP, ERSTAT)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- INGAC
C
C   LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE      COMPUTER- CRAY     
C
C   VERSION- 1.0    DATE- 07/03/91   PROGRAMMER- DAVE MOREL (SMSRC)
C   VERSION- 2.0    DATE- 07/14/95   PROGRAMMER- DIANE BEITZELL (SMSRC)
C      ATOVS-144 - CHANGED FOR ATOVS TO INPUT A 1B* FILE.
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- 04/03/98   PROGRAMMER- LEAH WILSON (RSTX)
C      ADD BAD GAC COUNTER TO FLAG EXCESSIVE GROSS ERROR LIMIT FAILURES.
C   VERSION- 3.2    DATE- 03/02/01   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C      WR #136 - ADDED CALLING ARGUMENT HIRSKP TO BE USED IN CALCULATING
C                THE STARTING HIRS BUFFER LINE FOR THE FIRST DATA FRAME.
C
C   FUNCTION-  THIS SUBROUTINE FILLS IN AVHRR INSTRUMENT GAC DATA
C              INTO THE HIRS DATA BUFFER, HIRDAT, IF THE HIRS
C              INSTRUMENT IS AVAILABLE.
C
C
C   CALLING SEQUENCE PARAMETERS-
C     NAME              TYPE I/O  DESCRIPTION
C     ----              ---- ---  -----------
C     HIRDAT            REAL I/O  3D HIRS BUFFER (/INSPARM/:HNSPOT X
C                                 /INSPARM/:HTOTLN X /INSPARM/:HDATEL)
C                                 WHICH HOLDS ONE DATA
C                                 FRAME OF 256 SECONDS OF HIRS
C                                 SPOTS IN 20 CHANNELS AND
C                                 AVHRR GAC SPOTS IN 5 CHANNELS IN
C                                 RADIANCES AND BRIGHTNESS TEMPERATURES(BT).
C
C     FIRST             LOG    I  SET TO .TRUE. FOR THE FIRST FRAME.
C
C     FEOF              LOG    O  NORMALLY SET TO .FALSE. - WHEN SET TO
C                                 TRUE GAC DATA IS NO LONGER PRESENT.
C
C     HIRSKP            INT    I  NUMBER OF HIRS LINES SKIPPED IN THE FIRST
C                                 DATA FRAME BUFFER BEFORE INSERTING DATA
C                                 FROM THE HIRS 1B* FILE.
C                                  
C     ERSTAT            INT    O  ERROR RETURN CODE.
C                                 = 0 IF NO ERROR OR END OF FILE FOUND,
C                                 = 99 IF ERROR OCCURRED IN GACSEL.
C
C   COMMON AREA PARAMETERS-
      INCLUDE 'INPARM'
      INCLUDE 'INSPARM'
      INCLUDE 'FRMINF'
      INCLUDE 'COLLOC' 
C
C   LOCAL PARAMETERS-
C     NAME              TYPE  DESCRIPTION
C     ----              ----  -----------
C     LINENO            INT   LOOP CONTROL INDEX.
C     SPTIME            INT   A VECTOR OF DIMENSION (CENTURY DAY/
C                             MILLISECOND TIME).
C     START             INT   BEGINNING OF HIRS BUFFER TO BE FILLED WITH
C                             GAC DATA.
C
C     SUBPROGRAMS CALLED-GACSEL
C
C     EXIT STATES- ROUTINE WILL RETURN WITH
C                       ERSTAT = 0  - IF PROGRAM RAN CORRECTLY OR
C                                     END OF FILE CONDITION FOUND.
C                       ERSTAT = 99 - IF ENCOUNTERS ERROR IN GACSEL.
C
C*********************************************************************
C
      INTEGER  ERSTAT, START, HIRSKP
      REAL     HIRDAT(HNSPOT,HTOTLN,HDATEL)
C
      INTEGER  LINENO, SPTIME(2)
      LOGICAL  FIRST, FEOF
C
      SAVE  SPTIME
C
C EACH HIRS DATA FRAME CONSISTS OF 256 SECONDS OF DATA IN 40 LINES.
C THE EXTENDED DATA FRAME CONSISTS OF THE 40 LINES OF DATA, PLUS AN
C OVERLAP OF 9 (HTOTBF-1) LINES. THE FIRST FRAME DOES NOT HAVE ANY
C OVERLAP DATA FROM THE PRECEDING FRAME, SO THE EXTENDED FRAME BUFFER
C IS FILLED BEGINNING WITH THE FIRST LINE OF HIRS DATA EXTRACTED FROM
C THE HIRS 1B* FILE. SINCE THIS LINE IS RARELY THE FIRST LINE
C FOLLOWING THE SECOND CALIBRATION LINE, THE STARTING POINT HAS TO
C BE CALCULATED FROM THE NUMBER OF LINES SKIPPED BEFORE HIRS DATA
C WERE INSERTED INTO THE EXTENDED HIRS FRAME BUFFERS.
C
      IF(FIRST) THEN
        START = 1 + HIRSKP
      ELSE
        START = HTOTBF
      ENDIF
C
C SET SPTIME TO THE SYNC TIME OF THE FRAME.
C
      DO 50 LINENO = START,HTOTLN-1
         IF(HGOODL(LINENO)) THEN
           SPTIME(1)=HSCTIM(1,LINENO)
           SPTIME(2)=HSCTIM(2,LINENO)
           CALL GACSEL(HIRDAT,LINENO,SPTIME,FEOF,ERSTAT)
           IF(FEOF .OR. ERSTAT .EQ. 99) RETURN
         END IF
   50 CONTINUE
C
      RETURN
      END
