pypsa-eur/scripts/build_clustered_population_layouts.py
2020-11-10 09:52:06 +01:00

36 lines
1008 B
Python

import geopandas as gpd
import xarray as xr
import pandas as pd
import atlite
import helper
year = snakemake.wildcards.year
cutout_name = snakemake.config['atlite']['cutout_name']
if year: cutout_name = cutout_name.format(year=year)
cutout = atlite.Cutout(cutout_name,
cutout_dir=snakemake.config['atlite']['cutout_dir'])
clustered_busregions_as_geopd = gpd.read_file(snakemake.input.regions_onshore).set_index('name', drop=True)
clustered_busregions = pd.Series(clustered_busregions_as_geopd.geometry, index=clustered_busregions_as_geopd.index)
helper.clean_invalid_geometries(clustered_busregions)
I = cutout.indicatormatrix(clustered_busregions)
items = ["total","urban","rural"]
pop = pd.DataFrame(columns=items,
index=clustered_busregions.index)
for item in items:
pop_layout = xr.open_dataarray(snakemake.input['pop_layout_'+item])
pop[item] = I.dot(pop_layout.stack(spatial=('y', 'x')))
pop.to_csv(snakemake.output.clustered_pop_layout)