2023-03-06 08:27:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2024-02-19 15:21:48 +00:00
|
|
|
# SPDX-FileCopyrightText: : 2023-2024 The PyPSA-Eur Authors
|
2023-03-06 17:49:23 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MIT
|
2022-11-13 12:43:05 +00:00
|
|
|
"""
|
|
|
|
Build regional demand for international navigation based on outflow volume of
|
|
|
|
ports.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
import geopandas as gpd
|
|
|
|
import pandas as pd
|
2024-02-12 10:53:20 +00:00
|
|
|
from _helpers import set_scenario_config
|
2023-03-06 08:27:45 +00:00
|
|
|
|
2022-11-13 12:43:05 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
if "snakemake" not in globals():
|
2023-03-06 18:09:45 +00:00
|
|
|
from _helpers import mock_snakemake
|
2023-03-06 08:27:45 +00:00
|
|
|
|
2024-09-13 13:37:01 +00:00
|
|
|
snakemake = mock_snakemake("build_shipping_demand", clusters=48)
|
2024-02-12 10:53:20 +00:00
|
|
|
set_scenario_config(snakemake)
|
2022-11-13 12:43:05 +00:00
|
|
|
|
|
|
|
scope = gpd.read_file(snakemake.input.scope).geometry[0]
|
|
|
|
regions = gpd.read_file(snakemake.input.regions).set_index("name")
|
2024-02-29 10:38:21 +00:00
|
|
|
demand = pd.read_csv(snakemake.input.demand, index_col=[0, 1])[
|
2022-11-13 12:43:05 +00:00
|
|
|
"total international navigation"
|
|
|
|
]
|
2024-02-29 10:38:21 +00:00
|
|
|
demand = demand.xs(snakemake.params.energy_totals_year, level=1)
|
2022-11-13 12:43:05 +00:00
|
|
|
|
|
|
|
# read port data into GeoDataFrame
|
|
|
|
with open(snakemake.input.ports, "r", encoding="latin_1") as f:
|
|
|
|
ports = json.load(f)
|
|
|
|
ports = pd.json_normalize(ports, "features", sep="_")
|
|
|
|
coordinates = ports.geometry_coordinates
|
|
|
|
geometry = gpd.points_from_xy(coordinates.str[0], coordinates.str[1])
|
|
|
|
ports = gpd.GeoDataFrame(ports, geometry=geometry, crs=4326)
|
2023-03-06 08:27:45 +00:00
|
|
|
|
2022-11-13 12:43:05 +00:00
|
|
|
# filter global port data by European ports
|
|
|
|
european_ports = ports[ports.within(scope)]
|
2023-03-06 08:27:45 +00:00
|
|
|
|
2022-11-13 12:43:05 +00:00
|
|
|
# assign ports to nearest region
|
|
|
|
p = european_ports.to_crs(3857)
|
|
|
|
r = regions.to_crs(3857)
|
2024-07-08 06:29:16 +00:00
|
|
|
outflows = p.sjoin_nearest(r).groupby("name").properties_outflows.sum().div(1e3)
|
2023-03-06 08:27:45 +00:00
|
|
|
|
2022-11-13 12:43:05 +00:00
|
|
|
# calculate fraction of each country's port outflows
|
|
|
|
countries = outflows.index.str[:2]
|
|
|
|
outflows_per_country = outflows.groupby(countries).sum()
|
|
|
|
fraction = outflows / countries.map(outflows_per_country)
|
2023-03-06 08:27:45 +00:00
|
|
|
|
2022-11-13 12:43:05 +00:00
|
|
|
# distribute per-country demands to nodes based on these fractions
|
|
|
|
nodal_demand = demand.loc[countries].fillna(0.0)
|
|
|
|
nodal_demand.index = fraction.index
|
|
|
|
nodal_demand = nodal_demand.multiply(fraction, axis=0)
|
|
|
|
nodal_demand = nodal_demand.reindex(regions.index, fill_value=0)
|
|
|
|
|
|
|
|
# export nodal international navigation demands
|
|
|
|
nodal_demand.to_csv(snakemake.output[0])
|