Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pygmt.grdtrack: Simplify the handling of optional virtual files #2708

Merged
merged 6 commits into from
Oct 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions pygmt/src/grdtrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'.")

Expand All @@ -295,31 +294,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
Expand Down