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

#file2_list=['GLORYS*', 'soda3*', 'cmip5*','cmip6*', 'trop*']      #    cmip5, cmip6, GLORYS, trop, soda3
file2_list=['cmip6*']      #    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=[110.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+'.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'
for section in section_list :
   section_str1='%rW'%section
   str1=str1+'<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">'+section_str1+' (75 levels)</td>\n'
   str1=str1+'<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">'+section_str1+' (0-300 m)</td>\n'
   str1=str1+'<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">'+section_str1+' (0-300 m)</td>\n'
   str1=str1+'<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">'+section_str1+' (0-300 m)</td>\n'
   str1=str1+'<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">'+section_str1+' (0-300 m)</td>\n'
   str1=str1+'<td align="center" rowspan="1" colspan="1" style="background-color: #fffed7"> EUC 5% au lieu de 20%\n'
str1=str1+'</tr>\n'

f1.write(str1)


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

image_dir='../images/section.'+var1+'.SAV'  # là où se fait la liste des exp... 
os.chdir('./'+image_dir)

image_dir_5_percent='../images/section.thetao_EUC_5_percent'

ii=1

#-------------------------------------------------------------------------------
#   For each project...
#-------------------------------------------------------------------------------
for file2 in file2_list :
   for exp_file in sorted(glob.glob (file2+'*_section_110.50W_from_TROP_uo_select_EUC_20_percent_75_levels.zip.png')):
   
#     print(project,':')
      print(os.getcwd().split('/'))

   #-------------------------------------------------------------------------------
   #   For each exp_file...
   #-------------------------------------------------------------------------------
      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')
      for section in section_list :
         f1.write('<td align="center" rowspan="1" colspan="5" style="background-color: #ffffff">n°'+str(ii)+': <span class="stabilo2">'+exp_title+'</span></td>\n')
      f1.write('</tr>\n')


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

         section_str2='%-4.2fW'%section
   
         exp_file01=exp_name+'_section_'+section_str2+'_from_TROP_uo_select_EUC_20_percent_75_levels.zip.png'
         print(exp_file01)
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_dir+'/'+exp_file01+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_dir+'/'+exp_file01+'\' /></a></td>\n')
      
#        exp_file01b=exp_name+'_section_'+section_str2+'_from_TROP_uo_select_EUC_5_percent_75_levels.zip.png'
#        print(exp_file01b)
#        f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_dir+'/'+exp_file01b+'\');">\n')
#        f1.write('    <img class="image1" src=\'./'+image_dir+'/'+exp_file01b+'\' /></a></td>\n')
      
         exp_file02=exp_name+'_section_'+section_str2+'_from_TROP_'+var1+'_0-300m.zip.png'
         print(exp_file02)
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_dir+'/'+exp_file02+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_dir+'/'+exp_file02+'\' /></a></td>\n')
      
         exp_file03=exp_name+'_section_'+section_str2+'_from_TROP_uo_0-300m.zip.png'
         print(exp_file03)
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_dir+'/'+exp_file03+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_dir+'/'+exp_file03+'\' /></a></td>\n')
      
         exp_file04=exp_name+'_section_'+section_str2+'_from_TROP_uo_select_EUC_20_percent_0-300m.zip.png'
         print(exp_file04)
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_dir+'/'+exp_file04+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_dir+'/'+exp_file04+'\' /></a></td>\n')
      
         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')

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