Skip to main content

nyx_space/od/msr/
types.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 anise::astro::AzElRange;
20use arrow::datatypes::{DataType, Field};
21use serde::{Deserialize, Serialize};
22use std::{collections::HashMap, str::FromStr};
23
24use crate::{io::InputOutputError, od::ODError};
25
26#[cfg(feature = "python")]
27use pyo3::prelude::*;
28
29#[cfg_attr(feature = "python", pyclass(from_py_object))]
30#[derive(Copy, Clone, Debug, Hash, Serialize, Deserialize, PartialEq, Eq, der::Enumerated)]
31#[repr(u8)]
32pub enum MeasurementType {
33    #[serde(rename = "range_km")]
34    Range = 0,
35    #[serde(rename = "doppler_km_s")]
36    Doppler = 1,
37    #[serde(rename = "azimuth_deg")]
38    Azimuth = 2,
39    #[serde(rename = "elevation_deg")]
40    Elevation = 3,
41    #[serde(rename = "receive_freq")]
42    ReceiveFrequency = 4,
43    #[serde(rename = "transmit_freq")]
44    TransmitFrequency = 5,
45    #[serde(rename = "transmit_freq_rate")]
46    TransmitFrequencyRate = 9,
47    #[serde(rename = "x")]
48    X = 6,
49    #[serde(rename = "y")]
50    Y = 7,
51    #[serde(rename = "z")]
52    Z = 8,
53}
54
55impl MeasurementType {
56    /// Returns the expected unit of this measurement type
57    pub fn unit(self) -> &'static str {
58        match self {
59            Self::Range => "km",
60            Self::Doppler => "km/s",
61            Self::Azimuth | Self::Elevation => "deg",
62            Self::ReceiveFrequency | Self::TransmitFrequency => "Hz",
63            Self::TransmitFrequencyRate => "Hz/s",
64            Self::X | Self::Y | Self::Z => "km",
65        }
66    }
67
68    /// Returns true if this measurement type could be a two-way measurement.
69    pub(crate) fn may_be_two_way(self) -> bool {
70        match self {
71            MeasurementType::Range | MeasurementType::Doppler => true,
72            MeasurementType::Azimuth
73            | MeasurementType::Elevation
74            | MeasurementType::ReceiveFrequency
75            | MeasurementType::TransmitFrequency
76            | MeasurementType::TransmitFrequencyRate
77            | MeasurementType::X
78            | MeasurementType::Y
79            | MeasurementType::Z => false,
80        }
81    }
82
83    /// Returns the fields for this kind of measurement. The metadata includes a `unit` field with the unit.
84    /// Column is nullable in case there is no such measurement at a given epoch.
85    pub fn to_field(&self) -> Field {
86        let mut meta = HashMap::new();
87        meta.insert("unit".to_string(), self.unit().to_string());
88
89        Field::new(
90            format!("{self:?} ({})", self.unit()),
91            DataType::Float64,
92            true,
93        )
94        .with_metadata(meta)
95    }
96
97    /// Computes the one way measurement from an AER object and the noise of this measurement type, returned in the units of this measurement type.
98    pub fn compute_one_way(self, aer: AzElRange, noise: f64) -> Result<f64, ODError> {
99        match self {
100            Self::Range => Ok(aer.range_km + noise),
101            Self::Doppler => Ok(aer.range_rate_km_s + noise),
102            Self::Azimuth => Ok(aer.azimuth_deg + noise),
103            Self::Elevation => Ok(aer.elevation_deg + noise),
104            Self::ReceiveFrequency | Self::TransmitFrequency | Self::TransmitFrequencyRate => {
105                Err(ODError::MeasurementSimError {
106                    details: format!("{self:?} is only supported in CCSDS TDM parsing"),
107                })
108            }
109            Self::X | Self::Y | Self::Z => Err(ODError::MeasurementSimError {
110                details: format!("{self:?} must be computed directly from the state"),
111            }),
112        }
113    }
114
115    /// Computes the two way measurement from two AER values and the noise of this measurement type, returned in the units of this measurement type.
116    /// Two way is modeled by averaging the measurement in between both times, and adding the noise divided by sqrt(2).
117    pub fn compute_two_way(
118        self,
119        aer_t0: AzElRange,
120        aer_t1: AzElRange,
121        noise: f64,
122    ) -> Result<f64, ODError> {
123        match self {
124            Self::Range => {
125                let range_km = (aer_t1.range_km + aer_t0.range_km) * 0.5;
126                Ok(range_km + noise / 2.0_f64.sqrt())
127            }
128            Self::Doppler => {
129                let doppler_km_s = (aer_t1.range_rate_km_s + aer_t0.range_rate_km_s) * 0.5;
130                Ok(doppler_km_s + noise / 2.0_f64.sqrt())
131            }
132            Self::Azimuth => {
133                let az_deg = (aer_t1.azimuth_deg + aer_t0.azimuth_deg) * 0.5;
134                Ok(az_deg + noise / 2.0_f64.sqrt())
135            }
136            Self::Elevation => {
137                let el_deg = (aer_t1.elevation_deg + aer_t0.elevation_deg) * 0.5;
138                Ok(el_deg + noise / 2.0_f64.sqrt())
139            }
140            Self::ReceiveFrequency | Self::TransmitFrequency | Self::TransmitFrequencyRate => {
141                Err(ODError::MeasurementSimError {
142                    details: format!("{self:?} is only supported in CCSDS TDM parsing"),
143                })
144            }
145            Self::X | Self::Y | Self::Z => Err(ODError::MeasurementSimError {
146                details: format!("{self:?} is not supported for two way measurements"),
147            }),
148        }
149    }
150
151    /// Returns the CCSDS TDM name for this measurement type.
152    pub fn ccsds_tdm_name(&self) -> &str {
153        match self {
154            MeasurementType::Range => "RANGE",
155            MeasurementType::Doppler => "DOPPLER_INTEGRATED",
156            MeasurementType::Azimuth => "ANGLE_1",
157            MeasurementType::Elevation => "ANGLE_2",
158            MeasurementType::ReceiveFrequency => "RECEIVE_FREQ",
159            MeasurementType::TransmitFrequency => "TRANSMIT_FREQ",
160            MeasurementType::TransmitFrequencyRate => "TRANSMIT_FREQ_RATE",
161            MeasurementType::X => "X",
162            MeasurementType::Y => "Y",
163            MeasurementType::Z => "Z",
164        }
165    }
166}
167
168impl FromStr for MeasurementType {
169    type Err = InputOutputError;
170
171    fn from_str(s: &str) -> Result<Self, Self::Err> {
172        match s.to_lowercase().as_str() {
173            "range" => Ok(Self::Range),
174            "doppler" => Ok(Self::Doppler),
175            "azimuth" => Ok(Self::Azimuth),
176            "elevation" => Ok(Self::Elevation),
177            "x" => Ok(Self::X),
178            "y" => Ok(Self::Y),
179            "z" => Ok(Self::Z),
180            _ => Err(InputOutputError::UnsupportedData {
181                which: s.to_string(),
182            }),
183        }
184    }
185}