|
| 1 | +#[cfg(doc)] |
| 2 | +use crate::prelude::SP3Key; |
| 3 | + |
| 4 | +use crate::Vector3D; |
| 5 | + |
| 6 | +#[cfg(feature = "serde")] |
| 7 | +use serde::{Deserialize, Serialize}; |
| 8 | + |
| 9 | +/// SP3 record content are [SP3Entry] indexed by [SP3Key]. |
| 10 | +#[derive(Debug, Copy, Clone)] |
| 11 | +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] |
| 12 | +pub struct SP3Entry { |
| 13 | + /// ECEF position in kilometers with 10⁻³ precision. |
| 14 | + pub position_km: Vector3D, |
| 15 | + |
| 16 | + /// ECEF velocity vectori in km.s⁻¹. |
| 17 | + pub velocity_km_s: Option<Vector3D>, |
| 18 | + |
| 19 | + /// True if the state vector is predicted |
| 20 | + pub orbit_prediction: bool, |
| 21 | + |
| 22 | + /// True if vehicle being maneuvered (rocket truster) |
| 23 | + /// since last state. |
| 24 | + pub maneuver: bool, |
| 25 | + |
| 26 | + /// Discontinuity in the satellite clock correction |
| 27 | + /// (for example: internal clock swap) |
| 28 | + pub clock_event: bool, |
| 29 | + |
| 30 | + /// True when the clock state is actually predicted |
| 31 | + pub clock_prediction: bool, |
| 32 | + |
| 33 | + /// Clock offset correction, in microsecond with 10⁻¹² precision. |
| 34 | + pub clock_us: Option<f64>, |
| 35 | + |
| 36 | + /// Clock drift in nanoseconds with 10⁻¹⁶ precision. |
| 37 | + pub clock_drift_ns: Option<f64>, |
| 38 | +} |
| 39 | + |
| 40 | +impl std::ops::Sub for SP3Entry { |
| 41 | + type Output = SP3Entry; |
| 42 | + |
| 43 | + fn sub(self, rhs: Self) -> Self { |
| 44 | + Self { |
| 45 | + position_km: ( |
| 46 | + self.position_km.0 - rhs.position_km.0, |
| 47 | + self.position_km.1 - self.position_km.1, |
| 48 | + self.position_km.2 - rhs.position_km.2, |
| 49 | + ), |
| 50 | + velocity_km_s: if let Some(velocity_km_s) = self.velocity_km_s { |
| 51 | + if let Some(rhs) = rhs.velocity_km_s { |
| 52 | + Some(( |
| 53 | + velocity_km_s.0 - rhs.0, |
| 54 | + velocity_km_s.1 - rhs.1, |
| 55 | + velocity_km_s.2 - rhs.2, |
| 56 | + )) |
| 57 | + } else { |
| 58 | + None |
| 59 | + } |
| 60 | + } else { |
| 61 | + None |
| 62 | + }, |
| 63 | + maneuver: self.maneuver, |
| 64 | + clock_event: self.clock_event, |
| 65 | + clock_prediction: self.clock_prediction, |
| 66 | + orbit_prediction: self.orbit_prediction, |
| 67 | + clock_us: if let Some(clock_us) = self.clock_us { |
| 68 | + if let Some(rhs) = rhs.clock_us { |
| 69 | + Some(clock_us - rhs) |
| 70 | + } else { |
| 71 | + None |
| 72 | + } |
| 73 | + } else { |
| 74 | + None |
| 75 | + }, |
| 76 | + clock_drift_ns: if let Some(clock_drift_ns) = self.clock_drift_ns { |
| 77 | + if let Some(rhs) = rhs.clock_drift_ns { |
| 78 | + Some(clock_drift_ns - rhs) |
| 79 | + } else { |
| 80 | + None |
| 81 | + } |
| 82 | + } else { |
| 83 | + None |
| 84 | + }, |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl std::ops::SubAssign for SP3Entry { |
| 90 | + fn sub_assign(&mut self, rhs: Self) { |
| 91 | + self.position_km.0 -= rhs.position_km.0; |
| 92 | + self.position_km.1 -= rhs.position_km.1; |
| 93 | + self.position_km.2 -= rhs.position_km.2; |
| 94 | + |
| 95 | + if let Some(velocity_km_s) = &mut self.velocity_km_s { |
| 96 | + if let Some(rhs) = rhs.velocity_km_s { |
| 97 | + velocity_km_s.0 -= rhs.0; |
| 98 | + velocity_km_s.1 -= rhs.1; |
| 99 | + velocity_km_s.2 -= rhs.2; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + if let Some(clock_us) = &mut self.clock_us { |
| 104 | + if let Some(rhs) = rhs.clock_us { |
| 105 | + *clock_us -= rhs; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if let Some(clock_drift_ns) = &mut self.clock_drift_ns { |
| 110 | + if let Some(rhs) = rhs.clock_drift_ns { |
| 111 | + *clock_drift_ns -= rhs; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl SP3Entry { |
| 118 | + /// Builds new [SP3Entry] with "true" position and all other |
| 119 | + /// fields are unknown. |
| 120 | + pub fn from_position_km(position_km: Vector3D) -> Self { |
| 121 | + Self { |
| 122 | + position_km, |
| 123 | + clock_us: None, |
| 124 | + maneuver: false, |
| 125 | + velocity_km_s: None, |
| 126 | + clock_drift_ns: None, |
| 127 | + clock_prediction: false, |
| 128 | + orbit_prediction: false, |
| 129 | + clock_event: false, |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /// Builds new [SP3Entry] with position prediction, in kilometers. |
| 134 | + pub fn from_predicted_position_km(position_km: Vector3D) -> Self { |
| 135 | + Self { |
| 136 | + position_km, |
| 137 | + clock_us: None, |
| 138 | + maneuver: false, |
| 139 | + velocity_km_s: None, |
| 140 | + clock_drift_ns: None, |
| 141 | + clock_prediction: false, |
| 142 | + orbit_prediction: true, |
| 143 | + clock_event: false, |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + /// Builds new [SP3Entry] with "true" position and velocity vector, |
| 148 | + /// any other fields are unknown. |
| 149 | + pub fn from_position_velocity_km_km_s(position_km: Vector3D, velocity_km_s: Vector3D) -> Self { |
| 150 | + Self { |
| 151 | + position_km, |
| 152 | + velocity_km_s: Some(velocity_km_s), |
| 153 | + clock_us: None, |
| 154 | + maneuver: false, |
| 155 | + clock_drift_ns: None, |
| 156 | + clock_prediction: false, |
| 157 | + orbit_prediction: false, |
| 158 | + clock_event: false, |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + /// Builds new [SP3Entry] with predicted position and velocity vectors, |
| 163 | + /// all other fields are unknown. |
| 164 | + pub fn from_predicted_position_velocity_km_km_s( |
| 165 | + position_km: Vector3D, |
| 166 | + velocity_km_s: Vector3D, |
| 167 | + ) -> Self { |
| 168 | + Self { |
| 169 | + position_km, |
| 170 | + clock_us: None, |
| 171 | + maneuver: false, |
| 172 | + clock_drift_ns: None, |
| 173 | + velocity_km_s: Some(velocity_km_s), |
| 174 | + clock_prediction: false, |
| 175 | + orbit_prediction: true, |
| 176 | + clock_event: false, |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /// Copies and returns [SP3Entry] with "true" position vector. |
| 181 | + pub fn with_position_km(&self, position_km: Vector3D) -> Self { |
| 182 | + let mut s = self.clone(); |
| 183 | + s.position_km = position_km; |
| 184 | + s.orbit_prediction = false; |
| 185 | + s |
| 186 | + } |
| 187 | + |
| 188 | + /// Copies and returns [SP3Entry] with predicted position vector. |
| 189 | + pub fn with_predicted_position_km(&self, position_km: Vector3D) -> Self { |
| 190 | + let mut s = self.clone(); |
| 191 | + s.position_km = position_km; |
| 192 | + s.orbit_prediction = true; |
| 193 | + s |
| 194 | + } |
| 195 | + |
| 196 | + /// Copies and returns [SP3Entry] with "true" velocity vector |
| 197 | + pub fn with_velocity_km_s(&self, velocity_km_s: Vector3D) -> Self { |
| 198 | + let mut s = self.clone(); |
| 199 | + s.velocity_km_s = Some(velocity_km_s); |
| 200 | + s.orbit_prediction = false; |
| 201 | + s |
| 202 | + } |
| 203 | + |
| 204 | + /// Copies and returns [SP3Entry] with predicted velocity vector |
| 205 | + pub fn with_predicted_velocity_km_s(&self, velocity_km_s: Vector3D) -> Self { |
| 206 | + let mut s = self.clone(); |
| 207 | + s.velocity_km_s = Some(velocity_km_s); |
| 208 | + s.orbit_prediction = true; |
| 209 | + s |
| 210 | + } |
| 211 | + |
| 212 | + /// Copies and returns [Self] with "true" clock offset in seconds |
| 213 | + pub fn with_clock_offset_s(&self, offset_s: f64) -> Self { |
| 214 | + let mut s = self.clone(); |
| 215 | + s.clock_us = Some(offset_s * 1.0E6); |
| 216 | + s.clock_prediction = false; |
| 217 | + s |
| 218 | + } |
| 219 | + |
| 220 | + /// Copies and returns [Self] with predicted clock offset in seconds |
| 221 | + pub fn with_predicted_clock_offset_s(&self, offset_s: f64) -> Self { |
| 222 | + let mut s = self.clone(); |
| 223 | + s.clock_us = Some(offset_s * 1.0E6); |
| 224 | + s.clock_prediction = true; |
| 225 | + s |
| 226 | + } |
| 227 | + |
| 228 | + /// Copies and returns [Self] with "true" clock offset in microseconds |
| 229 | + pub fn with_clock_offset_us(&self, offset_us: f64) -> Self { |
| 230 | + let mut s = self.clone(); |
| 231 | + s.clock_us = Some(offset_us); |
| 232 | + s.clock_prediction = false; |
| 233 | + s |
| 234 | + } |
| 235 | + |
| 236 | + /// Copies and returns [Self] with predicted clock offset in microseconds |
| 237 | + pub fn with_predicted_clock_offset_us(&self, offset_us: f64) -> Self { |
| 238 | + let mut s = self.clone(); |
| 239 | + s.clock_us = Some(offset_us); |
| 240 | + s.clock_prediction = true; |
| 241 | + s |
| 242 | + } |
| 243 | + |
| 244 | + /// Copies and returns [Self] with clock drift in seconds |
| 245 | + pub fn with_clock_drift_s(&self, drift_s: f64) -> Self { |
| 246 | + let mut s = self.clone(); |
| 247 | + s.clock_drift_ns = Some(drift_s * 1.0E9); |
| 248 | + s |
| 249 | + } |
| 250 | + |
| 251 | + /// Copies and returns [Self] with clock drift in nanoseconds |
| 252 | + pub fn with_clock_drift_ns(&self, drift_ns: f64) -> Self { |
| 253 | + let mut s = self.clone(); |
| 254 | + s.clock_drift_ns = Some(drift_ns); |
| 255 | + s |
| 256 | + } |
| 257 | +} |
0 commit comments