Skip to content

Commit

Permalink
add holiday snow indicator tests
Browse files Browse the repository at this point in the history
Signed-off-by: Trevor James Smith <10819524+Zeitsperre@users.noreply.github.com>
  • Loading branch information
Zeitsperre committed Dec 20, 2024
1 parent f9e6c5a commit 03d24ca
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/test_snow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pytest
import xarray as xr

from xclim import land
from xclim.core import ValidationError
Expand Down Expand Up @@ -120,3 +121,61 @@ def test_simple(self, snw_series):
snw = snw_series(a, start="2001-01-01")
out = land.snw_max_doy(snw, freq="YS")
np.testing.assert_array_equal(out, [21, np.nan])


class TestHolidaySnowIndicators:

def test_xmas_days_simple(self, nimbus):
ds = xr.open_dataset(
nimbus.fetch(
"cmip6/snw_day_CanESM5_historical_r1i1p1f1_gn_19910101-20101231.nc"
)
)
snd = land.snw_to_snd(ds.snw)

out = land.holiday_snow_days(snd)

assert out.units == "1"
assert out.long_name == "Number of holiday days with snow"
np.testing.assert_array_equal(
out.sum(dim="time"),
[
[7.0, 5.0, 2.0, 0.0, 0.0],
[14.0, 13.0, 9.0, 6.0, 2.0],
[18.0, 19.0, 19.0, 18.0, 13.0],
[20.0, 20.0, 20.0, 20.0, 20.0],
[20.0, 20.0, 20.0, 20.0, 20.0],
[20.0, 20.0, 20.0, 20.0, 20.0],
],
)

def test_perfect_xmas_days_simple(self, nimbus):
ds_snw = xr.open_dataset(
nimbus.fetch(
"cmip6/snw_day_CanESM5_historical_r1i1p1f1_gn_19910101-20101231.nc"
)
)
ds_prsn = xr.open_dataset(
nimbus.fetch(
"cmip6/prsn_day_CanESM5_historical_r1i1p1f1_gn_19910101-20101231.nc"
)
)

snd = land.snw_to_snd(ds_snw.snw)
prsn = ds_prsn.prsn

out = land.holiday_snow_and_snowfall_days(snd, prsn)

assert out.units == "1"
assert out.long_name == "Number of holiday days with snow and snowfall"
np.testing.assert_array_equal(
out.sum(dim="time"),
[
[0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 1.0, 0.0],
[1.0, 0.0, 0.0, 0.0, 1.0],
[0.0, 0.0, 1.0, 1.0, 1.0],
[0.0, 0.0, 0.0, 0.0, 0.0],
],
)

0 comments on commit 03d24ca

Please sign in to comment.