Skip to content

Commit

Permalink
Add days between temps
Browse files Browse the repository at this point in the history
  • Loading branch information
collijk committed Jul 23, 2024
1 parent 39b41ac commit 4ddfffc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/climate_data/generate/scenario_annual.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
from climate_data.generate import utils

TEMP_THRESHOLDS = list(range(20, 35))
BETWEEN_TEMP_THRESHOLDS = [
(15, 30),
(20, 30),
(15, 35),
(20, 35),
]


TRANSFORM_MAP = {
Expand All @@ -36,6 +42,16 @@
)
for temp in TEMP_THRESHOLDS
},
**{
f"days_under_{upper}C_over_{lower}C": utils.Transform(
source_variables=["mean_temperature"],
transform_funcs=[
utils.count_between_threshold(lower, upper),
utils.annual_sum,
],
)
for lower, upper in BETWEEN_TEMP_THRESHOLDS
},
"mean_heat_index": utils.Transform(
source_variables=["heat_index"],
transform_funcs=[utils.annual_mean],
Expand All @@ -51,6 +67,16 @@
)
for temp in TEMP_THRESHOLDS
},
**{
f"days_under_{upper}C_over_{lower}C_heat_index": utils.Transform(
source_variables=["heat_index"],
transform_funcs=[
utils.count_between_threshold(lower, upper),
utils.annual_sum,
],
)
for lower, upper in BETWEEN_TEMP_THRESHOLDS
},
"mean_humidex": utils.Transform(
source_variables=["humidex"],
transform_funcs=[utils.annual_mean],
Expand All @@ -66,6 +92,16 @@
)
for temp in TEMP_THRESHOLDS
},
**{
f"days_under_{upper}C_over_{lower}C_humidex": utils.Transform(
source_variables=["humidex"],
transform_funcs=[
utils.count_between_threshold(lower, upper),
utils.annual_sum,
],
)
for lower, upper in BETWEEN_TEMP_THRESHOLDS
},
"mean_effective_temperature": utils.Transform(
source_variables=["effective_temperature"],
transform_funcs=[utils.annual_mean],
Expand All @@ -81,6 +117,16 @@
)
for temp in TEMP_THRESHOLDS
},
**{
f"days_under_{upper}C_over_{lower}C_effective_temperature": utils.Transform(
source_variables=["effective_temperature"],
transform_funcs=[
utils.count_between_threshold(lower, upper),
utils.annual_sum,
],
)
for lower, upper in BETWEEN_TEMP_THRESHOLDS
},
"wind_speed": utils.Transform(
source_variables=["wind_speed"],
transform_funcs=[utils.annual_mean],
Expand Down
9 changes: 9 additions & 0 deletions src/climate_data/generate/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ def count(ds: xr.Dataset) -> xr.Dataset:
return count


def count_between_threshold(
lower_threshold: int | float, upper_threshold: int | float
) -> Callable[[xr.Dataset], xr.Dataset]:
def count(ds: xr.Dataset) -> xr.Dataset:
return (ds > lower_threshold) & (ds < upper_threshold)

return count


########################
# Data transformations #
########################
Expand Down

0 comments on commit 4ddfffc

Please sign in to comment.