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
//! Planetary ephemeris providers for high-precision astrometry.
//!
//! Two built-in backends are available as optional features:
//!
//! - `calceph` — wraps the C CALCEPH library; fast, mature, requires a
//! system `libcalceph`. Exposes [`CalcephEphemeris`].
//! - `anise` — pure-Rust ANISE/SPK reader ([nyx-space/anise]); no C
//! dependency beyond SuperNOVAS itself. Exposes [`AniseEphemeris`].
//!
//! Both features may be enabled simultaneously; each backend is an
//! independent type implementing [`EphemerisProvider`].
//!
//! When exactly one feature is enabled, [`Ephemeris::open`] is available as
//! a convenience that delegates to that backend. When both are enabled,
//! construct via [`Ephemeris::from_provider`] with the backend of your
//! choice.
//!
//! ## Custom backends
//!
//! Implement [`PlanetProvider`] on your data type to add a custom ephemeris
//! source without writing any `unsafe` code. The framework handles all C
//! callback registration and panic catching for you; a blanket impl
//! automatically gives your type [`EphemerisProvider`].
//!
//! ## Backend agreement
//!
//! CALCEPH and ANISE both evaluate the same Chebyshev polynomials from the
//! SPK binary, but as independent C and Rust implementations they accumulate
//! floating-point rounding differently. For a typical stellar pointing
//! (de440s.bsp, ~50–100 polynomial terms per body), the two backends agree to
//! within **~2 µas** in azimuth and **~0.05 µas** in elevation — well inside
//! SuperNOVAS's sub-µas full-accuracy guarantee and negligible for mm-wave
//! pointing. The residual divergence is irreducible rounding noise, not a bug.
//!
//! ```no_run
//! use supernovas::Ephemeris;
//!
//! Ephemeris::open("/path/to/de440s.bsp")
//! .expect("ephemeris file present")
//! .install()
//! .expect("install succeeded");
//! ```
// Path is only needed for the Ephemeris::open conveniences, available when
// exactly one backend feature is enabled.
use Path;
use ;
pub use AniseEphemeris;
pub use CalcephEphemeris;
use supernovas_ffi as sys;
use crate;
// ── PlanetProvider ────────────────────────────────────────────────────────────
/// The interface SuperNOVAS requires from a planetary ephemeris backend.
///
/// Implement this on your data type to bridge any ephemeris source to
/// SuperNOVAS without writing `unsafe` C callbacks or managing process-global
/// state yourself. The framework handles all of that, and a blanket impl
/// automatically gives your type [`EphemerisProvider`].
///
/// # Units and frame
///
/// - **Position**: AU in the BCRS J2000.0 (ICRF) frame.
/// - **Velocity**: AU/day in the same frame.
///
/// # Arguments
///
/// - `body`: which solar-system body SuperNOVAS needs.
/// Map to a NAIF integer ID with
/// `unsafe { supernovas::sys::novas_to_naif_planet(body) }`.
/// - `origin`: [`sys::novas_origin::NOVAS_BARYCENTER`] (Solar System
/// Barycenter, NAIF 0) or [`sys::novas_origin::NOVAS_HELIOCENTER`]
/// (Sun, NAIF 10).
/// - `jd_high` + `jd_low`: split TDB Julian date. Adding them as `f64` is
/// fine for most purposes; use both parts for sub-µs precision.
///
/// # Return value
///
/// Return `None` if `body` is not covered by your ephemeris — SuperNOVAS will
/// surface this as an ephemeris error. Return `Some(([x, y, z], [vx, vy, vz]))`.
///
/// # Panics
///
/// Panics inside `state` are caught at the C boundary and converted to a
/// non-zero error code so SuperNOVAS can surface them cleanly. Nevertheless,
/// prefer returning `None` over panicking for unsupported bodies.
///
/// # Example
///
/// ```no_run
/// use supernovas::{sys, Ephemeris, PlanetProvider};
///
/// struct LinearEphemeris;
///
/// impl PlanetProvider for LinearEphemeris {
/// fn state(
/// &self,
/// body: sys::novas_planet,
/// origin: sys::novas_origin,
/// jd_high: f64,
/// jd_low: f64,
/// ) -> Option<([f64; 3], [f64; 3])> {
/// let _naif = unsafe { sys::novas_to_naif_planet(body) };
/// // look up position and velocity from your ephemeris source...
/// Some(([1.0, 0.0, 0.0], [0.0, 0.017, 0.0]))
/// }
/// }
///
/// Ephemeris::from_provider(LinearEphemeris).install().unwrap();
/// ```
// ── Generic dispatch (used by the PlanetProvider blanket impl) ────────────────
static GENERIC_PROVIDER: = new;
unsafe extern "C"
unsafe extern "C"
/// Any [`PlanetProvider`] is automatically an [`EphemerisProvider`].
///
/// The blanket impl stores the provider in a process-global [`OnceLock`] and
/// registers the C dispatch callbacks with SuperNOVAS. At most one
/// `PlanetProvider` can be installed per process via this path; a second call
/// returns [`Error::Ephemeris`].
///
/// Built-in backends ([`CalcephEphemeris`], [`AniseEphemeris`]) implement
/// [`EphemerisProvider`] directly and do not go through this dispatch layer.
// ── EphemerisProvider ─────────────────────────────────────────────────────────
/// A planetary ephemeris backend that can be installed as the process-global
/// SuperNOVAS planet provider.
///
/// Most users should implement [`PlanetProvider`] instead — it exposes the
/// natural Rust interface (return a state vector) and the blanket impl here
/// handles all C callback wiring automatically.
///
/// Implement `EphemerisProvider` directly only when you need a non-standard
/// registration path, as CALCEPH does via `novas_use_calceph`.
// ── Ephemeris wrapper ─────────────────────────────────────────────────────────
/// A loaded planetary ephemeris, ready to install as the process-global
/// SuperNOVAS planet provider.
///
/// Construct via [`Ephemeris::open`] (when exactly one backend feature is
/// enabled) or [`Ephemeris::from_provider`] (with any [`EphemerisProvider`],
/// including the named backend types when both features are active).