Source code for energy_data_retrieval.hydro_catalogue.nve_registry
"""Retain exact-ID NVE registry assertions without changing physical totals."""
import copy
import json
from typing import Any
from energy_data_retrieval.pv_catalogue.catalogue import canonical_json
SOURCE_ID = "nve-registry-20261109"
[docs]
def enrich_reviewed(
existing: dict[str, Any],
entry: dict[str, Any],
*,
now: str,
source_id: str = SOURCE_ID,
country_code: str = "NOR",
extension_key: str = "nve_registry_assertions",
) -> dict[str, Any]:
"""Append source assertions; never sum replacement units or promote current MW.
MVA remains apparent power. Unqualified head remains unqualified. A stated
commissioning year is an owner assertion, not a resolution of conflicting
historical dates. A water volume never becomes storage energy.
"""
if (
existing["record_id"] != entry["record_id"]
or existing["country_code"] != country_code
or existing["name"] != entry["name"]
or entry.get("decision") != "reviewed_registry_assertions"
):
raise ValueError("Explicit registry identity review required")
if str(json.loads(existing["external_ids_json"]).get("nve")) != str(entry["nve_id"]):
raise ValueError("NVE registry identity changed")
facts = entry["facts"]
required = {"key", "value", "unit", "basis", "locator", "uncertainty"}
if not facts or any(
not required <= f.keys() or not f["basis"] or not f["locator"] for f in facts
):
raise ValueError("Require complete field provenance")
if len({f["key"] for f in facts}) != len(facts):
raise ValueError("Duplicate technical assertion key")
row = copy.deepcopy(existing)
ext = json.loads(row["extensions_json"])
assertions = {
"source_id": source_id,
"facts": facts,
"scope": (
"NVE registry statements; dates/boundaries retained; "
"no additive rating or current status"
),
}
if extension_key in ext:
if ext[extension_key] != assertions:
raise ValueError("Changed technical evidence requires conflict review")
return row
ext[extension_key] = assertions
fs, ev = json.loads(row["field_sources_json"]), json.loads(row["evidence_json"])
row["extensions_json"] = canonical_json(ext)
fs["extensions_json"] = sorted(set([*fs.get("extensions_json", []), source_id]))
ev["extensions_json"] = {
"locator": "Exact NVE registry ID; per-field JSON locators retained",
"confidence": "high",
"original_value": row["extensions_json"],
"normalization": "Source assertions only; no current rating or date conflict resolution",
}
row.update(
field_sources_json=canonical_json(fs),
evidence_json=canonical_json(ev),
sources=sorted(set(row["sources"] + [source_id])),
updated_at=now,
last_verified_at=now,
)
return row