Passed
Pull Request — dev (#1054)
by
unknown
05:31 queued 04:10
created

data.datasets.power_plants.mastr.import_mastr()   B

Complexity

Conditions 2

Size

Total Lines 71
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 71
rs 8.44
c 0
b 0
f 0
cc 2
nop 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
"""Import MaStR dataset and write to DB tables"""
2
import pandas as pd
3
4
from egon.data.datasets.power_plants import (
5
    EgonPowerPlantsBiomass,
6
    EgonPowerPlantsHydro,
7
    EgonPowerPlantsPv,
8
    EgonPowerPlantsWind,
9
)
10
import egon.data.config
11
12
13
def import_mastr():
14
    cfg = egon.data.config.datasets()["power_plants"]
15
16
    cols_mapping = {
17
        "all": {
18
            "EinheitMastrNummer": "gens_id",
19
            "EinheitBetriebsstatus": "status",
20
            "Inbetriebnahmedatum": "commissioning_date",
21
            "Postleitzahl": "postcode",
22
            "Ort": "city",
23
            "Bundesland": "federal_state",
24
            "Nettonennleistung": "capacity",
25
            "Einspeisungsart": "feedin_type",
26
        },
27
        "pv": {
28
            "Lage": "site_type",
29
            "Nutzungsbereich": "usage_sector",
30
            "Hauptausrichtung": "orientation_primary",
31
            "HauptausrichtungNeigungswinkel": "orientation_primary_angle",
32
            "Nebenausrichtung": "orientation_secondary",
33
            "NebenausrichtungNeigungswinkel": "orientation_secondary_angle",
34
            "EinheitlicheAusrichtungUndNeigungswinkel": "orientation_uniform",
35
            "AnzahlModule": "module_count",
36
            "ZugeordneteWirkleistungWechselrichter": "capacity_inverter",
37
        },
38
        "wind": {
39
            "Lage": "site_type",
40
            "Hersteller": "manufacturer_name",
41
            "Typenbezeichnung": "type_name",
42
            "Nabenhoehe": "hub_height",
43
            "Rotordurchmesser": "rotor_diameter",
44
        },
45
        "biomass": {
46
            "Technologie": "technology",
47
            "Hauptbrennstoff": "fuel_name",
48
            "Biomasseart": "fuel_type",
49
            "ThermischeNutzleistung": "th_capacity",
50
        },
51
        "hydro": {
52
            "ArtDerWasserkraftanlage": "type",
53
            "ArtDesZuflusses": "water_origin",
54
        },
55
    }
56
57
    source_files = {
58
        "pv": cfg["sources"]["mastr_pv"],
59
        "wind": cfg["sources"]["mastr_wind"],
60
        "biomass": cfg["sources"]["mastr_biomass"],
61
        "hydro": cfg["sources"]["mastr_hydro"],
62
    }
63
    target_tables = {
64
        "pv": EgonPowerPlantsPv,
65
        "wind": EgonPowerPlantsWind,
66
        "biomass": EgonPowerPlantsBiomass,
67
        "hydro": EgonPowerPlantsHydro,
68
    }
69
70
    # import locations
71
    locations = pd.read_csv(cfg["sources"]["mastr_location"], index_col=None)
72
73
    # import units
74
    technologies = ["pv", "wind", "biomass", "hydro"]
75
    for tech in technologies:
76
        units = pd.read_csv(
77
            source_files[tech],
78
            usecols=(
79
                list(cols_mapping["all"].keys())
80
                + list(cols_mapping[tech].keys())
81
            ),
82
            index_col=None,
83
        ).rename(columns=cols_mapping)
84