Skip to content

Commit 72bda44

Browse files
committed
Merge read_virtualfile and read_virtualfile_to_data into a single function
1 parent 75ba61f commit 72bda44

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

pygmt/clib/session.py

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,18 +1625,23 @@ def virtualfile_from_data(
16251625

16261626
return file_context
16271627

1628-
def read_virtualfile(self, vfname):
1628+
def read_virtualfile(self, vfname, kind=None):
16291629
"""
1630-
Read data from a virtual file.
1630+
Read data from a virtual file and cast it into a GMT data container if requested.
16311631
16321632
Parameters
16331633
----------
16341634
vfname : str
16351635
Name of the virtual file to read.
16361636
1637+
kind : str
1638+
Cast the data into a GMT data container. Choose from "grid" or
1639+
"dataset". If None, will return a ctypes void pointer.
1640+
16371641
Returns
16381642
-------
1639-
Pointer to the data, which can be casted into GMT data types.
1643+
Pointer to the GMT data container. If ``kind`` is None, returns a
1644+
ctypes void pointer instead.
16401645
"""
16411646
c_read_virtualfile = self.get_libgmt_func(
16421647
"GMT_Read_VirtualFile",
@@ -1646,29 +1651,18 @@ def read_virtualfile(self, vfname):
16461651
],
16471652
restype=ctp.c_void_p,
16481653
)
1649-
return c_read_virtualfile(self.session_pointer, vfname.encode())
1650-
1651-
def read_virtualfile_to_data(self, vfname, kind):
1652-
"""
1653-
Read a virtual file and convert to a GMT data container.
1654+
pointer = c_read_virtualfile(self.session_pointer, vfname.encode())
1655+
if kind is None: # Return the ctypes void pointer
1656+
return pointer
16541657

1655-
Parameters
1656-
----------
1657-
vfname : str
1658-
Name of the virtual file to read.
1659-
kind : str
1660-
The kind of data container to create. Choose from "grid" or
1661-
"dataset".
1662-
1663-
Returns
1664-
-------
1665-
Pointer to the GMT_GRID or GMT_DATASET data container.
1666-
"""
1658+
# The GMT C API function GMT_Read_VirtualFile returns a void pointer.
1659+
# It usually needs to be cast to a pointer to GMT data container (e.g.,
1660+
# GMT_GRID or GMT_DATASET).
16671661
type = {
16681662
"grid": GMT_GRID,
1669-
# "dataset": GMT_DATASET, # implemented in PR #2729
1663+
# "dataset": GMT_DATASET, # implemented in PR #2729
16701664
}[kind]
1671-
return ctp.cast(self.read_virtualfile(vfname), ctp.POINTER(type))
1665+
return ctp.cast(pointer, ctp.POINTER(type))
16721666

16731667
@contextmanager
16741668
def virtualfile_to_data(self, kind):

0 commit comments

Comments
 (0)