Skip to content

Commit

Permalink
Added additional validation for face edge connectivity. Also added un…
Browse files Browse the repository at this point in the history
…it tests.
  • Loading branch information
david-sh-csiro committed Dec 16, 2024
1 parent 0bbb470 commit 0cf3c89
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/emsarray/conventions/ugrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,31 @@ def has_valid_face_edge_connectivity(self) -> bool:
)
return False

try:
fill_value = data_array.encoding['_FillValue']
except KeyError:
return True

lower_bound = _get_start_index(data_array)
theoretical_upper_bound = self.face_count * self.max_node_count
actual_upper_bound = numpy.nanmax(data_array)

if lower_bound < fill_value < actual_upper_bound:
warnings.warn(
f"Got a face_edge_connectivity variable {data_array.name!r} with "
f"a _FillValue inside the actual index range",
ConventionViolationWarning,
)
return False

if lower_bound < fill_value < theoretical_upper_bound:
warnings.warn(
f"Got a face_edge_connectivity variable {data_array.name!r} with "
f"a _FillValue inside the theoretical index range",
ConventionViolationWarning,
)
return False

return True

@cached_property
Expand Down
37 changes: 37 additions & 0 deletions tests/conventions/test_ugrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,3 +924,40 @@ def da(attrs: dict) -> xarray.DataArray:

with pytest.raises(ConventionViolationError):
_get_start_index(da({'start_index': 2}))


def test_has_valid_face_edge_connectivity():
# Create dataset with face_edges
dataset = make_dataset(width=3, make_edges=True, make_face_coordinates=True)
topology = dataset.ems.topology
topology.mesh_variable.attrs.update({
'face_edge_connectivity': 'Mesh2_face_edges',
})

mesh2_face_edges_array = topology.face_edge_array

mesh2_face_edges = xarray.DataArray(
mesh2_face_edges_array,
dims=[topology.face_dimension, topology.max_node_dimension],
)

dataset = dataset.assign({
'Mesh2_face_edges': mesh2_face_edges,
})

dataset_fill_value_in_actual_range = dataset.copy()

dataset_fill_value_in_theoretical_range = dataset.copy()

# Make sure original dataset is valid
assert dataset.ems.topology.has_valid_face_edge_connectivity is True

dataset_fill_value_in_actual_range['Mesh2_face_edges'].encoding['_FillValue'] = 2

with pytest.warns(ConventionViolationWarning):
assert dataset_fill_value_in_actual_range.ems.topology.has_valid_face_edge_connectivity is not True

dataset_fill_value_in_theoretical_range['Mesh2_face_edges'].encoding['_FillValue'] = 88

with pytest.warns(ConventionViolationWarning):
assert dataset_fill_value_in_theoretical_range.ems.topology.has_valid_face_edge_connectivity is not True

0 comments on commit 0cf3c89

Please sign in to comment.