Skip to content

Commit 75ba61f

Browse files
committed
Use read_virtualfile_to_data to hide the technical details
1 parent 26f71ab commit 75ba61f

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

pygmt/clib/session.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import warnings
1111
from contextlib import contextmanager, nullcontext
1212

13+
from pygmt.datatypes import GMT_GRID
1314
import numpy as np
1415
import pandas as pd
1516
from packaging.version import Version
@@ -1647,6 +1648,28 @@ def read_virtualfile(self, vfname):
16471648
)
16481649
return c_read_virtualfile(self.session_pointer, vfname.encode())
16491650

1651+
def read_virtualfile_to_data(self, vfname, kind):
1652+
"""
1653+
Read a virtual file and convert to a GMT data container.
1654+
1655+
Parameters
1656+
----------
1657+
vfname : str
1658+
Name of the virtual file to read.
1659+
kind : str
1660+
The kind of data container to create. Choose from "grid" or
1661+
"dataset".
1662+
1663+
Returns
1664+
-------
1665+
Pointer to the GMT_GRID or GMT_DATASET data container.
1666+
"""
1667+
type = {
1668+
"grid": GMT_GRID,
1669+
# "dataset": GMT_DATASET, # implemented in PR #2729
1670+
}[kind]
1671+
return ctp.cast(self.read_virtualfile(vfname), ctp.POINTER(type))
1672+
16501673
@contextmanager
16511674
def virtualfile_to_data(self, kind):
16521675
"""
@@ -1703,8 +1726,6 @@ def _add_pad(x):
17031726
np.zeros((2, x.shape[1] + 4)),
17041727
]
17051728

1706-
from pygmt.datatypes import GMT_GRID
1707-
17081729
family = "GMT_IS_GRID"
17091730
geometry = "GMT_IS_SURFACE"
17101731
matrix, region, inc = dataarray_to_matrix(xrgrid)

pygmt/datatypes.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@
77

88
import numpy as np
99
import xarray as xr
10-
from pygmt.clib.session import Session
11-
12-
# Lengths of grid header variables
13-
with Session() as lib:
14-
GMT_GRID_UNIT_LEN80 = lib["GMT_GRID_UNIT_LEN80"]
15-
GMT_GRID_TITLE_LEN80 = lib["GMT_GRID_TITLE_LEN80"]
16-
GMT_GRID_COMMAND_LEN320 = lib["GMT_GRID_COMMAND_LEN320"]
17-
GMT_GRID_REMARK_LEN160 = lib["GMT_GRID_REMARK_LEN160"]
10+
11+
# from pygmt.clib.session import Session
12+
13+
# # Lengths of grid header variables
14+
# with Session() as lib:
15+
# GMT_GRID_UNIT_LEN80 = lib["GMT_GRID_UNIT_LEN80"]
16+
# GMT_GRID_TITLE_LEN80 = lib["GMT_GRID_TITLE_LEN80"]
17+
# GMT_GRID_COMMAND_LEN320 = lib["GMT_GRID_COMMAND_LEN320"]
18+
# GMT_GRID_REMARK_LEN160 = lib["GMT_GRID_REMARK_LEN160"]
19+
# Ideally we should be able to get the lengths of grid header variables
20+
# from GMT shared library, but it cause cyclic import error.
21+
# So we hardcode the values here.
22+
GMT_GRID_UNIT_LEN80 = 80
23+
GMT_GRID_TITLE_LEN80 = 80
24+
GMT_GRID_COMMAND_LEN320 = 320
25+
GMT_GRID_REMARK_LEN160 = 160
1826

1927
# GMT uses single-precision for grids by default, but can be built to use
2028
# double-precision. Currently, only single-precision is supported.

pygmt/src/grdcut.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,5 +109,6 @@ def grdcut(grid, outgrid=None, **kwargs):
109109
if outgrid is not None:
110110
lib.call_module("write", f"{outfile} {outgrid} -Tg")
111111
return None
112-
gmtgrid = lib.read_virtualfile(outfile)
113-
return ctp.cast(gmtgrid, ctp.POINTER(GMT_GRID)).contents.to_dataarray()
112+
113+
gmtgrid = lib.read_virtualfile_to_data(outfile, kind="grid")
114+
return gmtgrid.contents.to_dataarray()

0 commit comments

Comments
 (0)