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

create_commodity_sources_ewi()   A

Complexity

Conditions 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nop 0
dl 0
loc 22
rs 9.7
c 0
b 0
f 0
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
    >>> regions=geometries.deflex_regions(rmap="de21")  # doctest: +SKIP
31
    >>> pp=scenario_powerplants(dict(), regions, 2014, "de21"
32
    ...     )  # doctest: +SKIP
33
    >>> src=scenario_commodity_sources(pp)  # doctest: +SKIP
34
    >>> round(src.loc[("DE", "hard coal"), "costs"], 2)  # doctest: +SKIP
35
    12.53
36
    >>> round(src.loc[("DE", "natural gas"), "emission"], 2)  # doctest: +SKIP
37
    201.0
38
    """
39
    if cfg.get("basic", "costs_source") == "reegis":
40
        commodity_src = create_commodity_sources_reegis(year)
41
    elif cfg.get("basic", "costs_source") == "ewi":
42
        commodity_src = create_commodity_sources_ewi()
43
    else:
44
        commodity_src = None
45
46
    commodity_src = commodity_src.transpose()
47
48
    # Add region level to be consistent to other tables
49
    commodity_src.columns = pd.MultiIndex.from_product(
50
        [["DE"], commodity_src.columns]
51
    )
52
53
    return commodity_src.transpose()
54
55
56
def create_commodity_sources_ewi():
57
    """
58
59
    Returns
60
    -------
61
62
    """
63
    ewi = data.get_ewi_data()
64
    df = pd.DataFrame()
65
    df["costs"] = ewi.fuel_costs["value"] + ewi.transport_costs["value"]
66
    df["emission"] = ewi.emission["value"].multiply(1000)
67
    df["co2_price"] = float(ewi.co2_price["value"])
68
    missing = "bioenergy"
69
    msg = (
70
        "Costs/Emission for {0} in ewi is missing.\n"
71
        "Values for {0} are hard coded! Use with care."
72
    )
73
    warn(msg.format(missing), UserWarning)
74
    df.loc[missing, "emission"] = 7.2
75
    df.loc[missing, "costs"] = 20
76
    df.loc[missing, "co2_price"] = df.loc["natural gas", "co2_price"]
77
    return df
78
79
80
def create_commodity_sources_reegis(year, use_znes_2014=True):
81
    """
82
83
    Parameters
84
    ----------
85
    year
86
    use_znes_2014
87
88
    Returns
89
    -------
90
91
    """
92
    msg = (
93
        "The unit for {0} of the source is '{1}'. "
94
        "Will multiply it with {2} to get '{3}'."
95
    )
96
97
    converter = {
98
        "costs": ["costs", "EUR/J", 1e9 * 3.6, "EUR/MWh"],
99
        "emission": ["emission", "g/J", 1e6 * 3.6, "kg/MWh"],
100
    }
101
102
    cs = commodity_sources.get_commodity_sources()
103
    rename_cols = {
104
        key.lower(): value
105
        for key, value in cfg.get_dict("source_names").items()
106
    }
107
    cs = cs.rename(columns=rename_cols)
108
    cs_year = cs.loc[year]
109
    if use_znes_2014:
110
        before = len(cs_year[cs_year.isnull()])
111
        cs_year = cs_year.fillna(cs.loc[2014])
112
        after = len(cs_year[cs_year.isnull()])
113
        if before - after > 0:
114
            logging.warning("Values were replaced with znes2014 data.")
115
    cs_year = cs_year.sort_index().unstack()
116
117
    # convert units
118
    for key in converter.keys():
119
        cs_year[key] = cs_year[key].multiply(converter[key][2])
120
        logging.warning(msg.format(*converter[key]))
121
122
    return cs_year
123