#!/bin/sh
# Inserts date-relevant energy stats into an HTML page (eg in the footer).
# Must be fairly quick extracting from existing summaries (not making new ones).
# Should be fairly robust.
# All content wrapped in <aside> as this is a generic insert.
#
# Usage:
#     $0 .source.html [{YYYY|YYYY-MM}]
#
# 1) Always do an overview, eg because of missing/complex temporal coverage.
# 2) Then do a yearly view from YYYY part if available.
# 3) Then do a monthly view from YYYY-MM part if available.

#echo "INFO: $0: $*"  1>&2

# DHD20250424: abbreviating this to save page weight and build time.
echo '<p>See <a href="energy-series-dataset.html">16WW energy series datasets</a>.</p>'
exit 0

SRCHTML="$1"
TEMPCOV="$2"

if [ "" = "$SRCHTML" ]; then
    echo "ERROR: $0: missing source HTML arg."  1>&2
    exit 1
fi

# 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.
#GRANULARITIES="`awk -F, < $CONFIGGRANULARITY '/^[^#]/ {print $1}'`"
# Extract permitted PRIMARY variables (ie reported-on quantities).
VARIABLES="`awk -F, < $CONFIGVARIABLES '/^[^#]/ && $3 ~ /primary/ {print $1}'`"


echo '<aside>'
echo '<section class="cb conc secC">' # Style like SECTION.
# Note: "Auto-Energy-Stats" in output HTML is a rebuild/dependency signature.
echo '<h2 id="Auto-Energy-Stats">16WW Energy Stats Summary</h2>'
echo '<details>'
echo '<summary>A summary of consolidated energy statistics from multiple 16WW data sources.</summary>'


# Insert recent 16WW utility consumption.
#echo '<h3 id="Auto-Energy-Stats--Recent">16WW Recent Utility Stats</h3>'
#echo '<div>'"`sh .work/script/recentEGUse.sh`"'</div>'


# Events...
echo '<h3 id="Auto-Energy-Stats--Events">Events</h3>'
sh script/energyEventsTableHTML5.sh -novarlinks -dateprefix "$TEMPCOV"


# Enumerate over variables first, seeing what yearly data are available.
# Only interested in top-level summaries for now.
for variable in $VARIABLES;
    do
    vardir=$CONSOLIDOUTDIR/$variable
    if [ ! -d $vardir ]; then
        continue
    fi
echo '<section class="cb conc secC">' # Style like SECTION.
VARNAME=`awk -F, <$CONFIGVARIABLES '$1=="'$variable'" {s=$2; gsub("\"","",s); print s}'`
echo '<h3 id="Auto-Energy-Stats-'"$variable"'">'"$VARNAME"' ('$variable')</h3>'

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

        consolidated=$grandir/$variable-$granularity.csv
        if [ -s "$consolidated" ]; then
# Treat as a DigitalDocument, with the CSV file as an text/csv encoding.
echo '<details itemprop=hasPart itemscope itemtype=http://schema.org/DigitalDocument>'
echo "<summary itemprop=description>Yearly $VARNAME.</summary>"
            if [ "" != "`egrep < $CONFIGSYNTH '^'$variable',...'`" ]; then
echo "<p>(Formula for synth: <code>`awk -F, < $CONFIGSYNTH '$1~/^'$variable$'/ {print $2}'`</code>)</p>"
            fi
echo '<p itemprop=encoding itemscope itemtype=http://schema.org/DataDownload>Data as CSV: <a href="//WWW.earth.org.uk/'$consolidated'" itemprop=contentUrl>'$consolidated'</a>.<meta itemprop=encodingFormat content="text/csv"></p>'
            # Only render a table for Y or M granularity.
sh script/energyTableHTML.sh $consolidated $TEMPCOV
echo '</details>'
        fi

        done

echo '</section>'
    done


echo '<p>See all <a href="energy-series-dataset.html">16WW energy series datasets</a>.<p>'
echo '</details>'
echo '</section>'
echo '</aside>'


exit 0
