pro get_sfm, times, index, data, info=info, rebin=rebin, nimg=nimg, $
	debug=debug, mag=mag, nodata=nodata
;
;+
;   Name: get_sfm
;
;   Purpose: return sfm images and index records closest to requested time(s)
;
;   Input Parameters:
;
;   History: 
;      17-May-1993 (SLF)
;       8-Jul-1993 (SLF)	- changed input time definitions
;       8-feb-1995 (SLF)        - add nodata keyword
;       6-sep-1995 (SLF) 	- fix problem when rebin=512
;-
if n_elements (times) eq 0 then begin
   message,/info,'Requires at least 1 input time...'
   return
endif 

if keyword_set(nimg) then begin
   delta=lindgen(nimg) * 86400l
   times=anytim2ints(replicate(times(0),nimg),off=delta)
endif

nimages=n_elements(times)

debug=keyword_set(debug)

itime=anytim2ints(times)
start0=itime(0)
stop0=start0
if n_elements(start0) gt 0 then stop0=itime(nimages-1)

info=''
sfms=file_list('$DIR_SXT_SFM','sfm*')
break_file,sfms,sfmlog, sfmpath, sfmfile, sfmext, sfmver

fidlo='sfm' + ex2fid(anytim2ex(start0))
fidhi='sfm' + ex2fid(anytim2ex(stop0))

sfmlo=strmid(sfmfile,0,7) ge strmid(fidlo,0,7)	; uniq to nearest month
sfmhi=strmid(sfmfile,0,7) le strmid(fidhi,0,7)
sfmok=sfmlo and sfmhi
ss=where(sfmok, count)

if count eq 0 then begin
   message,/info,'No sfm files found for specified input times...'
   return
endif

sfms=sfms(ss)
rd_roadmap,sfms,rmap,filidx=filidx
dsets=tim2dset(rmap, times)
dset_str=mk_dset_str(filidx, dsets)
message,/info,'Reading data...'
rd_sda,sfms, dset_str, index, data, nodata=nodata
info=get_info(index,/noninteractive)
;
; rebin on request
mag=fltarr(n_elements(index)) + 1.
if keyword_set(rebin) then begin
   big=where(index.sxt.shape_cmd(0) eq 512, bcount)
   case rebin of 
      512: begin
         small=where(index.sxt.shape_cmd(0) eq 256,scount)
         if scount gt 0 then mag(small) = 2
	 if bcount eq 0 then begin
            if n_elements(index) eq 1 then $
	       data=rebin(temporary(data),512,512,/sample)
	 endif else begin
	    for i=0,scount-1 do $
               data(0,0,small(i)) = rebin(data(0:255,0:255,small(i)),512,512,/samp)
	 endelse
      endcase
   else: begin
      if bcount gt 0 then mag(big) = .5
      for i=0,bcount-1 do $
         data(0,0,i) = rebin(data(*,*,i),256,256,/sample)      
      data=temporary(data(0:255,0:255,*))
      endcase
   endcase
endif
if debug then stop

return
end
