2
2
grdvolume - Calculate grid volume and area constrained by a contour.
3
3
"""
4
4
5
+ from typing import Literal
6
+
5
7
import pandas as pd
8
+ import xarray as xr
6
9
from pygmt .clib import Session
7
10
from pygmt .helpers import (
8
- GMTTempFile ,
9
11
build_arg_string ,
10
12
fmt_docstring ,
11
13
kwargs_to_strings ,
24
26
V = "verbose" ,
25
27
)
26
28
@kwargs_to_strings (C = "sequence" , R = "sequence" )
27
- def grdvolume (grid , output_type = "pandas" , outfile = None , ** kwargs ):
29
+ def grdvolume (
30
+ grid ,
31
+ output_type : Literal ["pandas" , "numpy" , "file" ] = "pandas" ,
32
+ outfile : str | None = None ,
33
+ ** kwargs ,
34
+ ) -> pd .DataFrame | xr .DataArray | None :
28
35
r"""
29
36
Determine the volume between the surface of a grid and a plane.
30
37
@@ -41,15 +48,8 @@ def grdvolume(grid, output_type="pandas", outfile=None, **kwargs):
41
48
Parameters
42
49
----------
43
50
{grid}
44
- output_type : str
45
- Determine the format the output data will be returned in [Default is
46
- ``pandas``]:
47
-
48
- - ``numpy`` - :class:`numpy.ndarray`
49
- - ``pandas``- :class:`pandas.DataFrame`
50
- - ``file`` - ASCII file (requires ``outfile``)
51
- outfile : str
52
- The file name for the output ASCII file.
51
+ {output_type}
52
+ {outfile}
53
53
contour : str, float, or list
54
54
*cval*\|\ *low/high/delta*\|\ **r**\ *low/high*\|\ **r**\ *cval*.
55
55
Find area, volume and mean height (volume/area) inside and above the
@@ -69,14 +69,12 @@ def grdvolume(grid, output_type="pandas", outfile=None, **kwargs):
69
69
70
70
Returns
71
71
-------
72
- ret : pandas.DataFrame or numpy.ndarray or None
72
+ ret
73
73
Return type depends on ``outfile`` and ``output_type``:
74
74
75
- - None if ``outfile`` is set (output will be stored in file set by
76
- ``outfile``)
77
- - :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile``
78
- is not set (depends on ``output_type`` [Default is
79
- :class:`pandas.DataFrame`])
75
+ - None if ``outfile`` is set (output will be stored in file set by ``outfile``)
76
+ - :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is not set
77
+ (depends on ``output_type``)
80
78
81
79
Example
82
80
-------
@@ -103,22 +101,13 @@ def grdvolume(grid, output_type="pandas", outfile=None, **kwargs):
103
101
"""
104
102
output_type = validate_output_table_type (output_type , outfile = outfile )
105
103
106
- with GMTTempFile () as tmpfile :
107
- with Session () as lib :
108
- with lib .virtualfile_in (check_kind = "raster" , data = grid ) as vingrd :
109
- if outfile is None :
110
- outfile = tmpfile .name
111
- lib .call_module (
112
- module = "grdvolume" ,
113
- args = build_arg_string (kwargs , infile = vingrd , outfile = outfile ),
114
- )
115
-
116
- # Read temporary csv output to a pandas table
117
- if outfile == tmpfile .name : # if user did not set outfile, return pd.DataFrame
118
- result = pd .read_csv (tmpfile .name , sep = "\t " , header = None , comment = ">" )
119
- elif outfile != tmpfile .name : # return None if outfile set, output in outfile
120
- result = None
121
-
122
- if output_type == "numpy" :
123
- result = result .to_numpy ()
124
- return result
104
+ with Session () as lib :
105
+ with (
106
+ lib .virtualfile_in (check_kind = "raster" , data = grid ) as vingrd ,
107
+ lib .virtualfile_out (kind = "dataset" , fname = outfile ) as vouttbl ,
108
+ ):
109
+ lib .call_module (
110
+ module = "grdvolume" ,
111
+ args = build_arg_string (kwargs , infile = vingrd , outfile = vouttbl ),
112
+ )
113
+ return lib .virtualfile_to_dataset (output_type = output_type , vfname = vouttbl )
0 commit comments