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
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
import argparse
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import math
import mwalib

# Allow X windows support- but you need to
# pip install pycairo
# pip install pygobject
# first.
matplotlib.use("GTK3Agg")

dpi = 100
MODE_RANGE = "RANGE"
MODE_BASELINE = "BASELINE"


class ViewFITSArgs:
    def __init__(self, passed_args):
        self.filename = passed_args["filename"]
        self.metafits_filename = passed_args["metafits"]

        self.time_step1 = passed_args["timestep1"]
        self.time_step2 = passed_args["timestep2"]

        self.tile1 = passed_args["ant1"]
        self.tile2 = passed_args["ant2"]

        self.autos_only = passed_args["autosonly"]

        self.channel1 = passed_args["channel1"]
        self.channel2 = passed_args["channel2"]

        self.ppd_plot = passed_args["ppdplot"]
        self.ppd_plot2 = passed_args["ppdplot2"]
        self.grid_plot = passed_args["gridplot"]
        self.grid_plot2 = passed_args["gridplot2"]
        self.grid_pol = passed_args["gridpol"]
        self.phase_plot_all = passed_args["phaseplot_all"]
        self.phase_plot_one = passed_args["phaseplot_one"]
        self.mode = passed_args["mode"]

        self.dumpraw = passed_args["dump_raw"]
        self.dumpplot = passed_args["dump_plot"]

        # Are we plotting?
        self.any_plotting = (
            self.ppd_plot or self.ppd_plot2 or self.grid_plot or self.phase_plot_all or self.phase_plot_one
        )

        if not self.any_plotting and self.dumpplot:
            print("You must be plotting to dump out the plot data to file!")
            exit(-1)

        # Some constants not found in fits file
        self.values = 2  # real and imaginary

        self.validate_params()

    def validate_params(self):  # noqa: C901
        # Read fits file
        print(f"Opening with mwalib using metafits file {self.metafits_filename} and data file {self.filename}...")
        self.context = mwalib.CorrelatorContext(
            self.metafits_filename,
            [
                self.filename,
            ],
        )

        for p in self.context.provided_coarse_chan_indices:
            print(self.context.coarse_chans[p])

        self.correlator_version: mwalib.MWAVersion = self.context.mwa_version
        self.pols = self.context.metafits_context.num_visibility_pols  # xx,xy,yx,yy

        # in v2 NAXIS1 == fine channels * pols * r,i
        # in v2 NAXIS2 == baselines
        # Get number of tiles based on the number of signal chains
        self.fits_tiles = len(self.context.metafits_context.antennas)
        self.chan_x_pols_x_vals = self.context.metafits_context.num_corr_fine_chans_per_coarse * self.pols * self.values
        self.fits_channels = self.context.metafits_context.num_corr_fine_chans_per_coarse
        self.fits_has_weights = True

        # Check mode
        if self.mode != MODE_RANGE and self.mode != MODE_BASELINE:
            print(f"Error, mode should be {MODE_RANGE} or {MODE_BASELINE}!")
            exit(-1)

        # Check tiles
        if self.tile1 == -1:
            self.tile1 = 0

        if self.tile2 == -1:
            self.tile2 = self.fits_tiles - 1

        if self.tile1 > self.fits_tiles - 1:
            print("Error ant1 is more than the last tile index!")
            exit(-1)

        if self.tile2 > self.fits_tiles - 1:
            print("Error ant2 is more than the last tile index!")
            exit(-1)

        if self.tile1 > self.tile2:
            print("Error ant1 is more than the ant2!")
            exit(-1)

        # Check time steps
        self.fits_time_steps = self.context.num_timesteps

        if self.time_step1 == -1:
            self.time_step1 = 1

        if self.time_step2 == -1:
            self.time_step2 = self.fits_time_steps

        if self.time_step1 > self.fits_time_steps:
            print("Error t1 is more than the max time step!")
            exit(-1)

        if self.time_step2 > self.fits_time_steps:
            print("Error t2 is more than the max time step!")
            exit(-1)

        if self.time_step1 > self.time_step2:
            print("Error t1 is more than the t2!")
            exit(-1)

        # Check channels
        if self.channel1 == -1:
            self.channel1 = 0

        if self.channel2 == -1:
            self.channel2 = self.fits_channels - 1

        if self.channel1 > self.fits_channels - 1:
            print("Error c1 is more than the number of the last fine channel!")
            exit(-1)

        if self.channel2 > self.fits_channels - 1:
            print("Error c2 is more than the number of the last fine channel!")
            exit(-1)

        if self.channel1 > self.channel2:
            print("Error c1 is more than the c2!")
            exit(-1)

        # Some calculated fields
        self.tile_count = self.tile2 - self.tile1 + 1

        if self.mode == MODE_BASELINE:
            self.baseline_count = 1
        else:
            self.baseline_count = int((self.tile_count * (self.tile_count + 1)) / 2)

        self.fits_baseline_count = int((self.fits_tiles * (self.fits_tiles + 1)) / 2)
        self.channel_count = self.channel2 - self.channel1 + 1
        self.time_step_count = self.time_step2 - self.time_step1 + 1

        self.hdu_time1 = self.time_step1 - 1
        self.unix_time1 = self.context.timesteps[self.hdu_time1].unix_time_ms / 1000.0

        self.hdu_time2 = self.time_step2 - 1
        self.unix_time2 = self.context.timesteps[self.hdu_time2].unix_time_ms / 1000.0

        # print params
        if self.grid_plot2:
            self.param_string = (
                f"[{self.grid_pol}] {self.filename} "
                f"t={self.time_step1}-{self.time_step2} "
                f"t_hdu={self.hdu_time1}-{self.hdu_time2} \n"
                f"t_unix={self.unix_time1}-{self.unix_time2} "
                f"tile={self.tile1}-{self.tile2} "
                f"ch={self.channel1}-{self.channel2} "
                f"autosonly?={self.autos_only} "
                f"{self.tile_count}t/{self.fits_tiles}t"
            )
        elif self.phase_plot_one:
            self.param_string = (
                f"[{self.grid_pol}] {self.filename} "
                f"t={self.time_step1}-{self.time_step2} "
                f"t_hdu={self.hdu_time1}-{self.hdu_time2} \n"
                f"t_unix={self.unix_time1}-{self.unix_time2} "
                f"baseline={self.tile1}-{self.tile2} "
                f"chs={self.channel1}-{self.channel2} "
            )
        else:
            self.param_string = (
                f"{self.filename} t={self.time_step1}-{self.time_step2} "
                f"t_hdu={self.hdu_time1}-{self.hdu_time2} \n"
                f"t_unix={self.unix_time1}-{self.unix_time2} "
                f"tile={self.tile1}-{self.tile2} "
                f"ch={self.channel1}-{self.channel2} "
                f"autosonly?={self.autos_only} "
                f"{self.tile_count}t/{self.fits_tiles}t"
            )
        print(self.param_string)


def meets_criteria(i, j, a1, a2, mode):
    # mode == RANGE | BASELINE
    if mode == MODE_RANGE:
        return i >= a1 and j <= a2
    elif mode == MODE_BASELINE:
        return i == a1 and j == a2
    else:
        return None


# v1 = baseline, freq, pol
# v2 = freq,baseline,pol
def peek_fits(program_args: ViewFITSArgs):  # noqa: C901
    print("Initialising data structures...")

    plot_ppd_data_x = None
    plot_ppd_data_y = None
    plot_ppd2_data_x = None
    plot_ppd2_data_y = None
    plot_grid_data = None
    plot_grid2_data = None
    plot_phase_data_x = None
    plot_phase_data_y = None

    # ppd array will be [timestep][channel]
    if program_args.ppd_plot:
        plot_ppd_data_x = np.empty(shape=(program_args.channel_count, program_args.time_step_count))
        plot_ppd_data_x.fill(0)
        plot_ppd_data_y = np.empty(shape=(program_args.channel_count, program_args.time_step_count))
        plot_ppd_data_y.fill(0)

    # ppd plot 2 array will be [timestep][baseline][channel]
    if program_args.ppd_plot2:
        plot_ppd2_data_x = np.empty(
            shape=(
                program_args.time_step_count,
                program_args.baseline_count,
                program_args.channel_count,
            )
        )
        plot_ppd2_data_x.fill(0)
        plot_ppd2_data_y = np.empty(
            shape=(
                program_args.time_step_count,
                program_args.baseline_count,
                program_args.channel_count,
            )
        )
        plot_ppd2_data_y.fill(0)

    # Grid plot
    if program_args.grid_plot:
        plot_grid_data = np.empty(
            shape=(
                program_args.time_step_count,
                program_args.tile_count,
                program_args.tile_count,
            )
        )
        plot_grid_data.fill(0)

    # Grid plot 2 array
    if program_args.grid_plot2:
        plot_grid2_data = np.empty(
            shape=(
                program_args.time_step_count,
                program_args.tile_count,
                program_args.tile_count,
            )
        )
        plot_grid2_data.fill(0)

    # Phase plot
    if program_args.phase_plot_all or program_args.phase_plot_one:
        plot_phase_data_x = np.empty(
            shape=(
                program_args.time_step_count,
                program_args.baseline_count,
                program_args.channel_count,
            )
        )
        plot_phase_data_x.fill(0)
        plot_phase_data_y = np.empty(
            shape=(
                program_args.time_step_count,
                program_args.baseline_count,
                program_args.channel_count,
            )
        )
        plot_phase_data_y.fill(0)

    raw_dump_file = None
    plot_dump_file = None

    if program_args.correlator_version == mwalib.MWAVersion.CorrMWAXv2:
        filename = f"{program_args.context.metafits_context.obs_id}_mwax.csv"
    else:
        filename = f"{program_args.context.metafits_context.obs_id}_mwa.csv"

    # Open a file for dumping the plot values
    if program_args.dumpplot:
        plot_dump_filename = f"plot_dump_{filename}"
        print(f"Openning {plot_dump_filename} for writing")
        plot_dump_file = open(plot_dump_filename, "w")

        # Write the csv header depending on the type of plot
        if program_args.ppd_plot:
            plot_dump_file.write("time_index, fine_chan, x, y\n")

        elif program_args.ppd_plot2:
            plot_dump_file.write("plot_number, time_index, fine_chan, x, y\n")

        elif program_args.grid_plot:
            plot_dump_file.write("unix_time, tile1, tile2, log10_scaled_power\n")

        elif program_args.grid_plot2:
            plot_dump_file.write("unix_time, tile1, tile2, log10_scaled_power\n")

        elif program_args.phase_plot_one:
            plot_dump_file.write("time_index, baseline, x, y\n")

        elif program_args.phase_plot_all:
            plot_dump_file.write("time_index, baseline, x, y\n")

    # print a csv header for the raw dump
    if program_args.dumpraw:
        raw_dump_filename = f"raw_dump_{filename}"
        print(f"Openning {raw_dump_filename} for writing")
        raw_dump_file = open(raw_dump_filename, "w")

        raw_dump_file.write(
            "unix_time, baseline, ant1, ant2, fine_ch, "
            "xx_r, xx_i, xy_r, xy_i, yx_r, yx_i, yy_r, yy_i, "
            "xx_pow, xy_pow, yx_pow, yy_pow, x_phase_deg, y_phase_deg\n"
        )

    for timestep_index, timestep in enumerate(program_args.context.timesteps):
        time_index = timestep_index - program_args.hdu_time1

        if timestep_index < program_args.hdu_time1 or timestep_index > program_args.hdu_time2:
            # print(f"Skipping timestep index {timestep_index} (out of range)")
            continue
        else:
            print(f"Processing timestep: {timestep_index} (time index: {time_index})...")

        # Read data
        data = program_args.context.read_by_baseline(
            timestep_index,
            program_args.context.provided_coarse_chan_indices[0],
        )
        data = data.reshape(
            program_args.context.metafits_context.num_baselines,
            program_args.chan_x_pols_x_vals,
        )

        baseline = 0
        selected_baseline = 0

        # Print all tile info but only for the first timestep we have
        if time_index == 0:
            print(f"QUAKTIME:{program_args.context.metafits_context.quack_time_duration_ms / 1000.0} s\n")

            print("\nUnflagged tiles:")
            print("================")
            for i in range(0, len(program_args.context.metafits_context.antennas)):
                if (
                    program_args.context.metafits_context.antennas[i].rfinput_x.flagged is False
                    and program_args.context.metafits_context.antennas[i].rfinput_y.flagged is False
                ):
                    print(
                        f"Index {i}, TileID: {program_args.context.metafits_context.antennas[i].tile_id} "
                        f"{program_args.context.metafits_context.antennas[i].tile_name}"
                        f" (rec:{program_args.context.metafits_context.antennas[i].rfinput_x.rec_number},"
                        f"slot:{program_args.context.metafits_context.antennas[i].rfinput_x.rec_slot_number})"
                    )

            print("\nFlagged tiles:")
            print("================")
            for i in range(0, len(program_args.context.metafits_context.antennas)):
                if (
                    program_args.context.metafits_context.antennas[i].rfinput_x.flagged
                    or program_args.context.metafits_context.antennas[i].rfinput_y.flagged
                ):
                    print(
                        f"Index {i}, TileID: {program_args.context.metafits_context.antennas[i].tile_id} "
                        f"{program_args.context.metafits_context.antennas[i].tile_name}"
                        f" (rec:{program_args.context.metafits_context.antennas[i].rfinput_x.rec_number},"
                        f"slot:{program_args.context.metafits_context.antennas[i].rfinput_x.rec_slot_number})"
                    )

        for i in range(0, program_args.tile2 + 1):
            for j in range(i, program_args.fits_tiles):
                # Explaining this if:
                # Line 1. Check for autos if that's what we asked for
                # Line 2. OR just be True if we didn't ask for autos only.
                # Line 3 (Applicable to cases 1 and 2 above):
                #         Check the selected tile1 and tile2 are in range
                if ((i == j and program_args.autos_only is True) or (program_args.autos_only is False)) and (
                    meets_criteria(
                        i,
                        j,
                        program_args.tile1,
                        program_args.tile2,
                        program_args.mode,
                    )
                ):
                    for chan in range(program_args.channel1, program_args.channel2 + 1):
                        index = chan * (program_args.pols * program_args.values)

                        if (
                            program_args.context.metafits_context.antennas[i].rfinput_x.flagged
                            or program_args.context.metafits_context.antennas[i].rfinput_y.flagged
                            or program_args.context.metafits_context.antennas[j].rfinput_x.flagged
                            or program_args.context.metafits_context.antennas[j].rfinput_y.flagged
                        ) and 1 == 0:
                            xx_r = 0
                            xx_i = 0

                            xy_r = 0
                            xy_i = 0

                            yx_r = 0
                            yx_i = 0

                            yy_r = 0
                            yy_i = 0

                            power_xx = 0
                            power_xy = 0
                            power_yx = 0
                            power_yy = 0

                            phase_x = 0
                            phase_y = 0

                        else:
                            xx_r = data[baseline][index]
                            xx_i = data[baseline][index + 1]

                            xy_r = data[baseline][index + 2]
                            xy_i = data[baseline][index + 3]

                            yx_r = data[baseline][index + 4]
                            yx_i = data[baseline][index + 5]

                            yy_r = data[baseline][index + 6]
                            yy_i = data[baseline][index + 7]

                            power_xx = xx_i * xx_i + xx_r * xx_r
                            power_xy = xy_i * xy_i + xy_r * xy_r
                            power_yx = yx_i * yx_i + yx_r * yx_r
                            power_yy = yy_i * yy_i + yy_r * yy_r

                            phase_x = math.degrees(math.atan2(xx_i, xx_r))
                            phase_y = math.degrees(math.atan2(yy_i, yy_r))

                        if program_args.ppd_plot:
                            plot_ppd_data_x[chan][time_index] = plot_ppd_data_x[chan][time_index] + (
                                power_xx / program_args.baseline_count
                            )
                            plot_ppd_data_y[chan][time_index] = plot_ppd_data_y[chan][time_index] + (
                                power_yy / program_args.baseline_count
                            )  # noqa: E127

                            # No idea, but the line above fails E127 in PEP8.
                            # I have no idea why. The line above it
                            # does not fail... weird

                        elif program_args.ppd_plot2:
                            plot_ppd2_data_x[time_index][selected_baseline][chan] = power_xx
                            plot_ppd2_data_y[time_index][selected_baseline][chan] = power_yy

                        elif program_args.grid_plot:
                            plot_grid_data[time_index][j][i] = plot_grid_data[time_index][j][i] + power_xx + power_yy

                        elif program_args.grid_plot2:
                            # offset index by the polarisation
                            if program_args.grid_pol == "XX":
                                plot_grid2_data[time_index][j][i] += power_xx
                            elif program_args.grid_pol == "XY":
                                plot_grid2_data[time_index][j][i] += power_xy
                            elif program_args.grid_pol == "YX":
                                plot_grid2_data[time_index][j][i] += power_yx
                            elif program_args.grid_pol == "YY":
                                plot_grid2_data[time_index][j][i] += power_yy
                            else:
                                print(
                                    "Grid Plot requires you to specify "
                                    "-gp (--gridpol) and "
                                    "takes XX,XY,YX,YY as parameters"
                                )
                                exit(-1)

                        elif program_args.phase_plot_all or program_args.phase_plot_one:
                            plot_phase_data_x[time_index][selected_baseline][chan] = phase_x
                            plot_phase_data_y[time_index][selected_baseline][chan] = phase_y

                        if program_args.dumpraw:
                            raw_dump_file.write(
                                f"{timestep.unix_time_ms / 1000.0},"
                                f"{baseline},"
                                f"{i},"
                                f"{j},"
                                f"{chan},"
                                f"{xx_r},"
                                f"{xx_i},"
                                f"{xy_r},"
                                f"{xy_i},"
                                f"{yx_r},"
                                f"{yx_i},"
                                f"{yy_r},"
                                f"{yy_i},"
                                f"{power_xx},"
                                f"{power_xy},"
                                f"{power_yx},"
                                f"{power_yy},"
                                f"{phase_x},"
                                f"{phase_y}\n"
                            )

                    selected_baseline = selected_baseline + 1

                baseline = baseline + 1

    print("Processing of data done!")

    if program_args.dumpraw:
        if raw_dump_file:
            raw_dump_file.close()

    if program_args.ppd_plot:
        convert_to_db = False
        do_ppd_plot(
            program_args.param_string,
            program_args,
            plot_ppd_data_x,
            plot_ppd_data_y,
            convert_to_db,
        )

    if program_args.ppd_plot2:
        convert_to_db = False
        do_ppd_plot2(
            program_args.param_string,
            program_args,
            plot_ppd2_data_x,
            plot_ppd2_data_y,
            convert_to_db,
        )

    if program_args.grid_plot:
        do_grid_plot(program_args.param_string, program_args, plot_grid_data)

    if program_args.grid_plot2:
        do_grid_plot2(program_args.param_string, program_args, plot_grid2_data)

    if program_args.phase_plot_all:
        do_phase_plot(
            program_args.param_string,
            program_args,
            plot_phase_data_x,
            plot_phase_data_y,
        )

    if program_args.phase_plot_one:
        do_phase_plot1(
            program_args.param_string,
            program_args,
            plot_phase_data_x,
            plot_phase_data_y,
        )

    if program_args.dumpplot:
        if plot_dump_file:
            plot_dump_file.close()

    print("view_fits Done!\n")


def do_ppd_plot(
    title,
    program_args: ViewFITSArgs,
    plot_ppd_data_x,
    plot_ppd_data_y,
    convert_to_db,
):
    # This will sum across timesteps and baselines to make a single ppd
    print("Preparing ppd plot...")

    min_db = 0

    # Convert to a dB figure
    if convert_to_db:
        for t in range(0, program_args.time_step_count):
            for c in range(0, program_args.channel_count):
                plot_ppd_data_x[c][t] = math.log10(plot_ppd_data_x[c][t] + 1) * 10
                plot_ppd_data_y[c][t] = math.log10(plot_ppd_data_y[c][t] + 1) * 10

        # Get min dB value
        # Previously had min(np.array(plot_ppd_data_x).min(),
        #                    np.array(plot_ppd_data_y).min())
        min_db = 0

    fig, ax = plt.subplots(
        figsize=(20, 10),
        nrows=1,
        ncols=1,
        squeeze=False,
        sharey="all",
        dpi=dpi,
    )
    fig.suptitle(title)

    for t in range(0, program_args.time_step_count):
        print(f"Adding data points for time ({t})...")

        # Step down the dB by the min so we have a 0 base
        for c in range(0, program_args.channel_count):
            plot_ppd_data_x[c][t] = plot_ppd_data_x[c][t] - min_db
            plot_ppd_data_y[c][t] = plot_ppd_data_y[c][t] - min_db

    # Get the current plot
    plot = ax[0][0]

    # Draw this plot
    plot.plot(plot_ppd_data_x, "o", color="blue")
    plot.plot(plot_ppd_data_y, "o", color="green")

    # Set labels
    if convert_to_db:
        plot.set_ylabel("dB", size=6)
    else:
        plot.set_ylabel("Raw value", size=6)

    plot.set_xlabel("fine channel", size=6)

    # Set plot title
    plot.set_title(f"t={program_args.unix_time1}", size=6)

    print("Saving figure...")

    # Save the final plot to disk
    if program_args.correlator_version == mwalib.MWAVersion.CorrMWAXv2:
        filename = "ppd_plot_mwax.png"
    else:
        filename = "ppd_plot_mwa.png"
    plt.savefig(filename, bbox_inches="tight", dpi=dpi)
    print(f"saved {filename}")
    plt.show()


def do_ppd_plot2(
    title,
    program_args: ViewFITSArgs,
    plot_ppd_data_x,
    plot_ppd_data_y,
    convert_to_db,
):  # noqa: C901
    print("Preparing ppd plot2...")

    # Work out layout of plots
    plots = program_args.time_step_count
    plot_rows = math.floor(math.sqrt(plots))
    plot_cols = math.ceil(plots / plot_rows)
    plot_row = 0
    plot_col = 0
    min_db = 0

    # Convert to a dB figure
    if convert_to_db:
        for t in range(0, program_args.time_step_count):
            for c in range(0, program_args.channel_count):
                for b in range(0, program_args.baseline_count):
                    plot_ppd_data_x[t][b][c] = math.log10(plot_ppd_data_x[t][b][c] + 1) * 10
                    plot_ppd_data_y[t][b][c] = math.log10(plot_ppd_data_y[t][b][c] + 1) * 10

        # Get min dB value
        min_db = min(
            np.array(plot_ppd_data_x).min(initial=0),
            np.array(plot_ppd_data_y).min(initial=0),
        )

    fig, ax = plt.subplots(
        figsize=(20, 10),
        nrows=plot_rows,
        ncols=plot_cols,
        squeeze=False,
        sharey="all",
        dpi=dpi,
    )
    fig.suptitle(title)

    for t in range(0, program_args.time_step_count):
        # print(f"Adding data points for plot({t})...")

        # Step down the dB by the min so we have a 0 base
        if min_db != 0:
            for c in range(0, program_args.channel_count):
                for b in range(0, program_args.baseline_count):
                    plot_ppd_data_x[t][b][c] = plot_ppd_data_x[t][b][c] - min_db
                    plot_ppd_data_y[t][b][c] = plot_ppd_data_y[t][b][c] - min_db

        # Get the current plot
        plot = ax[plot_row][plot_col]

        # Draw this plot
        for b in range(0, program_args.baseline_count):
            plot.plot(plot_ppd_data_x[t][b], "o", markersize=1, color="blue")
            plot.plot(plot_ppd_data_y[t][b], "o", markersize=1, color="green")

        # Set labels
        if convert_to_db:
            plot.set_ylabel("dB", size=6)
        else:
            plot.set_ylabel("Raw value", size=6)

        plot.set_xlabel("fine channel", size=6)

        # Set plot title
        plot.set_title(f"t={t + program_args.time_step1}", size=6)

        # Increment so we know which plot we are on
        if plot_col < plot_cols - 1:
            plot_col = plot_col + 1
        else:
            plot_row = plot_row + 1
            plot_col = 0

    print("Saving figure...")

    # Save the final plot to disk
    if program_args.correlator_version == mwalib.MWAVersion.CorrMWAXv2:
        filename = "ppd_plot2_mwax.png"
    else:
        filename = "ppd_plot2_mwa.png"
    plt.savefig(filename, bbox_inches="tight", dpi=dpi)
    print(f"saved {filename}")
    plt.show()


def do_grid_plot(title, program_args: ViewFITSArgs, plot_grid_data):  # noqa: C901
    print("Preparing grid plot...")

    # Work out layout of plots
    plots = program_args.time_step_count
    plot_rows = math.floor(math.sqrt(plots))
    plot_cols = math.ceil(plots / plot_rows)
    plot_row = 0
    plot_col = 0

    if 1 == 1:
        for time_index in range(0, program_args.time_step_count):
            for t1 in range(0, program_args.tile_count):
                for t2 in range(t1, program_args.tile_count):
                    plot_grid_data[time_index][t2][t1] = math.log10(plot_grid_data[time_index][t2][t1] + 1) * 10

        # Get min dB value
        # np_array_nonzero = plot_grid_data[plot_grid_data > 1]
        # min_db = np_array_nonzero.min()

        # Apply min_db but only to values > 1
        # plot_grid_data = np.where(plot_grid_data > 1,
        # plot_grid_data - min_db, plot_grid_data)

    fig, ax = plt.subplots(
        figsize=(30, 30),
        nrows=plot_rows,
        ncols=plot_cols,
        squeeze=False,
        sharex="all",
        sharey="all",
        dpi=dpi,
    )
    fig.suptitle(title)

    n_step = math.ceil(plots / 1.25)

    if n_step % 2 != 0:
        n_step = n_step + 1

    if n_step % 4 != 0:
        n_step = n_step - 2

    if n_step <= 1:
        n_step = 1
    else:
        n_step = n_step * 2

    if n_step > 16:
        n_step = 16

    for time_index in range(0, program_args.time_step_count):
        for t1 in range(0, program_args.tile_count):
            for t2 in range(t1, program_args.tile_count):
                print(
                    time_index,
                    program_args.tile1 + t1,
                    program_args.tile1 + t2,
                    plot_grid_data[time_index][t2][t1],
                )

        print(f"Adding data points for plot({time_index})...")

        # Get the current plot
        plot = ax[plot_row][plot_col]

        plot.imshow(plot_grid_data[time_index], cmap="inferno", interpolation="None")

        plot.set_title(f"t={time_index + program_args.time_step1}", size=6)
        plot.set_xticks(np.arange(program_args.tile_count, step=n_step))
        plot.set_yticks(np.arange(program_args.tile_count, step=n_step))
        plot.set_xticklabels(np.arange(program_args.tile1, program_args.tile2 + 1, step=n_step))
        plot.set_yticklabels(np.arange(program_args.tile1, program_args.tile2 + 1, step=n_step))

        # Set labels
        # Only do y label for first col
        if plot_col == 0:
            plot.set_ylabel("ant2", size=6)

        # Only do x label for final row
        if plot_row == plot_rows - 1:
            plot.set_xlabel("ant1", size=6)

        plt.setp(
            plot.get_xticklabels(),
            rotation=90,
            ha="right",
            va="center",
            rotation_mode="anchor",
        )

        # Increment so we know which plot we are on
        if plot_col < plot_cols - 1:
            plot_col = plot_col + 1
        else:
            plot_row = plot_row + 1
            plot_col = 0

    print("Saving figure...")
    if program_args.correlator_version == mwalib.MWAVersion.CorrMWAXv2:
        filename = "grid_plot_mwax.png"
    else:
        filename = "grid_plot_mwa.png"
    plt.savefig(filename, bbox_inches="tight", dpi=dpi)
    print(f"saved {filename}")
    plt.show()


def do_grid_plot2(title, program_args: ViewFITSArgs, plot_grid_data):  # noqa: C901
    print("Preparing grid plot2...")

    # Work out layout of plots
    plots = program_args.time_step_count
    plot_rows = math.floor(math.sqrt(plots))
    plot_cols = math.ceil(plots / plot_rows)
    plot_row = 0
    plot_col = 0

    # Determine scaling value
    scaling_value: float = np.max(plot_grid_data)

    # Apply log10 and scaling value
    for time_index in range(0, program_args.time_step_count):
        for t1 in range(0, program_args.tile_count):
            for t2 in range(t1, program_args.tile_count):
                value: float = plot_grid_data[time_index][t2][t1] / scaling_value
                try:
                    if abs(value) < 0.0000001:
                        plot_grid_data[time_index][t2][t1] = 0
                    else:
                        plot_grid_data[time_index][t2][t1] = math.log10(value * 10000000)
                except Exception as e:
                    print(f"Exception trying math.log10({value * 10000000} / {scaling_value})")
                    raise e

    # Now print some stats
    print(
        f"scaling value was: {scaling_value}; now scaled and "
        f"log10'd-> min = {np.min(plot_grid_data)}; "
        f"max = {np.max(plot_grid_data)}"
    )

    fig, ax = plt.subplots(
        figsize=(30, 30),
        nrows=plot_rows,
        ncols=plot_cols,
        squeeze=False,
        sharex="all",
        sharey="all",
        dpi=dpi,
    )
    fig.suptitle(title)

    n_step = 1

    for time_index in range(0, program_args.time_step_count):
        # for t1 in range(0, program_args.tile_count):
        #    for t2 in range(t1, program_args.tile_count):
        #        print(time_index, program_args.tile1 + t1,
        #        program_args.tile1 + t2, plot_grid_data[time_index][t2][t1])

        print(f"Adding data points for plot({time_index})...")

        # Get the current plot
        plot = ax[plot_row][plot_col]

        plot.imshow(plot_grid_data[time_index], cmap="inferno", interpolation="None")

        plot.set_title(f"t={time_index + program_args.time_step1}", size=6)
        plot.set_xticks(np.arange(program_args.tile_count, step=n_step))
        plot.set_yticks(np.arange(program_args.tile_count, step=n_step))
        plot.set_xticklabels(np.arange(program_args.tile1, program_args.tile2 + 1, step=n_step))
        plot.set_yticklabels(np.arange(program_args.tile1, program_args.tile2 + 1, step=n_step))

        # Set labels
        # Only do y label for first col
        if plot_col == 0:
            plot.set_ylabel("ant2", size=6)

        # Only do x label for final row
        if plot_row == plot_rows - 1:
            plot.set_xlabel("ant1", size=6)

        plt.setp(
            plot.get_xticklabels(),
            rotation=90,
            ha="right",
            va="center",
            rotation_mode="anchor",
        )

        # Increment so we know which plot we are on
        if plot_col < plot_cols - 1:
            plot_col = plot_col + 1
        else:
            plot_row = plot_row + 1
            plot_col = 0

    print("Saving figure...")
    if program_args.correlator_version == mwalib.MWAVersion.CorrMWAXv2:
        filename = f"grid_plot2_{program_args.grid_pol}_mwax.png"
    else:
        filename = f"grid_plot2_{program_args.grid_pol}_mwa.png"

    plt.savefig(filename, bbox_inches="tight", dpi=dpi)
    print(f"saved {filename}")
    plt.show()


def do_phase_plot(title, program_args: ViewFITSArgs, plot_phase_data_x, plot_phase_data_y):  # noqa: C901
    print("Preparing phase plot...")

    # Work out layout of plots
    plots = program_args.baseline_count

    print(
        f"Timesteps: {program_args.time_step_count}, "
        f"baselines: {program_args.baseline_count}, "
        f"tiles: {program_args.tile_count}, "
        f"channels: {program_args.channel_count}, "
        f"plots: {plots}"
    )

    plot_cols = program_args.tile_count
    plot_rows = program_args.tile_count

    plot_row = 0
    plot_col = 0
    baseline = 0

    fig, ax = plt.subplots(
        figsize=(11.6, 8.3),
        nrows=plot_rows,
        ncols=plot_cols,
        squeeze=False,
        sharex="all",
        sharey="all",
        dpi=dpi,
    )
    fig.suptitle(title)

    for i in range(0, program_args.tile_count):
        for j in range(i, program_args.tile_count):
            if program_args.phase_plot_one:
                if not (i == 0 and j == (program_args.tile_count - 1)):
                    # skip this plot
                    print(f"{i} vs {j} skip")
                    baseline = baseline + 1
                    continue

            print(f"Adding data points for plot({i},{j})...")
            channel_list = range(program_args.channel1, program_args.channel2 + 1)

            # Get the current plot
            plot = ax[plot_row][plot_col]

            # Do plots
            for t in range(0, program_args.time_step_count):
                # print(program_args.context.num_timesteps)
                # print(f"Time {t}")
                # print("X")
                # print(plot_phase_data_x[t][baseline])
                # print("Y")
                # print(plot_phase_data_y[t][baseline])

                plot.plot(
                    channel_list,
                    plot_phase_data_x[t][baseline],
                    "o",
                    markersize=1,
                    color="blue",
                )
                plot.plot(
                    channel_list,
                    plot_phase_data_y[t][baseline],
                    "o",
                    markersize=1,
                    color="green",
                )

            # Set labels
            # Only do y label for first col
            if plot_col == 0:
                plot.set_ylabel("phase (deg)", size=6)

            # Only do x label for final row
            if plot_row == plot_rows - 1:
                plot.set_xlabel("fine channel", size=6)

            # Ensure Y axis goes from -180 to 180
            plot.set_ylim([-180, 180])

            tile1_name = program_args.context.metafits_context.antennas[i + program_args.tile1].tile_name
            tile2_name = program_args.context.metafits_context.antennas[j + program_args.tile1].tile_name
            plot.set_title(f"{tile1_name} v {tile2_name}", size=6, pad=2)

            # Increment so we know which plot we are on
            if plot_col < plot_cols - 1:
                plot_col = plot_col + 1
            else:
                plot_row = plot_row + 1
                plot_col = plot_row

            # Increment baseline
            baseline = baseline + 1

    print("Saving figure...")
    # Save final plot to disk
    if program_args.correlator_version == mwalib.MWAVersion.CorrMWAXv2:
        filename = "phase_plot_mwax.png"
    else:
        filename = "phase_plot_mwa.png"

    plt.savefig(filename, bbox_inches="tight", dpi=dpi)
    print(f"saved {filename}")
    plt.show()


def do_phase_plot1(title, program_args: ViewFITSArgs, plot_phase_data_x, plot_phase_data_y):  # noqa: C901
    print("Preparing phase plot1...")

    plots = program_args.channel_count
    sqrt_of_plots: int = math.floor(math.sqrt(plots))

    plot_cols = sqrt_of_plots
    plot_rows = program_args.channel_count // sqrt_of_plots

    print(plot_rows, plot_cols)

    # Work out layout of plots
    print(
        f"Timesteps: {program_args.time_step_count}, "
        f"baselines: {program_args.baseline_count}, "
        f"tiles: {program_args.tile_count}, "
        f"channels: {program_args.channel_count}, "
        f"plots: {plots}"
    )

    plot_row = 0
    plot_col = 0
    baseline = 0
    fine_chan = 0

    fig, ax = plt.subplots(
        figsize=(11.6, 8.3),
        nrows=plot_rows,
        ncols=plot_cols,
        squeeze=False,
        sharex="all",
        sharey="all",
        dpi=dpi,
    )
    fig.suptitle(title)

    print("Adding data points for plot...")
    timestep_list = range(program_args.time_step1, program_args.time_step2 + 1)

    for plot_row in range(0, plot_rows):
        for plot_col in range(0, plot_cols):
            plot_data_x = plot_phase_data_x[0 : program_args.time_step_count, baseline, fine_chan]
            plot_data_y = plot_phase_data_y[0 : program_args.time_step_count, baseline, fine_chan]

            # Get the current plot
            plot = ax[plot_row][plot_col]

            # Do plots
            plot.plot(
                timestep_list,
                plot_data_x,
                "o",
                markersize=1,
                color="blue",
            )
            plot.plot(
                timestep_list,
                plot_data_y,
                "o",
                markersize=1,
                color="green",
            )

            # Set labels
            plot.set_ylabel("phase (deg)", size=6)
            plot.set_xlabel("timestep", size=6)

            # Ensure Y axis goes from -180 to 180
            plot.set_ylim([-180, 180])

            plot.set_title(
                f"Fine ch: {program_args.context.metafits_context.metafits_fine_chan_freqs_hz[fine_chan] / (1000 * 1000):.3f} MHz",
                size=6,
                pad=2,
            )
            fine_chan += 1

    print("Saving figure...")
    # Save final plot to disk
    if program_args.correlator_version == mwalib.MWAVersion.CorrMWAXv2:
        filename = "phase_plot1_mwax.png"
    else:
        filename = "phase_plot1_mwa.png"

    plt.savefig(filename, bbox_inches="tight", dpi=dpi)
    print(f"saved {filename}")
    plt.show()


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("filename", help="fits filename")
    parser.add_argument("-m", "--metafits", required=True, help="Path to the metafits file.")
    parser.add_argument(
        "-t1",
        "--timestep1",
        required=False,
        help="timestep start (1 based index)",
        default=1,
        type=int,
    )
    parser.add_argument(
        "-t2",
        "--timestep2",
        required=False,
        help="timestep end (defaults to last index)",
        default=-1,
        type=int,
    )
    parser.add_argument(
        "-a1",
        "--ant1",
        required=False,
        help="antenna (start)",
        default=-1,
        type=int,
    )
    parser.add_argument(
        "-a2",
        "--ant2",
        required=False,
        help="antenna (end)",
        default=-1,
        type=int,
    )
    parser.add_argument(
        "-c1",
        "--channel1",
        required=False,
        help="fine channel number (start)",
        default=-1,
        type=int,
    )
    parser.add_argument(
        "-c2",
        "--channel2",
        required=False,
        help="fine channel number (end)",
        default=-1,
        type=int,
    )
    parser.add_argument(
        "-a",
        "--autosonly",
        required=False,
        help="Only output the auto correlations",
        action="store_true",
    )
    parser.add_argument(
        "-p",
        "--ppdplot",
        required=False,
        help="Create a ppd plot",
        action="store_true",
    )
    parser.add_argument(
        "-p2",
        "--ppdplot2",
        required=False,
        help="Create a ppd plot that does not sum across all baselines. ie it plots all baselines",
        action="store_true",
    )
    parser.add_argument(
        "-g",
        "--gridplot",
        required=False,
        help="Create a grid / baseline plot",
        action="store_true",
    )
    parser.add_argument(
        "-g2",
        "--gridplot2",
        required=False,
        help="Create a grid / baseline plot but show a single pol (XX,XY,YX,YY) for each tile. Use gridpol to specify",
        action="store_true",
    )
    parser.add_argument(
        "-gp",
        "--gridpol",
        required=False,
        help="If gridplot2 used, use this to specify the pol. Default is 'XX'",
        default="XX",
    )
    parser.add_argument(
        "-ph",
        "--phaseplot_all",
        required=False,
        help="Will do a phase plot for all baselines for given antennas and timesteps",
        action="store_true",
    )

    parser.add_argument(
        "-ph1",
        "--phaseplot_one",
        required=False,
        help="Will do a phase plot for given baseline and timesteps",
        action="store_true",
    )

    parser.add_argument(
        "-o",
        "--mode",
        required=True,
        help="How to interpret a1 and a2: RANGE or BASELINE",
    )

    parser.add_argument(
        "-dr",
        "--dump-raw",
        required=False,
        help="Dump the raw data",
        action="store_true",
    )
    parser.add_argument(
        "-dp",
        "--dump-plot",
        required=False,
        help="Dump the plot data",
        action="store_true",
    )
    args = vars(parser.parse_args())

    parsed_args = ViewFITSArgs(args)

    peek_fits(parsed_args)