From ad5a7ec8278eea1de9860d900e83b7d7986468f1 Mon Sep 17 00:00:00 2001 From: saschahofmann Date: Thu, 19 Sep 2024 17:59:34 +0200 Subject: [PATCH] Use ResamplingIndicatorWithIndexing for chill_units --- tests/test_atmos.py | 14 ++++++++++++++ xclim/indicators/atmos/_temperature.py | 15 +++++++++++---- xclim/indices/_agro.py | 1 + 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/tests/test_atmos.py b/tests/test_atmos.py index 23929550d..f0b86ceee 100644 --- a/tests/test_atmos.py +++ b/tests/test_atmos.py @@ -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 @@ -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) diff --git a/xclim/indicators/atmos/_temperature.py b/xclim/indicators/atmos/_temperature.py index 970b0d985..bdc94548a 100644 --- a/xclim/indicators/atmos/_temperature.py +++ b/xclim/indicators/atmos/_temperature.py @@ -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", @@ -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). ", @@ -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, ) diff --git a/xclim/indices/_agro.py b/xclim/indices/_agro.py index f3e3fa7c7..cbff71ca6 100644 --- a/xclim/indices/_agro.py +++ b/xclim/indices/_agro.py @@ -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")