;+
; PROJECT:
;	SDAC
; NAME:
;	FS_ACC
;
; PURPOSE:
;	Reads and corrects BATSE DISCLA data
;	Try to use the active, present tense.
;
; CATEGORY:
;	BATSE
;
; CALLING SEQUENCE:
;	FS_ACC
;
;
; INPUTS:
;	None explicit, Control parameters are in common in FSCOM
;	
; KEYWORD PARAMETERS:
;	ERROR
;
; OUTPUTS:
;	Data values are placed in common in fs_saveccum
;
; COMMON BLOCKS:
;	fscom
;	fs_saveaccum
;
; RESTRICTIONS:
;	Maximum size of data array to be read is initiated by SETUP_ARRAYS
;
; PROCEDURE:
;	fs_acc accumulates BATSE data in the fsplot program. The accumulated
;	data is in common fs_saveaccum.  The discla array is corrected for
;	overflow and livetime. 
;
;
; MODIFICATION HISTORY:
; Written by Shelby Kennard & Kim Tolbert
;
; Mod. 11/18/91 by K.T.  The SECONDS array contains the midpoint
; of the 1.024 second data accumulation, and has twice as many elements
; as the position arrays (XPOS, YPOS, XRA, etc.) which are available only
; every 2.048 seconds.  When SECONDS is used for the position arrays, need
; to add .512 seconds to each time so that the time will be at the center
; of the 2.048 second interval corresponding to the position data.
;
; Mod. 10/4/93 by K.T. Call fs_read_dd instead of fs_readbdb.
;
; Mod. 07/1/94 by A.E.S. Refer to channels starting from 0 rather than 1.
; Affects only comments, not code.
; ras, 15-dec-95, new call to restore_overflow
; aes, 29-jul-96, checked to make sure wnzero isn't -1 before multiplying
;-
;***************************************************************************
;
pro fs_acc, error=error

@fscom
@fs_saveaccum


error = 0

; 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
;print,'nfound=',nfound                         
;print,'sec_st,sec_en = ',sec_st, sec_en
;print,'ovr_sec_st,ovr_-sec_en = ', 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_read_dd, dd_open=discla_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 (seconds(0:maxindex) eq 0.d0, count)
if count ne 0 then seconds(gaps) = seconds(0) + 1.024*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

; Call det_point to generate array of cosines for each detector.

; First need to fill in gaps in pointing data during omni periods if any.
radec_fill, pseconds, xra, xdec, zra, zdec, maxposindex, $
   error=error
if error then begin
   print,'Error.  No good pointing data found yet.  Start with a longer ' + $
      'time interval.'
   goto,errorexit
endif

getcos:
det_point, pseconds(0:maxposindex), xra(0:maxposindex), $
   xdec(0:maxposindex), p_ndx, cos_arr, $
   zra(0:maxposindex), zdec(0:maxposindex), bad_data=bad_data
if bad_data then begin
   print,'No valid pointing data in requested time interval.'
   goto,errorexit
endif
;
; Save just one set of cosines for 8 detectors
cos8 = cos_arr (*,0)
restore_overflow, discla, rotate(sort(cos8),2), maxindex=maxindex
;
;DISCLA5 (index 4) is now known to contain an independent
;signal below the threshold of DISCLA1.  Therefore we
;now subtract the others from it to obtain this signal for display.
;We have yet to determine the implication of this signal for
;livetime considerations.
discla(4,*,*) = discla(4,*,*) - total( discla([indgen(4),5],*,*),1)
;;;;;;;;;;;;;aes-29-jul-96
wnzero = where( discla(5,0,*) ne 0, nz)
if nz ge 1 then begin 
for i=0,7 do begin
        temp= (discla(4,i,*))(wnzero)
        jumper, temp, reset
        discla(4,i,wnzero) = reset
endfor
endif 

;print, 'Done with reset.'
;
;Eliminate need for summing count rates with new version of LIVETIME
;       ras, 30-dec-93
;
for i=0,7 do livet(i,0:maxindex) = $
        1.024*livetime( reform(discla(0:3,i,0:maxindex)),det_id=i)

;
; Calculate rate.
;
; livetime will only be 0 where count_raw is 0, so set it to 1 wherever
; it is 0 just so we can divide by it.
;
bad_data = where (livet(*,0:maxindex) le 0, count_bad)
if count_bad ne 0 then livet (bad_data) = 1.
; Don't divide by livetime for channel 5 (CPD detector), just 0-4
for ich=0,4 do begin
   discla(ich,0,0) = discla(ich,*,0:maxindex) / livet(*,0:maxindex)
   if count_bad ne 0 then discla (6*bad_data+ich) = 0.
endfor
;
; Get square of uncertainty for each point
;
;sq_error(?) = discla (?) / livet((?)^2
;if count_bad ne 0 then sq_error(?bad_data) = 0
;
ones_arr = bytarr (8,maxindex+1) + 1
if count_bad ne 0 then ones_arr(bad_data) = 0
;
;
goto,getout
;
errorexit:
error = 1
goto,getout
;
;
getout:
;
return&end
                                         
