Source code for energy_data_retrieval.hydro_catalogue.nve_crosswalk

"""Explicitly frozen NVE identity reviews with no scalar capacity promotion."""

import copy
import json
from typing import Any

from energy_data_retrieval.hydro_catalogue.nve_registry import enrich_reviewed as enrich_registry
from energy_data_retrieval.pv_catalogue.catalogue import canonical_json


[docs] def enrich_reviewed( existing: dict[str, Any], entry: dict[str, Any], *, now: str, source_id: str ) -> dict[str, Any]: """Join only an explicit reviewed canonical identity to its NVE registry ID.""" if any(existing.get(k) != v for k, v in entry["reviewed_identity"].items()): raise ValueError("Canonical identity changed since review") attrs = entry["gis_evidence"]["attributes"] if ( str(attrs["vannkraftverknr"]) != entry["nve_id"] or attrs["vannkraftverknavn"] != existing["name"] ): raise ValueError("GIS identity mismatch") row = copy.deepcopy(existing) ids = json.loads(row["external_ids_json"]) if ids.get("nve") not in (None, entry["nve_id"]): raise ValueError("Conflicting NVE identity") ids["nve"] = entry["nve_id"] row["external_ids_json"] = canonical_json(ids) row = enrich_registry(row, entry, now=now, source_id=source_id) fs = json.loads(row["field_sources_json"]) ev = json.loads(row["evidence_json"]) fs["external_ids_json"] = sorted( set([*fs.get("external_ids_json", []), source_id, entry["gis_source_id"]]) ) ev["external_ids_json"] = dict( locator=f"NVE registry {entry['nve_id']}; frozen GIS feature in research ledger", confidence="high", original_value=row["external_ids_json"], normalization=( "Explicit reviewed name/country/station-location identity; " "physical capacity scope remains unresolved" ), ) row.update( field_sources_json=canonical_json(fs), evidence_json=canonical_json(ev), sources=sorted(set(row["sources"] + [entry["gis_source_id"]])), ) return row