      SUBROUTINE INHIRS(FSTFRM,SYNCTR,SYNTIM,HCALCF,HIRDAT,HIRANC,
     1                  ERSTAT,NSKIPPED,OBSHIR,N16HIR,SCNHIR,
     2                  FULFRM,METOP)
      IMPLICIT NONE
C***********************************************************************
C    NAME- INHIRS
C
C    LANGUAGE- FORTRAN 77    TYPE- SUBROUTINE   COMPUTER- CRAY
C
C    VERSION- 1.0  DATE- 01/29/91   PROGRAMMER- JEFF MANNING (SMSRC)
C    VERSION- 3.0  DATE- 05/09/96   PROGRAMMER- ALEX POZDNYAK (FDC)
C      MODIFIED TO RUN ON THE CRAY.  REPLACED I*4 WITH INT, L*4 WITH
C      LOG AND R*4 WITH REAL.
C    VERSION- 3.1  DATE- 01/29/97   PROGRAMMER- LEAH WILSON (HSTX)
C    VERSION- 3.2  DATE- 07/10/97   PROGRAMMER- LEAH WILSON (HSTX)
C    VERSION- 3.3  DATE- 04/06/98   PROGRAMMER- MIKE PETTEY (RSTX)
C    VERSION- 3.4  DATE- 01/08/99   PROGRAMMER- LEAH WILSON, RAYTHEON
C    VERSION- 3.5  DATE- 01/26/99   PROGRAMMER- LEAH WILSON, RAYTHEON
C    VERSION- 3.6  DATE- 05/03/99   PROGRAMMER- LEAH WILSON, RAYTHEON
C    VERSION- 3.7  DATE- 03/10/00   PROGRAMMER- LEAH WILSON, RAYTHEON
C    VERSION- 3.8  DATE- 06/14/00   PROGRAMMER- LEAH WILSON, RAYTHEON
C    VERSION- 3.9  DATE- 08/09/00   PROGRAMMER- R.KELLEY/MP FERGUSON
C    VERSION- 3.10 DATE- 12/15/00   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.11 DATE- 02/07/01   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.12 DATE- 04/18/01   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.13 DATE- 12/11/01   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.14 DATE- 01/14/02   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.15 DATE- 08/05/03   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.16 DATE- 09/09/03   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.17 DATE- 09/24/03   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.18 DATE- 01/10/05   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.19 DATE- 03/21/06   PROGRAMMER- FRANK TILLEY (ERT)
C    VERSION- 3.20 DATE- 08/04/06   PROGRAMMER- FRANK TILLEY (IMSG)
C
C    FUNCTION-  THIS SUBROUTINE BUILDS AN EXTENDED DATA FRAME OF HIRS
C               DATA.  THE EXTENDED DATA FRAME IS COMPRISED OF A 256
C               SECOND DATA FRAME PLUS A 32 SECOND OVERLAP FROM THE
C               PREVIOUS FRAME AND THE SUBSEQUENT FRAME.  TWO BUFFERS
C               FOR THE EXTENDED DATA FRAME ARE FILLED.  THE FIRST
C               BUFFER IS THE DATA BUFFER WHICH CONTAINS HIRS
C               BRIGHTNESS TEMPERATURES.  THE SECOND BUFFER, THE
C               ANCILLARY DATA BUFFER, CONTAINS SUCH DATA AS THE
C               LATITUDE, LONGITUDE, ETC.  SCAN LINES WHICH ARE FOUND
C               TO BE UNUSABLE AND/OR LINES USED FOR DATA
C               SYNCHRONIZATION ARE FILLED IN THE BUFFERS AS MISSING
C               VALUES (R4SPV).
C    VERSION- 3.1 RTOVS UPGRADES INTO ATOVS - ADD TOVS LOGIC TO APPLY
C                 CUTOFFS/OFFSETS TO HIRS CHANNEL 2-12. THESE VALUES
C                 ARE STORED IN /OFFSET/.  ADD OBSHIR TO CALL TO HIRLIN
C                 TO GET NODE.
C    VERSION- 3.2 REPLACE PRI_CAL_COEFFS WITH PRIMARY_CAL_COEFS.
C    VERSION- 3.3 ADD /OZTIME/ AND FILL TIPTIM BUFFER WITH TIP TIME
C                 CODES FROM EACH LINE IN THE FRAME.
C    VERSION- 3.4 CORRECT HIRS CALCULATION OF NUMLIN FOR FIRST FRAME.
C                 THIS PROBLEM APPEARED WITH NOAA-15, IN WHICH A NUMBER
C                 OF BAD HIRS LINES APPEAR AT THE BEGINNING OF A 1B*
C                 PRIOR TO THE REGULAR PATTERN OF 38 LINES, 2 CAL., ETC.
C                 THIS NEW NUMLIN CALCULATION ALLOWS ALL THE DATA TO BE
C                 PROCESSED WITHOUT ANY MISSING LINES.
C    VERSION- 3.5 PASS USE_CAL_PROB FLAG TO SCNCNF.
C    VERSION- 3.6 FOR CM, REMOVE OZTIME.
C    VERSION- 3.7 IN-LINE CHKSCT TO REFLECT USAGE OF SCAN LINE NUMBERS.
C                 REMOVE CHECKS OF TIMCOD=2 OR 3, WHICH ARE NO LONGER SET.
C                 ADD /DEBUG/ TO ALLOW DEBUG PRINTS CONTROLLABLE BY CDB
C                 FLAGS. INLINE SCNCNF AND ELIMINATE USE_CAL_PROB. ADD
C                 COUNTER FOR BADHFLAG IN /MONSTA/. ADD WARNING KIDS
C                 MESSAGE IF BACKWARDS SCAN LINES ARE FOUND.
C                 FOR EXAMPLE:201, 202, 197, 203).  ADD GOOD2CAL FOR
C                 USE BY INAMA, AND /FRAME1/.
C   VERSION- 3.8  WHEN LIMB CORRECTION DATA COLLECTION WAS TURNED ON,
C                 PROBLEMS WITH THE TIMES OF SOME LINES IN THE FIRST
C                 FRAME WERE NOTED.  THESE WERE RELATED TO THE CODE
C                 STILL REGARDING LINE 6 AS THE FIRST LINE IN THE FRAME,
C                 SO MODIFICATIONS WERE MADE TO CORRECT THE PROBLEM,
C                 BEGINING AROUND THE "DO 20" LOOP. ALSO ADDED MSG 306,
C                 TO NOTE IF SYNCTR EVER EXTENDS BEYOND DATA FRAME.
C   VERSION- 3.9  ADD COMMON HIRSTAT, INITIALIZE BADHIR BUFFER
C   VERSION- 3.10 ADDED CALLING ARGUMENT 'N16HIR' TO PASS IN TO
C                 SUBROUTINE 'HIRLIN'. (WR #131)
C   VERSION- 3.11 CHANGED THE OUTPUT ARGUMENT 'GOOD2CAL' TO 'NSKIPPED'
C                 TO REFLECT THE NUMBER OF LINES SKIPPED IN THE
C                 HIRS BUFFERS BEFORE INSERTING DATA FROM THE FIRST
C                 SCAN LINE IN THE HIRS 1B*. SUBROUTINE INAMA USES
C                 THIS VALUE IN ITS CALCULATION OF THE LINE IN THE
C                 AMSU-A BUFFERS WHERE DATA FROM THE FIRST SCAN LINE
C                 IN THE AMSU-A 1B* WILL BE INSERTED. (PR #26)
C   VERSION- 3.12 ADDED A CHECK OF THE TIME_SEQUENCE_ERROR FLAG
C                 TO THE DO_NOT_USE_SCAN CHECK FOR SETTING SCAN LINE
C                 CONFIDENCE TO ZERO TO EXCLUDE BAD HIRS SCAN LINES.
C                 (PR #30)
C   VERSION- 3.13 ADDED CODE TO RETURN TO THE CALLING ROUTINE WITH
C                 AN ERROR STATUS OF 99 WHEN OUT-OF-ORDER SCANS
C                 ARE FOUND OR WHEN A DATA GAP IS ENCOUNTERED (PR #52)
C   VERSION- 3.14 REMOVED THE CODE THAT SETS AN ERROR STATUS OF 99
C                 WHEN A DATA GAP IS ENCOUNTERED (PR #56).
C   VERSION- 3.15  PORTER TO IBM SP
C                 CHANGED FLUSH() to FLUSH_()
C   VERSION- 3.15 REPLACED THE REPORTED SCAN LINE 'SCNLIN' WITH LOCAL
C                 COUNTER 'CALCOUNT' IN SETTING THE VALUE OF 'NUMLIN'
C                 TO PREVENT 'NUMLIN' FROM POSSIBLY BEING NEGATIVE
C                 (WHICH ALLOWED HIRS DATA TO BE WRITTEN TO MEMORY
C                 LOCATIONS THAT WERE SUPPOSED TO CONTAIN OTHER
C                 VITAL DATA). (PR #76)
C   VERSION- 3.16 COMPLETELY REMOVED THE USE OF THE REPORTED FIRST
C                 SCAN LINE NUMBER IN CALCULATING 'NUMLIN'
C                 (WR #243).
C   VERSION- 3.17 CHANGED THE TEST FOR DATA GAP DETECTION AND
C                 EXTENT TO USE A CALCULATION BASED ON DIFFERENCES
C                 IN SCAN LINE START TIME RATHER THAN THE SCAN
C                 LINE NUMBERS REPORTED IN THE 1B*. USED A NEW
C                 INTEGER FUNCTION 'HNRSCN' TO CALCULATE THE
C                 DIFFERENCE IN SCAN LINE NUMBERS BASED ON THE
C                 SCAN LINE START TIMES. ADDED LOGIC IN FIRST FRAME
C                 PROCESSING TO HANDLE SITUATIONS WHERE A DATA GAP
C                 INCLUDES LINES WHERE THE FIRST CALIBRATION LINES
C                 SHOULD APPEAR (WR #244).
C   VERSION- 3.18 UNDER WR #230 (NOAA-N READINESS) ADDED
C                 CALLING ARGUMENT SCNHIR TO TRACK THE HIRS
C                 SCAN BEING PROCESSED. CHANGED HRSGT1 TO
C                 HRDGT1 FOR DIRECT ACCESS. CHANGED 'SHAPE' TO
C                 INTEGER*4. SUBSTITUTED I*4 'LENGTH' FOR DATA
C                 LENGTH IN 1B* UTILITY CALLS. CHANGED 'ERSTAT'
C                 TO I*4 'RETCOD' IN 1B* UTILITY CALLS.
C   VERSION- 3.19 UNDER WR #300 ADDED CALLING ARGUMENT FULFRM,
C                 WHICH INHIRS SETS TO 'FALSE' FOR THE PARTIAL
C                 DATA FRAMES AT THE BEGINNING AND END OF 1B*
C                 FILE PROCESSING. THE FLAG IS SET TO .TRUE.
C                 FOR THOSE RARE OCCASIONS WHEN THE FIRST
C                 DATA FRAME STARTS AT THE FIRST HIRS SCAN
C                 FOLLOWING A CALIBRATION LINE.
C   VERSION- 3.20 UNDER PR #87 WROTE MESSAGES TO THE LOG FILE
C                 AND KIDS FILE AND SET THE ERSTAT RETURN CODE
C                 TO 99 WHEN NO CALIBRATION LINE IS FOUND WITHIN
C                 THE FIRST 39 HIRS SCANS AT THE BEGINNING OF AN
C                 ORBIT. IN THIS SITUATION NO SYNCHRONIZATION WITH
C                 AMSU-A CAN NOT BE DONE.
C   VERSION- 3.21 UNDER WR #303 OUT-OF-ORDER SCAN LINES CAUSE THE
C                 HIRS INSTRUMENT TO BE REMOVED FROM ORBITAL
C                 PROCESSING BUT DOES NOT CAUSE A FATAL ERROR
C                 ERSTAT CODE TO BE SET.
C
C   FILES- DSN                        UNIT   I/O   REMARKS
C          ------------------------   ----   ---  ----------------
C          .........../HIRS1BS        H1BUNT  I   HIRS 1B* DATASET
C
C   CALLING SEQUENCE PARAMETERS-
C      VARIABLE  TYPE  I/O  DESCRIPTION
C      --------  ----  ---  -----------
C      FSTFRM    LOG    I   FLAG FOR FIRST FRAME CASE - SET TO .TRUE.
C                           IF DATA FRAME IS FIRST FRAME PROCESSED
C                           AFTER SYNCHRONIZATION, OTHERWISE SET TO
C                           .FALSE.
C      SYNCTR    INT    I   COUNTER FOR NUMBER OF MISSING LINES TO
C                           INSERT INTO FRAME BEFORE ACTUAL DATA INPUT
C                           (ONLY USED IF FIRST FRAME CASE)
C      SYNTIM    INT    I   ARRAY OF THE INSTRUMENT SYNC. TIME
C                           (CENTURY DAY, MILLISECOND OF DAY)
C      HCALCF    REAL I/O   3D ARRAY OF HIRS CALIBRATION COEFFICIENTS
C                           FOR EACH CHANNEL FOR EACH LINE IN THE
C                           EXTENDED DATA FRAME.
C      HIRDAT   REAL    O   3D BUFFER (INSPARM:HNSPOT X INSPARM:HTOTLN
C                           X INSPARM:HDATEL) CONTAINING HIRS DATA.
C      HIRANC   REAL    O   3D BUFFER (INSPARM:HNSPOT X INSPARM:HTOTLN
C                           X INSPARM:HNANC) CONTAINING HIRS "ANCILLARY"
C                           DATA
C                           PARM1 = LAT, PARM2 = LON, PARM3 = SOLAR
C                           ZENITH ANGLE, PARM4 = SOLAR AZIMUTH ANGLE,
C                           PARM5 = SATELLITE ZENITH ANGLE, ETC.
C      ERSTAT    INT    O   RETURN CODE INDICATING IF AN ERROR OCCURRED
C      NSKIPPED  INT    O   NUMBER OF LINES SKIPPED IN THE HIRS BUFFERS
C                           BEFORE INSERTING DATA FROM THE FIRST HIRS
C                           SCAN LINE IN A HIRS 1B*.
C      OBSHIR    REAL I/O   ARRAY (DIMENSIONED HNSPOT X HTOTLN X 20)
C                           CONTAINING THE NODE IN THE 20TH ELEMENT.
C      N16HIR    INT    I   FLAG TO PASS TO HIRLIN TO SHIFT BRIGHTNESS
C                           TEMPERATURES TO THE RIGHT BY ONE SPOT TO
C                           CORRECT FOR INSTRUMENT MISALIGNMENT.
C      SCNHIR    I*4  I/O   HIRS SCAN NUMBER.
C      FULFRM    LOG  I/O   'FULL FRAME' FLAG THAT INHIRS SETS TO FALSE
C                           FOR THE FIRST AND LAST PARTIAL DATA FRAMES
C                           IN A HIRS 1B* FILE (WR #300).
C
C   COMMON AREA PARAMETERS-
      INCLUDE'SPGUNT'
      INCLUDE'INSPARM'
      INCLUDE'INPARM'
      INCLUDE'INPCDB'
      INCLUDE'COLLOC'
      INCLUDE'ERPARM'
      INCLUDE'ERINFO'
      INCLUDE'HIRSTAT'
      INCLUDE'FRMINF'
      INCLUDE'OVRLAP'
      INCLUDE'MONSTA'
      INCLUDE'SPVAL'
      INCLUDE'DEBUG'
      INCLUDE'FRAME1'
C
C    LOCAL VARIABLES-
C      VARIABLE      TYPE   DESCRIPTION
C      --------      ----   -----------
C      BSHFT         INT    USED TO SHIFT BITS IN CONFIG.
C      CNFDNC        REAL   COMPUTED SCAN CONFIDENCE VALUE (100 HAVING
C                           TOTAL CONFIDENCE, 0 HAVING NO CONFIDENCE).
C      CALB1ST       INT    SCAN LINE NUMBER OF FIRST CALIBRATION LINE
C      CALCOUNT      INT    COUNTS HOW MANY LINES TO BACKSPACE THRU ONCE
C                           FIRST CALIBRATION LINE IS FOUND, IN ORDER TO
C                           RESET READ POINTER BACK TO ORIGINAL 1B* POSITION.
C      DAY           INT    DAY OF THE MONTH.
C      ERTVIW        INT    EARTH VIEW VALUE EXTRACTED FROM THE SCAN
C                           LINE.
C      FLGCOD        INT    QUALITY CODE FOR SCAN LINE OBTAINED FROM
C                           CHECKING FLAGS, 0=USABLE, 1=SUSPECT,
C                           2=UNUSABLE.
C      GOOD2CAL      INT    NUMBER OF SCAN LINES BETWEEN SYNCH POINT AND
C                           FIRST CALIBRATION LINE.
C      HANCOV        REAL   3D BUFFER CONTAINING "64-SEC OVERLAP"
C                           PARAMETERS.
C      ICHAN         INT    LOOP CONTROL VARIABLE OVER HIRS CHANNELS.
C      I, LINE       INT    DO LOOP COUNTERS.
C      INSTRU        INT    INSTRUMENT BEING PROCESSED (1=HIRS, 2=AMSU/A
C                           3=AMSU/B).
C      INT1VL        I*1    USED FOR EXTRACTING I*1 VALUES FROM THE 1B*
C      INT2VL        I*2    USED FOR EXTRACTING INTEGER BYTE 2 VALUES
C                           FROM THE HIRS SCAN LINE.
C      INT4VL        I*4    USED FOR EXTRACTING I*4 VALUES FROM THE 1B*
C      ITEMID       CH*32   NAME OF THE ITEM THAT IS TO BE EXTRACTED
C                           FROM THE HIRS 1B* SCAN LINE.
C      KMSG         CH*100  KIDS MESSAGE BUFFER.
C      LENGTH        I*4    DATA LENGTH FOR 1B* UTILITY CALLS.
C      LINTIM        INT    ARRAY CONTAINING CURRENT SCAN LINE TIME
C                           (ELEM 1 IS CENT DAY, ELEM 2 IS MSEC OF DAY).
C      NUMLIN        INT    NUMBER OF LINES WRITTEN TO FRAME BUFFER.
C      NUMMIS        INT    NUMBER OF MISSING LINES BETWEEN PREVIOUS
C                           AND CURRENT SCAN LINES.
C      MONTH         INT    MONTH OF YEAR.
C      PRVTIM        INT    ARRAY CONTAINING TIME OF PREVIOUS SCAN
C                           (ELEM 1 = CENT DAY, ELEM 2 = MSEC OF DAY).
C      PRVSCN        INT    SCAN NUMBER OF PREVIOUS SCAN.
C      RETCOD        I*4    RETURN CODE FROM 1B* UTILITIES.
C      SCAN1         INT    SAVES THE FIRST SCAN LINE NUMBER OF THE FIRST
C                           HIRS LINE IN THE 1B*.
C      SCNDIF        INT    DIFFERENCE BETWEEN SCAN LINE NUMBERS
C      SEQERR        INT    VALUE OF THE TIME_SEQUENCE_ERROR FLAG FROM A
C                           HIRS SCAN LINE.
C      SHAPE         I*4    SHAPE ARRAY, DIMENSIONED (0:7), USED IN CALL
C                           TO THE 1B* 'GET' ROUTINES.
C      SIGN          REAL   STORES SIGN OF HIRS CALIBRATION COEFFICIENT.
C      TIMCOD        INT    QUALITY CODE RETURNED FROM CHECKING THE SCAN
C                           LINE TIME.
C      Y             REAL   STORES ABSOLUTE VALUE OF 3RD HIRS CALIBRATION
C                           COEFFICIENT.
C
C    SUBPROGRAMS CALLED- CENDAY,CLRFIN,CMPTIM,FFPROC,HRG1I1,
C                        HRG1I2,HRG1I4,HRG1R4,HRDGT1,HIRLIN,
C                        INIFIN,INIFRM,JULDAY,MCLR,INHIR$
C
C    FUNCTIONS USED- IBCLR, MOD, HNRSCN
C
      INTEGER HNRSCN
C
C    EXIT STATES- ERSTAT=99 FOR NON-FATAL ERROR
C                 ERSTAT=0 FOR NO OR A NON-FATAL ERROR
C
C***********************************************************************
C
      LOGICAL FSTFRM, FULFRM, METOP
      INTEGER ERSTAT,SYNCTR,SYNTIM(2),DONOTUSE,SCNDIF,N16HIR,NSKIPPED
      INTEGER*4 SCNHIR
      REAL HIRDAT(HNSPOT,HTOTLN,HDATEL),HIRANC(HNSPOT,HTOTLN,HNANC)
      REAL HANCOV(HNSPOT,HTOTBF,HNANC),HCALCF(3,HNCHAN,HTOTLN)
      REAL*4 HCALC4(3,HNCHAN,HTOTLN)
      REAL OBSHIR(HNSPOT,HTOTLN,20)
      CHARACTER*32 ITEMID
      CHARACTER*100 KMSG
      INTEGER*1 INT1VL
      INTEGER*2 INT2VL
      INTEGER ERTVIW,SEQERR,J,K,IC
      INTEGER DAY,FLGCOD,I,LINE,LINTIM(2),NUMMIS,INSTRU, SCAN1
      INTEGER MONTH,NUMLIN,PRVSCN,PRVTIM(2),TIMCOD
      INTEGER*4 SHAPE(0:7), LENGTH, INT4VL, RETCOD
      INTEGER BSHFT, ICHAN,CALB1ST, CALCOUNT, GOOD2CAL, SCANLN
      INTEGER SC1TIM(2)
      REAL    CNFDNC, Y, SIGN
C
      DATA INSTRU/1/, PRVSCN/0/
C
      SAVE PRVTIM, PRVSCN, HANCOV
C
C  INITIALIZE ALL ELEMENTS OF THE SHAPE ARRAY TO 0. RETURN CODE TO 0.
C
      DO 10, I = 1, 8
          SHAPE(I-1) = 0
   10 CONTINUE
      ERSTAT = 0
C
C   INITIALIZE BADHIR BUFFER FOR KEEPING TRACK OF BAD HIRS VALUES.  THESE
C   WILL BE SET TO MISSING ONLY UPON WRITING TO THE RODF AND HODF FILES.
C
      DO I = 1,HNSPOT
       DO J = 1,HTOTLN
        BADRET(I,J) = .FALSE.
        DO K = 1,HNCHAN
         BADHIR(I,J,K) = 0
        END DO
       END DO
      END DO
C
C
C   CHECK FIRST FRAME FLAG TO DETERMINE IF THE FIRST FRAME HAS BEEN
C   PROCESSED.  IF IT HAS NOT (FSTFRM = .TRUE.), THEN PROCESS FIRST
C   FRAME, OTHERWISE PROCESS NORMALLY.
C
      IF (.NOT.(FSTFRM)) THEN
C
          CALL INIFRM(HDATOV,HANCOV,HTOTBF,HNSPOT,HDATEL,HNANC,HIRDAT,
     1                HIRANC,HTOTLN)
C
C   SET LINE NUMBER EQUAL TO HTOTBF - 1 SINCE THIS IS NUMBER OF LINES
C   USED IN HIRDAT/HIRANC BUFFERS.
C
          NUMLIN = HTOTBF -1
C
C   INITIALIZE THE SCAN LINE CONFIDENCE TO ZERO.
C
          CALL MCLR(HIRANC(1,NUMLIN+1,6),HNSPOT,HTOTLN-NUMLIN)
          CALL MCLR(HANCOV(1,1,6),HNSPOT,HTOTBF)
C
C   INITIALIZE THE CHANNEL AVAILABILITY TO ZERO.
C
          CALL MCLR(HIRANC(1,NUMLIN+1,7),HNSPOT,HTOTLN-NUMLIN)
          CALL MCLR(HANCOV(1,1,7),HNSPOT,HTOTBF)
C
C   INITIALIZE THE CALIBRATION COEFFICIENT ARRAY AND ARRAYS IN /FRMINF/
C
          CALL INIFIN(HGOODL,HQFLAG,HSCTIM,HCALCF,HTOTLN,HTOTBF,HNCHAN)
C
      ELSE
C======= FIRST FRAME PROCESSING =================================
C
C   FRAME IS FIRST FRAME TO BE PROCESSED FOLLOWING SYNCHRONIZATION.
C   CALL FFPROC TO INITIALIZE BUFFERS AND COMPUTE NUMBER OF LINES
C   TO SKIP OVER IN DATA FRAME BUFFER BEFORE INPUTTING ACTUAL DATA.
C
          CALL FFPROC(HNSPOT,HDATEL,HNANC,HTOTLN,HTOTBF,HIRDAT,HIRANC,
     1                HDATOV,HANCOV,SYNCTR,NUMLIN)
C
C   FOR THE NOAA-K SERIES, THE FIRST FRAME DOES NOT ALWAYS (READ "USUALLY DOES NOT")
C   CONTAIN A FULL 38 LINES FOLLOWED BY 2 CALIBRATION LINES.  THE ORIGINAL ATOVS
C   CODE ASSUMED THIS WOULD BE THE CASE, AND AS A RESULT, WHAT PHYSICALLY APPEARED
C   AS A FRAME IN ATOVS DID NOT MATCH THE DATA STORED IN HIRDAT/HIRANC, AND CAUSED
C   EXTRA MISSING LINES OF DATA AND PROBLEMS COLLOCATING TO THE AMSU-A.
C
C   A PREVIOUS "FUDGE" IN INHIRS AND INAMA TO CORRECT FOR THIS SEEMED TO WORK, BUT
C   WAS LATER DETERMINED TO CAUSE PROBLEMS IN THE R1T FILE TIMES.
C
C   ONCE THE 2 CALIBRATION LINES ARE FOUND IN THE HIRS 1B*, THE REST OF THE ORBIT
C   FOLLOWS THE NORMAL 38 DATA LINES + 2 CAL. LINE PATTERN.
C
C   ARMED WITH THAT KNOWLEDGE, WE FIRST PRE-CALCULATE WHERE FIRST 2 CALIBRATION LINES
C   ARE - THIS IS WHERE THE FRAME MUST END. WE  FIND THE CALIBRATION LINES BY SCANNING
C   THE HIRS SCAN LINES FOR SCAN TYPE = 1 OR 3.  NOTE THAT THE FIRST SCAN LINE IN A HIRS
C   1B* IS NOT NECESSARILY ASSIGNED "SCAN LINE NUMBER 1", SO WE MUST TAKE THAT INTO
C   ACCOUNT.  WHEN THE FIRST 2 CALIBRATION LINES HAVE BEEN FOUND,
C   WE RESET THE READ POINTER BACK TO THE STARTING POINT SO THAT DATA INGEST CAN
C   BEGIN.  THEN, WE CALCULATE WHERE IN HIRDAT THIS DATA SHOULD GO, AND PAD THE
C   HIRRDAT BUFFER UP TO THAT POINT WITH MISSING VALUES.  AFTER THAT, SINCE AFTER
C   THE FIRST 2 CALIBRATION LINES THE REGULAR PATTERN OF 38 LINES + 2 CAL IS FOLLOWED
C   FOR THE REST OF THE ORBIT, SUBSEQUENT FRAME FALL INTO PLACE PROPERLY.
C
C   THUS, EVERY FRAME OF HIRS DATA (I.E. THE HIRDAT/HIRANC BUFFERS) LOOKS LIKE THE FOLLOWING:
C               LINES 1-9        NEVER USED.
C               LINES 10-47      38 LINES OF EARTH-LOCATED HIRS DATA
C               LINES 48-49      2 CALIBRATION LINES
C               LINE 50          NEVER USED.
C
C
C   SET THE 'FULL FRAME' FLAG TO .FALSE. IF DATA FROM OTHER THAN A METOP SATELLITE
C   ARE BEING PROCESSED IN ORDER TO AVOID DUPLICATE RETRIEVALS AT THE BEGINNING AND
C   END OF AN ORBIT.
C
          IF (.NOT.(METOP)) FULFRM = .FALSE.

          CALB1ST = 0
          CALCOUNT = 0
   35     CONTINUE
          SCNHIR = SCNHIR + 1
          CALL HRDGT1(SCNHIR,RETCOD)
          ERSTAT = RETCOD
          IF(ERSTAT .LT. 0) GO TO 900
          IF(ERSTAT .EQ. 1) GO TO 700
C
C  GET THE SCAN LINE NUMBER.
C
          ITEMID = 'SCAN_LINE_NUMBER               '
          SHAPE(0) = 1
          SHAPE(1) = 1
          LENGTH = 1
          CALL HRG1I2(ITEMID,INT2VL,LENGTH,SHAPE,RETCOD)
          ERSTAT = RETCOD
          IF (ERSTAT .NE. 0) GO TO 900
          SCNLIN = INT2VL
C
C  IF CALCOUNT=0, THIS IS THE FIRST SCAN LINE NUMBER, SO SAVE IT
C  THEN INCREMENT THE COUNTER.
C
          IF(CALCOUNT.EQ.0) SCAN1 = SCNLIN

          CALCOUNT = CALCOUNT + 1
C
C  GET SCAN LINE TIME IN YEAR, JULIAN DAY, AND MSEC OF DAY.
C
C  PUT THE TWO DIGIT INTEGER*4 YEAR IN SCANTI(1).
C
          SHAPE(0) = 1
          SHAPE(1) = 1
C                   12345678901234567890123456789012
          ITEMID = 'YEAR_OF_SCAN                    '
          LENGTH = 1
          CALL HRG1I2(ITEMID,INT2VL,LENGTH,SHAPE,RETCOD)
          ERSTAT = RETCOD
          IF (ERSTAT.NE.0) GO TO 900
          SCANTI(1) = INT2VL
          SCANTI(1) = MOD(SCANTI(1),100)
C
C  PUT THE DAY OF YEAR IN SCANTI(2).
C
          ITEMID = 'DAY_OF_YEAR_OF_SCAN             '
          CALL HRG1I2(ITEMID,INT2VL,LENGTH,SHAPE,RETCOD)
          ERSTAT = RETCOD
          IF (ERSTAT.NE.0) GO TO 900
          SCANTI(2) = INT2VL
C
C  PUT THE TIME OF DAY (IN MILLISECONDS) IN SCANTI(3).
C
          ITEMID = 'TIME_OF_DAY_OF_SCAN             '
          CALL HRG1I4(ITEMID,INT4VL,LENGTH,SHAPE,RETCOD)
          ERSTAT = RETCOD
          IF (ERSTAT.NE.0) GO TO 900
          SCANTI(3) = INT4VL
C
C CALL CENDAY TO CONVERT YEAR, MONTH, DAY TO CENTURY DAY
C
          CALL JULDAY(SCANTI(2),SCANTI(1),MONTH,DAY)

          CALL CENDAY(SCANTI(1),MONTH,DAY,LINTIM(1))
          LINTIM(2) = SCANTI(3)
C
C SAVE THE SCAN LINE START TIME OF THE FIRST SCAN
C
          IF (CALCOUNT.EQ.1) THEN
             SC1TIM(1) = LINTIM(1)
             SC1TIM(2) = LINTIM(2)
          END IF
C
C GET SCAN TYPE - 1 OR 3 MEANS IT'S A CALIBRATION LINE
C
          ITEMID = 'SCAN_TYPE                      '
          CALL HRG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
          ERSTAT = RETCOD
          IF (ERSTAT .NE. 0) GO TO 900
          ERTVIW = INT1VL
          IF (DBG1BSTR.EQ.1) PRINT *, 'SCAN LINE # ',SCNLIN,
     1                      ' TYPE ',ERTVIW,' COUNT ',CALCOUNT
          IF (ERTVIW.NE.0) THEN
C
             IF (DBG1BSTR.EQ.1) PRINT *,'PRE-DETERMINED THAT',
     1                  ' 1ST CALIBRATION LINE IS AT',
     2                  ' SCAN# ',SCNLIN,'(CALB1ST) &',
     3                  ' CALCOUNT = ',CALCOUNT,
     4                  ' & SYNCTR= ',SYNCTR,' & SCAN1 = ',
     5                    SCAN1
C
             CALB1ST = CALCOUNT
C
C DO A CHECK TO VERIFY THAT THE CALIBRATION LINES FOR THE FIRST
C DATA FRAME WERE NOT IN A DATA GAP.
C
             SCNDIF = HNRSCN(LINTIM,SC1TIM)
C
C IF THE FIRST CALIBRATION LINE FOUND IS MORE THAN 38 SCANS AFTER
C THE FIRST SCAN IN THE 1B*, CALCULATE WHERE THE FIRST CALIBRATION
C LINE SHOULD BE. SHIFT THE LOCATION OF THE FIRST CALIBRATION
C LINE BACK 40 SCANS.
C
             IF (SCNDIF.GT.38) THEN
                IF (DBG1BSTR.EQ.1) PRINT *,'THE FIRST CALIBRATION ',
     1             'LINE IS ',SCNDIF,' LINES AFTER THE FIRST SCAN ',
     2             'LINE.'
C
   37           CONTINUE
                SCNDIF = SCNDIF -40
C
C IT IS POSSIBLE THAT MORE THAN ONE SET OF CALIBRATION LINES
C WERE IN DATA GAPS.  KEEP ADJUSTING BACKWARD AS LONG AS THE
C CALCULATED DIFFERENCE BETWEEN THE FIRST SCAN AND THE FIRST
C CALIBRATION LINE EXCEEDS 38.
C
                IF (SCNDIF.GT.38) GO TO 37
                CALB1ST = SCNDIF
             END IF
             
             GOOD2CAL = CALB1ST + SYNCTR + 1
             IF (DBG1BSTR.EQ.1) PRINT *,'INHIRS GOOD2CAL= ',GOOD2CAL
C
C IF NO CALIBRATION LINE IS FOUND IN THE FIRST 39 SCANS IN THE FIRST
C DATA FRAME, NO SYNCHRONIZATION CAN BE DONE. SET THE RETURN CODE
C TO 99 AND RETURN TO THE CALLING ROUTINE.
C
             IF (GOOD2CAL.GT.40) THEN
                CALL INHIR$(308,ERSTAT)
                ERSTAT = 99
                RETURN
             END IF
          ENDIF
C
C  IF THE CALIBRATION LINE HAS NOT YET BEEN FOUND, READ THE NEXT SCAN LINE.
C
          IF(CALB1ST.EQ.0) GO TO 35
C
C  IF THE CALIBRATION LINE HAS BEEN FOUND, RESET THE READ POINTER TO THE
C  PLACE WHERE IT ORIGINALLY BEGAN.
C
          IF (DBG1BSTR.EQ.1) PRINT *,'RESET READ POINTER BACK ',
     1        CALCOUNT,' TIMES'
C
C  IF THE FIRST DATA FRAME IS A FULL DATA FRAME (38 DATA SCANS) SET THE
C  FULL FRAME FLAG TO TRUE.
C
          IF (CALCOUNT.EQ.39) FULFRM = .TRUE.
C
C  IF THE FIRST HIRS LINE IS A CALIBRATION LINE, BACKSPACE THE 1B* READ
C  POINTER ONE TIME. OTHERWISE, BACKSPACE THE POINTER TO READ IN THE
C  FIRST SCAN LINE FROM THE HIRS 1B* FILE
C
          IF (CALCOUNT.EQ.1) THEN
             SCNHIR = SCNHIR -1
          ELSE
             DO IC=1,CALCOUNT
                SCNHIR = SCNHIR -1
             END DO
          END IF
C
C  SET NEW NUMLIN.
C
C  IF FIRST LINE IS CALIBRATION LINE (TYPE 1), SET NUMLIN = 47, SO AT FIRST INCREMENT
C  OF NUMLIN BELOW, THE DATA IS STORED IN LINE 48&49 OF BUFFER (I.E. BASICALLY, THE
C  FIRST FRAME IS EMPTY IN THESE CASES). IF THE FIRST LINE IS A CALIBRATION LINE
C  (TYPE 3), SET NUMLIN = 48.
C
          IF(CALCOUNT.EQ.1) THEN
             IF(ERTVIW.EQ.1) NUMLIN = HTOTLN - 3
             IF(ERTVIW.EQ.3) NUMLIN = HTOTLN - 2
          ELSE
C
             NUMLIN = HTOTLN - GOOD2CAL - 1
          ENDIF
          SKIPHIR = NUMLIN + 1
C
C  SET THE NUMBER OF HIRS BUFFER LINES SKIPPED FOR USE BY THE SUBROUTINE
C  THAT SETS THE AMSU-A BUFFER LINE NUMBER WHERE THE FIRST AMSU-A
C  SCAN LINE DATA WILL BE PLACED.
C
          NSKIPPED = NUMLIN
C
          IF (DBG1BSTR.EQ.1) THEN
           PRINT *,'PUT 1ST HIRS DATA IN HIRDAT STARTING AT ',NUMLIN+1,
     1              ' & SKIPHIR = ',SKIPHIR
             PRINT *,'NSKIPPED FOR INAMA = ',NSKIPPED
          ENDIF
C
C    INITIALIZE SPOT CONFIDENCE VALUES TO ZERO.
C
          CALL MCLR(HIRANC(1,1,6),HNSPOT,HTOTLN)
          CALL MCLR(HANCOV(1,1,6),HNSPOT,HTOTBF)
C
C   INITIALIZE THE CHANNEL AVAILABILITY TO ZERO.
C
          CALL MCLR(HIRANC(1,1,7),HNSPOT,HTOTLN)
          CALL MCLR(HANCOV(1,1,7),HNSPOT,HTOTBF)
C
C    INITIALIZE CALIBRATION COEFFICIENT ARRAY AND ARRAYS IN /FRMINF/
C    FOR FIRST FRAME CASE.
C
          CALL CLRFIN(HGOODL,HQFLAG,HSCTIM,HCALCF,HTOTLN,HNCHAN)
C
C    CALCULATE VALUES OF THE SCAN LINE TIME IN THE OVERLAP BUFFER
C    BEFORE SYNC. TIME.
C
          DO 20, LINE = 1,HTOTBF-1
              CALL CMPTIM(SYNTIM,HIRTIM(3),LINE-HTOTBF,
     1                    HSCTIM(1,LINE))
  20      CONTINUE
C
C    PUT THE SYNC TIME IN THE FIRST LINE OF THE SCAN LINE TIME ARRAY.
C
          HSCTIM(1,HTOTBF) = SYNTIM(1)
          HSCTIM(2,HTOTBF) = SYNTIM(2)
C
C    IF NUMLIN GOES BEYOND THE EXTENDED FRAME, THEN ADJUST THE VALUE
C    OF NUMLIN TO CORRESPOND TO THE LAST LINE OF THE EXTENDED FRAME
C
          IF (NUMLIN .GT. HTOTLN) NUMLIN = HTOTLN
C
C    CALCULATE THE VALUES OF THE SCAN LINE TIMES FOR SYNC. DUMMY
C    LINES IF ANY EXIST.
C
          IF (SYNCTR .NE. 0) THEN
              DO 30, LINE = HTOTBF+1,NUMLIN
                  CALL CMPTIM(SYNTIM,HIRTIM(3),LINE-HTOTBF,
     1                        HSCTIM(1,LINE))
  30          CONTINUE
          END IF
C
C    CHECK TO SEE IF SYNC COUNTER EXTENDS BEYOND DATA FRAME. IF
C    SO, UPDATE VALUE OF SYNCTR AND SYNTIM AND NOTE IT IN THE
C    KIDS FILE.
C
          IF (NUMLIN .EQ. HTOTLN) THEN
              SYNCTR = SYNCTR - HTOTLN + HTOTBF
              SYNTIM(1) = HSCTIM(1,HTOTLN-HTOTBF)
              SYNTIM(2) = HSCTIM(2,HTOTLN-HTOTBF)
              CALL INHIR$(306,ERSTAT)
              RETURN
          ENDIF
C
          SYNCTR = 0
C
C======= END FIRST FRAME PROCESSING =================================
      ENDIF
C
C   LOOP OVER THE EXTENDED FRAME AND FILL THE BUFFERS WITH HIRS DATA.
C   CALL 1B* UTILITIES TO GET THE NEXT SEQUENTIAL SCAN LINE AND EXTRACT
C   THE DESIRED INFORMATION.
C
   40 CONTINUE
      SCNHIR = SCNHIR+1
      CALL HRDGT1(SCNHIR,RETCOD)
      ERSTAT = RETCOD
      IF(ERSTAT .LT. 0) GO TO 900
      IF(ERSTAT .EQ. 1) GO TO 700
C
C  GET THE SCAN LINE NUMBER.
C
      ITEMID = 'SCAN_LINE_NUMBER               '
      SHAPE(0) = 1
      SHAPE(1) = 1
      LENGTH = 1
      CALL HRG1I2(ITEMID,INT2VL,LENGTH,SHAPE,RETCOD)
      ERSTAT = RETCOD
      IF (ERSTAT .NE. 0) GO TO 900
      SCNLIN = INT2VL
C
C  GET SCAN LINE TIME IN YEAR, JULIAN DAY, AND MSEC OF DAY.
C
C  PUT THE TWO DIGIT INTEGER*4 YEAR IN SCANTI(1).
C
      SHAPE(0) = 1
      SHAPE(1) = 1
      ITEMID = 'YEAR_OF_SCAN                    '
      CALL HRG1I2(ITEMID,INT2VL,LENGTH,SHAPE,RETCOD)
      ERSTAT = RETCOD
      IF (ERSTAT .NE. 0) GO TO 900
      SCANTI(1) = INT2VL
      SCANTI(1) = MOD(SCANTI(1),100)
C
C  PUT THE DAY OF YEAR IN SCANTI(2).
C
      ITEMID = 'DAY_OF_YEAR_OF_SCAN             '
      CALL HRG1I2(ITEMID,INT2VL,LENGTH,SHAPE,RETCOD)
      ERSTAT = RETCOD
      IF (ERSTAT .NE. 0) GO TO 900
      SCANTI(2) = INT2VL
C
C  PUT THE TIME OF DAY (IN MILLISECONDS) IN SCANTI(3).
C
      ITEMID = 'TIME_OF_DAY_OF_SCAN             '
      CALL HRG1I4(ITEMID,INT4VL,LENGTH,SHAPE,RETCOD)
      ERSTAT = RETCOD
      IF (ERSTAT .NE. 0) GO TO 900
      SCANTI(3) = INT4VL
C

C   CALL CENDAY TO CONVERT YEAR, MONTH, DAY TO CENTURY DAY
C
      CALL JULDAY(SCANTI(2),SCANTI(1),MONTH,DAY)
      CALL CENDAY(SCANTI(1),MONTH,DAY,LINTIM(1))
      LINTIM(2) = SCANTI(3)
C
C   CHECK THE HIRS SCAN LINE TIME AGAINST THE PREVIOUS
C   SCAN LINE PROCESSED. CHECK SHOULD NOT BE PERFORMED IF SCAN
C   LINE IS THE FIRST SCAN OF 1B* DATA SET.
C
      IF (.NOT. FSTFRM) THEN
C
          TIMCOD = 0
          NUMMIS = 0
Ccft      SCNDIF = SCNLIN - PRVSCN
C
C  USE THE DIFFERENCE IN SCAN LINE START TIMES TO DETECT
C  DATA GAPS
C
          SCNDIF = HNRSCN(LINTIM,PRVTIM)
          SCANLN = SCNDIF + PRVSCN
C
C  IF SCAN LINES ARE OUT OF ORDER, WRITE A MESSAGE OUT TO
C  THE KIDS FILE AND CLEAR THE HIRS CONFIGURATION BIT.
C
          IF(SCNDIF.LT.0) THEN
             CALL INHIR$(305,ERSTAT)
             GO TO 1000
          END IF
          IF(SCNDIF.NE.1) THEN
             NUMMIS=SCNDIF-1
             TIMCOD = 1
             KMSG = 'HIRS data gap of     lines encountered'
             WRITE(KMSG(18:20),'(I3)') NUMMIS
             PRINT *, KMSG
             CALL INHIR$(307,ERSTAT)
          ENDIF
C
C   CHECK CODE FOR MISSING SCAN LINES.
C
          IF (TIMCOD .EQ. 1) THEN
C
C   LOOP OVER MISSING LINES IN THE BUFFER AND CALCULATE THE SCAN LINE
C   TIME FOR EACH LINE.
C
              DO 50, LINE = 1,NUMMIS
                  NUMLIN = NUMLIN +1
C
C   CALCULATE THE LINE TIME.
C
                  CALL CMPTIM(PRVTIM,HIRTIM(3),LINE,HSCTIM(1,NUMLIN))
C
C   IF NUMLIN IS GREATER THAN OR EQUAL TO HTOTLN, THE DATA GAP EXTENDS
C   INTO THE NEXT DATA FRAME.  CALCULATE PRVTIM AND PRVSCN AS THE SCAN
C   LINE TIME AND NUMBER WHICH SHOULD CORRESPOND TO THE LAST SCAN LINE
C   OF THE BUFFER, THEN BACKSPACE OVER THE LINE JUST READ SO THAT IT
C   WILL BE PROCESSED IN THE NEXT DATA FRAME.
C
                  IF (NUMLIN .GE. HTOTLN) THEN
                      PRVTIM(1) = HSCTIM(1,NUMLIN)
                      PRVTIM(2) = HSCTIM(2,NUMLIN)
                      PRVSCN = PRVSCN + LINE
                      SCNHIR = SCNHIR-1
                      IF (ERSTAT .NE. 0) GO TO 900
                      RETURN
                  END IF
  50          CONTINUE
C
          END IF
C
      ELSE
          FSTFRM = .FALSE.
      END IF
C
C    UPDATE THE LINE COUNTER FOR THE CURRENT LINE.
C
      NUMLIN = NUMLIN + 1
C
C  COMPUTE THE SCAN LINE CONFIDENCE BASED ON DO_NOT_USE_SCAN AND
C  TIME_SEQUENCE_ERROR FLAGS.
C
      CNFDNC = 100
      FLGCOD = 0
      ITEMID = 'DO_NOT_USE_SCAN                 '
      LENGTH = 1
      CALL HRG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
      ERSTAT = RETCOD
      IF (ERSTAT .LT. 0) GO TO 900
      DONOTUSE = INT1VL
      ITEMID = 'TIME_SEQUENCE_ERROR             '
      CALL HRG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
      ERSTAT = RETCOD
      IF (ERSTAT .LT. 0) GO TO 900
      SEQERR = INT1VL
      IF(DONOTUSE.EQ.1.OR.SEQERR.EQ.1) THEN
         CNFDNC=0
         FLGCOD=2
         BADHFLAG(1) = BADHFLAG(1) + 1
      ENDIF
C
      HSCTIM(1,NUMLIN) = LINTIM(1)
      HSCTIM(2,NUMLIN) = LINTIM(2)
C
      IF (FLGCOD.NE.2) THEN
C
C  CHECK THE EARTH VIEW OF THE SCAN LINE. IF EARTH VIEW IS 0, THEN SKIP
C  THE SCAN LINE.
C
          ITEMID = 'SCAN_TYPE                      '
          SHAPE(0) = 1
          SHAPE(1) = 1
          LENGTH = 1
          CALL HRG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
          ERSTAT = RETCOD
          IF (ERSTAT .NE. 0) GO TO 900
          ERTVIW = INT1VL
C
          IF (ERTVIW .EQ. 0) THEN
C
C  EXTRACT THE CALIBRATION COEFFICIENTS.
C
              ITEMID =  'PRIMARY_CAL_COEFS               '
              SHAPE(0) = 2
              SHAPE(1) = 3
              SHAPE(2) = HNCHAN
              LENGTH = 3*HNCHAN
              CALL HRG1R4(ITEMID,HCALC4(1,1,NUMLIN),LENGTH,SHAPE,
     1                RETCOD)
              ERSTAT = RETCOD
              IF (ERSTAT .NE. 0) GO TO 900
C
C-- CONVERT THE PRIMARY CALIBRATION COEFFICIENTS FROM R*4 TO R*8
C
              DO ICHAN = 1, HNCHAN
                 HCALCF(1,ICHAN,NUMLIN) = HCALC4(1,ICHAN,NUMLIN)
                 HCALCF(2,ICHAN,NUMLIN) = HCALC4(2,ICHAN,NUMLIN)
                 HCALCF(3,ICHAN,NUMLIN) = HCALC4(3,ICHAN,NUMLIN)
              END DO
C
C   CALL HIRLIN TO FILL THE BUFFERS WITH THE SCAN LINE DATA.
C
              CALL HIRLIN(HCALCF(1,1,NUMLIN),NUMLIN,CNFDNC,
     1              HDATOV,HANCOV,HIRDAT,HIRANC,ERSTAT,OBSHIR,
     2              N16HIR)
              IF (ERSTAT .NE. 0) GO TO 800
C
              IF (DBG1BSTR.EQ.1)
     1           PRINT *,'1BSTAR: SCAN LINE/NUMLIN/LINTIM ',SCNLIN,
     2                   ' / ',NUMLIN,' / ',LINTIM,
     3                   ' SCAN TYPE ',ERTVIW,' LAT/LON: ',
     4                   HIRANC(1,NUMLIN,1),HIRANC(1,NUMLIN,2)
C
C   LINE CONTAINS USABLE DATA. PROCEED WITH NORMAL PROCESSING.
C
              HGOODL(NUMLIN) = .TRUE.
          ELSE
              HNERVW = HNERVW + 1
C
              IF (DBG1BSTR.EQ.1) THEN
                 PRINT *,'1BSTAR: SCAN LINE/NUMLIN/LINTIM ',SCNLIN,
     1                 ' / ',NUMLIN,' / ',LINTIM,' SCAN TYPE ',ERTVIW
                 CALL FLUSH_(101)
              ENDIF
          ENDIF

      ELSE
          HFLQCK = HFLQCK + 1
      ENDIF
C
C   SAVE CURRENT SCAN LINE TIME AND COUNTER AS PREVIOUS LINE TIME AND
C   COUNTER FOR PROCESSING NEXT LINE.
C
      PRVTIM(1) = LINTIM(1)
      PRVTIM(2) = LINTIM(2)
Ccft  PRVSCN = SCNLIN
      PRVSCN = SCANLN
C
C   CHECK TO SEE IF END OF DATA FRAME HAS BEEN REACHED.  IF END OF
C   FRAME HAS NOT BEEN REACHED, THEN CONTINUE PROCESSING FRAME.
C   NOTE THAT LINE 50 OF THE HIRDAT/HIRANC IS IGNORED, SO STOP
C   AT HTOTLN-1 = 49.  THIS ENSURES THE CALIBRATION LINES (48 & 49)
C   END THE FRAME.
C
      IF (NUMLIN .LT. HTOTLN-1) GO TO 40

      RETURN
C
C   1B* FILE END OF FILE CONDITION.
C
  700 CALL INHIR$(302,ERSTAT)
C
C   SET 'FULL FRAME' FLAG TO .FALSE. FOR THE PARTIAL FRAME AT THE
C   END OF AN ORBIT.
C
      FULFRM = .FALSE.

      GOTO 1000
C
C   ERROR IN A CALLED ROUTINE IN HIRLIN.
C
  800 CALL INHIR$(303,ERSTAT)
      GOTO 1000
C
C   ERROR IN A CALLED ROUTINE IN INHIRS.
C
  900 CALL INHIR$(304,ERSTAT)
C
C    IBCLR TREATS THE BITS OF A 4-BYTE WORD AS BEING ORDERED
C    RIGHT TO LEFT FROM 0 TO 31.  FOR OUR PURPOSES IN FRMCNF
C    AND CONFIG, WE CONSIDER BITS AS BEING ORDERED FROM LEFT
C    TO RIGHT FROM 1 TO 32.  THUS TO SET WHAT WE CONSIDER TO
C    BE THE CORRECT BIT CORRESPONDING TO INSTRUMENT 1 (HIRS),
C    THE BIT SET WITH IBCLR MUST BE 32 -1.  (BIT 1 CORRESPONDS
C    TO BIT 31, 2 TO 30, 3 TO 29 ...)
C
 1000 BSHFT = 32 - INSTRU
      CONFIG = IBCLR(CONFIG,BSHFT)
      RETURN
C
      END
C
C*********************************************************************
      SUBROUTINE INHIR$(MSGCOD,ERSTAT)
C
C*********************************************************************
C   NAME- INHIR$
C
C
C   LANGUAGE- FORTRAN       TYPE- SUBROUTINE        COMPUTER- CRAY
C
C   VERSION- 2.0    DATE- 07/27/95  PROGRAMMER- DAVE MOREL (SMSRC)
C   VERSION- 3.0    DATE- 07/09/97  PROGRAMMER- LEAH WILSON (HSTX)
C   VERSION- 3.1    DATE- 01/13/00  PROGRAMMER- LEAH WILSON (RAYTHEON)
C   VERSION- 3.2    DATE- 06/14/00  PROGRAMMER- LEAH WILSON (RAYTHEON)
C   VERSION- 3.3    DATE- 12/10/01  PROGRAMMER- FRANK TILLEY( RAYTHEON)
C   VERSION- 3.4    DATE- 01/14/02  PROGRAMMER- FRANK TILLEY( RAYTHEON)
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE INHIRS.
C   VERSION- 3.1 ADD MESSAGE 305, REMOVE 301.
C   VERSION- 3.2 ADD MESSAGE 306.
C   VERSION- 3.3 CHANGE MESSAGE 305 FROM A WARNING TO A FATAL ERROR.
C                ADDED MESSAGE 307 FOR DATA GAP (PR #52)
C   VERSION- 3.4 CHANGE 307 FROM FATAL TO A WARNING (PR #56)
C
C   FILES-
C     DSN                       I/O UNIT  FUNCTIONAL DESCRIPTION
C     ---                       --- ----  -----------------------
C     ...KIDS                    O  KUNIT KRAY INTERACTIVE DOCUMENTATION
C                                         FILE.
C
C   PROGRAM CONSTANTS-
      INCLUDE 'ERPARM'
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE               TYPE  I/O  DESCRIPTION
C     --------               ----  ---  -----------
C     MSGCOD                  INT   I   MESSAGE CODE USED TO
C                                       IDENTIFY THE ERROR PATTERN.
C     ERSTAT                  INT   O   STATUS CODE IF AN ERROR
C                                       OCCURS DURING I/O.
C
      INTEGER   MSGCOD,ERSTAT
C
C   COMMON AREA PARAMETERS-
C
      INCLUDE  'SPGUNT'
      INCLUDE  'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE                TYPE    DESCRIPTION
C     --------                ----    -----------
C     KMSG                   CH*100   KIDS MESSAGE BUFFER
C     C4TEMP                  CH*4    HOLDING BUFFER FOR WRITING TO KIDS
C
C   SUBPROGRAMS CALLED- KIDS
C
C   FUNCTIONS USED- NONE
C
C   EXIT STATES-
C       ERSTAT =  0; INDICATING NO OR NON-FATAL ERROR FOUND.
C       ERSTAT = 99; INDICATING A FATAL ERROR WAS DETECTED.
C
C   ASSUMPTIONS - THIS ROUTINE ASSUMES KIDS HAS BEEN CALLED PREVIOUSLY TO
C                 OPEN THE KIDS FILE.
C
C*********************************************************************
C
      CHARACTER*4   C4TEMP
      CHARACTER*100 KMSG
C
C  SET ERINFO:SUBNAM TO MAIN MODULE NAME.  NOTE THAT THE KIDS SUBROUTINE
C  DETERMINES THE APPROPRIATE SUBROUTINE NAME.
C
      SUBNAM = 'SPGDVR'
C
C  CHECK FOR END OF FILE ERROR.
C
      IF (MSGCOD .EQ. 302) THEN
        KMSG= 'HIR End of File reached, HIRS shut down'
        CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
        ERSTAT=0
C
C  CHECK FOR ERROR IN HIRLIN.
C
      ELSEIF (MSGCOD .EQ. 303) THEN
        KMSG= '* FATAL ERROR * 1b* utility error in HIRLIN'
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        ERSTAT=99
C
C  CHECK FOR ERROR IN INHIRS.
C
      ELSEIF (MSGCOD .EQ. 304) THEN
        KMSG= '* FATAL ERROR * 1b* utility error in INHIRS'
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        ERSTAT=99
C
C  FOUND AN OUT-OF-ORDER SCAN LINE
C
      ELSEIF (MSGCOD .EQ. 305) THEN
        KMSG= '* FATAL ERROR * HIRS Scan line      is out of order'
        WRITE(C4TEMP,'(I4)') SCNLIN
        KMSG(32:35) = C4TEMP
        PRINT *, KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        ERSTAT=0
C
C  FOUND A SYNC COUNTER THAT EXTENDS BEYOND THE DATA FRAME.
C
      ELSEIF (MSGCOD .EQ. 306) THEN
        KMSG= '* NOTE * Sync counter extends beyond data frame.'
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        ERSTAT=0
C
C  FOUND A DATA GAP IN THE HIRS DATA
C
      ELSEIF (MSGCOD .EQ. 307) THEN
        KMSG= '* WARNING *  Data Gap found in HIRS data '
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
        ERSTAT = 0
C
C  NO CALIBRATION LINE FOUND IN FIRST 39 HIRS SCANS
C
      ELSEIF (MSGCOD .EQ. 308) THEN
        KMSG= '* WARNING *  No calibration line in first HIRS data frame.'
        PRINT *,KMSG
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
        KMSG= '             Synchronization is impossible. Shutting down.'
        PRINT *,KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        ERSTAT = 0
C
C NOT A VALID MSGCOD
C
      ELSE
        KMSG = ' Invalid error message code = '
        WRITE(C4TEMP,'(I4)') MSGCOD
        KMSG(31:34) = C4TEMP
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
        ERSTAT=0
      ENDIF
      RETURN
      END

