Skip to content

Commit 20f2c78

Browse files
committed
Update text
1 parent ad5400f commit 20f2c78

File tree

5 files changed

+58
-62
lines changed

5 files changed

+58
-62
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ test_no_images: _runtest
6161

6262
format:
6363
docformatter --in-place $(FORMAT_FILES)
64-
ruff check --fix $(FORMAT_FILES)
6564
ruff format $(FORMAT_FILES)
65+
ruff check --fix $(FORMAT_FILES)
6666

6767
check:
6868
docformatter --check $(FORMAT_FILES)

pygmt/alias.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,14 @@ def convert_aliases():
3030
options = defaultdict(lambda: "")
3131
for alias in p_locals.get("_aliases"):
3232
value = params.get(alias.name)
33-
if value in (None, False): # None or False are skipped
33+
if is_nonstr_iter(value):
34+
if alias.separator != "":
35+
value = sequence_to_str(value, alias.separator)
36+
else:
37+
value = ""
38+
elif value in (None, False): # None or False are skipped
3439
continue
35-
if value is True: # Convert True to an empty string
40+
elif value is True: # Convert True to an empty string
3641
value = ""
37-
if alias.separator is not None and is_nonstr_iter(value):
38-
value = sequence_to_str(value, alias.separator)
3942
options[alias.flag] += f"{alias.modifier}{value}"
4043
return dict(options)

pygmt/src/legend.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def legend(self, spec=None, position="JTR+jTR+o0.2c", box="+gwhite+p1p", **kwarg
6161
Alias("verbose", "v", "", ""),
6262
Alias("panel", "c", "", ","),
6363
Alias("perspective", "p", "", "/"),
64-
Alias("transparency", "t", "", "")
64+
Alias("transparency", "t", "", ""),
6565
]
6666

6767
kwargs = self._preprocess(**kwargs)
@@ -74,4 +74,6 @@ def legend(self, spec=None, position="JTR+jTR+o0.2c", box="+gwhite+p1p", **kwarg
7474
specfile = spec
7575
else:
7676
raise GMTInvalidInput(f"Unrecognized data type: {type(spec)}")
77-
lib.call_module(module="legend", args=build_arg_string(options, infile=specfile))
77+
lib.call_module(
78+
module="legend", args=build_arg_string(options, infile=specfile)
79+
)

pygmt/src/text.py

Lines changed: 45 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
text - Plot text on a figure.
33
"""
44
import numpy as np
5+
from pygmt.alias import Alias, convert_aliases
56
from pygmt.clib import Session
67
from pygmt.exceptions import GMTInvalidInput
78
from pygmt.helpers import (
@@ -11,38 +12,12 @@
1112
is_nonstr_iter,
1213
kwargs_to_strings,
1314
non_ascii_to_octal,
14-
use_alias,
1515
)
1616

1717

1818
@fmt_docstring
19-
@use_alias(
20-
R="region",
21-
J="projection",
22-
B="frame",
23-
C="clearance",
24-
D="offset",
25-
G="fill",
26-
N="no_clip",
27-
V="verbose",
28-
W="pen",
29-
a="aspatial",
30-
c="panel",
31-
e="find",
32-
f="coltypes",
33-
h="header",
34-
it="use_word",
35-
p="perspective",
36-
t="transparency",
37-
w="wrap",
38-
)
39-
@kwargs_to_strings(
40-
R="sequence",
41-
textfiles="sequence_space",
42-
c="sequence_comma",
43-
p="sequence",
44-
)
45-
def text_( # noqa: PLR0912
19+
@kwargs_to_strings(textfiles="sequence_space")
20+
def text_(
4621
self,
4722
textfiles=None,
4823
x=None,
@@ -52,6 +27,9 @@ def text_( # noqa: PLR0912
5227
angle=None,
5328
font=None,
5429
justify=None,
30+
projection=None,
31+
region=None,
32+
transparency=None,
5533
**kwargs,
5634
):
5735
r"""
@@ -71,8 +49,6 @@ def text_( # noqa: PLR0912
7149
7250
Full option list at :gmt-docs:`text.html`
7351
74-
{aliases}
75-
7652
Parameters
7753
----------
7854
textfiles : str or list
@@ -182,6 +158,31 @@ def text_( # noqa: PLR0912
182158
"""
183159
kwargs = self._preprocess(**kwargs)
184160

161+
_aliases = [
162+
Alias("position", "F", "+c", ""),
163+
Alias("angle", "F", "+a", ""),
164+
Alias("font", "F", "+f", ""),
165+
Alias("justify", "F", "+j", ""),
166+
Alias("region", "R", "", "/"),
167+
Alias("projection", "J", "", ""),
168+
Alias("transparency", "t", "", ""),
169+
Alias("frame", "B", "", ""),
170+
Alias("clearance", "C", "", ""),
171+
Alias("offset", "D", "", ""),
172+
Alias("fill", "G", "", ""),
173+
Alias("no_clip", "N", "", ""),
174+
Alias("verbose", "V", "", ""),
175+
Alias("pen", "W", "", ""),
176+
Alias("aspatial", "a", "", ""),
177+
Alias("panel", "c", "", ","),
178+
Alias("find", "e", "", ""),
179+
Alias("coltypes", "f", "", ""),
180+
Alias("header", "h", "", ""),
181+
Alias("use_word", "it", "", ""),
182+
Alias("perspective", "p", "", "/"),
183+
Alias("wrap", "w", "", ""),
184+
]
185+
185186
# Ensure inputs are either textfiles, x/y/text, or position/text
186187
if position is None:
187188
if (x is not None or y is not None) and textfiles is not None:
@@ -201,33 +202,22 @@ def text_( # noqa: PLR0912
201202
kind = None
202203
textfiles = ""
203204

204-
# Build the -F option in gmt text.
205-
if kwargs.get("F") is None and any(
206-
v is not None for v in (position, angle, font, justify)
207-
):
208-
kwargs.update({"F": ""})
205+
# special handling with the position parameter
206+
if position is not None:
207+
position += f"+t{text}"
209208

210209
extra_arrays = []
211-
for arg, flag in [(angle, "+a"), (font, "+f"), (justify, "+j")]:
212-
if arg is True:
213-
kwargs["F"] += flag
214-
elif is_nonstr_iter(arg):
215-
kwargs["F"] += flag
216-
if flag == "+a": # angle is numeric type
217-
extra_arrays.append(np.atleast_1d(arg))
218-
else: # font or justify is str type
219-
extra_arrays.append(np.atleast_1d(arg).astype(str))
220-
elif isinstance(arg, (int, float, str)):
221-
kwargs["F"] += f"{flag}{arg}"
222-
223-
if isinstance(position, str):
224-
kwargs["F"] += f"+c{position}+t{text}"
225-
210+
# angle is numeric type
211+
if is_nonstr_iter(angle):
212+
extra_arrays.append(np.atleast_1d(angle))
213+
# font or justify is str type
214+
for arg in (font, justify):
215+
if is_nonstr_iter(arg):
216+
extra_arrays.append(np.atleast_1d(arg).astype(str))
226217
# If an array of transparency is given, GMT will read it from
227218
# the last numerical column per data record.
228-
if is_nonstr_iter(kwargs.get("t")):
229-
extra_arrays.append(kwargs["t"])
230-
kwargs["t"] = ""
219+
if is_nonstr_iter(transparency):
220+
extra_arrays.append(transparency)
231221

232222
# Append text at last column. Text must be passed in as str type.
233223
if kind == "vectors":
@@ -239,5 +229,6 @@ def text_( # noqa: PLR0912
239229
file_context = lib.virtualfile_from_data(
240230
check_kind="vector", data=textfiles, x=x, y=y, extra_arrays=extra_arrays
241231
)
232+
options = convert_aliases()
242233
with file_context as fname:
243-
lib.call_module(module="text", args=build_arg_string(kwargs, infile=fname))
234+
lib.call_module(module="text", args=build_arg_string(options, infile=fname))

pygmt/src/xyz2grd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def xyz2grd(
3535
incols=None,
3636
registration=None,
3737
wrap=None,
38-
**kwargs
38+
**kwargs,
3939
):
4040
r"""
4141
Create a grid file from table data.

0 commit comments

Comments
 (0)