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

scenario_builder.data.get_ewi_data()   A

Complexity

Conditions 3

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 28
nop 0
dl 0
loc 50
rs 9.208
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 os
9
from types import SimpleNamespace
10
11
import pandas as pd
12
from reegis.tools import download_file
13
14
from scenario_builder import config as cfg
15
16
TRANSLATION_FUEL = {
17
    "Abfall": "waste",
18
    "Kernenergie": "nuclear",
19
    "Braunkohle": "lignite",
20
    "Steinkohle": "hard coal",
21
    "Erdgas": "natural gas",
22
    "GuD": "natural gas",
23
    "Gasturbine": "natural gas",
24
    "Öl": "oil",
25
    "Sonstige": "other",
26
    "Emissionszertifikatspreis": "co2_price",
27
}
28
29
30
def get_ewi_data():
31
    """
32
33
    Returns
34
    -------
35
    namedtuple
36
37
    Examples
38
    --------
39
    >>> ewi_data = get_ewi_data()
40
    >>> round(ewi_data.fuel_costs.loc["hard coal", "value"], 2)
41
    11.28
42
43
    """
44
    # Download file
45
    url = (
46
        "https://www.ewi.uni-koeln.de/cms/wp-content/uploads/2019/12"
47
        "/EWI_Merit_Order_Tool_2019_1_4.xlsm"
48
    )
49
    fn = os.path.join(cfg.get("paths", "deflex_general"), "ewi.xlsm")
50
    download_file(fn, url)
51
52
    # Create named tuple with all sub tables
53
    ewi_tables = {
54
        "fuel_costs": {"skiprows": 7, "usecols": "C:F", "nrows": 7},
55
        "transport_costs": {"skiprows": 21, "usecols": "C:F", "nrows": 7},
56
        "variable_costs": {"skiprows": 31, "usecols": "C:F", "nrows": 8},
57
        "downtime_factor": {
58
            "skiprows": 31,
59
            "usecols": "H:K",
60
            "nrows": 8,
61
            "scale": 0.01,
62
        },
63
        "emission": {"skiprows": 31, "usecols": "M:P", "nrows": 7},
64
        "co2_price": {"skiprows": 17, "usecols": "C:F", "nrows": 1},
65
    }
66
    ewi_data = {}
67
    cols = ["fuel", "value", "unit", "source"]
68
    xls = pd.ExcelFile(fn)
69
    for table in ewi_tables.keys():
70
        tmp = xls.parse("Start", header=[0], **ewi_tables[table]).replace(
71
            TRANSLATION_FUEL
72
        )
73
        tmp.drop_duplicates(tmp.columns[0], keep="first", inplace=True)
74
        tmp.columns = cols
75
        ewi_data[table] = tmp.set_index("fuel")
76
        if "scale" in ewi_tables[table]:
77
            ewi_data[table]["value"] *= ewi_tables[table]["scale"]
78
79
    return SimpleNamespace(**ewi_data)
80