Skip to content

Commit 21c0387

Browse files
committed
Separate "X", "Y" from "latitude", "longitude".
"X" and "Y" are identified by "axis: X" and "axis: Y" "latitude" and "longitude" are identified by standard name, units "vertical", "Z" are synonymous "T", "time" are also synonymous I think this is sensible ;)
1 parent 4042165 commit 21c0387

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

cf_xarray/accessor.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616

1717
_AXIS_NAMES = ("X", "Y", "Z", "T")
1818
_COORD_NAMES = ("longitude", "latitude", "vertical", "time")
19-
_COORD_AXIS_MAPPING = dict(zip(_COORD_NAMES, _AXIS_NAMES))
2019
_CELL_MEASURES = ("area", "volume")
2120

2221
# Define the criteria for coordinate matches
2322
# Copied from metpy
2423
# Internally we only use X, Y, Z, T
25-
# TODO: Metpy adds latitude and longitude separately so we may revert to doing that too
26-
coordinate_criteria = {
24+
coordinate_criteria: dict = {
2725
"standard_name": {
2826
"T": ("time",),
27+
"time": ("time",),
2928
"Z": (
3029
"air_pressure",
3130
"height",
@@ -41,28 +40,29 @@
4140
"height_above_reference_ellipsoid",
4241
"height_above_mean_sea_level",
4342
),
44-
"Y": ("latitude",),
45-
"X": ("longitude",),
43+
"latitude": ("latitude",),
44+
"longitude": ("longitude",),
4645
},
47-
"long_name": {"T": ("time",)},
4846
"_CoordinateAxisType": {
4947
"T": ("Time",),
5048
"Z": ("GeoZ", "Height", "Pressure"),
51-
"Y": ("GeoY", "Lat"),
52-
"X": ("GeoX", "Lon"),
49+
"Y": ("GeoY",),
50+
"latitude": ("Lat",),
51+
"X": ("GeoX",),
52+
"longitude": ("Lon",),
5353
},
5454
"axis": {"T": ("T",), "Z": ("Z",), "Y": ("Y",), "X": ("X",)},
55-
"positive": {"Z": ("up", "down")},
55+
"positive": {"Z": ("up", "down"), "vertical": ("up", "down")},
5656
"units": {
57-
"Y": (
57+
"latitude": (
5858
"degree_north",
5959
"degree_N",
6060
"degreeN",
6161
"degrees_north",
6262
"degrees_N",
6363
"degreesN",
6464
),
65-
"X": (
65+
"longitude": (
6666
"degree_east",
6767
"degree_E",
6868
"degreeE",
@@ -84,6 +84,11 @@
8484
# },
8585
}
8686

87+
coordinate_criteria["standard_name"]["vertical"] = coordinate_criteria["standard_name"][
88+
"Z"
89+
]
90+
coordinate_criteria["long_name"] = coordinate_criteria["standard_name"]
91+
8792

8893
def _get_axis_coord_single(var, key, *args):
8994
""" Helper method for when we really want only one result per key. """
@@ -134,22 +139,12 @@ def _get_axis_coord(
134139
MetPy's parse_cf
135140
"""
136141

137-
axis = None
138-
if key in _COORD_NAMES:
139-
coord = key
140-
axis = _COORD_AXIS_MAPPING[key]
141-
elif key in _AXIS_NAMES:
142-
coord = ""
143-
axis = key
144-
else:
142+
if key not in _COORD_NAMES and key not in _AXIS_NAMES:
145143
if error:
146144
raise KeyError(f"Did not understand {key}")
147145
else:
148146
return [default]
149147

150-
if axis is None:
151-
raise AssertionError("Should be unreachable")
152-
153148
if "coordinates" in var.encoding:
154149
search_in = var.encoding["coordinates"].split(" ")
155150
elif "coordinates" in var.attrs:
@@ -160,8 +155,8 @@ def _get_axis_coord(
160155
results: Set = set()
161156
for coord in search_in:
162157
for criterion, valid_values in coordinate_criteria.items():
163-
if axis in valid_values: # type: ignore
164-
expected = valid_values[axis] # type: ignore
158+
if key in valid_values: # type: ignore
159+
expected = valid_values[key] # type: ignore
165160
if var.coords[coord].attrs.get(criterion, None) in expected:
166161
results.update((coord,))
167162

0 commit comments

Comments
 (0)