      SUBROUTINE INAMB(FSTFRM,SYNCTR,SYNTIM,BCALCF,AMBDAT,AMBANC,
     1                 LINAMA,TIMAMA,SCNAMB,ERSTAT)
      IMPLICIT NONE
C***********************************************************************
C    NAME- INAMB
C
C    LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE   COMPUTER- IBM SP
C
C    VERSION- 1.0  DATE-  1/15/04   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 1.1  DATE- 12/02/04   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C             ADDED SCNAMB TO CALLING ARGUMENTS AND CHANGED VARIABLES
C             FOR CALLS TO 1B* UTILITIES.
C    VERSION- 1.2  DATE- 06/27/06   PROGRAMMER- FRANK TILLEY (IMSG)
C             REPLACED GOOD2CAL WITH LINAMA AND TIMAMA IN CALLING
C             ARGUMENTS FOR SYCHRONIZING THE AMSU-B/MHS WITH THE
C             AMSU-A FOR THE FIRST FRAME.
C    VERSION- 1.3  DATE- 11/13/06   PROGRAMMER- FRANK TILLEY (IMSG)
C             WHEN OUT-OF-ORDER SCANS ARE DETECTED THE INSTRUMENT
C             BIT IS CLEARED IN INSTRUMENT CONFIGURATION INSTEAD OF
C             RETURNING A FATAL ERROR CODE.
C
C    FUNCTION-  THIS SUBROUTINE BUILDS AN EXTENDED DATA FRAME OF AMSU-B
C               OR MHS 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 AMSU-B/MHS
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 ARE FILLED IN THE BUFFERS AS MISSING
C               VALUES (R4SPV).
C                 
C
C   FILES- DSN                        UNIT   I/O   REMARKS
C          ------------------------    ----   ---  ----------------
C          .../AMSUB1BS                BUNIT   I   AMSU-B/MHS 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   2D ARRAY OF THE INSTRUMENT SYNC. TIME
C                           (CENTURY DAY X MILLISECOND OF DAY)
C      BCALCF    REAL I/O   3D ARRAY (3 X INPARM:BNCHAN X INSPARM:
C                           BTOTLN) OF CALIBRATION COEFFICIENTS
C                           FOR EACH CHANNEL FOR EACH LINE IN THE
C                           EXTENDED DATA FRAME.
C      AMBDAT    REAL   O   3D BUFFER (INSPARM:BNSPOT X INSPARM:
C                           BTOTLN X INSPARM:BDATEL) CONTAINING AMSU-B/MHS
C                           DATA.
C      AMBANC    REAL   O   3D BUFFER (INSPARM:BNSPOT X INSPARM:
C                           BTOTLN X INSPARM:BNANC) CONTAINING AMSU-B/MHS
C                           "ANCILLARY" DATA SUCH AS:
C                           PARM1 = LAT, PARM2 = LON, PARM3 = SOLAR
C                           ZENITH ANGLE, PARM4 = SATELLITE ZENITH
C                           ANGLE, ETC.
C      LINAMA    INT    I   FOR THE FIRST FRAME ONLY, THE LINE NUMBER WHERE
C                           THE FIRST AMSU-A DATA WERE PUT IN THE AMSU-A
C                           FRAME BUFFERS.
C      TIMAMA    INT    I   FOR THE FIRST FRAME ONLY, THE CENTURY DAY AND
C                           MILLISECOND OF DAY FOR THE FIRST AMSU-A
C                           SCAN LINE PUT IN THE AMSU-A FRAME BUFFERS.
C      SCNAMB    I*4   I/O  AMSU-B/MHS 1B* SCAN NUMBER
C      ERSTAT    INT    O   RETURN CODE INDICATING IF AN ERROR OCCURRED
C
C   COMMON AREA PARAMETERS-
      INCLUDE'SPGUNT'
      INCLUDE'INSPARM'
      INCLUDE'INPARM'
      INCLUDE'INPCDB'
      INCLUDE'COLLOC'
      INCLUDE'ERPARM'
      INCLUDE'ERINFO'
      INCLUDE'FRMINF'
      INCLUDE'MONSTA'
      INCLUDE'SPVAL'
      INCLUDE'DEBUG'
      INCLUDE'FRAME1'
C
C
C    LOCAL VARIABLES-
C      VARIABLE      TYPE   DESCRIPTION
C      --------      ----   -----------
C      BDATOV        REAL   3D BUFFER (INSPARM:BNSPOT X INPARM:
C                           BTOTBF X INSPARM:BDATEL) CONTAINING
C                           "64-SEC OVERLAP" DATA.
C      BANCOV        REAL   3D BUFFER (INSPARM:BNSPOT X INPARM:
C                           BTOTBF X INSPARM:BNANC) CONTAINING
C                           "64-SEC OVERLAP" PARAMETERS.
C      BCALC4        R*4    FOUR-BYTE VERSION OF BCALCF.
C      BSHFT         INT    USED TO SHIFT BITS IN CONFIG.
C      CNFDNC        REAL   SCAN LINE CONFIDENCE (100 HAVING TOTAL
C                           CONFIDENCE, 0 HAVING NO CONFIDENCE).
C      DAY           INT    DAY OF THE MONTH.
C      DONOTUSE      INT    HOLDS DO_NOT_USE_SCAN FLAG.
C      FLGCOD        INT    QUALITY CODE FOR SCAN LINE OBTAINED FROM
C                           CHECKING FLAGS, 0=USABLE, 1=SUSPECT,
C                           2=UNUSABLE.
C      I, LINE       INT    DO LOOP COUNTERS.
C      ICHAN         INT    AMSU-B CHANNEL NUMBER.
C      INSTRU        INT    INSTRUMENT FOR WHICH THE CONFIDENCE & USABILITY
C                           ARE COMPUTED (1=HIRS, 2=AMSU-A, 3=AMSU-B).
C      INT1VL        I*1    USED TO EXTRACT I*1 VALUES FROM THE 1B*.
C      INT2VL        I*2    USED TO EXTRACT I*2 VALUES FROM THE 1B*.
C      INT4VL        I*4    USED TO EXTRACT I*4 VALUES FROM THE 1B*.
C      ITEMID       CH*32   NAME OF THE ITEM THAT IS TO BE EXTRACTED
C                           FROM THE AMSU-A 1B* SCAN LINE.
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      MOVHTIM       INT    NUMBER OF SECONDS MOVED IN NHLINMOV LINES.
C      NHLINMOV      INT    NUMBER OF HIRS LINES MOVED.
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      RDUM          REAL   DUMMY FOR HOLDING REAL VALUES.
C      SHAPE         I*4    SHAPE ARRAY, DIMENSIONED (0:7), USED IN CALLS
C                           TO THE 1B* 'GET' ROUTINES.
C      TIMCOD        INT    QUALITY CODE RETURNED FROM CHECKING THE SCAN
C                           LINE TIME.
C      SCNDIF        INT    SCAN LINE NUMBER DIFFERENCE BETWEEN CURRENT
C                           AND PREVIOUS SCAN.
C
C    SUBPROGRAMS CALLED- CENDAY,CLRFIN,CMPTIM,FFPROC,
C                        ABG1I2,ABG1I4,ABG1R4,ABDGT1,INIFIN,
C                        INIFRM,JULDAY,MCLR,AMBLIN,INAMB$
C
C    FUNCTIONS USED- IBCLR, MOD, BNRSCN
C
      INTEGER BNRSCN
C
C    EXIT STATES- ERSTAT=99 FOR A FATAL ERROR
C                 ERSTAT=0 FOR A NON-FATAL OR NO ERROR
C
C***********************************************************************
C
      LOGICAL      FSTFRM
      INTEGER      ERSTAT,SYNCTR,SYNTIM(2),LINAMA,TIMAMA(2)
      INTEGER*4    SCNAMB
      REAL   AMBDAT(BNSPOT,BTOTLN,BDATEL),AMBANC(BNSPOT,BTOTLN,BNANC)
      REAL   BDATOV(BNSPOT,BTOTBF,BDATEL),ABNCOV(BNSPOT,BTOTBF,BNANC)
      REAL   BCALCF(3,BNCHAN,BTOTLN)
      REAL*4 BCALC4(3,BNCHAN,BTOTLN)
C
      CHARACTER*32 ITEMID
      INTEGER*1    INT1VL
      INTEGER*2    INT2VL
      INTEGER      SCNDIF,DONOTUSE, ICHAN
      INTEGER      TIMDIF, DIFSCN
      INTEGER      DAY,FLGCOD,I,LINE,LINTIM(2),NUMMIS,INSTRU
      INTEGER      MONTH,NUMLIN,PRVSCN,PRVTIM(2),TIMCOD
      INTEGER*4    SHAPE(0:7), LENGTH, INT4VL, RETCOD
      INTEGER      BSHFT
      REAL         CNFDNC,RDUM
      LOGICAL      SYNCAMA
C
      DATA PRVSCN/0/
      DATA SYNCAMA/.TRUE./
C
      SAVE PRVTIM, PRVSCN, BDATOV, ABNCOV, SYNCAMA
C
C  INITIALIZE ALL ELEMENTS OF THE SHAPE ARRAY TO 0. RETURN CODE TO 0.
C
      DO I = 1, 8
          SHAPE(I-1) = 0
      END DO
      ERSTAT = 0
      INSTRU = 3
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(BDATOV,ABNCOV,BTOTBF,BNSPOT,BDATEL,BNANC,AMBDAT,
     1                AMBANC,BTOTLN)
C
C   SET LINE NUMBER EQUAL TO BTOTBF SINCE THIS IS NUMBER OF LINES
C   WRITTEN TO BUFFER BY INIFRM.
C
          NUMLIN = BTOTBF
C
C   INITIALIZE THE SCAN LINE CONFIDENCE TO ZERO.
C
          CALL MCLR(AMBANC(1,NUMLIN+1,5),BNSPOT,BTOTLN-NUMLIN)
          CALL MCLR(ABNCOV(1,1,5),BNSPOT,BTOTBF)
C
C   INITIALIZE THE CHANNEL AVAILABILITY TO ZERO.
C
          CALL MCLR(AMBANC(1,NUMLIN+1,6),BNSPOT,BTOTLN-NUMLIN)
          CALL MCLR(ABNCOV(1,1,6),BNSPOT,BTOTBF)
C
C   INITIALIZE THE CALIBRATION COEFFICIENT ARRAY AND ARRAYS IN /FRMINF/
C
          CALL INIFIN(BGOODL,BQFLAG,BSCTIM,BCALCF,BTOTLN,BTOTBF,BNCHAN)
C
      ELSE
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 INPUTING ACTUAL DATA.
C
          CALL FFPROC(BNSPOT,BDATEL,BNANC,BTOTLN,BTOTBF,AMBDAT,AMBANC,
     1                BDATOV,ABNCOV,SYNCTR,NUMLIN)
C
C    ADJUST NUMLIN TO CORRESPOND TO THE LINE WHERE THE FIRST AMSU-A
C    DATA WERE PLACED IN THE FRAME BUFFERS. NUMLIN MAY BE ADJUSTED
C    AGAIN BASED ON THE RESPECTIVE SCAN LINE START TIMES OF THE
C    AMSU-A AND AMSU-B/MHS DATA.
C
          IF(DBG1BSTR.EQ.1)PRINT *,'INAMB-frame1 orig NUMLIN=',NUMLIN
          
          IF (LINAMA.GT.0) THEN
             NUMLIN = LINAMA*3
          END IF
          SKIPAMB = NUMLIN + 1
          IF(DBG1BSTR.EQ.1) THEN
             PRINT *,'1BSTAR - LINAMA & TIMAMA & SKIPAMB & new NUMLIN = ',
     1               LINAMA, TIMAMA(1), TIMAMA(2), SKIPAMB, NUMLIN
          END IF
C
C    INITIALIZE SPOT CONFIDENCE VALUES TO ZERO.
C
          CALL MCLR(AMBANC(1,1,5),BNSPOT,BTOTLN)
          CALL MCLR(ABNCOV(1,1,5),BNSPOT,BTOTBF)
C
C   INITIALIZE THE CHANNEL AVAILABILITY TO ZERO.
C
          CALL MCLR(AMBANC(1,1,6),BNSPOT,BTOTLN)
          CALL MCLR(ABNCOV(1,1,6),BNSPOT,BTOTBF)
C
C    INITIALIZE CALIBRATION COEFFICIENT ARRAY AND ARRAYS IN /FRMINF/
C    FOR FIRST FRAME CASE.
C
          CALL CLRFIN(BGOODL,BQFLAG,BSCTIM,BCALCF,BTOTLN,BNCHAN)
C
C    CALCULATE VALUES OF THE SCAN LINE TIME IN THE OVERLAP BUFFER
C    BEFORE SYNC. TIME.
C
          DO LINE = 1,BTOTBF/2
              CALL CMPTIM(SYNTIM,AMBTIM(3),LINE-BTOTBF/2-1,
     1                    BSCTIM(1,LINE))
          END DO
C
C    PUT THE SYNC TIME IN THE SCAN LINE TIME ARRAY.
C
          BSCTIM(1,BTOTBF/2+1) = SYNTIM(1)
          BSCTIM(2,BTOTBF/2+1) = 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. BTOTLN) NUMLIN = BTOTLN
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 LINE = BTOTBF/2+2,NUMLIN
                  CALL CMPTIM(SYNTIM,AMBTIM(3),LINE-BTOTBF/2-1,
     1                        BSCTIM(1,LINE))
              END DO
          END IF
C
C    CHECK TO SEE IF SYNC COUNTER EXTENDS BEYOND DATA FRAME. IF
C    SO, UPDATE VALUE OF SYNCTR AND SYNTIM AND RETURN.
C
          IF (NUMLIN .EQ. BTOTLN) THEN
              SYNCTR = SYNCTR - BTOTLN + BTOTBF
              SYNTIM(1) = BSCTIM(1,BTOTLN-BTOTBF/2+1)
              SYNTIM(2) = BSCTIM(2,BTOTLN-BTOTBF/2+1)
C
              RETURN
          ELSE
              SYNCTR = 0
          ENDIF
      ENDIF
C
C   LOOP OVER THE EXTENDED FRAME AND FILL THE BUFFERS WITH AMSU-B DATA.
C   CALL 1B* UTILITIES TO GET THE NEXT SEQUENTIAL SCAN LINE AND EXTRACT
C   THE DESIRED INFORMATION.
C
   40 CONTINUE
      SCNAMB = SCNAMB+1
      CALL ABDGT1(SCNAMB,RETCOD)
      ERSTAT = RETCOD
      IF(ERSTAT .EQ. 1) GO TO 700
      IF(ERSTAT .LT. 0) GO TO 900
C
C  GET THE SCAN LINE NUMBER.
C
      ITEMID = 'SCAN_LINE_NUMBER               '
      SHAPE(0) = 1
      SHAPE(1) = 1
      LENGTH = 1
      CALL ABG1I2(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
      ITEMID = 'YEAR_OF_SCAN                    '
      CALL ABG1I2(ITEMID,INT2VL,LENGTH,SHAPE,RETCOD)
      ERSTAT = RETCOD
      IF (ERSTAT .LT. 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 ABG1I2(ITEMID,INT2VL,LENGTH,SHAPE,RETCOD)
      ERSTAT = RETCOD
      IF (ERSTAT .LT. 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 ABG1I4(ITEMID,INT4VL,LENGTH,SHAPE,RETCOD)
      ERSTAT = RETCOD
      IF (ERSTAT .LT. 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   FOR THE FIRST FRAME ONLY, MAKE ANY NECESSARY ADJUSTMENTS
C   TO NUMLIN TO SYNCHRONIZE THE AMSU-B/MHS DATA WITH THE
C   AMSU-A.
C
      IF (SYNCAMA) THEN
         TIMDIF = TIMAMA(2) - LINTIM(2)
         IF (TIMDIF.GT.0) THEN
            DIFSCN = TIMDIF / AMBTIM(3)
            NUMLIN = NUMLIN - DIFSCN
            IF (DBG1BSTR.EQ.1) THEN
               PRINT *,'INAMB: TIMDIF, DIFSCN, NUMLIN = ',
     +                 TIMDIF, DIFSCN, NUMLIN
            END IF
         END IF
         SYNCAMA = .FALSE.
      END IF
      IF (NUMLIN.LT.1) THEN
         CALL INAMB$(307,ERSTAT)
         PRINT *,'IN INAMB - NUMLIN = ',NUMLIN
         GOTO 1000
      END IF
C
C   CHECK FOR ANY MISSING SCANS.  CHECK SHOULD NOT BE PERFORMED ON THE
C   FIRST SCAN OF THE 1B* DATA SET.
C
      IF(PRVSCN.GT.0) THEN
         TIMCOD = 0
         NUMMIS = 0
         SCNDIF = BNRSCN(LINTIM,PRVTIM)
C
C   IF OUT-OF-ORDER SCANS ARE DETECTED WRITE OUT A MESSAGE TO THE KIDS
C   FILE AND CLEAR THE AMSU-B/MHS BIT FROM THE INSTRUMENT CONFIGURATION
C   WORD.
C
         IF(SCNDIF.LT.0) THEN
            CALL INAMB$(305,ERSTAT)
            PRINT *,'Time of current line ',LINTIM(2),' earlier than time of ',
     +               'previous line ',PRVTIM(2)
            GOTO 1000
         ENDIF
         IF(SCNDIF.NE.1) THEN
            NUMMIS=SCNDIF-1
            TIMCOD = 1
            CALL INAMB$(306,ERSTAT)
         ENDIF
         IF (TIMCOD.NE.0) then
            PRINT *, '1BSTAR: INAMB FOUND ',NUMMIS,' MISSING SCAN ',
     1               ' LINES BETWEEN ',PRVSCN,' AND ',SCNLIN
         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,AMBTIM(3),LINE,BSCTIM(1,NUMLIN))
C
C   IF NUMLIN IS GREATER THAN OR EQUAL TO BTOTLN, 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. BTOTLN) THEN
                     PRVTIM(1) = BSCTIM(1,NUMLIN)
                     PRVTIM(2) = BSCTIM(2,NUMLIN)
                     PRVSCN = PRVSCN + LINE
                     SCNAMB = SCNAMB -1
                     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 FLAG.
C
      CNFDNC = 100.0
      FLGCOD = 0
      ITEMID = 'DO_NOT_USE_SCAN                 '
      CALL ABG1I1(ITEMID,INT1VL,LENGTH,SHAPE,RETCOD)
      ERSTAT = RETCOD
      IF (ERSTAT .LT. 0) GO TO 900
      DONOTUSE = INT1VL
      IF(DONOTUSE.EQ.1) THEN
         CNFDNC=0
         FLGCOD=2
         BADMFLAG(1) = BADMFLAG(1) + 1
c        print *,'DO_NOT_USE_SCAN flag set for MHS at ',LINTIM(1),LINTIM(2),
c    +           ' scan ',SCNLIN
      ENDIF
      
C
      BSCTIM(1,NUMLIN) = LINTIM(1)
      BSCTIM(2,NUMLIN) = LINTIM(2)
C
      IF (FLGCOD.NE.2) THEN
C
C  EXTRACT THE CALIBRATION COEFFICIENTS.
C
         ITEMID = 'PRI_CAL_COEFFS                  '
         SHAPE(0) = 2
         SHAPE(1) = 3
         SHAPE(2) = BNCHAN
         LENGTH = 3*BNCHAN
         CALL ABG1R4(ITEMID,BCALC4(1,1,NUMLIN),LENGTH,SHAPE,
     1               RETCOD)
         ERSTAT = RETCOD
         IF (ERSTAT .LT. 0) GO TO 900
C
C  CONVERT THE CALIBRATION COEFFICIENTS FROM R*4 TO R*8.
C
         DO ICHAN = 1, BNCHAN
            BCALCF(1,ICHAN,NUMLIN) = BCALC4(1,ICHAN,NUMLIN)
            BCALCF(2,ICHAN,NUMLIN) = BCALC4(2,ICHAN,NUMLIN)
            BCALCF(3,ICHAN,NUMLIN) = BCALC4(3,ICHAN,NUMLIN)
         END DO
C
C   CALL AMBLIN TO FILL THE BUFFERS WITH THE SCAN LINE DATA.
C
         CALL AMBLIN(BCALCF(1,1,NUMLIN),NUMLIN,CNFDNC,
     1               BDATOV,ABNCOV,AMBDAT,AMBANC,ERSTAT)
         IF (ERSTAT .LT. 0) GO TO 800
         IF (DBG1BSTR.EQ.1) THEN
            PRINT *, '1BSTAR: SCAN LINE/NUMLIN/LINTIM ',SCNLIN,
     1               ' / ',NUMLIN,' / ',LINTIM,' lat/lon: ',
     2               AMBANC(1,NUMLIN,1),AMBANC(1,NUMLIN,2)
         ENDIF
C
C   LINE CONTAINS USABLE DATA.  PROCEED WITH NORMAL PROCESSING.
C
         BGOODL(NUMLIN) = .TRUE.
      ELSE
         BFLQCK = BFLQCK + 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)
      PRVSCN = SCNLIN
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
      IF (NUMLIN .LT. BTOTLN) GO TO 40
      RETURN
C
C   END OF FILE FOUND ON THE AMSU-B 1B*.
C
  700 CALL INAMB$(302,ERSTAT)
      GO TO 1000
C
C   ERROR IN A CALLED ROUTINE IN AMBLIN.
C
  800 CALL INAMB$(303,ERSTAT)
      GO TO 1000
C
C   ERROR IN A 1B* UTILITY.
C
  900 CALL INAMB$(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 3 (AMSU/A),
C    THE BIT SET WITH IBCLR MUST BE 32 -4.  (BIT 1 CORRESPONDS
C    TO BIT 31, 2 TO 30, 3 TO 29 ...), (THE GAC IS BIT 29,
C    AND AMSU-B IS BIT 28, BUT INSTRU FOR AMSU-B IS 3).
C
 1000 BSHFT = 28
      CONFIG = IBCLR(CONFIG,BSHFT)
      RETURN
C
      END
C
C*********************************************************************
C
      SUBROUTINE INAMB$(MSGCOD,ERSTAT)
      IMPLICIT NONE
C
C*********************************************************************
C   NAME- INAMB$
C
C   LANGUAGE- FORTRAN       TYPE- SUBROUTINE        COMPUTER- IBM SP
C
C   VERSION- 1.0    DATE- 01/15/04  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE INAMB.
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'
      INCLUDE  'HIRUSE'
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= 'MHS End of file reached -  MHS shut down'
        PRINT *,KMSG
        CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
        ERSTAT=0
C
C  CHECK FOR ERROR IN AMBLIN.
C
      ELSEIF (MSGCOD .EQ. 303) THEN
        KMSG= '* FATAL ERROR * 1b* utility error in AMBLIN'
        PRINT *,KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        ERSTAT=99
C
C  CHECK FOR ERROR IN INAMB.
C
      ELSEIF (MSGCOD .EQ. 304) THEN
        KMSG='MHS Probable End of File reached - MHS shut down.'
        PRINT *,KMSG
        CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
        ERSTAT=0
C
C  FOUND AN OUT-OF-ORDER SCAN LINE
C
      ELSEIF (MSGCOD .EQ. 305) THEN
        KMSG= '* WARNING * MHS Scan line      is out of order. MHS Shut Down.'
        WRITE(C4TEMP,'(I4)') SCNLIN
        KMSG(27:30) = C4TEMP
        PRINT *, KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        ERSTAT=0
C
C  FOUND A DATA GAP
C
      ELSEIF (MSGCOD .EQ. 306) THEN
        KMSG= '* WARNING * A data gap was found in MHS data'
        PRINT *, KMSG
        CALL KIDS(KUNIT,SUBNAM,1,KMSG,0)
        ERSTAT=0
C
C  COULD NOT SYNCHRONIZE WITH AMSU-A
C
      ELSEIF (MSGCOD .EQ. 307) THEN
         KMSG = '* FATAL ERROR * Could not synchronize MHS with AMSU-A'
         PRINT *,KMSG
         CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
         ERSTAT=99
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,0,KMSG,0)
        ERSTAT=0
      ENDIF
      RETURN
      END

