#!/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="NorESM EnKF NOAA SST fulfield assimilation: Ensemble mean vs Single member evaluation"
comment=$'\n'
comment+='<h3> Experiments Details </h3>'
comment+='<ul>'
comment+='    <li> <strong>EnKF</strong> : EnKF assim evaluation using ensmbel mean data  </li>'$'\n'
comment+='    <li> <strong>EnKF_mem01</strong>  : EnKF evaluation for a single member (member 1)  </li>'$'\n'
comment+='    <li> <strong>Freerun_ensmean</strong>   : NorESM  30-members ensemble mean</li>'$'\n'
comment+='    <li> <strong>EXP. Period</strong>  : 1980-2000</li>'$'\n'
comment+='    <li> <strong>Evaluation Period</strong>  : 1990-2000 (except time series plots, which start from 1983)</li>'$'\n'
comment+='    <li> </li>'$'\n' 
#comment+='    <li>For the salinity profile, all models show degradation after assimilation (see RMSE "Depth vs Time" and "Lines" plot). Surface salinity (SSS) & SaltContent 200m (SC200) is strongly degraded by NorESM over the northeastern Pacific Ocean.</li>'$'\n'
#comment+='    <li>The RMSE lines plot for salinity suggests a very high error in the initial states (compared to the Freerun_ensmean) used to initialize the assimilation run.</li>'$'\n'
#comment+='    <li>This could be because the initial conditions were taken from a pre-existing EnKF_mem01 run, which may have degraded the salinity as we are seeing here.</li>'$'\n'
#comment+='    <li>Salinity error reduces and stabilizes until 2000(?), after which an increasing trend in errors is observed for all models. Need more investigation ?</li>'$'\n'
#comment+='    <li>A similar error pattern is seen for temperature.</li>'
#comment+='<ul>'

column=(
 '<p><h2><strong>SST RMSE (with bias)</strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' Freerun_ensmean - EnKF' ' Freerun_ensmean - EnKF_mem01' tr
   EnKF_SST_glb_rmse_wBias_map_NorESM EnKF_mem01_SST_glb_rmse_wBias_map_NorESM  DIFF_EnKF_SST_glb_rmse_wBias_diff_map_NorESM  DIFF_EnKF_mem01_SST_glb_rmse_wBias_diff_map_NorESM    tr
 '<p><h2><strong>SST anomaly Corr.</strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' EnKF - Freerun_ensmean' ' EnKF_mem01 - Freerun_ensmean' tr
   EnKF_SST_glb_corr_map_NorESM EnKF_mem01_SST_glb_corr_map_NorESM  DIFF_EnKF_SST_glb_corr_diff_map_NorESM  DIFF_EnKF_mem01_SST_glb_corr_diff_map_NorESM    tr

'<p><h4><strong>Salinity [0-360,60S-60N] RMSE (with bias)</strong></h4></p>' tr 
   'EnKF' 'EnKF_mem01' ' Freerun_ensmean - EnKF' ' Freerun_ensmean - EnKF_mem01' tr
    EnKF_SAL_rmse_wBias_Depth_vs_Time_NorESM  EnKF_mem01_SAL_rmse_wBias_Depth_vs_Time_NorESM  DIFF_EnKF_SAL_rmse_wBias_diff_Depth_vs_Time_NorESM DIFF_EnKF_mem01_SAL_rmse_wBias_diff_Depth_vs_Time_NorESM  tr

 '<p><h4><strong>Temperature [0-360,60S-60N] RMSE (with bias)</strong></h4></p>' tr 
   'EnKF' 'EnKF_mem01' ' Freerun_ensmean - EnKF' ' Freerun_ensmean - EnKF_mem01' tr
    EnKF_TEM_rmse_wBias_Depth_vs_Time_NorESM  EnKF_mem01_TEM_rmse_wBias_Depth_vs_Time_NorESM  DIFF_EnKF_TEM_rmse_wBias_diff_Depth_vs_Time_NorESM DIFF_EnKF_mem01_TEM_rmse_wBias_diff_Depth_vs_Time_NorESM  tr


 '<p><h2><strong>HC200 RMSE (with bias)</strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' Freerun_ensmean - EnKF' ' Freerun_ensmean - EnKF_mem01' tr
   EnKF_HC200_glb_rmse_wBias_map_NorESM EnKF_mem01_HC200_glb_rmse_wBias_map_NorESM  DIFF_EnKF_HC200_glb_rmse_wBias_diff_map_NorESM  DIFF_EnKF_mem01_HC200_glb_rmse_wBias_diff_map_NorESM    tr

   '<p><h2><strong>HC200 anomaly Corr. </strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' EnKF - Freerun_ensmean' ' EnKF_mem01 - Freerun_ensmean' tr
   EnKF_HC200_glb_corr_map_NorESM EnKF_mem01_HC200_glb_corr_map_NorESM  DIFF_EnKF_HC200_glb_corr_diff_map_NorESM  DIFF_EnKF_mem01_HC200_glb_corr_diff_map_NorESM   tr

   '<p><h2><strong>SSS RMSE (with bias)</strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' Freerun_ensmean - EnKF' ' Freerun_ensmean - EnKF_mem01' tr
   EnKF_SSS_glb_rmse_wBias_map_NorESM EnKF_mem01_SSS_glb_rmse_wBias_map_NorESM  DIFF_EnKF_SSS_glb_rmse_wBias_diff_map_NorESM  DIFF_EnKF_mem01_SSS_glb_rmse_wBias_diff_map_NorESM    tr
 '<p><h2><strong>SSS anomaly Corr.</strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' EnKF - Freerun_ensmean' ' EnKF_mem01 - Freerun_ensmean' tr
   EnKF_SSS_glb_corr_map_NorESM EnKF_mem01_SSS_glb_corr_map_NorESM  DIFF_EnKF_SSS_glb_corr_diff_map_NorESM  DIFF_EnKF_mem01_SSS_glb_corr_diff_map_NorESM    tr

 '<p><h2><strong>SC200 RMSE (with bias)</strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' Freerun_ensmean - EnKF' ' Freerun_ensmean - EnKF_mem01' tr
   EnKF_SC200_glb_rmse_wBias_map_NorESM EnKF_mem01_SC200_glb_rmse_wBias_map_NorESM  DIFF_EnKF_SC200_glb_rmse_wBias_diff_map_NorESM  DIFF_EnKF_mem01_SC200_glb_rmse_wBias_diff_map_NorESM    tr
   '<p><h2><strong>SC200 anomaly Corr. </strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' EnKF - Freerun_ensmean' ' EnKF_mem01 - Freerun_ensmean' tr
   EnKF_SC200_glb_corr_map_NorESM EnKF_mem01_SC200_glb_corr_map_NorESM  DIFF_EnKF_SC200_glb_corr_diff_map_NorESM  DIFF_EnKF_mem01_SC200_glb_corr_diff_map_NorESM   tr

 '<p><h2><strong>SSH anomaly Corr.</strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' EnKF - Freerun_ensmean' ' EnKF_mem01 - Freerun_ensmean' tr
   EnKF_SSH_glb_corr_map_NorESM EnKF_mem01_SSH_glb_corr_map_NorESM  DIFF_EnKF_SSH_glb_corr_diff_map_NorESM  DIFF_EnKF_mem01_SSH_glb_corr_diff_map_NorESM    tr

   '<p><h2><strong>HC2000 avg@[-5,5] RMSE (with bias)</strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' Freerun_ensmean - EnKF' ' Freerun_ensmean - EnKF_mem01' tr
   EnKF_HC200_EPO_rmse_wBias_map_lonVsMon_NorESM EnKF_mem01_HC200_EPO_rmse_wBias_map_lonVsMon_NorESM  DIFF_EnKF_HC200_EPO_rmse_wBias_diff_map_lonVsMon_NorESM DIFF_EnKF_mem01_HC200_EPO_rmse_wBias_diff_map_lonVsMon_NorESM  tr
 '<p><h2><strong>HC200 anomaly avg@[-5,5] Correlation</strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' EnKF - Freerun_ensmean' ' EnKF_mem01 - Freerun_ensmean' tr
   EnKF_HC200_EPO_corr_map_lonVsMon_NorESM EnKF_mem01_HC200_EPO_corr_map_lonVsMon_NorESM  DIFF_EnKF_HC200_EPO_corr_diff_map_lonVsMon_NorESM DIFF_EnKF_mem01_HC200_EPO_corr_diff_map_lonVsMon_NorESM  tr

   '<p><h2><strong>Z20 avg@[-5,5] RMSE (with bias)</strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' Freerun_ensmean - EnKF' ' Freerun_ensmean - EnKF_mem01' tr
   EnKF_Z20_EPO_rmse_wBias_map_lonVsMon_NorESM EnKF_mem01_Z20_EPO_rmse_wBias_map_lonVsMon_NorESM  DIFF_EnKF_Z20_EPO_rmse_wBias_diff_map_lonVsMon_NorESM DIFF_EnKF_mem01_Z20_EPO_rmse_wBias_diff_map_lonVsMon_NorESM  tr
 '<p><h2><strong>Z20 anomaly avg@[-5,5] Correlation</strong></h2></p>' tr
   'EnKF' 'EnKF_mem01' ' EnKF - Freerun_ensmean' ' EnKF_mem01 - Freerun_ensmean' tr
   EnKF_Z20_EPO_corr_map_lonVsMon_NorESM EnKF_mem01_Z20_EPO_corr_map_lonVsMon_NorESM  DIFF_EnKF_Z20_EPO_corr_diff_map_lonVsMon_NorESM DIFF_EnKF_mem01_Z20_EPO_corr_diff_map_lonVsMon_NorESM  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}"

