Skip to content

Commit 5682bdf

Browse files
authored
Some CMIP6 support
both for autoguessing and long_name == cell index along (first|second) dimension
1 parent d2d750f commit 5682bdf

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

cf_xarray/criteria.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66

77

8+
import copy
89
import re
910
from typing import MutableMapping, Tuple
1011

@@ -74,19 +75,20 @@
7475
}
7576

7677
# "long_name" and "standard_name" criteria are the same. For convenience.
77-
coordinate_criteria["long_name"] = coordinate_criteria["standard_name"]
78+
coordinate_criteria["long_name"] = copy.deepcopy(coordinate_criteria["standard_name"])
79+
coordinate_criteria["long_name"]["X"] += ("cell index along first dimension",)
80+
coordinate_criteria["long_name"]["Y"] += ("cell index along second dimension",)
7881

7982
#: regular expressions for guess_coord_axis
8083
regex = {
8184
"time": re.compile("\\bt\\b|(time|min|hour|day|week|month|year)[0-9]*"),
82-
"vertical": re.compile(
83-
"(z|nav_lev|gdep|lv_|bottom_top|sigma|h(ei)?ght|altitude|depth|"
85+
"Z": re.compile(
86+
"(z|nav_lev|gdep|lv_|[o]*lev|bottom_top|sigma|h(ei)?ght|altitude|depth|"
8487
"isobaric|pres|isotherm)[a-z_]*[0-9]*"
8588
),
86-
"Y": re.compile("y"),
89+
"Y": re.compile("y|j|nlat|nj"),
8790
"latitude": re.compile("y?(nav_lat|lat|gphi)[a-z0-9]*"),
88-
"X": re.compile("x"),
91+
"X": re.compile("x|i|nlon|ni"),
8992
"longitude": re.compile("x?(nav_lon|lon|glam)[a-z0-9]*"),
9093
}
91-
regex["Z"] = regex["vertical"]
9294
regex["T"] = regex["time"]

cf_xarray/tests/test_accessor.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -780,9 +780,9 @@ def _make_names(prefixes):
780780
"nav_lev",
781781
]
782782
)
783-
_X_NAMES = _make_names(["x"])
784-
_Y_NAMES = _make_names(["y"])
785-
_Z_NAMES = _VERTICAL_NAMES
783+
_X_NAMES = _make_names(["x", "nlon", "i", "ni"])
784+
_Y_NAMES = _make_names(["y", "nlat", "j", "nj"])
785+
_Z_NAMES = _VERTICAL_NAMES + ["olevel", "level", "zlevel"]
786786
_LATITUDE_NAMES = _make_names(["lat", "latitude", "gphi", "nav_lat"])
787787
_LONGITUDE_NAMES = _make_names(["lon", "longitude", "glam", "nav_lon"])
788788

@@ -1193,3 +1193,24 @@ def test_differentiate_positive_upward(obj):
11931193
obj.z.attrs["positive"] = "zzz"
11941194
with pytest.raises(ValueError):
11951195
obj.cf.differentiate("z", positive_upward=True)
1196+
1197+
1198+
def test_cmip6_attrs():
1199+
da = xr.DataArray(
1200+
np.ones((10, 10)),
1201+
dims=("nlon", "nlat"),
1202+
coords={
1203+
"nlon": (
1204+
"nlon",
1205+
np.arange(10),
1206+
{"long_name": "cell index along first dimension"},
1207+
),
1208+
"nlat": (
1209+
"nlat",
1210+
np.arange(10),
1211+
{"long_name": "cell index along second dimension"},
1212+
),
1213+
},
1214+
)
1215+
assert da.cf.axes["X"] == ["nlon"]
1216+
assert da.cf.axes["Y"] == ["nlat"]

0 commit comments

Comments
 (0)