-
Notifications
You must be signed in to change notification settings - Fork 250
/
retrieve_databundle.py
68 lines (50 loc) · 1.99 KB
/
retrieve_databundle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
# Copyright 2019-2024 Fabian Hofmann (TUB, FIAS), Fabian Neumann (TUB)
# SPDX-FileCopyrightText: : 2017-2024 The PyPSA-Eur Authors
#
# SPDX-License-Identifier: MIT
"""
.. image:: https://zenodo.org/badge/DOI/10.5281/zenodo.3517934.svg
:target: https://doi.org/10.5281/zenodo.3517934
The data bundle contains common GIS datasets like NUTS3 shapes, EEZ shapes,
CORINE Landcover, Natura 2000 and also electricity specific summary statistics
like historic per country yearly totals of hydro generation, GDP and population
data on NUTS3 levels and energy balances.
This rule downloads the data bundle from `zenodo
<https://doi.org/10.5281/zenodo.3517934>`_ and extracts it in the ``data``
sub-directory, such that all files of the bundle are stored in the
``data/bundle`` subdirectory.
**Outputs**
- ``data/bundle``: input data collected from various sources
"""
import logging
import tarfile
from pathlib import Path
from _helpers import (
configure_logging,
progress_retrieve,
set_scenario_config,
validate_checksum,
)
logger = logging.getLogger(__name__)
if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
snakemake = mock_snakemake("retrieve_databundle")
rootpath = ".."
else:
rootpath = "."
configure_logging(snakemake)
set_scenario_config(snakemake)
url = "https://zenodo.org/records/13757228/files/bundle.tar.xz"
tarball_fn = Path(f"{rootpath}/bundle.tar.xz")
to_fn = Path(rootpath) / Path(snakemake.output[0]).parent.parent
logger.info(f"Downloading databundle from '{url}'.")
disable_progress = snakemake.config["run"].get("disable_progressbar", False)
progress_retrieve(url, tarball_fn, disable=disable_progress)
validate_checksum(tarball_fn, url)
logger.info("Extracting databundle.")
tarfile.open(tarball_fn).extractall(to_fn)
logger.info("Unlinking tarball.")
tarball_fn.unlink()
logger.info(f"Databundle available in '{to_fn}'.")