#!/bin/sh
# Compute a linear regression of heat-pump kWh vs HDD.
# Usage:
#     $0 [-raw] [-adj sqhdd] YYYY MM [eddidatafile [HDD12datafile]]
#
#         -raw dump the raw HDD and kWh as line records to stdout
#         -adj XXXX EXPERIMENTAL adjust from linear/linear regression
#
# Output (on stdout) is:
#     nDays kWh/HDD baseline Rsquared
#
# Optionally the Eddi and HDD data files (uncompressed) can be specified.
#
# Eg:
#% sh script/linearRegHPH4.sh 2025 02 
#22 0.51 3.47 0.56

# Uses Eddi data for hph4 (prefers precomputed, can try live).
# Prefers DegreeDays.Net data for HDD, else can fall back to other sources.

RAW=false
if [ "-raw" = "$1" ]; then
    RAW=true
    shift
fi

# Pick up adjustment if not linear in both quantities.
adj=lin
if [ "-adj" = "$1" ]; then
    adj="$2";
    shift
    shift
fi

# Data period
#YEAR=2024
#MONTH=12
if [ "$#" -lt 2 ]; then
    echo "ERROR: missing YYYY MM" 1>&2
    exit 1
fi
YEAR="$1"
MONTH="$2"
shift 2

EDDIDATADIR=data/eddi/log
EDDIFILE="${EDDIDATADIR}/${YEAR}${MONTH}.daily.csv"
if [ "$#" -ge 1 ]; then
    EDDIFILE="$1"
    shift
fi

TEMPPREFIX=out/tmp/tmp.hph4vsHDD12.$$.
HPH4TMP="${TEMPPREFIX}hph4.tmp"
HDDTMP="${TEMPPREFIX}hdd.tmp"

# Try to avoid collation funnies...
export LC_ALL=C

# DegreeDays.Net HDD12 data path in data/HDD/EGLL_HDD_12.0C-YYYY.csv
# BEWARE CRLF LINE TERMINATION!
#Description:,"Celsius-based heating degree days with a base temperature of 12 C"
#
#Source:,"www.degreedays.net"
#Accuracy:,"No problems detected"
#Station:,"London, GB (0.45W,51.48N)"
#Station ID:,EGLL
#
#Date,HDD 12
#2024-01-01,3.5
#2024-01-02,0.7
#2024-01-03,2.2
#...
DDNDATADIR=data/HDD/
DDNFILE="${DDNDATADIR}/EGLL_HDD_12.0C-${YEAR}.csv"
if [ ! -s "$DDNFILE" ]; then
    echo "ERROR: missing DDN daily data $DDNFILE" 1>&2
    exit 1
fi
awk < "$DDNFILE" -F, -v adj="$adj" '/^'${YEAR}-${MONTH}'/ {gsub("\r","",$0); print $1, (adj=="sqhdd"?($2*$2):$2)}' | sort -b > "${HDDTMP}"


# Eddi pre-extracted data can be found under data/eddi/log/YYYYMM.daily.csv
# Extract col 1 (date) and col 9 (hph4).
# Exclude days with non-positive hph4 as every active day should be +ve.
##Eddi daily stats summary in kWh; partial if h lt 24
##UTCISOdate,h,h1d,h1b,imp,exp,h2d,h2b,hph4
#2024-12-01,24,0,2.939,9.437,0.002,0,0,2.728
#2024-12-02,24,0,0,9.527,0.018,0,0,4.006
#2024-12-03,24,0,0,11.385,0.02,0,0,6.044
#...
#2024-12-30,24,0,3.014,12.099,0.016,0,0,4.53
#2024-12-31,24,0,0,8.979,0.011,0,0,4.065
##2024-12,744,0,14.527,338.637,0.224,0,0,164.055
EDDISRC="cat $EDDIFILE"
if [ ! -s "$EDDIFILE" ]; then
    echo "WARNING: missing Eddi daily data $EDDIFILE, trying live" 1>&2
    EDDISRC="sh script/myenergi/eddiDaySummary.sh -m ${YEAR}${MONTH}"
fi
${EDDISRC} | awk -F, '/^'${YEAR}-${MONTH}'/ && $9>0 {print $1, $9}' | sort -b > "${HPH4TMP}"

# Join as HDD x kWh by date (and discard the date).
# Dump raw or compute linear regression.
#head -2 "${HDDTMP}" "${HPH4TMP}"
join -o 1.2,2.2 "${HDDTMP}" "${HPH4TMP}" | \
    if "$RAW"; then
	echo "#HDD kWh"
        cat
    else
        sh script/linearReg.sh
    fi


# Clean up.
rm -f "${HPH4TMP}" "${HDDTMP}"
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
##########
