Skip to content

Commit

Permalink
Merge pull request matplotlib#11960 from anntzer/jpl
Browse files Browse the repository at this point in the history
Make jpl_units a bit less painful to read.
  • Loading branch information
dopplershift authored Aug 28, 2018
2 parents 3c0bd99 + e1bb33c commit 58e60b3
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 181 deletions.
32 changes: 0 additions & 32 deletions lib/matplotlib/testing/jpl_units/Duration.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
# ==========================================================================
#
# Duration
#
# ==========================================================================


"""Duration module."""

# ==========================================================================
# Place all imports after here.
#
import operator
#
# Place all imports before here.
# ==========================================================================


# ==========================================================================
class Duration(object):
"""Class Duration in development.
"""
allowed = ["ET", "UTC"]

# ----------------------------------------------------------------------
def __init__(self, frame, seconds):
"""Create a new Duration object.
Expand All @@ -41,31 +26,25 @@ def __init__(self, frame, seconds):
self._frame = frame
self._seconds = seconds

# ----------------------------------------------------------------------
def frame(self):
"""Return the frame the duration is in."""
return self._frame

# ----------------------------------------------------------------------
def __abs__(self):
"""Return the absolute value of the duration."""
return Duration(self._frame, abs(self._seconds))

# ----------------------------------------------------------------------
def __neg__(self):
"""Return the negative value of this Duration."""
return Duration(self._frame, -self._seconds)

# ----------------------------------------------------------------------
def seconds(self):
"""Return the number of seconds in the Duration."""
return self._seconds

# ----------------------------------------------------------------------
def __bool__(self):
return self._seconds != 0

# ----------------------------------------------------------------------
def __eq__(self, rhs):
return self._cmp(rhs, operator.eq)

Expand Down Expand Up @@ -97,7 +76,6 @@ def _cmp(self, rhs, op):
self.checkSameFrame(rhs, "compare")
return op(self._seconds, rhs._seconds)

# ----------------------------------------------------------------------
def __add__(self, rhs):
"""Add two Durations.
Expand All @@ -119,7 +97,6 @@ def __add__(self, rhs):
self.checkSameFrame(rhs, "add")
return Duration(self._frame, self._seconds + rhs._seconds)

# ----------------------------------------------------------------------
def __sub__(self, rhs):
"""Subtract two Durations.
Expand All @@ -135,7 +112,6 @@ def __sub__(self, rhs):
self.checkSameFrame(rhs, "sub")
return Duration(self._frame, self._seconds - rhs._seconds)

# ----------------------------------------------------------------------
def __mul__(self, rhs):
"""Scale a UnitDbl by a value.
Expand All @@ -147,7 +123,6 @@ def __mul__(self, rhs):
"""
return Duration(self._frame, self._seconds * float(rhs))

# ----------------------------------------------------------------------
def __rmul__(self, lhs):
"""Scale a Duration by a value.
Expand All @@ -159,7 +134,6 @@ def __rmul__(self, lhs):
"""
return Duration(self._frame, self._seconds * float(lhs))

# ----------------------------------------------------------------------
def __div__(self, rhs):
"""Divide a Duration by a value.
Expand All @@ -171,7 +145,6 @@ def __div__(self, rhs):
"""
return Duration(self._frame, self._seconds / rhs)

# ----------------------------------------------------------------------
def __rdiv__(self, rhs):
"""Divide a Duration by a value.
Expand All @@ -183,17 +156,14 @@ def __rdiv__(self, rhs):
"""
return Duration(self._frame, rhs / self._seconds)

# ----------------------------------------------------------------------
def __str__(self):
"""Print the Duration."""
return "%g %s" % (self._seconds, self._frame)

# ----------------------------------------------------------------------
def __repr__(self):
"""Print the Duration."""
return "Duration('%s', %g)" % (self._frame, self._seconds)

# ----------------------------------------------------------------------
def checkSameFrame(self, rhs, func):
"""Check to see if frames are the same.
Expand All @@ -210,5 +180,3 @@ def checkSameFrame(self, rhs, func):
"LHS: %s\n" \
"RHS: %s" % (func, self._frame, rhs._frame)
raise ValueError(msg)

# ==========================================================================
31 changes: 1 addition & 30 deletions lib/matplotlib/testing/jpl_units/Epoch.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
# ===========================================================================
#
# Epoch
#
# ===========================================================================


"""Epoch module."""

# ===========================================================================
# Place all imports after here.
#
import operator
import math
import datetime as DT
from matplotlib.dates import date2num
#
# Place all imports before here.
# ===========================================================================


# ===========================================================================
class Epoch(object):
# Frame conversion offsets in seconds
# t(TO) = t(FROM) + allowed[ FROM ][ TO ]
Expand All @@ -32,7 +18,6 @@ class Epoch(object):
},
}

# -----------------------------------------------------------------------
def __init__(self, frame, sec=None, jd=None, daynum=None, dt=None):
"""Create a new Epoch object.
Expand All @@ -44,7 +29,6 @@ def __init__(self, frame, sec=None, jd=None, daynum=None, dt=None):
or using a matplotlib day number
# Epoch('ET', daynum=730119.5)
= ERROR CONDITIONS
- If the input units are not in the allowed list, an error is thrown.
Expand Down Expand Up @@ -97,7 +81,6 @@ def __init__(self, frame, sec=None, jd=None, daynum=None, dt=None):
self._jd += deltaDays
self._seconds -= deltaDays * 86400.0

# -----------------------------------------------------------------------
def convert(self, frame):
if self._frame == frame:
return self
Expand All @@ -106,19 +89,16 @@ def convert(self, frame):

return Epoch(frame, self._seconds + offset, self._jd)

# -----------------------------------------------------------------------
def frame(self):
return self._frame

# -----------------------------------------------------------------------
def julianDate(self, frame):
t = self
if frame != self._frame:
t = self.convert(frame)

return t._jd + t._seconds / 86400.0

# -----------------------------------------------------------------------
def secondsPast(self, frame, jd):
t = self
if frame != self._frame:
Expand All @@ -127,7 +107,6 @@ def secondsPast(self, frame, jd):
delta = t._jd - jd
return t._seconds + delta * 86400

# -----------------------------------------------------------------------
def __eq__(self, rhs):
return self._cmp(rhs, operator.eq)

Expand Down Expand Up @@ -165,7 +144,6 @@ def _cmp(self, rhs, op):

return op(t._seconds, rhs._seconds)

# -----------------------------------------------------------------------
def __add__(self, rhs):
"""Add a duration to an Epoch.
Expand All @@ -183,7 +161,6 @@ def __add__(self, rhs):

return Epoch(t._frame, sec, t._jd)

# -----------------------------------------------------------------------
def __sub__(self, rhs):
"""Subtract two Epoch's or a Duration from an Epoch.
Expand Down Expand Up @@ -214,17 +191,15 @@ def __sub__(self, rhs):

return U.Duration(rhs._frame, days*86400 + sec)

# -----------------------------------------------------------------------
def __str__(self):
"""Print the Epoch."""
return "%22.15e %s" % (self.julianDate(self._frame), self._frame)

# -----------------------------------------------------------------------
def __repr__(self):
"""Print the Epoch."""
return str(self)

# -----------------------------------------------------------------------
@staticmethod
def range(start, stop, step):
"""Generate a range of Epoch objects.
Expand Down Expand Up @@ -252,7 +227,3 @@ def range(start, stop, step):
i += 1

return elems

range = staticmethod(range)

# ===========================================================================
20 changes: 0 additions & 20 deletions lib/matplotlib/testing/jpl_units/EpochConverter.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
# ==========================================================================
#
# EpochConverter
#
# ==========================================================================


"""EpochConverter module containing class EpochConverter."""

# ==========================================================================
# Place all imports after here.
#
import matplotlib.units as units
import matplotlib.dates as date_ticker
from matplotlib.cbook import iterable
#
# Place all imports before here.
# ==========================================================================

__all__ = ['EpochConverter']


# ==========================================================================
class EpochConverter(units.ConversionInterface):
""": A matplotlib converter class. Provides matplotlib conversion
functionality for Monte Epoch and Duration classes.
Expand All @@ -30,7 +16,6 @@ class EpochConverter(units.ConversionInterface):
# matplotlib really wants "Jan 0, 0001"
jdRef = 1721425.5 - 1

# -----------------------------------------------------------------------
@staticmethod
def axisinfo(unit, axis):
""": Returns information on how to handle an axis that has Epoch data.
Expand All @@ -49,7 +34,6 @@ def axisinfo(unit, axis):

return units.AxisInfo(majloc=majloc, majfmt=majfmt, label=unit)

# -----------------------------------------------------------------------
@staticmethod
def float2epoch(value, unit):
""": Convert a matplotlib floating-point date into an Epoch of the
Expand All @@ -68,7 +52,6 @@ def float2epoch(value, unit):
secPastRef = value * 86400.0 * U.UnitDbl(1.0, 'sec')
return U.Epoch(unit, secPastRef, EpochConverter.jdRef)

# -----------------------------------------------------------------------
@staticmethod
def epoch2float(value, unit):
""": Convert an Epoch value to a float suitible for plotting as a
Expand All @@ -83,7 +66,6 @@ def epoch2float(value, unit):
"""
return value.julianDate(unit) - EpochConverter.jdRef

# -----------------------------------------------------------------------
@staticmethod
def duration2float(value):
""": Convert a Duration value to a float suitible for plotting as a
Expand All @@ -97,7 +79,6 @@ def duration2float(value):
"""
return value.seconds() / 86400.0

# -----------------------------------------------------------------------
@staticmethod
def convert(value, unit, axis):
""": Convert value using unit to a float. If value is a sequence, return
Expand Down Expand Up @@ -139,7 +120,6 @@ def convert(value, unit, axis):
else:
return EpochConverter.epoch2float(value, unit)

# -----------------------------------------------------------------------
@staticmethod
def default_units(value, axis):
""": Return the default unit for value, or None.
Expand Down
23 changes: 3 additions & 20 deletions lib/matplotlib/testing/jpl_units/StrConverter.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
# ==========================================================================
#
# StrConverter
#
# ==========================================================================


"""StrConverter module containing class StrConverter."""

# ==========================================================================
# Place all imports after here.
#
import matplotlib.units as units
from matplotlib.cbook import iterable
#
# Place all imports before here.
# ==========================================================================

__all__ = ['StrConverter']


# ==========================================================================
class StrConverter(units.ConversionInterface):
""": A matplotlib converter class. Provides matplotlib conversion
functionality for string data values.
Expand All @@ -31,7 +17,6 @@ class StrConverter(units.ConversionInterface):
- 'sorted-inverted' : A combination of 'sorted' and 'inverted'
"""

# -----------------------------------------------------------------------
@staticmethod
def axisinfo(unit, axis):
""": Returns information on how to handle an axis that has string data.
Expand All @@ -48,7 +33,6 @@ def axisinfo(unit, axis):

return None

# -----------------------------------------------------------------------
@staticmethod
def convert(value, unit, axis):
""": Convert value using unit to a float. If value is a sequence, return
Expand Down Expand Up @@ -105,10 +89,10 @@ def convert(value, unit, axis):
# DISABLED: this is due to design and is not really a bug.
# DISABLED: If this gets changed, then we can activate the following
# DISABLED: block of code. Note that this works for line plots.
# DISABLED if (unit):
# DISABLED if (unit.find("sorted") > -1):
# DISABLED if unit:
# DISABLED if unit.find("sorted") > -1:
# DISABLED labels.sort()
# DISABLED if (unit.find("inverted") > -1):
# DISABLED if unit.find("inverted") > -1:
# DISABLED labels = labels[::-1]

# add padding (so they do not appear on the axes themselves)
Expand Down Expand Up @@ -140,7 +124,6 @@ def convert(value, unit, axis):
ax.viewLim.ignore(-1)
return result

# -----------------------------------------------------------------------
@staticmethod
def default_units(value, axis):
""": Return the default unit for value, or None.
Expand Down
Loading

0 comments on commit 58e60b3

Please sign in to comment.