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.
|
2024-06-17 09:38:23 +00:00
|
|
|
|
|
|
|
Uses ``atlite.Cutout.temperature`` and ``atlite.Cutout.soil_temperature compute temperature ambient air and soil temperature for the respective cutout. The rule is executed in ``build_sector.smk``.
|
|
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
`Atlite.Cutout.temperature <https://atlite.readthedocs.io/en/master/ref_api.html#module-atlite.convert>`_
|
|
|
|
`Atlite.Cutout.soil_temperature <https://atlite.readthedocs.io/en/master/ref_api.html#module-atlite.convert>`_
|
|
|
|
|
|
|
|
Relevant Settings
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
.. code:: yaml
|
|
|
|
|
|
|
|
snapshots:
|
|
|
|
drop_leap_day:
|
|
|
|
atlite:
|
|
|
|
default_cutout:
|
|
|
|
|
|
|
|
Inputs
|
|
|
|
------
|
|
|
|
|
2024-07-24 11:19:57 +00:00
|
|
|
- ``resources/<run_name>/pop_layout_total.nc``:
|
2024-09-13 13:37:01 +00:00
|
|
|
- ``resources/<run_name>/regions_onshore_base_s<simpl>_<clusters>.geojson``:
|
2024-06-17 09:38:23 +00:00
|
|
|
- ``cutout``: Weather data cutout, as specified in config
|
|
|
|
|
|
|
|
Outputs
|
|
|
|
-------
|
|
|
|
|
2024-09-13 13:37:01 +00:00
|
|
|
- ``resources/temp_soil_total_base_s<simpl>_<clusters>.nc``:
|
|
|
|
- ``resources/temp_air_total_base_s<simpl>_<clusters>.nc`
|
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: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",
|
|
|
|
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)
|
|
|
|
|
2024-03-14 14:15:56 +00:00
|
|
|
time = get_snapshots(snakemake.params.snapshots, snakemake.params.drop_leap_day)
|
2020-11-10 08:52:06 +00:00
|
|
|
|
2024-03-04 17:24:01 +00:00
|
|
|
cutout = atlite.Cutout(snakemake.input.cutout).sel(time=time)
|
2019-04-16 14:03:51 +00:00
|
|
|
|
2021-07-01 18:09:04 +00:00
|
|
|
clustered_regions = (
|
|
|
|
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)
|