#!/usr/bin/env python


import os
import sys
import getpass  # just to get user login
import shutil
import datetime
import glob


#===============================================================================
#  Choix
#===============================================================================
#  so indispensable pour calculer la densité => va voir les fichiers so dispo!
var1='so';     Klev2=14;  lev2="lev 14 (26m)"      


#project_list=['soda3', 'cmip5','cmip6', 'trop']      #    soda3, cmip5, cmip6, trop
project_list=['soda3', 'cmip5','cmip6', 'trop']
#project_list=['cmip5','cmip6'] 

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

# là où se fait la liste des exp...
img_root_dir='../../images/'
suffix="_mean.PRE-PROCESS.gif"
initial_dir=img_root_dir+'02_pre-processing'

title1="Density Atlas - for all projects"

#===============================================================================
#  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])
if 'remake' in os.path.basename(sys.argv[0]) : script_name='atlas-density.py' 

script_name_root=script_name.split('.py')[0]

script_dir=os.getcwd()



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

# to open/create a new html file in the write mode
f1 = open(script_name_root+'.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 EUC_Upwelling" />

  <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')


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'

f1.write(str1)

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'

f1.write(str2)

#num_col_total=str2.count('\n')-2
num_col_total=4


os.chdir(initial_dir)
#-------------------------------------------------------------------------------
#   For each project...
#-------------------------------------------------------------------------------
for project in project_list :
   ii=1
   if project == 'GLORYS' : color1='#f8e3ff'
   if project == 'soda3' : color1='#f8e3ff'
   if project == 'woa2009' : color1='#f8e3ff'
   if project == 'cmip5' : color1='#e2ffd7'
   if project == 'cmip6' : color1='#d7fffb'
   if project == 'trop' : color1='#fffed7'
   

   #-------------------------------------------------------------------------------
   #   For each exp_file...
   #-------------------------------------------------------------------------------
#  for exp_file in eval(exp_list) :
   for exp_file in sorted(glob.glob(project+'*'+var1+suffix)) :

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

      exp_name=exp_file.split('_'+var1+'_')[0]

      for jj in range(num_col_total) :
#        print(jj, num_col, jj%num_col)
         if (jj%num_col == 00) : 
            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_file.replace(suffix,'')+'</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(\''+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_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(\''+img_root_dir+'section.thetao.SAV/'+exp_file2+'\');">\n')
      f1.write('    <img class="image1" src=\''+img_root_dir+'section.thetao.SAV/'+exp_file2+'\' /></a></td>\n')

      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')

      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>
"""


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

shutil.copy2(script_name,script_name_root+'.html-remake.py')








