5
5
import pandas as pd
6
6
from pygmt .clib import Session
7
7
from pygmt .helpers import (
8
+ GMTTempFile ,
8
9
build_arg_string ,
9
10
fmt_docstring ,
10
11
kwargs_to_strings ,
11
- return_table ,
12
12
use_alias ,
13
- validate_output_table_type ,
14
13
)
15
14
16
15
__doctest_skip__ = ["blockmean" , "blockmedian" , "blockmode" ]
17
16
18
17
19
- def _blockm (block_method , data , x , y , z , output_type , outfile , ** kwargs ):
18
+ def _blockm (block_method , data , x , y , z , outfile , ** kwargs ):
20
19
r"""
21
20
Block average (x, y, z) data tables by mean, median, or mode estimation.
22
21
@@ -42,26 +41,31 @@ def _blockm(block_method, data, x, y, z, output_type, outfile, **kwargs):
42
41
- None if ``outfile`` is set (filtered output will be stored in file
43
42
set by ``outfile``)
44
43
"""
45
- output_type = validate_output_table_type (output_type , outfile = outfile )
46
-
47
- with Session () as lib :
48
- with lib .virtualfile_from_data (
49
- check_kind = "vector" , data = data , x = x , y = y , z = z , required_z = True
50
- ) as vintbl , lib .virtualfile_to_data (kind = "dataset" , fname = outfile ) as vouttbl :
51
- lib .call_module (
52
- module = block_method ,
53
- args = build_arg_string (kwargs , infile = vintbl , outfile = vouttbl ),
44
+ with GMTTempFile (suffix = ".csv" ) as tmpfile :
45
+ with Session () as lib :
46
+ table_context = lib .virtualfile_from_data (
47
+ check_kind = "vector" , data = data , x = x , y = y , z = z , required_z = True
54
48
)
55
- column_names = None
56
- if isinstance (data , pd .DataFrame ):
57
- column_names = data .columns .to_list ()
58
-
59
- return return_table (
60
- session = lib ,
61
- output_type = output_type ,
62
- vfile = vouttbl ,
63
- column_names = column_names ,
64
- )
49
+ # Run blockm* on data table
50
+ with table_context as infile :
51
+ if outfile is None :
52
+ outfile = tmpfile .name
53
+ lib .call_module (
54
+ module = block_method ,
55
+ args = build_arg_string (kwargs , infile = infile , outfile = outfile ),
56
+ )
57
+
58
+ # Read temporary csv output to a pandas table
59
+ if outfile == tmpfile .name : # if user did not set outfile, return pd.DataFrame
60
+ try :
61
+ column_names = data .columns .to_list ()
62
+ result = pd .read_csv (tmpfile .name , sep = "\t " , names = column_names )
63
+ except AttributeError : # 'str' object has no attribute 'columns'
64
+ result = pd .read_csv (tmpfile .name , sep = "\t " , header = None , comment = ">" )
65
+ elif outfile != tmpfile .name : # return None if outfile set, output in outfile
66
+ result = None
67
+
68
+ return result
65
69
66
70
67
71
@fmt_docstring
@@ -82,9 +86,7 @@ def _blockm(block_method, data, x, y, z, output_type, outfile, **kwargs):
82
86
w = "wrap" ,
83
87
)
84
88
@kwargs_to_strings (I = "sequence" , R = "sequence" , i = "sequence_comma" , o = "sequence_comma" )
85
- def blockmean (
86
- data = None , x = None , y = None , z = None , output_type = "pandas" , outfile = None , ** kwargs
87
- ):
89
+ def blockmean (data = None , x = None , y = None , z = None , outfile = None , ** kwargs ):
88
90
r"""
89
91
Block average (x, y, z) data tables by mean estimation.
90
92
@@ -157,14 +159,7 @@ def blockmean(
157
159
>>> data_bmean = pygmt.blockmean(data=data, region=[245, 255, 20, 30], spacing="5m")
158
160
"""
159
161
return _blockm (
160
- block_method = "blockmean" ,
161
- data = data ,
162
- x = x ,
163
- y = y ,
164
- z = z ,
165
- output_type = output_type ,
166
- outfile = outfile ,
167
- ** kwargs ,
162
+ block_method = "blockmean" , data = data , x = x , y = y , z = z , outfile = outfile , ** kwargs
168
163
)
169
164
170
165
@@ -185,9 +180,7 @@ def blockmean(
185
180
w = "wrap" ,
186
181
)
187
182
@kwargs_to_strings (I = "sequence" , R = "sequence" , i = "sequence_comma" , o = "sequence_comma" )
188
- def blockmedian (
189
- data = None , x = None , y = None , z = None , output_type = "pandas" , outfile = None , ** kwargs
190
- ):
183
+ def blockmedian (data = None , x = None , y = None , z = None , outfile = None , ** kwargs ):
191
184
r"""
192
185
Block average (x, y, z) data tables by median estimation.
193
186
@@ -253,14 +246,7 @@ def blockmedian(
253
246
... )
254
247
"""
255
248
return _blockm (
256
- block_method = "blockmedian" ,
257
- data = data ,
258
- x = x ,
259
- y = y ,
260
- z = z ,
261
- output_type = output_type ,
262
- outfile = outfile ,
263
- ** kwargs ,
249
+ block_method = "blockmedian" , data = data , x = x , y = y , z = z , outfile = outfile , ** kwargs
264
250
)
265
251
266
252
@@ -281,9 +267,7 @@ def blockmedian(
281
267
w = "wrap" ,
282
268
)
283
269
@kwargs_to_strings (I = "sequence" , R = "sequence" , i = "sequence_comma" , o = "sequence_comma" )
284
- def blockmode (
285
- data = None , x = None , y = None , z = None , output_type = "pandas" , outfile = None , ** kwargs
286
- ):
270
+ def blockmode (data = None , x = None , y = None , z = None , outfile = None , ** kwargs ):
287
271
r"""
288
272
Block average (x, y, z) data tables by mode estimation.
289
273
@@ -347,12 +331,5 @@ def blockmode(
347
331
>>> data_bmode = pygmt.blockmode(data=data, region=[245, 255, 20, 30], spacing="5m")
348
332
"""
349
333
return _blockm (
350
- block_method = "blockmode" ,
351
- data = data ,
352
- x = x ,
353
- y = y ,
354
- z = z ,
355
- output_type = output_type ,
356
- outfile = outfile ,
357
- ** kwargs ,
334
+ block_method = "blockmode" , data = data , x = x , y = y , z = z , outfile = outfile , ** kwargs
358
335
)
0 commit comments