2
2
select - Select data table subsets based on multiple spatial criteria.
3
3
"""
4
4
5
+ from typing import Literal
6
+
7
+ import numpy as np
5
8
import pandas as pd
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 ,
12
14
use_alias ,
15
+ validate_output_table_type ,
13
16
)
14
17
15
18
__doctest_skip__ = ["select" ]
41
44
w = "wrap" ,
42
45
)
43
46
@kwargs_to_strings (M = "sequence" , R = "sequence" , i = "sequence_comma" , o = "sequence_comma" )
44
- def select (data = None , outfile = None , ** kwargs ):
47
+ def select (
48
+ data = None ,
49
+ output_type : Literal ["pandas" , "numpy" , "file" ] = "pandas" ,
50
+ outfile : str | None = None ,
51
+ ** kwargs ,
52
+ ) -> pd .DataFrame | np .ndarray | None :
45
53
r"""
46
54
Select data table subsets based on multiple spatial criteria.
47
55
@@ -70,8 +78,8 @@ def select(data=None, outfile=None, **kwargs):
70
78
data : str, {table-like}
71
79
Pass in either a file name to an ASCII data table, a 2-D
72
80
{table-classes}.
73
- outfile : str
74
- The file name for the output ASCII file.
81
+ {output_type}
82
+ {outfile}
75
83
{area_thresh}
76
84
dist2pt : str
77
85
*pointfile*\|\ *lon*/*lat*\ **+d**\ *dist*.
@@ -180,12 +188,12 @@ def select(data=None, outfile=None, **kwargs):
180
188
181
189
Returns
182
190
-------
183
- output : pandas.DataFrame or None
184
- Return type depends on whether the ``outfile`` parameter is set :
191
+ ret
192
+ Return type depends on ``outfile`` and ``output_type`` :
185
193
186
- - :class:`pandas.DataFrame` table if ``outfile`` is not set.
187
- - None if ``outfile`` is set (filtered output will be stored in file
188
- set by ``outfile ``).
194
+ - None if ``outfile`` is set (output will be stored in file set by ``outfile``)
195
+ - :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is not set
196
+ (depends on ``output_type ``)
189
197
190
198
Example
191
199
-------
@@ -196,25 +204,23 @@ def select(data=None, outfile=None, **kwargs):
196
204
>>> # longitudes 246 and 247 and latitudes 20 and 21
197
205
>>> out = pygmt.select(data=ship_data, region=[246, 247, 20, 21])
198
206
"""
199
-
200
- with GMTTempFile (suffix = ".csv" ) as tmpfile :
201
- with Session () as lib :
202
- with lib .virtualfile_in (check_kind = "vector" , data = data ) as vintbl :
203
- if outfile is None :
204
- outfile = tmpfile .name
205
- lib .call_module (
206
- module = "select" ,
207
- args = build_arg_string (kwargs , infile = vintbl , outfile = outfile ),
208
- )
209
-
210
- # Read temporary csv output to a pandas table
211
- if outfile == tmpfile .name : # if user did not set outfile, return pd.DataFrame
212
- try :
213
- column_names = data .columns .to_list ()
214
- result = pd .read_csv (tmpfile .name , sep = "\t " , names = column_names )
215
- except AttributeError : # 'str' object has no attribute 'columns'
216
- result = pd .read_csv (tmpfile .name , sep = "\t " , header = None , comment = ">" )
217
- elif outfile != tmpfile .name : # return None if outfile set, output in outfile
218
- result = None
219
-
220
- return result
207
+ output_type = validate_output_table_type (output_type , outfile = outfile )
208
+
209
+ column_names = None
210
+ if output_type == "pandas" and isinstance (data , pd .DataFrame ):
211
+ column_names = data .columns .to_list ()
212
+
213
+ with Session () as lib :
214
+ with (
215
+ lib .virtualfile_in (check_kind = "vector" , data = data ) as vintbl ,
216
+ lib .virtualfile_out (kind = "dataset" , fname = outfile ) as vouttbl ,
217
+ ):
218
+ lib .call_module (
219
+ module = "select" ,
220
+ args = build_arg_string (kwargs , infile = vintbl , outfile = vouttbl ),
221
+ )
222
+ return lib .virtualfile_to_dataset (
223
+ output_type = output_type ,
224
+ vfile = vouttbl ,
225
+ column_names = column_names ,
226
+ )
0 commit comments