diff --git a/pygmt/helpers/__init__.py b/pygmt/helpers/__init__.py index 5eb265e8002..bd3b9680257 100644 --- a/pygmt/helpers/__init__.py +++ b/pygmt/helpers/__init__.py @@ -21,3 +21,4 @@ launch_external_viewer, non_ascii_to_octal, ) +from pygmt.helpers.validators import validate_output_type diff --git a/pygmt/helpers/validators.py b/pygmt/helpers/validators.py new file mode 100644 index 00000000000..5d11b39f16e --- /dev/null +++ b/pygmt/helpers/validators.py @@ -0,0 +1,41 @@ +""" +Functions to check if given arguments are valid. +""" +import warnings + +from pygmt.exceptions import GMTInvalidInput + + +def validate_output_type(output_type, outfile=None): + """ + Check if the 'output_type' and 'outfile' parameters are valid. + + Parameters + ---------- + output_type : str + The type for a table output. Valid values are "file", "numpy", and + "pandas". + outfile : str + The file name for the output table file. Required if + ``output_type="file"``. + + Returns + ------- + str + The original or corrected output type. + """ + if output_type not in ["file", "numpy", "pandas"]: + raise GMTInvalidInput( + "Must specify 'output_type' either as 'file', 'numpy', or 'pandas'." + ) + if output_type == "file" and outfile is None: + raise GMTInvalidInput("Must specify 'outfile' for output_type='file'.") + if output_type != "file" and outfile is not None: + msg = ( + f"Changing 'output_type' from '{output_type}' to 'file' " + "since 'outfile' parameter is set. Please use output_type='file' " + "to silence this warning." + ) + warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) + output_type = "file" + return output_type diff --git a/pygmt/src/filter1d.py b/pygmt/src/filter1d.py index cc189a53053..1a82a88a694 100644 --- a/pygmt/src/filter1d.py +++ b/pygmt/src/filter1d.py @@ -1,12 +1,17 @@ """ filter1d - Time domain filtering of 1-D data tables """ -import warnings import pandas as pd from pygmt.clib import Session from pygmt.exceptions import GMTInvalidInput -from pygmt.helpers import GMTTempFile, build_arg_string, fmt_docstring, use_alias +from pygmt.helpers import ( + GMTTempFile, + build_arg_string, + fmt_docstring, + use_alias, + validate_output_type, +) @fmt_docstring @@ -109,18 +114,8 @@ def filter1d(data, output_type="pandas", outfile=None, **kwargs): """ if kwargs.get("F") is None: raise GMTInvalidInput("Pass a required argument to 'filter_type'.") - if output_type not in ["numpy", "pandas", "file"]: - raise GMTInvalidInput("Must specify format as either numpy, pandas, or file.") - if outfile is not None and output_type != "file": - msg = ( - f"Changing `output_type` of filter1d from '{output_type}' to 'file' " - "since `outfile` parameter is set. Please use `output_type='file'` " - "to silence this warning." - ) - warnings.warn(msg, category=RuntimeWarning, stacklevel=2) - output_type = "file" - elif output_type == "file" and outfile is None: - raise GMTInvalidInput("Must specify outfile for ASCII output.") + + output_type = validate_output_type(output_type, outfile=outfile) with GMTTempFile() as tmpfile: with Session() as lib: diff --git a/pygmt/src/grd2xyz.py b/pygmt/src/grd2xyz.py index bd3af2962b8..236290c698f 100644 --- a/pygmt/src/grd2xyz.py +++ b/pygmt/src/grd2xyz.py @@ -1,7 +1,6 @@ """ grd2xyz - Convert grid to data table """ -import warnings import pandas as pd import xarray as xr @@ -13,6 +12,7 @@ fmt_docstring, kwargs_to_strings, use_alias, + validate_output_type, ) __doctest_skip__ = ["grd2xyz"] @@ -143,21 +143,7 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs): 0 10.0 25.0 863.0 1 10.5 25.0 985.5 """ - if output_type not in ["numpy", "pandas", "file"]: - raise GMTInvalidInput( - "Must specify 'output_type' either as 'numpy', 'pandas' or 'file'." - ) - - if outfile is not None and output_type != "file": - msg = ( - f"Changing 'output_type' of grd2xyz from '{output_type}' to 'file' " - "since 'outfile' parameter is set. Please use output_type='file' " - "to silence this warning." - ) - warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) - output_type = "file" - elif outfile is None and output_type == "file": - raise GMTInvalidInput("Must specify 'outfile' for ASCII output.") + output_type = validate_output_type(output_type, outfile=outfile) if kwargs.get("o") is not None and output_type == "pandas": raise GMTInvalidInput( diff --git a/pygmt/src/grdhisteq.py b/pygmt/src/grdhisteq.py index 0d795a7dcbd..e66ebe79daa 100644 --- a/pygmt/src/grdhisteq.py +++ b/pygmt/src/grdhisteq.py @@ -1,7 +1,6 @@ """ grdhisteq - Perform histogram equalization for a grid. """ -import warnings import numpy as np import pandas as pd @@ -13,6 +12,7 @@ fmt_docstring, kwargs_to_strings, use_alias, + validate_output_type, ) from pygmt.io import load_dataarray @@ -321,23 +321,11 @@ def compute_bins( This method does a weighted histogram equalization for geographic grids to account for node area varying with latitude. """ - # Return a pandas.DataFrame if ``outfile`` is not set - if output_type not in ["numpy", "pandas", "file"]: - raise GMTInvalidInput( - "Must specify 'output_type' either as 'numpy', 'pandas' or 'file'." - ) + output_type = validate_output_type(output_type, outfile=outfile) if header is not None and output_type != "file": raise GMTInvalidInput("'header' is only allowed with output_type='file'.") - if isinstance(outfile, str) and output_type != "file": - msg = ( - f"Changing 'output_type' from '{output_type}' to 'file' " - "since 'outfile' parameter is set. Please use output_type='file' " - "to silence this warning." - ) - warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) - output_type = "file" with GMTTempFile(suffix=".txt") as tmpfile: if output_type != "file": outfile = tmpfile.name diff --git a/pygmt/src/grdvolume.py b/pygmt/src/grdvolume.py index ba4a2d871bc..77f7881b8ff 100644 --- a/pygmt/src/grdvolume.py +++ b/pygmt/src/grdvolume.py @@ -3,13 +3,13 @@ """ import pandas as pd from pygmt.clib import Session -from pygmt.exceptions import GMTInvalidInput from pygmt.helpers import ( GMTTempFile, build_arg_string, fmt_docstring, kwargs_to_strings, use_alias, + validate_output_type, ) __doctest_skip__ = ["grdvolume"] @@ -101,12 +101,7 @@ def grdvolume(grid, output_type="pandas", outfile=None, **kwargs): 3 350 2.018302e+12 5.222640e+14 258.764032 4 400 1.857370e+12 4.252699e+14 228.963499 """ - if output_type not in ["numpy", "pandas", "file"]: - raise GMTInvalidInput( - """Must specify format as either numpy, pandas, or file.""" - ) - if output_type == "file" and outfile is None: - raise GMTInvalidInput("""Must specify outfile for ASCII output.""") + output_type = validate_output_type(output_type, outfile=outfile) with GMTTempFile() as tmpfile: with Session() as lib: diff --git a/pygmt/src/triangulate.py b/pygmt/src/triangulate.py index b3578941dcd..60661b1242a 100644 --- a/pygmt/src/triangulate.py +++ b/pygmt/src/triangulate.py @@ -2,7 +2,6 @@ triangulate - Delaunay triangulation or Voronoi partitioning and gridding of Cartesian data. """ -import warnings import pandas as pd from pygmt.clib import Session @@ -13,6 +12,7 @@ fmt_docstring, kwargs_to_strings, use_alias, + validate_output_type, ) from pygmt.io import load_dataarray @@ -357,20 +357,7 @@ def delaunay_triples( # pylint: disable=too-many-arguments,too-many-locals ``triangulate`` is a Cartesian or small-geographic area operator and is unaware of periodic or polar boundary conditions. """ - # Return a pandas.DataFrame if ``outfile`` is not set - if output_type not in ["numpy", "pandas", "file"]: - raise GMTInvalidInput( - "Must specify 'output_type' either as 'numpy', 'pandas' or 'file'." - ) - - if isinstance(outfile, str) and output_type != "file": - msg = ( - f"Changing 'output_type' from '{output_type}' to 'file' " - "since 'outfile' parameter is set. Please use output_type='file' " - "to silence this warning." - ) - warnings.warn(message=msg, category=RuntimeWarning, stacklevel=2) - output_type = "file" + output_type = validate_output_type(output_type, outfile) # Return a pandas.DataFrame if ``outfile`` is not set with GMTTempFile(suffix=".txt") as tmpfile: