GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Grid network and substations / data-grid-gb / pipelines

Build Network Model and 1 more file

A Python command-line tool in the pipelines folder of Data Grid GB that works with rows after header (`rows_after_header`) and branch rows (`branch_rows`) (units: kV, MVA, MVAr).

542 lines of Python from data-grid-gb, in 2 files. Topic: Grid network and substations. Section 316 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. Build Network Model other · data-grid-gb / pipelines/build_network_model.py lines 1–375
  2. Fetch storage · data-grid-gb / ssen-substations/fetch.py lines 1–167

Build Network Model

A Python command-line tool in the pipelines folder of Data Grid GB that works with rows after header (`rows_after_header`) and branch rows (`branch_rows`) (units: kV, MVA, MVAr).

otherdata-grid-gb / pipelines/build_network_model.py · lines 1–375 of 375 · Entry point · pattern cells (shared by 6 files)

What it does
It lists and searches GB transmission network, transformers and interconnectors. Defines 7 functions or classes, including cells, is_header, rows_after_header, number, text. Uses libraries io, json, os, re, collections. Side effects: reads or writes files.
Why it exists in the grid workflow
Domain stage upstream_foresight (voltage levels 400kV, 275kV, 132kV (all 132kV or above)): 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: transformers, interconnectors, 400kV, 275kV, 132kV.
Inputs & outputs
In: JSON file gb-transmission-network.v1.json. Voltage levels named: 400kV, 275kV, 132kV.
Downstream impact
Used directly by 1 file: Refresh Network Products Workflow (data-grid-gb).
What it achieves
It is built from the cells step (`cells`), the is header helper (`is_header`) and the number step (`number`). Parse Electricity Ten Year Statement (ETYS) Appendix B and D into one clean network model. Its code works with 100 MVA, 132 kV and 275 kV.
Why it matters
Run by Refresh Network Products Workflow.
Application
data-grid-gb · pipelines · Python CLI
Uses
io, json, os, re, collections, openpyxl
Used by
run by Refresh Network Products Workflow (workflow run)
Produces
cells, is_header, rows_after_header, number, text, main
Defines here
cells, is_header, rows_after_header, number, text, main, branch_rows
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
data-grid-gb@4a9cef6c7c791f76d416c8b2fbafac8265f7a263 pipelines/build_network_model.py:1-375 · blob e5b9422d6cace5045101430354b355eb0026c758 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: ingest · effects: filesystem · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: supporting_data
"""Parse ETYS Appendix B and D into one clean network model.

WHAT COMES OUT
--------------
derived/gb-transmission-network.v1.json

  sites      every substation the system operator names, with its code,
             its transmission owner and the voltages present there
  nodes      the busbar-level identities that circuits actually connect,
             each carrying its site, its voltage and its fault level where
             the operator publishes one
  circuits   node to node, with length, type, R / X / B on a 100 MVA base
             and seasonal ratings
  transformers  node to node, with impedance and rating
  changes    the circuit and transformer changes the operator has already
             published for 2026/27 to 2033/34, which is the only public
             statement of what the network is about to become
  compensation  reactive plant, because a connection's reactive
             requirement is answered by what is already installed
  interconnectors

WHAT IS NOT DONE HERE
---------------------
No power flow is solved and none is implied. This is the published model's
parameters, restated in a form software can read. A rating is a rating and
a fault level is a fault level; neither is a statement about whether any
particular project can connect anywhere, which depends on queue position,
committed connections, consent and commercial terms that no published
appendix contains.

NODE CODES
----------
A node is a site code of up to four characters, then a digit for the
voltage level, then a suffix identifying the busbar or bay. The digit
convention is not documented in the appendix, so it is DERIVED here by
counting how each digit co-occurs with the voltages its site declares, and
the counts are published in the product rather than asserted. Observed on
the 2025 edition: 1 -> 132 kV, 2 -> 275 kV, 4 -> 400 kV, and a node whose
inferred voltage is not among its site's declared voltages is flagged
rather than silently corrected.

    python pipelines/build_network_model.py
"""

import io
import json
import os
import re
from collections import Counter, defaultdict

HERE = os.path.dirname(os.path.abspath(__file__))
REPO = os.path.dirname(HERE)
SOURCES = os.path.join(REPO, "sources")
DERIVED = os.path.join(REPO, "derived")

EDITION = "2025"
OWNERS = {"a": "SHET", "b": "SPT", "c": "NGET", "d": "OFTO"}
VOLTAGE_DIGIT = {"1": 132, "2": 275, "4": 400}
FAULT_HEADER_KEYS = {
    "Three Phase Initial Peak Current (kA)": "three_phase_initial_peak_current_ka",
    "Three Phase RMS Break Current (kA)": "three_phase_rms_break_current_ka",
    "Symmetrical Three Phase RMS Break Current (kA)": "three_phase_rms_break_current_ka",
    "Three Phase DC Break Current (kA)": "three_phase_dc_break_current_ka",
    "Three Phase Peak Break Current (kA)": "three_phase_peak_break_current_ka",
    "Asymmetrical Three Phase Peak Break Current (kA)": "three_phase_peak_break_current_ka",
    "Single Phase Initial Peak Current (kA)": "single_phase_initial_peak_current_ka",
    "Single Phase RMS Break Current (kA)": "single_phase_rms_break_current_ka",
    "Symmetrical Single Phase RMS Break Current (kA)": "single_phase_rms_break_current_ka",
    "Single Phase DC Break Current (kA)": "single_phase_dc_break_current_ka",
    "Single Phase Peak Break Current (kA)": "single_phase_peak_break_current_ka",
    "Asymmetrical Single Phase Peak Break Current (kA)": "single_phase_peak_break_current_ka",
}
FAULT_KEYS = (
    "three_phase_initial_peak_current_ka",
    "three_phase_rms_break_current_ka",
    "three_phase_dc_break_current_ka",
    "three_phase_peak_break_current_ka",
    "single_phase_initial_peak_current_ka",
    "single_phase_rms_break_current_ka",
    "single_phase_dc_break_current_ka",
    "single_phase_peak_break_current_ka",
)


def cells(worksheet, min_row=1):
    for row in worksheet.iter_rows(min_row=min_row, values_only=True):
        yield row


def is_header(row):
    return any(isinstance(v, str) and v.strip() in ("Node 1", "Node1", "Site Code",
                                                    "Site Name", "OFTO", "Wind Farm",
                                                    "Interconnector Name")
               for v in row if v is not None)


def rows_after_header(worksheet):
    """Yield data rows, skipping the title block and the header line.

    The appendix puts a title in the first rows and the header wherever it
    lands, so the header is found rather than assumed at a fixed offset.
    """
    started = False
    for row in cells(worksheet):
        if not started:
            if is_header(row):
                started = True
            continue
        if all(v in (None, "") for v in row):
            continue
        yield row


def number(value):
    if value in (None, ""):
        return None
    try:
        return round(float(value), 6)
    except (TypeError, ValueError):
        return None


def text(value):
    return None if value in (None, "") else str(value).strip()


def main():
    import openpyxl

    book = openpyxl.load_workbook(
        os.path.join(SOURCES, f"etys-{EDITION}-appendix-b-system-technical-data.xlsx"),
        read_only=True, data_only=True)

    # ── sites ────────────────────────────────────────────────────────────
    sites = {}
    for suffix, owner in OWNERS.items():
        for row in rows_after_header(book[f"B-1-1{suffix}"]):
            values = [v for v in row if v not in (None, "")]
            if len(values) < 3:
                continue
            code, name, kv = text(values[0]), text(values[1]), number(values[-1])
            if not code or not name or kv is None or len(code) > 6:
                continue
            site = sites.setdefault(code.upper(), {
                "code": code.upper(), "name": name,
                "transmission_owner": owner, "voltages_kv": []})
            if int(kv) not in site["voltages_kv"]:
                site["voltages_kv"].append(int(kv))
    for site in sites.values():
        site["voltages_kv"].sort(reverse=True)

    # ── circuits, transformers, changes, compensation ────────────────────
    def branch_rows(sheet, kind, with_year=False, lead_labels=0):
        out = []
        for row in rows_after_header(book[sheet]):
            values = list(row)
            if lead_labels:
                labels = [text(v) for v in values[:lead_labels]]
                values = values[lead_labels:]
            else:
                labels = []
            node1, node2 = text(values[0]), text(values[1])
            if not node1 or not node2:
                continue
            rest = values[2:]
            record = {"node_1": node1.upper(), "node_2": node2.upper()}
            if labels:
                record["labels"] = [l for l in labels if l]
            if with_year:
                record["year"] = text(rest[0])
                record["status"] = text(rest[1])
                rest = rest[2:]
            if kind == "circuit":
                keys = ["ohl_km", "cable_km", "circuit_type", "r_pct_100mva",
                        "x_pct_100mva", "b_pct_100mva", "winter_mva",
                        "spring_mva", "summer_mva", "autumn_mva"]
            else:
                keys = ["r_pct_100mva", "x_pct_100mva", "b_pct_100mva",
                        "rating_mva", "voltage_ratio_kv"]
            for index, key in enumerate(keys):
                if index >= len(rest):
                    break
                value = rest[index]
                record[key] = text(value) if key in ("circuit_type", "voltage_ratio_kv") \
                    else number(value)
            out.append(record)
        return out

    circuits, transformers, changes, compensation = [], [], [], []
    for suffix, owner in OWNERS.items():
        lead = 2 if suffix == "d" else 0
        for record in branch_rows(f"B-2-1{suffix}", "circuit", lead_labels=lead):
            record["transmission_owner"] = owner
            circuits.append(record)
        lead_change = 1 if suffix == "d" else 0
        for record in branch_rows(f"B-2-2{suffix}", "circuit", with_year=True,
                                  lead_labels=lead_change):
            record["transmission_owner"] = owner
            record["asset"] = "circuit"
            changes.append(record)
        lead_tx = 2 if suffix == "d" else 0
        for record in branch_rows(f"B-3-1{suffix}", "transformer", lead_labels=lead_tx):
            record["transmission_owner"] = owner
            transformers.append(record)
        lead_txc = 1 if suffix == "d" else 0
        for record in branch_rows(f"B-3-2{suffix}", "transformer", with_year=True,
                                  lead_labels=lead_txc):
            record["transmission_owner"] = owner
            record["asset"] = "transformer"
            changes.append(record)
        for row in rows_after_header(book[f"B-4-1{suffix}"]):
            values = [v for v in row]
            if suffix == "d":
                values = values[1:]
            name, node = text(values[0]), text(values[1])
            if not node:
                continue
            compensation.append({
                "transmission_owner": owner, "site_name": name,
                "node": node.upper(), "unit": text(values[2]),
                "mvar_generation": number(values[3]),
                "mvar_absorption": number(values[4]),
                "type": text(values[5]),
                "connection_kv": number(values[6]) if len(values) > 6 else None})

    interconnectors = []
    for row in rows_after_header(book["B-5-1"]):
        values = list(row)
        name = text(values[0])
        if not name:
            continue
        interconnectors.append({
            "name": name, "existing": text(values[1]),
            "planned_from_year": text(values[2]),
            "node_1": text(values[3]), "node_2": text(values[4]),
            "type": text(values[5]), "rated_kv": number(values[6]),
            "length_km": number(values[7])})
    book.close()

    # ── nodes, and the derived voltage-digit convention ──────────────────
    digit_counts = defaultdict(Counter)
    node_names = set()
    for record in circuits + transformers + changes:
        node_names.update([record["node_1"], record["node_2"]])
    for node in node_names:
        code, digit = node[:4], (node[4] if len(node) > 4 else "")
        if digit.isdigit() and code in sites:
            for kv in sites[code]["voltages_kv"]:
                digit_counts[digit][kv] += 1

    nodes, unresolved = {}, 0
    for node in sorted(node_names):
        code, digit = node[:4], (node[4] if len(node) > 4 else "")
        site = sites.get(code)
        kv = VOLTAGE_DIGIT.get(digit)
        consistent = bool(site and kv and kv in site["voltages_kv"])
        if not consistent:
            unresolved += 1
        nodes[node] = {
            "node": node, "site_code": code,
            "site_name": site["name"] if site else None,
            "transmission_owner": site["transmission_owner"] if site else None,
            "voltage_kv": kv,
            # Named, not hidden: a node whose inferred voltage is not one the
            # site declares is a fact about the convention, not a defect to
            # paper over.
            "voltage_consistent_with_site": consistent}

    # ── fault levels ─────────────────────────────────────────────────────
    fault_scenarios = []
    for label, filename in (("peak", f"etys-{EDITION}-appendix-d-fault-levels-peak.xlsx"),
                            ("minimum", f"etys-{EDITION}-appendix-d-fault-levels-minimum.xlsx")):
        path = os.path.join(SOURCES, filename)
        if not os.path.exists(path):
            continue
        book = openpyxl.load_workbook(path, read_only=True, data_only=True)
        for sheet_name in book.sheetnames:
            if not re.fullmatch(r"D[123]\.\d", sheet_name):
                continue
            sheet = book[sheet_name]
            title = text(sheet.cell(1, 1).value) or ""
            header = tuple(text(sheet.cell(2, column).value) for column in range(1, 11))
            metric_keys = tuple(FAULT_HEADER_KEYS.get(value) for value in header[2:])
            if (header[:2] != ("Location", "Voltage (kV)")
                    or None in metric_keys or metric_keys != FAULT_KEYS):
                raise ValueError(f"{sheet_name}: unknown Appendix D schema: {header}")
            year_index = re.search(r"\b(?:Yr|Year)\s*(\d+)\b", title, re.I)
            winter = re.search(r"\b(\d{4}/\d{2})\b", title)
            if not year_index or not winter:
                raise ValueError(f"{sheet_name}: scenario missing from title: {title!r}")
            owner = {"1": "SHET", "2": "SPT", "3": "NGET"}[sheet_name[1]]
            owner_sites = {code: site for code, site in sites.items()
                           if site["transmission_owner"] == owner}
            sites_by_name = {site["name"].upper(): code for code, site in owner_sites.items()}
            for source_row, row in enumerate(
                    sheet.iter_rows(min_row=3, max_col=10, values_only=True), start=3):
                if not isinstance(row[0], str) or not isinstance(row[1], (int, float)):
                    continue
                location = " ".join(row[0].split()).upper()
                prefix = location[:4]
                site_code = prefix if prefix in owner_sites else sites_by_name.get(location)
                record = {
                    "demand_case": label, "transmission_owner": owner,
                    "scenario_year_index": int(year_index.group(1)),
                    "winter": winter.group(1), "location": location,
                    "site_code": site_code, "voltage_kv": float(row[1]),
                    "source_sheet": sheet_name, "source_row": source_row,
                    "published_metric_labels": list(header[2:]),
                }
                for key, value in zip(metric_keys, row[2:]):
                    if not isinstance(value, (int, float)):
                        raise ValueError(f"{sheet_name}/{source_row}: nonnumeric {key}")
                    record[key] = float(value)
                fault_scenarios.append(record)
        book.close()
    fault_scenarios.sort(key=lambda row: (
        row["demand_case"], row["transmission_owner"], row["winter"],
        row["location"], row["voltage_kv"], row["source_sheet"], row["source_row"]))

    product = {
        "schema": "data-grid-gb.transmission-network.v1",
        "source": {
            "publisher": "NESO",
            "publication": f"Electricity Ten Year Statement {EDITION}",
            "appendices": ["B - system technical data",
                           "D - fault levels (peak and minimum)"],
            "note": "parameters as published; no power flow is solved here",
        },
        "not_a_connection_assessment": (
            "A rating is a rating and a fault level is a fault level. Neither "
            "states whether any project can connect at a node, which depends "
            "on queue position, committed connections, consent and commercial "
            "terms that no published appendix contains."),
        "node_code_convention": {
            "form": "site code (up to 4 characters) + voltage digit + busbar or bay suffix",
            "voltage_digit": VOLTAGE_DIGIT,
            "derived_not_documented": True,
            "observed_digit_to_site_voltage_counts": {
                digit: dict(counter.most_common(4))
                for digit, counter in sorted(digit_counts.items())},
            "nodes_whose_voltage_is_not_declared_by_their_site": unresolved,
        },
        "counts": {
            "sites": len(sites), "nodes": len(nodes), "circuits": len(circuits),
            "transformers": len(transformers), "planned_changes": len(changes),
            "reactive_compensation_units": len(compensation),
            "interconnectors": len(interconnectors),
            "published_fault_current_scenarios": len(fault_scenarios),
            "fault_current_scenarios_with_site_code":
                sum(1 for row in fault_scenarios if row["site_code"]),
        },
        "sites": sorted(sites.values(), key=lambda s: s["code"]),
        "nodes": [nodes[k] for k in sorted(nodes)],
        "circuits": circuits,
        "transformers": transformers,
        "planned_changes": changes,
        "reactive_compensation": compensation,
        "interconnectors": interconnectors,
        "fault_current_metrics": list(FAULT_KEYS),
        "fault_current_scenarios": fault_scenarios,
    }

    os.makedirs(DERIVED, exist_ok=True)
    out = os.path.join(DERIVED, "gb-transmission-network.v1.json")
    io.open(out, "w", encoding="utf-8", newline="\n").write(
        json.dumps(product, ensure_ascii=False, indent=1) + "\n")
    size = os.path.getsize(out) / 1024 / 1024
    print(f"wrote derived/gb-transmission-network.v1.json ({size:.1f} MB)")
    for key, value in product["counts"].items():
        print(f"  {key:<44} {value:>7,}")
    return 0


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

Fetch

A Python command-line tool in the SSEN substations folder of Data Grid GB that fetches SSEN substations, using SHA-256 from SSEN.

storagedata-grid-gb / ssen-substations/fetch.py · lines 1–167 of 167 · Entry point · pattern metadata (shared by 2 files)

What it does
It fetches SSEN substations. Defines request, metadata, sha, main. Uses libraries argparse, csv, datetime, hashlib, json. Side effects: reads or writes files; makes network requests; uses storage (browser storage, DuckDB or Parquet).
Why it exists in the grid workflow
Domain stage upstream_foresight (path word 'ssen'): 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: data source: SSEN; Parquet file ssen_substations.parquet; DuckDB queries; command-line arguments. Out: Parquet file.
Downstream impact
Used directly by 1 file: SSEN Substations Workflow (data-grid-gb).
What it achieves
It is built from the request step (`request`), the metadata step (`metadata`) and the sha step (`sha`). Retrieve official SSEN CKAN resource without authentication; fail closed. It accepts `--output`.
Why it matters
Run by SSEN Substations Workflow.
Application
data-grid-gb · ssen-substations · Python CLI
Uses
argparse, csv, datetime, hashlib, json, math, pathlib, time
Used by
run by SSEN Substations Workflow (workflow run)
Produces
request, metadata, sha, main
Defines here
request, metadata, sha, main
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
data-grid-gb@4a9cef6c7c791f76d416c8b2fbafac8265f7a263 ssen-substations/fetch.py:1-167 · blob 62cf6561d96ea1759704de2c74bcdea10e480341 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: filesystem, network, storage · domain stage: upstream_foresight · users: IPP_developer, ICP_grid_engineer · canonical: supporting_data
"""Retrieve official SSEN CKAN resource without authentication; fail closed."""
import argparse
import csv
import datetime as dt
import hashlib
import json
import math
import pathlib
import time
import urllib.parse
import urllib.request
import urllib.error
import tempfile

RESOURCE = "336d9720-353c-49d3-8415-11e1cf4a85b9"
HOST = "https://ckan-prod.sse.datopian.com/api/3/action/"
CATALOGUE = "https://data.ssen.co.uk/dataset/ssen-substation-data"
REQUIRED = {"owner_name", "type", "class", "number", "status", "data_confidence",
            "fence_type", "operating_area", "locality", "location_x_m",
            "location_y_m", "location_x_mm", "location_y_mm"}


def request(action, **params):
    url = HOST + action + "?" + urllib.parse.urlencode(params)
    for attempt in range(4):
        try:
            req = urllib.request.Request(url, headers={"Accept": "application/json",
                "User-Agent": "Ventus-DataGridGB/1.0 (public-data acquisition)"})
            with urllib.request.urlopen(req, timeout=90) as response:
                result = json.load(response)
            if not result.get("success"):
                raise RuntimeError(f"CKAN rejected {action}: {result.get('error')}")
            return result["result"]
        except urllib.error.HTTPError as e:
            if e.code not in (429, 500, 502, 503, 504) or attempt == 3:
                raise
        except (urllib.error.URLError, TimeoutError):
            if attempt == 3:
                raise
        time.sleep(2 ** attempt)


def metadata():
    try:
        info = request("resource_show", id=RESOURCE)
        if info.get("id") != RESOURCE:
            raise RuntimeError("Resource metadata identity mismatch")
        return {k: info.get(k) for k in
                ("id", "name", "last_modified", "datastore_last_updated", "hash", "sha256", "url", "package_id")}
    except urllib.error.HTTPError as e:
        if e.code not in (401, 403, 404):
            raise
        return {"id": RESOURCE, "metadata_unavailable_http": e.code}


def sha(path):
    with path.open("rb") as file:
        return hashlib.file_digest(file, "sha256").hexdigest()


def main():
    import duckdb
    parser = argparse.ArgumentParser()
    parser.add_argument("--output", required=True, type=pathlib.Path)
    args = parser.parse_args()
    out = args.output
    if out.exists():
        raise RuntimeError("Refusing to overwrite output")
    before = metadata()
    first = request("datastore_search", resource_id=RESOURCE, limit=1, sort="_id asc")
    fields = first["fields"]
    names = [f["id"] for f in fields]
    if len(names) != len(set(names)) or not REQUIRED.issubset(names) or "_id" not in names:
        raise RuntimeError(f"Unexpected provider schema: {names}")
    expected = first["total"]
    if not isinstance(expected, int) or not 0 < expected <= 2_000_000 or first.get("total_was_estimated"):
        raise RuntimeError("No exact bounded positive total")
    print(f"SSEN source rows reported: {expected}", flush=True)
    with tempfile.TemporaryDirectory() as work:
        raw = pathlib.Path(work) / "records.ndjson"
        seen = set()
        previous = -1
        with raw.open("w", encoding="utf-8", newline="\n") as file:
            offset = 0
            while offset < expected:
                page = request("datastore_search", resource_id=RESOURCE, limit=5000, offset=offset, sort="_id asc")
                if page.get("total") != expected or page.get("total_was_estimated") or page["fields"] != fields:
                    raise RuntimeError("Source changed during retrieval")
                records = page["records"]
                if not records or len(records) > expected-offset:
                    raise RuntimeError("Incomplete pagination")
                for record in records:
                    if set(record) != set(names):
                        raise RuntimeError("Record schema changed")
                    identity = record["_id"]
                    if not isinstance(identity, int) or identity <= previous or identity in seen:
                        raise RuntimeError("Invalid, repeated or unordered CKAN identity")
                    previous = identity
                    seen.add(identity)
                    file.write(json.dumps(record, ensure_ascii=False, allow_nan=False, sort_keys=True, separators=(",", ":")) + "\n")
                offset += len(records)
                if offset % 25000 == 0 or offset == expected:
                    print(f"Fetched {offset}/{expected}", flush=True)
                time.sleep(0.2)
        final = request("datastore_search", resource_id=RESOURCE, limit=1, sort="_id asc")
        after = metadata()
        if before != after or final["total"] != expected or final["fields"] != fields or final["records"] != first["records"]:
            raise RuntimeError("Source changed during capture")
        if len(seen) != expected:
            raise RuntimeError("Row closure failed")
        # Use explicit CKAN types, preserving text IDs and leading zeros.
        sqltypes = {"int": "BIGINT", "int4": "BIGINT", "int8": "BIGINT", "integer": "BIGINT",
                    "bigint": "BIGINT", "numeric": "DOUBLE", "float": "DOUBLE",
                    "float8": "DOUBLE", "double": "DOUBLE", "double precision": "DOUBLE",
                    "text": "VARCHAR", "string": "VARCHAR", "varchar": "VARCHAR",
                    "bool": "BOOLEAN", "boolean": "BOOLEAN", "timestamp": "VARCHAR",
                    "date": "VARCHAR"}
        columns = {}
        for field in fields:
            kind = field["type"].lower()
            if kind not in sqltypes:
                raise RuntimeError(f"Unrecognised CKAN type: {kind}")
            columns[field["id"]] = sqltypes[kind]
        db = duckdb.connect()
        db.execute("SET threads=1")
        db.execute("CREATE TABLE source AS SELECT * FROM read_json(?, format='newline_delimited', columns=?)", [str(raw), columns])
        count, unique, unplaced = db.execute(
            "SELECT count(*), count(DISTINCT _id), count(*) FILTER (WHERE location_x_m IS NULL OR location_y_m IS NULL OR NOT isfinite(location_x_m) OR NOT isfinite(location_y_m)) FROM source").fetchone()
        if count != expected or unique != expected:
            raise RuntimeError("Parquet input row closure failed")
        # Spatial locality only; source CRS is not inferred by this acquisition.
        out.mkdir(parents=True)
        parquet = out / "ssen_substations.parquet"
        ordered = """SELECT * FROM source ORDER BY
            floor(location_x_m / 10000) NULLS LAST,
            floor(location_y_m / 10000) NULLS LAST, _id"""
        db.execute("COPY (" + ordered + ") TO '" + str(parquet).replace("'", "''") +
                   "' (FORMAT PARQUET, COMPRESSION ZSTD, ROW_GROUP_SIZE 8192)")
        actual = db.execute("SELECT count(*), count(DISTINCT _id) FROM read_parquet(?)", [str(parquet)]).fetchone()
        if actual != (expected, expected):
            raise RuntimeError("Written Parquet closure failed")
        # Full bidirectional row comparison verifies no changed/dropped data.
        mismatches = db.execute(
            "SELECT count(*) FROM ((SELECT * FROM source EXCEPT ALL SELECT * FROM read_parquet(?)) UNION ALL (SELECT * FROM read_parquet(?) EXCEPT ALL SELECT * FROM source))",
            [str(parquet), str(parquet)]).fetchone()[0]
        if mismatches:
            raise RuntimeError("Parquet contents differ from source")
        owners = db.execute("SELECT owner_name,count(*) FROM source GROUP BY 1 ORDER BY 1").fetchall()
        manifest = {"schema": "data-grid-gb.ssen-substations.v1",
            "publisher": "SSEN Distribution", "catalogue": CATALOGUE,
            "resource_id": RESOURCE, "api": HOST + "datastore_search",
            "licence": "CC BY 4.0", "licence_url": "https://creativecommons.org/licenses/by/4.0/",
            "retrieved_utc": dt.datetime.now(dt.timezone.utc).isoformat(),
            "source_metadata": before, "source_fields": fields, "source_rows": expected,
            "distinct_ckan_rows": unique, "unplaced_or_nonfinite_rows": unplaced,
            "owner_counts": owners, "source_ndjson_sha256": sha(raw),
            "coordinate_note": "Original provider X/Y metre and millimetre coordinates; CRS not independently confirmed.",
            "row_order": "10km source-coordinate cells, then CKAN _id",
            "acquisition_note": "Ordered pagination and beginning/end checks; no transaction-wide snapshot guarantee from CKAN.",
            "artifacts": [{"path": parquet.name, "bytes": parquet.stat().st_size, "sha256": sha(parquet)}],
            "verification": {"row_count": actual[0], "distinct_ids": actual[1], "bidirectional_difference_rows": mismatches}}
        (out / "manifest.json").write_text(json.dumps(manifest, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
        print(json.dumps(manifest, ensure_ascii=False), flush=True)


if __name__ == "__main__":
    main()