pypsa-eur/scripts/build_population_weighted_energy_totals.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

42 lines
1.3 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
2024-02-19 15:21:48 +00:00
# SPDX-FileCopyrightText: : 2020-2024 The PyPSA-Eur Authors
2023-03-06 17:49:23 +00:00
#
# SPDX-License-Identifier: MIT
"""
2023-03-09 11:45:43 +00:00
Distribute country-level energy demands by population.
"""
import pandas as pd
2024-02-12 10:53:20 +00:00
from _helpers import set_scenario_config
if __name__ == "__main__":
if "snakemake" not in globals():
2023-03-06 18:09:45 +00:00
from _helpers import mock_snakemake
snakemake = mock_snakemake(
"build_population_weighted_energy_totals",
kind="heat",
2024-03-14 13:31:18 +00:00
clusters=60,
)
2024-02-12 10:53:20 +00:00
set_scenario_config(snakemake)
config = snakemake.config["energy"]
if snakemake.wildcards.kind == "heat":
years = pd.date_range(freq="h", **snakemake.params.snapshots).year.unique()
assert len(years) == 1, "Currently only works for single year."
data_year = years[0]
else:
data_year = int(config["energy_totals_year"])
pop_layout = pd.read_csv(snakemake.input.clustered_pop_layout, index_col=0)
totals = pd.read_csv(snakemake.input.energy_totals, index_col=[0, 1])
totals = totals.xs(data_year, level="year")
nodal_totals = totals.loc[pop_layout.ct].fillna(0.0)
nodal_totals.index = pop_layout.index
nodal_totals = nodal_totals.multiply(pop_layout.fraction, axis=0)
nodal_totals.to_csv(snakemake.output[0])