Skip to content

Commit 4770396

Browse files
committed
Merge branch 'main' into arg_with_space
2 parents 6c399ba + 5734902 commit 4770396

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

pygmt/src/grdtrack.py

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use_alias,
1313
)
1414

15+
__doctest_skip__ = ["grdtrack"]
16+
1517

1618
@fmt_docstring
1719
@use_alias(
@@ -264,6 +266,22 @@ def grdtrack(points, grid, newcolname=None, outfile=None, **kwargs):
264266
``outfile`` is not set
265267
- None if ``outfile`` is set (track output will be stored in file set
266268
by ``outfile``)
269+
270+
Example
271+
-------
272+
>>> import pygmt
273+
>>> # Load a grid of @earth_relief_30m data, with an x-range of -118 to
274+
>>> # -107, and a y-range of -49 to -42
275+
>>> grid = pygmt.datasets.load_earth_relief(
276+
... resolution="30m", region=[-118, -107, -49, -42]
277+
... )
278+
>>> # Load a pandas dataframe with ocean ridge points
279+
>>> points = pygmt.datasets.load_sample_data(name="ocean_ridge_points")
280+
>>> # Create a pandas dataframe from an input grid and set of points
281+
>>> # The output dataframe adds a column named "bathymetry"
282+
>>> output_dataframe = pygmt.grdtrack(
283+
... points=points, grid=grid, newcolname="bathymetry"
284+
... )
267285
"""
268286
if hasattr(points, "columns") and newcolname is None:
269287
raise GMTInvalidInput("Please pass in a str to 'newcolname'")

pygmt/src/xyz2grd.py

+5-1
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

+13
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)