Completed
Push — dev ( 85d654...312d71 )
by
unknown
21s queued 16s
created

  A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 44
dl 0
loc 128
rs 10
c 0
b 0
f 0
1
"""
2
Motorized Individual Travel (MIT) Charging Infrastructure
3
4
Main module for preparation of static model data for charging infrastructure for
5
motorized individual travel.
6
7
"""
8
from __future__ import annotations
9
10
from pathlib import Path
11
import zipfile
12
13
from loguru import logger
14
import requests
15
16
from egon.data import config, db
17
from egon.data.datasets import Dataset
18
from egon.data.datasets.emobility.motorized_individual_travel_charging_infrastructure.db_classes import (  # noqa: E501
19
    EgonEmobChargingInfrastructure,
20
)
21
from egon.data.datasets.emobility.motorized_individual_travel_charging_infrastructure.infrastructure_allocation import (  # noqa: E501
22
    run_tracbev,
23
)
24
25
WORKING_DIR = Path(".", "charging_infrastructure").resolve()
26
DATASET_CFG = config.datasets()["charging_infrastructure"]
27
28
29
def create_tables() -> None:
30
    """
31
    Create tables for charging infrastructure
32
33
    Returns
34
    -------
35
    None
36
    """
37
    engine = db.engine()
38
    EgonEmobChargingInfrastructure.__table__.drop(bind=engine, checkfirst=True)
39
    EgonEmobChargingInfrastructure.__table__.create(
40
        bind=engine, checkfirst=True
41
    )
42
43
    logger.debug("Created tables.")
44
45
46
def download_zip(url: str, target: Path, chunk_size: int | None = 128) -> None:
47
    """
48
    Download zip file from URL.
49
50
    Parameters
51
    ----------
52
    url : str
53
        URL to download the zip file from
54
    target : pathlib.Path
55
        Directory to save zip to
56
    chunk_size: int or None
57
        Size of chunks to download
58
59
    """
60
    r = requests.get(url, stream=True)
61
62
    target.parent.mkdir(parents=True, exist_ok=True)
63
64
    with open(target, "wb") as fd:
65
        for chunk in r.iter_content(chunk_size=chunk_size):
66
            fd.write(chunk)
67
68
69
def unzip_file(source: Path, target: Path) -> None:
70
    """
71
    Unzip zip file
72
73
    Parameters
74
    ----------
75
    source: Path
76
        Zip file path to unzip
77
    target: Path
78
        Directory to save unzipped content to
79
80
    """
81
    with zipfile.ZipFile(source, "r") as zip_ref:
82
        zip_ref.extractall(target)
83
84
85
def get_tracbev_data() -> None:
86
    """
87
    Wrapper function to get TracBEV data provided on Zenodo.
88
    """
89
    tracbev_cfg = DATASET_CFG["original_data"]["sources"]["tracbev"]
90
    file = WORKING_DIR / tracbev_cfg["file"]
91
92
    download_zip(url=tracbev_cfg["url"], target=file)
93
94
    unzip_file(source=file, target=WORKING_DIR)
95
96
97
class MITChargingInfrastructure(Dataset):
98
    """
99
    Preparation of static model data for charging infrastructure for
100
    motorized individual travel.
101
102
    The following is done:
103
104
    * Creation of DB tables
105
    * Download and preprocessing of vehicle registration data from zenodo
106
    * Determination of all potential charging locations for the four charging use cases
107
      home, work, public and hpc per MV grid district
108
    * Write results to DB
109
110
    *Dependencies*
111
      * :py:class:`MvGridDistricts <egon.data.datasets.mv_grid_districts.mv_grid_districts_setup>`
112
      * :py:func:`map_houseprofiles_to_buildings <egon.data.datasets.electricity_demand_timeseries.hh_buildings.map_houseprofiles_to_buildings>`
113
114
    *Resulting tables*
115
      * :py:class:`grid.egon_emob_charging_infrastructure
116
        <egon.data.datasets.emobility.motorized_individual_travel_charging_infrastructure.db_classes.EgonEmobChargingInfrastructure>`
117
        is created and filled
118
119
    *Configuration*
120
121
    The config of this dataset can be found in *datasets.yml* in section
122
    *charging_infrastructure*.
123
124
    *Charging Infrastructure*
125
126
    The charging infrastructure allocation is based on
127
    `TracBEV <https://github.com/rl-institut/tracbev>`_. TracBEV is a tool for the
128
    regional allocation of charging infrastructure. In practice this allows users to
129
    use results generated via `SimBEV <https://github.com/rl-institut/simbev>`_ and
130
    place the corresponding charging
131
    points on a map. These are split into the four use cases home, work, public and hpc.
132
133
    """
134
135
    #:
136
    name: str = "MITChargingInfrastructure"
137
    #:
138
    version: str = "0.0.1"
139
140
    def __init__(self, dependencies):
141
        super().__init__(
142
            name=self.name,
143
            version=self.version,
144
            dependencies=dependencies,
145
            tasks=(
146
                {
147
                    create_tables,
148
                    get_tracbev_data,
149
                },
150
                run_tracbev,
151
            ),
152
        )
153