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_uvsub import uvsub 00018 class uvsub_cli_: 00019 __name__ = "uvsub" 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__ = (uvsub_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'vis':None, 'reverse':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, reverse=None, async=None): 00047 00048 """Subtract/add model from/to the corrected visibility data. 00049 Help for uvsub task 00050 00051 This function subtracts model visibility data from corrected visibility 00052 data leaving the residuals in the corrected data column. If the 00053 parameter 'reverse' is set true, the process is reversed. 00054 Please note the model visibility used is the one that has been saved in the MODEL_DATA of the MS and the 00055 CORRECTED_DATA column is the one that is modified. If no CORRECTED_DATA column exists in the MS, one will be created and 00056 a copy of the DATA column is saved in it before the uvsub operation selected is performed. uvsub does not modify the DATA column. 00057 00058 Keyword arguments: 00059 vis -- Name of input visibility file (MS) 00060 default: none; example: vis='ngc5921.ms' 00061 reverse -- Reverse the operation (add rather than subtract) 00062 default: False; example: reverse=true 00063 00064 uvsub(vis='ngc5921.ms', reverse=False) 00065 00066 00067 """ 00068 if not hasattr(self, "__globals__") or self.__globals__ == None : 00069 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00070 #casac = self.__globals__['casac'] 00071 casalog = self.__globals__['casalog'] 00072 #casalog = casac.casac.logsink() 00073 self.__globals__['__last_task'] = 'uvsub' 00074 self.__globals__['taskname'] = 'uvsub' 00075 ### 00076 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00077 ### 00078 ### 00079 #Handle globals or user over-ride of arguments 00080 # 00081 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00082 useLocalDefaults = False 00083 00084 for item in function_signature_defaults.iteritems(): 00085 key,val = item 00086 keyVal = eval(key) 00087 if (keyVal == None): 00088 #user hasn't set it - use global/default 00089 pass 00090 else: 00091 #user has set it - use over-ride 00092 if (key != 'self') : 00093 useLocalDefaults = True 00094 00095 myparams = {} 00096 if useLocalDefaults : 00097 for item in function_signature_defaults.iteritems(): 00098 key,val = item 00099 keyVal = eval(key) 00100 exec('myparams[key] = keyVal') 00101 self.parameters[key] = keyVal 00102 if (keyVal == None): 00103 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00104 keyVal = eval(key) 00105 if(type(keyVal) == dict) : 00106 if len(keyVal) > 0 : 00107 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00108 else : 00109 exec('myparams[key] = ' + key + ' = {}') 00110 00111 else : 00112 async = self.parameters['async'] 00113 myparams['vis'] = vis = self.parameters['vis'] 00114 myparams['reverse'] = reverse = self.parameters['reverse'] 00115 00116 00117 result = None 00118 00119 # 00120 # The following is work around to avoid a bug with current python translation 00121 # 00122 mytmp = {} 00123 00124 mytmp['vis'] = vis 00125 mytmp['reverse'] = reverse 00126 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00127 trec = casac.casac.utils().torecord(pathname+'uvsub.xml') 00128 00129 casalog.origin('uvsub') 00130 try : 00131 #if not trec.has_key('uvsub') or not casac.casac.utils().verify(mytmp, trec['uvsub']) : 00132 #return False 00133 00134 casac.casac.utils().verify(mytmp, trec['uvsub'], True) 00135 scriptstr=[''] 00136 saveinputs = self.__globals__['saveinputs'] 00137 saveinputs('uvsub', 'uvsub.last', myparams, self.__globals__,scriptstr=scriptstr) 00138 if async : 00139 count = 0 00140 keybase = time.strftime("%y%m%d.%H%M%S") 00141 key = keybase + "_" + str(count) 00142 while self.__async__.has_key(key) : 00143 count += 1 00144 key = keybase + "_" + str(count) 00145 result = tm.execute('uvsub', vis, reverse) 00146 print "Use: " 00147 print " tm.retrieve(return_value) # to retrieve the status" 00148 print 00149 self.rkey = key 00150 self.__async__[key] = result 00151 else : 00152 tname = 'uvsub' 00153 spaces = ' '*(18-len(tname)) 00154 casalog.post('\n##########################################'+ 00155 '\n##### Begin Task: ' + tname + spaces + ' #####') 00156 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00157 result = uvsub(vis, reverse) 00158 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00159 '\n##########################################') 00160 00161 except Exception, instance: 00162 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00163 raise 00164 else : 00165 #print '**** Error **** ',instance 00166 tname = 'uvsub' 00167 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00168 pass 00169 00170 gc.collect() 00171 return result 00172 # 00173 # 00174 # 00175 def paramgui(self, useGlobals=True, ipython_globals=None): 00176 """ 00177 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00178 """ 00179 import paramgui 00180 if not hasattr(self, "__globals__") or self.__globals__ == None : 00181 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00182 00183 if useGlobals: 00184 if ipython_globals == None: 00185 myf=self.__globals__ 00186 else: 00187 myf=ipython_globals 00188 00189 paramgui.setGlobals(myf) 00190 else: 00191 paramgui.setGlobals({}) 00192 00193 paramgui.runTask('uvsub', myf['_ip']) 00194 paramgui.setGlobals({}) 00195 00196 # 00197 # 00198 # 00199 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00200 if not hasattr(self, "__globals__") or self.__globals__ == None : 00201 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00202 if ipython_globals == None: 00203 myf=self.__globals__ 00204 else: 00205 myf=ipython_globals 00206 00207 a = odict() 00208 a['vis'] = '' 00209 a['reverse'] = False 00210 00211 a['async']=False 00212 00213 ### This function sets the default values but also will return the list of 00214 ### parameters or the default value of a given parameter 00215 if(param == None): 00216 myf['__set_default_parameters'](a) 00217 elif(param == 'paramkeys'): 00218 return a.keys() 00219 else: 00220 if(paramvalue==None and subparam==None): 00221 if(a.has_key(param)): 00222 return a[param] 00223 else: 00224 return self.itsdefault(param) 00225 else: 00226 retval=a[param] 00227 if(type(a[param])==dict): 00228 for k in range(len(a[param])): 00229 valornotval='value' 00230 if(a[param][k].has_key('notvalue')): 00231 valornotval='notvalue' 00232 if((a[param][k][valornotval])==paramvalue): 00233 retval=a[param][k].copy() 00234 retval.pop(valornotval) 00235 if(subparam != None): 00236 if(retval.has_key(subparam)): 00237 retval=retval[subparam] 00238 else: 00239 retval=self.itsdefault(subparam) 00240 else: 00241 retval=self.itsdefault(subparam) 00242 return retval 00243 00244 00245 # 00246 # 00247 def check_params(self, param=None, value=None, ipython_globals=None): 00248 if ipython_globals == None: 00249 myf=self.__globals__ 00250 else: 00251 myf=ipython_globals 00252 # print 'param:', param, 'value:', value 00253 try : 00254 if str(type(value)) != "<type 'instance'>" : 00255 value0 = value 00256 value = myf['cu'].expandparam(param, value) 00257 matchtype = False 00258 if(type(value) == numpy.ndarray): 00259 if(type(value) == type(value0)): 00260 myf[param] = value.tolist() 00261 else: 00262 #print 'value:', value, 'value0:', value0 00263 #print 'type(value):', type(value), 'type(value0):', type(value0) 00264 myf[param] = value0 00265 if type(value0) != list : 00266 matchtype = True 00267 else : 00268 myf[param] = value 00269 value = myf['cu'].verifyparam({param:value}) 00270 if matchtype: 00271 value = False 00272 except Exception, instance: 00273 #ignore the exception and just return it unchecked 00274 myf[param] = value 00275 return value 00276 # 00277 # 00278 def description(self, key='uvsub', subkey=None): 00279 desc={'uvsub': 'Subtract/add model from/to the corrected visibility data.', 00280 'vis': 'Name of input visibility file (MS)', 00281 'reverse': 'reverse the operation (add rather than subtract)', 00282 00283 'async': 'If true the taskname must be started using uvsub(...)' 00284 } 00285 00286 # 00287 # Set subfields defaults if needed 00288 # 00289 00290 if(desc.has_key(key)) : 00291 return desc[key] 00292 00293 def itsdefault(self, paramname) : 00294 a = {} 00295 a['vis'] = '' 00296 a['reverse'] = False 00297 00298 #a = sys._getframe(len(inspect.stack())-1).f_globals 00299 00300 if a.has_key(paramname) : 00301 return a[paramname] 00302 uvsub_cli = uvsub_cli_()