Skip to content

Wrap GMT_Inquire_VirtualFile to get the family of virtualfiles #3152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]