;+
;
; NAME: 
;       FS_ACC_CONT
;
; PURPOSE:
;	fs_acc_cont accumulates BATSE CONT data in the fsplot program. 
;
; CATEGORY:
;       BATSE
;
; CALLING SEQUENCE:
;	FS_ACC_CONT
;
; CALLS:
;	CHECK_OVERFLOW, FS_OPEN, FS_READ_DD, DN_TRANS, FIX_CONT, SETUTBASE,
;
; INPUTS:
;       none explicit, only through commons;
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	ERROR - set if there is a problem.
; COMMON BLOCKS:
;	FSCOM, FS_SAVEACCUM, FS_SAVEPLOT
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
; The accumulated data is saved in common fs_saveaccum.  The CONT array is 
; corrected for overflow and livetime. 
;
; MODIFICATION HISTORY:
; Written by Kim Tolbert   11/93
; Modified 7/7/94 by Amy Skowronek so that detectors are referred to as 0-7
; when not aspect ordered and channels are always referred to as 0-(n-1)
;
; mod. amplifier dead time ras, 9-jul-94
; mod. 8/24/94 by AES  to call fix_cont to perform livetime and overflow
; calculations
; ras, 17-apr-95, changed to stream files for vms/unix compatibility
; Mod. 02/23/95 by RCJ to call 'fix_cont, cont_livet' instead of
;                      'fix_cont, clivet'
;
;	Version 7, richard.schwartz@gsfc.nasa.gov, revise documentation, 26-sep-1997.
;-
pro fs_acc_cont, error=error

@fscom
@fs_saveaccum
@fs_saveplot

error = 0

; Don't need to read data if data already accumulated in cont and position 
; arrays include the times requested.
if cdata_accumed then goto, got_data

print,'Accumulating CONT data...'
; See if any overflow intervals overlap with requested time interval.  If so,
; expand time interval to accumulate to include the 1 minute before the overflow
; started.
new_sec_st = sec_st
check_overflow, sec_st, sec_en, nfound, ovr_sec_st, ovr_sec_en
if nfound gt 0 then begin
   print, 'Correcting ', nfound, ' overflow intervals.'
   new_sec_st = min ([sec_st, ovr_sec_st-60.])
   xmin = sec_st
endif
;print, 'new_sec_st = ', new_sec_st, '  xmin = ',xmin


fs_open, flare=file2flare(discla_open.filename), dd_type='CONT', $
   dd_open=cont_open, /verbose, error=error

if error then goto, errorexit

fs_read_dd, dd_open=cont_open, startt=new_sec_st, endt=sec_en, error=error
if error then goto,errorexit

; Fill gaps in time with good times (gaps will appear as 0's because we
; initialized the time array to 0, then filled in the time and data arrays 
; using time as an index into the array).
gaps = where (cseconds(0:maxcindex) eq 0.d0, count)
if count ne 0 then cseconds(gaps) = seconds(0) + 2.048*gaps
gaps = where (pseconds(0:maxposindex) eq 0.d0, count)
if count ne 0 then pseconds(gaps) = pseconds(0) + 2.048*gaps

; Get day/night transition times, if any.

getdn:
dn_trans, pseconds(0:maxposindex), xpos(0:maxposindex), $
   ypos(0:maxposindex), zpos(0:maxposindex), d_mark, n_mark, ntrans=ntrans,$
   bad_data=bad_data

if bad_data then begin
   print,'No valid position data in requested time interval.'
   goto,errorexit
endif

fix_cont,cont_livet,count_bad=count_bad,error=error

cones_arr = bytarr (8,maxcindex+1) + 1
if count_bad ne 0 then cones_arr(bad_data) = 0
                                      
; (This is the breaking point between fs_acc and fs_gen_arr for DISCLA data.)

got_data:

; num_av is number of points after averaging cont_sam_av points together
num_av = (maxcindex+1) / cont_sam_av
limit = num_av * cont_sam_av

; rebin cseconds array to num_av points and offset to centers of time bins
sec_arr = cseconds(indgen(num_av)*cont_sam_av) + (cont_sam_av-1) * .512
setutbase,anytim(sec_arr(0),/sec,/date)


; cont_aspord (aspect ordered)=0/1 means choose detectors by number/angle to Sun

if cont_aspord then begin
; Figure out which detectors to use by their angles to sun (i.e. if operator
; selected detectors 1 & 2, use 2 most sunward detectors (largest cos(angle))
; det_sav will contain detectors to use in order of most to least sunward
  det_sorted = rotate (sort(cont_cos8), 2) ;sort and reverse order to descending
  cos_sorted = cont_cos8(det_sorted)
  det_sav = det_sorted (cont_det_arr - 1)
  cos_sav = cont_cos8(det_sav)
endif else begin
; choose detectors by their number and not by angle from Sun.
; mod by AES 6/28/94 so that detector id is between 0-7 if not aspect ordered
  det_sav = cont_det_arr 
  cos_sav = cont_cos8(det_sav)
endelse

ch_sav = cont_ch_arr
sam_av_sav = cont_sam_av
type_sav = 2

ndet = n_elements(det_sav)
nch = n_elements(cont_ch_arr)
out_arr = fltarr (num_av)

ones = fltarr(num_av)
rates = fltarr(num_av)

;---
; Loop over detectors

   for id = 0, ndet-1 do begin
      idet = det_sav(id)
;     get number of non-zero points in each output bin if cont_sam_av not = 1
      if cont_sam_av ne 1 then $
         ones(*) = rebin(reform(cones_arr(idet,0:limit-1)),num_av) * cont_sam_av
;
;     add all channels requested for this detector
      for ich = 0,nch-1 do begin
         ichan = cont_ch_arr(ich)
;        CHANGE: channels labelled 0-(n-1), so don't add one to cont_ch_arr
;        print, 'id=',id, '  ich=',ich, '  ichan=',ichan
;        if cont_sam_av not equal to 1,
;        add num_av discla rates together (rebin to num_av & multiply by sam_av)
;        and then divide by number of non-zero points in each sum (given 
;        in ones array) for this channel, this detector
         if cont_sam_av ne 1 then begin
            rates(*) = rebin (reform(cont(ichan,idet,0:limit-1)), num_av) * $
                       cont_sam_av
            out_arr(*) = out_arr + rates / (ones > 1.)
         endif else begin
            out_arr(*) = out_arr + cont(ichan,idet,0:num_av-1)
         endelse
      endfor  ; end of channel loop
   endfor  ; end of detector loop
   out_arr(*) = out_arr / ndet
;---

cdata_accumed = 1
goto,getout

errorexit:
error = 1
goto,getout


getout:
end
