2023-03-06 08:27:45 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2024-02-19 15:21:48 +00:00
|
|
|
# SPDX-FileCopyrightText: : 2020-2024 The PyPSA-Eur Authors
|
2023-03-06 17:49:23 +00:00
|
|
|
#
|
|
|
|
# SPDX-License-Identifier: MIT
|
2023-03-06 08:27:45 +00:00
|
|
|
"""
|
2023-03-09 11:45:43 +00:00
|
|
|
Build population layouts for all clustered model regions as total as well as
|
|
|
|
split by urban and rural population.
|
2023-03-06 08:27:45 +00:00
|
|
|
"""
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2023-03-06 08:27:45 +00:00
|
|
|
import atlite
|
2019-04-16 14:03:51 +00:00
|
|
|
import geopandas as gpd
|
|
|
|
import pandas as pd
|
2023-03-06 08:27:45 +00:00
|
|
|
import xarray as xr
|
2024-02-12 10:53:20 +00:00
|
|
|
from _helpers import set_scenario_config
|
|
|
|
|
2023-03-06 08:27:45 +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_clustered_population_layouts", clusters=48)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2024-02-12 10:53:20 +00:00
|
|
|
set_scenario_config(snakemake)
|
|
|
|
|
2023-03-06 14:07:09 +00:00
|
|
|
cutout = atlite.Cutout(snakemake.input.cutout)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2023-03-06 08:27:45 +00:00
|
|
|
clustered_regions = (
|
2024-01-03 12:16:43 +00:00
|
|
|
gpd.read_file(snakemake.input.regions_onshore).set_index("name").buffer(0)
|
2023-03-06 08:27:45 +00:00
|
|
|
)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2024-01-19 11:16:07 +00:00
|
|
|
I = cutout.indicatormatrix(clustered_regions) # noqa: E741
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
pop = {}
|
|
|
|
for item in ["total", "urban", "rural"]:
|
2023-03-06 08:27:45 +00:00
|
|
|
pop_layout = xr.open_dataarray(snakemake.input[f"pop_layout_{item}"])
|
|
|
|
pop[item] = I.dot(pop_layout.stack(spatial=("y", "x")))
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
pop = pd.DataFrame(pop, index=clustered_regions.index)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
pop["ct"] = pop.index.str[:2]
|
|
|
|
country_population = pop.total.groupby(pop.ct).sum()
|
|
|
|
pop["fraction"] = pop.total / pop.ct.map(country_population)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
pop.to_csv(snakemake.output.clustered_pop_layout)
|