#!/usr/bin/env python


import os
import sys
import shutil
import datetime
import glob


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

# selected/rejected lists infos : 
#var1='thetao'    #   thetao, so, o2, no3

#var1='thetao'; EUC_plot_name='PERU_upBas1_T_sst_T_69_133m_section_110.5W_EUC_uo_seuil_20pc_diff_upBas_F_exp_87_cmip5_cmip6_thetao_with_identifier'
#var1='thetao'; EUC_plot_name='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'
#var1='no3';    EUC_plot_name='PERU_upBas1_T_sst_T_69_133m_section_110.5W_EUC_uo_seuil_20pc_diff_upBas_F_36_cmip5_cmip6_no3_with_identifier'
var1='o2';     EUC_plot_name='PERU_upBas1_T_sst_T_69_133m_section_110.5W_EUC_uo_seuil_20pc_diff_upBas_F_25_cmip5_cmip6_o2_with_identifier'

title1=var1+' Analysing Atlas - based on selected exp'

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

dir='./exp_list/'
img_root_dir='../../../images/'

exp_test_dir='05_lev1_orig/'
suffix="_mean.grid_tr025-lev1_orig_xy_K_1.gif"
#suffix="_mean.grid_tr025-lev1_orig_lonlat-woa2009_K_1.gif"

if var1 == 'thetao' :
   depth1='27m'  #   images ferret
else : 
   depth1='97m'  #   images ferret

depth2='69m-133m'  #   images python

# add woa2009 to the web page? 
display_woa2009=True

#===============================================================================
#  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(dir+EUC_plot_name+'.selected_list.txt') as f0:
   exec(f0.read())

if display_woa2009 :
   tmp1=['woa2009_clim-2009']
   selected_list=tmp1+selected_list
   tmp2=['noid']
   selected_list_id=tmp2+selected_list_id

# to open/create a new html file in the write mode
f1 = open(dir+EUC_plot_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')
f1.write('<br /><br />\n')


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


str1='<tr>\n\
<td align="center" rowspan="1" colspan="2" style="background-color: #ffffff">thetao-thetao_ref</td>\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="1" style="background-color: #ffffff">section</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">global</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">global-woa2009</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">PERU-woa2009</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">PERU</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">PERU-ZOOM</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">global-woa2009</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">PERU-woa2009</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">PERU</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">PERU-ZOOM</td>\n\
</tr>\n'

str2='<tr>\n\
<td align="center" rowspan="1" colspan="2">ref based on density in the EUC core at 110.5W</td>\n\
<td align="center" rowspan="1" colspan="1">uo 110.5W</td>\n\
<td align="center" rowspan="1" colspan="1">'+var1+'110.5W</td>\n\
<td align="center" rowspan="1" colspan="1">'+var1+'110.5W</td>\n\
<td align="center" rowspan="1" colspan="1">surf</td>\n\
<td align="center" rowspan="1" colspan="1">surf</td>\n\
<td align="center" rowspan="1" colspan="1">surf</td>\n\
<td align="center" rowspan="1" colspan="1">surf</td>\n\
<td align="center" rowspan="1" colspan="1">surf</td>\n\
<td align="center" rowspan="1" colspan="1">'+str(depth1)+'</td>\n\
<td align="center" rowspan="1" colspan="1">'+str(depth1)+'</td>\n\
<td align="center" rowspan="1" colspan="1">'+depth2+'</td>\n\
<td align="center" rowspan="1" colspan="1">'+depth2+'</td>\n\
</tr>\n'

#f1.write(str1)

#-------------------------------------------------------------------------------
#   For each exp...
#-------------------------------------------------------------------------------
for exp_list in ['selected_list','rejected_list'] :

   if exp_list == 'selected_list' :
      color2='#C0C0C0'
   elif exp_list == 'rejected_list' :
      color2='#ffff94'

   str3='<td align="center" rowspan="1" colspan="'+str(num_col)+'" style="background-color: '+color2+'">'+exp_list+'</td>\n'
   str3vide='<td align="center" rowspan="1" colspan="'+str(num_col)+'" style="background-color: '+color2+'">   </td>\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(str3)
         else :
            f1.write(str3vide)

   ii=0
   for exp in eval(exp_list) :
      if exp_list == 'selected_list' :
#        color2='#C0C0C0'
         color2='#FFFFFF'
      elif exp_list == 'rejected_list' :
         color2='#ffff94'

      if 'GLORYS' in exp : color1='#f8e3ff'
      if 'soda3'  in exp:  color1='#f8e3ff'
      if 'woa2009'in exp:  color1='#f8e3ff'
      if 'cmip5'  in exp:  color1='#e2ffd7'
      if 'cmip6'  in exp:  color1='#d7fffb'
      if 'trop'   in exp:  color1='#fffed7'

      exp_name=exp
#     print('id='+exp_list+'_id['+str(ii)+']')
      exec('id='+exp_list+'_id['+str(ii)+']')

###   verif si fichier dispo pour le champ affiché. 
      exp_test='./exp_list/'+img_root_dir+exp_test_dir+exp_name+'_'+var1+suffix
      print(exp_test,':')

      if not(os.path.exists(exp_test)) : 
          if 'Omon_1m' in exp_test : 
             if os.path.exists(exp_test.replace('Omon_1m','Oyr_1y')) : exp_test=exp_test.replace('Omon_1m','Oyr_1y')
          if 'Oyr_1y'  in exp_test : 
             if os.path.exists(exp_test.replace('Oyr_1y','Omon_1m')) : exp_test=exp_test.replace('Oyr_1y','Omon_1m')

      if os.path.exists(exp_test) :

         print('\n'+exp_name+' :')
   #     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 : 
                  if var1 == 'thetao' :
                     if id == 'id5c36' : color2='#ff6767'
                     if id == 'id6c25' : color2='#596bff'
                  if var1 == 'o2' :
                     if id == 'id5c13' : color2='#ff6767'
                     if id == 'id5c41' : color2='#596bff'
                  if var1 == 'no3' :
                     if id == 'id5c35' : color2='#fefe9d'
                     if id == 'id5c15' : color2='#92b5ce'
                     if id == 'id5c34' : color2='#bdffae'
                     if id == 'id5c41' : color2='#ddc2ff'
                     if id == 'id6c40' : color2='#ffc2c2'
                  f1.write('<td align="center" rowspan="1" colspan="'+str(num_col)+'" style="background-color: '+color2+'">n°'+str(ii)+': <span class="stabilo2">('+id+') '+exp_name+'</span></td>\n')
               else :
                  f1.write('<td align="center" rowspan="1" colspan="'+str(num_col)+'" style="background-color: '+color2+'">   </td>\n')
   
         print('ii:'+str(ii)+'   ii:'+str(ii)+'   id = '+id+'   color2 ='+color2)
         f1.write('</tr>\n')
         f1.write('<tr>\n')
   
         section_str2='110.50W'

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

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

# section
         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(\''+img_root_dir+'/section.thetao.SAV/'+exp_file1+'\');">\n')
         f1.write('    <img class="image1" src=\''+img_root_dir+'/section.thetao.SAV/'+exp_file1+'\' /></a></td>\n')
   
         exp_file21=exp_name+'_section_110.50W_from_TROP_'+var1+'_0-300m.zip.png'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\''+img_root_dir+'/section.'+var1+'.SAV/'+exp_file21+'\');">\n')
         f1.write('    <img class="image1" src=\''+img_root_dir+'/section.'+var1+'.SAV/'+exp_file21+'\' /></a></td>\n')

         exp_file22=exp_name+'_section_110.50W_from_TROP_'+var1+'_select_EUC_20_percent_0-300m.zip.png'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\''+img_root_dir+'/section.'+var1+'.SAV/'+exp_file22+'\');">\n')
         f1.write('    <img class="image1" src=\''+img_root_dir+'/section.'+var1+'.SAV/'+exp_file22+'\' /></a></td>\n')

# ferret global & PERU  var1 - woa2009
         exp_file5=exp_name+'_'+var1+'_mean.grid_tr025-lev1_orig_xy_K_1.gif'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\''+img_root_dir+'/05_lev1_orig/'+exp_file5+'\');">\n')
         f1.write('    <img class="image1" src=\''+img_root_dir+'/05_lev1_orig/'+exp_file5+'\' /></a></td>\n')

# ferret global & PERU  var1 - woa2009  K=1
         exp_file8=exp_name+'_'+var1+'_mean.grid_tr025-lev1_orig_lonlat-woa2009_K_1.gif'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\''+img_root_dir+'/05_lev1_orig/'+exp_file8+'\');">\n')
         f1.write('    <img class="image1" src=\''+img_root_dir+'/05_lev1_orig/'+exp_file8+'\' /></a></td>\n')

         exp_file9=exp_name+'_'+var1+'_mean.grid_tr025-lev1_orig.PERU_lonlat-woa2009_K_1.gif'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\''+img_root_dir+'/05_lev1_orig/'+exp_file9+'\');">\n')
         f1.write('    <img class="image1" src=\''+img_root_dir+'/05_lev1_orig/'+exp_file9+'\' /></a></td>\n')

# matplotlib PERU  var1 - woa2009  K=1
         exp_file13=exp_name+'_PERU_'+var1+'_sst.zip.png'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+img_root_dir+'/PERU.'+var1+'.SAV/'+exp_file13+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+img_root_dir+'/PERU.'+var1+'.SAV/'+exp_file13+'\' /></a></td>\n')

         exp_file14=exp_name+'_PERU_'+var1+'_sst_ZOOM.zip.png'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+img_root_dir+'/PERU.'+var1+'.SAV/'+exp_file14+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+img_root_dir+'/PERU.'+var1+'.SAV/'+exp_file14+'\' /></a></td>\n')

# ferret global & PERU  var1 - woa2009  K=24
         exp_file6=exp_name+'_'+var1+'_mean.grid_tr025-lev1_orig_lonlat-woa2009_K_24.gif'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\''+img_root_dir+'/05_lev1_orig/'+exp_file6+'\');">\n')
         f1.write('    <img class="image1" src=\''+img_root_dir+'/05_lev1_orig/'+exp_file6+'\' /></a></td>\n')

         exp_file7=exp_name+'_'+var1+'_mean.grid_tr025-lev1_orig.PERU_lonlat-woa2009_K_24.gif'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\''+img_root_dir+'/05_lev1_orig/'+exp_file7+'\');">\n')
         f1.write('    <img class="image1" src=\''+img_root_dir+'/05_lev1_orig/'+exp_file7+'\' /></a></td>\n')

# matplotlib PERU  var1 - woa2009  K=24
         exp_file15=exp_name+'_PERU_'+var1+'_'+depth2+'_k_20-26.zip.png'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+img_root_dir+'/PERU.'+var1+'.SAV/'+exp_file15+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+img_root_dir+'/PERU.'+var1+'.SAV/'+exp_file15+'\' /></a></td>\n')

         exp_file16=exp_name+'_PERU_'+var1+'_'+depth2+'_k_20-26_ZOOM.zip.png'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+img_root_dir+'/PERU.'+var1+'.SAV/'+exp_file16+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+img_root_dir+'/PERU.'+var1+'.SAV/'+exp_file16+'\' /></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()
