Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.
44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
|
# -*- coding: utf-8 -*-
|
||
"""
|
|||
Build clustered population layouts.
|
|||
"""
|
|||
|
|
||
import atlite
|
|||
import geopandas as gpd
|
|||
import pandas as pd
|
|||
import xarray as xr
|
|||
|
|||
if __name__ == "__main__":
|
|||
if "snakemake" not in globals():
|
|||
from helper import mock_snakemake
|
|||
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
|
|
||
snakemake = mock_snakemake(
|
|||
"build_clustered_population_layouts",
|
|||
simpl="",
|
|||
clusters=48,
|
|||
)
|
|||
|
|
||
cutout = atlite.Cutout(snakemake.config["atlite"]["cutout"])
|
|||
|
|
||
clustered_regions = (
|
|||
gpd.read_file(snakemake.input.regions_onshore)
|
|||
.set_index("name")
|
|||
.buffer(0)
|
|||
.squeeze()
|
|||
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
|
)
|
||
|
|
||
I = cutout.indicatormatrix(clustered_regions)
|
|||
|
|
||
pop = {}
|
|||
for item in ["total", "urban", "rural"]:
|
|||
pop_layout = xr.open_dataarray(snakemake.input[f"pop_layout_{item}"])
|
|||
pop[item] = I.dot(pop_layout.stack(spatial=("y", "x")))
|
|||
|
|
||
pop = pd.DataFrame(pop, index=clustered_regions.index)
|
|||
|
|
||
pop["ct"] = pop.index.str[:2]
|
|||
country_population = pop.total.groupby(pop.ct).sum()
|
|||
pop["fraction"] = pop.total / pop.ct.map(country_population)
|
|||
|
|
||
pop.to_csv(snakemake.output.clustered_pop_layout)
|