Skip to content
10 changes: 10 additions & 0 deletions benchmarks/benchmarks/generate_data/um_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ def _create_um_files(
from tempfile import NamedTemporaryFile

from mule import ArrayDataProvider, Field3, FieldsFile
import mule.ff
from mule.pp import fields_to_pp_file
import numpy as np

from iris import load_cube
from iris import save as save_cube

def to_bytes_patch(self, field):
data = field.get_data()
dtype = mule.ff._DATA_DTYPES[self.WORD_SIZE][field.lbuser1]
data = data.astype(dtype)
return data.tobytes(), data.size

# TODO: remove this patch when fixed in mule, see https://github.com/MetOffice/simulation-systems/discussions/389
mule.ff._WriteFFOperatorUnpacked.to_bytes = to_bytes_patch

template = {
"fixed_length_header": {"dataset_type": 3, "grid_staggering": 3},
"integer_constants": {
Expand Down
3 changes: 3 additions & 0 deletions docs/src/whatsnew/latest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ This document explains the changes made to Iris for this release
#. `@schlunma`_ fixed loading of netCDF files with coordinates that have
non-string units. (:issue:`6505`, :pull:`6506`)

#. `@stephenworsley`_ fixed incompatibilities with numpy v2.3 affecting arrays of dates and
array printing. (:pull:`6518`)

💣 Incompatible Changes
=======================

Expand Down
2 changes: 1 addition & 1 deletion lib/iris/common/mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def _round(date):
warnings.warn(message, category=FutureWarning)

if hasattr(result, "shape"):
vfunc = np.vectorize(_round)
vfunc = np.frompyfunc(_round, 1, 1)
result = vfunc(result)
else:
result = _round(result)
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/common/resolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -2358,7 +2358,7 @@ def cube(self, data, in_place=False):
>>> resolver.map_rhs_to_lhs
True
>>> cube1.data.sum()
np.float32(124652160.0)
np.float32(1.2465214e+08)
>>> zeros.shape
(240, 37, 49)
>>> zeros.sum()
Expand Down
40 changes: 21 additions & 19 deletions lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,18 +162,19 @@ def insert(self, index, cube):

def xml(self, checksum=False, order=True, byteorder=True):
"""Return a string of the XML that this list of cubes represents."""
doc = Document()
cubes_xml_element = doc.createElement("cubes")
cubes_xml_element.setAttribute("xmlns", XML_NAMESPACE_URI)

for cube_obj in self:
cubes_xml_element.appendChild(
cube_obj._xml_element(
doc, checksum=checksum, order=order, byteorder=byteorder
with np.printoptions(legacy="2.2"):
doc = Document()
cubes_xml_element = doc.createElement("cubes")
cubes_xml_element.setAttribute("xmlns", XML_NAMESPACE_URI)

for cube_obj in self:
cubes_xml_element.appendChild(
cube_obj._xml_element(
doc, checksum=checksum, order=order, byteorder=byteorder
)
)
)

doc.appendChild(cubes_xml_element)
doc.appendChild(cubes_xml_element)

# return our newly created XML string
doc = Cube._sort_xml_attrs(doc)
Expand Down Expand Up @@ -3864,17 +3865,18 @@ def xml(
byteorder: bool = True,
) -> str:
"""Return a fully valid CubeML string representation of the Cube."""
doc = Document()
with np.printoptions(legacy="2.2"):
doc = Document()

cube_xml_element = self._xml_element(
doc, checksum=checksum, order=order, byteorder=byteorder
)
cube_xml_element.setAttribute("xmlns", XML_NAMESPACE_URI)
doc.appendChild(cube_xml_element)
cube_xml_element = self._xml_element(
doc, checksum=checksum, order=order, byteorder=byteorder
)
cube_xml_element.setAttribute("xmlns", XML_NAMESPACE_URI)
doc.appendChild(cube_xml_element)

# Print our newly created XML
doc = self._sort_xml_attrs(doc)
return iris.util._print_xml(doc)
# Print our newly created XML
doc = self._sort_xml_attrs(doc)
return iris.util._print_xml(doc)

def _xml_element(self, doc, checksum=False, order=True, byteorder=True):
cube_xml_element = doc.createElement("cube")
Expand Down
13 changes: 11 additions & 2 deletions lib/iris/tests/_shared_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,15 @@ def assert_text_file(source_filename, reference_filename, desc="text file"):

def assert_data_almost_equal(data, reference_filename, **kwargs):
reference_path = get_result_path(reference_filename)

def fixed_std(data):
# When data is constant, std() is too sensitive.
if data.max() == data.min():
data_std = 0
else:
data_std = data.std()
return data_std

if _check_reference_file(reference_path):
kwargs.setdefault("err_msg", "Reference file %s" % reference_path)
with open(reference_path, "r") as reference_file:
Expand All @@ -470,15 +479,15 @@ def assert_data_almost_equal(data, reference_filename, **kwargs):
assert math.isnan(data.mean())
else:
data_stats = np.array(
(data.mean(), data.std(), data.max(), data.min()),
(data.mean(), fixed_std(data), data.max(), data.min()),
dtype=np.float64,
)
assert_array_all_close(nstats, data_stats, **kwargs)
else:
_ensure_folder(reference_path)
stats = collections.OrderedDict(
[
("std", np.float64(data.std())),
("std", np.float64(fixed_std(data))),
("min", np.float64(data.min())),
("max", np.float64(data.max())),
("shape", data.shape),
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"std": 9.5367431640625e-07, "min": 11.111110687255859, "max": 11.111110687255859, "shape": [49, 50], "masked": false, "mean": 11.111111640930176}
{"std": 0.0, "min": 11.111110687255859, "max": 11.111110687255859, "shape": [49, 50], "masked": false, "mean": 11.111111640930176}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"std": 1.9073486328125e-06, "min": 10288680426.032187, "max": 10288680426.032187, "shape": [10], "masked": false, "mean": 10288680426.032185}
{"std": 0.0, "min": 10288680426.032187, "max": 10288680426.032187, "shape": [10], "masked": false, "mean": 10288680426.032185}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"std": 3.0517578125e-05, "min": 214667549910.37509, "max": 214667549910.37509, "shape": [10], "masked": false, "mean": 214667549910.37506}
{"std": 0.0, "min": 214667549910.37509, "max": 214667549910.37509, "shape": [10], "masked": false, "mean": 214667549910.37506}
4 changes: 3 additions & 1 deletion lib/iris/tests/test_pp_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from types import GeneratorType

import cftime
import numpy as np
from numpy.testing import assert_array_equal
import pytest

Expand Down Expand Up @@ -63,7 +64,8 @@ def check_pp(self, pp_fields, reference_filename):
for pp_field in pp_fields:
pp_field.data

test_string = str(pp_fields)
with np.printoptions(legacy="2.2"):
test_string = str(pp_fields)
reference_path = _shared_utils.get_result_path(reference_filename)
if os.path.isfile(reference_path):
with open(reference_path, "r") as reference_fh:
Expand Down
Loading
Loading