@@ -188,30 +188,37 @@ def _check_encoding(
188
188
189
189
190
190
def data_kind (
191
- data : Any = None , required : bool = True
191
+ data : Any , required : bool = True
192
192
) -> Literal ["arg" , "file" , "geojson" , "grid" , "image" , "matrix" , "vectors" ]:
193
193
"""
194
194
Check the kind of data that is provided to a module.
195
195
196
- The `` data`` argument can be in any type, but only following types are supported :
196
+ Recognized data kinds are:
197
197
198
- - a string or a :class:`pathlib.PurePath` object or a sequence of them, representing
199
- a file name or a list of file names
200
- - a 2-D or 3-D :class:`xarray.DataArray` object
201
- - a 2-D matrix
202
- - None, bool, int or float type representing an optional arguments
203
- - a geo-like Python object that implements ``__geo_interface__`` (e.g.,
204
- geopandas.GeoDataFrame or shapely.geometry)
198
+ - ``"arg"``: bool, int or float, representing an optional argument, mainly used for
199
+ dealing with optional virtual files
200
+ - ``"file"``: a string or a :class:`pathlib.PurePath` object or a sequence of them,
201
+ representing a file name or a list of file names
202
+ - ``"geojson"``: a geo-like Python object that implements ``__geo_interface__``
203
+ (e.g., geopandas.GeoDataFrame or shapely.geometry)
204
+ - ``"grid"``: a :class:`xarray.DataArray` object with dimensions not equal to 3
205
+ - ``"image"``: a :class:`xarray.DataArray` object with 3 dimensions
206
+ - ``"matrix"``: a 2-D matrix or a sequence of sequences
207
+
208
+ In addition, the data can be given via a series of vectors (e.g., x/y/z). In this
209
+ case, the ``data`` argument is ``None`` and the data kind is determined by the
210
+ ``required`` argument. The data kind is ``"vectors"`` if ``required`` is ``True``,
211
+ otherwise the data kind is ``"arg"``.
212
+
213
+ The function will fallback to ``"matrix"`` for any unrecognized data.
205
214
206
215
Parameters
207
216
----------
208
- data : str, pathlib.PurePath, None, bool, xarray.DataArray or {table-like}
209
- Pass in either a file name or :class:`pathlib.Path` to an ASCII data
210
- table, an :class:`xarray.DataArray`, a 1-D/2-D
211
- {table-classes} or an option argument.
217
+ data
218
+ The data that is provided to a module.
212
219
required
213
- Set to True when ' data' is required, or False when dealing with
214
- optional virtual files. [Default is True] .
220
+ If the data is required or not. Set to `` False`` when dealing with optional
221
+ virtual files.
215
222
216
223
Returns
217
224
-------
@@ -223,45 +230,54 @@ def data_kind(
223
230
>>> import numpy as np
224
231
>>> import xarray as xr
225
232
>>> import pathlib
233
+ >>> [data_kind(data=data) for data in (2, 2.0, True, False)]
234
+ ['arg', 'arg', 'arg', 'arg']
226
235
>>> data_kind(data=None)
227
236
'vectors'
228
- >>> data_kind(data=np.arange(10).reshape((5, 2)) )
229
- 'matrix '
237
+ >>> data_kind(data=None, required=False )
238
+ 'arg '
230
239
>>> data_kind(data="my-data-file.txt")
231
240
'file'
232
241
>>> data_kind(data=pathlib.Path("my-data-file.txt"))
233
242
'file'
234
- >>> data_kind(data=None, required=False)
235
- 'arg'
236
- >>> data_kind(data=2.0, required=False)
237
- 'arg'
238
- >>> data_kind(data=True, required=False)
239
- 'arg'
243
+ >>> data_kind(data=["data1.txt", "data2.txt"])
244
+ 'file'
240
245
>>> data_kind(data=xr.DataArray(np.random.rand(4, 3)))
241
246
'grid'
242
247
>>> data_kind(data=xr.DataArray(np.random.rand(3, 4, 5)))
243
248
'image'
249
+ >>> data_kind(data=np.arange(10).reshape((5, 2)))
250
+ 'matrix'
251
+ >>> data_kind(data=[1, 2, 3])
252
+ 'matrix'
244
253
"""
245
- kind : Literal ["arg" , "file" , "geojson" , "grid" , "image" , "matrix" , "vectors" ]
254
+ # data is None, so data must be given via a series of vectors (i.e., x/y/z).
255
+ # The only exception is when dealing with optional virtual files.
256
+ if data is None :
257
+ return "vectors" if required else "arg"
258
+
259
+ # A file or a list of files
246
260
if isinstance (data , str | pathlib .PurePath ) or (
247
- isinstance (data , list | tuple )
261
+ is_nonstr_iter (data )
248
262
and all (isinstance (_file , str | pathlib .PurePath ) for _file in data )
249
263
):
250
- # One or more files
251
- kind = "file"
252
- elif isinstance (data , bool | int | float ) or (data is None and not required ):
253
- kind = "arg"
254
- elif isinstance (data , xr .DataArray ):
255
- kind = "image" if len (data .dims ) == 3 else "grid"
256
- elif hasattr (data , "__geo_interface__" ):
257
- # geo-like Python object that implements ``__geo_interface__``
258
- # (geopandas.GeoDataFrame or shapely.geometry)
259
- kind = "geojson"
260
- elif data is not None :
261
- kind = "matrix"
262
- else :
263
- kind = "vectors"
264
- return kind
264
+ return "file"
265
+
266
+ # An option argument
267
+ if isinstance (data , bool | int | float ):
268
+ return "arg"
269
+
270
+ # A xr.DataArray grid or image
271
+ if isinstance (data , xr .DataArray ):
272
+ return "image" if len (data .dims ) == 3 else "grid"
273
+
274
+ # Geo-like Python object that implements ``__geo_interface__`` (e.g.,
275
+ # geopandas.GeoDataFrame or shapely.geometry)
276
+ if hasattr (data , "__geo_interface__" ):
277
+ return "geojson"
278
+
279
+ # Fallback to "matrix" for anything else
280
+ return "matrix"
265
281
266
282
267
283
def non_ascii_to_octal (
0 commit comments