|
3 | 3 | """
|
4 | 4 |
|
5 | 5 | from pathlib import Path
|
6 |
| -from typing import Any |
| 6 | +from typing import Any, Literal |
7 | 7 |
|
| 8 | +from pygmt.exceptions import GMTInvalidInput |
8 | 9 | from pygmt.src.which import which
|
9 | 10 |
|
10 | 11 |
|
@@ -39,3 +40,59 @@ def _data_geometry_is_point(data: Any, kind: str) -> bool:
|
39 | 40 | except FileNotFoundError:
|
40 | 41 | pass
|
41 | 42 | 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] |
0 commit comments