pro ssw_last_event_summary,path,debug=debug, legend_bottom=legend_bottom
;
;+
;   Name: ssw_last_event_summary
;
;   Purpose: integrate flare-locator and contextual dbases -> summary plot
;
;   History:
;      Circa March 2002 - S.L.Freelnd written
;      19-Sep-2002 - correct SWEPAM bulk speed units (km/hour -> km/sec)
;      15-Nov-2002 - add message / annotation if no data for nHours
;      21-Sep-2003 - Add titles to index and event summary pages
;      28-Sep-2003 - add call to trace_special_movies_online  
;                    (and annotations/links if any) - ala sep_events_online  
;      18-Dec-2003 - add link to Last Events Archive
;
;-
debug=keyword_set(debug)

phttp=get_logenv('path_http')

if n_elements(path) eq 0 then path=concat_dir(phttp,'latest_events')

cat=file_list(path,'*geny')
if cat(0) eq '' then begin 
   box_message,'No catalog available'
;   return
endif else begin 
   restgenx,file=cat(0),floc
   ss=where(floc.lfiles ne '',gcnt)
   if gcnt gt 0 then begin 
      floc=floc(ss)
      sxi=strpos(floc.lfiles,'SXI') eq 0
   endif 
endelse
t0x=reltime(days=-5,out='vms')
t1x=reltime(/now,out='vms')

set_plot,'z'
wdef,xx,1280,700,/zbuffer
loadct,3
linecolors
savesys,/aplot
!p.multi=[0,1,3]

!p.charsize=1.2
day_grid=gt_day(timegrid(t0x,reltime(t1x,days=1),/day,out='ints'),/string)

plot_goes,t0x,t1x,timerange=[t0x,t1x],back=11,color=7, xstyle=9, $
   ymargin=[2,3],xmargin=[8,0],/nolabel, gcolor=50, /ascii, /goes10

ssw_les_fdinfo, last_time=last_time, gapmess=gapmess, dtnow=dtnow
if gapmess ne '' then begin 
   ;evt_grid,last_time,align=0, $ ; label='No Flare Locator Images ->',$
   ;  color=5, labpos=.67, labcolor=2,labsize=.9, tickpos=.67,/noarrow
   if ssw_deltat(last_time,ref=t0x) lt 0 then last_time=t0x
   evt_grid, color=2, last_time,align=0,/noarrow,$
      label=gapmess, labpos=.87,labcol=2,labsize=1.,$ ; tickpos=.7 , $
      /imap,imagemap_coord=imcgap, linestyle=0 ; .695
endif

evt_grid,day_grid,linestyle=0,color=5
if 0 then begin 
evt_grid,anytim(floc.date_obs,/int),color=([4,5])(sxi),label=floc.helio,$
   labpos=stag_lab(floc.helio,min=.88,max=.99,sep=.035),labsize=.8, $
;   labpos=stag_lab(floc.helio,min=.875,max=.985,sep=.035),labsize=.8, $
   labcolor=([4,5])(sxi)

evt_grid,anytim(floc.date_obs,/int),labcolor=9,label=strtrim(indgen(gcnt)+1,2), $
    labpos=stag_lab(floc.helio,min=.9,max=1,sep=.035), labsize=.85,/noline, $
;    labpos=stag_lab(floc.helio,min=.89,max=.99,sep=.035), labsize=.85,/noline, $
    /imap,imagemap_coord=imc,/imcircle
endif else imc=''

trace_special_movies_online,t0x,t1x, $
   pt0=pt0tm, pt1=pt1tm, urls=urlstm,ecount=ecount
if ecount gt 0 then begin
   tracelabs=replicate('TRACE',ecount)
   evt_grid,anytim(pt0tm,/int), /noarrow, $
        label=tracelabs, $
      align=.5,labcolor=12,labsiz=.7, $
      labpos=stag_lab(tracelabs,min=.69,max=.73,sep=.02), $
     /imap,imagemap_coord=imctm,linestyle=1,color=12 
if debug then stop ,'TRACE Movies'
endif

goes8=ssw_deltat(t1x,ref='19-jun-2003') lt 0
goes11=1-goes8
plot_goesp, t0x, t1x,  xstyle=1, /nowindow, /log, $
   ymargin=[2,3], xmargin=[8,0], /proton_only,timerange=[t0x,t1x], $
   yrange=[1,10e4],/ystyle,/nolabel , ticklen=.01,/clear, $
   legend_bottom=legend_bottom, $
   goes8=goes8,goes11=goes11

evt_grid,day_grid,linestyle=0,color=5, /vertical, label=day_grid, $
      labcolor=5, labsize=.9

proton_events_online,t0x,t1x,peak=peak,/read_goesp,urls=urlsgp,ecount=ecount
if ecount gt 0 then begin
   evt_grid,anytim(peak,/int), /noarrow, $
        label=replicate('[SEP Details]',ecount), $
      align=1,labcolor=9,labpos=.6,  $
     /imap,imagemap_coord=imcgp,linestyle=0,color=2 
if debug then stop ,'SEP events'
endif

acedata=get_acedata(t0x,t1x,/daily,/swepam)

utplot,acedata,acedata.b_speed,title='ACE/SWEPAM and SoHO/EIT-284',ytitle='Bulk Speed (Km/Sec)', $
   ymargin=[4,2], xmargin=[8,0],timerange=[t0x,t1x], xstyle=1, $
   /ynozero,color=7, ticklen=.01
evt_grid,day_grid,linestyle=0,color=5
;
; plot some thumbnails...
e284=sswdb_files(t0x,t1x,pat='_284_',/l1q)
if e284(0) ne '' then begin 
   ss=grid_data(file2time(e284),hour=12)
   mreadfits,e284(ss),i284,d284,outsize=512
   s284=bytscl(do_eit_scaling(d284,/log,/no_prep))
   ssd=confac(s284,.30)
   evt_grid,i284
   split_colortab,0,200,16
   nei=n_elements(i284)
;   evt_grid,i284,labdata=ssd+16<!p.color, labpos=.05, $
;      /imap,imagemap_coord=imcthumb
endif
outplot,acedata,acedata.b_speed,color=7

summary='summary_plot.png'
plot_summ=concat_dir(path,summary)

zbuff2file2,plot_summ,/png
restsys,/aplot ,/init 

; generate top page
nevt=n_elements(floc)
epngs=concat_dir(path,floc.ename+'.png')
hpngs=str_replace(epngs,'png','html')
break_file,epngs,ll,pp,ff,ee,vv
relepng=ff+ee+vv
;
; Generate HTML table
table=get_infox(floc,'ename,fstart,fstop,fpeak,class,helio',header=header)
table=strjustify(strtrim(sindgen(nevt)+1,2)) + ' ' + table
tcols=str2cols(table)

; Eliminate extraneous date information 
tcols(2,0)=tcols(2,*) + tcols(3,*)
tcols(3,*)=''
tcols(4,*)=''
tcols(6,*)=''
tcols=strarrcompress(tcols,/col)

header=str2arr('Event#,EName,Start,Stop,Peak,GOES Class, Derived Position (SXI-GOES12 or EIT High Cadence Wavelength)') 
if n_elements(sxi) eq 0 then header=header(0:5)
htable=[[header],[tcols]] 

; Generate a page for each event 
for i=0,nevt-1 do begin
   html_doc,hpngs(i),/header, title= $
      'SolarSoft Latest Events - Summary of event: ' + floc(i).ename
   file_append,hpngs(i),strtab2html(htable(*,[0,i+1]),/row0)
;
;  Check for pre-existing event summary html
   sumdir='latest_events_summary/'+floc(i).ename
   summp=concat_dir(phttp,sumdir)
   sumhttp=concat_dir(summp,floc(i).ename+'_lm.html.dat')
   if file_exist(sumhttp) then begin 
      shttp=rd_tfile(sumhttp)
      relhttp=str_replace(shttp,'gev_','../'+sumdir+'/gev_')
      file_append,hpngs(i),'<h3>Flare Sequence - Full disk and derived FOV</h3>'
      file_append,hpngs(i),relhttp
   endif 
   sumhttp=concat_dir(summp,floc(i).ename+'_sxilm.html.dat')
   if file_exist(sumhttp) then begin 
      shttp=rd_tfile(sumhttp)
      relhttp=str_replace(shttp,'gev_','../'+sumdir+'/gev_')
      file_append,hpngs(i),'<h3>SXI-GOES12 Flare Sequence - Full disk and derived FOV</h3>'
      file_append,hpngs(i),relhttp
   endif 
   sumhttp=concat_dir(summp,floc(i).ename+'_laslm.html.dat')
   if file_exist(sumhttp) then begin 
      shttp=rd_tfile(sumhttp)
      relhttp=str_replace(shttp,'gev_','../'+sumdir+'/gev_')
      file_append,hpngs(i),'<h3>LASCO C2 Difference - GOES Start - 30 min <em>to</em> GOES End+4 hours</h3>'
      file_append,hpngs(i),'<em>Courtesy LASCO/NRL</em>'
      file_append,hpngs(i),relhttp
   endif 

   sumhttp=concat_dir(summp,floc(i).ename+'_trace.html.dat')
   if file_exist(sumhttp) then begin 
      shttp=rd_tfile(sumhttp)
      relhttp=str_replace(shttp,'HREF="','HREF="../'+sumdir+'/')
      relhttp=str_replace(relhttp,'SRC="','SRC="../'+sumdir+'/')
      file_append,hpngs(i),'<h3>TRACE event sequences</h3>'
      file_append,hpngs(i),relhttp
   endif 
   file_append,hpngs(i),'<h3>Flare Locator Image</h3>' 
   file_append,hpngs(i),'<em>Difference: '+ $
      arr2str(reverse(str2arr(floc(i).lfiles)),' - ')
   file_append,hpngs(i),$
       strtab2html('<IMG SRC="'+ relepng(i)+'">')
   html_doc,hpngs(i),/trailer
endfor
html_linklist,hpngs,insert='sundiv.gif'         ; link-list the event html files  
index=concat_dir(path,'index_orig.html')
enames=strtrim(reform(tcols(1,*)),2)
tcols(1,*)=str2html(enames+'.html',link=enames,/nopar)

; image map info 
imcurl=enames+'.html'
imcall=imc
if n_elements(imcthumb) gt 0 then begin   ; coronal thumbs?
   imcall=[imcall,imcthumb]
   ejsurl='http://sohowww.nascom.nasa.gov/sdb/http/movies/eit_fullfov_java.html'
   imcurl=[imcurl,replicate(ejsurl,n_elements(imcthumb))]
endif

help,imcgap
if n_elements(imcgap) gt 0 then begin 
   imcall=[imcall,imcgap]
   ;imcurl=[imcurl,'http://umbra.nascom.nasa.gov/eit/CCD_bakeout.html']
   ;imcurl=[imcurl,'http://sohowww.nascom.nasa.gov/whatsnew/HGA/']
   ;imcurl=[imcurl,'http://www.nasa.gov/vision/earth/lookingatearth/Isabels_Engine.html']
   imcurl=[imcurl,'http://sohowww.nascom.nasa.gov/data/synoptic/.targets/today.html']
endif

if n_elements(imctm) gt 0 then begin           ; TRACE special movies...
   imcall=[imcall,imctm]
   imcurl=[imcurl,urlstm]
endif

if n_elements(imcgp) gt 0 then begin 
   imcall=[imcall,imcgp]
   imcurl=[imcurl,urlsgp]
endif

if n_elements(imcall) ne n_elements(imcurl) then begin
   if debug then stop,'mismatch'

endif

imaphtml=ssw_imapcoord2html(summary,imcall,imcurl, $
    target='_blank')
ssw_ar_summary,outhtml=outhtml,/nodoc, outdir=path
html_doc,index,/header,title='SolarSoft Latest Events'
file_append,index,[ $
   '<a href="http://www.lmsal.com/solarsoft/latest_events_archive.html">',$
   'Data for earlier dates is available at <em>Latest Events Archive</em></a>']
file_append,index,[$
   '<a href="http://www.lmsal.com/solarsoft/last_events/index.html">',$
   '<font color=red>Alternate test version du jour</font></a>']

file_append,index,outhtml
;file_append,index,'<h4> Select AR above to link to BBSO/ARM details - Select Event# (<font color="#00CCFF">Bright Blue</font>) on the GOES X-Ray plot  for event summary page</h4>'
file_append,index,imaphtml
file_append,index,strtab2html([[header],[tcols]],/row0,cellpad=5,cellspac=2,border=2)
html_doc,index,/trailer

return
end