scenario_builder.commodity   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 57
dl 0
loc 127
rs 10
c 0
b 0
f 0
wmc 9

3 Functions

Rating   Name   Duplication   Size   Complexity  
A scenario_commodity_sources() 0 40 4
A create_commodity_sources_reegis() 0 43 4
A create_commodity_sources_ewi() 0 22 1
1
"""Create a basic scenario from the internal data structure.
2
3
SPDX-FileCopyrightText: 2016-2021 Uwe Krien <[email protected]>
4
5
SPDX-License-Identifier: MIT
6
"""
7
8
import logging
9
from warnings import warn
10
11
import pandas as pd
12
from reegis import commodity_sources
13
from reegis import config as cfg
14
15
from scenario_builder import data
16
17
18
def scenario_commodity_sources(year):
19
    """
20
21
    Parameters
22
    ----------
23
    year
24
25
    Returns
26
    -------
27
28
    Examples
29
    --------
30
    >>> from reegis import geometries
31
    >>> from scenario_builder import powerplants
32
    >>> fs=geometries.get_federal_states_polygon()  # doctest: +SKIP
33
    >>> pp=powerplants.scenario_powerplants(dict(), fs, 2014, "federal_states"
34
    ...     )  # doctest: +SKIP
35
    >>> src=scenario_commodity_sources(pp)  # doctest: +SKIP
36
    >>> round(src.loc[("DE", "hard coal"), "costs"], 2)  # doctest: +SKIP
37
    12.53
38
    >>> round(src.loc[("DE", "natural gas"), "emission"], 2)  # doctest: +SKIP
39
    201.0
40
    """
41
    if cfg.get("creator", "costs_source") == "reegis":
42
        commodity_src = create_commodity_sources_reegis(year)
43
    elif cfg.get("creator", "costs_source") == "ewi":
44
        commodity_src = create_commodity_sources_ewi()
45
    else:
46
        commodity_src = None
47
48
    # Add region level to be consistent to other tables
49
    commodity_src.index = pd.MultiIndex.from_product(
50
        [["DE"], commodity_src.index]
51
    )
52
53
    if cfg.get("creator", "use_CO2_costs") is False:
54
        commodity_src["co2_price"] = 0
55
56
    commodity_src["annual limit"] = "inf"
57
    return commodity_src
58
59
60
def create_commodity_sources_ewi():
61
    """
62
63
    Returns
64
    -------
65
66
    """
67
    ewi = data.get_ewi_data()
68
    df = pd.DataFrame()
69
    df["costs"] = ewi.fuel_costs["value"] + ewi.transport_costs["value"]
70
    df["emission"] = ewi.emission["value"].multiply(1000)
71
    df["co2_price"] = float(ewi.co2_price["value"])
72
    missing = "bioenergy"
73
    msg = (
74
        "Costs/Emission for {0} in ewi is missing.\n"
75
        "Values for {0} are hard coded! Use with care."
76
    )
77
    warn(msg.format(missing), UserWarning)
78
    df.loc[missing, "emission"] = 7.2
79
    df.loc[missing, "costs"] = 20
80
    df.loc[missing, "co2_price"] = df.loc["natural gas", "co2_price"]
81
    return df
82
83
84
def create_commodity_sources_reegis(year, use_znes_2014=True):
85
    """
86
87
    Parameters
88
    ----------
89
    year
90
    use_znes_2014
91
92
    Returns
93
    -------
94
95
    """
96
    msg = (
97
        "The unit for {0} of the source is '{1}'. "
98
        "Will multiply it with {2} to get '{3}'."
99
    )
100
101
    converter = {
102
        "costs": ["costs", "EUR/J", 1e9 * 3.6, "EUR/MWh"],
103
        "emission": ["emission", "g/J", 1e6 * 3.6, "kg/MWh"],
104
    }
105
106
    cs = commodity_sources.get_commodity_sources()
107
    rename_cols = {
108
        key.lower(): value
109
        for key, value in cfg.get_dict("source_names").items()
110
    }
111
    cs = cs.rename(columns=rename_cols)
112
    cs_year = cs.loc[year]
113
    if use_znes_2014:
114
        before = len(cs_year[cs_year.isnull()])
115
        cs_year = cs_year.fillna(cs.loc[2014])
116
        after = len(cs_year[cs_year.isnull()])
117
        if before - after > 0:
118
            logging.warning("Values were replaced with znes2014 data.")
119
    cs_year = cs_year.sort_index().unstack()
120
121
    # convert units
122
    for key in converter.keys():
123
        cs_year[key] = cs_year[key].multiply(converter[key][2])
124
        logging.warning(msg.format(*converter[key]))
125
126
    return cs_year
127