#!/bin/sh
# Generate on stdout HTML energy series tables.
# Generate sub-pages if requested rather than having everything in-line.
# Also create/update CSV (etc) machine-readable versions.
# All top-level sections at h2 level to allow capture in table of contents.
# Optionally farm out the detailed content to sub-pages, ie not on stdout,
# which are updated only if they actually change.
#
# Usage:
#     $0 [-lite|-offline] [-subpages]
#
# -lite  generate lite/mobile smaller HTML with some detail omitted
# -offline  generate smaller HTML offline pages
# -subpages  generate charts in sub-pages to keep main page size down

ISLITE=false
WEIGHT="d"
if [ "-lite" = "$1" ]; then
    ISLITE=true
    WEIGHT="m"
    shift
fi
if [ "-offline" = "$1" ]; then
    ISLITE=true
    WEIGHT="o"
    shift
fi

VARDETAILINLINE=true
if [ "-subpages" = "$1" ]; then
    SUBPAGES=true
    VARDETAILINLINE=false
    shift
fi

# Build detail in-line (true) or in sub-pages.

# Top directory for the consolidate configuration and outputs.
CONSOLIDDIR=data/consolidated
# Standardised output data directory.
CONSOLIDOUTDIR=data/consolidated/energy/std
# Configuration files.
CONFIGDATASOURCES=$CONSOLIDDIR/config_data_sources.csv
CONFIGGRANULARITY=$CONSOLIDDIR/config_granularity.csv
CONFIGVARIABLES=$CONSOLIDDIR/config_variables.csv
CONFIGSYNTH=$CONSOLIDDIR/config_synthetic.csv

# Extract permitted source names.
DATASOURCES="$(awk -F, < $CONFIGDATASOURCES '/^[^#]/ {print $1}')"
# Extract permitted granularities: reverse to show coarsest summaries first.
GRANULARITIES="$(tac $CONFIGGRANULARITY | awk -F, '/^[^#]/ {print $1}')"
# Extract permitted variables (ie reported-on quantities).
VARIABLES="$(awk -F, < $CONFIGVARIABLES '/^[^#]/ {print $1}')"

# Stem of HTML page name and the sub-page directory.
ENERGYSERIESNAME=energy-series-dataset
# Directory for the sub-pages.
SUBPAGEDIR="$ENERGYSERIESNAME"

# Create dir for sub-pages if needed.
if [ "true" != "$VARDETAILINLINE" ] && [ ! -d "$SUBPAGEDIR" ]; then
    mkdir "$SUBPAGEDIR" || exit 1
    chmod a+rx "$SUBPAGEDIR"
fi


# For parallelising...
# (Given that the target machines have 4 or 8 cores.)
# Number of CPUs.
NPROC="$(nproc)"
# Number of distinct granularities.
NGRAN="$(echo "$GRANULARITIES" | wc -w | awk '{print $1}')"
# Default parallelism.
# One more than the number of granularities to have two same in exec window.
NPAR="$(expr 1 + $NGRAN)"
# If more CPUs than default parallelism, bump it up.
if [ "$NPROC" -gt "$NPAR" ]; then NPAR="$NPROC"; fi


# Current year and month (UTC).
YEAR="$(date -u '+%Y')"
MONTH="$(date -u '+%m')"


# Enumerate each of the available parameters.
echo '<section class="cb conc secC">' # Style like SECTION.
echo '<h2 id="Parameters">Parameters</h2>'

echo '<p>Nominally-available data sources:</p>'
echo '<dl>'
for i in $DATASOURCES;
    do
    echo '<dt>'"$i"'</dt>'
    DSNAME="$(awk -F, <$CONFIGDATASOURCES '$1=="'$i'" {s=$2; gsub("\"","",s); print s}')"
    echo '<dd>'"$DSNAME"'</dd>'
    done
echo '</dl>'

echo '<p>Nominally-available variables:</p>'
echo '<dl>'
for i in $VARIABLES;
    do
    echo '<dt>'"$i"'</dt>'
    VARNAME="$(awk -F, <$CONFIGVARIABLES '$1=="'$i'" {s=$2; gsub("\"","",s); print s}')"
    echo '<dd>'"$VARNAME"'</dd>'
    done
echo '</dl>'

echo '<p>Nominally-available granularities:</p>'
echo '<dl>'
for i in $GRANULARITIES;
    do
    echo '<dt>'"$i"'</dt>'
    GRANAME="$(awk -F, <$CONFIGGRANULARITY '$1=="'$i'" {s=$2; gsub("\"","",s); print s}')"
    echo '<dd>'"$GRANAME"'</dd>'
    done
echo '</dl>'

echo '</section>'


# Insert recent 16WW utility consumption.
RECENT="$(sh .work/script/recentEGUse.sh 7)"
if [ "" != "$RECENT" ]; then
    echo '<h2 id="Recent">16WW Recent Utility Stats</h2>'
    echo '<div>'"$RECENT"'</div>'
    if [ "true" != "$ISLITE" ]; then
    echo '<div>'"$(sh .work/script/recentEGUse.sh 14)"'</div>'
    fi
    echo '<div>'"$(sh .work/script/recentEGUse.sh 28)"'</div>'
    echo '<div>'"$(sh .work/script/recentEGUse.sh 365)"'</div>'
    if [ "true" != "$ISLITE" ]; then
    echo '<div>'"$(sh .work/script/recentEGUse.sh 3652)"'</div>'
    echo '<div>'"$(sh .work/script/recentEGUse.sh 7305)"'</div>'
    fi
fi | uniq


# Insert energy CO2 net footprint year-to-date and projected.
# Recalculate on the fly as it is fast.
RAWCO2OUT="out/yearly/16WW-elect-kgCO2-$YEAR.txt"
if [ -s "$RAWCO2OUT" ]; then
    echo '<h2 id="CO2">16WW Utility Energy CO2 Year-to-date</h2>'
    echo '<p>Raw and projected net kgCO2 for year so far for utility grid electricity based on computed live grid intensity and 16WW imports and exports.</p>'
    awk < "$RAWCO2OUT" '{printf("<p><time>%d</time>: year to date (%d months) %dkgCO2 net, full-year linearly projected (more-of-the-same) %dkgCO2 net.</p>\n", $1, int(($2/30)+0.5), int($3), int($4))}'
fi


# Insert energy systems settings section.
SETTINGSSUMMARYSH=script/energySystemsSettingsSummary.sh
if [ -s "${SETTINGSSUMMARYSH}" ]; then
    sh script/energySystemsSettingsSummary.sh
else
    echo "WARNING: missing script ${SETTINGSSUMMARYSH}" 1>&2
fi


# Enumerate relevant system events.
echo '<section class="cb conc secC">' # Style like SECTION.
echo '<h2 id="Events">Energy System Events and Changes</h2>'
sh script/energyEventsTableHTML5.sh
echo '</section>'


# Enumerate over data sets, (sorted) variables first.
TASKLISTSUBPAGES=
for variable in $(echo "$VARIABLES" xargs -n1 | sort);
    do
    vardir="$CONSOLIDOUTDIR/$variable"
    if [ ! -d "$vardir" ]; then
        continue
    fi
    VARNAME="$(awk -F, <"$CONFIGVARIABLES" '$1=="'"$variable"'" {s=$2; gsub("\"","",s); print s}')"
# Treat as a DigitalDocument, with the CSV file as an text/csv encoding.
echo '<section class="cb conc secC">' # Style like SECTION.
echo '<h2 id="V-'"$variable"'">Variable '"$variable"': '"$VARNAME"'</h2>'

    if [ "" != "$(egrep < "$CONFIGSYNTH" '^'"$variable"',...')" ]; then
        echo "<p>(Formula for synth: <code>$(awk -F, < "$CONFIGSYNTH" '$1~/^'"$variable"$'/ {print $2}')</code>)</p>"
    fi

    for granularity in $GRANULARITIES;
        do
        grandir="$vardir/$granularity"
        if [ ! -d "$grandir" ]; then
            continue
        fi

        consolidated="$grandir/$variable-$granularity.csv"
        if [ -s "$consolidated" ]; then
firstDate="$(awk -F, <"$consolidated" '/^2/{print $1;exit}')"
lastDate="$(awk -F, <"$consolidated" '/^2/{d=$1}END{print d}')"
echo '<details itemprop=hasPart itemscope itemtype=http://schema.org/DigitalDocument>'
echo "<summary itemprop=description>Variable: $variable; granularity: $granularity; date range: $firstDate/$lastDate.</summary>"
echo '<meta itemprop=temporalCoverage content="'"$firstDate/$lastDate"'">'
# TODO: add isBasedOn pointing to source files
echo '<p itemprop=encoding itemscope itemtype=http://schema.org/DataDownload>Data as CSV: <a href="//WWW.earth.org.uk/'$consolidated'" itemprop=contentUrl><code>'$consolidated'</code></a>.<meta itemprop=encodingFormat content="text/csv"></p>'
            # INSERT SONIFICATION(S) if appropriate.
            # Bare/simple sonification.
            bareSonification="$grandir/$variable-$granularity.mid"
            if [ -s "$bareSonification" ]; then
echo '<p itemprop=encoding itemscope itemtype=http://schema.org/DataDownload>Data as a simple MIDI sonification: <a href="//WWW.earth.org.uk/'$bareSonification'" itemprop=contentUrl><code>'$bareSonification'</code></a>.<meta itemprop=encodingFormat content="audio/midi"></p>'
            fi
            # Mildly-augmented sonification, eg some alignment and rhythm.
            mildSonification="$grandir/$variable-$granularity.mild.mid"
            if [ -s "$mildSonification" ]; then
echo '<p itemprop=encoding itemscope itemtype=http://schema.org/DataDownload>Data as a mildly-augmented MIDI sonification: <a href="//WWW.earth.org.uk/'$mildSonification'" itemprop=contentUrl><code>'$mildSonification'</code></a>.<meta itemprop=encodingFormat content="audio/midi"></p>'
            fi
            # 'House' sonification, aiming for 'danceable'!
            houseSonification="$grandir/$variable-$granularity.house.mid"
            if [ -s "$houseSonification" ]; then
echo '<p itemprop=encoding itemscope itemtype=http://schema.org/DataDownload>Data as a house-style MIDI sonification: <a href="//WWW.earth.org.uk/'$houseSonification'" itemprop=contentUrl><code>'$houseSonification'</code></a>.<meta itemprop=encodingFormat content="audio/midi"></p>'
            fi

            # INSERT TABLE if appropriate.
            # Only render a table for Y or M granularity.
            # (Y only for 'lite'.)
            # SP is sub-page where not being in-lined.
            SP="$SUBPAGEDIR/table-$variable-$granularity.html"
            if [ "Y" = "$granularity" ]; then
                if [ "true" = "$VARDETAILINLINE" ]; then
                    sh script/energyTableHTML.sh "$consolidated"
                else
                    TASKLISTSUBPAGES="$TASKLISTSUBPAGES $consolidated"
                    echo "<p><a href=\"$SP\">Table/chart</a></p>"
                fi
            elif [ "M" = "$granularity" ] && [ "true" != "$ISLITE" ]; then
                if [ "true" = "$VARDETAILINLINE" ]; then
                    sh script/energyTableHTML.sh "$consolidated"
                else
                    TASKLISTSUBPAGES="$TASKLISTSUBPAGES $consolidated"
                    echo "<p><a href=\"$SP\">Table/chart</a></p>"
                fi
            fi

echo '</details>'
        fi

        done
echo '</section>'
    done

# Have xargs run the subpages tasks in parallel (max NPAR processes)...
# Run each job with exactly one task name.
# Note -t (verbose) option on xargs if needed.
# Redirect any leaking output to stderr.
echo "$TASKLISTSUBPAGES" | \
    xargs -r -n 1 -P "$NPAR" \
        sh script/energyTableHTML.sh -subpage "$WEIGHT" -pagestem "$ENERGYSERIESNAME" 1>&2


exit 0
