1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""
Unit handling utilities for astropy integration.
This module provides helper functions for converting between astropy Quantity
objects and raw numerical values used by the Rust backend. It enables the
high-level Python API to accept quantities with units while maintaining
backward compatibility with raw floats/arrays.
"""
# Common unit types for validation
= # Base SI unit for distance
= / # Base SI unit for velocity
= # Base SI unit for angles
= # Base SI unit for time
= # For eccentricity, etc.
"""
Check if a value is an astropy Quantity object.
Parameters
----------
value : any
Value to check
Returns
-------
bool
True if value is a Quantity, False otherwise
"""
return and
"""
Convert a Quantity to SI base unit value (scalar or array).
Handles both raw values (pass through) and Quantity objects (convert).
Parameters
----------
quantity : float, np.ndarray, or Quantity
Input value with or without units
target_unit : astropy.units.Unit
Target SI unit for conversion (e.g., u.m, u.m/u.s)
name : str, optional
Name of the parameter for error messages
Returns
-------
float or np.ndarray
Value in SI units (dimensionless)
Raises
------
ValueError
If quantity has incompatible units
Examples
--------
>>> import numpy as np
>>> from astropy import units as u
>>>
>>> # Raw value passes through
>>> _to_si_value(1000.0, u.m, "distance")
1000.0
>>>
>>> # Quantity converts to SI
>>> _to_si_value(1 << u.km, u.m, "distance")
1000.0
>>>
>>> # Array with units
>>> r = [7000, 0, 0] << u.km
>>> _to_si_value(r, u.m, "position")
array([7000000., 0., 0.])
"""
# If it's a Quantity, validate and convert
# Convert to target unit and extract value
=
return
# Raw value - return as-is (assume SI units)
return
"""
Convert length/distance to meters (SI).
Parameters
----------
value : float or Quantity
Length value (meters if raw, or with units)
name : str
Parameter name for error messages
Returns
-------
float
Length in meters
"""
=
return
"""
Convert velocity to m/s (SI).
Parameters
----------
value : float or Quantity
Velocity value (m/s if raw, or with units)
name : str
Parameter name for error messages
Returns
-------
float
Velocity in m/s
"""
=
return
"""
Convert angle to radians (SI).
Parameters
----------
value : float or Quantity
Angle value (radians if raw, or with units like degrees)
name : str
Parameter name for error messages
Returns
-------
float
Angle in radians
"""
=
return
"""
Convert time to seconds (SI).
Parameters
----------
value : float or Quantity
Time value (seconds if raw, or with units)
name : str
Parameter name for error messages
Returns
-------
float
Time in seconds
"""
=
return
"""
Convert dimensionless quantity to float.
Parameters
----------
value : float or Quantity
Dimensionless value (e.g., eccentricity)
name : str
Parameter name for error messages
Returns
-------
float
Dimensionless value as float
"""
# Check that it's dimensionless
return
return
"""
Convert 3D vector (position or velocity) to SI units.
Parameters
----------
vector : array-like or Quantity
3-element vector with or without units
target_unit : astropy.units.Unit
Target SI unit (u.m or u.m/u.s)
name : str
Parameter name for error messages
Returns
-------
np.ndarray
3-element array in SI units (meters or m/s)
Raises
------
ValueError
If vector is not 3-dimensional or has incompatible units
Examples
--------
>>> from astropy import units as u
>>>
>>> # Raw array
>>> to_si_vector([7000e3, 0, 0], u.m)
array([7000000., 0., 0.])
>>>
>>> # With units
>>> r = [7000, 0, 0] << u.km
>>> to_si_vector(r, u.m, "position")
array([7000000., 0., 0.])
"""
# Convert to SI
=
# Ensure it's a numpy array
=
# Validate shape
return
# Convenience functions for common conversions
"""Convert position vector to meters."""
return
"""Convert velocity vector to m/s."""
return
# Functions for returning Quantity objects from properties
"""
Convert scalar length from SI (meters) to Quantity with preferred unit.
Parameters
----------
value : float
Length in meters (SI)
preferred_unit : astropy.units.Unit
Preferred display unit (default: km)
Returns
-------
Quantity
Length as Quantity with preferred unit
"""
return
"""
Convert scalar velocity from SI (m/s) to Quantity with preferred unit.
Parameters
----------
value : float
Velocity in m/s (SI)
preferred_unit : astropy.units.Unit
Preferred display unit (default: km/s)
Returns
-------
Quantity
Velocity as Quantity with preferred unit
"""
return
"""
Convert scalar angle from SI (radians) to Quantity with preferred unit.
Parameters
----------
value : float
Angle in radians (SI)
preferred_unit : astropy.units.Unit
Preferred display unit (default: radians)
Returns
-------
Quantity
Angle as Quantity with preferred unit
"""
return
"""
Convert scalar time from SI (seconds) to Quantity with preferred unit.
Parameters
----------
value : float
Time in seconds (SI)
preferred_unit : astropy.units.Unit
Preferred display unit (default: seconds)
Returns
-------
Quantity
Time as Quantity with preferred unit
"""
return
"""
Convert vector from SI units to Quantity with appropriate unit.
Parameters
----------
vector : np.ndarray
3-element vector in SI units
unit_type : str
Type of vector: 'position' (km) or 'velocity' (km/s)
Returns
-------
Quantity
Vector as Quantity with appropriate units
"""
return *
return * /