|
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,72 @@ 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 | + Examples |
| 1608 | + -------- |
| 1609 | + >>> from pygmt.helpers import GMTTempFile |
| 1610 | + >>> from pygmt.clib import Session |
| 1611 | + >>> |
| 1612 | + >>> # Read dataset from a virtual file |
| 1613 | + >>> with GMTTempFile(suffix=".txt") as tmpfile: |
| 1614 | + ... with open(tmpfile.name, mode="w") as fp: |
| 1615 | + ... print("1.0 2.0 3.0 TEXT", file=fp) |
| 1616 | + ... with Session() as lib: |
| 1617 | + ... with lib.open_virtual_file( |
| 1618 | + ... "GMT_IS_DATASET", "GMT_IS_PLP", "GMT_OUT", None |
| 1619 | + ... ) as vfile: |
| 1620 | + ... lib.call_module("read", f"{tmpfile.name} {vfile} -Td") |
| 1621 | + ... # Read the virtual file as a void pointer |
| 1622 | + ... void_pointer = lib.read_virtualfile(vfile) |
| 1623 | + ... assert isinstance(void_pointer, int) # void pointer is an int |
| 1624 | + ... # Read the virtual file as a dataset |
| 1625 | + ... data_pointer = lib.read_virtualfile(vfile, kind="dataset") |
| 1626 | + ... assert isinstance(data_pointer, ctp.POINTER(_GMT_DATASET)) |
| 1627 | + >>> |
| 1628 | + >>> # Read grid from a virtual file |
| 1629 | + >>> with Session() as lib: |
| 1630 | + ... with lib.open_virtual_file( |
| 1631 | + ... "GMT_IS_GRID", "GMT_IS_SURFACE", "GMT_OUT", None |
| 1632 | + ... ) as vfile: |
| 1633 | + ... lib.call_module("read", f"@earth_relief_01d_g {vfile} -Tg") |
| 1634 | + ... # Read the virtual file as a void pointer |
| 1635 | + ... void_pointer = lib.read_virtualfile(vfile) |
| 1636 | + ... assert isinstance(void_pointer, int) # void pointer is an int |
| 1637 | + ... data_pointer = lib.read_virtualfile(vfile, kind="grid") |
| 1638 | + ... assert isinstance(data_pointer, ctp.POINTER(_GMT_GRID)) |
| 1639 | +
|
| 1640 | + Returns |
| 1641 | + ------- |
| 1642 | + Pointer to the GMT data container. If ``kind`` is None, returns a ctypes void |
| 1643 | + pointer instead. |
| 1644 | + """ |
| 1645 | + c_read_virtualfile = self.get_libgmt_func( |
| 1646 | + "GMT_Read_VirtualFile", |
| 1647 | + argtypes=[ctp.c_void_p, ctp.c_char_p], |
| 1648 | + restype=ctp.c_void_p, |
| 1649 | + ) |
| 1650 | + pointer = c_read_virtualfile(self.session_pointer, vfname.encode()) |
| 1651 | + # The GMT C API function GMT_Read_VirtualFile returns a void pointer. It usually |
| 1652 | + # needs to be cast into a pointer to a GMT data container (e.g., _GMT_GRID or |
| 1653 | + # _GMT_DATASET). |
| 1654 | + if kind is None: # Return the ctypes void pointer |
| 1655 | + return pointer |
| 1656 | + dtype = {"dataset": _GMT_DATASET, "grid": _GMT_GRID}[kind] |
| 1657 | + return ctp.cast(pointer, ctp.POINTER(dtype)) |
| 1658 | + |
1591 | 1659 | def extract_region(self):
|
1592 | 1660 | """
|
1593 | 1661 | Extract the WESN bounding box of the currently active figure.
|
|
0 commit comments