"""sphdistance - Create Voronoi distance, node,or natural nearest-neighbor grid on a sphere"""frompygmt.clibimportSessionfrompygmt.exceptionsimportGMTInvalidInputfrompygmt.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(I="sequence",R="sequence")defsphdistance(table,**kwargs):r""" Create Voroni polygons from lat/lon coordinates. Reads one or more ASCII [or binary] files (or standard input) containing lon, lat and performs the construction of Voronoi polygons. These polygons are then processed to calculate the nearest distance to each node of the lattice and written to the specified grid. {aliases} Parameters ---------- outgrid : str or None 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``) """if"I"notinkwargs.keys()or"R"notinkwargs.keys():raiseGMTInvalidInput("Both 'region' and 'spacing' must be specified.")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("sphdistance",arg_str)returnload_dataarray(outgrid)ifoutgrid==tmpfile.nameelseNone