Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pygmt/src/grdsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pygmt._typing import PathLike
from pygmt.alias import Alias, AliasSystem
from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import (
build_arg_list,
deprecate_parameter,
Expand Down Expand Up @@ -70,6 +71,7 @@ def grdsample(
Toggle between grid and pixel registration; if the input is grid-registered, the
output will be pixel-registered and vice-versa. This is a *destructive* grid
change; see :gmt-docs:`reference/options.html#switch-registrations`.
*Note**: ``toggle`` and ``registration`` are mutually exclusive.
{verbose}
{coltypes}
{interpolation}
Expand Down Expand Up @@ -97,6 +99,15 @@ def grdsample(
>>> # and set both x- and y-spacings to 0.5 arc-degrees
>>> new_grid = pygmt.grdsample(grid=grid, toggle=True, spacing=[0.5, 0.5])
"""
# Enforce mutual exclusivity between -T (toggle) and -r (registration)
if kwargs.get("T", toggle) and (
"r" in kwargs
or "registration" in kwargs
or registration in ("gridline", "pixel")
):
msg = "Parameters 'toggle' and 'registration' cannot be used together."
raise GMTInvalidInput(msg)

aliasdict = AliasSystem(
T=Alias(toggle, name="toggle"),
).add_common(
Expand Down
9 changes: 9 additions & 0 deletions pygmt/tests/test_grdsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import xarray as xr
from pygmt import grdsample
from pygmt.enums import GridRegistration, GridType
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import GMTTempFile
from pygmt.helpers.testing import load_static_earth_relief

Expand Down Expand Up @@ -94,3 +95,11 @@ def test_grdsample_registration_changes(grid):
assert translated_grid.gmt.registration is GridRegistration.GRIDLINE
registration_grid = grdsample(grid=translated_grid, registration="pixel")
assert registration_grid.gmt.registration is GridRegistration.PIXEL


def test_grdsample_toggle_and_registration_mutually_exclusive(grid):
"""
Raise an exception if toggle and registration are both set.
"""
with pytest.raises(GMTInvalidInput):
grdsample(grid=grid, toggle=True, registration="pixel")
Loading