Skip to content

Commit 73c70e2

Browse files
committed
Add _exclude_sub_monthly_coord_spanning_year to replicate cdms2 "co" flag
1 parent 5b91eca commit 73c70e2

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

e3sm_diags/driver/utils/dataset_xr.py

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,10 +1178,7 @@ def _subset_time_series_dataset(self, ds: xr.Dataset, filepath: str) -> xr.Datas
11781178
time_slice = self._get_time_slice(ds, filepath)
11791179
ds_subset = ds.sel(time=time_slice).squeeze()
11801180

1181-
# For sub-monthly data, exclude the last time coordinate. This mimics
1182-
# the CDAT "co" slice flag behavior for subsetting.
1183-
if self.is_sub_monthly:
1184-
ds_subset = ds_subset.isel(time=slice(0, -1))
1181+
ds_subset = self._exclude_sub_monthly_coord_spanning_year(ds_subset)
11851182

11861183
return ds_subset
11871184

@@ -1406,6 +1403,46 @@ def _get_month_day_str(self, month: int, day: int) -> str:
14061403

14071404
return f"{month_str}-{day_str}"
14081405

1406+
def _exclude_sub_monthly_coord_spanning_year(
1407+
self, ds_subset: xr.Dataset
1408+
) -> xr.Dataset:
1409+
"""
1410+
Exclude the last time coordinate for sub-monthly data if it extends into
1411+
the next year.
1412+
1413+
Excluding end time coordinates that extend to the next year is
1414+
necessary because downstream operations such as annual cycle climatology
1415+
should consist of data for full years for accurate calculations.
1416+
1417+
For example, if the time slice is ("0001-01-01", "0002-01-01") and
1418+
the last time coordinate is:
1419+
* "0002-01-01" -> exclude
1420+
* "0001-12-31" -> don't exclude
1421+
1422+
Parameters
1423+
----------
1424+
ds_subset : xr.Dataset
1425+
The subsetted dataset.
1426+
1427+
Returns
1428+
-------
1429+
xr.Dataset
1430+
The dataset with the last time coordinate excluded if necessary.
1431+
1432+
Notes
1433+
-----
1434+
This function replicates the CDAT cdms2 "co" slice flag (close, open).
1435+
"""
1436+
time_dim = xc.get_dim_keys(ds_subset, axis="T")
1437+
time_values = ds_subset[time_dim]
1438+
last_time_year = time_values[-1].dt.year.item()
1439+
second_time_year = time_values[-2].dt.year.item()
1440+
1441+
if self.is_sub_monthly and last_time_year > second_time_year:
1442+
ds_subset = ds_subset.isel(time=slice(0, -1))
1443+
1444+
return ds_subset
1445+
14091446
def _center_time_for_non_submonthly_data(self, ds: xr.Dataset) -> xr.Dataset:
14101447
"""Center time coordinates using bounds for non-submonthly data.
14111448

0 commit comments

Comments
 (0)