EpochConverter.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """EpochConverter module containing class EpochConverter."""
  2. import numpy as np
  3. from matplotlib import cbook
  4. import matplotlib.units as units
  5. import matplotlib.dates as date_ticker
  6. __all__ = ['EpochConverter']
  7. class EpochConverter(units.ConversionInterface):
  8. """
  9. Provides Matplotlib conversion functionality for Monte Epoch and Duration
  10. classes.
  11. """
  12. # julian date reference for "Jan 1, 0001" minus 1 day because
  13. # Matplotlib really wants "Jan 0, 0001"
  14. jdRef = 1721425.5 - 1
  15. @staticmethod
  16. def axisinfo(unit, axis):
  17. """: Returns information on how to handle an axis that has Epoch data.
  18. = INPUT VARIABLES
  19. - unit The units to use for a axis with Epoch data.
  20. = RETURN VALUE
  21. - Returns a AxisInfo data structure that contains
  22. minor/major formatters, major/minor locators, and default
  23. label information.
  24. """
  25. majloc = date_ticker.AutoDateLocator()
  26. majfmt = date_ticker.AutoDateFormatter(majloc)
  27. return units.AxisInfo(majloc=majloc, majfmt=majfmt, label=unit)
  28. @staticmethod
  29. def float2epoch(value, unit):
  30. """: Convert a Matplotlib floating-point date into an Epoch of the
  31. specified units.
  32. = INPUT VARIABLES
  33. - value The Matplotlib floating-point date.
  34. - unit The unit system to use for the Epoch.
  35. = RETURN VALUE
  36. - Returns the value converted to an Epoch in the specified time system.
  37. """
  38. # Delay-load due to circular dependencies.
  39. import matplotlib.testing.jpl_units as U
  40. secPastRef = value * 86400.0 * U.UnitDbl(1.0, 'sec')
  41. return U.Epoch(unit, secPastRef, EpochConverter.jdRef)
  42. @staticmethod
  43. def epoch2float(value, unit):
  44. """: Convert an Epoch value to a float suitable for plotting as a
  45. python datetime object.
  46. = INPUT VARIABLES
  47. - value An Epoch or list of Epochs that need to be converted.
  48. - unit The units to use for an axis with Epoch data.
  49. = RETURN VALUE
  50. - Returns the value parameter converted to floats.
  51. """
  52. return value.julianDate(unit) - EpochConverter.jdRef
  53. @staticmethod
  54. def duration2float(value):
  55. """: Convert a Duration value to a float suitable for plotting as a
  56. python datetime object.
  57. = INPUT VARIABLES
  58. - value A Duration or list of Durations that need to be converted.
  59. = RETURN VALUE
  60. - Returns the value parameter converted to floats.
  61. """
  62. return value.seconds() / 86400.0
  63. @staticmethod
  64. def convert(value, unit, axis):
  65. """: Convert value using unit to a float. If value is a sequence, return
  66. the converted sequence.
  67. = INPUT VARIABLES
  68. - value The value or list of values that need to be converted.
  69. - unit The units to use for an axis with Epoch data.
  70. = RETURN VALUE
  71. - Returns the value parameter converted to floats.
  72. """
  73. # Delay-load due to circular dependencies.
  74. import matplotlib.testing.jpl_units as U
  75. if not cbook.is_scalar_or_string(value):
  76. return [EpochConverter.convert(x, unit, axis) for x in value]
  77. if units.ConversionInterface.is_numlike(value):
  78. return value
  79. if unit is None:
  80. unit = EpochConverter.default_units(value, axis)
  81. if isinstance(value, U.Duration):
  82. return EpochConverter.duration2float(value)
  83. else:
  84. return EpochConverter.epoch2float(value, unit)
  85. @staticmethod
  86. def default_units(value, axis):
  87. """: Return the default unit for value, or None.
  88. = INPUT VARIABLES
  89. - value The value or list of values that need units.
  90. = RETURN VALUE
  91. - Returns the default units to use for value.
  92. """
  93. if cbook.is_scalar_or_string(value):
  94. return value.frame()
  95. else:
  96. return EpochConverter.default_units(value[0], axis)