Coverage for /wheeldirectory/casa-6.7.0-12-py3.10.el8/lib/py/lib/python3.10/site-packages/casatasks/private/task_applycal.py: 91%
112 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 time
3import numpy as np
5from .. import casalog
6from .callibrary import *
7from . import flaghelper as fh
8from .parallel.parallel_data_helper import ParallelDataHelper
9from .parallel.parallel_task_helper import ParallelTaskHelper
10from .mstools import write_history
11from casatools import ms, calibrater
13def applycal(
14 vis=None,
15 field=None,
16 spw=None,
17 intent=None,
18 selectdata=None,
19 timerange=None,
20 uvrange=None,
21 antenna=None,
22 scan=None,
23 observation=None,
24 msselect=None,
25 docallib=None,
26 callib=None,
27 gaintable=None,
28 gainfield=None,
29 interp=None,
30 spwmap=None,
31 calwt=None,
32 parang=None,
33 applymode=None,
34 flagbackup=None,
35 ):
37 # Python script
38 casalog.origin('applycal')
40 # Take care of the trivial parallelization
41 if ParallelDataHelper.isMMSAndNotServer(vis):
43 # Back up the flags, if requested (and if necessary)
44 if flagbackup and applymode != 'calonly' and applymode != 'trial':
45 fh.backupFlags(aflocal=None, msfile=vis, prename='applycal')
46 flagbackup = False
48 # To be safe convert file names to absolute paths.
49 gaintable = ParallelTaskHelper.findAbsPath(gaintable)
50 helper = ParallelTaskHelper('applycal', locals())
51 ret = helper.go()
52 if ParallelTaskHelper.getAsyncMode():
53 return ret
54 else:
55 return
57 try:
58 mycb = calibrater( )
59 makecorr = True
60 if (type(vis) == str) & os.path.exists(vis):
61 # if applymode is flagonly don't add CORRECTED_DATA column
62 # otherwise add CORRECTED_DATA column
63 if applymode in ['flagonly', 'flagonlystrict', 'trial']:
64 makecorr = False
65 mycb.open(filename=vis, compress=False, addcorr=makecorr,
66 addmodel=False)
67 else:
68 raise ValueError( 'Visibility data set not found - please verify the name' )
70 # enforce default if unspecified
71 if applymode == '':
72 applymode = 'calflag'
74 # Back up the flags, if requested (and if necessary)
75 if flagbackup and applymode != 'calonly' and applymode \
76 != 'trial':
77 fh.backupFlags(aflocal=None, msfile=vis, prename='applycal')
79 # Do data selection according to selectdata
80 if selectdata:
81 # pass all data selection parameters in as specified
82 mycb.selectvis(
83 time=timerange,
84 spw=spw,
85 scan=scan,
86 field=field,
87 intent=intent,
88 observation=str(observation),
89 baseline=antenna,
90 uvrange=uvrange,
91 chanmode='none',
92 msselect=msselect,
93 )
94 else:
95 # selectdata=F, so time,scan,baseline,uvrange,msselect=''
96 # using spw and field specifications only
97 mycb.selectvis(
98 time='',
99 spw=spw,
100 scan='',
101 field=field,
102 intent=intent,
103 observation='',
104 baseline='',
105 uvrange='',
106 chanmode='none',
107 msselect='',
108 )
110 # Arrange applies....
112 if docallib:
113 # by cal library from file
114 # parsing using c++ parser
115 thiscallib=mycb.parsecallibfile(callib)
116 mycb.setcallib(thiscallib)
118 else:
120 # by traditional parameters
122 ngaintab = 0
123 if gaintable != ['']:
124 ngaintab = len(gaintable)
126 ncalwt = len(calwt)
127 if ncalwt == 1:
128 calwt = [calwt[0] for i in range(ngaintab)]
130 ngainfld = len(gainfield)
131 nspwmap = len(spwmap)
132 ninterp = len(interp)
134 # handle list of list issues with spwmap
135 if nspwmap > 0:
136 if type(spwmap[0]) != list:
137 # first element not a list, only one spwmap specified
138 # make it a list of list
139 spwmap = [spwmap]
140 nspwmap = 1
142 for igt in range(ngaintab):
143 if gaintable[igt] != '':
145 # field selection is null unless specified
146 thisgainfield = ''
147 if igt < ngainfld:
148 thisgainfield = gainfield[igt]
150 # spwmap is null unless specifed
151 thisspwmap = [-1]
152 if igt < nspwmap:
153 thisspwmap = spwmap[igt]
155 # interp is 'linear' unless specified
156 thisinterp = 'linear'
157 if igt < ninterp:
158 if interp[igt] == '':
159 interp[igt] = thisinterp
160 thisinterp = interp[igt]
162 mycb.setapply(
163 t=0.0,
164 table=gaintable[igt],
165 field=thisgainfield,
166 calwt=calwt[igt],
167 spwmap=thisspwmap,
168 interp=thisinterp,
169 )
171 # ...and now the specialized terms
173 # Apply parallactic angle, if requested
174 if parang:
175 mycb.setapply(type='P')
177 mycb.correct(applymode)
179 # report what the flags did
180 reportflags(mycb.activityrec())
183 # write history
184 try:
185 param_names = \
186 applycal.__code__.co_varnames[:applycal.__code__.co_argcount]
187 local_vars = locals( )
188 param_vals = [local_vars[p] for p in param_names]
190 write_history(
191 ms(),
192 vis,
193 'applycal',
194 param_names,
195 param_vals,
196 casalog,
197 )
198 except Exception as instance:
199 casalog.post("*** Error \'%s\' updating HISTORY"
200 % instance, 'WARN')
201 finally:
202 mycb.close()
204def reportflags(rec):
205 try:
206 if 'origin' in rec and rec['origin'] \
207 == 'Calibrater::correct' and 'VisEquation' in rec:
208 casalog.post('Calibration apply flagging statistics (among calibrateable spws):'
209 )
210 VE = rec['VisEquation']
211 nterm = len(VE)
212 if nterm > 0:
213 nVisTotal=VE['nVisTotal']
215 casalog.post(' Total visibilities selected for correction (ncorr x nchan x nrow summed over spws) = '
216 + str(nVisTotal))
217 casalog.post(' Flags:')
218 partlog=False
219 for iterm in range(nterm-1): # one of the keys is nVisTotal; the rest are caltable indices
220 VEi = VE['*' + str(iterm + 1)]
222 nVisThis=VEi['ndata']
223 partial=' '
224 if nVisThis<nVisTotal:
225 partlog=True
226 partial='**'
227 flstr = ' ' + VEi['type']
228 flstr += ': '
229 flstr += 'In: ' + str(VEi['nflagIn'])
230 flstr += ' / '+str(nVisThis)+partial
231 flstr += ' (' + str(100. * VEi['nflagIn']/nVisThis) + '%) --> '
232 flstr += 'Out: ' + str(VEi['nflagOut'])
233 flstr += ' / '+str(nVisThis)+partial
234 flstr += ' (' + str(100. * VEi['nflagOut']/nVisThis) + '%)'
236 if 'table' in VEi:
237 flstr += ' (' + VEi['table'] + ')'
238 casalog.post(flstr)
239 if partlog:
240 casalog.post(' ** = Denotes caltable that only corrected a subset of total selected visibilities')
242 except Exception as instance:
243 # complain mildly, but don't alarm
244 casalog.post('Error formatting some or all of the applycal flagging log info: '
245 + str(instance), 'SEVERE')