Skip to content

Commit 10482f3

Browse files
authored
Refactor grd modules to use virtualfile_from_data (#992)
Remove duplicated if-elif-else code in grd2cpt, grdcontour, grdcut, grdfilter, grdimage, grdtrack, and grdview through the use of the virtualfile_from_data function.
1 parent 6d57b64 commit 10482f3

File tree

8 files changed

+11
-86
lines changed

8 files changed

+11
-86
lines changed

pygmt/clib/session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,7 +1411,7 @@ def virtualfile_from_data(self, check_kind=None, data=None, x=None, y=None, z=No
14111411
kind = data_kind(data, x, y, z)
14121412

14131413
if check_kind == "raster" and kind not in ("file", "grid"):
1414-
raise GMTInvalidInput(f"Unrecognized data type: {type(data)}")
1414+
raise GMTInvalidInput(f"Unrecognized data type for grid: {type(data)}")
14151415
if check_kind == "vector" and kind not in ("file", "matrix", "vectors"):
14161416
raise GMTInvalidInput(f"Unrecognized data type: {type(data)}")
14171417

pygmt/src/grd2cpt.py

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,7 @@
44

55
from pygmt.clib import Session
66
from pygmt.exceptions import GMTInvalidInput
7-
from pygmt.helpers import (
8-
build_arg_string,
9-
data_kind,
10-
dummy_context,
11-
fmt_docstring,
12-
kwargs_to_strings,
13-
use_alias,
14-
)
7+
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
158

169

1710
@fmt_docstring
@@ -169,14 +162,8 @@ def grd2cpt(grid, **kwargs):
169162
"""
170163
if "W" in kwargs and "Ww" in kwargs:
171164
raise GMTInvalidInput("Set only categorical or cyclic to True, not both.")
172-
kind = data_kind(grid)
173165
with Session() as lib:
174-
if kind == "file":
175-
file_context = dummy_context(grid)
176-
elif kind == "grid":
177-
file_context = lib.virtualfile_from_grid(grid)
178-
else:
179-
raise GMTInvalidInput(f"Unrecognized data type: {type(grid)}")
166+
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
180167
with file_context as infile:
181168
if "H" not in kwargs.keys(): # if no output is set
182169
arg_str = " ".join([infile, build_arg_string(kwargs)])

pygmt/src/grdcontour.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@
22
grdcontour - Plot a contour figure.
33
"""
44
from pygmt.clib import Session
5-
from pygmt.exceptions import GMTInvalidInput
6-
from pygmt.helpers import (
7-
build_arg_string,
8-
data_kind,
9-
dummy_context,
10-
fmt_docstring,
11-
kwargs_to_strings,
12-
use_alias,
13-
)
5+
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
146

157

168
@fmt_docstring
@@ -103,14 +95,8 @@ def grdcontour(self, grid, **kwargs):
10395
{t}
10496
"""
10597
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
106-
kind = data_kind(grid, None, None)
10798
with Session() as lib:
108-
if kind == "file":
109-
file_context = dummy_context(grid)
110-
elif kind == "grid":
111-
file_context = lib.virtualfile_from_grid(grid)
112-
else:
113-
raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid)))
99+
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
114100
with file_context as fname:
115101
arg_str = " ".join([fname, build_arg_string(kwargs)])
116102
lib.call_module("grdcontour", arg_str)

pygmt/src/grdcut.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
import xarray as xr
66
from pygmt.clib import Session
7-
from pygmt.exceptions import GMTInvalidInput
87
from pygmt.helpers import (
98
GMTTempFile,
109
build_arg_string,
11-
data_kind,
12-
dummy_context,
1310
fmt_docstring,
1411
kwargs_to_strings,
1512
use_alias,
@@ -89,17 +86,9 @@ def grdcut(grid, **kwargs):
8986
- None if ``outgrid`` is set (grid output will be stored in file set by
9087
``outgrid``)
9188
"""
92-
kind = data_kind(grid)
93-
9489
with GMTTempFile(suffix=".nc") as tmpfile:
9590
with Session() as lib:
96-
if kind == "file":
97-
file_context = dummy_context(grid)
98-
elif kind == "grid":
99-
file_context = lib.virtualfile_from_grid(grid)
100-
else:
101-
raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid)))
102-
91+
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
10392
with file_context as infile:
10493
if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
10594
kwargs.update({"G": tmpfile.name})

pygmt/src/grdfilter.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
import xarray as xr
66
from pygmt.clib import Session
7-
from pygmt.exceptions import GMTInvalidInput
87
from pygmt.helpers import (
98
GMTTempFile,
109
build_arg_string,
11-
data_kind,
12-
dummy_context,
1310
fmt_docstring,
1411
kwargs_to_strings,
1512
use_alias,
@@ -143,17 +140,9 @@ def grdfilter(grid, **kwargs):
143140
>>> grid = pygmt.datasets.load_earth_relief()
144141
>>> smooth_field = pygmt.grdfilter(grid=grid, filter="g600", distance="4")
145142
"""
146-
kind = data_kind(grid)
147-
148143
with GMTTempFile(suffix=".nc") as tmpfile:
149144
with Session() as lib:
150-
if kind == "file":
151-
file_context = dummy_context(grid)
152-
elif kind == "grid":
153-
file_context = lib.virtualfile_from_grid(grid)
154-
else:
155-
raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid)))
156-
145+
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
157146
with file_context as infile:
158147
if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
159148
kwargs.update({"G": tmpfile.name})

pygmt/src/grdimage.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,7 @@
22
grdimage - Plot grids or images.
33
"""
44
from pygmt.clib import Session
5-
from pygmt.exceptions import GMTInvalidInput
6-
from pygmt.helpers import (
7-
build_arg_string,
8-
data_kind,
9-
dummy_context,
10-
fmt_docstring,
11-
kwargs_to_strings,
12-
use_alias,
13-
)
5+
from pygmt.helpers import build_arg_string, fmt_docstring, kwargs_to_strings, use_alias
146

157

168
@fmt_docstring
@@ -157,14 +149,8 @@ def grdimage(self, grid, **kwargs):
157149
{x}
158150
"""
159151
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
160-
kind = data_kind(grid, None, None)
161152
with Session() as lib:
162-
if kind == "file":
163-
file_context = dummy_context(grid)
164-
elif kind == "grid":
165-
file_context = lib.virtualfile_from_grid(grid)
166-
else:
167-
raise GMTInvalidInput("Unrecognized data type: {}".format(type(grid)))
153+
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
168154
with file_context as fname:
169155
arg_str = " ".join([fname, build_arg_string(kwargs)])
170156
lib.call_module("grdimage", arg_str)

pygmt/src/grdtrack.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,7 @@ def grdtrack(points, grid, newcolname=None, outfile=None, **kwargs):
8484
raise GMTInvalidInput(f"Unrecognized data type {type(points)}")
8585

8686
# Store the xarray.DataArray grid in virtualfile
87-
if data_kind(grid) == "grid":
88-
grid_context = lib.virtualfile_from_grid(grid)
89-
elif data_kind(grid) == "file":
90-
grid_context = dummy_context(grid)
91-
else:
92-
raise GMTInvalidInput(f"Unrecognized data type {type(grid)}")
87+
grid_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
9388

9489
# Run grdtrack on the temporary (csv) points table
9590
# and (netcdf) grid virtualfile

pygmt/src/grdview.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pygmt.helpers import (
99
build_arg_string,
1010
data_kind,
11-
dummy_context,
1211
fmt_docstring,
1312
kwargs_to_strings,
1413
use_alias,
@@ -112,14 +111,8 @@ def grdview(self, grid, **kwargs):
112111
{t}
113112
"""
114113
kwargs = self._preprocess(**kwargs) # pylint: disable=protected-access
115-
kind = data_kind(grid, None, None)
116114
with Session() as lib:
117-
if kind == "file":
118-
file_context = dummy_context(grid)
119-
elif kind == "grid":
120-
file_context = lib.virtualfile_from_grid(grid)
121-
else:
122-
raise GMTInvalidInput(f"Unrecognized data type for grid: {type(grid)}")
115+
file_context = lib.virtualfile_from_data(check_kind="raster", data=grid)
123116

124117
with contextlib.ExitStack() as stack:
125118
if "G" in kwargs: # deal with kwargs["G"] if drapegrid is xr.DataArray

0 commit comments

Comments
 (0)