Skip to content

Commit

Permalink
Merge pull request #50 from rl-institut/features/plot-scalar-resources
Browse files Browse the repository at this point in the history
Features/plot scalar resources
  • Loading branch information
jnnr authored Nov 24, 2021
2 parents e0727d7 + 129d519 commit 88e77d4
Show file tree
Hide file tree
Showing 10 changed files with 303 additions and 34 deletions.
20 changes: 18 additions & 2 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ examples = [

scenario_list_example = ['examples']

resources = ['conv_pp_scalar']

scenarios = ["toy-scenario", "toy-scenario-2"]

# Target rules
rule plot_grouped_scenarios:
input:
Expand All @@ -30,6 +34,10 @@ rule report_all_examples:
input:
expand("results/{scenario}/report/", scenario=examples)

rule plot_all_resources:
input:
expand("results/_resources/plots/{resource}.png", resource=resources)

rule clean:
shell:
"""
Expand All @@ -56,12 +64,11 @@ rule prepare_conv_pp:
opsd="raw/conventional_power_plants_DE.csv",
gpkg="raw/boundaries_germany_nuts3.gpkg",
b3_regions="raw/b3_regions.yaml",
scalar_template="oemof_b3/schema/scalars.csv",
script="scripts/prepare_conv_pp.py"
output:
"results/_resources/conv_pp_scalar.csv"
shell:
"python scripts/prepare_conv_pp.py {input.opsd} {input.gpkg} {input.b3_regions} {input.scalar_template} {output}"
"python scripts/prepare_conv_pp.py {input.opsd} {input.gpkg} {input.b3_regions} {output}"

rule prepare_feedin:
input:
Expand Down Expand Up @@ -148,6 +155,15 @@ rule plot_dispatch:
shell:
"python scripts/plot_dispatch.py {input} {output}"

rule plot_conv_pp_scalars:
input:
data="results/_resources/{resource}.csv",
script="scripts/plot_conv_pp_scalars.py"
output:
"results/_resources/plots/{resource}_var_{var_name}.png"
shell:
"python {input.script} {input.data} {wildcards.var_name} {output}"

rule report:
input:
template="report/report.md",
Expand Down
2 changes: 1 addition & 1 deletion docs/model_pipeline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ Plotting
:maxdepth: 1
:glob:

plotting/*
plotting/*
6 changes: 6 additions & 0 deletions docs/plotting/plot_scalar_resources.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _plot_scalar_resources_label:

plot_scalar_resources
=====================

.. automodule:: plot_conv_pp
2 changes: 2 additions & 0 deletions oemof_b3/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

labels_dict = load_yaml(os.path.join(dir_name, "config", "labels.yml"))

labels_to_german = load_yaml(os.path.join(dir_name, "config", "labels_to_german.yml"))

colors_csv = pd.read_csv(
os.path.join(dir_name, "config", "colors.csv"), header=[0], index_col=[0]
)
Expand Down
18 changes: 12 additions & 6 deletions oemof_b3/config/colors.csv
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,18 @@ H2 Backpressure CHP,magenta
CH4 Backpressure CHP,brown
CH4 Extraction CHP,darkred

Heat demand,#000000
Heat excess,#7f7f7f
Heat shortage,#7f7f7f
Heat storage,#9467bd

CH4 boiler,#d62728
Heat cen. demand,#000000
Heat cen. excess,#7f7f7f
Heat cen. shortage,#7f7f7f
Heat cen. storage in,#9467bd
Heat cen. storage out,#9467bd
Heat dec. demand,#000000
Heat dec. excess,#7f7f7f
Heat dec. shortage,#7f7f7f
Heat dec. storage out,#9467bd
Heat dec. storage in,#9467bd

CH4 Boiler,#d62728
Heatpump,#2ca02c
res. PtH,#ff7f0e

Expand Down
11 changes: 8 additions & 3 deletions oemof_b3/config/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ electricity-pth: res. PtH
electricity-shortage: Shortage
electricity-transmission: Transmission
h2-gt: H2 GT
heat-demand: Heat demand
heat_central-demand: Heat cen. demand
heat_decentral-demand: Heat dec. demand
hydro-ror: Hydro ROR
solar-pv: PV
wind-onshore: Wind on
h2-bpchp: H2 Backpressure CHP
ch4-excess: CH4 Excess
heat-excess: Heat Excess
heat-storage: Heat Storage
heat_central-excess: Heat cen. excess
heat_central-storage: Heat cen. storage
heat_central-shortage: Heat cen. shortage
heat_decentral-excess: Heat dec. excess
heat_decentral-storage: Heat dec. storage
heat_decentral-shortage: Heat dec. shortage
33 changes: 33 additions & 0 deletions oemof_b3/config/labels_to_german.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Battery in: Batterie in
Battery out: Batterie out
Battery: Batterie
Biomass GT: Biomass GT
Biomass ST: Biomass ST
Biomass: Biomasse
CH4 Backpressure CHP: CH4 Backpressure CHP
CH4 Boiler: CH4 Boiler
CH4 Excess: CH4 Excess
CH4 Extraction CHP: CH4 Extraction CHP
CH4 GT: CH4 GT
CH4: CH4
Curtailment: Curtailment
El. demand: El. demand
El. in: Batterie in
Electrolyzer: Electrolyzer
H2 GT: H2 GT
Hard coal: Steinkohle
Heat demand: Heat demand
Heat Excess: Heat Excess
Heat Storage: Heat Storage
Hydro reservoir: Reservoir
Hydro ROR: Hydro ROR
Lignite: Braunkohle
Nuclear ST: Nuclear ST
Oil: Öl
Other: Andere
PV: PV
res. PtH: res. PtH
Shortage: Shortage
Transmission: Transmission
Wind off: Wind off
Wind on: Wind on
193 changes: 193 additions & 0 deletions scripts/plot_conv_pp_scalars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
r"""
Inputs
-------
resources : str
path of input file with data of prepared conventional power plant data as .csv
target : str
path of output file with plot
Outputs
---------
matplotlib.figure.Figure
plotted conventional power plant data
Description
-------------
The script takes prepared data of conventional power plants from OPSD, prepares the
data further and plots the data.
"""
import os
import sys

import matplotlib.pyplot as plt
import oemoflex.tools.plots as plots
import pandas as pd

import oemof_b3.tools.data_processing as dp
from oemof_b3 import colors_odict, labels_to_german

# User input
# converting from MW to W
conv_number = 1e6
german_translation = True
unit = "W"


# GENERAL
c_to_cl = {
"biomass": "Biomass",
"ch4": "CH4",
"hard coal": "Hard coal",
"oil": "Oil",
"lignite": "Lignite",
"other": "Other",
}

cl_to_gcl = labels_to_german

c_to_gcl = {carrier: cl_to_gcl[cl] for carrier, cl in c_to_cl.items()}

colors_odict_german = {
cl_to_gcl[cl]: color for cl, color in colors_odict.items() if cl in cl_to_gcl
}


def prepare_conv_pp_scalars(df_conv_pp_scalars, var_name, conv_number, label_mapping):
r"""
This function converts scalar data in oeomof-b3 format to a format that can be passed to
`plot_grouped_bar` .
It aggregates power plants with the same carrier.
Parameters
----------
df_conv_pp_scalars: pd.DataFrame
contains all conventional power plants in Berlin and Brandenburg
var_name: string
indicates the var_name which shall be plotted
conv_number: int
converts the value to SI unit, i.e. W
Returns
-------
df_pivot : pandas.DataFrame
Unique unit
color_dict: dict
contains colors for different carriers for plotting
"""
# select var_name to be plotted
selected_df = df_conv_pp_scalars[df_conv_pp_scalars["var_name"] == var_name].copy()

assert not selected_df.empty, f"Variable name '{var_name}' not found in data."

# CONV_PP specific
selected_df.loc[(selected_df["region"] != "Berlin"), "region"] = "Brandenburg"

# aggregate carriers in regions
selected_df_agg = dp.aggregate_scalars(
df=selected_df, columns_to_aggregate=["tech"]
)

# convert to SI unit:
if conv_number:
selected_df_agg.loc[:, "var_value"] *= conv_number

selected_df_agg["carrier"].replace(label_mapping, inplace=True)

# apply pivot table
df_pivot = pd.pivot_table(
selected_df_agg,
index=["region"],
columns="carrier",
values="var_value",
)

return df_pivot


def plot_grouped_bar(ax, df, color_dict, unit, stacked=False):
r"""
This function plots scalar data as grouped bar plot. The index of the DataFrame
will be interpreted as groups (e.g. regions), the columns as different categories (e.g. energy
carriers) within the groups which will be plotted in different colors.
Parameters
----------
ax: matplotlib Axes object
Axes to draw the plot.
df: pd.DataFrame
DataFrame with an index defining the groups and columns defining the bars of different color
with in the group.
color_dict: dict
Dictionary defining colors of the categories
unit: str
Unit of the variables
stacked : boolean
Stack bars of a group. False by default.
"""
alpha = 0.3
fontsize = 14
plt.rcParams.update({"font.size": fontsize})

# apply EngFormatter if power is plotted
ax = plots._eng_format(ax, unit)

df.plot.bar(
ax=ax,
color=[color_dict[key] for key in df.columns],
width=0.8,
zorder=2,
stacked=stacked,
rot=0,
)

for spine in ["top", "left", "right"]:
ax.spines[spine].set_visible(False)
ax.spines["bottom"].set_alpha(alpha)
ax.tick_params(axis="both", length=0, pad=7)

ax.grid(axis="y", zorder=1, color="black", alpha=alpha)
ax.set_xlabel(xlabel=None)
ax.legend()
plt.legend(title=None, frameon=True, framealpha=1)

plt.tight_layout()

return ax


if __name__ == "__main__":
resources = sys.argv[1]

var_name = sys.argv[2]

target = sys.argv[3]

target_dir = os.path.dirname(target)

# create the directory plotted where all plots are saved
if not os.path.exists(target_dir):
os.makedirs(target_dir)

# Load scalar data
df_conv_pp_scalars = dp.load_b3_scalars(resources)

if german_translation:
label_mapping = c_to_gcl
color_dict = colors_odict_german
else:
label_mapping = c_to_cl
color_dict = colors_odict

df_pivot = prepare_conv_pp_scalars(
df_conv_pp_scalars=df_conv_pp_scalars,
var_name=var_name,
conv_number=conv_number,
label_mapping=label_mapping,
)

fig, ax = plt.subplots(figsize=(12, 6))

plot_grouped_bar(ax, df_pivot, color_dict, unit)

plt.savefig(target, bbox_inches="tight")
6 changes: 4 additions & 2 deletions scripts/plot_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@
]

# select carrier
carrier = "electricity"
carriers = ["electricity", "heat_central", "heat_decentral"]

selected_bus_files = [file for file in bus_files if carrier in file]
selected_bus_files = [
file for file in bus_files for carrier in carriers if carrier in file
]

for bus_file in selected_bus_files:

Expand Down
Loading

0 comments on commit 88e77d4

Please sign in to comment.