;+
; NAME:
;	AVE_CTS
; PURPOSE: 
;	To obtain average count rates (counts/sec/subcollimator) of HXT 
;	in the four energy bands. 
; CALLING SEQUENCE:
;	ave = AVE_CTS(index,data)
;	ave = AVE_CTS(index,data,/ser,time=time)
;	ave = AVE_CTS(index,data,bgd)
; INPUT:
;	index	- HXT index
;	data	- HXT data
; OPTIONAL KEYWORD INPUT : 
;	serial	- If set, then the output data format is 
;		  ave = fltarr(4,*) [= (Ch,4*MF)] 
;		  instead of the default ave = fltarr(4,4,*) = (Ch,1/4MF,MF). 
;	sf	- If set, then the output data format is 
;		  ave = fltarr(4,*) = (Ch,MF). The output now contains 
;		  average count rates (in the four energy bands) per each MF.
;	time	- Returns time tag for each `ave' data. 
;		  Utplot will be done, for instance, as follows:
;			utplot,time,ave(0,*,*,*),inex(0)
;	bgd	- HXT background data structure variable. If this is set, 
;		  then background-subtracted data is obtained. 
; OUTPUT:
;	ave	- Average count rates of HXT in the four energy bands, in 
;		  units of CTS/S/SC. 
; HISTORY:
;	version 1.0	93.02.19 (Fri)
;	T.Sakao written.
;
;	version 1.1	93.03.22 (Mon)
;	Option ser is added. If this option is set return value s is 
;	s(4,*), otherwise s is s(4,4,*).
;	Option sf is also added. If this option is set counts are summed up 
;	every 1SF; s = s(4,*).
;
;	version 1.2	93.03.24 (Wed)
;	Greatly modified so that this program well work in both FL and QT 
;	mode, as well as in Bit HI, MED, and L.
;
;	version 1.3	93.05.14 (Fri)
;	time = time(4,*) unless option ser is set.
;
;	version 1.4	93.05.19 (Wed)
;	Option bgd is added. BGD subtraction can be done with this option.
;	Note the variable bgd is an HXT data structure.
;
;	version 1.5	93.06.17 (Thu)
;	bgd was changed to be an argument from an option.
;
;	version 1.6	93.06.18 (Fri)
;	A bug in time option was fixed. 
;
;	version 1.7	93.07.01 (Thu)
;	A bug, which appears in bit MED case when BGD subtraction is specified,
;	was fixed. 
;
;	version 1.8	93.07.28 (Wed)
;	One of the arguments for mk_timarr was changed from data to indgen(4). 
;	This is to avoid error in case QT data is passed.
;
;	version 1.9	93.12.24 (Fri)
;	A bug in time option was fixed. The bug appeared when the option /sf 
;	was specified. 
;
;       version 2.0	94.06.17 (Fri)  by M.Morrison
;       Modified the call to MK_TIMARR.
;               Used to be:    time = mk_timarr(index,indgen(4))-hxt_prestore(index)
;               Now is:        time = mk_timarr(index,4)-hxt_prestore(index)
;
;	version 2.1	96.01.22 (Mon)	Added documentation. 
;       version 2.2	96.01.23 (Mon)  by SLF - Merged changes (MDM + Sakao san)
;       version 2.3     97.01.08 (Wed)  by SLF - change 'prestore' call to 'hxt_prestore'
;-
function  AVE_CTS, index, data, bgd, serial=serial, sf=sf, time=time
  n_data = n_elements(data(0,0,0,*))

  if n_elements(bgd) ne 0 then dat = sbt_hxt_data(index,data,bgd,var=var) $
                    ; It is obvious that dat is decompressed in this case.
                    ; Corrections for different MF time intervals in different 
                    ; DP modes are not done in sbt_hxt_data.
  else dat = hxt_halfsec2(index,data) 

  if keyword_set(sf) then begin

    s = reform(rebin(sum64(dat),4,1,n_data))/64.*2.
    time = reform(mk_timarr(index,1)-hxt_prestore(index))
  endif else begin
    s = sum64(dat)/64.*2.
    time = mk_timarr(index,4)-hxt_prestore(index)
    if keyword_set(serial) then begin 
      s    = frame2serial(s)
      time = frame2serial(time)
    endif
  endelse

  return,s
end
