Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented fixes from eteq/astropy#10 #1

Merged
merged 8 commits into from Apr 17, 2014
80 changes: 34 additions & 46 deletions astropy/coordinates/representation.py
@@ -1,9 +1,8 @@
# In this file, we define the coordinate representation classes, which are
# used to represent low-level cartesian, spherical, cylindrical, and other #
# coordinate. All classes should define a to_cartesian method and a
# from_cartesian class method. By default, transformations are done via the
# cartesian system, but classes that want to define a smarter transformation
# path can overload the ``represent_as`` method.
"""
In this module, we define the coordinate representation classes, which are
used to represent low-level cartesian, spherical, cylindrical, and other
coordinates.
"""

from __future__ import (absolute_import, division, print_function,
unicode_literals)
Expand All @@ -17,16 +16,9 @@
from .distances import Distance
from ..extern import six

# Suggestions to improve API
#
# - change PhysicistSphericalRepresentation to PhysicsSphericalRepresentation
# (implemented below).
#
# - add a rotated() method that can rotate the 3D coordinates

__all__ = ["CartesianRepresentation", "SphericalRepresentation",
"UnitSphericalRepresentation", "PhysicsSphericalRepresentation",
"CylindricalRepresentation"]
__all__ = ["BaseRepresentation", "CartesianRepresentation",
"SphericalRepresentation", "UnitSphericalRepresentation",
"PhysicsSphericalRepresentation", "CylindricalRepresentation"]


def broadcast_quantity(*args, **kwargs):
Expand All @@ -43,7 +35,16 @@ def broadcast_quantity(*args, **kwargs):
@six.add_metaclass(abc.ABCMeta)
class BaseRepresentation(object):
"""
Base Representation object, for representing a point in a 3D coordinate system
Base Representation object, for representing a point in a 3D coordinate
system.

Notes
-----
All representation classes should subclass this base representation
class. All subclasses should then define a ``to_cartesian`` method and a
``from_cartesian`` class method. By default, transformations are done via
the cartesian system, but classes that want to define a smarter
transformation path can overload the ``represent_as`` method.
"""

def represent_as(self, other_class):
Expand Down Expand Up @@ -120,7 +121,7 @@ def __init__(self, x, y=None, z=None, unit=None, copy=True):
@property
def x(self):
"""
The x position of the point(s).
The x componen of the point(s).
"""
return self._x

Expand Down Expand Up @@ -232,9 +233,10 @@ def to_cartesian(self):
"""

# We need to convert Distance to Quantity to allow negative values.
# At the moment, there is no easy way to convert Distance objects to
# Quantity objects (https://github.com/astropy/astropy/issues/2259)
d = self.distance.view(u.Quantity)
if isinstance(self.distance, Distance):
d = self.distance.view(u.Quantity)
else:
d = self.distance

x = d * np.cos(self.lat) * np.cos(self.lon)
y = d * np.cos(self.lat) * np.sin(self.lon)
Expand All @@ -249,12 +251,8 @@ def from_cartesian(cls, cart):
coordinates.
"""

xsq = cart.x ** 2
ysq = cart.y ** 2
zsq = cart.z ** 2

r = (xsq + ysq + zsq) ** 0.5
s = (xsq + ysq) ** 0.5
s = np.hypot(cart.x, cart.y)
r = np.hypot(s, cart.z)

lon = np.arctan2(cart.y, cart.x)
lat = np.arctan2(cart.z, s)
Expand Down Expand Up @@ -331,11 +329,7 @@ def from_cartesian(cls, cart):
coordinates.
"""

xsq = cart.x ** 2
ysq = cart.y ** 2
zsq = cart.z ** 2

s = (xsq + ysq) ** 0.5
s = np.hypot(cart.x, cart.y)

lon = np.arctan2(cart.y, cart.x)
lat = np.arctan2(cart.z, s)
Expand Down Expand Up @@ -423,9 +417,10 @@ def to_cartesian(self):
"""

# We need to convert Distance to Quantity to allow negative values.
# At the moment, there is no easy way to convert Distance objects to
# Quantity objects (https://github.com/astropy/astropy/issues/2259)
d = self.distance.view(u.Quantity)
if isinstance(self.distance, Distance):
d = self.distance.view(u.Quantity)
else:
d = self.distance

x = d * np.sin(self.theta) * np.cos(self.phi)
y = d * np.sin(self.theta) * np.sin(self.phi)
Expand All @@ -440,12 +435,8 @@ def from_cartesian(cls, cart):
coordinates.
"""

xsq = cart.x ** 2
ysq = cart.y ** 2
zsq = cart.z ** 2

r = (xsq + ysq + zsq) ** 0.5
s = (xsq + ysq) ** 0.5
s = np.hypot(cart.x, cart.y)
r = np.hypot(s, cart.z)

phi = np.arctan2(cart.y, cart.x)
theta = np.arctan2(s, cart.z)
Expand Down Expand Up @@ -520,11 +511,8 @@ def from_cartesian(cls, cart):
coordinates.
"""

rho = np.sqrt(cart.x ** 2 + cart.y ** 2)

phi = np.zeros(cart.x.shape) * u.deg
phi[rho > 0] = np.arctan2(cart.y, cart.x)

rho = np.hypot(cart.x, cart.y)
phi = np.arctan2(cart.y, cart.x)
z = cart.z

return CylindricalRepresentation(rho=rho, phi=phi, z=z)
Expand Down