Skip to content

Commit d4edd70

Browse files
authored
deprecate the cdms2 conversion methods (#7876)
* 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
1 parent b319d86 commit d4edd70

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Breaking changes
3030

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

3436
Performance
3537
~~~~~~~~~~~

xarray/core/dataarray.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
ReprObject,
4343
_default,
4444
either_dict_or_kwargs,
45+
emit_user_level_warning,
4546
)
4647
from xarray.core.variable import (
4748
IndexVariable,
@@ -4331,16 +4332,44 @@ def from_series(cls, series: pd.Series, sparse: bool = False) -> DataArray:
43314332
return result
43324333

43334334
def to_cdms2(self) -> cdms2_Variable:
4334-
"""Convert this array into a cdms2.Variable"""
4335+
"""Convert this array into a cdms2.Variable
4336+
4337+
.. deprecated:: 2023.06.0
4338+
The `cdms2`_ library has been deprecated. Please consider using the
4339+
`xcdat`_ library instead.
4340+
4341+
.. _cdms2: https://github.com/CDAT/cdms
4342+
.. _xcdat: https://github.com/xCDAT/xcdat
4343+
"""
43354344
from xarray.convert import to_cdms2
43364345

4346+
emit_user_level_warning(
4347+
"The cdms2 library has been deprecated."
4348+
" Please consider using the xcdat library instead.",
4349+
DeprecationWarning,
4350+
)
4351+
43374352
return to_cdms2(self)
43384353

43394354
@classmethod
43404355
def from_cdms2(cls, variable: cdms2_Variable) -> DataArray:
4341-
"""Convert a cdms2.Variable into an xarray.DataArray"""
4356+
"""Convert a cdms2.Variable into an xarray.DataArray
4357+
4358+
.. deprecated:: 2023.06.0
4359+
The `cdms2`_ library has been deprecated. Please consider using the
4360+
`xcdat`_ library instead.
4361+
4362+
.. _cdms2: https://github.com/CDAT/cdms
4363+
.. _xcdat: https://github.com/xCDAT/xcdat
4364+
"""
43424365
from xarray.convert import from_cdms2
43434366

4367+
emit_user_level_warning(
4368+
"The cdms2 library has been deprecated."
4369+
" Please consider using the xcdat library instead.",
4370+
DeprecationWarning,
4371+
)
4372+
43444373
return from_cdms2(variable)
43454374

43464375
def to_iris(self) -> iris_Cube:

xarray/tests/test_dataarray.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,6 +3548,10 @@ def test_to_masked_array(self) -> None:
35483548
ma = da.to_masked_array()
35493549
assert len(ma.mask) == N
35503550

3551+
@pytest.mark.skipif(
3552+
Version(np.__version__) > Version("1.24") or sys.version_info[:2] > (3, 10),
3553+
reason="cdms2 is unmaintained and does not support newer `numpy` or python versions",
3554+
)
35513555
def test_to_and_from_cdms2_classic(self) -> None:
35523556
"""Classic with 1D axes"""
35533557
pytest.importorskip("cdms2")
@@ -3565,7 +3569,8 @@ def test_to_and_from_cdms2_classic(self) -> None:
35653569
IndexVariable("distance", [-2, 2]),
35663570
IndexVariable("time", [0, 1, 2]),
35673571
]
3568-
actual = original.to_cdms2()
3572+
with pytest.deprecated_call(match=".*cdms2"):
3573+
actual = original.to_cdms2()
35693574
assert_array_equal(actual.asma(), original)
35703575
assert actual.id == original.name
35713576
assert tuple(actual.getAxisIds()) == original.dims
@@ -3578,7 +3583,8 @@ def test_to_and_from_cdms2_classic(self) -> None:
35783583
assert len(component_times) == 3
35793584
assert str(component_times[0]) == "2000-1-1 0:0:0.0"
35803585

3581-
roundtripped = DataArray.from_cdms2(actual)
3586+
with pytest.deprecated_call(match=".*cdms2"):
3587+
roundtripped = DataArray.from_cdms2(actual)
35823588
assert_identical(original, roundtripped)
35833589

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

3596+
@pytest.mark.skipif(
3597+
Version(np.__version__) > Version("1.24") or sys.version_info[:2] > (3, 10),
3598+
reason="cdms2 is unmaintained and does not support newer `numpy` or python versions",
3599+
)
35903600
def test_to_and_from_cdms2_sgrid(self) -> None:
35913601
"""Curvilinear (structured) grid
35923602
@@ -3605,7 +3615,8 @@ def test_to_and_from_cdms2_sgrid(self) -> None:
36053615
coords=dict(x=x, y=y, lon=lon, lat=lat),
36063616
name="sst",
36073617
)
3608-
actual = original.to_cdms2()
3618+
with pytest.deprecated_call():
3619+
actual = original.to_cdms2()
36093620
assert tuple(actual.getAxisIds()) == original.dims
36103621
assert_array_equal(original.coords["lon"], actual.getLongitude().asma())
36113622
assert_array_equal(original.coords["lat"], actual.getLatitude().asma())
@@ -3616,6 +3627,10 @@ def test_to_and_from_cdms2_sgrid(self) -> None:
36163627
assert_array_equal(original.coords["lat"], back.coords["lat"])
36173628
assert_array_equal(original.coords["lon"], back.coords["lon"])
36183629

3630+
@pytest.mark.skipif(
3631+
Version(np.__version__) > Version("1.24") or sys.version_info[:2] > (3, 10),
3632+
reason="cdms2 is unmaintained and does not support newer `numpy` or python versions",
3633+
)
36193634
def test_to_and_from_cdms2_ugrid(self) -> None:
36203635
"""Unstructured grid"""
36213636
pytest.importorskip("cdms2")
@@ -3626,7 +3641,8 @@ def test_to_and_from_cdms2_ugrid(self) -> None:
36263641
original = DataArray(
36273642
np.arange(5), dims=["cell"], coords={"lon": lon, "lat": lat, "cell": cell}
36283643
)
3629-
actual = original.to_cdms2()
3644+
with pytest.deprecated_call(match=".*cdms2"):
3645+
actual = original.to_cdms2()
36303646
assert tuple(actual.getAxisIds()) == original.dims
36313647
assert_array_equal(original.coords["lon"], actual.getLongitude().getValue())
36323648
assert_array_equal(original.coords["lat"], actual.getLatitude().getValue())

0 commit comments

Comments
 (0)