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
extern crate crossbeam;
extern crate csv;
use self::crossbeam::thread;
use crate::io::scenario::StateSerde;
use crate::md::Ephemeris;
use crate::{
celestia::{Cosm, Frame},
NyxError,
};
use std::sync::{mpsc::channel, Arc};
pub fn traj_from_csv(
filename: &str,
default_frame: Option<Frame>,
cosm: Arc<Cosm>,
) -> Result<Ephemeris, NyxError> {
match csv::Reader::from_path(filename) {
Ok(mut rdr) => {
thread::scope(|s| {
let (tx, rx) = channel();
let mut first = true;
let traj_thread;
let this_frame;
let first_result: Result<StateSerde, _> = rdr.deserialize().next().unwrap();
match first_result {
Ok(record) => {
this_frame = match &record.frame {
Some(f) => cosm.try_frame(&f)?,
None => default_frame.unwrap(),
};
match record.as_state(this_frame) {
Ok(state) => {
traj_thread =
s.spawn(move |_| Ephemeris::new_bucket_as(state, 8, rx));
}
Err(e) => {
return Err(NyxError::LoadingError(format!(
"Could not parse first state: {:?}",
e
)))
}
};
}
Err(e) => return Err(NyxError::LoadingError(e.to_string())),
}
for (lno, result) in rdr.deserialize().enumerate() {
let maybe_result: Result<StateSerde, _> = result;
match maybe_result {
Ok(record) => {
match record.as_state(this_frame) {
Ok(state) => {
if first {
first = false;
continue;
}
tx.send(state).unwrap()
}
Err(e) => {
return Err(NyxError::LoadingError(format!(
"Could not parse state on line {}: {:?}",
lno + 2,
e
)))
}
};
}
Err(e) => return Err(NyxError::LoadingError(e.to_string())),
}
}
let traj = traj_thread.join().unwrap_or_else(|_| {
Err(NyxError::NoInterpolationData(
"Could not generate trajectory".to_string(),
))
})?;
Ok(traj)
})
.unwrap()
}
Err(e) => Err(NyxError::LoadingError(e.to_string())),
}
}