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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
use crate::gpubox_files::GpuboxTimeMap;
use crate::voltage_files::VoltageFileTimeMap;
pub mod error;
use crate::*;
use error::CoarseChannelError;
use std::fmt;
#[cfg(test)]
mod test;
#[derive(Clone)]
pub struct CoarseChannel {
pub corr_chan_number: usize,
pub rec_chan_number: usize,
pub gpubox_number: usize,
pub chan_width_hz: u32,
pub chan_start_hz: u32,
pub chan_centre_hz: u32,
pub chan_end_hz: u32,
}
impl CoarseChannel {
pub(crate) fn new(
corr_chan_number: usize,
rec_chan_number: usize,
gpubox_number: usize,
coarse_chan_width_hz: u32,
) -> Self {
let centre_chan_hz: u32 = (rec_chan_number as u32) * coarse_chan_width_hz;
Self {
corr_chan_number,
rec_chan_number,
gpubox_number,
chan_width_hz: coarse_chan_width_hz,
chan_centre_hz: centre_chan_hz,
chan_start_hz: centre_chan_hz - (coarse_chan_width_hz / 2),
chan_end_hz: centre_chan_hz + (coarse_chan_width_hz / 2),
}
}
fn get_metafits_coarse_chan_array(metafits_coarse_chans_string: &str) -> Vec<usize> {
metafits_coarse_chans_string
.replace(&['\'', '&'][..], "")
.split(',')
.map(|s| s.parse().unwrap())
.collect()
}
pub(crate) fn get_metafits_coarse_channel_info(
metafits_fptr: &mut fitsio::FitsFile,
hdu: &fitsio::hdu::FitsHdu,
observation_bandwidth_hz: u32,
) -> Result<(Vec<usize>, u32), FitsError> {
let coarse_chans_string =
get_required_fits_key_long_string!(metafits_fptr, hdu, "CHANNELS")?;
let coarse_chan_vec = Self::get_metafits_coarse_chan_array(&coarse_chans_string);
let coarse_chan_width_hz = observation_bandwidth_hz / coarse_chan_vec.len() as u32;
Ok((coarse_chan_vec, coarse_chan_width_hz))
}
pub(crate) fn populate_coarse_channels(
corr_version: metafits_context::CorrelatorVersion,
metafits_coarse_chan_vec: &[usize],
metafits_coarse_chan_width_hz: u32,
gpubox_time_map: Option<&GpuboxTimeMap>,
voltage_time_map: Option<&VoltageFileTimeMap>,
) -> Result<Vec<Self>, MwalibError> {
if gpubox_time_map.is_some() && voltage_time_map.is_some() {
return Err(MwalibError::CoarseChannel(
CoarseChannelError::BothGpuboxAndVoltageTimeMapSupplied,
));
}
let num_coarse_chans = metafits_coarse_chan_vec.len();
let mut coarse_chans: Vec<CoarseChannel> = Vec::new();
let mut first_chan_index_over_128: Option<usize> = None;
for (i, rec_chan_number) in metafits_coarse_chan_vec.iter().enumerate() {
let mut correlator_chan_number = i;
match corr_version {
CorrelatorVersion::Legacy | CorrelatorVersion::OldLegacy => {
if *rec_chan_number > 128 {
if first_chan_index_over_128.is_none() {
first_chan_index_over_128 = Some(i);
}
correlator_chan_number =
(num_coarse_chans - 1) - (i - first_chan_index_over_128.unwrap_or(0));
}
let gpubox_chan_number = correlator_chan_number + 1;
match gpubox_time_map {
Some(g) => {
if let Some((_, channel_map)) = g.iter().next() {
if channel_map.contains_key(&gpubox_chan_number) {
coarse_chans.push(CoarseChannel::new(
correlator_chan_number,
*rec_chan_number,
gpubox_chan_number,
metafits_coarse_chan_width_hz,
))
}
}
}
_ => match voltage_time_map {
Some(v) => {
if let Some((_, channel_map)) = v.iter().next() {
if channel_map.contains_key(&rec_chan_number) {
coarse_chans.push(CoarseChannel::new(
correlator_chan_number,
*rec_chan_number,
*rec_chan_number,
metafits_coarse_chan_width_hz,
))
}
}
}
_ => coarse_chans.push(CoarseChannel::new(
correlator_chan_number,
*rec_chan_number,
gpubox_chan_number,
metafits_coarse_chan_width_hz,
)),
},
}
}
CorrelatorVersion::V2 => {
match gpubox_time_map {
Some(g) => {
if let Some((_, channel_map)) = g.iter().next() {
if channel_map.contains_key(&rec_chan_number) {
coarse_chans.push(CoarseChannel::new(
correlator_chan_number,
*rec_chan_number,
*rec_chan_number,
metafits_coarse_chan_width_hz,
))
}
}
}
_ => match voltage_time_map {
Some(v) => {
if let Some((_, channel_map)) = v.iter().next() {
if channel_map.contains_key(&rec_chan_number) {
coarse_chans.push(CoarseChannel::new(
correlator_chan_number,
*rec_chan_number,
*rec_chan_number,
metafits_coarse_chan_width_hz,
))
}
}
}
_ => coarse_chans.push(CoarseChannel::new(
correlator_chan_number,
*rec_chan_number,
*rec_chan_number,
metafits_coarse_chan_width_hz,
)),
},
}
}
}
}
coarse_chans.sort_by(|a, b| a.rec_chan_number.cmp(&b.rec_chan_number));
Ok(coarse_chans)
}
}
impl fmt::Debug for CoarseChannel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"gpu={} corr={} rec={} @ {:.3} MHz",
self.gpubox_number,
self.corr_chan_number,
self.rec_chan_number,
self.chan_centre_hz as f32 / 1_000_000.
)
}
}