;+ ;function spex_bkgrd, t_d, rate, trange1, trange2, order, sigma=sigma, $ ; selected=selected, ltime=ltime ; ;PURPOSE: Fit the count rate, RATE, to a polynomial of order, ORDER, over ;the background intervals specified by the limits in TRANGE1 and TRANGE2. ;Return the value of the fit over the time range, T_D. ;Inputs: ; ; t_d - time array, n or (2 x n) where n is the number of time bins. ; if t_d is dimensioned 2xn, then the first column is the ; start times and the second column is the end times. ; if t_d is dimensioned n, then intervals are uniform and ; t_d is the center of each interval. t_d is monotonic. ; ; rate - count rate vector, n elements. ; ltime - livetime of each interval in rate, used for weighting ; ; selected - indices of selected points in t_d and rate to use in fit ; or ; trange1 - 2 points on the range covered by t_d. Should be prior ; to the event (flare). ; trange2 - 2 points after the event along t_d. ; ; order - polynomial to fit over the ranges specified by ; trange1 and trange2 ;If it is ;Keyword SIGMA is the average standard deviation in the fit ; ;Default ORDER =1 ;Uses POLY and POLY_FIT ; ;RAS, 92/1/27 ;17-oct-93, modified to take time arrays with start and stop edges ;- function spex_bkgrd, t_d, rate, trange1, trange2, order, sigma=sigma, $ selected=selected, ltime=ltime checkvar, order,1 order_t = (size(t_d))(0) ;what are the dimensions of t_d if order_t eq 1 then begin ; transform to edges ntd = n_elements( t_d) -1 xedges = [1.5* t_d(0)-.5*t_d(1), ( t_d(1:*)+t_d) / 2., $ 1.5*t_d(ntd) + 0.5*t_d(ntd-1) ] endif else xedges = [ (t_d(0,*))(*), t_d(1, n_elements(t_d(1,*))-1)] if not keyword_set(selected) then begin ;******************* ;TRANSFORM THE TIME RANGES INTO INDEX RANGES t1 = trange1(sort(trange1)) t2 = trange2(sort(trange2)) n1s = ((where( xedges ge t1(0), nx))(0) -1)>0 n1e = ((where( xedges ge t1(1), nx))(0) -1)>0 nrange1 = indgen((n1e-n1s)>1) + n1s n2s = ((where( xedges ge t2(0), nx))(0) -1)>0 n2e = ((where( xedges ge t2(1), nx))(0) -1)>0 nrange2 = indgen((n2e-n2s)>1) + n2s r = [nrange1, nrange2] ;******************* endif else r = selected xm = .5* (xedges + xedges(1:*)) if keyword_set(ltime) then $ back=poly(xm,polyfitw(xm(r),rate(r),ltime(r), order, yfit, yband)) else $ back=poly(xm,poly_fit(xm(r),rate(r), order, yfit, yband)) sigma = avg(yband) return, back end