pygmt.clib.Session.virtualfile_from_data
- Session.virtualfile_from_data(check_kind=None, data=None, x=None, y=None, z=None, extra_arrays=None, required_z=False)[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 or pathlib.Path or xarray.DataArray or numpy.ndarray or pandas.DataFrame or xarray.Dataset or geopandas.GeoDataFrame or 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 (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=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_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", fin + " ->" + fout.name) ... print(fout.read().strip()) ... <vector memory>: N = 3 <7/9> <4/6> <1/3>