casa
$Rev:20696$
|
00001 import os 00002 import time 00003 import numpy as np 00004 from taskinit import * 00005 from parallel.parallel_task_helper import ParallelTaskHelper 00006 00007 def applycal(vis=None, 00008 field=None, 00009 spw=None, 00010 intent=None, 00011 selectdata=None, 00012 timerange=None, 00013 uvrange=None, 00014 antenna=None, 00015 scan=None, 00016 observation=None, 00017 msselect=None, 00018 gaintable=None, 00019 gainfield=None, 00020 interp=None, 00021 spwmap=None, 00022 gaincurve=None, 00023 opacity=None, 00024 parang=None, 00025 calwt=None, 00026 applymode=None, 00027 flagbackup=None): 00028 00029 #Python script 00030 casalog.origin('applycal') 00031 00032 # Take care of the trivial parallelization 00033 if ParallelTaskHelper.isParallelMS(vis): 00034 # To be safe convert file names to absolute paths. 00035 gaintable = ParallelTaskHelper.findAbsPath(gaintable) 00036 helper = ParallelTaskHelper('applycal', locals()) 00037 helper.go() 00038 return 00039 00040 try: 00041 mycb = cbtool() 00042 if ((type(vis)==str) & (os.path.exists(vis))): 00043 # add CORRECTED_DATA column 00044 mycb.open(filename=vis,compress=False, 00045 addcorr=True,addmodel=False) 00046 else: 00047 raise Exception, 'Visibility data set not found - please verify the name' 00048 00049 # enforce default if unspecified 00050 if applymode=='': 00051 applymode='calflag' 00052 00053 # Back up the flags, if requested (and if necessary) 00054 if (flagbackup and 00055 (applymode!='calonly' and 00056 applymode!='trial') ): 00057 aflocal = casac.agentflagger() 00058 aflocal.open(vis) 00059 backup_flags(aflocal) 00060 aflocal.done() 00061 00062 # Do data selection according to selectdata 00063 if (selectdata): 00064 # pass all data selection parameters in as specified 00065 mycb.selectvis(time=timerange,spw=spw,scan=scan,field=field, 00066 intent=intent, observation=str(observation), 00067 baseline=antenna,uvrange=uvrange,chanmode='none', 00068 msselect=msselect); 00069 else: 00070 # selectdata=F, so time,scan,baseline,uvrange,msselect='' 00071 # using spw and field specifications only 00072 mycb.selectvis(time='',spw=spw,scan='',field=field,intent=intent, 00073 observation='', baseline='',uvrange='', 00074 chanmode='none', msselect='') 00075 00076 # Arrange all of the applies 00077 # NB: calwt and interp apply to all; spwmap to gain only 00078 # TBD: interp and spwmap probably should be hardwired here? 00079 # TBD: trap case of ngainfield>ngaintab, etc. 00080 # First other tables... 00081 00082 ngaintab = 0; 00083 if (gaintable!=['']): 00084 ngaintab=len(gaintable) 00085 00086 ngainfld = len(gainfield) 00087 nspwmap = len(spwmap) 00088 ninterp = len(interp) 00089 00090 # handle list of list issues with spwmap 00091 if (nspwmap>0): 00092 if (type(spwmap[0])!=list): 00093 # first element not a list, only one spwmap specified 00094 # make it a list of list 00095 spwmap=[spwmap]; 00096 nspwmap=1; 00097 00098 for igt in range(ngaintab): 00099 if (gaintable[igt]!=''): 00100 00101 # field selection is null unless specified 00102 thisgainfield='' 00103 if (igt<ngainfld): 00104 thisgainfield=gainfield[igt] 00105 00106 # spwmap is null unless specifed 00107 thisspwmap=[-1] 00108 if (igt<nspwmap): 00109 thisspwmap=spwmap[igt]; 00110 00111 # interp is 'linear' unless specified 00112 thisinterp='linear' 00113 if (igt<ninterp): 00114 if (interp[igt]==''): 00115 interp[igt]=thisinterp; 00116 thisinterp=interp[igt]; 00117 00118 mycb.setapply(t=0.0,table=gaintable[igt],field=thisgainfield, 00119 calwt=calwt,spwmap=thisspwmap,interp=thisinterp) 00120 00121 # ...and now the specialized terms 00122 # (BTW, interp irrelevant for these, since they are evaluated) 00123 00124 # opacity (if non-trivially specified and any >0.0) 00125 opacarr=np.array(opacity) # as numpy array for uniformity 00126 if (np.sum(opacarr)>0.0): 00127 # opacity transmitted as a list in all cases 00128 mycb.setapply(type='TOPAC',t=-1,opacity=opacarr.tolist(),calwt=calwt) 00129 00130 if gaincurve: mycb.setapply(type='GAINCURVE',t=-1,calwt=calwt) 00131 00132 # Apply parallactic angle, if requested 00133 if parang: mycb.setapply(type='P') 00134 00135 mycb.correct(applymode) 00136 00137 # report what the flags did 00138 reportflags(mycb.activityrec()) 00139 00140 mycb.close() 00141 00142 #write history 00143 try: 00144 param_names = applycal.func_code.co_varnames[:applycal.func_code.co_argcount] 00145 param_vals = [eval(p) for p in param_names] 00146 write_history(mstool(), vis, 'applycal', param_names, 00147 param_vals, casalog) 00148 except Exception, instance: 00149 casalog.post("*** Error \'%s\' updating HISTORY" % (instance), 00150 'WARN') 00151 00152 except Exception, instance: 00153 print '*** Error ***',instance 00154 mycb.close() 00155 raise Exception, instance 00156 00157 00158 def backup_flags(aflocal): 00159 00160 # Create names like this: 00161 # before_applycal_1, 00162 # before_applycal_2, 00163 # before_applycal_3, 00164 # etc 00165 # 00166 # Generally before_applycal_<i>, where i is the smallest 00167 # integer giving a name, which does not already exist 00168 00169 existing = aflocal.getflagversionlist(printflags=False) 00170 00171 # remove comments from strings 00172 existing = [x[0:x.find(' : ')] for x in existing] 00173 i = 1 00174 while True: 00175 versionname = "before_applycal_" + str(i) 00176 00177 if not versionname in existing: 00178 break 00179 else: 00180 i = i + 1 00181 00182 time_string = str(time.strftime('%Y-%m-%d %H:%M:%S')) 00183 00184 aflocal.saveflagversion(versionname=versionname, 00185 comment='Flag backup before applycal on ' + time_string, 00186 merge='replace') 00187 00188 00189 def reportflags(rec): 00190 try: 00191 if (rec.keys().count('origin')==1 and 00192 rec['origin']=='Calibrater::correct' and 00193 rec.keys().count('VisEquation')==1): 00194 casalog.post("Calibration apply flagging statistics:") 00195 VE=rec['VisEquation'] 00196 nterm=len(VE) 00197 if nterm>0: 00198 casalog.post(" Total selected visibilities = "+str(VE['*1']['ndata'])) 00199 casalog.post(" Flags:") 00200 for iterm in range(nterm): 00201 VEi=VE['*'+str(iterm+1)] 00202 flstr=' '+VEi['type'] 00203 flstr+=': ' 00204 flstr+='In: '+str(VEi['nflagIn']) 00205 flstr+=' ('+str(100.*VEi['nflagIn']/VEi['ndata'])+'%) --> ' 00206 flstr+='Out: '+str(VEi['nflagOut']) 00207 flstr+=' ('+str(100.*VEi['nflagOut']/VEi['ndata'])+'%)' 00208 if (VEi.has_key('table')): 00209 flstr+=' ('+VEi['table']+')' 00210 casalog.post(flstr) 00211 except Exception, instance: 00212 # complain mildly, but don't alarm 00213 casalog.post("Error formatting some or all of the applycal flagging log info: "+str(instance))