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
#[cfg(not(feature = "object_store"))]
use std::path::PathBuf;
#[cfg(feature = "object_store")]
use std::sync::Arc;
use crate::{Counter, DomeSeeing, DomeSeeingData, DomeSeeingError, OpdMapping};
#[cfg(feature = "object_store")]
use futures::TryStreamExt;
#[cfg(feature = "object_store")]
use object_store::{ObjectStore, path::Path};
type Result<T> = std::result::Result<T, DomeSeeingError>;
#[derive(Default, Debug)]
pub struct DomeSeeingBuilder {
#[cfg(not(feature = "object_store"))]
pub(crate) cfd_case_path: PathBuf,
#[cfg(feature = "object_store")]
pub(crate) cfd_case_path: Path,
pub(crate) upsampling: Option<usize>,
pub(crate) take: Option<usize>,
pub(crate) cycle: bool,
#[cfg(feature = "object_store")]
pub(crate) store: Option<Arc<dyn ObjectStore>>,
}
impl DomeSeeingBuilder {
/// Sets the ratio (>=1) between the desired OPD sampling frequency and the CFD sampling frequency (usually 5Hz)
pub fn upsampling_ratio(mut self, upsampling_ratio: usize) -> Self {
self.upsampling = Some(upsampling_ratio);
self
}
/// Sets the size of the dome seeing sample
pub fn sample_size(mut self, size: usize) -> Self {
self.take = Some(size);
self
}
/// Cycles though the dome seeing OPD back and forth
pub fn cycle(mut self) -> Self {
self.cycle = true;
self
}
#[cfg(feature = "object_store")]
/// Sets the remote storage where the dome seeing data is saved
pub fn store(mut self, store: impl ObjectStore) -> Self {
self.store = Some(Arc::new(store));
self
}
#[cfg(not(feature = "object_store"))]
/// Creates a new [DomeSeeing] instance
pub fn build(self) -> Result<DomeSeeing> {
use glob::glob;
let mut data: Vec<DomeSeeingData> = Vec::with_capacity(2005);
for entry in glob(
self.cfd_case_path
.join("optvol")
.join(if cfg!(feature = "bincode") {
"optvol_optvol_*.bin"
} else {
"optvol_optvol_*.npz"
})
.as_os_str()
.to_str()
.unwrap(),
)? {
let file = entry?;
let time_stamp = file
.file_stem()
// .and_then(|x| file_stem())
.and_then(|x| x.to_str())
.and_then(|x| x.split("_").last())
.and_then(|x| {
Some(
x.parse::<f64>()
.map_err(|e| DomeSeeingError::TimeStamp(e, x.into())),
)
})
.transpose()?
.unwrap();
// .expect("failed to parse dome seeing time stamp");
data.push(DomeSeeingData { time_stamp, file });
}
data.sort_by(|a, b| a.time_stamp.partial_cmp(&b.time_stamp).unwrap());
let counter = match (self.take, self.cycle) {
(None, true) => Box::new(
(0..data.len())
.chain((0..data.len()).skip(1).rev().skip(1))
.cycle(),
) as Counter,
(None, false) => {
if data.len() > 1 {
Box::new(0..data.len()) as Counter
} else {
Box::new([0usize, 0].into_iter()) as Counter
}
}
(Some(take), true) => Box::new(
(0..data.len())
.chain((0..data.len()).skip(1).rev().skip(1))
.cycle()
.take(take),
) as Counter,
(Some(take), false) => Box::new((0..data.len()).take(take)) as Counter,
};
// let counter = if let Some(take) = self.take {
// Box::new(
// (0..data.len())
// .chain((0..data.len()).skip(1).rev().skip(1))
// .cycle()
// .take(take),
// ) as Counter
// } else {
// Box::new(
// .file_stem()
// (0..data.len())
// .chain((0..data.len()).skip(1).rev().skip(1))
// .cycle(),
// ) as Counter
// };
let mut this = DomeSeeing {
upsampling: self.upsampling.unwrap_or(1),
data,
counter,
i: 0,
y1: Default::default(),
y2: Default::default(),
mapping: OpdMapping::Whole,
opd: None,
#[cfg(feature = "object_store")]
store: self.store,
};
if let Some(c) = this.counter.next() {
// let y2: Opd = bincode::deserialize_from(&File::open(&data[c].file)?)?;
//dbg!(y2.values.len());
//dbg!(y2.mask.len());
this.y2 = this.get(c)?;
};
Ok(this)
}
#[cfg(feature = "object_store")]
/// Creates a new [DomeSeeing] instance
pub async fn build(self) -> Result<DomeSeeing> {
let mut data: Vec<DomeSeeingData> = Vec::with_capacity(2005);
let path = object_store::path::Path::from(format!("{}/optvol", self.cfd_case_path));
let mut objects = self
.store
.as_ref()
.ok_or(DomeSeeingError::MissingStore)?
.list(Some(&path));
while let Some(object) = objects.try_next().await.unwrap() {
let path = object.location;
if path.to_string().ends_with(".npz") {
let time_stamp = path
.filename()
.and_then(|x| x.strip_suffix(".npz"))
.and_then(|x| x.split("_").last())
.and_then(|x| {
Some(
x.parse::<f64>()
.map_err(|e| DomeSeeingError::TimeStamp(e, x.into())),
)
})
.transpose()?
.unwrap();
data.push(DomeSeeingData {
time_stamp,
file: path,
});
}
}
if data.is_empty() {
return Err(DomeSeeingError::MissingData(path.into()));
}
data.sort_by(|a, b| a.time_stamp.partial_cmp(&b.time_stamp).unwrap());
let counter = match (self.take, self.cycle) {
(None, true) => Box::new(
(0..data.len())
.chain((0..data.len()).skip(1).rev().skip(1))
.cycle(),
) as Counter,
(None, false) => Box::new(0..data.len()) as Counter,
(Some(take), true) => Box::new(
(0..data.len())
.chain((0..data.len()).skip(1).rev().skip(1))
.cycle()
.take(take),
) as Counter,
(Some(take), false) => Box::new((0..data.len()).take(take)) as Counter,
};
// let counter = if let Some(take) = self.take {
// Box::new(
// (0..data.len())
// .chain((0..data.len()).skip(1).rev().skip(1))
// .cycle()
// .take(take),
// ) as Counter
// } else {
// Box::new(
// (0..data.len())
// .chain((0..data.len()).skip(1).rev().skip(1))
// .cycle(),
// ) as Counter
// };
let mut this = DomeSeeing {
upsampling: self.upsampling.unwrap_or(1),
data,
counter,
i: 0,
y1: Default::default(),
y2: Default::default(),
mapping: OpdMapping::Whole,
opd: None,
#[cfg(feature = "object_store")]
store: self.store,
};
if let Some(c) = this.counter.next() {
// let y2: Opd = bincode::deserialize_from(&File::open(&data[c].file)?)?;
//dbg!(y2.values.len());
//dbg!(y2.mask.len());
this.y2 = this.get(c).await?;
};
Ok(this)
}
}