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
12 changes: 12 additions & 0 deletions lib/iris/tests/unit/util/test__coord_regular.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ def test_irregular_points(self):
self.assertEqual(exp_avdiff, result_avdiff)
self.assertFalse(result)

def test_single_point(self):
lone_point = np.array([4])
result_avdiff, result = points_step(lone_point)
self.assertTrue(np.isnan(result_avdiff))
self.assertTrue(result)

def test_no_points(self):
no_points = np.array([])
result_avdiff, result = points_step(no_points)
self.assertTrue(np.isnan(result_avdiff))
self.assertTrue(result)


if __name__ == "__main__":
tests.main()
27 changes: 22 additions & 5 deletions lib/iris/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1397,11 +1397,28 @@ def regular_step(coord):


def points_step(points):
"""Determine whether a NumPy array has a regular step."""
diffs = np.diff(points)
avdiff = np.mean(diffs)
# TODO: This value for `rtol` is set for test_analysis to pass...
regular = np.allclose(diffs, avdiff, rtol=0.001)
"""Determine whether `points` has a regular step.

Parameters
----------
points : numeric, array-like
The sequence of values to check for a regular difference.

Returns
-------
numeric, bool
A tuple containing the average difference between values, and whether the difference is regular.
"""
# Calculations only make sense with multiple points
points = np.asanyarray(points)
if points.size >= 2:
diffs = np.diff(points)
avdiff = np.mean(diffs)
# TODO: This value for `rtol` is set for test_analysis to pass...
regular = np.allclose(diffs, avdiff, rtol=0.001)
else:
avdiff = np.nan
regular = True
return avdiff, regular


Expand Down