From d85bfdfc212ae5acf36921acdfd92c139288f30a Mon Sep 17 00:00:00 2001 From: Will Schlitzer Date: Sat, 12 Mar 2022 06:33:11 +0000 Subject: [PATCH 1/2] Add inline example for grdtrack (#1725) Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com> Co-authored-by: Dongdong Tian Co-authored-by: Meghan Jones --- pygmt/src/grdtrack.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/pygmt/src/grdtrack.py b/pygmt/src/grdtrack.py index 6e0e6d91431..1f15161b99c 100644 --- a/pygmt/src/grdtrack.py +++ b/pygmt/src/grdtrack.py @@ -12,6 +12,8 @@ use_alias, ) +__doctest_skip__ = ["grdtrack"] + @fmt_docstring @use_alias( @@ -264,6 +266,22 @@ def grdtrack(points, grid, newcolname=None, outfile=None, **kwargs): ``outfile`` is not set - None if ``outfile`` is set (track output will be stored in file set by ``outfile``) + + Example + ------- + >>> import pygmt + >>> # Load a grid of @earth_relief_30m data, with an x-range of -118 to + >>> # -107, and a y-range of -49 to -42 + >>> grid = pygmt.datasets.load_earth_relief( + ... resolution="30m", region=[-118, -107, -49, -42] + ... ) + >>> # Load a pandas dataframe with ocean ridge points + >>> points = pygmt.datasets.load_sample_data(name="ocean_ridge_points") + >>> # Create a pandas dataframe from an input grid and set of points + >>> # The output dataframe adds a column named "bathymetry" + >>> output_dataframe = pygmt.grdtrack( + ... points=points, grid=grid, newcolname="bathymetry" + ... ) """ if hasattr(points, "columns") and newcolname is None: raise GMTInvalidInput("Please pass in a str to 'newcolname'") From 5734902d1fbcd378a472d6cce8d570121f207391 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Sat, 12 Mar 2022 22:23:05 +0800 Subject: [PATCH 2/2] Fix the spacing parameter and check required parameters in xyz2grd (#1804) * Fix the spacing parameter and check required parameters in xyz2grd * Add a test --- pygmt/src/xyz2grd.py | 6 +++++- pygmt/tests/test_xyz2grd.py | 13 +++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pygmt/src/xyz2grd.py b/pygmt/src/xyz2grd.py index 634585a0e75..54324adf13a 100644 --- a/pygmt/src/xyz2grd.py +++ b/pygmt/src/xyz2grd.py @@ -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, @@ -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. @@ -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( diff --git a/pygmt/tests/test_xyz2grd.py b/pygmt/tests/test_xyz2grd.py index 4e141de1fb0..8414616a0a6 100644 --- a/pygmt/tests/test_xyz2grd.py +++ b/pygmt/tests/test_xyz2grd.py @@ -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 @@ -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)