From 1d9dc100b34e83b6f6d2b794582782d88c70c481 Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 2 Oct 2023 22:05:50 +0800 Subject: [PATCH 1/2] pygmt.grdtrack: Simplify the handling of optional virtual files --- pygmt/src/grdtrack.py | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/pygmt/src/grdtrack.py b/pygmt/src/grdtrack.py index 62380105407..616ea14ceb1 100644 --- a/pygmt/src/grdtrack.py +++ b/pygmt/src/grdtrack.py @@ -295,31 +295,18 @@ def grdtrack(grid, points=None, newcolname=None, outfile=None, **kwargs): with GMTTempFile(suffix=".csv") as tmpfile: with Session() as lib: - # Store the xarray.DataArray grid in virtualfile - grid_context = lib.virtualfile_from_data(check_kind="raster", data=grid) - - with grid_context as grdfile: - kwargs.update({"G": grdfile}) + with lib.virtualfile_from_data( + check_kind="raster", data=grid + ) as grdfile, lib.virtualfile_from_data( + check_kind="vector", data=points, required_data=False + ) as csvfile: + kwargs["G"] = grdfile if outfile is None: # Output to tmpfile if outfile is not set outfile = tmpfile.name - - if points is not None: - # Choose how data will be passed into the module - table_context = lib.virtualfile_from_data( - check_kind="vector", data=points - ) - with table_context as csvfile: - lib.call_module( - module="grdtrack", - args=build_arg_string( - kwargs, infile=csvfile, outfile=outfile - ), - ) - else: - lib.call_module( - module="grdtrack", - args=build_arg_string(kwargs, outfile=outfile), - ) + lib.call_module( + module="grdtrack", + args=build_arg_string(kwargs, infile=csvfile, outfile=outfile), + ) # Read temporary csv output to a pandas table if outfile == tmpfile.name: # if user did not set outfile, return pd.DataFrame From 67b8ed7ab1afb006a5008893f55cd693a783b71d Mon Sep 17 00:00:00 2001 From: Dongdong Tian Date: Mon, 9 Oct 2023 09:47:51 +0800 Subject: [PATCH 2/2] Remove too-many-branches pylint directive --- pygmt/src/grdtrack.py | 1 - 1 file changed, 1 deletion(-) diff --git a/pygmt/src/grdtrack.py b/pygmt/src/grdtrack.py index 616ea14ceb1..d9b005883b9 100644 --- a/pygmt/src/grdtrack.py +++ b/pygmt/src/grdtrack.py @@ -283,7 +283,6 @@ def grdtrack(grid, points=None, newcolname=None, outfile=None, **kwargs): ... points=points, grid=grid, newcolname="bathymetry" ... ) """ - # pylint: disable=too-many-branches if points is not None and kwargs.get("E") is not None: raise GMTInvalidInput("Can't set both 'points' and 'profile'.")