siderust 0.9.1

High-precision astronomy and satellite mechanics in Rust.
Documentation
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2026 Vallés Puig, Ramon

//! Minimal DAF (Double Precision Array File) parser for SPICE BSP files.
//!
//! Reference: NAIF DAF Required Reading
//! <https://naif.jpl.nasa.gov/pub/naif/toolkit_docs/C/req/daf.html>
//!
//! This is a runtime-capable version of the parser, it returns `Result`
//! errors instead of panicking and can be used at runtime (via the
//! `runtime-data` feature). Build-time JPL extraction lives in
//! `siderust-archive/generators/jpl/`.

use super::SpiceError;

/// Parsed DAF container.
#[derive(Debug)]
pub struct Daf {
    /// Number of double components per summary.
    pub nd: usize,
    /// Number of integer components per summary.
    pub ni: usize,
    /// All segment summaries found in the file.
    pub summaries: Vec<Summary>,
}

/// A single segment summary extracted from the DAF.
#[derive(Debug)]
pub struct Summary {
    /// Start epoch (TDB seconds past J2000).
    pub start_et: f64,
    /// End epoch (TDB seconds past J2000).
    pub end_et: f64,
    /// NAIF body ID of the target.
    pub target_id: i32,
    /// NAIF body ID of the center.
    pub center_id: i32,
    /// Reference frame ID (typically 1 = J2000).
    pub frame_id: i32,
    /// SPK segment type (2 = Chebyshev position, 3 = Chebyshev pos+vel).
    pub data_type: i32,
    /// 1-based word index of first data element in the file.
    pub start_word: usize,
    /// 1-based word index of last data element in the file.
    pub end_word: usize,
}

/// Raw summary entry for a generic DAF kernel.
///
/// The `doubles` slice contains `nd` values; their interpretation depends on
/// the kernel type. The `integers` slice contains `ni` values; the final two
/// integers are always the 1-based start and end word addresses.
#[derive(Debug, Clone)]
pub struct RawSummary {
    /// The `nd` double-precision values from this summary.
    pub doubles: Vec<f64>,
    /// The `ni` integer values from this summary.
    pub integers: Vec<i32>,
}

impl RawSummary {
    /// First 1-based data word.
    pub fn start_word(&self) -> usize {
        self.integers[self.integers.len() - 2] as usize
    }

    /// Last 1-based data word.
    pub fn end_word(&self) -> usize {
        self.integers[self.integers.len() - 1] as usize
    }
}

/// A DAF container parsed without kernel-type constraints.
///
/// ## Scientific scope
///
/// n/a — this type is a wire-format container only.
///
/// ## Technical scope
///
/// Parses the DAF binary container structure without assuming SPK-specific
/// summary layouts. Use this for CK, binary PCK, DSK, and other DAF-based
/// kernel types.
///
/// ## References
///
/// - NAIF DAF Required Reading
#[derive(Debug)]
pub struct DafRaw {
    /// Number of double-precision components per summary.
    pub nd: usize,
    /// Number of integer components per summary.
    pub ni: usize,
    /// Eight-character file locator.
    pub locator: String,
    /// All raw summaries parsed from the kernel.
    pub raw_summaries: Vec<RawSummary>,
    bytes: Vec<u8>,
}

impl Daf {
    /// Parse a DAF container from raw file bytes.
    pub fn parse(data: &[u8]) -> Result<Self, SpiceError> {
        if data.len() < 1024 {
            return Err(SpiceError::FormatParse(format!(
                "DAF file too small ({} bytes)",
                data.len()
            )));
        }

        let locid = std::str::from_utf8(&data[0..8]).unwrap_or("").trim();
        if !locid.starts_with("DAF") {
            return Err(SpiceError::FormatParse(format!(
                "Not a DAF file (locator = {:?})",
                locid
            )));
        }

        let nd_le = read_i32_le(data, 8);
        let ni_le = read_i32_le(data, 12);
        let nd_be = read_i32_be(data, 8);
        let ni_be = read_i32_be(data, 12);

        let (le, nd, ni) = if nd_le > 0 && nd_le <= 100 && ni_le > 0 && ni_le <= 100 {
            (true, nd_le as usize, ni_le as usize)
        } else if nd_be > 0 && nd_be <= 100 && ni_be > 0 && ni_be <= 100 {
            (false, nd_be as usize, ni_be as usize)
        } else {
            return Err(SpiceError::FormatParse(format!(
                "Cannot determine DAF endianness: ND/NI LE=({},{}), BE=({},{})",
                nd_le, ni_le, nd_be, ni_be
            )));
        };

        let read_i32 = if le { read_i32_le } else { read_i32_be };
        let read_f64 = if le { read_f64_le } else { read_f64_be };

        let fward = read_i32(data, 76) as usize;

        if nd != 2 || ni != 6 {
            return Err(SpiceError::FormatParse(format!(
                "Expected SPK format (ND=2, NI=6), got ND={}, NI={}",
                nd, ni
            )));
        }

        let ss = nd + ni.div_ceil(2);

        let mut summaries = Vec::new();
        let mut rec = fward;

        while rec > 0 {
            let rec_offset = (rec - 1) * 1024;
            if rec_offset + 1024 > data.len() {
                return Err(SpiceError::FormatParse(format!(
                    "Summary record {} extends past EOF",
                    rec
                )));
            }

            let next = read_f64(data, rec_offset) as i64 as usize;
            let _prev = read_f64(data, rec_offset + 8);
            let nsum = read_f64(data, rec_offset + 16) as usize;

            for i in 0..nsum {
                let off = rec_offset + 24 + i * ss * 8;
                if off + ss * 8 > data.len() {
                    return Err(SpiceError::FormatParse(format!(
                        "Summary {} in record {} extends past EOF",
                        i, rec
                    )));
                }

                let start_et = read_f64(data, off);
                let end_et = read_f64(data, off + 8);

                let int_off = off + nd * 8;
                let target_id = read_i32(data, int_off);
                let center_id = read_i32(data, int_off + 4);
                let frame_id = read_i32(data, int_off + 8);
                let data_type = read_i32(data, int_off + 12);
                let start_word = read_i32(data, int_off + 16) as usize;
                let end_word = read_i32(data, int_off + 20) as usize;

                summaries.push(Summary {
                    start_et,
                    end_et,
                    target_id,
                    center_id,
                    frame_id,
                    data_type,
                    start_word,
                    end_word,
                });
            }

            rec = next;
        }

        Ok(Daf { nd, ni, summaries })
    }

    /// Read a f64 from the data array at a given word index (1-based).
    #[inline]
    pub fn read_f64_at_word(&self, data: &[u8], word: usize) -> f64 {
        let offset = (word - 1) * 8;
        // DE440/DE441 kernels from NAIF are little-endian on modern systems.
        read_f64_le(data, offset)
    }
}

impl DafRaw {
    /// Parse any DAF kernel from raw bytes without restricting ND/NI.
    pub fn parse(data: Vec<u8>) -> Result<Self, SpiceError> {
        if data.len() < 1024 {
            return Err(SpiceError::FormatParse(format!(
                "DAF file too small ({} bytes)",
                data.len()
            )));
        }

        let locator = String::from_utf8_lossy(&data[0..8]).to_string();
        if !locator.trim().starts_with("DAF") {
            return Err(SpiceError::FormatParse(format!(
                "Not a DAF file (locator = {:?})",
                locator.trim()
            )));
        }

        let nd_le = read_i32_le(&data, 8);
        let ni_le = read_i32_le(&data, 12);
        let nd_be = read_i32_be(&data, 8);
        let ni_be = read_i32_be(&data, 12);

        let (le, nd, ni) = if nd_le > 0 && nd_le <= 100 && ni_le > 0 && ni_le <= 100 {
            (true, nd_le as usize, ni_le as usize)
        } else if nd_be > 0 && nd_be <= 100 && ni_be > 0 && ni_be <= 100 {
            (false, nd_be as usize, ni_be as usize)
        } else {
            return Err(SpiceError::FormatParse(format!(
                "Cannot determine DAF endianness: ND/NI LE=({},{}), BE=({},{})",
                nd_le, ni_le, nd_be, ni_be
            )));
        };

        let read_i32 = if le { read_i32_le } else { read_i32_be };
        let read_f64 = if le { read_f64_le } else { read_f64_be };
        let fward = read_i32(&data, 76) as usize;
        let ss = nd + ni.div_ceil(2);

        let mut raw_summaries = Vec::new();
        let mut rec = fward;
        while rec > 0 {
            let rec_offset = (rec - 1) * 1024;
            if rec_offset + 1024 > data.len() {
                return Err(SpiceError::FormatParse(format!(
                    "Summary record {} extends past EOF",
                    rec
                )));
            }

            let next = read_f64(&data, rec_offset) as i64 as usize;
            let nsum = read_f64(&data, rec_offset + 16) as usize;
            for i in 0..nsum {
                let off = rec_offset + 24 + i * ss * 8;
                if off + ss * 8 > data.len() {
                    return Err(SpiceError::FormatParse(format!(
                        "Summary {} in record {} extends past EOF",
                        i, rec
                    )));
                }

                let mut doubles = Vec::with_capacity(nd);
                for j in 0..nd {
                    doubles.push(read_f64(&data, off + j * 8));
                }
                let int_off = off + nd * 8;
                let mut integers = Vec::with_capacity(ni);
                for j in 0..ni {
                    integers.push(read_i32(&data, int_off + j * 4));
                }
                raw_summaries.push(RawSummary { doubles, integers });
            }
            rec = next;
        }

        Ok(Self {
            nd,
            ni,
            locator,
            raw_summaries,
            bytes: data,
        })
    }

    /// Read an `f64` at a 1-based word index (little-endian).
    #[inline]
    pub fn read_f64_at_word(&self, word: usize) -> f64 {
        let offset = (word - 1) * 8;
        read_f64_le(&self.bytes, offset)
    }

    /// Borrow the raw kernel bytes.
    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }
}

#[inline]
fn read_i32_le(data: &[u8], offset: usize) -> i32 {
    i32::from_le_bytes(data[offset..offset + 4].try_into().unwrap())
}

#[inline]
fn read_i32_be(data: &[u8], offset: usize) -> i32 {
    i32::from_be_bytes(data[offset..offset + 4].try_into().unwrap())
}

#[inline]
fn read_f64_le(data: &[u8], offset: usize) -> f64 {
    f64::from_le_bytes(data[offset..offset + 8].try_into().unwrap())
}

#[inline]
fn read_f64_be(data: &[u8], offset: usize) -> f64 {
    f64::from_be_bytes(data[offset..offset + 8].try_into().unwrap())
}

#[cfg(test)]
mod tests {
    use super::*;

    // ── Helpers to build synthetic DAF byte arrays ─────────────────────────

    /// Write a little-endian i32 into a buffer at a given offset.
    fn write_i32_le(buf: &mut [u8], offset: usize, val: i32) {
        buf[offset..offset + 4].copy_from_slice(&val.to_le_bytes());
    }

    /// Write a little-endian f64 into a buffer at a given offset.
    fn write_f64_le(buf: &mut [u8], offset: usize, val: f64) {
        buf[offset..offset + 8].copy_from_slice(&val.to_le_bytes());
    }

    /// Create a minimal valid DAF header (1024 bytes) with FWARD = 0 (no summaries).
    fn minimal_valid_daf() -> Vec<u8> {
        let mut buf = vec![0u8; 1024];
        buf[0..8].copy_from_slice(b"DAF/SPK ");
        write_i32_le(&mut buf, 8, 2); // ND = 2
        write_i32_le(&mut buf, 12, 6); // NI = 6
        write_i32_le(&mut buf, 76, 0); // FWARD = 0 (no summary records)
        buf
    }

    /// Create a DAF with one summary record containing a single Type-2 SPK summary.
    #[allow(clippy::too_many_arguments)]
    fn daf_with_one_summary(
        start_et: f64,
        end_et: f64,
        target_id: i32,
        center_id: i32,
        frame_id: i32,
        data_type: i32,
        start_word: i32,
        end_word: i32,
    ) -> Vec<u8> {
        let mut buf = vec![0u8; 2048];
        // Header record (bytes 0..1024)
        buf[0..8].copy_from_slice(b"DAF/SPK ");
        write_i32_le(&mut buf, 8, 2); // ND = 2
        write_i32_le(&mut buf, 12, 6); // NI = 6
        write_i32_le(&mut buf, 76, 2); // FWARD = 2 (second record)
                                       // Summary record (bytes 1024..2048)
        let rec = &mut buf[1024..];
        write_f64_le(rec, 0, 0.0); // next = 0 (no next record)
        write_f64_le(rec, 8, 0.0); // prev
        write_f64_le(rec, 16, 1.0); // nsum = 1
                                    // Summary at offset 24:  nd=2 doubles + ni=6 ints = 40 bytes
        write_f64_le(rec, 24, start_et);
        write_f64_le(rec, 32, end_et);
        write_i32_le(rec, 40, target_id);
        write_i32_le(rec, 44, center_id);
        write_i32_le(rec, 48, frame_id);
        write_i32_le(rec, 52, data_type);
        write_i32_le(rec, 56, start_word);
        write_i32_le(rec, 60, end_word);
        buf
    }

    // ── Error path tests ───────────────────────────────────────────────────

    #[test]
    fn parse_too_small_returns_error() {
        let data = vec![0u8; 512]; // less than 1024
        let result = Daf::parse(&data);
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(msg.contains("too small") || msg.contains("DAF"));
    }

    #[test]
    fn parse_wrong_magic_returns_error() {
        let mut buf = vec![0u8; 1024];
        buf[0..8].copy_from_slice(b"NOTADAF!");
        let result = Daf::parse(&buf);
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(msg.contains("Not a DAF"));
    }

    #[test]
    fn parse_bad_nd_ni_returns_error() {
        let mut buf = vec![0u8; 1024];
        buf[0..8].copy_from_slice(b"DAF/SPK ");
        // Set both LE and BE to nonsense values outside valid range
        write_i32_le(&mut buf, 8, 0); // ND LE = 0 (invalid)
        write_i32_le(&mut buf, 12, 0); // NI LE = 0 (invalid)
                                       // BE also invalid (zeros in both positions)
        let result = Daf::parse(&buf);
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(
            msg.contains("endianness")
                || msg.contains("ND")
                || msg.contains("NI")
                || msg.contains("parse error")
        );
    }

    #[test]
    fn parse_non_spk_format_returns_error() {
        // ND=3, NI=4, valid values but not SPK (ND=2, NI=6)
        let mut buf = vec![0u8; 1024];
        buf[0..8].copy_from_slice(b"DAF/CK  ");
        write_i32_le(&mut buf, 8, 3); // ND = 3
        write_i32_le(&mut buf, 12, 4); // NI = 4
        write_i32_le(&mut buf, 76, 0); // FWARD = 0
        let result = Daf::parse(&buf);
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(
            msg.contains("ND")
                || msg.contains("NI")
                || msg.contains("SPK")
                || msg.contains("parse error")
        );
    }

    #[test]
    fn parse_raw_daf_accepts_non_spk_locator() {
        let mut buf = vec![0u8; 1024];
        buf[0..8].copy_from_slice(b"DAF/CK  ");
        write_i32_le(&mut buf, 8, 2);
        write_i32_le(&mut buf, 12, 6);
        write_i32_le(&mut buf, 76, 0);
        let daf = DafRaw::parse(buf).unwrap();
        assert_eq!(daf.locator, "DAF/CK  ");
        assert!(daf.raw_summaries.is_empty());
    }

    #[test]
    fn parse_fward_beyond_eof_returns_error() {
        let mut buf = minimal_valid_daf();
        // Set FWARD to record 99, but file only has 1 record (1024 bytes)
        write_i32_le(&mut buf, 76, 99);
        let result = Daf::parse(&buf);
        assert!(result.is_err());
        let msg = format!("{}", result.unwrap_err());
        assert!(msg.contains("EOF") || msg.contains("extends past") || msg.contains("parse error"));
    }

    // ── Success path tests ─────────────────────────────────────────────────

    #[test]
    fn parse_minimal_valid_daf_empty_summaries() {
        let buf = minimal_valid_daf();
        let daf = Daf::parse(&buf).expect("should parse valid DAF");
        assert_eq!(daf.nd, 2);
        assert_eq!(daf.ni, 6);
        assert!(daf.summaries.is_empty());
    }

    #[test]
    fn parse_daf_with_one_summary() {
        let buf = daf_with_one_summary(
            0.0,     // start_et
            86400.0, // end_et
            10,      // target_id (Sun)
            0,       // center_id (SSB)
            1,       // frame_id (J2000)
            2,       // data_type (Type 2 Chebyshev)
            257,     // start_word
            300,     // end_word
        );
        let daf = Daf::parse(&buf).expect("should parse DAF with one summary");
        assert_eq!(daf.nd, 2);
        assert_eq!(daf.ni, 6);
        assert_eq!(daf.summaries.len(), 1);

        let s = &daf.summaries[0];
        assert!((s.start_et - 0.0).abs() < 1e-10);
        assert!((s.end_et - 86400.0).abs() < 1e-10);
        assert_eq!(s.target_id, 10);
        assert_eq!(s.center_id, 0);
        assert_eq!(s.frame_id, 1);
        assert_eq!(s.data_type, 2);
        assert_eq!(s.start_word, 257);
        assert_eq!(s.end_word, 300);
    }

    #[test]
    fn read_f64_at_word_reads_correct_value() {
        let daf = Daf {
            nd: 2,
            ni: 6,
            summaries: vec![],
        };
        // Create a buffer with a known f64 at word 1 (offset 0)
        let mut buf = vec![0u8; 64];
        let val = 42.0_f64;
        buf[0..8].copy_from_slice(&val.to_le_bytes());
        let result = daf.read_f64_at_word(&buf, 1);
        assert!((result - 42.0).abs() < 1e-15);
    }

    #[test]
    fn read_f64_at_word_second_word() {
        let daf = Daf {
            nd: 2,
            ni: 6,
            summaries: vec![],
        };
        let mut buf = vec![0u8; 64];
        let val = 99.5_f64;
        buf[8..16].copy_from_slice(&val.to_le_bytes());
        let result = daf.read_f64_at_word(&buf, 2);
        assert!((result - 99.5).abs() < 1e-15);
    }

    #[test]
    fn parse_summary_truncated_returns_error() {
        // Build a DAF that claims 5 summaries but only provides 1's worth of space
        let mut buf = vec![0u8; 2048];
        buf[0..8].copy_from_slice(b"DAF/SPK ");
        write_i32_le(&mut buf, 8, 2); // ND = 2
        write_i32_le(&mut buf, 12, 6); // NI = 6
        write_i32_le(&mut buf, 76, 2); // FWARD = 2
        let rec = &mut buf[1024..];
        write_f64_le(rec, 0, 0.0);
        write_f64_le(rec, 8, 0.0);
        write_f64_le(rec, 16, 100.0); // nsum = 100, far more than fits in 1024 bytes
        let result = Daf::parse(&buf);
        // Should either succeed with partial summaries or return an error, must not panic
        let _ = result;
    }

    #[test]
    fn raw_summary_word_helpers_return_trailing_addresses() {
        let summary = RawSummary {
            doubles: vec![0.0, 1.0],
            integers: vec![1, 2, 3, 4, 257, 300],
        };
        assert_eq!(summary.start_word(), 257);
        assert_eq!(summary.end_word(), 300);
    }
}