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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! # Single-Observer Astrometric Batch Ingestion
//!
//! This module provides the [`ObservationBatch`] type, which groups multiple
//! astrometric detections from a **single observer** into a compact container.
//! Such a batch can then be expanded into concrete [`Observation`]s and stored
//! in a [`TrajectorySet`].
//!
//! ## Overview
//! -----------------
//! A wide-field survey typically delivers angle-only astrometry (RA/DEC, with
//! per-epoch timestamps). [`ObservationBatch`] wraps such measurements, together
//! with uniform error estimates, into a structured form ready for ingestion into
//! orbit-determination pipelines.
//!
//! To actually turn batches into stored observations, use the trait
//! [`TrajectoryFile`](crate::trajectories::trajectory_file::TrajectoryFile):
//! - [`TrajectoryFile::new_from_vec`](crate::trajectories::trajectory_file::TrajectoryFile::new_from_vec) — build a new [`TrajectorySet`] from a batch.
//! - [`TrajectoryFile::add_from_vec`](crate::trajectories::trajectory_file::TrajectoryFile::add_from_vec) — append a batch into an existing [`TrajectorySet`].
//!
//! Both methods transparently handle the internal expansion of a batch into
//! per-sample [`Observation`]s (site position lookups, heliocentric positions,
//! RA/DEC/error propagation, etc.).
//!
//! ## Units & Conventions
//! -----------------
//! - **Angles:** Right ascension and declination in **radians**.
//! If your upstream data are in **degrees/arcseconds**, use
//! [`ObservationBatch::from_degrees_owned`] to convert once at construction.
//! - **Uncertainties:** 1-σ errors in RA/DEC (radians). For arcsecond inputs,
//! the degree-based constructor performs the conversion for you.
//! - **Epochs:** Times in **MJD (TT)** (days). Convert UTC/TAI upstream.
//! - **Observer:** All rows in a batch must come from the **same** observer.
//!
//! ## Invariants
//! -----------------
//! - `trajectory_id.len() == ra.len() == dec.len() == time.len()`
//! - All angles and uncertainties are in **radians**.
//! - All epochs are in **MJD (TT)**.
//! - Batch content belongs to a **single observer**.
//!
//! ## Construction Paths
//! -----------------
//! - [`ObservationBatch::from_radians_borrowed`] — zero-copy when your pipeline already
//! provides radians and MJD (TT).
//! - [`ObservationBatch::from_degrees_owned`] — converts degrees/arcseconds → radians
//! once and stores owned buffers.
//!
//! ## Example
//! -----------------
//! ```rust,no_run
//! use std::sync::Arc;
//! use outfit::{
//! Outfit, Observer, TrajectorySet, TrajectoryFile, ErrorModel,
//! trajectories::batch_reader::ObservationBatch,
//! };
//!
//! // Inputs in degrees / arcseconds (mixed objects: 0 and 1).
//! let traj_id = vec![0_u32, 0, 1];
//! let ra_deg = vec![210.01, 210.02, 211.00];
//! let dec_deg = vec![-5.00, -4.99, -4.00];
//! let mjd_tt = vec![60345.12, 60345.13, 60345.20];
//!
//! let batch = ObservationBatch::from_degrees_owned(
//! &traj_id, &ra_deg, &dec_deg, 0.5, 0.5, &mjd_tt
//! );
//!
//! // Global environment and observer.
//! let mut outfit = Outfit::new("horizon:DE440", ErrorModel::FCCT14).unwrap();
//! let observer = outfit.get_observer_from_mpc_code(&"I41".to_string());
//!
//! // Build a new TrajectorySet directly from the batch.
//! let traj_set = TrajectorySet::new_from_vec(&mut outfit, &batch, observer.clone())
//! .expect("ingestion OK");
//!
//! // Or append to an existing set:
//! let mut other = TrajectorySet::default();
//! other.add_from_vec(&mut outfit, &batch, observer).expect("append OK");
//! ```
//!
//! ## Notes
//! -----------------
//! - Internally, ingestion is performed by a crate-private routine
//! (`observation_from_batch`) which expands the batch into per-sample
//! [`Observation`]s and caches observer positions per epoch. Users should rely
//! on the public `TrajectoryFile` methods instead.
//! - For multi-observer datasets, create one [`ObservationBatch`] per observer,
//! then ingest them separately.
//!
//! ## See also
//! ------------
//! * [`ObservationBatch::from_radians_borrowed`] – Zero-copy construction.
//! * [`ObservationBatch::from_degrees_owned`] – Convert degrees/arcseconds once.
//! * [`TrajectoryFile::new_from_vec`](crate::trajectories::trajectory_file::TrajectoryFile::new_from_vec) – Public entry point for batch ingestion.
//! * [`TrajectoryFile::add_from_vec`](crate::trajectories::trajectory_file::TrajectoryFile::add_from_vec) – Append batch into an existing set.
use ;
use RandomState;
use Epoch;
use Vector3;
use OrderedFloat;
use SmallVec;
use crate::;
/// Batch of observations from a single observer (angles in **radians**).
///
/// This container groups multiple astrometric measurements sharing the same
/// observer into a single batch, ready to be expanded into
/// [`Observation`]s and stored in a
/// [`TrajectorySet`].
///
/// Each measurement includes:
/// - A trajectory identifier (`trajectory_id`) so that a single batch can hold
/// observations for multiple objects simultaneously.
/// - Right ascension and declination in **radians**, with uniform 1-σ uncertainties
/// (also in **radians**).
/// - Epochs in **MJD (TT)** (days).
///
/// Fields
/// -----------------
/// * `trajectory_id` — Integer trajectory IDs (object numbers). Length must match `ra`/`dec`/`time`.
/// * `ra` — Right ascension values (**radians**). Length must match `dec`, `time`, and `trajectory_id`.
/// * `error_ra` — 1-σ uncertainty on right ascension (**radians**) applied uniformly to the batch.
/// * `dec` — Declination values (**radians**). Length must match `ra`, `time`, and `trajectory_id`.
/// * `error_dec` — 1-σ uncertainty on declination (**radians**) applied uniformly to the batch.
/// * `time` — Observation epochs as **MJD (TT)** (days). Length must match `ra`/`dec`/`trajectory_id`.
///
/// Invariants
/// -----------------
/// * `trajectory_id.len() == ra.len() == dec.len() == time.len()`
/// * Angles and uncertainties are expressed in **radians**.
/// * Time scale is **TT** (use appropriate conversion if your source data are in UTC/TAI).
///
/// Construction
/// -----------------
/// Prefer the dedicated constructors:
/// * [`ObservationBatch::from_radians_borrowed`] — zero-copy when your inputs are already in radians.
/// * [`ObservationBatch::from_degrees_owned`] — converts degrees/arcseconds once into owned buffers.
///
/// Example
/// -----------------
/// ```rust, no_run
/// # use outfit::trajectories::batch_reader::ObservationBatch;
/// # let (traj_id, ra_deg, dec_deg, mjd) = (vec![0, 0, 1], vec![14.62, 14.63, 15.01], vec![9.98, 10.01, 11.02], vec![43785.35, 43785.36, 43785.40]);
/// // Inputs in degrees / arcseconds (converted once to radians internally):
/// let batch = ObservationBatch::from_degrees_owned(&traj_id, &ra_deg, &dec_deg, 0.5, 0.5, &mjd);
///
/// // Or, if you already have radians:
/// // let batch = ObservationBatch::from_radians_borrowed(&ra_rad, &dec_rad, err_ra_rad, err_dec_rad, &mjd);
/// ```
///
/// See also
/// ------------
/// * [`ObservationBatch::from_radians_borrowed`] – Borrow slices already in radians (zero-copy).
/// * [`ObservationBatch::from_degrees_owned`] – Convert degrees/arcseconds → radians once.
/// * [`conversion::arcsec_to_rad`](crate::conversion::arcsec_to_rad) – Arcseconds → radians helper.
/// Expand a single-observer batch into concrete [`Observation`]s and append them into a [`TrajectorySet`].
///
/// This routine ingests an [`ObservationBatch`] whose angles and uncertainties are in **radians**
/// and whose epochs are **MJD (TT)**, then materializes per-sample [`Observation`]s enriched with
/// site geocentric and heliocentric positions. All measurements are assumed to come from the **same
/// observer** (hence a single `observer: Arc<Observer>` argument).
///
/// For performance, it:
/// - resolves the observer into a compact `u16` **once** (hot path),
/// - pre-fetches the UT1 provider **once**,
/// - caches observer positions by epoch: **MJD(TT) → (geo_pos, helio_pos)**,
/// so repeated timestamps incur **no extra** position computation.
///
/// Internally, epoch keys are wrapped in `OrderedFloat` to enable their use in a hash map
/// (total order on `f64` while rejecting `NaN` inputs by construction).
///
/// Arguments
/// -----------------
/// * `trajectories` — Target container to receive observations, bucketed by `trajectory_id`.
/// * `env_state` — Global [`Outfit`] state (ephemerides, EOP/UT1 providers, etc.).
/// * `batch` — Angles and 1-σ uncertainties in **radians**; epochs as **MJD (TT)**; includes `trajectory_id`.
/// * `observer` — The (single) observer for **all** samples in `batch`.
///
/// Return
/// ----------
/// * `Ok(())` if all observations were successfully appended into `trajectories`,
/// * `Err(OutfitError)` if site position or heliocentric position computations fail.
///
/// Panics
/// ----------
/// * **Debug builds only**: length mismatches across `ra/dec/time/trajectory_id` trigger `debug_assert!`.
///
/// Complexity
/// ----------
/// * Time: **O(n)**, with at most **O(u)** geocentric/heliocentric computations where `u` is the number of
/// **unique** epochs in the batch (`u ≤ n`) thanks to the epoch→position cache.
/// * Space: **O(u)** for the epoch→position cache.
///
/// Notes
/// ----------
/// * Input angles (RA/DEC) and uncertainties **must already be in radians**. If your source is degrees/arcsec,
/// build the batch via [`ObservationBatch::from_degrees_owned`] (conversion done once at construction).
/// * Epochs are expected as **TT**. Convert upstream if your pipeline feeds UTC/TAI.
/// * This function mutates `trajectories` and reads from `env_state`. If you need parallelization, consider
/// extracting immutable position providers beforehand, or designing providers that accept shared references.
///
/// See also
/// ------------
/// * [`ObservationBatch::from_radians_borrowed`] – Zero-copy batch when inputs are already radians.
/// * [`ObservationBatch::from_degrees_owned`] – Degree/arcsec → rad conversion once at construction.
/// * [`parquet_to_trajset`] – Parquet ingestion using the same unit/weighting logic.
/// * [`conversion::arcsec_to_rad`] – Arcseconds → radians helper.
pub