Skip to content

Commit 0cf3c89

Browse files
Added additional validation for face edge connectivity. Also added unit tests.
1 parent 0bbb470 commit 0cf3c89

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

src/emsarray/conventions/ugrid.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,31 @@ def has_valid_face_edge_connectivity(self) -> bool:
748748
)
749749
return False
750750

751+
try:
752+
fill_value = data_array.encoding['_FillValue']
753+
except KeyError:
754+
return True
755+
756+
lower_bound = _get_start_index(data_array)
757+
theoretical_upper_bound = self.face_count * self.max_node_count
758+
actual_upper_bound = numpy.nanmax(data_array)
759+
760+
if lower_bound < fill_value < actual_upper_bound:
761+
warnings.warn(
762+
f"Got a face_edge_connectivity variable {data_array.name!r} with "
763+
f"a _FillValue inside the actual index range",
764+
ConventionViolationWarning,
765+
)
766+
return False
767+
768+
if lower_bound < fill_value < theoretical_upper_bound:
769+
warnings.warn(
770+
f"Got a face_edge_connectivity variable {data_array.name!r} with "
771+
f"a _FillValue inside the theoretical index range",
772+
ConventionViolationWarning,
773+
)
774+
return False
775+
751776
return True
752777

753778
@cached_property

tests/conventions/test_ugrid.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,3 +924,40 @@ def da(attrs: dict) -> xarray.DataArray:
924924

925925
with pytest.raises(ConventionViolationError):
926926
_get_start_index(da({'start_index': 2}))
927+
928+
929+
def test_has_valid_face_edge_connectivity():
930+
# Create dataset with face_edges
931+
dataset = make_dataset(width=3, make_edges=True, make_face_coordinates=True)
932+
topology = dataset.ems.topology
933+
topology.mesh_variable.attrs.update({
934+
'face_edge_connectivity': 'Mesh2_face_edges',
935+
})
936+
937+
mesh2_face_edges_array = topology.face_edge_array
938+
939+
mesh2_face_edges = xarray.DataArray(
940+
mesh2_face_edges_array,
941+
dims=[topology.face_dimension, topology.max_node_dimension],
942+
)
943+
944+
dataset = dataset.assign({
945+
'Mesh2_face_edges': mesh2_face_edges,
946+
})
947+
948+
dataset_fill_value_in_actual_range = dataset.copy()
949+
950+
dataset_fill_value_in_theoretical_range = dataset.copy()
951+
952+
# Make sure original dataset is valid
953+
assert dataset.ems.topology.has_valid_face_edge_connectivity is True
954+
955+
dataset_fill_value_in_actual_range['Mesh2_face_edges'].encoding['_FillValue'] = 2
956+
957+
with pytest.warns(ConventionViolationWarning):
958+
assert dataset_fill_value_in_actual_range.ems.topology.has_valid_face_edge_connectivity is not True
959+
960+
dataset_fill_value_in_theoretical_range['Mesh2_face_edges'].encoding['_FillValue'] = 88
961+
962+
with pytest.warns(ConventionViolationWarning):
963+
assert dataset_fill_value_in_theoretical_range.ems.topology.has_valid_face_edge_connectivity is not True

0 commit comments

Comments
 (0)