pro get_sxa, tim_in, index_s, sxa, offset=offset, filtb=filtb, res=res, qdebug=qdebug
;
;+
;NAME:
;	get_sxa
;PURPOSE:
;	Given a set of input times, return the single SXA data record
;	closest to that time.
;CALLING SEQUENCE:
;	get_sxa, index, index_s, sxa
;	get_sxa, index, index_s, sxa, offset=offset
;INPUT:
;	tim_in	- The input time(s) in any of the 3 standard formats
;OUTPUT:
;	index_s	- The SXA index structure
;	sxa	- The SXA data structure
;OPTIONAL KEYWORD OUTPUT:
;	offset	- The number of seconds that the matching SXA data is off
;		  from the input time(s)
;HISTORY:
;	Written 11-Aug-93 by M.Morrison (Using GET_PNT as starting point)
;	13-Sep-93 (MDM) - Modified slightly
;-
last_wid = 'xxx'
tim = anytim2ints(tim_in)
n = n_elements(tim)
;
offset = fltarr(n) - 1e+6
index_s = 0b
sxa = 0b
;
qfirst = 1
tim2orbit, tim, wid=wid, tim2fms=tim2fms
for i=0,n-1 do begin
    if (keyword_set(qdebug)) then print, i+1, ' of ', n
    if (wid(i) ne last_wid) then begin		;only read SXA file if new orbit
	infil = concat_dir('$DIR_SXT_SXA', 'sxa'+wid(i)+'a.01')
	rd_roadmap, infil, rmap
	last_wid = wid(i)
	status = 1 - (get_nbytes(rmap) gt 10)		;status=0 is ok
    end

    if (status eq 0) then begin
	nn = n_elements(rmap)
	qok = bytarr(nn) + 1b		;all ok
	if (keyword_set(filtb)) then begin & ss = where(gt_filtb(rmap) ne filtb) & if (ss(0) ne -1) then qok(ss) = 0b & end
	if (keyword_set(res))   then begin & ss = where(gt_res(rmap) ne res)     & if (ss(0) ne -1) then qok(ss) = 0b & end
	ss_ok = where(qok)

	if (ss_ok(0) ne -1) then begin
	    rr = rmap(ss_ok)
	    ii = tim2dset(rr, tim(i), delta=off0)		;find closest time within orbit of data
	    dset = ss_ok(ii)
	    rd_xda, infil, dset, index0, sxa0, rmap

	    if (qfirst) then begin
		sxa = replicate(sxa0, n)
		index_s = replicate(index0, n)
		qfirst = 0
	    end
	    sxa(i) = sxa0
	    index_s(i) = str_copy_tags(index_s(0), index0)
	end
    end
end
;
end
