#!/bin/sh
# Compute total carbon footprint for 16WW for given year or month.
# Provides raw to-date accounted for and full-year extrapolation.
# See saving-electricity-2021.html
# Uses Enphase and FUELINST data to do so at 1h granularity.
#
# Usage:
#     $0 [YYYY [MM]]
#
# where YYYY is a 4 digit year for FUELINST data already archived
# and LZMA compressed with xz.
# If no year is supplied, then the current year is used.
#
# Full-year results are saved to:
#     $OUTDIR/16WW-elect-kgCO2-$YEAR[$MONTH].txt
# in the form:
#     year days raw-CO2-to-date full-year-CO2-estimate
#
# Should be safe (if wasteful) to run multiple instances concurrently.

# METHOD
# Nets import and export for 1h blocks.
# Computes grid carbon intensity for 1h blocks.
# Joins and multiplies the two, scales up to allow for missing data points.

CURRENTYEAR=unknown
if [ "$#" -lt 1 ]; then
    #echo "ERROR: please supply a 4-digit year as the first argument." 1>&2
    #exit 1
    CURRENTYEAR=true
    YEAR="$(date -u '+%Y')"
else
    YEAR="$1"
fi

YEARMONTH="$YEAR"
MONTH=""
if [ "$#" -ge 2 ]; then
    MONTH="$2"
    YEARMONTH="$YEAR$MONTH"
fi

OUTDIR=out/yearly
if [ ! -d "$OUTDIR" ]; then
    mkdir -p "$OUTDIR"
    chmod a+rx "$OUTDIR"
fi
FUELINSTGZFILENAME="$OUTDIR/FUELINST-1h-$YEARMONTH.csv.gz"
ENPHASEGZFILENAME="$OUTDIR/Enphase-1h-$YEARMONTH.csv.gz"
JOINGZFILENAME="$OUTDIR/Enphase-join-FUELINST-1h-$YEARMONTH.csv.gz"
WWCFFILENAME="$OUTDIR/16WW-elect-kgCO2-$YEARMONTH.txt"

# If either input data collection fails,
# if early in the year exit with a zero result,
# else exit with an error.
if sh script/analytic/Enphase-bucket-1h.sh "$YEAR" "$MONTH" &&
   sh script/analytic/FUELINST-bucket-1h.sh "$YEAR" "$MONTH"; then
    echo "INFO: input data collected." 1>&2
elif [ "true" = "$CURRENTYEAR" ] && [ "$(date '+%m')" -le 2 ]; then
    echo "INFO: early data not available for current year (yet)." 1>&2
    echo "$YEARMONTH 0 0 0" > "$WWCFFILENAME.$$"
    mv -f "$WWCFFILENAME.$$" "$WWCFFILENAME"
    chmod a+r "$WWCFFILENAME"
    exit 0
else
    echo "ERROR: no data found for $YEARMONTH." 1>&2
    exit 1
fi


# Minimum hours in (non-leap) year.
HINY=8760
# Hours in leap year.
HINLY=8784

# Compute hours in union of data sets:
HINDATA="$(cat "$FUELINSTGZFILENAME" "$ENPHASEGZFILENAME" | gzip -d | \
    awk -F, '{print $1}' | sort -u | wc -l)"
#echo "INFO: unique hours in data: $HINDATA" 1>&2
HINTHISY="$HINY"
if [ "$HINDATA" -gt "$HINY" ]; then
    echo "INFO: assuming that this is a leap year..." 1>&2
    HINTHISY="$HINLY"
fi


TMPBASE="/tmp/16WWCO2-$$"
FTMP="$TMPBASE-FUELINST.csv"
ETMP="$TMPBASE-Enphase.csv"
JTMP="$TMPBASE-join.csv"

gzip -d < "$FUELINSTGZFILENAME" > "$FTMP"
gzip -d < "$ENPHASEGZFILENAME" > "$ETMP"

# Do the join!
join -t, -j1 "$ETMP" "$FTMP" > "$JTMP"

# Save the join result.
TEMPNAME="$JOINGZFILENAME.$$.tmp"
gzip -9 < "$JTMP" > "$TEMPNAME"
if [ -s "$TEMPNAME" ]; then
    if [ -f "$JOINGZFILENAME" ]; then chmod u+w "$JOINGZFILENAME"; fi
    chmod a+r "$TEMPNAME"
    mv -f "$TEMPNAME" "$JOINGZFILENAME"
    chmod a+r,go-w "$JOINGZFILENAME"
    echo "INFO: created/updated $JOINGZFILENAME" 1>&2
else
    echo "INFO: unable to create $JOINGZFILENAME" 1>&2
    exit 1
fi

HINJDATA="$(awk -F, < "$JTMP" '{print $1}' | sort -u | wc -l)"
DATADAYS="$(echo "$HINJDATA" | awk '{printf("%.0f", $1/24)}')"
echo "INFO: unique hours in joined data: $HINJDATA ($DATADAYS days)" 1>&2
# Multiplier to allow for hours missing from joined data.
ADJFACTOR=1
if [ "" = "$MONTH" ]; then
    ADJFACTOR="$(echo "$HINJDATA $HINTHISY" | awk '{print $2 / $1}')"
    echo "INFO: scale factor to allow for missing data: $ADJFACTOR" 1>&2
fi

RAWCO2="$(awk -F, <"$JTMP" '{sum += $2*$3} END {printf("%.0f\n", sum/1000000)}')" 1>&2
echo "INFO: raw to-date 16WW electricity CO2 kg for $YEARMONTH: $RAWCO2" 1>&2
ADJCO2="$(echo "$RAWCO2 $ADJFACTOR" | awk '{printf("%.0f\n", $1 * $2)}')"
echo "INFO: adjusted 16WW electricity CO2 kg for $YEARMONTH: $ADJCO2" 1>&2

# Save results of footprint computation.
#     year days raw-CO2-to-date full-year-CO2-estimate
echo "$YEARMONTH $DATADAYS $RAWCO2 $ADJCO2" > "$WWCFFILENAME.$$"
mv -f "$WWCFFILENAME.$$" "$WWCFFILENAME"
chmod a+r "$WWCFFILENAME"
echo "INFO: created $WWCFFILENAME" 1>&2

# Clear up.
rm -f "$FTMP" "$ETMP" "$JTMP"

exit 0



##########
# May be used / adapted / etc without any promise of fitness for purpose
# under the terms of the Apache License Version 2.0, January 2004
#     http://www.apache.org/licenses/LICENSE-2.0
##########
