;+
; Name:
;	READ_STTE
; Purpose:
;	Read STTE data files from MOPS format. and 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_stte, Filename, Stte, Dselb
;
; Input:	
;	Filename - complete file name, fits or native format
;
; Outputs:	
;	Dselb - A mask indicating the triggered detectors used for
;		STTE after the trigger
;	Stte - A structure with pha channel, time in ut format, and det_id
;
; Keywords:
;	DSTRCT- The original data structure
;	DET_ID- Return the structure for only this detector.
; Restrictions:
;	Only works for STTE data with no gaps and no period of 4.096 msec without an
;	event (count rates usually over 10-100/4.096 sec)
; Action:
;	File is opened and read into needed elements of burst_str
; Mod:		
; ras, 22-mar-96, written from BATSE Flight Software Manual description
; ras, 22-Apr-96, call sc_seconds_ed with /no_offset
;-
;
pro read_stte, Filename,  Stte, Dselb, DSTRCT=DSTRCT, DET_ID=DET_ID, ERROR=ERROR

error = 0 ;assume no problems to start

@burst_str
@fdr_msfc_str
@audit_trail_str
filenamed = (findfile(filename,count=nfile))(0)
if nfile eq 0 then begin
	error=1
	print,"!!!!!!!!!Can't find "+filename
	return
endif
  
break_file, filenamed, disk, dir, filnam, ext
if keyword_set(fits) or strpos(strlowcase(ext),'fits') ne -1 then itsfits=1 $
	else itsfits=0

	
if itsfits then begin
	test = strupcase(filenamed)
	p1 = strpos(test,'B_') +2
	p2 = strpos(test,'.FITS') 
	burst_no = strmid( test,p1,p2-p1)
	head1 = headfits( filenamed)
	data_fits = readfits( filenamed, 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(filenamed, c=cnt)
        if cnt eq 0 then begin
                printx,'Error finding/reading file'
                error = 1
                return
        endif

        printx, 'Reading '+filenamed
        openr,/get,lu,filenamed

        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

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

        dstrct = replicate( burst_str, npck)
        for i=0, npck-1 do begin
                readu, lu, test
                load_struct, test, burst_str, burst_str,/noieee
                dstrct(i) = burst_str
        endfor
        free_lun,lu
;************************************************************************
endelse


;
; Check to see if we want the PHA for all detectors or one
;

odet_id=(dstrct.sci and 7l)(*)
if exist(det_id) then wdet=where(odet_id eq det_id, nwdet) else wdet=indgen(n_elements(dstrct))
if wdet(0) eq -1 then begin
	print,'No detectors match ',fcheck(det_id,-1)
	error=1
	return
endif

dselb = dstrct(0).dselb ;bit mask for detectors
stte  = replicate( {stte, pha:0B, det_id:0B, ut:0.d0 }, n_elements(dstrct.sci) )

stte.pha = (byte((dstrct.sci and 65535l) / 256))(*)
stte.det_id=(dstrct.sci and 7l)(*)
btime  = sc_seconds_ed(dstrct.time,/no_offset)
btimes = btime(uniq(btime))
;
; Time bits rollover ever 32 * 128 microseconds.  Find the rollovers and add n*4.096 ms to the time values
;
for i=0,n_elements(btimes) -1 do begin
	w = where(btime eq btimes(i), nw)
	time=(dstrct(w).sci and 255l) / 8 
	roll= where( time gt time(1:*), nroll)
	ist = 0 
	ien = roll(0)
	for j=0,nroll-1 do begin
		time(ist:ien) = time(ist:ien)+ j*32L
		ist = ien + 1
		if j eq (nroll-1) then ien = 128L*nw -1 else ien=roll(j+1) 
	endfor
	if ist le ien then time(ist:ien) = time(ist:ien)+j*32L
	stte(w(0)*128L:(w(0)+nw)*128L-1).ut = (time *128.0d-6 + btimes(i))(*)
endfor

stte = stte(wdet)

end 


