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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
// 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/.
//! Code for allowing other languages to talk to this Rust library's FEE beam
//! code. See the examples directory for usage.
use ;
use Either;
use FEEBeam;
use crate;
cfg_if!
/// Create a new MWA FEE beam.
///
/// # Arguments
///
/// * `hdf5_file` - the path to the MWA FEE beam file.
/// * `fee_beam` - a double pointer to the `FEEBeam` struct which is set by this
/// function. This struct must be freed by calling `free_fee_beam`.
///
/// # Returns
///
/// * An exit code integer. If this is non-zero then an error occurred; the
/// details can be obtained by (1) getting the length of the error string by
/// calling `hb_last_error_length` and (2) calling `hb_last_error_message`
/// with a string buffer with a length at least equal to the error length.
///
pub unsafe extern "C"
/// Create a new MWA FEE beam. Requires the HDF5 beam file path to be specified
/// by the environment variable `MWA_BEAM_FILE`.
///
/// # Arguments
///
/// * `fee_beam` - a double pointer to the `FEEBeam` struct which is set by this
/// function. This struct must be freed by calling `free_fee_beam`.
///
/// # Returns
///
/// * An exit code integer. If this is non-zero then an error occurred; the
/// details can be obtained by (1) getting the length of the error string by
/// calling `hb_last_error_length` and (2) calling `hb_last_error_message`
/// with a string buffer with a length at least equal to the error length.
///
pub unsafe extern "C"
/// Get the beam response Jones matrix for the given direction and pointing. Can
/// optionally re-define the X and Y polarisations and apply a parallactic-angle
/// correction; see
/// <https://github.com/MWATelescope/mwa_hyperbeam/blob/main/fee_pols.pdf>
///
/// `delays` and `amps` apply to each dipole in a given MWA tile, and *must*
/// have 16 elements (each corresponds to an MWA dipole in a tile, in the M&C
/// order; see
/// <https://wiki.mwatelescope.org/pages/viewpage.action?pageId=48005139>).
/// `amps` being dipole gains (usually 1 or 0), not digital gains.
///
/// 16 or 32 elements can be supplied for `amps`. If there are 16, then the
/// dipole gains apply to both X and Y elements of dipoles. If there are 32, the
/// first 16 amps are for the X elements, the next 16 the Y elements.
///
/// Note the type of `jones` (`*double`); we can't pass complex numbers across
/// the FFI boundary, so the real and imaginary components are unpacked into
/// doubles. The output contains 8 doubles, where the j00 is the first pair, j01
/// is the second pair, etc.
///
/// # Arguments
///
/// * `fee_beam` - A pointer to a `FEEBeam` struct created with the
/// `new_fee_beam` function
/// * `az_rad` - The azimuth direction to get the beam response (units of
/// radians)
/// * `za_rad` - The zenith angle direction to get the beam response (units of
/// radians)
/// * `freq_hz` - The frequency used for the beam response in Hertz
/// * `delays` - A pointer to a 16-element array of dipole delays for an MWA
/// tile
/// * `amps` - A pointer to a 16- or 32-element array of dipole gains for an MWA
/// tile. The number of elements is indicated by `num_amps`.
/// * `num_amps` - The number of dipole gains used (either 16 or 32).
/// * `norm_to_zenith` - A boolean indicating whether the beam response should
/// be normalised with respect to zenith.
/// * `latitude_rad` - A pointer to a telescope latitude to use for the
/// parallactic-angle correction. If the pointer is null, no correction is
/// done.
/// * `iau_order` - A boolean indicating whether the Jones matrix should be
/// arranged [NS-NS NS-EW EW-NS EW-EW] (true) or not (false).
/// * `jones` - A pointer to a buffer with at least `8 * sizeof(double)`
/// allocated. The Jones matrix beam response is written here.
///
/// # Returns
///
/// * An exit code integer. If this is non-zero then an error occurred; the
/// details can be obtained by (1) getting the length of the error string by
/// calling `hb_last_error_length` and (2) calling `hb_last_error_message`
/// with a string buffer with a length at least equal to the error length.
///
pub unsafe extern "C"
/// Get the beam response Jones matrix for several az/za directions for the
/// given pointing. The Jones matrix elements for each direction are put into a
/// single array (made available with the output pointer `jones`). Can
/// optionally re-define the X and Y polarisations and apply a parallactic-angle
/// correction; see
/// <https://github.com/MWATelescope/mwa_hyperbeam/blob/main/fee_pols.pdf>
///
/// `delays` and `amps` apply to each dipole in a given MWA tile, and *must*
/// have 16 elements (each corresponds to an MWA dipole in a tile, in the M&C
/// order; see
/// <https://wiki.mwatelescope.org/pages/viewpage.action?pageId=48005139>).
/// `amps` being dipole gains (usually 1 or 0), not digital gains.
///
/// As there are 8 elements per Jones matrix, there must be at least `8 *
/// num_azza * sizeof(double)` allocated in the array. Rust will calculate the
/// Jones matrices in parallel. See the documentation for `calc_jones` for more
/// info.
///
/// # Arguments
///
/// * `fee_beam` - A pointer to a `FEEBeam` struct created with the
/// `new_fee_beam` function
/// * `num_azza` - The number of directions within `az_rad` and `za_rad`
/// * `az_rad` - The azimuth direction to get the beam response (units of
/// radians)
/// * `za_rad` - The zenith angle direction to get the beam response (units of
/// radians)
/// * `freq_hz` - The frequency used for the beam response in Hertz
/// * `delays` - A pointer to a 16-element array of dipole delays for an MWA
/// tile
/// * `amps` - A pointer to a 16- or 32-element array of dipole gains for an MWA
/// tile. The number of elements is indicated by `num_amps`.
/// * `num_amps` - The number of dipole gains used (either 16 or 32).
/// * `norm_to_zenith` - A boolean indicating whether the beam response should
/// be normalised with respect to zenith.
/// * `latitude_rad` - A pointer to a telescope latitude to use for the
/// parallactic-angle correction. If the pointer is null, no correction is
/// done.
/// * `iau_order` - A boolean indicating whether the Jones matrix should be
/// arranged [NS-NS NS-EW EW-NS EW-EW] (true) or not (false).
/// * `jones` - A pointer to a buffer with at least `8 * num_azza *
/// sizeof(double)` bytes allocated. The Jones matrix beam responses are
/// written here.
///
/// # Returns
///
/// * An exit code integer. If this is non-zero then an error occurred; the
/// details can be obtained by (1) getting the length of the error string by
/// calling `hb_last_error_length` and (2) calling `hb_last_error_message`
/// with a string buffer with a length at least equal to the error length.
///
pub unsafe extern "C"
/// Get the available frequencies inside the HDF5 file.
///
/// # Arguments
///
/// * `fee_beam` - the pointer to the `FEEBeam` struct.
/// * `freqs_ptr` - a double pointer to the FEE beam frequencies. The `const`
/// annotation is deliberate; the caller does not own the frequencies.
/// * `num_freqs` - a pointer to a `size_t` whose contents are set.
///
pub unsafe extern "C"
/// Given a frequency in Hz, get the closest available frequency inside the HDF5
/// file.
///
/// # Arguments
///
/// * `fee_beam` - the pointer to the `FEEBeam` struct.
///
/// # Returns
///
/// * The closest frequency to the specified frequency in Hz.
///
pub unsafe extern "C"
/// Free the memory associated with an `FEEBeam`.
///
/// # Arguments
///
/// * `fee_beam` - the pointer to the `FEEBeam` struct.
///
pub unsafe extern "C"
/// Get a `FEEBeamGpu` struct, which is used to calculate beam responses on a
/// GPU (CUDA- or HIP-capable device).
///
/// # Arguments
///
/// * `fee_beam` - a pointer to a previously set `FEEBeam` struct.
/// * `freqs_hz` - a pointer to an array of frequencies (units of Hz) at which
/// the beam responses will be calculated.
/// * `delays` - a pointer to two-dimensional array of dipole delays. There must
/// be 16 delays per row; each row corresponds to a tile.
/// * `amps` - a pointer to two-dimensional array of dipole amplitudes. There
/// must be 16 or 32 amps per row; each row corresponds to a tile. The number
/// of amps per row is specified by `num_amps`.
/// * `num_freqs` - the number of frequencies in the array pointed to by
/// `freqs_hz`.
/// * `num_tiles` - the number of tiles in both `delays` and `amps`.
/// * `num_amps` - either 16 or 32. See the documentation for `calc_jones` for
/// more explanation.
/// * `norm_to_zenith` - A boolean indicating whether the beam responses should
/// be normalised with respect to zenith.
/// * `gpu_fee_beam` - a double pointer to the `FEEBeamGpu` struct which is set
/// by this function. This struct must be freed by calling
/// `free_gpu_fee_beam`.
///
/// # Returns
///
/// * An exit code integer. If this is non-zero then an error occurred; the
/// details can be obtained by (1) getting the length of the error string by
/// calling `hb_last_error_length` and (2) calling `hb_last_error_message`
/// with a string buffer with a length at least equal to the error length.
///
pub unsafe extern "C"
/// Get beam response Jones matrices for the given directions, using a GPU. The
/// Jones matrix elements for each direction are put into a host-memory buffer
/// `jones`. Can optionally re-define the X and Y polarisations and apply a
/// parallactic-angle correction; see
/// <https://github.com/MWATelescope/mwa_hyperbeam/blob/main/fee_pols.pdf>
///
/// # Arguments
///
/// * `gpu_fee_beam` - A pointer to a `FEEBeamGpu` struct created with the
/// `new_gpu_fee_beam` function
/// * `az_rad` - The azimuth directions to get the beam response (units of
/// radians)
/// * `za_rad` - The zenith angle directions to get the beam response (units of
/// radians)
/// * `latitude_rad` - A pointer to a telescope latitude to use for the
/// parallactic-angle correction. If the pointer is null, no correction is
/// done.
/// * `iau_order` - A boolean indicating whether the Jones matrix should be
/// arranged [NS-NS NS-EW EW-NS EW-EW] (true) or not (false).
/// * `jones` - A pointer to a buffer with at least `num_unique_tiles *
/// num_unique_fee_freqs * num_azza * 8 * sizeof(FLOAT)` bytes allocated.
/// `FLOAT` is either `float` or `double`, depending on how `hyperbeam` was
/// compiled. The Jones matrix beam responses are written here. This should be
/// set up with the `get_num_unique_tiles` and `get_num_unique_fee_freqs`
/// functions; see the examples for more help.
///
/// # Returns
///
/// * An exit code integer. If this is non-zero then an error occurred; the
/// details can be obtained by (1) getting the length of the error string by
/// calling `hb_last_error_length` and (2) calling `hb_last_error_message`
/// with a string buffer with a length at least equal to the error length.
///
pub unsafe extern "C"
/// Get beam response Jones matrices for the given directions, using a GPU. The
/// Jones matrix elements for each direction are left on the device (the device
/// pointer is communicated via `d_jones`). Can optionally re-define the X and Y
/// polarisations and apply a parallactic-angle correction; see
/// <https://github.com/MWATelescope/mwa_hyperbeam/blob/main/fee_pols.pdf>
///
/// # Arguments
///
/// * `gpu_fee_beam` - A pointer to a `FEEBeamGpu` struct created with the
/// `new_gpu_fee_beam` function
/// * `az_rad` - The azimuth directions to get the beam response (units of
/// radians)
/// * `za_rad` - The zenith angle directions to get the beam response (units of
/// radians)
/// * `latitude_rad` - A pointer to a telescope latitude to use for the
/// parallactic-angle correction. If the pointer is null, no correction is
/// done.
/// * `iau_order` - A boolean indicating whether the Jones matrix should be
/// arranged [NS-NS NS-EW EW-NS EW-EW] (true) or not (false).
/// * `d_jones` - A pointer to a device buffer with at least `8 *
/// num_unique_tiles * num_unique_fee_freqs * num_azza * sizeof(FLOAT)` bytes
/// allocated. `FLOAT` is either `float` or `double`, depending on how
/// `hyperbeam` was compiled. The Jones matrix beam responses are written
/// here. This should be set up with the `get_num_unique_tiles` and
/// `get_num_unique_fee_freqs` functions; see the examples for more help.
///
/// # Returns
///
/// * An exit code integer. If this is non-zero then an error occurred; the
/// details can be obtained by (1) getting the length of the error string by
/// calling `hb_last_error_length` and (2) calling `hb_last_error_message`
/// with a string buffer with a length at least equal to the error length.
///
pub unsafe extern "C"
/// The same as `calc_jones_gpu_device`, but with the directions already
/// allocated on the device. As with `d_jones`, the precision of the floats
/// depends on how `hyperbeam` was compiled.
///
/// # Arguments
///
/// * `gpu_fee_beam` - A pointer to a `FEEBeamGpu` struct created with the
/// `new_gpu_fee_beam` function
/// * `d_az_rad` - The azimuth directions to get the beam response (units of
/// radians)
/// * `d_za_rad` - The zenith angle directions to get the beam response (units
/// of radians)
/// * `latitude_rad` - A pointer to a telescope latitude to use for the
/// parallactic-angle correction. If the pointer is null, no correction is
/// done.
/// * `iau_order` - A boolean indicating whether the Jones matrix should be
/// arranged [NS-NS NS-EW EW-NS EW-EW] (true) or not (false).
/// * `d_jones` - A pointer to a device buffer with at least `8 *
/// num_unique_tiles * num_unique_fee_freqs * num_azza * sizeof(FLOAT)` bytes
/// allocated. `FLOAT` is either `float` or `double`, depending on how
/// `hyperbeam` was compiled. The Jones matrix beam responses are written
/// here. This should be set up with the `get_num_unique_tiles` and
/// `get_num_unique_fee_freqs` functions; see the examples for more help.
///
/// # Returns
///
/// * An exit code integer. If this is non-zero then an error occurred; the
/// details can be obtained by (1) getting the length of the error string by
/// calling `hb_last_error_length` and (2) calling `hb_last_error_message`
/// with a string buffer with a length at least equal to the error length.
///
pub unsafe extern "C"
/// Get a pointer to the tile map. This is necessary to access de-duplicated
/// beam Jones matrices.
///
/// # Arguments
///
/// * `gpu_fee_beam` - the pointer to the `FEEBeamGpu` struct.
///
/// # Returns
///
/// * A pointer to the tile map. The const annotation is deliberate; the caller
/// does not own the map.
///
pub unsafe extern "C"
/// Get a pointer to the tile map. This is necessary to access de-duplicated
/// beam Jones matrices on the device.
///
/// # Arguments
///
/// * `gpu_fee_beam` - the pointer to the `FEEBeamGpu` struct.
///
/// # Returns
///
/// * A pointer to the device tile map. The const annotation is deliberate; the
/// caller does not own the map.
///
pub unsafe extern "C"
/// Get a pointer to the freq map. This is necessary to access de-duplicated
/// beam Jones matrices.
///
/// # Arguments
///
/// * `gpu_fee_beam` - the pointer to the `FEEBeamGpu` struct.
///
/// # Returns
///
/// * A pointer to the freq map. The const annotation is deliberate; the caller
/// does not own the map.
///
pub unsafe extern "C"
/// Get a pointer to the device freq map. This is necessary to access
/// de-duplicated beam Jones matrices on the device.
///
/// # Arguments
///
/// * `gpu_fee_beam` - the pointer to the `FEEBeamGpu` struct.
///
/// # Returns
///
/// * A pointer to the device freq map. The const annotation is deliberate; the
/// caller does not own the map.
///
pub unsafe extern "C"
/// Get the number of de-duplicated tiles associated with this `FEEBeamGpu`.
///
/// # Arguments
///
/// * `gpu_fee_beam` - the pointer to the `FEEBeamGpu` struct.
///
/// # Returns
///
/// * The number of de-duplicated tiles associated with this `FEEBeamGpu`.
///
pub unsafe extern "C"
/// Get the number of de-duplicated frequencies associated with this
/// `FEEBeamGpu`.
///
/// # Arguments
///
/// * `gpu_fee_beam` - the pointer to the `FEEBeamGpu` struct.
///
/// # Returns
///
/// * The number of de-duplicated frequencies associated with this
/// `FEEBeamGpu`.
///
pub unsafe extern "C"
/// Free the memory associated with an `FEEBeamGpu` beam.
///
/// # Arguments
///
/// * `gpu_fee_beam` - the pointer to the `FEEBeamGpu` struct.
///
pub unsafe extern "C"