Skip to content

Commit 2021de7

Browse files
authored
Figure.coast/pygmt.grdlandmask/pygmt.select: Refactor to use the new alias system for parameter 'resolution' (#4013)
* Figure.coast: Refactor using the new alias system * pygmt.grdlandmask: Refactor using the new alias system * pygmt.select: Refactor using the new alias system * Remove the unused _parse_coastline_resolution function * Remove the test_coast_resolution_short_form test
1 parent e490616 commit 2021de7

File tree

5 files changed

+39
-83
lines changed

5 files changed

+39
-83
lines changed

pygmt/src/_common.py

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -244,55 +244,3 @@ def from_params(
244244
if set(param_list).issubset(set(params)):
245245
return cls(convention, component=component)
246246
raise GMTValueError(params, description="focal mechanism parameters")
247-
248-
249-
def _parse_coastline_resolution(
250-
resolution: Literal["auto", "full", "high", "intermediate", "low", "crude", None],
251-
) -> Literal["a", "f", "h", "i", "l", "c", None]:
252-
"""
253-
Parse the 'resolution' parameter for coastline-related functions.
254-
255-
Parameters
256-
----------
257-
resolution
258-
The resolution of the coastline dataset to use. The available resolutions from
259-
highest to lowest are: ``"full"``, ``"high"``, ``"intermediate"``, ``"low"``,
260-
and ``"crude"``, which drops by 80% between levels. Alternatively, choose
261-
``"auto"`` to automatically select the most suitable resolution given the chosen
262-
map scale or region. ``None`` means using the default resolution.
263-
264-
Returns
265-
-------
266-
The single-letter resolution code or ``None``.
267-
268-
Raises
269-
------
270-
GMTValueError
271-
If the resolution is invalid.
272-
273-
Examples
274-
--------
275-
>>> _parse_coastline_resolution("full")
276-
'f'
277-
>>> _parse_coastline_resolution("f")
278-
'f'
279-
>>> _parse_coastline_resolution(None)
280-
>>> _parse_coastline_resolution("invalid")
281-
Traceback (most recent call last):
282-
...
283-
pygmt...GMTValueError: Invalid .... Expected ....
284-
"""
285-
if resolution is None:
286-
return None
287-
288-
_valid_res = {"auto", "full", "high", "intermediate", "low", "crude"}
289-
290-
if resolution in _valid_res: # Long-form arguments.
291-
return resolution[0] # type: ignore[return-value]
292-
293-
if resolution in {_res[0] for _res in _valid_res}: # Short-form arguments.
294-
return resolution # type: ignore[return-value]
295-
296-
raise GMTValueError(
297-
resolution, choices=_valid_res, description="GSHHG coastline resolution"
298-
)

pygmt/src/coast.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
kwargs_to_strings,
1515
use_alias,
1616
)
17-
from pygmt.src._common import _parse_coastline_resolution
1817

1918
__doctest_skip__ = ["coast"]
2019

@@ -24,7 +23,6 @@
2423
A="area_thresh",
2524
B="frame",
2625
C="lakes",
27-
D="resolution-",
2826
E="dcw",
2927
F="box",
3028
G="land",
@@ -68,6 +66,7 @@ def coast(
6866
Full GMT docs at :gmt-docs:`coast.html`.
6967
7068
{aliases}
69+
- D=resolution
7170
- J=projection
7271
7372
Parameters
@@ -214,9 +213,19 @@ def coast(
214213
)
215214
raise GMTInvalidInput(msg)
216215

217-
kwargs["D"] = kwargs.get("D", _parse_coastline_resolution(resolution))
218-
219216
aliasdict = AliasSystem(
217+
D=Alias(
218+
resolution,
219+
name="resolution",
220+
mapping={
221+
"auto": "a",
222+
"full": "f",
223+
"high": "h",
224+
"intermediate": "i",
225+
"low": "l",
226+
"crude": "c",
227+
},
228+
),
220229
J=Alias(projection, name="projection"),
221230
).merge(kwargs)
222231

pygmt/src/grdlandmask.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
kwargs_to_strings,
1717
use_alias,
1818
)
19-
from pygmt.src._common import _parse_coastline_resolution
2019

2120
__doctest_skip__ = ["grdlandmask"]
2221

2322

2423
@fmt_docstring
2524
@use_alias(
2625
A="area_thresh",
27-
D="resolution-",
2826
I="spacing",
2927
R="region",
3028
V="verbose",
@@ -53,6 +51,7 @@ def grdlandmask(
5351
Full GMT docs at :gmt-docs:`grdlandmask.html`.
5452
5553
{aliases}
54+
- D=resolution
5655
- E=bordervalues
5756
- N=maskvalues
5857
@@ -118,9 +117,19 @@ def grdlandmask(
118117
msg = "Both 'region' and 'spacing' must be specified."
119118
raise GMTInvalidInput(msg)
120119

121-
kwargs["D"] = kwargs.get("D", _parse_coastline_resolution(resolution))
122-
123120
aliasdict = AliasSystem(
121+
D=Alias(
122+
resolution,
123+
name="resolution",
124+
mapping={
125+
"auto": "a",
126+
"full": "f",
127+
"high": "h",
128+
"intermediate": "i",
129+
"low": "l",
130+
"crude": "c",
131+
},
132+
),
124133
N=Alias(maskvalues, name="maskvalues", sep="/", size=(2, 5)),
125134
E=Alias(bordervalues, name="bordervalues", sep="/", size=4),
126135
).merge(kwargs)

pygmt/src/select.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use_alias,
1717
validate_output_table_type,
1818
)
19-
from pygmt.src._common import _parse_coastline_resolution
2019

2120
__doctest_skip__ = ["select"]
2221

@@ -78,6 +77,7 @@ def select(
7877
Full GMT docs at :gmt-docs:`gmtselect.html`.
7978
8079
{aliases}
80+
- D=resolution
8181
- J=projection
8282
8383
Parameters
@@ -211,15 +211,25 @@ def select(
211211
>>> # longitudes 246 and 247 and latitudes 20 and 21
212212
>>> out = pygmt.select(data=ship_data, region=[246, 247, 20, 21])
213213
"""
214-
kwargs["D"] = kwargs.get("D", _parse_coastline_resolution(resolution))
215-
216214
output_type = validate_output_table_type(output_type, outfile=outfile)
217215

218216
column_names = None
219217
if output_type == "pandas" and isinstance(data, pd.DataFrame):
220218
column_names = data.columns.to_list()
221219

222220
aliasdict = AliasSystem(
221+
D=Alias(
222+
resolution,
223+
name="resolution",
224+
mapping={
225+
"auto": "a",
226+
"full": "f",
227+
"high": "h",
228+
"intermediate": "i",
229+
"low": "l",
230+
"crude": "c",
231+
},
232+
),
223233
J=Alias(projection, name="projection"),
224234
).merge(kwargs)
225235

pygmt/tests/test_coast.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,6 @@ def test_coast_dcw_list():
7676
return fig
7777

7878

79-
@pytest.mark.mpl_image_compare(filename="test_coast_world_mercator.png")
80-
def test_coast_resolution_short_form():
81-
"""
82-
Test using the short form of the 'resolution' parameter.
83-
84-
This test is the same as test_coast_world_mercator, but uses the short form of
85-
the 'resolution' parameter.
86-
"""
87-
fig = Figure()
88-
fig.coast(
89-
region=[-180, 180, -80, 80],
90-
projection="M15c",
91-
frame="af",
92-
land="#aaaaaa",
93-
D="crude",
94-
water="white",
95-
)
96-
return fig
97-
98-
9979
def test_coast_resolution_long_short_form_conflict():
10080
"""
10181
Test that using the short form of the 'resolution' parameter conflicts with

0 commit comments

Comments
 (0)