diff --git a/tests/test_grids/test_ugrid.py b/tests/test_grids/test_ugrid.py index 3bdcfd3..b745643 100644 --- a/tests/test_grids/test_ugrid.py +++ b/tests/test_grids/test_ugrid.py @@ -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. diff --git a/xarray_subset_grid/grids/ugrid.py b/xarray_subset_grid/grids/ugrid.py index b2b0a8e..3ef0239 100644 --- a/xarray_subset_grid/grids/ugrid.py +++ b/xarray_subset_grid/grids/ugrid.py @@ -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):