This repository was archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfringe_info.cc
1169 lines (1042 loc) · 40.2 KB
/
fringe_info.cc
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
/* Copyright (c) 2007 Joint Institute for VLBI in Europe (Netherlands)
* All rights reserved.
*
* Author(s): Nico Kruithof <[email protected]>, 2007
: Aard Keimpema <[email protected]>, 2010
$Id$
*/
#include <fstream>
#include "fringe_info.h"
#define MAX_SNR_VALUE 8
#define MIN_SNR_VALUE 3
Fringe_info::
Fringe_info(const Output_header_baseline &header,
const std::vector< std::complex<float> > &data_freq_,
const std::vector< std::complex<float> > &data_lag_)
: header(header), data_freq(data_freq_), data_lag(data_lag_),
initialised(true) {
assert(data_freq_.size() == data_lag_.size() + 1);
}
bool Fringe_info::operator==(const Fringe_info &other) const {
assert(initialised);
assert(other.initialised);
return (header == other.header);
}
bool Fringe_info::operator<(const Fringe_info &other) const {
assert(initialised);
assert(other.initialised);
return (header < other.header);
}
void Fringe_info::plot(char *filename, char *filename_large, char *title,
SPACE space, VALUE value, double frequency, double bandwidth) const {
std::vector<float> data;
if (space == FREQUENCY) {
data.resize(data_freq.size());
switch (value) {
case REAL: {
for (size_t i=0; i<data.size(); i++)
data[i] = data_freq[i].real();
break;
}
case IMAG: {
for (size_t i=0; i<data.size(); i++)
data[i] = data_freq[i].imag();
break;
}
case ABS: {
for (size_t i=0; i<data.size(); i++)
data[i] = std::abs(data_freq[i]);
break;
}
case PHASE: {
for (size_t i=0; i<data.size(); i++)
data[i] = std::arg(data_freq[i]);
break;
}
}
} else {
assert(space == LAG);
size_t size = data_lag.size();
data.resize(size);
switch (value) {
case REAL: {
for (size_t i=0; i<data.size(); i++)
data[i] = data_lag[i].real();
break;
}
case IMAG: {
for (size_t i=0; i<data.size(); i++)
data[i] = data_lag[i].imag();
break;
}
case ABS: {
for (size_t i=0; i<data.size(); i++)
data[i] = std::abs(data_lag[i]);
break;
}
case PHASE: {
for (size_t i=0; i<data.size(); i++)
data[i] = std::arg(data_lag[i]);
break;
}
}
}
char cmd[80];
gnuplot_ctrl *g = gnuplot_init();
// This works on huygens
gnuplot_cmd(g, (char*)"set terminal png small size 300,200");
// This works on das3
//gnuplot_cmd(g, (char*)"set terminal png small picsize 300 200");
snprintf(cmd, 80, "set output \"%s\"", filename);
gnuplot_cmd(g, cmd);
gnuplot_setstyle(g, (char*)"lines");
gnuplot_plot_x(g, &data[0], data.size(), title) ;
gnuplot_close(g);
g = gnuplot_init();
// This works on huygens
gnuplot_cmd(g, (char*)"set terminal png large size 1024,768");
// This works on das3
// gnuplot_cmd(g, (char*)"set terminal png large picsize 1024 768");
snprintf(cmd, 80, "set output \"%s\"", filename_large);
gnuplot_cmd(g, cmd);
gnuplot_setstyle(g, (char*)"lines");
if (space == FREQUENCY) {
std::vector<float> freqs;
freqs.resize(data.size());
for(int i = 0; i < freqs.size(); i++)
freqs[i] = frequency + i * bandwidth / freqs.size();
gnuplot_set_xlabel (g, (char*)"[mhz]");
gnuplot_plot_xy(g, &freqs[0], &data[0], data.size(), title) ;
}else
gnuplot_plot_x(g, &data[0], data.size(), title) ;
gnuplot_close(g);
}
float Fringe_info::signal_to_noise_ratio() const {
const size_t N = data_lag.size();
int index_max = max_value_offset();
index_max = (index_max+N)%N;
if (data_lag[index_max] == std::complex<float>(0,0)) return 0;
//return noise rms in array, skip 10 % around maximum
std::complex<float> mean(0,0);
int n2avg=0;
for (size_t i=0 ; i< N ; i++) {
// difference in the range [0,n)
size_t pos_diff = (N+index_max-i)%N;
// difference in the range [-n/2,n/2)
pos_diff = std::abs((int)((pos_diff+N/2)%N - N/2));
if (pos_diff > N/20) {
// skip 10% arround lag for max which is at imax
n2avg++;
mean += std::abs(data_lag[i]);
}
}
mean /= n2avg;
float sum = 0;
for (size_t i=0 ; i< N ; i++) {
// difference in the range [0,n)
size_t pos_diff = (N+index_max-i)%N;
// difference in the range [-n/2,n/2)
pos_diff = std::abs((int)((pos_diff+N/2)%N -
N/2));
if (pos_diff > N/20) {
sum += norm(std::abs(data_lag[i])-mean);
}
}
return sqrt(std::norm(std::abs(data_lag[index_max])-mean)/(sum/n2avg));
}
int Fringe_info::max_value_offset() const {
const size_t N = data_lag.size();
int index_max = 0;
for (size_t i=1; i<N; i++) {
if (std::abs(data_lag[i]) > std::abs(data_lag[index_max]))
index_max = i;
}
return index_max;
}
// Fringe_info_container
Fringe_info_container::
Fringe_info_container(FILE *input, bool stop_at_eof) : input(input) {
// read-in the global header
read_data_from_file(sizeof(Output_header_global),
(char *)&global_header, stop_at_eof);
if (eof()) return;
data_freq.resize(global_header.number_channels+1);
data_lag.resize(global_header.number_channels);
fft.resize(global_header.number_channels); // FIXME : THIS SHOULD BE 2*NCHAN
// Read the first timeslice header:
read_data_from_file(sizeof(Output_header_timeslice),
(char*)&last_timeslice_header, stop_at_eof);
if (eof()) return;
// Read the UVW coordinates, these are not used at the moment
for(int i=0 ; i<last_timeslice_header.number_uvw_coordinates ; i++){
struct Output_uvw_coordinates uvw_coordinates;
read_data_from_file(sizeof(Output_uvw_coordinates),
(char*)&uvw_coordinates, stop_at_eof);
if (eof()) return;
}
// Read in the bit statistics and process them
new_statistics.resize(last_timeslice_header.number_statistics);
read_data_from_file(sizeof(Output_header_bitstatistics)*new_statistics.size(),
(char*)&new_statistics[0], stop_at_eof);
assert(last_timeslice_header.number_baselines != 0);
}
bool Fringe_info_container::eof() {
return feof(input);
}
void
Fringe_info_container::read_data_from_file(int to_read, char * data,
bool stop_at_eof) {
while (!(stop_at_eof && feof(input))) {
int read = fread(data, to_read, 1, input);
if (read == 1) {
return;
} else if (read == 0) {
if (ferror(input)) {
exit(-1);
}
sleep(1);
}
}
}
void
Fringe_info_container::read_plots(bool stop_at_eof) {
// Clear previous plots
plots.clear();
first_timeslice_header = last_timeslice_header;
bool first = true;
while (first_timeslice_header.integration_slice ==
last_timeslice_header.integration_slice) {
process_new_bit_statistics();
int n_baselines = last_timeslice_header.number_baselines;
for (int i=0; i<n_baselines; i++) {
// Read the header of the baseline
Output_header_baseline baseline_header;
read_data_from_file(sizeof(Output_header_baseline),
(char*)&baseline_header,
stop_at_eof && (!first));
if (baseline_header.weight == -1) {
return;
}
// Read the data
read_data_from_file(data_freq.size()*sizeof(std::complex<float>),
(char *)&data_freq[0],
stop_at_eof && (!first));
// Reverse the lowerside bands, so that channels are in increasing frequency order
if(baseline_header.sideband == 0){
for(int j = 0, N = data_freq.size() - 1 ; j <= N / 2; j++){
std::complex<float> temp = data_freq[j];
data_freq[j] = data_freq[N-j];
data_freq[N-j] = temp;
}
}
fft.ifft(&data_freq[0], &data_lag[0]);
{ // Move the fringe to the center of the plot
std::vector< std::complex<float> > tmp = data_lag;
const size_t size = data_lag.size();
for (size_t i=0; i<size; i++) {
data_lag[i] = tmp[(i+size/2)%size];
}
}
set_plot(Fringe_info(baseline_header, data_freq, data_lag));
}
{ // Read the next timeslice header
read_data_from_file(sizeof(Output_header_timeslice),
(char*)&last_timeslice_header, stop_at_eof);
if (last_timeslice_header.number_baselines == 0) {
return;
}
first = false;
// Read the UVW coordinates, these are not used at the moment
for(int i=0 ; i<last_timeslice_header.number_uvw_coordinates ; i++){
struct Output_uvw_coordinates uvw_coordinates;
read_data_from_file(sizeof(Output_uvw_coordinates),
(char*)&uvw_coordinates, stop_at_eof);
if (eof()) return;
}
// Read in the bit statistics and process them
new_statistics.resize(last_timeslice_header.number_statistics);
read_data_from_file(sizeof(Output_header_bitstatistics)*new_statistics.size(),
(char*)&new_statistics[0], stop_at_eof);
}
}
}
void
Fringe_info_container::process_new_bit_statistics(){
for(int i=0;i<new_statistics.size();i++){
statistics.insert(new_statistics[i]);
}
}
void
Fringe_info_container::get_bbc(const Vex &vex, std::vector<std::string> &stations,
std::string &setup_station, std::string &mode,
std::vector< std::vector<int> > &bbcs,
std::vector<double> &bandwith)
{
Vex::Node root_node = vex.get_root_node();
for(int station=0; station < stations.size(); station++){
std::string freq = vex.get_frequency(mode, stations[station]);
std::string bbc = vex.get_BBC(mode, stations[station]);
std::vector<int> bbc_list;
if(freq != std::string()){
bool get_bandwidths = (bandwith.size() == 0)? true : false;
for(Vex::Node::iterator freq_it = root_node["FREQ"][freq]->begin("chan_def");
freq_it != root_node["FREQ"][freq]->end("chan_def"); freq_it++){
std::string bbc_name=(*freq_it)[5]->to_string();
if((get_bandwidths) && (stations[station] == setup_station)){
bandwith.push_back((*freq_it)[3]->to_double_amount("MHz"));
}
// Find the physical BBC number
for(Vex::Node::iterator bbc_it = root_node["BBC"][bbc]->begin("BBC_assign");
bbc_it != root_node["BBC"][bbc]->end("BBC_assign"); bbc_it++){
std::string cur_bbc = (*bbc_it)[0]->to_string();
if(cur_bbc == bbc_name){
int bbc_index = (*bbc_it)[1]->to_int();
bbc_list.push_back(bbc_index);
}
}
}
}
bbcs.push_back(bbc_list);
}
}
bool
Fringe_info_container::get_channels(const Vex &vex, const std::string &mode, std::vector<Channel> &channels)
{
Vex::Node root_node = vex.get_root_node();
Vex::Node::iterator mode_it = root_node["MODE"][mode];
std::string freq_node = mode_it->begin("FREQ")[0]->to_string();
std::string station = mode_it->begin("FREQ")[1]->to_string();
std::string if_node = vex.get_IF(mode,station);
std::string bbc_node = vex.get_BBC(mode,station);
// Get sorted list of frequencies
std::set<double> freq_set;
for (Vex::Node::iterator chan_it = root_node["FREQ"][freq_node]->begin("chan_def");
chan_it != root_node["FREQ"][freq_node]->end("chan_def"); ++chan_it) {
freq_set.insert((*chan_it)[1]->to_double_amount("MHz")*1000000);
}
channels.resize(0);
int freq_nr;
double freq=-1;
for (Vex::Node::iterator chan_it = root_node["FREQ"][freq_node]->begin("chan_def");
chan_it != root_node["FREQ"][freq_node]->end("chan_def"); ++chan_it) {
Channel new_chan;
double new_freq = (*chan_it)[1]->to_double_amount("MHz")*1000000;
if(new_freq!=freq){
freq=new_freq;
freq_nr=0;
for(std::set<double>::iterator f_it = freq_set.begin(); f_it != freq_set.end(); f_it++){
if((*f_it) == freq)
break;
freq_nr++ ;
}
}
new_chan.frequency_nr = freq_nr;
new_chan.frequency = new_freq;
new_chan.sideband=(*chan_it)[2]->to_char()=='L'?0:1;
std::string bbc = (*chan_it)[5]->to_string();
std::string if_name;
for (Vex::Node::const_iterator bbc_block = vex.get_root_node()["BBC"][bbc_node]->begin();
bbc_block != vex.get_root_node()["BBC"][bbc_node]->end(); ++bbc_block) {
if(bbc_block[0]->to_string()==bbc){
if_name = bbc_block[2]->to_string();
break;
}
}
char pol = vex.polarisation(if_node, if_name);
new_chan.polarization=(pol=='R')?0:1;
channels.push_back(new_chan);
}
return true;
}
void
Fringe_info_container::print_html(const Vex &vex, char *vex_filename, std::string setup_station_) {
// Array with the station names
std::vector<std::string> stations;
// Array with frequencies for a channel nr
std::vector<double> frequencies;
std::vector<double> bandwidths;
std::vector< std::vector<int> > bbcs;
const Vex::Node root_node = vex.get_root_node();
// Get an alphabetically sorted list of stations
std::set<std::string> station_names;
for (Vex::Node::const_iterator it = root_node["STATION"]->begin();
it != root_node["STATION"]->end(); it++) {
station_names.insert(it.key());
}
for (std::set<std::string>::iterator it = station_names.begin();
it != station_names.end(); it++) {
stations.push_back(*it);
}
std::string setup_station = (setup_station_=="") ? stations[0] : setup_station_;
std::ofstream index_html("index2.html");
assert(index_html.is_open());
index_html.precision(4);
index_html << "<html><head>" << std::endl
<< " <title>SFXC output - "<< global_header.experiment
<< "</title>" << std::endl
<< " <style> BODY,TH,TD{font-size: 10pt }</style>" << std::endl
<< "</head>"
<<"<body>"
<< std::endl;
index_html << "<script language=\"JavaScript\"><!--" << std::endl
<< "function show(imageSrc) {" << std::endl
<< " if (document.images) document.images['plot_image'].src"
<< " = imageSrc;" << std::endl
<< "}" << std::endl
<< "//--></script>" << std::endl
<< std::endl;
index_html << "<a href='" << vex_filename << "'>Vex file</a> -- "
<< std::endl;
double integration_time = global_header.integration_time/1000000.;
double sec = (global_header.start_time +
integration_time * first_timeslice_header.integration_slice);
Date start_time(global_header.start_year, global_header.start_day, (int) sec);
std::string mode = vex.get_mode(vex.get_scan_name(start_time));
vex.get_frequencies(mode, setup_station, frequencies);
get_bbc(vex, stations, setup_station, mode, bbcs, bandwidths);
index_html << " Integration time: "
<< integration_time << "s"
<< " -- Start of the integration: "
<< Date(global_header.start_year,
global_header.start_day,
(int)sec).to_string()
<< (sec-std::floor(sec))*1000 << "ms"
<< std::endl;
{ // Print the table
index_html << "<table border=1 bgcolor='#dddddd' cellspacing=0>" << std::endl;
// Print header
std::vector<int> autos;
std::vector< std::pair<int,int> > crosses;
const Fringe_info &first_plot = get_first_plot();
int freq = first_plot.header.frequency_nr;
int sideband = first_plot.header.sideband;
int pol1 = first_plot.header.polarisation1;
int pol2 = first_plot.header.polarisation2;
{ // Compute nAutos and nCrosses
for (iterator it = plots.begin(); it != plots.end(); it++) {
if ((it->header.frequency_nr == freq) &&
(it->header.sideband == sideband) &&
(it->header.polarisation1 == pol1) &&
(it->header.polarisation2 == pol2)) {
if (it->header.station_nr1 == it->header.station_nr2) {
autos.push_back(it->header.station_nr1);
} else {
crosses.push_back(std::make_pair(it->header.station_nr1,
it->header.station_nr2));
}
}
}
{ // first row
index_html << "<tr>" << std::endl;
index_html << " <th rowspan=2>" << global_header.experiment << "</th>" << std::endl;
index_html << " <th colspan="<< autos.size() << ">Auto correlations</th>" << std::endl;
index_html << " <th colspan="<< crosses.size() << ">Cross correlations</th>" << std::endl;
index_html << "</tr>" << std::endl;
}
{ // second row
index_html << "<tr>" << std::endl;
// autos
for (iterator it = plots.begin(); it != plots.end(); it++) {
if ((it->header.frequency_nr == freq) &&
(it->header.sideband == sideband) &&
(it->header.polarisation1 == pol1) &&
(it->header.polarisation2 == pol2)) {
if (it->header.station_nr1 == it->header.station_nr2) {
assert(it->header.station_nr1 < stations.size());
index_html << "<th>" << stations[it->header.station_nr1] << "</th>";
}
}
}
// crosses
for (iterator it = plots.begin(); it != plots.end(); it++) {
if ((it->header.frequency_nr == freq) &&
(it->header.sideband == sideband) &&
(it->header.polarisation1 == pol1) &&
(it->header.polarisation2 == pol2)) {
if (it->header.station_nr1 != it->header.station_nr2) {
assert(it->header.station_nr1 < stations.size());
assert(it->header.station_nr2 < stations.size());
index_html << "<th>"
<< stations[it->header.station_nr1] << "-"
<< stations[it->header.station_nr2] << "</th>";
}
}
}
char filename[80], filename_large[80], title[80];
generate_filename(filename, filename_large, title, 80, first_plot,
Fringe_info::FREQUENCY, Fringe_info::ABS);
index_html << "<td rowspan=99><img src=\""
<< filename << "\" name=\"plot_image\"></td>" << std::endl;
index_html << "</tr>" << std::endl;
}
{ // Print content of the table
assert(first_plot.header.station_nr1 == first_plot.header.station_nr2);
const Fringe_info &first_plot = get_first_plot();
int curr_freq = first_plot.header.frequency_nr;
int curr_sideband = first_plot.header.sideband;
int curr_pol1 = first_plot.header.polarisation1;
int curr_pol2 = first_plot.header.polarisation2;
begin_data_row(index_html,
frequencies,
first_plot);
size_t column = 0;
int freq_index=0;
for (iterator it = plots.begin(); it != plots.end(); it++) {
double bandwidth = bandwidths[it->header.frequency_nr];
double frequency = frequencies[it->header.frequency_nr] / 1000000 -
(1 - curr_sideband) * bandwidth;
if (!((it->header.frequency_nr == curr_freq) &&
(it->header.sideband == curr_sideband) &&
(it->header.polarisation1 == curr_pol1) &&
(it->header.polarisation2 == curr_pol2))) {
curr_freq = it->header.frequency_nr;
curr_sideband = it->header.sideband;
curr_pol1 = it->header.polarisation1;
curr_pol2 = it->header.polarisation2;
column = 0;
if(curr_pol1 == curr_pol2)
freq_index++; // don't increment when doing cross-polarizations
end_data_row(index_html);
begin_data_row(index_html,
frequencies,
*it);
}
// Print one plot
if (it->header.station_nr1 == it->header.station_nr2) {
int bbc = bbcs[(int)it->header.station_nr1][freq_index];
print_auto(index_html, *it, bbc, frequency, bandwidth);
} else {
if (column < autos.size()) {
index_html << "<td colspan='" << autos.size()-column << "'>Cross hands</td>";
column=autos.size();
}
print_cross(index_html, *it, frequency, bandwidth);
}
column ++;
index_html << "\n ";
}
}
end_data_row(index_html);
index_html << "</table>" << std::endl;
}
// Print the bit statistics
print_html_bitstatistics(vex, mode, index_html);
index_html << "</html>" << std::endl;
index_html.close();
// Atomic update
rename("index2.html", "index.html");
}
}
std::string Fringe_info_container::get_statistics_color(int64_t val, int64_t N){
// Compute color for the html_plot_page according to how many standard
// deviations val is away from the average assuming a binomial distribution
// where 0 and 1 are equally likely.
if(N==0)
return std::string("#FF0000");
double std = sqrt(N)/2;
double n_std = fabs((val-N/2)*1./std);
if(n_std <= 2){
char color[8];
int color_val = 255-128*n_std/2;
snprintf(color, 7, "#00%2X00", color_val);
return std::string(color);
}else if (n_std <= 3)
return std::string("#FF8C00");
return std::string("#FF0000");
}
void Fringe_info_container::
print_html_bitstatistics(const Vex &vex, const std::string &mode, std::ofstream &index_html){
// Array with all channels sorted on channel nr
std::vector<Channel> channels;
get_channels(vex, mode, channels);
const Vex::Node root_node = vex.get_root_node();
// Get an alphabetically sorted list of stations
std::vector<std::string> stations;
std::set<int> stations_in_experiment;
{
std::set<std::string> station_set;
for (Vex::Node::const_iterator it = root_node["STATION"]->begin();
it != root_node["STATION"]->end(); it++) {
station_set.insert(it.key());
}
for (std::set<std::string>::iterator it = station_set.begin();
it != station_set.end(); it++) {
stations.push_back(*it);
}
for(statistics_set::iterator it=statistics.begin(); it != statistics.end() ; it++){
stations_in_experiment.insert(it->station_nr);
}
}
index_html << "<h1> Sampler statistics </h1><br>" << std::endl;
// Iterate over all stations
for(std::set<int>::iterator stat_it = stations_in_experiment.begin();
stat_it != stations_in_experiment.end() ; stat_it++){
// Write table header
index_html << "<table border=1 bgcolor='#dddddd' cellspacing=0>" << std::endl;
index_html << "<tr>" << "\n";
index_html << " <th>" << stations[(*stat_it)] << "</th>" << std::endl;
index_html << " <th> - - </th>" << std::endl;
index_html << " <th> - + </th>"<< std::endl;
index_html << " <th> + - </th>" << std::endl;
index_html << " <th> + + </th>"<< std::endl;
index_html << " <th> invalid </th>"<< std::endl;
index_html << " <th> avg sign bit </th>"<< std::endl;
index_html << " <th> avg mag bit </th>"<< std::endl;
index_html << "</tr>" << std::endl;
// Iterate over all channels
for(int i=0;i<channels.size();i++){
// Find the current channel in the statistics set
Output_header_bitstatistics chan;
chan.station_nr=(*stat_it);
chan.frequency_nr=channels[i].frequency_nr;
chan.sideband=channels[i].sideband;
chan.polarisation=channels[i].polarization;
statistics_set::iterator chan_it=statistics.find(chan);
// Get the total number of samples
const int32_t (&levels)[4] = (*chan_it).levels;
int32_t n_invalid = (*chan_it).n_invalid;
int64_t N=0;
for(int j=0;j<4;j++)
N += levels[j];
N+=n_invalid;
// Print the statistics
begin_data_row(index_html, channels[i]);
for(int j=0;j<4;j++){
index_html << " <td> " << levels[j]*100./N <<"%" << "</td>";
}
index_html << " <td> " << n_invalid*100./N <<"%" << "</td>";
double b0=(levels[2]+levels[3])*1./(N-n_invalid);
index_html << " <td> " << b0 << "</td>";
if((levels[0]>0)||(levels[3]>0)){
double b1=(levels[1]+levels[3])*1./(N-n_invalid);
index_html << " <td> " << b1 << "</td>";
}else
index_html << " <td> ----------- </td>";
/* std::string color = get_statistics_color(levels[2]+levels[3],N-n_invalid);
index_html << " <td bgcolor=" << color << "> " << b0 << "</td>";
color = get_statistics_color(levels[1]+levels[3],N-n_invalid);
index_html << " <td bgcolor=" << color << "> " << b1 << "</td>";
end_data_row(index_html); */
index_html << "\n ";
}
index_html << "</table><br>" << std::endl;
}
}
void Fringe_info_container::
begin_data_row(std::ostream &index_html,
const std::vector<double> &frequencies,
const Fringe_info &fringe_info) {
index_html << "<tr>" << std::endl;
// First cell
index_html << "<th>";
index_html.precision(10);
index_html << frequencies[fringe_info.header.frequency_nr]/1000000
<< "MHz";
index_html.precision(4);
if (fringe_info.header.sideband == 0) {
index_html << ", LSB";
} else {
index_html << ", USB";
}
if (fringe_info.header.polarisation1 == 0) {
index_html << ", Rcp";
} else {
index_html << ", Lcp";
}
if (fringe_info.header.polarisation2 == 0) {
index_html << "-Rcp";
} else {
index_html << "-Lcp";
}
index_html << "</th>" << std::endl;
}
void Fringe_info_container::
begin_data_row(std::ostream &index_html, Channel &channel){
index_html << "<tr>" << std::endl;
// First cell
index_html << "<th>";
index_html.precision(10);
index_html << channel.frequency/1000000 << "MHz";
index_html.precision(4);
if (channel.sideband == 0) {
index_html << ", LSB";
} else {
index_html << ", USB";
}
if (channel.polarization == 0) {
index_html << ", Rcp";
} else {
index_html << ", Lcp";
}
index_html << "</th>" << std::endl;
}
void Fringe_info_container::end_data_row(std::ostream &index_html) {
index_html << "</tr>" << std::endl;
}
void
Fringe_info_container::set_plot(const Fringe_info &fringe_info) {
assert(fringe_info.initialised);
assert(plots.find(fringe_info) == plots.end());
plots.insert(fringe_info);
}
const Fringe_info &
Fringe_info_container::
get_first_plot() const {
assert(!plots.empty());
return *plots.begin();
}
void
Fringe_info_container::
generate_filename(char *filename,
char *filename_large,
char *title,
int size,
const Fringe_info &data,
const Fringe_info::SPACE space,
const Fringe_info::VALUE value) {
int sideband = data.header.sideband;
char sideband_ch = (sideband == 0 ? 'l' : 'u');
int channel = data.header.frequency_nr;
int station1 = data.header.station_nr1;
int station2 = data.header.station_nr2;
int pol1 = data.header.polarisation1;
char pol1_ch = (pol1 == 0 ? 'r' : 'l');
int pol2 = data.header.polarisation2;
char pol2_ch = (pol2 == 0 ? 'r' : 'l');
if (plots.find(data) == plots.end()) {
DEBUG_MSG("Not found: " << data.header);
DEBUG_MSG("size " << plots.size());
DEBUG_MSG("first " << plots.begin()->header);
assert(plots.find(data) != plots.end());
}
snprintf(filename, size,
"st%02d_%ccp-st%02d_%ccp-ch%01d-%csb-%d-%d.png",
station1, pol1_ch, station2, pol2_ch, channel, sideband_ch,
(int)space, (int)value);
snprintf(filename_large, size,
"st%02d_%ccp-st%02d_%ccp-ch%01d-%csb-%d-%d_large.png",
station1, pol1_ch, station2, pol2_ch, channel, sideband_ch,
(int)space, (int)value);
snprintf(title, size,
"(st%02d,%ccp)-(st%02d,%ccp) ch%01d %csb",
station1, pol1_ch, station2, pol2_ch, channel, sideband_ch);
}
void
Fringe_info_container::
print_auto(std::ostream &index_html, const Fringe_info &fringe_info, int bbc,
double frequency, double bandwidth) {
assert(fringe_info.initialised);
index_html << "<td>";
char filename[80], filename_large[80], title[80];
generate_filename(filename, filename_large, title, 80, fringe_info,
Fringe_info::FREQUENCY, Fringe_info::ABS);
fringe_info.plot(filename, filename_large, title, Fringe_info::FREQUENCY,
Fringe_info::ABS, frequency, bandwidth);
index_html << "<A href = '" << filename_large << "' "
<< "OnMouseOver=\"show('" << filename << "');\">"
<< bbc << "</a>";
index_html << "</td>";
}
void
Fringe_info_container::
print_cross(std::ostream &index_html, const Fringe_info &fringe_info,
double frequency, double bandwidth) {
if (fringe_info.initialised) {
char filename_abs[80], filename_large_abs[80], title[80];
generate_filename(filename_abs, filename_large_abs,
title, 80, fringe_info,
Fringe_info::FREQUENCY, Fringe_info::ABS);
fringe_info.plot(filename_abs, filename_large_abs, title, Fringe_info::FREQUENCY,
Fringe_info::ABS, frequency, bandwidth);
char filename_phase[80], filename_large_phase[80];
generate_filename(filename_phase, filename_large_phase,
title, 80, fringe_info,
Fringe_info::FREQUENCY, Fringe_info::PHASE);
fringe_info.plot(filename_phase, filename_large_phase, title, Fringe_info::FREQUENCY,
Fringe_info::PHASE, frequency, bandwidth);
char filename[80], filename_large[80];
generate_filename(filename, filename_large,
title, 80, fringe_info,
Fringe_info::LAG, Fringe_info::ABS);
fringe_info.plot(filename, filename_large, title, Fringe_info::LAG,
Fringe_info::ABS, frequency, bandwidth);
double snr = fringe_info.signal_to_noise_ratio();
int color_val =
(int)(255*(snr-MIN_SNR_VALUE) / (MAX_SNR_VALUE-MIN_SNR_VALUE));
if (color_val < 0)
color_val = 0;
if (color_val > 255)
color_val = 255;
char color[7];
if (color_val >= 128) {
snprintf(color, 7, "#00%2X00", color_val);
} else {
snprintf(color, 7, "#%2X0000", 255-color_val);
}
if (fringe_info.header.polarisation1 ==
fringe_info.header.polarisation2) {
index_html << "<td bgcolor='" << color << "'>";
} else {
index_html << "<td>";
}
index_html << "<A href = '" << filename_large << "' "
<< "OnMouseOver=\"show('" << filename << "');\">"
<< snr << "</a>"
<< " <A href = '" << filename_large_abs << "' "
<< "OnMouseOver=\"show('" << filename_abs << "');\">"
<< "A" << "</a>"
<< " <A href = '" << filename_large_phase << "' "
<< "OnMouseOver=\"show('" << filename_phase << "');\">"
<< "P" << "</a>"
<< "<br>"
<< "<font size=-2>offset: "
<< (fringe_info.max_value_offset() -
global_header.number_channels/2)
<< "</font>";
index_html << "</td>";
} else {
index_html << "<td></td>";
}
}
void
Fringe_info_container::
print_diff(std::ostream &index_html,
Fringe_info fringe_info1,
const Fringe_info &fringe_info2,
bool relative_error,
Fringe_info::SPACE space,
double frequency, double bandwidth) {
assert(fringe_info1.initialised);
assert(fringe_info2.initialised);
assert(fringe_info1.header == fringe_info2.header);
assert(fringe_info1.data_freq.size() == fringe_info2.data_freq.size());
float max_diff = 0;
index_html << "<td>";
if (space == Fringe_info::FREQUENCY) {
std::vector< std::complex<float> > &data1 = fringe_info1.data_freq;
const std::vector< std::complex<float> > &data2 = fringe_info2.data_freq;
for (size_t i=0; i<data1.size(); i++) {
if (relative_error) {
if (data1[i] != std::complex<float>(0.))
data1[i] = (data1[i]-data2[i])/data1[i];
} else {
data1[i] -= data2[i];
}
max_diff = std::max(max_diff, std::abs(data1[i]));
}
} else {
std::vector< std::complex<float> > &data1 = fringe_info1.data_lag;
const std::vector< std::complex<float> > &data2 = fringe_info2.data_lag;
for (size_t i=0; i<data1.size(); i++) {
if (relative_error) {
if (data1[i] != std::complex<float>(0.))
data1[i] = (data1[i]-data2[i])/data1[i];
} else {
data1[i] -= data2[i];
}
max_diff = std::max(max_diff, std::abs(data1[i]));
}
}
char filename[80], filename_large[80], title[80];
generate_filename(filename, filename_large, title, 80, fringe_info1,
space, Fringe_info::ABS);
fringe_info1.plot(filename, filename_large, title, space, Fringe_info::ABS,
frequency, bandwidth);
index_html << "<A href = '" << filename_large << "' "
<< "OnMouseOver=\"show('" << filename << "');\">"
<< max_diff << "</a>";
index_html << "</td>";
}
const Fringe_info &
Fringe_info_container::get_plot(const Output_header_baseline &h) const {
Fringe_info info(h,
std::vector< std::complex<float> >(),
std::vector< std::complex<float> >());
iterator it = plots.find(info);
if (it == plots.end())
return empty_fringe_info;
return *it;
}
void
Fringe_info_container::
print_diff_html(const Vex &vex,
const Fringe_info_container &other_info,
bool relative_error) {
{ // First check that info1 and info2 contain the same baselines
iterator it1 = plots.begin();
iterator it2 = other_info.plots.begin();
while ((it1!=plots.end()) && (it2!=other_info.plots.end())) {
if (!((*it1).header == (*it2).header)) {
std::cout << "Error: different plots in the output files" << std::endl;
return;
}
it1++;
it2++;
}
if ((it1!=plots.end()) || (it2!=other_info.plots.end())) {
std::cout << "Error: more plots in one file than the other" << std::endl;
return;
}
}
// Get the mode of the current scan
double integration_time = global_header.integration_time/1000000.;
double sec = (global_header.start_time + integration_time * first_timeslice_header.integration_slice);
Date start_time(global_header.start_year, global_header.start_day, (int) sec);
std::string mode = vex.get_mode(vex.get_scan_name(start_time));
// Array with the station names
std::vector<std::string> stations;
// Array with frequencies for a channel nr
std::vector<double> frequencies;
std::vector<double> bandwidths;
std::vector< std::vector<int> > bbcs;
const Vex::Node root_node = vex.get_root_node();
for (Vex::Node::const_iterator it = root_node["STATION"]->begin();
it != root_node["STATION"]->end(); it++) {