"""Recompute every figure quoted in the post from global-electricity-mix.csv.

Run from the download folder:  python3 solar-overtakes-wind-src/calculate.py
Standard library only. Prints the figures as JSON (rounded as the prose rounds).
"""
import csv, json, pathlib

HERE = pathlib.Path(__file__).resolve().parent
rows = {int(r["year"]): {k: float(v) for k, v in r.items() if k != "year" and v != ""}
        for r in csv.DictReader(open(HERE / "global-electricity-mix.csv", newline=""))}
years = sorted(rows)
assert years == list(range(1985, 2026)), years

def rise(col):
    return {y: rows[y][col] - rows[y - 1][col] for y in years[1:]}

solar_rise, wind_rise = rise("solar_electricity_twh"), rise("wind_electricity_twh")
total_rise = rise("electricity_generation_twh")
wind_max_year = max(wind_rise, key=wind_rise.get)
solar_max_year = max(solar_rise, key=solar_rise.get)
first_solar_ahead = min(y for y in years if rows[y]["solar_electricity_twh"] > rows[y]["wind_electricity_twh"])

r = lambda x, n=1: round(x, n)
fossil = lambda y: rows[y]["fossil_share_elec_pct"] * rows[y]["electricity_generation_twh"] / 100  # TWh, derived
fossil_peak_year = max(years, key=lambda y: rows[y]["fossil_share_elec_pct"])
out = {
    "solar_2025_twh": r(rows[2025]["solar_electricity_twh"]),
    "solar_2024_twh": r(rows[2024]["solar_electricity_twh"]),
    "wind_2025_twh": r(rows[2025]["wind_electricity_twh"]),
    "wind_2024_twh": r(rows[2024]["wind_electricity_twh"]),
    "solar_share_2025_pct": r(rows[2025]["solar_share_elec_pct"]),
    "solar_share_2015_pct": r(rows[2015]["solar_share_elec_pct"]),
    "solar_share_2025_recomputed_pct": r(100 * rows[2025]["solar_electricity_twh"] / rows[2025]["electricity_generation_twh"]),
    "solar_rise_2025_twh": r(solar_rise[2025]),
    "solar_largest_rise_year": solar_max_year,
    "wind_largest_rise_twh": r(wind_rise[wind_max_year]),
    "wind_largest_rise_year": wind_max_year,
    "total_rise_2025_twh": r(total_rise[2025]),
    "solar_pct_of_total_rise_2025": r(100 * solar_rise[2025] / total_rise[2025]),
    "wind_rise_2025_twh": r(wind_rise[2025]),
    "wind_pct_of_total_rise_2025": r(100 * wind_rise[2025] / total_rise[2025]),
    "solar_wind_pct_of_total_rise_2025": r(100 * (solar_rise[2025] + wind_rise[2025]) / total_rise[2025]),
    "other_rise_2025_twh": r(total_rise[2025] - solar_rise[2025] - wind_rise[2025]),
    "other_pct_of_total_rise_2025": r(100 * (total_rise[2025] - solar_rise[2025] - wind_rise[2025]) / total_rise[2025]),
    "fossil_2024_twh": r(fossil(2024), 0),
    "fossil_2025_twh": r(fossil(2025), 0),
    "fossil_change_2025_twh": r(fossil(2025) - fossil(2024)),
    "fossil_change_2025_pct": r(100 * (fossil(2025) / fossil(2024) - 1)),
    "fossil_share_peak_pct": r(rows[fossil_peak_year]["fossil_share_elec_pct"]),
    "fossil_share_peak_year": fossil_peak_year,
    "fossil_share_1985_1999_min_pct": r(min(rows[y]["fossil_share_elec_pct"] for y in range(1985, 2000))),
    "fossil_share_1985_1999_max_pct": r(max(rows[y]["fossil_share_elec_pct"] for y in range(1985, 2000))),
    "solar_lead_over_wind_2025_pct": r(100 * (rows[2025]["solar_electricity_twh"] / rows[2025]["wind_electricity_twh"] - 1)),
    "first_year_solar_ahead_of_wind": first_solar_ahead,
    "fossil_share_2025_pct": r(rows[2025]["fossil_share_elec_pct"]),
    "fossil_share_1985_pct": r(rows[1985]["fossil_share_elec_pct"]),
    "solar_twh_1985": rows[1985]["solar_electricity_twh"],
    "rows": len(years),
}
print(json.dumps(out, indent=2))
