@@ -1203,41 +1203,46 @@ def write_data(self, family, geometry, mode, wesn, output, data):
1203
1203
raise GMTCLibError (f"Failed to write dataset to '{ output } '" )
1204
1204
1205
1205
@contextlib .contextmanager
1206
- def open_virtualfile (self , family , geometry , direction , data ):
1206
+ def open_virtualfile (
1207
+ self ,
1208
+ family : str ,
1209
+ geometry : str ,
1210
+ direction : str ,
1211
+ data : ctp .c_void_p | None ,
1212
+ ) -> Generator [str , None , None ]:
1207
1213
"""
1208
- Open a GMT virtual file to pass data to and from a module .
1214
+ Open a GMT virtual file associated with a data object for reading or writing .
1209
1215
1210
- GMT uses a virtual file scheme to pass in data or get data from API
1211
- modules. Use it to pass in your GMT data structure (created using
1212
- :meth:`pygmt.clib.Session.create_data`) to a module that expects an
1213
- input file, or get the output from a module that writes to a file.
1216
+ GMT uses a virtual file scheme to pass in data or get data from API modules. Use
1217
+ it to pass in your GMT data structure (created using
1218
+ :meth:`pygmt.clib.Session.create_data`) to a module that expects an input file,
1219
+ or get the output from a module that writes to a file.
1214
1220
1215
- Use in a ``with`` block. Will automatically close the virtual file when
1216
- leaving the ``with`` block. Because of this, no wrapper for
1217
- ``GMT_Close_VirtualFile`` is provided.
1221
+ Use in a ``with`` block. Will automatically close the virtual file when leaving
1222
+ the ``with`` block. Because of this, no wrapper for ``GMT_Close_VirtualFile``
1223
+ is provided.
1218
1224
1219
1225
Parameters
1220
1226
----------
1221
- family : str
1222
- A valid GMT data family name (e.g., ``"GMT_IS_DATASET"``). Should
1223
- be the same as the one you used to create your data structure.
1224
- geometry : str
1225
- A valid GMT data geometry name (e.g., ``"GMT_IS_POINT"``). Should
1226
- be the same as the one you used to create your data structure.
1227
- direction : str
1228
- Either ``"GMT_IN"`` or ``"GMT_OUT"`` to indicate if passing data to
1229
- GMT or getting it out of GMT, respectively.
1230
- By default, GMT can modify the data you pass in. Add modifier
1231
- ``"GMT_IS_REFERENCE"`` to tell GMT the data are read-only, or
1232
- ``"GMT_IS_DUPLICATE"`` to tell GMT to duplicate the data.
1233
- data : int or None
1234
- The ctypes void pointer to your GMT data structure. For output
1235
- (i.e., ``direction="GMT_OUT"``), it can be ``None`` to have GMT
1236
- automatically allocate the output GMT data structure.
1227
+ family
1228
+ A valid GMT data family name (e.g., ``"GMT_IS_DATASET"``). Should be the
1229
+ same as the one you used to create your data structure.
1230
+ geometry
1231
+ A valid GMT data geometry name (e.g., ``"GMT_IS_POINT"``). Should be the
1232
+ same as the one you used to create your data structure.
1233
+ direction
1234
+ Either ``"GMT_IN"`` or ``"GMT_OUT"`` to indicate if passing data to GMT or
1235
+ getting it out of GMT, respectively. By default, GMT can modify the data you
1236
+ pass in. Add modifier ``"GMT_IS_REFERENCE"`` to tell GMT the data are
1237
+ read-only, or ``"GMT_IS_DUPLICATE"`` to tell GMT to duplicate the data.
1238
+ data
1239
+ The ctypes void pointer to the GMT data structure. For output (i.e.,
1240
+ ``direction="GMT_OUT"``), it can be ``None`` to have GMT automatically
1241
+ allocate the output GMT data structure.
1237
1242
1238
1243
Yields
1239
1244
------
1240
- vfname : str
1245
+ vfname
1241
1246
The name of the virtual file that you can pass to a GMT module.
1242
1247
1243
1248
Examples
@@ -1270,19 +1275,19 @@ def open_virtualfile(self, family, geometry, direction, data):
1270
1275
c_open_virtualfile = self .get_libgmt_func (
1271
1276
"GMT_Open_VirtualFile" ,
1272
1277
argtypes = [
1273
- ctp .c_void_p ,
1274
- ctp .c_uint ,
1275
- ctp .c_uint ,
1276
- ctp .c_uint ,
1277
- ctp .c_void_p ,
1278
- ctp .c_char_p ,
1278
+ ctp .c_void_p , # V_API
1279
+ ctp .c_uint , # family
1280
+ ctp .c_uint , # geometry
1281
+ ctp .c_uint , # direction
1282
+ ctp .c_void_p , # data
1283
+ ctp .c_char_p , # name
1279
1284
],
1280
1285
restype = ctp .c_int ,
1281
1286
)
1282
1287
1283
1288
c_close_virtualfile = self .get_libgmt_func (
1284
1289
"GMT_Close_VirtualFile" ,
1285
- argtypes = [ctp .c_void_p , ctp .c_char_p ],
1290
+ argtypes = [ctp .c_void_p , ctp .c_char_p ], # V_API, name
1286
1291
restype = ctp .c_int ,
1287
1292
)
1288
1293
@@ -1297,19 +1302,24 @@ def open_virtualfile(self, family, geometry, direction, data):
1297
1302
self .session_pointer , family_int , geometry_int , direction_int , data , buff
1298
1303
)
1299
1304
if status != 0 :
1300
- raise GMTCLibError ("Failed to create a virtual file." )
1305
+ msg = (
1306
+ f"Failed to create a virtual file with { family = } , { geometry = } , "
1307
+ f"{ direction = } ."
1308
+ )
1309
+ raise GMTCLibError (msg )
1301
1310
1302
1311
vfname = buff .value .decode ()
1303
1312
try :
1304
1313
yield vfname
1305
1314
finally :
1306
1315
status = c_close_virtualfile (self .session_pointer , vfname .encode ())
1307
1316
if status != 0 :
1308
- raise GMTCLibError (f"Failed to close virtual file '{ vfname } '." )
1317
+ msg = f"Failed to close virtual file '{ vfname } '."
1318
+ raise GMTCLibError (msg )
1309
1319
1310
1320
def open_virtual_file (self , family , geometry , direction , data ):
1311
1321
"""
1312
- Open a GMT virtual file to pass data to and from a module .
1322
+ Open a GMT virtual file associated with a data object for reading or writing .
1313
1323
1314
1324
.. deprecated: 0.11.0
1315
1325
0 commit comments