Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/iris/src/userguide/merge_and_concat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ Merge
Differences in the :data:`~iris.cube.Cube.attributes` the input cubes probably
cause the greatest amount of merge-related difficulties.
In recognition of this, Iris has a helper function,
:func:`~iris.experimental.equalise_cubes.equalise_attributes`, to equalise
:func:`~iris.util.equalise_attributes`, to equalise
attributes differences in the input cubes.

.. note::
Expand All @@ -407,16 +407,16 @@ attributes differences in the input cubes.
:meth:`iris.cube.Cube.is_compatible` are **not** designed to give user
indication of whether two cubes can be merged.

To demonstrate using :func:`~iris.experimental.equalise_cubes.equalise_attributes`,
To demonstrate using :func:`~iris.util.equalise_attributes`,
let's return to our non-merging list of input cubes from the merge_cube example
from earlier.
We'll call :func:`~iris.experimental.equalise_cubes.equalise_attributes` on the
We'll call :func:`~iris.util.equalise_attributes` on the
input cubes before merging the input cubes using :meth:`~iris.cube.CubeList.merge_cube`:

.. doctest:: merge_vs_merge_cube
:options: +ELLIPSIS, +NORMALIZE_WHITESPACE

>>> from iris.experimental.equalise_cubes import equalise_attributes
>>> from iris.util import equalise_attributes
>>> print(cubes)
0: air_temperature / (kelvin) (y: 4; x: 5)
1: air_temperature / (kelvin) (y: 4; x: 5)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* The :func:`iris.experimental.equalise_cubes.equalise_attributes` function has been moved from the
:mod:`iris.experimental` module into the :mod:`iris.util` module. Please use the :func:`iris.util.equalise_attributes`
function instead.
37 changes: 11 additions & 26 deletions lib/iris/experimental/equalise_cubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,23 @@
"""

import numpy as np


def equalise_attributes(cubes):
"""
Delete cube attributes that are not identical over all cubes in a group.
This function simply deletes any attributes which are not the same for
all the given cubes. The cubes will then have identical attributes. The
given cubes are modified in-place.
.. warning::
Args:
This function is now **disabled**.
* cubes (iterable of :class:`iris.cube.Cube`):
A collection of cubes to compare and adjust.
The functionality has been moved to
:func:`iris.util.equalise_attributes`.
"""
# Work out which attributes are identical across all the cubes.
common_keys = list(cubes[0].attributes.keys())
for cube in cubes[1:]:
cube_keys = list(cube.attributes.keys())
common_keys = [
key
for key in common_keys
if (
key in cube_keys
and np.all(cube.attributes[key] == cubes[0].attributes[key])
)
]

# Remove all the other attributes.
for cube in cubes:
for key in list(cube.attributes.keys()):
if key not in common_keys:
del cube.attributes[key]
old = "iris.experimental.equalise_cubes.equalise_attributes"
new = "iris.util.equalise_attributes"
emsg = (
f'The function "{old}" has been moved.\n'
f'Please replace "{old}(<cubes>)" with "{new}(<cubes>)".'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh our first f-string 🎉

Welcome to the wonderful world of Python3 (finally!)

)
raise Exception(emsg)
6 changes: 0 additions & 6 deletions lib/iris/tests/unit/experimental/equalise_cubes/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
"""
Unit tests for the :func:`iris.experimental.equalise_cubes.equalise_attributes`
function.
Unit tests for the :func:`iris.util.equalise_attributes` function.

"""

Expand All @@ -16,7 +15,7 @@
import numpy as np
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The doc string in the file header still references "experimental". I think it should be iris.util.equalise_attributes:

"""
Unit tests for the :func:iris.experimental.equalise_cubes.equalise_attributes
function.
"""


from iris.cube import Cube
from iris.experimental.equalise_cubes import equalise_attributes
from iris.util import equalise_attributes
import iris.tests.stock


Expand Down
34 changes: 34 additions & 0 deletions lib/iris/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1794,3 +1794,37 @@ def mask_cube(cube, points_to_mask):
cube.data = ma.masked_array(cube.data)
cube.data[points_to_mask] = ma.masked
return cube

Copy link
Contributor

@abooton abooton Nov 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this file: It is identical to before, except for syntax over multiple lines :)

Copy link
Member Author

@bjlittle bjlittle Nov 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abooton Yup, that's the magic of black... or should I say, black magic?! 😉

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👻 👿 🙈


def equalise_attributes(cubes):
"""
Delete cube attributes that are not identical over all cubes in a group.

This function simply deletes any attributes which are not the same for
all the given cubes. The cubes will then have identical attributes. The
given cubes are modified in-place.

Args:

* cubes (iterable of :class:`iris.cube.Cube`):
A collection of cubes to compare and adjust.

"""
# Work out which attributes are identical across all the cubes.
common_keys = list(cubes[0].attributes.keys())
for cube in cubes[1:]:
cube_keys = list(cube.attributes.keys())
common_keys = [
key
for key in common_keys
if (
key in cube_keys
and np.all(cube.attributes[key] == cubes[0].attributes[key])
)
]

# Remove all the other attributes.
for cube in cubes:
for key in list(cube.attributes.keys()):
if key not in common_keys:
del cube.attributes[key]