GLOBALGRID2050 / EXPLORE CODE

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

Build Renewable Energy Planning Database (REPD) Datacentres (part 1)

A Python command-line tool in the convergence folder of GlobalGrid2050 that reads datacentre proximity, colocation candidates, rollup region and rollup cluster, using EPSG:27700 and SHA-256.

700 lines of Python from globalgrid2050, in 1 file. Topic: Electricity markets and data pipelines. Section 771 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 Renewable Energy Planning Database (REPD) Datacentres geodesy · globalgrid2050 / automation/convergence/build_repd_datacentres.py lines 1–700

Build Renewable Energy Planning Database (REPD) Datacentres

A Python command-line tool in the convergence folder of GlobalGrid2050 that reads datacentre proximity, colocation candidates, rollup region and rollup cluster, using EPSG:27700 and SHA-256.

geodesyglobalgrid2050 / automation/convergence/build_repd_datacentres.py · lines 1–700 of 1063 · Entry point · pattern norm_operator (shared by 2 files)

What it does
It loads datacentre proximity, colocation candidates, rollup region and rollup cluster. Defines 47 functions or classes, including _tokens, _op_core, norm_operator, operator_keys, site_tokens. Uses libraries __future__, argparse, csv, hashlib, io. Side effects: reads or writes files; starts processes.
Why it exists in the grid workflow
Domain stage upstream_foresight (path word 'datacentres'): 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: 132kV.
Inputs & outputs
In: JSON file datacentre_proximity.json; JSON file colocation_candidates.json; JSON file rollup_region.json; JSON file rollup_cluster.json; JSON file schema.json; command-line arguments. Out: JSON file. Coordinate systems named: OSGB36 / EPSG:27700 (British National Grid), WGS84 / EPSG:4326. Voltage levels named: 132kV.
Downstream impact
Used directly by 1 file: Test Build Renewable Energy Planning Database (REPD) Datacentres (globalgrid2050). Reached from 1 other entry point, including GlobalGrid2050 Project Signals Acid Workflow (globalgrid2050).
What it achieves
It parses capacity (`parse_capacity`), parses coord (`parse_coord`) and loads REPD (`load_repd`). REPD x UK data-centre convergence: one deterministic, offline join. Its code works with 132 kV. It accepts `--repd`, `--identity` and `--datacentres`.
Why it matters
Checked by Test Build Renewable Energy Planning Database (REPD) Datacentres.
Application
globalgrid2050 · automation/convergence · Python CLI
Uses
__future__, argparse, csv, hashlib, io, json, math, re
Used by
imported by Test Build Renewable Energy Planning Database (REPD) Datacentres (sys.path import)
Produces
norm_operator, operator_keys, site_tokens, bng_to_wgs84, haversine_km, r3, msum, sha256_file, parse_capacity, parse_coord, load_repd, load_datacentres
Defines here
_tokens, _op_core, norm_operator, operator_keys, site_tokens, bng_to_wgs84, haversine_km, r3, msum, sha256_file, parse_capacity, parse_coord, load_repd, load_datacentres, mw, load_connection_points, _max_kv, load_osm_substations, GridIndex, __init__, _cell, nearest, empty_cells, add_cell, close_cells, project_brief, node_brief, dc_brief, infer_region, country_of
Cannot conclude
Whether this code runs, is used by a live app today, or is correct. This is a static reading.
Evidence
globalgrid2050@0aca5121f0795e469b6cf4fc3d3b628edba8d883 automation/convergence/build_repd_datacentres.py:1-700 · blob 12fbe3b0d728fc7858a24cbb79e81bcf3b903fe2 · GitHub at these lines
State
achieves: comment · 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
"""REPD x UK data-centre convergence: one deterministic, offline join.

Inputs (read only, pinned by SHA-256 in the manifest this writes):
  --repd               cleaned REPD register CSV (official 53 columns; X/Y are British National Grid metres)
  --identity           project_identity_<release>.json (V6 identity; gives gg_project_id per REPD Ref ID)
  --datacentres        uk_datacentre_pipeline_<stamp>.geojson (the GridAtlas / data-grid-gb DC-A7 baseline)
  --connection-points  data-grid-gb derived/connection-points.v3.json (NESO ETYS nodes >= 132 kV; OSM-joined points)
  --osm-substations    optional grid_substations.geojson (OpenStreetMap; only nodes >= 132 kV are used)

Outputs (in --out): JSON and CSV for
  datacentre_proximity      one row per data centre: REPD MW and counts by technology x status x radius,
                            nearest project per technology, nearest transmission node and the REPD projects
                            that share it as their own nearest node
  repd_datacentre_proximity one row per REPD solar / battery / wind project with usable coordinates: nearest
                            data centres and data-centre counts and stated MW within the same radii
  colocation_candidates     same-site / same-operator pairs between the two registers, each with an evidence class
  rollup_region, rollup_cluster  regional and data-centre-cluster roll-ups
plus manifest.json (input and output hashes, parameters, counts) and schema.json.

Rules carried from the source contracts:
  * blank REPD capacity is unknown, never zero: MW sums are over stated values and the count of projects with
    unknown capacity is reported beside every sum;
  * data-centre IT load MW and grid connection MW are separate and never derived from each other; the baseline's
    unclassified_mw is NOT summed as demand (it mixes facility, zone-ambition and even battery figures) and is only
    passed through with its source label;
  * distance is straight line (haversine on WGS84). Proximity is not connection, capacity or feasibility; the
    "shared nearest transmission node" is a screening grouping, not a statement about any feeder or connection.
  * REPD refs with known wrong coordinates are excluded from geography and listed in the manifest.
No network access, no clock: identical inputs give byte-identical outputs.
"""
from __future__ import annotations

import argparse
import csv
import hashlib
import io
import json
import math
import re
import sys
from collections import Counter, defaultdict
from pathlib import Path

TOOL_VERSION = "1.0.0"
SCHEMA_ID = "globalgrid2050.convergence.repd-datacentres.v1"

RADII_KM = (5, 10, 25, 50)
TECH_GROUPS = {
    "Solar Photovoltaics": "solar",
    "Battery": "battery",
    "Wind Onshore": "wind_onshore",
    "Wind Offshore": "wind_offshore",
}
TECH_ORDER = ("solar", "battery", "wind_onshore", "wind_offshore")
# REPD "Development Status (short)" -> convergence status group. Everything else (refused, withdrawn, abandoned,
# expired, revised, decommissioned, no application required/made) is outside the live pipeline and not counted.
STATUS_GROUPS = {
    "Operational": "operational",
    "Under Construction": "under_construction",
    "Awaiting Construction": "consented",
    "Application Submitted": "in_planning",
    "Appeal Lodged": "in_planning",
}
STATUS_ORDER = ("operational", "under_construction", "consented", "in_planning")
# Known wrong points (docs/repd/AUTOMATION.md "Wrong points"): Salters Lane, Sunderland, plotted in southern England.
BAD_COORD_REFS = frozenset({"18561", "18562"})
BNG_X_RANGE = (0.0, 700000.0)
BNG_Y_RANGE = (0.0, 1300000.0)
UK_LAT = (49.0, 61.5)   # WGS84 bounds a UK data-centre point must fall in
UK_LON = (-11.0, 2.5)
# Data-centre location precisions whose point is good enough for km-scale proximity. "region" points are regional
# centroids (AI Growth Zones and similar) and "unknown" has no point: they keep a row but take no part in
# per-project radii, clusters or same-site candidates.
PROXIMITY_PRECISIONS = frozenset({"exact_site", "settlement"})
NEAREST_DC_LIST = 3
CLUSTER_LINK_KM = 10.0
CLUSTER_MIN_SITES = 3
REGION_VOTE_K = 15
SAME_SITE_KM = 0.5
SAME_SITE_OPERATOR_KM = 1.0
NAME_MATCH_KM = 2.0
NAME_TOKEN_MAX_DF = 10
CLUSTER_TOP_PROJECTS = 5
CLUSTER_TOP_RADIUS_KM = 25
PROJECT_PARTS = (("solar", ("solar",)), ("battery", ("battery",)), ("wind", ("wind_onshore", "wind_offshore")))
MAX_FILE_BYTES = 5 * 1024 * 1024
EARTH_R_KM = 6371.0088  # IUGG mean radius, as data-grid-gb dcmerge.haversine_km

REGION_COUNTRY = {"Scotland": "Scotland", "Wales": "Wales", "Northern Ireland": "Northern Ireland",
                  "Offshore": "Offshore"}

CAVEATS = [
    "Straight-line distance between published points; proximity is not connection, capacity or feasibility.",
    "The shared nearest transmission node is a screening grouping of points nearest the same NESO ETYS node; it says nothing about any feeder, connection offer or queue position.",
    "Blank REPD capacity is unknown, never zero: MW sums are over stated values; unknown-capacity counts are given beside them.",
    "Data-centre IT load MW and grid connection MW are separate source figures and never derived from each other; unclassified_mw is not summed as demand.",
    "A data-centre pipeline entry is not a commitment; data-centre status is the baseline's own status field.",
    "REPD status groups: operational = Operational; under_construction = Under Construction; consented = Awaiting Construction; in_planning = Application Submitted or Appeal Lodged.",
    "REPD refs 18561 and 18562 carry known wrong coordinates and are excluded from all geography.",
    "Live REPD records repeating one site (same technology, site name, stated capacity and point) are counted once: the most advanced status, then the highest Ref ID; the manifest lists every collapsed ref.",
    "A data-centre point outside UK WGS84 bounds is treated as unplaced (never re-projected or guessed).",
]

# ---------------------------------------------------------------------------------------------- normalisers
# Copied from data-grid-gb datacentres/lanes/dc-a7-merge/dcmerge.py (main 01a4491) so operator matching here is
# the same strict rule the data-centre merge uses: tokens, corporate stop words removed, parenthesised gloss
# dropped, exact equality only (no subset or fuzzy matching).
_OP_STOP = {"ltd", "limited", "plc", "llp", "llc", "inc", "uk", "group", "holdings", "holding",
            "europe", "emea", "data", "centre", "centres", "center", "centers", "services",
            "the", "co", "company", "gb", "international", "and"}
# Site-name tokens too common to be evidence of the same site on their own.
_SITE_STOP = {"land", "at", "of", "and", "the", "north", "south", "east", "west", "road", "lane", "farm", "park",
              "site", "street", "near", "adjacent", "to", "former", "new", "old", "rear", "off", "estate",
              "industrial", "business", "centre", "center", "data", "datacentre", "datacenter", "campus", "uk",
              "ltd", "limited", "solar", "battery", "storage", "energy", "bess", "pv", "wind", "power", "station",
              "project", "scheme", "development", "extension", "phase", "plot", "unit", "units", "building",
              "house", "way", "close", "drive", "avenue", "court", "hill", "green", "great", "little", "upper",
              "lower", "st", "ltd", "facility", "proposed", "scheme", "system", "systems", "renewable", "plant",
              "a", "an", "in", "on", "by", "for", "with", "from", "s", "nr", "rd", "la"}
DC_TEXT = re.compile(r"\bdata\s*cent(re|er)s?\b|\bdatacent(re|er)s?\b", re.I)


def _tokens(s):
    s = (s or "").lower().replace("&", " and ")
    s = re.sub(r"[^a-z0-9]+", " ", s)
    return s.split()


def _op_core(s):
    return re.sub(r"\([^)]*\)", " ", s or "")


def norm_operator(s):
    return " ".join(t for t in _tokens(_op_core(s)) if t not in _OP_STOP)


def operator_keys(s):
    """Strict keys of an operator string; a ';'-separated list names several operators."""
    out = []
    for part in (s or "").split(";"):
        k = norm_operator(part)
        if len(k.replace(" ", "")) >= 4 and k not in out:
            out.append(k)
    return out


def site_tokens(s):
    return {t for t in _tokens(s) if len(t) >= 3 and t not in _SITE_STOP and not t.isdigit()}


# ---------------------------------------------------------------------------------------------- geodesy
def bng_to_wgs84(easting, northing):
    """OSGB36 British National Grid (EPSG:27700) -> WGS84 lat/lon, Ordnance Survey formulae and the published
    7-parameter Helmert (about 5 m accuracy, ample for km radii; no OSTN15 grid, so the result is the same on
    every machine)."""
    a, b = 6377563.396, 6356256.909
    f0 = 0.9996012717
    lat0, lon0 = math.radians(49.0), math.radians(-2.0)
    n0, e0 = -100000.0, 400000.0
    e2 = 1 - (b * b) / (a * a)
    n = (a - b) / (a + b)
    n2, n3 = n * n, n * n * n
    lat, m = lat0, 0.0
    while True:
        lat = (northing - n0 - m) / (a * f0) + lat
        ma = (1 + n + 1.25 * n2 + 1.25 * n3) * (lat - lat0)
        mb = (3 * n + 3 * n2 + 2.625 * n3) * math.sin(lat - lat0) * math.cos(lat + lat0)
        mc = (1.875 * n2 + 1.875 * n3) * math.sin(2 * (lat - lat0)) * math.cos(2 * (lat + lat0))
        md = (35 / 24) * n3 * math.sin(3 * (lat - lat0)) * math.cos(3 * (lat + lat0))
        m = b * f0 * (ma - mb + mc - md)
        if abs(northing - n0 - m) < 0.00001:
            break
    sl, cl, tl = math.sin(lat), math.cos(lat), math.tan(lat)
    nu = a * f0 / math.sqrt(1 - e2 * sl * sl)
    rho = a * f0 * (1 - e2) / (1 - e2 * sl * sl) ** 1.5
    eta2 = nu / rho - 1
    t2, t4, t6 = tl * tl, tl ** 4, tl ** 6
    vii = tl / (2 * rho * nu)
    viii = tl / (24 * rho * nu ** 3) * (5 + 3 * t2 + eta2 - 9 * t2 * eta2)
    ix = tl / (720 * rho * nu ** 5) * (61 + 90 * t2 + 45 * t4)
    x = 1 / cl / nu
    xi = 1 / cl / (6 * nu ** 3) * (nu / rho + 2 * t2)
    xii = 1 / cl / (120 * nu ** 5) * (5 + 28 * t2 + 24 * t4)
    xiia = 1 / cl / (5040 * nu ** 7) * (61 + 662 * t2 + 1320 * t4 + 720 * t6)
    de = easting - e0
    phi = lat - vii * de ** 2 + viii * de ** 4 - ix * de ** 6
    lam = lon0 + x * de - xi * de ** 3 + xii * de ** 5 - xiia * de ** 7
    # OSGB36 geodetic -> cartesian (Airy 1830)
    h = 0.0
    sp, cp = math.sin(phi), math.cos(phi)
    nu1 = a / math.sqrt(1 - e2 * sp * sp)
    x1 = (nu1 + h) * cp * math.cos(lam)
    y1 = (nu1 + h) * cp * math.sin(lam)
    z1 = ((1 - e2) * nu1 + h) * sp
    # Helmert OSGB36 -> WGS84
    tx, ty, tz = 446.448, -125.157, 542.060
    s = -20.4894e-6
    rx, ry, rz = (math.radians(v / 3600) for v in (0.1502, 0.2470, 0.8421))
    x2 = tx + (1 + s) * x1 - rz * y1 + ry * z1
    y2 = ty + rz * x1 + (1 + s) * y1 - rx * z1
    z2 = tz - ry * x1 + rx * y1 + (1 + s) * z1
    # cartesian -> WGS84 geodetic (GRS80/WGS84 ellipsoid)
    a2, b2 = 6378137.0, 6356752.314245
    e22 = 1 - (b2 * b2) / (a2 * a2)
    p = math.sqrt(x2 * x2 + y2 * y2)
    phi2 = math.atan2(z2, p * (1 - e22))
    for _ in range(10):
        nu2 = a2 / math.sqrt(1 - e22 * math.sin(phi2) ** 2)
        phi2 = math.atan2(z2 + e22 * nu2 * math.sin(phi2), p)
    lam2 = math.atan2(y2, x2)
    return round(math.degrees(phi2), 6), round(math.degrees(lam2), 6)


def haversine_km(lat1, lon1, lat2, lon2):
    p1, p2 = math.radians(lat1), math.radians(lat2)
    dp, dl = p2 - p1, math.radians(lon2 - lon1)
    h = math.sin(dp / 2) ** 2 + math.cos(p1) * math.cos(p2) * math.sin(dl / 2) ** 2
    return 2 * EARTH_R_KM * math.asin(min(1.0, math.sqrt(h)))


def r3(v):
    return None if v is None else round(v + 0.0, 3)


def msum(vals):
    return round(math.fsum(sorted(vals)), 3)


# ---------------------------------------------------------------------------------------------- loaders
def sha256_file(p):
    h = hashlib.sha256()
    with open(p, "rb") as fh:
        for chunk in iter(lambda: fh.read(1 << 20), b""):
            h.update(chunk)
    return h.hexdigest()


def parse_capacity(raw):
    s = (raw or "").strip().replace(",", "")
    if not s:
        return None
    try:
        v = float(s)
    except ValueError:
        return None
    return v if math.isfinite(v) and v >= 0 else None


def parse_coord(raw):
    s = (raw or "").strip().replace(",", "")
    if not s:
        return None
    try:
        v = float(s)
    except ValueError:
        return None
    return v if math.isfinite(v) else None


def load_repd(path, identity_path=None):
    gg = {}
    if identity_path:
        ident = json.loads(Path(identity_path).read_text(encoding="utf-8"))
        for r in ident.get("records", []):
            if r.get("repd_ref"):
                gg[str(r["repd_ref"])] = r.get("gg_project_id")
    rows, excluded = [], []
    with open(path, encoding="utf-8-sig", newline="") as fh:
        for raw in csv.DictReader(fh):
            ref = (raw.get("Ref ID") or "").strip()
            tech_raw = (raw.get("Technology Type") or "").strip()
            status_raw = (raw.get("Development Status (short)") or "").strip()
            x, y = parse_coord(raw.get("X-coordinate")), parse_coord(raw.get("Y-coordinate"))
            lat = lon = None
            why = None
            if ref in BAD_COORD_REFS:
                why = "known_wrong_coordinates"
            elif x is None or y is None:
                why = "no_coordinates"
            elif abs(x) <= 180 and abs(y) <= 90:
                why = "coordinates_look_like_degrees_not_bng"
            elif not (BNG_X_RANGE[0] <= x <= BNG_X_RANGE[1] and BNG_Y_RANGE[0] <= y <= BNG_Y_RANGE[1]):
                why = "coordinates_outside_bng_range"
            else:
                lat, lon = bng_to_wgs84(x, y)
            region = (raw.get("Region") or "").strip()
            country = (raw.get("Country") or "").strip()
            rec = {
                "ref": ref,
                "gg_project_id": gg.get(ref),
                "site_name": (raw.get("Site Name") or "").strip(),
                "operator": (raw.get("Operator (or Applicant)") or "").strip(),
                "address": (raw.get("Address") or "").strip(),
                "technology": tech_raw,
                "tech_group": TECH_GROUPS.get(tech_raw),
                "status": status_raw,
                "status_group": STATUS_GROUPS.get(status_raw),
                "capacity_mw": parse_capacity(raw.get("Installed Capacity (MWelec)")),
                "region": region or None,
                "country": country or None,
                "lat": lat,
                "lon": lon,
            }
            if why and rec["tech_group"]:
                excluded.append({"ref": ref, "technology": tech_raw, "status": status_raw, "reason": why})
            rows.append(rec)
    return rows, excluded


def load_datacentres(path):
    gj = json.loads(Path(path).read_text(encoding="utf-8"))
    out = []
    for f in gj["features"]:
        p = f["properties"]
        g = f.get("geometry")
        lat = lon = None
        geo_issue = None
        if g and g.get("type") == "Point" and g.get("coordinates"):
            lon, lat = float(g["coordinates"][0]), float(g["coordinates"][1])
            if not (UK_LAT[0] <= lat <= UK_LAT[1] and UK_LON[0] <= lon <= UK_LON[1]):
                # swapped axes or a projected CRS read as degrees: never guess, treat as unplaced
                geo_issue = "outside_uk_wgs84_bounds"
                lat = lon = None
        loc = p.get("location") or {}
        precision = loc.get("precision") or ("unknown" if lat is None else "unstated")
        if geo_issue:
            precision = "rejected"

        def mw(key):
            o = p.get(key) or {}
            return {"value": o.get("value"), "label": o.get("label"), "source_id": o.get("source_id")}

        out.append({
            "id": p.get("id") or f.get("id"),
            "name": p.get("name"),
            "status": p.get("status") or "unknown",
            "operator": p.get("operator"),
            "lat": lat, "lon": lon,
            "precision": precision,
            "proximity_reliable": lat is not None and precision in PROXIMITY_PRECISIONS,
            "it_load_mw": mw("it_load_mw"),
            "grid_connection_mw": mw("grid_connection_mw"),
            "unclassified_mw": mw("unclassified_mw"),
            "licences": sorted({s.get("licence") for s in p.get("sources", []) if s.get("licence")}),
            "geometry_issue": geo_issue,
        })
    out.sort(key=lambda d: d["id"])
    return out, gj.get("metadata", {})


def load_connection_points(path):
    d = json.loads(Path(path).read_text(encoding="utf-8"))
    nodes = []
    for c in d.get("connection_points", []):
        loc = c.get("location") or {}
        if loc.get("lat") is None or loc.get("lon") is None:
            continue
        nodes.append({"site_code": c["site_code"], "name": c.get("name"),
                      "transmission_owner": c.get("transmission_owner"),
                      "voltages_kv": c.get("voltages_kv") or [],
                      "lat": float(loc["lat"]), "lon": float(loc["lon"])})
    nodes.sort(key=lambda n: n["site_code"])
    return nodes, {"schema": d.get("schema"), "connection_points": len(d.get("connection_points", [])),
                   "located": len(nodes)}


def _max_kv(v):
    best = 0
    for part in str(v or "").replace(",", ";").split(";"):
        part = part.strip()
        if part.isdigit():
            best = max(best, int(part))
    return best // 1000


def load_osm_substations(path, min_kv=132):
    d = json.loads(Path(path).read_text(encoding="utf-8"))
    out = []
    for f in d.get("features", []):
        g = f.get("geometry") or {}
        if g.get("type") != "Point":
            continue
        p = f.get("properties") or {}
        kv = _max_kv(p.get("voltage"))
        if kv < min_kv:
            continue
        lon, lat = g["coordinates"][:2]
        out.append({"name": p.get("name"), "operator": p.get("operator"), "max_kv": kv,
                    "lat": float(lat), "lon": float(lon)})
    out.sort(key=lambda s: (s["lat"], s["lon"], s["name"] or ""))
    return out, {"features": len(d.get("features", [])), "used_min_kv": min_kv, "used": len(out)}


# ---------------------------------------------------------------------------------------------- spatial index
class GridIndex:
    """Bucket points by 0.5 degree cells; exact nearest-n by expanding rings, deterministic tie-break."""

    CELL = 0.5

    def __init__(self, items):
        self.items = items
        self.cells = defaultdict(list)
        for i, it in enumerate(items):
            self.cells[self._cell(it["lat"], it["lon"])].append(i)

    def _cell(self, lat, lon):
        return (math.floor(lat / self.CELL), math.floor(lon / self.CELL))

    def nearest(self, lat, lon, n=1, key=lambda it: ""):
        ci, cj = self._cell(lat, lon)
        found = []
        ring = 0
        km_per_cell = 111.0 * self.CELL * max(0.3, math.cos(math.radians(min(abs(lat) + 1, 89))))
        max_ring = 80
        while ring <= max_ring:
            for di in range(-ring, ring + 1):
                for dj in range(-ring, ring + 1):
                    if max(abs(di), abs(dj)) != ring:
                        continue
                    for i in self.cells.get((ci + di, cj + dj), ()):
                        it = self.items[i]
                        found.append((haversine_km(lat, lon, it["lat"], it["lon"]), key(it), i))
            if len(found) >= n:
                found.sort()
                # every unvisited cell is at least ring * km_per_cell away
                if found[n - 1][0] <= ring * km_per_cell:
                    break
            ring += 1
        found.sort()
        return [(d, self.items[i]) for d, _, i in found[:n]]


# ---------------------------------------------------------------------------------------------- core
def empty_cells():
    return {t: {s: {"count": 0, "mw": [], "mw_unknown": 0} for s in STATUS_ORDER} for t in TECH_ORDER}


def add_cell(cells, p):
    c = cells[p["tech_group"]][p["status_group"]]
    c["count"] += 1
    if p["capacity_mw"] is None:
        c["mw_unknown"] += 1
    else:
        c["mw"].append(p["capacity_mw"])


def close_cells(cells):
    return {t: {s: {"count": v["count"], "mw_stated": msum(v["mw"]), "count_mw_unknown": v["mw_unknown"]}
                for s, v in sts.items()} for t, sts in cells.items()}


def project_brief(p, d=None):
    o = {"ref": p["ref"], "gg_project_id": p["gg_project_id"], "site_name": p["site_name"],
         "technology": p["technology"], "status": p["status"], "status_group": p["status_group"],
         "capacity_mw": p["capacity_mw"]}
    if d is not None:
        o["distance_km"] = r3(d)
    return o


def node_brief(n, d):
    return {"site_code": n["site_code"], "name": n["name"], "transmission_owner": n["transmission_owner"],
            "voltages_kv": n["voltages_kv"], "distance_km": r3(d)}


def dc_brief(dc, d=None):
    o = {"id": dc["id"], "name": dc["name"], "status": dc["status"], "precision": dc["precision"]}
    if d is not None:
        o["distance_km"] = r3(d)
    return o


def infer_region(dc, repd_index):
    near = repd_index.nearest(dc["lat"], dc["lon"], REGION_VOTE_K, key=lambda p: int(p["ref"]))
    votes, first = Counter(), {}
    for rank, (_, p) in enumerate(near):
        votes[p["region"]] += 1
        first.setdefault(p["region"], rank)
    if not votes:
        return None
    region = sorted(votes, key=lambda r: (-votes[r], first[r]))[0]
    return region


def country_of(region):
    if region is None:
        return None
    return REGION_COUNTRY.get(region, "England")


def cluster_datacentres(dcs):
    """Single-linkage clusters of reliable data-centre points at CLUSTER_LINK_KM; clusters smaller than
    CLUSTER_MIN_SITES are not named. Ids are assigned by size (desc), then by member id, so they are stable."""
    pts = [d for d in dcs if d["proximity_reliable"]]
    parent = list(range(len(pts)))

    def find(i):
        while parent[i] != i:
            parent[i] = parent[parent[i]]
            i = parent[i]
        return i

    for i in range(len(pts)):
        for j in range(i + 1, len(pts)):
            if haversine_km(pts[i]["lat"], pts[i]["lon"], pts[j]["lat"], pts[j]["lon"]) <= CLUSTER_LINK_KM:
                a, b = find(i), find(j)
                if a != b:
                    parent[max(a, b)] = min(a, b)
    groups = defaultdict(list)
    for i, p in enumerate(pts):
        groups[find(i)].append(p)
    named = [sorted(g, key=lambda d: d["id"]) for g in groups.values() if len(g) >= CLUSTER_MIN_SITES]
    named.sort(key=lambda g: (-len(g), g[0]["id"]))
    out = []
    for k, g in enumerate(named, 1):
        out.append({"cluster_id": "DCC%02d" % k, "members": g})
    return out


STATUS_RANK = {s: i for i, s in enumerate(STATUS_ORDER)}


def find_duplicates(rows):
    """Live records that repeat one site: same technology group, same normalised site name, same stated
    capacity text (blank included) and the same BNG point. The record kept is the most advanced status
    (operational first), then the highest Ref ID (the latest entry); the others are collapsed: they keep their
    own row in the per-project table but are not counted in any radius or roll-up. Returns {ref: kept_ref}."""
    groups = defaultdict(list)
    for p in rows:
        if p["tech_group"] and p["status_group"] and p["lat"] is not None:
            k = (p["tech_group"], " ".join(_tokens(p["site_name"])), p["capacity_mw"], p["lat"], p["lon"])
            groups[k].append(p)
    out = {}
    for g in groups.values():
        if len(g) < 2:
            continue
        g = sorted(g, key=lambda p: (STATUS_RANK[p["status_group"]], -int(p["ref"])))
        for p in g[1:]:
            out[p["ref"]] = g[0]["ref"]
    return out


def build(repd_rows, dcs, nodes, osm=None):
    dups = find_duplicates(repd_rows)
    live = [p for p in repd_rows if p["tech_group"] and p["status_group"] and p["lat"] is not None
            and p["ref"] not in dups]
    located = [p for p in repd_rows if p["lat"] is not None and p["region"] and p["region"] != "Offshore"]
    repd_index = GridIndex(located)
    node_index = GridIndex(nodes) if nodes else None
    osm_index = GridIndex(osm) if osm else None
    reliable = [d for d in dcs if d["proximity_reliable"]]
    placed = [d for d in dcs if d["lat"] is not None]

    # nearest transmission node of every live project (for the shared-node grouping)
    proj_node = {}
    node_members = defaultdict(list)
    if node_index:
        for p in live:
            d, n = node_index.nearest(p["lat"], p["lon"], 1, key=lambda n: n["site_code"])[0]
            proj_node[p["ref"]] = (n, d)
            node_members[n["site_code"]].append(p)

    # region inference and clusters for data centres
    for dc in dcs:
        dc["region_inferred"] = infer_region(dc, repd_index) if dc["lat"] is not None else None
        dc["country_inferred"] = country_of(dc["region_inferred"])
    clusters = cluster_datacentres(dcs)
    dc_cluster = {}
    for c in clusters:
        for m in c["members"]:
            dc_cluster[m["id"]] = c["cluster_id"]

    # ---- (a) per data centre
    dc_rows = []
    for dc in dcs:
        row = {**dc_brief(dc), "operator": dc["operator"], "lat": dc["lat"], "lon": dc["lon"],
               "proximity_reliable": dc["proximity_reliable"], "cluster_id": dc_cluster.get(dc["id"]),
               "region_inferred": dc["region_inferred"], "country_inferred": dc["country_inferred"],
               "it_load_mw": dc["it_load_mw"]["value"], "grid_connection_mw": dc["grid_connection_mw"]["value"],
               "unclassified_mw": dc["unclassified_mw"]["value"],
               "unclassified_mw_label": dc["unclassified_mw"]["label"]}
        if dc["lat"] is None:
            row.update({"radii": None, "nearest_by_technology": None, "nearest_transmission_node": None,
                        "shared_node": None, "nearest_osm_substation_132kv_plus": None})
            dc_rows.append(row)
            continue
        radii = {r: empty_cells() for r in RADII_KM}
        best = {t: None for t in TECH_ORDER}
        for p in live:
            d = haversine_km(dc["lat"], dc["lon"], p["lat"], p["lon"])
            t = p["tech_group"]
            k = (d, int(p["ref"]))
            if best[t] is None or k < best[t][0]:
                best[t] = (k, p)
            for r in RADII_KM:
                if d <= r:
                    add_cell(radii[r], p)
        row["radii"] = {str(r): close_cells(radii[r]) for r in RADII_KM}
        row["nearest_by_technology"] = {t: (project_brief(best[t][1], best[t][0][0]) if best[t] else None)
                                        for t in TECH_ORDER}
        if node_index:
            d, n = node_index.nearest(dc["lat"], dc["lon"], 1, key=lambda n: n["site_code"])[0]
            row["nearest_transmission_node"] = node_brief(n, d)
            cells = empty_cells()
            members = node_members.get(n["site_code"], [])
            for p in members:
                add_cell(cells, p)
            row["shared_node"] = {"site_code": n["site_code"], "repd_projects": len(members),
                                  "by_technology": close_cells(cells),
                                  "repd_refs": sorted((p["ref"] for p in members), key=int)}
        else:
            row["nearest_transmission_node"] = row["shared_node"] = None
        if osm_index:
            d, s = osm_index.nearest(dc["lat"], dc["lon"], 1, key=lambda s: (s["name"] or ""))[0]
            row["nearest_osm_substation_132kv_plus"] = {"name": s["name"], "max_kv": s["max_kv"],
                                                        "operator": s["operator"], "distance_km": r3(d)}
        else:
            row["nearest_osm_substation_132kv_plus"] = None
        dc_rows.append(row)

    # ---- (b) per REPD project (solar and battery first, then wind), all statuses with usable coordinates
    dc_index = GridIndex(reliable) if reliable else None
    proj_rows = []
    order = {t: i for i, t in enumerate(TECH_ORDER)}
    targets = sorted((p for p in repd_rows if p["tech_group"] and p["lat"] is not None),
                     key=lambda p: (order[p["tech_group"]], int(p["ref"])))
    for p in targets:
        row = {**project_brief(p), "tech_group": p["tech_group"], "region": p["region"], "country": p["country"],
               "lat": p["lat"], "lon": p["lon"], "duplicate_of": dups.get(p["ref"])}
        dists = sorted(((haversine_km(p["lat"], p["lon"], dc["lat"], dc["lon"]), dc["id"], dc) for dc in reliable),
                       key=lambda t: (t[0], t[1]))
        row["nearest_datacentres"] = [dc_brief(dc, d) for d, _, dc in dists[:NEAREST_DC_LIST]]
        within = {}
        for r in RADII_KM:
            inr = [dc for d, _, dc in dists if d <= r]
            st = Counter(dc["status"] for dc in inr)
            it = [dc["it_load_mw"]["value"] for dc in inr if dc["it_load_mw"]["value"] is not None]
            gc = [dc["grid_connection_mw"]["value"] for dc in inr if dc["grid_connection_mw"]["value"] is not None]
            within[str(r)] = {"datacentres": len(inr), "operational": st.get("operational", 0),
                              "pipeline": len(inr) - st.get("operational", 0),
                              "it_load_mw_stated": msum(it), "it_load_mw_known_sites": len(it),
                              "grid_connection_mw_stated": msum(gc), "grid_connection_mw_known_sites": len(gc)}
        row["within"] = within
        nd = proj_node.get(p["ref"])
        if nd is None and node_index:
            nd = node_index.nearest(p["lat"], p["lon"], 1, key=lambda n: n["site_code"])[0][::-1]
        row["nearest_transmission_node"] = node_brief(*nd) if nd else None
        proj_rows.append(row)

    # ---- (c) co-location candidates
    coloc = []
    dc_ops = {dc["id"]: operator_keys(dc["operator"]) for dc in dcs}
    ops_to_dc = defaultdict(list)
    for dc in dcs:
        for k in dc_ops[dc["id"]]:
            ops_to_dc[k].append(dc)
    techful = [p for p in repd_rows if p["lat"] is not None]
    seen = set()

    def emit(cls, p, dc, d, evidence):
        key = (cls, p["ref"], dc["id"])
        if key in seen:
            return
        seen.add(key)
        coloc.append({"evidence_class": cls, "datacentre": dc_brief(dc), "repd": project_brief(p),
                      "distance_km": r3(d), "evidence": evidence})

    for p in sorted(repd_rows, key=lambda p: int(p["ref"])):
        pkeys = operator_keys(p["operator"])
        for k in pkeys:
            for dc in ops_to_dc.get(k, ()):
                d = (haversine_km(p["lat"], p["lon"], dc["lat"], dc["lon"])
                     if p["lat"] is not None and dc["lat"] is not None else None)
                if d is not None and d <= SAME_SITE_OPERATOR_KM and dc["precision"] == "exact_site":
                    emit("A_same_site_and_operator", p, dc, d, {"operator_key": k})
                emit("B_operator_match", p, dc, d, {"operator_key": k})
        text = "%s %s" % (p["site_name"], p["address"])
        if DC_TEXT.search(text) and p["lat"] is not None and dc_index:
            d, dc = dc_index.nearest(p["lat"], p["lon"], 1, key=lambda x: x["id"])[0]
            emit("E_repd_text_mentions_datacentre", p, dc, d,
                 {"matched_text": DC_TEXT.search(text).group(0), "datacentre_is_nearest_reliable_point": True,
                  "nearest_within_name_match_km": d <= NAME_MATCH_KM})
    # a site-name token is distinctive when it occurs in at most NAME_TOKEN_MAX_DF REPD name+address strings
    # (place names such as a town occur in many and are not evidence of one site)
    df = Counter()
    for p in repd_rows:
        df.update(site_tokens(p["site_name"] + " " + p["address"]))
    for dc in reliable:
        dtoks = site_tokens(dc["name"])
        for p in techful:
            d = haversine_km(dc["lat"], dc["lon"], p["lat"], p["lon"])
            if d > NAME_MATCH_KM:
                continue
            shared = sorted(dtoks & site_tokens(p["site_name"] + " " + p["address"]))
            distinctive = [t for t in shared if df[t] <= NAME_TOKEN_MAX_DF]
            if len(shared) >= 2 and distinctive:
                emit("C_same_site_name_and_proximity", p, dc, d,
                     {"shared_site_tokens": shared, "distinctive_tokens": distinctive})
            if d <= SAME_SITE_KM and dc["precision"] == "exact_site":