Passed
Pull Request — dev (#943)
by
unknown
02:06
created

motorized_individual_travel_charging_infrastructure   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

4 Functions

Rating   Name   Duplication   Size   Complexity  
A unzip_file() 0 14 2
A download_zip() 0 21 3
A create_tables() 0 15 1
A get_tracbev_data() 0 10 1

1 Method

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