Skip to content

Commit 30a27af

Browse files
committed
Merge branch 'main' into exception/typeerror
2 parents bc3edef + 985ff10 commit 30a27af

24 files changed

+129
-82
lines changed

doc/techref/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ visit the {gmt-docs}`GMT Technical Reference <reference.html>`.
1111
common_parameters.md
1212
projections.md
1313
fonts.md
14+
text_formatting.md
1415
patterns.md
1516
encodings.md
1617
environment_variables.md

doc/techref/text_formatting.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Text Formatting
2+
3+
The table below shows how text added to a plot, such as text strings added via
4+
`Figure.text`, plot title, labels of Cartesian axes and colorbars, and legend entries.
5+
Changing the font as well as its color and size in this way allows to change those
6+
only for specific characters of a longer text. The supported fonts are listed at
7+
[](/techref/fonts.md).
8+
9+
| Symbol | Behavior |
10+
| --- | --- |
11+
| @%*font*%*TEXT*@%% | Change the *font* of *TEXT* |
12+
| @;*color*;*TEXT*@;; | Change the font *color* of *TEXT* |
13+
| @:*size*:*TEXT*@:: | Change the font *size* of *TEXT* |
14+
| @-*TEXT*@- | Toggle subscripts on/off |
15+
| @+*TEXT*@+ | Toggle superscript on/off |
16+
| @#*TEXT*@# | Toggle small caps on/off |
17+
| @\_*TEXT*@\_ | Toggle underline on/off |
18+
| @!*CHAR1CHAR2* | Print two characters on top of each other (composite characters, e.g., overstrike) |
19+
| @\~*TEXT*@\~ | Toggle Greek letters on/off |
20+
| @@ | Print the @ sign |
21+
| @. | Print the ° (degree) symbol |
22+
| @s | Print letter ß |
23+
| @i | Print letter í |
24+
| @a or @A | Print letter å or Å |
25+
| @c or @C | Print letter ç or Ç |
26+
| @e or @E | Print letter æ or Æ |
27+
| @n or @N | Print letter ñ or Ñ |
28+
| @o or @O | Print letter ø or Ø |
29+
| @u or @U | Print letter ü or Ü |

examples/gallery/maps/country_polygons.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
Highlight country, continent and state polygons
33
===============================================
44
5-
The :meth:`pygmt.Figure.coast` method can highlight country polygons
6-
via the ``dcw`` parameter. It accepts the country code or full
7-
country name and can draw its borders and add a color to its landmass.
8-
It's also possible to define multiple countries at once by separating
9-
the individual names with commas.
5+
The :meth:`pygmt.Figure.coast` method can highlight country polygons via the ``dcw``
6+
parameter. It accepts the country code (following the 2-letter
7+
`ISO 3166-1 alpha-2 convention <https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2>`_) or
8+
full country name and can draw its borders and add a color to its landmass. It's also
9+
possible to define multiple countries at once by separating the individual names with
10+
commas.
1011
"""
1112

1213
# %%

pygmt/clib/conversion.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import pandas as pd
1313
import xarray as xr
1414
from packaging.version import Version
15-
from pygmt.exceptions import GMTInvalidInput
15+
from pygmt.exceptions import GMTValueError
1616

1717

1818
def dataarray_to_matrix(
@@ -49,7 +49,7 @@ def dataarray_to_matrix(
4949
5050
Raises
5151
------
52-
GMTInvalidInput
52+
GMTValueError
5353
If the grid has more than two dimensions or variable grid spacing.
5454
5555
Examples
@@ -92,8 +92,11 @@ def dataarray_to_matrix(
9292
[2.0, 2.0]
9393
"""
9494
if len(grid.dims) != 2:
95-
msg = f"Invalid number of grid dimensions 'len({grid.dims})'. Must be 2."
96-
raise GMTInvalidInput(msg)
95+
raise GMTValueError(
96+
len(grid.dims),
97+
description="number of grid dimensions",
98+
reason="The grid must be 2-D.",
99+
)
97100

98101
# Extract region and inc from the grid
99102
region, inc = [], []
@@ -113,8 +116,11 @@ def dataarray_to_matrix(
113116
)
114117
warnings.warn(msg, category=RuntimeWarning, stacklevel=2)
115118
if coord_inc == 0:
116-
msg = f"Grid has a zero increment in the '{dim}' dimension."
117-
raise GMTInvalidInput(msg)
119+
raise GMTValueError(
120+
coord_inc,
121+
description="grid increment",
122+
reason=f"Grid has a zero increment in the '{dim}' dimension.",
123+
)
118124
region.extend(
119125
[
120126
coord.min() - coord_inc / 2 * grid.gmt.registration,

pygmt/clib/session.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,8 @@ def _check_dtype_and_dim(self, array: np.ndarray, ndim: int) -> int:
916916
917917
Raises
918918
------
919+
GMTValueError
920+
If the array has the wrong number of dimensions.
919921
GMTTypeError
920922
If the array is an unsupported data type.
921923
@@ -935,8 +937,11 @@ def _check_dtype_and_dim(self, array: np.ndarray, ndim: int) -> int:
935937
"""
936938
# Check that the array has the given number of dimensions.
937939
if array.ndim != ndim:
938-
msg = f"Expected a numpy {ndim}-D array, got {array.ndim}-D."
939-
raise GMTInvalidInput(msg)
940+
raise GMTValueError(
941+
array.ndim,
942+
description="array dimension",
943+
reason=f"Expected a numpy {ndim}-D array, got {array.ndim}-D.",
944+
)
940945

941946
# 1-D arrays can be numeric or text, 2-D arrays can only be numeric.
942947
valid_dtypes = DTYPES if ndim == 1 else DTYPES_NUMERIC

pygmt/helpers/utils.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import xarray as xr
1919
from pygmt._typing import PathLike
2020
from pygmt.encodings import charset
21-
from pygmt.exceptions import GMTInvalidInput
21+
from pygmt.exceptions import GMTInvalidInput, GMTValueError
2222

2323
# Type hints for the list of encodings supported by PyGMT.
2424
Encoding = Literal[
@@ -597,8 +597,7 @@ def build_arg_list( # noqa: PLR0912
597597
or os.fspath(outfile) in {"", ".", ".."}
598598
or os.fspath(outfile).endswith(("/", "\\"))
599599
):
600-
msg = f"Invalid output file name '{outfile}'."
601-
raise GMTInvalidInput(msg)
600+
raise GMTValueError(outfile, description="output file name")
602601
gmt_args.append(f"->{os.fspath(outfile)}")
603602
return gmt_args
604603

pygmt/helpers/validators.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import Literal
77

88
from pygmt._typing import PathLike
9-
from pygmt.exceptions import GMTInvalidInput
9+
from pygmt.exceptions import GMTInvalidInput, GMTValueError
1010

1111

1212
def validate_output_table_type(
@@ -39,7 +39,7 @@ def validate_output_table_type(
3939
>>> validate_output_table_type(output_type="invalid-type")
4040
Traceback (most recent call last):
4141
...
42-
pygmt.exceptions.GMTInvalidInput: Must specify 'output_type' either as 'file', ...
42+
pygmt....GMTValueError: ...: 'invalid-type'. Expected one of: ...
4343
>>> validate_output_table_type("file", outfile=None)
4444
Traceback (most recent call last):
4545
...
@@ -49,9 +49,13 @@ def validate_output_table_type(
4949
... assert len(w) == 1
5050
'file'
5151
"""
52-
if output_type not in {"file", "numpy", "pandas"}:
53-
msg = "Must specify 'output_type' either as 'file', 'numpy', or 'pandas'."
54-
raise GMTInvalidInput(msg)
52+
_valids = {"file", "numpy", "pandas"}
53+
if output_type not in _valids:
54+
raise GMTValueError(
55+
output_type,
56+
description="value for parameter 'output_type'",
57+
choices=_valids,
58+
)
5559
if output_type == "file" and outfile is None:
5660
msg = "Must specify 'outfile' for output_type='file'."
5761
raise GMTInvalidInput(msg)

pygmt/src/_common.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from pathlib import Path
88
from typing import Any, ClassVar, Literal
99

10-
from pygmt.exceptions import GMTInvalidInput, GMTValueError
10+
from pygmt.exceptions import GMTValueError
1111
from pygmt.src.which import which
1212

1313

@@ -125,7 +125,7 @@ class _FocalMechanismConvention:
125125
>>> _FocalMechanismConvention.from_params(["strike", "dip", "rake"])
126126
Traceback (most recent call last):
127127
...
128-
pygmt.exceptions.GMTInvalidInput: Fail to determine focal mechanism convention...
128+
pygmt.exceptions.GMTValueError: Invalid focal mechanism parameters: ...
129129
"""
130130

131131
# Mapping of focal mechanism conventions to their parameters.
@@ -236,18 +236,14 @@ def from_params(
236236
237237
Raises
238238
------
239-
GMTInvalidInput
239+
GMTValueError
240240
If the focal mechanism convention cannot be determined from the given
241241
parameters.
242242
"""
243243
for convention, param_list in cls._params.items():
244244
if set(param_list).issubset(set(params)):
245245
return cls(convention, component=component)
246-
msg = (
247-
"Fail to determine focal mechanism convention from the given parameters: "
248-
f"{', '.join(params)}."
249-
)
250-
raise GMTInvalidInput(msg)
246+
raise GMTValueError(params, description="focal mechanism parameters")
251247

252248

253249
def _parse_coastline_resolution(

pygmt/src/coast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def coast(
168168
*code1,code2,…*\ [**+g**\ *fill*\ ][**+p**\ *pen*\ ][**+z**].
169169
Select painting country polygons from the `Digital Chart of the World
170170
<https://en.wikipedia.org/wiki/Digital_Chart_of_the_World>`__.
171-
Append one or more comma-separated countries using the 2-character
171+
Append one or more comma-separated countries using the 2-letter
172172
`ISO 3166-1 alpha-2 convention
173173
<https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2>`__.
174174
To select a state of a country (if available), append .\ *state*,

pygmt/src/grd2xyz.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import xarray as xr
1010
from pygmt._typing import PathLike
1111
from pygmt.clib import Session
12-
from pygmt.exceptions import GMTInvalidInput
12+
from pygmt.exceptions import GMTValueError
1313
from pygmt.helpers import (
1414
build_arg_list,
1515
fmt_docstring,
@@ -145,10 +145,11 @@ def grd2xyz(
145145
output_type = validate_output_table_type(output_type, outfile=outfile)
146146

147147
if kwargs.get("o") is not None and output_type == "pandas":
148-
msg = (
149-
"If 'outcols' is specified, 'output_type' must be either 'numpy' or 'file'."
148+
raise GMTValueError(
149+
output_type,
150+
description="value for parameter 'output_type'",
151+
reason="Expected one of: 'numpy', 'file' if 'outcols' is specified.",
150152
)
151-
raise GMTInvalidInput(msg)
152153
# Set the default column names for the pandas DataFrame header.
153154
column_names: list[str] = ["x", "y", "z"]
154155
# Let output pandas column names match input DataArray dimension names

0 commit comments

Comments
 (0)