Skip to content
This repository was archived by the owner on Oct 14, 2023. It is now read-only.

Commit 23c2dbd

Browse files
Fixed a few issues with Orbit.from_coords
- Handled the case where the coordinate instance has multiple representations - Handled the case where coordinates instance doesn't have shape 0 - Do not transform coordinates when coordinate instance is already in required frame(i.e. in a frame parallel to ICRS and centered at attractor) - Added tests for above changes Issue: #434
1 parent 58e40e0 commit 23c2dbd

2 files changed

Lines changed: 49 additions & 8 deletions

File tree

‎src/poliastro/tests/tests_twobody/test_orbit.py‎

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -601,11 +601,11 @@ def test_from_coord_fails_if_no_time_differential():
601601
pos = [30000, 0, 0] * u.km
602602
cartrep = CartesianRepresentation(*pos)
603603

604-
# Method fails if SkyCoord instance doesn't contain a differential with respect to time
604+
# Method fails if coordinate instance doesn't contain a differential with respect to time
605605
with pytest.raises(ValueError) as excinfo:
606606
ss = Orbit.from_coords(Earth, SkyCoord(cartrep))
607607
assert (
608-
"ValueError: SkyCoord instance passed as coord doesn't have a differential with respect to time"
608+
"ValueError: Coordinate instance doesn't have a differential with respect to time"
609609
in excinfo.exconly()
610610
)
611611

@@ -654,5 +654,35 @@ def test_orbit_creation_using_frame_obj(attractor, frame):
654654
pos_transformed_to_irf = coord_transformed_to_irf.cartesian.xyz
655655
vel_transformed_to_irf = coord_transformed_to_irf.cartesian.differentials["s"].d_xyz
656656

657-
assert (o.r == pos_transformed_to_irf).all()
658-
assert (o.v == vel_transformed_to_irf).all()
657+
assert_quantity_allclose(o.r, pos_transformed_to_irf, atol=1e-5 * u.km)
658+
assert_quantity_allclose(o.v, vel_transformed_to_irf, atol=1e-5 * u.km / u.s)
659+
660+
661+
def test_from_coord_fails_for_multiple_positions():
662+
cartdiff = CartesianDifferential(
663+
[[0, 1, 0], [-0.1, 0.9, 0]] * u.km / u.s, xyz_axis=1
664+
)
665+
cartrep = CartesianRepresentation(
666+
[[1, 0, 0], [0.9, 0.1, 0]] * u.km, differentials=cartdiff, xyz_axis=1
667+
)
668+
coords = GCRS(cartrep, representation_type=CartesianRepresentation, obstime=J2000)
669+
670+
with pytest.raises(ValueError) as excinfo:
671+
ss = Orbit.from_coords(Earth, coords)
672+
assert (
673+
"ValueError: Coordinate instance must represents exactly 1 position, found: 2"
674+
in excinfo.exconly()
675+
)
676+
677+
678+
def test_from_coord_if_coord_is_not_of_shape_zero():
679+
pos = [0, 1, 0]
680+
vel = [1, 0, 0]
681+
cartdiff = CartesianDifferential([vel] * u.km / u.s, xyz_axis=1)
682+
cartrep = CartesianRepresentation([pos] * u.km, differentials=cartdiff, xyz_axis=1)
683+
coords = GCRS(cartrep, representation_type=CartesianRepresentation, obstime=J2000)
684+
685+
ss = Orbit.from_coords(Earth, coords)
686+
687+
assert_quantity_allclose(ss.r, pos * u.km, rtol=1e-5)
688+
assert_quantity_allclose(ss.v, vel * u.km / u.s, rtol=1e-5)

‎src/poliastro/twobody/orbit.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,20 +242,31 @@ def from_coords(cls, attractor, coord):
242242
----------
243243
attractor: Body
244244
Main attractor
245-
coord: astropy.coordinates.SkyCoord
246-
Position and velocity vectors in any reference frame. Note that `SkyCoord` must have
245+
coord: astropy.coordinates.SkyCoord or BaseCoordinateFrame
246+
Position and velocity vectors in any reference frame. Note that coord must have
247247
a representation and its differential with respect to time.
248248
249249
"""
250250
if "s" not in coord.cartesian.differentials:
251251
raise ValueError(
252-
"SkyCoord instance passed as coord doesn't have a differential with respect to time"
252+
"Coordinate instance doesn't have a differential with respect to time"
253+
)
254+
if coord.size != 1:
255+
raise ValueError(
256+
"Coordinate instance must represents exactly 1 position, found: %d"
257+
% coord.size
253258
)
254259

260+
# Reshape coordinate to 0 dimension if it is not already dimensionless.
261+
coord = coord.reshape(())
262+
255263
# Get an inertial reference frame parallel to ICRS and centered at attractor
256264
inertial_frame_at_body_centre = get_frame(attractor, Planes.EARTH_EQUATOR)
257265

258-
coord_in_irf = coord.transform_to(inertial_frame_at_body_centre)
266+
if not coord.is_equivalent_frame(inertial_frame_at_body_centre):
267+
coord_in_irf = coord.transform_to(inertial_frame_at_body_centre)
268+
else:
269+
coord_in_irf = coord
259270

260271
pos = coord_in_irf.cartesian.xyz
261272
vel = coord_in_irf.cartesian.differentials["s"].d_xyz

0 commit comments

Comments
 (0)