mwalib 2.0.3

A library to simplify reading Murchison Widefield Array (MWA) raw visibilities, voltages and metadata.
Documentation
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

//! Unit tests for timestep metadata
use super::*;
use crate::gpubox_files::GpuboxTimeMap;
use crate::MetafitsContext;
use std::collections::BTreeMap;

///
/// This is helper method for many of the tests for generating the correlator timesteps
/// Returns:
/// * Vector of metafits timesteps
/// * GpuboxTimeMap of correlator data files
/// * Option, containing a populated Vector of correlator timesteps (or None). This is what we are checking for validity.
///
fn create_corr_timestep_testdata(
    scheduled_start_unix_ms: u64,
    scheduled_start_gpstime_ms: u64,
    scheduled_duration_ms: u64,
    corr_int_time_ms: u64,
    data_timesteps_unix_ms: Vec<u64>,
) -> (Vec<TimeStep>, GpuboxTimeMap, Option<Vec<TimeStep>>) {
    // Create metafits timesteps vec
    let mut metafits_timesteps: Vec<TimeStep> = Vec::new();

    // populate metafits timesteps
    for (t_index, t_time) in (scheduled_start_unix_ms
        ..(scheduled_start_unix_ms + scheduled_duration_ms))
        .step_by(corr_int_time_ms as usize)
        .enumerate()
    {
        metafits_timesteps.push(TimeStep::new(
            t_time,
            1_065_880_139_000 + (t_index as u64 * corr_int_time_ms),
        ));
    }

    // Now create a GpuboxtimeMap based on passed in Data unix time vector
    // Create a dummy BTree GPUbox map
    let mut gpubox_time_map = BTreeMap::new();

    // Populate gpubox time map
    for (i, time) in data_timesteps_unix_ms.iter().enumerate() {
        let mut new_time_tree = BTreeMap::new();
        // gpubox 0.
        new_time_tree.insert(0, (0, i + 1));
        // gpubox 1.
        new_time_tree.insert(1, (0, i + 1));
        gpubox_time_map.insert(*time, new_time_tree);
    }

    // perform the actual test
    let correlator_timesteps = TimeStep::populate_correlator_timesteps(
        &gpubox_time_map,
        &metafits_timesteps,
        scheduled_start_gpstime_ms,
        scheduled_start_unix_ms,
        corr_int_time_ms,
    );

    (metafits_timesteps, gpubox_time_map, correlator_timesteps)
}

#[test]
fn test_populate_correlator_timesteps_simple() {
    // In this scenario the data matches the metafits timesteps 1:1
    // metafits: [1_381_844_923_000, 1_381_844_923_500, 1_381_844_924_000, 1_381_844_924_500, 1_381_844_925_000, 1_381_844_925_500]
    // data    : [1_381_844_923_000, 1_381_844_923_500, 1_381_844_924_000, 1_381_844_924_500, 1_381_844_925_000, 1_381_844_925_500]
    //
    let (metafits_timesteps, _, correlator_timesteps) = create_corr_timestep_testdata(
        1_381_844_923_000,
        1_065_880_139_000,
        3000,
        500,
        vec![
            1_381_844_923_000,
            1_381_844_923_500,
            1_381_844_924_000,
            1_381_844_924_500,
            1_381_844_925_000,
            1_381_844_925_500,
        ],
    );

    // First check metafits is what we expect- this is testing the helper code!
    assert_eq!(metafits_timesteps.len(), 6);

    // Check returned correlator timesteps
    assert!(correlator_timesteps.is_some());
    let timesteps = correlator_timesteps.unwrap();

    assert_eq!(6, timesteps.len());
    assert_eq!(timesteps[0].unix_time_ms, 1_381_844_923_000);
    assert_eq!(timesteps[0].gps_time_ms, 1_065_880_139_000);
    assert_eq!(timesteps[5].unix_time_ms, 1_381_844_925_500);
    assert_eq!(timesteps[5].gps_time_ms, 1_065_880_141_500);
}

#[test]
fn test_populate_correlator_timesteps_data_earlier_and_later_than_metafits() {
    // In this scenario, the metafits first timestep starts AFTER the first data timestep
    // also the metafits last timestep is BEFORE the last data timestep
    // e.g.
    //
    // metafits:                                       [1_381_844_923_000, 1_381_844_923_500, 1_381_844_924_000, 1_381_844_924_500, 1_381_844_925_000, 1_381_844_925_500]
    // data:     [1_381_844_922_000, 1_381_844_922_500, 1_381_844_923_000, 1_381_844_923_500, 1_381_844_924_000, 1_381_844_924_500, 1_381_844_925_000, 1_381_844_925_500, 1_381_844_926_000]
    //
    let (metafits_timesteps, _, correlator_timesteps) = create_corr_timestep_testdata(
        1_381_844_923_000,
        1_065_880_139_000,
        3000,
        500,
        vec![
            1_381_844_922_000,
            1_381_844_922_500,
            1_381_844_923_000,
            1_381_844_923_500,
            1_381_844_924_000,
            1_381_844_924_500,
            1_381_844_925_000,
            1_381_844_925_500,
            1_381_844_926_000,
        ],
    );

    // First check metafits is what we expect- this is testing the helper code!
    assert_eq!(metafits_timesteps.len(), 6);

    // Check returned correlator timesteps
    assert!(correlator_timesteps.is_some());
    let timesteps = correlator_timesteps.unwrap();

    assert_eq!(9, timesteps.len());
    assert_eq!(timesteps[0].unix_time_ms, 1_381_844_922_000);
    assert_eq!(timesteps[0].gps_time_ms, 1_065_880_138_000);
    assert_eq!(timesteps[2].unix_time_ms, 1_381_844_923_000);
    assert_eq!(timesteps[2].gps_time_ms, 1_065_880_139_000);
    assert_eq!(timesteps[7].unix_time_ms, 1_381_844_925_500);
    assert_eq!(timesteps[7].gps_time_ms, 1_065_880_141_500);
    assert_eq!(timesteps[8].unix_time_ms, 1_381_844_926_000);
    assert_eq!(timesteps[8].gps_time_ms, 1_065_880_142_000);
}

#[test]
fn test_populate_correlator_timesteps_data_between_metafits_start_and_end() {
    // In this scenario, the metafits first timestep starts BEFORE the first data timestep
    // also the metafits last timestep is AFTER the last data timestep
    // e.g.
    //
    // metafits: [1_381_844_923_000, 1_381_844_923_500, 1_381_844_924_000, 1_381_844_924_500, 1_381_844_925_000, 1_381_844_925_500]
    // data:                        [1_381_844_923_500, 1_381_844_924_000, 1_381_844_924_500, 1_381_844_925_000]
    //
    let (metafits_timesteps, _, correlator_timesteps) = create_corr_timestep_testdata(
        1_381_844_923_000,
        1_065_880_139_000,
        3000,
        500,
        vec![
            1_381_844_923_500,
            1_381_844_924_000,
            1_381_844_924_500,
            1_381_844_925_000,
        ],
    );

    // First check metafits is what we expect- this is testing the helper code!
    assert_eq!(metafits_timesteps.len(), 6);

    // Check returned correlator timesteps
    assert!(correlator_timesteps.is_some());
    let timesteps = correlator_timesteps.unwrap();

    // Check
    assert_eq!(6, timesteps.len());
    assert_eq!(timesteps[0].unix_time_ms, 1_381_844_923_000);
    assert_eq!(timesteps[0].gps_time_ms, 1_065_880_139_000);
    assert_eq!(timesteps[5].unix_time_ms, 1_381_844_925_500);
    assert_eq!(timesteps[5].gps_time_ms, 1_065_880_141_500);
}

#[test]
fn test_populate_correlator_timesteps_data_between_metafits_start_and_end_gaps() {
    // In this scenario, the metafits first timestep starts BEFORE the first data timestep
    // also the metafits last timestep is AFTER the last data timestep. We also have data gaps
    // e.g.
    //
    // metafits: [1_381_844_923_000, 1_381_844_923_500, 1_381_844_924_000, 1_381_844_924_500, 1_381_844_925_000, 1_381_844_925_500]
    // data:                        [1_381_844_923_500,                                       1_381_844_925_000]
    //
    let (metafits_timesteps, _, correlator_timesteps) = create_corr_timestep_testdata(
        1_381_844_923_000,
        1_065_880_139_000,
        3000,
        500,
        vec![1_381_844_923_500, 1_381_844_925_000],
    );

    // First check metafits is what we expect- this is testing the helper code!
    assert_eq!(metafits_timesteps.len(), 6);

    // Check returned correlator timesteps
    assert!(correlator_timesteps.is_some());
    let timesteps = correlator_timesteps.unwrap();

    // Check
    assert_eq!(timesteps.len(), 6);
    assert_eq!(timesteps[0].unix_time_ms, 1_381_844_923_000);
    assert_eq!(timesteps[0].gps_time_ms, 1_065_880_139_000);
    assert_eq!(timesteps[5].unix_time_ms, 1_381_844_925_500);
    assert_eq!(timesteps[5].gps_time_ms, 1_065_880_141_500);
}

#[test]
fn test_populate_correlator_timesteps_data_earlier_and_later_than_metafits_with_offset() {
    // In this scenario, the metafits first timestep starts AFTER the first data timestep
    // also the metafits last timestep is BEFORE the last data timestep
    // AND the metafits timesteps are offset from the data timesteps by 1 second
    // e.g.
    //
    // metafits:                            [1_381_844_923_000, 1_381_844_925_000, 1_381_844_927_000, 1_381_844_929_000, 1_381_844_931_000, 1_381_844_933_000]
    // data:     [1_381_844_920_000, 1_381_844_922_000, 1_381_844_924_000, 1_381_844_926_000, 1_381_844_928_000, 1_381_844_930_000, 1_381_844_932_000, 1_381_844_934_000, 1_381_844_936_000]
    //
    let (metafits_timesteps, _, correlator_timesteps) = create_corr_timestep_testdata(
        1_381_844_923_000,
        1_065_880_139_000,
        12000,
        2000,
        vec![
            1_381_844_920_000,
            1_381_844_922_000,
            1_381_844_924_000,
            1_381_844_926_000,
            1_381_844_928_000,
            1_381_844_930_000,
            1_381_844_932_000,
            1_381_844_934_000,
            1_381_844_936_000,
        ],
    );

    // First check metafits is what we expect- this is testing the helper code!
    assert_eq!(metafits_timesteps.len(), 6);

    // Check returned correlator timesteps
    assert!(correlator_timesteps.is_some());
    let timesteps = correlator_timesteps.unwrap();

    // Check
    assert_eq!(9, timesteps.len());
    assert_eq!(timesteps[0].unix_time_ms, 1_381_844_920_000);
    assert_eq!(timesteps[0].gps_time_ms, 1_065_880_136_000);
    assert_eq!(timesteps[2].unix_time_ms, 1_381_844_924_000);
    assert_eq!(timesteps[2].gps_time_ms, 1_065_880_140_000);
    assert_eq!(timesteps[7].unix_time_ms, 1_381_844_934_000);
    assert_eq!(timesteps[7].gps_time_ms, 1_065_880_150_000);
    assert_eq!(timesteps[8].unix_time_ms, 1_381_844_936_000);
    assert_eq!(timesteps[8].gps_time_ms, 1_065_880_152_000);
}

#[test]
fn test_populate_correlator_timesteps_data_between_metafits_start_and_end_with_offset() {
    // In this scenario, the metafits first timestep starts BEFORE the first data timestep
    // also the metafits last timestep is AFTER the last data timestep
    // AND the metafits timesteps are offset from the data timesteps by 1 second
    // e.g.
    //
    // metafits: [1_381_844_923_000, 1_381_844_925_000, 1_381_844_927_000, 1_381_844_929_000, 1_381_844_931_000, 1_381_844_933_000]
    // data:                                 [1_381_844_926_000, 1_381_844_928_000, 1_381_844_930_000, 1_381_844_932_000]
    //
    let (metafits_timesteps, _, correlator_timesteps) = create_corr_timestep_testdata(
        1_381_844_923_000,
        1_065_880_139_000,
        12000,
        2000,
        vec![
            1_381_844_926_000,
            1_381_844_928_000,
            1_381_844_930_000,
            1_381_844_932_000,
        ],
    );

    // First check metafits is what we expect- this is testing the helper code!
    assert_eq!(metafits_timesteps.len(), 6);

    // Check returned correlator timesteps
    assert!(correlator_timesteps.is_some());
    let timesteps = correlator_timesteps.unwrap();

    // Check
    assert_eq!(5, timesteps.len());
    assert_eq!(timesteps[0].unix_time_ms, 1_381_844_924_000);
    assert_eq!(timesteps[0].gps_time_ms, 1_065_880_140_000);
    assert_eq!(timesteps[4].unix_time_ms, 1_381_844_932_000);
    assert_eq!(timesteps[4].gps_time_ms, 1_065_880_148_000);
}

#[test]
fn test_populate_correlator_timesteps_no_overlap_with_metafits() {
    // In this scenario, the metafits timesteps have no overlap with data timesteps
    // e.g.
    //
    // metafits: [1_381_844_923_000, 1_381_844_923_500, 1_381_844_924_000, 1_381_844_924_500, 1_381_844_925_000, 1_381_844_925_500]
    // data:                                                                                                                                           [1_381_844_926_500]
    //
    let (metafits_timesteps, _, correlator_timesteps) = create_corr_timestep_testdata(
        1_381_844_923_000,
        1_065_880_139_000,
        3000,
        500,
        vec![1_381_844_926_500],
    );

    // First check metafits is what we expect- this is testing the helper code!
    assert_eq!(metafits_timesteps.len(), 6);

    // Check returned correlator timesteps
    assert!(correlator_timesteps.is_some());
    let timesteps = correlator_timesteps.unwrap();

    assert_eq!(8, timesteps.len());
    assert_eq!(timesteps[0].unix_time_ms, 1_381_844_923_000);
    assert_eq!(timesteps[0].gps_time_ms, 1_065_880_139_000);
    assert_eq!(timesteps[5].unix_time_ms, 1_381_844_925_500);
    assert_eq!(timesteps[5].gps_time_ms, 1_065_880_141_500);
    assert_eq!(timesteps[6].unix_time_ms, 1_381_844_926_000);
    assert_eq!(timesteps[6].gps_time_ms, 1_065_880_142_000);
    assert_eq!(timesteps[7].unix_time_ms, 1_381_844_926_500);
    assert_eq!(timesteps[7].gps_time_ms, 1_065_880_142_500);
}

#[test]
fn test_populate_correlator_timesteps_none() {
    // Create a dummy BTree GPUbox map
    let gpubox_time_map = BTreeMap::new();

    let metafits_timesteps: Vec<TimeStep> = Vec::new();

    // Get a vector timesteps
    let scheduled_start_gpstime_ms = 0;
    let scheduled_start_unix_ms = 0;
    let corr_int_time_ms = 500;

    let timesteps = TimeStep::populate_correlator_timesteps(
        &gpubox_time_map,
        &metafits_timesteps,
        scheduled_start_gpstime_ms,
        scheduled_start_unix_ms,
        corr_int_time_ms,
    );

    // Check
    assert!(timesteps.is_none());
}

#[test]
fn test_timestep_new() {
    // This test is a bit of a waste right now but it will be useful once
    // julian date and possibly UTC conversions are done in the new() method
    let timestep = TimeStep {
        unix_time_ms: 1_381_844_923_000,
        gps_time_ms: 1_065_880_139_000,
    };
    let new_timestep = TimeStep::new(1_381_844_923_000, 1_065_880_139_000);

    assert_eq!(timestep.unix_time_ms, new_timestep.unix_time_ms,);
    assert_eq!(timestep.gps_time_ms, new_timestep.gps_time_ms);
}

#[test]
fn test_populate_timesteps_metafits_corr() {
    let metafits_file = "test_files/1101503312_1_timestep/1101503312.metafits";

    let versions: Vec<MWAVersion> = vec![
        MWAVersion::CorrOldLegacy,
        MWAVersion::CorrLegacy,
        MWAVersion::CorrMWAXv2,
    ];

    for mwa_version in versions {
        let metafits_context =
            MetafitsContext::new_internal(metafits_file).expect("Error creating metafits context");

        let timesteps = TimeStep::populate_timesteps(
            &metafits_context,
            mwa_version,
            metafits_context.sched_start_gps_time_ms,
            metafits_context.sched_duration_ms,
            metafits_context.sched_start_gps_time_ms,
            metafits_context.sched_start_unix_time_ms,
        );

        assert_eq!(timesteps.len(), 56);
        assert_eq!(timesteps[0].gps_time_ms, 1_101_503_312_000);
        assert_eq!(timesteps[0].unix_time_ms, 1_417_468_096_000);
        assert_eq!(timesteps[1].gps_time_ms, 1_101_503_314_000);
        assert_eq!(timesteps[1].unix_time_ms, 1_417_468_098_000);
        assert_eq!(timesteps[2].gps_time_ms, 1_101_503_316_000);
        assert_eq!(timesteps[2].unix_time_ms, 1_417_468_100_000);
        assert_eq!(timesteps[55].gps_time_ms, 1_101_503_422_000);
        assert_eq!(timesteps[55].unix_time_ms, 1_417_468_206_000);
    }
}

#[test]
fn test_populate_timesteps_metafits_vcs_legacy_recombined() {
    let metafits_file = "test_files/1101503312_1_timestep/1101503312.metafits";

    let metafits_context =
        MetafitsContext::new_internal(metafits_file).expect("Error creating metafits context");

    let timesteps = TimeStep::populate_timesteps(
        &metafits_context,
        MWAVersion::VCSLegacyRecombined,
        metafits_context.sched_start_gps_time_ms,
        metafits_context.sched_duration_ms,
        metafits_context.sched_start_gps_time_ms,
        metafits_context.sched_start_unix_time_ms,
    );

    assert_eq!(timesteps.len(), 112);
    assert_eq!(timesteps[0].gps_time_ms, 1_101_503_312_000);
    assert_eq!(timesteps[0].unix_time_ms, 1_417_468_096_000);
    assert_eq!(timesteps[1].gps_time_ms, 1_101_503_313_000);
    assert_eq!(timesteps[1].unix_time_ms, 1_417_468_097_000);
    assert_eq!(timesteps[2].gps_time_ms, 1_101_503_314_000);
    assert_eq!(timesteps[2].unix_time_ms, 1_417_468_098_000);
    assert_eq!(timesteps[111].gps_time_ms, 1_101_503_423_000);
    assert_eq!(timesteps[111].unix_time_ms, 1_417_468_207_000);
}

#[test]
fn test_populate_timesteps_metafits_vcs_mwaxv2() {
    let metafits_file = "test_files/1101503312_1_timestep/1101503312.metafits";

    let metafits_context =
        MetafitsContext::new_internal(metafits_file).expect("Error creating metafits context");

    let timesteps = TimeStep::populate_timesteps(
        &metafits_context,
        MWAVersion::VCSMWAXv2,
        metafits_context.sched_start_gps_time_ms,
        metafits_context.sched_duration_ms,
        metafits_context.sched_start_gps_time_ms,
        metafits_context.sched_start_unix_time_ms,
    );

    assert_eq!(timesteps.len(), 14);
    assert_eq!(timesteps[0].gps_time_ms, 1_101_503_312_000);
    assert_eq!(timesteps[0].unix_time_ms, 1_417_468_096_000);
    assert_eq!(timesteps[1].gps_time_ms, 1_101_503_320_000);
    assert_eq!(timesteps[1].unix_time_ms, 1_417_468_104_000);
    assert_eq!(timesteps[2].gps_time_ms, 1_101_503_328_000);
    assert_eq!(timesteps[2].unix_time_ms, 1_417_468_112_000);
    assert_eq!(timesteps[13].gps_time_ms, 1_101_503_416_000);
    assert_eq!(timesteps[13].unix_time_ms, 1_417_468_200_000);
}

#[test]
fn test_get_timstep_indicies() {
    let all_timesteps: Vec<TimeStep> = vec![
        TimeStep::new(1000, 1000),
        TimeStep::new(2000, 2000),
        TimeStep::new(3000, 3000),
        TimeStep::new(4000, 4000),
    ];

    let indices_1 = TimeStep::get_timstep_indicies(&all_timesteps, 1000, 5000);
    assert_eq!(indices_1.len(), 4);
    assert_eq!(indices_1[0], 0);
    assert_eq!(indices_1[1], 1);
    assert_eq!(indices_1[2], 2);
    assert_eq!(indices_1[3], 3);

    let indices_2 = TimeStep::get_timstep_indicies(&all_timesteps, 0000, 6000);
    assert_eq!(indices_2.len(), 4);
    assert_eq!(indices_2[0], 0);
    assert_eq!(indices_2[1], 1);
    assert_eq!(indices_2[2], 2);
    assert_eq!(indices_2[3], 3);

    let indices_3 = TimeStep::get_timstep_indicies(&all_timesteps, 2000, 6000);
    assert_eq!(indices_3.len(), 3);
    assert_eq!(indices_3[0], 1);
    assert_eq!(indices_3[1], 2);
    assert_eq!(indices_3[2], 3);

    let indices_4 = TimeStep::get_timstep_indicies(&all_timesteps, 2000, 4000);
    assert_eq!(indices_4.len(), 2);
    assert_eq!(indices_4[0], 1);
    assert_eq!(indices_4[1], 2);

    let indices_5 = TimeStep::get_timstep_indicies(&all_timesteps, 2000, 3000);
    assert_eq!(indices_5.len(), 1);
    assert_eq!(indices_5[0], 1);
}