Skip to content

Commit 9e86af8

Browse files
authored
Refactor out coordinate criteria to criteria.py (#205)
Closes #28
1 parent 9537914 commit 9e86af8

File tree

3 files changed

+94
-88
lines changed

3 files changed

+94
-88
lines changed

cf_xarray/accessor.py

Lines changed: 1 addition & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from xarray import DataArray, Dataset
2424
from xarray.core.arithmetic import SupportsArithmetic
2525

26+
from .criteria import coordinate_criteria, regex
2627
from .helpers import bounds_to_vertices
2728
from .utils import _is_datetime_like, invert_mappings, parse_cell_methods_attr
2829

@@ -44,93 +45,6 @@
4445
#: Cell measures understood by cf_xarray.
4546
_CELL_MEASURES = ("area", "volume")
4647

47-
# Define the criteria for coordinate matches
48-
# Copied from metpy
49-
# Internally we only use X, Y, Z, T
50-
coordinate_criteria: MutableMapping[str, MutableMapping[str, Tuple]] = {
51-
"standard_name": {
52-
"X": ("projection_x_coordinate",),
53-
"Y": ("projection_y_coordinate",),
54-
"T": ("time",),
55-
"time": ("time",),
56-
"vertical": (
57-
"air_pressure",
58-
"height",
59-
"depth",
60-
"geopotential_height",
61-
# computed dimensional coordinate name
62-
"altitude",
63-
"height_above_geopotential_datum",
64-
"height_above_reference_ellipsoid",
65-
"height_above_mean_sea_level",
66-
),
67-
"Z": (
68-
"model_level_number",
69-
"atmosphere_ln_pressure_coordinate",
70-
"atmosphere_sigma_coordinate",
71-
"atmosphere_hybrid_sigma_pressure_coordinate",
72-
"atmosphere_hybrid_height_coordinate",
73-
"atmosphere_sleve_coordinate",
74-
"ocean_sigma_coordinate",
75-
"ocean_s_coordinate",
76-
"ocean_s_coordinate_g1",
77-
"ocean_s_coordinate_g2",
78-
"ocean_sigma_z_coordinate",
79-
"ocean_double_sigma_coordinate",
80-
),
81-
"latitude": ("latitude",),
82-
"longitude": ("longitude",),
83-
},
84-
"_CoordinateAxisType": {
85-
"T": ("Time",),
86-
"Z": ("GeoZ", "Height", "Pressure"),
87-
"Y": ("GeoY",),
88-
"latitude": ("Lat",),
89-
"X": ("GeoX",),
90-
"longitude": ("Lon",),
91-
},
92-
"axis": {"T": ("T",), "Z": ("Z",), "Y": ("Y",), "X": ("X",)},
93-
"cartesian_axis": {"T": ("T",), "Z": ("Z",), "Y": ("Y",), "X": ("X",)},
94-
"positive": {"vertical": ("up", "down")},
95-
"units": {
96-
"latitude": (
97-
"degree_north",
98-
"degree_N",
99-
"degreeN",
100-
"degrees_north",
101-
"degrees_N",
102-
"degreesN",
103-
),
104-
"longitude": (
105-
"degree_east",
106-
"degree_E",
107-
"degreeE",
108-
"degrees_east",
109-
"degrees_E",
110-
"degreesE",
111-
),
112-
},
113-
}
114-
115-
# "long_name" and "standard_name" criteria are the same. For convenience.
116-
coordinate_criteria["long_name"] = coordinate_criteria["standard_name"]
117-
118-
#: regular expressions for guess_coord_axis
119-
regex = {
120-
"time": "\\bt\\b|(time|min|hour|day|week|month|year)[0-9]*",
121-
"vertical": (
122-
"(z|nav_lev|gdep|lv_|bottom_top|sigma|h(ei)?ght|altitude|depth|"
123-
"isobaric|pres|isotherm)[a-z_]*[0-9]*"
124-
),
125-
"Y": "y",
126-
"latitude": "y?(nav_lat|lat|gphi)[a-z0-9]*",
127-
"X": "x",
128-
"longitude": "x?(nav_lon|lon|glam)[a-z0-9]*",
129-
}
130-
regex["Z"] = regex["vertical"]
131-
regex["T"] = regex["time"]
132-
133-
13448
ATTRS = {
13549
"X": {"axis": "X"},
13650
"T": {"axis": "T", "standard_name": "time"},

cf_xarray/criteria.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""
2+
Criteria for identifying axes and coordinate variables.
3+
Reused with modification from MetPy under the terms of the BSD 3-Clause License.
4+
Copyright (c) 2017 MetPy Developers.
5+
"""
6+
7+
8+
from typing import MutableMapping, Tuple
9+
10+
coordinate_criteria: MutableMapping[str, MutableMapping[str, Tuple]] = {
11+
"standard_name": {
12+
"X": ("projection_x_coordinate",),
13+
"Y": ("projection_y_coordinate",),
14+
"T": ("time",),
15+
"time": ("time",),
16+
"vertical": (
17+
"air_pressure",
18+
"height",
19+
"depth",
20+
"geopotential_height",
21+
# computed dimensional coordinate name
22+
"altitude",
23+
"height_above_geopotential_datum",
24+
"height_above_reference_ellipsoid",
25+
"height_above_mean_sea_level",
26+
),
27+
"Z": (
28+
"model_level_number",
29+
"atmosphere_ln_pressure_coordinate",
30+
"atmosphere_sigma_coordinate",
31+
"atmosphere_hybrid_sigma_pressure_coordinate",
32+
"atmosphere_hybrid_height_coordinate",
33+
"atmosphere_sleve_coordinate",
34+
"ocean_sigma_coordinate",
35+
"ocean_s_coordinate",
36+
"ocean_s_coordinate_g1",
37+
"ocean_s_coordinate_g2",
38+
"ocean_sigma_z_coordinate",
39+
"ocean_double_sigma_coordinate",
40+
),
41+
"latitude": ("latitude",),
42+
"longitude": ("longitude",),
43+
},
44+
"_CoordinateAxisType": {
45+
"T": ("Time",),
46+
"Z": ("GeoZ", "Height", "Pressure"),
47+
"Y": ("GeoY",),
48+
"latitude": ("Lat",),
49+
"X": ("GeoX",),
50+
"longitude": ("Lon",),
51+
},
52+
"axis": {"T": ("T",), "Z": ("Z",), "Y": ("Y",), "X": ("X",)},
53+
"cartesian_axis": {"T": ("T",), "Z": ("Z",), "Y": ("Y",), "X": ("X",)},
54+
"positive": {"vertical": ("up", "down")},
55+
"units": {
56+
"latitude": (
57+
"degree_north",
58+
"degree_N",
59+
"degreeN",
60+
"degrees_north",
61+
"degrees_N",
62+
"degreesN",
63+
),
64+
"longitude": (
65+
"degree_east",
66+
"degree_E",
67+
"degreeE",
68+
"degrees_east",
69+
"degrees_E",
70+
"degreesE",
71+
),
72+
},
73+
}
74+
75+
# "long_name" and "standard_name" criteria are the same. For convenience.
76+
coordinate_criteria["long_name"] = coordinate_criteria["standard_name"]
77+
78+
#: regular expressions for guess_coord_axis
79+
regex = {
80+
"time": "\\bt\\b|(time|min|hour|day|week|month|year)[0-9]*",
81+
"vertical": (
82+
"(z|nav_lev|gdep|lv_|bottom_top|sigma|h(ei)?ght|altitude|depth|"
83+
"isobaric|pres|isotherm)[a-z_]*[0-9]*"
84+
),
85+
"Y": "y",
86+
"latitude": "y?(nav_lat|lat|gphi)[a-z0-9]*",
87+
"X": "x",
88+
"longitude": "x?(nav_lon|lon|glam)[a-z0-9]*",
89+
}
90+
regex["Z"] = regex["vertical"]
91+
regex["T"] = regex["time"]

cf_xarray/scripts/make_doc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from pandas import DataFrame
66

7-
from cf_xarray.accessor import _AXIS_NAMES, _COORD_NAMES, coordinate_criteria, regex
7+
from cf_xarray.accessor import _AXIS_NAMES, _COORD_NAMES
8+
from cf_xarray.criteria import coordinate_criteria, regex
89

910

1011
def main():

0 commit comments

Comments
 (0)