      SUBROUTINE FRMAMA (AMADAT,AMAANC,AMAFRMREC,AMACNT,AODF1ST,ALAST,
     1                   ERSTAT)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- FRMAMA
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- CRAY J916
C
C   VERSION- 2.0    DATE- 11/14/94   PROGRAMMER- SURYA JANDHYALA(SMSRC)
C   VERSION- 2.1    DATE- 12/11/95   PROGRAMMER- ROBERT FELDMAN (SMSRC)
C     ATOVS-272:   AMADAT AND AMAANC ARE ADDED TO INPUT ARGUMENTS  
C   VERSION- 2.2    DATE- 02/01/96   PROGRAMMER- DAVE MOREL (SMSRC)
C     ATOVS-333:   CHANGED CYCLEN FROM 63 TO 57, ALSO ADDED A
C                  WARNING IN THE COMMENT SECTION OF THIS DOC BLOCK.
C   VERSION- 3.0    DATE- 06/20/96   PROGRAMMER- LEAH WILSON (HSTX)
C                  MODIFICATIONS FOR ATOVS BASELINE ON CRAY.
C   VERSION- 4.0    DATE- 09/02/97   PROGRAMMER- LEAH WILSON (HSTX)
C                  CONVERT FROM IRDB TO AODF.  REMOVE FRMAM$.
C   VERSION- 4.1    DATE- 02/20/98   PROGRAMMER- LEAH WILSON (RAYTHEON)
C                  ADD AMACNT AND REPLACE AODREC WITH AMAFRMREC.  ADD
C                  AODF1ST AND ALAST.
C   VERSION- 4.2     DATE- 02/26/99  PROGRAMMER- LEAH WILSON (RAYTHEON)
C                  ADD 4-DIGIT YEAR OF FIRST AND LAST RECORDS.
C   VERSION- 4.3     DATE 11/14/01   PROGRAMMER- RICKEY ROLLINS (CSC)
C                  PORTED TO IBM SP 
C   VERSION- 4.4.   DATE-02/13/02    PROGRAMMER- R. ROLLINS (CSC)
C       PORTED TO IBM SP. CHANGED I4SPV to I2SPV
C       CHANGED AODFBUF to I*2(50)
C   VERSION- 4.5    DATE- 08/22/03   PROGRAMMER- F. Tilley (Raytheon)
C       Changed AOFD record length to a parameter and set it to 232.
C       Changed the size of the AODF buffer to a parameter that is
C       dependent on the size of the AODF record length.
C
C
C   FUNCTION- THIS SUBROUTINE CREATES AN AMSU-A ORBITAL DATA FILE
C             (AODF) FRAME HEADER FOR THE AMSU-A INSTRUMENT, AND WRITES
C             THE HEADER TO THE AODF.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O   UNIT    FUNCTIONAL DESCRIPTION
C     ------------------------ ---   ----    ----------------------
C     .../AODF                  O    AIDFU   AODF FLAT FILE
C     .../KIDS                  O    KUNIT   KIDS MESSAGE FILE
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE       TYPE I/O  FUNCTION
C     --------       ---- ---  --------
C     AMADAT         REAL I/O  ARRAY OF DIMENSION (ANSPOT X ATOTLN 
C                              X ADATEL) WHICH HOLDS ONE DATA FRAME
C                              OF 256 SECONDS OF AMSU-A SPOTS.   
C     AMAANC	     REAL I/O  ARRAY OF DIMENSION (ANSPOT X ATOTLN
C                              X ANANC) WHICH HOLD THE ANCILLARY 
C                              AMSU-A INFORMATION.   
C     AMAFRMREC      INT   I   AODF FRAME RECORD NUMBER.
C     AMACNT         INT   I   NUMBER OF DATA RECORDS WRITTEN TO AODF.
C     AODF1ST        INT   I   FIRST AMSU-A LINE WRITTEN TO AODF (OF ATOTLN).
C     ALAST          INT   I   LAST AMSU-A LINE WRITTEN TO AODF (OF ATOTLN).
C     ERSTAT         INT   O   ERROR STATUS CODE. NORMALLY SET TO
C                              ZERO, BUT IF AN ERROR OCCURS THIS
C                              STATUS CODE IS SET.
C
C
C   COMMON AREA PARAMETERS- COLLOC,FRMINF,INPARM,INSPARM,INSCOM,MONSTA,SPVAL,
C                           SPGUNT
      INCLUDE 'INSPARM'
      INCLUDE 'INPARM'
      INCLUDE 'COLLOC'
      INCLUDE 'FRMINF'
      INCLUDE 'MONSTA'
      INCLUDE 'SPGUNT'
      INCLUDE 'ERPARM'
      INCLUDE 'ERINFO'
C
C   LOCAL PARAMETERS-
C     VARIABLE          TYPE  FUNCTION
C     --------          ----  --------
C     AGAIN         LOGICAL   SET TRUE FOR AN ACTIVE AMSU-A SPOT.
C     ABFLEN            INT   AODF BUFFER LENGTH (AODFLN/2). 
C     AODFLN            INT   AODF RECORD LENGTH IN BYTES.
C     CONE             CH*1   A CONSTANT HEX ONE.
C     COUNT             INT   COUNT OF THE NUMBER OF AMSU-A SPOTS
C                             CONTAINING NON-MISSING VALUES.
C     CYCLEN            INT   CYCLE LENGTH OF THE DATA CYCLES
C     DUMMY             INT   VECTOR OF DIMENSION 2 HOLDING THE PLACE
C                             OF THE RECORD AND BYTE POINTERS.
C     DTYPE            CH*4   THE DATA TYPE OF THE DATA FRAME.
C     ENDDAY            INT   COMPUTED ENDING DAY OF THE LAST GOOD
C                             SCAN LINE.
C     ENDTIM            INT   VECTOR OF 3 CONTAINING THE ENDING TIME
C                             OF THE FRAME.
C     FIRST          LOGICAL  LOGICAL USED TO FIND THE FIRST ACTIVE SCAN
C                             LINE.
C     LAT               REAL  A VECTOR OF 4 INDICATING THE LATITUDE OF
C                             FIRST AND LAST SPOTS OF THE FIRST AND
C                             LAST ACTIVE SCAN LINES.
C     LON               REAL  A VECTOR OF 4 INDICATING THE LONGITUDE
C                             CORRESPONDING TO THE SAME 4 SPOTS AT
C                             LATITUDE.
C     ORBNU             INT   VECTOR OF 2 CONTAINING THE STARTING
C                             AND ENDING ORBIT NUMBERS OF THE
C                             AMSU-A INSTRUMENT.
C     I,J,K             INT   LOOP CONTROL INDICES.
C     MILTIM            INT   CONTAINS THE MILLISECOND PORTION OF THE
C                             LAST GOOD SCAN LINE.
C     N                 INT   NUMBER OF OVERLAP SCAN LINES IN THE
C                             AMSU-A BUFFERS.
C     PACK          LOGICAL   SET TO TRUE TO INDICATE TO SPGFAU THAT
C                             DATA IS BEING PACKED.
C     STRTIM            INT   VECTOR OF 3 CONTAINING THE START
C                             TIME OF THE FRAME.
C     STATUS            INT   ERROR STATUS CODE RETURNED FROM A
C                             FILE MANAGEMENT UTILITY.
C     TOTMIL            INT   TOTAL NUMBER OF MILLISECONDS IN A DAY.
C     C6TEMP           CH*6   TEMPORARY BUFFER FOR WRITING TO KIDS.
C     KMSG            CH*100  KIDS MESSAGE BUFFER.
C     AODFBUF           INT   AODF RECORD BUFFER.
C     C2AODF           CH*1   CHARACTER AODF RECORD BUFFER.
C
C   SUBPROGRAMS CALLED- ODBTIM, KIDS
C
C   EXIT STATES-
C       ERSTAT =  0; INDICATING NO ERRORS FOUND.
C       ERSTAT = 99; INDICATING A FATAL ERROR WAS DETECTED.
C
C*********************************************************************
C
      INTEGER ERSTAT, AMAFRMREC, AMACNT, IOERR, AODF1ST, ALAST
      REAL AMADAT(ANSPOT,ATOTLN,ADATEL),AMAANC(ANSPOT,ATOTLN,ANANC)
C
      INTEGER   AODFLN, ABFLEN
      PARAMETER (AODFLN=232, ABFLEN=AODFLN/2)
      CHARACTER*1 C2AODF(AODFLN)
      CHARACTER*4 DTYPE, C4TEMP
      CHARACTER*6 C6TEMP
      CHARACTER*100 KMSG
      INTEGER   COUNT,CYCLEN,ENDDAY,I,J,K,N,MILTIM
      INTEGER   STATUS,TOTMIL,DUMMY(2),ENDTIM(3),ORBNU(2),STRTIM(3)
C     INTEGER    AODFBUF(50), IDX
      INTEGER    IDX
      INTEGER*2  AODFBUF(ABFLEN)
      REAL      LAT(4),LON(4)
      LOGICAL   FIRST,AGAIN,PACK
      DATA TOTMIL/86400000/,CYCLEN/57/
      DATA DTYPE/'AMSA'/,PACK/.TRUE./
C
      INCLUDE 'SPVAL'
C
C
C  INITIALIZE LOCAL VARIABLES FOR THE AMSU-A HEADER INFORMATION.
C
      COUNT=0
      IOERR=0
      FIRST = .TRUE.
      AGAIN = .FALSE.
      ORBNU(1) = SORB(2)
      ORBNU(2) = EORB(2)
      N = ATOTBF/2
      DO IDX=1,ABFLEN
         AODFBUF(IDX)=I2SPV
      END DO    
C
C  PACK THE RECORD TYPE AND BEGINNING AND ENDING ORBIT NUMBERS.
C
      AODFBUF(1) = 1
      AODFBUF(2) = ORBNU(1)
      AODFBUF(3) = ORBNU(2)
C
C  PACK THE FRAME NUMBER FROM THE BEGINNING OF THE 1B DATA.
C
      AODFBUF(4) = FRAMNO
C
C  PACK THE NUMBER OF RECORDS WRITTEN TO THE AODF FILE.
C
      AODFBUF(5) = AMACNT
C
C  CONVERT THE TIME OF THE FIRST DATA RECORD
C
      CALL ODBTIM(ASCTIM(1,AODF1ST),ASCTIM(2,AODF1ST),STRTIM)
C
C  PACK THE START TIME OF THE FRAME: YEAR/MONTH, DAY/HOUR, MIN/SEC.
C
      AODFBUF(6) = STRTIM(1)
      AODFBUF(7) = STRTIM(2)
      AODFBUF(8) = STRTIM(3)
      IF(AODFBUF(6)/100.EQ.99) THEN
         AODFBUF(21) = 1999
      ELSE
         AODFBUF(21) = AODFBUF(6)/100 + 2000
      ENDIF
cclw
      if(strtim(1).lt.0.or.strtim(2).lt.0.or.strtim(3).lt.0) then
         print *, 'AODF: suspicious start time ',strtim
         print *, 'based on line aodf1st = ',aodf1st 
      endif
cclw
C
C  CONVERT THE ENDING TIME TO ODB TIME FORMAT.
C
      CALL ODBTIM(ASCTIM(1,ALAST),ASCTIM(2,ALAST),ENDTIM)
C
C  PACK THE END TIME OF THE FRAME: YEAR/MONTH, DAY/HOUR, MIN/SEC.
C
      AODFBUF(9) = ENDTIM(1)
      AODFBUF(10) = ENDTIM(2)
      AODFBUF(11) = ENDTIM(3)
      IF(AODFBUF(9)/100.EQ.99) THEN
         AODFBUF(22) = 1999
      ELSE
         AODFBUF(22) = AODFBUF(9)/100 + 2000
      ENDIF
cclw
      if(endtim(1).lt.0.or.endtim(2).lt.0.or.endtim(3).lt.0) then
         print *, 'AODF: suspicious end time ',endtim
         print *, 'based on line alast = ',alast  
      endif
cclw
C
C  EXTRACT THE LATITUDE AND LONGITUDE VERTICES OF THE FRAME.
C  PACK THE LATITUDE VERTICES.
C
      LAT(1) = AMAANC(1,AODF1ST,1)
      LAT(2) = AMAANC(ANSPOT,AODF1ST,1)
      LAT(3) = AMAANC(ANSPOT,ALAST,1)
      LAT(4) = AMAANC(1,ALAST,1)
      AODFBUF(12) = LAT(1)
      AODFBUF(13) = LAT(2)
      AODFBUF(14) = LAT(3)
      AODFBUF(15) = LAT(4)
C
C  SET UP THE DATA FRAME LONGITUDE VERTICES.
C  PACK THE LONGITUDE VERTICES.
C
      LON(1) = AMAANC(1,AODF1ST,2)
      LON(2) = AMAANC(ANSPOT,AODF1ST,2)
      LON(3) = AMAANC(ANSPOT,ALAST,2)
      LON(4) = AMAANC(1,ALAST,2)
      AODFBUF(16) = LON(1)
      AODFBUF(17) = LON(2)
      AODFBUF(18) = LON(3)
      AODFBUF(19) = LON(4)
C
C  PACK THE OPERATIONAL FLAGS. ONLY THE FIRST ONE IS SET.
C
      AODFBUF(20) = 1
C     
C WRITE THE FRAME HEADER TO THE AODF.
C THE AODF IS A 2-BYTE FILE, SO USE CRAY2IBM TO CONVERT THE 8-BYTE
C BUFFER TO A 2-BYTE BUFFER.
C
C     IERR=CRAY2IBM(7,50,C2AODF,0,AODFBUF(1))
C     WRITE(UNIT=AIDFU,REC=AMAFRMREC,IOSTAT=IOERR,ERR=900) C2AODF
      WRITE(UNIT=AIDFU,REC=AMAFRMREC,IOSTAT=IOERR,ERR=900) AODFBUF
      IF(IOERR.NE.0) GO TO 900
      GO TO 1000
C
900   KMSG = '* FATAL ERROR * Writing Record        of AODF FILE'
      WRITE(C6TEMP,'(I6)') AMAFRMREC
      KMSG(32:37) =  C6TEMP
      SUBNAM='SPGDVR'
      IF(IOERR.NE.0) THEN
         KMSG(39:100) = 'with IOSTAT =     '
         WRITE(C4TEMP,'(I4)') IOERR
         KMSG(53:56) = C4TEMP
      ENDIF
      CALL KIDS(KUNIT,SUBNAM,0,KMSG,0)
C
 1000 RETURN
      END
