Skip to main content

nyx_space/od/msr/trackingdata/
mod.rs

1/*
2    Nyx, blazing fast astrodynamics
3    Copyright (C) 2018-onwards Christopher Rabotin <christopher.rabotin@gmail.com>
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU Affero General Public License as published
7    by the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU Affero General Public License for more details.
14
15    You should have received a copy of the GNU Affero General Public License
16    along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*/
18use super::{measurement::Measurement, MeasurementType};
19use core::fmt;
20use hifitime::prelude::{Duration, Epoch};
21use indexmap::{IndexMap, IndexSet};
22use log::{error, info, warn};
23use std::collections::btree_map::Entry;
24use std::collections::BTreeMap;
25use std::ops::Bound::{self, Excluded, Included, Unbounded};
26use std::ops::{Add, AddAssign, RangeBounds};
27
28mod io_ccsds_tdm;
29mod io_parquet;
30
31#[cfg(feature = "python")]
32use pyo3::prelude::*;
33#[cfg(feature = "python")]
34mod python;
35
36/// Tracking data storing all of measurements as a B-Tree.
37/// It inherently does NOT support multiple concurrent measurements from several trackers.
38///
39/// # Measurement Moduli, e.g. range modulus
40///
41/// In the case of ranging, and possibly other data types, a code is used to measure the range to the spacecraft. The length of this code
42/// determines the ambiguity resolution, as per equation 9 in section 2.2.2.2 of the JPL DESCANSO, document 214, _Pseudo-Noise and Regenerative Ranging_.
43/// For example, using the JPL Range Code and a frequency range clock of 1 MHz, the range ambiguity is 75,660 km. In other words,
44/// as soon as the spacecraft is at a range of 75,660 + 1 km the JPL Range Code will report the vehicle to be at a range of 1 km.
45/// This is simply because the range code overlaps with itself, effectively loosing track of its own reference:
46/// it's due to the phase shift of the signal "lapping" the original signal length.
47///
48/// ```text
49///             (Spacecraft)
50///             ^
51///             |    Actual Distance = 75,661 km
52///             |
53/// 0 km                                         75,660 km (Wrap-Around)
54/// |-----------------------------------------------|
55///   When the "code length" is exceeded,
56///   measurements wrap back to 0.
57///
58/// So effectively:
59///     Observed code range = Actual range (mod 75,660 km)
60///     75,661 km → 1 km
61///
62/// ```
63///
64/// Nyx can only resolve the range ambiguity if the tracking data specifies a modulus for this specific measurement type.
65/// For example, in the case of the JPL Range Code and a 1 MHz range clock, the ambiguity interval is 75,660 km.
66///
67/// The measurement used in the Orbit Determination Process then becomes the following, where `//` represents the [Euclidian division](https://doc.rust-lang.org/std/primitive.f64.html#method.div_euclid).
68///
69/// ```text
70/// k = computed_obs // ambiguity_interval
71/// real_obs = measured_obs + k * modulus
72/// ```
73///
74/// Reference: JPL DESCANSO, document 214, _Pseudo-Noise and Regenerative Ranging_.
75///
76#[derive(Clone, Default)]
77#[cfg_attr(feature = "python", pyclass)]
78pub struct TrackingDataArc {
79    /// All measurements in this data arc
80    pub measurements: BTreeMap<Epoch, Measurement>, // BUG: Consider a map of tracking to epoch!
81    /// Source file if loaded from a file or saved to a file.
82    pub source: Option<String>,
83    /// Optionally provide a map of modulos (e.g. the RANGE_MODULO of CCSDS TDM).
84    pub moduli: Option<IndexMap<MeasurementType, f64>>,
85    /// Reject all of the measurements, useful for debugging passes.
86    pub force_reject: bool,
87}
88
89#[cfg_attr(feature = "python", pymethods)]
90impl TrackingDataArc {
91    /// Returns the start epoch of this tracking arc
92    pub fn start_epoch(&self) -> Option<Epoch> {
93        self.measurements.first_key_value().map(|(k, _)| *k)
94    }
95
96    /// Returns the end epoch of this tracking arc
97    pub fn end_epoch(&self) -> Option<Epoch> {
98        self.measurements.last_key_value().map(|(k, _)| *k)
99    }
100
101    /// Returns the duration this tracking arc
102    pub fn duration(&self) -> Option<Duration> {
103        match self.start_epoch() {
104            Some(start) => self.end_epoch().map(|end| end - start),
105            None => None,
106        }
107    }
108
109    /// Returns the number of measurements in this data arc
110    pub fn len(&self) -> usize {
111        self.measurements.len()
112    }
113
114    /// Returns whether this arc has no measurements.
115    pub fn is_empty(&self) -> bool {
116        self.measurements.is_empty()
117    }
118
119    /// Returns the minimum duration between two subsequent measurements.
120    pub fn min_duration_sep(&self) -> Option<Duration> {
121        if self.is_empty() {
122            None
123        } else {
124            let mut min_sep = Duration::MAX;
125            let mut prev_epoch = self.start_epoch().unwrap();
126            for (epoch, _) in self.measurements.iter().skip(1) {
127                let this_sep = *epoch - prev_epoch;
128                min_sep = min_sep.min(this_sep);
129                prev_epoch = *epoch;
130            }
131            Some(min_sep)
132        }
133    }
134}
135
136impl TrackingDataArc {
137    /// Set (or overwrites) the modulus of the provided measurement type.
138    pub fn set_moduli(&mut self, msr_type: MeasurementType, modulus: f64) {
139        if modulus.is_nan() || modulus.abs() < f64::EPSILON {
140            warn!("cannot set modulus for {msr_type:?} to {modulus}");
141            return;
142        }
143        if self.moduli.is_none() {
144            self.moduli = Some(IndexMap::new());
145        }
146
147        self.moduli.as_mut().unwrap().insert(msr_type, modulus);
148    }
149
150    /// Applies the moduli to each measurement, if defined.
151    pub fn apply_moduli(&mut self) {
152        if let Some(moduli) = &self.moduli {
153            for msr in self.measurements.values_mut() {
154                for (msr_type, modulus) in moduli {
155                    if let Some(msr_value) = msr.data.get_mut(msr_type) {
156                        *msr_value %= *modulus;
157                    }
158                }
159            }
160        }
161    }
162
163    /// Returns the unique list of aliases in this tracking data arc
164    pub fn unique_aliases(&self) -> IndexSet<String> {
165        self.unique().0
166    }
167
168    /// Returns the unique measurement types in this tracking data arc
169    pub fn unique_types(&self) -> IndexSet<MeasurementType> {
170        self.unique().1
171    }
172
173    /// Returns the unique trackers and unique measurement types in this data arc
174    pub fn unique(&self) -> (IndexSet<String>, IndexSet<MeasurementType>) {
175        let mut aliases = IndexSet::new();
176        let mut types = IndexSet::new();
177        for msr in self.measurements.values() {
178            aliases.insert(msr.tracker.clone());
179            for k in msr.data.keys() {
180                types.insert(*k);
181            }
182        }
183        (aliases, types)
184    }
185
186    /// Returns a new tracking arc that only contains measurements that fall within the given epoch range.
187    pub fn filter_by_epoch<R: RangeBounds<Epoch>>(mut self, bound: R) -> Self {
188        self.measurements = self
189            .measurements
190            .range(bound)
191            .map(|(epoch, msr)| (*epoch, msr.clone()))
192            .collect::<BTreeMap<Epoch, Measurement>>();
193        self
194    }
195
196    /// Returns a new tracking arc that only contains measurements that fall within the given offset from the first epoch.
197    /// For example, a bound of 30.minutes()..90.minutes() will only read measurements from the start of the arc + 30 minutes until start + 90 minutes.
198    pub fn filter_by_offset<R: RangeBounds<Duration>>(self, bound: R) -> Self {
199        if self.is_empty() {
200            return self;
201        }
202        // Rebuild an epoch bound.
203        let start = match bound.start_bound() {
204            Unbounded => self.start_epoch().unwrap(),
205            Included(offset) | Excluded(offset) => self.start_epoch().unwrap() + *offset,
206        };
207
208        let end = match bound.end_bound() {
209            Unbounded => self.end_epoch().unwrap(),
210            Included(offset) | Excluded(offset) => self.start_epoch().unwrap() + *offset,
211        };
212
213        self.filter_by_epoch(start..end)
214    }
215
216    /// Returns a new tracking arc that only contains measurements from the desired tracker.
217    pub fn filter_by_tracker(mut self, tracker: String) -> Self {
218        self.measurements = self
219            .measurements
220            .iter()
221            .filter_map(|(epoch, msr)| {
222                if msr.tracker == tracker {
223                    Some((*epoch, msr.clone()))
224                } else {
225                    None
226                }
227            })
228            .collect::<BTreeMap<Epoch, Measurement>>();
229        self
230    }
231
232    /// Returns a new tracking arc that only contains measurements of the provided type.
233    pub fn filter_by_measurement_type(mut self, included_type: MeasurementType) -> Self {
234        self.measurements.retain(|_epoch, msr| {
235            msr.data.retain(|msr_type, _| *msr_type == included_type);
236            !msr.data.is_empty()
237        });
238        self
239    }
240
241    /// Returns a new tracking arc that contains measurements from all trackers except the one provided
242    pub fn exclude_tracker(mut self, excluded_tracker: String) -> Self {
243        self.measurements = self
244            .measurements
245            .iter()
246            .filter_map(|(epoch, msr)| {
247                if msr.tracker != excluded_tracker {
248                    Some((*epoch, msr.clone()))
249                } else {
250                    None
251                }
252            })
253            .collect::<BTreeMap<Epoch, Measurement>>();
254        self
255    }
256
257    /// Returns a new tracking arc that excludes measurements within the given epoch range.
258    pub fn exclude_by_epoch<R: RangeBounds<Epoch>>(mut self, bound: R) -> Self {
259        info!(
260            "Excluding measurements from {:?} to {:?}",
261            bound.start_bound(),
262            bound.end_bound()
263        );
264
265        let mut new_measurements = BTreeMap::new();
266
267        // Include entries before the excluded range
268        new_measurements.extend(
269            self.measurements
270                .range((Bound::Unbounded, bound.start_bound()))
271                .map(|(epoch, msr)| (*epoch, msr.clone())),
272        );
273
274        // Include entries after the excluded range
275        new_measurements.extend(
276            self.measurements
277                .range((bound.end_bound(), Bound::Unbounded))
278                .map(|(epoch, msr)| (*epoch, msr.clone())),
279        );
280
281        self.measurements = new_measurements;
282        self
283    }
284
285    /// Returns a new tracking arc that contains measurements from all trackers except the one provided
286    pub fn exclude_measurement_type(mut self, excluded_type: MeasurementType) -> Self {
287        self.measurements = self
288            .measurements
289            .iter_mut()
290            .map(|(epoch, msr)| {
291                msr.data.retain(|msr_type, _| *msr_type != excluded_type);
292
293                (*epoch, msr.clone())
294            })
295            .collect::<BTreeMap<Epoch, Measurement>>();
296        self
297    }
298
299    /// Marks measurements within the given epoch range as rejected.
300    pub fn reject_by_epoch<R: RangeBounds<Epoch>>(mut self, bound: R) -> Self {
301        for (_epoch, msr) in self.measurements.range_mut(bound) {
302            msr.rejected = true;
303        }
304        self
305    }
306
307    /// Marks measurements from the provided tracker as rejected.
308    pub fn reject_by_tracker(mut self, tracker: String) -> Self {
309        for msr in self.measurements.values_mut() {
310            if msr.tracker == tracker {
311                msr.rejected = true;
312            }
313        }
314        self
315    }
316
317    /// Downsamples the tracking data to a lower frequency using a simple moving average low-pass filter followed by decimation,
318    /// returning new `TrackingDataArc` with downsampled measurements.
319    ///
320    /// It provides a computationally efficient approach to reduce the sampling rate while mitigating aliasing effects.
321    ///
322    /// # Algorithm
323    ///
324    /// 1. A simple moving average filter is applied as a low-pass filter.
325    /// 2. Decimation is performed by selecting every Nth sample after filtering.
326    ///
327    /// # Advantages
328    ///
329    /// - Computationally efficient, suitable for large datasets common in spaceflight applications.
330    /// - Provides basic anti-aliasing, crucial for preserving signal integrity in orbit determination and tracking.
331    /// - Maintains phase information, important for accurate timing in spacecraft state estimation.
332    ///
333    /// # Limitations
334    ///
335    /// - The frequency response is not as sharp as more sophisticated filters (e.g., FIR, IIR).
336    /// - May not provide optimal stopband attenuation for high-precision applications.
337    ///
338    /// ## Considerations for Spaceflight Applications
339    ///
340    /// - Suitable for initial data reduction in ground station tracking pipelines.
341    /// - Adequate for many orbit determination and tracking tasks where computational speed is prioritized.
342    /// - For high-precision applications (e.g., interplanetary navigation), consider using more advanced filtering techniques.
343    ///
344    pub fn downsample(self, target_step: Duration) -> Self {
345        if self.is_empty() {
346            return self;
347        }
348        let current_step = self.min_duration_sep().unwrap();
349
350        if current_step >= target_step {
351            warn!("cannot downsample tracking data from {current_step} to {target_step} (that would be upsampling)");
352            return self;
353        }
354
355        let current_hz = 1.0 / current_step.to_seconds();
356        let target_hz = 1.0 / target_step.to_seconds();
357
358        // Simple moving average as low-pass filter
359        let window_size = (current_hz / target_hz).round() as usize;
360
361        info!("downsampling tracking data from {current_step} ({current_hz:.6} Hz) to {target_step} ({target_hz:.6} Hz) (N = {window_size})");
362
363        let mut result = TrackingDataArc {
364            source: self.source.clone(),
365            ..Default::default()
366        };
367
368        let measurements: Vec<_> = self.measurements.iter().collect();
369
370        for (i, (epoch, _)) in measurements.iter().enumerate().step_by(window_size) {
371            let start = i.saturating_sub(window_size / 2);
372            let end = (i + window_size / 2 + 1).min(measurements.len());
373            let window = &measurements[start..end];
374
375            let mut filtered_measurement = Measurement {
376                tracker: window[0].1.tracker.clone(),
377                epoch: **epoch,
378                data: IndexMap::new(),
379                rejected: false,
380            };
381
382            // Apply moving average filter for each measurement type
383            for mtype in self.unique_types() {
384                let sum: f64 = window.iter().filter_map(|(_, m)| m.data.get(&mtype)).sum();
385                let count = window
386                    .iter()
387                    .filter(|(_, m)| m.data.contains_key(&mtype))
388                    .count();
389
390                if count > 0 {
391                    filtered_measurement.data.insert(mtype, sum / count as f64);
392                }
393            }
394
395            result.measurements.insert(**epoch, filtered_measurement);
396        }
397        result
398    }
399
400    pub fn resid_vs_ref_check(mut self) -> Self {
401        self.force_reject = true;
402        self
403    }
404}
405
406impl fmt::Display for TrackingDataArc {
407    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
408        if self.is_empty() {
409            write!(f, "Empty tracking arc")
410        } else {
411            let start = self.start_epoch().unwrap();
412            let end = self.end_epoch().unwrap();
413            let src = match &self.source {
414                Some(src) => format!(" (source: {src})"),
415                None => String::new(),
416            };
417            write!(
418                f,
419                "Tracking arc with {} measurements of type {:?} over {} (from {start} to {end}) with trackers {:?}{src}",
420                self.len(),
421                self.unique_types(),
422                end - start,
423                self.unique_aliases()
424            )
425        }
426    }
427}
428
429impl fmt::Debug for TrackingDataArc {
430    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
431        write!(f, "{self} @ {self:p}")
432    }
433}
434
435impl PartialEq for TrackingDataArc {
436    fn eq(&self, other: &Self) -> bool {
437        self.measurements == other.measurements
438    }
439}
440
441impl Add for TrackingDataArc {
442    type Output = Self;
443
444    fn add(mut self, rhs: Self) -> Self::Output {
445        self.force_reject = false;
446        for (epoch, msr) in rhs.measurements {
447            if let Entry::Vacant(e) = self.measurements.entry(epoch) {
448                e.insert(msr);
449            } else {
450                error!("merging tracking data with overlapping epoch is not supported");
451            }
452        }
453
454        self
455    }
456}
457
458impl AddAssign for TrackingDataArc {
459    fn add_assign(&mut self, rhs: Self) {
460        *self = self.clone() + rhs;
461    }
462}