GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Electricity markets and data pipelines / globalgrid2050 / scripts

GlobalGrid2050 Backfill Generation Aggregates Year V6 and 1 more file

A Python command-line tool in the scripts folder that reads generation monthly mwh by technology, generation annual mwh by technology and generation seasonal mwh by technology.

566 lines of Python from globalgrid2050, in 2 files. Topic: Electricity markets and data pipelines. Section 864 of 2044.

Cut 2026-12-06T03:07:51+00:00. A copy for reading; the repository at the stated commit is the source.

In the State lines below, domain stage, users and canonical status are rule-based first approximations from code and path evidence (rules explore-agent-rules-v1), not observed facts: the rules.

In this section

  1. GlobalGrid2050 Audit Generation Data Coverage V6 energy-data · globalgrid2050 / scripts/audit_generation_data_coverage_v6.py lines 1–209
  2. GlobalGrid2050 Backfill Generation Aggregates Year V6 energy-data · globalgrid2050 / scripts/backfill_generation_aggregates_year_v6.py lines 1–357

GlobalGrid2050 Audit Generation Data Coverage V6

A Python command-line tool in the scripts folder that reads generation coverage audit, generation history daily decade, generation recent halfhourly 30d and Elexon generation sources half hourly.

energy-dataglobalgrid2050 / scripts/audit_generation_data_coverage_v6.py · lines 1–209 of 209 · Entry point · pattern file_info (shared by 2 files)

What it does
It loads generation coverage audit, generation history daily decade, generation recent halfhourly 30d and Elexon generation sources half hourly. Defines 6 functions or classes, including utc_now, file_info, csv_summary, monthly_archive_summary, json_rows_summary. Uses libraries csv, json, collections, datetime, pathlib. Side effects: reads or writes files.
Why it exists in the grid workflow
Domain stage upstream_foresight (path word 'generation'): upstream engineering foresight over GB transmission and distribution topology (TSO/DNO, 132kV and above, the REPD pipeline, data centres), which Ventus Ltd states it models for foresight.
Inputs & outputs
In: JSON file generation_data_coverage_audit.json; JSON file generation_history_daily_decade.json; JSON file generation_recent_halfhourly_30d.json; CSV file elexon_generation_sources_half_hourly.csv.
Downstream impact
Used directly by 3 files: GlobalGrid2050 Audit Generation Data Coverage V6 Workflow (globalgrid2050); GlobalGrid2050 Backfill Generation Sources Overnight Sequence V6 Workflow (globalgrid2050); GlobalGrid2050 Backfill Generation Sources Staggered Years V6 Workflow (globalgrid2050).
What it achieves
It is built from the monthly archive summary helper (`monthly_archive_summary`) and the JSON rows summary helper (`json_rows_summary`). Defines `utc_now`, `file_info`, `csv_summary` and 2 more.
Why it matters
Run by GlobalGrid2050 Audit Generation Data Coverage V6 Workflow, GlobalGrid2050 Backfill Generation Sources Overnight Sequence V6 Workflow and GlobalGrid2050 Backfill Generation Sources Staggered Years V6 Workflow.
Application
globalgrid2050 · scripts · Python CLI
Uses
csv, json, collections, datetime, pathlib
Used by
run by GlobalGrid2050 Audit Generation Data Coverage V6 Workflow (workflow run), run by GlobalGrid2050 Backfill Generation Sources Overnight Sequence V6 Workflow (workflow run), run by GlobalGrid2050 Backfill Generation Sources Staggered Years V6 Workflow (workflow run)
Produces
utc_now, file_info, csv_summary, monthly_archive_summary, json_rows_summary, main
Defines here
utc_now, file_info, csv_summary, monthly_archive_summary, json_rows_summary, main
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 scripts/audit_generation_data_coverage_v6.py:1-209 · blob d38ed23265203fd651e90b55988329a8054f3f02 · GitHub at these lines
State
achieves: name · context: readme_derived · stage: output · effects: filesystem · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
import csv
import json
from collections import Counter
from datetime import datetime, timezone
from pathlib import Path

DATA_DIR = Path('data/generation')
ARCHIVE_DIR = DATA_DIR / 'archive'
OUT_DIR = Path('uk_energy_tracking_v6/generation_history')
REPORT_MD = OUT_DIR / 'GENERATION_DATA_COVERAGE_AUDIT.md'
REPORT_JSON = OUT_DIR / 'generation_data_coverage_audit.json'
DAILY_JSON = OUT_DIR / 'generation_history_daily_decade.json'
RECENT_JSON = OUT_DIR / 'generation_recent_halfhourly_30d.json'


def utc_now():
    return datetime.now(timezone.utc).isoformat().replace('+00:00', 'Z')


def file_info(path):
    return {
        'path': str(path),
        'exists': path.exists(),
        'sizeBytes': path.stat().st_size if path.exists() else 0,
    }


def csv_summary(path):
    info = file_info(path)
    info.update({
        'rows': 0,
        'minPeriodStartUTC': None,
        'maxPeriodStartUTC': None,
        'technologies': {},
        'sampleFirstDataRow': None,
    })
    if not path.exists() or path.stat().st_size == 0:
        return info
    try:
        with path.open('r', encoding='utf-8', newline='') as handle:
            reader = csv.DictReader(handle)
            counts = Counter()
            for row in reader:
                t = row.get('periodStartUTC') or row.get('priceTimeUTC') or ''
                fuel = row.get('fuelType') or row.get('technology') or 'UNKNOWN'
                if info['sampleFirstDataRow'] is None:
                    info['sampleFirstDataRow'] = {k: row.get(k, '') for k in list(row.keys())[:8]}
                if t:
                    if info['minPeriodStartUTC'] is None or t < info['minPeriodStartUTC']:
                        info['minPeriodStartUTC'] = t
                    if info['maxPeriodStartUTC'] is None or t > info['maxPeriodStartUTC']:
                        info['maxPeriodStartUTC'] = t
                counts[fuel] += 1
                info['rows'] += 1
            info['technologies'] = dict(sorted(counts.items()))
    except Exception as exc:
        info['error'] = str(exc)
    return info


def monthly_archive_summary(year):
    folder = ARCHIVE_DIR / str(year)
    files = sorted(folder.glob(f'elexon_generation_sources_{year}-[0-9][0-9].csv'))
    info = {
        'year': year,
        'folder': str(folder),
        'exists': bool(files),
        'months': len(files),
        'sizeBytes': 0,
        'rows': 0,
        'minPeriodStartUTC': None,
        'maxPeriodStartUTC': None,
        'technologies': {},
        'files': [],
    }
    total_counts = Counter()
    for path in files:
        item = csv_summary(path)
        info['files'].append(item)
        info['sizeBytes'] += item['sizeBytes']
        info['rows'] += item['rows']
        for tech, count in item.get('technologies', {}).items():
            total_counts[tech] += count
        mn = item.get('minPeriodStartUTC')
        mx = item.get('maxPeriodStartUTC')
        if mn and (info['minPeriodStartUTC'] is None or mn < info['minPeriodStartUTC']):
            info['minPeriodStartUTC'] = mn
        if mx and (info['maxPeriodStartUTC'] is None or mx > info['maxPeriodStartUTC']):
            info['maxPeriodStartUTC'] = mx
    info['technologies'] = dict(sorted(total_counts.items()))
    return info


def json_rows_summary(path, date_key):
    info = file_info(path)
    info.update({
        'rows': 0,
        'minDate': None,
        'maxDate': None,
        'technologies': {},
        'status': None,
    })
    if not path.exists() or path.stat().st_size == 0:
        return info
    try:
        payload = json.loads(path.read_text(encoding='utf-8'))
        rows = payload.get('rows', []) if isinstance(payload, dict) else []
        info['status'] = payload.get('status') if isinstance(payload, dict) else None
        counts = Counter()
        for row in rows:
            d = row.get(date_key) or row.get('date') or row.get('time') or ''
            tech = row.get('technology') or row.get('fuelType') or 'UNKNOWN'
            if d:
                if info['minDate'] is None or d < info['minDate']:
                    info['minDate'] = d
                if info['maxDate'] is None or d > info['maxDate']:
                    info['maxDate'] = d
            counts[tech] += 1
        info['rows'] = len(rows)
        info['technologies'] = dict(sorted(counts.items()))
    except Exception as exc:
        info['error'] = str(exc)
    return info


def main():
    OUT_DIR.mkdir(parents=True, exist_ok=True)
    years = list(range(2016, 2027))
    annual = {str(year): csv_summary(DATA_DIR / f'elexon_generation_sources_{year}.csv') for year in years}
    monthly = {str(year): monthly_archive_summary(year) for year in years}
    master = csv_summary(DATA_DIR / 'elexon_generation_sources_half_hourly.csv')
    daily = json_rows_summary(DAILY_JSON, 'date')
    recent = json_rows_summary(RECENT_JSON, 'time')

    audit = {
        'generatedUTC': utc_now(),
        'annualFiles': annual,
        'monthlyArchiveFiles': monthly,
        'masterHalfHourly': master,
        'dailyDecadeJson': daily,
        'recentHalfHourlyJson': recent,
    }
    REPORT_JSON.write_text(json.dumps(audit, indent=2), encoding='utf-8')

    lines = []
    lines.append('# Generation Data Coverage Audit V6')
    lines.append('')
    lines.append(f'Generated UTC: {audit["generatedUTC"]}')
    lines.append('')
    lines.append('## Monthly archive files')
    lines.append('')
    lines.append('| Year | Exists | Months | Size MB | Rows | Min UTC | Max UTC | Technology count |')
    lines.append('| --- | --- | ---: | ---: | ---: | --- | --- | ---: |')
    for year in years:
        item = monthly[str(year)]
        lines.append('| {year} | {exists} | {months} | {size:.2f} | {rows} | {mn} | {mx} | {tc} |'.format(
            year=year,
            exists='yes' if item['exists'] else 'no',
            months=item['months'],
            size=item['sizeBytes'] / 1024 / 1024,
            rows=item['rows'],
            mn=item['minPeriodStartUTC'] or '',
            mx=item['maxPeriodStartUTC'] or '',
            tc=len(item['technologies']),
        ))
    lines.append('')
    lines.append('## Legacy annual CSV files')
    lines.append('')
    lines.append('| Year | Exists | Size MB | Rows | Min UTC | Max UTC | Technology count |')
    lines.append('| --- | --- | ---: | ---: | --- | --- | ---: |')
    for year in years:
        item = annual[str(year)]
        lines.append('| {year} | {exists} | {size:.2f} | {rows} | {mn} | {mx} | {tc} |'.format(
            year=year,
            exists='yes' if item['exists'] else 'no',
            size=item['sizeBytes'] / 1024 / 1024,
            rows=item['rows'],
            mn=item['minPeriodStartUTC'] or '',
            mx=item['maxPeriodStartUTC'] or '',
            tc=len(item['technologies']),
        ))
    lines.append('')
    lines.append('## Browser files')
    lines.append('')
    lines.append('| File | Size MB | Rows | Min | Max | Status |')
    lines.append('| --- | ---: | ---: | --- | --- | --- |')
    lines.append('| master half hourly csv | {:.2f} | {} | {} | {} | {} |'.format(master['sizeBytes']/1024/1024, master['rows'], master['minPeriodStartUTC'] or '', master['maxPeriodStartUTC'] or '', master.get('error','')))
    lines.append('| daily decade json | {:.2f} | {} | {} | {} | {} |'.format(daily['sizeBytes']/1024/1024, daily['rows'], daily['minDate'] or '', daily['maxDate'] or '', daily.get('status') or daily.get('error','')))
    lines.append('| recent half hourly json | {:.2f} | {} | {} | {} | {} |'.format(recent['sizeBytes']/1024/1024, recent['rows'], recent['minDate'] or '', recent['maxDate'] or '', recent.get('status') or recent.get('error','')))
    lines.append('')
    lines.append('## Interpretation')
    lines.append('')
    missing = [str(y) for y in years if monthly[str(y)]['rows'] == 0 and annual[str(y)]['rows'] == 0]
    if missing:
        lines.append('Missing or empty years: ' + ', '.join(missing))
    else:
        lines.append('All years from 2016 to 2026 contain generation rows in monthly archive or legacy annual files.')
    if recent['rows'] == 0:
        lines.append('Recent half hourly slice is empty. Short windows will show no data until this file is populated.')
    if daily['rows'] == 0:
        lines.append('Daily aggregate is empty. Long windows will show no data.')
    lines.append('')
    REPORT_MD.write_text('\n'.join(lines) + '\n', encoding='utf-8')
    print(f'Wrote {REPORT_MD}')
    print(f'Wrote {REPORT_JSON}')


if __name__ == '__main__':
    main()

GlobalGrid2050 Backfill Generation Aggregates Year V6

A Python command-line tool in the scripts folder that reads generation monthly mwh by technology, generation annual mwh by technology and generation seasonal mwh by technology.

energy-dataglobalgrid2050 / scripts/backfill_generation_aggregates_year_v6.py · lines 1–357 of 357 · Entry point · pattern load_json_rows (shared by 2 files)

What it does
It loads generation monthly mwh by technology, generation annual mwh by technology, generation seasonal mwh by technology and generation day night mwh by technology. Defines 19 functions or classes, including group_for, parse_time, parse_mw, season_for_month, day_night_bucket. Uses libraries json, os, sys, datetime, collections. Side effects: reads or writes files.
Why it exists in the grid workflow
Domain stage upstream_foresight (path word 'generation'): upstream engineering foresight over GB transmission and distribution topology (TSO/DNO, 132kV and above, the REPD pipeline, data centres), which Ventus Ltd states it models for foresight.
Inputs & outputs
In: JSON file generation_monthly_mwh_by_technology.json; JSON file generation_annual_mwh_by_technology.json; JSON file generation_seasonal_mwh_by_technology.json; JSON file generation_day_night_mwh_by_technology.json; command-line arguments.
Downstream impact
Used directly by 5 files: GlobalGrid2050 Gridbot Generation MWh Interconnector UI Guard (globalgrid2050); GlobalGrid2050 Gridbot Generation MWh Active V6 Source Audit (globalgrid2050); GlobalGrid2050 Backfill Generation Aggregates Manual V6 Workflow (globalgrid2050); GlobalGrid2050 Backfill Generation Aggregates Sequence V6 Workflow (globalgrid2050); and 1 more. Reached from 2 other entry points, including GlobalGrid2050 Gridbot Generation MWh Active V6 Source Audit Workflow (globalgrid2050); GlobalGrid2050 Gridbot Generation MWh Interconnector UI Guard Workflow (globalgrid2050).
What it achieves
It loads JSON rows (`load_json_rows`) and is built from the season for month helper (`season_for_month`) and the day night bucket helper (`day_night_bucket`). Defines `group_for`, `parse_time`, `parse_mw` and 15 more.
Why it matters
Run by 5 workflows and scripts, including GlobalGrid2050 Gridbot Generation MWh Interconnector UI Guard, GlobalGrid2050 Gridbot Generation MWh Active V6 Source Audit and GlobalGrid2050 Backfill Generation Aggregates Manual V6 Workflow.
Application
globalgrid2050 · scripts · Python CLI
Uses
GlobalGrid2050 Backfill Generation Sources Year V6 (import), json, os, sys, datetime, collections, pathlib
Used by
run by GlobalGrid2050 Gridbot Generation MWh Interconnector UI Guard (script call), run by GlobalGrid2050 Gridbot Generation MWh Active V6 Source Audit (script call), run by GlobalGrid2050 Backfill Generation Aggregates Manual V6 Workflow (workflow run), run by GlobalGrid2050 Backfill Generation Aggregates Sequence V6 Workflow (workflow run), run by GlobalGrid2050 Update Generation Aggregates Ytd V6 Workflow (workflow run)
Produces
group_for, parse_time, parse_mw, season_for_month, day_night_bucket, load_json_rows, write_json, safe_float, value_score, record_count, completeness, weak_row
Defines here
group_for, parse_time, parse_mw, season_for_month, day_night_bucket, load_json_rows, write_json, safe_float, value_score, record_count, completeness, weak_row, should_replace_existing, merge_rows, add_stats, finalise_stat_row, estimate_interval_hours, aggregate_day, main
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 scripts/backfill_generation_aggregates_year_v6.py:1-357 · blob 0d963506f39ccdac237e7f38a0dea61df81eb810 · GitHub at these lines
State
achieves: name · context: readme_derived · stage: output · effects: filesystem · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
import json
import os
import sys
import datetime as dt
from collections import defaultdict
from pathlib import Path

from backfill_generation_sources_year_v6 import fetch_elexon_day, fetch_pvlive_day, utc_now

OUT_DIR = Path('uk_energy_tracking_v6/generation_history')
REPORT_DIR = OUT_DIR / 'backfill_reports'
MONTHLY_JSON = OUT_DIR / 'generation_monthly_mwh_by_technology.json'
ANNUAL_JSON = OUT_DIR / 'generation_annual_mwh_by_technology.json'
SEASONAL_JSON = OUT_DIR / 'generation_seasonal_mwh_by_technology.json'
DAY_NIGHT_JSON = OUT_DIR / 'generation_day_night_mwh_by_technology.json'

GROUPS = {
    'Solar': ['SOLAR', 'PV'],
    'Wind': ['WIND'],
    'Hydro': ['NPSHYD', 'HYDRO'],
    'Gas': ['CCGT', 'OCGT'],
    'Coal': ['COAL'],
    'Biomass': ['BIOMASS'],
    'Nuclear': ['NUCLEAR'],
    'Pumped Storage': ['PS'],
    'Imports & Exports': ['INT'],
}
ORDER = ['Solar', 'Wind', 'Hydro', 'Gas', 'Coal', 'Biomass', 'Nuclear', 'Pumped Storage', 'Imports & Exports', 'Other']


def group_for(fuel):
    f = str(fuel or '').upper()
    for label, prefixes in GROUPS.items():
        if any(f.startswith(prefix) for prefix in prefixes):
            return label
    return 'Other'


def parse_time(value):
    try:
        return dt.datetime.fromisoformat(str(value).replace('Z', '+00:00')).astimezone(dt.timezone.utc)
    except Exception:
        return None


def parse_mw(value):
    try:
        return float(value)
    except Exception:
        return None


def season_for_month(month):
    if month in (12, 1, 2):
        return 'Winter'
    if month in (3, 4, 5):
        return 'Spring'
    if month in (6, 7, 8):
        return 'Summer'
    return 'Autumn'


def day_night_bucket(t):
    return 'day' if 6 <= t.hour < 18 else 'night'


def load_json_rows(path):
    if not path.exists():
        return []
    try:
        payload = json.loads(path.read_text(encoding='utf-8'))
        rows = payload.get('rows', []) if isinstance(payload, dict) else []
        return rows if isinstance(rows, list) else []
    except Exception:
        return []


def write_json(path, rows, description):
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(json.dumps({
        'generatedUTC': utc_now(),
        'source': 'Elexon BMRS FUELINST and Sheffield Solar PVLive where available',
        'description': description,
        'unit': 'MWh',
        'rows': rows,
    }, indent=2), encoding='utf-8')


def safe_float(value):
    try:
        if value in (None, '', 'NaN'):
            return None
        return float(value)
    except Exception:
        return None


def value_score(row):
    fields = ['totalMWh', 'dayMWh', 'nightMWh', 'averageMW', 'highMW', 'lowMW']
    score = 0.0
    for field in fields:
        value = safe_float(row.get(field))
        if value is not None:
            score += abs(value)
    return score


def record_count(row):
    value = safe_float(row.get('records', row.get('periodCount', 0)))
    return int(value) if value is not None and value > 0 else 0


def completeness(row):
    value = safe_float(row.get('completeness'))
    if value is not None:
        return value
    # Existing v6 rows do not yet carry completeness. Until schema v2 lands,
    # records are the best available quality proxy.
    return float(record_count(row))


def weak_row(row):
    if not isinstance(row, dict):
        return True
    return record_count(row) <= 0 and value_score(row) <= 0


def should_replace_existing(existing, incoming):
    if existing is None:
        return not weak_row(incoming)
    if weak_row(incoming) and not weak_row(existing):
        return False
    existing_quality = completeness(existing)
    incoming_quality = completeness(incoming)
    if existing_quality > 0 and incoming_quality < existing_quality:
        return False
    return True


def merge_rows(path, new_rows, key_fields, description):
    existing = load_json_rows(path)
    merged = {}
    preserved = 0
    blocked = []

    for row in existing:
        key = tuple(row.get(field) for field in key_fields)
        merged[key] = row

    for row in new_rows:
        key = tuple(row.get(field) for field in key_fields)
        old = merged.get(key)
        if should_replace_existing(old, row):
            merged[key] = row
        else:
            preserved += 1
            blocked.append(key)

    if blocked:
        print(f'Preserved {preserved} existing rows in {path}; incoming rows were weaker or incomplete.')
        for key in blocked[:20]:
            print(f'Preserved existing row for key: {key}')
        if os.getenv('FAIL_ON_WEAK_OVERWRITE', 'false').lower() in ('1', 'true', 'yes'):
            raise ValueError(f'Blocked weak overwrite for {preserved} rows in {path}')

    rows = [merged[key] for key in sorted(merged)]
    write_json(path, rows, description)
    return len(rows)


def add_stats(bucket, mw, mwh):
    bucket['totalMWh'] += mwh
    bucket['mwSum'] += mw
    bucket['records'] += 1
    bucket['highMW'] = mw if bucket['highMW'] is None else max(bucket['highMW'], mw)
    bucket['lowMW'] = mw if bucket['lowMW'] is None else min(bucket['lowMW'], mw)


def finalise_stat_row(base, bucket):
    records = bucket['records'] or 0
    return {
        **base,
        'totalMWh': round(bucket['totalMWh'], 3),
        'averageMW': round(bucket['mwSum'] / records, 3) if records else 0,
        'highMW': round(bucket['highMW'], 3) if bucket['highMW'] is not None else 0,
        'lowMW': round(bucket['lowMW'], 3) if bucket['lowMW'] is not None else 0,
        'records': records,
    }


def estimate_interval_hours(items, index):
    t = items[index][0]
    if index + 1 < len(items):
        nxt = items[index + 1][0]
        delta = (nxt - t).total_seconds() / 3600
        if 0 < delta <= 1:
            return delta
    if index > 0:
        prv = items[index - 1][0]
        delta = (t - prv).total_seconds() / 3600
        if 0 < delta <= 1:
            return delta
    return 0.5


def aggregate_day(rows):
    deduped = {}
    for row in rows:
        t = parse_time(row.get('periodStartUTC'))
        mw = parse_mw(row.get('generationMW'))
        fuel = row.get('fuelType', '')
        if not t or mw is None or not fuel:
            continue
        key = (t.isoformat(), fuel)
        deduped[key] = {'time': t, 'fuelType': fuel, 'technology': group_for(fuel), 'mw': mw}

    by_technology = defaultdict(list)
    for item in deduped.values():
        by_technology[item['technology']].append((item['time'], item['mw'], item['fuelType']))

    output = []
    for technology, items in by_technology.items():
        items.sort(key=lambda x: (x[0], x[2]))
        for i, (t, mw, fuel) in enumerate(items):
            hours = estimate_interval_hours(items, i)
            output.append({'time': t, 'technology': technology, 'fuelType': fuel, 'mw': mw, 'mwh': mw * hours, 'bucket': day_night_bucket(t)})
    return output, len(deduped)


def main():
    year = int(os.getenv('YEAR') or (sys.argv[1] if len(sys.argv) > 1 else dt.datetime.now(dt.timezone.utc).year))
    include_solar = os.getenv('INCLUDE_SOLAR', 'true').lower() not in ('0', 'false', 'no')
    current_year = dt.datetime.now(dt.timezone.utc).year
    current_date = dt.datetime.now(dt.timezone.utc).date()
    start_day = dt.date(year, 1, 1)
    end_day = dt.date(year, 12, 31)
    if year == current_year:
        end_day = min(end_day, current_date - dt.timedelta(days=1))
    if end_day < start_day:
        print(f'No complete days available for {year}')
        return

    monthly = defaultdict(lambda: {'totalMWh': 0.0, 'mwSum': 0.0, 'records': 0, 'highMW': None, 'lowMW': None})
    annual = defaultdict(lambda: {'totalMWh': 0.0, 'mwSum': 0.0, 'records': 0, 'highMW': None, 'lowMW': None})
    seasonal = defaultdict(lambda: {'totalMWh': 0.0, 'mwSum': 0.0, 'records': 0, 'highMW': None, 'lowMW': None})
    daynight = defaultdict(lambda: {'dayMWh': 0.0, 'nightMWh': 0.0, 'records': 0})

    raw_elexon = 0
    raw_solar = 0
    deduped_rows = 0
    failed_days = []
    solar_status = 'not requested'
    solar_url = ''

    day = start_day
    while day <= end_day:
        rows = []
        try:
            elexon = fetch_elexon_day(day)
            raw_elexon += len(elexon)
            rows.extend(elexon)
            print(f'{year} {day}: Elexon {len(elexon)} rows')
        except Exception as exc:
            failed_days.append(f'{day} Elexon {exc}')
            print(f'Warning {year} {day}: Elexon failed: {exc}')
        if include_solar:
            try:
                solar, status, url = fetch_pvlive_day(day)
                raw_solar += len(solar)
                rows.extend(solar)
                if url and not solar_url:
                    solar_url = url
                if solar:
                    solar_status = 'ok'
                elif solar_status != 'ok':
                    solar_status = status
                print(f'{year} {day}: PVLive solar {len(solar)} rows')
            except Exception as exc:
                failed_days.append(f'{day} PVLive {exc}')
                if solar_status != 'ok':
                    solar_status = str(exc)
        items, deduped = aggregate_day(rows)
        deduped_rows += deduped
        for item in items:
            t = item['time']
            tech = item['technology']
            month = t.month
            season = season_for_month(month)
            add_stats(monthly[(year, month, tech)], item['mw'], item['mwh'])
            add_stats(annual[(year, tech)], item['mw'], item['mwh'])
            add_stats(seasonal[(year, season, tech)], item['mw'], item['mwh'])
            dn = daynight[(year, month, season, tech)]
            if item['bucket'] == 'day':
                dn['dayMWh'] += item['mwh']
            else:
                dn['nightMWh'] += item['mwh']
            dn['records'] += 1
        day += dt.timedelta(days=1)

    monthly_rows = [finalise_stat_row({'year': y, 'month': m, 'season': season_for_month(m), 'technology': tech}, bucket) for (y, m, tech), bucket in monthly.items()]
    annual_rows = [finalise_stat_row({'year': y, 'technology': tech}, bucket) for (y, tech), bucket in annual.items()]
    seasonal_rows = [finalise_stat_row({'year': y, 'season': season, 'technology': tech}, bucket) for (y, season, tech), bucket in seasonal.items()]
    daynight_rows = []
    for (y, m, season, tech), bucket in daynight.items():
        total = bucket['dayMWh'] + bucket['nightMWh']
        daynight_rows.append({
            'year': y,
            'month': m,
            'season': season,
            'technology': tech,
            'dayMWh': round(bucket['dayMWh'], 3),
            'nightMWh': round(bucket['nightMWh'], 3),
            'totalMWh': round(total, 3),
            'daySharePercent': round((bucket['dayMWh'] / total) * 100, 3) if total else 0,
            'nightSharePercent': round((bucket['nightMWh'] / total) * 100, 3) if total else 0,
            'records': bucket['records'],
        })

    total_monthly = merge_rows(MONTHLY_JSON, monthly_rows, ['year', 'month', 'technology'], 'Monthly MWh by generation technology, with MW statistics')
    total_annual = merge_rows(ANNUAL_JSON, annual_rows, ['year', 'technology'], 'Annual MWh by generation technology')
    total_seasonal = merge_rows(SEASONAL_JSON, seasonal_rows, ['year', 'season', 'technology'], 'Seasonal MWh by generation technology')
    total_daynight = merge_rows(DAY_NIGHT_JSON, daynight_rows, ['year', 'month', 'technology'], 'Monthly day versus night MWh by generation technology')

    REPORT_DIR.mkdir(parents=True, exist_ok=True)
    report = REPORT_DIR / f'GENERATION_AGGREGATE_BACKFILL_{year}.md'
    report.write_text('\n'.join([
        f'# Generation Aggregate Backfill {year}',
        '',
        f'Updated UTC: {utc_now()}',
        f'Year: {year}',
        f'Window: {start_day} to {end_day}',
        f'Elexon raw rows fetched: {raw_elexon}',
        f'PVLive raw solar rows fetched: {raw_solar}',
        f'Deduped timestamp plus fuel rows processed: {deduped_rows}',
        f'Monthly aggregate rows for this year: {len(monthly_rows)}',
        f'Annual aggregate rows for this year: {len(annual_rows)}',
        f'Seasonal aggregate rows for this year: {len(seasonal_rows)}',
        f'Day night aggregate rows for this year: {len(daynight_rows)}',
        f'Total monthly output rows after merge: {total_monthly}',
        f'Total annual output rows after merge: {total_annual}',
        f'Total seasonal output rows after merge: {total_seasonal}',
        f'Total day night output rows after merge: {total_daynight}',
        f'PVLive status: {solar_status}',
        f'PVLive working URL sample: {solar_url or "not confirmed"}',
        f'Failed days: {len(failed_days)}',
        'Day definition: 06:00 to 18:00 UTC. Night definition: 18:00 to 06:00 UTC.',
        'MWh method: MW multiplied by observed interval hours, inferred from adjacent timestamps and capped at 1 hour.',
        'Raw API rows are not committed by this aggregate workflow.',
        '',
        '## Failed day details',
        *failed_days[:200],
    ]) + '\n', encoding='utf-8')
    print(f'Wrote aggregate report {report}')


if __name__ == '__main__':
    main()