Skip to content

deprecate the cdms2 conversion methods #7876

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 10 commits into from
May 29, 2023
Merged
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Breaking changes

Deprecations
~~~~~~~~~~~~
- Deprecate the `cdms2 <https://github.com/CDAT/cdms>`_ conversion methods (:pull:`7876`)
By `Justus Magin <https://github.com/keewis>`_.

Performance
~~~~~~~~~~~
Expand Down
33 changes: 31 additions & 2 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
ReprObject,
_default,
either_dict_or_kwargs,
emit_user_level_warning,
)
from xarray.core.variable import (
IndexVariable,
Expand Down Expand Up @@ -4331,16 +4332,44 @@ def from_series(cls, series: pd.Series, sparse: bool = False) -> DataArray:
return result

def to_cdms2(self) -> cdms2_Variable:
"""Convert this array into a cdms2.Variable"""
"""Convert this array into a cdms2.Variable

.. deprecated:: 2023.06.0
The `cdms2`_ library has been deprecated. Please consider using the
`xcdat`_ library instead.

.. _cdms2: https://github.com/CDAT/cdms
.. _xcdat: https://github.com/xCDAT/xcdat
"""
from xarray.convert import to_cdms2

emit_user_level_warning(
"The cdms2 library has been deprecated."
" Please consider using the xcdat library instead.",
DeprecationWarning,
)

return to_cdms2(self)

@classmethod
def from_cdms2(cls, variable: cdms2_Variable) -> DataArray:
"""Convert a cdms2.Variable into an xarray.DataArray"""
"""Convert a cdms2.Variable into an xarray.DataArray

.. deprecated:: 2023.06.0
The `cdms2`_ library has been deprecated. Please consider using the
`xcdat`_ library instead.

.. _cdms2: https://github.com/CDAT/cdms
.. _xcdat: https://github.com/xCDAT/xcdat
"""
from xarray.convert import from_cdms2

emit_user_level_warning(
"The cdms2 library has been deprecated."
" Please consider using the xcdat library instead.",
DeprecationWarning,
)

return from_cdms2(variable)

def to_iris(self) -> iris_Cube:
Expand Down
24 changes: 20 additions & 4 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -3548,6 +3548,10 @@ def test_to_masked_array(self) -> None:
ma = da.to_masked_array()
assert len(ma.mask) == N

@pytest.mark.skipif(
Version(np.__version__) > Version("1.24") or sys.version_info[:2] > (3, 10),
reason="cdms2 is unmaintained and does not support newer `numpy` or python versions",
)
def test_to_and_from_cdms2_classic(self) -> None:
"""Classic with 1D axes"""
pytest.importorskip("cdms2")
Expand All @@ -3565,7 +3569,8 @@ def test_to_and_from_cdms2_classic(self) -> None:
IndexVariable("distance", [-2, 2]),
IndexVariable("time", [0, 1, 2]),
]
actual = original.to_cdms2()
with pytest.deprecated_call(match=".*cdms2"):
actual = original.to_cdms2()
assert_array_equal(actual.asma(), original)
assert actual.id == original.name
assert tuple(actual.getAxisIds()) == original.dims
Expand All @@ -3578,7 +3583,8 @@ def test_to_and_from_cdms2_classic(self) -> None:
assert len(component_times) == 3
assert str(component_times[0]) == "2000-1-1 0:0:0.0"

roundtripped = DataArray.from_cdms2(actual)
with pytest.deprecated_call(match=".*cdms2"):
roundtripped = DataArray.from_cdms2(actual)
assert_identical(original, roundtripped)

back = from_cdms2(actual)
Expand All @@ -3587,6 +3593,10 @@ def test_to_and_from_cdms2_classic(self) -> None:
for coord_name in original.coords.keys():
assert_array_equal(original.coords[coord_name], back.coords[coord_name])

@pytest.mark.skipif(
Version(np.__version__) > Version("1.24") or sys.version_info[:2] > (3, 10),
reason="cdms2 is unmaintained and does not support newer `numpy` or python versions",
)
def test_to_and_from_cdms2_sgrid(self) -> None:
"""Curvilinear (structured) grid

Expand All @@ -3605,7 +3615,8 @@ def test_to_and_from_cdms2_sgrid(self) -> None:
coords=dict(x=x, y=y, lon=lon, lat=lat),
name="sst",
)
actual = original.to_cdms2()
with pytest.deprecated_call():
actual = original.to_cdms2()
assert tuple(actual.getAxisIds()) == original.dims
assert_array_equal(original.coords["lon"], actual.getLongitude().asma())
assert_array_equal(original.coords["lat"], actual.getLatitude().asma())
Expand All @@ -3616,6 +3627,10 @@ def test_to_and_from_cdms2_sgrid(self) -> None:
assert_array_equal(original.coords["lat"], back.coords["lat"])
assert_array_equal(original.coords["lon"], back.coords["lon"])

@pytest.mark.skipif(
Version(np.__version__) > Version("1.24") or sys.version_info[:2] > (3, 10),
reason="cdms2 is unmaintained and does not support newer `numpy` or python versions",
)
def test_to_and_from_cdms2_ugrid(self) -> None:
"""Unstructured grid"""
pytest.importorskip("cdms2")
Expand All @@ -3626,7 +3641,8 @@ def test_to_and_from_cdms2_ugrid(self) -> None:
original = DataArray(
np.arange(5), dims=["cell"], coords={"lon": lon, "lat": lat, "cell": cell}
)
actual = original.to_cdms2()
with pytest.deprecated_call(match=".*cdms2"):
actual = original.to_cdms2()
assert tuple(actual.getAxisIds()) == original.dims
assert_array_equal(original.coords["lon"], actual.getLongitude().getValue())
assert_array_equal(original.coords["lat"], actual.getLatitude().getValue())
Expand Down