"""
Build reconciled Nigeria grid-electricity datasets for NEFDB.

Inputs
------
- International databases (Ember, OWID, IRENA, World Bank)
- Nigerian Energy Data Bank hydro plant records
- Academic plant-level records
- Alternative-source generation-mix estimates

Outputs
-------
- nigeria_generation_mix_reconciled.csv
- hydro_plants_annual_gwh.csv
- consolidation_decision.md (decision log)
"""

from pathlib import Path
import csv
import json

ROOT = Path("C:/Users/HP/palmgrove/NEFDB")
OUT_DIR = ROOT / "datasets" / "grid_electricity" / "derived"
OUT_DIR.mkdir(parents=True, exist_ok=True)


def read_csv(path):
    with open(path, newline="", encoding="utf-8") as f:
        return list(csv.DictReader(f))


def write_csv(path, rows, fieldnames):
    with open(path, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=fieldnames)
        writer.writeheader()
        writer.writerows(rows)


# ---------------------------------------------------------------------------
# Load source data
# ---------------------------------------------------------------------------
ember = read_csv(ROOT / "datasets" / "grid_electricity" / "international" / "ember_nigeria.csv")
owid = read_csv(ROOT / "datasets" / "grid_electricity" / "international" / "owid_energy_nigeria.csv")
irena = read_csv(ROOT / "datasets" / "grid_electricity" / "international" / "irena_nigeria_generation_by_technology.csv")
nedb = read_csv(ROOT / "datasets" / "grid_electricity" / "hyperbrowser" / "nedb_hydro_generation.csv")
alt = json.load(open(ROOT / "datasets" / "grid_electricity" / "alternative" / "nigeria_generation_mix_alternative_extracts.json"))

def ember_year(year):
    y = str(year)
    def get(var):
        for r in ember:
            if r["Year"] == y and r["Category"] == "Electricity generation" and r["Subcategory"] == "Fuel" and r["Variable"] == var and r["Unit"] == "TWh":
                return float(r["Value"]) * 1000
        return None
    total = next(float(r["Value"]) * 1000 for r in ember if r["Year"] == y and r["Category"] == "Electricity generation" and r["Subcategory"] == "Total" and r["Variable"] == "Total Generation")
    return {
        "year": year,
        "source": "Ember/OWID",
        "total_gwh": total,
        "gas_gwh": get("Gas") or 0,
        "hydro_gwh": get("Hydro") or 0,
        "oil_gwh": get("Other Fossil") or 0,
        "solar_gwh": get("Solar") or 0,
        "wind_gwh": get("Wind") or 0,
        "coal_gwh": get("Coal") or 0,
        "biomass_gwh": get("Bioenergy") or 0,
        "other_gwh": 0,
        "confidence": "medium",
        "notes": f"Ember Yearly Electricity Data and OWID energy dataset (identical values for Nigeria {year}).",
    }


def irena_year(year):
    y = str(year)
    rows = [r for r in irena if r["Year"] == y]
    def get(tech):
        for r in rows:
            if r["Technology"] == tech:
                v = r["Electricity generation statistics"].strip()
                return float(v) if v and v != "-" else 0
        return 0
    hydro = get("Renewable hydropower")
    solar = get("Solar photovoltaic")
    wind = get("Onshore wind energy") + get("Offshore wind energy")
    biomass = get("Solid biofuels") + get("Biogas")
    oil = get("Oil")
    gas = get("Natural gas")
    non_renew = get("Total non-renewable")
    total_renew = get("Total renewable")
    total = non_renew + total_renew
    return {
        "year": year,
        "source": "IRENA (IRENASTAT)",
        "total_gwh": total,
        "gas_gwh": gas,
        "hydro_gwh": hydro,
        "oil_gwh": oil,
        "solar_gwh": solar,
        "wind_gwh": wind,
        "coal_gwh": get("Coal and peat"),
        "biomass_gwh": biomass,
        "other_gwh": 0,
        "confidence": "medium",
        "notes": f"IRENA gross electricity generation by technology, {year} edition; total = renewable + non-renewable.",
    }


# ---------------------------------------------------------------------------
# Candidate rows for national mix
# ---------------------------------------------------------------------------
candidates = []
for year in range(2020, 2025):
    candidates.append(ember_year(year))
for year in range(2020, 2024):
    candidates.append(irena_year(year))

# IEA 2022 (from alternative extracts)
for est in alt["generation_mix_annual_estimates"]:
    if est["year"] == 2022:
        total = float(est["total_generation_gwh"])
        gas = total * float(est["gas_percent"]) / 100
        hydro = total * float(est["hydro_percent"]) / 100
        bio = total * float(est.get("bioenergy_percent", 0)) / 100
        candidates.append({
            "year": 2022,
            "source": "IEA Country Profile Nigeria",
            "total_gwh": total,
            "gas_gwh": round(gas, 2),
            "hydro_gwh": round(hydro, 2),
            "oil_gwh": 0,
            "solar_gwh": 0,
            "wind_gwh": 0,
            "coal_gwh": 0,
            "biomass_gwh": round(bio, 2),
            "other_gwh": 0,
            "confidence": "medium",
            "notes": "IEA World Energy Balances figure reported in IEA Nigeria country profile.",
        })
    if est["year"] == 2023:
        total = float(est["total_generation_twh"]) * 1000
        gas = total * float(est["gas_percent"]) / 100
        hydro = total * float(est["hydro_percent"]) / 100
        candidates.append({
            "year": 2023,
            "source": "EIA Country Analysis Brief 2025",
            "total_gwh": round(total, 2),
            "gas_gwh": round(gas, 2),
            "hydro_gwh": round(hydro, 2),
            "oil_gwh": 0,
            "solar_gwh": 0,
            "wind_gwh": 0,
            "coal_gwh": 0,
            "biomass_gwh": 0,
            "other_gwh": 0,
            "confidence": "medium",
            "notes": "EIA Nigeria Country Analysis Brief 2025; only gas/hydro split provided.",
        })
    if est["year"] == 2024:
        total = float(est["total_generation_gwh"])
        hydro = float(est["hydro_gwh"])
        candidates.append({
            "year": 2024,
            "source": "Guardian Nigeria (NERC 2024 data)",
            "total_gwh": round(total, 2),
            "gas_gwh": round(total - hydro, 2),
            "hydro_gwh": round(hydro, 2),
            "oil_gwh": 0,
            "solar_gwh": 0,
            "wind_gwh": 0,
            "coal_gwh": 0,
            "biomass_gwh": 0,
            "other_gwh": 0,
            "confidence": "high",
            "notes": "Nigeria official figure from Guardian Nigeria summary of NERC 2024 annual statistics.",
        })

# UNDP 2020 estimate (shares only) – kept as note rather than a candidate row because no absolute total.

# ---------------------------------------------------------------------------
# NEDB hydro plant totals
# ---------------------------------------------------------------------------
nedb_hydro = {}
for r in nedb:
    code = r["code"]
    year = int(r["year"])
    val = float(r["value"])
    unit = r["unit"].strip()
    # Map ELPD codes to plant names
    plant_map = {
        "ELPD_KAINJI_GEN": "Kainji",
        "ELPD_JEBBA_GEN": "Jebba",
        "ELPD_SHIRORO_GEN": "Shiroro",
        "ELPD_NESCO_GEN": "NESCO",
    }
    plant = plant_map.get(code, code)
    # NESCO values are labelled MWh but are actually kWh (known unit-scale error in the portal).
    # Kainji/Jebba labels say MW but numeric magnitudes are annual MWh.
    # Shiroro label says Bill.Cubic Meter but numeric magnitudes match MWh.
    if code == "ELPD_NESCO_GEN":
        gwh = val / 1_000_000  # kWh -> GWh
    else:
        gwh = val / 1_000       # MWh -> GWh
    nedb_hydro.setdefault(plant, {})[year] = round(gwh, 3)

# Quick QA: 2020 plant sum
print("NEDB hydro plant totals (GWh) sample 2020:")
for plant in ["Kainji", "Jebba", "Shiroro", "NESCO"]:
    print(f"  {plant}: {nedb_hydro.get(plant, {}).get(2020)}")

# ---------------------------------------------------------------------------
# Build chosen (reconciled) national mix rows
# ---------------------------------------------------------------------------
chosen = []

# 2020: Ember/OWID total + NEDB hydro + IRENA solar/biomass/oil
total_2020 = 38000.0
hydro_2020 = sum(nedb_hydro[p][2020] for p in ["Kainji", "Jebba", "Shiroro", "NESCO"] if 2020 in nedb_hydro.get(p, {}))
irena_2020 = irena_year(2020)
solar_2020 = irena_2020["solar_gwh"]
biomass_2020 = irena_2020["biomass_gwh"]
oil_2020 = irena_2020["oil_gwh"]
gas_2020 = total_2020 - hydro_2020 - solar_2020 - biomass_2020 - oil_2020
chosen.append({
    "year": 2020,
    "source": "NEFDB reconciled",
    "total_gwh": round(total_2020, 2),
    "gas_gwh": round(gas_2020, 2),
    "hydro_gwh": round(hydro_2020, 2),
    "oil_gwh": round(oil_2020, 2),
    "solar_gwh": round(solar_2020, 2),
    "wind_gwh": 0,
    "coal_gwh": 0,
    "biomass_gwh": round(biomass_2020, 2),
    "other_gwh": 0,
    "confidence": "high",
    "notes": (
        "Chosen for 2020: total generation from Ember/OWID; hydro from Nigerian Energy Data Bank "
        "plant records (Kainji+Jebba+Shiroro+NESCO). Solar/biomass/oil from IRENA. Gas is the residual. "
        "NEDB hydro (8,341.99 GWh) is ~8.5% higher than Ember hydro (7,690 GWh); plant-level official data preferred."
    ),
})

# 2021: Ember/OWID total + IRENA hydro + IRENA solar/biomass/oil
total_2021 = 39200.0
irena_2021 = irena_year(2021)
hydro_2021 = irena_2021["hydro_gwh"]
solar_2021 = irena_2021["solar_gwh"]
biomass_2021 = irena_2021["biomass_gwh"]
oil_2021 = irena_2021["oil_gwh"]
gas_2021 = total_2021 - hydro_2021 - solar_2021 - biomass_2021 - oil_2021
chosen.append({
    "year": 2021,
    "source": "NEFDB reconciled",
    "total_gwh": round(total_2021, 2),
    "gas_gwh": round(gas_2021, 2),
    "hydro_gwh": round(hydro_2021, 2),
    "oil_gwh": round(oil_2021, 2),
    "solar_gwh": round(solar_2021, 2),
    "wind_gwh": 0,
    "coal_gwh": 0,
    "biomass_gwh": round(biomass_2021, 2),
    "other_gwh": 0,
    "confidence": "medium",
    "notes": (
        "Chosen for 2021: total from Ember/OWID; hydro/solar/biomass/oil from IRENA because "
        "NEDB hydro plant records are incomplete for Kainji and Jebba after 2020. Gas is the residual."
    ),
})

# 2022: Ember/OWID total + IRENA hydro + IRENA solar/biomass/oil
total_2022 = 38000.0
irena_2022 = irena_year(2022)
hydro_2022 = irena_2022["hydro_gwh"]
solar_2022 = irena_2022["solar_gwh"]
biomass_2022 = irena_2022["biomass_gwh"]
oil_2022 = irena_2022["oil_gwh"]
gas_2022 = total_2022 - hydro_2022 - solar_2022 - biomass_2022 - oil_2022
chosen.append({
    "year": 2022,
    "source": "NEFDB reconciled",
    "total_gwh": round(total_2022, 2),
    "gas_gwh": round(gas_2022, 2),
    "hydro_gwh": round(hydro_2022, 2),
    "oil_gwh": round(oil_2022, 2),
    "solar_gwh": round(solar_2022, 2),
    "wind_gwh": 0,
    "coal_gwh": 0,
    "biomass_gwh": round(biomass_2022, 2),
    "other_gwh": 0,
    "confidence": "medium",
    "notes": (
        "Chosen for 2022: total from Ember/OWID (closely matched by IEA 37,916 GWh); hydro/solar/biomass/oil from IRENA. "
        "Gas is the residual."
    ),
})

# 2023: Ember/OWID total + IRENA hydro + IRENA solar/biomass/oil
total_2023 = 40920.0
irena_2023 = irena_year(2023)
hydro_2023 = irena_2023["hydro_gwh"]
solar_2023 = irena_2023["solar_gwh"]
biomass_2023 = irena_2023["biomass_gwh"]
oil_2023 = irena_2023["oil_gwh"]
gas_2023 = total_2023 - hydro_2023 - solar_2023 - biomass_2023 - oil_2023
chosen.append({
    "year": 2023,
    "source": "NEFDB reconciled",
    "total_gwh": round(total_2023, 2),
    "gas_gwh": round(gas_2023, 2),
    "hydro_gwh": round(hydro_2023, 2),
    "oil_gwh": round(oil_2023, 2),
    "solar_gwh": round(solar_2023, 2),
    "wind_gwh": 0,
    "coal_gwh": 0,
    "biomass_gwh": round(biomass_2023, 2),
    "other_gwh": 0,
    "confidence": "medium",
    "notes": (
        "Chosen for 2023: total from Ember/OWID; hydro/solar/biomass/oil from IRENA. "
        "EIA Country Analysis Brief gives a higher total (42,500 GWh) and is included as a candidate row. "
        "Gas is the residual."
    ),
})

# 2024: Guardian Nigeria / NERC official total and hydro; Ember/OWID for solar/biomass
total_2024 = 37093.70
hydro_2024 = 11469.85
emb_2024 = ember_year(2024)
solar_2024 = emb_2024["solar_gwh"]
biomass_2024 = emb_2024["biomass_gwh"]
gas_2024 = total_2024 - hydro_2024 - solar_2024 - biomass_2024
chosen.append({
    "year": 2024,
    "source": "NEFDB reconciled",
    "total_gwh": round(total_2024, 2),
    "gas_gwh": round(gas_2024, 2),
    "hydro_gwh": round(hydro_2024, 2),
    "oil_gwh": 0,
    "solar_gwh": round(solar_2024, 2),
    "wind_gwh": 0,
    "coal_gwh": 0,
    "biomass_gwh": round(biomass_2024, 2),
    "other_gwh": 0,
    "confidence": "high",
    "notes": (
        "Chosen for 2024: total and hydro from Guardian Nigeria summary of NERC 2024 official data. "
        "Solar/biomass from Ember/OWID. Gas is the residual. Hydro share of 30.92% is materially higher "
        "than Ember/OWID hydro share (~24.5%)."
    ),
})

# ---------------------------------------------------------------------------
# Combine and write national mix CSV
# ---------------------------------------------------------------------------
fieldnames = [
    "year", "source", "total_gwh", "gas_gwh", "hydro_gwh", "oil_gwh",
    "solar_gwh", "wind_gwh", "coal_gwh", "biomass_gwh", "other_gwh",
    "confidence", "notes",
]
# Round all numeric fields
all_rows = []
for row in candidates + chosen:
    out = {"year": row["year"], "source": row["source"], "confidence": row["confidence"], "notes": row["notes"]}
    for k in ["total_gwh", "gas_gwh", "hydro_gwh", "oil_gwh", "solar_gwh", "wind_gwh", "coal_gwh", "biomass_gwh", "other_gwh"]:
        v = row.get(k)
        out[k] = round(v, 2) if isinstance(v, (int, float)) else v
    all_rows.append(out)

# Sort: year ascending, reconciled last within each year
all_rows.sort(key=lambda r: (r["year"], 0 if r["source"] == "NEFDB reconciled" else 1))

mix_path = OUT_DIR / "nigeria_generation_mix_reconciled.csv"
write_csv(mix_path, all_rows, fieldnames)
print(f"Wrote {mix_path} with {len(all_rows)} rows ({len(chosen)} reconciled + {len(candidates)} candidates).")

# ---------------------------------------------------------------------------
# Hydro plant annual GWh CSV
# ---------------------------------------------------------------------------
plant_rows = []
plants = ["Kainji", "Jebba", "Shiroro", "NESCO"]

# NEDB primary rows (best available direct plant records)
for plant in plants:
    for year, gwh in sorted(nedb_hydro.get(plant, {}).items()):
        if 2020 <= year <= 2024:
            notes = "NEDB value: unit label corrected to annual energy (kWh for NESCO, MWh for Kainji/Jebba/Shiroro)."
            if plant in ("Kainji", "Jebba") and year > 2020:
                continue  # not available
            if plant == "Shiroro" and year > 2021:
                continue
            plant_rows.append({
                "year": year,
                "plant": plant,
                "gwh": gwh,
                "source": "Nigerian Energy Data Bank",
                "confidence": "high",
                "notes": notes,
            })

# Academic candidate rows: Adoghe et al. 2023 for 2020 (noted as inconsistent)
adoghe_2020 = {
    "Kainji": 362.55,
    "Jebba": 356.66,
    "Shiroro": 331.11,
}
for plant, gwh in adoghe_2020.items():
    plant_rows.append({
        "year": 2020,
        "plant": plant,
        "gwh": gwh,
        "source": "Adoghe et al. 2023 Heliyon (PMC10010986)",
        "confidence": "low",
        "notes": (
            "Academic plant-level figure for 2020. Values are an order of magnitude lower than NEDB/Okakwu "
            "for the same plants and are not used for the reconciled total."
        ),
    })

# Placeholder rows for missing years/plants (null gwh)
missing_note = "No direct annual GWh record found in reviewed sources;"
for plant in plants:
    available = set(nedb_hydro.get(plant, {}).keys())
    for year in range(2020, 2025):
        if year not in available:
            plant_rows.append({
                "year": year,
                "plant": plant,
                "gwh": "null",
                "source": "not found",
                "confidence": "low",
                "notes": f"{missing_note} estimate would require NERC quarterly dispatch data or Elec-T scraping.",
            })

# Add Zungeru and Gurara as known plants with no annual GWh data
for plant in ("Zungeru", "Gurara"):
    for year in range(2020, 2025):
        plant_rows.append({
            "year": year,
            "plant": plant,
            "gwh": "null",
            "source": "not found",
            "confidence": "low",
            "notes": "Identified hydro asset; no annual generation GWh record located in this research pass.",
        })

plant_fieldnames = ["year", "plant", "gwh", "source", "confidence", "notes"]
plant_rows.sort(key=lambda r: (r["year"], r["plant"], r["source"] == ""))
plant_path = OUT_DIR / "hydro_plants_annual_gwh.csv"
write_csv(plant_path, plant_rows, plant_fieldnames)
print(f"Wrote {plant_path} with {len(plant_rows)} rows.")

# ---------------------------------------------------------------------------
# Consolidation decision markdown
# ---------------------------------------------------------------------------
md = """# Grid-electricity consolidation decision log

Generated: 2026-06-19
Scope: Reconcile all recent research into authoritative Nigeria grid generation datasets (2020–2024) for NEFDB.

## 1. Summary of sources reviewed

| Source | Type | Years available | What it provides |
|---|---|---|---|
| Nigerian Energy Data Bank (NEDB) | Nigeria official plant records | 1962–2022 (partial) | Annual hydro generation by plant (Kainji, Jebba, Shiroro, NESCO). Unit labels required correction. |
| Ember – Yearly Electricity Data | International database | 2000–2024 | Country-level generation by fuel (TWh). |
| Our World in Data (OWID) energy data | International database | 1900–2024 | Same underlying data as Ember for Nigeria. |
| IRENA IRENASTAT | International database | 2020–2023 | Generation by technology (GWh), including renewables, hydro, solar, biomass, oil, gas. |
| World Bank WDI API | International database | 2015–2023 | Share-of-output indicators only (gas, hydro, renewables, oil, coal). |
| IEA Country Profile Nigeria | International / government | 2022 | Total generation and gas/hydro/bioenergy shares. |
| EIA Country Analysis Brief 2025 | International / government | 2023 | Total generation and gas/hydro shares. |
| Guardian Nigeria summary of NERC 2024 data | Nigeria official (via press) | 2024 | Total generation and hydro output for 2024. |
| UNDP Nigeria Energy Finance Assessment (2020) | Alt / development report | 2020 | Gas/hydro shares only, no absolute total. |
| Adoghe et al. (2023) Heliyon | Academic | 2017–2020 | Plant-level hydro GWh; 2020 figures conflict with official records. |
| Okakwu et al. (2019) AZOJETE | Academic | 2008–2017 | Kainji total energy MWh; 2016/2017 values match NEDB. |
| Salisu et al. (2025) IJISETR | Academic | 2012–2022 | Kainji monthly mean values; unit ambiguous and not used. |

## 2. Reconciliation rules used

1. **Use Nigeria official / plant-root data when directly available.** NEDB hydro plant records are preferred for 2020. Guardian Nigeria/NERC figures are preferred for 2024 total and hydro.
2. **Use a consistent international time series for national totals when Nigeria official totals are not available.** Ember/OWID provide an identical, continuous 2020–2024 series and are used as the national total anchor for 2020–2023.
3. **Fill fuel details from the most granular available source.** Solar, biomass and oil are taken from IRENA (2020–2023) and Ember/OWID (2024) because they provide explicit values.
4. **Gas is the residual** after subtracting hydro, solar, wind, biomass, oil and coal from total generation.
5. **Do not invent plant-level data.** Missing years/plants are written as empty cells in the hydro plant file.
6. **When sources conflict, include all candidate rows in the national mix file** and mark the reconciled value with source = `NEFDB reconciled`.

## 3. Year-by-year decisions

### 2020
- **Total:** Ember/OWID 38,000 GWh.
- **Hydro:** NEDB plant sum = 8,341.99 GWh (Kainji 2,953.28 + Jebba 2,678.91 + Shiroro 2,634.21 + NESCO 75.59).
- **Solar/biomass/oil:** IRENA (115.62 / 40.78 / 17.45 GWh).
- **Gas residual:** 29,484.16 GWh.
- **Rationale:** This is the only year with complete official plant-level hydro records. NEDB hydro is ~8.5% higher than Ember hydro (7,690 GWh); the plant-level official sum is preferred.

### 2021
- **Total:** Ember/OWID 39,200 GWh.
- **Hydro, solar, biomass, oil:** IRENA (9,153.81 / 163.60 / 61.12 / 17.45 GWh).
- **Gas residual:** 29,804.02 GWh.
- **Rationale:** NEDB records for Kainji and Jebba stop at 2020, so IRENA hydro is used. NESCO 2021 = 45.66 GWh but is not a full hydro picture.

### 2022
- **Total:** Ember/OWID 38,000 GWh (IEA gives a closely aligned 37,916 GWh).
- **Hydro, solar, biomass, oil:** IRENA (9,235.35 / 207.13 / 59.51 / 20.08 GWh).
- **Gas residual:** 28,477.93 GWh.
- **Rationale:** IEA and Ember/OWID totals are within 0.2%; IRENA provides the fuel breakdown.

### 2023
- **Total:** Ember/OWID 40,920 GWh.
- **Hydro, solar, biomass, oil:** IRENA (9,426.41 / 267.96 / 62.06 / 46.36 GWh).
- **Gas residual:** 31,117.21 GWh.
- **Rationale:** EIA’s Nigeria Brief gives a notably higher total (42,500 GWh) and is included as a candidate. Ember/OWID are retained as the anchor for consistency with the rest of the time series.

### 2024
- **Total and hydro:** Guardian Nigeria / NERC (37,093.70 GWh total; hydro 11,469.85 GWh, 30.92%).
- **Solar/biomass:** Ember/OWID (130 / 60 GWh).
- **Gas residual:** 25,433.85 GWh.
- **Rationale:** Nigeria official NERC-sourced total and hydro are preferred over international estimates. The hydro share is materially higher than Ember/OWID (~24.5%), reflecting the importance of hydropower during the 2024 gas shortfall.

## 4. Plant-level hydro decisions

| Plant | Best source | Coverage | Notes |
|---|---|---|---|
| Kainji | NEDB | 2020 only (1969–2020) | Values treated as MWh. 2020 = 2,953.28 GWh. Adoghe et al. 2020 figure (362.55 GWh) rejected as inconsistent with NEDB/Okakwu. |
| Jebba | NEDB | 2020 only (1986–2020) | Values treated as MWh. 2020 = 2,678.91 GWh. Adoghe et al. 2020 figure (356.66 GWh) rejected. |
| Shiroro | NEDB | 2020–2021 (1990–2021) | Label says Bill.Cubic Meter but numeric magnitudes are MWh. 2020 = 2,634.21 GWh, 2021 = 2,401.20 GWh. Adoghe 2020 figure (331.11 GWh) rejected. |
| NESCO | NEDB | 2020–2022 (1962–2022) | Unit label is MWh but values are actually kWh. 2020 = 75.59 GWh, 2021 = 45.66 GWh, 2022 = 12.71 GWh. |
| Zungeru | — | None | 700 MW commissioned June 2023; no annual GWh record found. |
| Gurara | — | None | 30 MW; typical ~115 GWh/year cited by NSP, but no verified annual series found. |

## 5. Known gaps and conflicts

1. **NEDB hydro records stop in 2020 for Kainji/Jebba, 2021 for Shiroro, 2022 for NESCO.** Filling 2021–2024 plant-level hydro requires NERC quarterly reports, TCN operational data or scraping the Elec-T dashboard.
2. **Gas totals disagree across sources.** EIA 2023 (42,500 GWh) is ~1,580 GWh above Ember/OWID; IRENA 2023 (37,402 GWh) is ~3,500 GWh below. The selected reconciled totals keep the Ember/OWID anchor.
3. **2024 hydro share conflict.** Guardian/NERC reports 30.92% hydro while Ember/OWID reports ~24.5%. The official figure is preferred but it creates a step-change in the series.
4. **Oil/coal/wind generation is negligible** in all sources. IRENA reports small oil-fired generation (17–46 GWh/year) while Ember/OWID report zero.
5. **Adoghe et al. (2023) plant-level hydro figures for 2020 are unusable** without clarification; they are anomalously low compared with NEDB and Okakwu et al. (2019).
6. **World Bank and UNDP provide shares only** and are used for cross-checking rather than as absolute primary sources.

## 6. Recommendation: can we compute a better national location-based EF?

**Yes, for 2020 and 2024; partial for 2021–2023.**

- **2020** now has a strong, plant-level hydro total (8,342 GWh) and a verifiable gas residual (29,484 GWh). The previous NEFDB location-based EF of 0.5717 kgCO₂e/kWh was based on only 75.6 GWh of hydro because Kainji/Jebba/Shiroro were excluded. Recomputing with the reconciled 2020 mix will lower the EF because hydro displaces gas.
- **2024** has an official total and a large hydro share (30.92%). This supports a noticeably lower grid EF than the 2020 value.
- **2021–2023** still rely on international database hydro estimates rather than plant-level Nigerian data. The EF for these years should be flagged as **medium confidence** until NERC/TCN plant dispatch records are obtained.

**Next concrete steps:**
1. Extract 2021–2024 quarterly hydro dispatch from NERC annual/quarterly PDFs or from the Elec-T dashboard.
2. Recompute `location_based_ef.csv` and `grid_electricity.ts` using the reconciled generation mix and the same IPCC 2006 stationary-combustion factors.
3. Add uncertainty bands to the new EF values; keep the 2020 WAPP ASB operating/combined margins as a cross-check.
"""

md_path = ROOT / "docs" / "grid_electricity" / "consolidation_decision.md"
md_path.parent.mkdir(parents=True, exist_ok=True)
with open(md_path, "w", encoding="utf-8") as f:
    f.write(md)
print(f"Wrote {md_path}.")

# Summary prints
print(f"\nNational mix rows: {len(all_rows)}")
print(f"Hydro plant rows: {len(plant_rows)}")
print("Reconciled 2020 hydro:", round(hydro_2020, 2), "GWh")
print("Reconciled 2024 hydro:", round(hydro_2024, 2), "GWh (", round(hydro_2024/total_2024*100, 1), "%)")
