Source code for energy_data_retrieval.hydro_catalogue.doed_operating
"""Stage Nepal operating-inventory evidence without automatic identity merges."""
import math
import re
from typing import Any
from .doed import _Table
from .ons_new import name_key
NAMESPACE = "Nepal-DoED-powerplants-hydro-over1"
[docs]
def parse_inventory(html: str) -> list[dict[str, Any]]:
"""Keep source dates and bounds verbatim; omit the contact/address column."""
table = _Table()
table.feed(html)
headers = [
"S No",
"Project",
"Capacity (MW)",
"River",
"Lic No",
"Isuue Date",
"Validity",
"Promoter",
"Address",
"Latitiude N",
"Longitude E",
"VDC/District",
"C O D",
]
if not table.rows or table.rows[0] != headers:
raise ValueError("Unexpected operating inventory headers")
result = []
for index, cells in enumerate(table.rows[1:], 1):
if len(cells) != 15 or cells[0] != str(index) or not cells[1] or not cells[4]:
raise ValueError("Malformed operating inventory row")
capacity = float(cells[2])
if not math.isfinite(capacity) or capacity <= 0:
raise ValueError("Invalid reported MW")
result.append(
dict(
row=index,
name=cells[1],
reported_capacity_mw=capacity,
river=cells[3],
licence=cells[4],
issue_date_raw=cells[5],
validity_raw=cells[6],
promoter=re.sub(r",?\s*\b\d{7,}\b", "", cells[7]).strip(),
bounds_raw=cells[9:13],
district_raw=cells[13],
cod_raw=cells[14] or None,
namespace=NAMESPACE,
)
)
if not result:
raise ValueError("Empty operating inventory")
return result
[docs]
def audit_identities(
operating: list[dict[str, Any]], construction: list[dict[str, Any]]
) -> list[dict[str, Any]]:
"""Return review candidates only; shared licence numbers never authorize links.
A namespace identifies the source table, not proven legal licence uniqueness.
Even matching names and numbers need physical-scope review before promotion.
"""
result = []
for item in operating:
same_number = [r for r in construction if r["licence"] == item["licence"]]
same_name = [r for r in construction if name_key(r["name"]) == name_key(item["name"])]
result.append(
dict(
operating_row=item["row"],
duplicate_operating_number_rows=[
r["row"]
for r in operating
if r["licence"] == item["licence"] and r["row"] != item["row"]
],
operating_licence=item["licence"],
construction_number_candidates=[r["row"] for r in same_number],
construction_name_candidates=[r["row"] for r in same_name],
conflicting_number_rows=[
r["row"]
for r in same_number
if (
name_key(r["name"]) != name_key(item["name"])
or r["bounds_raw"] != item["bounds_raw"]
)
],
decision="candidate_requires_explicit_identity_and_scope_review",
)
)
return result