-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpolconvert_CASA.py
executable file
·1379 lines (1072 loc) · 46.3 KB
/
polconvert_CASA.py
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) Ivan Marti-Vidal 2012-2023
# EU ALMA Regional Center. Nordic node.
# Universitat de 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 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, see <http://www.gnu.org/licenses/>,
# or write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# a. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# b. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the
# distribution.
# c. Neither the name of the author nor the names of contributors may
# be used to endorse or promote products derived from this software
# without specific prior written permission.
#
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#
#
from __future__ import absolute_import
from __future__ import print_function
__version__ = "2.0.7 " # 7 characters
date = 'Aug 14, 2023'
################
# Import all necessary modules.
import os,sys,shutil,re
import gc
import time
import struct as stk
import scipy.optimize as spopt
import numpy as np
import pylab as pl
import datetime as dt
import pickle as pk
mypath = os.path.dirname(__file__)
sys.path.append(mypath)
import polconvert_standalone as PCONV
if sys.version_info.major < 3:
try:
from taskinit import *
tb = gentools(['tb'])[0]
except Exception as ex:
print('unable to load casa tools in python2\n\n')
raise ex
else:
try:
# ms is not actually used
# from casatools import ms as ms_casa
from casatools import table as tb_casa
tb = tb_casa()
except Exception as ex:
print('unable to load casa tools in python3\n\n')
raise ex
################
# this is the CASA xml-based command sequence:
# defaults are supplied & consistent with version 2.0.7
def polconvert(IDI='', OUTPUTIDI='', DiFXinput='', DiFXcalc='', doIF=[], linAntIdx=[1],
Range=[], ALMAant='', spw=-1, calAPP='', calAPPTime=[0.,5.], APPrefant='',
gains=[['NONE']], interpolation=[], gainmode=[], XYavgTime=0.0,
dterms=['NONE'], amp_norm=0.01, XYadd={}, XYdel={}, XYratio={},
usePcal={}, swapXY=[False], swapRL=False, feedRotation=[],
correctParangle=False, IDI_conjugated=False, plotIF=-1,
plotRange=[], plotAnt=-1, excludeAnts=[], excludeBaselines=[],
doSolve=-1, solint=[1,1], doTest=True, npix=50, solveAmp=True,
solveMethod='COBYLA', calstokes=[1.,0.,0.,0.], calfield=-1,
saveArgs=False):
""" POLCONVERT - CASA INTERFACE VERSION 2.0.7.
Converts VLBI visibilities from mixed-polarization (linear-circular)
into a circular basis. Works with single VLBI stations as well as with
calibrated phased arrays (i.e., phased ALMA).
IDI: Input FITS-IDI file with VLBI visibilities. It can also be a
directory containing SWIN files from DiFX.
OUTPUTIDI: Output FITS-IDI file (or SWIN directory). If equal to
IDI, the file(s) will be overwritten.
DiFXinput: If SWIN files are being converted, this must be the
*.input file used by DiFX.
DiFXcalc: If SWIN files are being converted, this must be the
*.calc file used by DiFX. This is optional, but if it is not
provided, the cross-polarization gain estimates may be incorrect
if doSolve>0.
doIF = List of IFs to convert (starts from 1). Default means all.
linAntIdx: List of indices of the linear-polarization antennas
in the IDI/SWIN file (lowest index starts with 1).
It can also be a list of the antenna code names.
Range: Time range to convert (integer list; AIPS format). Default
(empty list) means all data.
ALMAant: If ALMA has been used, this is the antenna table from
the MS with the intra-ALMA visibilities.
spw: Spectral window in ALMA visibilities that contains the VLBI band. If
negative, the program will derive it automatically.
calAPP: If ALMA has been used, this is the combined
ASDM_CALAPPPHASE table from the ASDM. The list of
measurement sets can also be given (so the table is
concatenated from all of them).
calAPPTime: Time shift and time tolerance (in sec) for the
CALAPPPHASE table obtained from the ASDM.
APPrefant: If not empty, re-reference the TelCal phases, assuming
that the X-Y phase-difference table provided in 'gains'
(see keyword below) uses APPrefant as the reference
antenna. Notice that the name of the gain table with
the X-Y phase differences has to contain the string
'.XY0'.
gains: Gain tables to pre-calibrate the linear-pol VLBI stations
(one list of gains per linear-pol station).
interpolation: Interpolation type to use (one per calibration
table). Tells whether to apply linear or nearest
interpolation. Default is to use linear for all
tables.
gainmode: Mode of gain calibration to impose (one per calibration
table). Default is 'T' for all tables, unless either
the string 'XY0', 'bandpass' or 'Gxyamp' appears in the
table name. The gain types can be either 'G' (i.e.,
split gains per polarization) or 'T' (i.e., combine
polarizations).
XYavgTime: Re-compute the G-mode gains by adding a time smoothing
of X-Y differences to the T-mode gains. Default is NOT
to do this (i.e., use the G-mode gains as given). If
positive, use a running time average with this size
(in seconds).
dterms: D-term tables to pre-calibrate the linear-pol VLBI
stations (one table per linear-pol station).
amp_norm: If positive, normalize the amplitude correction to the
X-Y average, and save the scaling factor (vs time) in
an external (ASCII) file (ANTAB format, assuming a
DPFU=amp_norm). If zero, or negative, apply the
amplitude correction as is.
XYadd: Add manually a phase between X and Y before conversion (in
deg.). This is a dictionary (antenna codes as keywords) with
elements set to either a list of one value per IF OR a
list of lists (one value per channel, for each IF).
XYdel: Add manually a multiband delay between X and Y before
conversion (in deg./chanel). This is a dictionary (antenna codes
as keywords) with one value per linear-pol station.
XYratio: Add manually an amplitude ratio between X and Y before
conversion (R=X/Y). Follows the same format as XYadd. If
a negative value is given for an antenna, the X/Y ratio
will be estimated from its autocorrelations (the
spectrum for antenna i will be computed using a
running-median filter of width equal to -1/XYratio[i] of
the IF bandwidth). If 0.0 is given for an antenna, the
ratio will be estimated from the phasecal amplitudes (as
long as usePcal is True and there are pcal files
available; not supported for FITS-IDI).
usePcal: Dictionary (antenna codes as keywords), whose elements
are booleans (i.e., one boolean per linear-polarization
station). If True, use the X-Y difference of phasecal
tones as an estimate of the X-Y cross-polarization
phase. Default means to NOT use the phasecals.
swapXY: If true, swap the X-Y channels (one boolean per antenna).
swapRL: Swap R-L of the OTHER antenna(s) when plotting the
fringes.
feedRotation: Rotation angle of the feed (one value per antenna,
in degrees). Default means zero degrees (so that
X is truly horizontal for the linear-pol.
antennas). These angles are used in the
gain-solver step.
correctParangle: If True, the correction for parallactic angle
is applied to the converted antenna(s).
IDI_conjugated: Assume a swap in the baseline defintion (i.e.,
conjugation) of the FITS-IDI file. This has NO
effect on SWIN files.
plotIF: IF index(es) to plot. Default means to NOT plot. An
empty list, [], means to plot ALL IFs being converted
(but do not forget to set plotRange and plotAnt!)
plotRange: Time range to plot (integer list; AIPS format).
Default means to NOT plot.
plotAnt: Index of the other antenna in the baseline to plot.
Default means to NOT plot.
excludeAnts: List of antennas (i.e., list of antenna codenames)
to NOT use in the cross-polarization gain
estimates.
excludeBaselines: List of baselines (i.e., a list of lists of
two antenna codenames) to NOT use in the
cross-polarization gain estimates.
doSolve: If negative, do not estimate the cross-polarization
gains. If positive or zero, estimate the gains using
a Global Cross-Pol Fringe Fitting (GCPFF). The gains
are fitted with an error function (Chi Square)
defined as:
sum( doSolve*(RR/LL-1)^2 + (RL^2 + LR^2) ),
so that doSolve=0 minimizes the cross-hand
polarizations (so it assumes a small linear
polarization of the source), whereas doSolve >> 1
assumes a negligible Stokes V.
solint: If solint[0] null or negative, solve the
cross-polarization phase plus a multi-band delay (MBD).
If not, solve in bandpass mode by averaging solint[0]
channels per solution. Divide the solution time range
(per scan) in solint[1] chunks (i.e., in solint[1]
subscans). I.e., if solint[1]==1, the fringes are fully
averaged in time for each scan (but corrected for the
scan fringe rates) before the GPLFF condition is
computed. solint[2] is the minimum time jump (in
seconds) to split the data into different scans
(default: 50 seconds).
doTest: If true, only compute (and eventually plot), the data,
but leave OUTPUTIDI untouched.
npix: Number of pixels for the fringe plots (and fringe search).
solveAmp: If the cross-polarization gains are being estimated,
solve also for the X/Y amplitude ratios.
solveMethod: Method for the minimization of the Chi squared in
the GCPFF. Can be, for instance: 'COBYLA'.
calstokes: Stokes parameters, [I,Q,U,V] of the calibrator (of
course, this is only used if doSolve is not negative).
The total intensity is not needed in the calibration
(i.e., calstokes[0] can be left to 1.0, so that the
other parameters will correspond to fractional
polarization).
calfield: If not negative, field ID of the calibrator (useful
if a time range covering several scans is being used
in the GCPFF). If negative, use all data in the time
range, regardless of the field ID.
saveArgs: If true, arguments are saved in a file PolConvert_CASA.last
(a pickled dictionary); note that nothing currently reads this file.
"""
############################################
# this turns into the verbosity argument of _PolConvert.so
print('Entered polconvert_CASA::polconvert()')
DEBUG = False
if 'POLCONVERTDEBUG' in os.environ:
if os.environ['POLCONVERTDEBUG'] == 'True': DEBUG = True
else: DEBUG = False
print('DEBUG setting is ' + str(DEBUG))
print('__name__ is ' + __name__)
# Auxiliary function: print error and raise exception:
def printError(msg):
print(msg,'\n')
lfile = open("PolConvert.log","a")
print('\n'+msg+'\n', file=lfile)
lfile.close()
sys.stdout.flush()
raise Exception(msg)
# Auxiliary function: print message (terminal and log):
def printMsg(msg, doterm=True, dolog=True):
if doterm:
print(msg)
if dolog:
lfile = open("PolConvert.log","a")
print(msg,file=lfile)
sys.stdout.flush()
lfile.close()
# Auxiliary function: Geometric Median of a complex number:
def geoMedian(Window, method='phasor'):
WinData = np.array(Window)
# Simplest approach (Amp & Phase separate). Assume NO WRAPS in phase:
pAvg = np.median(np.abs(WinData))*np.exp(1.j*np.median(np.angle(WinData)))
if method=='phasor':
return pAvg
# A bit more complicated (point of minimum distance).
elif method=='Torricelli':
def L1Dist(p):
return np.sum(np.abs(WinData-p[0]-1.j*p[1]))
Torr = spopt.minimize(L1Dist, [pAvg.real, pAvg.imag], method='COBYLA')
return Torr.x[0] + 1.j*Torr.x[1]
# Auxiliary function: Smooth the X-Y difference of G-mode gains
# using a running average:
def XYsmooth(GAINTABLE, DTIME, SPW, IANT=-1):
os.system('rm -rf %s.XYsmooth.PolConvert'%GAINTABLE)
try:
os.system('cp -r %s %s.XYsmooth.PolConvert'%(GAINTABLE,GAINTABLE))
tb.open('%s.XYsmooth.PolConvert/ANTENNA'%GAINTABLE)
GallAnts = tb.getcol('NAME')
tb.close()
if IANT>=0:
allAnts = [IANT]
else:
allAnts = list(range(len(GallAnts)))
tb.open('%s.XYsmooth.PolConvert'%GAINTABLE,nomodify=False)
Gtimes = tb.getcol('TIME')
Gants = tb.getcol('ANTENNA1')
Gspws = tb.getcol('SPECTRAL_WINDOW_ID')
Gflg = np.logical_not(tb.getcol('FLAG'))
if np.shape(Gflg)[0] > 1:
isT = 1
else:
isT = 0
# print isT, np.shape(Gflg)
Ggood = np.logical_and(Gflg[0,0,:],Gflg[isT,0,:])
Mask = np.logical_and(Gspws == SPW, Ggood)
Ggains = tb.getcol('CPARAM')
except:
printError('ERROR: Bad gain table %s!'%GAINTABLE)
# Get the X-Y cross-phase gain:
GDiff = Ggains[0,0,:]/Ggains[isT,0,:]
# Get the polarization-independent gain:
TMode = Ggains[0,0,:]*Ggains[isT,0,:]
# Smooth them:
for iant in allAnts:
Mask2 = np.where(np.logical_and(Mask,Gants==iant))[0]
sys.stdout.write('\rSmoothing X-Y difference for antenna %s (%i of %i) '%(GallAnts[iant],iant+1,len(GallAnts)))
sys.stdout.flush()
for tii,ti in enumerate(Mask2):
Window = []
tij = tii
while tij>=0 and np.abs(Gtimes[ti]-Gtimes[Mask2[tij]])<DTIME/2.:
Window.append(GDiff[Mask2[tij]])
tij -= 1
tij = tii+1
while tij<len(Mask2) and np.abs(Gtimes[ti]-Gtimes[Mask2[tij]])<DTIME/2.:
Window.append(GDiff[Mask2[tij]])
tij += 1
# Median (normalized):
AvgDiff = geoMedian(Window)
AvgDiff /= np.abs(AvgDiff)
Ggains[0,0,ti] = np.sqrt(TMode[ti]*AvgDiff)
Ggains[isT,0,ti] = np.sqrt(TMode[ti]/AvgDiff)
# Update the table:
tb.putcol('CPARAM',Ggains)
tb.close()
print('\nDONE!\n\n')
# Auxiliary function: Re-reference XY-phases to
# another ALMA refant, using the CalAPPPhase table:
def ReReference(CALAPPPHASE,XY0,SPW,REFANT):
printMsg("\n\n GOING TO RE-REFERENCE X-Y PHASES TO %s.\n\n"%REFANT)
DTMax = 180. # Minimum time gap (sec) to assume that TelCal has been reset.
# Figure out the baseband:
tb.open('%s/SPECTRAL_WINDOW'%XY0)
try:
spname = tb.getcol('NAME')[SPW]
BB = [ii for ii in spname.split('#') if "BB_" in ii][0]
except:
printError("\n ERROR: BAD NAME FOR SPW %i IN TABLE %s\n"%(SPW,XY0))
tb.close()
# Read TelCal's phases:
tb.open(CALAPPPHASE)
IMAX = np.argmax(tb.getcol('numPhasedAntennas'))
ANT = list(tb.getcell('phasedAntennas',IMAX))
try:
REFIDX = ANT.index(REFANT)
except:
printError("\n\n ERROR: ANTENNA %s IS NOT IN CALAPP-PHASE TABLE!"%REFANT)
ti = tb.getcol('startValidTime')
bb = tb.getcol('basebandName')
Tsort = np.argsort(ti)
Torder = np.copy(ti[Tsort])
UT = 24.*(Torder/86400.-int(Torder[0]/86400.))
# Arrange phases of the REFANT:
Gains = []
for i in range(len(ti)):
aux = list(tb.getcell('phasedAntennas', rownr = i))
try:
REFI = aux.index(REFANT)
aux2 = tb.getcell('phaseValues', rownr = i)
NIF = tb.getcell('numChannels', rownr = i)
Gains.append([aux2[NIF*REFI:NIF*(REFI+1)],aux2[NIF*(REFI+len(aux)):NIF*(REFI+len(aux)+1)]])
except:
printMsg("WARNING: ANTENNA %s NOT IN LIST OF PHASE ANTENNAS AT TIME %.1f.\n THE RESULTING X-Y PHASES MAY BE *WRONG* AT THIS TIME!"%(REFANT,ti))
Gains.append([np.zeros(NIF),np.zeros(NIF)])
GainsA = np.array(Gains)
# Filter phases for the SPW:
b1 = bb[Tsort] == BB
Nchan = np.shape(GainsA)[-1]
IX = np.copy(GainsA[Tsort,:][b1,0,:])
IY = np.copy(GainsA[Tsort,:][b1,1,:])
tb.close()
Mask = np.where(np.logical_and(Torder[b1][1:]-Torder[b1][:-1]>DTMax,IX[:-1,0]!=0.0))
# Plot TelCal phases for REFANT:
plfig = pl.figure()
plsub = plfig.add_subplot(111)
symb = ['or','og','ob','ok','om','oy','^r','^g','^b','^k','^m','^y']
XYDiff = []
AvXY = []
for ni in range(Nchan):
IntX = IX[1:,ni][Mask]
IntY = IY[1:,ni][Mask]
XYDiff.append(np.mod(IntX-IntY,2.*np.pi))
# We take the time MEDIAN as the best XY-phase estimate for the REFANT:
AvXY.append(np.median(XYDiff[-1]))
plsub.plot(UT[b1][1:][Mask],XYDiff[-1]*180./np.pi,symb[ni],label='CH %i'%(ni+1))
printMsg('For antenna %s, Chan %i, found TelCal median X-Y phase of %.1f deg.'%(REFANT,ni+1,180./np.pi*AvXY[-1]))
pl.legend(numpoints=1)
plsub.set_xlabel('UT (h)')
plsub.set_ylabel('X-Y phase (deg.)')
plsub.set_xlim((0,24))
pl.title('TelCal X-Y phases for new REFANT: %s'%REFANT)
pl.savefig('%s.RE-REFERENCING.png'%CALAPPPHASE)
# Correct XY=phase table:
printMsg("\n\n ADDING PHASES TO NEW XY0 TABLE\n\n")
os.system("rm -rf %s.REFANT_%s"%(XY0,REFANT))
os.system("cp -r %s %s.REFANT_%s"%(XY0,XY0,REFANT))
tb.open("%s.REFANT_%s"%(XY0,REFANT),nomodify=False)
spwi = tb.getcol('SPECTRAL_WINDOW_ID')
XYData = []
Mask = spwi==SPW
for si in np.where(Mask)[0]:
XYData.append(tb.getcell("CPARAM",si))
XYData = np.array(XYData)
# XYData = tb.getcol("CPARAM")
# Mask = spwi==SPW
NNu = np.shape(XYData)[1]
NuPerChan = NNu/Nchan
for ni in range(Nchan):
XYData[0,ni*NuPerChan:(ni+1)*NuPerChan,Mask] *= np.exp(-1.j*(AvXY[ni]))
for sii,si in enumerate(np.where(Mask)[0]):
tb.putcell("CPARAM",si,XYData[sii])
# tb.putcol("CPARAM",XYData)
tb.close()
# Plot new vs. old XY-phases:
Idx = np.where(Mask)[0][0]
tb.open(XY0)
XOrig = tb.getcell("CPARAM",Idx)[0,:]
YOrig = tb.getcell("CPARAM",Idx)[1,:]
tb.close()
plfig.clf()
plsub = plfig.add_subplot(111)
plsub.plot(np.angle(XOrig/YOrig)*180./np.pi,'or',label='Original')
tb.open("%s.REFANT_%s"%(XY0,REFANT))
XOrig = tb.getcell("CPARAM",Idx)[0,:]
YOrig = tb.getcell("CPARAM",Idx)[1,:]
tb.close()
plsub.plot(np.angle(XOrig/YOrig)*180./np.pi,'ob',label='Re-Referenced')
pl.legend(loc=0,numpoints=1)
plsub.set_ylim((-250,250))
pl.savefig('%s_RE-REF_XYPHASES.png'%XY0)
# Auxiliary function: unwrap phases for time interpolation
def unwrap(phases, check=False):
dims = np.shape(phases)
if dims[1]==0: # Bandpass type
for i in range(len(phases)-1):
if phases[i+1]-phases[i] > np.pi:
phases[i+1,:] -= 2.*np.pi
elif phases[i+1]-phases[i] < -np.pi:
phases[i+1,:] += 2.*np.pi
if check:
pl.figure()
pl.plot(180./np.pi*phases)
elif dims[0]>1: # Bandpass-gain type
for j in range(dims[1]):
for i in range(dims[0]-1):
if phases[i+1,j]-phases[i,j] > np.pi:
phases[i+1:,j] -= 2.*np.pi
printMsg('Adding phase wrap to gain at channel %i'%i)
elif phases[i+1,j]-phases[i,j] < -np.pi:
phases[i+1:,j] += 2.*np.pi
printMsg('Removing phase wrap to gain at channel %i'%i)
if check:
pl.figure()
pl.plot(180./np.pi*phases[:,0])
# Auxiliary function: prepare the CALAPPPHASE, from a set of MSs:
def makeCalAPP(mslist):
printMsg('Will create CALAPPPHASE table from measurement sets.')
os.system('rm -rf ./CALAPPPHASE.tab')
for i,asd in enumerate(mslist):
printMsg('Working out MS #%i - %s'%(i+1,asd))
if i==0:
os.system('cp -rf %s/ASDM_CALAPPPHASE ./CALAPPPHASE.tab'%asd)
else:
tb.open(os.path.join(asd,'ASDM_CALAPPPHASE'))
tb.copyrows('./CALAPPPHASE.tab')
tb.close()
tb.open(os.path.join(asd,'ASDM_CALAPPPHASE'))
time0 = tb.getcol('startValidTime')
time1 = tb.getcol('endValidTime')
tb.close()
tb.open(asd)
mstime = tb.getcol('TIME')
tb.close()
if len(time0)==0:
printMsg('WARNING! NO APPCAL DATA FOR %s\n'%asd)
else:
print('\n\nMEAS SET %s:'%asd)
tmin = np.min(time0)/86400.
tmax = np.max(time1)/86400.
mstmin = np.min(mstime)/86400.
mstmax = np.max(mstime)/86400.
printMsg('MS TIME RUNS FROM %8.5f TO %8.5f DAYS'%(mstmin,mstmax))
if len(time0)>0:
printMsg('FOR APP TABLE, TIME RUNS FROM %8.5f TO %8.5f DAYS'%(tmin,tmax))
return timeranges
tic = time.time()
#########################################
# DO SOME SANITY CHECKS OF PARAMETERS
try:
doSolve = float(doSolve)
except:
printError("ERROR! doSolve should be a float!")
nALMA = len(linAntIdx)
if not os.path.exists(IDI):
printError("ERROR! IDI file (or SWIN folder) does not exist!")
if type(calAPP) is list:
try:
makeCalAPP(calAPP)
calAPP = './CALAPPPHASE.tab'
except:
printError('ERROR: Could not create nor interprete the CALAPPPHASE table!')
c0 = len(calAPP) == 0 ; c1 = len(ALMAant) == 0
if c0 != c1:
printError("ERROR! either both or none of calAPP and ALMAant need to be set!")
if c0:
printMsg("WARNING: calAPP and ALMAant are not set.")
printMsg("ALMA CAL TABLES DO NOT SEEM TO BE USED, ACCORDING TO USER.")
printMsg("This is not a problem in some cases...(isPhased now False).")
isPhased=False
else:
if not os.path.exists(calAPP) or not os.path.exists(ALMAant):
printError("ERROR! calAPP and/or ALMAant WERE NOT FOUND!")
else:
isPhased = True
try:
spw = int(spw)
except:
printError("ERROR! spw should be an integer!")
if len(gains)!= nALMA or len(dterms)!= nALMA:
printError("Invalid format for gains and/or dterms!\n Should be lists as large as the number of linear-polarization VLBI stations!")
# Sanity check for interpolation:
if type(interpolation) is not list:
printError("Interpolation must be a list (or a list of lists)!")
if len(interpolation)==0:
interpolation = [[] for i in range(nALMA)]
if len(interpolation)!= nALMA:
printError("Wrong length for interpolation!")
for i,intype in enumerate(interpolation):
if type(intype) is not list:
printError("Interpolation must be a list (or a list of of lists)!")
if len(intype)==0:
interpolation[i] = ['linear' for g in gains[i]]
intype = interpolation[i]
if len(intype) != len(gains[i]):
printError("Interpolation must have the same dimensions as gains!")
for ints in intype:
if ints not in ['linear','nearest']:
printMsg("integration type " + ints + " requested.")
printError("Only \'linear\' and \'nearest\' interpolations are supported!")
try:
XYavgTime = float(XYavgTime)
except:
printError("XYavgTime must be a positive (or zero) double!")
if XYavgTime < 0.0:
printError("XYavgTime must be positive or zero!")
if type(gainmode) is not list:
printError("gainmode must be a list (or a list of lists)!")
if len(gainmode)==0:
gainmode = [[] for i in range(nALMA)]
if len(gainmode)!= nALMA:
printError("Wrong length for gainmode!")
for i,gtype in enumerate(gainmode):
if type(gtype) is not list:
printError("gainmode must be a list (or a list of of lists)!")
if len(gtype)==0:
gainmode[i] = [{True:'G',False:'T'}['XY0' in g or 'bandpass' in g or 'Gxyamp' in g] for g in gains[i]]
gtype = gainmode[i]
if len(gtype) != len(gains[i]):
printError("gainmode must have the same dimensions as gains!")
for ints in gtype:
if ints not in ['G','T','S']:
printMsg("Gain type " + ints + " requested.")
printError("Only \'G\', \'S\' and \'T\' interpolations are supported!")
for gi in range(len(gains)):
for gii in range(len(gains[gi])):
printMsg('Will calibrate with table %s in %s mode.'%(os.path.basename(gains[gi][gii]),gainmode[gi][gii]))
# Sanity check for gains and dterms:
for g in gains:
if type(g) is not list:
printError("Invalid format for gains!\n Each element should be a list with the names of the gain tables for the ith linear-pol. VLBI station")
else:
for elem in g:
if type(elem) is not str:
printError("Invalid format for gains!\n Each element (of each element) should be a string (the name of a calibration table)")
for elem in dterms:
if type(elem) is not str:
printError("Invalid format for dterms!\n Each element should be a string (the name of a calibration table)")
if len(calAPPTime) != 2:
printError("Bad format for calAPPTime. Should be a list of 2 floats!")
try:
CALAPPTSHIFT, CALAPPDT = [float(cc) for cc in calAPPTime]
except:
printError("Bad format for calAPPTime. Should be a list of 2 floats!")
#########################################
######################
# READ IDs OF ALMA ANTENNAS USED IN THE PHASING:
if isPhased:
success = tb.open(calAPP)
if not success:
printError('ERROR: INVALID calAPP TABLE!')
try:
time0 = tb.getcol('startValidTime')-CALAPPDT+CALAPPTSHIFT
time1 = tb.getcol('endValidTime')+CALAPPDT+CALAPPTSHIFT
#########
# Figure out times with unphased data:
ADJ = tb.getcol('adjustToken')
BBN = tb.getcol('basebandName')
BBC = ['BB_%i'%i for i in range(1,5)]
sc2flag = [[] for i in range(4)] # Currently, we assume 1 spw per BBC in the APP mode.
scgood = [[] for i in range(4)] # Currently, we assume 1 spw per BBC in the APP mode.
timeranges = [[] for i in range(4)] # Currently, we assume 1 spw per BBC in the APP mode.
for j in range(4):
sc2flag[j] = [i for i in range(len(ADJ)) if BBN[i]==BBC[j] and ('PHASE_UPDATED' not in ADJ[i]) and ('PHASE_NOCHANGE' not in ADJ[i])]
scgood[j] = [i for i in range(len(ADJ)) if BBN[i]==BBC[j] and ('PHASE_UPDATED' in ADJ[i] or 'PHASE_NOCHANGE' in ADJ[i])]
SUBSCANDUR = time1[sc2flag[0][0]] - time0[sc2flag[0][0]]
sec = 1.
for j in range(4):
if len(sc2flag[j])>0:
for si in sc2flag[j]:
timerange = [time1[si]-SUBSCANDUR-sec, time1[si]+sec]
if timerange not in timeranges[j]:
timeranges[j].append(timerange)
for si in scgood[j]:
timerange = [time1[si]-SUBSCANDUR-sec,time0[si]+sec]
if si-1 in sc2flag[j]:
if timerange not in timeranges[j]:
timeranges[j].append(timerange)
# For testing:
timerangesArr = [np.array(ti) for ti in timeranges]
#########
nphant = tb.getcol('numPhasedAntennas')
refs = tb.getcol('refAntennaIndex')
asdmtimes = [time0,time1]
phants = []
for i in range(len(nphant)):
phants.append(list(tb.getcell('phasedAntennas', rownr = i)))
tb.close()
except:
printError('ERROR: INVALID calAPP TABLE CONTENT!')
success = tb.open(ALMAant)
if not success:
printError('ERROR: NO VALID ANTENNA TABLE FOUND!')
allants = list(tb.getcol('NAME'))
allantidx = list(range(len(allants)))
tb.close()
refants = np.array([allants.index(phants[t][refs[t]]) for t in range(len(phants))])
antimes = [[[0.,0.]] for ian in allants]
for ian,antenna in enumerate(allants):
for j,phan in enumerate(phants):
if antenna in phan:
antimes[ian].append([time0[j]-CALAPPDT+CALAPPTSHIFT,time1[j]+CALAPPDT+CALAPPTSHIFT])
auxarr = np.zeros((len(antimes[ian]),2))
auxarr[:] = antimes[ian]
antimes[ian] = auxarr
nphtimes = [len(anti) for anti in antimes]
else: # is(not)Phased
# Not a phased array. Dummy values for phasing info:
allants = [1]
allantidx = [0]
antimes = [np.array([0.,1.e20])]
nphtimes = [1]
refants = np.array([0],dtype=np.int)
asdmtimes = [np.array([0.]),np.array([1.e20])]
timerangesArr = [np.array([0.,0.]) for i in range(4)]
######################
#######
# CHECK IF THIS IS A FITS-IDI FILE OR A SWIN DIR.:
if os.path.isdir(IDI):
isSWIN = True
try:
printMsg('Opening "%s"' % (DiFXcalc))
antcodes = [];
calc = open(DiFXcalc)
lines = calc.readlines()
calc.close()
for ii, line in enumerate(lines):
if 'TELESCOPE' in line and 'NAME' in line:
antcodes.append(line.split()[-1])
except:
raise Exception('ERROR! NO ANTENNAS FOUND IN CALC FILE!')
ifile = open(DiFXinput)
inputlines = ifile.readlines()
ifile.close()
FreqL = [inputlines.index(l) for l in inputlines if 'FREQ TABLE' in l]
# ONLY ONE FREQ TABLE IS ALLOWED:
try:
fr = FreqL[0]
Nfreq = int(list(filter(
lambda x: 'FREQ ENTRIES' in x, inputlines[fr+1:]))[0].split()[-1])
Nr = list(range(Nfreq))
except Exception as ex:
printMsg(str(ex))
printError("BAD input file!")
FrInfo = {'FREQ (MHZ)':[0. for i in Nr], 'BW (MHZ)':[0. for i in Nr],
'SIDEBAND':['U' for i in Nr], 'NUM CHANNELS':[1 for i in Nr],
'CHANS TO AVG':[1 for i in Nr], 'OVERSAMPLE FAC.':[1 for i in Nr],
'DECIMATION FAC.':[1 for i in Nr], 'SIGN' :[1. for i in Nr]}
# READ METADATA FOR ALL FREQUENCIES:
for entry in FrInfo.keys():
for line in inputlines[fr+1:]:
if entry in line:
index = int((line.split(':')[0]).split()[-1])
FrInfo[entry][index] = type(FrInfo[entry][0])(line.split(':')[-1].strip())
# SORT OUT THE CHANNEL FREQUENCIES:
if len(doIF)==0:
doIF = list(range(1,len(Nr)+1))
metadata = []
IFchan = 0
for nu in Nr:
nu0 = FrInfo['FREQ (MHZ)'][nu]
bw = FrInfo['BW (MHZ)'][nu]
nchan = FrInfo['NUM CHANNELS'][nu]
# MAX. NUMBER OF CHANNELS:
chav = FrInfo['CHANS TO AVG'][nu]
if nu in doIF:
IFchan = max([IFchan,int(nchan/chav)])
sb = {True: 1.0 , False: -1.0}[FrInfo['SIDEBAND'][nu] == 'U']
FrInfo['SIGN'][nu] = float(sb)
if float(nchan//chav) != nchan/chav:
printMsg("linspace check chan: %d / %d = %f" %
(nchan, chav, nchan/chav))
freqs = (nu0 + np.linspace((sb-1.)/2.,(sb+1.)/2.,
nchan//chav, # should be exactly divisible
endpoint=False)*bw)*1.e6
metadata.append(freqs)
elif os.path.isfile(IDI):
isSWIN = False
try:
import pyfits as pf
ffile = pf.open(IDI)
antcodes = [ff[:2] for ff in ffile['ANTENNA'].data['ANNAME']]
except:
raise Exception('ERROR! NO ANTENNAS FOUND IN FITS FILE!')
# READ FREQUENCY INFO TO HELP SELECTING THE SPW AUTOMATICALLY:
fitsf = pf.open(IDI)
nu0 = fitsf['FREQUENCY'].header['REF_FREQ']
bw = fitsf['FREQUENCY'].header['CHAN_BW']
nch = fitsf['FREQUENCY'].header['NO_CHAN'] #*fitsf['FREQUENCY'].header['NO_BAND']
IFchan = nch
Nr = fitsf['FREQUENCY'].header['NO_BAND']
sgn = {True:1.0,False:-1.0}[bw>0.0]
FrInfo = {'FREQ (MHZ)':[], 'BW (MHZ)':[], 'SIGN':[], 'NUM CHANNELS':[]}
if sgn:
FrInfo['SIDEBAND'] = ['U' for i in range(Nr)]
else:
FrInfo['SIDEBAND'] = ['L' for i in range(Nr)]
metadata = []
for i in range(Nr):
FrInfo['FREQ (MHZ)'] += [(nu0 + i*bw*nch)/1.e6]
FrInfo['BW (MHZ)'] += [bw*nch/1.e6]
FrInfo['SIGN'] += [sgn]
FrInfo['NUM CHANNELS'] += [int(nch)]
freqs = nu0 + np.linspace((sgn-1.)/2.,(sgn+1.)/2.,nch,endpoint=False)*bw
metadata.append(freqs)
FrInfo['CHANS TO AVG'] = [1 for i in range(Nr)]
FrInfo['OVERSAMPLE FAC.'] = [1 for i in range(Nr)]
FrInfo['DECIMATION FAC.']=[1 for i in range(Nr)]