Add grid size check for corrupted .grd files (#451)
Summary
Add a simple check in load_oasis_montaj_grid to raise a clear ValueError if the number of grid elements does not match the expected shape. This helps users quickly see if a .grd file is corrupted or incomplete.
What changed
- Added an explicit element count check before
.reshape(). - Raises a clear error instead of NumPy’s reshape error.
- Manually tested by creating a fake
.grdwith missing elements.
Closes #451.
Hi @Nanakjoth. Thanks for taking the time to work on this and opening the PR!
I like the error message you added. Before we merge this PR I would like to add some test that checks that the error is being raised when a corrupted file has been passed. We could add the sample corrupted file you used to manually test the function to harmonica/tests/data, name it something like incomplete_grid.grd, and add a test function in harmonica/tests/test_oasis.py that tries to load the file and checks your error was raised. We can use pytest.raises to run the check.
Would you like to work on that test?
Hi @santisoler, Absolutely, I would love to do it.
I actually thought about adding it at the same time as the fix but didn’t get around to it, so thanks for suggesting, that helps a lot.
Will update the PR shortly! 🚀