      SUBROUTINE FRMAMB (AMBDAT,AMBANC,AMBFRMREC,AMBCNT,BODF1ST,BLAST,
     1                   ERSTAT)
      IMPLICIT NONE
C
C**********************************************************************
C
C   NAME- FRMAMB
C
C   LANGUAGE- VS FORTRAN  TYPE- SUBROUTINE      COMPUTER- IBM SP
C
C   VERSION- 1.0    DATE- 01/22/04   PROGRAMMER- FRANK TILLEY (RAYTHEON)
C
C   FUNCTION- THIS SUBROUTINE CREATES AN AMSU-B ORBITAL DATA FILE
C             (BODF) FRAME HEADER FOR THE AMSU-B INSTRUMENT, AND WRITES
C             THE HEADER TO THE BODF.
C
C   FILES-
C     DSN OR DESCRIPTIVE TITLE I/O   UNIT    FUNCTIONAL DESCRIPTION
C     ------------------------ ---   ----    ----------------------
C     .../BODF                  O    BIDFU   BODF FLAT FILE
C     .../KIDS                  O    KUNIT   KIDS MESSAGE FILE
C
C   CALLING SEQUENCE PARAMETERS-
C     VARIABLE       TYPE I/O  FUNCTION
C     --------       ---- ---  --------
C     AMBDAT         REAL I/O  ARRAY OF DIMENSION (BNSPOT X BTOTLN 
C                              X BDATEL) WHICH HOLDS ONE DATA FRAME
C                              OF 256 SECONDS OF AMSU-B SPOTS.   
C     AMBANC	     REAL I/O  ARRAY OF DIMENSION (BNSPOT X BTOTLN
C                              X BNANC) WHICH HOLD THE ANCILLARY 
C                              AMSU-B INFORMATION.   
C     AMBFRMREC      INT   I   BODF FRAME RECORD NUMBER.
C     AMBCNT         INT   I   NUMBER OF DATA RECORDS WRITTEN TO BODF.
C     BODF1ST        INT   I   FIRST AMSU-B LINE WRITTEN TO BODF (OF BTOTLN).
C     BLAST          INT   I   LAST AMSU-B LINE WRITTEN TO BODF (OF BTOTLN).
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-B SPOT.
C     BBFLEN            INT   BODF BUFFER LENGTH (BODFLN/2). 
C     BODFLN            INT   BODF RECORD LENGTH IN BYTES.
C     CONE             CH*1   A CONSTANT HEX ONE.
C     COUNT             INT   COUNT OF THE NUMBER OF AMSU-B SPOTS
C                             CONTAINING NON-MISSING VALUES.
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-B 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-B BUFFERS.
C     STRTIM            INT   VECTOR OF 3 CONTAINING THE START
C                             TIME OF THE FRAME.
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     BODFBUF           INT   BODF 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, AMBFRMREC, AMBCNT, IOERR, BODF1ST, BLAST
      REAL AMBDAT(BNSPOT,BTOTLN,BDATEL),AMBANC(BNSPOT,BTOTLN,BNANC)
C
      INTEGER   BODFLN, BBFLEN
      PARAMETER (BODFLN=100, BBFLEN=BODFLN/2)
      CHARACTER*4 DTYPE, C4TEMP
      CHARACTER*6 C6TEMP
      CHARACTER*100 KMSG
      INTEGER   COUNT,ENDDAY,I,J,K,N,MILTIM
      INTEGER   TOTMIL,DUMMY(2),ENDTIM(3),ORBNU(2),STRTIM(3)
      INTEGER    IDX
      INTEGER*2  BODFBUF(BBFLEN)
      REAL      LAT(4),LON(4)
      LOGICAL   FIRST,AGAIN,PACK
      DATA TOTMIL/86400000/
      DATA DTYPE/'AMSB'/
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 = BTOTBF/2
      DO IDX=1,BBFLEN
         BODFBUF(IDX)=I2SPV
      END DO    
C
C  PACK THE RECORD TYPE AND BEGINNING AND ENDING ORBIT NUMBERS.
C
      BODFBUF(1) = 1
      BODFBUF(2) = ORBNU(1)
      BODFBUF(3) = ORBNU(2)
C
C  PACK THE FRAME NUMBER FROM THE BEGINNING OF THE 1B DATA.
C
      BODFBUF(4) = FRAMNO
C
C  PACK THE NUMBER OF RECORDS WRITTEN TO THE BODF FILE.
C
      BODFBUF(5) = AMBCNT
C
C  CONVERT THE TIME OF THE FIRST DATA RECORD
C
      CALL ODBTIM(BSCTIM(1,BODF1ST),BSCTIM(2,BODF1ST),STRTIM)
C
C  PACK THE START TIME OF THE FRAME: YEAR/MONTH, DAY/HOUR, MIN/SEC.
C
      BODFBUF(6) = STRTIM(1)
      BODFBUF(7) = STRTIM(2)
      BODFBUF(8) = STRTIM(3)
      IF(BODFBUF(6)/100.EQ.99) THEN
         BODFBUF(21) = 1999
      ELSE
         BODFBUF(21) = BODFBUF(6)/100 + 2000
      ENDIF
cclw
      if(strtim(1).lt.0.or.strtim(2).lt.0.or.strtim(3).lt.0) then
         print *, 'BODF: suspicious start time ',strtim
         print *, 'based on line bodf1st = ',bodf1st 
      endif
cclw
C
C  CONVERT THE ENDING TIME TO ODB TIME FORMAT.
C
      CALL ODBTIM(BSCTIM(1,BLAST),BSCTIM(2,BLAST),ENDTIM)
C
C  PACK THE END TIME OF THE FRAME: YEAR/MONTH, DAY/HOUR, MIN/SEC.
C
      BODFBUF(9) = ENDTIM(1)
      BODFBUF(10) = ENDTIM(2)
      BODFBUF(11) = ENDTIM(3)
      IF(BODFBUF(9)/100.EQ.99) THEN
         BODFBUF(22) = 1999
      ELSE
         BODFBUF(22) = BODFBUF(9)/100 + 2000
      ENDIF
cclw
      if(endtim(1).lt.0.or.endtim(2).lt.0.or.endtim(3).lt.0) then
         print *, 'BODF: suspicious end time ',endtim
         print *, 'based on line blast = ',blast  
      endif
cclw
C
C  EXTRACT THE LATITUDE AND LONGITUDE VERTICES OF THE FRAME.
C  PACK THE LATITUDE VERTICES.
C
      LAT(1) = AMBANC(1,BODF1ST,1)
      LAT(2) = AMBANC(BNSPOT,BODF1ST,1)
      LAT(3) = AMBANC(BNSPOT,BLAST,1)
      LAT(4) = AMBANC(1,BLAST,1)
      BODFBUF(12) = LAT(1)
      BODFBUF(13) = LAT(2)
      BODFBUF(14) = LAT(3)
      BODFBUF(15) = LAT(4)
C
C  SET UP THE DATA FRAME LONGITUDE VERTICES.
C  PACK THE LONGITUDE VERTICES.
C
      LON(1) = AMBANC(1,BODF1ST,2)
      LON(2) = AMBANC(BNSPOT,BODF1ST,2)
      LON(3) = AMBANC(BNSPOT,BLAST,2)
      LON(4) = AMBANC(1,BLAST,2)
      BODFBUF(16) = LON(1)
      BODFBUF(17) = LON(2)
      BODFBUF(18) = LON(3)
      BODFBUF(19) = LON(4)
C
C  PACK THE OPERATIONAL FLAGS. ONLY THE FIRST ONE IS SET.
C
      BODFBUF(20) = 1
C     
C WRITE THE FRAME HEADER TO THE BODF.
C
      WRITE(UNIT=BIDFU,REC=AMBFRMREC,IOSTAT=IOERR,ERR=900) BODFBUF
      IF(IOERR.NE.0) GO TO 900
      GO TO 1000
C
900   KMSG = '* FATAL ERROR * Writing Record        of BODF FILE'
      WRITE(C6TEMP,'(I6)') AMBFRMREC
      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
