Skip to content

Fix the spacing parameter and check required parameters in xyz2grd #1804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 12, 2022
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
6 changes: 5 additions & 1 deletion pygmt/src/xyz2grd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xyz2grd - Convert data table to a grid.
"""
from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import (
GMTTempFile,
build_arg_string,
Expand Down Expand Up @@ -30,7 +31,7 @@
r="registration",
w="wrap",
)
@kwargs_to_strings(R="sequence")
@kwargs_to_strings(I="sequence", R="sequence")
def xyz2grd(data=None, x=None, y=None, z=None, **kwargs):
r"""
Create a grid file from table data.
Expand Down Expand Up @@ -132,6 +133,9 @@ def xyz2grd(data=None, x=None, y=None, z=None, **kwargs):
- None if ``outgrid`` is set (grid output will be stored in file set by
``outgrid``)
"""
if "I" not in kwargs or "R" not in kwargs:
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")

with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_from_data(
Expand Down
13 changes: 13 additions & 0 deletions pygmt/tests/test_xyz2grd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import xarray as xr
from pygmt import load_dataarray, xyz2grd
from pygmt.datasets import load_sample_data
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import GMTTempFile


Expand Down Expand Up @@ -65,3 +66,15 @@ def test_xyz2grd_input_array_file_out(ship_data, expected_grid):
assert os.path.exists(path=tmpfile.name)
temp_grid = load_dataarray(tmpfile.name)
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)


def test_xyz2grd_missing_region_spacing(ship_data):
"""
Test xyz2grd raise an exception if region or spacing is missing.
"""
with pytest.raises(GMTInvalidInput):
xyz2grd(data=ship_data)
with pytest.raises(GMTInvalidInput):
xyz2grd(data=ship_data, region=[245, 255, 20, 30])
with pytest.raises(GMTInvalidInput):
xyz2grd(data=ship_data, spacing=5)