Skip to content

Commit 6be1a10

Browse files
committed
Wrap GMT_Read_VirtualFile
1 parent 89a51c0 commit 6be1a10

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed

doc/api/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -308,5 +308,6 @@ Low level access (these are mostly used by the :mod:`pygmt.clib` package):
308308
clib.Session.put_vector
309309
clib.Session.write_data
310310
clib.Session.open_virtual_file
311+
clib.Session.read_virtualfile
311312
clib.Session.extract_region
312313
clib.Session.get_libgmt_func

pygmt/clib/session.py

+35
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import pathlib
1010
import sys
1111
import warnings
12+
from typing import Literal
1213

1314
import numpy as np
1415
import pandas as pd
@@ -21,6 +22,7 @@
2122
vectors_to_arrays,
2223
)
2324
from pygmt.clib.loading import load_libgmt
25+
from pygmt.datatypes import _GMT_DATASET, _GMT_GRID
2426
from pygmt.exceptions import (
2527
GMTCLibError,
2628
GMTCLibNoSessionError,
@@ -1588,6 +1590,39 @@ def virtualfile_from_data( # noqa: PLR0912
15881590

15891591
return file_context
15901592

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+
15911626
def extract_region(self):
15921627
"""
15931628
Extract the WESN bounding box of the currently active figure.

pygmt/datatypes/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""
2+
Wrappers for GMT data types.
3+
"""
4+
from pygmt.datatypes.dataset import _GMT_DATASET
5+
from pygmt.datatypes.grid import _GMT_GRID

pygmt/datatypes/dataset.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Wrapper for the GMT_DATASET data type.
3+
"""
4+
5+
6+
class _GMT_DATASET: # noqa: N801
7+
pass

pygmt/datatypes/grid.py

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"""
2+
Wrapper for the GMT_GRID data type.
3+
"""
4+
5+
6+
class _GMT_GRID: # noqa: N801
7+
pass

0 commit comments

Comments
 (0)