Skip to content

Commit 5e1ebe8

Browse files
committed
Move the parser function into _common.py
1 parent 5fac3ae commit 5e1ebe8

File tree

4 files changed

+71
-13
lines changed

4 files changed

+71
-13
lines changed

pygmt/src/_common.py

+58-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
"""
44

55
from pathlib import Path
6-
from typing import Any
6+
from typing import Any, Literal
77

8+
from pygmt.exceptions import GMTInvalidInput
89
from pygmt.src.which import which
910

1011

@@ -39,3 +40,59 @@ def _data_geometry_is_point(data: Any, kind: str) -> bool:
3940
except FileNotFoundError:
4041
pass
4142
return False
43+
44+
45+
def _parse_coastline_resolution(
46+
resolution: Literal["auto", "full", "high", "intermediate", "low", "crude", None],
47+
allow_auto: bool = False,
48+
) -> str | None:
49+
"""
50+
Parse the resolution parameter for coastline-related functions.
51+
52+
Parameters
53+
----------
54+
resolution
55+
The resolution of the coastline dataset to use. The available resolutions from
56+
highest to lowest are: ``"full"``, ``"high"``, ``"intermediate"``, ``"low"``,
57+
and ``"crude"``, which drops by 80% between levels.
58+
allow_auto
59+
Whether to allow the ``"auto"`` resolution.
60+
61+
Returns
62+
-------
63+
str or None
64+
The parsed resolution value.
65+
66+
Raises
67+
------
68+
GMTInvalidInput
69+
If the resolution is invalid.
70+
71+
Examples
72+
--------
73+
>>> _parse_coastline_resolution("full")
74+
"f"
75+
>>> _parse_coastline_resolution("f")
76+
"f"
77+
>>> _parse_coastline_resolution("auto", allow_auto=True)
78+
"a"
79+
>>> _parse_coastline_resolution("invalid")
80+
pygmt.exceptions.GMTInvalidInput: Invalid resolution: invalid. Valid values are ...
81+
>>> _parse_coastline_resolution(None)
82+
None
83+
>>> _parse_coastline_resolution("auto")
84+
pygmt.exceptions.GMTInvalidInput: Invalid resolution: auto. Valid values are ...
85+
"""
86+
if resolution is None:
87+
return None
88+
89+
valid_resolutions = {"full", "high", "intermediate", "low", "crude"}
90+
if allow_auto:
91+
valid_resolutions.add("auto")
92+
if resolution not in {*valid_resolutions, *[res[0] for res in valid_resolutions]}:
93+
msg = (
94+
f"Invalid resolution: {resolution}."
95+
f"Valid values are {', '.join(valid_resolutions)}."
96+
)
97+
raise GMTInvalidInput(msg)
98+
return resolution[0]

pygmt/src/coast.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
kwargs_to_strings,
1414
use_alias,
1515
)
16+
from pygmt.src._common import _parse_coastline_resolution
1617

1718
__doctest_skip__ = ["coast"]
1819

@@ -41,8 +42,8 @@
4142
def coast(
4243
self,
4344
resolution: Literal[
44-
"auto", "full", "high", "intermediate", "low", "crude"
45-
] = "auto",
45+
"auto", "full", "high", "intermediate", "low", "crude", None
46+
] = None,
4647
**kwargs,
4748
):
4849
r"""
@@ -208,9 +209,8 @@ def coast(
208209
"""At least one of the following parameters must be specified:
209210
lakes, land, water, rivers, borders, dcw, Q, or shorelines"""
210211
)
211-
212-
# Alias 'resolution' to "D".
213-
kwargs["D"] = resolution[0]
214-
212+
kwargs["D"] = kwargs.get(
213+
"D", _parse_coastline_resolution(resolution, allow_auto=True)
214+
)
215215
with Session() as lib:
216216
lib.call_module(module="coast", args=build_arg_list(kwargs))

pygmt/src/grdlandmask.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pygmt.clib import Session
99
from pygmt.exceptions import GMTInvalidInput
1010
from pygmt.helpers import build_arg_list, fmt_docstring, kwargs_to_strings, use_alias
11+
from pygmt.src._common import _parse_coastline_resolution
1112

1213
__doctest_skip__ = ["grdlandmask"]
1314

@@ -26,7 +27,7 @@
2627
@kwargs_to_strings(I="sequence", R="sequence", N="sequence", E="sequence")
2728
def grdlandmask(
2829
outgrid: str | None = None,
29-
resolution: Literal["full", "high", "intermediate", "low", "crude"] = "low",
30+
resolution: Literal["full", "high", "intermediate", "low", "crude", None] = None,
3031
**kwargs,
3132
) -> xr.DataArray | None:
3233
r"""
@@ -99,8 +100,8 @@ def grdlandmask(
99100
if kwargs.get("I") is None or kwargs.get("R") is None:
100101
raise GMTInvalidInput("Both 'region' and 'spacing' must be specified.")
101102

102-
# Alias "resolution" to "D"
103-
kwargs["D"] = resolution[0]
103+
kwargs["D"] = kwargs.get("D", _parse_coastline_resolution(resolution))
104+
104105
with Session() as lib:
105106
with lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd:
106107
kwargs["G"] = voutgrd

pygmt/src/select.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use_alias,
1515
validate_output_table_type,
1616
)
17+
from pygmt.src._common import _parse_coastline_resolution
1718

1819
__doctest_skip__ = ["select"]
1920

@@ -47,7 +48,7 @@ def select(
4748
data=None,
4849
output_type: Literal["pandas", "numpy", "file"] = "pandas",
4950
outfile: str | None = None,
50-
resolution: Literal["full", "high", "intermediate", "low", "crude"] = "low",
51+
resolution: Literal["full", "high", "intermediate", "low", "crude", None] = None,
5152
**kwargs,
5253
) -> pd.DataFrame | np.ndarray | None:
5354
r"""
@@ -202,8 +203,7 @@ def select(
202203
>>> # longitudes 246 and 247 and latitudes 20 and 21
203204
>>> out = pygmt.select(data=ship_data, region=[246, 247, 20, 21])
204205
"""
205-
# Alias "resolution" to "D"
206-
kwargs["D"] = resolution[0]
206+
kwargs["D"] = kwargs.get("D", _parse_coastline_resolution(resolution))
207207

208208
output_type = validate_output_table_type(output_type, outfile=outfile)
209209

0 commit comments

Comments
 (0)