@@ -1613,7 +1613,7 @@ def virtualfile_from_data( # noqa: PLR0912
1613
1613
def virtualfile_out (
1614
1614
self , kind : Literal ["dataset" , "grid" ] = "dataset" , fname : str | None = None
1615
1615
):
1616
- """
1616
+ r """
1617
1617
Create a virtual file or an actual file for storing output data.
1618
1618
1619
1619
If ``fname`` is not given, a virtual file will be created to store the output
@@ -1642,23 +1642,25 @@ def virtualfile_out(
1642
1642
>>> from pygmt.datatypes import _GMT_DATASET
1643
1643
>>> from pygmt.helpers import GMTTempFile
1644
1644
>>>
1645
- >>> # Create a virtual file for storing the output table.
1646
1645
>>> with GMTTempFile(suffix=".txt") as tmpfile:
1647
1646
... with open(tmpfile.name, mode="w") as fp:
1648
1647
... print("1.0 2.0 3.0 TEXT", file=fp)
1648
+ ...
1649
+ ... # Create a virtual file for storing the output table.
1649
1650
... with Session() as lib:
1650
1651
... with lib.virtualfile_out(kind="dataset") as vouttbl:
1651
1652
... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
1652
1653
... ds = lib.read_virtualfile(vouttbl, kind="dataset")
1653
- >>> isinstance(ds.contents, _GMT_DATASET)
1654
- True
1655
- >>> # Write data to an actual file without creating a virtual file.
1656
- >>> with GMTTempFile(suffix=".nc") as tmpfile:
1654
+ ... assert isinstance(ds.contents, _GMT_DATASET)
1655
+ ...
1656
+ ... # Write data to an actual file without creating a virtual file.
1657
1657
... with Session() as lib:
1658
- ... with lib.virtualfile_out(fname=tmpfile.name) as voutgrd:
1659
- ... assert voutgrd == tmpfile.name
1660
- ... lib.call_module("read", f"@earth_relief_01d_g {voutgrd} -Tg")
1661
- ... assert Path(voutgrd).stat().st_size > 0
1658
+ ... with lib.virtualfile_out(fname=tmpfile.name) as vouttbl:
1659
+ ... assert vouttbl == tmpfile.name
1660
+ ... lib.call_module("read", f"{tmpfile.name} {vouttbl} -Td")
1661
+ ... with open(vouttbl, mode="r") as fp:
1662
+ ... line = fp.readline()
1663
+ ... assert line == "1\t2\t3\tTEXT\n"
1662
1664
"""
1663
1665
if fname is not None : # Yield the actual file name.
1664
1666
yield fname
0 commit comments