forked from marti-vidal-i/PolConvert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_PolGainSolve.cpp
executable file
·3150 lines (2372 loc) · 90.2 KB
/
_PolGainSolve.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
/* POLGAINSOLVE - Solving cross-polarizer gains for PolConvert
Copyright (C) 2017-2022 Ivan Marti-Vidal
Nordic Node of EU ALMA Regional Center (Onsala, Sweden)
Max-Planck-Institut fuer Radioastronomie (Bonn, Germany)
University of Valencia (Spain)
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 3 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, see <http://www.gnu.org/licenses/>
*/
#include <Python.h>
// compiler warning that we use a deprecated NumPy API
// #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
//#define NO_IMPORT_ARRAY
#if PY_MAJOR_VERSION >= 3
#define NPY_NO_DEPRECATED_API 0x0
#endif
#include <numpy/npy_common.h>
#include <numpy/arrayobject.h>
#include <sys/types.h>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <complex>
#include <dirent.h>
#include <fftw3.h>
//#include <gsl/gsl_errno.h>
//#include <gsl/gsl_linalg.h>
// cribbed from SWIG machinery
#if PY_MAJOR_VERSION >= 3
#define PyClass_Check(obj) PyObject_IsInstance(obj, (PyObject *)&PyType_Type)
#define PyInt_Check(x) PyLong_Check(x)
#define PyInt_AsLong(x) PyLong_AsLong(x)
#define PyInt_FromLong(x) PyLong_FromLong(x)
#define PyInt_FromSize_t(x) PyLong_FromSize_t(x)
#define PyString_Check(name) PyBytes_Check(name)
#define PyString_FromString(x) PyUnicode_FromString(x)
#define PyString_Format(fmt, args) PyUnicode_Format(fmt, args)
//#define PyString_AsString(str) PyBytes_AsString(str)
#define PyString_Size(str) PyBytes_Size(str)
#define PyString_InternFromString(key) PyUnicode_InternFromString(key)
#define Py_TPFLAGS_HAVE_CLASS Py_TPFLAGS_BASETYPE
#define PyString_AS_STRING(x) PyUnicode_AS_STRING(x)
#define _PyLong_FromSsize_t(x) PyLong_FromSsize_t(x)
// For PyArray_FromDimsAndData -> PyArray_SimpleNewFromData
//#define INTEGER long
//#define INTEGERCAST (const npy_intp*)
//#else
// For PyArray_FromDimsAndData -> PyArray_SimpleNewFromData
//#define INTEGER int
//#define INTEGERCAST (long int *)
#endif
// and after some hacking
#if PY_MAJOR_VERSION >= 3
#define PyString_AsString(obj) PyUnicode_AsUTF8(obj)
#endif
typedef std::complex<double> cplx64f;
typedef std::complex<float> cplx32f;
/* Docstrings */
static char module_docstring[] =
"This module provides the cross-polarization gain-solver engine.";
static char PolGainSolve_docstring[] =
"Solves for cross-polarization gains from mixed-polarization visibilities";
static char ReadData_docstring[] =
"Reads data (one IF) for PolGainSolve";
static char FreeData_docstring[] =
"Releases the data pointers of PolGainSolve";
static char GetChi2_docstring[] =
"Computes the Chi2 for a given set of cross-pol gains";
static char GetIFs_docstring[] =
"Returns the array of frequencies for a given IF";
static char DoGFF_docstring[] =
"Performs a simplified GFF (delays and rates). The reference antenna is set by not adding it to the list of fittable antennas";
static char SetFringeRates_docstring[] =
"Forces the antenna fringe-rates to the list give, before the GCPFF is performed";
static char GetNScan_docstring[] =
"Returns the number of scans for the given IF.";
static char SetFit_docstring[] =
"Allocates memory for the GCPFF.";
static char GetNchan_docstring[] =
"Returns the number of channels for the given IF.";
/* Available functions */
static PyObject *PolGainSolve(PyObject *self, PyObject *args);
static PyObject *ReadData(PyObject *self, PyObject *args);
static PyObject *GetChi2(PyObject *self, PyObject *args);
static PyObject *GetIFs(PyObject *self, PyObject *args);
static PyObject *GetNchan(PyObject *self, PyObject *args);
static PyObject *DoGFF(PyObject *self, PyObject *args);
static PyObject *SetFringeRates(PyObject *self, PyObject *args);
static PyObject *GetNScan(PyObject *self, PyObject *args);
static PyObject *FreeData(PyObject *self, PyObject *args);
static PyObject *SetFit(PyObject *self, PyObject *args);
bool solveSystem(int Neq, double *Hessian, double *Residuals, double *Solution, double *Errors);
/* Module specification */
static PyMethodDef module_methods[] = {
{"PolGainSolve", PolGainSolve, METH_VARARGS, PolGainSolve_docstring},
{"ReadData", ReadData, METH_VARARGS, ReadData_docstring},
{"GetChi2", GetChi2, METH_VARARGS, GetChi2_docstring},
{"GetIFs", GetIFs, METH_VARARGS, GetIFs_docstring},
{"DoGFF", DoGFF, METH_VARARGS, DoGFF_docstring},
{"SetFringeRates", SetFringeRates, METH_VARARGS, SetFringeRates_docstring},
{"GetNScan",GetNScan, METH_VARARGS, GetNScan_docstring},
{"GetNchan",GetNchan, METH_VARARGS, GetNchan_docstring},
{"FreeData", FreeData, METH_VARARGS, FreeData_docstring},
{"SetFit", SetFit, METH_VARARGS, SetFit_docstring},
{NULL, NULL, 0, NULL} /* terminated by list of NULLs, apparently */
};
// normally abort() is called on problems, which breaks CASA.
// here we report and save the error condition which can be
// noticed for a cleaner exit.
//int gsl_death_by = GSL_SUCCESS;
//static void gsl_death(const char * reason, const char * file,
// int line, int gsl_errno) {
// // stderr does not end up synchronized with stdout
// printf("GSL Death by '%s' in file %s at line %d: GSL Error %d\n",
// reason, file, line, gsl_errno);
// fflush(stdout); std::cout << std::flush;
// gsl_death_by = gsl_errno;
//}
static long chisqcount = 0;
//static long twincounter = 0;
/* Initialize the module */
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef pc_module_def = {
PyModuleDef_HEAD_INIT,
"_PolGainSolve", /* m_name */
module_docstring, /* m_doc */
-1, /* m_size */
module_methods, /* m_methods */
NULL,NULL,NULL,NULL /* m_reload, m_traverse, m_clear, m_free */
};
PyMODINIT_FUNC PyInit__PolGainSolve(void)
{
PyObject *m = PyModule_Create(&pc_module_def);
// import_array();
// (void)gsl_set_error_handler(gsl_death);
return(m);
}
#else
PyMODINIT_FUNC init_PolGainSolve(void)
{
PyObject *m = Py_InitModule3("_PolGainSolve", module_methods, module_docstring);import_array();
// (void)gsl_set_error_handler(gsl_death);
if (m == NULL)
return;
}
#endif
///////////////////////
char message[512];
bool doParang;
static double TWOPI = 6.283185307179586;
int MaxChan = 1;
int MAXIF = 8; // Will reallocate if needed.
int NCalAnt, Nlin, Ncirc, *Nchan, SolMode, SolAlgor;
int *IFNum;
int *Lant, *Cant, *NVis, *NLVis, *NCVis, *CalAnts, *NScan;
int *Twins[2], Ntwin;
int solveAmp, useCov, solveQU;
int *LinBasNum, NLinBas;
int NIF, NIFComp;
int NBas, NantFit, Npar=-1;
int npix = 0;
double **Frequencies, ***Rates[5], ***Delays[5];
double *Tm = nullptr;
double *BasWgt;
int *doIF, *antFit;
double *feedAngle;
double **DStokes; // = new double*[1];
double UVTAPER = 1.e9;
double Chi2Old = 0.0;
double TAvg = 1.0;
double RelWeight = 1.0;
double T0, T1, DT;
int **Ant1, **Ant2, **BasNum, **Scan;
double **Times, **ScanDur, **Weights, *CovMat, *IndVec, *SolVec;
double **UVGauss;
cplx64f **PA1, **PA2, **auxC00, **auxC01, **auxC10, **auxC11;
cplx64f **auxC00Flp, **auxC11Flp; // To better check Parangle Flip.
cplx64f ***RR, ***RL, ***LR, ***LL, **CrossSpec00, **CrossSpec11;
double *UVWeights;
double SNR_CUTOFF;
double Lambda;
bool doCov;
double Stokes[4];
// gsl_matrix_view m;
// gsl_vector_view x, v;
// gsl_permutation *perm;
bool AddCrossHand = true;
bool AddParHand = true;
bool StokesSolve;
cplx64f *G1,*G2, *G1nu, *G2nu;
double *DirDer,*MBD1,*MBD2;
int *DerIdx,*AvVis;
FILE *logFile = nullptr;
bool solveSystem(int Neq, double *HessianOrig, double *ResidualsOrig, double *Solution, double *Errors){
bool isSingular;
int i,j,k;
double *buffer = new double[Neq+1];
double f;
double *Hessian = new double[Neq*Neq];
double *Residuals = new double[Neq];
memcpy((void *)Hessian, (void *)HessianOrig, sizeof(double)*Neq*Neq);
memcpy((void *)Residuals, (void *)ResidualsOrig, sizeof(double)*Neq);
int Nmissing = 0;
int *missing = new int[Neq];
double *L = new double[Neq*Neq];
double *U = new double[Neq*Neq];
for(i=0;i<Neq;i++){Solution[i]=0.0;};
for(i=0;i<Neq*Neq;i++){L[i]=0.0;U[i]=0.0;};
for(i=0;i<Neq;i++){L[i*Neq+i]=1.0;};
// Getting null rows out of the computation:
for(i=0;i<Neq;i++){
isSingular=true;
for(j=0;j<Neq;j++){
if(HessianOrig[i*Neq+j]!=0.0){isSingular=false;break;};
};
if(isSingular){
missing[Nmissing]=i;Nmissing+=1;
// printf("Antenna %i does not seem to have valid data.\n",i);
};
};
/* performing Gaussian elimination */
for(i=0;i<Neq-1;i++){
// PIVOTING:
isSingular = false;
for(k=0;k<Nmissing;k++){
if(i==missing[k]){isSingular=true;break;};
};
if(!isSingular){
for(j=i+1;j<Neq;j++){
isSingular = false;
for(k=0;k<Nmissing;k++){
if(j==missing[k]){isSingular=true;break;};
};
if(!isSingular){
if(abs(Hessian[i*Neq + i]) < abs(Hessian[j*Neq + i])){
for(k=0;k<Neq;k++){
buffer[k] = Hessian[i*Neq + k];
Hessian[i*Neq + k] = Hessian[j*Neq + k];
Hessian[j*Neq + k] = buffer[k];
};
buffer[Neq] = Residuals[i];
Residuals[i] = Residuals[j];
Residuals[j] = buffer[Neq];
};
};
};
if(Hessian[i*Neq + i]==0.0){ //isSingular=true; return isSingular;
missing[Nmissing] = i;
Nmissing += 1;
// printf("Antenna %i does not seem to have valid data EITHER.\n",i);
} else {
for(j=i+1;j<Neq;j++){
isSingular = false;
for(k=0;k<Nmissing;k++){
if(j==missing[k]){isSingular=true;break;};
};
if(!isSingular){
f=Hessian[j*Neq + i]/Hessian[i*Neq + i];
L[j*Neq+i] = f;
for(k=0;k<Neq;k++){
Hessian[j*Neq + k]=Hessian[j*Neq + k]-f*Hessian[i*Neq + k];
};
Residuals[j] = Residuals[j]-f*Residuals[i];
} else {L[j*Neq+i]=0.0;};
};
};
};
};
// if(Hessian[Neq*Neq-1]==0.0){
// missing[Nmissing]=Neq-1;Nmissing+=1;
// printf("Antenna %i does not seem to have valid data EITHER.\n",i);
// };
for(i=0;i<Neq*Neq;i++){U[i]=Hessian[i];};
/* Backward substitution for discovering values of unknowns */
for(i=Neq-1;i>=0;i--){
isSingular=false;
for(j=0; j<Nmissing; j++){
if(missing[j]==i){isSingular=true;Solution[i]=0.0;break;};
};
if(!isSingular){
Solution[i]=Residuals[i];
for(j=i+1;j<Neq;j++){
if(i!=j){
Solution[i]=Solution[i]-Hessian[i*Neq + j]*Solution[j];
};
};
Solution[i]=Solution[i]/Hessian[i*Neq + i];
};
};
int Nfree;
Nfree = Neq - Nmissing;
double *invL = new double[Nfree*Nfree];
double *invU = new double[Nfree*Nfree];
double *Ufree = new double[Nfree*Nfree];
int ifree, jfree;
ifree=0; jfree=0;
// printf("Nfree: %i %i %i\n",Neq,Nmissing,Nfree);
for(i=0;i<Neq;i++){
isSingular=false;
for(k=0;k<Nmissing;k++){
if(i==missing[k]){isSingular=true;break;};
};
if(!isSingular){
jfree=0;
for(j=0;j<Neq;j++){
isSingular = false;
for(k=0;k<Nmissing;k++){
if(j==missing[k]){isSingular=true;break;};
};
if(!isSingular){
invL[ifree*Nfree+jfree]=L[i*Neq+j]; Ufree[ifree*Nfree+jfree]=U[i*Neq+j];
jfree+=1;
};
};
ifree += 1;
};
};
// Now, invert the smaller (nonSingular) matrices L and U:
double *ident = new double[Nfree];
for(k=0;k<Nfree;k++){
for(j=0;j<Nfree;j++){ident[j]=0.0;};
ident[k]=1.0;
for(i=Nfree-1;i>=0;i--){
invU[i*Nfree + k]=ident[i];
for(j=i+1;j<Nfree;j++){
if(i!=j){
invU[i*Nfree+k]=invU[i*Nfree+k]-Ufree[i*Nfree + j]*invU[j*Nfree+k];
};
};
invU[i*Nfree+k]=invU[i*Nfree+k]/Ufree[i*Nfree + i];
};
};
// The parameter errors are just the (sqrt of) the diagonal cov. matrix:
ifree = 0;
for(i=0;i<Neq;i++){
isSingular=false;
Errors[i]=0.0;
for(k=0;k<Nmissing;k++){
if(missing[k]==i){isSingular=true;Errors[i]=-1.0; break;};
};
if(!isSingular){
for(j=0;j<Nfree;j++){
Errors[i] += invU[ifree*Nfree+j]*invL[j*Nfree+ifree];
};
ifree += 1;
Errors[i] = std::sqrt(Errors[i]);
};
};
delete Hessian;
delete Residuals;
delete invU;
delete Ufree;
delete invL;
delete ident;
isSingular = Nmissing>0;
return isSingular;
};
static PyObject *GetNchan(PyObject *self, PyObject *args){
int cIF, k, j;
PyObject *ret;
// append after first call
if (!logFile) logFile = fopen("PolConvert.GainSolve.log","a");
fprintf(logFile,"into GetNchan...\n"); fflush(logFile);
if (!PyArg_ParseTuple(args, "i",&cIF)){
sprintf(message,"Failed GetNchan! Check inputs!\n");
fprintf(logFile,"%s",message); std::cout<<message; fflush(logFile);
ret = Py_BuildValue("i",-1);
return ret;
};
k=-1;
for (j=0; j<NIF; j++){
if(IFNum[j] == cIF){k=j;break;};
};
if(k<0){
sprintf(message,"GetNchan: IF %d not found\n", cIF);
fprintf(logFile,"%s",message); std::cout<<message; fflush(logFile);
PyObject *ret = Py_BuildValue("i",-2);
return ret;
};
fprintf(logFile,"... and got %d\n", Nchan[k]); fflush(logFile);
ret = Py_BuildValue("i",Nchan[k]);
return ret;
};
static PyObject *GetNScan(PyObject *self, PyObject *args){
int cIF, k, j;
PyObject *ret;
// append after first call
if (!logFile) logFile = fopen("PolConvert.GainSolve.log","a");
fprintf(logFile,"into GetNScan...\n"); fflush(logFile);
if (!PyArg_ParseTuple(args, "i",&cIF)){
sprintf(message,"Failed GetNScan! Check inputs!\n");
fprintf(logFile,"%s",message); std::cout<<message; fflush(logFile);
ret = Py_BuildValue("i",-1);
return ret;
};
k=-1;
for (j=0; j<NIF; j++){
if(IFNum[j] == cIF){k=j;break;};
};
if(k<0){
sprintf(message,"GetNScan: IF %d not found\n", cIF);
fprintf(logFile,"%s",message); std::cout<<message; fflush(logFile);
PyObject *ret = Py_BuildValue("i",-2);
return ret;
};
fprintf(logFile,"... and got %d\n", NScan[k]); fflush(logFile);
ret = Py_BuildValue("i",NScan[k]);
return ret;
};
static PyObject *PolGainSolve(PyObject *self, PyObject *args){
PyObject *calant, *linant, *solints, *flagBas, *logNameObj;
if (!PyArg_ParseTuple(args, "ddOOOOO",&RelWeight, &UVTAPER, &solints, &calant,
&linant,&flagBas, &logNameObj)){
sprintf(message,"Failed initialization of PolGainSolve! Check inputs!\n");
std::cout<<message;
PyObject *ret = Py_BuildValue("i",-1);
return ret;
};
UVTAPER = 2.*UVTAPER*UVTAPER;
// truncate for first call
std::string logName = PyString_AsString(logNameObj);
if (!logFile) logFile = fopen(logName.c_str(),"a");
// Assign dummy sizes to all variables:
NIF = 0;
Npar=0;
DStokes = new double*[1];
DStokes[0] = new double[4];
CovMat = new double[1];
IndVec = new double[1];
SolVec = new double[1];
G1 = new cplx64f[1];
G2 = new cplx64f[1];
G1nu = new cplx64f[1];
G2nu = new cplx64f[1];
DirDer = new double[1];
MBD1 = new double[1];
MBD2 = new double[1];
DerIdx = new int[1];
AvVis = new int[1];
Tm = new double[1];
doIF = new int[1];
antFit = new int[1];
TAvg = (double) PyInt_AsLong(PyList_GetItem(solints,1));
SolAlgor = (int) PyInt_AsLong(PyList_GetItem(solints,0));
Twins[0] = (int *)PyArray_DATA(PyList_GetItem(flagBas,0));
Twins[1] = (int *)PyArray_DATA(PyList_GetItem(flagBas,1));
Ntwin = PyArray_DIM(PyList_GetItem(flagBas,0),0);
sprintf(message,"There are %i baselines to flag\n",Ntwin);
fprintf(logFile,"%s",message); std::cout<<message; fflush(logFile);
sprintf(message,"Will pre-average the data in chunks of %.1f seconds\n",TAvg);
fprintf(logFile,"%s",message); std::cout<<message; fflush(logFile);
AddParHand = RelWeight>0.0;
AddCrossHand = true;
Lant = (int *)PyArray_DATA(linant);
Nlin = PyArray_DIM(linant,0);
CalAnts = (int *)PyArray_DATA(calant);
NCalAnt = PyArray_DIM(calant,0);
int i,j,k,l;
k=0;
int MaxAnt = 0;
for(i=0;i<NCalAnt;i++){
if(CalAnts[i]>MaxAnt){
MaxAnt=CalAnts[i];
};
};
BasNum = new int*[MaxAnt];
LinBasNum = new int[MaxAnt*(MaxAnt-1)/2];
NLinBas = 0;
bool isCal1, isCal2;
for(i=0;i<MaxAnt;i++){
BasNum[i] = new int[MaxAnt];
for(j=0;j<MaxAnt;j++){
BasNum[i][j] = -1;
};
for(j=i+1;j<MaxAnt;j++){
isCal1=false; isCal2=false;
for(l=0;l<NCalAnt;l++){
if(i==CalAnts[l]-1){isCal1=true;};
if(j==CalAnts[l]-1){isCal2=true;};
};
if (isCal1 && isCal2){
BasNum[i][j] = k;
// printf("Baseline %i-%i will have assigned number %i\n",i+1,j+1,k);
for(l=0;l<Nlin; l++){if(i==Lant[l]-1 || j==Lant[l]-1){LinBasNum[NLinBas]=k; NLinBas += 1; break; };};
k += 1;
};
};
};
printf("There are %i baselines.\n",k);
int BNum;
BasWgt = new double[k];
for(i=0;i<MaxAnt;i++){
for(j=i+1;j<MaxAnt;j++){
BNum = BasNum[i][j];
if (BNum>=0){ //printf("Weighting baselines %i\n",BNum);
BasWgt[BNum] = 1.0;} else {BasWgt[BNum]=0.0;};
for(l=0;l<Ntwin;l++){
if((Twins[0][l]==i+1 && Twins[1][l]==j+1)||(Twins[0][l]==j+1 && Twins[1][l]==i+1)){
printf("Flagging baseline %i\n",BNum); BasNum[i][j] = -1; BasWgt[BNum] = 0.0; break;
};
};
};
};
auxC00 = new cplx64f*[k];
auxC01 = new cplx64f*[k];
auxC10 = new cplx64f*[k];
auxC11 = new cplx64f*[k];
auxC00Flp = new cplx64f*[k];
auxC11Flp = new cplx64f*[k];
UVWeights = new double[k];
for(i=0;i<k;i++){
auxC00[i] = new cplx64f[3*NCalAnt+1];
auxC11[i] = new cplx64f[3*NCalAnt+1];
auxC01[i] = new cplx64f[3*NCalAnt+1];
auxC10[i] = new cplx64f[3*NCalAnt+1];
auxC00Flp[i] = new cplx64f[3*NCalAnt+1];
auxC11Flp[i] = new cplx64f[3*NCalAnt+1];
};
NBas = k;
CrossSpec00 = (cplx64f **) malloc(NBas*sizeof(cplx64f*));
CrossSpec11 = (cplx64f **) malloc(NBas*sizeof(cplx64f*));
for(i=0;i<NBas;i++){
CrossSpec00[i] = (cplx64f *) malloc(MaxChan*sizeof(cplx64f));
CrossSpec11[i] = (cplx64f *) malloc(MaxChan*sizeof(cplx64f));
};
NIF = 0;
// Set Memory:
NVis = (int *) malloc(MAXIF*sizeof(int));
IFNum = (int *) malloc(MAXIF*sizeof(int));
NCVis = (int *) malloc(MAXIF*sizeof(int));
NLVis = (int *) malloc(MAXIF*sizeof(int));
Nchan = (int *) malloc(MAXIF*sizeof(int));
Frequencies = (double **) malloc(MAXIF*sizeof(double*));
Ant1 = (int**) malloc(MAXIF*sizeof(int*));
Ant2 = (int**) malloc(MAXIF*sizeof(int*));
Scan = (int**) malloc(MAXIF*sizeof(int*));
NScan = (int*) malloc(MAXIF*sizeof(int));
Times = (double**) malloc(MAXIF*sizeof(double*));
Weights = (double**) malloc(MAXIF*sizeof(double*));
ScanDur = (double**) malloc(MAXIF*sizeof(double*));
PA1 = (cplx64f**) malloc(MAXIF*sizeof(cplx64f*));
PA2 = (cplx64f**) malloc(MAXIF*sizeof(cplx64f*));
UVGauss = (double**) malloc(MAXIF*sizeof(double*));
RR = (cplx64f***) malloc(MAXIF*sizeof(cplx64f**));
LR = (cplx64f***) malloc(MAXIF*sizeof(cplx64f**));
RL = (cplx64f***) malloc(MAXIF*sizeof(cplx64f**));
LL = (cplx64f***) malloc(MAXIF*sizeof(cplx64f**));
// Rates = (double ***) malloc(MAXIF*sizeof(double**));
for(i=0;i<5;i++){
Rates[i] = (double ***) malloc(MAXIF*sizeof(double**));
Delays[i] = (double ***) malloc(MAXIF*sizeof(double**));
};
PyObject *ret = Py_BuildValue("i",0);
return ret;
};
static PyObject *FreeData(PyObject *self, PyObject *args) {
int i,j;
if (!logFile) logFile = fopen("PolConvert.GainSolve.log","a");
sprintf(message,"Freeing Data NIF = %d\n", NIF);
fprintf(logFile,"%s",message); std::cout<<message; fflush(logFile);
for(i=0;i<NIF;i++){
for(j=0;j<NVis[i]+1;j++){
free(RR[i][j]);free(RL[i][j]);
free(LR[i][j]);free(LL[i][j]);
};
free(Ant1[i]);free(Ant2[i]);free(Scan[i]);free(Times[i]);
free(PA1[i]);free(PA2[i]);free(RR[i]);free(RL[i]);free(UVGauss[i]);
free(LR[i]);free(LL[i]);free(ScanDur[i]);free(Weights[i]);
delete Frequencies[i];
};
delete(UVWeights);
if(NIF>0){
free(NScan);free(Nchan);free(NVis);
free(NCVis);free(NLVis);free(IFNum);
free(Frequencies); free(Scan);
NIF = -1;
PyObject *ret = Py_BuildValue("i",0);
return ret;
};
// Problem with NIF. Returns error:
PyObject *ret = Py_BuildValue("i",1);
return ret;
};
// Read the data in polconvert's binary format.
// In addition, arrange the data in scans.
// MaxDT is the maximum allowed time separation between
// neighboring entries of the same scan (in seconds).
static PyObject *ReadData(PyObject *self, PyObject *args) {
int IFN;
const char *file1, *file2;
std::ifstream CPfile, MPfile;
double MaxDT;
if (!logFile) logFile = fopen("PolConvert.GainSolve.log","a");
fprintf(logFile,"ReadData entered...\n"); fflush(logFile);
if (!PyArg_ParseTuple(args, "issd", &IFN,&file1, &file2,&MaxDT)){
sprintf(message,"Failed ReadData! Check inputs! (return -1)\n");
fprintf(logFile,"%s",message); std::cout<<message; fflush(logFile);
fclose(logFile);
PyObject *ret = Py_BuildValue("i",-1);
return ret;
};
fprintf(logFile,"ReadData parsed...\n"); fflush(logFile);
int i, j, k;
double AuxT, AuxPA1, AuxPA2, AuxUV;
bool is1, is2;
CPfile.open(file1, std::ios::in | std::ios::binary);
MPfile.open(file2, std::ios::in | std::ios::binary);
NIF += 1;
//////////
// Set memory for new IF:
if (NIF > MAXIF){
fprintf(logFile,"(realloc) %d > %d\n", NIF, MAXIF); fflush(logFile);
MAXIF *= 2;
Nchan = (int*) realloc(Nchan,MAXIF*sizeof(int));
Frequencies = (double**) realloc(Frequencies,MAXIF*sizeof(double*));
NVis = (int*) realloc(NVis,MAXIF*sizeof(int));
NCVis = (int*) realloc(NCVis,MAXIF*sizeof(int));
NLVis = (int*) realloc(NLVis,MAXIF*sizeof(int));
IFNum = (int*) realloc(IFNum,MAXIF*sizeof(int));
if(!Nchan || !Frequencies || !NVis || !NCVis || !NLVis || !IFNum){
Nchan=nullptr; Frequencies=nullptr; NVis=nullptr; NCVis=nullptr; NLVis=nullptr; IFNum=nullptr;
fprintf(logFile,"(return -2)"); fflush(logFile);
PyObject *ret = Py_BuildValue("i",-2);
return ret;
};
// Set memory for the visibilities and metadata:
Ant1 = (int**) realloc(Ant1,MAXIF*sizeof(int*));
Ant2 = (int**) realloc(Ant2,MAXIF*sizeof(int*));
Scan = (int**) realloc(Scan,MAXIF*sizeof(int*));
NScan = (int*) realloc(NScan,MAXIF*sizeof(int));
Times = (double**) realloc(Times,MAXIF*sizeof(double*));
Weights = (double**) realloc(Weights,MAXIF*sizeof(double*));
ScanDur = (double**) realloc(ScanDur,MAXIF*sizeof(double*));
PA1 = (cplx64f**) realloc(PA1,MAXIF*sizeof(cplx64f*));
PA2 = (cplx64f**) realloc(PA2,MAXIF*sizeof(cplx64f*));
UVGauss = (double**) realloc(UVGauss,MAXIF*sizeof(double*));
RR = (cplx64f***) realloc(RR,MAXIF*sizeof(cplx64f**));
LR = (cplx64f***) realloc(LR,MAXIF*sizeof(cplx64f**));
RL = (cplx64f***) realloc(RL,MAXIF*sizeof(cplx64f**));
LL = (cplx64f***) realloc(LL,MAXIF*sizeof(cplx64f**));
// Rates = (double ***) realloc(Rates,MAXIF*sizeof(double**));
for(i=0;i<5;i++){
Delays[i] = (double ***) realloc(Delays[i],MAXIF*sizeof(double**));
Rates[i] = (double ***) realloc(Rates[i],MAXIF*sizeof(double**));
};
if(!Ant1 || !Ant2 || !Times || !Weights || !PA1 || !PA2 || !RR || !LR || !RL || !LL){
Ant1=nullptr; Ant2=nullptr; Times=nullptr; PA1=nullptr; PA2=nullptr; UVGauss=nullptr;
RR=nullptr; LR=nullptr; RL=nullptr; LL=nullptr; ScanDur=nullptr; Weights=nullptr;
fprintf(logFile,"(return -3)"); fflush(logFile);
PyObject *ret = Py_BuildValue("i",-3);
return ret;
};
if(!Rates[0] || !Delays[0] || !Delays[1] || !Delays[2] || !Delays[3]){
for(i=0;i<5;i++){Rates[i] = nullptr; Delays[i]=nullptr;};
fprintf(logFile,"(return -4)"); fflush(logFile);
PyObject *ret = Py_BuildValue("i",-4);
return ret;
};
};
//////////
// IF NUMBER:
IFNum[NIF-1] = IFN;
// Number of channels for this IF:
CPfile.read(reinterpret_cast<char*>(&Nchan[NIF-1]), sizeof(int));
fprintf(logFile, "IF%d has %i channels\n",IFN,Nchan[NIF-1]); fflush(logFile);
printf("IF%d (%i) has %i channels\n",IFN,NIF,Nchan[NIF-1]); fflush(stdout);
// Maximum number of channels:
if (Nchan[NIF-1] > MaxChan){
MaxChan=Nchan[NIF-1];
for (i=0; i< NBas; i++) {
CrossSpec00[i] = (cplx64f *) realloc(CrossSpec00[i],MaxChan*sizeof(cplx64f));
CrossSpec11[i] = (cplx64f *) realloc(CrossSpec11[i],MaxChan*sizeof(cplx64f));
if(!CrossSpec00[i] || !CrossSpec11[i]){
CrossSpec00[i]=nullptr; CrossSpec11[i]=nullptr;
fprintf(logFile,"(return -5)"); fflush(logFile);
PyObject *ret = Py_BuildValue("i",-5);
return ret;
};
};
};
// ignores noI
MPfile.ignore(sizeof(int));
// Have we applied parang??
MPfile.read(reinterpret_cast<char*>(&doParang), sizeof(bool));
// Get frequencies for this IF:
Frequencies[NIF-1] = new double[Nchan[NIF-1]];
CPfile.read(reinterpret_cast<char*>(Frequencies[NIF-1]), Nchan[NIF-1]*sizeof(double));
fprintf(logFile,"Freqs. %.8e %.8e\n",
Frequencies[NIF-1][0],Frequencies[NIF-1][Nchan[NIF-1]-1]);
fflush(logFile);
// Number of integration times:
int NDiffTimes = 0;
int TimeBuff = 1000;
double *DiffTimes = (double *) malloc(TimeBuff*sizeof(double));
bool isTime;
// Get Number of visibilities observed by the CalAnts (Circ Pol):
NCVis[NIF-1] = 0;
int AuxA1, AuxA2;
fprintf(logFile,"Reading CPfile...(NCalAnt=%d)\n", NCalAnt); fflush(logFile);
// eof() doesn't do what everyone thinks....
while(!CPfile.eof() && CPfile.peek() >= 0){
is1 = false; is2 = false;
// CPfile.ignore(sizeof(int));
CPfile.ignore(sizeof(double)); // daytemp
CPfile.read(reinterpret_cast<char*>(&AuxA1), sizeof(int));
CPfile.read(reinterpret_cast<char*>(&AuxA2), sizeof(int));
for (i=0; i<NCalAnt; i++) {
if (AuxA1 == CalAnts[i]){is1=true;};
if (AuxA2 == CalAnts[i]){is2=true;};
};
if(is1 && is2 && AuxA1 != AuxA2){
NCVis[NIF-1] += 1;
};
// here we are ignoring all the visibility data
CPfile.ignore(3*sizeof(double)+4*Nchan[NIF-1]*sizeof(cplx32f));
};
fprintf(logFile,"Finished CPfile...\n"); fflush(logFile);
// Get Number of visibilities observed by the CalAnts (Mix Pol):
NLVis[NIF-1] = 0;
fprintf(logFile,"Reading MPfile... (NCalAnt=%d)\n", NCalAnt); fflush(logFile);
// eof() doesn't do what everyone thinks....
while(!MPfile.eof() && MPfile.peek() >= 0){
is1 = false; is2 = false;
MPfile.ignore(sizeof(int));
MPfile.ignore(sizeof(double)); // Time
MPfile.read(reinterpret_cast<char*>(&AuxA1), sizeof(int));
MPfile.read(reinterpret_cast<char*>(&AuxA2), sizeof(int));
for (i=0; i<NCalAnt; i++) {
if (AuxA1 == CalAnts[i]){is1=true;};
if (AuxA2 == CalAnts[i]){is2=true;};
};
if(is1 && is2 && AuxA1 != AuxA2){
NLVis[NIF-1] += 1;
};
MPfile.ignore(3*sizeof(double)+12*Nchan[NIF-1]*sizeof(cplx32f));
};
fprintf(logFile,"Finished MPfile...\n"); fflush(logFile);
// Total number of visibilities:
NVis[NIF-1] = NCVis[NIF-1]+ NLVis[NIF-1];
sprintf(message,"Found %i vis in CPol and %i vis in LPol for a total of %i\n",
NCVis[NIF-1],NLVis[NIF-1],NVis[NIF-1]);
fprintf(logFile,"%s",message); fflush(logFile);
// Set memory for the visibilities:
j = NVis[NIF-1]+1;
Ant1[NIF-1] = (int*) malloc(j*sizeof(int));
Ant2[NIF-1] = (int*) malloc(j*sizeof(int));
Scan[NIF-1] = (int*) malloc(j*sizeof(int));
Times[NIF-1] = (double*) malloc(j*sizeof(double));
Weights[NIF-1] = (double*) malloc(j*sizeof(double));
PA1[NIF-1] = (cplx64f*) malloc(j*sizeof(cplx64f));
PA2[NIF-1] = (cplx64f*) malloc(j*sizeof(cplx64f));
UVGauss[NIF-1] = (double*) malloc(j*sizeof(double));
RR[NIF-1] = (cplx64f**) malloc(j*sizeof(cplx64f*));
LR[NIF-1] = (cplx64f**) malloc(j*sizeof(cplx64f*));
RL[NIF-1] = (cplx64f**) malloc(j*sizeof(cplx64f*));
LL[NIF-1] = (cplx64f**) malloc(j*sizeof(cplx64f*));
for (i=0;i<j;i++){
RR[NIF-1][i] = (cplx64f*) malloc(Nchan[NIF-1]*sizeof(cplx64f));
LR[NIF-1][i] = (cplx64f*) malloc(Nchan[NIF-1]*sizeof(cplx64f));
RL[NIF-1][i] = (cplx64f*) malloc(Nchan[NIF-1]*sizeof(cplx64f));
LL[NIF-1][i] = (cplx64f*) malloc(Nchan[NIF-1]*sizeof(cplx64f));
};
// Rewind files:
CPfile.clear();
CPfile.seekg(sizeof(int)+Nchan[NIF-1]*sizeof(double),CPfile.beg);
MPfile.clear();