;+
;  Name:
;       CREATE_GIF_HTML
; 
; Procedure to create a hypertext file which can be displayed by Mosaic (shows
; flare #, start/end time, and peak rate).  User can click on a flare to 
; see a gif time profile.  
;
; Kim Tolbert  2/94
;
; Modified 12/13/94 Amy Skowronek - replaced Kim Tolbert's name as
;    	listed contact.
; Modified  10/24/95 AES - use ftp:// instead of file://
; Modified 1/18/96 AES - put links to years before and after, and still
; 	create page even if there are no valid flares
; Mod 34/1/96 AES - changed selection of fldata to not exclude 
;	dates, so further routines opened during a session won't
;	be disturbed
; Mod 17/9/97 AES changed mildly to reflect y2k and changes to anytim
;            also change address to gsfc one.
; Mod 99/3/18 AES removed reference to Mosaic from html code.
;-

pro create_gif_html, year

year = fix(year)
;if year gt 1900 then year = year - 1900


secstart = anytim ([0,0,0,0,1,1,year], /sec)
secend   = anytim ([0,0,0,0,1,1,year+1], /sec)
if year gt 1900 then year = year - 1900
yearst = strtrim(year + 1900, 2)
yearbefore = strtrim(year + 1899, 2)
yearafter = strtrim(year + 1901, 2)
htmlfile = 'batse_flgifs_' + yearst + '.html'
htmlbefore = 'batse_flgifs_' + yearbefore + '.html'
htmlafter = 'batse_flgifs_' + yearafter + '.html'

openw, lunout, htmlfile, /get_lun

head = ['<html>', $
       ' ', $
       '<head>', $
       '<title>' + yearst + ' BATSE Solar Flare Light Curves</title>', $
       ' ', $
       '<h1>' + yearst + ' BATSE Solar Flare Light Curves</h1>', $
       '</head>', $
       ' ', $
       '<body>' , $
       '<b> Click on any entry to view a gif file of a light curve of that ' + $
          'event.</b><p>', $
       ' ']

for i = 0,n_elements(head)-1 do printf, lunout, head(i)

tab = '	'
tabs2 = '		'
tabs3 = '			'
columns =['<xmp>', $
      'Flare' + tab + 'Start' + tabs3 + 'End' + tabs2 + 'Peak ', $
      '  #' + tab + 'Time' + tabs3 + 'Time' + tabs2 + 'Rate ', $
      tabs3 + tabs3 + 'c/s', $
      '</xmp>']

for i=0,n_elements(columns)-1 do printf, lunout, columns(i)
printf,lunout,'<a href="http://umbra.nascom.nasa.gov/batse/' + htmlbefore +$
	      '"> <b> Go to ' + yearbefore + '</b></a><p>
batse_read_cat
@flare_catalog

; sort into time order, then narrow down to only flares within selected year.
s = sort(fldata.start_secs)
fldata = fldata(s)
q = where (fldata.start_secs ge secstart(0) and fldata.start_secs le secend(0),kyear)
;print,q
;if q(0) ne -1 then begin fldata = fldata(q)
; fldata=fldata(q)

for i=0,kyear-1 do begin
;for i=0,50 do begin
   iflare = string(fldata(q(i)).flare_num,format='(i5)')
   flare = strtrim(iflare, 2)
   start = strmid(anytim(fldata(q(i)).start_secs,/yohkoh),0,15)
   endt = strmid ( anytim ($
     fldata(q(i)).start_secs + fldata(q(i)).duration, /time, /yohkoh), 0, 5)
   peak = strtrim(long(fldata(q(i)).peak_rate), 2)

   line = '<a href="ftp://umbra.nascom.nasa.gov/pub/batse/batsegifs/batse_' + $
      flare + '.gif"><b><xmp>' + iflare + tab + start + tabs2 + endt + $
      tabs2 + peak + '</xmp></b></a><p>'
   printf, lunout, line

endfor
;endif
printf,lunout,'<a href="http://umbra.nascom.nasa.gov/batse/'+htmlafter+$
	'"><b>Go to '+ yearafter + '</b></a><p>
currdate = anytim(systime(1) + utime('70/1/1'), /yohkoh, /date)
tail = ['</body>', $
        ' ', $
        '<hr>', $
        ' ', $
        '<address><a href="http://umbra.nascom.nasa.gov/people/amy.html">', $
        'Amy Skowronek </a><br> <a href="mailto:Amy.Skowronek@gsfc.nasa.gov">',$ 
	'Amy.Skowronek@gsfc.nasa.gov</a> <br>+1 301 286-4713<p>',$
        'NASA Goddard Space Flight Center<br>', $
        'Solar Physics Branch / Code 682<br>', $
        'Greenbelt, MD 20771<br>', $
        '<p>', $
        'Last revised ' + currdate + ' - A. Skowronek', $
        '</address>', $
        '</html>']

for i=0,n_elements(tail)-1 do printf, lunout, tail(i)

free_lun, lunout
;goto, getout     ;#$(#*$&(#*$&$(*#&$(*&   D E B U G  !!!!!!!!!!!!!!!!!!!!!!!!!
hxps=get_hxp()

; Write a .com file with the commands needed to ftp the html file to umbra.
; Then delete the html file and and the .com file.

openw, lun, 'ftp_html.com', /get_lun
printf, lun, '$ ftp umbra'
printf, lun, 'hxrbs'
printf, lun, hxps
printf, lun, 'cd batse_htdocs'
printf, lun, 'put "' + htmlfile + '" "' + htmlfile + '"'
printf, lun, 'quit'
free_lun, lun
spawn, '@ftp_html.com'
spawn, 'delete ftp_html.com;, ' + htmlfile + ';'
 
getout:
return&end
