GLOBALGRID2050 / EXPLORE CODE

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

GlobalGrid2050 Renewable Energy Planning Database (REPD) Sources V6 and 2 more files

A Python command-line tool in the scripts folder that fetches REPD canonical, REPD reconciliation, REPD publication q2 and the REPD register, using SHA-256.

700 lines of Python from globalgrid2050, in 3 files. Topic: Electricity markets and data pipelines. Section 887 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 Renewable Energy Planning Database (REPD) Q3 2026 Ingest energy-data · globalgrid2050 / scripts/repd_q3_2026_ingest.py lines 359–370
  2. GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 Staging Verify energy-data · globalgrid2050 / scripts/repd_q3_staging_verify.py lines 1–123
  3. GlobalGrid2050 Renewable Energy Planning Database (REPD) Sources V6 energy-data · globalgrid2050 / scripts/repd_sources_v6.py lines 1–565

GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 2026 Ingest

A Python command-line tool in the scripts folder that reads REPD publication q3, REPD publication q2, q2 baseline sha256 audit and q3 date provenance, using SHA-256.

energy-dataglobalgrid2050 / scripts/repd_q3_2026_ingest.py · lines 359–370 of 370 (continued from section 886) · Entry point · pattern reconcile (shared by 7 files)

What it does
It loads REPD publication q3, REPD publication q2, q2 baseline sha256 audit and q3 date provenance. Defines 7 functions or classes, including strict_date_iso, day_first, digest, check, canonicalise_exact_duplicates. Uses libraries __future__, hashlib, json, os, sys. Side effects: reads or writes files.
Why it exists in the grid workflow
Domain stage upstream_foresight (path word 'repd'): 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: CSV file REPD_Publication_Q3_2026.csv; Excel file REPD_Publication_Q3_2026.xlsx; CSV file repd.csv; CSV file Q3_DATE_PROVENANCE.csv. Out: CSV file REPD_Publication_Q2_2026.csv; Excel file REPD_Publication_Q2_2026.xlsx; JSON file Q2_BASELINE_SHA256_AUDIT.json; CSV file REPD_Publication_Q3_2026.csv; Excel file REPD_Publication_Q3_2026.xlsx; JSON file Q3_RECONCILIATION.json; CSV file Q3_CANONICAL_STAGED.csv.
Downstream impact
Used directly by 2 files: GlobalGrid2050 Renewable Energy Planning Database (REPD) 2026 Q3 Ingestion Workflow (globalgrid2050); GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 Evidence Branch Workflow (globalgrid2050).
What it achieves
It is built from the strict date iso helper (`strict_date_iso`), the day first helper (`day_first`) and the digest step (`digest`). Isolated DESNZ October 2026 (Q3) REPD intake and Q2 delta audit. Its code works with 0.0050001 MW.
Why it matters
Run by GlobalGrid2050 Renewable Energy Planning Database (REPD) 2026 Q3 Ingestion Workflow and GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 Evidence Branch Workflow.
Application
globalgrid2050 · scripts · Python CLI
Uses
GlobalGrid2050 Renewable Energy Planning Database (REPD) Sources V6 (import), __future__, hashlib, json, os, sys, datetime, pandas, pathlib
Used by
run by GlobalGrid2050 Renewable Energy Planning Database (REPD) 2026 Q3 Ingestion Workflow (workflow run), run by GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 Evidence Branch Workflow (workflow run)
Produces
strict_date_iso, day_first, digest, check, canonicalise_exact_duplicates, reconcile, main
Defines here
none
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/repd_q3_2026_ingest.py:359-370 · blob e59eec0b7ed013345cf603686f3260b9350849a1 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: filesystem · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
        handle.write("Live published datasets: UNCHANGED\n")
    if report["failures"]:
        raise RuntimeError("Q3 intake failed validation: " + "; ".join(report["failures"]))
    assert canonical is not None
    # Pin the record terminator: the pandas default is os.linesep, so the same
    # candidate would hash differently on a Windows runner.
    canonical.to_csv(OUT / "Q3_CANONICAL_STAGED.csv", index=False, encoding="utf-8-sig", lineterminator="\n")
    print(json.dumps({"status": report["stage"], "rows": report["csv_rows"], "comparison_baseline": comparison_name, "changes": report["reference_changes_vs_comparison_baseline"], "sha256": report["q3_source_sha256"]}, indent=2))


if __name__ == "__main__":
    main()

GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 Staging Verify

A Python command-line tool in the scripts folder that reads REPD grid batteries, REPD solar operational over 4MW, REPD master and REPD reconciliation, using SHA-256.

energy-dataglobalgrid2050 / scripts/repd_q3_staging_verify.py · lines 1–123 of 123 · Entry point

What it does
It loads REPD grid batteries, REPD solar operational over 4MW, REPD master and REPD reconciliation. Defines sha, csv_rows, main. Uses libraries csv, hashlib, io, json, os. Side effects: reads or writes files; starts processes.
Why it exists in the grid workflow
Domain stage upstream_foresight (path word 'repd'): 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: CSV file repd.csv; CSV file repd-grid-batteries.csv; CSV file repd-solar-operational.csv; CSV file repd-solar-operational-over-4mw.csv; JSON file repd_master.json; JSON file manifest_v6.json; JSON file repd_source_reconciliation_v6.json; JSON file Q3_RECONCILIATION.json. Out: JSON file INTEGRITY-VERIFICATION.json.
Downstream impact
Used directly by 1 file: GlobalGrid2050 Renewable Energy Planning Database (REPD) 2026 Q3 Ingestion Workflow (globalgrid2050).
What it achieves
Defines `sha` and `csv_rows`.
Why it matters
Run by GlobalGrid2050 Renewable Energy Planning Database (REPD) 2026 Q3 Ingestion Workflow.
Application
globalgrid2050 · scripts · Python CLI
Uses
csv, hashlib, io, json, os, re, subprocess, datetime
Used by
run by GlobalGrid2050 Renewable Energy Planning Database (REPD) 2026 Q3 Ingestion Workflow (workflow run)
Produces
sha, csv_rows, main
Defines here
sha, csv_rows, 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/repd_q3_staging_verify.py:1-123 · blob 315c49ebacdfec9dc9a4357ec4cdbc9a48247ca3 · GitHub at these lines
State
achieves: name · context: readme_derived · stage: output · effects: filesystem, process · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: production_core
#!/usr/bin/env python3
"""Independent candidate integrity and Git backup verifier (no production writes)."""
import csv
import hashlib
import io
import json
import os
import re
import subprocess
from datetime import datetime
from collections import Counter
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
OUT = Path(os.environ.get("REPD_Q3_OUTPUT", ROOT / ".repd-staging" / "2026-10"))
PREVIOUS = {
    "repd.csv": "01fe17ae9531d3504490a1418a68350262b06f86",
    "repd-grid-batteries.csv": "2d4142c7bb97bbd4be1c67698731730d2e25c672",
    "repd-solar-operational.csv": "5d8bab7f640eb943dcf66b2631ad6bcde1f52122",
    "repd-solar-operational-over-4mw.csv": "31e840cfe206cfb53d9ed2c043e00bdd69080a82",
    "dist/repd_master.json": "e221065a7b144a9e4b691cf282e23a68c5661e45",
    "dist/manifest_v6.json": "dbaf471f262d3b6557e7f327269b663eeddc432c",
    "dist/repd_source_reconciliation_v6.json": "35835b6b46aa1f996fedac8c7bf3044a3d423814",
}


def sha(data):
    return hashlib.sha256(data).hexdigest()


def csv_rows(data):
    try:
        decoded = data.decode("utf-8-sig")
    except UnicodeDecodeError:
        decoded = data.decode("cp1252")
    reader = csv.DictReader(io.StringIO(decoded, newline=""))
    rows = list(reader)
    assert len(reader.fieldnames) == 53
    assert all(None not in row and all(v is not None for v in row.values()) for row in rows)
    return reader.fieldnames, rows


def main():
    r = json.loads((OUT / "Q3_RECONCILIATION.json").read_text())
    assert r["no_production_promotion"] is True and not r["failures"]
    assert r["stage"] == "STAGED_WITH_SOURCE_DATE_LOCALE_NORMALISATIONS"
    raw = (OUT / "official-sources/REPD_Publication_Q3_2026.csv").read_bytes()
    excel = (OUT / "official-sources/REPD_Publication_Q3_2026.xlsx").read_bytes()
    candidate = (OUT / "Q3_CANONICAL_STAGED.csv").read_bytes()
    assert sha(raw) == r["q3_source_sha256"]["csv"]
    assert sha(excel) == r["q3_source_sha256"]["xlsx"]
    assert raw.endswith(b"\n") and candidate.endswith(b"\n")
    input_columns, source_rows = csv_rows(raw)
    output_columns, output_rows = csv_rows(candidate)
    normalized_columns = [re.sub(r"\s+", " ", name.strip()) for name in input_columns]
    assert normalized_columns == output_columns, "Column loss, collision or reordering beyond whitespace canonicalisation"
    assert len(set(normalized_columns)) == len(output_columns), "Input header normalisation collision"
    src = Counter(row["Ref ID"].strip() for row in source_rows)
    dst = Counter(row["Ref ID"].strip() for row in output_rows)
    assert len(source_rows) == r["original_csv_rows"] == 14968
    assert len(output_rows) == r["q3_unique_ids"] == 14967
    assert set(src) == set(dst) and all(v == 1 for v in dst.values())
    assert {k:v for k,v in src.items() if v != 1} == {"7679": 2}
    assert source_rows[-1]["Ref ID"] == output_rows[-1]["Ref ID"] == "21493"
    assert r["quarantined_official_date_disagreements"] == []
    assert len(r["source_locale_date_normalisations"]) == r["source_locale_date_normalisation_count"] == 25
    assert len(r["formerly_ambiguous_date_representations"]) == 5
    refs = {row["Ref ID"].strip(): row for row in output_rows}
    for item in r["source_locale_date_normalisations"]:
        assert refs[item["repd_ref"]][item["field"]] == item["candidate_value"], "Month-first date not corrected"
        assert datetime.strptime(item["candidate_value"], "%d/%m/%Y").strftime("%Y-%m-%d") == item["official_xlsx"]
        assert datetime.strptime(item["official_csv"], "%m/%d/%Y").strftime("%Y-%m-%d") == item["official_xlsx"]
        assert item["official_xlsx"] <= item["record_last_updated"]
    date_columns = [
        "Planning Application Submitted", "Planning Application Withdrawn",
        "Planning Permission Refused", "Planning Permission Granted",
        "Planning Permission Expired", "Under Construction", "Operational",
        "Record Last Updated (dd/mm/yyyy)",
    ]
    # Also hold every date column the general rule discovered to strict dd/mm/yyyy.
    date_columns = list(dict.fromkeys(date_columns + r["date_rule"]["date_columns"]))
    assert r["date_rule"]["ok"] and r["date_rule"]["failures"] == 0
    non_day_first_dates = []
    for row in output_rows:
        for field in date_columns:
            if field not in row:
                continue
            value = row[field].strip()
            if not value:
                continue
            if not re.fullmatch(r"\d{2}/\d{2}/\d{4}", value):
                non_day_first_dates.append((row["Ref ID"], field, value))
                continue
            try:
                datetime.strptime(value, "%d/%m/%Y")
            except ValueError:
                non_day_first_dates.append((row["Ref ID"], field, value))
    assert non_day_first_dates == [], f"Candidate has invalid/non-dd/mm/yyyy dates: {non_day_first_dates[:10]}"
    results = {
        "source_csv_sha256": sha(raw), "source_xlsx_sha256": sha(excel),
        "source_csv_bytes": len(raw), "source_xlsx_bytes": len(excel),
        "source_rows": len(source_rows), "candidate_rows": len(output_rows),
        "final_ref_id": output_rows[-1]["Ref ID"],
        "columns": len(output_columns), "date_locale_corrections": 25,
        "non_day_first_candidate_date_cells": len(non_day_first_dates), "historical_blobs": {},
    }
    for path, expected_sha in PREVIOUS.items():
        original = ROOT / path
        assert original.is_file(), "Missing historical file: " + path
        blob = subprocess.check_output(["git", "hash-object", str(original)], text=True).strip()
        assert blob == expected_sha, "Historical source changed: " + path
        backup = OUT / "pre-ingestion-backup" / original.name
        assert backup.is_file(), "Missing historical backup: " + path
        assert sha(original.read_bytes()) == sha(backup.read_bytes()), "Backup mismatch: " + path
        results["historical_blobs"][path] = blob
    master = json.loads((ROOT / "dist/repd_master.json").read_text())
    assert len(master["features"]) == 10784, "Historical REPD GeoJSON changed"
    (OUT / "INTEGRITY-VERIFICATION.json").write_text(json.dumps(results, indent=2)+"\n")
    print(json.dumps({"result":"PASS", **results},indent=2))


if __name__ == "__main__":
    main()

GlobalGrid2050 Renewable Energy Planning Database (REPD) Sources V6

A Python command-line tool in the scripts folder that fetches REPD canonical, REPD reconciliation, REPD publication q2 and the REPD register, using SHA-256.

energy-dataglobalgrid2050 / scripts/repd_sources_v6.py · lines 1–565 of 597 · Entry point · pattern planning_refs_equivalent (shared by 2 files)

What it does
It fetches REPD canonical, REPD reconciliation, REPD publication q2 and the REPD register. Defines 24 functions or classes, including utc_now, sha256, collapse, norm_text, norm_date. Uses libraries __future__, hashlib, io, json, math. Side effects: reads or writes files; makes network requests.
Why it exists in the grid workflow
Domain stage upstream_foresight (path word 'repd'): 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: the REPD register.
Inputs & outputs
In: CSV file latest_repd_v6_canonical.csv; JSON file repd_source_reconciliation_v6.json; JSON file manifest_v6.json; CSV file REPD_Publication_Q2_2026.csv; Excel file REPD_Publication_Q2_2026.xlsx; command-line arguments. Out: CSV file.
Downstream impact
Used directly by 6 files: GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 2026 Ingest (globalgrid2050); GlobalGrid2050 Validate Renewable Energy Planning Database (REPD) Sources V6 (globalgrid2050); GlobalGrid2050 Renewable Energy Planning Database (REPD) 2026 Q3 Ingestion Workflow (globalgrid2050); GlobalGrid2050 Build Project Identity V6 (globalgrid2050); and 2 more. Reached from 2 other entry points, including GlobalGrid2050 Major Project News V6 Workflow (globalgrid2050); GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 Evidence Branch Workflow (globalgrid2050).
What it achieves
It cleans ref (`clean_ref`), fetches bytes (`fetch_bytes`) and reads CSV (`read_csv`). Official DESNZ Q2 2026 source retrieval and deterministic reconciliation. Its code works with 1 MW, 100 MW and 0.005 MW. It accepts `--csv-file` and `--xlsx-file`.
Why it matters
Imported by 5 files, including GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 2026 Ingest, GlobalGrid2050 Validate Renewable Energy Planning Database (REPD) Sources V6 and GlobalGrid2050 Build Project Identity V6. Run by GlobalGrid2050 Renewable Energy Planning Database (REPD) 2026 Q3 Ingestion Workflow.
Application
globalgrid2050 · scripts · Python CLI
Uses
__future__, hashlib, io, json, math, os, re, time
Used by
imported by GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 2026 Ingest (import), imported by GlobalGrid2050 Validate Renewable Energy Planning Database (REPD) Sources V6 (import), run by GlobalGrid2050 Renewable Energy Planning Database (REPD) 2026 Q3 Ingestion Workflow (workflow run), imported by GlobalGrid2050 Build Project Identity V6 (import), imported by GlobalGrid2050 Validate Project Identity V6 (import), imported by GlobalGrid2050 Validate V6 Manifest Provenance (import)
Produces
utc_now, sha256, collapse, norm_text, norm_date, strict_date, month_first_date, is_typed_date, drop_identical_duplicates, norm_number, clean_ref, canonicalise_headers
Defines here
utc_now, sha256, collapse, norm_text, norm_date, strict_date, month_first_date, is_typed_date, drop_identical_duplicates, norm_number, clean_ref, canonicalise_headers, fetch_bytes, read_csv, read_xlsx, indexed, threshold_counts, planning_refs_equivalent, build_canonical, reconcile, check, run_reconciliation
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/repd_sources_v6.py:1-565 · blob d7470d97a2e34da8cdc5e9eb410a6e6d77f58016 · 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
"""Official DESNZ Q2 2026 source retrieval and deterministic reconciliation.

The published CSV is a two-decimal representation of several capacity values,
whereas the XLSX retains the underlying precision.  V6 therefore uses the XLSX
capacity as the canonical official value only after both publications pass the
same row/Ref-ID gates and every difference is format-equivalent.
"""
from __future__ import annotations

import hashlib
import io
import json
import math
import os
import re
import time
from datetime import datetime, timezone
from pathlib import Path
from urllib.request import Request, urlopen

import pandas as pd

ROOT = Path(__file__).resolve().parents[1]
DATA = ROOT / "data"
DIST = ROOT / "dist"
CANONICAL_CSV = DATA / "latest_repd_v6_canonical.csv"
REPORT = DIST / "repd_source_reconciliation_v6.json"
MANIFEST = DIST / "manifest_v6.json"

SOURCE_PAGE = "https://www.gov.uk/government/publications/renewable-energy-planning-database-quarterly-extract"
CSV_URL = "https://assets.publishing.service.gov.uk/media/6a6cbdc00c36759b5ccaa305/REPD_Publication_Q2_2026.csv"
XLSX_URL = "https://assets.publishing.service.gov.uk/media/6a6cbdd2862aaf18d9c62b02/REPD_Publication_Q2_2026.xlsx"
PUBLICATION_DATE = "2026-08-03"
DATASET_TITLE = "Renewable Energy Planning Database (REPD): July 2026 (Q2 2026)"

EXPECTED_ROWS = 14657
EXPECTED_SOLAR_GT1 = 3445
EXPECTED_BESS_GT100 = 269
EXPECTED_CSV_SHA256 = "84c1b5f958a934d8b4b86ec88f50bdcf43830ded7ff2efc27bffca0c98695035"
EXPECTED_XLSX_SHA256 = "624a0a9712c58a7a93716e51f2bf054eec8b1af7170f6f9516cc10cd248e2657"
CAPACITY_ROUNDING_TOLERANCE_MW = 0.0050001

# Release descriptor. The July 2026 (Q2) values above are the DEFAULT descriptor,
# so a run without REPD_RELEASE_DESCRIPTOR behaves exactly as the historical V6
# chain did (same names, same gates, same bytes). A descriptor JSON overrides the
# pins for a later release and MUST redirect every output to REPD_OUT_DIR under
# new release-suffixed names; the frozen *_v6 files are never written by it.
RELEASE_ID = "2026q2"
QUARTER_LABEL = "Q2"
OUTPUT_SUFFIX = "v6"
EXPECTED_UNIQUE_REFS = EXPECTED_ROWS
DUPLICATE_ROW_POLICY = "reject"  # "reject" | "drop_identical"
DATE_RULE = "csv"  # "csv" (historical) | "xlsx_typed_wins"
NA_DATE_MARKERS = {"na", "n/a"}
RELEASE_DESCRIPTOR_PATH = os.environ.get("REPD_RELEASE_DESCRIPTOR", "").strip()
RELEASE_DESCRIPTOR: dict = {}
if RELEASE_DESCRIPTOR_PATH:
    RELEASE_DESCRIPTOR = json.loads(Path(RELEASE_DESCRIPTOR_PATH).read_text(encoding="utf-8"))
    _required = [
        "release_id", "quarter_label", "output_suffix", "csv_url", "xlsx_url", "publication_date",
        "dataset_title", "expected_rows", "expected_unique_refs", "expected_solar_gt1",
        "expected_bess_gt100", "expected_csv_sha256", "expected_xlsx_sha256",
    ]
    _missing = [key for key in _required if RELEASE_DESCRIPTOR.get(key) in (None, "")]
    if _missing:
        raise RuntimeError(f"Release descriptor {RELEASE_DESCRIPTOR_PATH} missing keys: {_missing}")
    RELEASE_ID = str(RELEASE_DESCRIPTOR["release_id"])
    QUARTER_LABEL = str(RELEASE_DESCRIPTOR["quarter_label"])
    OUTPUT_SUFFIX = str(RELEASE_DESCRIPTOR["output_suffix"])
    if not re.fullmatch(r"[0-9]{4}q[1-4]", OUTPUT_SUFFIX):
        raise RuntimeError(f"Descriptor output_suffix must look like 2026q3 (never v6): {OUTPUT_SUFFIX!r}")
    CSV_URL = RELEASE_DESCRIPTOR["csv_url"]
    XLSX_URL = RELEASE_DESCRIPTOR["xlsx_url"]
    PUBLICATION_DATE = RELEASE_DESCRIPTOR["publication_date"]
    DATASET_TITLE = RELEASE_DESCRIPTOR["dataset_title"]
    EXPECTED_ROWS = int(RELEASE_DESCRIPTOR["expected_rows"])
    EXPECTED_UNIQUE_REFS = int(RELEASE_DESCRIPTOR["expected_unique_refs"])
    EXPECTED_SOLAR_GT1 = int(RELEASE_DESCRIPTOR["expected_solar_gt1"])
    EXPECTED_BESS_GT100 = int(RELEASE_DESCRIPTOR["expected_bess_gt100"])
    EXPECTED_CSV_SHA256 = RELEASE_DESCRIPTOR["expected_csv_sha256"]
    EXPECTED_XLSX_SHA256 = RELEASE_DESCRIPTOR["expected_xlsx_sha256"]
    DUPLICATE_ROW_POLICY = RELEASE_DESCRIPTOR.get("duplicate_row_policy", DUPLICATE_ROW_POLICY)
    DATE_RULE = RELEASE_DESCRIPTOR.get("date_rule", DATE_RULE)
    if DUPLICATE_ROW_POLICY not in {"reject", "drop_identical"} or DATE_RULE not in {"csv", "xlsx_typed_wins"}:
        raise RuntimeError(f"Unsupported descriptor policy: duplicates={DUPLICATE_ROW_POLICY!r} dates={DATE_RULE!r}")
    _out = os.environ.get("REPD_OUT_DIR", "").strip()
    if not _out:
        raise RuntimeError("REPD_OUT_DIR is required with a release descriptor (outputs are never written in place)")
    DATA = Path(_out) / "data"
    DIST = Path(_out) / "dist"
    CANONICAL_CSV = DATA / f"latest_repd_{OUTPUT_SUFFIX}_canonical.csv"
    REPORT = DIST / f"repd_source_reconciliation_{OUTPUT_SUFFIX}.json"
    MANIFEST = DIST / f"manifest_{OUTPUT_SUFFIX}.json"

CORE_TEXT_FIELDS = [
    "Site Name",
    "Technology Type",
    "Development Status (short)",
    "Record Last Updated (dd/mm/yyyy)",
    "Operator (or Applicant)",
    "Planning Application Reference",
    "Planning Authority",
]
OFFICIAL_DATE_FIELDS = [
    "Planning Application Submitted",
    "Planning Application Withdrawn",
    "Planning Permission Refused",
    "Planning Permission Granted",
    "Planning Permission Expired",
    "Under Construction",
    "Operational",
]


def utc_now() -> str:
    # REPD_VALIDATED_AT pins the timestamp so a rerun on the same bytes is byte-identical.
    return os.environ.get("REPD_VALIDATED_AT", "").strip() or datetime.now(timezone.utc).isoformat()


def sha256(raw: bytes) -> str:
    return hashlib.sha256(raw).hexdigest()


def collapse(value) -> str:
    if value is None or (not isinstance(value, str) and pd.isna(value)):
        return ""
    text = str(value).replace("_x000D_", " ").replace("\r", " ").replace("\n", " ")
    return re.sub(r"\s+", " ", text).strip()


def norm_text(value) -> str:
    return collapse(value).casefold()


def norm_date(value) -> str:
    if value is None or (not isinstance(value, str) and pd.isna(value)) or not str(value).strip():
        return ""
    if isinstance(value, (datetime, pd.Timestamp)):
        return value.strftime("%Y-%m-%d")
    text = collapse(value)
    for pattern in ("%d/%m/%Y", "%Y-%m-%d", "%Y-%m-%d %H:%M:%S", "%m/%d/%Y"):
        try:
            return datetime.strptime(text, pattern).strftime("%Y-%m-%d")
        except ValueError:
            continue
    return ""


def strict_date(value) -> str:
    """Day-first or ISO only; never infers month-first (used by DATE_RULE xlsx_typed_wins)."""
    if value is None or (not isinstance(value, str) and pd.isna(value)):
        return ""
    if isinstance(value, (datetime, pd.Timestamp)):
        return value.strftime("%Y-%m-%d")
    text = collapse(value)
    for pattern in ("%d/%m/%Y", "%Y-%m-%d", "%Y-%m-%d %H:%M:%S"):
        try:
            return datetime.strptime(text, pattern).strftime("%Y-%m-%d")
        except ValueError:
            continue
    return ""


def month_first_date(value) -> str:
    try:
        return datetime.strptime(collapse(value), "%m/%d/%Y").strftime("%Y-%m-%d")
    except ValueError:
        return ""


def is_typed_date(value) -> bool:
    return isinstance(value, (datetime, pd.Timestamp)) and not pd.isna(value)


def drop_identical_duplicates(frame: pd.DataFrame, label: str) -> tuple[pd.DataFrame, list[dict]]:
    """Keep the first of exactly identical repeated rows; conflicting duplicates still fail closed."""
    if "Ref ID" not in frame.columns:
        raise RuntimeError(f"{label} missing Ref ID")
    refs = [clean_ref(value) for value in frame["Ref ID"].tolist()]
    positions: dict[str, list[int]] = {}
    for index, ref in enumerate(refs):
        positions.setdefault(ref, []).append(index)
    evidence, drop = [], set()
    for ref, indices in positions.items():
        if len(indices) < 2 or not ref:
            continue
        first = [collapse(v) for v in frame.iloc[indices[0]].tolist()]
        if any([collapse(v) for v in frame.iloc[i].tolist()] != first for i in indices[1:]):
            raise RuntimeError(f"{label} conflicting duplicate Ref ID {ref}; refusing to choose a record")
        drop.update(indices[1:])
        evidence.append({"repd_ref": ref, "occurrences": len(indices),
                         "data_rows_1_based": [i + 1 for i in indices], "exactly_identical": True})
    kept = frame.iloc[[i for i in range(len(frame)) if i not in drop]].reset_index(drop=True)
    return kept, evidence


def norm_number(value):
    if value is None or (not isinstance(value, str) and pd.isna(value)) or not str(value).strip():
        return None
    try:
        number = float(str(value).replace(",", ""))
        return number if math.isfinite(number) else None
    except Exception:
        return None


def clean_ref(value) -> str:
    text = collapse(value)
    if re.fullmatch(r"\d+\.0", text):
        text = text[:-2]
    return text


def canonicalise_headers(frame: pd.DataFrame) -> pd.DataFrame:
    recognised = CORE_TEXT_FIELDS + OFFICIAL_DATE_FIELDS + [
        "Ref ID",
        "Old Ref ID",
        "Installed Capacity (MWelec)",
        "County",
        "Region",
        "Country",
        "Storage Co-location REPD Ref ID",
        "Are they re-applying (New REPD Ref)",
        "Are they re-applying (Old REPD Ref)",
        "Planning Permission Granted",
    ]
    aliases = {name.casefold(): name for name in recognised}
    aliases.update({
        "record last updated": "Record Last Updated (dd/mm/yyyy)",
        "record last updated (dd/mm/yyyy)": "Record Last Updated (dd/mm/yyyy)",
        "planning permission granted": "Planning Permission Granted",
    })
    renamed = {}
    seen = {}
    for source in frame.columns:
        collapsed = re.sub(r"\s+", " ", str(source).strip())
        target = aliases.get(collapsed.casefold(), collapsed)
        if target in seen:
            raise RuntimeError(f"Header canonicalisation collision: {seen[target]!r} and {source!r} -> {target!r}")
        seen[target] = source
        renamed[source] = target
    return frame.rename(columns=renamed)


def fetch_bytes(url: str) -> bytes:
    last_error = None
    for attempt in range(3):
        request = Request(url, headers={"User-Agent": "GlobalGrid2050/6.0 (+https://globalgrid2050.com/)"})
        try:
            with urlopen(request, timeout=75) as response:
                payload = response.read()
                if not payload:
                    raise RuntimeError("empty response")
                return payload
        except Exception as exc:
            last_error = exc
            if attempt < 2:
                time.sleep(2 ** attempt)
    raise RuntimeError(f"Unable to retrieve official source after three attempts: {url}: {last_error}")


def read_csv(raw: bytes) -> pd.DataFrame:
    last_error = None
    for encoding in ("utf-8-sig", "cp1252"):
        try:
            frame = pd.read_csv(
                io.BytesIO(raw),
                encoding=encoding,
                dtype=str,
                keep_default_na=False,
                on_bad_lines="error",
                engine="python",
            )
            return canonicalise_headers(frame)
        except UnicodeDecodeError as exc:
            last_error = exc
    raise RuntimeError(f"Unable to decode official REPD CSV: {last_error}")


def read_xlsx(raw: bytes) -> tuple[pd.DataFrame, str]:
    book = pd.ExcelFile(io.BytesIO(raw), engine="openpyxl")
    required = {"Ref ID", "Site Name", "Technology Type", "Installed Capacity (MWelec)"}
    for sheet in book.sheet_names:
        frame = canonicalise_headers(pd.read_excel(book, sheet_name=sheet, engine="openpyxl"))
        if required.issubset(frame.columns):
            return frame, sheet
    raise RuntimeError(f"No REPD data sheet found in official XLSX; sheets={book.sheet_names}")


def indexed(frame: pd.DataFrame, label: str) -> pd.DataFrame:
    if "Ref ID" not in frame.columns:
        raise RuntimeError(f"{label} missing Ref ID")
    refs = [clean_ref(value) for value in frame["Ref ID"].tolist()]
    missing = sum(not value for value in refs)
    duplicates = len(refs) - len(set(refs))
    if missing or duplicates:
        raise RuntimeError(f"{label} invalid Ref IDs: missing={missing} duplicates={duplicates}")
    out = frame.copy()
    out["__repd_ref"] = refs
    return out.set_index("__repd_ref", drop=False)


def threshold_counts(frame: pd.DataFrame) -> tuple[int, int]:
    solar = 0
    bess = 0
    for _, row in frame.iterrows():
        capacity = norm_number(row.get("Installed Capacity (MWelec)"))
        if capacity is None:
            continue
        technology = norm_text(row.get("Technology Type"))
        if "solar photovoltaic" in technology and capacity > 1.0:
            solar += 1
        if "battery" in technology and capacity > 100.0:
            bess += 1
    return solar, bess


def planning_refs_equivalent(csv_value, xlsx_value) -> bool:
    left, right = norm_text(csv_value), norm_text(xlsx_value)
    missing = {"", "n/a", "na", "none", "not known", "unknown", "not supplied"}
    if left in missing and right in missing:
        return True
    if left == right:
        return True
    # Excel may coerce a digits-only planning reference and remove a leading zero.
    return left.isdigit() and right.isdigit() and int(left) == int(right)


def build_canonical(csv_frame: pd.DataFrame, xlsx_frame: pd.DataFrame) -> pd.DataFrame:
    csv_ix = indexed(csv_frame, "CSV")
    xlsx_ix = indexed(xlsx_frame, "XLSX")
    canonical = csv_ix.copy()

    # Sanitise control-code representation without inventing a replacement value.
    for column in canonical.columns:
        if column != "__repd_ref":
            canonical[column] = canonical[column].map(collapse)

    # The workbook is the precision-preserving official representation of capacity.
    capacities = []
    for ref in canonical.index:
        value = norm_number(xlsx_ix.at[ref, "Installed Capacity (MWelec)"])
        capacities.append("" if value is None else format(value, ".15g"))
    canonical["Installed Capacity (MWelec)"] = capacities
    # Planning references are identifiers rather than numbers. Excel's typed import
    # can expose a leading zero that the CSV serialisation omitted; keep that exact
    # official workbook representation after integer-equivalence has been validated.
    if "Planning Application Reference" in canonical.columns:
        canonical["Planning Application Reference"] = [
            collapse(xlsx_ix.at[ref, "Planning Application Reference"]) for ref in canonical.index
        ]
    if DATE_RULE == "xlsx_typed_wins":
        # General date rule: a typed workbook date is the official value; every consumed
        # date is serialised strictly dd/mm/yyyy; NA text is a blank (unknown), never a date.
        for field in ["Record Last Updated (dd/mm/yyyy)"] + OFFICIAL_DATE_FIELDS:
            if field not in canonical.columns or field not in xlsx_ix.columns:
                continue
            values = []
            for ref in canonical.index:
                typed = xlsx_ix.at[ref, field]
                iso = strict_date(typed) if is_typed_date(typed) else strict_date(csv_ix.at[ref, field])
                values.append(datetime.strptime(iso, "%Y-%m-%d").strftime("%d/%m/%Y") if iso else "")
            canonical[field] = values
    canonical["Ref ID"] = [clean_ref(value) for value in canonical["Ref ID"]]
    return canonical.drop(columns=["__repd_ref"])


def reconcile(csv_raw: bytes, xlsx_raw: bytes) -> tuple[pd.DataFrame, dict, dict]:
    csv_frame = read_csv(csv_raw)
    xlsx_frame, xlsx_sheet = read_xlsx(xlsx_raw)
    csv_source_rows, xlsx_source_rows = len(csv_frame), len(xlsx_frame)
    duplicate_evidence = {"csv": [], "xlsx": []}
    if DUPLICATE_ROW_POLICY == "drop_identical":
        csv_frame, duplicate_evidence["csv"] = drop_identical_duplicates(csv_frame, "CSV")
        xlsx_frame, duplicate_evidence["xlsx"] = drop_identical_duplicates(xlsx_frame, "XLSX")
    csv_ix = indexed(csv_frame, "CSV")
    xlsx_ix = indexed(xlsx_frame, "XLSX")

    errors = []
    checks = []

    def check(ok, gate, detail=""):
        checks.append({"gate": gate, "pass": bool(ok), "detail": detail})
        if not ok:
            errors.append(f"{gate}: {detail}" if detail else gate)

    csv_refs, xlsx_refs = set(csv_ix.index), set(xlsx_ix.index)
    check(csv_source_rows == EXPECTED_ROWS, "CSV row count exact", f"actual={csv_source_rows} expected={EXPECTED_ROWS}")
    check(xlsx_source_rows == EXPECTED_ROWS, "XLSX row count exact", f"actual={xlsx_source_rows} expected={EXPECTED_ROWS}")
    check(len(csv_refs) == EXPECTED_UNIQUE_REFS, "CSV unique Ref ID count exact", f"actual={len(csv_refs)}")
    check(len(xlsx_refs) == EXPECTED_UNIQUE_REFS, "XLSX unique Ref ID count exact", f"actual={len(xlsx_refs)}")
    check(csv_refs == xlsx_refs, "CSV/XLSX Ref ID sets equal", f"csv_only={len(csv_refs-xlsx_refs)} xlsx_only={len(xlsx_refs-csv_refs)}")
    check(sha256(csv_raw) == EXPECTED_CSV_SHA256, f"{QUARTER_LABEL} CSV immutable SHA-256", sha256(csv_raw))
    check(sha256(xlsx_raw) == EXPECTED_XLSX_SHA256, f"{QUARTER_LABEL} XLSX immutable SHA-256", sha256(xlsx_raw))
    if DUPLICATE_ROW_POLICY != "reject":
        check(
            [d["repd_ref"] for d in duplicate_evidence["csv"]] == [d["repd_ref"] for d in duplicate_evidence["xlsx"]],
            "CSV/XLSX identical duplicate rows agree",
            f"csv={[d['repd_ref'] for d in duplicate_evidence['csv']]} xlsx={[d['repd_ref'] for d in duplicate_evidence['xlsx']]}",
        )

    material = []
    representation = {
        "capacity_precision": 0,
        "control_code_whitespace": 0,
        "planning_reference_leading_zero": 0,
        "planning_reference_missing_marker": 0,
    }
    representation_examples = {
        "capacity_precision": [],
        "control_code_whitespace": [],
        "planning_reference_leading_zero": [],
        "planning_reference_missing_marker": [],
    }
    if DATE_RULE == "xlsx_typed_wins":
        for key in ("csv_month_first_date", "na_date_text"):
            representation[key] = 0
            representation_examples[key] = []
    for ref in sorted(csv_refs & xlsx_refs, key=lambda value: (int(value) if value.isdigit() else 10**18, value)):
        left, right = csv_ix.loc[ref], xlsx_ix.loc[ref]
        for field in CORE_TEXT_FIELDS + OFFICIAL_DATE_FIELDS:
            if field not in csv_ix.columns or field not in xlsx_ix.columns:
                material.append({"repd_ref": ref, "field": field, "reason": "missing field in one publication"})
                continue
            if (field == "Record Last Updated (dd/mm/yyyy)" or field in OFFICIAL_DATE_FIELDS) and DATE_RULE == "xlsx_typed_wins":
                csv_value, xlsx_value = left.get(field), right.get(field)
                key = ""
                if is_typed_date(xlsx_value):
                    equivalent = strict_date(csv_value) == strict_date(xlsx_value)
                    if not equivalent and month_first_date(csv_value) == strict_date(xlsx_value):
                        equivalent, key = True, "csv_month_first_date"
                elif norm_text(csv_value) in NA_DATE_MARKERS | {""} and norm_text(xlsx_value) in NA_DATE_MARKERS | {""}:
                    equivalent = True
                    key = "na_date_text" if (norm_text(csv_value) or norm_text(xlsx_value)) else ""
                else:
                    # Unparseable non-blank text on either side is material: fail closed.
                    equivalent = bool(strict_date(csv_value)) and strict_date(csv_value) == strict_date(xlsx_value)
                if key:
                    representation[key] += 1
                    if len(representation_examples[key]) < 30:
                        representation_examples[key].append(
                            {"repd_ref": ref, "field": field, "csv": collapse(csv_value), "xlsx": strict_date(xlsx_value) or collapse(xlsx_value)}
                        )
            elif field == "Record Last Updated (dd/mm/yyyy)" or field in OFFICIAL_DATE_FIELDS:
                equivalent = norm_date(left.get(field)) == norm_date(right.get(field))
            elif field == "Planning Application Reference":
                equivalent = planning_refs_equivalent(left.get(field), right.get(field))
                if equivalent and norm_text(left.get(field)) != norm_text(right.get(field)):
                    missing = {"", "n/a", "na", "none", "not known", "unknown", "not supplied"}
                    key = (
                        "planning_reference_missing_marker"
                        if norm_text(left.get(field)) in missing and norm_text(right.get(field)) in missing
                        else "planning_reference_leading_zero"
                    )
                    representation[key] += 1
                    if len(representation_examples[key]) < 10:
                        representation_examples[key].append(
                            {"repd_ref": ref, "csv": collapse(left.get(field)), "xlsx": collapse(right.get(field))}
                        )
            else:
                equivalent = norm_text(left.get(field)) == norm_text(right.get(field))
                raw_left = re.sub(r"\s+", " ", str(left.get(field) or "")).strip().casefold()
                raw_right = re.sub(r"\s+", " ", str(right.get(field) or "")).strip().casefold()
                if equivalent and raw_left != raw_right:
                    representation["control_code_whitespace"] += 1
                    if len(representation_examples["control_code_whitespace"]) < 10:
                        representation_examples["control_code_whitespace"].append(
                            {"repd_ref": ref, "field": field, "csv": collapse(left.get(field)), "xlsx": collapse(right.get(field))}
                        )
            if not equivalent:
                material.append({"repd_ref": ref, "field": field, "csv": collapse(left.get(field)), "xlsx": collapse(right.get(field))})

        csv_capacity = norm_number(left.get("Installed Capacity (MWelec)"))
        xlsx_capacity = norm_number(right.get("Installed Capacity (MWelec)"))
        if csv_capacity is None or xlsx_capacity is None:
            if csv_capacity != xlsx_capacity:
                material.append({"repd_ref": ref, "field": "Installed Capacity (MWelec)", "csv": csv_capacity, "xlsx": xlsx_capacity})
        elif abs(csv_capacity - xlsx_capacity) > CAPACITY_ROUNDING_TOLERANCE_MW:
            material.append({"repd_ref": ref, "field": "Installed Capacity (MWelec)", "csv": csv_capacity, "xlsx": xlsx_capacity})
        elif csv_capacity != xlsx_capacity:
            representation["capacity_precision"] += 1
            if len(representation_examples["capacity_precision"]) < 10:
                representation_examples["capacity_precision"].append(
                    {"repd_ref": ref, "csv": csv_capacity, "xlsx": xlsx_capacity}
                )

    check(not material, "CSV/XLSX material fields reconcile", f"material_mismatches={len(material)}")

    canonical = build_canonical(csv_frame, xlsx_frame) if not errors else pd.DataFrame()
    csv_solar, csv_bess = threshold_counts(csv_frame)
    xlsx_solar, xlsx_bess = threshold_counts(xlsx_frame)
    canonical_solar, canonical_bess = threshold_counts(canonical) if not canonical.empty else (0, 0)
    check(xlsx_solar == EXPECTED_SOLAR_GT1, "XLSX/canonical solar >1MW exact", f"actual={xlsx_solar} expected={EXPECTED_SOLAR_GT1}")
    check(xlsx_bess == EXPECTED_BESS_GT100, "XLSX/canonical BESS >100MW exact", f"actual={xlsx_bess} expected={EXPECTED_BESS_GT100}")
    check(canonical_solar == EXPECTED_SOLAR_GT1, "canonical solar >1MW exact", f"actual={canonical_solar}")
    check(canonical_bess == EXPECTED_BESS_GT100, "canonical BESS >100MW exact", f"actual={canonical_bess}")

    validated_at = utc_now()
    metrics = {
        "csv_rows": csv_source_rows,
        "xlsx_rows": xlsx_source_rows,
        "csv_unique_refs": len(csv_refs),
        "xlsx_unique_refs": len(xlsx_refs),
        "csv_native_solar_gt1_rounded": csv_solar,
        "xlsx_canonical_solar_gt1": xlsx_solar,
        "canonical_solar_gt1": canonical_solar,
        "csv_bess_gt100": csv_bess,
        "xlsx_bess_gt100": xlsx_bess,
        "canonical_bess_gt100": canonical_bess,
        "canonical_combined_projects": canonical_solar + canonical_bess,
        "material_mismatches": len(material),
        "representation_differences": representation,
    }
    report = {
        "schema": "globalgrid2050.repd-source-reconciliation.v6",
        "pass": not errors,
        "validated_at": validated_at,
        "csv_url": CSV_URL,
        "xlsx_url": XLSX_URL,
        "xlsx_sheet": xlsx_sheet,
        "source_hashes": {"csv_sha256": sha256(csv_raw), "xlsx_sha256": sha256(xlsx_raw)},
        "capacity_policy": "XLSX precision is canonical; CSV must be within 0.005 MW display-rounding tolerance",
        "text_policy": "trim/collapse whitespace; treat XLSX _x000D_ as whitespace; use the XLSX representation of integer-equivalent planning references and report leading-zero differences",
        "metrics": metrics,
        "checks": checks,
        "representation_difference_examples": representation_examples,
        "material_mismatch_examples": material[:25],
        "errors": errors,
    }
    if RELEASE_DESCRIPTOR:
        report["release_id"] = RELEASE_ID
        report["date_rule"] = DATE_RULE
        report["duplicate_row_policy"] = DUPLICATE_ROW_POLICY
        report["identical_duplicate_rows_dropped"] = duplicate_evidence
    manifest = {
        "schema": "globalgrid2050.repd-manifest.v6",
        "schema_version": 6,
        "status": "VALIDATED" if not errors else "REJECTED",
        "validated_at": validated_at,
        "source_owner": "Department for Energy Security and Net Zero (DESNZ)",
        "source_page": SOURCE_PAGE,
        "source_page_last_updated": PUBLICATION_DATE,
        "source_dataset_title": DATASET_TITLE,
        "source_url": CSV_URL,
        "source_excel_url": XLSX_URL,
        "source_hashes": report["source_hashes"],
        "source_record_count": EXPECTED_ROWS,
        "source_unique_ref_count": EXPECTED_UNIQUE_REFS,
        "canonical_capacity_source": "official XLSX precision, reconciled to official CSV by Ref ID",
        "header_policy": "trim + collapse internal whitespace + case-insensitive aliases",
        "missing_value_policy": "official blanks remain null; no value or date is invented",
        "thresholds": {"solar_mw_exclusive": 1.0, "bess_mw_exclusive": 100.0},
        "canonical_counts": {"solar": EXPECTED_SOLAR_GT1, "bess": EXPECTED_BESS_GT100, "combined": EXPECTED_SOLAR_GT1 + EXPECTED_BESS_GT100},
    }
    if RELEASE_DESCRIPTOR:
        manifest["release_id"] = RELEASE_ID
        manifest["date_rule"] = DATE_RULE
        manifest["duplicate_row_policy"] = DUPLICATE_ROW_POLICY
    return canonical, report, manifest


def run_reconciliation(csv_file: str = "", xlsx_file: str = "", fetch=fetch_bytes) -> tuple[pd.DataFrame, dict, dict]:
    # Offline mode: local official bytes (same hash gates apply); otherwise the injectable fetcher.
    csv_file = csv_file or os.environ.get("REPD_SOURCE_CSV_FILE", "").strip()