diff --git a/cf_xarray/criteria.py b/cf_xarray/criteria.py index 81b78ab8..42aa1b12 100644 --- a/cf_xarray/criteria.py +++ b/cf_xarray/criteria.py @@ -102,6 +102,7 @@ "height_above_mean_sea_level", ), "positive": ("up", "down"), + "_CoordinateZisPositive": ("up", "down"), }, "X": { "standard_name": ( diff --git a/cf_xarray/tests/test_accessor.py b/cf_xarray/tests/test_accessor.py index 1214546d..82b6fd46 100644 --- a/cf_xarray/tests/test_accessor.py +++ b/cf_xarray/tests/test_accessor.py @@ -1791,6 +1791,53 @@ def test_missing_variable_in_coordinates() -> None: assert_identical(airds.time, airds.air.cf.mean(["X", "Y"]).cf["time"]) +@pytest.mark.parametrize("positive", ["up", "down"]) +@pytest.mark.parametrize("as_dataarray", [False, True]) +def test_cdm_vertical_direction(positive, as_dataarray): + ds = xr.Dataset( + {"data": ("level", [1, 2])}, + coords={"level": ("level", [0, 1], {"_CoordinateZisPositive": positive})}, + ) + obj = ds["data"] if as_dataarray else ds + assert obj.cf.coordinates["vertical"] == ["level"] + xr.testing.assert_identical(obj.cf["vertical"], ds["level"]) + + +@pytest.mark.parametrize("value", ["sideways", "UP", "", "Vertical"]) +def test_cdm_invalid_vertical_direction(value): + ds = xr.Dataset( + coords={"level": ("level", [0, 1], {"_CoordinateZisPositive": value})} + ) + assert "vertical" not in ds.cf.coordinates + + +@pytest.mark.parametrize( + ("axis_type", "axis"), + [ + ("Lat", "latitude"), + ("Lon", "longitude"), + ("GeoX", "X"), + ("GeoY", "Y"), + ("GeoZ", "Z"), + ("Height", "Z"), + ("Pressure", "Z"), + ("Time", "T"), + ], +) +def test_existing_cdm_axis_types(axis_type, axis): + ds = xr.Dataset( + coords={"coord": ("coord", [0, 1], {"_CoordinateAxisType": axis_type})} + ) + xr.testing.assert_identical(ds.cf[axis], ds["coord"]) + + +def test_cdm_transform_is_not_vertical_coordinate(): + ds = xr.Dataset( + {"transform": xr.DataArray(0, attrs={"_CoordinateTransformType": "Vertical"})} + ) + assert "vertical" not in ds.cf.coordinates + + def test_Z_vs_vertical_ROMS() -> None: from ..datasets import romsds diff --git a/doc/coord_axes.md b/doc/coord_axes.md index f5fb07bc..d4d62778 100644 --- a/doc/coord_axes.md +++ b/doc/coord_axes.md @@ -57,6 +57,12 @@ This table lists these internal criteria Any DataArray that has `standard_name: "latitude"` or `_CoordinateAxisType: "Lat"` or `"units": "degrees_north"` in its `attrs` will be identified as the `"latitude"` variable by cf-xarray. Similarly for other coordinate names. +The Unidata [CDM coordinate attributes](https://docs.unidata.ucar.edu/netcdf-java/current/userguide/coord_attr_conv.html) +`_CoordinateAxisType` and `_CoordinateZisPositive` are also recognized. +`_CoordinateZisPositive: "up"` or `"down"` identifies a `"vertical"` coordinate, +just like the CF `positive` attribute. This does not implement CDM coordinate +transforms or interpret `_CoordinateAxes` references. + ## Axis Names Similar criteria exist for the concept of "axes".