Passed
Pull Request — master (#2)
by Uwe
03:17
created

scenario_builder.commodity   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 9

3 Functions

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