EpochConverter.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """EpochConverter module containing class EpochConverter."""
  2. from matplotlib import cbook, units
  3. import matplotlib.dates as date_ticker
  4. __all__ = ['EpochConverter']
  5. class EpochConverter(units.ConversionInterface):
  6. """
  7. Provides Matplotlib conversion functionality for Monte Epoch and Duration
  8. classes.
  9. """
  10. jdRef = 1721425.5
  11. @staticmethod
  12. def axisinfo(unit, axis):
  13. # docstring inherited
  14. majloc = date_ticker.AutoDateLocator()
  15. majfmt = date_ticker.AutoDateFormatter(majloc)
  16. return units.AxisInfo(majloc=majloc, majfmt=majfmt, label=unit)
  17. @staticmethod
  18. def float2epoch(value, unit):
  19. """
  20. Convert a Matplotlib floating-point date into an Epoch of the specified
  21. units.
  22. = INPUT VARIABLES
  23. - value The Matplotlib floating-point date.
  24. - unit The unit system to use for the Epoch.
  25. = RETURN VALUE
  26. - Returns the value converted to an Epoch in the specified time system.
  27. """
  28. # Delay-load due to circular dependencies.
  29. import matplotlib.testing.jpl_units as U
  30. secPastRef = value * 86400.0 * U.UnitDbl(1.0, 'sec')
  31. return U.Epoch(unit, secPastRef, EpochConverter.jdRef)
  32. @staticmethod
  33. def epoch2float(value, unit):
  34. """
  35. Convert an Epoch value to a float suitable for plotting as a python
  36. datetime object.
  37. = INPUT VARIABLES
  38. - value An Epoch or list of Epochs that need to be converted.
  39. - unit The units to use for an axis with Epoch data.
  40. = RETURN VALUE
  41. - Returns the value parameter converted to floats.
  42. """
  43. return value.julianDate(unit) - EpochConverter.jdRef
  44. @staticmethod
  45. def duration2float(value):
  46. """
  47. Convert a Duration value to a float suitable for plotting as a python
  48. datetime object.
  49. = INPUT VARIABLES
  50. - value A Duration or list of Durations that need to be converted.
  51. = RETURN VALUE
  52. - Returns the value parameter converted to floats.
  53. """
  54. return value.seconds() / 86400.0
  55. @staticmethod
  56. def convert(value, unit, axis):
  57. # docstring inherited
  58. # Delay-load due to circular dependencies.
  59. import matplotlib.testing.jpl_units as U
  60. if not cbook.is_scalar_or_string(value):
  61. return [EpochConverter.convert(x, unit, axis) for x in value]
  62. if unit is None:
  63. unit = EpochConverter.default_units(value, axis)
  64. if isinstance(value, U.Duration):
  65. return EpochConverter.duration2float(value)
  66. else:
  67. return EpochConverter.epoch2float(value, unit)
  68. @staticmethod
  69. def default_units(value, axis):
  70. # docstring inherited
  71. if cbook.is_scalar_or_string(value):
  72. return value.frame()
  73. else:
  74. return EpochConverter.default_units(value[0], axis)