"""sph2grd - Compute grid from spherical harmonic coefficients"""importxarrayasxrfrompygmt.clibimportSessionfrompygmt.helpersimport(GMTTempFile,build_arg_string,fmt_docstring,kwargs_to_strings,use_alias,)
[docs]@fmt_docstring@use_alias(G="outgrid",I="increment",R="region",V="verbose",)@kwargs_to_strings(I="sequence",R="sequence")defsph2grd(table,**kwargs):r""" Create spherical grid files in tension of data. Reads a spherical harmonics coefficient table with records of L, M, C[L,M], S[L,M] and evaluates the spherical harmonic model on 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``) """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("sph2grd",arg_str)ifoutgrid==tmpfile.name:# if user did not set outgrid, return DataArraywithxr.open_dataarray(outgrid)asdataarray:result=dataarray.load()_=result.gmt# load GMTDataArray accessor informationelse:result=None# if user sets an outgrid, return Nonereturnresult