1
1
"""
2
- blockm - Block average (x, y, z) data tables by mean, median, or mode
3
- estimation.
2
+ blockm - Block average (x, y, z) data tables by mean, median, or mode estimation.
4
3
"""
5
4
5
+ from typing import Literal
6
+
7
+ import numpy as np
6
8
import pandas as pd
7
9
from pygmt .clib import Session
8
10
from pygmt .helpers import (
9
- GMTTempFile ,
10
11
build_arg_string ,
11
12
fmt_docstring ,
12
13
kwargs_to_strings ,
13
14
use_alias ,
15
+ validate_output_table_type ,
14
16
)
15
17
16
18
__doctest_skip__ = ["blockmean" , "blockmedian" , "blockmode" ]
17
19
18
20
19
- def _blockm (block_method , data , x , y , z , outfile , ** kwargs ):
21
+ def _blockm (
22
+ block_method , data , x , y , z , output_type , outfile , ** kwargs
23
+ ) -> pd .DataFrame | np .ndarray | None :
20
24
r"""
21
25
Block average (x, y, z) data tables by mean, median, or mode estimation.
22
26
@@ -34,38 +38,34 @@ def _blockm(block_method, data, x, y, z, outfile, **kwargs):
34
38
35
39
Returns
36
40
-------
37
- output : pandas.DataFrame or None
38
- Return type depends on whether the ``outfile`` parameter is set :
41
+ ret
42
+ Return type depends on ``outfile`` and ``output_type`` :
39
43
40
- - :class:`pandas.DataFrame` table with (x, y, z) columns if ``outfile``
41
- is not set
42
- - None if ``outfile`` is set (filtered output will be stored in file
43
- set by ``outfile ``)
44
+ - ``None`` if ``outfile`` is set (output will be stored in file set by
45
+ ``outfile``)
46
+ - :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is not set
47
+ (depends on ``output_type ``)
44
48
"""
45
- with GMTTempFile (suffix = ".csv" ) as tmpfile :
46
- with Session () as lib :
47
- with lib .virtualfile_in (
49
+ output_type = validate_output_table_type (output_type , outfile = outfile )
50
+
51
+ column_names = None
52
+ if output_type == "pandas" and isinstance (data , pd .DataFrame ):
53
+ column_names = data .columns .to_list ()
54
+
55
+ with Session () as lib :
56
+ with (
57
+ lib .virtualfile_in (
48
58
check_kind = "vector" , data = data , x = x , y = y , z = z , required_z = True
49
- ) as vintbl :
50
- # Run blockm* on data table
51
- if outfile is None :
52
- outfile = tmpfile .name
53
- lib .call_module (
54
- module = block_method ,
55
- args = build_arg_string (kwargs , infile = vintbl , 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
59
+ ) as vintbl ,
60
+ lib .virtualfile_out (kind = "dataset" , fname = outfile ) as vouttbl ,
61
+ ):
62
+ lib .call_module (
63
+ module = block_method ,
64
+ args = build_arg_string (kwargs , infile = vintbl , outfile = vouttbl ),
65
+ )
66
+ return lib .virtualfile_to_dataset (
67
+ output_type = output_type , vfname = vouttbl , column_names = column_names
68
+ )
69
69
70
70
71
71
@fmt_docstring
@@ -86,7 +86,15 @@ def _blockm(block_method, data, x, y, z, outfile, **kwargs):
86
86
w = "wrap" ,
87
87
)
88
88
@kwargs_to_strings (I = "sequence" , R = "sequence" , i = "sequence_comma" , o = "sequence_comma" )
89
- def blockmean (data = None , x = None , y = None , z = None , outfile = None , ** kwargs ):
89
+ def blockmean (
90
+ data = None ,
91
+ x = None ,
92
+ y = None ,
93
+ z = None ,
94
+ output_type : Literal ["pandas" , "numpy" , "file" ] = "pandas" ,
95
+ outfile : str | None = None ,
96
+ ** kwargs ,
97
+ ) -> pd .DataFrame | np .ndarray | None :
90
98
r"""
91
99
Block average (x, y, z) data tables by mean estimation.
92
100
@@ -111,9 +119,9 @@ def blockmean(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
111
119
{table-classes}.
112
120
x/y/z : 1-D arrays
113
121
Arrays of x and y coordinates and values z of the data points.
114
-
122
+ {output_type}
123
+ {outfile}
115
124
{spacing}
116
-
117
125
summary : str
118
126
[**m**\|\ **n**\|\ **s**\|\ **w**].
119
127
Type of summary values calculated by blockmean.
@@ -122,12 +130,7 @@ def blockmean(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
122
130
- **n** - report the number of input points inside each block
123
131
- **s** - report the sum of all z-values inside a block
124
132
- **w** - report the sum of weights
125
-
126
133
{region}
127
-
128
- outfile : str
129
- The file name for the output ASCII file.
130
-
131
134
{verbose}
132
135
{aspatial}
133
136
{binary}
@@ -142,13 +145,13 @@ def blockmean(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
142
145
143
146
Returns
144
147
-------
145
- output : pandas.DataFrame or None
146
- Return type depends on whether the ``outfile`` parameter is set :
148
+ ret
149
+ Return type depends on ``outfile`` and ``output_type`` :
147
150
148
- - :class:`pandas.DataFrame` table with (x, y, z) columns if ``outfile``
149
- is not set.
150
- - None if ``outfile`` is set (filtered output will be stored in file
151
- set by ``outfile ``).
151
+ - ``None`` if ``outfile`` is set (output will be stored in file set by
152
+ ``outfile``)
153
+ - :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is not set
154
+ (depends on ``output_type ``)
152
155
153
156
Example
154
157
-------
@@ -159,7 +162,14 @@ def blockmean(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
159
162
>>> data_bmean = pygmt.blockmean(data=data, region=[245, 255, 20, 30], spacing="5m")
160
163
"""
161
164
return _blockm (
162
- block_method = "blockmean" , data = data , x = x , y = y , z = z , outfile = outfile , ** kwargs
165
+ block_method = "blockmean" ,
166
+ data = data ,
167
+ x = x ,
168
+ y = y ,
169
+ z = z ,
170
+ output_type = output_type ,
171
+ outfile = outfile ,
172
+ ** kwargs ,
163
173
)
164
174
165
175
@@ -180,7 +190,15 @@ def blockmean(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
180
190
w = "wrap" ,
181
191
)
182
192
@kwargs_to_strings (I = "sequence" , R = "sequence" , i = "sequence_comma" , o = "sequence_comma" )
183
- def blockmedian (data = None , x = None , y = None , z = None , outfile = None , ** kwargs ):
193
+ def blockmedian (
194
+ data = None ,
195
+ x = None ,
196
+ y = None ,
197
+ z = None ,
198
+ output_type : Literal ["pandas" , "numpy" , "file" ] = "pandas" ,
199
+ outfile : str | None = None ,
200
+ ** kwargs ,
201
+ ) -> pd .DataFrame | np .ndarray | None :
184
202
r"""
185
203
Block average (x, y, z) data tables by median estimation.
186
204
@@ -205,14 +223,10 @@ def blockmedian(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
205
223
{table-classes}.
206
224
x/y/z : 1-D arrays
207
225
Arrays of x and y coordinates and values z of the data points.
208
-
226
+ {output_type}
227
+ {outfile}
209
228
{spacing}
210
-
211
229
{region}
212
-
213
- outfile : str
214
- The file name for the output ASCII file.
215
-
216
230
{verbose}
217
231
{aspatial}
218
232
{binary}
@@ -227,13 +241,13 @@ def blockmedian(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
227
241
228
242
Returns
229
243
-------
230
- output : pandas.DataFrame or None
231
- Return type depends on whether the ``outfile`` parameter is set :
244
+ ret
245
+ Return type depends on ``outfile`` and ``output_type`` :
232
246
233
- - :class:`pandas.DataFrame` table with (x, y, z) columns if ``outfile``
234
- is not set.
235
- - None if ``outfile`` is set (filtered output will be stored in file
236
- set by ``outfile ``).
247
+ - ``None`` if ``outfile`` is set (output will be stored in file set by
248
+ ``outfile``)
249
+ - :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is not set
250
+ (depends on ``output_type ``)
237
251
238
252
Example
239
253
-------
@@ -246,7 +260,14 @@ def blockmedian(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
246
260
... )
247
261
"""
248
262
return _blockm (
249
- block_method = "blockmedian" , data = data , x = x , y = y , z = z , outfile = outfile , ** kwargs
263
+ block_method = "blockmedian" ,
264
+ data = data ,
265
+ x = x ,
266
+ y = y ,
267
+ z = z ,
268
+ output_type = output_type ,
269
+ outfile = outfile ,
270
+ ** kwargs ,
250
271
)
251
272
252
273
@@ -267,7 +288,15 @@ def blockmedian(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
267
288
w = "wrap" ,
268
289
)
269
290
@kwargs_to_strings (I = "sequence" , R = "sequence" , i = "sequence_comma" , o = "sequence_comma" )
270
- def blockmode (data = None , x = None , y = None , z = None , outfile = None , ** kwargs ):
291
+ def blockmode (
292
+ data = None ,
293
+ x = None ,
294
+ y = None ,
295
+ z = None ,
296
+ output_type : Literal ["pandas" , "numpy" , "file" ] = "pandas" ,
297
+ outfile : str | None = None ,
298
+ ** kwargs ,
299
+ ) -> pd .DataFrame | np .ndarray | None :
271
300
r"""
272
301
Block average (x, y, z) data tables by mode estimation.
273
302
@@ -292,14 +321,10 @@ def blockmode(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
292
321
{table-classes}.
293
322
x/y/z : 1-D arrays
294
323
Arrays of x and y coordinates and values z of the data points.
295
-
324
+ {output_type}
325
+ {outfile}
296
326
{spacing}
297
-
298
327
{region}
299
-
300
- outfile : str
301
- The file name for the output ASCII file.
302
-
303
328
{verbose}
304
329
{aspatial}
305
330
{binary}
@@ -314,13 +339,13 @@ def blockmode(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
314
339
315
340
Returns
316
341
-------
317
- output : pandas.DataFrame or None
318
- Return type depends on whether the ``outfile`` parameter is set :
342
+ ret
343
+ Return type depends on ``outfile`` and ``output_type`` :
319
344
320
- - :class:`pandas.DataFrame` table with (x, y, z) columns if ``outfile``
321
- is not set.
322
- - None if ``outfile`` is set (filtered output will be stored in file
323
- set by ``outfile ``).
345
+ - ``None`` if ``outfile`` is set (output will be stored in file set by
346
+ ``outfile``)
347
+ - :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is not set
348
+ (depends on ``output_type ``)
324
349
325
350
Example
326
351
-------
@@ -331,5 +356,12 @@ def blockmode(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
331
356
>>> data_bmode = pygmt.blockmode(data=data, region=[245, 255, 20, 30], spacing="5m")
332
357
"""
333
358
return _blockm (
334
- block_method = "blockmode" , data = data , x = x , y = y , z = z , outfile = outfile , ** kwargs
359
+ block_method = "blockmode" ,
360
+ data = data ,
361
+ x = x ,
362
+ y = y ,
363
+ z = z ,
364
+ output_type = output_type ,
365
+ outfile = outfile ,
366
+ ** kwargs ,
335
367
)
0 commit comments