Skip to content
Draft
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
18 changes: 13 additions & 5 deletions src/modelskill/comparison/_comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -907,17 +907,25 @@ def sel(
k: v.sel(time=slice(start, end)) for k, v in raw_mod_data.items()
} # type: ignore
if time is not None:
d = d.sel(time=time)
try:
d = d.sel(time=time)
except KeyError:
d = d.isel(time=slice(0, 0)) # No data for this time, return empty
else:
if "time" not in d.dims:
# Exact timestamp match returns 0-dimensional dataset; expand back to 1D
d = d.expand_dims("time")
raw_mod = {}
# Nearest time selection for raw_mod_data,
# as it may not have the same time points as the matched data
if isinstance(time, slice):
raw_mod_data = {k: v.sel(time=time) for k, v in raw_mod_data.items()} # type: ignore
else:
for k, v in raw_mod_data.items():
time_vals = v.time.values
idx = np.abs(time_vals - np.datetime64(time)).argmin()
raw_mod[k] = v.sel(time=time_vals[idx])
if len(d.time) > 0:
for k, v in raw_mod_data.items():
time_vals = v.time.values
idx = np.abs(time_vals - np.datetime64(time)).argmin()
raw_mod[k] = v.sel(time=time_vals[idx])
raw_mod_data = raw_mod
if area is not None:
if _area_is_bbox(area):
Expand Down
25 changes: 15 additions & 10 deletions tests/test_comparercollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,21 @@ def test_cc_sel_model_last(cc):
assert cc2.mod_names == ["m3"]


# TODO: FAILS
# def test_cc_sel_time_single(cc):
# cc1 = cc.sel(time="2019-01-03")
# assert cc1.n_comparers == 2
# assert cc1.n_models == 3
# assert cc1.n_points == 6
# assert cc1.start == pd.Timestamp("2019-01-03")
# assert cc1.end == pd.Timestamp("2019-01-05")
# assert cc1.obs_names == ["fake point obs", "fake track obs"]
# assert cc1.mod_names == ["m1", "m2", "m3"]
def test_cc_sel_time_string(cc):
"""Selecting a date string where both comparers have data should include both."""
# pc has data from 2019-01-01, tc starts from 2019-01-03 - both have data on Jan 3
cc1 = cc.sel(time="2019-01-03")
assert len(cc1) == 2
assert cc1.obs_names == ["fake point obs", "fake track obs"]


def test_cc_sel_time_string_no_overlap(cc):
"""Selecting a date string where one comparer has no data should exclude that comparer."""
# pc has data from 2019-01-01, tc starts from 2019-01-03
# Selecting 2019-01-01 should return only pc
cc1 = cc.sel(time="2019-01-01")
assert len(cc1) == 1
assert cc1.obs_names == ["fake point obs"]


def test_cc_sel_time(cc):
Expand Down