Skip to main content

nyx_space/od/msr/trackingdata/
python.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*/
18
19use super::{Measurement, MeasurementType, TrackingDataArc};
20use crate::io::{ExportCfg, InputOutputError};
21use hifitime::{Duration, Epoch};
22use pyo3::prelude::*;
23use pyo3::types::PyType;
24use std::collections::HashMap;
25use std::ops::Bound::{Excluded, Included, Unbounded};
26
27#[pymethods]
28impl TrackingDataArc {
29    #[new]
30    fn py_new(measurements: Vec<Measurement>) -> Self {
31        let mut trk_data = Self {
32            measurements,
33            source: None,
34            moduli: None,
35            force_reject: false,
36        };
37
38        trk_data.sort();
39
40        trk_data
41    }
42
43    /// Initializes a new Almanac from a file path to CCSDS OEM file, after converting to to SPICE SPK/BSP
44    ///
45    /// :type path: str
46    /// :type aliases: dict
47    /// :rtype: nyx_space.od.TrackingDataArc
48    #[classmethod]
49    #[pyo3(name = "from_ccsds_tdm")]
50    fn py_from_ccsds_tdm_file(
51        _cls: Bound<'_, PyType>,
52        path: &str,
53        aliases: Option<HashMap<String, String>>,
54    ) -> Result<Self, InputOutputError> {
55        TrackingDataArc::from_tdm(path, aliases)
56    }
57
58    #[classmethod]
59    #[pyo3(name = "from_parquet")]
60    fn py_from_parquet(_cls: Bound<'_, PyType>, path: &str) -> Result<Self, InputOutputError> {
61        Self::from_parquet(path)
62    }
63
64    #[pyo3(name = "write_ccsds_tdm")]
65    fn py_write_ccsds_tdm(
66        &self,
67        spacecraft_name: String,
68        aliases: Option<HashMap<String, String>>,
69        path: &str,
70    ) -> Result<String, InputOutputError> {
71        Ok(self
72            .clone()
73            .to_tdm_file(spacecraft_name, aliases, path, ExportCfg::default())?
74            .to_str()
75            .unwrap_or("woah_bug_building_path")
76            .to_string())
77    }
78
79    #[pyo3(name = "unique_aliases")]
80    fn py_unique_aliases(&self) -> Vec<String> {
81        self.unique_aliases().iter().cloned().collect()
82    }
83    #[pyo3(name = "unique_types")]
84    fn py_unique_types(&self) -> Vec<MeasurementType> {
85        self.unique_types().iter().cloned().collect()
86    }
87
88    fn __str__(&self) -> String {
89        format!("{self}")
90    }
91
92    fn __repr__(&self) -> String {
93        format!("{self} @ {self:p}")
94    }
95
96    #[getter]
97    fn get_force_reject(&self) -> bool {
98        self.force_reject
99    }
100
101    #[setter]
102    fn set_force_reject(&mut self, reject: bool) {
103        self.force_reject = reject;
104    }
105
106    #[pyo3(name = "filter_by_epoch")]
107    fn py_filter_by_epoch(&self, start: Option<Epoch>, end: Option<Epoch>) -> Self {
108        let start_bound = start.map(Included).unwrap_or(Unbounded);
109        let end_bound = end.map(Excluded).unwrap_or(Unbounded);
110        self.clone().filter_by_epoch((start_bound, end_bound))
111    }
112
113    #[pyo3(name = "filter_by_offset")]
114    fn py_filter_by_offset(&self, start: Option<Duration>, end: Option<Duration>) -> Self {
115        let start_bound = match start {
116            Some(s) => Included(s),
117            None => Unbounded,
118        };
119        let end_bound = match end {
120            Some(e) => Excluded(e),
121            None => Unbounded,
122        };
123        self.clone().filter_by_offset((start_bound, end_bound))
124    }
125
126    #[pyo3(name = "filter_by_tracker")]
127    fn py_filter_by_tracker(&self, tracker: String) -> Self {
128        self.clone().filter_by_tracker(tracker)
129    }
130
131    #[pyo3(name = "filter_by_measurement_type")]
132    fn py_filter_by_measurement_type(&self, msr_type: MeasurementType) -> Self {
133        self.clone().filter_by_measurement_type(msr_type)
134    }
135
136    #[pyo3(name = "exclude_tracker")]
137    fn py_exclude_tracker(&self, tracker: String) -> Self {
138        self.clone().exclude_tracker(tracker)
139    }
140
141    #[pyo3(name = "exclude_by_epoch")]
142    fn py_exclude_by_epoch(&self, start: Option<Epoch>, end: Option<Epoch>) -> Self {
143        let start_bound = match start {
144            Some(s) => Included(s),
145            None => Unbounded,
146        };
147        let end_bound = match end {
148            Some(e) => Excluded(e),
149            None => Unbounded,
150        };
151        self.clone().exclude_by_epoch((start_bound, end_bound))
152    }
153
154    #[pyo3(name = "exclude_measurement_type")]
155    fn py_exclude_measurement_type(&self, msr_type: MeasurementType) -> Self {
156        self.clone().exclude_measurement_type(msr_type)
157    }
158
159    #[pyo3(name = "resid_vs_ref_check")]
160    fn py_resid_vs_ref_check(&self) -> Self {
161        self.clone().resid_vs_ref_check()
162    }
163
164    #[pyo3(name = "to_parquet")]
165    fn py_to_parquet(&self, path: String, cfg: ExportCfg) -> Result<String, InputOutputError> {
166        self.to_parquet(path, cfg)
167            .map(|pathbuf| pathbuf.to_string_lossy().into_owned())
168    }
169}