Skip to content

Commit

Permalink
Expand table-like input options for xyz2grd (#1566)
Browse files Browse the repository at this point in the history
* Expand table-like input options for xyz2grd
* Move xyz2grd to the tabular data section
* Compact code for arg_str
  • Loading branch information
seisman authored Oct 5, 2021
1 parent a933364 commit 3c908aa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion doc/api/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Operations on tabular data:
sphdistance
sphinterpolate
surface
xyz2grd

Operations on grids:

Expand All @@ -103,7 +104,6 @@ Operations on grids:
grdproject
grdsample
grdtrack
xyz2grd

Crossover analysis with x2sys:

Expand Down
18 changes: 10 additions & 8 deletions pygmt/src/xyz2grd.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
V="verbose",
)
@kwargs_to_strings(R="sequence")
def xyz2grd(data, **kwargs):
def xyz2grd(data=None, x=None, y=None, z=None, **kwargs):
"""
Create a grid file from table data.
Expand All @@ -34,9 +34,10 @@ def xyz2grd(data, **kwargs):
Parameters
----------
data : str or {table-like}
Pass in either a file name to an ASCII data table, a 1D/2D
{table-classes}.
Pass in (x, y, z) or (longitude, latitude, elevation) values by
providing a file name to an ASCII data table, a 2D {table-classes}.
x/y/z : 1d arrays
The arrays of x and y coordinates and z data points.
outgrid : str or None
Optional. The name of the output netCDF file with extension .nc to
store the grid in.
Expand All @@ -51,17 +52,18 @@ def xyz2grd(data, **kwargs):
- :class:`xarray.DataArray`: if ``outgrid`` is not set
- None if ``outgrid`` is set (grid output will be stored in file set by
``outgrid``)```
``outgrid``)
"""
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
file_context = lib.virtualfile_from_data(check_kind="vector", data=data)
file_context = lib.virtualfile_from_data(
check_kind="vector", data=data, x=x, y=y, z=z, required_z=True
)
with file_context as infile:
if "G" not in kwargs.keys(): # if outgrid is unset, output to tempfile
kwargs.update({"G": tmpfile.name})
outgrid = kwargs["G"]
arg_str = build_arg_string(kwargs)
arg_str = " ".join([infile, arg_str])
arg_str = " ".join([infile, build_arg_string(kwargs)])
lib.call_module("xyz2grd", arg_str)

return load_dataarray(outgrid) if outgrid == tmpfile.name else None

0 comments on commit 3c908aa

Please sign in to comment.