#!/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="NI_reana vs Freerun (NorESM, CESM, Freerun): Impact of NOAA SST assimilation [1990-2020]"
comment=$'\n'
comment+='<h3> Notes </h3>'
comment+='<ul>'

column=(
 '<p><h2><strong>Correlation: SST anomaly</strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'Freerun' tr
      FR_SST_glb_corr_map_MODELS_MEAN FR_SST_glb_corr_map_NorESM FR_SST_glb_corr_map_CESM FR_SST_glb_corr_map_MPIESM tr 
    'NI_reana' tr
     NI_SST_glb_corr_map_MODELS_MEAN NI_SST_glb_corr_map_NorESM NI_SST_glb_corr_map_CESM NI_SST_glb_corr_map_MPIESM tr
    'NI_reana - Freerun' 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
    'Freerun' tr
      FR_SST_glb_rmse_wBias_map_MODELS_MEAN FR_SST_glb_rmse_wBias_map_NorESM FR_SST_glb_rmse_wBias_map_CESM FR_SST_glb_rmse_wBias_map_MPIESM tr 
    'NI_reana' tr
     NI_SST_glb_rmse_wBias_map_MODELS_MEAN NI_SST_glb_rmse_wBias_map_NorESM NI_SST_glb_rmse_wBias_map_CESM NI_SST_glb_rmse_wBias_map_MPIESM tr
    'Freerun - NI_reana' 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
    'Freerun                    '  tr
    FR_SAL_rmse_wBias_Depth_vs_Time_MODELS_MEAN FR_SAL_rmse_wBias_Depth_vs_Time_NorESM FR_SAL_rmse_wBias_Depth_vs_Time_CESM FR_SAL_rmse_wBias_Depth_vs_Time_MPIESM  tr
    'NI_reana                    ' tr
    NI_SAL_rmse_wBias_Depth_vs_Time_MODELS_MEAN NI_SAL_rmse_wBias_Depth_vs_Time_NorESM NI_SAL_rmse_wBias_Depth_vs_Time_CESM NI_SAL_rmse_wBias_Depth_vs_Time_MPIESM  tr
     'Freerun - NI_reana' 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
    'Freerun                    ' tr
    FR_TEM_rmse_wBias_Depth_vs_Time_MODELS_MEAN FR_TEM_rmse_wBias_Depth_vs_Time_NorESM FR_TEM_rmse_wBias_Depth_vs_Time_CESM FR_TEM_rmse_wBias_Depth_vs_Time_MPIESM  tr
    'NI_reana                    ' tr
    NI_TEM_rmse_wBias_Depth_vs_Time_MODELS_MEAN NI_TEM_rmse_wBias_Depth_vs_Time_NorESM NI_TEM_rmse_wBias_Depth_vs_Time_CESM NI_TEM_rmse_wBias_Depth_vs_Time_MPIESM  tr
     'Freerun - NI_reana' 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
    'Freerun                    ' tr
     FR_HC200_EPO_corr_map_lonVsMon_MODELS_MEAN FR_HC200_EPO_corr_map_lonVsMon_NorESM FR_HC200_EPO_corr_map_lonVsMon_CESM FR_HC200_EPO_corr_map_lonVsMon_MPIESM  tr
    'NI_reana                    ' tr
     NI_HC200_EPO_corr_map_lonVsMon_MODELS_MEAN NI_HC200_EPO_corr_map_lonVsMon_NorESM NI_HC200_EPO_corr_map_lonVsMon_CESM NI_HC200_EPO_corr_map_lonVsMon_MPIESM  tr
    'NI_reana - Freerun         ' 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
    'Freerun' tr
     FR_HC200_EPO_rmse_wBias_map_lonVsMon_MODELS_MEAN FR_HC200_EPO_rmse_wBias_map_lonVsMon_NorESM FR_HC200_EPO_rmse_wBias_map_lonVsMon_CESM FR_HC200_EPO_rmse_wBias_map_lonVsMon_MPIESM  tr
     'NI_reana' tr
     NI_HC200_EPO_rmse_wBias_map_lonVsMon_MODELS_MEAN NI_HC200_EPO_rmse_wBias_map_lonVsMon_NorESM NI_HC200_EPO_rmse_wBias_map_lonVsMon_CESM NI_HC200_EPO_rmse_wBias_map_lonVsMon_MPIESM  tr
     'Freerun - NI_reana' 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: Z20 anomaly avg@[-5,5]</strong></h2></p>' tr

    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'Freerun' tr
     FR_Z20_EPO_corr_map_lonVsMon_MODELS_MEAN FR_Z20_EPO_corr_map_lonVsMon_NorESM FR_Z20_EPO_corr_map_lonVsMon_CESM FR_Z20_EPO_corr_map_lonVsMon_MPIESM  tr
    'NI_reana' tr
     NI_Z20_EPO_corr_map_lonVsMon_MODELS_MEAN NI_Z20_EPO_corr_map_lonVsMon_NorESM NI_Z20_EPO_corr_map_lonVsMon_CESM NI_Z20_EPO_corr_map_lonVsMon_MPIESM  tr
    'NI_reana - Freerun' 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): Freerun' tr
     FR_Z20_EPO_rmse_wBias_map_lonVsMon_MODELS_MEAN FR_Z20_EPO_rmse_wBias_map_lonVsMon_NorESM FR_Z20_EPO_rmse_wBias_map_lonVsMon_CESM FR_Z20_EPO_rmse_wBias_map_lonVsMon_MPIESM  tr
     'RMSE (with-bias): NI_reana' tr
     NI_Z20_EPO_rmse_wBias_map_lonVsMon_MODELS_MEAN NI_Z20_EPO_rmse_wBias_map_lonVsMon_NorESM NI_Z20_EPO_rmse_wBias_map_lonVsMon_CESM NI_Z20_EPO_rmse_wBias_map_lonVsMon_MPIESM  tr
     'RMSE (with-bias) Diff: Freerun - NI_reana' 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: SSS anomaly</strong></h2></p>' tr
    'MODELS_MEAN' 'NorESM' 'CESM' 'MPIESM' tr
    'Freerun' tr
      FR_SSS_glb_corr_map_MODELS_MEAN FR_SSS_glb_corr_map_NorESM FR_SSS_glb_corr_map_CESM FR_SSS_glb_corr_map_MPIESM tr 
    'NI_reana' tr
     NI_SSS_glb_corr_map_MODELS_MEAN NI_SSS_glb_corr_map_NorESM NI_SSS_glb_corr_map_CESM NI_SSS_glb_corr_map_MPIESM tr
    'NI_reana - Freerun' 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
    'Freerun' tr
      FR_SSS_glb_rmse_wBias_map_MODELS_MEAN FR_SSS_glb_rmse_wBias_map_NorESM FR_SSS_glb_rmse_wBias_map_CESM FR_SSS_glb_rmse_wBias_map_MPIESM tr 
    'NI_reana' tr
     NI_SSS_glb_rmse_wBias_map_MODELS_MEAN NI_SSS_glb_rmse_wBias_map_NorESM NI_SSS_glb_rmse_wBias_map_CESM NI_SSS_glb_rmse_wBias_map_MPIESM tr
    'Freerun - NI_reana' 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
    'Freerun' tr
      FR_SSH_glb_corr_map_MODELS_MEAN FR_SSH_glb_corr_map_NorESM FR_SSH_glb_corr_map_CESM FR_SSH_glb_corr_map_MPIESM tr
    'NI_reana' tr
     NI_SSH_glb_corr_map_MODELS_MEAN NI_SSH_glb_corr_map_NorESM NI_SSH_glb_corr_map_CESM NI_SSH_glb_corr_map_MPIESM tr
    'NI_reana - Freerun' 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
    'Freerun' tr
      FR_HC200_glb_corr_map_MODELS_MEAN FR_HC200_glb_corr_map_NorESM FR_HC200_glb_corr_map_CESM FR_HC200_glb_corr_map_MPIESM tr
    'NI_reana' tr
     NI_HC200_glb_corr_map_MODELS_MEAN NI_HC200_glb_corr_map_NorESM NI_HC200_glb_corr_map_CESM NI_HC200_glb_corr_map_MPIESM tr
    'NI_reana - Freerun' 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
    'Freerun' tr
      FR_HC200_glb_rmse_wBias_map_MODELS_MEAN FR_HC200_glb_rmse_wBias_map_NorESM FR_HC200_glb_rmse_wBias_map_CESM FR_HC200_glb_rmse_wBias_map_MPIESM tr
    'NI_reana' tr
     NI_HC200_glb_rmse_wBias_map_MODELS_MEAN NI_HC200_glb_rmse_wBias_map_NorESM NI_HC200_glb_rmse_wBias_map_CESM NI_HC200_glb_rmse_wBias_map_MPIESM tr
    'Freerun - NI_reana' 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
    'Freerun' tr
      FR_SC200_glb_corr_map_MODELS_MEAN FR_SC200_glb_corr_map_NorESM FR_SC200_glb_corr_map_CESM FR_SC200_glb_corr_map_MPIESM tr
    'NI_reana' tr
     NI_SC200_glb_corr_map_MODELS_MEAN NI_SC200_glb_corr_map_NorESM NI_SC200_glb_corr_map_CESM NI_SC200_glb_corr_map_MPIESM tr
    'NI_reana - Freerun' 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
    'Freerun' tr
      FR_SC200_glb_rmse_wBias_map_MODELS_MEAN FR_SC200_glb_rmse_wBias_map_NorESM FR_SC200_glb_rmse_wBias_map_CESM FR_SC200_glb_rmse_wBias_map_MPIESM tr
    'NI_reana' tr
     NI_SC200_glb_rmse_wBias_map_MODELS_MEAN NI_SC200_glb_rmse_wBias_map_NorESM NI_SC200_glb_rmse_wBias_map_CESM NI_SC200_glb_rmse_wBias_map_MPIESM tr
    'Freerun - NI_reana' 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}"

