Skip to content

Commit c6db38a

Browse files
djkirkhamDPeterK
authored andcommitted
Fix issues where +=, -= were being used on arrays with incompatible dtypes (#2128)
* Fix issues where +=, -= were being used on arrays with incompatible dtypes * Fix copyright year * Add spaces to copyright years * Fix errors, ensure arrays have correct dtype * Properly fix errors. Definitely. I'm sure of it
1 parent 7b85535 commit c6db38a

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/iris/experimental/regrid.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,9 +953,15 @@ def _src_align_and_flatten(coord):
953953
min_sx, min_tx = np.min(sx.points), np.min(tx.points)
954954
if min_sx < 0 and min_tx >= 0:
955955
indices = np.where(sx_points < 0)
956+
# Ensure += doesn't raise a TypeError
957+
if not np.can_cast(modulus, sx_points.dtype):
958+
sx_points = sx_points.astype(type(modulus), casting='safe')
956959
sx_points[indices] += modulus
957960
elif min_sx >= 0 and min_tx < 0:
958961
indices = np.where(sx_points > (modulus / 2))
962+
# Ensure -= doesn't raise a TypeError
963+
if not np.can_cast(modulus, sx_points.dtype):
964+
sx_points = sx_points.astype(type(modulus), casting='safe')
959965
sx_points[indices] -= modulus
960966

961967
# Create target grid cube x and y cell boundaries.

lib/iris/tests/unit/analysis/test_WPERCENTILE.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# (C) British Crown Copyright 2015, Met Office
1+
# (C) British Crown Copyright 2015 - 2016, Met Office
22
#
33
# This file is part of Iris.
44
#
@@ -141,7 +141,7 @@ def test_2d_multi(self):
141141
actual = WPERCENTILE.aggregate(data, axis=0, percent=percent,
142142
weights=weights)
143143
self.assertTupleEqual(actual.shape, (shape[-1], percent.size))
144-
expected = np.tile(np.arange(shape[-1]), percent.size)
144+
expected = np.tile(np.arange(shape[-1]), percent.size).astype('f8')
145145
expected = expected.reshape(percent.size, shape[-1]).T
146146
expected[:, 1:-1] += (percent[1:-1]-25)*0.2
147147
expected[:, -1] += 10.
@@ -156,7 +156,7 @@ def test_masked_2d_multi(self):
156156
actual = WPERCENTILE.aggregate(data, axis=0, percent=percent,
157157
weights=weights)
158158
self.assertTupleEqual(actual.shape, (shape[-1], percent.size))
159-
expected = np.tile(np.arange(shape[-1]), percent.size)
159+
expected = np.tile(np.arange(shape[-1]), percent.size).astype('f8')
160160
expected = expected.reshape(percent.size, shape[-1]).T
161161
expected[:, 1:-1] += (percent[1:-1]-25)*0.4
162162
expected[:, -1] += 20.

0 commit comments

Comments
 (0)