forked from codeformuenster/klimawatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_plots.py
427 lines (363 loc) · 14 KB
/
generate_plots.py
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
# coding=utf-8
import json # writing json
import os # possibility to delete files
import sys # reading command line arguments
import textwrap # wrapping long lines
import numpy as np # make it easier with numeric values
import pandas
import plotly.graph_objects as go # plots
from scipy.stats import linregress # for computing the trend
# read data
if len(sys.argv) <= 1:
print("No city given, plotting data for Münster ('data/muenster.csv')")
city = "muenster"
df = pandas.read_csv("data/muenster.csv")
else:
print("Plotting data for " + sys.argv[1])
city = sys.argv[1]
try:
df = pandas.read_csv("data/" + city + ".csv")
except:
print(
"File not found (or error in file). Does the file data/" + city + ".csv",
"exist? Is it valid?",
)
exit(1)
with open("data/colors.json", "r") as color_filehandle:
color_dict = json.loads(color_filehandle.read())
# create plot
fig = go.Figure()
start_year = {}
# look for category-wise start_year
for cat in set(df.category):
if cat == "Einwohner":
continue
start_year[str(cat)] = df.loc[
(df.category == cat) & (df.type == "real"), "year"
].min()
emission_start = {}
# compute category-wise percentage (compared to start)
for cat in set(df.category):
if cat != "Einwohner":
emission_start[str(cat)] = df[
(df.category == cat) & (df.year == start_year[cat]) & (df.type == "real")
].co2.values[0]
df.loc[df.category == cat, "percentage"] = (
df[df.category == cat].co2.astype(float) / emission_start[str(cat)]
)
# set() only lists unique values
# this loop plots all categories present in the csv, if type is either "real" or "geplant"
for cat in set(df.category):
if cat == "Einwohner":
continue
subdf = df[(df.category == cat)]
subdf_real = subdf[subdf.type == "real"]
if cat.lower() in color_dict.keys():
cat_color = color_dict[cat.lower()]
else:
print(
f"Missing color definition for category {cat.lower()}. Add it to data/colors.json"
)
cat_color = color_dict["sonstiges"]
# add the real part as solid lines and markers
fig.add_trace(
go.Scatter(
x=subdf_real.year,
y=subdf_real.co2,
name=cat + ", real",
mode="lines+markers",
legendgroup=cat,
text=subdf_real.percentage,
line=dict(color=cat_color),
hovertemplate="<b>tatsächliche</b> Emissionen, Kategorie: "
+ cat
+ "<br>Jahr: %{x}<br>"
+ "CO<sub>2</sub>-Emissionen (tausend Tonnen): %{y:.1f}<br>"
+ "Prozent von Emissionen "
+ str(start_year[cat])
+ ": %{text:.0%}"
+ "<extra></extra>",
) # no additional legend text in tooltip
)
subdf_planned = subdf[subdf.type == "geplant"]
fig.add_trace(
go.Scatter(
x=subdf_planned.year,
y=subdf_planned.co2,
name=cat + ", geplant",
mode="lines+markers",
line=dict(dash="dash", color=cat_color),
legendgroup=cat,
text=subdf_planned.percentage,
hovertemplate="<b>geplante</b> Emissionen, Kategorie: "
+ cat
+ "<br>Jahr: %{x}<br>"
+ "CO<sub>2</sub>-Emissionen (tausend Tonnen): %{y:.1f}<br>"
+ "Prozent von Emissionen "
+ str(start_year[cat])
+ ": %{text:.0%}"
+ "<extra></extra>",
) # no additional legend text in tooltip
)
# compute trend based on current "Gesamt" data
subdf = df[df.category == "Gesamt"]
subdf_real = subdf[subdf.type == "real"]
if len(subdf) == 0 or len(subdf_real) == 0:
raise ValueError(
"The data is missing entries in a category 'Gesamt' with type 'real'. Please add them."
)
# variables to write to JSON later on
years_past_total_real = list(subdf_real.year)
values_past_total_real = list(subdf_real.co2)
trend_plot_name = "Trend"
# compute trend beginning later than 1990 (if user wants it because data are missing)
if len(sys.argv) == 3:
print("Computing trend from", sys.argv[2], "onwards")
subdf_real = subdf_real[subdf_real.year > int(sys.argv[2])]
trend_plot_name = "Trend (ab " + sys.argv[2] + ")"
slope, intercept, r, p, stderr = linregress(subdf_real.year, subdf_real.co2)
# print info about trend
print("linearer Trend: Steigung: ", slope, "Y-Achsenabschnitt: ", intercept, "R^2: ", r)
# compute remaining paris budget
last_emissions = df[df.note == "last_emissions"].co2.values
if len(last_emissions) == 0:
print(
"No 'last_emissions' keyword found. You need to mark the last measured total emission with this keyword in the note column. Exiting."
)
exit()
else:
last_emissions = last_emissions[0]
# see https://scilogs.spektrum.de/klimalounge/wie-viel-co2-kann-deutschland-noch-ausstossen/
# remaining budget for germany from beginning 2019 onwards
paris_budget_germany_from_jan_2019 = 7300000
inhabitants_germany = 83019213
paris_budget_per_capita_from_jan_2019 = paris_budget_germany_from_jan_2019 / inhabitants_germany
# take last 'Einwohner'-entry as reference
paris_budget_full_city_from_jan_2019 = (
paris_budget_per_capita_from_jan_2019 * df[df.type == "Einwohner"].iloc[-1].co2
)
# substract individual CO2 use; roughly 40%, see https://uba.co2-rechner.de/
paris_budget_wo_individual_city_from_jan_2019 = paris_budget_full_city_from_jan_2019 * 0.6
# substract already emitted CO2 from 2019 onwards
# that is: emissions from 2019 and 2020
# data for these years are most likely not available so we use the trend data for 2019 and 2020
last_emissions_year = df[df.note == "last_emissions"].year.values
if last_emissions_year < 2019: # use trend data, no real data given
emissions_2019 = slope * 2019 + intercept
emissions_2020 = slope * 2020 + intercept
print("No emission data for 2019 given, using trend data for 2019: ", emissions_2019)
print("No emission data for 2020 given, using trend data for 2020: ", emissions_2020)
elif last_emissions_year == 2019:
emissions_2019 = last_emissions
emissions_2020 = slope * 2020 + intercept
print("No emission data for 2020 given, using trend data for 2020: ", emissions_2020)
elif last_emissions_year == 2020:
emissions_2019 = subdf_real[subdf_real.year == 2019]
emissions_2020 = last_emissions
paris_budget_wo_individual_city_from_jan_2021 = (
paris_budget_wo_individual_city_from_jan_2019 - emissions_2019 - emissions_2020
)
# compute slope for linear reduction of paris budget
# We know the starting point b (in 2021), the area under the curve (remaining budget) and the function (m*x + b), but not the end point
# solve for m / slope to get a linear approximation
paris_slope = (-pow(emissions_2020, 2)) / (2 * paris_budget_wo_individual_city_from_jan_2021)
years_to_climate_neutral = -emissions_2020 / paris_slope
full_years_to_climate_neutral = int(np.round(years_to_climate_neutral))
# add final year of paris budget to trend data, if it is not included yet
paris_target_year = 2021 + full_years_to_climate_neutral
trend_years = subdf.year.copy()
if trend_years.iloc[-1] < paris_target_year:
trend_years.loc[trend_years.index[-1] + 1] = paris_target_year
# plot trend
fig.add_trace(
go.Scatter(
x=trend_years,
y=slope * trend_years + intercept,
name=trend_plot_name,
mode="lines",
line=dict(dash="dot", color=color_dict["trend"]),
legendgroup="future",
text=(slope * trend_years + intercept) / emission_start["Gesamt"],
hovertemplate="<b>bisheriger "
+ trend_plot_name
+ "</b>"
+ "<br>Jahr: %{x}<br>"
+ "CO<sub>2</sub>-Emissionen (tausend Tonnen): %{y:.1f}<br>"
+ "Prozent von Emissionen "
+ str(start_year["Gesamt"])
+ ": %{text:.0%}"
+ "<extra></extra>",
) # no additional legend text in tooltip
)
# plot paris line
future = list(range(0, full_years_to_climate_neutral, 1)) # from 2021 to 2050
future.append(float(years_to_climate_neutral))
future = pandas.DataFrame(np.array(future), columns=["year"])
# TODO: make df instead of (double) calculation inline?
fig.add_trace(
go.Scatter(
x=future.year + 2021,
y=paris_slope * future.year + emissions_2020,
name="Paris berechnet",
mode="lines+markers",
line=dict(dash="dash", color=color_dict["paris"]),
legendgroup="future",
text=(paris_slope * future.year + emissions_2020) / emission_start["Gesamt"],
hovertemplate="<b>Paris-Budget</b>"
+ "<br>Jahr: %{x:.0f}<br>"
+ "CO<sub>2</sub>-Emissionen (tausend Tonnen): %{y:.1f}<br>"
+ "Prozent von Gesamt-Emissionen "
+ str(start_year["Gesamt"])
+ ": %{text:.0%}"
+ "<extra></extra>",
) # no additional legend text in tooltip
)
fig.add_trace(
go.Scatter(
x=[2021],
y=[emission_start["Gesamt"] + (emission_start["Gesamt"] / 30)],
mode="text",
text="heute",
hoverinfo="none",
legendgroup="future",
showlegend=False,
)
)
# horizontal legend; vertical line at 2021
fig.update_layout(
title="Realität und Ziele",
yaxis_title="CO<sub>2</sub> in tausend Tonnen",
xaxis_title="Jahr",
# horizontal legend
legend_orientation="h",
# put legend above plot to avoid overlapping-bug
legend_xanchor="center",
legend_y=-0.25,
legend_x=0.5,
legend_font_size=10,
# disable dragmode for better mobile experience
dragmode=False,
# German number separators
separators=",.",
# vertical "today" line
shapes=[
go.layout.Shape(
type="line", x0=2021, y0=0, x1=2021, y1=emission_start["Gesamt"]
)
],
)
# write plot to file
fig.write_html(
"hugo/layouts/shortcodes/paris_" + city + ".html",
include_plotlyjs=False,
config={"displayModeBar": False},
full_html=False,
auto_open=True,
)
# write computed Paris budget to JSON file for you-draw-it
paris_data = {}
paris_data = {}
paris_data["chart_id"] = "you-draw-it"
max_past_emission = df.loc[(df.type == "real"), "co2"].max()
paris_data["chart"] = {
"heading": "Wie sollte sich der CO2-Ausstoß entwickeln?",
"lastPointShownAt": 2021,
"y_unit": "kt",
"yAxisMax": max_past_emission + 0.1 * max_past_emission,
"data": [],
}
# past data
if start_year["Gesamt"] > 1990:
while start_year["Gesamt"] % 5 != 0:
# go back in time (at most 4 years) to have a larger x-axis
start_year["Gesamt"] = start_year["Gesamt"] - 1
past = range(start_year["Gesamt"], 2021, 5)
for y in past:
try:
yidx = years_past_total_real.index(y)
paris_data["chart"]["data"].append({y: values_past_total_real[yidx]})
except ValueError:
print(
"You-draw-it-chart: Emissions for", y, "unknown. Estimating from the trend."
)
paris_data["chart"]["data"].append({y: slope * y + intercept})
# years with remaining budget
paris_years = future[:-1].year + 2021
budget_per_year = paris_slope * future[:-1].year + emissions_2020
for y in range(len(paris_years)):
if y % 5 == 0: # print only every 5th year
paris_data["chart"]["data"].append({int(paris_years[y]): budget_per_year[y]})
climate_neutral_by = int(np.round(max(paris_years)))
# range every climate-neutral year, because
# we don't know the climate-neutral year and can't do 5-year steps
years_after_budget = range(climate_neutral_by + 1, 2051, 1)
for y in years_after_budget:
if y % 5 == 0: # print only every 5th year
paris_data["chart"]["data"].append({y: 0})
with open("hugo/data/you_draw_it_" + city + ".json", "w", encoding="utf8") as outfile:
json.dump(paris_data, outfile, indent=2, ensure_ascii=False)
##############################################################
## Visualisation of status of modules of Klimaschutzkonzepte##
##############################################################
try:
modules_df = pandas.read_csv("data/" + city + "_sachstand.csv")
except:
print(
"Sachstand file for "
+ city
+ " (data/"
+ city
+ "_sachstand.csv) not found. Not creating module plot."
)
exit(1)
# find unique overarching categories (here: first character of ID)
categories = set()
for c in modules_df["id"]:
categories.add(c[0:1])
## create a single treemap plot for every overarching category
# delete old plot file
try:
os.remove("hugo/layouts/shortcodes/modules_" + city + ".html")
except:
print("No old modules-plot file, skipping deletion")
modules_plot_file = open("hugo/layouts/shortcodes/modules_" + city + ".html", "a")
for cat in categories:
modules_onecat = modules_df[modules_df.id.str.startswith(cat)]
fig_modules = go.Figure(
go.Treemap(
branchvalues="remainder",
ids=modules_onecat["id"],
labels="<b>"
+ modules_onecat["title"]
+ "</b> ("
+ modules_onecat["id"]
+ ")",
parents=modules_onecat["parent"],
values=modules_onecat["priority"],
marker_colors=modules_onecat["assessment"],
text=(modules_onecat["text"]).apply(
lambda txt: "<br>".join(textwrap.wrap(txt, width=100))
),
textinfo="label+text",
hovertext=(
modules_onecat["text"] + " (" + modules_onecat["id"] + ")"
"<br>Priorität: "
+ (modules_onecat["priority"]).astype(str)
+ "<br>Potential: "
+ (modules_onecat["potential"]).astype(str)
).apply(lambda txt: "<br>".join(textwrap.wrap(txt, width=100))),
hoverinfo="text",
pathbar={"visible": True},
insidetextfont={"size": 75},
)
)
fig_modules.update_layout(
margin=dict(r=10, l=10)
# ~ height = 750
)
modules_plot_file.write(
fig_modules.to_html(
include_plotlyjs=False, config={"displayModeBar": False}, full_html=False
)
)
modules_plot_file.close()