"""Where the 2023 daily intensity peak and trough fall in UTC, month by month, and whether raw records carry forecasts."""
import sys, json, gzip, datetime as dt
from pathlib import Path
from collections import defaultdict
root = Path(sys.argv[1])
sums = defaultdict(lambda: [0.0] * 48); cnt = defaultdict(lambda: [0] * 48)
has_fc = total = 0
for src in json.loads((root / 'data/SOURCES.json').read_text()):
    for rec in json.loads(gzip.decompress((root / src['path']).read_bytes()))['data']:
        t = dt.datetime.strptime(rec['from'], '%Y-%m-%dT%H:%MZ'); a = rec['intensity'].get('actual')
        total += 1; has_fc += rec['intensity'].get('forecast') is not None
        if t.year == 2023 and a is not None:
            s = t.hour * 2 + t.minute // 30; sums[t.month][s] += a; cnt[t.month][s] += 1
fmt = lambda s: f'{s // 2:02d}:{30 * (s % 2):02d}'
for m in range(1, 13):
    p = [sums[m][i] / cnt[m][i] for i in range(48)]
    ev = max(range(28, 48), key=p.__getitem__)
    print(m, 'evening peak UTC', fmt(ev), 'min UTC', fmt(min(range(48), key=p.__getitem__)))
print('records with a forecast value:', has_fc, 'of', total)
