Skip to content

Commit 5734902

Browse files
authored
Fix the spacing parameter and check required parameters in xyz2grd (#1804)
* Fix the spacing parameter and check required parameters in xyz2grd * Add a test
1 parent d85bfdf commit 5734902

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

pygmt/src/xyz2grd.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
xyz2grd - Convert data table to a grid.
33
"""
44
from pygmt.clib import Session
5+
from pygmt.exceptions import GMTInvalidInput
56
from pygmt.helpers import (
67
GMTTempFile,
78
build_arg_string,
@@ -30,7 +31,7 @@
3031
r="registration",
3132
w="wrap",
3233
)
33-
@kwargs_to_strings(R="sequence")
34+
@kwargs_to_strings(I="sequence", R="sequence")
3435
def xyz2grd(data=None, x=None, y=None, z=None, **kwargs):
3536
r"""
3637
Create a grid file from table data.
@@ -132,6 +133,9 @@ def xyz2grd(data=None, x=None, y=None, z=None, **kwargs):
132133
- None if ``outgrid`` is set (grid output will be stored in file set by
133134
``outgrid``)
134135
"""
136+
if "I" not in kwargs or "R" not in kwargs:
137+
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")
138+
135139
with GMTTempFile(suffix=".nc") as tmpfile:
136140
with Session() as lib:
137141
file_context = lib.virtualfile_from_data(

pygmt/tests/test_xyz2grd.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import xarray as xr
99
from pygmt import load_dataarray, xyz2grd
1010
from pygmt.datasets import load_sample_data
11+
from pygmt.exceptions import GMTInvalidInput
1112
from pygmt.helpers import GMTTempFile
1213

1314

@@ -65,3 +66,15 @@ def test_xyz2grd_input_array_file_out(ship_data, expected_grid):
6566
assert os.path.exists(path=tmpfile.name)
6667
temp_grid = load_dataarray(tmpfile.name)
6768
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
69+
70+
71+
def test_xyz2grd_missing_region_spacing(ship_data):
72+
"""
73+
Test xyz2grd raise an exception if region or spacing is missing.
74+
"""
75+
with pytest.raises(GMTInvalidInput):
76+
xyz2grd(data=ship_data)
77+
with pytest.raises(GMTInvalidInput):
78+
xyz2grd(data=ship_data, region=[245, 255, 20, 30])
79+
with pytest.raises(GMTInvalidInput):
80+
xyz2grd(data=ship_data, spacing=5)

0 commit comments

Comments
 (0)