Skip to content

Commit e990131

Browse files
authored
Fix update clocks (#151)
* fix update clocks: 1 coord given for master clock * update + add regression test * update release notes
1 parent 27bd7a2 commit e990131

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

doc/whats_new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Bug fixes
3131
``_FillValue`` attribute (:issue:`148`).
3232
- Ensure that classes given in a Model are all process-decorated, even those
3333
which inherit from process-decorated classes (:issue:`149`).
34+
- Fix :func:`~xarray.Dataset.xsimlab.update_clocks` when only the master clock is
35+
updated implicitly (:issue:`151`).
3436

3537
v0.4.1 (17 April 2020)
3638
----------------------

xsimlab/tests/test_xr_accessor.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -218,42 +218,37 @@ def test_set_input_vars(self, model, in_dataset):
218218

219219
def test_update_clocks(self, model):
220220
ds = xr.Dataset()
221-
with pytest.raises(ValueError) as excinfo:
221+
with pytest.raises(ValueError, match="Cannot determine which clock.*"):
222222
ds.xsimlab.update_clocks(model=model, clocks={})
223-
assert "Cannot determine which clock" in str(excinfo.value)
224223

225224
ds = xr.Dataset()
226-
with pytest.raises(ValueError) as excinfo:
225+
with pytest.raises(ValueError, match="Cannot determine which clock.*"):
227226
ds.xsimlab.update_clocks(
228227
model=model, clocks={"clock": [0, 1, 2], "out": [0, 2]}
229228
)
230-
assert "Cannot determine which clock" in str(excinfo.value)
231229

232230
ds = xr.Dataset()
233-
with pytest.raises(KeyError) as excinfo:
231+
with pytest.raises(KeyError, match="Master clock dimension name.*"):
234232
ds.xsimlab.update_clocks(
235233
model=model,
236234
clocks={"clock": [0, 1, 2]},
237235
master_clock="non_existing_clock_dim",
238236
)
239-
assert "Master clock dimension name" in str(excinfo.value)
240237

241238
ds = xr.Dataset()
242-
with pytest.raises(ValueError) as excinfo:
239+
with pytest.raises(ValueError, match="Invalid dimension.*"):
243240
ds.xsimlab.update_clocks(
244241
model=model,
245242
clocks={"clock": ("x", [0, 1, 2])},
246243
)
247-
assert "Invalid dimension" in str(excinfo.value)
248244

249245
ds = xr.Dataset()
250-
with pytest.raises(ValueError) as excinfo:
246+
with pytest.raises(ValueError, match=".*not synchronized.*"):
251247
ds.xsimlab.update_clocks(
252248
model=model,
253249
clocks={"clock": [0, 1, 2], "out": [0, 0.5, 2]},
254250
master_clock="clock",
255251
)
256-
assert "not synchronized" in str(excinfo.value)
257252

258253
ds = xr.Dataset()
259254
ds = ds.xsimlab.update_clocks(model=model, clocks={"clock": [0, 1, 2]})
@@ -285,6 +280,10 @@ def test_update_clocks(self, model):
285280
new_ds = ds.xsimlab.update_clocks(model=model, clocks={"out2": [0, 2]})
286281
assert new_ds.xsimlab.master_clock_dim == "clock"
287282

283+
new_ds = ds.xsimlab.update_clocks(model=model, clocks={"clock": [0, 2, 4]})
284+
assert new_ds.xsimlab.master_clock_dim == "clock"
285+
np.testing.assert_array_equal(new_ds.clock.values, [0, 2, 4])
286+
288287
def test_update_vars(self, model, in_dataset):
289288
ds = in_dataset.xsimlab.update_vars(
290289
model=model,

xsimlab/xr_accessor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ def update_clocks(self, model=None, clocks=None, master_clock=None):
509509
):
510510
master_clock_dict = {"dim": list(clocks.keys())[0]}
511511
else:
512-
master_clock_dict = {}
512+
master_clock_dict = {"dim": self.master_clock_dim}
513513

514514
else:
515515
master_clock_dict = master_clock

0 commit comments

Comments
 (0)