#!/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

#;DIAG_NORCPM; FIGFILES: 
    # figure files name "without suffix"
    # should be ps or png file
    # with suffix: make thumbnail only
#;DIAG_NORCPM; HTMLFILENAME: index.html
#;DIAG_NORCPM; TITLE: TITLE here
#;DIAG_NORCPM; COMMENT: COMMENT here
#;DIAG_NORCPM; SUMMARY: 
#;DIAG_NORCPM; COLUMN: 1
#;DIAG_NORCPM; THUMBNAIL: 
#;DIAG_NORCPM; ENTRYHTMLFN: entry.html
    ## this file will be read by diag.py, do not modified it.
#;DIAG_NORCPM; ENTRYHTMLCONTAIN: 
    ## this will be the contain of entry.html if not empyt

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"
}
summary_to_html () {
    summary=$1
    html=''
    for i in $summary; do
        h="$(fig2png $i)"
        test "$h" == "br" && h='</br>' || true
        html="$html $h"
    done
    echo -n "$html"
}

# parameters
htmlfn="index.html"
comment="N1_HIST 200hPa Velocity Potential plots
P1: 1980-2000
p2: 2000-2020
"
title="NorESM SST pacemaker 200hPa Velocity Potential plots"
figs=""
column=('P2-P1' tr
'JJA' 'SON' 'DJF'  'MAM' tr
vp200_dif_JJA_P2-P1 
vp200_dif_SON_P2-P1 
vp200_dif_DJF_P2-P1 
vp200_dif_MAM_P2-P1
tr

'Climatology'                                                                   tr
'season' 'all years'              'P1'                   'P2'                   tr
'JJA'    vp200_allp_JJA_1980_2020 vp200_P1_JJA_1980_2000 vp200_P2_JJA_2000_2020 tr
'SON'    vp200_allp_SON_1980_2020 vp200_P1_SON_1980_2000 vp200_P2_SON_2000_2020 tr
'DJF'    vp200_allp_DJF_1980_2020 vp200_P1_DJF_1980_2000 vp200_P2_DJF_2000_2020 tr
'MAM'    vp200_allp_MAM_1980_2020 vp200_P1_MAM_1980_2000 vp200_P2_MAM_2000_2020 tr
'Cor. U200-Atl3 SST' 'Cor. V200-Atl3 SST' 'Cor. U200 NCEP2' tr
'P1' tr
cor_ens_U200-Atl3SST_P1 cor_ens_V200-Atl3SST_P1 cor_ens_U200-NCEP2_P1 tr
'P2' tr
cor_ens_U200-Atl3SST_P2 cor_ens_V200-Atl3SST_P2 cor_ens_U200-NCEP2_P2 tr
)
    ## number of figures in a row
    ## should be a array
ncolarr=${#column[*]}
entryfn='entry.html'
entryhtml=''

thumb="vp200_allp_JJA_1980_2020_thumb.png"
summary=""
#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 comment on top
text+=$'<div id="comment">\n'
text+=$'<p>\n'
text+=$'<pre>\n'
text+="$comment"
text+=$'</pre>\n'
#text+=$'</p>\n'
text+=$'</div>\n'

# summary section, mixed with figures and text. not done yet, HOW?
## identy in figures
## put inline figures in table?
if [ ! -z "$summary" ];then
    text+=$'<div id="summary">\n'
    text+=$'<h3>Summary: </h3>\n'
    text+="$(summary_to_html "$summary") "$'\n'
    text+=$'</div>\n'
    text+=$'</br>\n'
    text+=$'<hr>\n'
    text+=$'<h3>Figures:</h3>\n'
    
fi


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


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

# columns
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}"


## The entry snipp html for parent index.html

nowdir=$(basename $(pwd -P))
if [ -z "$entryhtml" ]; then
    entryhtml="${entryhtml}    <span style='display:inline-block'>${nl}"
    entryhtml="${entryhtml}    <a href='${nowdir}/index.html'>${nl}"
    entryhtml="${entryhtml}    <h4>${title}</h4>${nl}"
    entryhtml="${entryhtml}    <img ALIGN='left' src='${nowdir}/${thumb}'>${nl}"
    entryhtml="${entryhtml}    </a>${nl}"
    entryhtml="${entryhtml}    <pre>${comment}</pre>${nl}"
    entryhtml="${entryhtml}    </ br>${nl}"
    entryhtml="${entryhtml}    </span>${nl}"
    entryhtml="${entryhtml}    </ br>${nl}"
    entryhtml="${entryhtml}    <hr>${nl}"
fi
echo "${entryhtml}" > "${entryfn}"

