28
28
from pygmt .helpers import data_kind , dummy_context , fmt_docstring , tempfile_from_geojson
29
29
30
30
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
36
39
]
37
40
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
+ ]
39
45
40
46
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
47
60
]
48
61
49
- METHODS = ["GMT_IS_DUPLICATE " , "GMT_IS_REFERENCE " ]
62
+ DIRECTIONS = ["GMT_IN " , "GMT_OUT " ]
50
63
51
64
MODES = ["GMT_CONTAINER_ONLY" , "GMT_IS_OUTPUT" ]
52
65
@@ -985,12 +998,12 @@ def write_data(self, family, geometry, mode, wesn, output, data):
985
998
@contextmanager
986
999
def open_virtual_file (self , family , geometry , direction , data ):
987
1000
"""
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.
989
1002
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
992
1005
: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.
994
1007
995
1008
Use in a ``with`` block. Will automatically close the virtual file when
996
1009
leaving the ``with`` block. Because of this, no wrapper for
@@ -999,19 +1012,21 @@ def open_virtual_file(self, family, geometry, direction, data):
999
1012
Parameters
1000
1013
----------
1001
1014
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
1003
1016
be the same as the one you used to create your data structure.
1004
1017
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
1006
1019
be the same as the one you used to create your data structure.
1007
1020
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
1009
1022
GMT or getting it out of GMT, respectively.
1010
1023
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.
1015
1030
1016
1031
Yields
1017
1032
------
@@ -1022,7 +1037,6 @@ def open_virtual_file(self, family, geometry, direction, data):
1022
1037
--------
1023
1038
1024
1039
>>> from pygmt.helpers import GMTTempFile
1025
- >>> import os
1026
1040
>>> import numpy as np
1027
1041
>>> x = np.array([0, 1, 2, 3, 4])
1028
1042
>>> y = np.array([5, 6, 7, 8, 9])
@@ -1070,20 +1084,17 @@ def open_virtual_file(self, family, geometry, direction, data):
1070
1084
family_int = self ._parse_constant (family , valid = FAMILIES , valid_modifiers = VIAS )
1071
1085
geometry_int = self ._parse_constant (geometry , valid = GEOMETRIES )
1072
1086
direction_int = self ._parse_constant (
1073
- direction , valid = [ "GMT_IN" , "GMT_OUT" ] , valid_modifiers = METHODS
1087
+ direction , valid = DIRECTIONS , valid_modifiers = METHODS
1074
1088
)
1075
1089
1076
1090
buff = ctp .create_string_buffer (self ["GMT_VF_LEN" ])
1077
-
1078
1091
status = c_open_virtualfile (
1079
1092
self .session_pointer , family_int , geometry_int , direction_int , data , buff
1080
1093
)
1081
-
1082
1094
if status != 0 :
1083
1095
raise GMTCLibError ("Failed to create a virtual file." )
1084
1096
1085
1097
vfname = buff .value .decode ()
1086
-
1087
1098
try :
1088
1099
yield vfname
1089
1100
finally :
0 commit comments