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
use std::{
fs::File,
io::{BufWriter, Write},
path::Path,
str::FromStr,
};
use itertools::Itertools;
use crate::{errors::FormattingError, prelude::SP3};
#[cfg(feature = "flate2")]
use flate2::{write::GzEncoder, Compression as GzCompression};
use hifitime::efmt::{Format, Formatter};
pub(crate) struct CoordsFormatter {
value: f64,
width: usize,
precision: usize,
}
impl CoordsFormatter {
pub fn coordinates(value: f64) -> Self {
Self {
value,
width: 13,
precision: 6,
}
}
pub fn fractional_mjd(value: f64) -> Self {
Self {
value,
width: 15,
precision: 13,
}
}
}
impl std::fmt::Display for CoordsFormatter {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let value = self.value;
let sign_str = if self.precision == 13 {
""
} else {
if value.is_sign_positive() {
" "
} else {
""
}
};
let formatted = if value.is_sign_positive() {
format!(
"{:width$.precision$}",
value,
width = self.width,
precision = self.precision
)
} else {
format!(
"{:width$.precision$}",
value,
width = self.width + 1,
precision = self.precision
)
};
write!(f, "{}{}", sign_str, formatted)
}
}
impl SP3 {
/// Formats [SP3] into writable I/O using efficient buffered writer
/// and following standard specifications.
pub fn format<W: Write>(&self, writer: &mut BufWriter<W>) -> Result<(), FormattingError> {
let efmt = Format::from_str("%Y %m %d %H %M %S.%f").unwrap();
self.header.format(writer)?;
for comment in self.comments.iter() {
writeln!(writer, "/* {}", comment)?;
}
for epoch in self.data.keys().map(|k| k.epoch).unique().sorted() {
let formatter = Formatter::new(epoch, efmt);
writeln!(writer, "* {}", formatter)?;
for key in self
.data
.keys()
.filter_map(|k| if k.epoch == epoch { Some(k) } else { None })
.unique()
.sorted()
{
if let Some(entry) = self.data.get(key) {
entry.format(key.sv, writer)?;
}
}
}
writeln!(writer, "EOF")?;
writer.flush()?;
Ok(())
}
/// Dumps [SP3] into writable local file (as readable ASCII UTF-8),
/// using efficient buffered formatting.
/// This is the mirror operation of [SP3::from_file]
/// ```
/// use sp3::prelude::*;
///
/// let sp3 = SP3::from_file("data/SP3/C/co108870.sp3").unwrap();
///
/// assert!(sp3.to_file("output.sp3").is_ok());
/// ```
pub fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), FormattingError> {
let fd = File::create(path)?;
let mut writer = BufWriter::new(fd);
self.format(&mut writer)?;
Ok(())
}
/// Dumps [SP3] into Gzip compressed file.
/// This is the [SP3::from_gzip_file] mirror operation.
/// ```
/// use sp3::prelude::*;
///
/// // Parse readable ASCII file
/// let sp3 = SP3::from_file("data/SP3/D/example.txt")
/// .unwrap();
///
/// // Dump and compress at the same time
/// assert!(sp3.to_gzip_file("output.sp3.gz").is_ok());
///
/// // Use our file name determination method
/// // NB: coming from plain/readable ASCII
/// let filename = format!(
/// "{}.gz",
/// sp3.standardized_filename(),
/// );
///
/// assert!(sp3.to_gzip_file(filename).is_ok());
///
/// // parse this file back
/// let _ = SP3::from_gzip_file("IGS0OPSRAP_20193000000_01D_05M_ORB.SP3.gz")
/// .unwrap();
/// ```
#[cfg(feature = "flate2")]
#[cfg_attr(docsrs, doc(cfg(feature = "flate2")))]
pub fn to_gzip_file<P: AsRef<Path>>(&self, path: P) -> Result<(), FormattingError> {
let fd = File::create(path)?;
let compression = GzCompression::new(5);
let mut writer = BufWriter::new(GzEncoder::new(fd, compression));
self.format(&mut writer)?;
Ok(())
}
}
#[cfg(test)]
mod test {
use crate::prelude::SP3;
#[test]
fn sp3_c_formatting() {
let sp3 = SP3::from_file("data/SP3/C/co108870.sp3").unwrap();
sp3.to_file("test-c.sp3").unwrap_or_else(|e| {
panic!("SP3/formatting issue: {}", e);
});
let parsed = SP3::from_file("test-c.sp3").unwrap_or_else(|e| {
panic!("SP3/failed to parse back: {}", e);
});
assert_eq!(parsed, sp3);
}
#[test]
fn sp3_d_formatting() {
let sp3 = SP3::from_file("data/SP3/D/example.txt").unwrap();
sp3.to_file("test-d.sp3").unwrap_or_else(|e| {
panic!("SP3/formatting issue: {}", e);
});
let _ = SP3::from_file("test-d.sp3").unwrap_or_else(|e| {
panic!("SP3/failed to parse back: {}", e);
});
// TODO: achieve this equality
// assert_eq!(parsed, sp3);
}
#[test]
fn sp3_c_predicted_orbits() {
let sp3 =
SP3::from_gzip_file("data/SP3/C/ESA0OPSULT_20232320600_02D_15M_ORB.SP3.gz").unwrap();
sp3.to_file("test-c-predicted.sp3").unwrap_or_else(|e| {
panic!("SP3/formatting issue: {}", e);
});
let _ = SP3::from_file("test-c-predicted.sp3").unwrap_or_else(|e| {
panic!("SP3/failed to parse back: {}", e);
});
// assert_eq!(parsed, sp3);
}
}