Skip to content

Commit 7bd5a8f

Browse files
seismanweiji14
andauthored
Raise RuntimeWarning instead of an exception for irregular grid spacing (#1530)
* Raise a warning intsead an exception for irregular grid spacing * Calcuate grid spacing if irregular spacing is detected * Update pygmt/tests/test_clib.py Co-authored-by: Wei Ji <[email protected]> Co-authored-by: Wei Ji <[email protected]>
1 parent 6aba9ae commit 7bd5a8f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

pygmt/clib/conversion.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
Functions to convert data types into ctypes friendly formats.
33
"""
4+
import warnings
5+
46
import numpy as np
57
import pandas as pd
68
from pygmt.exceptions import GMTInvalidInput
@@ -95,9 +97,14 @@ def dataarray_to_matrix(grid):
9597
coord_incs = coord[1:] - coord[0:-1]
9698
coord_inc = coord_incs[0]
9799
if not np.allclose(coord_incs, coord_inc):
98-
raise GMTInvalidInput(
99-
f"Grid appears to have irregular spacing in the '{dim}' dimension."
100+
# calculate the increment if irregular spacing is found
101+
coord_inc = (coord[-1] - coord[0]) / (coord.size - 1)
102+
msg = (
103+
f"Grid may have irregular spacing in the '{dim}' dimension, "
104+
"but GMT only supports regular spacing. Calculated regular spacing "
105+
f"{coord_inc} is assumed in the '{dim}' dimension."
100106
)
107+
warnings.warn(msg, category=RuntimeWarning)
101108
if coord_inc == 0:
102109
raise GMTInvalidInput(
103110
f"Grid has a zero increment in the '{dim}' dimension."

pygmt/tests/test_clib.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -805,16 +805,18 @@ def test_dataarray_to_matrix_dims_fails():
805805
dataarray_to_matrix(grid)
806806

807807

808-
def test_dataarray_to_matrix_inc_fails():
808+
def test_dataarray_to_matrix_irregular_inc_warning():
809809
"""
810-
Check that it fails for variable increments.
810+
Check that it warns for variable increments, see also
811+
https://github.com/GenericMappingTools/pygmt/issues/1468.
811812
"""
812813
data = np.ones((4, 5), dtype="float64")
813814
x = np.linspace(0, 1, 5)
814815
y = np.logspace(2, 3, 4)
815816
grid = xr.DataArray(data, coords=[("y", y), ("x", x)])
816-
with pytest.raises(GMTInvalidInput):
817+
with pytest.warns(expected_warning=RuntimeWarning) as record:
817818
dataarray_to_matrix(grid)
819+
assert len(record) == 1
818820

819821

820822
def test_dataarray_to_matrix_zero_inc_fails():

0 commit comments

Comments
 (0)