scenario_builder.data.get_ewi_data()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 52
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 28
nop 0
dl 0
loc 52
rs 9.208
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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