Skip to content

Commit 9abfdec

Browse files
Merge 4598c17 into 99cbe83
2 parents 99cbe83 + 4598c17 commit 9abfdec

File tree

13 files changed

+132
-109
lines changed

13 files changed

+132
-109
lines changed

benchmarks/benchmarks/generate_data/um_files.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,22 @@ def _create_um_files(
2222
from tempfile import NamedTemporaryFile
2323

2424
from mule import ArrayDataProvider, Field3, FieldsFile
25+
import mule.ff
2526
from mule.pp import fields_to_pp_file
2627
import numpy as np
2728

2829
from iris import load_cube
2930
from iris import save as save_cube
3031

32+
def to_bytes_patch(self, field):
33+
data = field.get_data()
34+
dtype = mule.ff._DATA_DTYPES[self.WORD_SIZE][field.lbuser1]
35+
data = data.astype(dtype)
36+
return data.tobytes(), data.size
37+
38+
# TODO: remove this patch when fixed in mule, see https://github.com/MetOffice/simulation-systems/discussions/389
39+
mule.ff._WriteFFOperatorUnpacked.to_bytes = to_bytes_patch
40+
3141
template = {
3242
"fixed_length_header": {"dataset_type": 3, "grid_staggering": 3},
3343
"integer_constants": {

docs/src/whatsnew/latest.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ This document explains the changes made to Iris for this release
5959
#. `@schlunma`_ fixed loading of netCDF files with coordinates that have
6060
non-string units. (:issue:`6505`, :pull:`6506`)
6161

62+
#. `@stephenworsley`_ fixed incompatibilities with numpy v2.3 affecting arrays of dates and
63+
array printing. (:pull:`6518`)
64+
6265
💣 Incompatible Changes
6366
=======================
6467

lib/iris/common/mixin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def _round(date):
203203
warnings.warn(message, category=FutureWarning)
204204

205205
if hasattr(result, "shape"):
206-
vfunc = np.vectorize(_round)
206+
vfunc = np.frompyfunc(_round, 1, 1)
207207
result = vfunc(result)
208208
else:
209209
result = _round(result)

lib/iris/common/resolve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2358,7 +2358,7 @@ def cube(self, data, in_place=False):
23582358
>>> resolver.map_rhs_to_lhs
23592359
True
23602360
>>> cube1.data.sum()
2361-
np.float32(124652160.0)
2361+
np.float32(1.2465214e+08)
23622362
>>> zeros.shape
23632363
(240, 37, 49)
23642364
>>> zeros.sum()

lib/iris/cube.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,18 +162,19 @@ def insert(self, index, cube):
162162

163163
def xml(self, checksum=False, order=True, byteorder=True):
164164
"""Return a string of the XML that this list of cubes represents."""
165-
doc = Document()
166-
cubes_xml_element = doc.createElement("cubes")
167-
cubes_xml_element.setAttribute("xmlns", XML_NAMESPACE_URI)
168-
169-
for cube_obj in self:
170-
cubes_xml_element.appendChild(
171-
cube_obj._xml_element(
172-
doc, checksum=checksum, order=order, byteorder=byteorder
165+
with np.printoptions(legacy="2.2"):
166+
doc = Document()
167+
cubes_xml_element = doc.createElement("cubes")
168+
cubes_xml_element.setAttribute("xmlns", XML_NAMESPACE_URI)
169+
170+
for cube_obj in self:
171+
cubes_xml_element.appendChild(
172+
cube_obj._xml_element(
173+
doc, checksum=checksum, order=order, byteorder=byteorder
174+
)
173175
)
174-
)
175176

176-
doc.appendChild(cubes_xml_element)
177+
doc.appendChild(cubes_xml_element)
177178

178179
# return our newly created XML string
179180
doc = Cube._sort_xml_attrs(doc)
@@ -3864,17 +3865,18 @@ def xml(
38643865
byteorder: bool = True,
38653866
) -> str:
38663867
"""Return a fully valid CubeML string representation of the Cube."""
3867-
doc = Document()
3868+
with np.printoptions(legacy="2.2"):
3869+
doc = Document()
38683870

3869-
cube_xml_element = self._xml_element(
3870-
doc, checksum=checksum, order=order, byteorder=byteorder
3871-
)
3872-
cube_xml_element.setAttribute("xmlns", XML_NAMESPACE_URI)
3873-
doc.appendChild(cube_xml_element)
3871+
cube_xml_element = self._xml_element(
3872+
doc, checksum=checksum, order=order, byteorder=byteorder
3873+
)
3874+
cube_xml_element.setAttribute("xmlns", XML_NAMESPACE_URI)
3875+
doc.appendChild(cube_xml_element)
38743876

3875-
# Print our newly created XML
3876-
doc = self._sort_xml_attrs(doc)
3877-
return iris.util._print_xml(doc)
3877+
# Print our newly created XML
3878+
doc = self._sort_xml_attrs(doc)
3879+
return iris.util._print_xml(doc)
38783880

38793881
def _xml_element(self, doc, checksum=False, order=True, byteorder=True):
38803882
cube_xml_element = doc.createElement("cube")

lib/iris/tests/_shared_utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,15 @@ def assert_text_file(source_filename, reference_filename, desc="text file"):
451451

452452
def assert_data_almost_equal(data, reference_filename, **kwargs):
453453
reference_path = get_result_path(reference_filename)
454+
455+
def fixed_std(data):
456+
# When data is constant, std() is too sensitive.
457+
if data.max() == data.min():
458+
data_std = 0
459+
else:
460+
data_std = data.std()
461+
return data_std
462+
454463
if _check_reference_file(reference_path):
455464
kwargs.setdefault("err_msg", "Reference file %s" % reference_path)
456465
with open(reference_path, "r") as reference_file:
@@ -470,15 +479,15 @@ def assert_data_almost_equal(data, reference_filename, **kwargs):
470479
assert math.isnan(data.mean())
471480
else:
472481
data_stats = np.array(
473-
(data.mean(), data.std(), data.max(), data.min()),
482+
(data.mean(), fixed_std(data), data.max(), data.min()),
474483
dtype=np.float64,
475484
)
476485
assert_array_all_close(nstats, data_stats, **kwargs)
477486
else:
478487
_ensure_folder(reference_path)
479488
stats = collections.OrderedDict(
480489
[
481-
("std", np.float64(data.std())),
490+
("std", np.float64(fixed_std(data))),
482491
("min", np.float64(data.min())),
483492
("max", np.float64(data.max())),
484493
("shape", data.shape),
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"std": 9.5367431640625e-07, "min": 11.111110687255859, "max": 11.111110687255859, "shape": [49, 50], "masked": false, "mean": 11.111111640930176}
1+
{"std": 0.0, "min": 11.111110687255859, "max": 11.111110687255859, "shape": [49, 50], "masked": false, "mean": 11.111111640930176}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"std": 1.9073486328125e-06, "min": 10288680426.032187, "max": 10288680426.032187, "shape": [10], "masked": false, "mean": 10288680426.032185}
1+
{"std": 0.0, "min": 10288680426.032187, "max": 10288680426.032187, "shape": [10], "masked": false, "mean": 10288680426.032185}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"std": 3.0517578125e-05, "min": 214667549910.37509, "max": 214667549910.37509, "shape": [10], "masked": false, "mean": 214667549910.37506}
1+
{"std": 0.0, "min": 214667549910.37509, "max": 214667549910.37509, "shape": [10], "masked": false, "mean": 214667549910.37506}

lib/iris/tests/test_pp_module.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from types import GeneratorType
99

1010
import cftime
11+
import numpy as np
1112
from numpy.testing import assert_array_equal
1213
import pytest
1314

@@ -63,7 +64,8 @@ def check_pp(self, pp_fields, reference_filename):
6364
for pp_field in pp_fields:
6465
pp_field.data
6566

66-
test_string = str(pp_fields)
67+
with np.printoptions(legacy="2.2"):
68+
test_string = str(pp_fields)
6769
reference_path = _shared_utils.get_result_path(reference_filename)
6870
if os.path.isfile(reference_path):
6971
with open(reference_path, "r") as reference_fh:

0 commit comments

Comments
 (0)