nebius/SWE-rebench-V2-PRs huggingface dataset:
Repository: DHI/mikeio
Pull request #468 (2022-11-14 07:31:02)

Wildcard selection fails for dfsu 3d


Patch:
diff --git a/mikeio/dfsutil.py b/mikeio/dfsutil.py
index ba4b7efb..f5fe6f63 100644
--- a/mikeio/dfsutil.py
+++ b/mikeio/dfsutil.py
@@ -8,11 +8,11 @@ from .custom_exceptions import ItemsError
 from mikecore.DfsFile import DfsDynamicItemInfo, DfsFileInfo
 
 
-def _fuzzy_item_search(dfsItemInfo: List[DfsDynamicItemInfo], search: str):
+def _fuzzy_item_search(*,dfsItemInfo: List[DfsDynamicItemInfo], search: str, start_idx:int=0):
     import fnmatch
 
     names = [info.Name for info in dfsItemInfo]
-    item_numbers = [i for i, name in enumerate(names) if fnmatch.fnmatch(name, search)]
+    item_numbers = [i-start_idx for i, name in enumerate(names) if fnmatch.fnmatch(name, search)]
     if len(item_numbers) == 0:
         raise KeyError(f"No items like: {search} found. Valid names are {names}")
     return item_numbers
@@ -30,7 +30,7 @@ def _valid_item_numbers(
 
     if np.isscalar(items):
         if isinstance(items, str) and "*" in items:
-            return _fuzzy_item_search(dfsItemInfo=dfsItemInfo, search=items)
+            return _fuzzy_item_search(dfsItemInfo=dfsItemInfo, search=items, start_idx=start_idx)
         else:
             items = [items]
 


Test patch:
diff --git a/tests/test_dfsu_layered.py b/tests/test_dfsu_layered.py
index 046b59f7..11928c44 100644
--- a/tests/test_dfsu_layered.py
+++ b/tests/test_dfsu_layered.py
@@ -635,3 +635,11 @@ def test_dataset_write_dfsu3d_max(tmp_path):
     ds2 = mikeio.read(outfilename)
     assert ds2.n_timesteps == 1
     assert ds2.geometry.is_layered
+
+def test_read_wildcard_items():
+    dfs = mikeio.open("tests/testdata/oresund_sigma_z.dfsu")
+    assert dfs.items[1].name == "Salinity"
+    
+    ds = dfs.read(items="Sal*")
+    assert ds.items[0].name == "Salinity"
+    assert ds.n_items == 1
\ No newline at end of file