Skip to content

Commit

Permalink
Merge read_virtualfile and read_virtualfile_to_data into a single fun…
Browse files Browse the repository at this point in the history
…ction
  • Loading branch information
seisman committed Oct 11, 2023
1 parent 75ba61f commit 72bda44
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1625,18 +1625,23 @@ def virtualfile_from_data(

return file_context

def read_virtualfile(self, vfname):
def read_virtualfile(self, vfname, kind=None):
"""
Read data from a virtual file.
Read data from a virtual file and cast it into a GMT data container if requested.
Parameters
----------
vfname : str
Name of the virtual file to read.
kind : str
Cast the data into a GMT data container. Choose from "grid" or
"dataset". If None, will return a ctypes void pointer.
Returns
-------
Pointer to the data, which can be casted into GMT data types.
Pointer to the GMT data container. If ``kind`` is None, returns a
ctypes void pointer instead.
"""
c_read_virtualfile = self.get_libgmt_func(
"GMT_Read_VirtualFile",
Expand All @@ -1646,29 +1651,18 @@ def read_virtualfile(self, vfname):
],
restype=ctp.c_void_p,
)
return c_read_virtualfile(self.session_pointer, vfname.encode())

def read_virtualfile_to_data(self, vfname, kind):
"""
Read a virtual file and convert to a GMT data container.
pointer = c_read_virtualfile(self.session_pointer, vfname.encode())
if kind is None: # Return the ctypes void pointer
return pointer

Parameters
----------
vfname : str
Name of the virtual file to read.
kind : str
The kind of data container to create. Choose from "grid" or
"dataset".
Returns
-------
Pointer to the GMT_GRID or GMT_DATASET data container.
"""
# The GMT C API function GMT_Read_VirtualFile returns a void pointer.
# It usually needs to be cast to a pointer to GMT data container (e.g.,
# GMT_GRID or GMT_DATASET).
type = {
"grid": GMT_GRID,
# "dataset": GMT_DATASET, # implemented in PR #2729
# "dataset": GMT_DATASET, # implemented in PR #2729
}[kind]
return ctp.cast(self.read_virtualfile(vfname), ctp.POINTER(type))
return ctp.cast(pointer, ctp.POINTER(type))

@contextmanager
def virtualfile_to_data(self, kind):
Expand Down

0 comments on commit 72bda44

Please sign in to comment.