Skip to content

Commit 73a8124

Browse files
authored
pygmt.grdfill: Use the new alias system for parameters constantfill/gridfill/neighborfill/splinefill/inquire/mode (#4035)
1 parent d36adf0 commit 73a8124

File tree

1 file changed

+21
-53
lines changed

1 file changed

+21
-53
lines changed

pygmt/src/grdfill.py

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import xarray as xr
99
from pygmt._typing import PathLike
10+
from pygmt.alias import Alias, AliasSystem
1011
from pygmt.clib import Session
1112
from pygmt.exceptions import GMTInvalidInput
1213
from pygmt.helpers import (
@@ -71,52 +72,11 @@ def _validate_params(
7172
raise GMTInvalidInput(msg)
7273

7374

74-
def _parse_fill_mode(
75-
constantfill=None, gridfill=None, neighborfill=None, splinefill=None
76-
) -> str | None:
77-
"""
78-
Parse the fill parameters and return the appropriate string for the -A option.
79-
80-
>>> import numpy as np
81-
>>> import xarray as xr
82-
>>> _parse_fill_mode(constantfill=20.0)
83-
'c20.0'
84-
>>> _parse_fill_mode(gridfill="bggrid.nc")
85-
'g'
86-
>>> _parse_fill_mode(gridfill=xr.DataArray(np.zeros((10, 10))))
87-
'g'
88-
>>> _parse_fill_mode(neighborfill=20)
89-
'n20'
90-
>>> _parse_fill_mode(neighborfill=True)
91-
'n'
92-
>>> _parse_fill_mode(splinefill=0.5)
93-
's0.5'
94-
>>> _parse_fill_mode(splinefill=True)
95-
's'
96-
"""
97-
if constantfill is not None:
98-
return f"c{constantfill}"
99-
if gridfill is not None:
100-
return "g" # Append grid file name later to support xarray.DataArray.
101-
if neighborfill is not None and neighborfill is not False:
102-
return "n" if neighborfill is True else f"n{neighborfill}"
103-
if splinefill is not None and splinefill is not False:
104-
return "s" if splinefill is True else f"s{splinefill}"
105-
return None
106-
107-
10875
@fmt_docstring
10976
# TODO(PyGMT>=0.19.0): Remove the deprecated 'no_data' parameter.
11077
# TODO(PyGMT>=0.19.0): Remove the deprecated 'mode' parameter.
11178
@deprecate_parameter("no_data", "hole", "v0.15.0", remove_version="v0.19.0")
112-
@use_alias(
113-
A="constantfill/gridfill/neighborfill/splinefill/mode-",
114-
L="inquire-",
115-
N="hole",
116-
R="region",
117-
V="verbose",
118-
f="coltypes",
119-
)
79+
@use_alias(N="hole", R="region", V="verbose", f="coltypes")
12080
@kwargs_to_strings(R="sequence")
12181
def grdfill(
12282
grid: PathLike | xr.DataArray,
@@ -141,6 +101,11 @@ def grdfill(
141101
Full GMT docs at :gmt-docs:`grdfill.html`.
142102
143103
{aliases}
104+
- Ac=constantfill
105+
- Ag=gridfill
106+
- An=neighborfill
107+
- As=splinefill
108+
- L=inquire
144109
145110
Parameters
146111
----------
@@ -209,21 +174,24 @@ def grdfill(
209174
# Validate the fill/inquire parameters.
210175
_validate_params(constantfill, gridfill, neighborfill, splinefill, inquire, mode)
211176

212-
# Parse the fill parameters and return the appropriate string for the -A option.
213-
kwargs["A"] = (
214-
_parse_fill_mode(constantfill, gridfill, neighborfill, splinefill)
215-
if mode is None
216-
else mode
217-
)
177+
# _validate_params has already ensured that only one of the parameters is set.
178+
aliasdict = AliasSystem(
179+
A=Alias(mode, name="mode"),
180+
Ac=Alias(constantfill, name="constantfill"),
181+
# For grdfill, append the actual or virtual grid file name later.
182+
Ag=Alias(gridfill is not None, name="gridfill"),
183+
An=Alias(neighborfill, name="neighborfill"),
184+
As=Alias(splinefill, name="splinefill"),
185+
L=Alias(inquire, name="inquire"),
186+
).merge(kwargs)
218187

219188
with Session() as lib:
220189
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
221190
if inquire: # Inquire mode.
222-
kwargs["L"] = True
223191
with lib.virtualfile_out(kind="dataset") as vouttbl:
224192
lib.call_module(
225193
module="grdfill",
226-
args=build_arg_list(kwargs, infile=vingrd, outfile=vouttbl),
194+
args=build_arg_list(aliasdict, infile=vingrd, outfile=vouttbl),
227195
)
228196
return lib.virtualfile_to_dataset(
229197
vfname=vouttbl, output_type="numpy"
@@ -238,9 +206,9 @@ def grdfill(
238206
):
239207
if gridfill is not None:
240208
# Fill by a grid. Append the actual or virtual grid file name.
241-
kwargs["A"] = f"g{vbggrd}"
242-
kwargs["G"] = voutgrd
209+
aliasdict["Ag"] = vbggrd
210+
aliasdict["G"] = voutgrd
243211
lib.call_module(
244-
module="grdfill", args=build_arg_list(kwargs, infile=vingrd)
212+
module="grdfill", args=build_arg_list(aliasdict, infile=vingrd)
245213
)
246214
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)

0 commit comments

Comments
 (0)