Skip to content

Commit d3c7bd2

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

File tree

1 file changed

+62
-9
lines changed

1 file changed

+62
-9
lines changed

pygmt/src/grdview.py

Lines changed: 62 additions & 9 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,27 @@ 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:
101+
surftype
102+
Specify cover type of the grid. Valid values are:
94103
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.
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).
101109
102110
For any of these choices, you may force a monochrome image by appending the
103111
modifier **+m**.
112+
dpi
113+
Effective dots-per-unit resolution for the rasterization for image plots (i.e.,
114+
``surftype="image"``) [Default is :gmt-term:`GMT_GRAPHICS_DPU`]
115+
meshfill
116+
For mesh plot or waterfall plots, set the mesh fill [Default is white].
117+
monochrome
118+
Force conversion to monochrome image using the (television) YIQ transformation.
119+
nan_transparent
120+
Make grid nodes with z = NaN transparent, using the color-masking feature in
121+
PostScript Level 3. Only applies when ``surftype="image"``.
104122
contourpen : str
105123
Draw contour lines on top of surface or mesh (not image). Append pen attributes
106124
used for the contours.
@@ -159,9 +177,44 @@ def grdview( # noqa: PLR0913
159177
"""
160178
self._activate_figure()
161179

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

0 commit comments

Comments
 (0)