Passed
Pull Request — dev (#1138)
by
unknown
02:19
created

data.datasets.emobility.heavy_duty_transport   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 47
dl 0
loc 154
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A download_hgv_data() 0 24 3
A create_tables() 0 13 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A HeavyDutyTransport.__init__() 0 12 1
1
"""
2
Main module for preparation of model data (static and timeseries) for
3
heavy duty transport.
4
5
**Contents of this module**
6
7
* Creation of DB tables
8
* Download and preprocessing of vehicle registration data from BAST
9
* Calculation of hydrogen demand based on a Voronoi distribution of counted
10
  truck traffic among NUTS 3 regions.
11
* Writing results to DB
12
* Mapping demand to H2 buses and writing to DB
13
14
"""
15
from pathlib import Path
16
import csv
17
18
from loguru import logger
19
import requests
20
21
from egon.data import config, db
22
from egon.data.datasets import Dataset
23
from egon.data.datasets.emobility.heavy_duty_transport.create_h2_buses import (
24
    insert_hgv_h2_demand,
25
)
26
from egon.data.datasets.emobility.heavy_duty_transport.db_classes import (
27
    EgonHeavyDutyTransportVoronoi,
28
)
29
from egon.data.datasets.emobility.heavy_duty_transport.h2_demand_distribution import (  # noqa: E501
30
    run_egon_truck,
31
)
32
33
WORKING_DIR = Path(".", "heavy_duty_transport").resolve()
34
DATASET_CFG = config.datasets()["mobility_hgv"]
35
TESTMODE_OFF = (
36
    config.settings()["egon-data"]["--dataset-boundary"] == "Everything"
37
)
38
39
40
def create_tables():
41
    """
42
    Drops existing :py:class:`demand.egon_heavy_duty_transport_voronoi <egon.data.datasets.emobility.heavy_duty_transport.db_classes.EgonHeavyDutyTransportVoronoi>` is extended
43
    table and creates new one.
44
45
    """
46
    engine = db.engine()
47
    EgonHeavyDutyTransportVoronoi.__table__.drop(bind=engine, checkfirst=True)
48
    EgonHeavyDutyTransportVoronoi.__table__.create(
49
        bind=engine, checkfirst=True
50
    )
51
52
    logger.debug("Created tables.")
53
54
55
def download_hgv_data():
56
    """
57
    Downloads BAST data.
58
59
    The data is downloaded to file specified in *datasets.yml* in section
60
    *mobility_hgv/original_data/sources/BAST/file*.
61
62
    """
63
    sources = DATASET_CFG["original_data"]["sources"]
64
65
    # Create the folder, if it does not exist
66
    WORKING_DIR.mkdir(parents=True, exist_ok=True)
67
68
    url = sources["BAST"]["url"]
69
    file = WORKING_DIR / sources["BAST"]["file"]
70
71
    response = requests.get(url)
72
73
    with open(file, "w") as f:
74
        writer = csv.writer(f)
75
        for line in response.iter_lines():
76
            writer.writerow(line.decode("ISO-8859-1").split(";"))
77
78
    logger.debug("Downloaded BAST data.")
79
80
81
class HeavyDutyTransport(Dataset):
82
    """
83
84
    *Dependencies*
85
      * :py:class:`Vg250 <egon.data.datasets.vg250.Vg250>`
86
      * :py:class:`EtragoSetup <egon.data.datasets.etrago_setup.EtragoSetup>`
87
      * :py:class:`GasAreaseGon2035 <egon.data.datasets.gas_areas.GasAreaseGon2035>`
88
89
    *Resulting tables*
90
      * :py:class:`demand.egon_heavy_duty_transport_voronoi
91
        <egon.data.datasets.emobility.heavy_duty_transport.db_classes.EgonHeavyDutyTransportVoronoi>`
92
        is created and filled
93
      * :py:class:`grid.egon_etrago_load<egon.data.datasets.etrago_setup.EgonPfHvLoad>`
94
        is extended
95
      * :py:class:`grid.egon_etrago_load_timeseries
96
        <egon.data.datasets.etrago_setup.EgonPfHvLoadTimeseries>` is extended
97
98
    *Configuration*
99
100
    The config of this dataset can be found in *datasets.yml* in section
101
    *mobility_hgv*.
102
103
    *Scenarios and variations*
104
105
    Assumptions can be changed within the *datasets.yml*.
106
107
    In the context of the eGon project, it is assumed that e-trucks will be
108
    completely hydrogen-powered and in both scenarios the hydrogen consumption is
109
    assumed to be 6.68 kg H2 per 100 km with an additional
110
    `supply chain leakage rate of 0.5 %
111
    <https://www.energy.gov/eere/fuelcells/doe-technical-targets-hydrogen-delivery>`_.
112
113
    ### Scenario NEP C 2035
114
115
    The ramp-up figures are taken from
116
    `Scenario C 2035 Grid Development Plan 2021-2035
117
    <https://www.netzentwicklungsplan.de/sites/default/files/paragraphs-files/
118
    NEP_2035_V2021_2_Entwurf_Teil1.pdf>`_. According to this, 100,000 e-trucks are
119
    expected in Germany in 2035, each covering an average of 100,000 km per year.
120
    In total this means 10 billion km.
121
122
    ### Scenario eGon100RE
123
124
    In the case of the eGon100RE scenario it is assumed that the HGV traffic is
125
    completely hydrogen-powered. The total freight traffic with 40 Billion km is
126
    taken from the
127
    `BMWK Langfristszenarien GHG-emission free scenarios (SNF > 12 t zGG)
128
    <https://www.langfristszenarien.de/enertile-explorer-wAssets/docs/
129
    LFS3_Langbericht_Verkehr_final.pdf#page=17>`_.
130
131
    *Methodology*
132
133
    Using a Voronoi interpolation, the censuses of the BASt data is distributed
134
    according to the area fractions of the Voronoi fields within each mv grid or
135
    any other geometries like NUTS-3.
136
137
    """
138
    #:
139
    name: str = "HeavyDutyTransport"
140
    #:
141
    version: str = "0.0.2"
142
    def __init__(self, dependencies):
143
        super().__init__(
144
            name=self.name,
145
            version=self.version,
146
            dependencies=dependencies,
147
            tasks=(
148
                {
149
                    create_tables,
150
                    download_hgv_data,
151
                },
152
                run_egon_truck,
153
                insert_hgv_h2_demand,
154
            ),
155
        )
156