      SUBROUTINE INAMA(FSTFRM,SYNCTR,SYNTIM,ACALCF,AMADAT,AMAANC,
     1                 GOOD2CAL,SCNAMA,LINAMA,TIMAMA,ERSTAT)
      IMPLICIT NONE
C***********************************************************************
C    NAME- INAMA
C
C    LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE   COMPUTER- CRAY
C
C    VERSION- 3.0  DATE- 5/10/96   PROGRAMMER- ALEX POZDNYAK (FDC)
C       FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 AND I*2 DECLARATIONS
C       TO INTEGER AND ALL R*4 DECLARATIONS TO REAL.
C    VERSION- 3.1  DATE- 01/06/98  PROGRAMMER- LEAH WILSON, RAYTHEON
C    VERSION- 3.2  DATE- 03/10/00  PROGRAMMER- LEAH WILSON, RAYTHEON
C    VERSION- 3.4  DATE- 02/07/01  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.5  DATE- 12/11/01  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.6  DATE- 01/14/02  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.6.1                PROGRAMMER- RICKEY ROLLINS (CSC)
C    VERSION- 3.7  DATE- 09/24/03  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.8  DATE- 09/21/04  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C    VERSION- 3.9  DATE- 11/13/06  PROGRAMMER- FRANK TILLEY (IMSG)
C
C    FUNCTION-  THIS SUBROUTINE BUILDS AN EXTENDED DATA FRAME OF AMSU/A
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 AMSU/A
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  ADD HSYNCTR TO CORRECT NUMLIN CALCULATION FOR NOAA-15
C                 FOR FIRST FRAME.  NOTE THAT THIS GIVES NEGATIVE ARRAY
C                 INDICES, SO ONE DAY IT SHOULD BE RE-DONE.  BUT FOR
C                 NOW, THE HIRS AND AMSU_A DATA ARE SYNCHED CORRECTLY,
C                 AND THE FRAMES HAVE NO MISSING LINES, AND THE CRAY
C                 SEEMS TO HANDLE THE NEGATIVE INDICES WITH NO ILL EFFECTS.
C   VERSION- 3.2  WELL, THE DAY TO RE-DO 3.1 IS NOW!  REMOVE USE OF HSYNCTR
C                 AND IN-LINE CHKSCT TO REFLECT CHANGE TO CHECKING SCAN
C                 LINE NUMBERS INSTEAD OF TIMES. (NOTE: NEGATIVE ARRAY
C                 INDICES CAUSED INCORRECT TIMES IN THE R1T FILE IN 3.1.)
C                 ADD /DEBUG/ TO ALLOW CDB-CONTROLLED PRINT STATEMENTS.
C                 IN-LINE SCNCNF, SINCE IT IS ONLY BASED ON DO_NOT_USE_SCAN.
C                 INCREMENT BADAFLAG FOR BAD SCANS. ADD KIDS WARNING MESSAGE
C                 FOR BACKWARDS SCAN LINES. (FOR EXAMPLE, 201, 202, 197, 203)
C                 ADD GOOD2CAL FOR CALCULATING NUMLIN OF FIRST FRAME.  ADD
C                 /FRAME1/ TO STORE FIRST LINE FILLED IN AMADAT FOR THE FIRST
C                 FRAME.
C   VERSION- 3.4  REWROTE DESCRIPTION OF ARGUMENT 'GOOD2CAL' TO REFLECT ITS
C                 ACTUAL USE. CHANGED CALCULATION OF THE NUMBER OF LINES
C                 SKIPPED BEFORE PUTTING SCAN LINE DATA INTO THE HIRS
C                 BUFFERS (NHLINMOV). ADDED THE 'ANINT' (NEAREST INTEGER)
C                 TO THE CALCULATION OF THE NUMBER OF SECONDS REPRESENTED
C                 BY THE NUMBER OF HIRS BUFFER LINES SKIPPED BEFORE STARTING
C                 TO INPUT DATA. 'ANINT' ROUNDS TO THE NEAREST INTEGER
C                 RATHER THAN TRUNCATING. THESE CHANGES WERE MADE TO
C                 CORRECT PROBLEMS WITH MISALIGNMENT OF HIRS AND AMSU-A
C                 DATA. (PR #26)
C   VERSION- 3.5  ADDED CODE TO SET THE ERROR STATUS TO 99 AND RETURN TO
C                 THE CALLING ROUTINE WHEN OUT-OF-ORDER SCANS ARE FOUND
C                 OR WHEN A DATA GAP IS ENCOUNTERED (PR #52).
C   VERSION- 3.6  REMOVED CODE THAT SETS THE ERROR STATUS TO 99 WHEN
C                 A DATA GAP IS ENCOUNTERED (PR #56).
C   VERSION- 3.6.1  PORTED TO IBM SP
C                 CHANGED FLUSH() TO FLUSH_()
C   VERSION- 3.7  ADDED DATA GAP TESTING FOR THE FIRST DATA FRAME (WR #244).
C   VERSION- 3.8  UNDER WR #230 (NOAA-N READINESS) CHANGED AASGT1
C                 TO AADGT1 FOR DIRECT ACCESS. ADDED SCNAMA TO
C                 CALLING ARGUMENTS TO TRACK THE AMSU-A SCAN NUMBER
C                 FOR CALLS TO AADGT1. CHANGED 'SHAPE' TO INTEGER*4.
C                 SUBSTITUTED I*4 'LENGTH' FOR THE DATA LENGTH IN 1B*
C                 UTILITY CALLS. CHANGED 'ERSTAT' TO I*4 'RETCOD' IN
C                 1B* UTILITY CALLS. ADDED LOCAL ARRAY 'ACALC4' FOR
C                 EXTRACTING 4-BYTE CALIBRATION COEFFICIENTS FROM THE
C                 AMSU-A 1B* FILE.
C   VERSION- 3.9  UNDER WR #303 THE ACTION TAKEN WHEN OUT-OF-ORDER SCANS
C                 WAS CHANGED TO CLEAR THE AMSU-A CONFIGURATION BIT
C                 RATHER THAN SETTING THE ERROR RETURN CODE TO 99.
C
C   FILES- DSN                        UNIT   I/O   REMARKS
C          ------------------------    ----   ---  ----------------
C          .../AMSUA1BS                AUNIT   I   AMSU/A 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      ACALCF    REAL I/O   3D ARRAY (3 X INPARM:ANCHAN X INSPARM:
C                           ATOTLN) OF AMSU/A CALIBRATION COEFFICIENTS
C                           FOR EACH CHANNEL FOR EACH LINE IN THE
C                           EXTENDED DATA FRAME.
C      AMADAT    REAL   O   3D BUFFER (INSPARM:ANSPOT X INSPARM:
C                           ATOTLN X INSPARM:ADATEL) CONTAINING AMSU/A
C                           DATA.
C      AMAANC    REAL   O   3D BUFFER (INSPARM:ANSPOT X INSPARM:
C                           ATOTLN X INSPARM:ANANC) CONTAINING AMSU/A
C                           "ANCILLARY" DATA SUCH AS:
C                           PARM1 = LAT, PARM2 = LON, PARM3 = SOLAR
C                           ZENITH ANGLE, PARM4 = SATELLITE ZENITH
C                           ANGLE, ETC.
C      GOOD2CAL  INT    I   FOR THE FIRST FRAME ONLY, NUMBER OF LINES
C                           SKIPPED OVER IN THE HIRS BUFFERS BEFORE PUTTING
C                           IN DATA FROM THE FIRST SCAN LINE FROM THE
C                           HIRS 1B*
C      SCNAMA    I*4   I/O  AMSU-A 1B* SCAN NUMBER
C      LINAMA    INT    O   LINE IN THE AMSU-A FRAME BUFFERS WHERE THE
C                           FIRST AMSU-A DATA WERE PLACED FOR THE FIRST
C                           DATA FRAME.
C      TIMAMA    INT    O   CENTURY DAY AND MILLISECOND OF DAY FOR
C                           THE FIRST AMSU-A LINE USED IN THE FIRST
C                           DATA FRAME.
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      ADATOV        REAL   3D BUFFER (INSPARM:ANSPOT X INPARM:
C                           ATOTBF X INSPARM:ADATEL) CONTAINING
C                           "64-SEC OVERLAP" DATA.
C      AANCOV        REAL   3D BUFFER (INSPARM:ANSPOT X INPARM:
C                           ATOTBF X INSPARM:ANANC) CONTAINING
C                           "64-SEC OVERLAP" PARAMETERS.
C      ACALC4        R*4    FOUR-BYTE VERSION OF ACALCF.
C      AMA1ST        LOG    FLAG USED TO IDENTIFY THE FIRST AMSU-A
C                           SCAN LINE TIME AND LINE FOR AMSU-B/
C                           MHS SYNCHRONIZATION.
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    INDEX FOR LOOPING THROUGH CHANNELS.
C      INSTRU        INT    INSTRUMENT FOR WHICH THE CONFIDENCE &
C                           USABILITY ARE COMPUTED (1=HIRS, 2=AMSU/A,
C                           3=AMSU/B).
C      INT1VL        I*1    USED TO EXTRACT I*1 VALUES FROM THE 1B*.
C      INT2VL        I*2    USED FOR EXTRACTING INTEGER BYTE 2 VALUES
C                           FROM THE AMSU/A SCAN LINE.
C      INT4VL        I*4    USED FOR EXTRACTING I*4 VALUES FROM THE
C                           1B* SCAN LINE.
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      RETCOD        I*4    RETURN CODE FROM 1B* UTILITIES.
C      SHAPE         I*4    SHAPE ARRAY, DIMENSIONED (0:7), USED IN
C                           CALLS 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                        AAG1I2,AAG1I4,AAG1R4,AADGT1,INIFIN,
C                        INIFRM,JULDAY,MCLR,AMALIN,INAMA$
C
C    FUNCTIONS USED- IBCLR, MOD, ANRSCN
C
      INTEGER ANRSCN
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    SCNAMA
      REAL   AMADAT(ANSPOT,ATOTLN,ADATEL),AMAANC(ANSPOT,ATOTLN,ANANC)
      REAL   ADATOV(ANSPOT,ATOTBF,ADATEL),AANCOV(ANSPOT,ATOTBF,ANANC)
      REAL   ACALCF(3,ANCHAN,ATOTLN)
      REAL*4 ACALC4(3,ANCHAN,ATOTLN)
C
      CHARACTER*32 ITEMID
      INTEGER*1    INT1VL
      INTEGER*2    INT2VL
      INTEGER      SCNDIF,DONOTUSE, ICHAN
      INTEGER      GOOD2CAL, NHLINMOV, MOVHTIM
      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      AMA1ST
C
      DATA INSTRU/2/
      DATA PRVSCN/0/
      DATA AMA1ST/.TRUE./
C
      SAVE PRVTIM, PRVSCN, ADATOV, AANCOV, AMA1ST
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   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(ADATOV,AANCOV,ATOTBF,ANSPOT,ADATEL,ANANC,AMADAT,
     1                AMAANC,ATOTLN)
C
C   SET LINE NUMBER EQUAL TO ATOTBF SINCE THIS IS NUMBER OF LINES
C   WRITTEN TO BUFFER BY INIFRM.
C
          NUMLIN = ATOTBF
C
C   INITIALIZE THE SCAN LINE CONFIDENCE TO ZERO.
C
          CALL MCLR(AMAANC(1,NUMLIN+1,5),ANSPOT,ATOTLN-NUMLIN)
          CALL MCLR(AANCOV(1,1,5),ANSPOT,ATOTBF)
C
C   INITIALIZE THE CHANNEL AVAILABILITY TO ZERO.
C
          CALL MCLR(AMAANC(1,NUMLIN+1,6),ANSPOT,ATOTLN-NUMLIN)
          CALL MCLR(AANCOV(1,1,6),ANSPOT,ATOTBF)
C
C   INITIALIZE THE CALIBRATION COEFFICIENT ARRAY AND ARRAYS IN /FRMINF/
C
          CALL INIFIN(AGOODL,AQFLAG,ASCTIM,ACALCF,ATOTLN,ATOTBF,ANCHAN)
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(ANSPOT,ADATEL,ANANC,ATOTLN,ATOTBF,AMADAT,AMAANC,
     1                ADATOV,AANCOV,SYNCTR,NUMLIN)
C
C    ADJUST NUMLIN FOR PLACEMENT OF FIRST CALIBRATION LINES IN HIRS
C    IF THIS IS NO-HIRS, THIS HAS NO EFFECT.   FIRST DETERMINE THE
C    NUMBER OF LINES THE HIRS WAS MOVED WITHIN ITS DATA FRAME, IN
C    SECONDS.  THEN, DETERMINE HOW MANY LINES THE AMSU-A SHOULD BE
C    MOVED WITHIN ITS DATA FRAME TO CORRESPOND TO THE SAME TIME PERIOD.
C    NOTE: HIRS SCAN-TO-SCAN TIME IS 6.4 SEC, SO RDUM AND FLOAT IS NEEDED.
C    AMSU-A SCAN TIME IS 8 SEC, SO RDUM IS NOT NEEDED.  IF THE 8 IS EVER
C    CHANGED TO A NON-INTEGER NUMBER, RDUM/FLOAT WOULD NEED TO BE EMPLOYED
C    FOR AMSU-A.
C
          IF(DBG1BSTR.EQ.1)PRINT *,'INAMA-frame1 orig NUMLIN=',NUMLIN
          NHLINMOV = GOOD2CAL
          IF (NHLINMOV.GT.0) THEN
             RDUM = FLOAT(HIRTIM(3))/1000.0
             MOVHTIM = ANINT(RDUM * NHLINMOV)
             NUMLIN = MOVHTIM/(AMATIM(3)/1000)
          END IF
          SKIPAMA = NUMLIN + 1
          IF(DBG1BSTR.EQ.1) THEN
             PRINT *,'INAMA - NHLINMOV & SKIPAMA & new NUMLIN = ',
     1               NHLINMOV, SKIPAMA, NUMLIN
          END IF
C
C    INITIALIZE SPOT CONFIDENCE VALUES TO ZERO.
C
          CALL MCLR(AMAANC(1,1,5),ANSPOT,ATOTLN)
          CALL MCLR(AANCOV(1,1,5),ANSPOT,ATOTBF)
C
C   INITIALIZE THE CHANNEL AVAILABILITY TO ZERO.
C
          CALL MCLR(AMAANC(1,1,6),ANSPOT,ATOTLN)
          CALL MCLR(AANCOV(1,1,6),ANSPOT,ATOTBF)
C
C    INITIALIZE CALIBRATION COEFFICIENT ARRAY AND ARRAYS IN /FRMINF/
C    FOR FIRST FRAME CASE.
C
          CALL CLRFIN(AGOODL,AQFLAG,ASCTIM,ACALCF,ATOTLN,ANCHAN)
C
C    CALCULATE VALUES OF THE SCAN LINE TIME IN THE OVERLAP BUFFER
C    BEFORE SYNC. TIME.
C
ccft      DO 20, LINE = 1,ATOTBF/2
c             CALL CMPTIM(SYNTIM,AMATIM(3),LINE-ATOTBF/2-1,
c    1                    ASCTIM(1,LINE))
c 20      CONTINUE
          DO LINE = 1,ATOTBF
              CALL CMPTIM(SYNTIM,AMATIM(3),LINE-ATOTBF-1,
     1                    ASCTIM(1,LINE))
          END DO
C
C    PUT THE SYNC TIME IN THE SCAN LINE TIME ARRAY.
C
          ASCTIM(1,ATOTBF+1) = SYNTIM(1)
          ASCTIM(2,ATOTBF+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. ATOTLN) NUMLIN = ATOTLN
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 = ATOTBF+2,NUMLIN
                  CALL CMPTIM(SYNTIM,AMATIM(3),LINE-ATOTBF-1,
     1                        ASCTIM(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 RETURN.
C
          IF (NUMLIN .EQ. ATOTLN) THEN
              SYNCTR = SYNCTR - ATOTLN + ATOTBF
ccft          SYNTIM(1) = ASCTIM(1,ATOTLN-ATOTBF+1)
ccft          SYNTIM(2) = ASCTIM(2,ATOTLN-ATOTBF+1)
              SYNTIM(1) = ASCTIM(1,ATOTLN)
              SYNTIM(2) = ASCTIM(2,ATOTLN)
C
              RETURN
          ELSE
              SYNCTR = 0
          ENDIF
      ENDIF
C
C   LOOP OVER THE EXTENDED FRAME AND FILL THE BUFFERS WITH AMSU/A DATA.
C   CALL 1B* UTILITIES TO GET THE NEXT SCAN LINE AND EXTRACT
C   THE DESIRED INFORMATION.
C
   40 CONTINUE
      SCNAMA = SCNAMA+1
      CALL AADGT1(SCNAMA,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 AAG1I2(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 AAG1I2(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 AAG1I2(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             '
      LENGTH = 1
      CALL AAG1I4(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   CAPTURE THE BUFFER LINE NUMBER AND LINE TIME FOR THE FIRST
C   AMSU=A LINE FOR AMSU-B/MHS SYNCHRONIZATION.
C
      IF (AMA1ST) THEN
         LINAMA = NUMLIN
         TIMAMA(1) = LINTIM(1)
         TIMAMA(2) = LINTIM(2)
         AMA1ST = .FALSE.
      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( .NOT. FSTFRM) THEN
         TIMCOD = 0
         NUMMIS = 0
C        SCNDIF = SCNLIN - PRVSCN
         SCNDIF = ANRSCN(LINTIM,PRVTIM)
C
C   IF OUT-OF-ORDER SCANS ARE FOUND WRITE A MESSAGE TO THE KIDS
C   FILE AND CLEAR THE AMSU-A CONFIGURATION BIT.
C
         IF(SCNDIF.LT.0) THEN
            CALL INAMA$(305,ERSTAT)
            GOTO 1000
         ENDIF
         IF(SCNDIF.NE.1) THEN
            NUMMIS=SCNDIF-1
            TIMCOD = 1
            CALL INAMA$(306,ERSTAT)
         ENDIF
         IF(TIMCOD.NE.0) THEN
            PRINT *, '1BSTAR: INAMA FOUND ',NUMMIS,' MISSING SCAN ',
     1               ' LINES BETWEEN ',PRVSCN,' AND ',SCNLIN,' WITH ',
     2               ' SCAN TIMES of ',PRVTIM(1),PRVTIM(2),' and ',
     3               LINTIM(1),LINTIM(2)
         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,AMATIM(3),LINE,ASCTIM(1,NUMLIN))
C
C   IF NUMLIN IS GREATER THAN OR EQUAL TO ATOTLN, 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. ATOTLN) THEN
                     PRVTIM(1) = ASCTIM(1,NUMLIN)
                     PRVTIM(2) = ASCTIM(2,NUMLIN)
                     PRVSCN = PRVSCN + LINE
                     SCNAMA = SCNAMA -1
                     IF (ERSTAT .LT. 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 FLAG.
C
      CNFDNC = 100
      FLGCOD = 0
      ITEMID = 'DO_NOT_USE_SCAN                 '
      LENGTH = 1
      CALL AAG1I1(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
         BADAFLAG(1) = BADAFLAG(1) + 1
      ENDIF
C
      ASCTIM(1,NUMLIN) = LINTIM(1)
      ASCTIM(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) = ANCHAN
         LENGTH = 3*ANCHAN
         CALL AAG1R4(ITEMID,ACALC4(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, ANCHAN
            ACALCF(1,ICHAN,NUMLIN) = ACALC4(1,ICHAN,NUMLIN)
            ACALCF(2,ICHAN,NUMLIN) = ACALC4(2,ICHAN,NUMLIN)
            ACALCF(3,ICHAN,NUMLIN) = ACALC4(3,ICHAN,NUMLIN)
         END DO
C
C   CALL AMALIN TO FILL THE BUFFERS WITH THE SCAN LINE DATA.
C
         CALL AMALIN(ACALCF(1,1,NUMLIN),NUMLIN,CNFDNC,
     1               ADATOV,AANCOV,AMADAT,AMAANC,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               AMAANC(1,NUMLIN,1),AMAANC(1,NUMLIN,2)
            CALL FLUSH_(101)
         ENDIF
C
C   LINE CONTAINS USABLE DATA.  PROCEED WITH NORMAL PROCESSING.
C
         AGOODL(NUMLIN) = .TRUE.
      ELSE
         AFLQCK = AFLQCK + 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. ATOTLN) GO TO 40
      RETURN
C
C   END OF FILE FOUND ON THE AMSU/A 1B*.
C
  700 CALL INAMA$(302,ERSTAT)
      GO TO 1000
C
C   ERROR IN A CALLED ROUTINE IN AMALIN.
C
  800 CALL INAMA$(303,ERSTAT)
      GO TO 1000
C
C   ERROR IN A 1B* UTILITY.
C
  900 CALL INAMA$(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 2 (AMSU/A),
C    THE BIT SET WITH IBCLR MUST BE 32 -2.  (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*********************************************************************
C
      SUBROUTINE INAMA$(MSGCOD,ERSTAT)
C
C*********************************************************************
C   NAME- INAMA$
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- 12/11/01  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C   VERSION- 3.3    DATE- 01/14/02  PROGRAMMER- FRANK TILLEY (RAYTHEON)
C
C   FUNCTION- THIS SUBROUTINE IS THE ERROR HANDLING ROUTINE FOR
C             ROUTINE INAMA.
C   VERSION- 3.1 ADD 305, REMOVE 301.
C   VERSION- 3.2 ADD 306, CHANGE 305 FROM WARNING TO FATAL (PR #52)
C   VERSION- 3.3 CHANGE 306 FROM FATAL TO 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= 'AMSU-A End of file reached -  AMSU-A shut down'
        PRINT *,KMSG
        CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
        ERSTAT=0
C
C  CHECK FOR ERROR IN AMALIN.
C
      ELSEIF (MSGCOD .EQ. 303) THEN
        KMSG= '* FATAL ERROR * 1b* utility error in AMALIN'
        PRINT *,KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        ERSTAT=99
C
C  CHECK FOR ERROR IN INAMA.
C
      ELSEIF (MSGCOD .EQ. 304) THEN
        KMSG(1:50)='AMSU-A Probable End of File reached - AMSU-A shut '
        KMSG(51:100)='down.'
        PRINT *,KMSG
        CALL KIDS(KUNIT,SUBNAM,2,KMSG,0)
        ERSTAT=99
C
C  FOUND AN OUT-OF-ORDER SCAN LINE
C
      ELSEIF (MSGCOD .EQ. 305) THEN
        KMSG= '* FATAL ERROR * AMSU-A Scan line      is out of order'
        WRITE(C4TEMP,'(I4)') SCNLIN
        KMSG(34:37) = 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 in AMSU-A data'
        PRINT *, KMSG
        CALL KIDS(KUNIT,SUBNAM,1,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
        PRINT *,KMSG
        CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
        ERSTAT=0
      ENDIF
      RETURN
      END

