2
2
project - Project data onto lines or great circles, or generate tracks.
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 .exceptions import GMTInvalidInput
8
11
from pygmt .helpers import (
9
- GMTTempFile ,
10
12
build_arg_string ,
11
13
fmt_docstring ,
12
14
kwargs_to_strings ,
13
15
use_alias ,
16
+ validate_output_table_type ,
14
17
)
15
18
16
19
32
35
f = "coltypes" ,
33
36
)
34
37
@kwargs_to_strings (E = "sequence" , L = "sequence" , T = "sequence" , W = "sequence" , C = "sequence" )
35
- def project (data = None , x = None , y = None , z = None , outfile = None , ** kwargs ):
38
+ def project (
39
+ data = None ,
40
+ x = None ,
41
+ y = None ,
42
+ z = None ,
43
+ output_type : Literal ["pandas" , "numpy" , "file" ] = "pandas" ,
44
+ outfile : str | None = None ,
45
+ ** kwargs ,
46
+ ) -> pd .DataFrame | np .ndarray | None :
36
47
r"""
37
48
Project data onto lines or great circles, or generate tracks.
38
49
@@ -105,6 +116,8 @@ def project(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
105
116
Pass in (x, y, z) or (longitude, latitude, elevation) values by
106
117
providing a file name to an ASCII data table, a 2-D
107
118
{table-classes}.
119
+ {output_type}
120
+ {outfile}
108
121
109
122
center : str or list
110
123
*cx*/*cy*.
@@ -196,22 +209,18 @@ def project(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
196
209
*direction* is counter-clockwise from the horizontal instead of an
197
210
*azimuth*.
198
211
199
- outfile : str
200
- The file name for the output ASCII file.
201
-
202
212
{coltypes}
203
213
204
214
Returns
205
215
-------
206
- track: pandas.DataFrame or None
207
- Return type depends on whether the ``outfile`` parameter is set :
216
+ ret
217
+ Return type depends on ``outfile`` and ``output_type`` :
208
218
209
- - :class:`pandas.DataFrame` table with (x, y, ..., newcolname) if
210
- ``outfile`` is not set
211
- - None if ``outfile`` is set (output will be stored in file set
212
- by ``outfile ``)
219
+ - ``None`` if ``outfile`` is set (output will be stored in file set by
220
+ ``outfile``)
221
+ - :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is not set
222
+ (depends on ``output_type ``)
213
223
"""
214
-
215
224
if kwargs .get ("C" ) is None :
216
225
raise GMTInvalidInput ("The `center` parameter must be specified." )
217
226
if kwargs .get ("G" ) is None and data is None :
@@ -223,29 +232,31 @@ def project(data=None, x=None, y=None, z=None, outfile=None, **kwargs):
223
232
"The `convention` parameter is not allowed with `generate`."
224
233
)
225
234
226
- with GMTTempFile (suffix = ".csv" ) as tmpfile :
227
- if outfile is None : # Output to tmpfile if outfile is not set
228
- outfile = tmpfile .name
229
- with Session () as lib :
230
- if kwargs .get ("G" ) is None :
231
- with lib .virtualfile_in (
232
- check_kind = "vector" , data = data , x = x , y = y , z = z , required_z = False
233
- ) as vintbl :
234
- # Run project on the temporary (csv) data table
235
- arg_str = build_arg_string (kwargs , infile = vintbl , outfile = outfile )
236
- else :
237
- arg_str = build_arg_string (kwargs , outfile = outfile )
238
- lib .call_module (module = "project" , args = arg_str )
239
-
240
- # if user did not set outfile, return pd.DataFrame
241
- if outfile == tmpfile .name :
242
- if kwargs .get ("G" ) is not None :
243
- column_names = list ("rsp" )
244
- result = pd .read_csv (tmpfile .name , sep = "\t " , names = column_names )
245
- else :
246
- result = pd .read_csv (tmpfile .name , sep = "\t " , header = None , comment = ">" )
247
- # return None if outfile set, output in outfile
248
- elif outfile != tmpfile .name :
249
- result = None
250
-
251
- return result
235
+ output_type = validate_output_table_type (output_type , outfile = outfile )
236
+
237
+ column_names = None
238
+ if output_type == "pandas" and kwargs .get ("G" ) is not None :
239
+ column_names = list ("rsp" )
240
+
241
+ with Session () as lib :
242
+ with (
243
+ lib .virtualfile_in (
244
+ check_kind = "vector" ,
245
+ data = data ,
246
+ x = x ,
247
+ y = y ,
248
+ z = z ,
249
+ required_z = False ,
250
+ required_data = False ,
251
+ ) as vintbl ,
252
+ lib .virtualfile_out (kind = "dataset" , fname = outfile ) as vouttbl ,
253
+ ):
254
+ lib .call_module (
255
+ module = "project" ,
256
+ args = build_arg_string (kwargs , infile = vintbl , outfile = vouttbl ),
257
+ )
258
+ return lib .virtualfile_to_dataset (
259
+ output_type = output_type ,
260
+ vfname = vouttbl ,
261
+ column_names = column_names ,
262
+ )
0 commit comments