From 727ecd441a41bb76c45e69da1ff4720c5a5a8ea3 Mon Sep 17 00:00:00 2001 From: saschahofmann Date: Thu, 28 Nov 2024 10:04:38 +0100 Subject: [PATCH 1/6] Add positive chill units --- tests/test_indices.py | 3 +++ xclim/indices/_agro.py | 13 ++++++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/tests/test_indices.py b/tests/test_indices.py index 7269931c4..db8302a3c 100644 --- a/tests/test_indices.py +++ b/tests/test_indices.py @@ -372,6 +372,9 @@ def test_chill_units(self, tas_series): out = xci.chill_units(tas) assert out[0] == 0.5 * num_cu_05 + num_cu_1 - 0.5 * num_cu_min_05 - num_cu_min_1 + out = xci.chill_units(tas, positive_only=True) + # Only the last day contains negative chill units. + assert out[0] == 0.5 * num_cu_05 + num_cu_1 - 0.5 * 3 def test_cool_night_index(self, open_dataset): ds = open_dataset("cmip5/tas_Amon_CanESM2_rcp85_r1i1p1_200701-200712.nc") ds = ds.rename(dict(tas="tasmin")) diff --git a/xclim/indices/_agro.py b/xclim/indices/_agro.py index d26b0a918..5115c3f56 100644 --- a/xclim/indices/_agro.py +++ b/xclim/indices/_agro.py @@ -1636,17 +1636,21 @@ def chill_portions( @declare_units(tas="[temperature]") -def chill_units(tas: xarray.DataArray, freq: str = "YS") -> xarray.DataArray: +def chill_units( + tas: xarray.DataArray, positive_only=False, freq: str = "YS" +) -> xarray.DataArray: """Chill units using the Utah model Chill units are a measure to estimate the bud breaking potential of different crop based on Richardson et al. (1974). The Utah model assigns a weight to each hour depending on the temperature recognising that high temperatures can actual decrease, - the potential for bud breaking. + the potential for bud breaking. Providing `positive_only=True` will ignore days with negative chill units. Parameters ---------- tas : xr.DataArray Hourly temperature. + positive_only : bool + If True, only positive daily chill units are aggregated. Returns ------- @@ -1681,4 +1685,7 @@ def chill_units(tas: xarray.DataArray, freq: str = "YS") -> xarray.DataArray: ), ) cu = cu.where(tas.notnull()) - return cu.resample(time=freq).sum().assign_attrs(units="") + daily = cu.resample(time="1D").sum() + if positive_only: + daily = daily.where(daily > 0) + return daily.resample(time=freq).sum().assign_attrs(units="") From ad4eb6c8b75db878a2e6faf7d24431a790039b7b Mon Sep 17 00:00:00 2001 From: saschahofmann Date: Thu, 28 Nov 2024 10:10:18 +0100 Subject: [PATCH 2/6] Update changelog --- CHANGELOG.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index edd3e3e23..c0c1bad56 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -44,6 +44,10 @@ Bug fixes * Reorganised how ``Indicator`` subclasses can added arguments to the call signature. Injecting such arguments now works. For xclim's subclasses, this bug only affected the ``indexer`` argument of indicators subclassing ``xc.core.indicator.IndexingIndicator``. (:pull:`1981`). * All-nan slices are now treated correctly in method `ExtremeValues`. (:issue:`1982`, :pull:`1983`). +New features and enhancements +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +* ``chill_unit`` now accepts a new argument `positive_only` to compute the daily positive chill units. (:pull:`2003`). + v0.53.1 (2024-10-21) -------------------- Contributors to this version: Trevor James Smith (:user:`Zeitsperre`). From 1849dd921a24294b3a310d42739bb97ad53932cf Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 28 Nov 2024 09:12:33 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_indices.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_indices.py b/tests/test_indices.py index ff508cc0e..22978b49d 100644 --- a/tests/test_indices.py +++ b/tests/test_indices.py @@ -375,6 +375,7 @@ def test_chill_units(self, tas_series): out = xci.chill_units(tas, positive_only=True) # Only the last day contains negative chill units. assert out[0] == 0.5 * num_cu_05 + num_cu_1 - 0.5 * 3 + def test_cool_night_index(self, open_dataset): ds = open_dataset("cmip5/tas_Amon_CanESM2_rcp85_r1i1p1_200701-200712.nc") ds = ds.rename(dict(tas="tasmin")) From e0a639a21bd3fcbf8e51e5476c1b7c686fcbee24 Mon Sep 17 00:00:00 2001 From: saschahofmann Date: Thu, 28 Nov 2024 11:29:49 +0100 Subject: [PATCH 4/6] Add positive_only parameter to indicator definition --- xclim/indicators/atmos/_temperature.py | 1 + 1 file changed, 1 insertion(+) diff --git a/xclim/indicators/atmos/_temperature.py b/xclim/indicators/atmos/_temperature.py index 2f0cc1e87..70aed3870 100644 --- a/xclim/indicators/atmos/_temperature.py +++ b/xclim/indicators/atmos/_temperature.py @@ -1518,4 +1518,5 @@ def cfcheck(self, tas, snd=None): long_name="Chill units after the Utah Model", allowed_periods=["Y"], compute=indices.chill_units, + parameters=dict(positive_only={"default": False, "kind": InputKind.BOOL}), ) From fb0570b116ce6a322f5887f999e4bed50cfbf353 Mon Sep 17 00:00:00 2001 From: saschahofmann Date: Mon, 2 Dec 2024 09:23:11 +0100 Subject: [PATCH 5/6] Resolve PR comments --- CHANGELOG.rst | 9 +++++---- xclim/indicators/atmos/_temperature.py | 1 - xclim/indices/_agro.py | 11 ++++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c0c1bad56..19d9e92a5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,7 +4,7 @@ Changelog v0.54.0 (unreleased) -------------------- -Contributors to this version: Trevor James Smith (:user:`Zeitsperre`), Pascal Bourgault (:user:`aulemahal`), Éric Dupuis (:user:`coxipi`). +Contributors to this version: Trevor James Smith (:user:`Zeitsperre`), Pascal Bourgault (:user:`aulemahal`), Éric Dupuis (:user:`coxipi`), Sascha Hofmann (:user:`saschahofmann`). Breaking changes ^^^^^^^^^^^^^^^^ @@ -28,6 +28,10 @@ CI changes ^^^^^^^^^^ * Added the `green-coding-solutions/eco-ci-energy-estimation` GitHub Action to the workflows to establish energy and carbon usage of CI activity. (:pull:`1863`). +New features and enhancements +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +* ``chill_unit`` now accepts a new argument `positive_only` to compute the daily positive chill units. (:pull:`2003`). + v0.53.2 (2024-10-31) -------------------- Contributors to this version: Éric Dupuis (:user:`coxipi`), Pascal Bourgault (:user:`aulemahal`), Trevor James Smith (:user:`Zeitsperre`). @@ -44,9 +48,6 @@ Bug fixes * Reorganised how ``Indicator`` subclasses can added arguments to the call signature. Injecting such arguments now works. For xclim's subclasses, this bug only affected the ``indexer`` argument of indicators subclassing ``xc.core.indicator.IndexingIndicator``. (:pull:`1981`). * All-nan slices are now treated correctly in method `ExtremeValues`. (:issue:`1982`, :pull:`1983`). -New features and enhancements -^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -* ``chill_unit`` now accepts a new argument `positive_only` to compute the daily positive chill units. (:pull:`2003`). v0.53.1 (2024-10-21) -------------------- diff --git a/xclim/indicators/atmos/_temperature.py b/xclim/indicators/atmos/_temperature.py index 70aed3870..2f0cc1e87 100644 --- a/xclim/indicators/atmos/_temperature.py +++ b/xclim/indicators/atmos/_temperature.py @@ -1518,5 +1518,4 @@ def cfcheck(self, tas, snd=None): long_name="Chill units after the Utah Model", allowed_periods=["Y"], compute=indices.chill_units, - parameters=dict(positive_only={"default": False, "kind": InputKind.BOOL}), ) diff --git a/xclim/indices/_agro.py b/xclim/indices/_agro.py index 00116ffa0..4fe48a4e9 100644 --- a/xclim/indices/_agro.py +++ b/xclim/indices/_agro.py @@ -1636,7 +1636,7 @@ def chill_portions( @declare_units(tas="[temperature]") def chill_units( - tas: xarray.DataArray, positive_only=False, freq: str = "YS" + tas: xarray.DataArray, positive_only: bool = False, freq: str = "YS" ) -> xarray.DataArray: """Chill units using the Utah model @@ -1649,7 +1649,7 @@ def chill_units( tas : xr.DataArray Hourly temperature. positive_only : bool - If True, only positive daily chill units are aggregated. + If `True`, only positive daily chill units are aggregated. Returns ------- @@ -1684,7 +1684,8 @@ def chill_units( ), ) cu = cu.where(tas.notnull()) - daily = cu.resample(time="1D").sum() + if positive_only: - daily = daily.where(daily > 0) - return daily.resample(time=freq).sum().assign_attrs(units="") + daily = cu.resample(time="1D").sum() + cu = daily.where(daily > 0) + return cu.resample(time=freq).sum().assign_attrs(units="") From 407c2e9ed4761c18016bc2d831c3438d95675a8d Mon Sep 17 00:00:00 2001 From: Trevor James Smith <10819524+Zeitsperre@users.noreply.github.com> Date: Mon, 2 Dec 2024 10:13:42 -0500 Subject: [PATCH 6/6] Update CHANGELOG.rst --- CHANGELOG.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 19d9e92a5..6fa1c1bf1 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -48,7 +48,6 @@ Bug fixes * Reorganised how ``Indicator`` subclasses can added arguments to the call signature. Injecting such arguments now works. For xclim's subclasses, this bug only affected the ``indexer`` argument of indicators subclassing ``xc.core.indicator.IndexingIndicator``. (:pull:`1981`). * All-nan slices are now treated correctly in method `ExtremeValues`. (:issue:`1982`, :pull:`1983`). - v0.53.1 (2024-10-21) -------------------- Contributors to this version: Trevor James Smith (:user:`Zeitsperre`).