#!/bin/bash
## Generate html file to list figures with table
## Also generate descriptions for upper index.html
## column: figures filename (without suffix) or text or newrow
##    ex. 'c1' 'c2' 'c3' tr    <- text row, tr means end of row
##         fig1 fig2 fig3 tr   <- 3 figures
##         fig4 fig5 fig6 tr   <- 3 figures
##         'title2' tr         <- the row with text only
##         fig6 fig7 tr
##         fig8 fig9 fig10 tr  <- 2 and 3 figures at this and next row


fig2png () { ## if text is a file, return .png name. if not, return text
    i=$1
    ii=$i
    if [ -f "${i}.ps" ] ; then ## ps2png
        convert -density 300 "${i}.ps" ${i}.png
        if [  $? != 0 ];then
            MAGICK_THREAD_LIMIT=1 convert -density 300 "${i}.ps" ${i}.png
        fi
        gzip -f "${i}.ps"
        convert ${i}.png -fuzz 1% -trim +repage tmp-${i}-${pid}.png
        mv tmp-${i}-${pid}.png ${i}.png
    fi
    if [ -f "${i}.png" ] ; then ## trim white edge and make thumbnail
        ## thumbnail
        test "${i}.png" -nt "${i}_thumb.png" && convert -thumbnail 300 "${i}.png" "${i}_thumb.png"
        ii="<a href='${i}.png'><img src='${i%.*}_thumb.png'></a>"
    fi
    if [ -f "${i}" ] ; then ## other files: make thumbnail
        #convert ${i}.png -fuzz 1% -trim +repage tmp-${i}-${pid}.png 
        #mv tmp-${i}-${pid}.png ${i}.png
        ## thumbnail
        test "${i}.png" -nt "${i}_thumb.png" && convert -thumbnail 300 "${i}" "${i%.*}_thumb.png"
        ii="<a href='${i}'><img src='${i%.*}_thumb.png'></a>"
    fi
    test -f "$thumb" || thumb="${i%.*}_thumb.png"
    echo -n "$ii"
}

cd figs/

# parameters
htmlfn="index_reana.html"
title="Comparing SM reanalyses performed using PF weights from the Freerun vs EnOI run" 
comment=$'\n'
#comment+='<h3> Notes </h3>'
comment+='<ul>'
comment+='    <li> <strong>SM_reana</strong> :  SM reanalysis using PF wgt from the Freerun data  </li>'$'\n'
comment+='    <li> <strong>SM_reana_WiEnOI</strong> :  SM reanalysis using PF wgt from the EnOI run (NI reana) data  </li>'$'\n'
comment+='    <li> <strong>Evaluation Period</strong>  : 1990-2005 </li>'$'\n'
comment+='    <li> Green color shows the improvment in SM_reana_WiEnOI over SM_reana</li>'$'\n'
comment+='    <li> </li>'$'\n'
comment+='<ul>'
column=(
 '<p><h2><strong>Correlation: SST anomaly</strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI' tr
      SM2_SST_glb_corr_map_MODELS_MEAN SM2_SST_glb_corr_map_NorESM SM2_SST_glb_corr_map_CESM SM2_SST_glb_corr_map_MPIESM tr 
    'SM_reana' tr
     SM1_SST_glb_corr_map_MODELS_MEAN SM1_SST_glb_corr_map_NorESM SM1_SST_glb_corr_map_CESM SM1_SST_glb_corr_map_MPIESM tr
    'SM_reana_WiEnOI - SM_reana' tr
     DIFF_SST_glb_corr_diff_map_MODELS_MEAN DIFF_SST_glb_corr_diff_map_NorESM DIFF_SST_glb_corr_diff_map_CESM DIFF_SST_glb_corr_diff_map_MPIESM tr 
 '<p><h2><strong>RMSE (with-bias): SST  </strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI' tr
      SM2_SST_glb_rmse_wBias_map_MODELS_MEAN SM2_SST_glb_rmse_wBias_map_NorESM SM2_SST_glb_rmse_wBias_map_CESM SM2_SST_glb_rmse_wBias_map_MPIESM tr 
    'SM_reana' tr
     SM1_SST_glb_rmse_wBias_map_MODELS_MEAN SM1_SST_glb_rmse_wBias_map_NorESM SM1_SST_glb_rmse_wBias_map_CESM SM1_SST_glb_rmse_wBias_map_MPIESM tr
    'SM_reana - SM_reana_WiEnOI' tr
    DIFF_SST_glb_rmse_wBias_diff_map_MODELS_MEAN DIFF_SST_glb_rmse_wBias_diff_map_NorESM DIFF_SST_glb_rmse_wBias_diff_map_CESM DIFF_SST_glb_rmse_wBias_diff_map_MPIESM tr 


     '<p><h4><strong>Salinity [0-360,60S-60N]: RMSE (with-bias)</strong></h4></p>' tr 
   'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI                    '  tr
    SM2_SAL_rmse_wBias_Depth_vs_Time_MODELS_MEAN SM2_SAL_rmse_wBias_Depth_vs_Time_NorESM SM2_SAL_rmse_wBias_Depth_vs_Time_CESM SM2_SAL_rmse_wBias_Depth_vs_Time_MPIESM  tr
    'SM_reana                    ' tr
    SM1_SAL_rmse_wBias_Depth_vs_Time_MODELS_MEAN SM1_SAL_rmse_wBias_Depth_vs_Time_NorESM SM1_SAL_rmse_wBias_Depth_vs_Time_CESM SM1_SAL_rmse_wBias_Depth_vs_Time_MPIESM  tr
     'SM_reana_WiEnOI - SM_reana' tr
    'SM_reana - SM_reana_WiEnOI' tr
    DIFF_SAL_rmse_wBias_diff_Depth_vs_Time_MODELS_MEAN DIFF_SAL_rmse_wBias_diff_Depth_vs_Time_NorESM DIFF_SAL_rmse_wBias_diff_Depth_vs_Time_CESM DIFF_SAL_rmse_wBias_diff_Depth_vs_Time_MPIESM  tr
 
 '<p><h4><strong>Temperature [0-360,60S-60N]: RMSE (with-bias)</strong></h4></p>' tr 
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI                    ' tr
    SM2_TEM_rmse_wBias_Depth_vs_Time_MODELS_MEAN SM2_TEM_rmse_wBias_Depth_vs_Time_NorESM SM2_TEM_rmse_wBias_Depth_vs_Time_CESM SM2_TEM_rmse_wBias_Depth_vs_Time_MPIESM  tr
    'SM_reana                    ' tr
    SM1_TEM_rmse_wBias_Depth_vs_Time_MODELS_MEAN SM1_TEM_rmse_wBias_Depth_vs_Time_NorESM SM1_TEM_rmse_wBias_Depth_vs_Time_CESM SM1_TEM_rmse_wBias_Depth_vs_Time_MPIESM  tr
    'SM_reana - SM_reana_WiEnOI' tr
    DIFF_TEM_rmse_wBias_diff_Depth_vs_Time_MODELS_MEAN DIFF_TEM_rmse_wBias_diff_Depth_vs_Time_NorESM DIFF_TEM_rmse_wBias_diff_Depth_vs_Time_CESM DIFF_TEM_rmse_wBias_diff_Depth_vs_Time_MPIESM  tr


 '<p><h2><strong>Correlation: HC200 anomaly avg@[-5,5]</strong></h2></p>' tr

    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI                    ' tr
     SM2_HC200_EPO_corr_map_lonVsMon_MODELS_MEAN SM2_HC200_EPO_corr_map_lonVsMon_NorESM SM2_HC200_EPO_corr_map_lonVsMon_CESM SM2_HC200_EPO_corr_map_lonVsMon_MPIESM  tr
    'SM_reana                    ' tr
     SM1_HC200_EPO_corr_map_lonVsMon_MODELS_MEAN SM1_HC200_EPO_corr_map_lonVsMon_NorESM SM1_HC200_EPO_corr_map_lonVsMon_CESM SM1_HC200_EPO_corr_map_lonVsMon_MPIESM  tr
    'SM_reana_WiEnOI - SM_reana         ' tr
     DIFF_HC200_EPO_corr_diff_map_lonVsMon_MODELS_MEAN DIFF_HC200_EPO_corr_diff_map_lonVsMon_NorESM DIFF_HC200_EPO_corr_diff_map_lonVsMon_CESM DIFF_HC200_EPO_corr_diff_map_lonVsMon_MPIESM tr
 
 '<p><h2><strong>RMSE (with-bias): HC200 avg@[-5,5]</strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI' tr
     SM2_HC200_EPO_rmse_wBias_map_lonVsMon_MODELS_MEAN SM2_HC200_EPO_rmse_wBias_map_lonVsMon_NorESM SM2_HC200_EPO_rmse_wBias_map_lonVsMon_CESM SM2_HC200_EPO_rmse_wBias_map_lonVsMon_MPIESM  tr
     'SM_reana' tr
     SM1_HC200_EPO_rmse_wBias_map_lonVsMon_MODELS_MEAN SM1_HC200_EPO_rmse_wBias_map_lonVsMon_NorESM SM1_HC200_EPO_rmse_wBias_map_lonVsMon_CESM SM1_HC200_EPO_rmse_wBias_map_lonVsMon_MPIESM  tr
    'SM_reana - SM_reana_WiEnOI' tr
     DIFF_HC200_EPO_rmse_wBias_diff_map_lonVsMon_MODELS_MEAN DIFF_HC200_EPO_rmse_wBias_diff_map_lonVsMon_NorESM DIFF_HC200_EPO_rmse_wBias_diff_map_lonVsMon_CESM DIFF_HC200_EPO_rmse_wBias_diff_map_lonVsMon_MPIESM tr

  '<p><h2><strong>Correlation: HC200 anomaly averaged over [120E-280E,5S-5N]</strong></h2></p>' tr
     DIFF_HC200_EPO_corr_lines_seasCyc   tr


     
 '<p><h2><strong>Correlation: Z20 anomaly avg@[-5,5]</strong></h2></p>' tr

    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI' tr
     SM2_Z20_EPO_corr_map_lonVsMon_MODELS_MEAN SM2_Z20_EPO_corr_map_lonVsMon_NorESM SM2_Z20_EPO_corr_map_lonVsMon_CESM SM2_Z20_EPO_corr_map_lonVsMon_MPIESM  tr
    'SM_reana' tr
     SM1_Z20_EPO_corr_map_lonVsMon_MODELS_MEAN SM1_Z20_EPO_corr_map_lonVsMon_NorESM SM1_Z20_EPO_corr_map_lonVsMon_CESM SM1_Z20_EPO_corr_map_lonVsMon_MPIESM  tr
    'SM_reana_WiEnOI - SM_reana' tr
    'SM_reana - SM_reana_WiEnOI' tr
     DIFF_Z20_EPO_corr_diff_map_lonVsMon_MODELS_MEAN DIFF_Z20_EPO_corr_diff_map_lonVsMon_NorESM DIFF_Z20_EPO_corr_diff_map_lonVsMon_CESM DIFF_Z20_EPO_corr_diff_map_lonVsMon_MPIESM tr

 '<p><h2><strong>RMSE (with-bias): Z20 avg@[-5,5]</strong></h2></p>' tr
     'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'RMSE (with-bias): SM_reana_WiEnOI' tr
     SM2_Z20_EPO_rmse_wBias_map_lonVsMon_MODELS_MEAN SM2_Z20_EPO_rmse_wBias_map_lonVsMon_NorESM SM2_Z20_EPO_rmse_wBias_map_lonVsMon_CESM SM2_Z20_EPO_rmse_wBias_map_lonVsMon_MPIESM  tr
     'RMSE (with-bias): SM_reana' tr
     SM1_Z20_EPO_rmse_wBias_map_lonVsMon_MODELS_MEAN SM1_Z20_EPO_rmse_wBias_map_lonVsMon_NorESM SM1_Z20_EPO_rmse_wBias_map_lonVsMon_CESM SM1_Z20_EPO_rmse_wBias_map_lonVsMon_MPIESM  tr
     'RMSE (with-bias) Diff: SM_reana - SM_reana_WiEnOI' tr
     DIFF_Z20_EPO_rmse_wBias_diff_map_lonVsMon_MODELS_MEAN DIFF_Z20_EPO_rmse_wBias_diff_map_lonVsMon_NorESM DIFF_Z20_EPO_rmse_wBias_diff_map_lonVsMon_CESM DIFF_Z20_EPO_rmse_wBias_diff_map_lonVsMon_MPIESM tr

 '<p><h2><strong>Correlation: Z20 anomaly averaged over [120E-280E,5S-5N]</strong></h4></p> ' tr
     DIFF_Z20_EPO_corr_lines_seasCyc tr 


 '<p><h2><strong>Correlation: SSS anomaly</strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI' tr
      SM2_SSS_glb_corr_map_MODELS_MEAN SM2_SSS_glb_corr_map_NorESM SM2_SSS_glb_corr_map_CESM SM2_SSS_glb_corr_map_MPIESM tr 
    'SM_reana' tr
     SM1_SSS_glb_corr_map_MODELS_MEAN SM1_SSS_glb_corr_map_NorESM SM1_SSS_glb_corr_map_CESM SM1_SSS_glb_corr_map_MPIESM tr
    'SM_reana_WiEnOI - SM_reana' tr
     DIFF_SSS_glb_corr_diff_map_MODELS_MEAN DIFF_SSS_glb_corr_diff_map_NorESM DIFF_SSS_glb_corr_diff_map_CESM DIFF_SSS_glb_corr_diff_map_MPIESM tr 
 '<p><h2><strong>RMSE (with-bias): SSS  </strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI' tr
      SM2_SSS_glb_rmse_wBias_map_MODELS_MEAN SM2_SSS_glb_rmse_wBias_map_NorESM SM2_SSS_glb_rmse_wBias_map_CESM SM2_SSS_glb_rmse_wBias_map_MPIESM tr 
    'SM_reana' tr
     SM1_SSS_glb_rmse_wBias_map_MODELS_MEAN SM1_SSS_glb_rmse_wBias_map_NorESM SM1_SSS_glb_rmse_wBias_map_CESM SM1_SSS_glb_rmse_wBias_map_MPIESM tr
    'SM_reana - SM_reana_WiEnOI' tr
     DIFF_SSS_glb_rmse_wBias_diff_map_MODELS_MEAN DIFF_SSS_glb_rmse_wBias_diff_map_NorESM DIFF_SSS_glb_rmse_wBias_diff_map_CESM DIFF_SSS_glb_rmse_wBias_diff_map_MPIESM tr 

    
  '<p><h2><strong>Correlation: SSH anomaly</strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI' tr
      SM2_SSH_glb_corr_map_MODELS_MEAN SM2_SSH_glb_corr_map_NorESM SM2_SSH_glb_corr_map_CESM SM2_SSH_glb_corr_map_MPIESM tr
    'SM_reana' tr
     SM1_SSH_glb_corr_map_MODELS_MEAN SM1_SSH_glb_corr_map_NorESM SM1_SSH_glb_corr_map_CESM SM1_SSH_glb_corr_map_MPIESM tr
    'SM_reana_WiEnOI - SM_reana' tr
     DIFF_SSH_glb_corr_diff_map_MODELS_MEAN DIFF_SSH_glb_corr_diff_map_NorESM DIFF_SSH_glb_corr_diff_map_CESM DIFF_SSH_glb_corr_diff_map_MPIESM tr

 '<p><h2><strong>Correlation: HC200 anomaly</strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI' tr
      SM2_HC200_glb_corr_map_MODELS_MEAN SM2_HC200_glb_corr_map_NorESM SM2_HC200_glb_corr_map_CESM SM2_HC200_glb_corr_map_MPIESM tr
    'SM_reana' tr
     SM1_HC200_glb_corr_map_MODELS_MEAN SM1_HC200_glb_corr_map_NorESM SM1_HC200_glb_corr_map_CESM SM1_HC200_glb_corr_map_MPIESM tr
    'SM_reana_WiEnOI - SM_reana' tr
     DIFF_HC200_glb_corr_diff_map_MODELS_MEAN DIFF_HC200_glb_corr_diff_map_NorESM DIFF_HC200_glb_corr_diff_map_CESM DIFF_HC200_glb_corr_diff_map_MPIESM tr
 '<p><h2><strong>RMSE (with-bias): HC200  </strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI' tr
      SM2_HC200_glb_rmse_wBias_map_MODELS_MEAN SM2_HC200_glb_rmse_wBias_map_NorESM SM2_HC200_glb_rmse_wBias_map_CESM SM2_HC200_glb_rmse_wBias_map_MPIESM tr
    'SM_reana' tr
     SM1_HC200_glb_rmse_wBias_map_MODELS_MEAN SM1_HC200_glb_rmse_wBias_map_NorESM SM1_HC200_glb_rmse_wBias_map_CESM SM1_HC200_glb_rmse_wBias_map_MPIESM tr
    'SM_reana - SM_reana_WiEnOI' tr
     DIFF_HC200_glb_rmse_wBias_diff_map_MODELS_MEAN DIFF_HC200_glb_rmse_wBias_diff_map_NorESM DIFF_HC200_glb_rmse_wBias_diff_map_CESM DIFF_HC200_glb_rmse_wBias_diff_map_MPIESM tr

 '<p><h2><strong>Correlation: SC200 anomaly</strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI' tr
      SM2_SC200_glb_corr_map_MODELS_MEAN SM2_SC200_glb_corr_map_NorESM SM2_SC200_glb_corr_map_CESM SM2_SC200_glb_corr_map_MPIESM tr
    'SM_reana' tr
     SM1_SC200_glb_corr_map_MODELS_MEAN SM1_SC200_glb_corr_map_NorESM SM1_SC200_glb_corr_map_CESM SM1_SC200_glb_corr_map_MPIESM tr
    'SM_reana_WiEnOI - SM_reana' tr
     DIFF_SC200_glb_corr_diff_map_MODELS_MEAN DIFF_SC200_glb_corr_diff_map_NorESM DIFF_SC200_glb_corr_diff_map_CESM DIFF_SC200_glb_corr_diff_map_MPIESM tr
 '<p><h2><strong>RMSE (with-bias): SC200  </strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'SM_reana_WiEnOI' tr
      SM2_SC200_glb_rmse_wBias_map_MODELS_MEAN SM2_SC200_glb_rmse_wBias_map_NorESM SM2_SC200_glb_rmse_wBias_map_CESM SM2_SC200_glb_rmse_wBias_map_MPIESM tr
    'SM_reana' tr
     SM1_SC200_glb_rmse_wBias_map_MODELS_MEAN SM1_SC200_glb_rmse_wBias_map_NorESM SM1_SC200_glb_rmse_wBias_map_CESM SM1_SC200_glb_rmse_wBias_map_MPIESM tr
    'SM_reana - SM_reana_WiEnOI' tr
     DIFF_SC200_glb_rmse_wBias_diff_map_MODELS_MEAN DIFF_SC200_glb_rmse_wBias_diff_map_NorESM DIFF_SC200_glb_rmse_wBias_diff_map_CESM DIFF_SC200_glb_rmse_wBias_diff_map_MPIESM tr
    )
    ## number of figures in a row
    ## should be a array
ncolarr=${#column[*]}

thumb="THUMBNAIL"
#export MAGICK_THREAD_LIMIT=1   ## avoid "libgomp: Thread creation failed"

# html header
text=$'\n'
text+=$'<!DOCTYPE html>\n'
text+=$'<html>\n'
text+=$'<body>\n'

# put title on top
text+=$'<div id=comment>\n'
text+=$'<p>\n'
text+=$'<meta http-equiv="expires" content="0">\n'
text+=$'<h1>\n'
text+="$title"
text+=$'</h1>\n'
text+="$comment"
#text+=$'</p>\n'
text+=$'</div>\n'


nl=$'\n'  #newline
# convert ps to png and output entry to html
pid=$$
row=1
icol=0


text+="<table>"$'\n'
text+="<tr>"$'\n'

# if column start with non integer
while [ ! -z "${column[$icol]}" ] ; do
    dst="${column[$icol]}"
    if [ "${dst}" = 'tr' ]; then
        dst='</tr>'$'\n''<tr>'
    fi
    if [ "${dst}" = '</tr><tr>' ]; then
        text+="${dst}"$'\n'
    else
        text+="<th>"$(fig2png "${dst}")"</th>"$'\n'
    fi
    icol=$(( $icol + 1))
done
wait

## show figures with table
text+="</tr>"$'\n'
text+="</table>"$'\n'

# html footer
text+=$'</body>\n'
text+=$'</html> \n'

# output to html file
echo "${text}" >  "${htmlfn}"

