Skip to content

Commit f44768d

Browse files
add check for invalid start_index when inferring.
1 parent 57c29f8 commit f44768d

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

tests/test_grids/test_ugrid.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,22 @@ def test_assign_ugrid_topology_start_index_zero_infer():
387387
assert ds["mesh_edge_nodes"].attrs["start_index"] == 0
388388
assert ds["mesh_boundary_nodes"].attrs["start_index"] == 0
389389

390+
def test_assign_ugrid_topology_start_index_zero_infer_bad_data():
391+
"""
392+
if the connectivity array has negative or no zero or one indexes -- not good.
393+
"""
394+
# set a negative index
395+
ds = xr.open_dataset(EXAMPLE_DATA / "small_ugrid_zero_based.nc")
396+
faces = ds['mesh_face_nodes'].data[0, 0] = -3
397+
with pytest.raises(ValueError):
398+
ds = ugrid.assign_ugrid_topology(ds, face_node_connectivity="mesh_face_nodes")
399+
400+
# remove zero and one indexes
401+
data = ds['mesh_face_nodes'].data
402+
data[data < 2] = 100
403+
with pytest.raises(ValueError):
404+
ds = ugrid.assign_ugrid_topology(ds, face_node_connectivity="mesh_face_nodes")
405+
390406

391407
# NOTE: these tests are probably not complete -- but they are something.
392408
# we really should have a complete UGRID example to test with.

xarray_subset_grid/grids/ugrid.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,12 @@ def assign_ugrid_topology(
464464
mapping = [(dim, ds[dim].size) for dim in dims]
465465
mesh.face_dimension = next(x[0] for x in mapping if x[1] != 3 and x[1] != 4)
466466

467-
# check for start_index, and set it if there.
467+
# check for start_index, and set it not there.
468468
if mesh.start_index is None:
469+
start_index = int(ds[mesh.face_node_connectivity].min())
470+
if start_index not in (0, 1):
471+
raise ValueError(f"minimum index in face_node_connectivity array is {start_index}"
472+
" -- it should be zero (C-style indexing) or 1 (Fortran-style indexing)")
469473
mesh.start_index = int(ds[mesh.face_node_connectivity].min())
470474

471475
if mesh.start_index not in (0, 1):

0 commit comments

Comments
 (0)