From e304efbf3c2e13da1423ccd107ab37a67655cb7b Mon Sep 17 00:00:00 2001 From: "daniel.rdt" Date: Wed, 9 Aug 2023 14:35:39 +0200 Subject: [PATCH 1/4] fix in co2_emissions_year function and correspondigly in plot_summary and snakemake rule. Also changes to historical_emissions in plot_summary since some countries are not in eea dataset. --- rules/postprocess.smk | 3 +++ scripts/plot_summary.py | 20 ++++++++++++++++++-- scripts/prepare_sector_network.py | 11 +++++------ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/rules/postprocess.smk b/rules/postprocess.smk index 2618680e..d0909c4e 100644 --- a/rules/postprocess.smk +++ b/rules/postprocess.smk @@ -106,6 +106,8 @@ rule plot_summary: countries=config["countries"], planning_horizons=config["scenario"]["planning_horizons"], sector_opts=config["scenario"]["sector_opts"], + emissions_scope=config["energy"]["emissions"], + eurostat_report_year=config["energy"]["eurostat_report_year"], plotting=config["plotting"], RDIR=RDIR, input: @@ -113,6 +115,7 @@ rule plot_summary: energy=RESULTS + "csvs/energy.csv", balances=RESULTS + "csvs/supply_energy.csv", eurostat=input_eurostat, + co2="data/eea/UNFCCC_v23.csv", output: costs=RESULTS + "graphs/costs.pdf", energy=RESULTS + "graphs/energy.pdf", diff --git a/scripts/plot_summary.py b/scripts/plot_summary.py index e7de5473..f77d01c8 100644 --- a/scripts/plot_summary.py +++ b/scripts/plot_summary.py @@ -387,6 +387,19 @@ def historical_emissions(countries): countries.remove("GB") countries.append("UK") + # Albania (AL) and Bosnia Herzegovina (BA), Montenegro (ME), Macedonia (MK) and Serbia (RS) + # not included in eea historical emission dataset + if "AL" in countries: + countries.remove("AL") + if "BA" in countries: + countries.remove("BA") + if "ME" in countries: + countries.remove("ME") + if "MK" in countries: + countries.remove("MK") + if "RS" in countries: + countries.remove("RS") + year = np.arange(1990, 2018).tolist() idx = pd.IndexSlice @@ -457,9 +470,12 @@ def plot_carbon_budget_distribution(input_eurostat): ax1.set_ylim([0, 5]) ax1.set_xlim([1990, snakemake.params.planning_horizons[-1] + 1]) - path_cb = "results/" + snakemake.params.RDIR + "/csvs/" + path_cb = "results/" + snakemake.params.RDIR + "csvs/" countries = snakemake.params.countries - e_1990 = co2_emissions_year(countries, input_eurostat, opts, year=1990) + emissions_scope = snakemake.params.emissions_scope + report_year = snakemake.params.eurostat_report_year + input_co2 = snakemake.input.co2 + e_1990 = co2_emissions_year(countries, input_eurostat, opts, emissions_scope, report_year, input_co2, year=1990) CO2_CAP = pd.read_csv(path_cb + "carbon_budget_distribution.csv", index_col=0) ax1.plot(e_1990 * CO2_CAP[o], linewidth=3, color="dodgerblue", label=None) diff --git a/scripts/prepare_sector_network.py b/scripts/prepare_sector_network.py index 0c1faacc..03f0e33b 100644 --- a/scripts/prepare_sector_network.py +++ b/scripts/prepare_sector_network.py @@ -191,17 +191,15 @@ def get(item, investment_year=None): def co2_emissions_year( - countries, input_eurostat, opts, emissions_scope, report_year, year + countries, input_eurostat, opts, emissions_scope, report_year, input_co2, year ): """ Calculate CO2 emissions in one specific year (e.g. 1990 or 2018). """ - emissions_scope = snakemake.params.energy["emissions"] - eea_co2 = build_eea_co2(snakemake.input.co2, year, emissions_scope) + eea_co2 = build_eea_co2(input_co2, year, emissions_scope) # TODO: read Eurostat data from year > 2014 # this only affects the estimation of CO2 emissions for BA, RS, AL, ME, MK - report_year = snakemake.params.energy["eurostat_report_year"] if year > 2014: eurostat_co2 = build_eurostat_co2( input_eurostat, countries, report_year, year=2014 @@ -240,12 +238,12 @@ def build_carbon_budget(o, input_eurostat, fn, emissions_scope, report_year): countries = snakemake.params.countries e_1990 = co2_emissions_year( - countries, input_eurostat, opts, emissions_scope, report_year, year=1990 + countries, input_eurostat, opts, emissions_scope, report_year, input_co2, year=1990 ) # emissions at the beginning of the path (last year available 2018) e_0 = co2_emissions_year( - countries, input_eurostat, opts, emissions_scope, report_year, year=2018 + countries, input_eurostat, opts, emissions_scope, report_year, input_co2, year=2018 ) planning_horizons = snakemake.params.planning_horizons @@ -3396,6 +3394,7 @@ if __name__ == "__main__": if not os.path.exists(fn): emissions_scope = snakemake.params.emissions_scope report_year = snakemake.params.eurostat_report_year + input_co2 = snakemake.input.co2 build_carbon_budget( o, snakemake.input.eurostat, fn, emissions_scope, report_year ) From eed52d04aeec6174fb18b25ca644e78cc8dda1ed Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Aug 2023 13:04:35 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- scripts/plot_summary.py | 10 +++++++++- scripts/prepare_sector_network.py | 16 ++++++++++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/scripts/plot_summary.py b/scripts/plot_summary.py index f77d01c8..0da0eb9d 100644 --- a/scripts/plot_summary.py +++ b/scripts/plot_summary.py @@ -475,7 +475,15 @@ def plot_carbon_budget_distribution(input_eurostat): emissions_scope = snakemake.params.emissions_scope report_year = snakemake.params.eurostat_report_year input_co2 = snakemake.input.co2 - e_1990 = co2_emissions_year(countries, input_eurostat, opts, emissions_scope, report_year, input_co2, year=1990) + e_1990 = co2_emissions_year( + countries, + input_eurostat, + opts, + emissions_scope, + report_year, + input_co2, + year=1990, + ) CO2_CAP = pd.read_csv(path_cb + "carbon_budget_distribution.csv", index_col=0) ax1.plot(e_1990 * CO2_CAP[o], linewidth=3, color="dodgerblue", label=None) diff --git a/scripts/prepare_sector_network.py b/scripts/prepare_sector_network.py index 03f0e33b..a4e4e6b7 100644 --- a/scripts/prepare_sector_network.py +++ b/scripts/prepare_sector_network.py @@ -238,12 +238,24 @@ def build_carbon_budget(o, input_eurostat, fn, emissions_scope, report_year): countries = snakemake.params.countries e_1990 = co2_emissions_year( - countries, input_eurostat, opts, emissions_scope, report_year, input_co2, year=1990 + countries, + input_eurostat, + opts, + emissions_scope, + report_year, + input_co2, + year=1990, ) # emissions at the beginning of the path (last year available 2018) e_0 = co2_emissions_year( - countries, input_eurostat, opts, emissions_scope, report_year, input_co2, year=2018 + countries, + input_eurostat, + opts, + emissions_scope, + report_year, + input_co2, + year=2018, ) planning_horizons = snakemake.params.planning_horizons From 49b2cd382da8bf9416955a7e2557a134a3905576 Mon Sep 17 00:00:00 2001 From: Fabian Neumann Date: Tue, 22 Aug 2023 14:43:15 +0200 Subject: [PATCH 3/4] Update scripts/plot_summary.py Co-authored-by: Fabian Hofmann --- scripts/plot_summary.py | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/scripts/plot_summary.py b/scripts/plot_summary.py index 0da0eb9d..be2b4d86 100644 --- a/scripts/plot_summary.py +++ b/scripts/plot_summary.py @@ -387,19 +387,9 @@ def historical_emissions(countries): countries.remove("GB") countries.append("UK") - # Albania (AL) and Bosnia Herzegovina (BA), Montenegro (ME), Macedonia (MK) and Serbia (RS) - # not included in eea historical emission dataset - if "AL" in countries: - countries.remove("AL") - if "BA" in countries: - countries.remove("BA") - if "ME" in countries: - countries.remove("ME") - if "MK" in countries: - countries.remove("MK") - if "RS" in countries: - countries.remove("RS") - + # remove countries which are not included in eea historical emission dataset + countries_to_remove = {"AL", "BA", "ME", "MK", "RS"} + countries = list(set(countries) - countries_to_remove) year = np.arange(1990, 2018).tolist() idx = pd.IndexSlice From 3ccc63aae4f4865cc3b10234d0d7d5e506aa8428 Mon Sep 17 00:00:00 2001 From: Fabian Neumann Date: Tue, 22 Aug 2023 14:49:44 +0200 Subject: [PATCH 4/4] build_carbon_budget: parse input_co2 --- scripts/make_summary.py | 2 +- scripts/plot_summary.py | 2 +- scripts/prepare_sector_network.py | 11 ++++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/scripts/make_summary.py b/scripts/make_summary.py index 56ee98c9..98a6a6d7 100644 --- a/scripts/make_summary.py +++ b/scripts/make_summary.py @@ -711,5 +711,5 @@ if __name__ == "__main__": if snakemake.params.foresight == "myopic": cumulative_cost = calculate_cumulative_cost() cumulative_cost.to_csv( - "results/" + snakemake.params.RDIR + "/csvs/cumulative_cost.csv" + "results/" + snakemake.params.RDIR + "csvs/cumulative_cost.csv" ) diff --git a/scripts/plot_summary.py b/scripts/plot_summary.py index be2b4d86..072c7128 100644 --- a/scripts/plot_summary.py +++ b/scripts/plot_summary.py @@ -549,7 +549,7 @@ def plot_carbon_budget_distribution(input_eurostat): fancybox=True, fontsize=18, loc=(0.01, 0.01), facecolor="white", frameon=True ) - path_cb_plot = "results/" + snakemake.params.RDIR + "/graphs/" + path_cb_plot = "results/" + snakemake.params.RDIR + "graphs/" plt.savefig(path_cb_plot + "carbon_budget_plot.pdf", dpi=300) diff --git a/scripts/prepare_sector_network.py b/scripts/prepare_sector_network.py index a4e4e6b7..11406bff 100644 --- a/scripts/prepare_sector_network.py +++ b/scripts/prepare_sector_network.py @@ -220,7 +220,7 @@ def co2_emissions_year( # TODO: move to own rule with sector-opts wildcard? -def build_carbon_budget(o, input_eurostat, fn, emissions_scope, report_year): +def build_carbon_budget(o, input_eurostat, fn, emissions_scope, report_year, input_co2): """ Distribute carbon budget following beta or exponential transition path. """ @@ -3402,13 +3402,18 @@ if __name__ == "__main__": if "cb" not in o: continue limit_type = "carbon budget" - fn = "results/" + snakemake.params.RDIR + "/csvs/carbon_budget_distribution.csv" + fn = "results/" + snakemake.params.RDIR + "csvs/carbon_budget_distribution.csv" if not os.path.exists(fn): emissions_scope = snakemake.params.emissions_scope report_year = snakemake.params.eurostat_report_year input_co2 = snakemake.input.co2 build_carbon_budget( - o, snakemake.input.eurostat, fn, emissions_scope, report_year + o, + snakemake.input.eurostat, + fn, + emissions_scope, + report_year, + input_co2, ) co2_cap = pd.read_csv(fn, index_col=0).squeeze() limit = co2_cap.loc[investment_year]