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_widebandpbcor import widebandpbcor 00018 class widebandpbcor_cli_: 00019 __name__ = "widebandpbcor" 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__ = (widebandpbcor_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'vis':None, 'imagename':None, 'nterms':None, 'threshold':None, 'action':None, 'reffreq':None, 'pbmin':None, 'field':None, 'spwlist':None, 'chanlist':None, 'weightlist':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, imagename=None, nterms=None, threshold=None, action=None, reffreq=None, pbmin=None, field=None, spwlist=None, chanlist=None, weightlist=None, async=None): 00047 00048 """Wideband PB-correction on the output of the MS-MFS algorithm 00049 00050 Wide-band Primary-beam correction 00051 00052 (1) Compute a set of Primary Beams at the specified frequencies 00053 (2) Calculate Taylor-coefficient images that represent the PB spectrum 00054 (3) Perform a polynomial division to PB-correct the output Taylor-coefficient 00055 images from the MS-MFS algorithm ( clean(nterms>1) ) 00056 (4) Recompute spectral index (and curvature) using the corrected Taylor-coefficient images. 00057 00058 [ Optionally, skip PB-correction, and only recalculate spectral index 00059 with a different threshold ] 00060 00061 This is a temporary task, meant for use until projection-based gridding algorithms 00062 are available via the 'clean' task. 00063 00064 An output directory named imagename.pbcor.workdirectory is created, and filled with 00065 an image-cube of the evaluated primary beams at all specified frequencies, 00066 Taylor-coefficients, and a 'spectral index' due to the primary beam. 00067 Note that for the actual pb-correction, only the Taylor-coefficient images are used. 00068 00069 Task parameters : 00070 00071 vis -- Name of input visibility file 00072 example : vis = 'ngc5921.ms' 00073 Only one MS can be specified here, and it must contain at-least one 00074 timestep of data at all frequencies required to calculate the PB spectrum. 00075 ( In case of multiple MSs with different spectral windows, for now, 00076 please split/concat a small fraction of the data to form such an MS ) 00077 00078 imagename -- Pre-name of input and output images. Same as in the clean task. 00079 example : imagename = 'run1' 00080 Restored-images ( run1.image.tt0,etc) and residual images ( run1.residual.tt0, etc.. ) 00081 must be available on disk. 00082 00083 nterms -- Number of Taylor terms to be used to model the frequency-dependence 00084 of the primary beam. 00085 example : nterms = 2 00086 nterms must be less than or equal to the number of frequencies specified via 00087 spwlist, chanlist and weightlist. 00088 nterms=1 will do a standard division by the average PB computed over all 00089 specified frequencies. 00090 00091 threshold -- Flux level in the restored intensity map, below which to not 00092 recalculate spectral index. 00093 example : threshold = '0.1Jy' 00094 00095 action -- Choice of PB-correction with spectral-index recalculation 00096 or only spectral-index recalculation (using the specified threshold) 00097 example : action='pbcor' or action='calcalpha' 00098 00099 With action='pbcor', the following output images are created/overwritten. 00100 00101 - imagename.pbcor.workdirectory : This directory contains an image cube with 00102 PBs at the list of specified frequencies, and Taylor-coefficient images that 00103 describe the PB spectrum. 00104 - imagename.pb.cube : Concatenated cube of PBs 00105 - imagename.pb.tt0, tt1, ... : Taylor coefficients describing the PB spectrum 00106 - imagename.pb.alpha : Spectral index of the PB (for information only) 00107 - imagename.image.pbcor.tt0,tt1,... : Corrected Taylor coefficients 00108 - imagename.pbcor.image.alpha : Corrected Spectral Index 00109 - imagename.pbcor.image.alpha.error : New error map. 00110 00111 With action='calcalpha', the following output images are created/overwritten 00112 - imagename.image.alpha : Corrected Spectral Index 00113 - imagename.image.alpha.error : New error map. 00114 00115 reffreq -- Reference frequency about which the Taylor-expansion is defined. 00116 example : reffreq = '1.5GHz' 00117 If left unspecified, it is picked from the input restored image. 00118 Note : If reffreq was specified during task clean to produce the images 00119 it must be specified here. 00120 00121 pbmin -- PB gain level below which to not compute Taylor-coefficients or 00122 apply PB-corrections. 00123 example : pbmin = 0.1 00124 00125 field -- Field selection for the Primary Beam calculation. 00126 example : field = '3C291' 00127 This field selection must be identical to that used in 'clean' 00128 00129 spwlist -- List of SPW ids for which to make separate Primary Beams 00130 chanlist -- List of channel ids, within the above SPW ids, at which to make PBs. 00131 00132 example : spwlist=[0,1,2], chanlist=[32,32,32] 00133 Make PBs at frequencies corresponding to channel 32 of 00134 spws 0,1 and 2. 00135 example : spwlist=[0,0,0], chanlist=[0,10,20] 00136 Make PBs at frequencies corresponding to channels 0,10,20 00137 of spw 0 00138 00139 Primary beams are computed at these specified frequencies and 00140 for pointings selected by 'field'. Taylor-coefficients that represent 00141 the PB spectrum are computed from these images. 00142 00143 weightlist -- List of relative weights to apply to the PBs selected via the 00144 spwlist,chanlist parameters. Weights should approximately represent the 00145 sum-of-weights applicable during imaging each of these frequencies. 00146 example : weightlist=[0.5,1.0,1.0] 00147 The first frequency had less usable data due to flagged RFI, 00148 but the other two had relatively equal weight. 00149 These weights are applied to the PB spectrum while computing 00150 PB Taylor-coefficients. Setting weights to anything other than 1.0 00151 makes a difference only with very lop-sided weights. 00152 00153 00154 """ 00155 if not hasattr(self, "__globals__") or self.__globals__ == None : 00156 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00157 #casac = self.__globals__['casac'] 00158 casalog = self.__globals__['casalog'] 00159 #casalog = casac.casac.logsink() 00160 self.__globals__['__last_task'] = 'widebandpbcor' 00161 self.__globals__['taskname'] = 'widebandpbcor' 00162 ### 00163 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00164 ### 00165 ### 00166 #Handle globals or user over-ride of arguments 00167 # 00168 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00169 useLocalDefaults = False 00170 00171 for item in function_signature_defaults.iteritems(): 00172 key,val = item 00173 keyVal = eval(key) 00174 if (keyVal == None): 00175 #user hasn't set it - use global/default 00176 pass 00177 else: 00178 #user has set it - use over-ride 00179 if (key != 'self') : 00180 useLocalDefaults = True 00181 00182 myparams = {} 00183 if useLocalDefaults : 00184 for item in function_signature_defaults.iteritems(): 00185 key,val = item 00186 keyVal = eval(key) 00187 exec('myparams[key] = keyVal') 00188 self.parameters[key] = keyVal 00189 if (keyVal == None): 00190 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00191 keyVal = eval(key) 00192 if(type(keyVal) == dict) : 00193 if len(keyVal) > 0 : 00194 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00195 else : 00196 exec('myparams[key] = ' + key + ' = {}') 00197 00198 else : 00199 async = self.parameters['async'] 00200 myparams['vis'] = vis = self.parameters['vis'] 00201 myparams['imagename'] = imagename = self.parameters['imagename'] 00202 myparams['nterms'] = nterms = self.parameters['nterms'] 00203 myparams['threshold'] = threshold = self.parameters['threshold'] 00204 myparams['action'] = action = self.parameters['action'] 00205 myparams['reffreq'] = reffreq = self.parameters['reffreq'] 00206 myparams['pbmin'] = pbmin = self.parameters['pbmin'] 00207 myparams['field'] = field = self.parameters['field'] 00208 myparams['spwlist'] = spwlist = self.parameters['spwlist'] 00209 myparams['chanlist'] = chanlist = self.parameters['chanlist'] 00210 myparams['weightlist'] = weightlist = self.parameters['weightlist'] 00211 00212 if type(spwlist)==int: spwlist=[spwlist] 00213 if type(chanlist)==int: chanlist=[chanlist] 00214 if type(weightlist)==float: weightlist=[weightlist] 00215 00216 result = None 00217 00218 # 00219 # The following is work around to avoid a bug with current python translation 00220 # 00221 mytmp = {} 00222 00223 mytmp['vis'] = vis 00224 mytmp['imagename'] = imagename 00225 mytmp['nterms'] = nterms 00226 mytmp['threshold'] = threshold 00227 mytmp['action'] = action 00228 mytmp['reffreq'] = reffreq 00229 mytmp['pbmin'] = pbmin 00230 mytmp['field'] = field 00231 mytmp['spwlist'] = spwlist 00232 mytmp['chanlist'] = chanlist 00233 mytmp['weightlist'] = weightlist 00234 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00235 trec = casac.casac.utils().torecord(pathname+'widebandpbcor.xml') 00236 00237 casalog.origin('widebandpbcor') 00238 try : 00239 #if not trec.has_key('widebandpbcor') or not casac.casac.utils().verify(mytmp, trec['widebandpbcor']) : 00240 #return False 00241 00242 casac.casac.utils().verify(mytmp, trec['widebandpbcor'], True) 00243 scriptstr=[''] 00244 saveinputs = self.__globals__['saveinputs'] 00245 saveinputs('widebandpbcor', 'widebandpbcor.last', myparams, self.__globals__,scriptstr=scriptstr) 00246 if async : 00247 count = 0 00248 keybase = time.strftime("%y%m%d.%H%M%S") 00249 key = keybase + "_" + str(count) 00250 while self.__async__.has_key(key) : 00251 count += 1 00252 key = keybase + "_" + str(count) 00253 result = tm.execute('widebandpbcor', vis, imagename, nterms, threshold, action, reffreq, pbmin, field, spwlist, chanlist, weightlist) 00254 print "Use: " 00255 print " tm.retrieve(return_value) # to retrieve the status" 00256 print 00257 self.rkey = key 00258 self.__async__[key] = result 00259 else : 00260 tname = 'widebandpbcor' 00261 spaces = ' '*(18-len(tname)) 00262 casalog.post('\n##########################################'+ 00263 '\n##### Begin Task: ' + tname + spaces + ' #####') 00264 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00265 result = widebandpbcor(vis, imagename, nterms, threshold, action, reffreq, pbmin, field, spwlist, chanlist, weightlist) 00266 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00267 '\n##########################################') 00268 00269 except Exception, instance: 00270 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00271 raise 00272 else : 00273 #print '**** Error **** ',instance 00274 tname = 'widebandpbcor' 00275 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00276 pass 00277 00278 gc.collect() 00279 return result 00280 # 00281 # 00282 # 00283 def paramgui(self, useGlobals=True, ipython_globals=None): 00284 """ 00285 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00286 """ 00287 import paramgui 00288 if not hasattr(self, "__globals__") or self.__globals__ == None : 00289 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00290 00291 if useGlobals: 00292 if ipython_globals == None: 00293 myf=self.__globals__ 00294 else: 00295 myf=ipython_globals 00296 00297 paramgui.setGlobals(myf) 00298 else: 00299 paramgui.setGlobals({}) 00300 00301 paramgui.runTask('widebandpbcor', myf['_ip']) 00302 paramgui.setGlobals({}) 00303 00304 # 00305 # 00306 # 00307 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00308 if not hasattr(self, "__globals__") or self.__globals__ == None : 00309 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00310 if ipython_globals == None: 00311 myf=self.__globals__ 00312 else: 00313 myf=ipython_globals 00314 00315 a = odict() 00316 a['vis'] = '' 00317 a['imagename'] = '' 00318 a['nterms'] = 2 00319 a['threshold'] = '' 00320 a['action'] = 'pbcor' 00321 00322 a['async']=False 00323 a['action'] = { 00324 0:odict([{'value':'pbcor'}, {'reffreq':''}, {'pbmin':0.2}, {'field':''}, {'spwlist':[]}, {'chanlist':[]}, {'weightlist':[]}])} 00325 00326 ### This function sets the default values but also will return the list of 00327 ### parameters or the default value of a given parameter 00328 if(param == None): 00329 myf['__set_default_parameters'](a) 00330 elif(param == 'paramkeys'): 00331 return a.keys() 00332 else: 00333 if(paramvalue==None and subparam==None): 00334 if(a.has_key(param)): 00335 return a[param] 00336 else: 00337 return self.itsdefault(param) 00338 else: 00339 retval=a[param] 00340 if(type(a[param])==dict): 00341 for k in range(len(a[param])): 00342 valornotval='value' 00343 if(a[param][k].has_key('notvalue')): 00344 valornotval='notvalue' 00345 if((a[param][k][valornotval])==paramvalue): 00346 retval=a[param][k].copy() 00347 retval.pop(valornotval) 00348 if(subparam != None): 00349 if(retval.has_key(subparam)): 00350 retval=retval[subparam] 00351 else: 00352 retval=self.itsdefault(subparam) 00353 else: 00354 retval=self.itsdefault(subparam) 00355 return retval 00356 00357 00358 # 00359 # 00360 def check_params(self, param=None, value=None, ipython_globals=None): 00361 if ipython_globals == None: 00362 myf=self.__globals__ 00363 else: 00364 myf=ipython_globals 00365 # print 'param:', param, 'value:', value 00366 try : 00367 if str(type(value)) != "<type 'instance'>" : 00368 value0 = value 00369 value = myf['cu'].expandparam(param, value) 00370 matchtype = False 00371 if(type(value) == numpy.ndarray): 00372 if(type(value) == type(value0)): 00373 myf[param] = value.tolist() 00374 else: 00375 #print 'value:', value, 'value0:', value0 00376 #print 'type(value):', type(value), 'type(value0):', type(value0) 00377 myf[param] = value0 00378 if type(value0) != list : 00379 matchtype = True 00380 else : 00381 myf[param] = value 00382 value = myf['cu'].verifyparam({param:value}) 00383 if matchtype: 00384 value = False 00385 except Exception, instance: 00386 #ignore the exception and just return it unchecked 00387 myf[param] = value 00388 return value 00389 # 00390 # 00391 def description(self, key='widebandpbcor', subkey=None): 00392 desc={'widebandpbcor': 'Wideband PB-correction on the output of the MS-MFS algorithm', 00393 'vis': 'Name of measurement set. ', 00394 'imagename': 'Name-prefix of multi-termimages to operate on. ', 00395 'nterms': 'Number of taylor terms to use', 00396 'threshold': 'Intensity above which to re-calculate spectral index ', 00397 'action': 'PB-correction (pbcor) or only calc spectral-index (calcalpha)', 00398 'reffreq': 'Reference frequency (if specified in clean)', 00399 'pbmin': 'PB threshold below which to not correct', 00400 'field': 'Fields to include in the PB calculation', 00401 'spwlist': 'List of N spw ids', 00402 'chanlist': 'List of N channel ids', 00403 'weightlist': 'List of N weights (relative)', 00404 00405 'async': 'If true the taskname must be started using widebandpbcor(...)' 00406 } 00407 00408 # 00409 # Set subfields defaults if needed 00410 # 00411 00412 if(desc.has_key(key)) : 00413 return desc[key] 00414 00415 def itsdefault(self, paramname) : 00416 a = {} 00417 a['vis'] = '' 00418 a['imagename'] = '' 00419 a['nterms'] = 2 00420 a['threshold'] = '' 00421 a['action'] = 'pbcor' 00422 a['reffreq'] = '' 00423 a['pbmin'] = 0.2 00424 a['field'] = '' 00425 a['spwlist'] = [] 00426 a['chanlist'] = [] 00427 a['weightlist'] = [] 00428 00429 #a = sys._getframe(len(inspect.stack())-1).f_globals 00430 00431 if self.parameters['action'] == 'pbcor': 00432 a['reffreq'] = '' 00433 a['pbmin'] = 0.2 00434 a['field'] = '' 00435 a['spwlist'] = [] 00436 a['chanlist'] = [] 00437 a['weightlist'] = [] 00438 00439 if a.has_key(paramname) : 00440 return a[paramname] 00441 widebandpbcor_cli = widebandpbcor_cli_()