Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Snow season length is 0 if there is 0 snow - fix solar_zenith_angle typo #1492

Merged
merged 7 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Changelog

v0.46.0 (unreleased)
--------------------
Contributors to this version: Éric Dupuis (:user:`coxipi`), Trevor James Smith (:user:`Zeitsperre`), David Huard (:user:`huard`).
Contributors to this version: Éric Dupuis (:user:`coxipi`), Trevor James Smith (:user:`Zeitsperre`), David Huard (:user:`huard`) and Pascal Bourgault (:user:`aulemahal`).

New features and enhancements
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -19,6 +19,7 @@ Bug fixes
* Fixed an error in the `pytest` configuration that prevented copying of testing data to thread-safe caches of workers under certain conditions (this should always occur). (:pull:`1473`).
* Coincidentally, this also fixes an error that caused `pytest` to error-out when invoked without an active internet connection. Running `pytest` without network access is now supported (requires cached testing data). (:issue:`1468`).
* Calling a ``sdba.map_blocks``-wrapped function with data chunked along the reduced dimensions will raise an error. This forbids chunking the trained dataset along the distribution dimensions, for example. (:issue:`1481`, :pull:`1482`).
* Indicators ``snd_season_length`` and ``snw_season_length`` will return 0 instead of NaN if all inputs have a (non-NaN) 0 snow depth (or water-equivalent thickness). (:pull:`1492`, :issue:`1491`)
aulemahal marked this conversation as resolved.
Show resolved Hide resolved

Breaking changes
^^^^^^^^^^^^^^^^
Expand Down
11 changes: 6 additions & 5 deletions tests/test_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2682,20 +2682,21 @@ def test_nan_slices(self, snd_series, snw_series):


class TestSnowCover:
def test_snow_season_length(self, snd_series, snw_series):
a = np.ones(366) / 100.0
a[10:20] = 0.3
@pytest.mark.parametrize("length", [0, 10])
def test_snow_season_length(self, snd_series, snw_series, length):
a = np.zeros(366)
a[10 : 10 + length] = 0.3
snd = snd_series(a)
# kg m-2 = 1000 kg m-3 * 1 m
snw = snw_series(1000 * a)

out = xci.snd_season_length(snd)
assert len(out) == 2
assert out[0] == 10
assert out[0] == length

out = xci.snw_season_length(snw)
assert len(out) == 2
assert out[0] == 10
assert out[0] == length

def test_continous_snow_season_start(self, snd_series, snw_series):
a = np.arange(366) / 100.0
Expand Down
9 changes: 6 additions & 3 deletions tests/test_snow.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ def test_simple(self, snd_series):


class TestSnowWaterCoverDuration:
def test_simple(self, snw_series):
snw = snw_series(np.ones(110) * 1000, start="2001-01-01")
@pytest.mark.parametrize(
"factor,exp", ([1000, [31, 28, 31, np.NaN]], [0, [0, 0, 0, np.NaN]])
)
def test_simple(self, snw_series, factor, exp):
snw = snw_series(np.ones(110) * factor, start="2001-01-01")
out = land.snw_season_length(snw, freq="M")
assert out.units == "days"
np.testing.assert_array_equal(out, [31, 28, 31, np.nan])
np.testing.assert_array_equal(out, exp)


class TestContinuousSnowDepthCoverStartEnd:
Expand Down
4 changes: 2 additions & 2 deletions xclim/indices/_threshold.py
Original file line number Diff line number Diff line change
Expand Up @@ -2069,7 +2069,7 @@ def snd_season_length(
xarray.DataArray, [time]
Number of days where snow depth is greater than or equal to threshold.
"""
valid = at_least_n_valid(snd.where(snd > 0), n=1, freq=freq)
valid = at_least_n_valid(snd, n=1, freq=freq)
thresh = convert_units_to(thresh, snd)
out = threshold_count(snd, op, thresh, freq)
return to_agg_units(out, snd, "count").where(~valid)
Expand Down Expand Up @@ -2106,7 +2106,7 @@ def snw_season_length(
xarray.DataArray, [time]
Number of days where snow water is greater than or equal to threshold.
"""
valid = at_least_n_valid(snw.where(snw > 0), n=1, freq=freq)
valid = at_least_n_valid(snw, n=1, freq=freq)
thresh = convert_units_to(thresh, snw)
out = threshold_count(snw, op, thresh, freq)
return to_agg_units(out, snw, "count").where(~valid)
Expand Down
2 changes: 1 addition & 1 deletion xclim/indices/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def cosine_of_solar_zenith_angle(
_wrap_radians(h_e),
stat == "average",
input_core_dims=[[]] * 6,
dask="parallel",
dask="parallelized",
)


Expand Down