Skip to content

Commit

Permalink
Some CMIP6 support
Browse files Browse the repository at this point in the history
both for autoguessing and long_name == cell index along (first|second) dimension
  • Loading branch information
dcherian authored May 11, 2021
1 parent d2d750f commit 5682bdf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
14 changes: 8 additions & 6 deletions cf_xarray/criteria.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""


import copy
import re
from typing import MutableMapping, Tuple

Expand Down Expand Up @@ -74,19 +75,20 @@
}

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

#: regular expressions for guess_coord_axis
regex = {
"time": re.compile("\\bt\\b|(time|min|hour|day|week|month|year)[0-9]*"),
"vertical": re.compile(
"(z|nav_lev|gdep|lv_|bottom_top|sigma|h(ei)?ght|altitude|depth|"
"Z": re.compile(
"(z|nav_lev|gdep|lv_|[o]*lev|bottom_top|sigma|h(ei)?ght|altitude|depth|"
"isobaric|pres|isotherm)[a-z_]*[0-9]*"
),
"Y": re.compile("y"),
"Y": re.compile("y|j|nlat|nj"),
"latitude": re.compile("y?(nav_lat|lat|gphi)[a-z0-9]*"),
"X": re.compile("x"),
"X": re.compile("x|i|nlon|ni"),
"longitude": re.compile("x?(nav_lon|lon|glam)[a-z0-9]*"),
}
regex["Z"] = regex["vertical"]
regex["T"] = regex["time"]
27 changes: 24 additions & 3 deletions cf_xarray/tests/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,9 +780,9 @@ def _make_names(prefixes):
"nav_lev",
]
)
_X_NAMES = _make_names(["x"])
_Y_NAMES = _make_names(["y"])
_Z_NAMES = _VERTICAL_NAMES
_X_NAMES = _make_names(["x", "nlon", "i", "ni"])
_Y_NAMES = _make_names(["y", "nlat", "j", "nj"])
_Z_NAMES = _VERTICAL_NAMES + ["olevel", "level", "zlevel"]
_LATITUDE_NAMES = _make_names(["lat", "latitude", "gphi", "nav_lat"])
_LONGITUDE_NAMES = _make_names(["lon", "longitude", "glam", "nav_lon"])

Expand Down Expand Up @@ -1193,3 +1193,24 @@ def test_differentiate_positive_upward(obj):
obj.z.attrs["positive"] = "zzz"
with pytest.raises(ValueError):
obj.cf.differentiate("z", positive_upward=True)


def test_cmip6_attrs():
da = xr.DataArray(
np.ones((10, 10)),
dims=("nlon", "nlat"),
coords={
"nlon": (
"nlon",
np.arange(10),
{"long_name": "cell index along first dimension"},
),
"nlat": (
"nlat",
np.arange(10),
{"long_name": "cell index along second dimension"},
),
},
)
assert da.cf.axes["X"] == ["nlon"]
assert da.cf.axes["Y"] == ["nlat"]

0 comments on commit 5682bdf

Please sign in to comment.