#!/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='no3'; 


#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
num_col_total=6
if (var1 == 'thetao') | (var1 == 'o2'):  num_col_total=num_col_total+4
if (var1 == 'thetao') | (var1 == 'no3'): num_col_total=num_col_total+4


# là où se fait la liste des exp...
img_root_dir='../../images/'
#initial_dir=img_root_dir+'02_pre-processing'
#suffix="_mean.PRE-PROCESS.gif"
suffix='_density_01_'+var1+'_on_EUC_core_110.5W_isopicne.png'
initial_dir=img_root_dir+'density.SAV'

title1=var1+" 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+'_'+var1+'.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</td>\n\
<td align="center" rowspan="1" colspan="2" style="background-color: #ffffff">thetao-thetao_ref</td>\n'

if (var1 == 'thetao') | (var1 == 'o2'): 
   str1=str1+\
'<td align="center" rowspan="1" colspan="2" style="background-color: #ffffff">o2</td>\n\
<td align="center" rowspan="1" colspan="2" style="background-color: #ffffff">o2-o2_ref</td>\n'

if (var1 == 'thetao') | (var1 == 'no3'): 
   str1=str1+\
'<td align="center" rowspan="1" colspan="2" style="background-color: #ffffff">no3</td>\n\
<td align="center" rowspan="1" colspan="2" style="background-color: #ffffff">no3-no3_ref</td>\n'

str1=str1+'</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="4">ref based on density in the EUC core at 110.5W</td>\n'

if (var1 == 'thetao') | (var1 == 'o2'): 
   str2=str2+\
'<td align="center" rowspan="1" colspan="4">ref based on density in the EUC core at 110.5W</td>\n'

if (var1 == 'thetao') | (var1 == 'no3'): 
   str2=str2+\
'<td align="center" rowspan="1" colspan="4">ref based on density in the EUC core at 110.5W</td>\n'

str2=str2+'</tr>\n'

#f1.write(str2)



os.chdir(initial_dir)
#-------------------------------------------------------------------------------
#   For each project...
#-------------------------------------------------------------------------------
for project in project_list :
   nn=0
   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+'*'+suffix)) :

      print(exp_file,':')
      if nn%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')
   
#  section uo
      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+'EUC_Upwelling_characterize/section.thetao.SAV/'+exp_file1+'\');">\n')
      f1.write('    <img class="image1" src=\''+img_root_dir+'EUC_Upwelling_characterize/section.thetao.SAV/'+exp_file1+'\' /></a></td>\n')

#  section thetao
      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+'EUC_Upwelling_characterize/section.thetao.SAV/'+exp_file2+'\');">\n')
      f1.write('    <img class="image1" src=\''+img_root_dir+'EUC_Upwelling_characterize/section.thetao.SAV/'+exp_file2+'\' /></a></td>\n')

#  density thetao
      exp_file3=exp_name+'_density_01_thetao_on_EUC_core_110.5W_isopicne.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_thetao_on_EUC_core_110.5W_isopicne_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')

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

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

      if (var1 == 'thetao') | (var1 == 'o2'): 
#  density o2 diff
         exp_file7=exp_name+'_density_01_o2-o2_ref_on_EUC_core_110.5W_isopicne.png'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\''+img_root_dir+'/density.SAV/'+exp_file7+'\');">\n')
         f1.write('    <img class="image1" src=\''+img_root_dir+'/density.SAV/'+exp_file7+'\' /></a></td>\n')

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

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

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

      if (var1 == 'thetao') | (var1 == 'no3'): 
#  density no3 diff
         exp_file11=exp_name+'_density_01_no3-no3_ref_on_EUC_core_110.5W_isopicne.png'
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\''+img_root_dir+'/density.SAV/'+exp_file11+'\');">\n')
         f1.write('    <img class="image1" src=\''+img_root_dir+'/density.SAV/'+exp_file11+'\' /></a></td>\n')

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

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

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




#     if var1 != 'thetao' : 
#        exp_file5=exp_name+'_density_01_'+var1+'_on_EUC_core_110.5W_isopicne.png'
#        f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\''+img_root_dir+'/density.SAV/'+exp_file5+'\');">\n')
#        f1.write('    <img class="image1" src=\''+img_root_dir+'/density.SAV/'+exp_file5+'\' /></a></td>\n')
   
      f1.write('</tr>\n')
      ii=ii+1
      nn=nn+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')








