diff --git a/pygmt/clib/session.py b/pygmt/clib/session.py index eedb4572992..b54ad76f887 100644 --- a/pygmt/clib/session.py +++ b/pygmt/clib/session.py @@ -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 ): diff --git a/pygmt/tests/test_clib_virtualfiles.py b/pygmt/tests/test_clib_virtualfiles.py index 7e7822f99e5..26ebfc5d379 100644 --- a/pygmt/tests/test_clib_virtualfiles.py +++ b/pygmt/tests/test_clib_virtualfiles.py @@ -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": 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]