Skip to content
Open
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
17 changes: 16 additions & 1 deletion lib/iris/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import dask.array as da
import numpy as np
import numpy.ma as ma
from scipy.stats import rankdata

from iris._cube_coord_common import CFVariableMixin
import iris._concatenate
Expand Down Expand Up @@ -2808,7 +2809,21 @@ def remap_dim_coord(coord_and_dim):

def remap_aux_coord(coord_and_dims):
coord, dims = coord_and_dims
return coord, tuple(dim_mapping[dim] for dim in dims)
new_order = tuple(dim_mapping[dim] for dim in dims)
new_rank = (rankdata(new_order, method='ordinal') - 1).tolist()
new_order_inc = tuple(set(new_order))

# Enforce increasing order for mapping.
points = coord.points.transpose(new_rank)
bounds = None
if coord.has_bounds():
bounds = coord.bounds.transpose(new_rank)

self.remove_coord(coord)
new_coord = coord.copy(points, bounds=bounds)
self.add_aux_coord(new_coord, new_order_inc)
return new_coord, new_order_inc

self._aux_coords_and_dims = list(map(remap_aux_coord,
self._aux_coords_and_dims))

Expand Down
13 changes: 13 additions & 0 deletions lib/iris/tests/unit/cube/test_Cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,19 @@ def test_bad_transpose_order(self):
with self.assertRaisesRegexp(ValueError, exp_emsg):
self.cube.transpose([1])

def test_transpose_multidim_coords(self):
# Ensure that multidimensional coordinates are transposed where
# necessary so that their dimension mapping is in increasing order.
data = np.zeros((1, 2, 3, 4, 5))
cube = Cube(data)
aux_coord = iris.coords.AuxCoord(np.zeros((2, 4, 1)), long_name='bing')
cube.add_aux_coord(aux_coord, (1, 3, 0))
cube.transpose((3, 4, 2, 1, 0))

coord = cube.coord('bing')
self.assertEqual(coord.shape, (4, 2, 1))
self.assertEqual(cube.coord_dims(coord), (0, 3, 4))


if __name__ == '__main__':
tests.main()
50 changes: 50 additions & 0 deletions lib/iris/tests/unit/util/test_sanitise_auxcoords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# (C) British Crown Copyright 2017, Met Office
#
# This file is part of Iris.
#
# Iris is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Iris is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Iris. If not, see <http://www.gnu.org/licenses/>.
"""Test function :func:`iris.util.sanitise_auxcoords`."""

from __future__ import (absolute_import, division, print_function)
from six.moves import (filter, input, map, range, zip) # noqa

# import iris tests first so that some things can be initialised before
# importing anything else
import iris.tests as tests

import iris
import numpy as np

from iris.util import sanitise_auxcoords


class TestAll(tests.IrisTest):
def test(self):
# Ensure that multidimensional coordinates are transposed where
# necessary so that their dimension mapping is in increasing order.
data = np.zeros((1, 2, 3, 4, 5, 6))
cube = iris.cube.Cube(data)
aux_coord = iris.coords.AuxCoord(np.zeros((3, 1, 5, 2)),
long_name='bing')
cube.add_aux_coord(aux_coord, (2, 0, 4, 1))

sanitise_auxcoords(cube)

coord = cube.coord('bing')
self.assertEqual(coord.shape, (1, 2, 3, 5))
self.assertEqual(cube.coord_dims(coord), (0, 1, 2, 4))


if __name__ == '__main__':
tests.main()
31 changes: 31 additions & 0 deletions lib/iris/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,43 @@
import cf_units
import numpy as np
import numpy.ma as ma
from scipy.stats import rankdata

from iris._deprecation import warn_deprecated
import iris
import iris.exceptions


def sanitise_auxcoords(cube):
"""
Enforce increasing dimension mappings for all coordinates.

Helper function to transpose multidimensional coordinates as neccessary to
enforce increasing order dimension mappings.

Args:

* cube
An instance of :class:`iris.cube.Cube`

"""
for coord in cube.aux_coords:
if coord.ndim > 1:
dims = cube.coord_dims(coord)
dim_rank = (rankdata(dims, method='ordinal') - 1).tolist()
if dim_rank != range(coord.ndim):
# Sanitise coordinate
new_order = range(len(dim_rank))
transpose_indx = [dim_rank.index(val) for val in new_order]
points = coord.points.transpose(transpose_indx)
bounds = None
if coord.has_bounds():
bounds = coord.bounds.transpose(transpose_indx)
new_coord = coord.copy(points, bounds=bounds)
cube.remove_coord(coord)
cube.add_aux_coord(new_coord, sorted(dims))


def broadcast_weights(weights, array, dims):
"""
Broadcast a weights array to the shape of another array.
Expand Down