nyx_space/md/param.rs
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
/*
Nyx, blazing fast astrodynamics
Copyright (C) 2018-onwards Christopher Rabotin <christopher.rabotin@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use super::NyxError;
use arrow::datatypes::{DataType, Field};
use core::fmt;
use enum_iterator::Sequence;
#[cfg(feature = "python")]
use pyo3::prelude::*;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, str::FromStr};
/// Common state parameters
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
#[derive(Copy, Clone, Debug, PartialEq, Sequence, Serialize, Deserialize)]
#[cfg_attr(feature = "python", pyclass)]
pub enum StateParameter {
/// Argument of Latitude (deg)
AoL,
/// Argument of Periapse (deg)
AoP,
/// Apoapsis, shortcut for TA == 180.0
Apoapsis,
/// Radius of apoapsis (km)
ApoapsisRadius,
/// B-Plane B⋅R
BdotR,
/// B-Plane B⋅T
BdotT,
/// B-Plane LTOF
BLTOF,
/// C_3 in (km/s)^2
C3,
/// Coefficient of drag
Cd,
/// Coefficient of reflectivity
Cr,
/// Declination (deg) (also called elevation if in a body fixed frame)
Declination,
/// Dry mass (kg)
DryMass,
/// The epoch of the state
Epoch,
/// Eccentric anomaly (deg)
EccentricAnomaly,
/// Eccentricity (no unit)
Eccentricity,
/// Specific energy
Energy,
/// Flight path angle (deg)
FlightPathAngle,
/// fuel mass in kilograms
FuelMass,
/// Geodetic height (km)
Height,
/// Geodetic latitude (deg)
Latitude,
/// Geodetic longitude (deg)
Longitude,
/// Return the guidance mode of the spacecraft
GuidanceMode,
/// Orbital momentum
Hmag,
/// X component of the orbital momentum vector
HX,
/// Y component of the orbital momentum vector
HY,
/// Z component of the orbital momentum vector
HZ,
/// Hyperbolic anomaly (deg), only valid for hyperbolic orbits
HyperbolicAnomaly,
/// Inclination (deg)
Inclination,
/// Specific impulse (isp) in seconds
Isp,
/// Mean anomaly (deg)
MeanAnomaly,
/// Periapsis, shortcut for TA == 0.0
Periapsis,
/// Radius of periapse (km)
PeriapsisRadius,
/// Orbital period (s)
Period,
/// Right ascension (deg)
RightAscension,
/// Right ascension of the ascending node (deg)
RAAN,
/// Norm of the radius vector
Rmag,
/// Semi parameter (km)
SemiParameter,
/// Semi major axis (km)
SMA,
/// Semi minor axis (km)
SemiMinorAxis,
/// Thrust (Newtons)
Thrust,
/// True anomaly
TrueAnomaly,
/// True longitude
TrueLongitude,
/// Velocity declination (deg)
VelocityDeclination,
/// Norm of the velocity vector (km/s)
Vmag,
/// X component of the radius (km)
X,
/// Y component of the radius (km)
Y,
/// Z component of the radius (km)
Z,
/// X component of the velocity (km/s)
VX,
/// Y component of the velocity (km/s)
VY,
/// Z component of the velocity (km/s)
VZ,
}
#[cfg_attr(feature = "python", pymethods)]
impl StateParameter {
/// Returns the default event finding precision in the unit of that parameter
pub fn default_event_precision(&self) -> f64 {
match self {
Self::Eccentricity => 1e-5,
// Non anomaly angles
Self::AoL
| Self::AoP
| Self::Declination
| Self::Latitude
| Self::Longitude
| Self::FlightPathAngle
| Self::Inclination
| Self::RightAscension
| Self::RAAN
| Self::TrueLongitude
| Self::VelocityDeclination => 1e-1,
// Anomaly angles
Self::Apoapsis
| Self::Periapsis
| Self::MeanAnomaly
| Self::EccentricAnomaly
| Self::HyperbolicAnomaly
| Self::TrueAnomaly => 1e-3,
// Distances
Self::ApoapsisRadius
| Self::BdotR
| Self::BdotT
| Self::Height
| Self::Hmag
| Self::HX
| Self::HY
| Self::HZ
| Self::PeriapsisRadius
| Self::Rmag
| Self::SemiParameter
| Self::SMA
| Self::SemiMinorAxis
| Self::X
| Self::Y
| Self::Z => 1e-3,
// Velocities
Self::C3 | Self::VX | Self::VY | Self::VZ | Self::Vmag => 1e-3,
// Special
Self::Energy => 1e-3,
Self::DryMass | Self::FuelMass => 1e-3,
Self::Period => 1e-1,
_ => unimplemented!("{self} cannot be used for event finding"),
}
}
/// Returns whether this parameter is of the B-Plane kind
pub const fn is_b_plane(&self) -> bool {
matches!(&self, Self::BdotR | Self::BdotT | Self::BLTOF)
}
/// Returns whether this is an orbital parameter
pub const fn is_orbital(&self) -> bool {
!self.is_for_spacecraft() && !matches!(self, Self::Apoapsis | Self::Periapsis | Self::Epoch)
}
/// Returns whether this parameter is only applicable to a spacecraft state
pub const fn is_for_spacecraft(&self) -> bool {
matches!(
&self,
Self::DryMass
| Self::FuelMass
| Self::Cr
| Self::Cd
| Self::Isp
| Self::GuidanceMode
| Self::Thrust
)
}
pub const fn unit(&self) -> &'static str {
match self {
// Angles
Self::AoL
| Self::AoP
| Self::Declination
| Self::Latitude
| Self::Longitude
| Self::FlightPathAngle
| Self::Inclination
| Self::RightAscension
| Self::RAAN
| Self::TrueLongitude
| Self::VelocityDeclination
| Self::Apoapsis
| Self::Periapsis
| Self::MeanAnomaly
| Self::EccentricAnomaly
| Self::HyperbolicAnomaly
| Self::TrueAnomaly => "deg",
// Distances
Self::ApoapsisRadius
| Self::BdotR
| Self::BdotT
| Self::Height
| Self::Hmag
| Self::HX
| Self::HY
| Self::HZ
| Self::PeriapsisRadius
| Self::Rmag
| Self::SemiParameter
| Self::SMA
| Self::SemiMinorAxis
| Self::X
| Self::Y
| Self::Z => "km",
// Velocities
Self::VX | Self::VY | Self::VZ | Self::Vmag => "km/s",
Self::C3 | Self::Energy => "km^2/s^2",
Self::DryMass | Self::FuelMass => "kg",
Self::Isp => "isp",
Self::Thrust => "N",
_ => "",
}
}
/// Prints this orbit in Keplerian form
#[cfg(feature = "python")]
fn __str__(&self) -> String {
format!("{self}")
}
#[cfg(feature = "python")]
#[new]
fn py_new(name: String) -> Result<Self, NyxError> {
Self::from_str(&name)
}
}
impl StateParameter {
/// Returns the parquet field of this parameter
pub(crate) fn to_field(self, more_meta: Option<Vec<(String, String)>>) -> Field {
self.to_field_generic(false, more_meta)
}
/// Returns the parquet field of this parameter
pub(crate) fn to_cov_field(self, more_meta: Option<Vec<(String, String)>>) -> Field {
self.to_field_generic(true, more_meta)
}
/// Returns the parquet field of this parameter
fn to_field_generic(self, is_sigma: bool, more_meta: Option<Vec<(String, String)>>) -> Field {
let mut meta = HashMap::new();
meta.insert("unit".to_string(), self.unit().to_string());
if let Some(more_data) = more_meta {
for (k, v) in more_data {
meta.insert(k, v);
}
}
Field::new(
if is_sigma {
format!("Sigma {self}")
} else {
format!("{self}")
},
if self == Self::GuidanceMode {
DataType::Utf8
} else {
DataType::Float64
},
false,
)
.with_metadata(meta)
}
}
impl FromStr for StateParameter {
type Err = NyxError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let keyword = s.split_whitespace().next().ok_or(NyxError::LoadingError {
msg: format!("Unknown state parameter: {s}"),
})?;
match keyword.to_lowercase().as_str() {
"apoapsis" => Ok(Self::Apoapsis),
"periapsis" => Ok(Self::Periapsis),
"aol" => Ok(Self::AoL),
"aop" => Ok(Self::AoP),
"bltof" => Ok(Self::BLTOF),
"bdotr" => Ok(Self::BdotR),
"bdott" => Ok(Self::BdotT),
"c3" => Ok(Self::C3),
"cd" => Ok(Self::Cd),
"cr" => Ok(Self::Cr),
"declin" => Ok(Self::Declination),
"dry_mass" => Ok(Self::DryMass),
"apoapsis_radius" => Ok(Self::ApoapsisRadius),
"ea" => Ok(Self::EccentricAnomaly),
"ecc" => Ok(Self::Eccentricity),
"energy" => Ok(Self::Energy),
"fpa" => Ok(Self::FlightPathAngle),
"fuel_mass" => Ok(Self::FuelMass),
"guidance_mode" | "mode" => Ok(Self::GuidanceMode),
"geodetic_height" => Ok(Self::Height),
"geodetic_latitude" => Ok(Self::Latitude),
"geodetic_longitude" => Ok(Self::Longitude),
"ha" => Ok(Self::HyperbolicAnomaly),
"hmag" => Ok(Self::Hmag),
"hx" => Ok(Self::HX),
"hy" => Ok(Self::HY),
"hz" => Ok(Self::HZ),
"inc" => Ok(Self::Inclination),
"isp" => Ok(Self::Isp),
"ma" => Ok(Self::MeanAnomaly),
"periapsis_radius" => Ok(Self::PeriapsisRadius),
"period" => Ok(Self::Period),
"right_asc" => Ok(Self::RightAscension),
"raan" => Ok(Self::RAAN),
"rmag" => Ok(Self::Rmag),
"semi_parameter" => Ok(Self::SemiParameter),
"semi_minor" => Ok(Self::SemiMinorAxis),
"sma" => Ok(Self::SMA),
"ta" => Ok(Self::TrueAnomaly),
"tlong" => Ok(Self::TrueLongitude),
"thrust" => Ok(Self::Thrust),
"vdeclin" => Ok(Self::VelocityDeclination),
"vmag" => Ok(Self::Vmag),
"x" => Ok(Self::X),
"y" => Ok(Self::Y),
"z" => Ok(Self::Z),
"vx" => Ok(Self::VX),
"vy" => Ok(Self::VY),
"vz" => Ok(Self::VZ),
_ => Err(NyxError::LoadingError {
msg: format!("Unknown state parameter: {s}"),
}),
}
}
}
impl fmt::Display for StateParameter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let repr = match *self {
Self::Apoapsis => "apoapsis",
Self::Periapsis => "periapsis",
Self::AoL => "aol",
Self::AoP => "aop",
Self::BLTOF => "BLToF",
Self::BdotR => "BdotR",
Self::BdotT => "BdotT",
Self::C3 => "c3",
Self::Cd => "cd",
Self::Cr => "cr",
Self::Declination => "declin",
Self::DryMass => "dry_mass",
Self::Epoch => "epoch",
Self::ApoapsisRadius => "apoapsis_radius",
Self::EccentricAnomaly => "ea",
Self::Eccentricity => "ecc",
Self::Energy => "energy",
Self::FlightPathAngle => "fpa",
Self::FuelMass => "fuel_mass",
Self::GuidanceMode => "guidance_mode",
Self::Height => "geodetic_height",
Self::Latitude => "geodetic_latitude",
Self::Longitude => "geodetic_longitude",
Self::HyperbolicAnomaly => "ha",
Self::Hmag => "hmag",
Self::HX => "hx",
Self::HY => "hy",
Self::HZ => "hz",
Self::Inclination => "inc",
Self::Isp => "isp",
Self::MeanAnomaly => "ma",
Self::PeriapsisRadius => "periapsis_radius",
Self::Period => "period",
Self::RightAscension => "right_asc",
Self::RAAN => "raan",
Self::Rmag => "rmag",
Self::SemiParameter => "semi_parameter",
Self::SemiMinorAxis => "semi_minor",
Self::SMA => "sma",
Self::Thrust => "thrust",
Self::TrueAnomaly => "ta",
Self::TrueLongitude => "tlong",
Self::VelocityDeclination => "vdeclin",
Self::Vmag => "vmag",
Self::X => "x",
Self::Y => "y",
Self::Z => "z",
Self::VX => "vx",
Self::VY => "vy",
Self::VZ => "vz",
// _ => &default,
};
let unit = if self.unit().is_empty() {
String::new()
} else {
format!(" ({})", self.unit())
};
write!(f, "{repr}{unit}")
}
}
#[cfg(test)]
mod ut_state_param {
use super::{FromStr, StateParameter};
#[test]
fn test_str_to_from() {
for s in [
StateParameter::Apoapsis,
StateParameter::Periapsis,
StateParameter::AoL,
StateParameter::AoP,
StateParameter::BdotR,
StateParameter::BdotT,
StateParameter::BLTOF,
StateParameter::C3,
StateParameter::Cd,
StateParameter::Cr,
StateParameter::Declination,
StateParameter::DryMass,
StateParameter::ApoapsisRadius,
StateParameter::EccentricAnomaly,
StateParameter::Eccentricity,
StateParameter::Energy,
StateParameter::FlightPathAngle,
StateParameter::FuelMass,
StateParameter::GuidanceMode,
StateParameter::Height,
StateParameter::Latitude,
StateParameter::Longitude,
StateParameter::HyperbolicAnomaly,
StateParameter::Hmag,
StateParameter::HX,
StateParameter::HY,
StateParameter::HZ,
StateParameter::Inclination,
StateParameter::Isp,
StateParameter::MeanAnomaly,
StateParameter::PeriapsisRadius,
StateParameter::Period,
StateParameter::RightAscension,
StateParameter::RAAN,
StateParameter::Rmag,
StateParameter::SemiParameter,
StateParameter::SemiMinorAxis,
StateParameter::SMA,
StateParameter::Thrust,
StateParameter::TrueAnomaly,
StateParameter::TrueLongitude,
StateParameter::VelocityDeclination,
StateParameter::Vmag,
StateParameter::X,
StateParameter::Y,
StateParameter::Z,
StateParameter::VX,
StateParameter::VY,
StateParameter::VZ,
] {
let as_str = format!("{s}");
println!("{as_str}");
let loaded = StateParameter::from_str(&as_str).unwrap();
assert_eq!(loaded, s);
}
}
}