Skip to content

Instantly share code, notes, and snippets.

@rcomer
Last active August 10, 2018 14:24
Show Gist options
  • Save rcomer/443dc06dbef2d804ebf703b44aba5bc5 to your computer and use it in GitHub Desktop.
Save rcomer/443dc06dbef2d804ebf703b44aba5bc5 to your computer and use it in GitHub Desktop.
Print the differences between two Iris coordinates
from __future__ import print_function
import numpy as np
def coord_diffs(coord1, coord2):
"""
Print which aspects of two coords do not match.
"""
for attr in ['standard_name', 'long_name', 'var_name', 'units',
'coord_system', 'circular']:
if getattr(coord1, attr, None) != getattr(coord2, attr, None):
print('{} difference: {} vs {}'.format(
attr, repr(getattr(coord1, attr, None)),
repr(getattr(coord2, attr, None))))
common_keys = set(coord1.attributes).intersection(coord2.attributes)
for key in set(coord1.attributes) - common_keys:
print('attribute only on coord1: {}'.format(key))
for key in set(coord2.attributes) - common_keys:
print('attribute only on coord2: {}'.format(key))
for key in common_keys:
if np.any(coord1.attributes[key] != coord2.attributes[key]):
print('attributes differ: {}'.format(key))
for attr in ['points', 'bounds']:
array1 = getattr(coord1, attr)
array2 = getattr(coord2, attr)
if (array1 is None) != (array2 is None):
has_array, = [coord for (coord, array) in
zip(['coord1', 'coord2'], [array1, array2]) if
array is not None]
print('{} exist only on {}'.format(attr, has_array))
else:
if np.any(array1 != array2):
print('{} differ'.format(attr))
if array1 is not None and array1.dtype != array2.dtype:
print('{} dypes differ: {} vs {}'.format(
attr, array1.dtype, array2.dtype))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment