;+
; Project: SDAC
;
;  Name:   HKG_MODULE
;
;
; PURPOSE:  extract BATSE parameters from module housekeeping data.
;
;
; CATEGORY: BATSE
;
;
; CALLING SEQUENCE:  out = hkg_module( hkgmod, hseq, sequence_number )
;
;
; CALLED BY:
;
;
; CALLS:
;	
;
; INPUTS:
;       Hkgmod - hkgmod extracted from fdb, bdb, and hkg files using read_dd.
;		i.e. read_dd, ut, rates, housekeeping, HKGMOD=HKGMOD
;	Hseq   - Housekeeping sequence numbers.  Synched with HKGMOD from read_dd.
;	Sequence_number - sequence number, selects type of data read
;		Found in Table 2.3-3 on page 13 of BATSE Flight Software Users Manual
;		Some important ones:
;		Seq No. 	Mnemonic 	Function
;		4		HV5VLTm		Spectroscopy PMT's High Voltage
;		23		PBPTMPm		PMT baseplate temperature
;
; OPTIONAL KEYWORD INPUTS:
;	hkgmtb - if the HouseKeepingModuleTaBle is input, the slld values are returned.  
;	See page 54 of the BATSE Flight Software Users Manual.
;	While the L/A LLD is listed as byte 6, and the SLLD byte 7, we're using byte 7 since
;	the data is encoded as integers on a vms machine.  This is a bit dangerous but is expected
;	to continue to work through the mission.
;	
;
;
; OUTPUTS:
;       Returns 8 integer values, one for each detector.
;
; OPTIONAL OUTPUTS:
;	none
;
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none.
;
; RESTRICTIONS:
;	none.
;
; PROCEDURE:
;	The sequence number is used to decommutate the tabular data.
;
; MODIFICATION HISTORY:
;	
;       Version 1, Richard.Schwartz@gsfc.nasa.gov, 30-mar-1998.
;-
;
function hkg_module, hkgmod, hseq, sequence_number, hkgmtb=hkgmtb  


if not keyword_set(hkgmtb) then begin
	w = where(hseq eq sequence_number, nw)
	if nw ge 1 then begin
		tspec = total(hkgmod(*,w),1)
		wv    = where( tspec ne 0, nwv)
		if nwv ge 1 then return, hkgmod(*,w(wv(0)))
	endif


	return, 0
endif else begin

;Get the SPEC LLD setting
	tbl = bytarr(8,8)
	offset1= 2*(32-hseq(0))
	tblend = offset1 + 63
	while n_elements(hkgmtb) gt tblend do begin
		tbl(*) = hkgmtb(offset1:tblend)
		slld = (tbl(6,*))(*)
		offset1 = offset1+64
		tblend  = tblend+64
		if total(slld) ne 0 then tblend = n_elements(hkgmtb) ;done here
	endwhile		 
return, slld
endelse
return, 0
end
