Skip to content

Commit

Permalink
deprecate the cdms2 conversion methods (pydata#7876)
Browse files Browse the repository at this point in the history
* emit deprecation warnings for the cdms2 conversion methods

* add the deprecation notice to the docstring

* check that the deprecation warnings are properly raised

* actually check that the deprecation message mentions cdms2

* skip cdms2 checks for new numpy / python versions

* whats-new entry

* make the deprecation message consistent

* functions → methods

* adjust the skip condition

* maintenance mode → deprecated
  • Loading branch information
keewis authored and dstansby committed Jun 28, 2023
1 parent d629adb commit 9d75dd5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
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

0 comments on commit 9d75dd5

Please sign in to comment.