;+  
;
; NAME: 
;	Load_sher
;
; PURPOSE:
;	Read SHER and SHERB into a single data structure, calibrate the
;	energy scale, read the DRMs for a single chosen solar burst,
;	correct for overflow, and convert to counts/sec/cm2/keV
;	or optionally just calibrate and save calibration info
;
; CATEGORY: I/O
;       BATSE
;
; CALLING SEQUENCE:
;   	load_sher, flare=flare, detids=detids, rsher=rsher, $
;	drm_sp=drm_sp, dsel=dsel, prob_dsel=prob_dsel, $
;	drm_files = drm_files, sdrm_files= sdrm_files, sher_files=sher_files,$
;	sherb_file = sherb_file
;
; INPUTS:
;	flare - SDAC archive number
;	detids - detectors to include in the calibration/accumulation
;	sher_files - sher filenames
;	sherb_file - sherb filename
;	drm_files - direct drm filenames
;	sdrm_files - scattering drm filenames
;	/prob_dsel - return the probable detectors used in the memory accum.
;	/auto	  - calibrate without interaction
;	/noplot	  - no plotting in calibration run 	
; OUTPUTS:
;	rsher - structure with sher and sherb spectra and time intervals      
;       drm_sp- structure with response matrix
;	dsel - IDs of the detectors in the memory (0-7)
;
; MODIFICATION HISTORY: 
;	ras, 18-Nov-93
;
;-
pro load_sher, flare=flare, intime=intime, detids=detids, rsher=rsher, $
	drm_sp=drm_sp, dsel=dsel, prob_dsel=prob_dsel, $
	drm_files = drm_files, sdrm_files= sdrm_files, sher_files=sher_files,$
	sherb_file = sherb_file, auto=auto, noplot=noplot


;


checkvar,detids,[0,1,2]
ndet = n_elements(detids)

nchan = 256
@flare_catalog
batse_read_cat

if n_elements(intime) ne 0 then begin
	intime=anytim(intime,/utime) 
	a = min( abs(fldata.start_secs - intime), min_in )
	flare = fldata(min_in).flare_num ;select the closest flare to INTIME
endif

fld = fldata(flare-1)
checkvar, intime, fld.start_secs

;Return the id's of the brightest LADS in channel 1(0-3)
;These should be the burst memory selected detectors
if keyword_set(prob_dsel) then begin
	
	peaktime = utime( tjd2ymd( fld.burst_tjd))+fld.burst_trig_secs
	endtime = peaktime > (fld.startsecs + 300.)
	fdbread, ut, y, flare=flare, endtime=atime(/hxrbs,endtime)
	a = min( abs(fld.burst_trig_secs+fld.burst_tjd - ut ), min_in)
	dsel = (reverse( sort(y(1,*,min_in))))(0:3)
     	printx, string(/print,'Detectors probably selected for memory accumulation are:',dsel)
	return

endif

@drm_sp_str
@rsher_str

drm_sp = replicate( drm_sp_str, ndet)
rsher  = rsher_str

;rsher_str = {rsher, det_id:0, rate:fltarr(256),start_time:0.d0,$
;		end_time:0.d0, units:'counts'}


break_file, sher_files, sher_disk, sher_dir, fshername, extsher
break_file, drm_files, drm_disk, drm_dir, fdrmname
break_file, sdrm_files, drm_disk, drm_dir, fsdrmname

for ii = 0,ndet-1 do begin 
       wibdb = where( strpos( strupcase(fshername),'IBDB') ne -1, nibdb)
	if  nibdb ge 1 then $
       isher =	where(strpos(STRUPCASE(fshername),$
		'IBDB_'+strtrim(detids(ii),2)) ne -1,ns) else $
		isher = where( extsher eq '.'+strtrim(detids(ii),2) ,ns) 
       idrm =	where(strpos(fdrmname,$	
		'_'+strtrim(detids(ii),2)+'_') ne -1,  ndrm)
       isdrm =	where(strpos(fsdrmname,$	
		'_'+strtrim(detids(ii),2)+'_') ne -1,  nsdrm)

shers_load, det_id=detids(ii), nsher=nsher, area=area, evec=evec,$
	sher_file=sher_files(isher), sherb_file = sherb_file(0), timerange=timerange,$
	seconds=seconds, dsel=dsel, auto=auto, noplot=noplot

edge_products, evec, width=wvec, mean=emvec
		
r = replicate( rsher_str, n_elements(nsher(0,*)))
start_time = (seconds(0,*))(*)
end_time = (seconds(1,*))(*)
deltat = end_time-start_time
	r.det_id = detids(ii)
	r.start_time = start_time
	r.end_time = end_time
	for k=0,n_elements(deltat)-1 do $
		r(k).rate = f_div(float(nsher(*,k))/area/deltat(k),wvec)
	r.units = 'counts cm!u-2!n s!u-1!n keV!u-1!n'

if ndrm eq 1 then begin
	read_bdas_drm, file=drm_files(idrm), drm=drm_in, $
		head=hd, area=area_sp
	read_bdas_drm, file=sdrm_files(isdrm), drm=sdrm_in, $
		head=shd, area=area_sp
;There is a bug in the smoothing parameter!!!!!!!
;	add_scatter_drm, ddrm= drm_in, sdrm= sdrm_in, new_drm=drm_in,$
;		shead = shd, dhead=hd, nsmooth=4

	add_scatter_drm, ddrm= drm_in, sdrm= sdrm_in, new_drm=drm_in,$
		shead = shd, dhead=hd, nsmooth=1
                                                                                        
	drm_sp(ii).drm=  x_eout_drm(in_drm=drm_in,out_e_out=evec, $
			in_e_out=hd.e_out(*,0:hd.nobins_out-1), nsub=10)
	drm_sp(ii).head = hd
	drm_sp(ii).head.nobins_out = nchan
	drm_sp(ii).head.e_out = evec
	
endif else printx,string(/print,'No DRM availabel for ',detids(ii))

rsher = temporary( [rsher, r])
endfor
rsher = rsher(1:*)
	
end

