casa
$Rev:20696$
|
00001 # 00002 # This file was generated using xslt from its XML file 00003 # 00004 # Copyright 2008, Associated Universities Inc., Washington DC 00005 # 00006 import sys 00007 import os 00008 #from casac import * 00009 import casac 00010 import string 00011 import time 00012 import inspect 00013 import gc 00014 import numpy 00015 from odict import odict 00016 from taskmanager import tm 00017 from task_blcal import blcal 00018 class blcal_cli_: 00019 __name__ = "blcal" 00020 __async__ = {} 00021 rkey = None 00022 i_am_a_casapy_task = None 00023 # The existence of the i_am_a_casapy_task attribute allows help() 00024 # (and other) to treat casapy tasks as a special case. 00025 00026 def __init__(self) : 00027 self.__bases__ = (blcal_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'vis':None, 'caltable':None, 'field':None, 'spw':None, 'intent':None, 'selectdata':None, 'timerange':None, 'uvrange':None, 'antenna':None, 'scan':None, 'observation':None, 'msselect':None, 'solint':None, 'combine':None, 'freqdep':None, 'calmode':None, 'solnorm':None, 'gaintable':None, 'gainfield':None, 'interp':None, 'spwmap':None, 'gaincurve':None, 'opacity':None, 'parang':None, 'async':None} 00031 00032 00033 def result(self, key=None): 00034 #### here we will scan the task-ids in __async__ 00035 #### and add any that have completed... 00036 if key is not None and self.__async__.has_key(key) and self.__async__[key] is not None: 00037 ret = tm.retrieve(self.__async__[key]) 00038 if ret['state'] == "done" : 00039 self.__async__[key] = None 00040 elif ret['state'] == 'crashed' : 00041 self.__async__[key] = None 00042 return ret 00043 return None 00044 00045 00046 def __call__(self, vis=None, caltable=None, field=None, spw=None, intent=None, selectdata=None, timerange=None, uvrange=None, antenna=None, scan=None, observation=None, msselect=None, solint=None, combine=None, freqdep=None, calmode=None, solnorm=None, gaintable=None, gainfield=None, interp=None, spwmap=None, gaincurve=None, opacity=None, parang=None, async=None): 00047 00048 """Calculate a baseline-based calibration solution (gain or bandpass) 00049 00050 This task determines a baseline by baseline gain (time) or bandpass (freq) 00051 for all baseline pairs in the data set. For the usual antenna-based calibration 00052 of interferometric data, this task gaincal is recommended, even with only one 00053 to three baselines. For arrays with closure errors, use blcal 00054 00055 Keyword arguments: 00056 vis -- Name of input visibility file 00057 default: none; example: vis='ngc5921.ms' 00058 caltable -- Name of output Gain calibration table 00059 default: none; example: caltable='ngc5921.gcal' 00060 00061 --- Data Selection (see help par.selectdata for more detailed information) 00062 00063 field -- Select field using field id(s) or field name(s). 00064 [run listobs to obtain the list id's or names] 00065 default: ''=all fields 00066 If field string is a non-negative integer, it is assumed a field index 00067 otherwise, it is assumed a field name 00068 field='0~2'; field ids 0,1,2 00069 field='0,4,5~7'; field ids 0,4,5,6,7 00070 field='3C286,3C295'; field named 3C286 adn 3C295 00071 field = '3,4C*'; field id 3, all names starting with 4C 00072 spw -- Select spectral window/channels 00073 default: ''=all spectral windows and channels 00074 spw='0~2,4'; spectral windows 0,1,2,4 (all channels) 00075 spw='<2'; spectral windows less than 2 (i.e. 0,1) 00076 spw='0:5~61'; spw 0, channels 5 to 61 00077 spw='0,10,3:3~45'; spw 0,10 all channels, spw 3, channels 3 to 45. 00078 spw='0~2:2~6'; spw 0,1,2 with channels 2 through 6 in each. 00079 spw='0:0~10;15~60'; spectral window 0 with channels 0-10,15-60 00080 spw='0:0~10,1:20~30,2:1;2;3'; spw 0, channels 0-10, 00081 spw 1, channels 20-30, and spw 2, channels, 1,2 and 3 00082 intent -- Select observing intent 00083 default: '' (no selection by intent) 00084 intent='*BANDPASS*' (selects data labelled with 00085 BANDPASS intent) 00086 selectdata -- Other data selection parameters 00087 default: True 00088 timerange -- Select data based on time range: 00089 default = '' (all); examples, 00090 timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss' 00091 Note: if YYYY/MM/DD is missing dat defaults to first day in data set 00092 timerange='09:14:0~09:54:0' picks 40 min on first day 00093 timerange= '25:00:00~27:30:00' picks 1 hr to 3 hr 30min on next day 00094 timerange='09:44:00' data within one integration of time 00095 timerange='>10:24:00' data after this time 00096 uvrange -- Select data within uvrange (default units kilo-lambda) 00097 default: '' (all); example: 00098 uvrange='0~1000kl'; uvrange from 0-1000 kilo-lamgda 00099 uvrange='>4kl';uvranges greater than 4 kilo lambda 00100 uvrange='0~1000km'; uvrange in kilometers 00101 antenna -- Select data based on antenna/baseline 00102 default: '' (all) 00103 If antenna string is a non-negative integer, it is assumed an antenna index 00104 otherwise, it is assumed as an antenna name 00105 antenna='5&6'; baseline between antenna index 5 and index 6. 00106 antenna='VA05&VA06'; baseline between VLA antenna 5 and 6. 00107 antenna='5&6;7&8'; baseline 5-6 and 7-8 00108 antenna='5'; all baselines with antenna 5 00109 antenna='5,6,10'; all baselines with antennas 5 and 6 00110 scan -- Scan number range - New, under developement 00111 observation -- Observation ID(s). 00112 default: '' = all 00113 example: '0~2,4' 00114 msselect -- Optional complex data selection (ignore for now) 00115 00116 solint -- Solution interval (units optional) 00117 default: 'inf' (~infinite, up to boundaries controlled by combine); 00118 Options: 'inf' (~infinite), 'int' (per integration), any float 00119 or integer value with or without units 00120 examples: solint='1min'; solint='60s', solint=60 --> 1 minute 00121 solint='0s'; solint=0; solint='int' --> per integration 00122 solint-'-1s'; solint='inf' --> ~infinite, up to boundaries 00123 enforced by combine 00124 combine -- Data axes to combine for solving 00125 default: 'scan' --> solutions will break at field and spw boundaries, 00126 but may extend over multiple scans (per field and spw) up 00127 to solint. 00128 Options: '','scan','spw',field', or any comma-separated combination 00129 example: combine='scan,spw' --> extend solutions over scan boundaries 00130 (up to the solint), and combine spws for solving 00131 freqdep -- Solve for frequency dependent solutions 00132 default: False (gain; True=bandpass); example: freqdep=True 00133 calmode -- Type of solution 00134 default: 'ap' (amp and phase); example: calmode='p' 00135 Options: 'p','a','ap' 00136 solnorm -- Normalize solutions. For freqdep=F, this is a global (per-spw) 00137 normalization of amplitudes (only). For freqdep=T, each baseline 00138 solution spectrum is separately normalized by its (complex) mean. 00139 default: False (no normalization) 00140 00141 gaintable -- Gain calibration table(s) to apply 00142 default: '' (none); 00143 examples: gaintable='ngc5921.gcal' 00144 gaintable=['ngc5921.ampcal','ngc5921.phcal'] 00145 gainfield -- Select a subset of calibrators from gaintable(s) 00146 default:'' ==> all sources in table; 00147 'nearest' ==> nearest (on sky) available field in table 00148 otherwise, same syntax as field 00149 example: gainfield='0~3' 00150 gainfield=['0~3','4~6'] 00151 interp -- Interpolation type (in time[,freq]) to use for each gaintable. 00152 When frequency interpolation is relevant (B, Df, Xf), 00153 separate time-dependent and freq-dependent interp 00154 types with a comma (freq _after_ the comma). 00155 Specifications for frequency are ignored when the 00156 calibration table has no channel-dependence. 00157 Time-dependent interp options ending in 'PD' enable a 00158 "phase delay" correction per spw for non-channel-dependent 00159 calibration types. 00160 default: '' --> 'linear,linear' for all gaintable(s) 00161 example: interp='nearest' (in time, freq-dep will be 00162 linear, if relevant) 00163 interp='linear,cubic' (linear in time, cubic 00164 in freq) 00165 interp=',spline' (spline in freq; linear in 00166 time by default) 00167 interp=['nearest,spline','linear'] (for multiple gaintables) 00168 Options: Time: 'nearest', 'linear' 00169 Freq: 'nearest', 'linear', 'cubic', 'spline' 00170 spwmap -- Spectral windows combinations to form for gaintable(s) 00171 default: [] (apply solutions from each spw to that spw only) 00172 Example: spwmap=[0,0,1,1] means apply the caltable solutions 00173 from spw = 0 to the spw 0,1 and spw 1 to spw 2,3. 00174 spwmap=[[0,0,1,1],[0,1,0,1]] 00175 gaincurve -- Apply internal VLA antenna gain curve correction (True/False) 00176 default: False; 00177 Use gaincurve=True ONLY for VLA data 00178 opacity -- Opacity correction to apply (nepers), per spw 00179 default: [] (no opacity correction for any spw) 00180 examples: 00181 A global value for all spws: 00182 opacity=0.051 00183 Different values for spws 0,1,2: 00184 opacity=[0.051, 0.055, 0.057] 00185 (if more than 3 spws, spw 3 and higher will 00186 be assigned the last specified value, or 0.057) 00187 Typical VLA values are: 5 GHz - 0.013, 8 GHz - 0.013 00188 15 GHz - 0.016, 23 GHz - 0.051, 43 GHz - 0.07 00189 parang -- If True, apply the parallactic angle correction (required 00190 for polarization calibration) 00191 default: False 00192 00193 00194 """ 00195 if not hasattr(self, "__globals__") or self.__globals__ == None : 00196 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00197 #casac = self.__globals__['casac'] 00198 casalog = self.__globals__['casalog'] 00199 #casalog = casac.casac.logsink() 00200 self.__globals__['__last_task'] = 'blcal' 00201 self.__globals__['taskname'] = 'blcal' 00202 ### 00203 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00204 ### 00205 ### 00206 #Handle globals or user over-ride of arguments 00207 # 00208 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00209 useLocalDefaults = False 00210 00211 for item in function_signature_defaults.iteritems(): 00212 key,val = item 00213 keyVal = eval(key) 00214 if (keyVal == None): 00215 #user hasn't set it - use global/default 00216 pass 00217 else: 00218 #user has set it - use over-ride 00219 if (key != 'self') : 00220 useLocalDefaults = True 00221 00222 myparams = {} 00223 if useLocalDefaults : 00224 for item in function_signature_defaults.iteritems(): 00225 key,val = item 00226 keyVal = eval(key) 00227 exec('myparams[key] = keyVal') 00228 self.parameters[key] = keyVal 00229 if (keyVal == None): 00230 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00231 keyVal = eval(key) 00232 if(type(keyVal) == dict) : 00233 if len(keyVal) > 0 : 00234 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00235 else : 00236 exec('myparams[key] = ' + key + ' = {}') 00237 00238 else : 00239 async = self.parameters['async'] 00240 myparams['vis'] = vis = self.parameters['vis'] 00241 myparams['caltable'] = caltable = self.parameters['caltable'] 00242 myparams['field'] = field = self.parameters['field'] 00243 myparams['spw'] = spw = self.parameters['spw'] 00244 myparams['intent'] = intent = self.parameters['intent'] 00245 myparams['selectdata'] = selectdata = self.parameters['selectdata'] 00246 myparams['timerange'] = timerange = self.parameters['timerange'] 00247 myparams['uvrange'] = uvrange = self.parameters['uvrange'] 00248 myparams['antenna'] = antenna = self.parameters['antenna'] 00249 myparams['scan'] = scan = self.parameters['scan'] 00250 myparams['observation'] = observation = self.parameters['observation'] 00251 myparams['msselect'] = msselect = self.parameters['msselect'] 00252 myparams['solint'] = solint = self.parameters['solint'] 00253 myparams['combine'] = combine = self.parameters['combine'] 00254 myparams['freqdep'] = freqdep = self.parameters['freqdep'] 00255 myparams['calmode'] = calmode = self.parameters['calmode'] 00256 myparams['solnorm'] = solnorm = self.parameters['solnorm'] 00257 myparams['gaintable'] = gaintable = self.parameters['gaintable'] 00258 myparams['gainfield'] = gainfield = self.parameters['gainfield'] 00259 myparams['interp'] = interp = self.parameters['interp'] 00260 myparams['spwmap'] = spwmap = self.parameters['spwmap'] 00261 myparams['gaincurve'] = gaincurve = self.parameters['gaincurve'] 00262 myparams['opacity'] = opacity = self.parameters['opacity'] 00263 myparams['parang'] = parang = self.parameters['parang'] 00264 00265 if type(gaintable)==str: gaintable=[gaintable] 00266 if type(gainfield)==str: gainfield=[gainfield] 00267 if type(interp)==str: interp=[interp] 00268 if type(spwmap)==int: spwmap=[spwmap] 00269 if type(opacity)==float: opacity=[opacity] 00270 00271 result = None 00272 00273 # 00274 # The following is work around to avoid a bug with current python translation 00275 # 00276 mytmp = {} 00277 00278 mytmp['vis'] = vis 00279 mytmp['caltable'] = caltable 00280 mytmp['field'] = field 00281 mytmp['spw'] = spw 00282 mytmp['intent'] = intent 00283 mytmp['selectdata'] = selectdata 00284 mytmp['timerange'] = timerange 00285 mytmp['uvrange'] = uvrange 00286 mytmp['antenna'] = antenna 00287 mytmp['scan'] = scan 00288 mytmp['observation'] = observation 00289 mytmp['msselect'] = msselect 00290 mytmp['solint'] = solint 00291 mytmp['combine'] = combine 00292 mytmp['freqdep'] = freqdep 00293 mytmp['calmode'] = calmode 00294 mytmp['solnorm'] = solnorm 00295 mytmp['gaintable'] = gaintable 00296 mytmp['gainfield'] = gainfield 00297 mytmp['interp'] = interp 00298 mytmp['spwmap'] = spwmap 00299 mytmp['gaincurve'] = gaincurve 00300 mytmp['opacity'] = opacity 00301 mytmp['parang'] = parang 00302 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00303 trec = casac.casac.utils().torecord(pathname+'blcal.xml') 00304 00305 casalog.origin('blcal') 00306 try : 00307 #if not trec.has_key('blcal') or not casac.casac.utils().verify(mytmp, trec['blcal']) : 00308 #return False 00309 00310 casac.casac.utils().verify(mytmp, trec['blcal'], True) 00311 scriptstr=[''] 00312 saveinputs = self.__globals__['saveinputs'] 00313 saveinputs('blcal', 'blcal.last', myparams, self.__globals__,scriptstr=scriptstr) 00314 if async : 00315 count = 0 00316 keybase = time.strftime("%y%m%d.%H%M%S") 00317 key = keybase + "_" + str(count) 00318 while self.__async__.has_key(key) : 00319 count += 1 00320 key = keybase + "_" + str(count) 00321 result = tm.execute('blcal', vis, caltable, field, spw, intent, selectdata, timerange, uvrange, antenna, scan, observation, msselect, solint, combine, freqdep, calmode, solnorm, gaintable, gainfield, interp, spwmap, gaincurve, opacity, parang) 00322 print "Use: " 00323 print " tm.retrieve(return_value) # to retrieve the status" 00324 print 00325 self.rkey = key 00326 self.__async__[key] = result 00327 else : 00328 tname = 'blcal' 00329 spaces = ' '*(18-len(tname)) 00330 casalog.post('\n##########################################'+ 00331 '\n##### Begin Task: ' + tname + spaces + ' #####') 00332 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00333 result = blcal(vis, caltable, field, spw, intent, selectdata, timerange, uvrange, antenna, scan, observation, msselect, solint, combine, freqdep, calmode, solnorm, gaintable, gainfield, interp, spwmap, gaincurve, opacity, parang) 00334 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00335 '\n##########################################') 00336 00337 except Exception, instance: 00338 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00339 raise 00340 else : 00341 #print '**** Error **** ',instance 00342 tname = 'blcal' 00343 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00344 pass 00345 00346 gc.collect() 00347 return result 00348 # 00349 # 00350 # 00351 def paramgui(self, useGlobals=True, ipython_globals=None): 00352 """ 00353 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00354 """ 00355 import paramgui 00356 if not hasattr(self, "__globals__") or self.__globals__ == None : 00357 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00358 00359 if useGlobals: 00360 if ipython_globals == None: 00361 myf=self.__globals__ 00362 else: 00363 myf=ipython_globals 00364 00365 paramgui.setGlobals(myf) 00366 else: 00367 paramgui.setGlobals({}) 00368 00369 paramgui.runTask('blcal', myf['_ip']) 00370 paramgui.setGlobals({}) 00371 00372 # 00373 # 00374 # 00375 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00376 if not hasattr(self, "__globals__") or self.__globals__ == None : 00377 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00378 if ipython_globals == None: 00379 myf=self.__globals__ 00380 else: 00381 myf=ipython_globals 00382 00383 a = odict() 00384 a['vis'] = '' 00385 a['caltable'] = '' 00386 a['field'] = '' 00387 a['spw'] = '' 00388 a['intent'] = '' 00389 a['selectdata'] = True 00390 a['solint'] = 'inf' 00391 a['combine'] = 'scan' 00392 a['freqdep'] = False 00393 a['calmode'] = 'ap' 00394 a['solnorm'] = False 00395 a['gaintable'] = [''] 00396 a['gainfield'] = [''] 00397 a['interp'] = [''] 00398 a['spwmap'] = [] 00399 a['gaincurve'] = False 00400 a['opacity'] = [] 00401 a['parang'] = False 00402 00403 a['async']=False 00404 a['selectdata'] = { 00405 0:odict([{'value':True}, {'timerange':''}, {'uvrange':''}, {'antenna':''}, {'scan':''}, {'observation':''}, {'msselect':''}]), 00406 1:{'value':False}} 00407 00408 ### This function sets the default values but also will return the list of 00409 ### parameters or the default value of a given parameter 00410 if(param == None): 00411 myf['__set_default_parameters'](a) 00412 elif(param == 'paramkeys'): 00413 return a.keys() 00414 else: 00415 if(paramvalue==None and subparam==None): 00416 if(a.has_key(param)): 00417 return a[param] 00418 else: 00419 return self.itsdefault(param) 00420 else: 00421 retval=a[param] 00422 if(type(a[param])==dict): 00423 for k in range(len(a[param])): 00424 valornotval='value' 00425 if(a[param][k].has_key('notvalue')): 00426 valornotval='notvalue' 00427 if((a[param][k][valornotval])==paramvalue): 00428 retval=a[param][k].copy() 00429 retval.pop(valornotval) 00430 if(subparam != None): 00431 if(retval.has_key(subparam)): 00432 retval=retval[subparam] 00433 else: 00434 retval=self.itsdefault(subparam) 00435 else: 00436 retval=self.itsdefault(subparam) 00437 return retval 00438 00439 00440 # 00441 # 00442 def check_params(self, param=None, value=None, ipython_globals=None): 00443 if ipython_globals == None: 00444 myf=self.__globals__ 00445 else: 00446 myf=ipython_globals 00447 # print 'param:', param, 'value:', value 00448 try : 00449 if str(type(value)) != "<type 'instance'>" : 00450 value0 = value 00451 value = myf['cu'].expandparam(param, value) 00452 matchtype = False 00453 if(type(value) == numpy.ndarray): 00454 if(type(value) == type(value0)): 00455 myf[param] = value.tolist() 00456 else: 00457 #print 'value:', value, 'value0:', value0 00458 #print 'type(value):', type(value), 'type(value0):', type(value0) 00459 myf[param] = value0 00460 if type(value0) != list : 00461 matchtype = True 00462 else : 00463 myf[param] = value 00464 value = myf['cu'].verifyparam({param:value}) 00465 if matchtype: 00466 value = False 00467 except Exception, instance: 00468 #ignore the exception and just return it unchecked 00469 myf[param] = value 00470 return value 00471 # 00472 # 00473 def description(self, key='blcal', subkey=None): 00474 desc={'blcal': 'Calculate a baseline-based calibration solution (gain or bandpass)', 00475 'vis': 'Name of input visibility file', 00476 'caltable': 'Name of output gain calibration table', 00477 'field': 'Select field using field id(s) or field name(s)', 00478 'spw': 'Select spectral window/channels', 00479 'intent': 'Select observing intent', 00480 'selectdata': 'Other data selection parameters', 00481 'timerange': 'Select data based on time range', 00482 'uvrange': 'Select data within uvrange (default units meters)', 00483 'antenna': 'Select data based on antenna/baseline', 00484 'scan': 'Scan number range', 00485 'observation': 'Select by observation ID(s)', 00486 'msselect': 'Optional complex data selection (ignore for now)', 00487 'solint': 'Solution interval', 00488 'combine': 'Data axes which to combine for solve (scan, spw, and/or field)', 00489 'freqdep': 'Solve for frequency dependent solutions', 00490 'calmode': 'Type of solution" (\'ap\', \'p\', \'a\')', 00491 'solnorm': 'Normalize average solution amplitudes to 1.0', 00492 'gaintable': 'Gain calibration table(s) to apply on the fly', 00493 'gainfield': 'Select a subset of calibrators from gaintable(s)', 00494 'interp': 'Interpolation mode (in time) to use for each gaintable', 00495 'spwmap': 'Spectral windows combinations to form for gaintables(s)', 00496 'gaincurve': 'Apply internal VLA antenna gain curve correction', 00497 'opacity': 'Opacity correction to apply (nepers), per spw', 00498 'parang': 'Apply parallactic angle correction', 00499 00500 'async': 'If true the taskname must be started using blcal(...)' 00501 } 00502 00503 # 00504 # Set subfields defaults if needed 00505 # 00506 00507 if(desc.has_key(key)) : 00508 return desc[key] 00509 00510 def itsdefault(self, paramname) : 00511 a = {} 00512 a['vis'] = '' 00513 a['caltable'] = '' 00514 a['field'] = '' 00515 a['spw'] = '' 00516 a['intent'] = '' 00517 a['selectdata'] = True 00518 a['timerange'] = '' 00519 a['uvrange'] = '' 00520 a['antenna'] = '' 00521 a['scan'] = '' 00522 a['observation'] = '' 00523 a['msselect'] = '' 00524 a['solint'] = 'inf' 00525 a['combine'] = 'scan' 00526 a['freqdep'] = False 00527 a['calmode'] = 'ap' 00528 a['solnorm'] = False 00529 a['gaintable'] = [''] 00530 a['gainfield'] = [''] 00531 a['interp'] = [''] 00532 a['spwmap'] = [] 00533 a['gaincurve'] = False 00534 a['opacity'] = [] 00535 a['parang'] = False 00536 00537 #a = sys._getframe(len(inspect.stack())-1).f_globals 00538 00539 if self.parameters['selectdata'] == True: 00540 a['timerange'] = '' 00541 a['uvrange'] = '' 00542 a['antenna'] = '' 00543 a['scan'] = '' 00544 a['observation'] = '' 00545 a['msselect'] = '' 00546 00547 if a.has_key(paramname) : 00548 return a[paramname] 00549 blcal_cli = blcal_cli_()