Passed
Pull Request — master (#2)
by Uwe
01:11
created

scenario_builder.commodity   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 8

3 Functions

Rating   Name   Duplication   Size   Complexity  
A scenario_commodity_sources() 0 38 3
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-2019 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
14
from scenario_builder import config as cfg
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
    commodity_src = commodity_src.transpose()
49
50
    # Add region level to be consistent to other tables
51
    commodity_src.columns = pd.MultiIndex.from_product(
52
        [["DE"], commodity_src.columns]
53
    )
54
55
    return commodity_src.transpose()
56
57
58
def create_commodity_sources_ewi():
59
    """
60
61
    Returns
62
    -------
63
64
    """
65
    ewi = data.get_ewi_data()
66
    df = pd.DataFrame()
67
    df["costs"] = ewi.fuel_costs["value"] + ewi.transport_costs["value"]
68
    df["emission"] = ewi.emission["value"].multiply(1000)
69
    df["co2_price"] = float(ewi.co2_price["value"])
70
    missing = "bioenergy"
71
    msg = (
72
        "Costs/Emission for {0} in ewi is missing.\n"
73
        "Values for {0} are hard coded! Use with care."
74
    )
75
    warn(msg.format(missing), UserWarning)
76
    df.loc[missing, "emission"] = 7.2
77
    df.loc[missing, "costs"] = 20
78
    df.loc[missing, "co2_price"] = df.loc["natural gas", "co2_price"]
79
    return df
80
81
82
def create_commodity_sources_reegis(year, use_znes_2014=True):
83
    """
84
85
    Parameters
86
    ----------
87
    year
88
    use_znes_2014
89
90
    Returns
91
    -------
92
93
    """
94
    msg = (
95
        "The unit for {0} of the source is '{1}'. "
96
        "Will multiply it with {2} to get '{3}'."
97
    )
98
99
    converter = {
100
        "costs": ["costs", "EUR/J", 1e9 * 3.6, "EUR/MWh"],
101
        "emission": ["emission", "g/J", 1e6 * 3.6, "kg/MWh"],
102
    }
103
104
    cs = commodity_sources.get_commodity_sources()
105
    rename_cols = {
106
        key.lower(): value
107
        for key, value in cfg.get_dict("source_names").items()
108
    }
109
    cs = cs.rename(columns=rename_cols)
110
    cs_year = cs.loc[year]
111
    if use_znes_2014:
112
        before = len(cs_year[cs_year.isnull()])
113
        cs_year = cs_year.fillna(cs.loc[2014])
114
        after = len(cs_year[cs_year.isnull()])
115
        if before - after > 0:
116
            logging.warning("Values were replaced with znes2014 data.")
117
    cs_year = cs_year.sort_index().unstack()
118
119
    # convert units
120
    for key in converter.keys():
121
        cs_year[key] = cs_year[key].multiply(converter[key][2])
122
        logging.warning(msg.format(*converter[key]))
123
124
    return cs_year
125