Skip to content
Merged
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 src/harmonica/_io/oasis_montaj_grd.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,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
if (expected_size := shape[0] * shape[1]) != grid.size:
msg = (
f"Grid data size mismatch: found {grid.size} elements, "
f"expected {expected_size} (shape {shape}). "
"The file may be corrupted or incomplete."
)
raise ValueError(msg)
grid = grid.reshape(shape, order=order)
# Build coords
if header["rotation"] == 0:
Expand Down
Binary file added test/data/incomplete_grid.grd
Binary file not shown.
7 changes: 7 additions & 0 deletions test/test_oasis.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,10 @@ def test_rotated_grid(self):
npt.assert_allclose(
grid.rotation, np.degrees(np.arctan((north - south) / (east - west)))
)


def test_incomplete_grid_raises_error():
"""Test error if grid size doesn't match the size in the header."""
corrupted_file = TEST_DATA_DIR / "incomplete_grid.grd"
with pytest.raises(ValueError, match="Grid data size mismatch"):
load_oasis_montaj_grid(corrupted_file)
Loading