|
28 | 28 | import dask.array as da
|
29 | 29 | import numpy as np
|
30 | 30 | import numpy.ma as ma
|
| 31 | +from packaging.version import Version |
31 | 32 |
|
32 | 33 | import iris._constraints
|
33 | 34 | from iris._data_manager import DataManager
|
|
60 | 61 | __all__ = ["Cube", "CubeAttrsDict", "CubeList"]
|
61 | 62 |
|
62 | 63 |
|
| 64 | +# Determine the numpy printoptions legacy setting. |
| 65 | +_NP_VERSION = Version(np.__version__) |
| 66 | +_NP_LEGACY_VERSION = Version("2.2") |
| 67 | +NP_PRINTOPTIONS_LEGACY = ( |
| 68 | + str(_NP_LEGACY_VERSION) |
| 69 | + if Version(f"{_NP_VERSION.major}.{_NP_VERSION.minor}") > _NP_LEGACY_VERSION |
| 70 | + else False |
| 71 | +) |
| 72 | + |
63 | 73 | # The XML namespace to use for CubeML documents
|
64 | 74 | XML_NAMESPACE_URI = "urn:x-iris:cubeml-0.2"
|
65 | 75 |
|
@@ -162,7 +172,7 @@ def insert(self, index, cube):
|
162 | 172 |
|
163 | 173 | def xml(self, checksum=False, order=True, byteorder=True):
|
164 | 174 | """Return a string of the XML that this list of cubes represents."""
|
165 |
| - with np.printoptions(legacy="2.2"): |
| 175 | + with np.printoptions(legacy=NP_PRINTOPTIONS_LEGACY): |
166 | 176 | doc = Document()
|
167 | 177 | cubes_xml_element = doc.createElement("cubes")
|
168 | 178 | cubes_xml_element.setAttribute("xmlns", XML_NAMESPACE_URI)
|
@@ -3865,7 +3875,7 @@ def xml(
|
3865 | 3875 | byteorder: bool = True,
|
3866 | 3876 | ) -> str:
|
3867 | 3877 | """Return a fully valid CubeML string representation of the Cube."""
|
3868 |
| - with np.printoptions(legacy="2.2"): |
| 3878 | + with np.printoptions(legacy=NP_PRINTOPTIONS_LEGACY): |
3869 | 3879 | doc = Document()
|
3870 | 3880 |
|
3871 | 3881 | cube_xml_element = self._xml_element(
|
|
0 commit comments