diff --git a/pygmt/helpers/decorators.py b/pygmt/helpers/decorators.py index 729234a4d66..bec2a06ec9e 100644 --- a/pygmt/helpers/decorators.py +++ b/pygmt/helpers/decorators.py @@ -8,6 +8,7 @@ import functools import textwrap import warnings +from inspect import Parameter, signature import numpy as np from pygmt.exceptions import GMTInvalidInput @@ -322,6 +323,30 @@ def fmt_docstring(module_func): return module_func +def _insert_alias(module_func, default_value=None): + """ + Function to insert PyGMT long aliases into the signature of a method. + """ + + # Get current signature and parameters + sig = signature(module_func) + wrapped_params = list(sig.parameters.values()) + kwargs_param = wrapped_params.pop(-1) + # Add new parameters from aliases + for alias in module_func.aliases.values(): + if alias not in sig.parameters.keys(): + new_param = Parameter( + alias, kind=Parameter.KEYWORD_ONLY, default=default_value + ) + wrapped_params = wrapped_params + [new_param] + all_params = wrapped_params + [kwargs_param] + # Update method signature + sig_new = sig.replace(parameters=all_params) + module_func.__signature__ = sig_new + + return module_func + + def use_alias(**aliases): """ Decorator to add aliases to keyword arguments of a function. @@ -383,6 +408,8 @@ def new_module(*args, **kwargs): new_module.aliases = aliases + new_module = _insert_alias(new_module) + return new_module return alias_decorator diff --git a/pygmt/src/grdfill.py b/pygmt/src/grdfill.py index a642f631bff..6b553b964fe 100644 --- a/pygmt/src/grdfill.py +++ b/pygmt/src/grdfill.py @@ -4,6 +4,7 @@ import xarray as xr from pygmt.clib import Session +from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( GMTTempFile, build_arg_string, @@ -18,6 +19,7 @@ A="mode", G="outgrid", R="region", + V="verbose", ) @kwargs_to_strings(R="sequence") def grdfill(grid, **kwargs): @@ -45,8 +47,12 @@ def grdfill(grid, **kwargs): constant fill and append the constant value, **n** for nearest neighbor (and optionally append a search radius in pixels [default radius is :math:`r^2 = \sqrt{{ X^2 + Y^2 }}`, - where (*X,Y*) are the node dimensions of the grid]). + where (*X,Y*) are the node dimensions of the grid]), or + **s** for bicubic spline (optionally append a *tension* + parameter [Default is no tension]). + {R} + {V} Returns ------- @@ -57,6 +63,8 @@ def grdfill(grid, **kwargs): - None if ``outgrid`` is set (grid output will be stored in file set by ``outgrid``) """ + if "A" not in kwargs.keys() and "L" not in kwargs.keys(): + raise GMTInvalidInput("At least parameter 'mode' or 'L' must be specified.") with GMTTempFile(suffix=".nc") as tmpfile: with Session() as lib: file_context = lib.virtualfile_from_data(check_kind="raster", data=grid) diff --git a/pygmt/src/plot.py b/pygmt/src/plot.py index 192a33c1fae..df87972074a 100644 --- a/pygmt/src/plot.py +++ b/pygmt/src/plot.py @@ -80,7 +80,7 @@ def plot(self, x=None, y=None, data=None, size=None, direction=None, **kwargs): data : str or {table-like} Pass in either a file name to an ASCII data table, a 2D {table-classes}. - Use parameter ``columns`` to choose which columns are x, y, color, and + Use parameter ``incols`` to choose which columns are x, y, color, and size, respectively. size : 1d array The size of the data points in units specified using ``style``. diff --git a/pygmt/tests/test_grdfill.py b/pygmt/tests/test_grdfill.py index edc19456e71..7dc66aad472 100644 --- a/pygmt/tests/test_grdfill.py +++ b/pygmt/tests/test_grdfill.py @@ -1,5 +1,5 @@ """ -Tests for grdclip. +Tests for grdfill. """ import os @@ -8,6 +8,7 @@ import xarray as xr from pygmt import grdfill, grdinfo from pygmt.datasets import load_earth_relief +from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import GMTTempFile @@ -47,3 +48,11 @@ def test_grdfill_file_out(grid): assert os.path.exists(path=tmpfile.name) # check that outgrid exists result = grdinfo(tmpfile.name, per_column=True).strip() assert result == "-5 5 -5 5 -5130.5 inf 1 1 10 10 1 1" + + +def test_grdfill_required_args(grid): + """ + Test that grdfill fails without arguments for `mode` and `L`. + """ + with pytest.raises(GMTInvalidInput): + grdfill(grid=grid)