GLOBALGRID2050 / EXPLORE CODE

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

GlobalGrid2050 Backfill Generation Fuelhh Halfhourly All Months V6 and 1 more file

A Python command-line tool in the scripts folder that fetches backfill progress, fuelhh backfill, solar and wind, using SHA-256 from Elexon Balancing Mechanism Reporting Service (BMRS).

543 lines of Python from globalgrid2050, in 2 files. Topic: Electricity markets and data pipelines. Section 865 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 Backfill Generation Clean Halfhourly Month V6 energy-data · globalgrid2050 / scripts/backfill_generation_clean_halfhourly_month_v6.py lines 1–184
  2. GlobalGrid2050 Backfill Generation Fuelhh Halfhourly All Months V6 energy-data · globalgrid2050 / scripts/backfill_generation_fuelhh_halfhourly_all_months_v6.py lines 1–359

GlobalGrid2050 Backfill Generation Clean Halfhourly Month V6

A Python command-line tool in the scripts folder that parses time (`parse_time`) and MW (`parse_mw`) (units: MW), covering solar and wind.

energy-dataglobalgrid2050 / scripts/backfill_generation_clean_halfhourly_month_v6.py · lines 1–184 of 184 · Entry point · pattern month_window (shared by 2 files)

What it does
Defines 6 functions or classes, including group_for, parse_time, parse_mw, month_window, compact_time. Uses libraries csv, json, os, sys, datetime. 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. Domain terms in the code: solar, wind.
Inputs & outputs
In: command-line arguments.
Downstream impact
Used directly by 1 file: GlobalGrid2050 Backfill Generation Clean Halfhourly Month V6 Workflow (globalgrid2050).
What it achieves
It is built from the month window helper (`month_window`) and the compact time helper (`compact_time`). Defines `group_for`, `parse_time`, `parse_mw` and 2 more.
Why it matters
Run by GlobalGrid2050 Backfill Generation Clean Halfhourly Month V6 Workflow.
Application
globalgrid2050 · scripts · Python CLI
Uses
GlobalGrid2050 Backfill Generation Sources Year V6 (import), csv, json, os, sys, datetime, collections, pathlib
Used by
run by GlobalGrid2050 Backfill Generation Clean Halfhourly Month V6 Workflow (workflow run)
Produces
group_for, parse_time, parse_mw, month_window, compact_time, main
Defines here
group_for, parse_time, parse_mw, month_window, compact_time, 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_clean_halfhourly_month_v6.py:1-184 · blob 0828ada248e83e3dee16f9a241e2e4fa162be335 · 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
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_ROOT = Path('data/generation/halfhourly_clean')
REPORT_DIR = Path('uk_energy_tracking_v6/generation_history/backfill_reports')
FIELDS = ['time', 'technology', 'generationMW', 'source']

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


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 month_window(year, month):
    start = dt.date(year, month, 1)
    if month == 12:
        end = dt.date(year, 12, 31)
    else:
        end = dt.date(year, month + 1, 1) - dt.timedelta(days=1)
    current_date = dt.datetime.now(dt.timezone.utc).date()
    if year == current_date.year and month == current_date.month:
        end = min(end, current_date - dt.timedelta(days=1))
    return start, end


def compact_time(t):
    return t.isoformat().replace('+00:00', 'Z')


def main():
    year = int(os.getenv('YEAR') or (sys.argv[1] if len(sys.argv) > 1 else dt.datetime.now(dt.timezone.utc).year))
    month = int(os.getenv('MONTH') or (sys.argv[2] if len(sys.argv) > 2 else dt.datetime.now(dt.timezone.utc).month))
    include_solar = os.getenv('INCLUDE_SOLAR', 'true').lower() not in ('0', 'false', 'no')
    if month < 1 or month > 12:
        raise SystemExit('MONTH must be 1 to 12')

    start_day, end_day = month_window(year, month)
    if end_day < start_day:
        raise SystemExit(f'No complete days available for {year}-{month:02d}')

    raw_elexon = 0
    raw_solar = 0
    raw_deduped = {}
    failed = []
    solar_status = 'not requested'
    solar_url = ''

    day = start_day
    while day <= end_day:
        try:
            rows = fetch_elexon_day(day)
            raw_elexon += len(rows)
            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 = (compact_time(t), str(fuel).upper())
                raw_deduped[key] = {'time': key[0], 'fuelType': key[1], 'technology': group_for(fuel), 'generationMW': mw, 'source': 'Elexon BMRS FUELINST'}
            print(f'{day}: Elexon raw rows {len(rows)}')
        except Exception as exc:
            failed.append(f'{day} Elexon {exc}')
            print(f'Warning {day}: Elexon failed: {exc}')

        if include_solar:
            try:
                rows, status, url = fetch_pvlive_day(day)
                raw_solar += len(rows)
                if url and not solar_url:
                    solar_url = url
                if rows:
                    solar_status = 'ok'
                elif solar_status != 'ok':
                    solar_status = status
                for row in rows:
                    t = parse_time(row.get('periodStartUTC'))
                    mw = parse_mw(row.get('generationMW'))
                    if not t or mw is None:
                        continue
                    key = (compact_time(t), 'SOLAR')
                    raw_deduped[key] = {'time': key[0], 'fuelType': 'SOLAR', 'technology': 'Solar', 'generationMW': mw, 'source': 'Sheffield Solar PVLive'}
                print(f'{day}: PVLive solar raw rows {len(rows)}')
            except Exception as exc:
                failed.append(f'{day} PVLive {exc}')
                if solar_status != 'ok':
                    solar_status = str(exc)
                print(f'Warning {day}: PVLive failed: {exc}')
        day += dt.timedelta(days=1)

    by_time_technology = defaultdict(float)
    sources = defaultdict(set)
    for row in raw_deduped.values():
        key = (row['time'], row['technology'])
        by_time_technology[key] += row['generationMW']
        sources[key].add(row['source'])

    out_rows = []
    for key in sorted(by_time_technology, key=lambda item: (item[0], item[1])):
        out_rows.append({
            'time': key[0],
            'technology': key[1],
            'generationMW': f'{by_time_technology[key]:.3f}',
            'source': '+'.join(sorted(sources[key])),
        })

    out_dir = OUT_ROOT / str(year)
    out_dir.mkdir(parents=True, exist_ok=True)
    out_csv = out_dir / f'generation_mw_{year}_{month:02d}.csv'
    with out_csv.open('w', encoding='utf-8', newline='') as handle:
        writer = csv.DictWriter(handle, fieldnames=FIELDS)
        writer.writeheader()
        writer.writerows(out_rows)

    report = REPORT_DIR / f'GENERATION_CLEAN_HALFHOUR_MONTH_{year}_{month:02d}.md'
    REPORT_DIR.mkdir(parents=True, exist_ok=True)
    size_mb = out_csv.stat().st_size / 1024 / 1024
    report.write_text('\n'.join([
        f'# Clean Half Hourly Generation MW {year}-{month:02d}',
        '',
        f'Updated UTC: {utc_now()}',
        f'Window: {start_day} to {end_day}',
        f'Elexon raw rows fetched: {raw_elexon}',
        f'PVLive raw solar rows fetched: {raw_solar}',
        f'Deduped raw timestamp plus fuel rows: {len(raw_deduped)}',
        f'Clean timestamp plus technology rows: {len(out_rows)}',
        f'Output CSV: {out_csv}',
        f'Output size MB: {size_mb:.2f}',
        f'PVLive status: {solar_status}',
        f'PVLive working URL sample: {solar_url or "not confirmed"}',
        f'Failed days: {len(failed)}',
        'Cleaning rule: dedupe by time plus raw fuel, then group raw fuels into technology and sum MW by time plus technology.',
        'Stored fields: time, technology, generationMW, source.',
        'Raw API rows are not committed.',
        '',
        '## Failed day details',
        *failed[:200],
    ]) + '\n', encoding='utf-8')

    if size_mb > 25:
        raise SystemExit(f'Clean monthly file is {size_mb:.2f} MB, above 25 MB target. Split this month weekly before committing.')

    print(f'Wrote {len(out_rows)} clean rows to {out_csv}')
    print(f'Wrote report {report}')


if __name__ == '__main__':
    main()

GlobalGrid2050 Backfill Generation Fuelhh Halfhourly All Months V6

A Python command-line tool in the scripts folder that fetches backfill progress, fuelhh backfill, solar and wind, using SHA-256 from Elexon Balancing Mechanism Reporting Service (BMRS).

energy-dataglobalgrid2050 / scripts/backfill_generation_fuelhh_halfhourly_all_months_v6.py · lines 1–359 of 359 · Entry point · pattern month_window (shared by 2 files)

What it does
It fetches backfill progress, fuelhh backfill, solar and wind. Defines 17 functions or classes, including utc_now, iso_z, pick, group_for, fetch_window. Uses libraries __future__, csv, datetime, hashlib, json. Side effects: reads or writes files; makes network requests.
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. Domain terms in the code: solar, wind.
Inputs & outputs
In: data source: Elexon BMRS; JSON file BACKFILL_PROGRESS.json; JSON file FUELHH_BACKFILL_LATEST.json.
Downstream impact
Used directly by 2 files: GlobalGrid2050 Gridbot Generation MWh Active V6 Source Audit (globalgrid2050); GlobalGrid2050 Backfill Generation Fuelhh Halfhourly V6 Workflow (globalgrid2050). Reached from 1 other entry point, including GlobalGrid2050 Gridbot Generation MWh Active V6 Source Audit Workflow (globalgrid2050).
What it achieves
It fetches window (`fetch_window`), cleans rows (`clean_rows`) and writes shard (`write_shard`). Backfill confirmed FUELHH half hourly generation as monthly shards.
Why it matters
Run by GlobalGrid2050 Gridbot Generation MWh Active V6 Source Audit and GlobalGrid2050 Backfill Generation Fuelhh Halfhourly V6 Workflow.
Application
globalgrid2050 · scripts · Python CLI
Uses
__future__, csv, datetime, hashlib, json, os, time, urllib.parse
Used by
run by GlobalGrid2050 Gridbot Generation MWh Active V6 Source Audit (script call), run by GlobalGrid2050 Backfill Generation Fuelhh Halfhourly V6 Workflow (workflow run)
Produces
utc_now, iso_z, pick, group_for, fetch_window, month_window, windows, clean_rows, shard_path, write_shard, row_hash, already_done
Defines here
utc_now, iso_z, pick, group_for, fetch_window, month_window, windows, clean_rows, shard_path, write_shard, row_hash, already_done, month_iter, build_month, write_progress, write_run_report, 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_fuelhh_halfhourly_all_months_v6.py:1-359 · blob 2e85f7453842389dca904d95131eb6cc1445a666 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: filesystem, network · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
#!/usr/bin/env python3
"""
Backfill confirmed FUELHH half hourly generation as monthly shards.

WHY THIS EXISTS
  The existing recent generation layer sources FUELINST, which is provisional.
  THE_DATA_SPINE doctrine requires the confirmed historic spine to come from FUELHH,
  the settled half hourly generation dataset. This script fetches FUELHH per month,
  normalises fuels into the same technology buckets and writes one shard per year
  month under a dedicated FUELHH path so it never collides with FUELINST data.

DESIGN
  Resumable: a month whose shard already exists and is non empty is skipped unless
  FORCE=true. Re-running simply fills whatever is missing.
  Throttled: REQUEST_DELAY_SECONDS between requests and exponential backoff on failure.
  Time budgeted: stops starting new months once TIME_BUDGET_MINUTES is reached.
  Size disciplined: each monthly shard is checked against MAX_SHARD_MB.
  Provenance: every shard carries a source column and progress is written to JSON.
"""

from __future__ import annotations

import csv
import datetime as dt
import hashlib
import json
import os
import time
import urllib.parse
import urllib.request
from collections import defaultdict
from pathlib import Path
from typing import Any

ELEXON_FUELHH = "https://data.elexon.co.uk/bmrs/api/v1/datasets/FUELHH"
OUT_ROOT = Path("data/generation/fuelhh_halfhourly")
PROGRESS_FILE = OUT_ROOT / "BACKFILL_PROGRESS.json"
REPORT_DIR = Path("data_science_protocol/audit_reports")
JSON_REPORT_DIR = REPORT_DIR / "json"
FIELDS = ["time", "technology", "generationMW", "source"]
SOURCE_LABEL = "Elexon BMRS FUELHH"
SCHEMA_VERSION = "1.0.0-fuelhh-shard"

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

START_YEAR_MONTH = os.getenv("START_YEAR_MONTH", "2016-01")
WINDOW_DAYS = int(os.getenv("WINDOW_DAYS", "7"))
REQUEST_DELAY_SECONDS = float(os.getenv("REQUEST_DELAY_SECONDS", "1.5"))
TIME_BUDGET_MINUTES = float(os.getenv("TIME_BUDGET_MINUTES", "300"))
MAX_RETRIES = int(os.getenv("MAX_RETRIES", "5"))
FORCE = os.getenv("FORCE", "false").lower() in {"1", "true", "yes"}
MAX_SHARD_MB = float(os.getenv("MAX_SHARD_MB", "25"))


def utc_now() -> str:
    return dt.datetime.now(dt.timezone.utc).isoformat().replace("+00:00", "Z")


def iso_z(value: Any) -> str:
    if not value:
        return ""
    text = str(value).replace("Z", "+00:00")
    try:
        d = dt.datetime.fromisoformat(text)
        if d.tzinfo is None:
            d = d.replace(tzinfo=dt.timezone.utc)
        return d.astimezone(dt.timezone.utc).isoformat().replace("+00:00", "Z")
    except Exception:
        return str(value)


def pick(row: dict[str, Any], names: list[str]) -> Any:
    folded = {str(k).lower(): v for k, v in row.items()}
    for name in names:
        value = folded.get(name.lower())
        if value not in (None, ""):
            return value
    return ""


def group_for(fuel: str) -> str:
    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 fetch_window(start_day: dt.date, end_day: dt.date) -> list[dict[str, Any]]:
    query = urllib.parse.urlencode({
        "settlementDateFrom": start_day.isoformat(),
        "settlementDateTo": end_day.isoformat(),
        "format": "json",
    })
    url = f"{ELEXON_FUELHH}?{query}"
    last_err: Exception | None = None
    for attempt in range(1, MAX_RETRIES + 1):
        try:
            req = urllib.request.Request(url, headers={"User-Agent": "GlobalGrid2050 GridBot"})
            with urllib.request.urlopen(req, timeout=90) as response:
                data = json.loads(response.read().decode("utf-8"))
            rows = data if isinstance(data, list) else data.get("data", [])
            return rows if isinstance(rows, list) else []
        except Exception as exc:
            last_err = exc
            backoff = min(60, REQUEST_DELAY_SECONDS * (2 ** attempt))
            print(f"retry {attempt}/{MAX_RETRIES} for {start_day} to {end_day}: {exc} sleep {backoff:.0f}s")
            time.sleep(backoff)
    raise RuntimeError(f"window {start_day} to {end_day} failed after {MAX_RETRIES} retries: {last_err}")


def month_window(year: int, month: int) -> tuple[dt.date, dt.date]:
    start = dt.date(year, month, 1)
    end = dt.date(year, 12, 31) if month == 12 else dt.date(year, month + 1, 1) - dt.timedelta(days=1)
    today = dt.datetime.now(dt.timezone.utc).date()
    if (year, month) == (today.year, today.month):
        end = min(end, today - dt.timedelta(days=1))
    return start, end


def windows(start_day: dt.date, end_day: dt.date, span_days: int):
    cur = start_day
    while cur <= end_day:
        win_end = min(cur + dt.timedelta(days=span_days - 1), end_day)
        yield cur, win_end
        cur = win_end + dt.timedelta(days=1)


def clean_rows(raw_rows: list[Any]) -> tuple[list[dict[str, str]], int]:
    deduped: dict[tuple[str, str], tuple[str, str, float]] = {}
    for row in raw_rows:
        if not isinstance(row, dict):
            continue
        fuel = pick(row, ["fuelType", "fuelTypeName", "fuel", "psrType"])
        generation = pick(row, ["generation", "generationMW", "quantity"])
        timestamp = iso_z(pick(row, ["startTime", "settlementPeriodStartTime", "periodStartUTC", "publishDateTime", "settlementDate"])
        )
        if not fuel or generation == "" or not timestamp:
            continue
        try:
            mw = float(generation)
        except Exception:
            continue
        deduped[(timestamp, str(fuel).upper())] = (timestamp, str(fuel).upper(), mw)

    by_tech: dict[tuple[str, str], float] = defaultdict(float)
    for timestamp, fuel, mw in deduped.values():
        by_tech[(timestamp, group_for(fuel))] += mw

    out: list[dict[str, str]] = []
    for timestamp, tech in sorted(by_tech):
        out.append({
            "time": timestamp,
            "technology": tech,
            "generationMW": f"{by_tech[(timestamp, tech)]:.3f}",
            "source": SOURCE_LABEL,
        })
    return out, len(deduped)


def shard_path(year: int, month: int) -> Path:
    return OUT_ROOT / str(year) / f"generation_fuelhh_{year}_{month:02d}.csv"


def write_shard(path: Path, rows: list[dict[str, str]]) -> int:
    path.parent.mkdir(parents=True, exist_ok=True)
    with path.open("w", encoding="utf-8", newline="") as handle:
        writer = csv.DictWriter(handle, fieldnames=FIELDS)
        writer.writeheader()
        writer.writerows(rows)
    return path.stat().st_size


def row_hash(rows: list[dict[str, str]]) -> str:
    h = hashlib.sha256()
    for row in rows:
        h.update(f"{row['time']}|{row['technology']}|{row['generationMW']}".encode("utf-8"))
    return "sha256:" + h.hexdigest()


def already_done(year: int, month: int) -> bool:
    path = shard_path(year, month)
    return path.exists() and path.stat().st_size > 0


def month_iter(start_ym: str):
    sy, sm = (int(x) for x in start_ym.split("-"))
    today = dt.datetime.now(dt.timezone.utc).date()
    last = dt.date(today.year, today.month, 1) - dt.timedelta(days=1)
    year, month = sy, sm
    while (year, month) <= (last.year, last.month):
        yield year, month
        month += 1
        if month > 12:
            year, month = year + 1, 1


def build_month(year: int, month: int) -> tuple[list[dict[str, str]], int, float, list[str]]:
    start_day, end_day = month_window(year, month)
    if end_day < start_day:
        return [], 0, 0.0, []
    raw: list[Any] = []
    failed: list[str] = []
    for w_start, w_end in windows(start_day, end_day, WINDOW_DAYS):
        try:
            chunk = fetch_window(w_start, w_end)
            raw.extend(chunk)
            print(f"{year}-{month:02d} {w_start} to {w_end}: {len(chunk)} raw rows")
        except Exception as exc:
            failed.append(f"{w_start} to {w_end}: {exc}")
            print(f"WARNING {year}-{month:02d} {w_start} to {w_end} failed: {exc}")
        time.sleep(REQUEST_DELAY_SECONDS)
    rows, deduped_count = clean_rows(raw)
    days = (end_day - start_day).days + 1
    techs_seen = len({row["technology"] for row in rows}) or 1
    expected = 48 * days * techs_seen
    completeness = round(min(1.0, len(rows) / expected), 4) if expected else 0.0
    return rows, deduped_count, completeness, failed


def write_progress(done: list[str], failed: list[str], remaining: list[str]) -> None:
    OUT_ROOT.mkdir(parents=True, exist_ok=True)
    PROGRESS_FILE.write_text(json.dumps({
        "schemaVersion": SCHEMA_VERSION,
        "updatedUTC": utc_now(),
        "source": SOURCE_LABEL,
        "startYearMonth": START_YEAR_MONTH,
        "windowDays": WINDOW_DAYS,
        "requestDelaySeconds": REQUEST_DELAY_SECONDS,
        "monthsDone": sorted(set(done)),
        "monthsFailed": sorted(set(failed)),
        "monthsRemaining": sorted(set(remaining)),
        "complete": len(remaining) == 0,
    }, indent=2) + "\n", encoding="utf-8")


def write_run_report(processed: dict[str, Any], skipped: list[str], failed_months: dict[str, str], remaining: list[str], ran_out_of_time: bool) -> None:
    REPORT_DIR.mkdir(parents=True, exist_ok=True)
    JSON_REPORT_DIR.mkdir(parents=True, exist_ok=True)
    s = dt.datetime.now(dt.timezone.utc).strftime("%Y%m%dT%H%M%SZ")
    payload = {
        "schemaVersion": SCHEMA_VERSION,
        "updatedUTC": utc_now(),
        "source": SOURCE_LABEL,
        "startYearMonth": START_YEAR_MONTH,
        "windowDays": WINDOW_DAYS,
        "requestDelaySeconds": REQUEST_DELAY_SECONDS,
        "timeBudgetMinutes": TIME_BUDGET_MINUTES,
        "monthsProcessedThisRun": len(processed),
        "monthsSkippedAlreadyPresent": len(skipped),
        "monthsFailedThisRun": len(failed_months),
        "monthsRemainingAfterRun": len(remaining),
        "stoppedOnTimeBudget": ran_out_of_time,
        "backfillComplete": len(remaining) == 0,
        "processed": processed,
        "failedMonths": failed_months,
        "remaining": remaining,
    }
    lines = [
        f"# FUELHH Half-Hourly Backfill Run {s}",
        "",
        f"Updated UTC: {payload['updatedUTC']}",
        f"Source: {SOURCE_LABEL}",
        f"Start year-month: {START_YEAR_MONTH}",
        f"Window days per request: {WINDOW_DAYS}",
        f"Request delay seconds: {REQUEST_DELAY_SECONDS}",
        f"Months processed this run: {len(processed)}",
        f"Months skipped already present: {len(skipped)}",
        f"Months failed this run: {len(failed_months)}",
        f"Months still remaining after run: {len(remaining)}",
        f"Stopped on time budget: {ran_out_of_time}",
        f"Backfill complete: {len(remaining) == 0}",
        "",
        "## Processed",
    ]
    for ym, info in processed.items():
        lines.append(f"- {ym}: rows={info['rows']} completeness={info['completeness']} size={info['sizeMB']:.2f} MB")
    lines.extend(["", "## Failed", ""])
    if failed_months:
        for ym, detail in failed_months.items():
            lines.append(f"- {ym}: {detail}")
    else:
        lines.append("No failed months.")
    text = "\n".join(lines) + "\n"
    for path in (REPORT_DIR / f"FUELHH_BACKFILL_{s}.md", REPORT_DIR / "FUELHH_BACKFILL_LATEST.md"):
        path.write_text(text, encoding="utf-8")
    js = json.dumps(payload, indent=2, ensure_ascii=False) + "\n"
    for path in (JSON_REPORT_DIR / f"FUELHH_BACKFILL_{s}.json", JSON_REPORT_DIR / "FUELHH_BACKFILL_LATEST.json"):
        path.write_text(js, encoding="utf-8")


def main() -> int:
    deadline = time.monotonic() + TIME_BUDGET_MINUTES * 60
    all_months = list(month_iter(START_YEAR_MONTH))
    done: list[str] = []
    failed_all: list[str] = []
    processed: dict[str, Any] = {}
    skipped: list[str] = []
    failed_months: dict[str, str] = {}
    ran_out_of_time = False

    for year, month in all_months:
        ym = f"{year}-{month:02d}"
        if already_done(year, month) and not FORCE:
            skipped.append(ym)
            done.append(ym)
            continue
        if time.monotonic() > deadline:
            ran_out_of_time = True
            print(f"Time budget reached. Stopping before {ym}.")
            break
        print(f"Building {ym}")
        try:
            rows, raw_count, completeness, failed_windows = build_month(year, month)
        except Exception as exc:
            failed_months[ym] = str(exc)
            failed_all.append(ym)
            continue
        if not rows:
            failed_months[ym] = "no rows returned"
            failed_all.append(ym)
            continue
        size = write_shard(shard_path(year, month), rows)
        size_mb = size / 1024 / 1024
        if size_mb > MAX_SHARD_MB:
            failed_months[ym] = f"shard {size_mb:.2f} MB exceeds {MAX_SHARD_MB} MB"
            shard_path(year, month).unlink(missing_ok=True)
            failed_all.append(ym)
            continue
        processed[ym] = {
            "rows": len(rows),
            "rawRows": raw_count,
            "completeness": completeness,
            "sizeMB": size_mb,
            "hash": row_hash(rows),
            "failedWindows": failed_windows,
        }
        done.append(ym)
        print(f"wrote {ym}: {len(rows)} rows, {size_mb:.2f} MB, completeness {completeness}")

    remaining = [f"{year}-{month:02d}" for year, month in all_months if f"{year}-{month:02d}" not in done]
    write_progress(done, failed_all, remaining)
    write_run_report(processed, skipped, failed_months, remaining, ran_out_of_time)
    print(f"Done. processed={len(processed)} skipped={len(skipped)} failed={len(failed_months)} remaining={len(remaining)} complete={len(remaining) == 0}")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())