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 time series for air and soil temperatures per clustered model region.
|
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 pandas as pd
|
|
|
|
import xarray as xr
|
2024-02-12 10:53:20 +00:00
|
|
|
from _helpers import set_scenario_config
|
2022-12-28 11:20:34 +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(
|
|
|
|
"build_temperature_profiles",
|
2023-04-29 16:49:49 +00:00
|
|
|
weather_year="",
|
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:20:34 +00:00
|
|
|
nprocesses = int(snakemake.threads)
|
|
|
|
cluster = LocalCluster(n_workers=nprocesses, threads_per_worker=1)
|
|
|
|
client = Client(cluster, asynchronous=True)
|
2022-06-15 12:56:46 +00:00
|
|
|
cutout_name = snakemake.input.cutout
|
2022-02-18 10:25:38 +00:00
|
|
|
year = snakemake.wildcards.weather_year
|
2022-12-28 11:20:34 +00:00
|
|
|
|
2022-06-15 12:56:46 +00:00
|
|
|
if year:
|
2023-04-30 08:52:58 +00:00
|
|
|
snapshots = dict(start=year, end=str(int(year) + 1), inclusive="left")
|
2022-06-15 12:56:46 +00:00
|
|
|
cutout_name = cutout_name.format(weather_year=year)
|
|
|
|
else:
|
2023-07-26 08:23:32 +00:00
|
|
|
snapshots = snakemake.params.snapshots
|
2023-04-30 08:52:58 +00:00
|
|
|
|
|
|
|
time = pd.date_range(freq="h", **snapshots)
|
2024-03-01 09:10:26 +00:00
|
|
|
if snakemake.params.drop_leap_day:
|
2022-06-15 12:56:46 +00:00
|
|
|
time = time[~((time.month == 2) & (time.day == 29))]
|
2020-11-10 08:52:06 +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:20:34 +00:00
|
|
|
pop_layout = xr.open_dataarray(snakemake.input.pop_layout)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2022-12-28 11:20:34 +00:00
|
|
|
stacked_pop = pop_layout.stack(spatial=("y", "x"))
|
|
|
|
M = I.T.dot(np.diag(I.dot(stacked_pop)))
|
2020-05-07 12:45:14 +00:00
|
|
|
|
2022-12-28 11:20:34 +00:00
|
|
|
nonzero_sum = M.sum(axis=0, keepdims=True)
|
|
|
|
nonzero_sum[nonzero_sum == 0.0] = 1.0
|
|
|
|
M_tilde = M / nonzero_sum
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2022-12-28 11:20:34 +00:00
|
|
|
temp_air = cutout.temperature(
|
|
|
|
matrix=M_tilde.T,
|
|
|
|
index=clustered_regions.index,
|
|
|
|
dask_kwargs=dict(scheduler=client),
|
|
|
|
show_progress=False,
|
|
|
|
)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2022-12-28 11:20:34 +00:00
|
|
|
temp_air.to_netcdf(snakemake.output.temp_air)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2022-12-28 11:20:34 +00:00
|
|
|
temp_soil = cutout.soil_temperature(
|
|
|
|
matrix=M_tilde.T,
|
|
|
|
index=clustered_regions.index,
|
|
|
|
dask_kwargs=dict(scheduler=client),
|
|
|
|
show_progress=False,
|
|
|
|
)
|
2020-04-30 16:38:55 +00:00
|
|
|
|
2022-12-28 11:20:34 +00:00
|
|
|
temp_soil.to_netcdf(snakemake.output.temp_soil)
|