pro search_obs, time0, time1,                                        $
   ffi=ffi, pfi=pfi, conf_file=conf_file, index=index,	             $
   outfile=outfile, bcs=bcs, hxt=hxt, wbs=wbs, quiet=quiet, wid=wid, $
   mail=mail
;+
;   Name: search_obs
;
;   Purpose: search observing log for matching records
;
;   Input Parameters:
;      time0 - start time for search (default=launch)
;      time1 - stop time for search  (default=now)
;
;   Keyword Parameters:
;      ffi or sxtf - if set, sxt FFI search (default)
;      pfi or sxtp - if set, sxt PFI search
;      conf_file - name of SS configuration file
;      listfile - if set, outputfile for text summary of matching index records
;      outfile - file containing matching index structures
;      
;      index (output) - maching records
;
;   History:
;      22-mar-1995 (SLF) - extension of mk_spd logic
;      12-apr-1995 (SLF) - extend time buffer to assure stop time covered
;			   add tag names and some text info to save file
;      22-may-1995 (SLF) - default config file in HOME directory
;
;   Restrictions:
;      only sxt for now 
;-

loud=1-keyword_set(quiet)
outlog=concat_dir(get_logenv('HOME'),'search_obs.txt')
if n_elements(conf_file) eq 0 then $
   conf_file=concat_dir(get_logenv('HOME'),'ss.config')

if not file_exist(conf_file) then begin
   message,/info,"Cannot find SS configuration file: " + conf_file
   more,rd_tfile(concat_dir("$DIR_SXT_DOC",'ssconfig.doc'))
   return
endif

if n_elements(time0) eq 0 then time0=get_yo_dates(/launch)
if n_elements(time1) eq 0 then time1=ut_time()

prstr,["Start time: " + time0, $
       " Stop time: " + time1, ""]

file_append,outlog,["search_obs run at: " + systime(),"",$
   "Start time: " + time0," Stop time: " + time1, ""], /new

file_append,outlog,["Configuration file: " + conf_file,  $
                    "..........",rd_tfile(conf_file),".........",""]
;
pfi=keyword_set(pfi) or keyword_set(sxtp)
ffi=keyword_set(ffi) or keyword_set(sxtf) or (1-pfi)

ints=anytim2ints([time0,time1])

; break into smaller 'chunks' for memory management (if required)

if (int2secarr(ints))(1) gt 604800. then begin
   times=timegrid(time0,time1,/week,/string) 
   times=[times,timegrid(time1,/day,/string)]	; extend 
   endif else times=[time0,time1]           

if keyword_set(wid) then widget_control,get_value=text,wid

for i=0,n_elements(times)-2 do begin
   mess="Range>> " + times(i) + ' - ' + times(i+1)
   if keyword_set(wid) then widget_control,wid, set_value=[text,mess] else $
      message,/info,mess      
   get_utevent,times(i),times(i+1), sxtobs, sxtp=pfi, sxtf=ffi, $
      			wbs=wbs, hxt=hxt, bcs=bcs
   ss=sxt_where(sxtobs, sscnt, conf_file=conf_file, quiet=quiet)
   if sscnt gt 0 then begin
      if n_elements(outind) eq 0 then outind=sxtobs(ss) else $
         outind=str_concat(outind,sxtobs(ss))
   endif
endfor

; trim to time
mess="No matches found..."

mail=keyword_set(mail)
if n_elements(outind) gt 0 then begin
   ss=sel_timrange(outind,times(0),times(n_elements(times)-1),/between)
   if ss(0) ne -1 then begin
      mess="Number of matches found: " + strtrim(n_elements(ss),2)
      mmess=["xsearch_obs status message....","",mess,rd_tfile(conf_file)]
      index=temporary(outind(ss))
      if keyword_set(outfile) then begin
         if data_chk(outfile,/string) then ofile=outfile else $
            ofile=concat_dir(get_logenv('HOME'),'search_obs.genx')
            print,"Saving index records in : " + ofile
            savegen,file=ofile, index, get_info(index,/non), $
               names=['obs_records','info_array'], $
               text= ['SEARCH_OBS results save file','',		 $ 	
                      'Parmeters from configuration file: ' + conf_file, $
                      '----------------------------------------------',	 $ 
                      rd_tfile(conf_file)]

         mmess=[mmess,"Matches saved in file: " + ofile]
      endif
   endif
endif

if mail then mail,mmess,user=get_user(), subj='xsearch_obs ' + mess,/no_def

message,/info,mess
file_append,outlog,mess


return
end
