pygmt.clib.Session.virtualfile_in
- Session.virtualfile_in(check_kind=None, data=None, x=None, y=None, z=None, required=True, mincols=2)[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 or None) – Used to validate the type of data that can be passed in. Choose from ‘raster’, ‘vector’, or None. Default is None (no validation).
data (default:
None) – Any raster or vector data format. This could be a file name or path, a raster grid, a vector matrix/arrays, or other supported data input.x/y/z (1-D arrays or None) – x, y, and z columns as numpy arrays.
required (bool) – Set to True when ‘data’ or (‘x’ and ‘y’) is required. Set to False when dealing with optional virtual files. Default is True.
mincols (default:
2) – Number of minimum required columns. Default is 2 (i.e. require x and y columns).
- 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=dict(index=[0, 1, 2]), ... data_vars=dict( ... x=("index", [9, 8, 7]), ... y=("index", [6, 5, 4]), ... z=("index", [3, 2, 1]), ... ), ... ) >>> with Session() as ses: ... with ses.virtualfile_in(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", [fin, f"->{fout.name}"]) ... print(fout.read().strip()) <vector memory>: N = 3 <7/9> <4/6> <1/3>