#!/usr/bin/env python


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


#===============================================================================
#  Choix
#===============================================================================
var1='thetao'    #   thetao, o2, no3

title1=var1+' Atlas Sections'

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

file2_list=['GLORYS*', 'soda3*', 'cmip5*','cmip6*', 'trop*']      #    cmip5, cmip6, GLORYS, trop, soda3
#file2_list=['trop025_now*']
#file2_list=['trop075_now*']
#file2_list=['trop0??_now*']

#file2_list=['cmip6_GFDL-CM4', \
#'cmip6_TaiESM1', \
#'soda3.11.2_mn_ocean_reg_1980-2015', \
#'soda3.12.2_mn_ocean_reg_1980-2017', \
#'soda3.15.2_mn_ocean_reg_1981-2022', \
#'cmip6_FIO-ESM-2-0', \
#'cmip6_CESM2-WACCM_historical', \
#'cmip6_CESM2_historical', \
#'cmip5_bcc-csm1-1-m', \
#'cmip6_CIESM', \
#'cmip5_MIROC4h']

#file2_list=['cmip6_GFDL-CM4']

#section_list=['85.00W','98.50W','110.50W','120.25W','130.00W','140.50W','170.50W']
section_list=[85,98.5,110.5,120.25,130,140.5,170.5]
#===============================================================================
#  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])

script_dir=os.getcwd()



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

# to open/create a new html file in the write mode
f1 = open(script_name.replace('.py','_'+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 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')


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


str1='<tr>\n'
ii=0
for section in section_list :
   section_str1='%rW'%section
   if ii%2 == 0 : 
      color1='#ededed'
   else : 
      color1='#ffffff'
   str1=str1+'<td align="center" rowspan="1" colspan="1" style="background-color: '+color1+'">'+section_str1+'</td>\n'
   ii=ii+1
str1=str1+'</tr>\n'


f1.write(str1)


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

image_dir='../../images/section.'+var1+'.SAV'

os.chdir('./'+image_dir)
ii=1

#-------------------------------------------------------------------------------
#   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'
   
   suffix='_section_110.50W_from_TROP_uo_select_EUC_20_percent_75_levels.zip.png'
   #-------------------------------------------------------------------------------
   #   For each exp_file...
   #-------------------------------------------------------------------------------
   for exp_file in sorted(glob.glob(project+'*'+suffix)) :

      print('\nexp_file : ',exp_file)

      if exp_file[0:4] == 'cmip' :
         yb1=        exp_file.split('_Omon')[0][-17:-13]
         ye1=        exp_file.split('_Omon')[0][-8:-4]
         exp_title = exp_file.split('_historical_')[0]
         if exp_file[4] == '5' : color1='#e2ffd7'
         if exp_file[4] == '6' : color1='#d7fffb'
      elif exp_file[0:4] == 'trop' :
         yb1=        exp_file.split('_section_')[0][-9:-5]
         ye1=        exp_file.split('_section_')[0][-4:]
         exp_title=  exp_file.split('_section_')[0][0:-10]
         color1='#fffed7'
      elif exp_file[0:9] == 'GLORYS2V3' :
         yb1=        exp_file.split('_R20130808')[0][-9:-5]
         ye1=        exp_file.split('_R20130808')[0][-4:]
         exp_title = exp_file.split('_R20130808')[0][:]
         color1='#f8e3ff'
      elif exp_file[0:5] == 'soda3' :
         yb1=        exp_file.split('_section_')[0][-9:-5]
         ye1=        exp_file.split('_section_')[0][-4:]
         exp_title = exp_file.split('_section_')[0][:]
         color1='#f8e3ff'

      exp_name=exp_file.split('_section_110.50W_from_TROP_uo_select_EUC_20_percent_75_levels.zip.png')[0]
      print('exp_name : ',exp_name,'\n')
   
      if ii%4 == 0 : f1.write(str1)

      f1.write('<tr>\n')
      f1.write('<td align="center" rowspan="1" colspan="7" style="background-color: #ffffff">n°'+str(ii)+': <span class="stabilo2">'+exp_name+'</span></td>\n')
      f1.write('</tr>\n')


      f1.write('<tr>\n')
      for section in section_list :

         section_str2='%-4.2fW'%section
   
         exp_file041=exp_name+'_section_'+section_str2+'_from_TROP_'+var1+'_select_EUC_20_percent_0-300m.zip.png'
         print(exp_file041)
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_dir+'/'+exp_file041+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_dir+'/'+exp_file041+'\' /></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()
