"""grdvolume - Calculate grid volume and area constrained by a contour."""importnumpyasnpimportpandasaspdfrompygmt.clibimportSessionfrompygmt.exceptionsimportGMTInvalidInputfrompygmt.helpersimport(GMTTempFile,build_arg_string,fmt_docstring,kwargs_to_strings,use_alias,)
[docs]@fmt_docstring@use_alias(C="plane",Cr="outside_volume",D="slice",R="region",S="unit",V="verbose",)@kwargs_to_strings(C="sequence",R="sequence")defgrdvolume(grid,data_format="a",**kwargs):r""" {aliases} Parameters ---------- grid : str or xarray.DataArray The file name of the input grid or the grid loaded as a DataArray. This is the only required parameter. {R} {V} Returns ------- volume : str A string with the volume between the surface and specified plane. """withGMTTempFile()asoutfile:withSession()aslib:file_context=lib.virtualfile_from_data(check_kind="raster",data=grid)withfile_contextasinfile:arg_str=" ".join([infile,build_arg_string(kwargs),"->"+outfile.name])lib.call_module("grdvolume",arg_str)result=outfile.read()ifdata_format=="s":returnresultdata_list=[]forstring_entryinresult.strip().split("\n"):float_entry=[]string_list=string_entry.strip().split()foriinstring_list:float_entry.append(float(i))data_list.append(float_entry)data_array=np.array(data_list)ifdata_format=="a":result=data_arrayelifdata_format=="d":result=pd.DataFrame(data_array)else:raiseGMTInvalidInput("""Must specify format as either a, d, or s.""")returnresult