Skip to content

WIP: Figure.coast:/pygmt.grdlandmask/pygmt.select: Refactor using the new alias system #4013

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: AliasSystem/aliassystem
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 0 additions & 52 deletions pygmt/src/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,55 +244,3 @@ def from_params(
if set(param_list).issubset(set(params)):
return cls(convention, component=component)
raise GMTValueError(params, description="focal mechanism parameters")


def _parse_coastline_resolution(
resolution: Literal["auto", "full", "high", "intermediate", "low", "crude", None],
) -> Literal["a", "f", "h", "i", "l", "c", None]:
"""
Parse the 'resolution' parameter for coastline-related functions.

Parameters
----------
resolution
The resolution of the coastline dataset to use. The available resolutions from
highest to lowest are: ``"full"``, ``"high"``, ``"intermediate"``, ``"low"``,
and ``"crude"``, which drops by 80% between levels. Alternatively, choose
``"auto"`` to automatically select the most suitable resolution given the chosen
map scale or region. ``None`` means using the default resolution.

Returns
-------
The single-letter resolution code or ``None``.

Raises
------
GMTValueError
If the resolution is invalid.

Examples
--------
>>> _parse_coastline_resolution("full")
'f'
>>> _parse_coastline_resolution("f")
'f'
>>> _parse_coastline_resolution(None)
>>> _parse_coastline_resolution("invalid")
Traceback (most recent call last):
...
pygmt...GMTValueError: Invalid .... Expected ....
"""
if resolution is None:
return None

_valid_res = {"auto", "full", "high", "intermediate", "low", "crude"}

if resolution in _valid_res: # Long-form arguments.
return resolution[0] # type: ignore[return-value]

if resolution in {_res[0] for _res in _valid_res}: # Short-form arguments.
return resolution # type: ignore[return-value]

raise GMTValueError(
resolution, choices=_valid_res, description="GSHHG coastline resolution"
)
21 changes: 17 additions & 4 deletions pygmt/src/coast.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from typing import Literal

from pygmt.alias import Alias, AliasSystem
from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import (
Expand All @@ -13,7 +14,6 @@
kwargs_to_strings,
use_alias,
)
from pygmt.src._common import _parse_coastline_resolution

__doctest_skip__ = ["coast"]

Expand All @@ -23,7 +23,6 @@
A="area_thresh",
B="frame",
C="lakes",
D="resolution-",
E="dcw",
F="box",
G="land",
Expand Down Expand Up @@ -67,6 +66,7 @@ def coast(
Full GMT docs at :gmt-docs:`coast.html`.

{aliases}
- D=resolution

Parameters
----------
Expand Down Expand Up @@ -212,7 +212,20 @@ def coast(
)
raise GMTInvalidInput(msg)

kwargs["D"] = kwargs.get("D", _parse_coastline_resolution(resolution))
aliasdict = AliasSystem(
D=Alias(
resolution,
name="resolution",
mapping={
"auto": "a",
"full": "f",
"high": "h",
"intermediate": "i",
"low": "l",
"crude": "c",
},
),
).merge(kwargs)

with Session() as lib:
lib.call_module(module="coast", args=build_arg_list(kwargs))
lib.call_module(module="coast", args=build_arg_list(aliasdict))
28 changes: 21 additions & 7 deletions pygmt/src/grdlandmask.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import xarray as xr
from pygmt._typing import PathLike
from pygmt.alias import Alias, AliasSystem
from pygmt.clib import Session
from pygmt.exceptions import GMTInvalidInput
from pygmt.helpers import (
Expand All @@ -16,15 +17,13 @@
sequence_join,
use_alias,
)
from pygmt.src._common import _parse_coastline_resolution

__doctest_skip__ = ["grdlandmask"]


@fmt_docstring
@use_alias(
A="area_thresh",
D="resolution-",
E="bordervalues-",
I="spacing",
N="maskvalues-",
Expand Down Expand Up @@ -55,6 +54,7 @@ def grdlandmask(
Full GMT docs at :gmt-docs:`grdlandmask.html`.

{aliases}
- D=resolution

Parameters
----------
Expand Down Expand Up @@ -118,12 +118,26 @@ def grdlandmask(
msg = "Both 'region' and 'spacing' must be specified."
raise GMTInvalidInput(msg)

kwargs["D"] = kwargs.get("D", _parse_coastline_resolution(resolution))
kwargs["N"] = sequence_join(maskvalues, size=(2, 5), name="maskvalues")
kwargs["E"] = sequence_join(bordervalues, size=(1, 4), name="bordervalues")
aliasdict = AliasSystem(
D=Alias(
resolution,
name="resolution",
mapping={
"auto": "a",
"full": "f",
"high": "h",
"intermediate": "i",
"low": "l",
"crude": "c",
},
),
).merge(kwargs)

aliasdict["N"] = sequence_join(maskvalues, size=(2, 5), name="maskvalues")
aliasdict["E"] = sequence_join(bordervalues, size=(1, 4), name="bordervalues")

with Session() as lib:
with lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd:
kwargs["G"] = voutgrd
lib.call_module(module="grdlandmask", args=build_arg_list(kwargs))
aliasdict["G"] = voutgrd
lib.call_module(module="grdlandmask", args=build_arg_list(aliasdict))
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)
20 changes: 17 additions & 3 deletions pygmt/src/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import numpy as np
import pandas as pd
from pygmt._typing import PathLike, TableLike
from pygmt.alias import Alias, AliasSystem
from pygmt.clib import Session
from pygmt.helpers import (
build_arg_list,
Expand All @@ -15,7 +16,6 @@
use_alias,
validate_output_table_type,
)
from pygmt.src._common import _parse_coastline_resolution

__doctest_skip__ = ["select"]

Expand Down Expand Up @@ -77,6 +77,7 @@ def select(
Full GMT docs at :gmt-docs:`gmtselect.html`.

{aliases}
- D=resolution

Parameters
----------
Expand Down Expand Up @@ -209,7 +210,20 @@ def select(
>>> # longitudes 246 and 247 and latitudes 20 and 21
>>> out = pygmt.select(data=ship_data, region=[246, 247, 20, 21])
"""
kwargs["D"] = kwargs.get("D", _parse_coastline_resolution(resolution))
aliasdict = AliasSystem(
D=Alias(
resolution,
name="resolution",
mapping={
"auto": "a",
"full": "f",
"high": "h",
"intermediate": "i",
"low": "l",
"crude": "c",
},
),
).merge(kwargs)

output_type = validate_output_table_type(output_type, outfile=outfile)

Expand All @@ -224,7 +238,7 @@ def select(
):
lib.call_module(
module="select",
args=build_arg_list(kwargs, infile=vintbl, outfile=vouttbl),
args=build_arg_list(aliasdict, infile=vintbl, outfile=vouttbl),
)
return lib.virtualfile_to_dataset(
vfname=vouttbl,
Expand Down
20 changes: 0 additions & 20 deletions pygmt/tests/test_coast.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,6 @@ def test_coast_dcw_list():
return fig


@pytest.mark.mpl_image_compare(filename="test_coast_world_mercator.png")
def test_coast_resolution_short_form():
"""
Test using the short form of the 'resolution' parameter.

This test is the same as test_coast_world_mercator, but uses the short form of
the 'resolution' parameter.
"""
fig = Figure()
fig.coast(
region=[-180, 180, -80, 80],
projection="M15c",
frame="af",
land="#aaaaaa",
D="crude",
water="white",
)
return fig


def test_coast_resolution_long_short_form_conflict():
"""
Test that using the short form of the 'resolution' parameter conflicts with
Expand Down
Loading