|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import itertools |
| 6 | +from collections import ChainMap |
6 | 7 | from typing import ( |
7 | 8 | Any, |
8 | 9 | Callable, |
|
25 | 26 |
|
26 | 27 | import cf_pandas as cfp |
27 | 28 |
|
28 | | -from .criteria import coordinate_criteria |
| 29 | +from .criteria import coordinate_criteria, guess_regex |
29 | 30 | from .options import OPTIONS |
30 | | -from .utils import always_iterable, match_criteria_key, set_up_criteria |
| 31 | +from .utils import ( |
| 32 | + _is_datetime_like, |
| 33 | + always_iterable, |
| 34 | + match_criteria_key, |
| 35 | + set_up_criteria, |
| 36 | +) |
31 | 37 | from .vocab import Vocab |
32 | 38 |
|
33 | 39 | #: `axis` names understood by cf_xarray |
@@ -195,7 +201,6 @@ def axes(self) -> Dict[str, List[str]]: |
195 | 201 | """ |
196 | 202 | # vardict = {key: self.__getitem__(key) for key in _AXIS_NAMES} |
197 | 203 | vardict = {key: _get_all(self._obj, key) for key in _AXIS_NAMES} |
198 | | - |
199 | 204 | return {k: sorted(v) for k, v in vardict.items() if v} |
200 | 205 |
|
201 | 206 | @property |
@@ -275,23 +280,28 @@ def standard_names(self): |
275 | 280 |
|
276 | 281 | def _get_axis_coord(obj: Union[DataFrame, Series], key: str) -> list: |
277 | 282 | """ |
278 | | - Translate from axis or coord name to variable name |
| 283 | + Translate from axis or coord name to variable name. After matching based on coordinate_criteria, |
| 284 | + if there are no matches for key, then guess_regex is used to search for matches. |
| 285 | +
|
279 | 286 | Parameters |
280 | 287 | ---------- |
281 | 288 | obj : DataArray, Dataset |
282 | 289 | DataArray belonging to the coordinate to be checked |
283 | 290 | key : str, ["X", "Y", "Z", "T", "longitude", "latitude", "vertical", "time"] |
284 | 291 | key to check for. |
| 292 | +
|
285 | 293 | Returns |
286 | 294 | ------- |
287 | 295 | List[str], Variable name(s) in parent xarray object that matches axis or coordinate `key` |
| 296 | +
|
288 | 297 | Notes |
289 | 298 | ----- |
290 | 299 | This functions checks for the following attributes in order |
291 | 300 | - `standard_name` (CF option) |
292 | 301 | - `_CoordinateAxisType` (from THREDDS) |
293 | 302 | - `axis` (CF option) |
294 | 303 | - `positive` (CF standard for non-pressure vertical coordinate) |
| 304 | +
|
295 | 305 | References |
296 | 306 | ---------- |
297 | 307 | MetPy's parse_cf |
@@ -340,6 +350,18 @@ def _get_axis_coord(obj: Union[DataFrame, Series], key: str) -> list: |
340 | 350 | # units = getattr(col.data, "units", None) |
341 | 351 | # if units in expected: |
342 | 352 | # results.update((col,)) |
| 353 | + |
| 354 | + # also use the guess_regex approach by default, but only if no results so far |
| 355 | + # this takes the logic from cf-xarray guess_coord_axis |
| 356 | + if len(results) == 0: |
| 357 | + if obj[col].ndim == 1 and _is_datetime_like(obj[col]): |
| 358 | + results.update((col,)) |
| 359 | + continue # prevent second detection |
| 360 | + |
| 361 | + pattern = guess_regex[key] |
| 362 | + if pattern.match(col.lower()): |
| 363 | + results.update((col,)) |
| 364 | + |
343 | 365 | return list(results) |
344 | 366 |
|
345 | 367 |
|
|
0 commit comments