3
3
"""
4
4
import pandas as pd
5
5
from pygmt .clib import Session
6
- from pygmt .exceptions import GMTInvalidInput
7
6
from pygmt .helpers import (
8
7
GMTTempFile ,
9
8
build_arg_string ,
10
- data_kind ,
11
- dummy_context ,
12
9
fmt_docstring ,
13
10
kwargs_to_strings ,
14
11
use_alias ,
@@ -41,29 +38,24 @@ def _blockm(block_method, table, outfile, **kwargs):
41
38
set by ``outfile``)
42
39
"""
43
40
44
- kind = data_kind (table )
45
41
with GMTTempFile (suffix = ".csv" ) as tmpfile :
46
42
with Session () as lib :
47
- if kind == "matrix" :
48
- if not hasattr (table , "values" ):
49
- raise GMTInvalidInput (f"Unrecognized data type: { type (table )} " )
50
- file_context = lib .virtualfile_from_matrix (table .values )
51
- elif kind == "file" :
52
- if outfile is None :
53
- raise GMTInvalidInput ("Please pass in a str to 'outfile'" )
54
- file_context = dummy_context (table )
55
- else :
56
- raise GMTInvalidInput (f"Unrecognized data type: { type (table )} " )
57
-
58
- with file_context as infile :
43
+ # Choose how data will be passed into the module
44
+ table_context = lib .virtualfile_from_data (check_kind = "vector" , data = table )
45
+ # Run blockm* on data table
46
+ with table_context as infile :
59
47
if outfile is None :
60
48
outfile = tmpfile .name
61
49
arg_str = " " .join ([infile , build_arg_string (kwargs ), "->" + outfile ])
62
50
lib .call_module (module = block_method , args = arg_str )
63
51
64
52
# Read temporary csv output to a pandas table
65
53
if outfile == tmpfile .name : # if user did not set outfile, return pd.DataFrame
66
- result = pd .read_csv (tmpfile .name , sep = "\t " , names = table .columns )
54
+ try :
55
+ column_names = table .columns .to_list ()
56
+ result = pd .read_csv (tmpfile .name , sep = "\t " , names = column_names )
57
+ except AttributeError : # 'str' object has no attribute 'columns'
58
+ result = pd .read_csv (tmpfile .name , sep = "\t " , header = None , comment = ">" )
67
59
elif outfile != tmpfile .name : # return None if outfile set, output in outfile
68
60
result = None
69
61
@@ -95,10 +87,10 @@ def blockmean(table, outfile=None, **kwargs):
95
87
96
88
Parameters
97
89
----------
98
- table : pandas.DataFrame or str
99
- Either a pandas dataframe with (x, y, z) or (longitude, latitude,
100
- elevation) values in the first three columns, or a file name to an
101
- ASCII data table.
90
+ table : str or {table-like}
91
+ Pass in (x, y, z) or (longitude, latitude, elevation) values by
92
+ providing a file name to an ASCII data table, a 2D
93
+ { table-classes} .
102
94
103
95
spacing : str
104
96
*xinc*\[\ *unit*\][**+e**\|\ **n**]
@@ -110,8 +102,7 @@ def blockmean(table, outfile=None, **kwargs):
110
102
Specify the region of interest.
111
103
112
104
outfile : str
113
- Required if ``table`` is a file. The file name for the output ASCII
114
- file.
105
+ The file name for the output ASCII file.
115
106
116
107
{V}
117
108
{a}
@@ -156,10 +147,10 @@ def blockmedian(table, outfile=None, **kwargs):
156
147
157
148
Parameters
158
149
----------
159
- table : pandas.DataFrame or str
160
- Either a pandas dataframe with (x, y, z) or (longitude, latitude,
161
- elevation) values in the first three columns, or a file name to an
162
- ASCII data table.
150
+ table : str or {table-like}
151
+ Pass in (x, y, z) or (longitude, latitude, elevation) values by
152
+ providing a file name to an ASCII data table, a 2D
153
+ { table-classes} .
163
154
164
155
spacing : str
165
156
*xinc*\[\ *unit*\][**+e**\|\ **n**]
@@ -171,8 +162,7 @@ def blockmedian(table, outfile=None, **kwargs):
171
162
Specify the region of interest.
172
163
173
164
outfile : str
174
- Required if ``table`` is a file. The file name for the output ASCII
175
- file.
165
+ The file name for the output ASCII file.
176
166
177
167
{V}
178
168
{a}
0 commit comments