;+
;NAME:
;	hxtsbt_mult
;PURPOSE:
;	Subtracts the background from the image count rate, and rearranges
;	the order of the data so that the correct count rates are matched to 
;	the correct Sub Collimators.
;CATEGORY:
;       Yohkoh HXT
;CALLING SEQUENCE:
;	hxtsbt_mult,hxi_index,counts,bcounts,fobs,sig
;INPUT:	
;	hxi_index= Structure with info pertaining to image
;	tags:
;	hxi_index.actim= Flare accumulation time
;	hxi_index.bkg_actim= Background accumulation time
;	counts= Flare total counts, all SC's, all channels
;	bcounts= Background counts
;OUTPUT:
;	fobs= Counts - Background
;	sig= Uncertainty in fobs
;KEYWORDS;
;       b_zero= if set do not subtract the background, for the alternative
;               background option
;MODIFICATION HISTORY:
;	Adapted from Fortran routine HXTSBT of T. Sakao, by J McTiernan, July '92
;       Made useful for multiple channels and intervals 29-oct-93 (JMM)
;       Added the csigma and bsigma keywords, for improved uncertainty
;       calculation, ala Tom M's hxt_back_sub.pro, 1-10-95, jmm.
;       Removed the call to setgra, which gives the subscripts for
;       the subcollimator switch, and added into HXTIMG_ACCUM, 6-apr-95, jmm.
;       I don't think anyone will notice.
;-
PRO Hxtsbt_mult, hxi_index, counts, bcounts, fobs, sig, b_zero=b_zero, $
                 csigma=csigma, bsigma=bsigma
   
   grdnum = 64                    ;number of sc's
   nc0 = 4                        ;number of channels
   n_intv = N_ELEMENTS(counts(0, 0, *))

; obtain background count rate
   brate = fltarr(grdnum, nc0)
   bsig2 = brate
   btim = hxi_index(0).bkg_actim/10.0
   IF(btim LE 0.0) OR (KEYWORD_SET(b_zero)) THEN brate(*) = 0.0 ELSE brate = bcounts/btim

; obtain uncertainty in bcounts, if not input, squared
   IF(KEYWORD_SET(bsigma)) THEN bsig2 = bsigma^2 else bsig2 = bcounts

; now do the background subtraction
; exrate= excess count rate
   ctim = hxi_index.actim/10.0
   fobs = fltarr(grdnum, nc0, n_intv)
   sig = fobs
   FOR k = 0, n_intv-1 DO FOR i = 0, nc0-1 DO BEGIN
      cnts = counts(*, i, k)
; Uncertainty in cnts, squared
      IF(KEYWORD_SET(csigma)) THEN csig2 = csigma(*, i, k)^2 $
        ELSE csig2 = cnts
      fobs(*, i, k) = cnts-brate(*, i)*ctim(i, k) ;excess counts
      sig(*, i, k) = sqrt(csig2+bsig2(*, i)*(ctim(i, k)/btim)^2) ;sigma(fobs),
;altered to give the same result as mk_hxt_sbt.pro, for debugging purposes,
;jmm, 12-29-94, note that this is the right way to do it.
   ENDFOR
   RETURN

END

