#!/usr/bin/env python


import os
import sys
import shutil
import datetime
import glob


#===============================================================================
#  Choix
#===============================================================================

# selected/rejected lists infos : 
EUC_plot_name='./exp_list/PERU_upBas1_T_sst_T_69_133m_section_110.5W_EUC_uo_seuil_20pc_diff_upBas_at_T_31_108m_-0.5_dx.ge.0.0_dy.le.0.4_exp_15_cmip5_cmip6_thetao_with_identifier'

title1="Atlas based on selected exp"

num_col=4   #   nombre de colonnes pour répéter le nom de l'expérience


#===============================================================================
#  Definitions
#===============================================================================
local_time1=datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d')    #  '2018-09-19'
local_time2=datetime.datetime.strftime(datetime.datetime.now(), '%d-%b-%Y')    #  '19-Sep-2018'
local_time3=datetime.datetime.strftime(datetime.datetime.now(), 'the %d %b %Y at %H:%M:%S')    #  '20-Jun-2019 at 16:27:43'
local_time4=datetime.datetime.strftime(datetime.datetime.now(), '%Y_%m_%d-%H_%M_%S')    #  '2019_08_12-18_33_13'

script_name=os.path.basename(sys.argv[0]).split('.py')[0]

script_dir=os.getcwd()


#===============================================================================
#  Main
#===============================================================================

#  read selected/rejected lists infos :
with open(EUC_plot_name+'.selected_list.txt') as f0:
      exec(f0.read())

# to open/create a new html file in the write mode
f1 = open(script_name+'.html', 'w')
  
# the html code which will go in the file GFG.html

html_template_begin1 = """
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml">

  <head>
"""
f1.write(html_template_begin1)

f1.write('   <title>'+title1+'</title>\n')

html_template_begin2 = """
    <link type="text/css" rel="stylesheet" href="./css/chris_EUC_Upwelling.css" title="feuille de style SOLAB" />

  <meta charset="UTF-8" /></head>



<!--========================================================================-->
<!--                      Javascript (Open Windows)                         -->
<!--========================================================================-->
  <script type="text/javascript">
    // initialise the visibility check
    function affCache(idDiv) {
    var div = document.getElementById(idDiv);
    if (div.style.display == "none")
    div.style.display = "";
    else
    div.style.display = "none";
    }

    // Chargement images dans window.open
    function ChargeImage1(img1) {
    window.open(img1,img1,'width=1000, height=700, status=no').focus();return false;
    }
  </script>


<!--========================================================================-->
<!--                              BODY                                      -->
<!--========================================================================-->
  <body>
"""
f1.write(html_template_begin2)

f1.write('    <h1>'+title1+'</h1>\n')


f1.write('\n\n')
f1.write('<a href="./'+EUC_plot_name+'.zip.png " target="_blank">      <img class="image1" src="'+EUC_plot_name+'.zip.png" /></a>\n')
f1.write('<a href="./'+EUC_plot_name+'.txt " target="_blank"> identifier legends </a>\n')


html_template_begin3 = """
<!--========================================================================-->
<!--                              TABLE                                     -->
<!--========================================================================-->
<table border="1px">
  <tbody>
"""
f1.write(html_template_begin3)


str1='<tr>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">section</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">section</td>\n\
<td align="center" rowspan="1" colspan="2" style="background-color: #ffffff">thetao-thetao_ref</td>\n\
</tr>\n'

str2='<tr>\n\
<td align="center" rowspan="1" colspan="1">110.5W</td>\n\
<td align="center" rowspan="1" colspan="1">110.5W</td>\n\
<td align="center" rowspan="1" colspan="2">ref based on density in the EUC core at 110.5W</td>\n\
</tr>\n'


num_col_total=0
for jj in range(str2.count('\n')-2) :
   num_col_total=num_col_total+int(str2.split('colspan="')[jj+1][0])
#  print(jj, str2.split('colspan="')[jj+1][0], num_col_total)


#########
### Code des images

# là où se fait la liste des exp...
image_05_lev1_orig='../images/05_lev1_orig'
os.chdir('./'+image_05_lev1_orig)
suffix="_mean.grid_tr025-lev1_orig_xy_K_1.gif"



ii=0
#-------------------------------------------------------------------------------
#   For each exp_file...
#-------------------------------------------------------------------------------
for exp_list in ['selected_list','rejected_list'] :

   str3='<td align="center" rowspan="1" colspan="4" style="background-color: #ff0000">'+exp_list+'</td>\n'
   f1.write(str3)

   for exp in eval(exp_list) :

      if exp_list == 'selected_list' :
         color1='#FFFFFF'
         
      elif exp_list == 'rejected_list' :
         color1='#000000'

      exp_name=exp
      exp_file=exp+'_thetao'+suffix

      print(exp_file,':')
#     print('ii :',ii, ii%4)
      if ii%4 == 0 : f1.write(str1);  f1.write(str2)
      f1.write('<tr>\n')

      for jj in range(num_col_total) :
         if (jj%num_col == 00) : 
#           print(jj, num_col, num_col_total-jj)
            if (num_col_total-jj) >= num_col : 
               f1.write('<td align="center" rowspan="1" colspan="'+str(num_col)+'" style="background-color: #ffffff">n°'+str(ii)+': <span class="stabilo2">'+exp_name+'</span></td>\n')
   
      f1.write('</tr>\n')
      f1.write('<tr>\n')
   
      exp_file1=exp_name+'_section_110.50W_from_TROP_uo_0-300m.zip.png'
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'../images/section.thetao.SAV/'+exp_file1+'\');">\n')
      f1.write('    <img class="image1" src=\'../images/section.thetao.SAV/'+exp_file1+'\' /></a></td>\n')
   
      exp_file2=exp_name+'_section_110.50W_from_TROP_thetao_select_EUC_20_percent_0-300m.zip.png'
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'../images/section.thetao.SAV/'+exp_file2+'\');">\n')
      f1.write('    <img class="image1" src=\'../images/section.thetao.SAV/'+exp_file2+'\' /></a></td>\n')

      exp_file3=exp_name+'.png'
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'../images/density.SAV/'+exp_file3+'\');">\n')
      f1.write('    <img class="image1" src=\'../images/density.SAV/'+exp_file3+'\' /></a></td>\n')
   
      exp_file4=exp_name+'_PACIFIC_wland.png'
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'../images/density.SAV/'+exp_file4+'\');">\n')
      f1.write('    <img class="image1" src=\'../images/density.SAV/'+exp_file4+'\' /></a></td>\n')
   
      f1.write('</tr>\n')
      ii=ii+1

os.chdir(script_dir)
#########




html_template_end = """
  <tr>
  </tr>
  </tbody>
</table>

   <!--=================================== Pied de page  ===================================-->
   <br />
   <br />
   <br />

   <table width="100%" border="1px">
      <tbody>
         <tr>
            <td align="center">Webmaster: christophe.hourdin @ locean-ipsl.upmc.fr</td>
         </tr>
         <tr>
            <td align="center">Croco-Agrif-Pisces Projects - <a href="https://pagesperso.locean-ipsl.upmc.fr/cholod/EUC_Upwelling/index.xhtml">Home</a></td>
         </tr>
      </tbody>
   </table>
</body>
</html>
"""


print('\nnumber of column to display the exp name :')
print('num_col='+str(num_col))
print('num_col_total='+str(num_col_total))


f1.write(html_template_end)
  
# close the file
f1.close()
