-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathplot_summary.py
More file actions
516 lines (408 loc) · 14.2 KB
/
plot_summary.py
File metadata and controls
516 lines (408 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# SPDX-FileCopyrightText: Contributors to PyPSA-Eur <https://github.com/pypsa/pypsa-eur>
#
# SPDX-License-Identifier: MIT
"""
Creates plots from summary CSV files.
"""
import logging
import matplotlib.gridspec as gridspec
import matplotlib.pyplot as plt
import pandas as pd
from scripts._helpers import configure_logging, rename_techs, set_scenario_config
from scripts.prepare_sector_network import co2_emissions_year
logger = logging.getLogger(__name__)
plt.style.use("bmh")
# consolidate and rename
preferred_order = pd.Index(
[
"transmission lines",
"hydroelectricity",
"hydro reservoir",
"run of river",
"pumped hydro storage",
"solid biomass",
"biogas",
"onshore wind",
"offshore wind",
"offshore wind (AC)",
"offshore wind (DC)",
"offshore floating wind",
"wave farshore",
"wave nearshore",
"wave shallow",
"solar PV",
"solar thermal",
"solar rooftop",
"solar",
"floating solar",
"building retrofitting",
"ground heat pump",
"air heat pump",
"heat pump",
"resistive heater",
"power-to-heat",
"gas-to-power/heat",
"CHP",
"OCGT",
"gas boiler",
"gas",
"natural gas",
"methanation",
"ammonia",
"hydrogen storage",
"power-to-gas",
"power-to-liquid",
"battery storage",
"hot water storage",
"CO2 sequestration",
]
)
def plot_costs():
cost_df = pd.read_csv(
snakemake.input.costs, index_col=list(range(3)), header=list(range(n_header))
)
df = cost_df.groupby("carrier").sum()
# convert to billions
df = df / 1e9
df = df.groupby(df.index.map(rename_techs)).sum()
to_drop = df.index[df.max(axis=1) < snakemake.params.plotting["costs_threshold"]]
logger.info(
f"Dropping technology with costs below {snakemake.params['plotting']['costs_threshold']} EUR billion per year"
)
logger.debug(df.loc[to_drop])
df = df.drop(to_drop)
logger.info(f"Total system cost of {round(df.sum().iloc[0])} EUR billion per year")
new_index = preferred_order.intersection(df.index).append(
df.index.difference(preferred_order)
)
# new_columns = df.sum().sort_values().index
fig, ax = plt.subplots(figsize=(12, 8))
df.loc[new_index].T.plot(
kind="bar",
ax=ax,
stacked=True,
color=[snakemake.params.plotting["tech_colors"][i] for i in new_index],
)
handles, labels = ax.get_legend_handles_labels()
handles.reverse()
labels.reverse()
ax.set_ylim([0, snakemake.params.plotting["costs_max"]])
ax.set_ylabel("System Cost [EUR billion per year]")
ax.set_xlabel("")
ax.grid(axis="x")
ax.legend(
handles, labels, ncol=1, loc="upper left", bbox_to_anchor=[1, 1], frameon=False
)
fig.savefig(snakemake.output.costs, bbox_inches="tight")
plt.close(fig)
def plot_energy():
energy_df = pd.read_csv(
snakemake.input.energy, index_col=list(range(2)), header=list(range(n_header))
)
df = energy_df.groupby("carrier").sum()
# convert MWh to TWh
df = df / 1e6
df = df.groupby(df.index.map(rename_techs)).sum()
to_drop = df.index[
df.abs().max(axis=1) < snakemake.params.plotting["energy_threshold"]
]
logger.info(
f"Dropping all technology with energy consumption or production below {snakemake.params['plotting']['energy_threshold']} TWh/a"
)
logger.debug(df.loc[to_drop])
df = df.drop(to_drop)
logger.info(f"Total energy of {round(df.sum().iloc[0])} TWh/a")
if df.empty:
fig, ax = plt.subplots(figsize=(12, 8))
fig.savefig(snakemake.output.energy, bbox_inches="tight")
plt.close(fig)
return
new_index = preferred_order.intersection(df.index).append(
df.index.difference(preferred_order)
)
# new_columns = df.columns.sort_values()
fig, ax = plt.subplots(figsize=(12, 8))
logger.debug(df.loc[new_index])
df.loc[new_index].T.plot(
kind="bar",
ax=ax,
stacked=True,
color=[snakemake.params.plotting["tech_colors"][i] for i in new_index],
)
handles, labels = ax.get_legend_handles_labels()
handles.reverse()
labels.reverse()
ax.set_ylim(
[
snakemake.params.plotting["energy_min"],
snakemake.params.plotting["energy_max"],
]
)
ax.set_ylabel("Energy [TWh/a]")
ax.set_xlabel("")
ax.grid(axis="x")
ax.legend(
handles, labels, ncol=1, loc="upper left", bbox_to_anchor=[1, 1], frameon=False
)
fig.savefig(snakemake.output.energy, bbox_inches="tight")
plt.close(fig)
def plot_balances():
co2_carriers = ["co2", "co2 stored", "process emissions"]
balances_df = pd.read_csv(
snakemake.input.balances, index_col=list(range(3)), header=list(range(n_header))
)
balances = {k: df for k, df in balances_df.groupby("bus_carrier")}
balances["energy"] = balances_df.groupby(["component", "carrier"]).sum()
for bus_carrier, df in balances.items():
df = df.groupby("carrier").sum()
# convert MWh to TWh
df = df / 1e6
df = df.groupby(df.index.map(rename_techs)).sum()
to_drop = df.index[
df.abs().max(axis=1) < snakemake.params.plotting["energy_threshold"] / 10
]
units = "MtCO2/a" if bus_carrier in co2_carriers else "TWh/a"
logger.debug(
f"Dropping technology energy balance smaller than {snakemake.params['plotting']['energy_threshold'] / 10} {units}"
)
logger.debug(df.loc[to_drop])
df = df.drop(to_drop)
logger.debug(
f"Total energy balance for {bus_carrier} of {round(df.sum().iloc[0], 2)} {units}"
)
if df.empty:
continue
new_index = preferred_order.intersection(df.index).append(
df.index.difference(preferred_order)
)
new_columns = df.columns.sort_values()
fig, ax = plt.subplots(figsize=(12, 8))
df.loc[new_index, new_columns].T.plot(
kind="bar",
ax=ax,
stacked=True,
color=[snakemake.params.plotting["tech_colors"][i] for i in new_index],
)
handles, labels = ax.get_legend_handles_labels()
handles.reverse()
labels.reverse()
if bus_carrier in co2_carriers:
ax.set_ylabel("CO2 [MtCO2/a]")
else:
ax.set_ylabel("Energy [TWh/a]")
ax.set_xlabel("")
ax.grid(axis="x")
ax.legend(
handles,
labels,
ncol=1,
loc="upper left",
bbox_to_anchor=[1, 1],
frameon=False,
)
fig.savefig(
snakemake.output.balances[:-10] + bus_carrier + ".pdf", bbox_inches="tight"
)
plt.close(fig)
def historical_emissions(countries):
"""
Read historical emissions to add them to the carbon budget plot.
"""
# https://www.eea.europa.eu/data-and-maps/data/national-emissions-reported-to-the-unfccc-and-to-the-eu-greenhouse-gas-monitoring-mechanism-16
# downloaded 201228 (modified by EEA last on 201221)
df = pd.read_csv(snakemake.input.co2, encoding="latin-1", low_memory=False)
df.loc[df["Year"] == "1985-1987", "Year"] = 1986
df["Year"] = df["Year"].astype(int)
df = df.set_index(
["Year", "Sector_name", "Country_code", "Pollutant_name"]
).sort_index()
e = pd.Series()
e["electricity"] = "1.A.1.a - Public Electricity and Heat Production"
e["residential non-elec"] = "1.A.4.b - Residential"
e["services non-elec"] = "1.A.4.a - Commercial/Institutional"
e["rail non-elec"] = "1.A.3.c - Railways"
e["road non-elec"] = "1.A.3.b - Road Transportation"
e["domestic navigation"] = "1.A.3.d - Domestic Navigation"
e["international navigation"] = "1.D.1.b - International Navigation"
e["domestic aviation"] = "1.A.3.a - Domestic Aviation"
e["international aviation"] = "1.D.1.a - International Aviation"
e["total energy"] = "1 - Energy"
e["industrial processes"] = "2 - Industrial Processes and Product Use"
e["agriculture"] = "3 - Agriculture"
e["LULUCF"] = "4 - Land Use, Land-Use Change and Forestry"
e["waste management"] = "5 - Waste management"
e["other"] = "6 - Other Sector"
e["indirect"] = "ind_CO2 - Indirect CO2"
e["other LULUCF"] = "4.H - Other LULUCF"
pol = ["CO2"] # ["All greenhouse gases - (CO2 equivalent)"]
if "GB" in countries:
countries.remove("GB")
countries.append("UK")
year = df.index.levels[0][df.index.levels[0] >= 1990]
missing = pd.Index(countries).difference(df.index.levels[2])
if not missing.empty:
logger.warning(
f"The following countries are missing and not considered when plotting historic CO2 emissions: {missing}"
)
countries = pd.Index(df.index.levels[2]).intersection(countries)
idx = pd.IndexSlice
co2_totals = (
df.loc[idx[year, e.values, countries, pol], "emissions"]
.unstack("Year")
.rename(index=pd.Series(e.index, e.values))
)
co2_totals = (1 / 1e6) * co2_totals.groupby(level=0, axis=0).sum() # Gton CO2
co2_totals.loc["industrial non-elec"] = (
co2_totals.loc["total energy"]
- co2_totals.loc[
[
"electricity",
"services non-elec",
"residential non-elec",
"road non-elec",
"rail non-elec",
"domestic aviation",
"international aviation",
"domestic navigation",
"international navigation",
]
].sum()
)
emissions = co2_totals.loc["electricity"]
if options["transport"]:
emissions += co2_totals.loc[[i + " non-elec" for i in ["rail", "road"]]].sum()
if options["heating"]:
emissions += co2_totals.loc[
[i + " non-elec" for i in ["residential", "services"]]
].sum()
if options["industry"]:
emissions += co2_totals.loc[
[
"industrial non-elec",
"industrial processes",
"domestic aviation",
"international aviation",
"domestic navigation",
"international navigation",
]
].sum()
return emissions
def plot_carbon_budget_distribution(input_eurostat, options):
"""
Plot historical carbon emissions in the EU and decarbonization path.
"""
import seaborn as sns
sns.set()
sns.set_style("ticks")
plt.rcParams["xtick.direction"] = "in"
plt.rcParams["ytick.direction"] = "in"
plt.rcParams["xtick.labelsize"] = 20
plt.rcParams["ytick.labelsize"] = 20
emissions_scope = snakemake.params.emissions_scope
input_co2 = snakemake.input.co2
# historic emissions
countries = snakemake.params.countries
e_1990 = co2_emissions_year(
countries,
input_eurostat,
options,
emissions_scope,
input_co2,
year=1990,
)
emissions = historical_emissions(countries)
# add other years https://sdi.eea.europa.eu/data/0569441f-2853-4664-a7cd-db969ef54de0
emissions.loc[2019] = 3.414362
emissions.loc[2020] = 3.092434
emissions.loc[2021] = 3.290418
emissions.loc[2022] = 3.213025
if snakemake.config["foresight"] == "myopic":
path_cb = "results/" + snakemake.params.RDIR + "/csvs/"
co2_cap = pd.read_csv(path_cb + "carbon_budget_distribution.csv", index_col=0)[
["cb"]
]
co2_cap *= e_1990
else:
supply_energy = pd.read_csv(
snakemake.input.balances, index_col=[0, 1, 2], header=[0, 1, 2, 3]
)
co2_cap = (
supply_energy.loc["co2"].droplevel(0).drop("co2").sum().unstack().T / 1e9
)
co2_cap.rename(index=lambda x: int(x), inplace=True)
plt.figure(figsize=(10, 7))
gs1 = gridspec.GridSpec(1, 1)
ax1 = plt.subplot(gs1[0, 0])
ax1.set_ylabel("CO$_2$ emissions \n [Gt per year]", fontsize=22)
# ax1.set_ylim([0, 5])
ax1.set_xlim([1990, snakemake.params.planning_horizons[-1] + 1])
ax1.plot(emissions, color="black", linewidth=3, label=None)
# plot committed and under-discussion targets
# (notice that historical emissions include all countries in the
# network, but targets refer to EU)
ax1.plot(
[2020],
[0.8 * emissions[1990]],
marker="*",
markersize=12,
markerfacecolor="black",
markeredgecolor="black",
)
ax1.plot(
[2030],
[0.45 * emissions[1990]],
marker="*",
markersize=12,
markerfacecolor="black",
markeredgecolor="black",
)
ax1.plot(
[2030],
[0.6 * emissions[1990]],
marker="*",
markersize=12,
markerfacecolor="black",
markeredgecolor="black",
)
ax1.plot(
[2050, 2050],
[x * emissions[1990] for x in [0.2, 0.05]],
color="gray",
linewidth=2,
marker="_",
alpha=0.5,
)
ax1.plot(
[2050],
[0.0 * emissions[1990]],
marker="*",
markersize=12,
markerfacecolor="black",
markeredgecolor="black",
label="EU committed target",
)
for col in co2_cap.columns:
ax1.plot(co2_cap[col], linewidth=3, label=col)
ax1.legend(
fancybox=True, fontsize=18, loc=(0.01, 0.01), facecolor="white", frameon=True
)
plt.grid(axis="y")
path = snakemake.output.balances.split("balances")[0] + "carbon_budget.pdf"
plt.savefig(path, bbox_inches="tight")
plt.close()
if __name__ == "__main__":
if "snakemake" not in globals():
from scripts._helpers import mock_snakemake
snakemake = mock_snakemake("plot_summary")
configure_logging(snakemake)
set_scenario_config(snakemake)
n_header = 3
plot_costs()
plot_energy()
plot_balances()
co2_budget = snakemake.params["co2_budget"]
if (
isinstance(co2_budget, str) and co2_budget.startswith("cb")
) or snakemake.params["foresight"] == "perfect":
options = snakemake.params.sector
plot_carbon_budget_distribution(snakemake.input.eurostat, options)