
pro accum_rate, xstart, rate, ncount, xend, done=done
;
;Given an array of counts in RATE, starting from XSTART, the fractional
;bin number, integrate RATE until NCOUNT is reached and return the fractional
;bin position, XEND.  For fraction bins, the counts are pro-rated to the bin
;size. A given bin runs from 0 to 1 so the mid-point is 0.5.
;The minimum accumulation time is 0.128 s.

;intialize sum of RATE

done = 0 
tot_rate = 0L

;load pro-rated counts from first bin

tot_rate = tot_rate + rate(xstart) * (1.0 - (xstart mod 1.0))
nrate = n_elements(rate)

for index = long(xstart) + 1l, nrate - 1 do begin ; keep adding until NCOUNT is reached

	tot_rate = tot_rate + rate(index < (nrate-1))
	if tot_rate ge ncount then goto, ADDNOMORE
endfor

;SUBTRACT EXCESS TO FIND EXACT BIN POSITION

ADDNOMORE:
	if index ge nrate then done=1
	index = index < (nrate-1)
	fraction = (tot_rate - ncount) / rate(index)

	xend = (index - fraction +1) > (xstart +0.128)

return
end


   function  memory_intrvls, flare=flare, ncount=ncount, t1=t1, t2=t2
;+
;  Name:
;       MEMORY_INTRVLS
;
;	Compute the BATSE SHERB accumulation intervals based on the
;	adjustable parameters in the on-board algorithm
;	
;	Inputs:
;		flare  - BATSE archive flare number
;
;	Optional inputs:
;		ncount - end accum. interval after this many counts in LAD
;		t1     - test time for 1/3 of accumulation intervals
;		t2     - test time for 2/3 of accum intervals
;
;	BATSE memory algorithm - 192 intervals - 96 for brightest detector
;	Accumulation continues until brightest LAD counts NCOUNT from start
;	of interval.  If there are 32 intervals accumulated before t1 then
;	NCOUNT changes to NCOUNT*2, if there are 64 intervals accumulated
;	before t2 then NCOUNT changes to NCOUNT*2.
;-

batse_read_cat
@flare_catalog


utbase = getutbase(0)
df_ncount = 5000.
if utbase lt utime('91/6/4') then df_ncount = 2000.0

df1 = 20.48
df2 = 200.0
if utbase lt utime('92/02/11') then begin
	df1 = 10.24
	df2 = 20.48
endif
checkvar, ncount, df_ncount
checkvar, t1, df1
checkvar, t2, df2

if n_elements(flare) ne 1 then begin
	print,'Error in input.  result=memory_intrvls( flare=xxx )
	return, 0
endif

end_time = fldata( flare-1).peak_secs + 600 < fldata(flare-1).start_secs + $
	fldata(flare-1).duration

fdbread, seconds, discla, cos8, live=livet, flare=flare, endt=atime(end_time)
trig = fldata(flare-1).burst_trig_secs


top = (reverse(sort(cos8)))(0) ;index of most sunward detector
rate = total(discla(0:3,top,*), 1)
counts = rate(*) * livet(top,*)
times = fltarr(97)
times(0)= trig

ncount_now = ncount ;current test value for count accumulation

;start of DISCLA intervals 
xstart = (trig - (seconds(0) - 0.512) )/ 1.024
times(0) = xstart

for i=0,31 do begin
	accum_rate, xstart, counts, ncount_now, xend, done=done
	times( i+1 ) = xend
	xstart = xend
	if done then goto, DONE
endfor

;if the accumulation is too fast, increase the test value
if (xstart-xend)*1.024  lt t1 then ncount_now = ncount_now * 2
	
for i=32,63 do begin
	accum_rate, xstart, counts, ncount_now, xend, done=done
	times( i+1 ) = xend
	xstart = xend
	if done then goto, DONE
endfor

;if the accumulation is too fast, increase the test value
if (xstart-xend)*1.024  lt t1 then ncount_now = ncount_now * 2

for i=64,95 do begin
	accum_rate, xstart, counts, ncount_now, xend, done=done
	times( i+1 ) = xend
	xstart = xend
	if done then goto, DONE
endfor

DONE:

times = seconds(0) + times * 1.024

return, times(0:(i+1)<96)
end

