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

Use consistent names (vintbl and vingrd) for input virtual files #3082

Merged
merged 1 commit into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions pygmt/src/binstats.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ def binstats(data, **kwargs):
"""
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="vector", data=data)
with file_context as infile:
with lib.virtualfile_in(check_kind="vector", data=data) as vintbl:
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="binstats", args=build_arg_string(kwargs, infile=infile)
module="binstats", args=build_arg_string(kwargs, infile=vintbl)
)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
9 changes: 4 additions & 5 deletions pygmt/src/blockm.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,15 @@ def _blockm(block_method, data, x, y, z, outfile, **kwargs):
"""
with GMTTempFile(suffix=".csv") as tmpfile:
with Session() as lib:
table_context = lib.virtualfile_in(
with lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
)
# Run blockm* on data table
with table_context as infile:
) as vintbl:
# Run blockm* on data table
if outfile is None:
outfile = tmpfile.name
lib.call_module(
module=block_method,
args=build_arg_string(kwargs, infile=infile, outfile=outfile),
args=build_arg_string(kwargs, infile=vintbl, outfile=outfile),
)

# Read temporary csv output to a pandas table
Expand Down
7 changes: 3 additions & 4 deletions pygmt/src/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,9 @@ def contour(self, data=None, x=None, y=None, z=None, **kwargs):
kwargs = self._preprocess(**kwargs)

with Session() as lib:
file_context = lib.virtualfile_in(
with lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
)
with file_context as fname:
) as vintbl:
lib.call_module(
module="contour", args=build_arg_string(kwargs, infile=fname)
module="contour", args=build_arg_string(kwargs, infile=vintbl)
)
5 changes: 2 additions & 3 deletions pygmt/src/dimfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,11 @@ def dimfilter(grid, **kwargs):

with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="dimfilter", args=build_arg_string(kwargs, infile=infile)
module="dimfilter", args=build_arg_string(kwargs, infile=vingrd)
)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
5 changes: 2 additions & 3 deletions pygmt/src/filter1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,12 @@ def filter1d(data, output_type="pandas", outfile=None, **kwargs):

with GMTTempFile() as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="vector", data=data)
with file_context as infile:
with lib.virtualfile_in(check_kind="vector", data=data) as vintbl:
if outfile is None:
outfile = tmpfile.name
lib.call_module(
module="filter1d",
args=build_arg_string(kwargs, infile=infile, outfile=outfile),
args=build_arg_string(kwargs, infile=vintbl, outfile=outfile),
)

# Read temporary csv output to a pandas table
Expand Down
7 changes: 3 additions & 4 deletions pygmt/src/grd2cpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,13 +184,12 @@ def grd2cpt(grid, **kwargs):
if kwargs.get("W") is not None and kwargs.get("Ww") is not None:
raise GMTInvalidInput("Set only categorical or cyclic to True, not both.")
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if kwargs.get("H") is None: # if no output is set
arg_str = build_arg_string(kwargs, infile=infile)
arg_str = build_arg_string(kwargs, infile=vingrd)
else: # if output is set
outfile, kwargs["H"] = kwargs["H"], True
if not outfile or not isinstance(outfile, str):
raise GMTInvalidInput("'output' should be a proper file name.")
arg_str = build_arg_string(kwargs, infile=infile, outfile=outfile)
arg_str = build_arg_string(kwargs, infile=vingrd, outfile=outfile)
lib.call_module(module="grd2cpt", args=arg_str)
5 changes: 2 additions & 3 deletions pygmt/src/grd2xyz.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,12 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs):

with GMTTempFile() as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if outfile is None:
outfile = tmpfile.name
lib.call_module(
module="grd2xyz",
args=build_arg_string(kwargs, infile=infile, outfile=outfile),
args=build_arg_string(kwargs, infile=vingrd, outfile=outfile),
)

# Read temporary csv output to a pandas table
Expand Down
5 changes: 2 additions & 3 deletions pygmt/src/grdclip.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ def grdclip(grid, **kwargs):
"""
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="grdclip", args=build_arg_string(kwargs, infile=infile)
module="grdclip", args=build_arg_string(kwargs, infile=vingrd)
)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
5 changes: 2 additions & 3 deletions pygmt/src/grdcontour.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ def grdcontour(self, grid, **kwargs):
"""
kwargs = self._preprocess(**kwargs)
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as fname:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
lib.call_module(
module="grdcontour", args=build_arg_string(kwargs, infile=fname)
module="grdcontour", args=build_arg_string(kwargs, infile=vingrd)
)
5 changes: 2 additions & 3 deletions pygmt/src/grdcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ def grdcut(grid, **kwargs):
"""
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="grdcut", args=build_arg_string(kwargs, infile=infile)
module="grdcut", args=build_arg_string(kwargs, infile=vingrd)
)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
5 changes: 2 additions & 3 deletions pygmt/src/grdfill.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,11 @@ def grdfill(grid, **kwargs):
raise GMTInvalidInput("At least parameter 'mode' or 'L' must be specified.")
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="grdfill", args=build_arg_string(kwargs, infile=infile)
module="grdfill", args=build_arg_string(kwargs, infile=vingrd)
)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
5 changes: 2 additions & 3 deletions pygmt/src/grdfilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,11 @@ def grdfilter(grid, **kwargs):
"""
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="grdfilter", args=build_arg_string(kwargs, infile=infile)
module="grdfilter", args=build_arg_string(kwargs, infile=vingrd)
)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
5 changes: 2 additions & 3 deletions pygmt/src/grdgradient.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,11 @@ def grdgradient(grid, **kwargs):
azimuth, direction, or radiance"""
)
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="grdgradient", args=build_arg_string(kwargs, infile=infile)
module="grdgradient", args=build_arg_string(kwargs, infile=vingrd)
)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
8 changes: 4 additions & 4 deletions pygmt/src/grdimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,12 @@ def grdimage(self, grid, **kwargs):

with Session() as lib:
with (
lib.virtualfile_in(check_kind="raster", data=grid) as fname,
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
lib.virtualfile_in(
check_kind="raster", data=kwargs.get("I"), required_data=False
) as shadegrid,
) as vshadegrid,
):
kwargs["I"] = shadegrid
kwargs["I"] = vshadegrid
lib.call_module(
module="grdimage", args=build_arg_string(kwargs, infile=fname)
module="grdimage", args=build_arg_string(kwargs, infile=vingrd)
)
5 changes: 2 additions & 3 deletions pygmt/src/grdinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,10 @@ def grdinfo(grid, **kwargs):
"""
with GMTTempFile() as outfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
lib.call_module(
module="grdinfo",
args=build_arg_string(kwargs, infile=infile, outfile=outfile.name),
args=build_arg_string(kwargs, infile=vingrd, outfile=outfile.name),
)
result = outfile.read()
return result
5 changes: 2 additions & 3 deletions pygmt/src/grdproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ def grdproject(grid, **kwargs):
raise GMTInvalidInput("The projection must be specified.")
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="grdproject", args=build_arg_string(kwargs, infile=infile)
module="grdproject", args=build_arg_string(kwargs, infile=vingrd)
)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
5 changes: 2 additions & 3 deletions pygmt/src/grdsample.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ def grdsample(grid, **kwargs):
"""
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="grdsample", args=build_arg_string(kwargs, infile=infile)
module="grdsample", args=build_arg_string(kwargs, infile=vingrd)
)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
8 changes: 4 additions & 4 deletions pygmt/src/grdtrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,17 +294,17 @@ def grdtrack(grid, points=None, newcolname=None, outfile=None, **kwargs):
with GMTTempFile(suffix=".csv") as tmpfile:
with Session() as lib:
with (
lib.virtualfile_in(check_kind="raster", data=grid) as grdfile,
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
lib.virtualfile_in(
check_kind="vector", data=points, required_data=False
) as csvfile,
) as vintbl,
):
kwargs["G"] = grdfile
kwargs["G"] = vingrd
if outfile is None: # Output to tmpfile if outfile is not set
outfile = tmpfile.name
lib.call_module(
module="grdtrack",
args=build_arg_string(kwargs, infile=csvfile, outfile=outfile),
args=build_arg_string(kwargs, infile=vintbl, outfile=outfile),
)

# Read temporary csv output to a pandas table
Expand Down
8 changes: 4 additions & 4 deletions pygmt/src/grdview.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ def grdview(self, grid, **kwargs):
kwargs = self._preprocess(**kwargs)
with Session() as lib:
with (
lib.virtualfile_in(check_kind="raster", data=grid) as fname,
lib.virtualfile_in(check_kind="raster", data=grid) as vingrd,
lib.virtualfile_in(
check_kind="raster", data=kwargs.get("G"), required_data=False
) as drapegrid,
) as vdrapegrid,
):
kwargs["G"] = drapegrid
kwargs["G"] = vdrapegrid
lib.call_module(
module="grdview", args=build_arg_string(kwargs, infile=fname)
module="grdview", args=build_arg_string(kwargs, infile=vingrd)
)
5 changes: 2 additions & 3 deletions pygmt/src/grdvolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,12 @@ def grdvolume(grid, output_type="pandas", outfile=None, **kwargs):

with GMTTempFile() as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="raster", data=grid)
with file_context as infile:
with lib.virtualfile_in(check_kind="raster", data=grid) as vingrd:
if outfile is None:
outfile = tmpfile.name
lib.call_module(
module="grdvolume",
args=build_arg_string(kwargs, infile=infile, outfile=outfile),
args=build_arg_string(kwargs, infile=vingrd, outfile=outfile),
)

# Read temporary csv output to a pandas table
Expand Down
5 changes: 2 additions & 3 deletions pygmt/src/histogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ def histogram(self, data, **kwargs):
"""
kwargs = self._preprocess(**kwargs)
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="vector", data=data)
with file_context as infile:
with lib.virtualfile_in(check_kind="vector", data=data) as vintbl:
lib.call_module(
module="histogram", args=build_arg_string(kwargs, infile=infile)
module="histogram", args=build_arg_string(kwargs, infile=vintbl)
)
5 changes: 2 additions & 3 deletions pygmt/src/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,11 @@ def info(data, **kwargs):
- str if none of the above parameters are used.
"""
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="vector", data=data)
with GMTTempFile() as tmpfile:
with file_context as fname:
with lib.virtualfile_in(check_kind="vector", data=data) as vintbl:
lib.call_module(
module="info",
args=build_arg_string(kwargs, infile=fname, outfile=tmpfile.name),
args=build_arg_string(kwargs, infile=vintbl, outfile=tmpfile.name),
)
result = tmpfile.read()

Expand Down
5 changes: 2 additions & 3 deletions pygmt/src/meca.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,5 @@ def meca( # noqa: PLR0912, PLR0913, PLR0915
# Assemble -S flag
kwargs["S"] = f"{data_format}{scale}"
with Session() as lib:
file_context = lib.virtualfile_in(check_kind="vector", data=spec)
with file_context as fname:
lib.call_module(module="meca", args=build_arg_string(kwargs, infile=fname))
with lib.virtualfile_in(check_kind="vector", data=spec) as vintbl:
lib.call_module(module="meca", args=build_arg_string(kwargs, infile=vintbl))
7 changes: 3 additions & 4 deletions pygmt/src/nearneighbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,13 @@ def nearneighbor(data=None, x=None, y=None, z=None, **kwargs):
"""
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
table_context = lib.virtualfile_in(
with lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
)
with table_context as infile:
) as vintbl:
if (outgrid := kwargs.get("G")) is None:
kwargs["G"] = outgrid = tmpfile.name # output to tmpfile
lib.call_module(
module="nearneighbor", args=build_arg_string(kwargs, infile=infile)
module="nearneighbor", args=build_arg_string(kwargs, infile=vintbl)
)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None
8 changes: 3 additions & 5 deletions pygmt/src/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,7 @@ def plot( # noqa: PLR0912
kwargs[flag] = ""

with Session() as lib:
file_context = lib.virtualfile_in(
with lib.virtualfile_in(
check_kind="vector", data=data, x=x, y=y, extra_arrays=extra_arrays
)

with file_context as fname:
lib.call_module(module="plot", args=build_arg_string(kwargs, infile=fname))
) as vintbl:
lib.call_module(module="plot", args=build_arg_string(kwargs, infile=vintbl))
Loading
Loading