	PRO LOAD_SCIENCE, PACKET, SCIENCE, VALID
;+
; Project     : SOHO - CDS
;
; Name        : LOAD_SCIENCE
;
; Purpose     : Stores science data from packet stream into array.
;
; Explanation : This routine extracts the (compressed) science data bytes
;		from a packet, and loads them into arrays for later processing.
;
; Use         : LOAD_SCIENCE, PACKET, SCIENCE, VALID
;
; Inputs      : PACKET	= Exposure header or science packet containing science
;			  telemetry data.  Has either the {EXPHEADERPKT} or
;			  {SCIPACKETPKT} structure from TM_DEFINE_STRUC.
;
;		SCIENCE	= Byte array containing the science telemetry data that
;			  has been collected so far.  If PACKET is an exposure
;			  header, then SCIENCE and VALID will be reinitialized.
;
;		VALID	= Byte array containing validity data for the bytes in
;			  the SCIENCE array.  The values of VALID must be
;			  either all bits set ('FF'XB) for valid data bytes, or
;			  all bits not set ('00'XB) for missing data bytes.
;			  VALID will have the same number of elements as
;			  SCIENCE.
; Opt. Inputs : None.
;
; Outputs     : SCIENCE	= Byte array containing the previously collected
;			  science telemetry data combined with the data stored
;			  in PACKET.  The size of this array will grow to
;			  accommodate the added data.
;
;		VALID	= Will also be updated to reflect the changes to
;			  SCIENCE.
;
; Opt. Outputs: None.
;
; Keywords    : None.
;
; Calls       : None.
;
; Common      : None.
;
; Restrictions: None.
;
; Side effects: The number of bytes in the SCIENCE and VALID arrays will in
;		general be bigger than the number of bytes actually extracted
;		from the science telemetry stream.
;
; Category    : Data Handling, Telemetry.
;
; Prev. Hist. : None.
;
; Written     : William Thompson, GSFC, 24 March 1993.
;
; Modified    : Version 1, William Thompson, GSFC, 20 April 1993.
;		Version 2, William Thompson, GSFC, 7 June 1993.
;			Modified for version 2.8 of packet format document.
;		Version 3, William Thompson, GSFC, 17 November 1993.
;			Changed way arrays are reinitialized, to speed
;			response.
;
; Version     : Version 3, 17 November 1993.
;-
;
	ON_ERROR, 2
;
;  Check the number of parameters.
;
	IF N_PARAMS() NE 3 THEN MESSAGE,	$
		'Syntax:  LOAD_SCIENCE, PACKET, SCIENCE, VALID'
;
;  If SCIENCE and VALID haven't been defined yet, then initialize them.
;
	IF N_ELEMENTS(SCIENCE) EQ 0 THEN BEGIN
		SCIENCE = BYTE(0)
		VALID	= BYTE(0)
	ENDIF
;
;  Make sure that SCIENCE and VALID are both byte arrays, and that their sizes
;  are consistent with each other.
;
	SZ = SIZE(SCIENCE)
	IF SZ(SZ(0)+1) NE 1 THEN MESSAGE, 'SCIENCE must be a byte array'
	SZ = SIZE(VALID)
	IF SZ(SZ(0)+1) NE 1 THEN MESSAGE, 'VALID must be a byte array'
	IF N_ELEMENTS(SCIENCE) NE N_ELEMENTS(VALID) THEN MESSAGE,	$
		'SCIENCE and VALID must have the same number of elements'
;
;  If the packet is an exposure header, then reinitialize SCIENCE and VALID.
;
	COUNTER = PACKET.PKT.PACKETCOUNTER AND '7FFF'X
	IF COUNTER EQ 0 THEN BEGIN
		SCIENCE = BYTARR(N_ELEMENTS(SCIENCE))
		VALID   = BYTARR(N_ELEMENTS(VALID))
	ENDIF
;
;  Determine the position of the first byte of the science data stored in
;  PACKET in the new SCIENCE array, and the length of the new array.
;
	POSITION = (252 + 276*(COUNTER - 1L)) > 0
	NN = N_ELEMENTS(PACKET.PKT.SCIENCE)
	LENGTH = POSITION + NN
;
;  If necessary, increase the size of the SCIENCE and VALID arrays, in
;  increments of 100000 bytes.
;
	IF LENGTH GT N_ELEMENTS(SCIENCE) THEN BEGIN
		SCIENCE = [SCIENCE,BYTARR(100000)]
		VALID   = [VALID,  BYTARR(100000)]
	ENDIF
;
;  Merge together the old and new data.
;
	SCIENCE(POSITION) = PACKET.PKT.SCIENCE
	VALID(POSITION)	  = REPLICATE('FF'XB,NN)
;
	RETURN
	END
