Skip to content

Commit 8e4cc96

Browse files
authored
pygmt.grdclip: Deprecate parameter 'new' to 'replace' (remove in v0.19.0) (#3884)
1 parent a11fc15 commit 8e4cc96

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

pygmt/src/grdclip.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@
44

55
import xarray as xr
66
from pygmt.clib import Session
7-
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
7+
from pygmt.helpers import (
8+
build_arg_list,
9+
deprecate_parameter,
10+
fmt_docstring,
11+
kwargs_to_strings,
12+
use_alias,
13+
)
814

915
__doctest_skip__ = ["grdclip"]
1016

1117

18+
# TODO(PyGMT>=0.19.0): Remove the deprecated "new" parameter.
1219
@fmt_docstring
20+
@deprecate_parameter("new", "replace", "v0.15.0", remove_version="v0.19.0")
1321
@use_alias(
1422
R="region",
1523
Sa="above",
1624
Sb="below",
1725
Si="between",
18-
Sr="new",
26+
Sr="replace",
1927
V="verbose",
2028
)
2129
@kwargs_to_strings(
@@ -55,7 +63,7 @@ def grdclip(grid, outgrid: str | None = None, **kwargs) -> xr.DataArray | None:
5563
between : str or list
5664
[*low*, *high*, *between*].
5765
Set all data[i] >= *low* and <= *high* to *between*.
58-
new : str or list
66+
replace : str or list
5967
[*old*, *new*].
6068
Set all data[i] == *old* to *new*. This is mostly useful when
6169
your data are known to be integer values.

pygmt/tests/test_grdclip.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
from pathlib import Path
66

7+
import numpy as np
8+
import numpy.testing as npt
79
import pytest
810
import xarray as xr
911
from pygmt import grdclip, load_dataarray
12+
from pygmt.datasets import load_earth_mask
1013
from pygmt.enums import GridRegistration, GridType
1114
from pygmt.helpers import GMTTempFile
1215
from pygmt.helpers.testing import load_static_earth_relief
@@ -69,3 +72,19 @@ def test_grdclip_no_outgrid(grid, expected_grid):
6972
assert temp_grid.gmt.gtype == GridType.GEOGRAPHIC
7073
assert temp_grid.gmt.registration == GridRegistration.PIXEL
7174
xr.testing.assert_allclose(a=temp_grid, b=expected_grid)
75+
76+
77+
def test_grdclip_replace():
78+
"""
79+
Test the replace parameter for grdclip.
80+
"""
81+
grid = load_earth_mask(region=[0, 10, 0, 10])
82+
npt.assert_array_equal(np.unique(grid), [0, 1]) # Only have 0 and 1
83+
grid = grdclip(grid=grid, replace=[0, 2]) # Replace 0 with 2
84+
npt.assert_array_equal(np.unique(grid), [1, 2])
85+
86+
# Test for the deprecated 'new' parameter
87+
# TODO(PyGMT>=0.19.0): Remove this test below for the 'new' parameter
88+
with pytest.warns(FutureWarning):
89+
grid = grdclip(grid=grid, new=[1, 3]) # Replace 1 with 3
90+
npt.assert_array_equal(np.unique(grid), [2, 3])

0 commit comments

Comments
 (0)