Skip to content
Open
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
16 changes: 16 additions & 0 deletions tests/test_grids/test_ugrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,22 @@ def test_assign_ugrid_topology_start_index_zero_infer():
assert ds["mesh_edge_nodes"].attrs["start_index"] == 0
assert ds["mesh_boundary_nodes"].attrs["start_index"] == 0

def test_assign_ugrid_topology_start_index_zero_infer_bad_data():
"""
if the connectivity array has negative or no zero or one indexes -- not good.
"""
# set a negative index
ds = xr.open_dataset(EXAMPLE_DATA / "small_ugrid_zero_based.nc")
faces = ds['mesh_face_nodes'].data[0, 0] = -3
with pytest.raises(ValueError):
ds = ugrid.assign_ugrid_topology(ds, face_node_connectivity="mesh_face_nodes")

# remove zero and one indexes
data = ds['mesh_face_nodes'].data
data[data < 2] = 100
with pytest.raises(ValueError):
ds = ugrid.assign_ugrid_topology(ds, face_node_connectivity="mesh_face_nodes")


# NOTE: these tests are probably not complete -- but they are something.
# we really should have a complete UGRID example to test with.
Expand Down
6 changes: 5 additions & 1 deletion xarray_subset_grid/grids/ugrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,12 @@ def assign_ugrid_topology(
mapping = [(dim, ds[dim].size) for dim in dims]
mesh.face_dimension = next(x[0] for x in mapping if x[1] != 3 and x[1] != 4)

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

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