Skip to content

Commit c83ff88

Browse files
committed
Add an additional method and test to calculate the projected distance along the major and minor axis (issue astropy#1012)
1 parent bbdf2ce commit c83ff88

3 files changed

Lines changed: 126 additions & 1 deletion

File tree

‎astropy/coordinates/baseframe.py‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1806,7 +1806,7 @@ def separation(self, other):
18061806
self_unit_sph.lon, self_unit_sph.lat, other_unit_sph.lon, other_unit_sph.lat
18071807
)
18081808
return Angle(sep, unit=u.degree)
1809-
1809+
18101810
def separation_3d(self, other):
18111811
"""
18121812
Computes three dimensional separation between this coordinate
@@ -1856,6 +1856,57 @@ def separation_3d(self, other):
18561856
return dist
18571857
else:
18581858
return Distance(dist)
1859+
1860+
def separation_ellipse(self, other, eccentricity=1.0):
1861+
"""
1862+
Computes projected distance along the major axis and minor axis of an ellipse
1863+
which vertix is constructed by this coordinate and another.
1864+
1865+
Parameters
1866+
----------
1867+
other : `~astropy.coordinates.BaseCoordinateFrame`
1868+
The coordinate system to get the distance to.
1869+
eccentricity: optional input for fraction of the distance along the major axis.
1870+
1871+
Returns
1872+
-------
1873+
sep_a : `~astropy.coordinates.Distance`
1874+
The real-space distance between these two coordinates along the major axis.
1875+
sep_b : `~astropy.coordinates.Distance`
1876+
The real-space distance between these two coordinates along the minor axis.
1877+
1878+
Raises
1879+
------
1880+
ValueError
1881+
If this or the other coordinate do not have distances.
1882+
If the range of eccentricity is out of [0.0, 1.0]
1883+
"""
1884+
1885+
if issubclass(self.data.__class__, r.UnitSphericalRepresentation):
1886+
raise ValueError(
1887+
"This object does not have a distance; cannot compute the projected separation."
1888+
)
1889+
1890+
if not 0.0 <= eccentricity <= 1.0:
1891+
raise ValueError(
1892+
"Eccentricity should be within the range of [0.0, 1.0]; cannot compute the projected separation"
1893+
)
1894+
1895+
from .angles import angular_separation
1896+
from .distances import Distance
1897+
1898+
self_unit_sph = self.represent_as(r.UnitSphericalRepresentation)
1899+
other_transformed = other.transform_to(self)
1900+
other_unit_sph = other_transformed.represent_as(r.UnitSphericalRepresentation)
1901+
1902+
# Get the separation as a Quantity, convert to Angle in degrees
1903+
sep = angular_separation(
1904+
self_unit_sph.lon, self_unit_sph.lat, other_unit_sph.lon, other_unit_sph.lat
1905+
)
1906+
1907+
sep_a = self.distance * np.sin(sep/2.0) # a/D = sin(theta/2)
1908+
sep_b = np.sqrt(sep_a**2 * (1 - eccentricity**2))
1909+
return Distance(sep_a), Distance(sep_b)
18591910

18601911
@property
18611912
def cartesian(self):

‎astropy/coordinates/sky_coordinate.py‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,65 @@ def separation_3d(self, other):
12371237
c1 = self.cartesian.without_differentials()
12381238
c2 = other.cartesian.without_differentials()
12391239
return Distance((c1 - c2).norm())
1240+
1241+
def separation_ellipse(self, other, eccentricity=1.0):
1242+
"""
1243+
Computes projected distance along the major axis and minor axis of an ellipse
1244+
which vertix is constructed by this coordinate and another.
1245+
1246+
Parameters
1247+
----------
1248+
other : `~astropy.coordinates.SkyCoord` or `~astropy.coordinates.BaseCoordinateFrame`
1249+
The coordinate to get the separation to.
1250+
eccentricity: optional input for fraction of the distance along the major axis.
12401251
1252+
Returns
1253+
-------
1254+
sep_a : `~astropy.coordinates.Distance`
1255+
The real-space distance between these two coordinates along the major axis.
1256+
sep_b : `~astropy.coordinates.Distance`
1257+
The real-space distance between these two coordinates along the minor axis.
1258+
1259+
Raises
1260+
------
1261+
ValueError
1262+
If this or the other coordinate do not have distances.
1263+
If the range of eccentricity is out of [0.0, 1.0]
1264+
"""
1265+
if not self.is_equivalent_frame(other):
1266+
try:
1267+
kwargs = (
1268+
{"merge_attributes": False} if isinstance(other, SkyCoord) else {}
1269+
)
1270+
other = other.transform_to(self, **kwargs)
1271+
except TypeError:
1272+
raise TypeError(
1273+
"Can only get separation to another SkyCoord "
1274+
"or a coordinate frame with data"
1275+
)
1276+
1277+
if issubclass(self.data.__class__, UnitSphericalRepresentation):
1278+
raise ValueError(
1279+
"This object does not have a distance; cannot compute the projected separation."
1280+
)
1281+
1282+
if not 0.0 <= eccentricity <= 1.0:
1283+
raise ValueError(
1284+
"Eccentricity should be within the range of [0.0, 1.0]; cannot compute the projected separation"
1285+
)
1286+
1287+
lon1 = self.spherical.lon
1288+
lat1 = self.spherical.lat
1289+
lon2 = other.spherical.lon
1290+
lat2 = other.spherical.lat
1291+
1292+
from .angles import angular_separation
1293+
1294+
sep = angular_separation(lon1, lat1, lon2, lat2)
1295+
sep_a = self.distance * np.sin(sep/2.0) # a/D = sin(theta/2)
1296+
sep_b = np.sqrt(sep_a**2 * (1 - eccentricity**2))
1297+
return Distance(sep_a), Distance(sep_b)
1298+
12411299
def spherical_offsets_to(self, tocoord):
12421300
r"""
12431301
Computes angular offsets to go *from* this coordinate *to* another.

‎astropy/coordinates/tests/test_angular_separation.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ def test_proj_separations():
9090
assert_allclose(ncp.separation(ngp.transform_to(ICRS())).degree, 62.87174758503201)
9191

9292

93+
def test_elliptical_separations():
94+
"""
95+
Test elliptical separation functionality
96+
"""
97+
c1 = ICRS(ra=0 * u.deg, dec=0 * u.deg)
98+
c2 = ICRS(ra=0 * u.deg, dec=1 * u.deg, distance=10 * u.kpc)
99+
100+
eccentricity = 0.8
101+
sep_a, sep_b = c2.separation_ellipse(c1, eccentricity)
102+
103+
assert isinstance(sep_a, Distance)
104+
assert isinstance(sep_b, Distance)
105+
106+
assert_allclose(eccentricity, np.sqrt(1-sep_b.value**2/sep_a.value**2))
107+
108+
93109
def test_3d_separations():
94110
"""
95111
Test 3D separation functionality

0 commit comments

Comments
 (0)