Skip to content

Commit 5ce1971

Browse files
Chuan1937seisman
andauthored
pygmt.grdsample: Add check and test for exclusive parameters 'translate' and 'registration' (#4183)
Co-authored-by: Dongdong Tian <seisman.info@gmail.com>
1 parent 1cb8eeb commit 5ce1971

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

pygmt/src/grdsample.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pygmt._typing import PathLike
1010
from pygmt.alias import Alias, AliasSystem
1111
from pygmt.clib import Session
12+
from pygmt.exceptions import GMTInvalidInput
1213
from pygmt.helpers import (
1314
build_arg_list,
1415
deprecate_parameter,
@@ -70,6 +71,7 @@ def grdsample(
7071
Toggle between grid and pixel registration; if the input is grid-registered, the
7172
output will be pixel-registered and vice-versa. This is a *destructive* grid
7273
change; see :gmt-docs:`reference/options.html#switch-registrations`.
74+
*Note**: ``toggle`` and ``registration`` are mutually exclusive.
7375
{verbose}
7476
{coltypes}
7577
{interpolation}
@@ -97,6 +99,11 @@ def grdsample(
9799
>>> # and set both x- and y-spacings to 0.5 arc-degrees
98100
>>> new_grid = pygmt.grdsample(grid=grid, toggle=True, spacing=[0.5, 0.5])
99101
"""
102+
# Enforce mutual exclusivity between -T (toggle) and -r (registration)
103+
if kwargs.get("T", toggle) and kwargs.get("r", registration):
104+
msg = "Parameters 'toggle' and 'registration' cannot be used together."
105+
raise GMTInvalidInput(msg)
106+
100107
aliasdict = AliasSystem(
101108
T=Alias(toggle, name="toggle"),
102109
).add_common(

pygmt/tests/test_grdsample.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import xarray as xr
99
from pygmt import grdsample
1010
from pygmt.enums import GridRegistration, GridType
11+
from pygmt.exceptions import GMTInvalidInput
1112
from pygmt.helpers import GMTTempFile
1213
from pygmt.helpers.testing import load_static_earth_relief
1314

@@ -94,3 +95,11 @@ def test_grdsample_registration_changes(grid):
9495
assert translated_grid.gmt.registration is GridRegistration.GRIDLINE
9596
registration_grid = grdsample(grid=translated_grid, registration="pixel")
9697
assert registration_grid.gmt.registration is GridRegistration.PIXEL
98+
99+
100+
def test_grdsample_toggle_and_registration_mutually_exclusive(grid):
101+
"""
102+
Raise an exception if toggle and registration are both set.
103+
"""
104+
with pytest.raises(GMTInvalidInput):
105+
grdsample(grid=grid, toggle=True, registration="pixel")

0 commit comments

Comments
 (0)