|
| 1 | +import quivr as qv |
| 2 | +import spiceypy as sp |
| 3 | + |
| 4 | +from ...constants import KM_P_AU, S_P_DAY |
1 | 5 | from ...time import Timestamp
|
| 6 | +from ...utils.spice import get_perturber_state, setup_SPICE |
2 | 7 | from ..cartesian import CartesianCoordinates
|
3 |
| -from ..origin import Origin |
| 8 | +from ..origin import Origin, OriginCodes |
4 | 9 | from ..transform import cartesian_to_frame, transform_coordinates
|
5 | 10 | from .test_transforms_translation import assert_coords_equal
|
6 | 11 |
|
@@ -121,3 +126,130 @@ def test_transform_coordinates_frame(orbital_elements, orbital_elements_equatori
|
121 | 126 | assert_coords_equal(
|
122 | 127 | cartesian_coordinates_equatorial_actual, cartesian_coordinates_equatorial
|
123 | 128 | )
|
| 129 | + |
| 130 | + |
| 131 | +def test_transform_coordinates_to_itrf93(): |
| 132 | + """ |
| 133 | + Test that transform_coordinates correctly converts between ecliptic and |
| 134 | + ITRF93 cartesian coordinates |
| 135 | + """ |
| 136 | + setup_SPICE() |
| 137 | + |
| 138 | + # Get perturber states for a few of the planets |
| 139 | + times = Timestamp.from_mjd([59000, 59500, 60000], scale="tdb") |
| 140 | + states = CartesianCoordinates.empty() |
| 141 | + target_ids = [] |
| 142 | + for perturber in [ |
| 143 | + OriginCodes.MOON, |
| 144 | + OriginCodes.VENUS, |
| 145 | + OriginCodes.MARS_BARYCENTER, |
| 146 | + OriginCodes.JUPITER_BARYCENTER, |
| 147 | + OriginCodes.SATURN_BARYCENTER, |
| 148 | + OriginCodes.URANUS_BARYCENTER, |
| 149 | + OriginCodes.NEPTUNE_BARYCENTER, |
| 150 | + ]: |
| 151 | + states = qv.concatenate( |
| 152 | + [ |
| 153 | + states, |
| 154 | + get_perturber_state( |
| 155 | + perturber, times, origin=OriginCodes.SUN, frame="ecliptic" |
| 156 | + ), |
| 157 | + ] |
| 158 | + ) |
| 159 | + target_ids.extend(perturber.name for _ in range(len(times))) |
| 160 | + |
| 161 | + states_itrf93 = transform_coordinates(states, frame_out="itrf93") |
| 162 | + |
| 163 | + # Repeat with SPICE |
| 164 | + states_spice_itrf93 = CartesianCoordinates.empty() |
| 165 | + for coord, target_id in zip(states_itrf93, target_ids): |
| 166 | + # Rotate the coordinates to the ITRF93 frame using SPICE |
| 167 | + state, lt = sp.spkezr( |
| 168 | + target_id, coord.time.et()[0].as_py(), "ITRF93", "NONE", "SUN" |
| 169 | + ) |
| 170 | + states_spice_itrf93 = qv.concatenate( |
| 171 | + [ |
| 172 | + states_spice_itrf93, |
| 173 | + CartesianCoordinates.from_kwargs( |
| 174 | + x=[state[0] / KM_P_AU], |
| 175 | + y=[state[1] / KM_P_AU], |
| 176 | + z=[state[2] / KM_P_AU], |
| 177 | + vx=[state[3] / KM_P_AU * S_P_DAY], |
| 178 | + vy=[state[4] / KM_P_AU * S_P_DAY], |
| 179 | + vz=[state[5] / KM_P_AU * S_P_DAY], |
| 180 | + time=coord.time, # Add time from original coordinate |
| 181 | + frame="itrf93", # Specify the frame |
| 182 | + origin=Origin.from_kwargs(code=["SUN"]), # Specify the origin |
| 183 | + ), |
| 184 | + ] |
| 185 | + ) |
| 186 | + |
| 187 | + # Test that the two coordinate sets are equal within tolerance |
| 188 | + assert_coords_equal( |
| 189 | + states_itrf93, states_spice_itrf93, position_tol_mm=10, velocity_tol_nm_s=500 |
| 190 | + ) |
| 191 | + |
| 192 | + |
| 193 | +def test_transform_coordinates_from_itrf93(): |
| 194 | + """ |
| 195 | + Test that transform_coordinates correctly converts between ITRF93 and |
| 196 | + ecliptic cartesian coordinates |
| 197 | + """ |
| 198 | + setup_SPICE() |
| 199 | + |
| 200 | + # Get perturber states for a few of the planets |
| 201 | + times = Timestamp.from_mjd([59000, 59500, 60000], scale="tdb") |
| 202 | + states = CartesianCoordinates.empty() |
| 203 | + target_ids = [] |
| 204 | + for perturber in [ |
| 205 | + OriginCodes.MOON, |
| 206 | + OriginCodes.VENUS, |
| 207 | + OriginCodes.MARS_BARYCENTER, |
| 208 | + OriginCodes.JUPITER_BARYCENTER, |
| 209 | + OriginCodes.SATURN_BARYCENTER, |
| 210 | + OriginCodes.URANUS_BARYCENTER, |
| 211 | + OriginCodes.NEPTUNE_BARYCENTER, |
| 212 | + ]: |
| 213 | + states = qv.concatenate( |
| 214 | + [ |
| 215 | + states, |
| 216 | + get_perturber_state( |
| 217 | + perturber, times, origin=OriginCodes.SUN, frame="itrf93" |
| 218 | + ), |
| 219 | + ] |
| 220 | + ) |
| 221 | + target_ids.extend(perturber.name for _ in range(len(times))) |
| 222 | + |
| 223 | + states_ecliptic = transform_coordinates(states, frame_out="ecliptic") |
| 224 | + |
| 225 | + # Repeat with SPICE |
| 226 | + states_spice_ecliptic = CartesianCoordinates.empty() |
| 227 | + for coord, target_id in zip(states_ecliptic, target_ids): |
| 228 | + # Rotate the coordinates to the ecliptic frame using SPICE |
| 229 | + state, lt = sp.spkezr( |
| 230 | + target_id, coord.time.et()[0].as_py(), "ECLIPJ2000", "NONE", "SUN" |
| 231 | + ) |
| 232 | + states_spice_ecliptic = qv.concatenate( |
| 233 | + [ |
| 234 | + states_spice_ecliptic, |
| 235 | + CartesianCoordinates.from_kwargs( |
| 236 | + x=[state[0] / KM_P_AU], |
| 237 | + y=[state[1] / KM_P_AU], |
| 238 | + z=[state[2] / KM_P_AU], |
| 239 | + vx=[state[3] / KM_P_AU * S_P_DAY], |
| 240 | + vy=[state[4] / KM_P_AU * S_P_DAY], |
| 241 | + vz=[state[5] / KM_P_AU * S_P_DAY], |
| 242 | + time=coord.time, # Add time from original coordinate |
| 243 | + frame="ecliptic", # Specify the frame |
| 244 | + origin=Origin.from_kwargs(code=["SUN"]), # Specify the origin |
| 245 | + ), |
| 246 | + ] |
| 247 | + ) |
| 248 | + |
| 249 | + # Test that the two coordinate sets are equal within tolerance |
| 250 | + assert_coords_equal( |
| 251 | + states_ecliptic, |
| 252 | + states_spice_ecliptic, |
| 253 | + position_tol_mm=10, |
| 254 | + velocity_tol_nm_s=500, |
| 255 | + ) |
0 commit comments