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
|
2021-07-01 18:09:04 +00:00
|
|
|
"""
|
2023-03-09 11:45:43 +00:00
|
|
|
Build heat demand time series using heating degree day (HDD) approximation.
|
2021-07-01 18:09:04 +00:00
|
|
|
"""
|
2019-04-16 14:03:51 +00:00
|
|
|
|
|
|
|
import atlite
|
|
|
|
import geopandas as gpd
|
2021-07-01 18:09:04 +00:00
|
|
|
import numpy as np
|
2019-04-16 14:03:51 +00:00
|
|
|
import xarray as xr
|
2024-03-14 14:15:56 +00:00
|
|
|
from _helpers import get_snapshots, set_scenario_config
|
2022-12-28 11:21:00 +00:00
|
|
|
from dask.distributed import Client, LocalCluster
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2021-07-01 18:09:04 +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
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
snakemake = mock_snakemake(
|
2024-01-22 08:18:26 +00:00
|
|
|
"build_daily_heat_demands",
|
|
|
|
scope="total",
|
2021-07-01 18:09:04 +00:00
|
|
|
simpl="",
|
|
|
|
clusters=48,
|
|
|
|
)
|
2024-02-12 10:53:20 +00:00
|
|
|
set_scenario_config(snakemake)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2022-12-28 11:21:00 +00:00
|
|
|
nprocesses = int(snakemake.threads)
|
|
|
|
cluster = LocalCluster(n_workers=nprocesses, threads_per_worker=1)
|
|
|
|
client = Client(cluster, asynchronous=True)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2022-06-15 12:56:46 +00:00
|
|
|
cutout_name = snakemake.input.cutout
|
|
|
|
|
2024-03-14 14:15:56 +00:00
|
|
|
time = get_snapshots(snakemake.params.snapshots, snakemake.params.drop_leap_day)
|
|
|
|
daily = get_snapshots(
|
|
|
|
snakemake.params.snapshots,
|
|
|
|
snakemake.params.drop_leap_day,
|
|
|
|
freq="D",
|
|
|
|
)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2022-06-15 12:56:46 +00:00
|
|
|
cutout = atlite.Cutout(cutout_name).sel(time=time)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2021-07-01 18:09:04 +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
|
|
|
|
2022-12-28 11:21:00 +00:00
|
|
|
pop_layout = xr.open_dataarray(snakemake.input.pop_layout)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2022-12-28 11:21:00 +00:00
|
|
|
stacked_pop = pop_layout.stack(spatial=("y", "x"))
|
|
|
|
M = I.T.dot(np.diag(I.dot(stacked_pop)))
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2022-12-28 11:21:00 +00:00
|
|
|
heat_demand = cutout.heat_demand(
|
|
|
|
matrix=M.T,
|
|
|
|
index=clustered_regions.index,
|
|
|
|
dask_kwargs=dict(scheduler=client),
|
|
|
|
show_progress=False,
|
2023-04-29 16:49:49 +00:00
|
|
|
).sel(time=daily)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2022-12-28 11:21:00 +00:00
|
|
|
heat_demand.to_netcdf(snakemake.output.heat_demand)
|