	FUNCTION ENGB_DECODE, PACKET
;+
; Project     :	SOHO - CDS
;
; Name        :	ENGB_DECODE()
;
; Purpose     :	Decode CDS Engineering B packets.
;
; Category    :	Telemetry, Science
;
; Explanation :	Decodes CDS Engineering B packets to extract information to be
;		put into the FITS header.  Called from ENGB_STORE.
;
; Syntax      :	Result = ENGB_DECODE( PACKET )
;
; Examples    :	
;
; Inputs      :	PACKET	= The Engineering B packet, in the structure format
;			  {ANYPACKETPKT}.
;
; Opt. Inputs :	None.
;
; Outputs     :	The result of the function is an output structure containing
;		various housekeeping parameters.  Each tag in the structure is
;		a four-element array, with one value for each GIS detector.
;
; Opt. Outputs:	None.
;
; Keywords    :	None.
;
; Calls       :	None.
;
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	If the packet is not an Engineering B packet, then all values
;		are set to -1.
;
;		Everything is returned as a short integer, including those
;		which were originally byte values.
;
; Prev. Hist. :	None.
;
; History     :	Version 1, 24-Aug-1995, William Thompson, GSFC
;		Version 2, 13-Oct-1995, William Thompson, GSFC
;			Split off averaging of packets to a separate routine.
;			Everything is returned as short integer.
;
; Contact     :	WTHOMPSON
;-
;
	ON_ERROR, 2
;
;  Check the number of parameters.
;
	IF N_PARAMS() NE 1 THEN MESSAGE,	$
		'Syntax:  Result = ENGB_DECODE( PACKET )'
;
;  Decide whether or not the packet is actually an Engineering B packet.  If
;  not, then set everything to -1.
;
	OKAY = PACKET(0).HDR.ID EQ '0B'X
;
;  Decode the packet.  Note that one needs to subtract 20 from the byte
;  positions in the file new_database.txt to get the correct position.  Note
;  also that bytes don't always seem to be in the order one would expect.
;
	DATA = PACKET.PKT.DATA
;
;  High voltage
;
	BUGHV = FIX([DATA(109), DATA(108), DATA(111), DATA(110)])
	IF NOT OKAY THEN BUGHV = REPLICATE(-1, 4)
;
;  Lookup table checksum
;
	BKGCHK = [FIX(DATA(186:189),0,2), FIX(DATA(218:221),0,2)]
	IEEE_TO_HOST, BKGCHK
	IF NOT OKAY THEN BKGCHK = REPLICATE(-1, 4)
;
;  Detector processed event count.
;
	BKGPCD = FIX(DATA(148:155),0,4)
	IEEE_TO_HOST, BKGPCD
	IF NOT OKAY THEN BKGPCD = REPLICATE(-1, 4)
;
;  High voltage current
;
	BIGMCP = FIX([DATA(113), DATA(112), DATA(115), DATA(114)])
	IF NOT OKAY THEN BIGMCP = REPLICATE(-1, 4)
;
;  MCP F/face detector
;
	BUGMCPB = FIX([DATA(117), DATA(116), DATA(119), DATA(118)])
	IF NOT OKAY THEN BUGMCPB = REPLICATE(-1, 4)
;
;  Detector raw event count
;
	BKGRCD = FIX(DATA(156:163),0,4)
	IEEE_TO_HOST, BKGRCD
	IF NOT OKAY THEN BKGRCD = REPLICATE(-1, 4)
;
;  Detector upper level discriminator count
;
	BKGULCD = FIX(DATA(164:171),0,4)
	IEEE_TO_HOST, BKGULCD
	IF NOT OKAY THEN BKGULCD = REPLICATE(-1, 4)
;
;  High voltage last set value
;
	BKGHV_L = FIX([DATA(173), DATA(172), DATA(175), DATA(174)])
	IF NOT OKAY THEN BKGHV_L = REPLICATE(-1, 4)
;
;  Detector last setting low level discriminator
;
	BKGLDLD = [DATA(185) AND 'F'XB, ISHFT(DATA(185) AND 'F0'XB, -4), $
		   DATA(184) AND 'F'XB, ISHFT(DATA(184) AND 'F0'XB, -4)]
	BKGLDLD = FIX(BKGLDLD)
	IF NOT OKAY THEN BKGLDLD = REPLICATE(-1, 4)
;
;  Form the output structure.
;
	RETURN,{ENGB_PKT,		$
		BUGHV:   BUGHV,		$
		BKGCHK:  BKGCHK,	$
		BIGMCP:  BIGMCP,	$
		BUGMCPB: BUGMCPB,	$
		BKGRCD:  BKGRCD,	$
		BKGPCD:  BKGPCD,	$
		BKGULCD: BKGULCD,	$
		BKGHV_L: BKGHV_L,	$
		BKGLDLD: BKGLDLD}
;
	END
