2
2
grd2xyz - Convert grid to data table
3
3
"""
4
4
5
+ from typing import TYPE_CHECKING , Literal
6
+
5
7
import pandas as pd
6
8
import xarray as xr
7
9
from pygmt .clib import Session
8
10
from pygmt .exceptions import GMTInvalidInput
9
11
from pygmt .helpers import (
10
- GMTTempFile ,
11
12
build_arg_string ,
12
13
fmt_docstring ,
13
14
kwargs_to_strings ,
14
15
use_alias ,
15
16
validate_output_table_type ,
16
17
)
17
18
19
+ if TYPE_CHECKING :
20
+ from collections .abc import Hashable
21
+
18
22
__doctest_skip__ = ["grd2xyz" ]
19
23
20
24
33
37
s = "skiprows" ,
34
38
)
35
39
@kwargs_to_strings (R = "sequence" , o = "sequence_comma" )
36
- def grd2xyz (grid , output_type = "pandas" , outfile = None , ** kwargs ):
40
+ def grd2xyz (
41
+ grid ,
42
+ output_type : Literal ["pandas" , "numpy" , "file" ] = "pandas" ,
43
+ outfile : str | None = None ,
44
+ ** kwargs ,
45
+ ) -> pd .DataFrame | xr .DataArray | None :
37
46
r"""
38
47
Convert grid to data table.
39
48
@@ -47,15 +56,8 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs):
47
56
Parameters
48
57
----------
49
58
{grid}
50
- output_type : str
51
- Determine the format the xyz data will be returned in [Default is
52
- ``pandas``]:
53
-
54
- - ``numpy`` - :class:`numpy.ndarray`
55
- - ``pandas``- :class:`pandas.DataFrame`
56
- - ``file`` - ASCII file (requires ``outfile``)
57
- outfile : str
58
- The file name for the output ASCII file.
59
+ {output_type}
60
+ {outfile}
59
61
cstyle : str
60
62
[**f**\|\ **i**].
61
63
Replace the x- and y-coordinates on output with the corresponding
@@ -118,13 +120,12 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs):
118
120
119
121
Returns
120
122
-------
121
- ret : pandas.DataFrame or numpy.ndarray or None
123
+ ret
122
124
Return type depends on ``outfile`` and ``output_type``:
123
125
124
- - None if ``outfile`` is set (output will be stored in file set by
125
- ``outfile``)
126
- - :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is
127
- not set (depends on ``output_type``)
126
+ - None if ``outfile`` is set (output will be stored in file set by ``outfile``)
127
+ - :class:`pandas.DataFrame` or :class:`numpy.ndarray` if ``outfile`` is not set
128
+ (depends on ``output_type``)
128
129
129
130
Example
130
131
-------
@@ -149,31 +150,22 @@ def grd2xyz(grid, output_type="pandas", outfile=None, **kwargs):
149
150
"or 'file'."
150
151
)
151
152
152
- # Set the default column names for the pandas dataframe header
153
- dataframe_header = ["x" , "y" , "z" ]
153
+ # Set the default column names for the pandas dataframe header.
154
+ column_names : list [ Hashable ] = ["x" , "y" , "z" ]
154
155
# Let output pandas column names match input DataArray dimension names
155
- if isinstance (grid , xr .DataArray ) and output_type == "pandas" :
156
+ if output_type == "pandas" and isinstance (grid , xr .DataArray ):
156
157
# Reverse the dims because it is rows, columns ordered.
157
- dataframe_header = [grid .dims [1 ], grid .dims [0 ], grid .name ]
158
-
159
- with GMTTempFile () as tmpfile :
160
- with Session () as lib :
161
- with lib .virtualfile_in (check_kind = "raster" , data = grid ) as vingrd :
162
- if outfile is None :
163
- outfile = tmpfile .name
164
- lib .call_module (
165
- module = "grd2xyz" ,
166
- args = build_arg_string (kwargs , infile = vingrd , outfile = outfile ),
167
- )
168
-
169
- # Read temporary csv output to a pandas table
170
- if outfile == tmpfile .name : # if user did not set outfile, return pd.DataFrame
171
- result = pd .read_csv (
172
- tmpfile .name , sep = "\t " , names = dataframe_header , comment = ">"
158
+ column_names = [grid .dims [1 ], grid .dims [0 ], grid .name ]
159
+
160
+ with Session () as lib :
161
+ with (
162
+ lib .virtualfile_in (check_kind = "raster" , data = grid ) as vingrd ,
163
+ lib .virtualfile_out (kind = "dataset" , fname = outfile ) as vouttbl ,
164
+ ):
165
+ lib .call_module (
166
+ module = "grd2xyz" ,
167
+ args = build_arg_string (kwargs , infile = vingrd , outfile = vouttbl ),
168
+ )
169
+ return lib .virtualfile_to_dataset (
170
+ output_type = output_type , vfname = vouttbl , column_names = column_names
173
171
)
174
- elif outfile != tmpfile .name : # return None if outfile set, output in outfile
175
- result = None
176
-
177
- if output_type == "numpy" :
178
- result = result .to_numpy ()
179
- return result
0 commit comments