pro soon_search, sttim, entim,  catfiles=catfiles, mail=mail, address=address,  $
   outfil=outfil,                               $
   ref_loc=ref_loc, carrington=carrington, helio=helio, ref_date=ref_date,      $
   full_disk=full_disk, sea_info=sea_info, sea_head=sea_head,               $
   gif_file=gif_file, image_list=image_list, ar=ar, more=more


dir = getenv('DIR_GSN_OBS')
prefix = 'st' + ['l','s','h','r']
;

qmail=keyword_set(mail) or keyword_set(address)

if (n_elements(sttim) eq 0) then sttim = ' 1-MAY-94'
if (n_elements(entim) eq 0) then entim = gt_day(!stime, /str)
if (n_elements(catfiles) eq 0) then catfiles = ''
if (n_elements(address) eq 0) then address = get_user() + '@' + get_host()
if (n_elements(sea_info) eq 0) then sea_info = 'NO search location specified'

kludge_time=timegrid(entim,day=50,/string)
if catfiles eq '' then begin
  for i=0,n_elements(prefix)-1 do $
    catfiles = [catfiles, file_seltime(dir, prefix(i), sttim, kludge_time, /last_before)]
  ;     catfiles = [catfiles, sel_filetimes(sttim, kludge_time, dir=dir, $
;       filter=prefix(i)+'*',/one_before)]
  catfiles=catfiles(1:*)
endif
box_message,catfiles
en_dis_arr = ['Disabled', 'Enabled']
code = ' '
code2 = ' '
qdone = 0

if not keyword_set(outfil) then $
   outfil = concat_dir(getenv('HOME'), 'soon_sea.temp')

hel=data_chk(helio,/string) and keyword_set(ref_date)
car=keyword_set(carrington)
angle=n_elements(ref_loc) eq 2
arx=keyword_set(ar)
full_disk=keyword_set(full_disk)

case 1 of 
   car: begin
      ref_loc=carrington
      sea_info = 'Search on Carrington Lat/Lon: ' + arr2str(ref_loc)
   endcase
   hel: begin
      if not keyword_set(ref_date) then begin
         tbeep
         message,/info,"Heliographic Coord. Search...."
         message,/info,"You must also supply a reference date via REF_DATE"
         return
      endif
      ref_loc=helio
      sea_info = 'Search on Heliocentric Coordinate: ' + ref_loc + ' for ' + ref_date
   endcase
   arx:sea_info="Search on Region# " + strtrim(ar,2)
   full_disk: begin
      full_disk=1
      sea_info = 'Search for full disk images'
      ref_loc = 'FULLD'
   endcase
   else: begin
      sea_info = "Search for all Large Scale images"
   endelse
endcase

sea_head = 0b
qfirst = 1
if (catfiles(0) ne '') then for ifil=0,n_elements(catfiles)-1 do begin
   print, 'Searching>>> ' + catfiles(ifil)
   rd_gsn, catfiles(ifil), head     ;, obs, imap
   entim_arr = anytim2ints(head, off=head.duration)

   if arx or not keyword_set(ref_loc) then qok=lonarr(n_elements(head))+1 else $
      qok = in_fov(ref_loc, ref_date, gsn=head, carrington = carrington, $
       helio=helio, full_disk=full_disk, /boolean)
   timok=tim2match(sttim, head, entim_ref=entim, /entim_dur) ge 0
   obj = strmid(string(head.st$object),0,4)
   case 1 of 
      arx: begin 
           object=strupcase(strtrim(ar,2))
           nobj=str2number(object)
           sobj=(['',strmid(object,0,2)])((strspecial(object,/alpha))(0))

           tapobj = strupcase(string(head.st$object))
           ntap=str2number(tapobj)
           stap=strarr(n_elements(ntap))
           alpha=where(strspecial(tapobj,/alpha),acnt)
       if acnt gt 0 then stap(alpha)=strmid(tapobj(alpha),0,2)
       ss = where(timok and (ntap eq nobj) and (stap eq sobj) )
     endcase
      full_disk: ss = where(qok and timok and obj eq '9999')
      else: ss=where(qok and timok and obj ne '9999' )
   endcase
   if (ss(0) ne -1) then begin
      if (qfirst) then sea_head = head(ss) else sea_head = [sea_head, head(ss)]
          qfirst = 0
   end
end

sea_tit = ['SOON/SPOT  ' + sea_info, $
' Time range: ' + sttim + ' to ' + entim]

if data_chk(sea_head,/struct) then begin
   pr_gsn, aa, summarr, /sum, gsn_head=sea_head, comment=sea_tit
   if keyword_set(image_list) then begin
      pr_gsn, catfiles, summary_arr, /sum, gsn_head=sea_head
      pr_gsn, catfiles, image_array,  comment=sea_tit, tapefile=summary_arr
      file_append, outfil, /new,                        $
      ['------------------------ SUMMARY LISTING ----------------------------', $
        '',summarr,'',                                  $
       '------------------------  IMAGE LISTING  ----------------------------', $
        image_array]
   endif else file_append, outfil, summarr,/new

;   if data_chk(gif_file,/string) then begin
;      if gif_file(0) ne '' then $
;      plot_gsn, gsn_head=sea_head, tit=arr2str(sea_tit, delim='!c'), $
;         gif_file=gif_file
;   endif
endif else begin
   file_append,outfil,["******* NO SOONSPOT CATALOG MATCHES FOUND ********"],/new
endelse

if qmail then $
   mail, file=outfil, /no_defsubj, subj="SOON SEARCH RESULTS", user=address

if keyword_set(more) then more,rd_tfile(outfil)

return
end