pygmt.clib.Session.virtualfile_from_data

Session.virtualfile_from_data(check_kind=None, data=None, x=None, y=None, z=None, extra_arrays=None)[source]

Store any data inside a virtual file.

This convenience function automatically detects the kind of data passed into it, and produces a virtualfile that can be passed into GMT later on.

Parameters
  • check_kind (str) – Used to validate the type of data that can be passed in. Choose from ‘raster’, ‘vector’ or None. Default is None (no validation).

  • data (str, xarray.DataArray, 2d array, or None) – Any raster or vector data format. This could be a file name, a raster grid, a vector matrix/arrays, or other supported data input.

  • x/y/z (1d arrays or None) – x, y and z columns as numpy arrays.

  • extra_arrays (list of 1d arrays) – Optional. A list of numpy arrays in addition to x, y and z. All of these arrays must be of the same size as the x/y/z arrays.

Returns

file_context (contextlib._GeneratorContextManager) – The virtual file stored inside a context manager. Access the file name of this virtualfile using with file_context as fname: ....

Examples

>>> from pygmt.helpers import GMTTempFile
>>> import xarray as xr
>>> data = xr.Dataset(
...     coords={"index": [0, 1, 2]},
...     data_vars={
...         "x": ("index", [9, 8, 7]),
...         "y": ("index", [6, 5, 4]),
...         "z": ("index", [3, 2, 1]),
...     },
... )
>>> with Session() as ses:
...     with ses.virtualfile_from_data(
...         check_kind="vector", data=data
...     ) as fin:
...         # Send the output to a file so that we can read it
...         with GMTTempFile() as fout:
...             ses.call_module("info", f"{fin} ->{fout.name}")
...             print(fout.read().strip())
...
<vector memory>: N = 3 <7/9> <4/6> <1/3>
Copy to clipboard