;+
;  Name:
;       READ_DISCSC
;
;	Read DISCSC data files from MOPS format and optionally write
;	IDL save files in /xdr format with the data and time arrays and
;	the bit mask for the detectors.  Times are written in seconds
;	from 79/1/1.  
;
;Call:	read_discsc, file=filename, DISCSC_TIME, DISCSC, DSELB, save=save
;
;Input:	
;	file		complete file name, fits or native format
;	save=save	If set, then write a save file in /xdr
;	
;Outputs:	DSELB - A mask indicating the triggered detectors used for
;			DISCSC
;		DISCSC - 64 MS counter in 4 DISCLA channels
;			saved as an unsigned value in intarr(4,numbins)
;		DISCSC_TIME - Mid-point time dblarr(numbins) in sec fr. 79/1/1
;		ltime - Live time correction.  Divide DISCSC output
;				by ltime to obtain approximate rate
;				corrected for dead time effects
;Action:	File is opened and read into needed elements of burst_str
;		If Save set then:
;			IDL save file is written in /xdr format with the three 
;			output variables. File name is
;			SPEC_DATA:DISCSC_SAV.BURST_NO
;Restrictions:	Native format files are assumed created with VAX architecture.
;History:	ras, 31-jan-94
;-
;
pro read_discsc, file=filename, DISCSC_TIME, DISCSC, DSELB, save=save, $
	ltime = ltime

error = 0 ;assume no problems to start

@burst_str
@fdr_msfc_str
@audit_trail_str


break_file, filename(0), disk, dir, filnam, ext
if keyword_set(fits) or strpos(strlowcase(ext),'fits') ne -1 then itsfits=1 $
	else itsfits=0

nrep = 32  ;number of spectra in one packet
nch = 4
	
if itsfits then begin
	test = strupcase(filename(0))
	p1 = strpos(test,'B_') +2
	p2 = strpos(test,'.FITS') 
	burst_no = strmid( test,p1,p2-p1)
	head1 = headfits( filename(0))
	data_fits = readfits( filename(0), head2, /ext)
	nspec = n_elements( data_fits(0,*) )
	load_struct, data_fits, burst_str, dstrct
	
	npck = n_elements(dstrct)

endif else begin
	burst_no = strtrim(strmid(ext(0),1,5),2)
;************************************************************************
;HEADER RECORD
;

	test = bytarr(n_tags(burst_str,/length))
	
        files = findfile(filename, c=cnt)
        if cnt eq 0 then begin
                printx,'Error finding/reading file'
                error = 1
                return
        endif

        printx, 'Reading '+filename(0)
        openr,/get,lu,filename(0)

        readu,lu, test  ;READ HEADER RECORD
	load_struct, test, fdr_msfc, fdr, /noieee

;************************************************************************
;AUDIT RECORDS?
;
        if fdr.num_audit_rec ge 1 then begin
                audit = replicate( audit_trail, fdr.num_audit_rec)
                for i=0,fdr.num_audit_rec - 1 do begin ;READ AUDIT TRAIL
                        readu,lu,test
			load_struct, test, audit_trail, audit_trail, /noieee
                        audit(i) = audit_trail
                endfor & npck = audit(i-1).data_recs_num
        endif else npck= fdr.data_recs_num

;************************************************************************

	test = rebin( test, n_elements(test), npck)
	readu, lu, test
        load_struct, test, burst_str, dstrct, /noieee
	free_lun,lu
endelse
;************************************************************************



                 
discsc = reform( dstrct.sci, 4, 32*npck)
dselb = dstrct(0).dselb ;bit mask for detectors

;Do live time correction.  Correct only for
;equal rates in the selected detectors!  Usually only two detectors triggered.
;Livetime function set for DISCLA rates, at 1.024 seconds
;so multiply .064 s rate by 16 to obtain 1.024 s rate
bits, dselb, bitarr
ltime = livetime(discsc*16./total(bitarr),det=0)

packet_time= replicate(1,32,1) # sc_seconds_ed(dstrct.sctime)


discsc_time = (dindgen(32,1)#replicate(1d0,1,npck)*.064d0 + .032d0)
discsc_time = (discsc_time + packet_time)(*)

if keyword_set(save) then $
	save,file='discsc_sav.'+strtrim(burst_no,2),/xdr, discsc_time,discsc,dselb

return
end
