;+
;
; NAME: 
;       FS_TESTGEN
;
; PURPOSE:
;
;
; CATEGORY:
;       BATSE
;
; CALLING SEQUENCE:
;
;
; CALLS:
;	none
;
; INPUTS:
;       none explicit, only through commons;
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	none
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;
;-
pro fs_gen_arr, error=error, f_start = f_start, f_end = f_end, $
                b_start = b_start, b_end = b_end, b_num = b_num
;
;
@fscom
@fs_saveaccum
@fs_saveplot
;
error = 0
; 
; num_av is number of points after averaging sam_av points together
num_av = (maxindex+1) / sam_av
limit = num_av * sam_av
;
; rebin seconds array to num_av points and offset to centers of time bins
sec_arr = seconds(indgen(num_av)*sam_av) + (sam_av-1) * .512
setutbase,anytim(sec_arr(0),/sec,/date)
;
; aspord (aspect ordered) = 0/1 means choose detectors by number / angle to Sun
;
if 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(cos8), 2) ;sort and reverse order to descending
  cos_sorted = cos8(det_sorted)
  det_sav = det_sorted (det_arr - 1)
  cos_sav = cos8(det_sav)
endif else begin
; choose detectors by their number and not by angle from Sun.
  det_sav = det_arr - 1
  cos_sav = cos8(det_sav)
endelse

ch_sav = ch_arr
sam_av_sav = sam_av
type_sav = 1

ndet = n_elements(det_sav)
nch = n_elements(ch_arr)
out_arr = fltarr (num_av)
;
ones = fltarr(num_av)
rates = fltarr(num_av)

; if channels 4, 5, or 6 were selected, check to see if any part of interval
; is in omni mode.  If so, write message.
if (where(ch_arr ge 4))(0) ne -1 then begin
   qomni = where(discla(0,0,0:limit-1) ne 0 and $
      discla(3,0,0:limit-1) eq 0, komni)
   if komni gt 1 then begin
      print, ' '
      print, 'WARNING:  Data for Channels 4-6 are zero from ' + $
         atime(seconds(qomni(0)),/hxr) + ' to '
      print,'  ' + atime(seconds(qomni(komni-1)),/hxr) + '.  During this ' + $
         'period, the omni directional antenna was'
      print, '  used, requiring a compressed data format.  Only Channels ' +$
         '1-3 are available.'
      print, ' '
   endif
endif

if sub_back ne 1 then begin
   for id = 0, ndet-1 do begin
      idet = det_sav(id)
;     get number of non-zero points in each output bin if sam_av not = 1
      if sam_av ne 1 then $
         ones(*) = rebin (reform(ones_arr(idet,0:limit-1)), num_av) * sam_av
;
;     add all channels requested for this detector
      for ich = 0,nch-1 do begin
         ichan = ch_arr(ich)-1
;        print, 'id=',id, '  ich=',ich, '  ichan=',ichan
;        if 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 sam_av ne 1 then begin
            rates(*) = rebin(reform(discla(ichan,idet,0:limit-1)),num_av)*sam_av
            out_arr(*) = out_arr + rates / (ones > 1.)
         endif else begin
            out_arr(*) = out_arr + discla(ichan,idet,0:num_av-1)
         endelse
;        stop
      endfor  ; end of channel loop
   endfor  ; end of detector loop
   out_arr(*) = out_arr / ndet
;
endif else begin
;  if want to subtract background and correct for angle, do it here
;  bckind will hold the indices into the rates array for the background 
;  interval(s).
   bcks = fix ( (b_start - sec_arr(0)) / (1.024d0*sam_av) )
   bcke = fix ( (b_end - sec_arr(0)) / (1.024d0*sam_av) )
   bckind = bcks(0) + indgen(bcke(0) - bcks(0) + 1)
   if b_num gt 1 then bckind = [bckind, bcks(1) + indgen(bcke(1) - bcks(1) + 1)]
;  flrind will hold the indices into the flare interval.
   flrs = fix ((f_start - sec_arr(0)) / (1.024d0 * sam_av))
   flre = fix ((f_end - sec_arr(0)) / (1.024d0 * sam_av))
   if flre gt maxindex then flre = maxindex 
   flrind = flrs + indgen(flre - flrs + 1)
;  noflrind will hold the indices into everything outside of the flare.
;  noflrind = [indgen(flrs), flre + indgen(num_av-flre-1) + 1]
;
;  We'll need to multiply rates by .988 (which is 2000/2025 to scale to 
;  counts/s/(2000cm**2) )
   scale = .988
   ave_rates = fltarr(num_av)
   binterp = fltarr(num_av)
   for ich = 0, nch - 1 do begin
      ichan = ch_arr(ich)-1
      numer = fltarr(num_av)
      denom = fltarr(num_av)
      for id = 0,ndet-1 do begin
         idet = det_sav(id)
;        print, 'ich,id=',ich,id,'  ichan,idet=',ichan,idet
         cosine = cos_sav(id)
         if cosine lt .259 then begin
            det_sav = det_sav(0:id-1)
            ndet = n_elements(det_sav)
            print, 'Detector ',idet, ' is too far from Sun, cosine=',cosine
            print, 'Don''t use it.  New number of detectors is ',ndet
            goto,done_det
         endif
         aluminum, cosine, alum
         corr = cosine * alum
;        print, 'corr = ', corr
         if sam_av ne 1 then begin
;           get number of non-zero points in each output bin
            ones(*) = rebin (reform(ones_arr(idet,0:limit-1)), num_av) * sam_av
;           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
            rates(*)=rebin(reform(discla(ichan,idet,0:limit-1)),num_av) * sam_av
            ave_rates(*) = rates / (ones > 1.)
         endif else begin
            ave_rates(*) = discla(ichan,idet,0:num_av-1)
         endelse
         coeff = poly_fit (sec_arr(bckind) - getutbase(0), $
                 ave_rates(bckind), 1)
         binterp(*) = poly (sec_arr - getutbase(0), coeff)
         zeros = where (ave_rates eq 0., zcount)
         if zcount gt 0 then binterp(zeros) = 0.
;         out_arr(noflrind) = out_arr(noflrind) + $
;                             (ave_rates(noflrind) - binterp(noflrind))
         numer(*) = numer + corr(ich) *(ave_rates - binterp) / $
                    (ave_rates > 1.)
         denom(*) = denom + corr(ich)^2 / (ave_rates > 1.)
;        print,'end of detector loop'
;        stop
      endfor  ; end of detector loop
done_det:
      out_arr = out_arr + scale * (numer/denom)
;     print,'end of channel loop'
;     stop
   endfor   ; end of channel loop
;   out_arr(noflrind) = out_arr(noflrind) / n_elements(det_sav)
endelse

;
;stop
;
getout:
;
return&end
