Skip to content

Commit

Permalink
Use ResamplingIndicatorWithIndexing for chill_units
Browse files Browse the repository at this point in the history
  • Loading branch information
saschahofmann committed Sep 19, 2024
1 parent 0659e6a commit ad5a7ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
14 changes: 14 additions & 0 deletions tests/test_atmos.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import xarray as xr

from xclim import atmos, set_options
from xclim.indices.helpers import make_hourly_temperature

K2C = 273.16

Expand Down Expand Up @@ -624,3 +625,16 @@ def test_late_frost_days(self, atmosds):
lfd = atmos.late_frost_days(tasmin, date_bounds=("04-01", "06-30"))

np.testing.assert_allclose(lfd.isel(time=0), exp, rtol=1e-03)


def test_chill_units(atmosds):
tasmax = atmosds.tasmax
tasmin = atmosds.tasmin
tas = make_hourly_temperature(tasmin, tasmax)
cu = atmos.chill_units(tas, date_bounds=("04-01", "06-30"))
assert cu.attrs["units"] == "1"
assert cu.name == "cu"
assert cu.time.size == 4

exp = [-5029.5, -6634.5, -5993.0, -6596.0, -5654.0]
np.testing.assert_allclose(cu.isel(time=0), exp, rtol=1e-03)
15 changes: 11 additions & 4 deletions xclim/indicators/atmos/_temperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ class TempWithIndexing(ResamplingIndicatorWithIndexing):
keywords = "temperature"


class TempHourlyWithIndexing(ResamplingIndicatorWithIndexing):
"""Indicators involving hourly temperature and adding an indexing possibility."""

src_freq = "h"
keywords = "temperature"


tn_days_above = TempWithIndexing(
title="Number of days with minimum temperature above a given threshold",
identifier="tn_days_above",
Expand Down Expand Up @@ -1459,8 +1466,7 @@ def cfcheck(self, tas, snd=None):
title="Chill portions",
identifier="cp",
units="",
# TODO: check what this does
cell_methods="",
cell_methods="time: sum",
description="Chill portions are a measure to estimate the bud breaking potential of different crops. "
"The constants and functions are taken from Luedeling et al. (2009) which formalises "
"the method described in Fishman et al. (1987). ",
Expand All @@ -1478,14 +1484,15 @@ def cfcheck(self, tas, snd=None):
compute=indices.chill_portions,
)

chill_units = TempHourly(
chill_units = TempHourlyWithIndexing(
title="Chill units",
identifier="cu",
units="",
cell_methods="time: sum",
description="Chill units are a measure to estimate the bud breaking potential of different crops based on the Utah model developed in "
"Richardson et al. (1974). The Utah model assigns a weight to each hour depending on the temperature recognising that high temperatures can "
"actually decrease the potential for bud breaking.",
long_name="Chill units after the Utah Model",
allowed_periods=["A"],
allowed_periods=["Y"],
compute=indices.chill_units,
)
1 change: 1 addition & 0 deletions xclim/indices/_agro.py
Original file line number Diff line number Diff line change
Expand Up @@ -1570,6 +1570,7 @@ def _apply_chill_portion_one_season(tas_K):
tas_K,
input_core_dims=[["time"]],
output_core_dims=[["time"]],
output_dtypes=[tas_K.dtype],
dask="parallelized",
).sum("time")

Expand Down

0 comments on commit ad5a7ec

Please sign in to comment.