Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Py3k unicode collapse #1832

Merged
merged 3 commits into from
Nov 11, 2015
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
5 changes: 3 additions & 2 deletions lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -962,16 +962,17 @@ def collapsed(self, dims_to_collapse=None):
# bounds as strings.
serialize = lambda x: '|'.join([str(i) for i in x.flatten()])
bounds = None
string_type_fmt = 'S{}' if six.PY2 else 'U{}'
if self.bounds is not None:
shape = self.bounds.shape[1:]
bounds = []
for index in np.ndindex(shape):
index_slice = (slice(None),) + tuple(index)
bounds.append(serialize(self.bounds[index_slice]))
dtype = np.dtype('S{}'.format(max(map(len, bounds))))
dtype = np.dtype(string_type_fmt.format(max(map(len, bounds))))
bounds = np.array(bounds, dtype=dtype).reshape((1,) + shape)
points = serialize(self.points)
dtype = np.dtype('S{}'.format(len(points)))
dtype = np.dtype(string_type_fmt.format(len(points)))
Copy link
Member

Choose a reason for hiding this comment

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

string_type_fmt is only set if self.bounds is not None?

# Create the new collapsed coordinate.
coord = self.copy(points=np.array(points, dtype=dtype),
bounds=bounds)
Expand Down
14 changes: 9 additions & 5 deletions lib/iris/tests/unit/coords/test_Coord.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ def test_serialize(self):
['three', 'five'],
['five', 'seven'],
['seven', 'nine']]))
string_nobounds = Pair(np.array(['ecks', 'why', 'zed']),
None)
string_multi = Pair(np.array(['three', 'six', 'nine']),
np.array([['one', 'two', 'four', 'five'],
['four', 'five', 'seven', 'eight'],
Expand All @@ -214,15 +216,17 @@ def _serialize(data):
return '|'.join(str(item) for item in data.flatten())

for units in ['unknown', 'no_unit']:
for points, bounds in [string, string_multi]:
for points, bounds in [string, string_nobounds, string_multi]:
coord = AuxCoord(points=points, bounds=bounds, units=units)
collapsed_coord = coord.collapsed()
self.assertArrayEqual(collapsed_coord.points,
_serialize(points))
for index in np.ndindex(bounds.shape[1:]):
index_slice = (slice(None),) + tuple(index)
self.assertArrayEqual(collapsed_coord.bounds[index_slice],
_serialize(bounds[index_slice]))
if bounds is not None:
for index in np.ndindex(bounds.shape[1:]):
index_slice = (slice(None),) + tuple(index)
self.assertArrayEqual(
collapsed_coord.bounds[index_slice],
_serialize(bounds[index_slice]))

def test_dim_1d(self):
# Numeric coords should not be serialised.
Expand Down