"""
xyz2grd - Convert data table to a grid.
"""
from pygmt.clib import Session
from pygmt.helpers import (
GMTTempFile,
build_arg_string,
fmt_docstring,
kwargs_to_strings,
use_alias,
)
from pygmt.io import load_dataarray
[docs]@fmt_docstring
@use_alias(
G="outgrid",
I="spacing",
R="region",
V="verbose",
)
@kwargs_to_strings(R="sequence")
def xyz2grd(data=None, x=None, y=None, z=None, **kwargs):
"""
Create a grid file from table data.
xyz2grd reads one or more z or xyz tables and creates a binary grid file.
xyz2grd will report if some of the nodes are not filled in with data. Such
unconstrained nodes are set to a value specified by the user [Default is
NaN]. Nodes with more than one value will be set to the mean value.
Full option list at :gmt-docs:`xyz2grd.html`
Parameters
----------
data : str or {table-like}
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.
{I}
{R}
{V}
Returns
-------
ret: xarray.DataArray or None
Return type depends on whether the ``outgrid`` parameter is set:
- :class:`xarray.DataArray`: if ``outgrid`` is not set
- None if ``outgrid`` is set (grid output will be stored in file set by
``outgrid``)
"""
with GMTTempFile(suffix=".nc") as tmpfile:
with Session() as lib:
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 = " ".join([infile, build_arg_string(kwargs)])
lib.call_module("xyz2grd", arg_str)
return load_dataarray(outgrid) if outgrid == tmpfile.name else None