diff --git a/pygmt/clib/conversion.py b/pygmt/clib/conversion.py index 24c6c4470f3..57b2f92cc28 100644 --- a/pygmt/clib/conversion.py +++ b/pygmt/clib/conversion.py @@ -82,7 +82,7 @@ def dataarray_to_matrix(grid): """ if len(grid.dims) != 2: raise GMTInvalidInput( - "Invalid number of grid dimensions '{}'. Must be 2.".format(len(grid.dims)) + f"Invalid number of grid dimensions '{len(grid.dims)}'. Must be 2." ) # Extract region and inc from the grid region = [] @@ -96,9 +96,11 @@ def dataarray_to_matrix(grid): coord_inc = coord_incs[0] if not np.allclose(coord_incs, coord_inc): raise GMTInvalidInput( - "Grid appears to have irregular spacing in the '{}' dimension.".format( - dim - ) + f"Grid appears to have irregular spacing in the '{dim}' dimension." + ) + if coord_inc == 0: + raise GMTInvalidInput( + f"Grid has a zero increment in the '{dim}' dimension." ) region.extend( [ diff --git a/pygmt/tests/test_clib.py b/pygmt/tests/test_clib.py index 4f2b8f4a980..f31a28fc7a9 100644 --- a/pygmt/tests/test_clib.py +++ b/pygmt/tests/test_clib.py @@ -743,6 +743,24 @@ def test_dataarray_to_matrix_inc_fails(): dataarray_to_matrix(grid) +def test_dataarray_to_matrix_zero_inc_fails(): + """ + Check that dataarray_to_matrix fails for zero increments grid. + """ + data = np.ones((5, 5), dtype="float32") + x = np.linspace(0, 1, 5) + y = np.zeros_like(x) + grid = xr.DataArray(data, coords=[("y", y), ("x", x)]) + with pytest.raises(GMTInvalidInput): + dataarray_to_matrix(grid) + + y = np.linspace(0, 1, 5) + x = np.zeros_like(x) + grid = xr.DataArray(data, coords=[("y", y), ("x", x)]) + with pytest.raises(GMTInvalidInput): + dataarray_to_matrix(grid) + + def test_get_default(): """ Make sure get_default works without crashing and gives reasonable results.