pypsa-eur/scripts/build_ship_raster.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

101 lines
2.7 KiB
Python
Raw Permalink Normal View History

# -*- coding: utf-8 -*-
2022-07-29 12:56:00 +00:00
# SPDX-FileCopyrightText: : 2022 The PyPSA-Eur Authors
2022-07-28 08:38:24 +00:00
#
# SPDX-License-Identifier: MIT
"""
2023-02-16 10:50:55 +00:00
Transforms the global ship density data from the `World Bank Data Catalogue.
<https://datacatalog.worldbank.org/search/dataset/0037580/Global-Shipping-Traffic-Density>`_
to the size of the considered cutout. The global ship density raster is later
used for the exclusion when calculating the offshore potentials.
2022-07-28 08:38:24 +00:00
Relevant Settings
-----------------
.. code:: yaml
renewable:
{technology}:
cutout:
.. seealso::
Documentation of the configuration file ``config/config.yaml`` at
2022-07-28 08:38:24 +00:00
:ref:`renewable_cf`
Inputs
------
2023-02-16 11:37:30 +00:00
- ``data/bundle/shipdensity/shipdensity_global.zip``: Global shipping traffic
density from `World Bank Data Catalogue
<https://datacatalog.worldbank.org/search/dataset/0037580/>`_.
2022-07-28 08:38:24 +00:00
Outputs
-------
2023-02-16 11:37:30 +00:00
- ``resources/europe_shipdensity_raster.nc``: Reduced version of global shipping
traffic density from `World Bank Data Catalogue
<https://datacatalog.worldbank.org/search/dataset/0037580/>`_ to reduce
computation time.
2022-07-28 08:38:24 +00:00
Description
-----------
"""
import logging
import zipfile
2023-08-15 13:02:41 +00:00
from pathlib import Path
2024-04-15 13:28:02 +00:00
import atlite
import rioxarray
2023-08-15 13:02:41 +00:00
from _helpers import configure_logging, set_scenario_config
2022-07-28 08:38:24 +00:00
logger = logging.getLogger(__name__)
2024-04-15 12:48:34 +00:00
def determine_cutout_xXyY(cutout_name):
"""
Determine the full extent of a cutout.
Since the coordinates of the cutout data are given as the
center of the grid cells, the extent of the cutout is
calculated by adding/subtracting half of the grid cell size.
Parameters
----------
cutout_name : str
Path to the cutout.
Returns
-------
A list of extent coordinates in the order [x, X, y, Y].
"""
cutout = atlite.Cutout(cutout_name)
assert cutout.crs.to_epsg() == 4326
x, X, y, Y = cutout.extent
dx, dy = cutout.dx, cutout.dy
return [x - dx / 2.0, X + dx / 2.0, y - dy / 2.0, Y + dy / 2.0]
2022-07-28 08:38:24 +00:00
if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers import mock_snakemake
2022-07-28 08:38:24 +00:00
snakemake = mock_snakemake("build_ship_raster")
configure_logging(snakemake)
2023-08-15 13:02:41 +00:00
set_scenario_config(snakemake)
2022-07-28 08:38:24 +00:00
x, X, y, Y = determine_cutout_xXyY(snakemake.input.cutout)
2022-07-28 13:21:21 +00:00
2022-07-28 08:38:24 +00:00
with zipfile.ZipFile(snakemake.input.ship_density) as zip_f:
2023-08-15 13:02:41 +00:00
resources = Path(snakemake.output[0]).parent
fn = "shipdensity_global.tif"
zip_f.extract(fn, resources)
with rioxarray.open_rasterio(resources / fn) as ship_density:
ship_density = ship_density.drop_vars(["band"]).sel(
x=slice(x, X), y=slice(Y, y)
2023-08-15 13:02:41 +00:00
)
ship_density.rio.to_raster(snakemake.output[0])
(resources / fn).unlink()