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
8 changes: 8 additions & 0 deletions harmonica/_io/oasis_montaj_grd.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def load_oasis_montaj_grid(fname):
order = "F"
shape = (header["shape_e"], header["shape_v"])
spacing = (header["spacing_e"], header["spacing_v"])
# Check that the number of elements matches the expected shape
expected_size = shape[0] * shape[1]
actual_size = grid.size
if actual_size != expected_size:
raise ValueError(
f"Grid data size mismatch: found {actual_size} elements, expected {expected_size} (shape {shape}). "
"The file may be corrupted or incomplete."
)
grid = grid.reshape(shape, order=order)
# Build coords
if header["rotation"] == 0:
Expand Down
Binary file added harmonica/tests/data/incomplete_grid.grd
Binary file not shown.
6 changes: 6 additions & 0 deletions harmonica/tests/test_oasis.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,9 @@ def test_rotated_grid(self):
npt.assert_allclose(
grid.rotation, np.degrees(np.arctan((north - south) / (east - west)))
)


def test_incomplete_grid_raises_error():
corrupted_file = "harmonica/tests/data/incomplete_grid.grd"
with pytest.raises(ValueError, match="Grid data size mismatch"):
load_oasis_montaj_grid(corrupted_file)
Loading