Skip to content

Commit

Permalink
Wrap GMT_Inquire_VirtualFile to get the family of virtualfiles (#3152)
Browse files Browse the repository at this point in the history
Co-authored-by: Yvonne Fröhlich <94163266+yvonnefroehlich@users.noreply.github.com>
  • Loading branch information
seisman and yvonnefroehlich authored Apr 7, 2024
1 parent 6069ebc commit a32049d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pygmt/clib/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -1709,6 +1709,35 @@ def virtualfile_out(
with self.open_virtualfile(family, geometry, "GMT_OUT", None) as vfile:
yield vfile

def inquire_virtualfile(self, vfname: str) -> int:
"""
Get the family of a virtual file.
Parameters
----------
vfname
Name of the virtual file to inquire.
Returns
-------
family
The integer value for the family of the virtual file.
Examples
--------
>>> from pygmt.clib import Session
>>> with Session() as lib:
... with lib.virtualfile_out(kind="dataset") as vfile:
... family = lib.inquire_virtualfile(vfile)
... assert family == lib["GMT_IS_DATASET"]
"""
c_inquire_virtualfile = self.get_libgmt_func(
"GMT_Inquire_VirtualFile",
argtypes=[ctp.c_void_p, ctp.c_char_p],
restype=ctp.c_uint,
)
return c_inquire_virtualfile(self.session_pointer, vfname.encode())

def read_virtualfile(
self, vfname: str, kind: Literal["dataset", "grid", None] = None
):
Expand Down
28 changes: 28 additions & 0 deletions pygmt/tests/test_clib_virtualfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,31 @@ def test_virtualfile_from_vectors_arraylike():
bounds = "\t".join([f"<{min(i):.0f}/{max(i):.0f}>" for i in (x, y, z)])
expected = f"<vector memory>: N = {size}\t{bounds}\n"
assert output == expected


def test_inquire_virtualfile():
"""
Test that the inquire_virtualfile method returns the correct family.
Currently, only output virtual files are tested.
"""
with clib.Session() as lib:
for family in [
"GMT_IS_DATASET",
"GMT_IS_DATASET|GMT_VIA_MATRIX",
"GMT_IS_DATASET|GMT_VIA_VECTOR",
]:
with lib.open_virtualfile(
family, "GMT_IS_PLP", "GMT_OUT|GMT_IS_REFERENCE", None
) as vfile:
assert lib.inquire_virtualfile(vfile) == lib["GMT_IS_DATASET"]

for family, geometry in [
("GMT_IS_GRID", "GMT_IS_SURFACE"),
("GMT_IS_IMAGE", "GMT_IS_SURFACE"),
("GMT_IS_CUBE", "GMT_IS_VOLUME"),
("GMT_IS_PALETTE", "GMT_IS_NONE"),
("GMT_IS_POSTSCRIPT", "GMT_IS_NONE"),
]:
with lib.open_virtualfile(family, geometry, "GMT_OUT", None) as vfile:
assert lib.inquire_virtualfile(vfile) == lib[family]

0 comments on commit a32049d

Please sign in to comment.