#!/usr/bin/env python


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


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

file2_list=['woa2009*','GLORYS*', 'soda3*', 'cmip5*','cmip6*', 'trop*']      #    cmip5, cmip6, GLORYS, trop, soda3, woa2009
#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']


#===============================================================================
#  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>'+var1+' Atlas Section 110.5W / PERU(surf + 69m-133m)</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>'+var1+' - Atlas Section 110.5W / PERU(surf + 31m-108m)</h1>\n')


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


str2='<tr>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">section 110.50W</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">section 110.50W</td>\n'

print ('str2 = \n',str2)

if var1 == 'thetao' :
   str2=str2+'<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">sst-GLORYS</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">sst-GLORYS</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">hfdsO NATIVE grid</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">hfdsO</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">hfdsO NATIVE grid ZOOM</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">hfdsO ZOOM</td>\n'

str2=str2+'<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">surf NATIVE grid</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">surf</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">surf NATIVE grid ZOOM</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">surf ZOOM</td>\n'

if var1 == 'thetao' :
   str2=str2+'<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">thetao_87m-GLORYS</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">thetao_87m-GLORYS</td>\n'

str2=str2+'<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">87m</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">87m ZOOM</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">31m-108m</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">31m-108m ZOOM</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">69m-133m</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">69m-133m ZOOM</td>\n\
</tr>\n'

print ('str2 = \n',str2)

f1.write(str2)

nb_column = str2.count("\n")

#str2='<tr>\n\
#<td align="center" rowspan="1" colspan="1">grid 025</td>\n\
#<td align="center" rowspan="1" colspan="1">grid 025</td>\n\
#<td align="center" rowspan="1" colspan="1">grid 025</td>\n\
#<td align="center" rowspan="1" colspan="1">grid 025</td>\n\
#<td align="center" rowspan="1" colspan="1">grid 025 (26m)</td>\n\
#<td align="center" rowspan="1" colspan="1">grid 025 natif</td>\n\
#</tr>\n'

#f1.write(str2)


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

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

image_05_lev1_orig_dir='../images/05_lev1_orig' # là où se fait la liste des exp...

image_PERU_hfdsO_dir='../images/PERU.hfdsO.SAV'
image_PERU_dir='../images/PERU.'+var1+'.SAV'

image_PERU_native_grid_hfdsO_dir='../images/PERU_native_grid.hfdsO.SAV'
image_PERU_native_grid_dir='../images/PERU_native_grid.'+var1+'.SAV'

image_section_thetao_dir='../images/section.thetao.SAV'

#os.chdir('./'+image_section_dir)
os.chdir('./'+image_PERU_dir)

if var1 == 'thetao' : 
   surf_name='sst'
else : 
   surf_name = var1+'_surf'

ii=1

#-------------------------------------------------------------------------------
#   For each project...
#-------------------------------------------------------------------------------
#suffix='_section_110.50W_from_TROP_uo_select_EUC_20_percent_75_levels.zip.png'
#suffix='_section_110.50W_from_TROP_'+var1+'_select_EUC_20_percent_0-300m.zip.png'
suffix='_PERU_'+var1+'_sst.zip.png'

for file2 in file2_list :
#  for exp_file in sorted(glob.glob (file2+'*_'+var1+'_mean.grid_tr025-lev1_orig_lonlat_K_1.gif')):
   for exp_file in sorted(glob.glob (file2+'*'+suffix)):
   
#     print(project,':')
      print(os.getcwd().split('/'))

   #-------------------------------------------------------------------------------
   #   For each exp_file...
   #-------------------------------------------------------------------------------
#  for exp_file in sorted(glob.glob (project+'*_'+var1+'_mean.grid_tr025-lev1_orig_lonlat_K_1.gif')):


#    if file2 in exp_file :  

      print(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('_'+var1+'_mean')[0][-9:-5]
         ye1=        exp_file.split('_'+var1+'_mean')[0][-4:]
         exp_title=  exp_file.split('_'+var1+'_mean')[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('_'+var1+'_mean')[0][-9:-5]
         ye1=        exp_file.split('_'+var1+'_mean')[0][-4:]
         exp_title = exp_file.split('_'+var1+'_mean')[0][:]
         color1='#f8e3ff'
      elif exp_file[0:7] == 'woa2009' :
         yb1=        exp_file.split('_'+var1+'_mean')[0][-9:-5]
         ye1=        exp_file.split('_'+var1+'_mean')[0][-4:]
         exp_title = exp_file.split('_'+var1+'_mean')[0][:]
         color1='#f8e3ff'

      exp_name=exp_file.split(suffix)[0]
      print('exp_name :', exp_name)

#     if 'GLORYS' in exp_name : exp_short_name=
   
   #  if ii%4 == 0 : f1.write(str1);  f1.write(str2)
      if ii%4 == 0 : f1.write(str2)
      f1.write('<tr>\n')
     
      every=4      
      for jj in range(nb_column) :
#        print (every, jj, jj%every, nb_column - jj )
         if (jj%every == 0) & ((nb_column - jj) > every): 
            f1.write('<td align="center" rowspan="1" colspan='+str(every)+' style="background-color: #ffffff">n°'+str(ii)+': <span class="stabilo2">'+exp_name+'</span></td>\n')
         jj= jj+1
   
      f1.write('</tr>\n')
      f1.write('<tr>\n')
      
      exp_file01=exp_name+'_section_110.50W_from_TROP_'+var1+'_select_EUC_20_percent_0-300m.zip.png'
      print(exp_file01,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_section_dir+'/'+exp_file01+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_section_dir+'/'+exp_file01+'\' /></a></td>\n')
   
      exp_file02=exp_name+'_section_110.50W_from_TROP_uo_0-300m.zip.png'
      print(exp_file02,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_section_dir+'/'+exp_file02+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_section_dir+'/'+exp_file02+'\' /></a></td>\n')

      if var1 == 'thetao' :
   
         exp_file03=exp_name+'_'+var1+'_mean.grid_tr025-lev1_orig_lonlat-GLORYS_K_1.gif'
         print(exp_file03,':')
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_05_lev1_orig_dir+'/'+exp_file03+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_05_lev1_orig_dir+'/'+exp_file03+'\' /></a></td>\n')
      
         exp_file04=exp_name+'_'+var1+'_mean.grid_tr025-lev1_orig.PERU_lonlat-GLORYS_K_1.gif'
         print(exp_file04,':')
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_05_lev1_orig_dir+'/'+exp_file04+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_05_lev1_orig_dir+'/'+exp_file04+'\' /></a></td>\n')
      
         exp_file041=exp_name+'_PERU_hfdsO_native_grid.zip.png'
         print(exp_file041,':')
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_native_grid_hfdsO_dir+'/'+exp_file041+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_PERU_native_grid_hfdsO_dir+'/'+exp_file041+'\' /></a></td>\n')
      
         exp_file042=exp_name+'_PERU_hfdsO.zip.png'
         print(exp_file042,':')
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_hfdsO_dir+'/'+exp_file042+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_PERU_hfdsO_dir+'/'+exp_file042+'\' /></a></td>\n')
      
         exp_file043=exp_name+'_PERU_hfdsO_native_grid_ZOOM.zip.png'
         print(exp_file043,':')
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_native_grid_hfdsO_dir+'/'+exp_file043+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_PERU_native_grid_hfdsO_dir+'/'+exp_file043+'\' /></a></td>\n')
      
         exp_file044=exp_name+'_PERU_hfdsO_ZOOM.zip.png'
         print(exp_file044,':')
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_hfdsO_dir+'/'+exp_file044+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_PERU_hfdsO_dir+'/'+exp_file044+'\' /></a></td>\n')
   
      exp_file05=exp_name+'_PERU_'+surf_name+'_native_grid.zip.png'
      print(exp_file05,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_native_grid_dir+'/'+exp_file05+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_PERU_native_grid_dir+'/'+exp_file05+'\' /></a></td>\n')
   
      exp_file06=exp_name+'_PERU_'+var1+'_sst.zip.png'
      print(exp_file06,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_dir+'/'+exp_file06+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_PERU_dir+'/'+exp_file06+'\' /></a></td>\n')
   
      exp_file07=exp_name+'_PERU_'+surf_name+'_native_grid_ZOOM.zip.png'
      print(exp_file07,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_native_grid_dir+'/'+exp_file07+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_PERU_native_grid_dir+'/'+exp_file07+'\' /></a></td>\n')
   
      exp_file08=exp_name+'_PERU_'+var1+'_sst_ZOOM.zip.png'
      print(exp_file08,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_dir+'/'+exp_file08+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_PERU_dir+'/'+exp_file08+'\' /></a></td>\n')
   
      if var1 == 'thetao' :
   
         exp_file09=exp_name+'_'+var1+'_mean.grid_tr025-lev1_orig_lonlat-GLORYS_K_23.gif'
         print(exp_file09,':')
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_05_lev1_orig_dir+'/'+exp_file09+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_05_lev1_orig_dir+'/'+exp_file09+'\' /></a></td>\n')
   
         exp_file10=exp_name+'_'+var1+'_mean.grid_tr025-lev1_orig.PERU_lonlat-GLORYS_K_23.gif'
         print(exp_file10,':')
         f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_05_lev1_orig_dir+'/'+exp_file10+'\');">\n')
         f1.write('    <img class="image1" src=\'./'+image_05_lev1_orig_dir+'/'+exp_file10+'\' /></a></td>\n')
   
      exp_file11=exp_name+'_PERU_'+var1+'_87m_k_22.zip.png'
      print(exp_file11,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_dir+'/'+exp_file11+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_PERU_dir+'/'+exp_file11+'\' /></a></td>\n')
   
      exp_file12=exp_name+'_PERU_'+var1+'_87m_k_22_ZOOM.zip.png'
      print(exp_file12,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_dir+'/'+exp_file12+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_PERU_dir+'/'+exp_file12+'\' /></a></td>\n')
   
      exp_file13=exp_name+'_PERU_'+var1+'_31m-108m_k_14-24.zip.png'
      print(exp_file13,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_dir+'/'+exp_file13+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_PERU_dir+'/'+exp_file13+'\' /></a></td>\n')
   
      exp_file14=exp_name+'_PERU_'+var1+'_31m-108m_k_14-24_ZOOM.zip.png'
      print(exp_file14,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_dir+'/'+exp_file14+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_PERU_dir+'/'+exp_file14+'\' /></a></td>\n')
   
      exp_file15=exp_name+'_PERU_'+var1+'_69m-133m_k_20-26.zip.png'
      print(exp_file15,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_dir+'/'+exp_file15+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_PERU_dir+'/'+exp_file15+'\' /></a></td>\n')
   
      exp_file16=exp_name+'_PERU_'+var1+'_69m-133m_k_20-26_ZOOM.zip.png'
      print(exp_file16,':')
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+image_PERU_dir+'/'+exp_file16+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+image_PERU_dir+'/'+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>
"""









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