Coverage for /wheeldirectory/casa-6.7.0-12-py3.10.el8/lib/py/lib/python3.10/site-packages/casatasks/private/task_gaincal.py: 94%
94 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-01 07:19 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-01 07:19 +0000
1import os
2import numpy as np
4from .callibrary import *
5from casatools import calibrater
6from casatasks import casalog
8def gaincal(vis=None,caltable=None,
9 field=None,spw=None,intent=None,
10 selectdata=None,timerange=None,uvrange=None,antenna=None,scan=None,
11 observation=None, msselect=None,
12 solint=None,combine=None,preavg=None,
13 refant=None,refantmode=None,minblperant=None,
14 minsnr=None,solnorm=None,normtype=None,
15 gaintype=None,smodel=None,calmode=None,solmode=None,rmsthresh=None,corrdepflags=None,
16 append=None,
17 splinetime=None,npointaver=None,phasewrap=None,
18 docallib=None,callib=None,
19 gaintable=None,gainfield=None,interp=None,spwmap=None,
20 parang=None):
22 #Python script
23 casalog.origin('gaincal')
25 try:
26 mycb = calibrater()
28 # Revert to old VI for GSPLINE
29 if (gaintype=='GSPLINE'):
30 mycb.setvi(old=True,quiet=False);
32 if ((type(vis)==str) & (os.path.exists(vis))):
33 mycb.open(filename=vis,compress=False,addcorr=False,addmodel=False)
34 else:
35 raise ValueError('Visibility data set not found - please verify the name')
37 # Do data selection according to selectdata
38 casalog.post("NB: gaincal automatically excludes auto-correlations.")
39 if (selectdata):
40 # insist no ACs
41 if len(msselect)>0:
42 msselect='('+msselect+') && ANTENNA1!=ANTENNA2'
43 else:
44 msselect='ANTENNA1!=ANTENNA2'
46 # pass all data selection parameters in as specified
47 mycb.selectvis(time=timerange,spw=spw, scan=scan, field=field,
48 intent=intent, observation=str(observation),
49 baseline=antenna,uvrange=uvrange,chanmode='none',
50 msselect=msselect);
51 else:
52 # selectdata=F, so time,scan,baseline,uvrange,msselect=''
53 # using spw and field specifications only
54 # also insist no ACs
55 mycb.selectvis(time='',spw=spw,scan='',field=field,intent=intent,
56 observation='', baseline='', uvrange='',
57 chanmode='none', msselect='ANTENNA1!=ANTENNA2')
59 # signal use of correlation-dependent flags, if requested
60 if corrdepflags:
61 mycb.setcorrdepflags(True)
64 # set the model, if specified
65 if (len(smodel)>0):
66 mycb.setptmodel(smodel);
69 # Arrange applies....
71 if docallib:
72 # by cal library from file
73 mycallib=callibrary()
74 mycallib.read(callib)
75 mycb.setcallib(mycallib.cld)
77 else:
79 # by traditional parameters
81 ngaintab = 0;
82 if (gaintable!=['']):
83 ngaintab=len(gaintable)
85 ngainfld = len(gainfield)
86 nspwmap = len(spwmap)
87 ninterp = len(interp)
89 # handle list of list issues with spwmap
90 if (nspwmap>0):
91 if (type(spwmap[0])!=list):
92 # first element not a list, only one spwmap specified
93 # make it a list of list
94 spwmap=[spwmap];
95 nspwmap=1;
97 for igt in range(ngaintab):
98 if (gaintable[igt]!=''):
100 # field selection is null unless specified
101 thisgainfield=''
102 if (igt<ngainfld):
103 thisgainfield=gainfield[igt]
105 # spwmap is null unless specifed
106 thisspwmap=[-1]
107 if (igt<nspwmap):
108 thisspwmap=spwmap[igt];
110 # interp is 'linear' unless specified
111 thisinterp='linear'
112 if (igt<ninterp):
113 if (interp[igt]==''):
114 interp[igt]=thisinterp;
115 thisinterp=interp[igt];
117 mycb.setapply(t=0.0,table=gaintable[igt],field=thisgainfield,
118 calwt=True,spwmap=thisspwmap,interp=thisinterp)
120 # ...and now the specialized terms
121 # (BTW, interp irrelevant for these, since they are evaluated)
123 # Apply parallactic angle, if requested
124 if parang: mycb.setapply(type='P')
126 # Set up for solving:
127 phaseonly=False
128 if (gaintype=='G'):
129 mycb.setsolve(type='G',t=solint,combine=combine,preavg=preavg,
130 refant=refant,refantmode=refantmode,
131 minblperant=minblperant,
132 solnorm=solnorm,normtype=normtype,
133 minsnr=minsnr,table=caltable,
134 apmode=calmode,solmode=solmode,rmsthresh=rmsthresh,
135 phaseonly=phaseonly,append=append)
136 elif (gaintype=='T'):
137 mycb.setsolve(type='T',t=solint,combine=combine,preavg=preavg,
138 refant=refant,refantmode=refantmode,
139 minblperant=minblperant,
140 solnorm=solnorm,normtype=normtype,
141 minsnr=minsnr,table=caltable,
142 apmode=calmode,solmode=solmode,rmsthresh=rmsthresh,phaseonly=phaseonly,append=append)
143 elif (gaintype=='K' or gaintype=='KCROSS' or gaintype=='XY+QU' or gaintype=='XYf+QU'):
144 mycb.setsolve(type=gaintype,t=solint,combine=combine,preavg=preavg,refant=refant,
145 minblperant=minblperant,
146 minsnr=minsnr,table=caltable,
147 apmode=calmode,phaseonly=phaseonly,append=append)
148 elif (gaintype=='GSPLINE'):
149 mycb.setsolvegainspline(table=caltable,append=append,mode=calmode,
150 refant=refant,splinetime=splinetime,preavg=preavg,
151 npointaver=npointaver,phasewrap=phasewrap)
152 mycb.solve()
154 reportsolvestats(mycb.activityrec());
156 results_dict = mycb.returndict()
157 # Temp special handling of gaintype K
158 if (gaintype=='K'):
159 results_dict = resultsDictGaintypeExcept(results_dict)
161 finally:
162 mycb.close()
164 return results_dict
166def resultsDictGaintypeExcept(results_dict):
167 # For each level of the dict set all to data_unflagged counts
168 for i in results_dict["solvestats"]:
169 results_dict["solvestats"]["above_minsnr"] = results_dict["solvestats"]["data_unflagged"]
170 results_dict["solvestats"]["above_minblperant"] = results_dict["solvestats"]["data_unflagged"]
172 if i.startswith("spw"):
173 for j in results_dict["solvestats"][i]:
174 results_dict["solvestats"][i]["above_minsnr"] = results_dict["solvestats"][i]["data_unflagged"]
175 results_dict["solvestats"][i]["above_minblperant"] = results_dict["solvestats"][i]["data_unflagged"]
177 if j.startswith("ant"):
178 results_dict["solvestats"][i][j]["above_minsnr"] = results_dict["solvestats"][i][j]["data_unflagged"]
179 results_dict["solvestats"][i][j]["above_minblperant"] = results_dict["solvestats"][i][j]["data_unflagged"]
180 return results_dict
182def reportsolvestats(rec):
183 if (list(rec.keys()).count('origin')==1 and
184 rec['origin']=='Calibrater::genericGatherAndSolve'):
185 casalog.post("Calibration solve statistics per spw: (expected/attempted/succeeded):")
186 nexp=rec['nExpected']
187 natt=rec['nAttempt']
188 nsuc=rec['nSucceed']
189 for ispw in range(len(nexp)):
190 solstatstr=" Spw "+str(ispw)+": "
191 solstatstr+=str(nexp[ispw])+"/"
192 solstatstr+=str(natt[ispw])+"/"
193 solstatstr+=str(nsuc[ispw])
194 casalog.post(solstatstr)