"""xyz2grd - Convert data table to a grid."""frompygmt.clibimportSessionfrompygmt.helpersimport(GMTTempFile,build_arg_string,fmt_docstring,kwargs_to_strings,use_alias,)frompygmt.ioimportload_dataarray
[docs]@fmt_docstring@use_alias(G="outgrid",I="spacing",R="region",V="verbose",)@kwargs_to_strings(R="sequence")defxyz2grd(table,**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 ---------- table : str or {table-like} Pass in either a file name to an ASCII data table, a 1D/2D {table-classes}. 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``)``` """withGMTTempFile(suffix=".nc")astmpfile:withSession()aslib:file_context=lib.virtualfile_from_data(check_kind="vector",data=table)withfile_contextasinfile:if"G"notinkwargs.keys():# if outgrid is unset, output to tempfilekwargs.update({"G":tmpfile.name})outgrid=kwargs["G"]arg_str=build_arg_string(kwargs)arg_str=" ".join([infile,arg_str])lib.call_module("xyz2grd",arg_str)returnload_dataarray(outgrid)ifoutgrid==tmpfile.nameelseNone