;+
;  Name:
;       READ_MER
;
;	Read MER 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.
; CATEGORY:
; 	BATSE
;Calling Sequence:
;	read_mer, file=filename, mer_time, mer, DSELB, dstrct=dstrct
;
;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
;			MER
;                MER - Intarr(4096,16) - 16 energy channels from the selected
;		LADs given by DSELB.  First 2048 bins accumulated over 16msec
;		and the last 2048 bins over 64msec.
;Restrictions:
;		Only works for MER data with no gaps
;
;Procedure:
;	File is opened and read into needed elements of burst_str
;History:
;Mod:		ras, 2-feb-94
; ras, 3-mar-94, corrected time conversion to include no packet offset 
;		 and use burst load time not burst start time
; ras, 16-jun-94, revise documentation
;-
;
pro read_mer, file=filename, mer_time, mer, DSELB, dstrct=dstrct

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 = 8  ;number of spectra in one packet
nch = 16
	
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

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

        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


mer = intarr(4096,16) 
mer(0) = (dstrct.sci)(*)

dselb = dstrct(0).dselb ;bit mask for detectors

mer_time = (sc_seconds_ed( dstrct(0).bstlt,/no_offset))(0)+ $	;ras, 9-mar-94
	[.008+.016*dindgen(2048),.032+.064*dindgen(2048)+32.768]
 


return
end
