CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
      SUBROUTINE GET_SR( SCI_UNIT, SCIENCE_RECORD )
C+
C
C FUNCTIONAL DESCRIPTION:
C
C     This routine places the data for one science record into a structure.
C     Twenty EDB's comprise one science record.
C
C FORMAL ARGUMENTS:
C
C     SCI_UNIT:
C	INT	  I	The stream id on which the science data file is open.
C
C     SCIENCE_RECORD:
C	/SCI_REC/ O	A record of structure type SCI_REC that, upon return,
C			will contain one science record's worth of data.
C
C INTERFACE:
!
!	INTERFACE GET_SR
!	  SUBROUTINE GET_SR( SCI_UNIT, SCIENCE_RECORD )
!	    INTEGER, INTENT(IN) :: SCI_UNIT
!	    RECORD /SCI_REC/ SCIENCE_RECORD
!	    INTENT(OUT) SCIENCE_RECORD
!	  END SUBROUTINE GET_SR
!	END INTERFACE GET_SR
C
C COMMON BLOCKS:
C
C     None
C
C INCLUDE FILES:
C
C     EDBDEF.INC
C
C DESIGN:
C
C     {tbs}
C
C IMPLICIT INPUTS:
C
C     None
C
C IMPLICIT OUTPUTS:
C
C     None
C
C SIDE EFFECTS:
C
C     None
C
C CREATED:
C
C	 22-SEP-1995    John Paquette
C
C This version:   1.10   11-SEP-1997
C
C DEVELOPMENT HISTORY:
C 
C  Author       Date		Description of Change 
C  --------	-----------	--------------------- 
C  J. PAQUETTE  22-SEP-1995     Get Commands, Limits, Deltas
C  J. PAQUETTE  25-SEP-1995     Get data from appropriate EDB
C  J. PAQUETTE  12-OCT-1995     Added comments
C  J. PAQUETTE  24-OCT-1995     Declared FRACTIONAL_EPOCH to be REAL*8 to avoid
C				loss of precision
C  J. PAQUETTE  27-OCT-1995     Correct error in Science record number byte
C				order
C  J. PAQUETTE  17-JAN-1996     Correct error in selection of the EDB to draw
C				SR-related info from; reset EDB_CNTR before
C                               transfer of "bad" data
C  J. PAQUETTE  19-JAN-1996     Add logic to prevent a subscript out of range
C                               condition at the end of data.
C  J. PAQUETTE   8-AUG-1996     Do not decrement EDB_CNTR when we have missing
C                               EDB's; do not set ALREADY_HAVE_EDB when the gap
C                               spans a science record 
C  J. PAQUETTE   8-AUG-1996     Decrement EDB_CNTR only if GAP is .TRUE.
C  J. PAQUETTE  11-SEP-1997 1.10 Get DELTA and act on it
C  L. Bleau	 3-NOV-1999	add comments
C-
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CDEC$ IDENT	'1.10'
      INCLUDE 'INC_DIR:EDBDEF.INC'
      INTEGER EDB_CNTR,LAST_EDB_CNTR, LENGTH, EDB_TO_USE
      INTEGER SCI_UNIT, I, INTEGER_EPOCH, DELTA
      REAL*8 FRACTIONAL_EPOCH
      LOGICAL ALREADY_HAVE_EDB, GAP 
      RECORD /SCI_REC/ SCIENCE_RECORD
      RECORD /EDB_ALL/ EDB
C
C Loop over the 20 EDB's ( numbered 0-19 ) in a science record.
C
      EDB_CNTR = -1
      DO 100 WHILE(EDB_CNTR .LT. 19) 
C
C Get enough data for one EDB, and the start time of that EDB
C
        IF (.NOT. ALREADY_HAVE_EDB) THEN
          CALL GET_EDB( SCI_UNIT, EDB, GAP, DELTA )
        ENDIF
        ALREADY_HAVE_EDB = .FALSE.
C
C First save the last value of the EDB counter; comparison to the current value
C will tell us if we have any missing EDB's. Then the EDB counter can be read 
C directly from the data for this EDB.
C
        LAST_EDB_CNTR = EDB_CNTR
        EDB_CNTR = EDB.START.COUNTER
C
C If a data gap whose length is a multiple of 20 EDB's occurs, then EDB_CNTR -
C LAST_EDB will be 1, and the test below to detect data gaps will fail.  If the
C data gap is more than 20 EDB's but EDB_CNTR is greater than LAST_EDB_CNTR
C anyway, the code below will erroneously attempt to place the EDB's from
C either side of the gap into the same SR.  To deal with  these conditions, we
C need to check the variable DELTA. DELTA was set (by GET_EDB) to the
C difference between the sequence counters of subsequent packets if there was a
C gap.  Since there are 8 packets in each of 20 EDB's, we have a gap lasting
C more than one science record if DELTA is >or = 160.  In fact, since EDB's are
C 8 packets and an incomplete EDB is not counted, we have a 20 EDB gap if
C DELTA > or = 153.
C
        IF(DELTA .GE. 153)THEN
            DO 30 I = LAST_EDB_CNTR + 1, 19
              SCIENCE_RECORD.EDB(I).QUALITY = -1        !temporarily using -1 for missing data.
              SCIENCE_RECORD.EDB(I).START.COUNTER = I
  30        CONTINUE
            EDB_CNTR = 19 ! Causes exit of the DO 100 loop
        ENDIF 
C
C If we have consecutive EDB's, the difference between EDB_CNTR and
C LAST_EDB_CNTR should be 1 
C
        IF((EDB_CNTR - LAST_EDB_CNTR).NE.1)THEN
C
C If EDB_CNTR - LAST_EDB_CNTR is 2 or more, we have some missing EDB's in this
C science record.  Mark them as missing, then continue on.
C
          IF(EDB_CNTR .GT. LAST_EDB_CNTR)THEN
            DO 50 I = LAST_EDB_CNTR + 1, EDB_CNTR - 1
              SCIENCE_RECORD.EDB(I).QUALITY = -1        !temporarily using -1 for missing data.
              SCIENCE_RECORD.EDB(I).START.COUNTER = I
  50        CONTINUE
            IF(EDB_CNTR .LT. 20)SCIENCE_RECORD.EDB(EDB_CNTR) = EDB
C
C If GAP is .TRUE., we encountered a data gap while attempting to complete an
C EDB; in this case, we have only the 1st packet of the EDB denoted by
C EDB_CNTR.  Since the next call to GET_EDB will provide the balance of this
C same EDB, we have to decrement EDB_CNTR.  If we didn't do this then
C LAST_EDB_CNTR and EDB_CNTR would be equal on the next pass through the DO 100
C loop, and a whole science record would (erroneously) appear to be missing.
C
            IF(GAP)EDB_CNTR = EDB_CNTR - 1
C
C If LAST_EDB_CNTR is > or = to EDB_CNTR, all the rest of this science record
C is missing.  Mark it as bad, exit the loop, and return.  Any missing EDB's at
C the beginning of the next science record will be caught by the DO 50 loop,
C above, the next time that this program is called. 
C
          ELSE
            DO 55 I = LAST_EDB_CNTR + 1, 19
              SCIENCE_RECORD.EDB(I).QUALITY = -1        !temporarily using -1 for missing data.
              SCIENCE_RECORD.EDB(I).START.COUNTER = I
  55        CONTINUE
            EDB_CNTR = 19 ! Causes exit of the DO 100 loop
C            ALREADY_HAVE_EDB = .TRUE.
          ENDIF
        ELSE
C
C Normal case, transfer data for 1 EDB
C
          SCIENCE_RECORD.EDB(EDB_CNTR) = EDB
        ENDIF
 100  CONTINUE
C
C Get information pertaining to the entire SR.  The last 6 bytes of DPU_STATUS
C in EDB's 8 and 18 contain an epoch time for the EDB.  4 bytes of these are
C the integer portion of the time.  Since these are actually 3 2-byte words in
C reverse order, we need to reorder the bytes to get an answer. 
C
C First try EDB 8
C
      LENGTH = SCIENCE_RECORD.EDB(8).HEADER.DPU_STATUS_LEN ! Shorthand variable
      EDB_TO_USE = 8
C
C If (for whatever reason) EDB 8 has no DPU_STATUS words, try EDB 18.  If
C that's empty too, the information for this SR is simply not present, so skip
C all this. 
C
      IF(LENGTH .EQ. 0)THEN
        LENGTH = SCIENCE_RECORD.EDB(18).HEADER.DPU_STATUS_LEN
        EDB_TO_USE = 18
      ENDIF
C
      IF(LENGTH.NE.0)THEN 
C
C The last 32 bits contain the integer portion of the EPOCH time
C
        INTEGER_EPOCH = ISHFT(ZEXT(SCIENCE_RECORD.EDB(EDB_TO_USE).DATA.DPU_STATUS(LENGTH - 1)), 24) + 
     &                  ISHFT(ZEXT(SCIENCE_RECORD.EDB(EDB_TO_USE).DATA.DPU_STATUS(LENGTH)), 16)     + 
     &                  ISHFT(ZEXT(SCIENCE_RECORD.EDB(EDB_TO_USE).DATA.DPU_STATUS(LENGTH - 3)), 8)  + 
     &                  ZEXT(SCIENCE_RECORD.EDB(EDB_TO_USE).DATA.DPU_STATUS(LENGTH - 2))
C                     
C The next 11 bits contain the fractional portion of the EPOCH time
C
        FRACTIONAL_EPOCH = ISHFT(ZEXT(SCIENCE_RECORD.EDB(EDB_TO_USE).DATA.DPU_STATUS(LENGTH - 5)), 3) +
     &                     IBITS(ZEXT(SCIENCE_RECORD.EDB(EDB_TO_USE).DATA.DPU_STATUS(LENGTH - 4)), 5, 3)
C
C Combine these as a real number.  This involves dividing the fractional part
C by 2 to the 11th (because there are 11 bits of fraction) which is 2048, then
C adding. 
C
        SCIENCE_RECORD.EPOCH = INTEGER_EPOCH + FRACTIONAL_EPOCH/2048
C
C Get the time in Gregorian format 
C
        CALL SOHO_EPOCH_TO_GREG( SCIENCE_RECORD.EPOCH, SCIENCE_RECORD.TIME.YEAR, SCIENCE_RECORD.TIME.DOY, 
     &                           SCIENCE_RECORD.TIME.HR, SCIENCE_RECORD.TIME.MIN, SCIENCE_RECORD.TIME.SEC, 
     &                           SCIENCE_RECORD.TIME.MSEC, SCIENCE_RECORD.TIME.MICRO )
C
C In these same EDB's, the 4-byte record number is also present
C
        SCIENCE_RECORD.RECORD_NO = ISHFT(ZEXT(SCIENCE_RECORD.EDB(EDB_TO_USE).DATA.DPU_STATUS(LENGTH - 15)), 24) + 
     &                             ISHFT(ZEXT(SCIENCE_RECORD.EDB(EDB_TO_USE).DATA.DPU_STATUS(LENGTH - 14)), 16) + 
     &                             ISHFT(ZEXT(SCIENCE_RECORD.EDB(EDB_TO_USE).DATA.DPU_STATUS(LENGTH - 13)), 8)  + 
     &                             ZEXT(SCIENCE_RECORD.EDB(EDB_TO_USE).DATA.DPU_STATUS(LENGTH - 12))
C
      ENDIF
C                     
C The information for one science record is now present in the structure 
C SCIENCE_RECORD.  Return.
C
      RETURN
      END
