"""grdproject - Forward and inverse map transformation of grids."""importxarrayasxrfrompygmt.clibimportSessionfrompygmt.exceptionsimportGMTInvalidInputfrompygmt.helpersimport(GMTTempFile,build_arg_string,fmt_docstring,kwargs_to_strings,use_alias,)
[docs]@fmt_docstring@use_alias(G="outgrid",J="projection",I="inverse",R="region",V="verbose",)@kwargs_to_strings(R="sequence")defgrdproject(grid,**kwargs):r""" {aliases} Parameters ---------- grid : str or xarray.DataArray The file name of the input grid or the grid loaded as a DataArray. outgrid : str or None The name of the output netCDF file with extension .nc to store the grid in. inverse : bool When set to ``True`` transforms grid from rectangular to geographical [Default is False]. {J} {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"J"notinkwargs.keys():raiseGMTInvalidInput("The projection must be specified.")withGMTTempFile(suffix=".nc")astmpfile:withSession()aslib:file_context=lib.virtualfile_from_data(check_kind="raster",data=grid)withfile_contextasinfile:if"G"notinkwargs.keys():# if outgrid is unset, output to tempfilekwargs.update({"G":tmpfile.name})outgrid=kwargs["G"]arg_str=" ".join([infile,build_arg_string(kwargs)])lib.call_module("grdproject",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