Skip to content

Commit 39c85b0

Browse files
committed
Allow only str or None inputs to outgrid parameter
Xref #1807
1 parent 7ae5f6a commit 39c85b0

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

pygmt/src/triangulate.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def regular_grid( # pylint: disable=too-many-arguments,too-many-locals
205205
{J}
206206
{R}
207207
{I}
208-
outgrid : str or bool or None
208+
outgrid : str or None
209209
The name of the output netCDF file with extension .nc to store the
210210
grid in. The interpolation is performed in the original
211211
coordinates, so if your triangles are close to the poles you are
@@ -228,17 +228,22 @@ def regular_grid( # pylint: disable=too-many-arguments,too-many-locals
228228
ret: xarray.DataArray or None
229229
Return type depends on whether the ``outgrid`` parameter is set:
230230
231-
- xarray.DataArray if ``outgrid`` is True or None (default)
231+
- xarray.DataArray if ``outgrid`` is None (default)
232232
- None if ``outgrid`` is a str (grid output is stored in
233233
``outgrid``)
234234
"""
235235
# Return an xarray.DataArray if ``outgrid`` is not set
236236
with GMTTempFile(suffix=".nc") as tmpfile:
237237
if isinstance(outgrid, str):
238238
output_type = "file"
239-
else:
239+
elif outgrid is None:
240240
output_type = "xarray"
241241
outgrid = tmpfile.name
242+
else:
243+
raise GMTInvalidInput(
244+
"'outgrid' should be a proper file name or `None`"
245+
)
246+
242247
return triangulate._triangulate(
243248
data=data,
244249
x=x,

pygmt/tests/test_triangulate.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,13 @@ def test_delaunay_triples_invalid_format(dataframe):
131131
triangulate.delaunay_triples(data=dataframe, output_type="pandas", header="o+c")
132132

133133

134-
def test_regular_grid_with_outgrid_true(dataframe, expected_grid):
134+
def test_regular_grid_no_outgrid(dataframe, expected_grid):
135135
"""
136-
Run triangulate.regular_grid with outgrid=True and see it load into an
136+
Run triangulate.regular_grid with no set outgrid and see it load into an
137137
xarray.DataArray.
138138
"""
139139
data = dataframe.to_numpy()
140-
output = triangulate.regular_grid(
141-
data=data, spacing=1, region=[2, 4, 5, 6], outgrid=True
142-
)
140+
output = triangulate.regular_grid(data=data, spacing=1, region=[2, 4, 5, 6])
143141
assert isinstance(output, xr.DataArray)
144142
assert output.gmt.registration == 0 # Gridline registration
145143
assert output.gmt.gtype == 0 # Cartesian type
@@ -162,3 +160,12 @@ def test_regular_grid_with_outgrid_param(dataframe, expected_grid):
162160
assert grid.gmt.registration == 0 # Gridline registration
163161
assert grid.gmt.gtype == 0 # Cartesian type
164162
xr.testing.assert_allclose(a=grid, b=expected_grid)
163+
164+
165+
def test_regular_grid_invalid_format(dataframe):
166+
"""
167+
Test that triangulate.regular_grid fails with outgrid that is not None or
168+
a proper file name.
169+
"""
170+
with pytest.raises(GMTInvalidInput):
171+
triangulate.regular_grid(data=dataframe, outgrid=True)

0 commit comments

Comments
 (0)