Skip to content

Commit

Permalink
Refactor Figure.text to make it more readable/maintainable (#3365)
Browse files Browse the repository at this point in the history
  • Loading branch information
seisman authored Sep 23, 2024
1 parent a5dbc13 commit f7110e2
Showing 1 changed file with 56 additions and 37 deletions.
93 changes: 56 additions & 37 deletions pygmt/src/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,55 +179,61 @@ def text_( # noqa: PLR0912
kwargs = self._preprocess(**kwargs)

# Ensure inputs are either textfiles, x/y/text, or position/text
if position is None:
if any(v is not None for v in (x, y, text)) and textfiles is not None:
raise GMTInvalidInput(
"Provide either position only, or x/y pairs, or textfiles."
)
kind = data_kind(textfiles)
if kind == "vectors" and text is None:
raise GMTInvalidInput("Must provide text with x/y pairs")
else:
if any(v is not None for v in (x, y, textfiles)):
raise GMTInvalidInput(
"Provide either position only, or x/y pairs, or textfiles."
)
if text is None or is_nonstr_iter(text):
raise GMTInvalidInput("Text can't be None or array.")
kind = None
textfiles = ""
if (
(textfiles is not None)
+ (position is not None)
+ (x is not None or y is not None)
) != 1:
raise GMTInvalidInput("Provide either textfiles, x/y/text, or position/text.")

required_data = position is None
kind = data_kind(textfiles, required=required_data)

# Build the -F option in gmt text.
if position is not None and (text is None or is_nonstr_iter(text)):
raise GMTInvalidInput("'text' can't be None or array when 'position' is given.")
if textfiles is not None and text is not None:
raise GMTInvalidInput("'text' can't be specified when 'textfiles' is given.")
if kind == "vectors" and text is None:
raise GMTInvalidInput("Must provide text with x/y pairs.")

# Arguments that can accept arrays.
array_args = [
(angle, "+a", "angle"),
(font, "+f", "font"),
(justify, "+j", "justify"),
]

# Build the -F option.
if kwargs.get("F") is None and any(
v is not None for v in (position, angle, font, justify)
):
kwargs.update({"F": ""})

extra_arrays = []
for arg, flag in [(angle, "+a"), (font, "+f"), (justify, "+j")]:
for arg, flag, _ in array_args:
if arg is True:
kwargs["F"] += flag
elif is_nonstr_iter(arg):
kwargs["F"] += flag
if flag == "+a": # angle is numeric type
extra_arrays.append(np.atleast_1d(arg))
else: # font or justify is str type
extra_arrays.append(np.atleast_1d(arg).astype(str))
elif isinstance(arg, int | float | str):
kwargs["F"] += f"{flag}{arg}"

if isinstance(position, str):
kwargs["F"] += f"+c{position}+t{text}"

# If an array of transparency is given, GMT will read it from
# the last numerical column per data record.
if is_nonstr_iter(kwargs.get("t")):
extra_arrays.append(kwargs["t"])
kwargs["t"] = ""

# Append text at last column. Text must be passed in as str type.
extra_arrays = []
confdict = {}
if kind == "vectors":
for arg, flag, name in array_args:
if is_nonstr_iter(arg):
kwargs["F"] += flag
# angle is numeric type and font/justify are str type.
if name == "angle":
extra_arrays.append(np.atleast_1d(arg))
else:
extra_arrays.append(np.atleast_1d(arg).astype(str))

# If an array of transparency is given, GMT will read it from the last numerical
# column per data record.
if is_nonstr_iter(kwargs.get("t")):
extra_arrays.append(np.atleast_1d(kwargs["t"]))
kwargs["t"] = True

# Append text to the last column. Text must be passed in as str type.
text = np.atleast_1d(text).astype(str)
encoding = _check_encoding("".join(text))
if encoding != "ascii":
Expand All @@ -238,10 +244,23 @@ def text_( # noqa: PLR0912

if encoding not in {"ascii", "ISOLatin1+"}:
confdict = {"PS_CHAR_ENCODING": encoding}
else:
if isinstance(position, str):
kwargs["F"] += f"+c{position}+t{text}"

for arg, _, name in [*array_args, (kwargs.get("t"), "", "transparency")]:
if is_nonstr_iter(arg):
msg = f"Argument of '{name}' must be a single value or True."
raise GMTInvalidInput(msg)

with Session() as lib:
with lib.virtualfile_in(
check_kind="vector", data=textfiles, x=x, y=y, extra_arrays=extra_arrays
check_kind="vector",
data=textfiles,
x=x,
y=y,
extra_arrays=extra_arrays,
required_data=required_data,
) as vintbl:
lib.call_module(
module="text",
Expand Down

0 comments on commit f7110e2

Please sign in to comment.