Skip to content

Commit 880bff9

Browse files
committed
Update virtualfile_to_data as done in PR #2729
1 parent 72bda44 commit 880bff9

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

pygmt/clib/session.py

+23-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import warnings
1111
from contextlib import contextmanager, nullcontext
1212

13-
from pygmt.datatypes import GMT_GRID
1413
import numpy as np
1514
import pandas as pd
1615
from packaging.version import Version
@@ -22,6 +21,7 @@
2221
vectors_to_arrays,
2322
)
2423
from pygmt.clib.loading import load_libgmt
24+
from pygmt.datatypes import GMT_GRID
2525
from pygmt.exceptions import (
2626
GMTCLibError,
2727
GMTCLibNoSessionError,
@@ -1627,7 +1627,8 @@ def virtualfile_from_data(
16271627

16281628
def read_virtualfile(self, vfname, kind=None):
16291629
"""
1630-
Read data from a virtual file and cast it into a GMT data container if requested.
1630+
Read data from a virtual file and cast it into a GMT data container if
1631+
requested.
16311632
16321633
Parameters
16331634
----------
@@ -1665,27 +1666,37 @@ def read_virtualfile(self, vfname, kind=None):
16651666
return ctp.cast(pointer, ctp.POINTER(type))
16661667

16671668
@contextmanager
1668-
def virtualfile_to_data(self, kind):
1669+
def virtualfile_to_data(self, kind, fname=None):
16691670
"""
1670-
Create a virtual file for writing a GMT data container.
1671+
Create a virtual file for writing a GMT data container or yield the
1672+
output file name.
16711673
16721674
Parameters
16731675
----------
16741676
kind : str
16751677
The kind of data container to create. Choose from "grid" or
1676-
"dataset".
1678+
"dataset". It has no effect if ``fname`` is given.
1679+
1680+
fname : str or None
1681+
If given, yield the output file name instead of the virtual file.
16771682
16781683
Yields
16791684
------
16801685
vfile : str
1681-
Name of the virtual file.
1686+
Name of the virtual file or the output file name.
16821687
"""
1683-
family, geometry = {
1684-
"grid": ("GMT_IS_GRID", "GMT_IS_SURFACE"),
1685-
"dataset": ("GMT_IS_DATASET", "GMT_IS_PLP"),
1686-
}[kind]
1687-
with self.open_virtual_file(family, geometry, "GMT_OUT", None) as vfile:
1688-
yield vfile
1688+
# If fname is given, yield the output file name.
1689+
if fname is not None:
1690+
yield fname
1691+
# Otherwise, create a virtual file for writing a GMT data container.
1692+
else:
1693+
# Determine the family and geometry of the data container based on 'kind'.
1694+
family, geometry = {
1695+
"grid": ("GMT_IS_GRID", "GMT_IS_SURFACE"),
1696+
"dataset": ("GMT_IS_DATASET", "GMT_IS_PLP"),
1697+
}[kind]
1698+
with self.open_virtual_file(family, geometry, "GMT_OUT", None) as vfile:
1699+
yield vfile
16891700

16901701
@contextmanager
16911702
def virtualfile_from_gmtgrid(self, grid_pointer):

pygmt/datatypes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class GMT_GRID_HEADER(ctp.Structure):
3333
"""
3434
Structure for GMT grid header.
3535
36-
See https://docs.generic-mapping-tools.org/dev/devdocs/api.html#gmt-grid
36+
See
37+
https://docs.generic-mapping-tools.org/dev/devdocs/api.html#gmt-grid
3738
for definition.
3839
"""
3940

pygmt/src/grdcut.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,14 @@ def grdcut(grid, outgrid=None, **kwargs):
100100
with Session() as lib:
101101
with lib.virtualfile_from_data(
102102
check_kind="raster", data=grid
103-
) as infile, lib.virtualfile_to_data(kind="grid") as outfile:
104-
kwargs["G"] = f"{outfile}"
103+
) as invfile, lib.virtualfile_to_data(kind="grid", fname=outgrid) as outvfile:
104+
kwargs["G"] = f"{outvfile}"
105105
lib.call_module(
106-
module="grdcut", args=build_arg_string(kwargs, infile=infile)
106+
module="grdcut", args=build_arg_string(kwargs, infile=invfile)
107107
)
108+
108109
# Output to a file or return an xarray.DataArray object
109110
if outgrid is not None:
110-
lib.call_module("write", f"{outfile} {outgrid} -Tg")
111111
return None
112-
113-
gmtgrid = lib.read_virtualfile_to_data(outfile, kind="grid")
112+
gmtgrid = lib.read_virtualfile(outvfile, kind="grid")
114113
return gmtgrid.contents.to_dataarray()

0 commit comments

Comments
 (0)