-
Notifications
You must be signed in to change notification settings - Fork 1
/
data.cpp
executable file
·1293 lines (1160 loc) · 39.3 KB
/
data.cpp
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) 2006-2008 Sarod Yatawatta <sarod@users.sf.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
$Id$
*/
#include "data.h"
#include "sagecal.h"
#include <measures/Measures/MDirection.h>
#include <measures/Measures/UVWMachine.h>
#include <casa/Quanta.h>
#include <casa/Quanta/Quantum.h>
/* speed of light */
#ifndef CONST_C
#define CONST_C 299792458.0
#endif
using namespace casa;
int Data::numChannels = 1;
unsigned long int Data::numRows;
char *Data::cmdFile = NULL;
char *Data::inputFile = NULL;
char *Data::outputFile = NULL;
char *Data::shareDir = NULL;
char *Data::TableName = NULL;
char *Data::shortName = NULL;
float Data::min_uvcut = 0.0f;
float Data::max_uvcut = 100e6;
float Data::max_uvtaper = 0.0f;
String Data::DataField = "DATA";
String Data::OutField = "CORRECTED_DATA";
int Data::TileSize = 120;
int Data::Nt = 6;
char *Data::SkyModel = NULL;
char *Data::Clusters = NULL;
int Data::format = 0; /* old LSM */
double Data::nulow = 2.0;
double Data::nuhigh = 30.0;
int Data::max_emiter = 3;
int Data::max_iter = 2;
int Data::max_lbfgs = 10;
int Data::lbfgs_m = 7;
int Data::gpu_threads = 128;
int Data::linsolv = 1;
int Data::randomize = 1;
int Data::whiten = 0;
int Data::DoSim = 0;
int Data::DoDiag = 0;
int Data::doChan = 0; /* if 1, solve for each channel in multi channel data */
int Data::doBeam = 0; /* if >0, enable LOFAR beam model */
int Data::phaseOnly = 0; /* if >0, enable phase only correction */
int Data::solver_mode = 0;
int Data::ccid = -99999;
double Data::rho = 1e-9;
char *Data::solfile = NULL;
char *Data::ignorefile = NULL;
char *Data::MSlist = NULL;
char *Data::MSpattern = NULL;
/* distributed sagecal parameters */
int Data::Nadmm = 1;
int Data::Npoly = 2;
int Data::PolyType = 2;
double Data::admm_rho = 5.0;
char *Data::admm_rho_file = NULL;
/* no upper limit, solve for all timeslots */
int Data::Nmaxtime = 0;
/* skip starting time slots if given */
int Data::Nskip = 0;
int Data::verbose = 0; /* no verbose output */
using namespace Data;
void Data::readMSlist(char *fname, vector <string> *msnames) {
cout << "Reading " << Data::MSlist << endl;
/* multiple MS */
ifstream infile(fname);
/* check if the file exists and readable */
if (!infile.good()) {
cout << "File " << Data::MSlist << " does not exist." << endl;
exit(1);
}
string buffer;
if (infile.is_open()) {
while (infile.good()) {
std::getline(infile, buffer);
if (buffer.length() > 0) {
cout << buffer << endl;
msnames->push_back(buffer);
}
}
}
}
void Data::readAuxData(const char *fname, Data::IOData *data) {
cout << "using Data::TableName = "<< fname << endl;
Table _t = Table(fname);
Table _ant = Table(_t.keywordSet().asTable("ANTENNA"));
ROScalarColumn <String> a1(_ant, "NAME");
data->N = a1.nrow();
data->Nbase = data->N * (data->N - 1) / 2;
cout << "Stations: " << data->N << " Baselines: " << data->Nbase << endl;
ROScalarColumn<double> timeCol(_t, "INTERVAL");
data->deltat = timeCol.get(0);
data->totalt = (timeCol.nrow() + data->Nbase + data->N - 1) / (data->Nbase + data->N);
cout << "Integration Time: " << data->deltat << " s," << " Total timeslots: " << data->totalt << endl;
Table _field = Table(_t.keywordSet().asTable("FIELD"));
ROArrayColumn<double> ref_dir(_field, "REFERENCE_DIR");
Array<double> dir = ref_dir(0);
double *c = dir.data();
data->ra0 = c[0];
data->dec0 = c[1];
cout << "Phase center (" << c[0] << ", " << c[1] << ")" << endl;
//obtain the chanel freq information
Table _freq = Table(_t.keywordSet().asTable("SPECTRAL_WINDOW"));
ROArrayColumn<double> chan_freq(_freq, "CHAN_FREQ");
data->Nchan = chan_freq.shape(0)[0];
data->Nms = 1;
/* allocate memory */
data->u = new double[data->Nbase * data->tilesz];
data->v = new double[data->Nbase * data->tilesz];
data->w = new double[data->Nbase * data->tilesz];
data->x = new double[8 * data->Nbase * data->tilesz];
data->xo = new double[8 * data->Nbase * data->tilesz * data->Nchan];
data->freqs = new double[data->Nchan];
data->flag = new double[data->Nbase * data->tilesz];
data->NchanMS = new int[data->Nms];
data->NchanMS[0] = data->Nchan;
/* copy freq */
data->freq0 = 0.0;
for (int ci = 0; ci < data->Nchan; ci++) {
data->freqs[ci] = chan_freq(0).data()[ci];
data->freq0 += data->freqs[ci];
}
data->freq0 /= (double) data->Nchan;
/* need channel widths to calculate bandwidth */
ROArrayColumn<double> chan_width(_freq, "CHAN_WIDTH");
data->deltaf = (double) data->Nchan * (chan_width(0).data()[0]);
}
void
Data::readAuxData(const char *fname, Data::IOData *data, Data::LBeam *binfo) {
cout << "using Data::TableName = "<< Data::TableName << endl;
Table _t = Table(fname);
Table _ant = Table(_t.keywordSet().asTable("ANTENNA"));
ROScalarColumn <String> a1(_ant, "NAME");
data->N = a1.nrow();
data->Nbase = data->N * (data->N - 1) / 2;
cout << "Stations: " << data->N << " Baselines: " << data->Nbase << endl;
ROScalarColumn<double> timeCol(_t, "INTERVAL");
data->deltat = timeCol.get(0);
data->totalt = (timeCol.nrow() + data->Nbase + data->N - 1) / (data->Nbase + data->N);
cout << "Integration Time: " << data->deltat << " s," << " Total timeslots: " << data->totalt << endl;
Table _field = Table(_t.keywordSet().asTable("FIELD"));
ROArrayColumn<double> ref_dir(_field, "PHASE_DIR"); /* old REFERENCE_DIR */
Array<double> dir = ref_dir(0);
double *c = dir.data();
data->ra0 = c[0];
data->dec0 = c[1];
cout << "Phase center (" << c[0] << ", " << c[1] << ")" << endl;
//obtain the chanel freq information
Table _freq = Table(_t.keywordSet().asTable("SPECTRAL_WINDOW"));
ROArrayColumn<double> chan_freq(_freq, "CHAN_FREQ");
data->Nchan = chan_freq.shape(0)[0];
data->Nms = 1;
/* allocate memory */
data->u = new double[data->Nbase * data->tilesz];
data->v = new double[data->Nbase * data->tilesz];
data->w = new double[data->Nbase * data->tilesz];
data->x = new double[8 * data->Nbase * data->tilesz];
data->xo = new double[8 * data->Nbase * data->tilesz * data->Nchan];
data->freqs = new double[data->Nchan];
data->flag = new double[data->Nbase * data->tilesz];
data->NchanMS = new int[data->Nms];
data->NchanMS[0] = data->Nchan;
/* copy freq */
data->freq0 = 0.0;
for (int ci = 0; ci < data->Nchan; ci++) {
data->freqs[ci] = chan_freq(0).data()[ci];
data->freq0 += data->freqs[ci];
}
data->freq0 /= (double) data->Nchan;
/* need channel widths to calculate bandwidth */
ROArrayColumn<double> chan_width(_freq, "CHAN_WIDTH");
data->deltaf = (double) data->Nchan * (chan_width(0).data()[0]);
/* UTC time */
binfo->time_utc = new double[data->tilesz];
/* no of elements in each station */
binfo->Nelem = new int[data->N];
/* positions of stations */
binfo->sx = new double[data->N];
binfo->sy = new double[data->N];
binfo->sz = new double[data->N];
/* coordinates of elements */
binfo->xx = new double *[data->N];
binfo->yy = new double *[data->N];
binfo->zz = new double *[data->N];
binfo->len_xyz = new int[data->N];
Table antfield = Table(_t.keywordSet().asTable("LOFAR_ANTENNA_FIELD"));
ROArrayColumn<double> position(antfield, "POSITION");
ROArrayColumn<double> offset(antfield, "ELEMENT_OFFSET");
ROArrayColumn<double> coord(antfield, "COORDINATE_AXES");
ROArrayColumn<bool> eflag(antfield, "ELEMENT_FLAG");
ROArrayColumn<double> tileoffset(antfield, "TILE_ELEMENT_OFFSET");
/* check if TILE_ELEMENT_OFFSET has any rows, of no rows present,
we know this is LBA */
bool isHBA = tileoffset.hasContent(0);
/* read positions, also setup memory for element coords */
for (int ci = 0; ci < data->N; ci++) {
Array<double> _pos = position(ci);
double *tx = _pos.data();
binfo->sz[ci] = tx[2];
MPosition stnpos(MVPosition(tx[0], tx[1], tx[2]), MPosition::ITRF);
Array<double> _radpos = stnpos.getAngle("rad").getValue();
tx = _radpos.data();
binfo->sx[ci] = tx[0];
binfo->sy[ci] = tx[1];
//cout<<"position "<<binfo->sx[ci]<<" "<<binfo->sy[ci]<<" "<<binfo->sz[ci]<<endl;
binfo->Nelem[ci] = offset.shape(ci)[1];
}
if (isHBA) {
double cones[16];
for (int ci = 0; ci < 16; ci++) {
cones[ci] = 1.0;
}
double tempT[3 * 16];
/* now read in element offsets, also transform them to local coordinates */
for (int ci = 0; ci < data->N; ci++) {
Array<double> _off = offset(ci);
double *off = _off.data();
Array<double> _coord = coord(ci);
double *coordmat = _coord.data();
Array<bool> _eflag = eflag(ci);
bool *ef = _eflag.data();
Array<double> _toff = tileoffset(ci);
double *toff = _toff.data();
/*
cout<<"A=["<<endl;
for (int cj=0; cj<binfo->Nelem[ci]; cj++) {
cout<<off[3*cj]<<","<<off[3*cj+1]<<","<<off[3*cj+2]<<endl;
}
cout<<"];"<<endl;
cout<<"T0=["<<endl;
for (int cj=0; cj<16; cj++) {
cout<<toff[3*cj]<<","<<toff[3*cj+1]<<","<<toff[3*cj+2]<<endl;
}
cout<<"];"<<endl;
cout<<"BT=["<<endl;
for (int cj=0; cj<3; cj++) {
cout<<coordmat[3*cj]<<","<<coordmat[3*cj+1]<<","<<coordmat[3*cj+2]<<endl;
}
cout<<"];"<<endl;
*/
double *tempC = new double[3 * binfo->Nelem[ci]];
my_dgemm('T', 'N', binfo->Nelem[ci], 3, 3, 1.0, off, 3, coordmat, 3, 0.0, tempC, binfo->Nelem[ci]);
my_dgemm('T', 'N', 16, 3, 3, 1.0, toff, 3, coordmat, 3, 0.0, tempT, 16);
/*
cout<<"C=["<<endl;
for (int cj=0; cj<binfo->Nelem[ci]; cj++) {
cout<<tempC[cj]<<","<<tempC[cj+binfo->Nelem[ci]]<<","<<tempC[cj+2*binfo->Nelem[ci]]<<endl;
}
cout<<"];"<<endl;
cout<<"T1=["<<endl;
for (int cj=0; cj<16; cj++) {
cout<<tempT[cj]<<","<<tempT[cj+16]<<","<<tempT[cj+2*16]<<endl;
}
cout<<"];"<<endl;
*/
/* now inspect the element flag table to see if any of the dipoles are flagged */
int fcount = 0;
for (int cj = 0; cj < binfo->Nelem[ci]; cj++) {
if (ef[2 * cj] == 1 || ef[2 * cj + 1] == 1) {
fcount++;
}
}
//cout<<"%%Flagged "<<fcount<<endl;
binfo->xx[ci] = new double[16 * (binfo->Nelem[ci] - fcount)];
binfo->yy[ci] = new double[16 * (binfo->Nelem[ci] - fcount)];
binfo->zz[ci] = new double[16 * (binfo->Nelem[ci] - fcount)];
binfo->len_xyz[ci] = 16 * (binfo->Nelem[ci] - fcount);
/* copy unflagged coords, 16 times for each dipole */
fcount = 0;
for (int cj = 0; cj < binfo->Nelem[ci]; cj++) {
if (!(ef[2 * cj] == 1 || ef[2 * cj + 1] == 1)) {
//binfo->xx[ci][fcount]=tempC[cj];
//binfo->yy[ci][fcount]=tempC[cj+binfo->Nelem[ci]];
//binfo->zz[ci][fcount]=tempC[cj+2*binfo->Nelem[ci]];
my_dcopy(16, &tempT[0], 1, &(binfo->xx[ci][fcount]), 1);
my_daxpy(16, cones, tempC[cj], &(binfo->xx[ci][fcount]));
my_dcopy(16, &tempT[16], 1, &(binfo->yy[ci][fcount]), 1);
my_daxpy(16, cones, tempC[cj + binfo->Nelem[ci]], &(binfo->yy[ci][fcount]));
my_dcopy(16, &tempT[24], 1, &(binfo->zz[ci][fcount]), 1);
my_daxpy(16, cones, tempC[cj + 2 * binfo->Nelem[ci]], &(binfo->zz[ci][fcount]));
fcount += 16;
}
}
binfo->Nelem[ci] = fcount;
/*
cout<<"%%Copied "<<fcount<<endl;
cout<<"D=["<<endl;
for (int cj=0; cj<binfo->Nelem[ci]; cj++) {
cout<<binfo->xx[ci][cj]<<","<<binfo->yy[ci][cj]<<","<<binfo->zz[ci][cj]<<endl;
}
cout<<"];"<<endl;
*/
delete[] tempC;
}
} else { /* LBA */
/* now read in element offsets, also transform them to local coordinates */
for (int ci = 0; ci < data->N; ci++) {
Array<double> _off = offset(ci);
double *off = _off.data();
Array<double> _coord = coord(ci);
double *coordmat = _coord.data();
Array<bool> _eflag = eflag(ci);
bool *ef = _eflag.data();
/*
cout<<"A=["<<endl;
for (int cj=0; cj<binfo->Nelem[ci]; cj++) {
cout<<off[3*cj]<<","<<off[3*cj+1]<<","<<off[3*cj+2]<<endl;
}
cout<<"];"<<endl;
cout<<"BT=["<<endl;
for (int cj=0; cj<3; cj++) {
cout<<coordmat[3*cj]<<","<<coordmat[3*cj+1]<<","<<coordmat[3*cj+2]<<endl;
}
cout<<"];"<<endl;
*/
double *tempC = new double[3 * binfo->Nelem[ci]];
my_dgemm('T', 'N', binfo->Nelem[ci], 3, 3, 1.0, off, 3, coordmat, 3, 0.0, tempC, binfo->Nelem[ci]);
/*
cout<<"C=["<<endl;
for (int cj=0; cj<binfo->Nelem[ci]; cj++) {
cout<<tempC[cj]<<","<<tempC[cj+binfo->Nelem[ci]]<<","<<tempC[cj+2*binfo->Nelem[ci]]<<endl;
}
cout<<"];"<<endl;
*/
/* now inspect the element flag table to see if any of the dipoles are flagged */
int fcount = 0;
for (int cj = 0; cj < binfo->Nelem[ci]; cj++) {
if (ef[2 * cj] == 1 || ef[2 * cj + 1] == 1) {
fcount++;
}
}
//cout<<"%%Flagged "<<fcount<<endl;
binfo->xx[ci] = new double[(binfo->Nelem[ci] - fcount)];
binfo->yy[ci] = new double[(binfo->Nelem[ci] - fcount)];
binfo->zz[ci] = new double[(binfo->Nelem[ci] - fcount)];
/* copy unflagged coords for each dipole */
fcount = 0;
for (int cj = 0; cj < binfo->Nelem[ci]; cj++) {
if (!(ef[2 * cj] == 1 || ef[2 * cj + 1] == 1)) {
binfo->xx[ci][fcount] = tempC[cj];
binfo->yy[ci][fcount] = tempC[cj + binfo->Nelem[ci]];
binfo->zz[ci][fcount] = tempC[cj + 2 * binfo->Nelem[ci]];
fcount++;
}
}
binfo->Nelem[ci] = fcount;
/*
cout<<"%%Copied "<<fcount<<endl;
cout<<"D=["<<endl;
for (int cj=0; cj<binfo->Nelem[ci]; cj++) {
cout<<binfo->xx[ci][cj]<<","<<binfo->yy[ci][cj]<<","<<binfo->zz[ci][cj]<<endl;
}
cout<<"];"<<endl;
*/
delete[] tempC;
}
}
/* read beam pointing direction (use Tile beam info: LBA also has it) */
ROArrayColumn<double> point_dir(_field, "LOFAR_TILE_BEAM_DIR");
Array<double> pdir = point_dir(0);
double *pc = pdir.data();
binfo->p_ra0 = pc[0];
binfo->p_dec0 = pc[1];
/* convert positions from xyz to longitude,latitude,height */
/* double *longitude=new double[data->N];
double *latitude=new double[data->N];
double *height=new double[data->N];
xyz2llh(binfo->sx,binfo->sy,binfo->sz,longitude,latitude,height,data->N);
delete [] binfo->sx;
delete [] binfo->sy;
delete [] binfo->sz;
binfo->sx=longitude;
binfo->sy=latitude;
binfo->sz=height;
*/
}
void
Data::readAuxDataList(vector <string> msnames, Data::IOData *data) {
/* read first filename */
const char *fname = msnames[0].c_str();
Table _t = Table(fname);
//char buff[2048] = {0};
//sprintf(buff, "%s/ANTENNA", fname);
//Table _ant=Table(buff);
Table _ant = Table(_t.keywordSet().asTable("ANTENNA"));
ROScalarColumn <String> a1(_ant, "NAME");
data->N = a1.nrow();
data->Nbase = data->N * (data->N - 1) / 2;
cout << "Stations: " << data->N << " Baselines: " << data->Nbase << endl;
ROScalarColumn<double> timeCol(_t, "INTERVAL");
data->deltat = timeCol.get(0);
data->totalt = (timeCol.nrow() + data->Nbase + data->N - 1) / (data->Nbase + data->N);
cout << "Integration Time: " << data->deltat << " s," << " Total timeslots: " << data->totalt << endl;
//sprintf(buff, "%s/FIELD", fname);
//Table _field = Table(buff);
Table _field = Table(_t.keywordSet().asTable("FIELD"));
ROArrayColumn<double> ref_dir(_field, "REFERENCE_DIR");
Array<double> dir = ref_dir(0);
double *c = dir.data();
data->ra0 = c[0];
data->dec0 = c[1];
cout << "Phase center (" << c[0] << ", " << c[1] << ")" << endl;
data->Nchan = 0;
data->Nms = msnames.size();
data->NchanMS = new int[data->Nms];
for (int cm = 0; cm < data->Nms; cm++) {
//obtain the chanel freq information
fname = msnames[cm].c_str();
Table _t1 = Table(fname);
//sprintf(buff, "%s/SPECTRAL_WINDOW", fname);
//Table _freq = Table(buff);
Table _freq = Table(_t1.keywordSet().asTable("SPECTRAL_WINDOW"));
ROArrayColumn<double> chan_freq(_freq, "CHAN_FREQ");
data->Nchan += chan_freq.shape(0)[0];
data->NchanMS[cm] = chan_freq.shape(0)[0];
}
cout << "Total channels=" << data->Nchan << endl;
/* allocate memory */
data->u = new double[data->Nbase * data->tilesz];
data->v = new double[data->Nbase * data->tilesz];
data->w = new double[data->Nbase * data->tilesz];
data->x = new double[8 * data->Nbase * data->tilesz];
data->xo = new double[8 * data->Nbase * data->tilesz * data->Nchan];
data->freqs = new double[data->Nchan];
data->flag = new double[data->Nbase * data->tilesz];
/* copy freq */
data->freq0 = 0.0;
data->deltaf = 0.0;
int ck = 0;
for (int cm = 0; cm < data->Nms; cm++) {
fname = msnames[cm].c_str();
Table _t1 = Table(fname);
//sprintf(buff, "%s/SPECTRAL_WINDOW", fname);
//Table _freq = Table(buff);
Table _freq = Table(_t1.keywordSet().asTable("SPECTRAL_WINDOW"));
ROArrayColumn<double> chan_freq(_freq, "CHAN_FREQ");
/* need channel widths to calculate bandwidth */
ROArrayColumn<double> chan_width(_freq, "CHAN_WIDTH");
for (int ci = 0; ci < chan_freq.shape(0)[0]; ci++) {
data->freqs[ck] = chan_freq(0).data()[ci];
data->freq0 += data->freqs[ck++];
data->deltaf += (chan_width(0).data()[ci]);
}
}
data->freq0 /= (double) data->Nchan;
cout << "freq0=" << data->freq0 / 1e6 << endl;
cout << "deltaf=" << data->deltaf / 1e6 << endl;
for (ck = 0; ck < data->Nchan; ck++) {
cout << ck << " " << data->freqs[ck] << endl;
}
}
/* each time this is called read in data from MS, and format them as
u,v,w: u,v,w coordinates (wavelengths) size Nbase*tilesz x 1
u,v,w are ordered with baselines, timeslots
x: data to write size Nbase*8*tileze x 1
ordered by XX(re,im),XY(re,im),YX(re,im), YY(re,im), baseline, timeslots
fratio: flagged data as a ratio to all available data
*/
void
Data::loadData(Table ti, Data::IOData iodata, double *fratio) {
/* sort input table by ant1 and ant2 */
Block <String> iv1(3);
iv1[0] = "TIME";
iv1[1] = "ANTENNA1";
iv1[2] = "ANTENNA2";
Table t = ti.sort(iv1, Sort::Ascending);
ROScalarColumn<int> a1(t, "ANTENNA1"), a2(t, "ANTENNA2");
/* only read only access for input */
ROArrayColumn <Complex> dataCol(t, Data::DataField);
ROArrayColumn<double> uvwCol(t, "UVW");
ROArrayColumn<bool> flagCol(t, "FLAG");
/* check we get correct rows */
int nrow = t.nrow();
if (nrow - iodata.N * iodata.tilesz > iodata.tilesz * iodata.Nbase) {
cout << "Error in rows" << endl;
}
int row0 = 0;
/* tapering */
bool dotaper = false;
double invtaper = 1.0;
if (max_uvtaper > 0.0f) {
dotaper = true;
/* taper in m */
invtaper = iodata.freq0 / ((double) max_uvtaper * CONST_C);
}
/* counters for finding flagged data ratio */
int countgood = 0;
int countbad = 0;
for (int row = 0; row < nrow; row++) {
uInt i = a1(row); //antenna1
uInt j = a2(row); //antenna2
/* only work with cross correlations */
if (i != j) {
Array <Complex> data = dataCol(row);
Matrix<double> uvw = uvwCol(row);
Array<bool> flag = flagCol(row);
Complex cxx(0, 0);
Complex cxy(0, 0);
Complex cyx(0, 0);
Complex cyy(0, 0);
/* calculate sqrt(u^2+v^2) to select uv cuts */
double *c = uvw.data();
double uvd = sqrt(c[0] * c[0] + c[1] * c[1]);
bool flag_uvcut = 0;
if (uvd < min_uvcut || uvd > max_uvcut) {
flag_uvcut = true;
}
double uvtaper = 1.0;
if (dotaper) {
uvtaper = uvd * invtaper;
if (uvtaper > 1.0) {
uvtaper = 1.0;
}
}
int nflag = 0;
for (int k = 0; k < iodata.Nchan; k++) {
Complex *ptr = data[k].data();
bool *flgptr = flag[k].data();
if (!flgptr[0] && !flgptr[1] && !flgptr[2] && !flgptr[3]) {
cxx += ptr[0];
cxy += ptr[1];
cyx += ptr[2];
cyy += ptr[3];
nflag++; /* remeber unflagged datapoints */
}
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8] = ptr[0].real();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 1] = ptr[0].imag();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 2] = ptr[1].real();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 3] = ptr[1].imag();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 4] = ptr[2].real();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 5] = ptr[2].imag();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 6] = ptr[3].real();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 7] = ptr[3].imag();
}
if (nflag > iodata.Nchan / 2) { /* at least half channels should have good data */
double invnflag = 1.0 / (double) nflag;
cxx *= invnflag;
cxy *= invnflag;
cyx *= invnflag;
cyy *= invnflag;
if (dotaper) {
cxx *= uvtaper;
cxy *= uvtaper;
cyx *= uvtaper;
cyy *= uvtaper;
}
iodata.flag[row0] = 0;
countgood++;
} else {
if (!nflag) {
/* all channels flagged, flag this row */
iodata.flag[row0] = 1;
countbad++;
} else {
iodata.flag[row0] = 2;
}
}
iodata.u[row0] = c[0];
iodata.v[row0] = c[1];
iodata.w[row0] = c[2];
if (flag_uvcut) {
iodata.flag[row0] = 2;
}
iodata.x[row0 * 8] = cxx.real();
iodata.x[row0 * 8 + 1] = cxx.imag();
iodata.x[row0 * 8 + 2] = cxy.real();
iodata.x[row0 * 8 + 3] = cxy.imag();
iodata.x[row0 * 8 + 4] = cyx.real();
iodata.x[row0 * 8 + 5] = cyx.imag();
iodata.x[row0 * 8 + 6] = cyy.real();
iodata.x[row0 * 8 + 7] = cyy.imag();
row0++;
}
}
/* now if there is a tail of empty data remaining, flag them */
if (row0 < iodata.tilesz * iodata.Nbase) {
for (int row = row0; row < iodata.tilesz * iodata.Nbase; row++) {
iodata.flag[row] = 1;
}
}
/* flagged data / total usable data, not counting excluded baselines */
if (countgood + countbad > 0) {
*fratio = (double) countbad / (double) (countgood + countbad);
} else {
*fratio = 1.0;
}
}
/* each time this is called read in data from MS, and format them as
u,v,w: u,v,w coordinates (wavelengths) size Nbase*tilesz x 1
u,v,w are ordered with baselines, timeslots
x: data to write size Nbase*8*tileze x 1
ordered by XX(re,im),XY(re,im),YX(re,im), YY(re,im), baseline, timeslots
time_utc: UTC time, 1 per each Nbase
fratio: flagged data as a ratio to all available data
*/
void
Data::loadData(Table ti, Data::IOData iodata, LBeam binfo, double *fratio) {
/* sort input table by ant1 and ant2 */
Block <String> iv1(3);
iv1[0] = "TIME";
iv1[1] = "ANTENNA1";
iv1[2] = "ANTENNA2";
Table t = ti.sort(iv1, Sort::Ascending);
ROScalarColumn<int> a1(t, "ANTENNA1"), a2(t, "ANTENNA2");
/* only read only access for input */
ROArrayColumn <Complex> dataCol(t, Data::DataField);
ROArrayColumn<double> uvwCol(t, "UVW");
ROArrayColumn<bool> flagCol(t, "FLAG");
ROScalarColumn<double> tut(t, "TIME");
/* check we get correct rows */
int nrow = t.nrow();
if (nrow - iodata.N * iodata.tilesz > iodata.tilesz * iodata.Nbase) {
cout << "Error in rows" << endl;
}
int row0 = 0;
int rowt = 0;
/* tapering */
bool dotaper = false;
double invtaper = 1.0;
if (max_uvtaper > 0.0f) {
dotaper = true;
/* taper in m */
invtaper = iodata.freq0 / ((double) max_uvtaper * CONST_C);
}
/* counters for finding flagged data ratio */
int countgood = 0;
int countbad = 0;
for (int row = 0; row < nrow; row++) {
uInt i = a1(row); //antenna1
uInt j = a2(row); //antenna2
if (!i && !j) {/* use baseline 0-0 to extract time */
double tt = tut(row);
//cout.precision(22);
//cout<<"("<<i<<","<<j<<") "<<rowt<<"="<<tt<<endl;
/* convert MJD (s) to JD (days) */
binfo.time_utc[rowt++] = (tt / 86400.0 + 2400000.5); /* no +0.5 added */
}
/* only work with cross correlations */
if (i != j) {
Array <Complex> data = dataCol(row);
Matrix<double> uvw = uvwCol(row);
Array<bool> flag = flagCol(row);
Complex cxx(0, 0);
Complex cxy(0, 0);
Complex cyx(0, 0);
Complex cyy(0, 0);
/* calculate sqrt(u^2+v^2) to select uv cuts */
double *c = uvw.data();
double uvd = sqrt(c[0] * c[0] + c[1] * c[1]);
bool flag_uvcut = 0;
if (uvd < min_uvcut || uvd > max_uvcut) {
flag_uvcut = true;
}
double uvtaper = 1.0;
if (dotaper) {
uvtaper = uvd * invtaper;
if (uvtaper > 1.0) {
uvtaper = 1.0;
}
}
int nflag = 0;
for (int k = 0; k < iodata.Nchan; k++) {
Complex *ptr = data[k].data();
bool *flgptr = flag[k].data();
if (!flgptr[0] && !flgptr[1] && !flgptr[2] && !flgptr[3]) {
cxx += ptr[0];
cxy += ptr[1];
cyx += ptr[2];
cyy += ptr[3];
nflag++; /* remeber unflagged datapoints */
}
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8] = ptr[0].real();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 1] = ptr[0].imag();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 2] = ptr[1].real();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 3] = ptr[1].imag();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 4] = ptr[2].real();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 5] = ptr[2].imag();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 6] = ptr[3].real();
iodata.xo[iodata.Nbase * iodata.tilesz * 8 * k + row0 * 8 + 7] = ptr[3].imag();
}
if (nflag > iodata.Nchan / 2) { /* at least half channels should have good data */
double invnflag = 1.0 / (double) nflag;
cxx *= invnflag;
cxy *= invnflag;
cyx *= invnflag;
cyy *= invnflag;
if (dotaper) {
cxx *= uvtaper;
cxy *= uvtaper;
cyx *= uvtaper;
cyy *= uvtaper;
}
iodata.flag[row0] = 0;
countgood++;
} else {
if (!nflag) {
/* all channels flagged, flag this row */
iodata.flag[row0] = 1;
countbad++;
} else {
iodata.flag[row0] = 2;
}
}
iodata.u[row0] = c[0];
iodata.v[row0] = c[1];
iodata.w[row0] = c[2];
if (flag_uvcut) {
iodata.flag[row0] = 2;
}
iodata.x[row0 * 8] = cxx.real();
iodata.x[row0 * 8 + 1] = cxx.imag();
iodata.x[row0 * 8 + 2] = cxy.real();
iodata.x[row0 * 8 + 3] = cxy.imag();
iodata.x[row0 * 8 + 4] = cyx.real();
iodata.x[row0 * 8 + 5] = cyx.imag();
iodata.x[row0 * 8 + 6] = cyy.real();
iodata.x[row0 * 8 + 7] = cyy.imag();
row0++;
}
}
/* now if there is a tail of empty data remaining, flag them */
if (row0 < iodata.tilesz * iodata.Nbase) {
for (int row = row0; row < iodata.tilesz * iodata.Nbase; row++) {
iodata.flag[row] = 1;
}
}
/* flagged data / total usable data, not counting excluded baselines */
if (countgood + countbad > 0) {
*fratio = (double) countbad / (double) (countgood + countbad);
} else {
*fratio = 1.0;
}
}
/* each time this is called read in data from MS, and format them as
u,v,w: u,v,w coordinates (wavelengths) size Nbase*tilesz x 1
u,v,w are ordered with baselines, timeslots
x: data to write size Nbase*8*tileze x 1
ordered by XX(re,im),XY(re,im),YX(re,im), YY(re,im), baseline, timeslots
fratio: flagged data as a ratio to all available data
*/
void
Data::loadDataList(vector < MSIter * > msitr, Data::IOData
iodata,
double *fratio
) {
Table ti = msitr[0]->table();
/* sort input table by ant1 and ant2 */
Block <String> iv1(3);
iv1[0] = "TIME";
iv1[1] = "ANTENNA1";
iv1[2] = "ANTENNA2";
Table t = ti.sort(iv1, Sort::Ascending);
ROScalarColumn<int> a1(t, "ANTENNA1"), a2(t, "ANTENNA2");
/* only read only access for input */
ROArrayColumn<double> uvwCol(t, "UVW");
/* check we get correct rows */
int nrow = t.nrow();
if(nrow-iodata.
N *iodata
.tilesz>iodata.
tilesz *iodata
.Nbase) {
cout<<"Error in rows"<<
endl;
}
vector<ROArrayColumn < Complex>* >
dataCols(iodata
.Nms);
vector<ROArrayColumn < bool>* >
flagCols(iodata
.Nms);
for (
int cm = 0;
cm<iodata.
Nms;
cm++) {
Table tti = (msitr[cm]->table());
Table *tt = new Table(tti.sort(iv1, Sort::Ascending));
dataCols[cm] = new
ROArrayColumn<Complex>(*tt, Data::DataField
);
flagCols[cm] = new
ROArrayColumn<bool>(*tt,
"FLAG");
}
/* tapering */
bool dotaper = false;
double invtaper = 1.0;
if (max_uvtaper>0.0f) {
dotaper = true;
/* taper in m */
invtaper = iodata.freq0 / ((double) max_uvtaper * CONST_C);
}
/* counters for finding flagged data ratio */
int countgood = 0;
int countbad = 0;
int row0 = 0;
for(
int row = 0;
row<nrow;
row++) {
uInt i = a1(row); //antenna1
uInt j = a2(row); //antenna2
/* only work with cross correlations */
if (i!=j) {
Matrix<double> uvw = uvwCol(row);
Complex cxx(0, 0);
Complex cxy(0, 0);
Complex cyx(0, 0);
Complex cyy(0, 0);
/* calculate sqrt(u^2+v^2) to select uv cuts */
double *c = uvw.data();
double uvd = sqrt(c[0] * c[0] + c[1] * c[1]);
bool flag_uvcut = 0;
if (
uvd<min_uvcut || uvd> max_uvcut
) {
flag_uvcut = true;
}
int nflag = 0;
double uvtaper = 1.0;
if (dotaper) {
uvtaper = uvd * invtaper;
if (uvtaper>1.0) {
uvtaper = 1.0;
}
}
int chanoff = 0;
for (
int cm = 0;
cm<iodata.
Nms;
cm++) {
Array <Complex> data = (*(dataCols[cm]))(row);
Array<bool> flag = (*(flagCols[cm]))(row);
for(
int k = 0;
k<iodata.NchanMS[cm]; k++) {
Complex *ptr = data[k].data();
bool *flgptr = flag[k].data();
if (!flgptr[0] && !flgptr[1] && !flgptr[2] && !flgptr[3]){
cxx+=ptr[0];
cxy+=ptr[1];
cyx+=ptr[2];
cyy+=ptr[3];
nflag++; /* remeber unflagged datapoints */
}
iodata.xo[iodata.
Nbase *iodata
.tilesz*8*chanoff+row0*8]=ptr[0].
real();
iodata.xo[iodata.
Nbase *iodata
.tilesz*8*chanoff+row0*8+1]=ptr[0].
imag();
iodata.xo[iodata.
Nbase *iodata
.tilesz*8*chanoff+row0*8+2]=ptr[1].
real();
iodata.xo[iodata.
Nbase *iodata
.tilesz*8*chanoff+row0*8+3]=ptr[1].
imag();
iodata.xo[iodata.
Nbase *iodata
.tilesz*8*chanoff+row0*8+4]=ptr[2].
real();
iodata.xo[iodata.
Nbase *iodata
.tilesz*8*chanoff+row0*8+5]=ptr[2].
imag();
iodata.xo[iodata.
Nbase *iodata
.tilesz*8*chanoff+row0*8+6]=ptr[3].
real();
iodata.xo[iodata.
Nbase *iodata
.tilesz*8*chanoff+row0*8+7]=ptr[3].
imag();
chanoff++;
}
}
if (nflag>iodata.Nchan/2) {/* at least half channels should have good data */
double invnflag = 1.0 / (double) nflag;
cxx*=
invnflag;