GLOBALGRID2050 / EXPLORE CODE

Part of: Explore Code / Automation and CI / globalgrid2050 / scripts

GlobalGrid2050 Gridbot V6 Same Slot Reconciliation Audit and 2 more files

A Python command-line tool in the scripts folder that fetches electricity price history, solar and wind from Elexon Balancing Mechanism Reporting Service (BMRS).

401 lines of Python and HTML from globalgrid2050, in 3 files. Topic: Automation and CI. Section 1769 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 Gridbot V6 Same Slot Reconciliation Audit energy-data · globalgrid2050 / scripts/gridbot_v6_same_slot_reconciliation_audit.py lines 1–305
  2. GlobalGrid2050 Homepage Template render · globalgrid2050 / scripts/homepage.template.html lines 1–23
  3. GlobalGrid2050 Import Homepage Owner storage · globalgrid2050 / scripts/import_homepage_owner.py lines 1–73

GlobalGrid2050 Gridbot V6 Same Slot Reconciliation Audit

A Python command-line tool in the scripts folder that fetches electricity price history, solar and wind from Elexon Balancing Mechanism Reporting Service (BMRS).

energy-dataglobalgrid2050 / scripts/gridbot_v6_same_slot_reconciliation_audit.py · lines 1–305 of 305 · Entry point · pattern fetch_fuelinst (shared by 2 files)

What it does
It fetches electricity price history, solar and wind. Defines 14 functions or classes, including iso_z, parse_iso, http_json, rows, pick. Uses libraries __future__, argparse, csv, datetime, json. Side effects: reads or writes files; makes network requests.
Why it exists in the grid workflow
Domain terms in the code: solar, wind.
Inputs & outputs
In: data source: Elexon BMRS; CSV file electricity_price_history.csv; JSON file V6_SAME_SLOT_RECONCILIATION_AUDIT_LATEST.json; command-line arguments.
Downstream impact
Used directly by 1 file: GlobalGrid2050 Gridbot V6 Same Slot Reconciliation Audit Workflow (globalgrid2050).
What it achieves
It reads repo price (`read_repo_price`) and fetches Elexon price (`fetch_elexon_price`). Defines `iso_z`, `parse_iso`, `http_json` and 10 more. It accepts `--date`, `--time` and `--timezone`.
Why it matters
Run by GlobalGrid2050 Gridbot V6 Same Slot Reconciliation Audit Workflow.
Application
globalgrid2050 · scripts · Python CLI
Uses
__future__, argparse, csv, datetime, json, urllib.parse, urllib.request, pathlib
Used by
run by GlobalGrid2050 Gridbot V6 Same Slot Reconciliation Audit Workflow (workflow run)
Produces
iso_z, parse_iso, http_json, rows, pick, number, read_repo_price, fetch_elexon_price, group_for, fetch_fuelinst, fetch_pvlive_solar, candidate_slots
Defines here
iso_z, parse_iso, http_json, rows, pick, number, read_repo_price, fetch_elexon_price, group_for, fetch_fuelinst, fetch_pvlive_solar, candidate_slots, compare, 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/gridbot_v6_same_slot_reconciliation_audit.py:1-305 · blob fca5b8ae9cdf9ad54427862f64967c6149743976 · GitHub at these lines
State
achieves: name · context: readme_derived · stage: output · effects: filesystem, network · domain stage: unknown · users: none · canonical: production_core
#!/usr/bin/env python3
from __future__ import annotations

import argparse
import csv
import datetime as dt
import json
import urllib.parse
import urllib.request
from pathlib import Path
from zoneinfo import ZoneInfo

ROOT = Path(__file__).resolve().parents[1]
PRICE_CSV = ROOT / "uk_energy_tracking_v6" / "electricity_price_history.csv"
REPORT_DIR = ROOT / "data_science_protocol" / "audit_reports"
REPORT_JSON_DIR = REPORT_DIR / "json"
REPORT_MD = REPORT_DIR / "V6_SAME_SLOT_RECONCILIATION_AUDIT_LATEST.md"
REPORT_JSON = REPORT_JSON_DIR / "V6_SAME_SLOT_RECONCILIATION_AUDIT_LATEST.json"
ELEXON = "https://data.elexon.co.uk/bmrs/api/v1"
PVLIVE = "https://api.solar.sheffield.ac.uk/pvlive/api/v4/gsp/0"

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


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


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


def http_json(url: str) -> dict | list:
    req = urllib.request.Request(url, headers={"Accept": "application/json", "User-Agent": "GlobalGrid2050 GridBot"})
    with urllib.request.urlopen(req, timeout=40) as response:
        return json.loads(response.read().decode("utf-8"))


def rows(payload):
    if isinstance(payload, list):
        return payload
    if isinstance(payload, dict):
        for key in ("data", "results", "items"):
            if isinstance(payload.get(key), list):
                return payload[key]
    return []


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


def number(value):
    try:
        if value in (None, ""):
            return None
        return float(value)
    except Exception:
        return None


def read_repo_price(slot_utc: dt.datetime) -> dict | None:
    target = iso_z(slot_utc)
    if not PRICE_CSV.exists():
        return None
    with PRICE_CSV.open("r", encoding="utf-8", newline="") as handle:
        for row in csv.DictReader(handle):
            if row.get("priceTimeUTC") == target:
                return row
    return None


def fetch_elexon_price(slot_utc: dt.datetime) -> list[dict]:
    start = slot_utc - dt.timedelta(minutes=5)
    end = slot_utc + dt.timedelta(minutes=35)
    q = urllib.parse.urlencode({"from": iso_z(start), "to": iso_z(end), "format": "json"})
    url = f"{ELEXON}/balancing/pricing/market-index?{q}"
    out = []
    for row in rows(http_json(url)):
        price = number(pick(row, ["price", "marketIndexPrice", "MarketIndexPrice", "value"]))
        time_value = pick(row, ["startTime", "publishDateTime", "publishTime", "time", "datetime"])
        parsed = parse_iso(str(time_value)) if time_value else None
        if parsed and price is not None:
            out.append({"timeUTC": iso_z(parsed), "priceGBPperMWh": round(price, 2), "raw": row})
    return out


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


def fetch_fuelinst(slot_utc: dt.datetime) -> dict:
    start = slot_utc - dt.timedelta(minutes=90)
    end = slot_utc + dt.timedelta(minutes=45)
    q = urllib.parse.urlencode({"publishDateTimeFrom": iso_z(start), "publishDateTimeTo": iso_z(end), "format": "json"})
    url = f"{ELEXON}/datasets/FUELINST?{q}"
    raw = rows(http_json(url))
    grouped = {label: 0.0 for label in ORDER if label != "Solar"}
    matched_raw = []
    for row in raw:
        t = parse_iso(str(pick(row, ["startTime", "periodStartUTC", "publishDateTime"])))
        if not t or iso_z(t) != iso_z(slot_utc):
            continue
        fuel = str(pick(row, ["fuelType", "fuelTypeName", "fuel", "psrType"]) or "")
        mw = number(pick(row, ["generation", "generationMW", "currentUsage", "quantity"]))
        if not fuel or mw is None:
            continue
        grouped[group_for(fuel)] = grouped.get(group_for(fuel), 0.0) + mw / 1000.0
        matched_raw.append(row)
    return {"url": url, "rawRowsReturned": len(raw), "matchedRows": len(matched_raw), "groupedGW": {k: round(v, 3) for k, v in grouped.items()}}


def fetch_pvlive_solar(slot_utc: dt.datetime) -> dict:
    day_start = dt.datetime.combine(slot_utc.date(), dt.time(0, 0), tzinfo=dt.timezone.utc)
    day_end = dt.datetime.combine(slot_utc.date(), dt.time(23, 59), tzinfo=dt.timezone.utc)
    candidates = [
        {"start": iso_z(day_start), "end": iso_z(day_end)},
        {"start_date": slot_utc.date().isoformat(), "end_date": slot_utc.date().isoformat()},
    ]
    for params in candidates:
        url = PVLIVE + "?" + urllib.parse.urlencode(params)
        try:
            payload = http_json(url)
            values = []
            for row in rows(payload):
                if isinstance(row, list) and len(row) >= 3:
                    t = parse_iso(str(row[1])); mw = number(row[2])
                elif isinstance(row, dict):
                    t = parse_iso(str(pick(row, ["datetime_gmt", "datetime", "time", "timestamp", "periodStartUTC"])))
                    mw = number(pick(row, ["generation_mw", "generationMW", "generation", "power"]))
                else:
                    continue
                if t and mw is not None and iso_z(t) == iso_z(slot_utc):
                    values.append(mw / 1000.0)
            if values:
                return {"url": url, "solarGW": round(values[-1], 3), "matchedRows": len(values)}
        except Exception as exc:
            last_error = str(exc)
    return {"url": "", "solarGW": None, "matchedRows": 0, "error": locals().get("last_error", "no matching PVLive row")}


def candidate_slots(date_text: str, time_text: str, tz_name: str) -> list[dict]:
    date = dt.date.fromisoformat(date_text)
    hour, minute = [int(x) for x in time_text.split(":")]
    local = dt.datetime(date.year, date.month, date.day, hour, minute, tzinfo=ZoneInfo(tz_name))
    direct_utc = dt.datetime(date.year, date.month, date.day, hour, minute, tzinfo=dt.timezone.utc)
    return [
        {"basis": f"{tz_name} local converted to UTC", "slotUTC": local.astimezone(dt.timezone.utc), "slotLocal": local},
        {"basis": "input treated as UTC", "slotUTC": direct_utc, "slotLocal": direct_utc.astimezone(ZoneInfo(tz_name))},
    ]


def compare(value, expected):
    if expected is None or value is None:
        return None
    return round(float(value) - float(expected), 3)


def main() -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument("--date", required=True)
    parser.add_argument("--time", required=True)
    parser.add_argument("--timezone", default="Europe/London")
    parser.add_argument("--benchmark-price", type=float)
    parser.add_argument("--benchmark-carbon", type=float)
    parser.add_argument("--benchmark-demand", type=float)
    parser.add_argument("--benchmark-solar", type=float)
    parser.add_argument("--benchmark-wind", type=float)
    parser.add_argument("--benchmark-hydro", type=float)
    parser.add_argument("--benchmark-gas", type=float)
    parser.add_argument("--benchmark-coal", type=float)
    parser.add_argument("--benchmark-biomass", type=float)
    parser.add_argument("--benchmark-nuclear", type=float)
    parser.add_argument("--benchmark-pumped-storage", type=float)
    parser.add_argument("--benchmark-imports-exports", type=float)
    args = parser.parse_args()

    results = []
    benchmark_generation = {
        "Solar": args.benchmark_solar,
        "Wind": args.benchmark_wind,
        "Hydro": args.benchmark_hydro,
        "Gas": args.benchmark_gas,
        "Coal": args.benchmark_coal,
        "Biomass": args.benchmark_biomass,
        "Nuclear": args.benchmark_nuclear,
        "Pumped Storage": args.benchmark_pumped_storage,
        "Imports & Exports": args.benchmark_imports_exports,
    }
    for cand in candidate_slots(args.date, args.time, args.timezone):
        slot_utc = cand["slotUTC"]
        repo_price = read_repo_price(slot_utc)
        elexon_prices = fetch_elexon_price(slot_utc)
        exact_prices = [r for r in elexon_prices if r["timeUTC"] == iso_z(slot_utc)]
        fuel = fetch_fuelinst(slot_utc)
        solar = fetch_pvlive_solar(slot_utc)
        generation = dict(fuel["groupedGW"])
        if solar.get("solarGW") is not None:
            generation["Solar"] = solar["solarGW"]
        demand = round(sum(v for v in generation.values() if isinstance(v, (int, float))), 3)
        row_price = number(repo_price.get("priceGBPperMWh")) if repo_price else None
        row_carbon = number(repo_price.get("carbonGperKWh")) if repo_price else None
        result = {
            "basis": cand["basis"],
            "slotUTC": iso_z(slot_utc),
            "slotLocal": cand["slotLocal"].isoformat(),
            "repoPriceRowFound": repo_price is not None,
            "repoPriceGBPperMWh": row_price,
            "repoCarbonGperKWh": row_carbon,
            "elexonExactPriceRows": exact_prices,
            "elexonFetchedPriceRows": len(elexon_prices),
            "fuelinst": fuel,
            "pvliveSolar": solar,
            "generationGW": {k: generation.get(k) for k in ORDER if k in generation},
            "demandGWCalculatedFromGroupedGeneration": demand,
            "benchmarkDifference": {
                "price": compare(row_price, args.benchmark_price),
                "carbon": compare(row_carbon, args.benchmark_carbon),
                "demand": compare(demand, args.benchmark_demand),
                "generation": {k: compare(generation.get(k), v) for k, v in benchmark_generation.items() if v is not None},
            },
        }
        results.append(result)
    checks = {
        "price_history_file_exists": PRICE_CSV.exists(),
        "two_slot_interpretations_checked": len(results) == 2,
        "at_least_one_repo_price_or_elexon_price_found": any(r["repoPriceRowFound"] or r["elexonFetchedPriceRows"] for r in results),
        "fuelinst_attempted_for_each_slot": all("fuelinst" in r for r in results),
        "no_repo_data_files_modified": True,
        "no_external_site_named_or_scraped": True,
    }
    passed = all(checks.values())
    REPORT_DIR.mkdir(parents=True, exist_ok=True)
    REPORT_JSON_DIR.mkdir(parents=True, exist_ok=True)
    report = {
        "reportTitle": "V6 Same Slot Reconciliation Audit",
        "schemaVersion": "1.0.0",
        "generatedUTC": dt.datetime.now(dt.timezone.utc).isoformat().replace("+00:00", "Z"),
        "repository": "Ventusltd/globalgrid2050",
        "mode": "audit",
        "input": vars(args),
        "sourceFiles": [str(PRICE_CSV.relative_to(ROOT))],
        "sourceApis": ["Elexon BMRS Market Index Data", "Elexon BMRS FUELINST", "Sheffield Solar PVLive"],
        "results": results,
        "checks": checks,
        "changedFiles": [],
        "applied": False,
        "pass": passed,
        "executiveSummary": "Audits a named half hour slot against V6 repo price history and public source data. The audit checks both local time converted to UTC and direct UTC interpretation, to identify timestamp alignment errors without naming or scraping third party comparison pages.",
        "nextAction": "Review which slot basis matches the benchmark values, then decide whether V6 needs timezone labelling, extra same day generation logging, or aggregation changes.",
    }
    REPORT_JSON.write_text(json.dumps(report, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
    lines = [
        "# V6 Same Slot Reconciliation Audit",
        "",
        f"Generated UTC: `{report['generatedUTC']}`",
        f"Pass: `{passed}`",
        "",
        report["executiveSummary"],
        "",
        "## Slot results",
        "",
        "| Basis | Slot UTC | Repo price found | Repo price | Repo carbon | Fuel rows | Solar row | Demand from grouped GW |",
        "|---|---|---:|---:|---:|---:|---:|---:|",
    ]
    for r in results:
        lines.append(f"| {r['basis']} | {r['slotUTC']} | {r['repoPriceRowFound']} | {r['repoPriceGBPperMWh']} | {r['repoCarbonGperKWh']} | {r['fuelinst']['matchedRows']} | {r['pvliveSolar'].get('matchedRows')} | {r['demandGWCalculatedFromGroupedGeneration']} |")
    lines += ["", "## Checks", "", "| Check | Result |", "|---|---|"]
    for k, v in checks.items():
        lines.append(f"| {k} | {'✅' if v else '❌'} |")
    REPORT_MD.write_text("\n".join(lines) + "\n", encoding="utf-8")
    print(json.dumps(report, indent=2, ensure_ascii=False))
    return 0 if passed else 1


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

GlobalGrid2050 Homepage Template

A web page in the scripts folder covering solar. Published at https://globalgrid2050.com/scripts/homepage.template.html.

renderglobalgrid2050 / scripts/homepage.template.html · lines 1–23 of 23 · Entry point

What it does
It draws solar. Side effects: changes the web page (DOM).
Why it exists in the grid workflow
Domain terms in the code: solar.
Inputs & outputs
Out: web page.
Downstream impact
It is itself an entry point, published at https://globalgrid2050.com/scripts/homepage.template.html.
What it achieves
Explore the original Kuiper, GridAtlas, Pipeline News and Solar Design Studio.
Why it matters
No other file in the estate was found to import it; it is an entry point.
Application
globalgrid2050 · scripts · page
Uses
none found
Used by
none found
Produces
none declared
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/homepage.template.html:1-23 · blob 0415fee4e2fbdc362c68f1a2f41530d0712ac5fb · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: dom · domain stage: unknown · users: none · canonical: production_core
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>GlobalGrid2050 &mdash; Kuiper and the energy system</title>
<meta name="description" content="Explore the original Kuiper, GridAtlas, Pipeline News and Solar Design Studio. Four entrances to an evolving energy system.">
<style>
:root{color-scheme:dark;--ink:#e8f2ed;--muted:#a7bab0;--line:#283e34;--green:#85ffb0;--cyan:#8de5ff}
*{box-sizing:border-box}body{margin:0;background:#070c0a;color:var(--ink);font:16px/1.6 ui-monospace,SFMono-Regular,Consolas,monospace}main,header,footer{max-width:960px;margin:auto;padding:0 28px}header{padding-top:64px;padding-bottom:38px}.brand{font-size:14px;letter-spacing:.16em;color:var(--green)}h1{font-size:clamp(32px,6vw,58px);font-weight:500;line-height:1.12;letter-spacing:-.045em;margin:  … [line shortened here: 1,898 characters; full line on GitHub]
</style>
</head>
<body>
<header><div class="brand">GLOBALGRID2050</div><h1>A universe to build from.</h1><p>Start with Kuiper. Explore the network, follow projects and shape a solar design.</p></header>
<main>
<section aria-label="Four nests">
{{NESTS}}
</section>
<nav class="archive" aria-label="History"><a href="{{ARCHIVE_URL}}">{{ARCHIVE_LABEL}} &rarr;</a><span>Earlier tools and versions, preserved.</span></nav>
</main>
<footer><p>Funded by VENTUS LTD. Built through open collaboration.</p></footer>
</body>
</html>

GlobalGrid2050 Import Homepage Owner

A Python command-line tool in the scripts folder that fetches active pages, using SHA-256.

storageglobalgrid2050 / scripts/import_homepage_owner.py · lines 1–73 of 73 · Entry point

What it does
It fetches active pages. Defines main. Uses libraries hashlib, json, re, urllib.request, html. Side effects: reads or writes files; makes network requests.
Inputs & outputs
In: JSON file homepage.json; JSON file active-pages.json; JSON file presentation.json; JSON file entity-types.json; JSON file homepage-owner.json.
Downstream impact
Used directly by 4 files: GlobalGrid2050 Verify VEDIC Homepage Release (globalgrid2050); GlobalGrid2050 Deploy Pages Workflow (globalgrid2050); GlobalGrid2050 Public Language Gate Workflow (globalgrid2050); GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 Homepage Pin QA Workflow (globalgrid2050). Reached from 1 other entry point, including GlobalGrid2050 VEDIC Homepage Promotion Workflow (globalgrid2050).
What it achieves
Publish only the hash-pinned small homepage bundle from its confirmed owner.
Why it matters
Run by 4 workflows and scripts, including GlobalGrid2050 Verify VEDIC Homepage Release, GlobalGrid2050 Deploy Pages Workflow and GlobalGrid2050 Public Language Gate Workflow.
Application
globalgrid2050 · scripts · Python CLI
Uses
hashlib, json, re, urllib.request, html, pathlib
Used by
run by GlobalGrid2050 Verify VEDIC Homepage Release (script call), run by GlobalGrid2050 Deploy Pages Workflow (workflow run), run by GlobalGrid2050 Public Language Gate Workflow (workflow run), run by GlobalGrid2050 Renewable Energy Planning Database (REPD) Q3 Homepage Pin QA Workflow (workflow run)
Produces
main
Defines here
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/import_homepage_owner.py:1-73 · blob e15557e34036c9e38e13221038ac852d4f331cc3 · GitHub at these lines
State
achieves: comment · context: readme_derived · stage: output · effects: filesystem, network · domain stage: unknown · users: none · canonical: production_core
"""Publish only the hash-pinned small homepage bundle from its confirmed owner."""
import hashlib
import json
import re
import urllib.request
from html import escape
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
FILES = {'index.html', 'assets/versioned-homepage.css', 'assets/versioned-homepage.js', 'data/homepage.json', 'data/active-pages.json', 'data/presentation.json', 'data/entity-types.json'}

def main():
    pin = json.loads((ROOT/'catalogue/homepage-owner.json').read_text(encoding='utf-8'))
    if pin['repository'] != 'Ventusltd/globalgrid2050-homepage' or not re.fullmatch('[0-9a-f]{40}', pin['commit']):
        raise ValueError('Unexpected homepage owner or unpinned commit')
    if set(pin['files']) != FILES:
        raise ValueError('Unexpected homepage bundle files')
    bundle = {}
    for name, digest in pin['files'].items():
        url = f'https://raw.githubusercontent.com/{pin["repository"]}/{pin["commit"]}/{name}'
        with urllib.request.urlopen(url, timeout=30) as response:
            data = response.read(1000001)
        if len(data) > 1000000 or hashlib.sha256(data).hexdigest() != digest:
            raise ValueError(f'Homepage size/hash mismatch: {name}')
        bundle[name] = data
    if sum(map(len, bundle.values())) >= 1000000:
        raise ValueError('Homepage bundle exceeds 1 MB')
    config = json.loads(bundle['data/homepage.json'].decode('utf-8-sig'))
    if config.get('schema_version') != 1:
        raise ValueError('Unsupported owner catalogue')
    index = bundle.pop('index.html').decode('utf-8-sig')
    for name in ('versioned-homepage.css', 'versioned-homepage.js'):
        source = f'./assets/{name}'
        if index.count(source) != 1:
            raise ValueError(f'Unexpected index reference: {name}')
        index = index.replace(source, f'./homepage-shell/assets/{name}')
    # The owner serves its catalogue beside index.html; the domain assembles it
    # under homepage-shell, just like the stylesheet and JavaScript above.
    source = 'href="data/homepage.json"'
    if index.count(source) != 1:
        raise ValueError('Unexpected catalogue fallback reference')
    index = index.replace(source, 'href="./homepage-shell/data/homepage.json"')
    # The short phone link follows the same release as the homepage. Earlier
    # catalogue pins still have the original SatNav section to fall back to.
    satnav_url = '/#satnav-concept'
    for entity in config['entities']:
        if entity['id'] == 'satnav-concept':
            release = next(row for row in entity['releases'] if row['id'] == entity['operative_release_id'])
            satnav_url = release['url']
            if release['status'] != 'available' or not satnav_url.startswith('https://'):
                raise ValueError('SatNav needs a published HTTPS destination')
            break
    destination = escape(satnav_url, quote=True)
    satnav_page = (
        '<!doctype html>\n<html lang="en"><head><meta charset="utf-8">\n'
        '<meta name="viewport" content="width=device-width,initial-scale=1">\n'
        f'<meta http-equiv="refresh" content="0;url={destination}">\n'
        '<title>Ventus SatNav</title></head>\n'
        f'<body><p><a href="{destination}">Open Ventus SatNav</a></p></body></html>\n'
    )
    # All remote inputs are verified before any output changes. Site promotion is
    # performed by Pages after the complete build, not while these files write.
    for name, data in bundle.items():
        target = ROOT/'homepage-shell'/name
        target.parent.mkdir(parents=True, exist_ok=True)
        target.write_bytes(data)
    (ROOT/'index.html').write_text(index.rstrip() + '\n', encoding='utf-8', newline='\n')
    (ROOT/'satnav').mkdir(exist_ok=True)
    (ROOT/'satnav/index.html').write_text(satnav_page, encoding='utf-8', newline='\n')
    print(f'Imported verified homepage {pin["commit"]}: {len(FILES)} files')

if __name__ == '__main__':
    main()