Skip to content

Commit f52722f

Browse files
authored
Merge branch 'main' into AliasSystem/panel
2 parents ee00133 + e490616 commit f52722f

File tree

5 files changed

+85
-108
lines changed

5 files changed

+85
-108
lines changed

pygmt/src/grdclip.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66

77
import xarray as xr
88
from pygmt._typing import PathLike
9+
from pygmt.alias import Alias, AliasSystem
910
from pygmt.clib import Session
1011
from pygmt.exceptions import GMTInvalidInput
1112
from pygmt.helpers import (
1213
build_arg_list,
1314
deprecate_parameter,
1415
fmt_docstring,
1516
kwargs_to_strings,
16-
sequence_join,
1717
use_alias,
1818
)
1919

@@ -23,9 +23,7 @@
2323
# TODO(PyGMT>=0.19.0): Remove the deprecated "new" parameter.
2424
@fmt_docstring
2525
@deprecate_parameter("new", "replace", "v0.15.0", remove_version="v0.19.0")
26-
@use_alias(
27-
R="region", Sa="above-", Sb="below-", Si="between-", Sr="replace-", V="verbose"
28-
)
26+
@use_alias(R="region", V="verbose")
2927
@kwargs_to_strings(R="sequence")
3028
def grdclip(
3129
grid: PathLike | xr.DataArray,
@@ -54,6 +52,10 @@ def grdclip(
5452
Full GMT docs at :gmt-docs:`grdclip.html`.
5553
5654
{aliases}
55+
- Sa=above
56+
- Sb=below
57+
- Si=between
58+
- Sr=replace
5759
5860
Parameters
5961
----------
@@ -113,19 +115,20 @@ def grdclip(
113115
)
114116
raise GMTInvalidInput(msg)
115117

116-
# Parse the -S option.
117-
kwargs["Sa"] = sequence_join(above, size=2, name="above")
118-
kwargs["Sb"] = sequence_join(below, size=2, name="below")
119-
kwargs["Si"] = sequence_join(between, size=3, ndim=2, name="between")
120-
kwargs["Sr"] = sequence_join(replace, size=2, ndim=2, name="replace")
118+
aliasdict = AliasSystem(
119+
Sa=Alias(above, name="above", sep="/", size=2),
120+
Sb=Alias(below, name="below", sep="/", size=2),
121+
Si=Alias(between, name="between", sep="/", size=3, ndim=2),
122+
Sr=Alias(replace, name="replace", sep="/", size=2, ndim=2),
123+
).merge(kwargs)
121124

122125
with Session() as lib:
123126
with (
124127
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
125128
lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd,
126129
):
127-
kwargs["G"] = voutgrd
130+
aliasdict["G"] = voutgrd
128131
lib.call_module(
129-
module="grdclip", args=build_arg_list(kwargs, infile=vingrd)
132+
module="grdclip", args=build_arg_list(aliasdict, infile=vingrd)
130133
)
131134
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)

pygmt/src/grdfill.py

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import numpy as np
88
import xarray as xr
99
from pygmt._typing import PathLike
10+
from pygmt.alias import Alias, AliasSystem
1011
from pygmt.clib import Session
1112
from pygmt.exceptions import GMTInvalidInput
1213
from pygmt.helpers import (
@@ -71,52 +72,11 @@ def _validate_params(
7172
raise GMTInvalidInput(msg)
7273

7374

74-
def _parse_fill_mode(
75-
constantfill=None, gridfill=None, neighborfill=None, splinefill=None
76-
) -> str | None:
77-
"""
78-
Parse the fill parameters and return the appropriate string for the -A option.
79-
80-
>>> import numpy as np
81-
>>> import xarray as xr
82-
>>> _parse_fill_mode(constantfill=20.0)
83-
'c20.0'
84-
>>> _parse_fill_mode(gridfill="bggrid.nc")
85-
'g'
86-
>>> _parse_fill_mode(gridfill=xr.DataArray(np.zeros((10, 10))))
87-
'g'
88-
>>> _parse_fill_mode(neighborfill=20)
89-
'n20'
90-
>>> _parse_fill_mode(neighborfill=True)
91-
'n'
92-
>>> _parse_fill_mode(splinefill=0.5)
93-
's0.5'
94-
>>> _parse_fill_mode(splinefill=True)
95-
's'
96-
"""
97-
if constantfill is not None:
98-
return f"c{constantfill}"
99-
if gridfill is not None:
100-
return "g" # Append grid file name later to support xarray.DataArray.
101-
if neighborfill is not None and neighborfill is not False:
102-
return "n" if neighborfill is True else f"n{neighborfill}"
103-
if splinefill is not None and splinefill is not False:
104-
return "s" if splinefill is True else f"s{splinefill}"
105-
return None
106-
107-
10875
@fmt_docstring
10976
# TODO(PyGMT>=0.19.0): Remove the deprecated 'no_data' parameter.
11077
# TODO(PyGMT>=0.19.0): Remove the deprecated 'mode' parameter.
11178
@deprecate_parameter("no_data", "hole", "v0.15.0", remove_version="v0.19.0")
112-
@use_alias(
113-
A="constantfill/gridfill/neighborfill/splinefill/mode-",
114-
L="inquire-",
115-
N="hole",
116-
R="region",
117-
V="verbose",
118-
f="coltypes",
119-
)
79+
@use_alias(N="hole", R="region", V="verbose", f="coltypes")
12080
@kwargs_to_strings(R="sequence")
12181
def grdfill(
12282
grid: PathLike | xr.DataArray,
@@ -141,6 +101,11 @@ def grdfill(
141101
Full GMT docs at :gmt-docs:`grdfill.html`.
142102
143103
{aliases}
104+
- Ac=constantfill
105+
- Ag=gridfill
106+
- An=neighborfill
107+
- As=splinefill
108+
- L=inquire
144109
145110
Parameters
146111
----------
@@ -209,21 +174,24 @@ def grdfill(
209174
# Validate the fill/inquire parameters.
210175
_validate_params(constantfill, gridfill, neighborfill, splinefill, inquire, mode)
211176

212-
# Parse the fill parameters and return the appropriate string for the -A option.
213-
kwargs["A"] = (
214-
_parse_fill_mode(constantfill, gridfill, neighborfill, splinefill)
215-
if mode is None
216-
else mode
217-
)
177+
# _validate_params has already ensured that only one of the parameters is set.
178+
aliasdict = AliasSystem(
179+
A=Alias(mode, name="mode"),
180+
Ac=Alias(constantfill, name="constantfill"),
181+
# For grdfill, append the actual or virtual grid file name later.
182+
Ag=Alias(gridfill is not None, name="gridfill"),
183+
An=Alias(neighborfill, name="neighborfill"),
184+
As=Alias(splinefill, name="splinefill"),
185+
L=Alias(inquire, name="inquire"),
186+
).merge(kwargs)
218187

219188
with Session() as lib:
220189
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
221190
if inquire: # Inquire mode.
222-
kwargs["L"] = True
223191
with lib.virtualfile_out(kind="dataset") as vouttbl:
224192
lib.call_module(
225193
module="grdfill",
226-
args=build_arg_list(kwargs, infile=vingrd, outfile=vouttbl),
194+
args=build_arg_list(aliasdict, infile=vingrd, outfile=vouttbl),
227195
)
228196
return lib.virtualfile_to_dataset(
229197
vfname=vouttbl, output_type="numpy"
@@ -238,9 +206,9 @@ def grdfill(
238206
):
239207
if gridfill is not None:
240208
# Fill by a grid. Append the actual or virtual grid file name.
241-
kwargs["A"] = f"g{vbggrd}"
242-
kwargs["G"] = voutgrd
209+
aliasdict["Ag"] = vbggrd
210+
aliasdict["G"] = voutgrd
243211
lib.call_module(
244-
module="grdfill", args=build_arg_list(kwargs, infile=vingrd)
212+
module="grdfill", args=build_arg_list(aliasdict, infile=vingrd)
245213
)
246214
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)

pygmt/src/grdlandmask.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
import xarray as xr
99
from pygmt._typing import PathLike
10+
from pygmt.alias import Alias, AliasSystem
1011
from pygmt.clib import Session
1112
from pygmt.exceptions import GMTInvalidInput
1213
from pygmt.helpers import (
1314
build_arg_list,
1415
fmt_docstring,
1516
kwargs_to_strings,
16-
sequence_join,
1717
use_alias,
1818
)
1919
from pygmt.src._common import _parse_coastline_resolution
@@ -25,9 +25,7 @@
2525
@use_alias(
2626
A="area_thresh",
2727
D="resolution-",
28-
E="bordervalues-",
2928
I="spacing",
30-
N="maskvalues-",
3129
R="region",
3230
V="verbose",
3331
r="registration",
@@ -55,6 +53,8 @@ def grdlandmask(
5553
Full GMT docs at :gmt-docs:`grdlandmask.html`.
5654
5755
{aliases}
56+
- E=bordervalues
57+
- N=maskvalues
5858
5959
Parameters
6060
----------
@@ -119,11 +119,14 @@ def grdlandmask(
119119
raise GMTInvalidInput(msg)
120120

121121
kwargs["D"] = kwargs.get("D", _parse_coastline_resolution(resolution))
122-
kwargs["N"] = sequence_join(maskvalues, size=(2, 5), name="maskvalues")
123-
kwargs["E"] = sequence_join(bordervalues, size=(1, 4), name="bordervalues")
122+
123+
aliasdict = AliasSystem(
124+
N=Alias(maskvalues, name="maskvalues", sep="/", size=(2, 5)),
125+
E=Alias(bordervalues, name="bordervalues", sep="/", size=4),
126+
).merge(kwargs)
124127

125128
with Session() as lib:
126129
with lib.virtualfile_out(kind="grid", fname=outgrid) as voutgrd:
127-
kwargs["G"] = voutgrd
128-
lib.call_module(module="grdlandmask", args=build_arg_list(kwargs))
130+
aliasdict["G"] = voutgrd
131+
lib.call_module(module="grdlandmask", args=build_arg_list(aliasdict))
129132
return lib.virtualfile_to_raster(vfname=voutgrd, outgrid=outgrid)

pygmt/src/ternary.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
B="frame",
1616
C="cmap",
1717
G="fill",
18-
L="alabel/blabel/clabel-",
1918
JX="width",
2019
R="region",
2120
S="style",
@@ -48,6 +47,7 @@ def ternary(
4847
Full GMT docs at :gmt-docs:`ternary.html`.
4948
5049
{aliases}
50+
- L=alabel/blabel/clabel
5151
- c=panel
5252
5353
Parameters
@@ -87,19 +87,19 @@ def ternary(
8787
self._activate_figure()
8888

8989
# -Lalabel/blabel/clabel. '-' means skipping the label.
90-
labels = (alabel, blabel, clabel)
91-
if any(v is not None for v in labels):
92-
kwargs["L"] = "/".join(str(v) if v is not None else "-" for v in labels)
90+
_labels = [v if v is not None else "-" for v in (alabel, blabel, clabel)]
91+
labels = _labels if any(v != "-" for v in _labels) else None
92+
93+
aliasdict = AliasSystem(
94+
L=Alias(labels, name="alabel/blabel/clabel", sep="/", size=3),
95+
c=Alias(panel, name="panel", sep=",", size=2),
96+
).merge(kwargs)
9397

9498
# TODO(GMT>=6.5.0): Remove the patch for upstream bug fixed in GMT 6.5.0.
9599
# See https://github.com/GenericMappingTools/pygmt/pull/2138
96100
if Version(__gmt_version__) < Version("6.5.0") and isinstance(data, pd.DataFrame):
97101
data = data.to_numpy()
98102

99-
aliasdict = AliasSystem(
100-
c=Alias(panel, name="panel", sep=",", size=2),
101-
).merge(kwargs)
102-
103103
with Session() as lib:
104104
with lib.virtualfile_in(check_kind="vector", data=data) as vintbl:
105105
lib.call_module(

pygmt/src/timestamp.py

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
from packaging.version import Version
99
from pygmt._typing import AnchorCode
10+
from pygmt.alias import Alias, AliasSystem
1011
from pygmt.clib import Session, __gmt_version__
11-
from pygmt.helpers import build_arg_list, kwargs_to_strings
12+
from pygmt.helpers import build_arg_list
1213

1314
__doctest_skip__ = ["timestamp"]
1415

1516

16-
@kwargs_to_strings(offset="sequence")
1717
def timestamp(
1818
self,
1919
text: str | None = None,
@@ -78,39 +78,42 @@ def timestamp(
7878
"""
7979
self._activate_figure()
8080

81-
# Build the options passed to the "plot" module
82-
kwdict: dict = {"T": True, "U": ""}
83-
if label is not None:
84-
kwdict["U"] += f"{label}"
85-
kwdict["U"] += f"+j{justify}"
81+
if text is not None and len(str(text)) > 64:
82+
msg = (
83+
"Parameter 'text' expects a string no longer than 64 characters. "
84+
"The given text string will be truncated to 64 characters."
85+
)
86+
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
87+
text = str(text)[:64]
8688

87-
# TODO(GMT>=6.5.0): Remove the patch for upstream bug fixed in GMT 6.5.0.
88-
if Version(__gmt_version__) < Version("6.5.0") and "/" not in str(offset):
89-
# Giving a single offset doesn't work in GMT < 6.5.0.
90-
# See https://github.com/GenericMappingTools/gmt/issues/7107.
91-
offset = f"{offset}/{offset}"
92-
kwdict["U"] += f"+o{offset}"
89+
# TODO(GMT>=6.5.0): Remove the patch for upstream "offset" bug fixed in GMT 6.5.0.
90+
# TODO(GMT>=6.5.0): Remove the workaround for the '+t' modifier added in GMT 6.5.0.
91+
# Related issues:
92+
# - https://github.com/GenericMappingTools/gmt/issues/7107
93+
# - https://github.com/GenericMappingTools/gmt/pull/7127
94+
if Version(__gmt_version__) < Version("6.5.0"):
95+
if "/" not in str(offset): # Giving a single offset doesn't work in GMT<6.5.0
96+
offset = f"{offset}/{offset}"
97+
if text is not None:
98+
# Workaround for GMT<6.5.0 by overriding the 'timefmt' parameter and
99+
# unsetting 'text'.
100+
timefmt = str(text)
101+
text = None
93102

94-
# The +t modifier was added in GMT 6.5.0.
95-
# See https://github.com/GenericMappingTools/gmt/pull/7127.
96-
if text is not None:
97-
if len(str(text)) > 64:
98-
msg = (
99-
"Argument of 'text' must be no longer than 64 characters. "
100-
"The given text string will be truncated to 64 characters."
101-
)
102-
warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2)
103-
# TODO(GMT>=6.5.0): Remove the workaround for the new '+t' modifier.
104-
if Version(__gmt_version__) < Version("6.5.0"):
105-
# Workaround for GMT<6.5.0 by overriding the 'timefmt' parameter
106-
timefmt = text[:64]
107-
else:
108-
kwdict["U"] += f"+t{text}"
103+
aliasdict = AliasSystem(
104+
U=[
105+
Alias(label, name="label"),
106+
Alias(justify, name="justify", prefix="+j"),
107+
Alias(offset, name="offset", prefix="+o", sep="/", size=2),
108+
Alias(text, name="text", prefix="+t"),
109+
]
110+
)
111+
aliasdict["T"] = "" # Add '-T' to the "plot" module.
109112

110113
with Session() as lib:
111114
lib.call_module(
112115
module="plot",
113116
args=build_arg_list(
114-
kwdict, confdict={"FONT_LOGO": font, "FORMAT_TIME_STAMP": timefmt}
117+
aliasdict, confdict={"FONT_LOGO": font, "FORMAT_TIME_STAMP": timefmt}
115118
),
116119
)

0 commit comments

Comments
 (0)