import os, urllib, glob


def html_header(html_fn, title):

    new_html = open(html_fn,'w')

    # Write out html footer
    new_html.write('<HTML>\n')
    new_html.write('<HEAD>\n')
    new_html.write('<TITLE>%s</TITLE\n' % title)
    new_html.write('</HEAD>\n')
    new_html.write('<BODY BGCOLOR="white">\n')
    new_html.write('<H1>%s</H1>\n' % title)

    return new_html


def add_plot(plot, new_html):

    new_html.write('<BR><a href="'+plot+'"><img src="'+plot+'"border=1 hspace=3 vspace=3" style="width:500px" alt="'+plot+'"></a><BR>\n')



def html_footer(new_html):

    # Write our html footer
    new_html.write('</BODY>\n')
    new_html.write('</HTML>\n')
    new_html.close()


def create_html(nc_file, title, dir):

   html = html_header('%s_all_variables.html' % dir,'All variables in %s' % nc_file)
   
   plts = glob.glob('%s/*.png' % dir)
   for p in plts:
       add_plot(p, html)
  
   html_footer(html)


