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