stkcraft-time 0.1.13

NAIF/SPICE-aligned astronomical time scales in Rust: UTC, TAI, TT, TDB/ET, GPS, UT1 and SCLK, with LSK/SCLK/EOP kernels, no_std support, WebGeocalc parity and an MCP server.
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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//! Spacecraft clock (SCLK) kernels, type 1.
//!
//! A type-1 clock is a set of cascading integer counters. The SCLK kernel
//! stores, for one spacecraft:
//!
//! * `SCLK01_N_FIELDS_<id>` — number of fields,
//! * `SCLK01_MODULI_<id>` — modulus of each field,
//! * `SCLK01_OFFSETS_<id>` — offset added to each field value,
//! * `SCLK01_TIME_SYSTEM_<id>` — `1` for TDB/ET, `2` for TT (default 1),
//! * `SCLK01_COEFFICIENTS_<id>` — triplets `(ticks, parallel_seconds,
//!   rate)` describing the piecewise-linear map from encoded ticks to the
//!   parallel time system,
//! * `SCLK01_OUTPUT_DELIM_<id>` — delimiter used in formatted strings.
//!
//! The encoded tick count of a field vector is
//!
//! ```text
//! ticks = (((f0 - o0) * m1 + (f1 - o1)) * m2 + (f2 - o2)) * ...
//! ```
//!
//! The offsets give the value of the first count of each field. For
//! Voyager, the third field starts at 1, so its offset is 1 and the
//! encoded counter runs from 0 to 799.
//!
//! and the number of ticks in one count of the most significant field is
//! `m1 * m2 * ...`. The spacing between successive coefficient samples is
//! therefore `rate / ticks_per_msb` seconds per tick, which is the
//! behaviour verified against CSPICE `sct2e`.
//!
//! # Scope
//!
//! Partition boundaries are parsed but not applied. Encoded-tick to time
//! conversion does not need them; partition-aware reformatting of clock
//! strings does. Within a single partition the field/tick mapping above
//! is exact.
//!
//! Only clock type 1 is supported. No SCLK kernel is bundled; load the
//! mission kernel you need at deployment time.

use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use crate::convert::{et_to_tai, tai_to_et, tai_to_tt, tt_to_tai};
use crate::epoch::Epoch;
use crate::error::TimeError;
use crate::leap::LeapSecondTable;
use crate::math;

/// The parallel time system that SCLK coefficients map to.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum SclkTimeSystem {
    /// Barycentric Dynamical Time: the coefficients give ET seconds past
    /// J2000 directly.
    Tdb,
    /// Terrestrial Dynamical Time: the coefficients give TT seconds past
    /// J2000, and the LSK is needed to reach ET.
    Tdt,
}

/// One piecewise-linear segment of the tick-to-time map.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SclkCoefficient {
    /// Encoded tick count at the start of the segment.
    pub ticks: f64,
    /// Parallel-time value at the start of the segment, in seconds.
    pub seconds: f64,
    /// Parallel-time rate per most-significant-field count.
    pub rate: f64,
}

/// A parsed type-1 SCLK kernel for one spacecraft clock.
#[derive(Clone, Debug)]
pub struct SclkKernel {
    spacecraft_id: i32,
    data_type: i32,
    time_system: SclkTimeSystem,
    n_fields: usize,
    moduli: Vec<i64>,
    offsets: Vec<i64>,
    coefficients: Vec<SclkCoefficient>,
    delimiter: char,
    partition_start: Vec<f64>,
    partition_end: Vec<f64>,
}

impl SclkKernel {
    /// Parses a `KPL/SCLK` text kernel for the given clock.
    ///
    /// `spacecraft_id` may be given with either sign; kernel variables are
    /// looked up with the magnitude, matching NAIF kernels such as
    /// `MRO_SCLKSCET` (variables `_74`, clock id `-74`).
    pub fn parse(text: &str, spacecraft_id: i32) -> Result<Self, TimeError> {
        let tokens = tokenize(&extract_data(text)?);

        let n_fields_token = find_var(&tokens, "SCLK01_N_FIELDS", spacecraft_id)
            .ok_or(TimeError::SclkMissing("SCLK01_N_FIELDS"))?;
        let n_fields_values = parse_list(&tokens, n_fields_token)?;
        let n_fields = parse_i64(first(n_fields_values)?)?;
        if n_fields <= 0 || n_fields > 64 {
            return Err(TimeError::SclkParse);
        }
        let n_fields = n_fields as usize;

        let data_type = match find_var(&tokens, "SCLK_DATA_TYPE", spacecraft_id) {
            Some(index) => parse_i64(first(parse_list(&tokens, index)?)?)? as i32,
            None => 1,
        };
        if data_type != 1 {
            return Err(TimeError::SclkUnsupported);
        }

        let time_system = match find_var(&tokens, "SCLK01_TIME_SYSTEM", spacecraft_id) {
            Some(index) => match parse_i64(first(parse_list(&tokens, index)?)?)? {
                1 => SclkTimeSystem::Tdb,
                2 => SclkTimeSystem::Tdt,
                _ => return Err(TimeError::SclkParse),
            },
            None => SclkTimeSystem::Tdb,
        };

        let moduli_index = find_var(&tokens, "SCLK01_MODULI", spacecraft_id)
            .ok_or(TimeError::SclkMissing("SCLK01_MODULI"))?;
        let moduli = parse_i64_list(&tokens, moduli_index)?;
        if moduli.len() != n_fields || moduli.iter().any(|&value| value <= 0) {
            return Err(TimeError::SclkParse);
        }

        let offsets = match find_var(&tokens, "SCLK01_OFFSETS", spacecraft_id) {
            Some(index) => parse_i64_list(&tokens, index)?,
            None => alloc::vec![0i64; n_fields],
        };
        if offsets.len() != n_fields {
            return Err(TimeError::SclkParse);
        }

        let coefficients_index = find_var(&tokens, "SCLK01_COEFFICIENTS", spacecraft_id)
            .ok_or(TimeError::SclkMissing("SCLK01_COEFFICIENTS"))?;
        let coefficient_values = parse_list(&tokens, coefficients_index)?;
        if coefficient_values.len() < 3 || coefficient_values.len() % 3 != 0 {
            return Err(TimeError::SclkParse);
        }
        let mut coefficients = Vec::new();
        let mut previous_ticks = f64::NEG_INFINITY;
        let mut previous_seconds = f64::NEG_INFINITY;
        for chunk in coefficient_values.chunks(3) {
            let ticks = parse_f64(chunk[0])?;
            let seconds = parse_f64(chunk[1])?;
            let rate = parse_f64(chunk[2])?;
            if ticks <= previous_ticks || seconds <= previous_seconds || rate <= 0.0 {
                return Err(TimeError::SclkParse);
            }
            previous_ticks = ticks;
            previous_seconds = seconds;
            coefficients.push(SclkCoefficient {
                ticks,
                seconds,
                rate,
            });
        }

        let delimiter = match find_var(&tokens, "SCLK01_OUTPUT_DELIM", spacecraft_id) {
            Some(index) => delimiter_from_code(parse_i64(first(parse_list(&tokens, index)?)?)?),
            None => '.',
        };

        let partition_start = match find_var(&tokens, "SCLK_PARTITION_START", spacecraft_id) {
            Some(index) => parse_f64_list(&tokens, index)?,
            None => Vec::new(),
        };
        let partition_end = match find_var(&tokens, "SCLK_PARTITION_END", spacecraft_id) {
            Some(index) => parse_f64_list(&tokens, index)?,
            None => Vec::new(),
        };

        if partition_start.len() != partition_end.len() {
            return Err(TimeError::SclkParse);
        }
        // Only per-partition consistency is required. NAIF lets a clock
        // readout jump ahead or regress, which is exactly why partitions
        // exist, so a later partition may start at a lower encoded count
        // than the previous one ended. Partition bounds are not used by any
        // conversion here, so rejecting such a kernel would only make it
        // impossible to load.
        for (start, end) in partition_start.iter().zip(partition_end.iter()) {
            if !start.is_finite() || !end.is_finite() || start > end {
                return Err(TimeError::SclkParse);
            }
        }

        Ok(Self {
            spacecraft_id,
            data_type,
            time_system,
            n_fields,
            moduli,
            offsets,
            coefficients,
            delimiter,
            partition_start,
            partition_end,
        })
    }

    /// The clock id this kernel was parsed for.
    pub fn spacecraft_id(&self) -> i32 {
        self.spacecraft_id
    }

    /// The clock type. Only type 1 is supported.
    pub fn data_type(&self) -> i32 {
        self.data_type
    }

    /// The parallel time system of the coefficients.
    pub fn time_system(&self) -> SclkTimeSystem {
        self.time_system
    }

    /// Number of fields in a formatted clock string.
    pub fn n_fields(&self) -> usize {
        self.n_fields
    }

    /// Field moduli.
    pub fn moduli(&self) -> &[i64] {
        &self.moduli
    }

    /// Field offsets.
    pub fn offsets(&self) -> &[i64] {
        &self.offsets
    }

    /// Piecewise-linear coefficients.
    pub fn coefficients(&self) -> &[SclkCoefficient] {
        &self.coefficients
    }

    /// Parsed partition boundaries (not applied by this version).
    pub fn partitions(&self) -> (&[f64], &[f64]) {
        (&self.partition_start, &self.partition_end)
    }

    /// Number of ticks per count of the most significant field.
    pub fn ticks_per_msb(&self) -> f64 {
        let mut product = 1.0;
        for &modulus in &self.moduli[1..] {
            product *= modulus as f64;
        }
        product
    }

    /// Encodes a field vector as a total tick count.
    pub fn ticks_from_fields(&self, fields: &[i64]) -> Result<f64, TimeError> {
        self.ensure_partitionless()?;
        if fields.len() != self.n_fields {
            return Err(TimeError::SclkParse);
        }

        let mut ticks: i64 = 0;
        for (index, &field) in fields.iter().enumerate() {
            let value = field
                .checked_sub(self.offsets[index])
                .ok_or(TimeError::SclkParse)?;
            if value < 0 {
                return Err(TimeError::SclkParse);
            }
            ticks = if index == 0 {
                value
            } else {
                ticks
                    .checked_mul(self.moduli[index])
                    .and_then(|partial| partial.checked_add(value))
                    .ok_or(TimeError::SclkParse)?
            };
        }

        Ok(ticks as f64)
    }

    /// Decodes a total tick count into a field vector.
    ///
    /// Partition boundaries are not applied; the fields are those of the
    /// continuous tick encoding.
    pub fn fields_from_ticks(&self, ticks: f64) -> Result<Vec<i64>, TimeError> {
        self.ensure_partitionless()?;
        if !(ticks.is_finite() && ticks >= 0.0) {
            return Err(TimeError::SclkParse);
        }
        let rounded = math::round(ticks);
        if (ticks - rounded).abs() > 1e-6 {
            return Err(TimeError::SclkParse);
        }
        let mut remainder = rounded as i64;

        let mut fields = alloc::vec![0i64; self.n_fields];

        for index in (1..self.n_fields).rev() {
            let modulus = self.moduli[index];
            fields[index] = remainder % modulus + self.offsets[index];
            remainder /= modulus;
        }

        if remainder >= self.moduli[0] {
            return Err(TimeError::SclkParse);
        }
        fields[0] = remainder + self.offsets[0];

        Ok(fields)
    }

    /// Converts encoded ticks to the parallel time system, in seconds.
    pub fn parallel_seconds(&self, ticks: f64) -> Result<f64, TimeError> {
        if !ticks.is_finite() {
            return Err(TimeError::SclkParse);
        }
        let segment = self.segment_for_ticks(ticks)?;
        Ok(segment.seconds + (ticks - segment.ticks) * segment.rate / self.ticks_per_msb())
    }

    /// Converts a parallel-time value in seconds to encoded ticks.
    pub fn ticks_from_parallel(&self, seconds: f64) -> Result<f64, TimeError> {
        if !seconds.is_finite() {
            return Err(TimeError::SclkParse);
        }
        let segment = self.segment_for_seconds(seconds)?;
        if segment.rate == 0.0 {
            return Err(TimeError::SclkParse);
        }
        Ok(segment.ticks + (seconds - segment.seconds) * self.ticks_per_msb() / segment.rate)
    }

    /// Converts encoded ticks to `ET` (TDB seconds past J2000).
    pub fn ticks_to_et(&self, ticks: f64, lsk: &LeapSecondTable) -> Result<f64, TimeError> {
        let parallel = self.parallel_seconds(ticks)?;
        match self.time_system {
            SclkTimeSystem::Tdb => Ok(parallel),
            SclkTimeSystem::Tdt => Ok(tai_to_et(tt_to_tai(parallel, lsk), lsk)),
        }
    }

    /// Converts `ET` (TDB seconds past J2000) to encoded ticks.
    pub fn et_to_ticks(&self, et: f64, lsk: &LeapSecondTable) -> Result<f64, TimeError> {
        match self.time_system {
            SclkTimeSystem::Tdb => self.ticks_from_parallel(et),
            SclkTimeSystem::Tdt => self.ticks_from_parallel(tai_to_tt(et_to_tai(et, lsk), lsk)),
        }
    }

    /// Converts encoded ticks to a TAI epoch.
    pub fn ticks_to_tai(&self, ticks: f64, lsk: &LeapSecondTable) -> Result<Epoch, TimeError> {
        let parallel = self.parallel_seconds(ticks)?;
        Ok(match self.time_system {
            SclkTimeSystem::Tdb => et_to_tai(parallel, lsk),
            SclkTimeSystem::Tdt => tt_to_tai(parallel, lsk),
        })
    }

    /// Converts a TAI epoch to encoded ticks.
    pub fn tai_to_ticks(&self, epoch: Epoch, lsk: &LeapSecondTable) -> Result<f64, TimeError> {
        let parallel = match self.time_system {
            SclkTimeSystem::Tdb => tai_to_et(epoch, lsk),
            SclkTimeSystem::Tdt => tai_to_tt(epoch, lsk),
        };
        self.ticks_from_parallel(parallel)
    }

    /// Parses a formatted clock string into a field vector.
    pub fn parse_string(&self, sclk: &str) -> Result<Vec<i64>, TimeError> {
        self.ensure_partitionless()?;
        let mut fields = Vec::new();
        for part in sclk.split(self.delimiter) {
            fields.push(parse_i64(part.trim())?);
        }
        if fields.len() != self.n_fields {
            return Err(TimeError::SclkParse);
        }
        Ok(fields)
    }

    /// Formats a field vector as a clock string.
    pub fn format_string(&self, fields: &[i64]) -> Result<String, TimeError> {
        self.ensure_partitionless()?;
        if fields.len() != self.n_fields {
            return Err(TimeError::SclkParse);
        }
        let delimiter = self.delimiter.to_string();
        let parts: Vec<String> = fields.iter().map(|value| value.to_string()).collect();
        Ok(parts.join(&delimiter))
    }

    /// Fields and strings are only defined without partition offsets in
    /// this version; keep encoded ticks available for partition-aware
    /// callers that apply the boundaries themselves.
    fn ensure_partitionless(&self) -> Result<(), TimeError> {
        if self.partition_start.is_empty() && self.partition_end.is_empty() {
            return Ok(());
        }
        if self.partition_start.len() == 1 && self.partition_start[0] == 0.0 {
            return Ok(());
        }
        Err(TimeError::SclkPartitionsUnsupported)
    }

    fn segment_for_ticks(&self, ticks: f64) -> Result<SclkCoefficient, TimeError> {
        if !ticks.is_finite() {
            return Err(TimeError::SclkParse);
        }
        if self.coefficients.is_empty() {
            return Err(TimeError::SclkMissing("SCLK01_COEFFICIENTS"));
        }
        let index = self
            .coefficients
            .partition_point(|segment| segment.ticks <= ticks);
        let index = if index == 0 { 0 } else { index - 1 };
        Ok(self.coefficients[index])
    }

    fn segment_for_seconds(&self, seconds: f64) -> Result<SclkCoefficient, TimeError> {
        if !seconds.is_finite() {
            return Err(TimeError::SclkParse);
        }
        if self.coefficients.is_empty() {
            return Err(TimeError::SclkMissing("SCLK01_COEFFICIENTS"));
        }
        let index = self
            .coefficients
            .partition_point(|segment| segment.seconds <= seconds);
        let index = if index == 0 { 0 } else { index - 1 };
        Ok(self.coefficients[index])
    }
}

/// A spacecraft clock bound to a leap-second kernel.
///
/// The [`SclkMap`] trait needs the LSK for the `TT -> ET` step of clocks
/// whose coefficients are in TT.
pub struct SclkClock<'a> {
    /// Parsed SCLK kernel.
    pub kernel: &'a SclkKernel,
    /// Leap-second kernel used to relate TT and ET.
    pub lsk: &'a LeapSecondTable,
}

impl SclkMap for SclkClock<'_> {
    fn sclk_to_et(&self, sclk: &str) -> Result<f64, TimeError> {
        let fields = self.kernel.parse_string(sclk)?;
        let ticks = self.kernel.ticks_from_fields(&fields)?;
        self.kernel.ticks_to_et(ticks, self.lsk)
    }

    fn et_to_sclk(&self, et: f64) -> Result<String, TimeError> {
        let ticks = self.kernel.et_to_ticks(et, self.lsk)?;
        let fields = self.kernel.fields_from_ticks(ticks)?;
        self.kernel.format_string(&fields)
    }
}

/// Converts between a spacecraft clock string and `ET` (TDB seconds past
/// J2000).
pub trait SclkMap {
    /// Converts an SCLK string to `ET`.
    fn sclk_to_et(&self, sclk: &str) -> Result<f64, TimeError>;

    /// Converts `ET` to an SCLK string.
    fn et_to_sclk(&self, et: f64) -> Result<String, TimeError>;
}

/// Extracts every `\begindata` ... `\begintext` block.
fn extract_data(text: &str) -> Result<String, TimeError> {
    const BEGIN: &str = "\\begindata";
    const END: &str = "\\begintext";

    let mut data = String::new();
    let mut rest = text;
    let mut found = false;

    while let Some(start) = rest.find(BEGIN) {
        found = true;
        let after = &rest[start + BEGIN.len()..];
        let end = after.find(END).unwrap_or(after.len());
        data.push_str(&after[..end]);
        data.push('\n');
        if end == after.len() {
            break;
        }
        rest = &after[end + END.len()..];
    }

    if found {
        Ok(data)
    } else {
        Err(TimeError::SclkParse)
    }
}

/// Splits on whitespace and the assignment punctuation.
fn tokenize(input: &str) -> Vec<String> {
    let mut tokens = Vec::new();
    let mut current = String::new();

    for ch in input.chars() {
        match ch {
            '=' | '(' | ')' | ',' => {
                if !current.is_empty() {
                    tokens.push(core::mem::take(&mut current));
                }
                tokens.push(ch.to_string());
            }
            c if c.is_whitespace() => {
                if !current.is_empty() {
                    tokens.push(core::mem::take(&mut current));
                }
            }
            c => current.push(c),
        }
    }

    if !current.is_empty() {
        tokens.push(current);
    }

    tokens
}

/// Finds the index of a kernel variable token, trying both id signs.
fn find_var(tokens: &[String], base: &str, spacecraft_id: i32) -> Option<usize> {
    let magnitude = i64::from(spacecraft_id).abs();
    let candidates = [
        format!("{base}_{spacecraft_id}"),
        format!("{base}_{magnitude}"),
        format!("{base}_-{magnitude}"),
    ];
    tokens
        .iter()
        .rposition(|token| candidates.iter().any(|candidate| candidate == token))
}

/// Reads the parenthesised list following `name = (`.
fn parse_list(tokens: &[String], name_index: usize) -> Result<Vec<&str>, TimeError> {
    let mut index = name_index + 1;
    expect(tokens, index, "=")?;
    index += 1;
    expect(tokens, index, "(")?;
    index += 1;

    let mut values = Vec::new();
    while index < tokens.len() && tokens[index] != ")" {
        if tokens[index] != "," {
            values.push(tokens[index].as_str());
        }
        index += 1;
    }
    if index >= tokens.len() {
        return Err(TimeError::SclkParse);
    }
    Ok(values)
}

fn expect(tokens: &[String], index: usize, expected: &str) -> Result<(), TimeError> {
    if tokens.get(index).map(String::as_str) == Some(expected) {
        Ok(())
    } else {
        Err(TimeError::SclkParse)
    }
}

fn first(values: Vec<&str>) -> Result<&str, TimeError> {
    values.into_iter().next().ok_or(TimeError::SclkParse)
}

fn parse_i64(token: &str) -> Result<i64, TimeError> {
    token.parse::<i64>().map_err(|_| TimeError::SclkParse)
}

fn parse_f64(token: &str) -> Result<f64, TimeError> {
    // NAIF text kernels use Fortran `D` exponents.
    let normalised = token.replace('D', "E").replace('d', "e");
    let value = normalised
        .parse::<f64>()
        .map_err(|_| TimeError::SclkParse)?;
    if value.is_finite() {
        Ok(value)
    } else {
        Err(TimeError::SclkParse)
    }
}

fn parse_i64_list(tokens: &[String], name_index: usize) -> Result<Vec<i64>, TimeError> {
    parse_list(tokens, name_index)?
        .into_iter()
        .map(parse_i64)
        .collect()
}

fn parse_f64_list(tokens: &[String], name_index: usize) -> Result<Vec<f64>, TimeError> {
    parse_list(tokens, name_index)?
        .into_iter()
        .map(parse_f64)
        .collect()
}

fn delimiter_from_code(code: i64) -> char {
    match code {
        2 => ':',
        3 => '-',
        4 => ',',
        5 => ' ',
        _ => '.',
    }
}