Skip to content

Commit 4b2bf85

Browse files
obaneyMeghan Jonesactions-bot
authored
pygmt.surface: Deprecate parameter "outfile" to "outgrid" (remove in v0.7.0) (#1458)
* Rename outfile to outgrid in surface * Switch outfile to outgrid in test file * Update pygmt/tests/test_surface.py Co-authored-by: Meghan Jones <[email protected]> Co-authored-by: actions-bot <[email protected]>
1 parent 2e74fa1 commit 4b2bf85

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

pygmt/src/surface.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
GMTTempFile,
1010
build_arg_string,
1111
data_kind,
12+
deprecate_parameter,
1213
dummy_context,
1314
fmt_docstring,
1415
kwargs_to_strings,
@@ -17,10 +18,11 @@
1718

1819

1920
@fmt_docstring
21+
@deprecate_parameter("outfile", "outgrid", "v0.5.0", remove_version="v0.7.0")
2022
@use_alias(
2123
I="spacing",
2224
R="region",
23-
G="outfile",
25+
G="outgrid",
2426
V="verbose",
2527
a="aspatial",
2628
f="coltypes",
@@ -60,7 +62,7 @@ def surface(x=None, y=None, z=None, data=None, **kwargs):
6062
*xmin/xmax/ymin/ymax*\[**+r**][**+u**\ *unit*].
6163
Specify the region of interest.
6264
63-
outfile : str
65+
outgrid : str
6466
Optional. The file name for the output netcdf file with extension .nc
6567
to store the grid in.
6668
@@ -72,11 +74,11 @@ def surface(x=None, y=None, z=None, data=None, **kwargs):
7274
Returns
7375
-------
7476
ret: xarray.DataArray or None
75-
Return type depends on whether the ``outfile`` parameter is set:
77+
Return type depends on whether the ``outgrid`` parameter is set:
7678
77-
- :class:`xarray.DataArray`: if ``outfile`` is not set
78-
- None if ``outfile`` is set (grid output will be stored in file set by
79-
``outfile``)
79+
- :class:`xarray.DataArray`: if ``outgrid`` is not set
80+
- None if ``outgrid`` is set (grid output will be stored in file set by
81+
``outgrid``)
8082
"""
8183
kind = data_kind(data, x, y, z)
8284
if kind == "vectors" and z is None:
@@ -93,17 +95,17 @@ def surface(x=None, y=None, z=None, data=None, **kwargs):
9395
else:
9496
raise GMTInvalidInput("Unrecognized data type: {}".format(type(data)))
9597
with file_context as infile:
96-
if "G" not in kwargs.keys(): # if outfile is unset, output to tmpfile
98+
if "G" not in kwargs.keys(): # if outgrid is unset, output to tmpfile
9799
kwargs.update({"G": tmpfile.name})
98-
outfile = kwargs["G"]
100+
outgrid = kwargs["G"]
99101
arg_str = " ".join([infile, build_arg_string(kwargs)])
100102
lib.call_module(module="surface", args=arg_str)
101103

102-
if outfile == tmpfile.name: # if user did not set outfile, return DataArray
103-
with xr.open_dataarray(outfile) as dataarray:
104+
if outgrid == tmpfile.name: # if user did not set outgrid, return DataArray
105+
with xr.open_dataarray(outgrid) as dataarray:
104106
result = dataarray.load()
105107
_ = result.gmt # load GMTDataArray accessor information
106-
elif outfile != tmpfile.name: # if user sets an outfile, return None
108+
elif outgrid != tmpfile.name: # if user sets an outgrid, return None
107109
result = None
108110

109111
return result

pygmt/tests/test_surface.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,34 +82,55 @@ def test_surface_wrong_kind_of_input(ship_data):
8282
surface(data=data, spacing="5m", region=[245, 255, 20, 30])
8383

8484

85-
def test_surface_with_outfile_param(ship_data):
85+
def test_surface_with_outgrid_param(ship_data):
8686
"""
8787
Run surface with the -Goutputfile.nc parameter.
8888
"""
8989
data = ship_data.values # convert pandas.DataFrame to numpy.ndarray
9090
try:
9191
output = surface(
92-
data=data, spacing="5m", region=[245, 255, 20, 30], outfile=TEMP_GRID
92+
data=data, spacing="5m", region=[245, 255, 20, 30], outgrid=TEMP_GRID
9393
)
94-
assert output is None # check that output is None since outfile is set
95-
assert os.path.exists(path=TEMP_GRID) # check that outfile exists at path
94+
assert output is None # check that output is None since outgrid is set
95+
assert os.path.exists(path=TEMP_GRID) # check that outgrid exists at path
9696
with xr.open_dataarray(TEMP_GRID) as grid:
9797
assert isinstance(grid, xr.DataArray) # ensure netcdf grid loads ok
9898
finally:
9999
os.remove(path=TEMP_GRID)
100100
return output
101101

102102

103+
def test_surface_deprecate_outfile_to_outgrid(ship_data):
104+
"""
105+
Make sure that the old parameter "outfile" is supported and it reports a
106+
warning.
107+
"""
108+
with pytest.warns(expected_warning=FutureWarning) as record:
109+
data = ship_data.values # convert pandas.DataFrame to numpy.ndarray
110+
try:
111+
output = surface(
112+
data=data, spacing="5m", region=[245, 255, 20, 30], outfile=TEMP_GRID
113+
)
114+
assert output is None # check that output is None since outfile is set
115+
assert os.path.exists(path=TEMP_GRID) # check that file exists at path
116+
117+
with xr.open_dataarray(TEMP_GRID) as grid:
118+
assert isinstance(grid, xr.DataArray) # ensure netcdf grid loads ok
119+
finally:
120+
os.remove(path=TEMP_GRID)
121+
assert len(record) == 1 # check that only one warning was raised
122+
123+
103124
def test_surface_short_aliases(ship_data):
104125
"""
105126
Run surface using short aliases -I for spacing, -R for region, -G for
106-
outfile.
127+
outgrid.
107128
"""
108129
data = ship_data.values # convert pandas.DataFrame to numpy.ndarray
109130
try:
110131
output = surface(data=data, I="5m", R=[245, 255, 20, 30], G=TEMP_GRID)
111-
assert output is None # check that output is None since outfile is set
112-
assert os.path.exists(path=TEMP_GRID) # check that outfile exists at path
132+
assert output is None # check that output is None since outgrid is set
133+
assert os.path.exists(path=TEMP_GRID) # check that outgrid exists at path
113134
with xr.open_dataarray(TEMP_GRID) as grid:
114135
assert isinstance(grid, xr.DataArray) # ensure netcdf grid loads ok
115136
finally:

0 commit comments

Comments
 (0)