
;+
; PROJECT:
;	YOHKOH
; CATEGORY:
;	HXT, INSTRUMENT
; NAME: 
;  	AVE_CTS2
;
; PURPOSE:
;  This function returns the  counts/sec/subcollimator value from given data(....,64,....).
;  Note that 'data' should be in unit of cts / 0.5 sec for each sensor 
;  (should be decompressed from 8 to 12 bit beforehand).
;  N.B. data should not be decompressed when use CALIBRATION data
;  These data are stored into 8 bit counters only so are transmitted
;  without compression.
;	
; CALLING SEQUENCE:
;	hxt_rate = ave_cts2( index, data, time=time $
;		   [,sf=sf] [,ser=ser] [,dt=dt]	
; INPUTS:
;	index - index returned by yodat for hda file
;	data  - data array returned by yodat corresponding to index
;
; OPTIONAL INPUTS:
;	sf    - average over the frames
;	ser   - serial, return with hxt_rate = fltarr(4,4*n_index)
;			            time     = fltarr(4*n_index)	
;	datarectypes - if this value is 2, then these are calibration data
;			which is 64 channels x 64 detectors every 8 seconds
;			uses only the first value whether array or not
;			so let caller beware.	
; OUTPUTS:
;	hxt_rate - hxt cnt rate in each energy bin per second per
;		   subcollimator
;	           returned as fltarr(4,4,n_index) where n_index is
;		   the number of elements in index
; OPTIONAL OUTPUTS:
; 	time  - time in seconds relative to the time given by index(0)
;	        time is centered within the accumulation interval
;	dt    - accumulation time for each sample
;	iout64- index structure for 64 channel hxt data
;	dout64- data records for 64 channel hxt data (intarr(64, 64, n)
;
; PROCEDURE: data is extracted and converted in groups of 64 major
;	     frames to avoid page fault delays
;
; RESTRICTIONS:
;
; MODIFICATION HISTORY:
;  19-apr-94 R.Schwartz, adopted from ave_cts.pro
;  eradicated do loops and added documentation
;  21-jun-94, ras, replace div with f_div
;  22-jun-94, ras, changed mk_timarr 
;  01-jul-94, ras, fixed bug induced from dprate2sec not returning a vector
;		   for a single element
;  16-jan-1996, ras, add calibration data format
;  31-jan-1996, ras, added calibration data overflow correction, dout64, iout64
;   2-may-1996, ras, fixed error in subscripting non-existent dtypes
;   8-jan-1996, ras, prestore==>hxt_prestore
;  15-jan-1996, ras, protect use of dtype
;   9-feb-1997, ras, switch to hx_decomp from hxt_decomp, use the McTiernan version
; CONTACT:
;	richard.schwartz@gsfc.nasa.gov
;-
function ave_cts2, index, data, time=time, sf=sf, ser=ser, dt=dt_out, $
	datarectypes=dtypes, iout64= iout, dout64=dout

if (fcheck(dtypes,0))(0) eq 2  then begin
	w = where(dtypes  eq 2)
;
;	Provide a spigot to look at the data detector by detector
;
	;common ave_cts2_64com, iout, dout
	reform_hxtcal2,  index(w),  data(*,*,*,w),  iout,  dout
;
; These data have not been compressed.  They are full scale
; but can suffer from overflow during a flare.  Hence use
; my time-series overflow correction algorithm, restore_overflow.
; In this algorithm the data are first scaled as though they are
; 16 bit data and are corrected on the basis of continuity in
; the time series.
;
	dout = dout * 1 ;correct possible overflow
	restore_overflow, dout
	dout = fix( dout )
	n_data = n_elements(iout)
	time = (mk_timarr(iout,1) - 8.0625 )(*)  ;after discussion with Kosugi-san
	dt_out = dprate2sec(iout)*4
	out = f_div( total(dout,2), rebin(reform(dt_out,1,n_data), 64, n_data) )/64.
endif else begin


n_data = n_elements(data(0,0,0,*))
out = fltarr(4, 4, n_data)
time= fltarr(4,n_data)
dt_out = time

;Break into groups of 64 frames to reduce page faults
;caused by accessing indices throughout a huge array

for i=0, n_data-1, 64 do begin
  n_datai = (n_data-i) <  64
  wi = i + indgen(n_datai)
  mode   = gt_dp_mode(index(wi))

  wnoflare= where(mode ne 9, noflare)


  dt = dprate2sec(index(wi))/4. + wi*0.0 ;time duration of each full frame
  dt = rebin(reform( dt, 1, 1, n_datai),4,4,n_datai)

  dat = total(hx_decomp(data(*,*,*,wi))*1.,2)/ 64.

;for non-flare data fill in from housekeeping
  if noflare ge 1 then begin
	temp = reform( $
	 total(hx_decomp(index(wi(wnoflare)).hxt.dhk_data)*1.,1)/64.,1,1,noflare)
	dat(0,*,wnoflare) = rebin(temp,1,4,noflare)/4.	;average the counts over 4 samples
  endif

out(0,0,i)= f_div(dat,dt)
dt_out(0,i) = reform(dt(0,*,*))

endfor

;time = reform(mk_timarr(index,indgen(4))-hxt_prestore(index))
;change mk_timarr to preferred standard, ras, 22-jun-94
time = reform(mk_timarr(index, 4)-hxt_prestore(index))

if keyword_set(sf) then begin
  out = reform(rebin( out, 4, 1, n_data))
  time= reform(rebin(time, 1,n_data))
  dt_out = total(dt_out,1)
endif else begin
  if keyword_set(ser) then begin
    out = reform(out,4,4*n_data)
    time= time(*)
    dt_out = dt_out(*)
  endif
endelse

endelse

return, out
end
