astropy/astropy — pull request #1431 by mhvk (2013-09-10 05:32:18+00:00)
Title: Allow input/interaction/output with Quantities (closes #751)

In #751, it was argued that `Time` and `Quantity` should interact properly, with @taldcroft giving a list of expected behavour:

* One should be able to convert `TimeDelta` to different units with `to()`.
* Adding or subtracting `Quantity` and `TimeDelta` or `Time` (regardless of argument order) should give a `TimeDelta`.
* Multiplying or dividing a `TimeDelta` by a Quantity should result in a Quantity.
* Multiplying or dividing a DeltaTime by a scalar should result in a DeltaTime.

Furthermore, currently, input of Quantities is ignored -- just the value is taken (`TimeDelta(10.*u.s, format='jd')` silently gives a 10-day time difference; same for `Time`).

This PR addresses the initialisation of both `Time` and `TimeDelta` and implements most of the above -- the exception is that `Quantity TimeDelta` does not work, as `Quantity` (and `ndarray`) fail to return `NotImplemented` on trying to add a `Time` or `TimeDelta`.
Suggestions most welcome; if it is agreed this is in the right direction, I can add tests/documentation (PR's to my branch more than welcome...).

```
In [1]: from astropy.time import Time, TimeDelta; import astropy.units as u; import numpy as np

In [2]: t1=Time('2012-01-01', scale='utc')

In [3]: t1 + 1*u.d
Out[3]:

In [4]: t1 + 1*u.yr
Out[4]:
# u.yr = Julian year = 365.25 day, but there was a leap second and a leap day

In [5]: dt = Time('2013-01-01', scale='utc')-t1

In [6]: dt + 1.*u.d
Out[6]:

In [7]: dt * (10. * u.m / u.s**2)
Out[7]:

In [8]: 10.*u.cycle / dt
ERROR: TypeError: scalar 'TimeDelta' object is not subscriptable. [astropy.time.core]
... # Quantity should return NotImplemented so that TimeDelta can have a go
In [9]: 10. / dt
Out[9]:

In [10]: dt.to(u.yr)
Out[10]:

```