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

Fix multiple temporal avg calls on same dataset breaking #329

Merged
merged 1 commit into from
Aug 25, 2022
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
13 changes: 13 additions & 0 deletions tests/test_temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,7 @@ def test_weights_for_yearly_averages(self):
ds.temporal._mode = "average"
ds.temporal._freq = "year"
ds.temporal._weighted = "True"
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="year",
data=np.array(
Expand Down Expand Up @@ -1668,6 +1669,7 @@ def test_weights_for_monthly_averages(self):
ds.temporal._mode = "average"
ds.temporal._freq = "month"
ds.temporal._weighted = "True"
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="month",
data=np.array(
Expand Down Expand Up @@ -1734,6 +1736,7 @@ def test_weights_for_yearly_averages(self):
ds.temporal._mode = "group_average"
ds.temporal._freq = "year"
ds.temporal._weighted = "True"
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="year",
data=np.array(
Expand Down Expand Up @@ -1796,6 +1799,7 @@ def test_weights_for_monthly_averages(self):
ds.temporal._mode = "group_average"
ds.temporal._freq = "month"
ds.temporal._weighted = "True"
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="year_month",
data=np.array(
Expand Down Expand Up @@ -1940,6 +1944,7 @@ def test_weights_for_seasonal_averages_with_DJF_and_drop_incomplete_seasons(
ds.temporal._freq = "season"
ds.temporal._weighted = "True"
ds.temporal._season_config = {"dec_mode": "DJF"}
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="year_season",
data=np.array(
Expand Down Expand Up @@ -2001,6 +2006,7 @@ def test_weights_for_seasonal_averages_with_JFD(self):
ds.temporal._freq = "season"
ds.temporal._weighted = "True"
ds.temporal._season_config = {"dec_mode": "JDF"}
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="year_season",
data=np.array(
Expand Down Expand Up @@ -2069,6 +2075,7 @@ def test_custom_season_time_series_weights(self):
ds.temporal._mode = "group_average"
ds.temporal._freq = "season"
ds.temporal._weighted = "True"
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._season_config = {
"custom_seasons": {
"JanFebMar": ["Jan", "Feb", "Mar"],
Expand Down Expand Up @@ -2145,6 +2152,7 @@ def test_weights_for_daily_averages(self):
ds.temporal._mode = "group_average"
ds.temporal._freq = "day"
ds.temporal._weighted = "True"
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="year_month_day",
data=np.array(
Expand Down Expand Up @@ -2190,6 +2198,7 @@ def test_weights_for_hourly_averages(self):
ds.temporal._freq = "hour"
ds.temporal._weighted = "True"
ds.temporal._season_config = {"dec_mode": "JDF"}
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="year_month_day_hour",
data=np.array(
Expand Down Expand Up @@ -2337,6 +2346,7 @@ def test_weights_for_seasonal_climatology_with_DJF(self):
ds.temporal._freq = "season"
ds.temporal._weighted = "True"
ds.temporal._season_config = {"dec_mode": "DJF"}
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="season",
data=np.array(
Expand Down Expand Up @@ -2393,6 +2403,7 @@ def test_weights_for_seasonal_climatology_with_JFD(self):
ds.temporal._freq = "season"
ds.temporal._weighted = "True"
ds.temporal._season_config = {"dec_mode": "JDF"}
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="season",
data=np.array(
Expand Down Expand Up @@ -2456,6 +2467,7 @@ def test_weights_for_annual_climatology(self):
ds.temporal._mode = "climatology"
ds.temporal._freq = "month"
ds.temporal._weighted = "True"
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="month",
data=np.array(
Expand Down Expand Up @@ -2518,6 +2530,7 @@ def test_weights_for_daily_climatology(self):
ds.temporal._mode = "climatology"
ds.temporal._freq = "day"
ds.temporal._weighted = "True"
ds.temporal._time_bounds = ds.time_bnds
ds.temporal._labeled_time = xr.DataArray(
name="month_day",
data=np.array(
Expand Down
6 changes: 4 additions & 2 deletions xcdat/temporal.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ class TemporalAccessor:
def __init__(self, dataset: xr.Dataset):
self._dataset: xr.Dataset = dataset

# The name of the time dimension.
self._dim = get_axis_coord(self._dataset, "T").name

self._time_bounds = self._dataset.bounds.get_bounds("T").copy()

try:
self.calendar = self._dataset[self._dim].encoding["calendar"]
self.date_type = get_date_type(self.calendar)
Expand Down Expand Up @@ -739,6 +738,9 @@ def _set_obj_attrs(
ValueError
If an incorrect ``dec_mode`` arg was passed.
"""
# The time bounds.
self._time_bounds = self._dataset.bounds.get_bounds("T")

Comment on lines +741 to +743
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had this line here originally, but made the silly mistake of moving it to the class__init__ method in another PR which caused this bug.

# General configuration attributes.
if mode not in list(MODES):
modes = ", ".join(f'"{word}"' for word in MODES)
Expand Down