Skip to content

Commit f96d708

Browse files
authored
Test regexes for _guess_coord_axis (#172)
1 parent d05c59f commit f96d708

File tree

2 files changed

+74
-22
lines changed

2 files changed

+74
-22
lines changed

cf_xarray/accessor.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116

117117
#: regular expressions for guess_coord_axis
118118
regex = {
119-
"time": "time[0-9]*|min|hour|day|week|month|year",
119+
"time": "(time|min|hour|day|week|month|year)[0-9]*",
120120
"vertical": (
121121
"(lv_|bottom_top|sigma|h(ei)?ght|altitude|depth|isobaric|pres|"
122122
"isotherm)[a-z_]*[0-9]*"
@@ -130,16 +130,16 @@
130130
regex["T"] = regex["time"]
131131

132132

133-
attrs = {
133+
ATTRS = {
134134
"X": {"axis": "X"},
135135
"T": {"axis": "T", "standard_name": "time"},
136136
"Y": {"axis": "Y"},
137137
"Z": {"axis": "Z"},
138138
"latitude": {"units": "degrees_north", "standard_name": "latitude"},
139139
"longitude": {"units": "degrees_east", "standard_name": "longitude"},
140140
}
141-
attrs["time"] = attrs["T"]
142-
attrs["vertical"] = attrs["Z"]
141+
ATTRS["time"] = ATTRS["T"]
142+
ATTRS["vertical"] = ATTRS["Z"]
143143

144144

145145
# Type for Mapper functions
@@ -1361,7 +1361,7 @@ def guess_coord_axis(self, verbose: bool = False) -> Union[DataArray, Dataset]:
13611361
print(
13621362
f"I think {var!r} is of type 'time'. It has a datetime-like type."
13631363
)
1364-
obj[var].attrs = dict(ChainMap(obj[var].attrs, attrs["time"]))
1364+
obj[var].attrs = dict(ChainMap(obj[var].attrs, ATTRS["time"]))
13651365
continue # prevent second detection
13661366

13671367
for axis, pattern in regex.items():
@@ -1371,7 +1371,7 @@ def guess_coord_axis(self, verbose: bool = False) -> Union[DataArray, Dataset]:
13711371
print(
13721372
f"I think {var!r} is of type {axis!r}. It matched {pattern!r}"
13731373
)
1374-
obj[var].attrs = dict(ChainMap(obj[var].attrs, attrs[axis]))
1374+
obj[var].attrs = dict(ChainMap(obj[var].attrs, ATTRS[axis]))
13751375
return obj
13761376

13771377

cf_xarray/tests/test_accessor.py

Lines changed: 68 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import itertools
12
from textwrap import dedent
23

34
import matplotlib as mpl
@@ -627,26 +628,77 @@ def test_docstring():
627628
assert "One or more of ('X'" in airds.cf.mean.__doc__
628629

629630

630-
def test_guess_coord_axis():
631+
def _make_names(prefixes):
632+
suffixes = ["", "a", "_a", "0", "_0"]
633+
return [
634+
f"{prefix}{suffix}" for prefix, suffix in itertools.product(prefixes, suffixes)
635+
]
636+
637+
638+
_TIME_NAMES = _make_names(
639+
[
640+
"time",
641+
"min",
642+
"hour",
643+
"day",
644+
"week",
645+
"month",
646+
"year",
647+
]
648+
)
649+
_VERTICAL_NAMES = _make_names(
650+
[
651+
"lv_1",
652+
"bottom_top",
653+
"sigma",
654+
"sigma_w",
655+
"hght",
656+
"height",
657+
"altitude",
658+
"depth",
659+
"isobaric",
660+
"pressure",
661+
"isotherm",
662+
]
663+
)
664+
_X_NAMES = _make_names(["x"])
665+
_Y_NAMES = _make_names(["y"])
666+
_Z_NAMES = _VERTICAL_NAMES
667+
_LATITUDE_NAMES = _make_names(["lat", "latitude"])
668+
_LONGITUDE_NAMES = _make_names(["lon", "longitude"])
669+
670+
671+
@pytest.mark.parametrize(
672+
"kind, names",
673+
[
674+
["X", _X_NAMES],
675+
["Y", _Y_NAMES],
676+
["Z", _Z_NAMES],
677+
["T", _TIME_NAMES],
678+
["latitude", _LATITUDE_NAMES],
679+
["longitude", _LONGITUDE_NAMES],
680+
],
681+
)
682+
def test_guess_coord_axis(kind, names):
683+
from cf_xarray.accessor import ATTRS
684+
685+
for varname in names:
686+
ds = xr.Dataset()
687+
ds[varname] = (varname, [1, 2, 3, 4, 5])
688+
dsnew = ds.cf.guess_coord_axis()
689+
assert dsnew[varname].attrs == ATTRS[kind]
690+
691+
varname = varname.upper()
692+
ds[varname] = (varname, [1, 2, 3, 4, 5])
693+
dsnew = ds.cf.guess_coord_axis()
694+
assert dsnew[varname].attrs == ATTRS[kind]
695+
696+
697+
def test_guess_coord_axis_datetime():
631698
ds = xr.Dataset()
632699
ds["time"] = ("time", pd.date_range("2001-01-01", "2001-04-01"))
633-
ds["lon_rho"] = ("lon_rho", [1, 2, 3, 4, 5])
634-
ds["lat_rho"] = ("lat_rho", [1, 2, 3, 4, 5])
635-
ds["x1"] = ("x1", [1, 2, 3, 4, 5])
636-
ds["y1"] = ("y1", [1, 2, 3, 4, 5])
637-
638700
dsnew = ds.cf.guess_coord_axis()
639701
assert dsnew.time.attrs == {"standard_name": "time", "axis": "T"}
640-
assert dsnew.lon_rho.attrs == {
641-
"standard_name": "longitude",
642-
"units": "degrees_east",
643-
}
644-
assert dsnew.lat_rho.attrs == {
645-
"standard_name": "latitude",
646-
"units": "degrees_north",
647-
}
648-
assert dsnew.x1.attrs == {"axis": "X"}
649-
assert dsnew.y1.attrs == {"axis": "Y"}
650702

651703

652704
def test_attributes():

0 commit comments

Comments
 (0)