Skip to content

Commit 7594df4

Browse files
committed
Add initial temporal performance refactor code
1 parent 7745757 commit 7594df4

File tree

3 files changed

+10
-26
lines changed

3 files changed

+10
-26
lines changed

.vscode/xcdat.code-workspace

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"configurations": [
6262
{
6363
"name": "Python: Current File",
64-
"type": "python",
64+
"type": "debugpy",
6565
"request": "launch",
6666
"program": "${file}",
6767
"console": "integratedTerminal",

tests/test_temporal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ def test_seasonal_departures_relative_to_climatology_reference_period(self):
18101810
},
18111811
)
18121812

1813-
assert result.identical(expected)
1813+
xr.testing.assert_identical(result, expected)
18141814

18151815
def test_monthly_departures_relative_to_climatology_reference_period_with_same_output_freq(
18161816
self,

xcdat/temporal.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -786,28 +786,19 @@ def departures(
786786

787787
# 5. Align time dimension names using the labeled time dimension name.
788788
# ----------------------------------------------------------------------
789-
# The climatology's time dimension is renamed to the labeled time
790-
# dimension in step #4 above (e.g., "time" -> "season"). xarray requires
791-
# dimension names to be aligned to perform grouped arithmetic, which we
792-
# use for calculating departures in step #5. Otherwise, this error is
793-
# raised: "`ValueError: incompatible dimensions for a grouped binary
794-
# operation: the group variable '<FREQ ARG>' is not a dimension on the
795-
# other argument`".
796789
dv_climo = ds_climo[data_var]
797-
dv_climo = dv_climo.rename({self.dim: self._labeled_time.name})
798790

799791
# 6. Calculate the departures for the data variable.
800792
# ----------------------------------------------------------------------
801793
# departures = observation - climatology
802794
with xr.set_options(keep_attrs=True):
803795
dv_departs = dv_obs_grouped - dv_climo
804796
dv_departs = self._add_operation_attrs(dv_departs)
805-
ds_obs[data_var] = dv_departs
806797

807-
# The original time dimension name is restored after grouped
808-
# arithmetic, so the labeled time dimension name is no longer needed
809-
# and therefore dropped.
810-
ds_obs = ds_obs.drop_vars(str(self._labeled_time.name))
798+
# The original time dimension is dropped from the dataset to
799+
# accomodate the new time dimension and its associated coordinates.
800+
ds_obs = ds_obs.drop_dims(str(self.dim))
801+
ds_obs[data_var] = dv_departs
811802

812803
if weighted and keep_weights:
813804
self._weights = ds_climo.time_wts
@@ -1196,6 +1187,7 @@ def _group_average(self, ds: xr.Dataset, data_var: str) -> xr.DataArray:
11961187
# Label the time coordinates for grouping weights and the data variable
11971188
# values.
11981189
self._labeled_time = self._label_time_coords(dv[self.dim])
1190+
dv = dv.assign_coords({self.dim: self._labeled_time})
11991191

12001192
if self._weighted:
12011193
time_bounds = ds.bounds.get_bounds("T", var_key=data_var)
@@ -1222,14 +1214,6 @@ def _group_average(self, ds: xr.Dataset, data_var: str) -> xr.DataArray:
12221214
else:
12231215
dv = self._group_data(dv).mean()
12241216

1225-
# After grouping and aggregating the data variable values, the
1226-
# original time dimension is replaced with the grouped time dimension.
1227-
# For example, grouping on "year_season" replaces the time dimension
1228-
# with "year_season". This dimension needs to be renamed back to
1229-
# the original time dimension name before the data variable is added
1230-
# back to the dataset so that the original name is preserved.
1231-
dv = dv.rename({self._labeled_time.name: self.dim})
1232-
12331217
# After grouping and aggregating, the grouped time dimension's
12341218
# attributes are removed. Xarray's `keep_attrs=True` option only keeps
12351219
# attributes for data variables and not their coordinates, so the
@@ -1321,7 +1305,7 @@ def _group_data(self, data_var: xr.DataArray) -> DataArrayGroupBy:
13211305
if self._mode == "average":
13221306
dv_gb = dv.groupby(f"{self.dim}.{self._freq}")
13231307
else:
1324-
dv.coords[self._labeled_time.name] = self._labeled_time
1308+
dv = dv.assign_coords({self.dim: self._labeled_time})
13251309
dv_gb = dv.groupby(self._labeled_time.name)
13261310

13271311
return dv_gb
@@ -1374,9 +1358,9 @@ def _label_time_coords(self, time_coords: xr.DataArray) -> xr.DataArray:
13741358
dt_objects = self._convert_df_to_dt(df_dt_components)
13751359

13761360
time_grouped = xr.DataArray(
1377-
name="_".join(df_dt_components.columns),
1361+
name=self.dim,
13781362
data=dt_objects,
1379-
coords={self.dim: time_coords[self.dim]},
1363+
coords={self.dim: dt_objects},
13801364
dims=[self.dim],
13811365
attrs=time_coords[self.dim].attrs,
13821366
)

0 commit comments

Comments
 (0)