Skip to content

Commit 2f32c36

Browse files
committed
Figure.grdview: Pythonic syntax for surftype parameter
1 parent 56e5780 commit 2f32c36

File tree

1 file changed

+63
-13
lines changed

1 file changed

+63
-13
lines changed

pygmt/src/grdview.py

Lines changed: 63 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pygmt._typing import PathLike
1010
from pygmt.alias import Alias, AliasSystem
1111
from pygmt.clib import Session
12+
from pygmt.exceptions import GMTInvalidInput
1213
from pygmt.helpers import build_arg_list, fmt_docstring, use_alias
1314

1415
__doctest_skip__ = ["grdview"]
@@ -19,7 +20,6 @@
1920
C="cmap",
2021
G="drapegrid",
2122
N="plane",
22-
Q="surftype",
2323
Wc="contourpen",
2424
Wm="meshpen",
2525
Wf="facadepen",
@@ -30,6 +30,14 @@
3030
def grdview( # noqa: PLR0913
3131
self,
3232
grid: PathLike | xr.DataArray,
33+
surftype: Literal[
34+
"mesh", "surface", "surface+mesh", "image", "waterfallx", "waterfally"
35+
]
36+
| None = None,
37+
dpi: int | None = None,
38+
meshfill: float | None = None,
39+
monochrome: bool = False,
40+
nan_transparent: bool = False,
3341
projection: str | None = None,
3442
zscale: float | str | None = None,
3543
zsize: float | str | None = None,
@@ -59,6 +67,7 @@ def grdview( # noqa: PLR0913
5967
- Jz = zscale
6068
- JZ = zsize
6169
- R = region
70+
- Q = surftype
6271
- V = verbose
6372
- c = panel
6473
- p = perspective
@@ -89,18 +98,24 @@ def grdview( # noqa: PLR0913
8998
Draw a plane at this z-level. If the optional color is provided via the **+g**
9099
modifier, and the projection is not oblique, the frontal facade between the
91100
plane and the data perimeter is colored.
92-
surftype : str
93-
Specify cover type of the grid. Select one of following settings:
94-
95-
- **m**: mesh plot [Default].
96-
- **mx** or **my**: waterfall plots (row or column profiles).
97-
- **s**: surface plot, and optionally append **m** to have mesh lines drawn on
98-
top of the surface.
99-
- **i**: image plot.
100-
- **c**: Same as **i** but will make nodes with z = NaN transparent.
101-
102-
For any of these choices, you may force a monochrome image by appending the
103-
modifier **+m**.
101+
surftype
102+
Specify cover type of the grid. Valid values are:
103+
104+
- ``"mesh"``: mesh plot [Default].
105+
- ``"surface``: surface plot.
106+
- ``"surface+mesh"``: surface plot with mesh lines drawn on top of the surface.
107+
- ``"image"``: image plot.
108+
- ``"waterfall_x"``/``"waterfall_y"``: waterfall plots (row or column profiles).
109+
dpi
110+
Effective dots-per-unit resolution for the rasterization for image plots (i.e.,
111+
``surftype="image"``) [Default is :gmt-term:`GMT_GRAPHICS_DPU`]
112+
meshfill
113+
For mesh plot or waterfall plots, set the mesh fill [Default is white].
114+
monochrome
115+
Force conversion to monochrome image using the (television) YIQ transformation.
116+
nan_transparent
117+
Make grid nodes with z = NaN transparent, using the color-masking feature in
118+
PostScript Level 3. Only applies when ``surftype="image"``.
104119
contourpen : str
105120
Draw contour lines on top of surface or mesh (not image). Append pen attributes
106121
used for the contours.
@@ -159,9 +174,44 @@ def grdview( # noqa: PLR0913
159174
"""
160175
self._activate_figure()
161176

177+
if dpi is not None and surftype != "image":
178+
msg = "Parameter 'dpi' can only be used when 'surftype' is 'image'."
179+
raise GMTInvalidInput(msg)
180+
if nan_transparent and surftype != "image":
181+
msg = "Parameter 'nan_transparent' can only be used when 'surftype' is 'image'."
182+
raise GMTInvalidInput(msg)
183+
if meshfill is not None and surftype not in {"mesh", "waterfallx", "waterfally"}:
184+
msg = (
185+
"Parameter 'meshfill' can only be used when 'surftype' is "
186+
"'mesh', 'waterfallx', or 'waterfally'."
187+
)
188+
raise GMTInvalidInput(msg)
189+
190+
_surtype_mapping = {
191+
"surface": "s",
192+
"mesh": "m",
193+
"surface+mesh": "sm",
194+
"image": "c" if nan_transparent is True else "i",
195+
"waterfallx": "mx",
196+
"waterfally": "my",
197+
}
198+
199+
# surftype was aliased to Q previously.
200+
_old_surftype_syntax = surftype not in _surtype_mapping and surftype is not None
201+
162202
aliasdict = AliasSystem(
163203
Jz=Alias(zscale, name="zscale"),
164204
JZ=Alias(zsize, name="zsize"),
205+
Q=[
206+
Alias(
207+
surftype,
208+
name="surftype",
209+
mapping=_surtype_mapping if not _old_surftype_syntax else None,
210+
),
211+
Alias(dpi, name="dpi"),
212+
Alias(meshfill, name="meshfill"),
213+
Alias(monochrome, name="monochrome", prefix="+m"),
214+
],
165215
).add_common(
166216
B=frame,
167217
J=projection,

0 commit comments

Comments
 (0)