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