"""Complete NVE wind registry snapshots with conservative identity screening."""
from __future__ import annotations
import hashlib
import json
import math
import re
import unicodedata
from collections import Counter
from difflib import SequenceMatcher
from typing import Any
from energy_data_retrieval.pv_catalogue.catalogue import canonical_json
from .catalogue import stable_id
from .gppd import evidence_record
REFERENCE_IDS = ("nve_wind_api_v1",)
[docs]
def normalized_name(value: str) -> str:
"""Normalize for candidate screening only; never establish identity."""
value = value.casefold().replace("ø", "o").replace("æ", "ae").replace("å", "a")
value = "".join(c for c in unicodedata.normalize("NFKD", value) if not unicodedata.combining(c))
return re.sub(r"[^a-z0-9]", "", value)
[docs]
def stage_nve(
originals: dict[str, bytes],
expected_hashes: dict[str, str],
expected_ids: list[int],
existing: list[dict[str, Any]],
*,
source_id: str,
now: str,
) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
"""Review every all-plant row and the independent operating-subset response.
A cohort with count >1 is not an individual turbine. Registry membership,
phase dates, mean geometry and normal-year production retain their boundaries.
No capacity total is enabled by this structural inventory import.
"""
for key in ("all", "operation"):
if hashlib.sha256(originals[key]).hexdigest() != expected_hashes[key]:
raise ValueError("NVE original checksum mismatch")
data = json.loads(originals["all"])
operating = json.loads(originals["operation"])
ids = [r["VindkraftAnleggId"] for r in data]
opids = [r["VindkraftAnleggId"] for r in operating]
if len(ids) != len(set(ids)) or sorted(ids) != sorted(expected_ids):
raise ValueError("Incomplete or duplicate all-plant partition")
if len(opids) != len(set(opids)) or not set(opids) <= set(ids):
raise ValueError("Operating subset has duplicate or orphan identities")
lookup = {r["VindkraftAnleggId"]: r for r in data}
if any(lookup[r["VindkraftAnleggId"]] != r for r in operating):
raise ValueError("Responses disagree; capture a consistent pair before staging")
for raw in data:
for key in ("InstallertEffekt_MW", "AntallOperativeTurbiner"):
_number(raw.get(key), integer=key == "AntallOperativeTurbiner")
for cohort in raw["Turbiner"]:
_number(cohort.get("AntallTurbiner"), integer=True)
_number(cohort.get("TurbinStorrelse_kW"))
prior = [
r for r in existing if r["entity_type"] == "projects" and r.get("country_code") == "NOR"
]
prior_names = [
(r["record_id"], normalized_name(n))
for r in prior
for n in [r["name"], *(r.get("aliases") or [])]
]
plant_numbers = Counter(r["AnleggsNr"] for r in data if r.get("AnleggsNr"))
rows, ledger = [], []
for raw in data:
ident = raw["VindkraftAnleggId"]
name = normalized_name(raw["Navn"])
candidates = sorted(
{
rid
for rid, n in prior_names
if name == n
or (
min(len(name), len(n)) >= 5
and (name in n or n in name or SequenceMatcher(None, name, n).ratio() >= 0.75)
)
}
)
related = sorted(
r["VindkraftAnleggId"]
for r in data
if r is not raw
and (
(raw.get("AnleggsNr") and r.get("AnleggsNr") == raw["AnleggsNr"])
or (
min(len(name), len(normalized_name(r["Navn"]))) >= 5
and (name in normalized_name(r["Navn"]) or normalized_name(r["Navn"]) in name)
)
)
)
decision = {
"registry_id": ident,
"name": raw["Navn"],
"candidate_record_ids": candidates,
"related_registry_ids": related,
}
if candidates or related or plant_numbers[raw.get("AnleggsNr")] > 1:
ledger.append(
{
**decision,
"decision": "identity_hold",
"reason": (
"Prior-name similarity or shared plant-number/name scope requires "
"primary crosswalk; not a merge"
),
"source_observation": raw,
}
)
continue
cohorts = raw["Turbiner"]
cids = [c["TurbinID"] for c in cohorts]
if len(cids) != len(set(cids)):
raise ValueError("Duplicate cohort ID")
active = [c for c in cohorts if not c.get("DatoUtavdrift")]
count_check = (
sum(c["AntallTurbiner"] for c in active)
if all(c.get("AntallTurbiner") is not None for c in active)
else None
)
power_check = (
sum(c["AntallTurbiner"] * c["TurbinStorrelse_kW"] for c in active) / 1000
if all(
c.get("AntallTurbiner") is not None and c.get("TurbinStorrelse_kW") is not None
for c in active
)
else None
)
fleet_models = sorted({(c["TurbinProdusent"], c["TurbinType"]) for c in active})
ext = {
"nve_registry": {
"source_fields": raw,
"in_operating_endpoint": ident in opids,
"fleet_boundary": (
"Complete returned cohort list; no individual-unit inventory or "
"exact OEM revision inferred"
),
"mixed_active_model_labels": len(fleet_models) > 1,
"active_model_labels": fleet_models,
"count_check": {
"cohort_sum": count_check,
"reported_operating_count": raw["AntallOperativeTurbiner"],
"matches": (
count_check == raw["AntallOperativeTurbiner"]
if count_check is not None and raw["AntallOperativeTurbiner"] is not None
else None
),
},
"power_check": {
"cohort_count_times_rating_mw": power_check,
"reported_mw": raw["InstallertEffekt_MW"],
"role": "Arithmetic check only; never replaces reported MW",
},
"first_stage_date": raw["IdriftsettelseForsteByggetrinn"],
"geometry_boundary": (
"Gjsnitt values are plant means, not exact turbine configurations"
),
"normal_year_generation": {
"value": raw["NormalAArsproduksjon_GWh"],
"unit": "GWh/year",
"kind": "modeled normal-year",
"climate_period": None,
"gross_net": None,
"capacity_factor": None,
},
"energy_per_swept_area": {
"value": raw["EnergiPerSveiptAreal"],
"status": (
"quarantined from normalized metrics: documentation mixes "
"generator power and kWh/m2"
),
},
"ownership_share": None,
}
}
values = {
"record_id": stable_id("projects", "nve-wind", str(ident)),
"entity_type": "projects",
"record_scope": "farm",
"name": raw["Navn"],
"country_code": "NOR",
"region": raw["Fylke"],
"locality": raw["Kommune"],
"technology": "unknown",
"status": "operational" if ident in opids else "unknown",
"status_as_of": now[:10],
"status_date_basis": (
"Operating endpoint membership at retrieval; absent membership "
"does not independently establish retirement"
),
"aggregation_role": "unresolved",
"capacity_scope": "NVE registry plant; physical phase/repowering overlap unresolved",
"installed_capacity_mw": raw["InstallertEffekt_MW"],
"turbine_count": raw["AntallOperativeTurbiner"],
"fleet_completeness": "partial",
"owner_names": [raw["HovedEierNavn"]] if raw.get("HovedEierNavn") else [],
"external_ids_json": canonical_json(
{
"nve_wind_plant": str(ident),
**({"nve_anleggsnr": str(raw["AnleggsNr"])} if raw.get("AnleggsNr") else {}),
}
),
"extensions_json": canonical_json(ext),
"notes": (
"Primary NVE registry observation. First-stage commissioning is "
"not whole-farm COD. Normal-year production is not measured "
"generation. Coordinates, individual turbine inventory and exact "
"OEM configuration links remain unknown."
),
}
row = evidence_record(
values,
source_id=source_id,
now=now,
locator=(
f"GetWindPowerPlants VindkraftAnleggId={ident}; API v1 field "
f"dictionary and operating endpoint"
),
)
rows.append(row)
ledger.append({**decision, "decision": "addition", "record_id": row["record_id"]})
return sorted(rows, key=lambda r: r["record_id"]), ledger
def _number(value: Any, *, integer: bool = False) -> None:
if value is None:
return
if (
isinstance(value, bool)
or not isinstance(value, (int, float))
or not math.isfinite(value)
or value < 0
or (integer and value != int(value))
):
raise ValueError("Invalid NVE nonnegative quantity")