#!/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, so, wo, o2, no3

title1=var1+' Atlas interpolation LINEAR+NEAREST / NEAREST + option'

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

dir='./exp_list/'
img_root_dir='../../images/'

Klev1="20-26";  lev1='69m-133m'
Klev2="24";     lev2="97m"
Klev3="18";     lev3="47m"

obs='woa23'

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


#===============================================================================
#  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\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">PERU</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">Native grid-ZOOM</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">NEAREST+option</td>\n\
<td align="center" rowspan="1" colspan="1" style="background-color: #ffffff">LINEAR+NEAREST</td>\n\
</tr>\n'

f1.write(str1)

str2='<tr>\n\
<td align="center" rowspan="1" colspan="1">surf</td>\n\
<td align="center" rowspan="1" colspan="1">surf</td>\n\
<td align="center" rowspan="1" colspan="1">surf</td>\n\
<td align="center" rowspan="1" colspan="1">surf</td>\n\
</tr>\n'

f1.write(str2)

ii=0




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

#image_dir='../../images/EUC_Upwelling_characterize/section.'+var1+'.SAV'
#suffix='_section_110.50W_from_TROP_uo_select_EUC_20_percent_75_levels.zip.png'

image_dir=img_root_dir+'05_lev1_orig/'
suffix='_'+var1+'_mean.grid_tr025-lev1_orig_lonlat_K_1.png'

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

#-------------------------------------------------------------------------------
#   For each project...
#-------------------------------------------------------------------------------
for project in project_list :
   ii=1
   if project == 'cmip5' : color1='#e2ffd7'
   elif project == 'cmip6' : color1='#d7fffb'
   elif project == 'trop' : color1='#fffed7'
   else : color1='#f8e3ff'
   
   #-------------------------------------------------------------------------------
   #   For each exp_file...
   #-------------------------------------------------------------------------------
   for exp_file in sorted(glob.glob(project+'*'+suffix)) :

      print('\nexp_file : ',ii, '  ',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(suffix)[0]

      print('exp_name : ',ii, '  ',exp_name)

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

      for jj in range(num_col_total) :
         if (jj%num_col == 00) : 
#           print(jj, num_col, num_col_total-jj)
            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_name+'</span></td>\n')

      f1.write('</tr>\n')

      f1.write('<tr>\n')

#-------------------------------------------------------------------------------
#  DISPLAY
#-------------------------------------------------------------------------------

# matplotlib PERU  var1 K=1
      exp_file13=exp_name+'_PERU_'+var1+'_sst.zip.png'
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+img_root_dir+'/EUC_Upwelling_characterize/PERU.'+var1+'.SAV/'+exp_file13+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+img_root_dir+'/EUC_Upwelling_characterize/PERU.'+var1+'.SAV/'+exp_file13+'\' /></a></td>\n')

# matplotlib PERU ZOOM  native grid var1 K=1
      exp_file18=exp_name+'_PERU_'+var1+'_surf_native_grid_ZOOM.zip.png'
#     exp_file18=exp_name+'_PERU_sst_native_grid_ZOOM.zip.png'
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+img_root_dir+'/EUC_Upwelling_characterize/PERU_native_grid.'+var1+'.SAV/'+exp_file18+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+img_root_dir+'/EUC_Upwelling_characterize/PERU_native_grid.'+var1+'.SAV/'+exp_file18+'\' /></a></td>\n')

# matplotlib PERU ZOOM  var1 K=1   NEW interp NEAREST + option 
      exp_file14=exp_name+'_PERU_'+var1+'_sst_ZOOM.zip.png'
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./images_NEAREST_option/'+exp_file14+'\');">\n')
      f1.write('    <img class="image1" src=\'./images_NEAREST_option/'+exp_file14+'\' /></a></td>\n')

# matplotlib PERU ZOOM  var1 K=1   OLD interp LINEAR + NEAREST
      exp_file14=exp_name+'_PERU_'+var1+'_sst_ZOOM.zip.png'
      f1.write('<td style="background-color: '+color1+'"><a href="javascript:;" onclick="ChargeImage1(\'./'+img_root_dir+'/EUC_Upwelling_characterize/PERU.'+var1+'.SAV/'+exp_file14+'\');">\n')
      f1.write('    <img class="image1" src=\'./'+img_root_dir+'/EUC_Upwelling_characterize/PERU.'+var1+'.SAV/'+exp_file14+'\' /></a></td>\n')

      ii=ii+1

#-------------------------------------------------------------------------------
#  end of DISPLAY
#-------------------------------------------------------------------------------

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