Skip to content

Commit 43aaa52

Browse files
add check for invalid start_index when inferring.
1 parent ce61e14 commit 43aaa52

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
@@ -419,6 +419,22 @@ def test_assign_ugrid_topology_start_index_zero_infer():
419419
assert ds["mesh_edge_nodes"].attrs["start_index"] == 0
420420
assert ds["mesh_boundary_nodes"].attrs["start_index"] == 0
421421

422+
def test_assign_ugrid_topology_start_index_zero_infer_bad_data():
423+
"""
424+
if the connectivity array has negative or no zero or one indexes -- not good.
425+
"""
426+
# set a negative index
427+
ds = xr.open_dataset(EXAMPLE_DATA / "small_ugrid_zero_based.nc")
428+
faces = ds['mesh_face_nodes'].data[0, 0] = -3
429+
with pytest.raises(ValueError):
430+
ds = ugrid.assign_ugrid_topology(ds, face_node_connectivity="mesh_face_nodes")
431+
432+
# remove zero and one indexes
433+
data = ds['mesh_face_nodes'].data
434+
data[data < 2] = 100
435+
with pytest.raises(ValueError):
436+
ds = ugrid.assign_ugrid_topology(ds, face_node_connectivity="mesh_face_nodes")
437+
422438

423439
# NOTE: these tests are probably not complete -- but they are something.
424440
# 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)