Skip to content

Commit 68fab7c

Browse files
committed
add formatting to return array or DataFrame
1 parent d3ed614 commit 68fab7c

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

pygmt/src/grdvolume.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
"""
22
grdvolume - Calculate grid volume and area constrained by a contour.
33
"""
4+
import numpy as np
5+
import pandas as pd
46
from pygmt.clib import Session
7+
from pygmt.exceptions import GMTInvalidInput
58
from pygmt.helpers import (
69
GMTTempFile,
710
build_arg_string,
@@ -21,7 +24,7 @@
2124
V="verbose",
2225
)
2326
@kwargs_to_strings(C="sequence", R="sequence")
24-
def grdvolume(grid, **kwargs):
27+
def grdvolume(grid, data_format="a", **kwargs):
2528
r"""
2629
{aliases}
2730
@@ -47,4 +50,20 @@ def grdvolume(grid, **kwargs):
4750
)
4851
lib.call_module("grdvolume", arg_str)
4952
result = outfile.read()
53+
if data_format == "s":
54+
return result
55+
data_list = []
56+
for string_entry in result.strip().split("\n"):
57+
float_entry = []
58+
string_list = string_entry.strip().split()
59+
for i in string_list:
60+
float_entry.append(float(i))
61+
data_list.append(float_entry)
62+
data_array = np.array(data_list)
63+
if data_format == "a":
64+
result = data_array
65+
elif data_format == "d":
66+
result = pd.DataFrame(data_array)
67+
else:
68+
raise GMTInvalidInput("""Must specify format as either a, d, or s.""")
5069
return result

0 commit comments

Comments
 (0)