Skip to content

Commit

Permalink
Gracefully handle long time intervals (SciTools#2354)
Browse files Browse the repository at this point in the history
Gracefully handle long time intervals (months & years) when plotting
  • Loading branch information
DPeterK authored and pelson committed May 2, 2018
1 parent a01036b commit a3a9a19
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/iris/coords.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,13 @@ def __str__(self):
fmt = '{cls}({points}{bounds}' \
', standard_name={self.standard_name!r}' \
', calendar={self.units.calendar!r}{other_metadata})'
points = self._str_dates(self.points)
if self.units.is_long_time_interval():
# A time unit with a long time interval ("months" or "years")
# cannot be converted to a date using `num2date` so gracefully
# fall back to printing points as numbers, not datetimes.
points = self.points
else:
points = self._str_dates(self.points)
bounds = ''
if self.has_bounds():
bounds = ', bounds=' + self._str_dates(self.bounds)
Expand Down
27 changes: 26 additions & 1 deletion lib/iris/tests/unit/coords/test_Coord.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# (C) British Crown Copyright 2013 - 2017, Met Office
# (C) British Crown Copyright 2013 - 2018, Met Office
#
# This file is part of Iris.
#
Expand Down Expand Up @@ -323,5 +323,30 @@ def test_convert_unknown_units(self):
with self.assertRaisesRegexp(UnitConversionError, emsg):
coord.convert_units('degrees')


class Test___str__(tests.IrisTest):

def test_short_time_interval(self):
coord = DimCoord([5], standard_name='time',
units='days since 1970-01-01')
expected = ("DimCoord([1970-01-06 00:00:00], standard_name='time', "
"calendar='gregorian')")
result = coord.__str__()
self.assertEqual(expected, result)

def test_long_time_interval(self):
coord = DimCoord([5], standard_name='time',
units='years since 1970-01-01')
expected = "DimCoord([5], standard_name='time', calendar='gregorian')"
result = coord.__str__()
self.assertEqual(expected, result)

def test_non_time_unit(self):
coord = DimCoord([1.])
expected = repr(coord)
result = coord.__str__()
self.assertEqual(expected, result)


if __name__ == '__main__':
tests.main()
2 changes: 1 addition & 1 deletion requirements/core.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ netcdf4
numpy
scipy
# pyke (not pip installable) #conda: pyke
cf_units
cf_units>=1.2
dask>=0.17.1

0 comments on commit a3a9a19

Please sign in to comment.