CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
      SUBROUTINE GET_EDB( SCI_UNIT, EDB, GAP, DELTA )
C+
C
C FUNCTIONAL DESCRIPTION:
C
C     This routine gets the data for one EDB, and calls STUCTURE_EDB to place
C     it into the appropriate structure.  Eight packets comprise one EDB.
C
C FORMAL ARGUMENTS:
C
C     SCI_UNIT:
C	INT	  I	The Fortran unit number of the file.  This is no longer
C			used.
C
C     EDB:
C	/EDB_ALL/  O	A record of structure type EDB_ALL.  Upon return it
C			will contain the next EDB from the input file.
C
C     GAP:
C	L*4	  O	Upon return will be .TRUE. if 
C
C     DELTA:
C	INT	  O	An integer variable that upon return will contain the
C			difference between the sequence counters of the two
C			most recently read packets.
C
C INTERFACE:
C
!	INTERFACE
!	  SUBROUTINE GET_EDB( SCI_UNIT, EDB, GAP, DELTA )
!	    INTEGER, INTENT(IN) :: SCI_UNIT
!	    INTEGER, INTENT(OUT) :: DELTA
!	    LOGICAL, INTENT(OUT) :: GAP
!	    RECORD /EDB_ALL/ EDB		! INTENT(OUT)
!	  END SUBROUTINE GET_EDB
!	END INTERFACE
C
C COMMON BLOCKS:
C
C	PACKET_CONTROL		contains NPACKETS, PACKET_COUNTER
C
C INCLUDE FILES:
C
C	EDBDEF.INC
C
C CREATED:
C
C      30-AUG-1995    John PAquette
C
C This version:   1.24   20-APR-1999
C
C DEVELOPMENT HISTORY:
C 
C  Author       Date		Description of Change 
C  --------	-----------	--------------------- 
C  J. PAQUETTE  30-AUG-1995     Created
C  J. PAQUETTE   6-SEP-1995     Reading of LZ file now done by SOHO_READ_LZ
C  J. PAQUETTE   8-SEP-1995     Altered to handle Dummy packets
C  J. PAQUETTE  11-SEP-1995     Removed dead variable C_PACKET
C  J. PAQUETTE  21-SEP-1995     Fixed error in EPOCH calculation
C  J. PAQUETTE  31-OCT-1995     Zero EDB before calculation
C  J. PAQUETTE  21-DEC-1995     Set EDB Counter = 19 if we read a packet not
C				394 bytes long
C  J. PAQUETTE  16-JAN-1996     Change call to STRUCTURE_EDB
C  J. PAQUETTE  17-JAN-1996     Make sure that EDB.START.COUNTER is valid when
C				we have missing EDB's
C  J. PAQUETTE  19-JAN-1996     Add PACKET_CONTROL COMMON block to deal with
C				running out of packets 
C  J. PAQUETTE  26-JAN-1996     Detect problem if packet 0 has no sync word
C  J. PAQUETTE  21-FEB-1996     Flag EDB as bad if packet(s) contain errors
C				according to QAC 
C  J. PAQUETTE   5-APR-1996     Ignore quality flag of 08
C  L. Bleau	 8-APR-1996	changed SOHO_READ_LZ to READ_STREAM
C  J. PAQUETTE  15-APR-1996     If data packet less than 394 bytes, do not STOP
C                               (except in start up section)
C  J. PAQUETTE  13-JUN-1996     Call FIND_QAC if at the beginning of an input
C                               data file, not just when FIRST .EQ. TRUE; make
C                               sure that QAC.COUNTER zeroed at start of each
C                               file 
C  J. PAQUETTE  17-JUN-1996     Require byte offset to be w/in 8192 of
C                               PACKET_COUNTER*394 before calling FIND_QAC 
C  J. PAQUETTE   8-AUG-1996     Only set ALREADY_HAVE_PACKET when a sync word
C                               is found; Make sure that QAC's checked when
C                               that packet is read 
C  J. PAQUETTE   8-AUG-1996     Add GAP variable to calling sequence; make sure
C                               that a bad packet encountered while searching
C                               for a sync word does not affect the quality of
C                               the EDB that comes after it 
C  J. PAQUETTE  12-AUG-1996     Make certain that PACKET_COUNTER is always
C                               incremented after a call to READ_STREAM -
C                               unless the program is going to STOP 
C  J. PAQUETTE  23-SEP-1996     Make sure that errors in the first few packets
C                               of a file are properly accounted for in the QAC
C                               list 
C  J. PAQUETTE  30-APR-1997     Add code to handle the case when there is no
C                               sync word in a file 
C  J. PAQUETTE   1-MAY-1997     To allow for data gaps in QKL files (which have
C                               no QAC lists), compare sequence counter to
C                               previous sequence counter. Flag EDB as bad if
C                               the difference between them is not 1 or -16383
C                               (14 bit Seq cntr)
C  J. PAQUETTE  11-SEP-1997     Compute (and pass back) the difference between
C                               succesive Seq cntr's 
C  L. Bleau	20-APR-1999	added IMPLICIT NONE, comments
C
C- 
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CDEC$ IDENT	'1.24'
      IMPLICIT NONE
      INCLUDE 'INC_DIR:EDBDEF.INC'
      BYTE PACKET(394), EDB_BYTE(3056)
      LOGICAL ALREADY_HAVE_PACKET/.FALSE./, FIRST/.TRUE./
      INTEGER*4 INTEGER_EPOCH, EDB_CNTR, OLD_EDB_CNTR, SEQ_CNTR, OLD_SEQ_CNTR, NPACKETS, 
     &          PACKET_COUNTER, NBYTES, DUMMY/0/
      INTEGER*4 SCI_UNIT, I, NPACKET, NEXT_EDB_QUALITY, DELTA
      REAL*8 FRACTIONAL_EPOCH
      LOGICAL GAP
      RECORD /EDB_ALL/ EDB, EMPTY
      RECORD /QUAL_ACCT_CAPS/ QAC
      COMMON /PACKET_CONTROL/ NPACKETS, PACKET_COUNTER
C
C Zero the EDB structure
C
      EDB = EMPTY
      GAP = .FALSE.
      DELTA = 0
C
C The first packet in the file may not be the first one in an EDB. Find first
C sync word to find first complete EDB
C
      IF(FIRST)THEN
        NEXT_EDB_QUALITY = 0
        CALL READ_STREAM( 1, PACKET, 394, NBYTES)
C
C Error trapping
C
        IF(NBYTES.NE.394)THEN 
          TYPE*,' Data Packet not 394 bytes (the only legal value), length is ',NBYTES
          STOP 10
        ENDIF
C
C Increment packet counter; check to see if there are packets left
C
        PACKET_COUNTER = PACKET_COUNTER + 1
        IF(PACKET_COUNTER .GT. NPACKETS)THEN
          STOP 11
        ENDIF
C
C Get a QAC list element
C
        CALL READ_QAC(2, QAC)
C
C Calculate Sequence counter. Set the value of a fictitious previous packet to
C one less than this value.
C
        CALL MVBITS( ZEXT(PACKET(3)), 0, 6, SEQ_CNTR, 8 )
        CALL MVBITS( ZEXT(PACKET(4)), 0, 8, SEQ_CNTR, 0 )
        OLD_SEQ_CNTR = SEQ_CNTR - 1
C
C Compare the Sequence counter we have just calculated to the sequence counter
C of the next element in the QAC list.  The purpose of this is to ensure that
C we get the next QAC list element; we set EDB.QUALITY to -1 just in case this
C is the first packet of an EDB. 
C
        IF(SEQ_CNTR .EQ. QAC.SEQ_CNTR)THEN
          IF(ABS(QAC.OFFSET - PACKET_COUNTER*394) .LT. 8192)THEN
            IF(QAC.ERROR_TYPE_FLAGS .NE. 8)THEN
              EDB.QUALITY = -1 ! Temporarily use -1 for bad
            ENDIF
            CALL READ_QAC(2, QAC) 
          ENDIF
        ENDIF
C
C Keep reading until we find a sync word (for Celias, EB 90 hex), followed by a
C flag indicating "DATA" (DA hex) and the EDB length (3056, or BF0 hex).  Also,
C if this is the first packet of an EDB, the next byte contains an EDB counter
C (in the range 0-19). 
C
        DO 100 WHILE(.NOT.( (PACKET(13).EQ.'EB'X) .AND. (PACKET(14).EQ.'90'X) .AND. (PACKET(15).EQ.'DA'X)
     &    .AND. (PACKET(16).EQ.'B'X) .AND. (PACKET(17).EQ.'F0'X) .AND. (ZEXT(PACKET(18)).LE.19)) )
C
C If we got here, the packet that we just read is not the first one in an EDB.
C Since we have not yet begun to read the first complete EDB, zero the quality
C flag to avoid problems when there's an error in the first few packets.
C
          EDB.QUALITY = 0
C
          CALL READ_STREAM( 1, PACKET, 394, NBYTES)
          OLD_SEQ_CNTR = SEQ_CNTR
C
C Error trapping
C
          IF(NBYTES.NE.394)THEN 
            TYPE*,' Data Packet not 394 bytes (the only legal value), length is ',NBYTES
            PACKET_COUNTER = NPACKETS + 1
            EDB.START.COUNTER = 20
            RETURN
          ENDIF
C
C Increment packet counter; check to see if there are packets left
C
          PACKET_COUNTER = PACKET_COUNTER + 1
          IF(PACKET_COUNTER .GT. NPACKETS)THEN
            STOP 13
          ENDIF
C
C Get Sequence counter. This is present in every packet, so we can get it even
C if we haven't yet found the start of an EDB.
C
          CALL MVBITS( ZEXT(PACKET(3)), 0, 6, SEQ_CNTR, 8 )
          CALL MVBITS( ZEXT(PACKET(4)), 0, 8, SEQ_CNTR, 0 )
C
C Compare the Sequence counter we have just calculated to the sequence counter
C of the next element in the QAC list.  If they are equal and the error type
C flag is not 08(hex) (Reed-Solomon error correction was performed), flag this
C EDB as bad, and get the next QAC list element. 
C
          IF(SEQ_CNTR .EQ. QAC.SEQ_CNTR)THEN
            IF(ABS(QAC.OFFSET - PACKET_COUNTER*394) .LT. 8192)THEN
              IF(QAC.ERROR_TYPE_FLAGS .NE. 8)THEN
                EDB.QUALITY = -1 ! Temporarily use -1 for bad
              ENDIF
              CALL READ_QAC(2, QAC) 
            ENDIF
          ENDIF
C
          IF(((SEQ_CNTR - OLD_SEQ_CNTR).NE. 1).AND.((SEQ_CNTR - OLD_SEQ_CNTR).NE. -16383))THEN
            EDB.QUALITY = -1
            DELTA = SEQ_CNTR - OLD_SEQ_CNTR
          ENDIF
C
 100    CONTINUE
C
C Once we reach this point, we have found the start of an EDB, and we can
C therefore extract the EDB counter.
C
        EDB_CNTR = ZEXT(PACKET(18))
        OLD_EDB_CNTR = EDB_CNTR - 1
C
C Make sure that we don't overwrite the packet that we already have
C
        ALREADY_HAVE_PACKET =.TRUE.
C
C All of the above is "start up code" to be executed only once (the first time
C through this routine). Set a flag to guarantee this behavior.
C
        FIRST = .FALSE.
      ENDIF
C
C If we are at the start of a new input file, then get a QAC list element
C
      IF(PACKET_COUNTER .EQ. 0)THEN
        QAC.COUNTER = 0
        CALL READ_QAC(2, QAC)
      ENDIF
C
C 8 packets comprise an EDB. Get them one by one and assemble them into a byte
C array. 
C
      NPACKET = 0
      DO 400 WHILE(NPACKET .LE. 7)
C
C Get a data packet - UNLESS we already have one. That could happen if we have
C a data gap. 
C
        IF(.NOT.ALREADY_HAVE_PACKET)THEN
          NEXT_EDB_QUALITY = 0
          CALL READ_STREAM( 1, PACKET, 394, NBYTES)
          OLD_SEQ_CNTR = SEQ_CNTR
C
C Increment packet counter; check to see if there are packets left. If we are
C out of packets, then set EDB Counter to 20 (to tell the calling routine that
C there is no more data) and RETURN. 
C
          PACKET_COUNTER = PACKET_COUNTER + 1
          IF(PACKET_COUNTER .GT. NPACKETS)THEN
            EDB.START.COUNTER = 20
            RETURN
          ENDIF
C
C Error trapping. Assume that if we have a packet that's not 394 bytes, we have
C reached the end of the packet data. Set EDB Counter to 20 (to tell the
C calling routine that there is no more data) and the packet counter to 1 more
C than the number of packets in the file to tell the main routine that there's
C no more data) and RETURN. 
C
          IF(NBYTES.NE.394)THEN 
            TYPE*,' Data Packet not 394 bytes (the only legal value), length is ',NBYTES
            PACKET_COUNTER = NPACKETS + 1
            EDB.START.COUNTER = 20
            RETURN
          ENDIF
C
        ENDIF
        ALREADY_HAVE_PACKET = .FALSE.
C
C If we are constructing the first EDB in a file, then EDB.QUALITY may have
C been set by the IF(FIRST)... code above. If it has been, then don't overwrite
C it!  Alternately, we may be in the middle of a file, in which case
C EDB.QUALITY is most likely 0 at this point (since EDB is zeroed at the
C beginning of this routine) and should be overwrittedn with NEXT_EDB_QUALITY.
C
        IF(EDB.QUALITY .EQ. 0)EDB.QUALITY = NEXT_EDB_QUALITY
C
C Calculate Sequence counter.
C
        CALL MVBITS( ZEXT(PACKET(3)), 0, 6, SEQ_CNTR, 8 )
        CALL MVBITS( ZEXT(PACKET(4)), 0, 8, SEQ_CNTR, 0 )
C
C Compare the Sequence counter we have just calculated to the sequence counter
C of the next element in the QAC list.  If they are equal and the error type
C flag is not 08(hex) (Reed-Solomon error correction was performed), flag this
C EDB as bad, and get the next QAC list element. 
C
        IF(SEQ_CNTR .EQ. QAC.SEQ_CNTR)THEN
          IF(ABS(QAC.OFFSET - PACKET_COUNTER*394) .LT. 8192)THEN
            IF(QAC.ERROR_TYPE_FLAGS .NE. 8)THEN
              EDB.QUALITY = -1 ! Temporarily use -1 for bad
            ENDIF
            CALL READ_QAC(2, QAC) 
          ENDIF
        ENDIF
C
        IF(((SEQ_CNTR - OLD_SEQ_CNTR).NE. 1).AND.((SEQ_CNTR - OLD_SEQ_CNTR).NE. -16383))THEN
          EDB.QUALITY = -1
          DELTA = SEQ_CNTR - OLD_SEQ_CNTR
        ENDIF
C
C Check to see if this is actually the first packet of an EDB. If so, then a
C sync word should be present (for Celias, EB 90 hex), followed by a flag
C indicating "DATA" (DA hex) and the EDB length (3056, or BF0 hex). The next
C byte contains an EDB counter (in the range 0-19). 
C
        EDB_CNTR = ZEXT(PACKET(18))
        IF( (PACKET(13).EQ.'EB'X) .AND. (PACKET(14).EQ.'90'X) .AND. (PACKET(15).EQ.'DA'X)
     &    .AND. (PACKET(16).EQ.'B'X) .AND. (PACKET(17).EQ.'F0'X) .AND. (EDB_CNTR.LE.19)) THEN
C
C This is the first packet of an EDB.  Save its time tag (which is an EPOCH
C time, number of seconds since 1-JAN-1958, but its stored as an integer). The
C integer portion is 32 bits... 
C
          INTEGER_EPOCH = ISHFT(ZEXT(PACKET(7)), 24) + ISHFT(ZEXT(PACKET(8)), 16) + 
     &                    ISHFT(ZEXT(PACKET(9)), 8) + ZEXT(PACKET(10))
C
C ...and the fractional part is 11 bits.
C
          FRACTIONAL_EPOCH = ISHFT(ZEXT(PACKET(11)), 3) + IBITS(ZEXT(PACKET(12)), 5, 3)
C
C Combine these as a real number. This involves dividing the fractional part by
C 2 to the 11th (because there are 11 bits of fraction) which is 2048, then
C adding. 
C
          EDB.EPOCH = INTEGER_EPOCH + FRACTIONAL_EPOCH/2048
C
C Get the time in Gregorian format 
C
          CALL SOHO_EPOCH_TO_GREG( EDB.EPOCH, EDB.EDB_TIME.YEAR, EDB.EDB_TIME.DOY, EDB.EDB_TIME.HR,
     &                             EDB.EDB_TIME.MIN, EDB.EDB_TIME.SEC,EDB.EDB_TIME.MSEC, EDB.EDB_TIME.MICRO )
C
C A sync word is contained only in the first packet of an EDB. If our counter
C does not show this to be the first packet, we have a data gap; there are
C missing packets. 
C
          IF(NPACKET.NE.0)THEN
C
C Mark this EDB as bad
C
            EDB.QUALITY = -1 ! Temporarily use -1 for bad 
C
C Save the packet that we just read, as it belongs to the next good EDB, then
C return 
C
            ALREADY_HAVE_PACKET = .TRUE.
C
C Make sure that the EDB.START.COUNTER is valid, since the routine that calls
C this one will use it to determine how many EDB's are missing.
C
            EDB.START.COUNTER = EDB_CNTR
            GAP = .TRUE.
            RETURN
          ENDIF
C
C If we don't have a regular sync word, check for a Dummy packet (sync word EB
C 90 DD hex, and a length of 382 = 17E hex). Increment dummy counter, then go
C to the top of the loop and get another packet. 
C
        ELSEIF( (PACKET(13).EQ.'EB'X) .AND. (PACKET(14).EQ.'90'X) .AND. (PACKET(15).EQ.'DD'X)
     &    .AND. (PACKET(16).EQ.'1'X) .AND. (PACKET(17).EQ.'7E'X)) THEN
          DUMMY = DUMMY + 1
          GOTO 400
        ELSE
C
C If we have neither a dummy EDB nor an EDB, this is not the first packet of an
C EDB. Get another and try again. 
C
          IF(NPACKET.EQ.0)THEN
C
C Bad packets found while searching for a sync word have no bearing on the
C quality of the EDB to come 
C
            EDB.QUALITY = 0
            GO TO 400
          ENDIF
        ENDIF
C
C If this is the first packet of an EDB, save EDB_CNTR
C
        IF(NPACKET.EQ.0)THEN
          OLD_EDB_CNTR = EDB_CNTR
          IF(EDB_CNTR.EQ.19)OLD_EDB_CNTR = -1  
        ENDIF
C
C Store only the data, not the packet header. For Celias science data, the
C header is the first 12 bytes, and the remaining 382 bytes are data.
C
        DO 200 I = 1, 382
          EDB_BYTE(I + NPACKET*382) = PACKET(I + 12)
 200    CONTINUE
C
        NPACKET = NPACKET + 1
 400  CONTINUE
C
C Check to see if the next packet contains a sync word, as expected. If it
C doesn't, then we will mark the current EDB as bad.
C
      CALL READ_STREAM( 1, PACKET, 394, NBYTES)
      OLD_SEQ_CNTR = SEQ_CNTR
C
C Increment packet counter; check to see if there are packets left
C
      PACKET_COUNTER = PACKET_COUNTER + 1
      IF(PACKET_COUNTER .GT. NPACKETS)THEN
        EDB.START.COUNTER = 20
        RETURN
      ENDIF
C
C Error trapping. 
C
      IF(NBYTES.NE.394)RETURN
C
C Calculate Sequence counter.
C
      CALL MVBITS( ZEXT(PACKET(3)), 0, 6, SEQ_CNTR, 8 )
      CALL MVBITS( ZEXT(PACKET(4)), 0, 8, SEQ_CNTR, 0 )
C
      IF(((SEQ_CNTR - OLD_SEQ_CNTR).NE. 1).AND.((SEQ_CNTR - OLD_SEQ_CNTR).NE. -16383))DELTA = SEQ_CNTR - OLD_SEQ_CNTR
C
C Compare the Sequence counter we have just calculated to the sequence counter
C of the next element in the QAC list.  If they are equal and the error type
C flag is not 08(hex) (Reed-Solomon error correction was performed), flag this
C EDB as bad, and get the next QAC list element. 
C
      IF(SEQ_CNTR .EQ. QAC.SEQ_CNTR)THEN
        IF(ABS(QAC.OFFSET - PACKET_COUNTER*394) .LT. 8192)THEN
          IF((QAC.ERROR_TYPE_FLAGS .NE. 8).AND.(QAC.ERROR_TYPE_FLAGS .NE. 32))THEN
            NEXT_EDB_QUALITY = -1 ! Temporarily use -1 for bad
          ENDIF
          CALL READ_QAC(2, QAC) 
        ENDIF
      ENDIF
C
C Check for beginning of an EDB
C
      IF( (PACKET(13).EQ.'EB'X) .AND. (PACKET(14).EQ.'90'X) .AND. (PACKET(15).EQ.'DA'X)
     &   .AND. (PACKET(16).EQ.'B'X) .AND. (PACKET(17).EQ.'F0'X) .AND. (PACKET(18).LE.19)) THEN
C
C Set flag to tell us that we already have the next packet
C
        ALREADY_HAVE_PACKET=.TRUE.      
C
C Check for a dummy
C
      ELSEIF( (PACKET(13).EQ.'EB'X) .AND. (PACKET(14).EQ.'90'X) .AND. (PACKET(15).EQ.'DD'X)
     &       .AND. (PACKET(16).EQ.'1'X) .AND. (PACKET(17).EQ.'7E'X)) THEN
C
C Set flag to tell us that we already have the next packet
C
        ALREADY_HAVE_PACKET=.TRUE.      
      ELSE
C
C Don't set ALREADY_HAVE_PACKET to .TRUE. if we didn't find a sync word when we
C expected it 
C
        EDB.QUALITY = -1
      ENDIF
C
C Move information for this EDB to appropriate structures
C
      CALL STRUCTURE_EDB( EDB_BYTE, EDB.START, EDB.HEADER, EDB.DATA, EDB.QUALITY )
C
C We have now stored an EDB. Return.
C
      RETURN
      END
