Skip to content

Commit 0b5a69b

Browse files
Improve the docstrings for the GMT_Open_VirtualFile wrapper (#2408)
Co-authored-by: Michael Grund <23025878+michaelgrund@users.noreply.github.com>
1 parent 57c3a1c commit 0b5a69b

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

pygmt/clib/session.py

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,38 @@
2828
from pygmt.helpers import data_kind, dummy_context, fmt_docstring, tempfile_from_geojson
2929

3030
FAMILIES = [
31-
"GMT_IS_DATASET",
32-
"GMT_IS_GRID",
33-
"GMT_IS_PALETTE",
34-
"GMT_IS_MATRIX",
35-
"GMT_IS_VECTOR",
31+
"GMT_IS_DATASET", # Entity is a data table
32+
"GMT_IS_GRID", # Entity is a grid
33+
"GMT_IS_IMAGE", # Entity is a 1- or 3-band unsigned char image
34+
"GMT_IS_PALETTE", # Entity is a color palette table
35+
"GMT_IS_POSTSCRIPT", # Entity is a PostScript content struct
36+
"GMT_IS_MATRIX", # Entity is a user matrix
37+
"GMT_IS_VECTOR", # Entity is a set of user vectors
38+
"GMT_IS_CUBE", # Entity is a 3-D data cube
3639
]
3740

38-
VIAS = ["GMT_VIA_MATRIX", "GMT_VIA_VECTOR"]
41+
VIAS = [
42+
"GMT_VIA_MATRIX", # dataset is passed as a matrix
43+
"GMT_VIA_VECTOR", # dataset is passed as a set of vectors
44+
]
3945

4046
GEOMETRIES = [
41-
"GMT_IS_NONE",
42-
"GMT_IS_POINT",
43-
"GMT_IS_LINE",
44-
"GMT_IS_POLYGON",
45-
"GMT_IS_PLP",
46-
"GMT_IS_SURFACE",
47+
"GMT_IS_NONE", # items without geometry (e.g., CPT)
48+
"GMT_IS_POINT", # items are points
49+
"GMT_IS_LINE", # items are lines
50+
"GMT_IS_POLY", # items are polygons
51+
"GMT_IS_LP", # items could be any one of LINE or POLY
52+
"GMT_IS_PLP", # items could be any one of POINT, LINE, or POLY
53+
"GMT_IS_SURFACE", # items are 2-D grid
54+
"GMT_IS_VOLUME", # items are 3-D grid
55+
]
56+
57+
METHODS = [
58+
"GMT_IS_DUPLICATE", # tell GMT the data are read-only
59+
"GMT_IS_REFERENCE", # tell GMT to duplicate the data
4760
]
4861

49-
METHODS = ["GMT_IS_DUPLICATE", "GMT_IS_REFERENCE"]
62+
DIRECTIONS = ["GMT_IN", "GMT_OUT"]
5063

5164
MODES = ["GMT_CONTAINER_ONLY", "GMT_IS_OUTPUT"]
5265

@@ -985,12 +998,12 @@ def write_data(self, family, geometry, mode, wesn, output, data):
985998
@contextmanager
986999
def open_virtual_file(self, family, geometry, direction, data):
9871000
"""
988-
Open a GMT Virtual File to pass data to and from a module.
1001+
Open a GMT virtual file to pass data to and from a module.
9891002
990-
GMT uses a virtual file scheme to pass in data to API modules. Use it
991-
to pass in your GMT data structure (created using
1003+
GMT uses a virtual file scheme to pass in data or get data from API
1004+
modules. Use it to pass in your GMT data structure (created using
9921005
:meth:`pygmt.clib.Session.create_data`) to a module that expects an
993-
input or output file.
1006+
input file, or get the output from a module that writes to a file.
9941007
9951008
Use in a ``with`` block. Will automatically close the virtual file when
9961009
leaving the ``with`` block. Because of this, no wrapper for
@@ -999,19 +1012,21 @@ def open_virtual_file(self, family, geometry, direction, data):
9991012
Parameters
10001013
----------
10011014
family : str
1002-
A valid GMT data family name (e.g., ``'GMT_IS_DATASET'``). Should
1015+
A valid GMT data family name (e.g., ``"GMT_IS_DATASET"``). Should
10031016
be the same as the one you used to create your data structure.
10041017
geometry : str
1005-
A valid GMT data geometry name (e.g., ``'GMT_IS_POINT'``). Should
1018+
A valid GMT data geometry name (e.g., ``"GMT_IS_POINT"``). Should
10061019
be the same as the one you used to create your data structure.
10071020
direction : str
1008-
Either ``'GMT_IN'`` or ``'GMT_OUT'`` to indicate if passing data to
1021+
Either ``"GMT_IN"`` or ``"GMT_OUT"`` to indicate if passing data to
10091022
GMT or getting it out of GMT, respectively.
10101023
By default, GMT can modify the data you pass in. Add modifier
1011-
``'GMT_IS_REFERENCE'`` to tell GMT the data are read-only, or
1012-
``'GMT_IS_DUPLICATE'`` to tell GMT to duplicate the data.
1013-
data : int
1014-
The ctypes void pointer to your GMT data structure.
1024+
``"GMT_IS_REFERENCE"`` to tell GMT the data are read-only, or
1025+
``"GMT_IS_DUPLICATE"`` to tell GMT to duplicate the data.
1026+
data : int or None
1027+
The ctypes void pointer to your GMT data structure. For output
1028+
(i.e., ``direction="GMT_OUT"``), it can be ``None`` to have GMT
1029+
automatically allocate the output GMT data structure.
10151030
10161031
Yields
10171032
------
@@ -1022,7 +1037,6 @@ def open_virtual_file(self, family, geometry, direction, data):
10221037
--------
10231038
10241039
>>> from pygmt.helpers import GMTTempFile
1025-
>>> import os
10261040
>>> import numpy as np
10271041
>>> x = np.array([0, 1, 2, 3, 4])
10281042
>>> y = np.array([5, 6, 7, 8, 9])
@@ -1070,20 +1084,17 @@ def open_virtual_file(self, family, geometry, direction, data):
10701084
family_int = self._parse_constant(family, valid=FAMILIES, valid_modifiers=VIAS)
10711085
geometry_int = self._parse_constant(geometry, valid=GEOMETRIES)
10721086
direction_int = self._parse_constant(
1073-
direction, valid=["GMT_IN", "GMT_OUT"], valid_modifiers=METHODS
1087+
direction, valid=DIRECTIONS, valid_modifiers=METHODS
10741088
)
10751089

10761090
buff = ctp.create_string_buffer(self["GMT_VF_LEN"])
1077-
10781091
status = c_open_virtualfile(
10791092
self.session_pointer, family_int, geometry_int, direction_int, data, buff
10801093
)
1081-
10821094
if status != 0:
10831095
raise GMTCLibError("Failed to create a virtual file.")
10841096

10851097
vfname = buff.value.decode()
1086-
10871098
try:
10881099
yield vfname
10891100
finally:

0 commit comments

Comments
 (0)