pygmt.clib.Session.open_virtual_file¶
- Session.open_virtual_file(family, geometry, direction, data)[source]¶
Open a GMT Virtual File to pass data to and from a module.
GMT uses a virtual file scheme to pass in data to API modules. Use it to pass in your GMT data structure (created using
pygmt.clib.Session.create_data
) to a module that expects an input or output file.Use in a
with
block. Will automatically close the virtual file when leaving thewith
block. Because of this, no wrapper forGMT_Close_VirtualFile
is provided.- Parameters
family (str) – A valid GMT data family name (e.g.,
'GMT_IS_DATASET'
). Should be the same as the one you used to create your data structure.geometry (str) – A valid GMT data geometry name (e.g.,
'GMT_IS_POINT'
). Should be the same as the one you used to create your data structure.direction (str) – Either
'GMT_IN'
or'GMT_OUT'
to indicate if passing data to GMT or getting it out of GMT, respectively. By default, GMT can modify the data you pass in. Add modifier'GMT_IS_REFERENCE'
to tell GMT the data are read-only, or'GMT_IS_DUPLICATE'
to tell GMT to duplicate the data.data (int) – The ctypes void pointer to your GMT data structure.
- Yields
vfname (str) – The name of the virtual file that you can pass to a GMT module.
Examples
>>> from pygmt.helpers import GMTTempFile >>> import os >>> import numpy as np >>> x = np.array([0, 1, 2, 3, 4]) >>> y = np.array([5, 6, 7, 8, 9]) >>> with Session() as lib: ... family = "GMT_IS_DATASET|GMT_VIA_VECTOR" ... geometry = "GMT_IS_POINT" ... dataset = lib.create_data( ... family=family, ... geometry=geometry, ... mode="GMT_CONTAINER_ONLY", ... dim=[2, 5, 1, 0], # columns, lines, segments, type ... ) ... lib.put_vector(dataset, column=0, vector=x) ... lib.put_vector(dataset, column=1, vector=y) ... # Add the dataset to a virtual file ... vfargs = (family, geometry, "GMT_IN|GMT_IS_REFERENCE", dataset) ... with lib.open_virtual_file(*vfargs) as vfile: ... # Send the output to a temp file so that we can read it ... with GMTTempFile() as ofile: ... args = "{} ->{}".format(vfile, ofile.name) ... lib.call_module("info", args) ... print(ofile.read().strip()) ... <vector memory>: N = 5 <0/4> <5/9>