12
12
import warnings
13
13
import webbrowser
14
14
from collections .abc import Iterable , Sequence
15
- from typing import Any
15
+ from typing import Any , Literal
16
16
17
17
import xarray as xr
18
18
from pygmt .encodings import charset
@@ -79,6 +79,10 @@ def _validate_data_input(
79
79
Traceback (most recent call last):
80
80
...
81
81
pygmt.exceptions.GMTInvalidInput: Too much data. Use either data or x/y/z.
82
+ >>> _validate_data_input(data="infile", x=[1, 2, 3], y=[4, 5, 6])
83
+ Traceback (most recent call last):
84
+ ...
85
+ pygmt.exceptions.GMTInvalidInput: Too much data. Use either data or x/y/z.
82
86
>>> _validate_data_input(data="infile", z=[7, 8, 9])
83
87
Traceback (most recent call last):
84
88
...
@@ -111,77 +115,69 @@ def _validate_data_input(
111
115
raise GMTInvalidInput ("data must provide x, y, and z columns." )
112
116
113
117
114
- def data_kind (data = None , x = None , y = None , z = None , required_z = False , required_data = True ):
118
+ def data_kind (
119
+ data : Any = None , required : bool = True
120
+ ) -> Literal ["arg" , "file" , "geojson" , "grid" , "image" , "matrix" , "vectors" ]:
115
121
"""
116
- Check what kind of data is provided to a module.
122
+ Check the kind of data that is provided to a module.
117
123
118
- Possible types:
124
+ The ``data`` argument can be in any type, but only following types are supported :
119
125
120
- * a file name provided as 'data'
121
- * a pathlib.PurePath object provided as 'data'
122
- * an xarray.DataArray object provided as 'data'
123
- * a 2-D matrix provided as 'data'
124
- * 1-D arrays x and y (and z, optionally)
125
- * an optional argument (None, bool, int or float) provided as 'data'
126
-
127
- Arguments should be ``None`` if not used. If doesn't fit any of these
128
- categories (or fits more than one), will raise an exception.
126
+ - a string or a :class:`pathlib.PurePath` object or a sequence of them, representing
127
+ a file name or a list of file names
128
+ - a 2-D or 3-D :class:`xarray.DataArray` object
129
+ - a 2-D matrix
130
+ - None, bool, int or float type representing an optional arguments
131
+ - a geo-like Python object that implements ``__geo_interface__`` (e.g.,
132
+ geopandas.GeoDataFrame or shapely.geometry)
129
133
130
134
Parameters
131
135
----------
132
136
data : str, pathlib.PurePath, None, bool, xarray.DataArray or {table-like}
133
137
Pass in either a file name or :class:`pathlib.Path` to an ASCII data
134
138
table, an :class:`xarray.DataArray`, a 1-D/2-D
135
139
{table-classes} or an option argument.
136
- x/y : 1-D arrays or None
137
- x and y columns as numpy arrays.
138
- z : 1-D array or None
139
- z column as numpy array. To be used optionally when x and y are given.
140
- required_z : bool
141
- State whether the 'z' column is required.
142
- required_data : bool
140
+ required
143
141
Set to True when 'data' is required, or False when dealing with
144
142
optional virtual files. [Default is True].
145
143
146
144
Returns
147
145
-------
148
- kind : str
149
- One of ``'arg'``, ``'file'``, ``'grid'``, ``image``, ``'geojson'``,
150
- ``'matrix'``, or ``'vectors'``.
146
+ kind
147
+ The data kind.
151
148
152
149
Examples
153
150
--------
154
-
155
151
>>> import numpy as np
156
152
>>> import xarray as xr
157
153
>>> import pathlib
158
- >>> data_kind(data=None, x=np.array([1, 2, 3]), y=np.array([4, 5, 6]) )
154
+ >>> data_kind(data=None)
159
155
'vectors'
160
- >>> data_kind(data=np.arange(10).reshape((5, 2)), x=None, y=None )
156
+ >>> data_kind(data=np.arange(10).reshape((5, 2)))
161
157
'matrix'
162
- >>> data_kind(data="my-data-file.txt", x=None, y=None )
158
+ >>> data_kind(data="my-data-file.txt")
163
159
'file'
164
- >>> data_kind(data=pathlib.Path("my-data-file.txt"), x=None, y=None )
160
+ >>> data_kind(data=pathlib.Path("my-data-file.txt"))
165
161
'file'
166
- >>> data_kind(data=None, x=None, y=None, required_data =False)
162
+ >>> data_kind(data=None, required =False)
167
163
'arg'
168
- >>> data_kind(data=2.0, x=None, y=None, required_data =False)
164
+ >>> data_kind(data=2.0, required =False)
169
165
'arg'
170
- >>> data_kind(data=True, x=None, y=None, required_data =False)
166
+ >>> data_kind(data=True, required =False)
171
167
'arg'
172
168
>>> data_kind(data=xr.DataArray(np.random.rand(4, 3)))
173
169
'grid'
174
170
>>> data_kind(data=xr.DataArray(np.random.rand(3, 4, 5)))
175
171
'image'
176
172
"""
177
- # determine the data kind
173
+ kind : Literal [ "arg" , "file" , "geojson" , "grid" , "image" , "matrix" , "vectors" ]
178
174
if isinstance (data , str | pathlib .PurePath ) or (
179
175
isinstance (data , list | tuple )
180
176
and all (isinstance (_file , str | pathlib .PurePath ) for _file in data )
181
177
):
182
178
# One or more files
183
179
kind = "file"
184
- elif isinstance (data , bool | int | float ) or (data is None and not required_data ):
180
+ elif isinstance (data , bool | int | float ) or (data is None and not required ):
185
181
kind = "arg"
186
182
elif isinstance (data , xr .DataArray ):
187
183
kind = "image" if len (data .dims ) == 3 else "grid"
@@ -193,15 +189,6 @@ def data_kind(data=None, x=None, y=None, z=None, required_z=False, required_data
193
189
kind = "matrix"
194
190
else :
195
191
kind = "vectors"
196
- _validate_data_input (
197
- data = data ,
198
- x = x ,
199
- y = y ,
200
- z = z ,
201
- required_z = required_z ,
202
- required_data = required_data ,
203
- kind = kind ,
204
- )
205
192
return kind
206
193
207
194
0 commit comments