#!/bin/sh
# Construct an HTML5 table from the the output of OptExportMargin.sh (on stdin).
# The output should nominally be valid XHTML also.
# The output should otherwise be reasonable efficient and compressible.
# This assumes that table-20200310.min.css or newer CSS support is in place.
# See end of script for sample input.

# Published under an Apache 2.0 licence:
#     https://www.apache.org/licenses/LICENSE-2.0
# Copyright 2022 Damon Hart-Davis

echo "<!-- HTML5 table generated by $0 START -->"

awk '
    BEGIN {
        print  "<table class=\"cb tb1 doptsml\">"
        printf "<caption>Model run output diversion time fraction and mean daily energy, for various year slices and export margin values"
        }
    /^## / { # Model parameter line, eg "## YEARSTOUSE 2019 2020 2021"
        printf " (parameter";
        for(i = 2; i <= NF; ++i) { printf(" %s", $i); }
        printf ")";
        next;
        }
    /^#[^#]/ { # Nominal header line, eg "#margin-W year-divert-frac ..."
        print  "</caption>"
        printf "<colgroup>"
        printf "<col span=\"1\">"
        printf "<col span=\"2\">"
        printf "<col span=\"2\" style=\"background-color:rgba(255,255,0,.3)\">"
        printf "<col span=\"2\">"
        print  "</colgroup>"
        printf "<thead>"
        printf "<tr>"
        printf "<th></th>"
        printf "<th colspan=\"2\">All Year</th>"
        printf "<th colspan=\"2\">Mar to Sep (bulk)</th>"
        printf "<th colspan=\"2\">Nov to Jan (worst)</th>"
        printf "</tr>"
        printf "<tr class=\"doptsml\">"
        printf "<th>Export Margin</th>"
        printf "<th>Time</th><th>Daily</th>"
        printf "<th>Time</th><th>Daily</th>"
        printf "<th>Time</th><th>Daily</th>"
        printf "</tr>"
        printf "<tr class=\"doptsml\">"
        printf "<th>W</th>"
        printf "<th>%%</th><th>kWh/d</th>"
        printf "<th>%%</th><th>kWh/d</th>"
        printf "<th>%%</th><th>kWh/d</th>"
        printf "</tr>"
        print  "</thead>"
        print  "<tbody>"
        next;
        }
    /^[0-9]/ { # Data row.
        printf "<tr>"
        printf("<th scope=\"row\">%d</th>", $1);
        printf("<td style=\"text-align:right\">%.1f%%</td>", $2*100);
            printf("<td style=\"text-align:right\">%.1f</td>", $3);
        printf("<td style=\"text-align:right\">%.1f%%</td>", $4*100);
            printf("<td style=\"text-align:right\">%.1f</td>", $5);
        printf("<td style=\"text-align:right\">%.1f%%</td>", $6*100);
            printf("<td style=\"text-align:right\">%.1f</td>", $7);
        print  "</tr>"
        }
    END {
        print  "</tbody>"
        print  "</table>"
        }
    '

echo "<!-- HTML5 table generated by $0 END -->"

exit 0

# Input looks like...

## YEARSTOUSE 2019 2020 2021
## MAXDIVERTW 3000
## DHWWhPERDAY 4000
## STOREMAXWh 7000
#margin-W year-divert-frac year-divert-daily-kWh MarToSep-divert-frac MarToSep-divert-daily-kWh NovToJan-divert-frac NovToJan-divert-daily-kWh
0 0.179 2.70 0.219 3.88 0.073 0.31
50 0.158 2.63 0.205 3.86 0.045 0.25
100 0.151 2.57 0.201 3.83 0.037 0.20
150 0.146 2.52 0.199 3.79 0.031 0.16

