Skip to content

Commit 26f71ab

Browse files
committed
Generalize virtualfile_to_gmtgrid to virtualfile_to_data so it works for both grid and dataset
1 parent 0042728 commit 26f71ab

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

doc/api/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ conversion of Python variables to GMT virtual files:
290290
clib.Session.virtualfile_from_grid
291291
clib.Session.virtualfile_from_gmtgrid
292292
clib.Session.virtualfile_from_xrgrid
293-
clib.Session.virtualfile_to_gmtgrid
293+
clib.Session.virtualfile_to_data
294294

295295
Low level access (these are mostly used by the :mod:`pygmt.clib` package):
296296

pygmt/clib/session.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -1648,29 +1648,37 @@ def read_virtualfile(self, vfname):
16481648
return c_read_virtualfile(self.session_pointer, vfname.encode())
16491649

16501650
@contextmanager
1651-
def virtualfile_from_gmtgrid(self, grid_pointer):
1651+
def virtualfile_to_data(self, kind):
16521652
"""
1653-
Create a virtual file for reading a GMT_GRID object.
1653+
Create a virtual file for writing a GMT data container.
16541654
1655-
Parameter
1656-
---------
1657-
grid_pointer : ctp.POINTER(GMT_GRID)
1658-
Pointer to a GMT_GRID object.
1655+
Parameters
1656+
----------
1657+
kind : str
1658+
The kind of data container to create. Choose from "grid" or
1659+
"dataset".
16591660
16601661
Yields
16611662
------
16621663
vfile : str
16631664
Name of the virtual file.
16641665
"""
1665-
family = "GMT_IS_GRID"
1666-
geometry = "GMT_IS_SURFACE"
1667-
with self.open_virtual_file(family, geometry, "GMT_IN", grid_pointer) as vfile:
1666+
family, geometry = {
1667+
"grid": ("GMT_IS_GRID", "GMT_IS_SURFACE"),
1668+
"dataset": ("GMT_IS_DATASET", "GMT_IS_PLP"),
1669+
}[kind]
1670+
with self.open_virtual_file(family, geometry, "GMT_OUT", None) as vfile:
16681671
yield vfile
16691672

16701673
@contextmanager
1671-
def virtualfile_to_gmtgrid(self):
1674+
def virtualfile_from_gmtgrid(self, grid_pointer):
16721675
"""
1673-
Create a virtual file for writing a GMT_GRID object.
1676+
Create a virtual file for reading a GMT_GRID object.
1677+
1678+
Parameter
1679+
---------
1680+
grid_pointer : ctp.POINTER(GMT_GRID)
1681+
Pointer to a GMT_GRID object.
16741682
16751683
Yields
16761684
------
@@ -1679,7 +1687,7 @@ def virtualfile_to_gmtgrid(self):
16791687
"""
16801688
family = "GMT_IS_GRID"
16811689
geometry = "GMT_IS_SURFACE"
1682-
with self.open_virtual_file(family, geometry, "GMT_OUT", None) as vfile:
1690+
with self.open_virtual_file(family, geometry, "GMT_IN", grid_pointer) as vfile:
16831691
yield vfile
16841692

16851693
@contextmanager

pygmt/datatypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class GMT_GRID(ctp.Structure):
8888
8989
>>> with Session() as lib:
9090
... # create a virtual file for output
91-
... with lib.virtualfile_to_gmtgrid() as vfile:
91+
... with lib.virtualfile_to_data(kind="grid") as vfile:
9292
... # read in a grid file and output to the virtual file
9393
... lib.call_module("read", f"@static_earth_relief.nc {vfile} -Tg")
9494
... # read the data in the virtual file and cast the data into a pointer to GMT_GRID

pygmt/src/grdcut.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ 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_gmtgrid() as outfile:
103+
) as infile, lib.virtualfile_to_data(kind="grid") as outfile:
104104
kwargs["G"] = f"{outfile}"
105105
lib.call_module(
106106
module="grdcut", args=build_arg_string(kwargs, infile=infile)

0 commit comments

Comments
 (0)