Skip to content

Commit 55662f6

Browse files
committed
Change the function definition to 'grid, points=None' in an compatible way
1 parent 1080ace commit 55662f6

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

pygmt/src/grdtrack.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
"""
22
grdtrack - Sample grids at specified (x,y) locations.
33
"""
4+
import warnings
5+
46
import pandas as pd
7+
import xarray as xr
58
from pygmt.clib import Session
69
from pygmt.exceptions import GMTInvalidInput
710
from pygmt.helpers import (
@@ -11,6 +14,7 @@
1114
kwargs_to_strings,
1215
use_alias,
1316
)
17+
from pygmt.src.which import which
1418

1519
__doctest_skip__ = ["grdtrack"]
1620

@@ -43,7 +47,7 @@
4347
w="wrap",
4448
)
4549
@kwargs_to_strings(R="sequence", S="sequence", i="sequence_comma", o="sequence_comma")
46-
def grdtrack(points=None, grid=None, newcolname=None, outfile=None, **kwargs):
50+
def grdtrack(grid, points=None, newcolname=None, outfile=None, **kwargs):
4751
r"""
4852
Sample grids at specified (x,y) locations.
4953
@@ -67,14 +71,14 @@ def grdtrack(points=None, grid=None, newcolname=None, outfile=None, **kwargs):
6771
6872
Parameters
6973
----------
70-
points : str or {table-like}
71-
Pass in either a file name to an ASCII data table, a 2D
72-
{table-classes}.
73-
7474
grid : xarray.DataArray or str
7575
Gridded array from which to sample values from, or a filename (netcdf
7676
format).
7777
78+
points : str or {table-like}
79+
Pass in either a file name to an ASCII data table, a 2D
80+
{table-classes}.
81+
7882
newcolname : str
7983
Required if ``points`` is a :class:`pandas.DataFrame`. The name for the
8084
new column in the track :class:`pandas.DataFrame` table where the
@@ -292,6 +296,29 @@ def grdtrack(points=None, grid=None, newcolname=None, outfile=None, **kwargs):
292296
if points is not None and kwargs.get("E") is not None:
293297
raise GMTInvalidInput("Can't set both 'points' and 'profile'.")
294298

299+
# Backward compatibility with old parameter order "points, grid".
300+
# deprecated_version="0.7.0", remove_version="v0.9.0"
301+
is_a_grid = True
302+
if not isinstance(grid, (xr.DataArray, xr.Dataset, str)):
303+
is_a_grid = False
304+
elif isinstance(grid, str):
305+
try:
306+
xr.open_dataarray(which(grid, download="a"))
307+
is_a_grid = True
308+
except ValueError:
309+
is_a_grid = False
310+
if not is_a_grid:
311+
msg = (
312+
"Positional parameters 'points, grid' of pygmt.grdtrack() is changed "
313+
"to 'grid, points=None' since v0.7.0. It's likely that you're NOT "
314+
"passing an valid grid as the first positional argument or "
315+
"passing an invalid grid to the 'grid' parameter. "
316+
"Please check the order of arguments with the latest documentation. "
317+
"The warning will be removed in v0.9.0."
318+
)
319+
grid, points = points, grid
320+
warnings.warn(msg, category=FutureWarning, stacklevel=1)
321+
295322
with GMTTempFile(suffix=".csv") as tmpfile:
296323
with Session() as lib:
297324
# Store the xarray.DataArray grid in virtualfile

pygmt/tests/test_grdtrack.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,18 @@ def test_grdtrack_set_points_and_profile(dataarray, dataframe):
176176
"""
177177
with pytest.raises(GMTInvalidInput):
178178
grdtrack(grid=dataarray, points=dataframe, profile="BL/TR")
179+
180+
181+
def test_grdtrack_old_parameter_order(dataframe, dataarray, expected_array):
182+
"""
183+
Run grdtrack with the old parameter order 'points, grid'.
184+
185+
This test should be removed in v0.9.0.
186+
"""
187+
for points in (POINTS_DATA, dataframe):
188+
for grid in ("@static_earth_relief.nc", dataarray):
189+
with pytest.warns(expected_warning=FutureWarning) as record:
190+
output = grdtrack(points, grid)
191+
assert len(record) == 1
192+
assert isinstance(output, pd.DataFrame)
193+
npt.assert_allclose(np.array(output), expected_array)

0 commit comments

Comments
 (0)