Skip to content

Refactor out coordinate criteria to criteria.py #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 1 addition & 87 deletions cf_xarray/accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from xarray import DataArray, Dataset
from xarray.core.arithmetic import SupportsArithmetic

from .criteria import coordinate_criteria, regex
from .helpers import bounds_to_vertices
from .utils import _is_datetime_like, invert_mappings, parse_cell_methods_attr

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

# Define the criteria for coordinate matches
# Copied from metpy
# Internally we only use X, Y, Z, T
coordinate_criteria: MutableMapping[str, MutableMapping[str, Tuple]] = {
"standard_name": {
"X": ("projection_x_coordinate",),
"Y": ("projection_y_coordinate",),
"T": ("time",),
"time": ("time",),
"vertical": (
"air_pressure",
"height",
"depth",
"geopotential_height",
# computed dimensional coordinate name
"altitude",
"height_above_geopotential_datum",
"height_above_reference_ellipsoid",
"height_above_mean_sea_level",
),
"Z": (
"model_level_number",
"atmosphere_ln_pressure_coordinate",
"atmosphere_sigma_coordinate",
"atmosphere_hybrid_sigma_pressure_coordinate",
"atmosphere_hybrid_height_coordinate",
"atmosphere_sleve_coordinate",
"ocean_sigma_coordinate",
"ocean_s_coordinate",
"ocean_s_coordinate_g1",
"ocean_s_coordinate_g2",
"ocean_sigma_z_coordinate",
"ocean_double_sigma_coordinate",
),
"latitude": ("latitude",),
"longitude": ("longitude",),
},
"_CoordinateAxisType": {
"T": ("Time",),
"Z": ("GeoZ", "Height", "Pressure"),
"Y": ("GeoY",),
"latitude": ("Lat",),
"X": ("GeoX",),
"longitude": ("Lon",),
},
"axis": {"T": ("T",), "Z": ("Z",), "Y": ("Y",), "X": ("X",)},
"cartesian_axis": {"T": ("T",), "Z": ("Z",), "Y": ("Y",), "X": ("X",)},
"positive": {"vertical": ("up", "down")},
"units": {
"latitude": (
"degree_north",
"degree_N",
"degreeN",
"degrees_north",
"degrees_N",
"degreesN",
),
"longitude": (
"degree_east",
"degree_E",
"degreeE",
"degrees_east",
"degrees_E",
"degreesE",
),
},
}

# "long_name" and "standard_name" criteria are the same. For convenience.
coordinate_criteria["long_name"] = coordinate_criteria["standard_name"]

#: regular expressions for guess_coord_axis
regex = {
"time": "\\bt\\b|(time|min|hour|day|week|month|year)[0-9]*",
"vertical": (
"(z|nav_lev|gdep|lv_|bottom_top|sigma|h(ei)?ght|altitude|depth|"
"isobaric|pres|isotherm)[a-z_]*[0-9]*"
),
"Y": "y",
"latitude": "y?(nav_lat|lat|gphi)[a-z0-9]*",
"X": "x",
"longitude": "x?(nav_lon|lon|glam)[a-z0-9]*",
}
regex["Z"] = regex["vertical"]
regex["T"] = regex["time"]


ATTRS = {
"X": {"axis": "X"},
"T": {"axis": "T", "standard_name": "time"},
Expand Down
91 changes: 91 additions & 0 deletions cf_xarray/criteria.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""
Criteria for identifying axes and coordinate variables.
Reused with modification from MetPy under the terms of the BSD 3-Clause License.
Copyright (c) 2017 MetPy Developers.
"""


from typing import MutableMapping, Tuple

coordinate_criteria: MutableMapping[str, MutableMapping[str, Tuple]] = {
"standard_name": {
"X": ("projection_x_coordinate",),
"Y": ("projection_y_coordinate",),
"T": ("time",),
"time": ("time",),
"vertical": (
"air_pressure",
"height",
"depth",
"geopotential_height",
# computed dimensional coordinate name
"altitude",
"height_above_geopotential_datum",
"height_above_reference_ellipsoid",
"height_above_mean_sea_level",
),
"Z": (
"model_level_number",
"atmosphere_ln_pressure_coordinate",
"atmosphere_sigma_coordinate",
"atmosphere_hybrid_sigma_pressure_coordinate",
"atmosphere_hybrid_height_coordinate",
"atmosphere_sleve_coordinate",
"ocean_sigma_coordinate",
"ocean_s_coordinate",
"ocean_s_coordinate_g1",
"ocean_s_coordinate_g2",
"ocean_sigma_z_coordinate",
"ocean_double_sigma_coordinate",
),
"latitude": ("latitude",),
"longitude": ("longitude",),
},
"_CoordinateAxisType": {
"T": ("Time",),
"Z": ("GeoZ", "Height", "Pressure"),
"Y": ("GeoY",),
"latitude": ("Lat",),
"X": ("GeoX",),
"longitude": ("Lon",),
},
"axis": {"T": ("T",), "Z": ("Z",), "Y": ("Y",), "X": ("X",)},
"cartesian_axis": {"T": ("T",), "Z": ("Z",), "Y": ("Y",), "X": ("X",)},
"positive": {"vertical": ("up", "down")},
"units": {
"latitude": (
"degree_north",
"degree_N",
"degreeN",
"degrees_north",
"degrees_N",
"degreesN",
),
"longitude": (
"degree_east",
"degree_E",
"degreeE",
"degrees_east",
"degrees_E",
"degreesE",
),
},
}

# "long_name" and "standard_name" criteria are the same. For convenience.
coordinate_criteria["long_name"] = coordinate_criteria["standard_name"]

#: regular expressions for guess_coord_axis
regex = {
"time": "\\bt\\b|(time|min|hour|day|week|month|year)[0-9]*",
"vertical": (
"(z|nav_lev|gdep|lv_|bottom_top|sigma|h(ei)?ght|altitude|depth|"
"isobaric|pres|isotherm)[a-z_]*[0-9]*"
),
"Y": "y",
"latitude": "y?(nav_lat|lat|gphi)[a-z0-9]*",
"X": "x",
"longitude": "x?(nav_lon|lon|glam)[a-z0-9]*",
}
regex["Z"] = regex["vertical"]
regex["T"] = regex["time"]
3 changes: 2 additions & 1 deletion cf_xarray/scripts/make_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from pandas import DataFrame

from cf_xarray.accessor import _AXIS_NAMES, _COORD_NAMES, coordinate_criteria, regex
from cf_xarray.accessor import _AXIS_NAMES, _COORD_NAMES
from cf_xarray.criteria import coordinate_criteria, regex


def main():
Expand Down