Skip to content

Commit 177048a

Browse files
committed
Add private function _validate_params to simplify main codes
1 parent cc13f00 commit 177048a

File tree

1 file changed

+60
-27
lines changed

1 file changed

+60
-27
lines changed

pygmt/src/grdfill.py

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,57 @@
1919
__doctest_skip__ = ["grdfill"]
2020

2121

22+
def _validate_params(
23+
constantfill=None,
24+
gridfill=None,
25+
neighborfill=None,
26+
splinefill=None,
27+
inquire=False,
28+
mode=None,
29+
):
30+
"""
31+
Validate the fill/inquire parameters.
32+
33+
>>> _validate_params(constantfill=20.0)
34+
>>> _validate_params(inquire=True)
35+
>>> _validate_params(mode="c20.0")
36+
>>> _validate_params(constantfill=20.0, gridfill="bggrid.nc")
37+
Traceback (most recent call last):
38+
...
39+
pygmt.exceptions.GMTInvalidInput: Parameters ... are mutually exclusive.
40+
>>> _validate_params(constantfill=20.0, inquire=True)
41+
Traceback (most recent call last):
42+
...
43+
pygmt.exceptions.GMTInvalidInput: Parameters ... are mutually exclusive.
44+
>>> _validate_params()
45+
Traceback (most recent call last):
46+
...
47+
pygmt.exceptions.GMTInvalidInput: Need to specify parameter ...
48+
"""
49+
_fill_params = "'constantfill'/'gridfill'/'neighborfill'/'splinefill'"
50+
# The deprecated 'mode' parameter is given.
51+
if mode is not None:
52+
msg = (
53+
"The 'mode' parameter is deprecated since v0.15.0 and will be removed in "
54+
f"v0.19.0. Use {_fill_params} instead."
55+
)
56+
warnings.warn(msg, FutureWarning, stacklevel=2)
57+
58+
n_given = sum(
59+
param is not None and param is not False
60+
for param in [constantfill, gridfill, neighborfill, splinefill, inquire, mode]
61+
)
62+
if n_given > 1: # More than one mutually exclusive parameters are given.
63+
msg = f"Parameters {_fill_params}/'inquire'/'mode' are mutually exclusive."
64+
raise GMTInvalidInput(msg)
65+
if n_given == 0: # No parameters are given.
66+
msg = (
67+
"Need to specify parameter {_fill_params} for filling holes or "
68+
"'inquire' for inquiring the bounds of each hole."
69+
)
70+
raise GMTInvalidInput(msg)
71+
72+
2273
def _parse_fill_mode(
2374
constantfill=None, gridfill=None, neighborfill=None, splinefill=None
2475
) -> str | None:
@@ -41,19 +92,7 @@ def _parse_fill_mode(
4192
's0.5'
4293
>>> _parse_fill_mode(splinefill=True)
4394
's'
44-
>>> _parse_fill_mode(constantfill=20, gridfill="bggrid.nc")
45-
Traceback (most recent call last):
46-
...
47-
pygmt.exceptions.GMTInvalidInput: The ... parameters are mutually exclusive.
4895
"""
49-
fill_params = [constantfill, gridfill, neighborfill, splinefill]
50-
if sum(param is not None for param in fill_params) > 1:
51-
msg = (
52-
"The 'constantfill', 'gridfill', 'neighborfill', and 'splinefill' "
53-
"parameters are mutually exclusive."
54-
)
55-
raise GMTInvalidInput(msg)
56-
5796
if constantfill is not None:
5897
return f"c{constantfill}"
5998
if gridfill is not None:
@@ -157,21 +196,15 @@ def grdfill(
157196
array([[1.83333333, 6.16666667, 3.83333333, 8.16666667],
158197
[6.16666667, 7.83333333, 0.5 , 2.5 ]])
159198
"""
160-
if mode is not None: # The deprecated 'mode' parameter is given.
161-
warnings.warn(
162-
"The 'mode' parameter is deprecated since v0.15.0 and will be removed in "
163-
"v0.19.0. Use 'constantfill'/'gridfill'/'neighborfill'/'splinefill' "
164-
"instead.",
165-
FutureWarning,
166-
stacklevel=1,
167-
)
168-
kwargs["A"] = mode
169-
else:
170-
kwargs["A"] = _parse_fill_mode(constantfill, gridfill, neighborfill, splinefill)
171-
172-
if kwargs.get("A") is None and inquire is False:
173-
msg = "At least parameter 'mode' or 'inquire' must be specified."
174-
raise GMTInvalidInput(msg)
199+
# Validate the fill/inquire parameters.
200+
_validate_params(constantfill, gridfill, neighborfill, splinefill, inquire, mode)
201+
202+
# Parse the fill parameters and return the appropriate string for the -A option.
203+
kwargs["A"] = (
204+
_parse_fill_mode(constantfill, gridfill, neighborfill, splinefill)
205+
if mode is None
206+
else mode
207+
)
175208

176209
with Session() as lib:
177210
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:

0 commit comments

Comments
 (0)