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
use super::Sv;
use std::collections::HashMap;
use strum_macros::EnumString;

pub mod record;
pub use record::{Map, Record};

pub mod grid;
pub use grid::{Grid, GridLinspace};

pub mod system;
pub use system::RefSystem;

#[cfg(feature = "serde")]
use serde::Serialize;

#[derive(Debug, Clone, PartialEq, PartialOrd, EnumString)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
/// Mapping function used in when determining this IONEX
pub enum MappingFunction {
    /// 1/cos(z)
    #[strum(serialize = "COSZ")]
    CosZ,
    /// Q-factor
    #[strum(serialize = "QFAC")]
    QFac,
}

/// Possible source of DCBs
#[derive(Debug, Clone, PartialEq, PartialOrd, Hash, Eq, EnumString)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum BiasSource {
    /// Referenced against a given vehicle
    SpaceVehicle(Sv),
    /// Referenced for an observation station on Earth
    Station(String),
}

/// `IONEX` specific header fields
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct HeaderFields {
    /// Reference system used for following TEC maps,
    /// cf. [system::RefSystem].
    pub reference: RefSystem,
    /// It is highly recommended to give a brief description
    /// of the technique, model.. description is not a
    /// general purpose comment
    pub description: Option<String>,
    /// Mapping function adopted for TEC determination,
    /// if None: No mapping function, e.g altimetry
    pub mapping: Option<MappingFunction>,
    /// Maps dimension, can either be a 2D (= fixed altitude mode), or 3D
    pub map_dimension: u8,
    /// Mean earth radius or bottom of height grid, in km.
    pub base_radius: f32,
    /// Reference rid definition.
    pub grid: grid::Grid,
    /// Minimum elevation angle filter used. In degrees.
    pub elevation_cutoff: f32,
    /// Verbose description of observables used in determination.
    /// When no Observables were used, that means we're based off a theoretical model.
    pub observables: Option<String>,
    /// Number of stations that contributed to following data
    pub nb_stations: u32,
    /// Number of satellites that contributed to following data
    pub nb_satellites: u32,
    /// exponent: scaling to apply in current TEC blocs
    pub exponent: i8,
    /// Differential Code Biases (DBCs),
    /// per Vehicle #PRN, (Bias and RMS bias) values.
    pub dcbs: HashMap<BiasSource, (f64, f64)>,
}

impl Default for HeaderFields {
    fn default() -> Self {
        Self {
            reference: RefSystem::default(),
            exponent: -1,     // very important: allows missing EXPONENT fields
            map_dimension: 2, // 2D map by default
            mapping: None,
            observables: None,
            description: None,
            elevation_cutoff: 0.0,
            base_radius: 0.0,
            grid: grid::Grid::default(),
            nb_stations: 0,
            nb_satellites: 0,
            dcbs: HashMap::new(),
        }
    }
}

impl HeaderFields {
    /// Copies and builds Self with given Reference System
    pub fn with_reference_system(&self, reference: RefSystem) -> Self {
        let mut s = self.clone();
        s.reference = reference;
        s
    }
    /// Copies and sets exponent / scaling to currently use
    pub fn with_exponent(&self, e: i8) -> Self {
        let mut s = self.clone();
        s.exponent = e;
        s
    }
    /// Copies and sets model description
    pub fn with_description(&self, desc: &str) -> Self {
        let mut s = self.clone();
        if let Some(ref mut d) = s.description {
            d.push_str(" ");
            d.push_str(desc)
        } else {
            s.description = Some(desc.to_string())
        }
        s
    }
    pub fn with_mapping_function(&self, mf: MappingFunction) -> Self {
        let mut s = self.clone();
        s.mapping = Some(mf);
        s
    }
    /// Copies & sets minimum elevation angle used.
    pub fn with_elevation_cutoff(&self, e: f32) -> Self {
        let mut s = self.clone();
        s.elevation_cutoff = e;
        s
    }
    pub fn with_observables(&self, o: &str) -> Self {
        let mut s = self.clone();
        if o.len() > 0 {
            s.observables = Some(o.to_string())
        }
        s
    }
    /// Returns true if this Ionosphere Maps describes
    /// a theoretical model, not measured data
    pub fn is_theoretical_model(&self) -> bool {
        self.observables.is_some()
    }
    /// Copies self and set Nb of stations
    pub fn with_nb_stations(&self, n: u32) -> Self {
        let mut s = self.clone();
        s.nb_stations = n;
        s
    }
    /// Copies self and set Nb of satellites
    pub fn with_nb_satellites(&self, n: u32) -> Self {
        let mut s = self.clone();
        s.nb_satellites = n;
        s
    }
    /// Copies & set Base Radius [km]
    pub fn with_base_radius(&self, b: f32) -> Self {
        let mut s = self.clone();
        s.base_radius = b;
        s
    }
    pub fn with_map_dimension(&self, d: u8) -> Self {
        let mut s = self.clone();
        s.map_dimension = d;
        s
    }
    /// Adds latitude grid definition
    pub fn with_latitude_grid(&self, grid: GridLinspace) -> Self {
        let mut s = self.clone();
        s.grid.latitude = grid;
        s
    }
    /// Adds longitude grid definition
    pub fn with_longitude_grid(&self, grid: GridLinspace) -> Self {
        let mut s = self.clone();
        s.grid.longitude = grid;
        s
    }
    /// Adds altitude grid definition
    pub fn with_altitude_grid(&self, grid: GridLinspace) -> Self {
        let mut s = self.clone();
        s.grid.height = grid;
        s
    }
    /// Copies & sets Diffenretial Code Bias estimates
    /// for given vehicle
    pub fn with_dcb(&self, src: BiasSource, value: (f64, f64)) -> Self {
        let mut s = self.clone();
        s.dcbs.insert(src, value);
        s
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::str::FromStr;
    #[test]
    fn test_mapping_func() {
        let content = "COSZ";
        let func = MappingFunction::from_str(content);
        assert_eq!(func.is_ok(), true);
        assert_eq!(func.unwrap(), MappingFunction::CosZ);
        let content = "QFAC";
        let func = MappingFunction::from_str(content);
        assert_eq!(func.is_ok(), true);
        assert_eq!(func.unwrap(), MappingFunction::QFac);
        let content = "DONT";
        let func = MappingFunction::from_str(content);
        assert_eq!(func.is_err(), true);
    }
}