|
9 | 9 | import pathlib
|
10 | 10 | import sys
|
11 | 11 | import warnings
|
| 12 | +from typing import Literal |
12 | 13 |
|
13 | 14 | import numpy as np
|
14 | 15 | import pandas as pd
|
|
21 | 22 | vectors_to_arrays,
|
22 | 23 | )
|
23 | 24 | from pygmt.clib.loading import load_libgmt
|
| 25 | +from pygmt.datatypes import _GMT_DATASET, _GMT_GRID |
24 | 26 | from pygmt.exceptions import (
|
25 | 27 | GMTCLibError,
|
26 | 28 | GMTCLibNoSessionError,
|
@@ -1588,6 +1590,39 @@ def virtualfile_from_data( # noqa: PLR0912
|
1588 | 1590 |
|
1589 | 1591 | return file_context
|
1590 | 1592 |
|
| 1593 | + def read_virtualfile( |
| 1594 | + self, vfname: str, kind: Literal["dataset", "grid", None] = None |
| 1595 | + ): |
| 1596 | + """ |
| 1597 | + Read data from a virtual file and optionally cast into a GMT data container. |
| 1598 | +
|
| 1599 | + Parameters |
| 1600 | + ---------- |
| 1601 | + vfname |
| 1602 | + Name of the virtual file to read. |
| 1603 | + kind |
| 1604 | + Cast the data into a GMT data container. Valid values are ``"dataset"``, |
| 1605 | + ``"grid"`` and ``None``. If ``None``, will return a ctypes void pointer. |
| 1606 | +
|
| 1607 | + Returns |
| 1608 | + ------- |
| 1609 | + Pointer to the GMT data container. If ``kind`` is None, returns a ctypes void |
| 1610 | + pointer instead. |
| 1611 | + """ |
| 1612 | + c_read_virtualfile = self.get_libgmt_func( |
| 1613 | + "GMT_Read_VirtualFile", |
| 1614 | + argtypes=[ctp.c_void_p, ctp.c_char_p], |
| 1615 | + restype=ctp.c_void_p, |
| 1616 | + ) |
| 1617 | + pointer = c_read_virtualfile(self.session_pointer, vfname.encode()) |
| 1618 | + # The GMT C API function GMT_Read_VirtualFile returns a void pointer. It usually |
| 1619 | + # needs to be cast into a pointer to a GMT data container (e.g., _GMT_GRID or |
| 1620 | + # _GMT_DATASET). |
| 1621 | + if kind is None: # Return the ctypes void pointer |
| 1622 | + return pointer |
| 1623 | + dtype = {"dataset": _GMT_DATASET, "grid": _GMT_GRID}[kind] |
| 1624 | + return ctp.cast(pointer, ctp.POINTER(dtype)) |
| 1625 | + |
1591 | 1626 | def extract_region(self):
|
1592 | 1627 | """
|
1593 | 1628 | Extract the WESN bounding box of the currently active figure.
|
|
0 commit comments