Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
62 changes: 62 additions & 0 deletions tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,68 @@ def test_weighted_seasonal_averages_with_DJF_and_drop_incomplete_djf(self):

xr.testing.assert_identical(result, expected)

@staticmethod
def _daily_month_dataset() -> xr.Dataset:
# Four complete years of daily data whose value is the calendar month.
time = pd.date_range("2001-01-01", "2004-12-31", freq="D")
ds = xr.Dataset(
{"ts": ("time", time.month.values.astype("float64"))},
coords={
"time": xr.DataArray(
time, dims="time", attrs={"axis": "T", "standard_name": "time"}
)
},
)

return ds.bounds.add_missing_bounds(axes=["T"])

@pytest.mark.parametrize(
"season_config",
[
{"dec_mode": "DJF", "drop_incomplete_seasons": True},
{"dec_mode": "DJF", "drop_incomplete_djf": True},
],
)
def test_seasonal_averages_with_DJF_drop_incomplete_on_daily_data(
self, season_config
):
ds = self._daily_month_dataset()

with warnings.catch_warnings():
# `drop_incomplete_djf` is deprecated and emits a warning.
warnings.simplefilter("ignore", DeprecationWarning)
result = ds.temporal.group_average(
"ts", "season", season_config=season_config
)

# The incomplete Jan-Feb 2001 and Dec 2004 seasons are dropped entirely,
# leaving 3 complete DJF seasons and 4 each of MAM, JJA and SON.
assert result.time.size == 15
assert result.time.dt.month.values[0] == 4
assert result.time.dt.year.values[0] == 2001

djf = result.ts.where(result.time.dt.month == 1, drop=True)
assert djf.size == 3
# Each DJF mean mixes Dec (12) with Jan (1) and Feb (2) days.
assert np.all(djf.values > 4.0)
assert np.all(djf.values < 6.0)

def test_climatology_with_DJF_drop_incomplete_seasons_on_daily_data(self):
ds = self._daily_month_dataset()

result = ds.temporal.climatology(
"ts",
"season",
season_config={"dec_mode": "DJF", "drop_incomplete_seasons": True},
)

assert result.time.size == 4
djf = result.ts.where(result.time.dt.month == 1, drop=True).item()
# Only complete DJF seasons (Dec 2001-Feb 2004) enter the climatology.
expected = ds.ts.sel(time=slice("2001-12-01", "2004-02-29"))
expected = expected.where(expected.time.dt.season == "DJF", drop=True)
np.testing.assert_allclose(djf, expected.mean().item(), rtol=1e-2)

def test_weighted_seasonal_averages_with_JFD(self):
ds = self.ds.copy()

Expand Down
13 changes: 10 additions & 3 deletions xcdat/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1461,8 +1461,11 @@ def _drop_incomplete_djf(self, dataset: xr.Dataset) -> xr.Dataset:

for year_month in incomplete_seasons:
try:
coord_pt = ds.loc[dict(time=year_month)][self.dim][0]
ds_time = ds_time.where(ds_time[self.dim] != coord_pt, drop=True)
# Drop every time coordinate in the incomplete month, not just
# the first one, so that sub-monthly (e.g., daily) data are
# handled correctly.
coord_pts = ds.loc[dict(time=year_month)][self.dim].values
ds_time = ds_time.where(~ds_time[self.dim].isin(coord_pts), drop=True)
except (KeyError, IndexError):
continue

Expand Down Expand Up @@ -1509,7 +1512,11 @@ def _drop_incomplete_seasons(self, ds: xr.Dataset) -> xr.Dataset:

# Get the expected and actual number of months for each season group.
df["expected_months"] = df["season"].str.split(r"(?<=.)(?=[A-Z])").str.len()
df["actual_months"] = df.groupby(["year", "season"])["year"].transform("count")
# Count the distinct months (not the time steps) in each season so that
# sub-monthly (e.g., daily) data are handled correctly.
df["actual_months"] = df.groupby(["year", "season"])["month"].transform(
"nunique"
)

# Get the incomplete seasons and drop the time coordinates that are in
# those incomplete seasons.
Expand Down
Loading