Skip to content

Commit

Permalink
Merge branch 'faktaoklimatu:master' into states_selected_emissions_ar…
Browse files Browse the repository at this point in the history
…eas_per_person
  • Loading branch information
matejpiro authored Mar 21, 2023
2 parents 27248b6 + 64822a4 commit d9a2dd5
Show file tree
Hide file tree
Showing 15 changed files with 139,163 additions and 344 deletions.
22,281 changes: 22,281 additions & 0 deletions data/chmi/B2BTUR01.csv

Large diffs are not rendered by default.

22,281 changes: 22,281 additions & 0 deletions data/chmi/O1LYSA01.csv

Large diffs are not rendered by default.

22,281 changes: 22,281 additions & 0 deletions data/chmi/P1PKLE01.csv

Large diffs are not rendered by default.

22,281 changes: 22,281 additions & 0 deletions data/chmi/P1PRUZ01.csv

Large diffs are not rendered by default.

22,281 changes: 22,281 additions & 0 deletions data/chmi/P3PRIB01.csv

Large diffs are not rendered by default.

22,281 changes: 22,281 additions & 0 deletions data/chmi/U2LIBC01.csv

Large diffs are not rendered by default.

Large diffs are not rendered by default.

264 changes: 264 additions & 0 deletions notebooks/EU_emissions.ipynb

Large diffs are not rendered by default.

70 changes: 36 additions & 34 deletions notebooks/electricity-mixes-eu.ipynb

Large diffs are not rendered by default.

57 changes: 51 additions & 6 deletions notebooks/regional-emissions-by-category-CZ.ipynb

Large diffs are not rendered by default.

19 changes: 6 additions & 13 deletions notebooks/regional-emissions-by-category-EU.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 1,
"id": "ac15e06a",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -63,7 +63,7 @@
" 'sum': ['CRF3'],\n",
" 'color': '#1ecfbd'}, \n",
" {'code': 'CRF2', # vnitřní graf, políčko 6 PRŮMYSLOVÉ PROCESY\n",
" 'label': 'Průmyslové procesy (výroba)',\n",
" 'label': 'Průmyslové procesy (výroba cementu, oceli...)',\n",
" 'sum': ['CRF2'],\n",
" 'color': '#7363bd'},\n",
" {'code': 'CRF5', # vnitřní graf, políčko 7 ODPADOVÉ HOSPODÁŘSTVÍ\n",
Expand All @@ -81,7 +81,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 2,
"id": "733a4bcf-afcb-4d78-89e7-a0481009b708",
"metadata": {},
"outputs": [],
Expand All @@ -91,8 +91,8 @@
},
{
"cell_type": "code",
"execution_count": 19,
"id": "9c57b465-8734-4ca1-b6fe-1831e5fb9f47",
"execution_count": 3,
"id": "f3ddef4f",
"metadata": {},
"outputs": [
{
Expand All @@ -111,16 +111,9 @@
"# 'EU27_2020' for EU since 2020 (without UK)\n",
"STATE = 'EU28'\n",
"YEAR = 2019\n",
"\n",
"create_plot(STATE, YEAR, definition)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2e9fbf0b-6680-435e-84a4-bdc0ee067d71",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down
36 changes: 24 additions & 12 deletions notebooks/regional-emissions-by-category-SK.ipynb

Large diffs are not rendered by default.

75 changes: 63 additions & 12 deletions notebooks/regional_emissions_by_category_lib/emission_plots.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import matplotlib.pyplot as plt
import pandas as pd
import eurostat

TOTAL_EMISSION = 'TOTX4_MEMONIA' # global variable
HEADER_LINE_START = 16 # global variable


def _get_data(state, year):
Expand Down Expand Up @@ -30,26 +32,70 @@ def _get_sum(keys, df):
return sum


def _add_sums_and_reminder(definition, total_value_code, perc_dict, df):
"""Computes the sum values and a reminder.
perc_dict gathers percents for each wedge.
So we can show them in outer chart labels.
def _add_powerplant_data(source_df, year, powerplants):
"""Import data from xls.
Select certain data.
Rename columns, set index,
add to core dataframe.
"""
# load excel
df_input = pd.read_excel("verified_emissions_2021_en.xlsx", header=HEADER_LINE_START)

# adjust excel
df = df_input.loc[df_input["PERMIT_IDENTIFIER"].isin(powerplants)]
df = df.rename(columns={f'VERIFIED_EMISSIONS_{year}': "value"})
df = df.set_index('PERMIT_IDENTIFIER')["value"]
df = df.div(1000000)
df = pd.DataFrame(df)

# join dataframes
new_df = pd.concat([source_df, df])
df = new_df
return df


def _add_line_to_df(wedge_code, wedge_value, df):
df.loc[wedge_code, 'value'] = wedge_value # add a new line to current df


def _create_powerplant_allowances_list(definition):
"""Creates list of powerplants with allowances from definition"""
allowances = []

for inner_cat in definition:
if "breakdown" not in inner_cat:
continue
for outer_cat in inner_cat["breakdown"]:
if 'allowances' in outer_cat:
allowances += outer_cat['sum']

return(allowances)


def _add_sums_and_reminder(definition, total_value_code, perc_dict, df):
cumulative_sum = 0
total_divider = _get_value(TOTAL_EMISSION, df) # divider for percentage computing

# first pass to compute cumulative_sum
for wedge_def in definition:
wedge_code = wedge_def['code']
if 'sum' in wedge_def:
wedge_value = _get_sum(wedge_def['sum'], df)
_add_line_to_df(wedge_def['code'], wedge_value, df)
cumulative_sum += wedge_value
elif 'reminder' in wedge_def:

# second pass to compute the reminder (if exists)
for wedge_def in definition:
if 'reminder' in wedge_def:
wedge_value = _get_value(total_value_code, df) - cumulative_sum
perc_dict[wedge_code] = (
wedge_value / total_divider)
df.loc[wedge_code, 'value'] = wedge_value # add a new line to current df
_add_line_to_df(wedge_def['code'], wedge_value, df)

# third pass to compute values for outer perc dict (if in second pass, total outer chart sum > 1 so plot gets error)
for wedge_def in definition:
wedge_value = _get_value(wedge_def['code'], df)
perc_dict[wedge_def['code']] = (wedge_value / total_divider)


def _compute_values(definition, df, outer_perc_dict):
def _compute_values(definition, outer_perc_dict, df):
"""Compute values for inner and outer chart structure"""
_add_sums_and_reminder(definition, TOTAL_EMISSION, {}, df)

Expand Down Expand Up @@ -104,7 +150,7 @@ def _draw_plot(state, year, plot_dict, outer_perc_dict, df):
# title
plt.title(f'Emise skleníkových plynů pro {state} za rok {year} v CO2 ekviv.', fontsize=20)

# the number in the middle
# show number in the middle
total_emisions = round(df.loc[TOTAL_EMISSION, 'value'], 2)
ax.annotate(total_emisions, xy=(0.1, 0.1), xytext=(-0.15, -0.01), fontsize=25)

Expand All @@ -115,9 +161,14 @@ def create_plot(state, year, definition):
"""Call the main functions together"""
df = _get_data(state, year)

powerplants = _create_powerplant_allowances_list(definition)

df = _add_powerplant_data(df, year, powerplants)

outer_perc_dict = {}
_compute_values(definition, df, outer_perc_dict)
_compute_values(definition, outer_perc_dict, df)

plot_dict = _create_plot_lists(definition, outer_perc_dict)

_draw_plot(state, year, plot_dict, outer_perc_dict, df)

2,918 changes: 2,918 additions & 0 deletions notebooks/teplotni-extremy-cr.ipynb

Large diffs are not rendered by default.

Binary file added notebooks/verified_emissions_2021_en.xlsx
Binary file not shown.

0 comments on commit d9a2dd5

Please sign in to comment.