pygmt.clib.Session.virtualfile_from_matrix

Session.virtualfile_from_matrix(matrix)[source]

Store a 2-D numpy array as a matrix inside a virtual file.

Use the virtual file name to pass in the data in your matrix to a GMT module.

Context manager (use in a with block). Yields the virtual file name that you can pass as an argument to a GMT module call. Closes the virtual file upon exit of the with block.

The virtual file will contain the array as a GMT_MATRIX data container pretending to be a GMT_DATASET data container.

Not meant for creating ``GMT_GRID``. The grid requires more metadata than just the data matrix. Use pygmt.clib.Session.virtualfile_from_grid instead.

Use this instead of creating the data container and virtual file by hand with pygmt.clib.Session.create_data, pygmt.clib.Session.put_matrix, and pygmt.clib.Session.open_virtualfile.

The matrix must be C contiguous in memory. If it is not (e.g., it is a slice of a larger array), the array will be copied to make sure it is.

Parameters:

matrix (ndarray) – The matrix that will be included in the GMT data container.

Yields:

fname – The name of virtual file. Pass this as a file name argument to a GMT module.

Return type:

Generator[str, None, None]

Examples

>>> from pygmt.helpers import GMTTempFile
>>> import numpy as np
>>> data = np.arange(12).reshape((4, 3))
>>> print(data)
[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
>>> with Session() as ses:
...     with ses.virtualfile_from_matrix(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())
<matrix memory>: N = 4 <0/9> <1/10> <2/11>