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_delmod import delmod 00018 class delmod_cli_: 00019 __name__ = "delmod" 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__ = (delmod_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'vis':None, 'otf':None, 'field':None, 'scr':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, otf=None, field=None, scr=None, async=None): 00047 00048 """Deletes model representations in the MS 00049 00050 This utility task is to be used to delete the model visibility 00051 data representations in the MS. The 'otf' representation is 00052 the new (as of v3.4) 'scratch-less' model data, stored as 00053 keywords in the MS header containing model data formation 00054 instructions. It is generated by the setjy, ft, and clean 00055 tasks (usescratch=F), and if present, overrides the 00056 old-fashioned MODEL_DATA column (if present). If a user 00057 wishes to use the MODEL_DATA column _after_ having operated 00058 with the 'otf' representation, this task can be used 00059 to delete the 'otf' represenatation to make the MODEL_DATA 00060 column visible. (Create the MODEL_DATA column by using 00061 usescratch=T in setjy, ft, or clean; or by running the 00062 clearcal task with addmodel=T.) 00063 00064 If otf=T, specific fields can be selected for deletion 00065 using standard field selection semantics. If field='', 00066 all fields' models will be deleted. 00067 00068 For convenience, this method also provides a means for 00069 deleting the MODEL_DATA column by setting scr=T. Note 00070 that it is not possible to delete the MODEL_DATA column 00071 per field. 00072 00073 If otf=F and scr=F, delmod will provide a listing 00074 of the header field records. 00075 00076 00077 00078 """ 00079 if not hasattr(self, "__globals__") or self.__globals__ == None : 00080 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00081 #casac = self.__globals__['casac'] 00082 casalog = self.__globals__['casalog'] 00083 #casalog = casac.casac.logsink() 00084 self.__globals__['__last_task'] = 'delmod' 00085 self.__globals__['taskname'] = 'delmod' 00086 ### 00087 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00088 ### 00089 ### 00090 #Handle globals or user over-ride of arguments 00091 # 00092 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00093 useLocalDefaults = False 00094 00095 for item in function_signature_defaults.iteritems(): 00096 key,val = item 00097 keyVal = eval(key) 00098 if (keyVal == None): 00099 #user hasn't set it - use global/default 00100 pass 00101 else: 00102 #user has set it - use over-ride 00103 if (key != 'self') : 00104 useLocalDefaults = True 00105 00106 myparams = {} 00107 if useLocalDefaults : 00108 for item in function_signature_defaults.iteritems(): 00109 key,val = item 00110 keyVal = eval(key) 00111 exec('myparams[key] = keyVal') 00112 self.parameters[key] = keyVal 00113 if (keyVal == None): 00114 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00115 keyVal = eval(key) 00116 if(type(keyVal) == dict) : 00117 if len(keyVal) > 0 : 00118 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00119 else : 00120 exec('myparams[key] = ' + key + ' = {}') 00121 00122 else : 00123 async = self.parameters['async'] 00124 myparams['vis'] = vis = self.parameters['vis'] 00125 myparams['otf'] = otf = self.parameters['otf'] 00126 myparams['field'] = field = self.parameters['field'] 00127 myparams['scr'] = scr = self.parameters['scr'] 00128 00129 00130 result = None 00131 00132 # 00133 # The following is work around to avoid a bug with current python translation 00134 # 00135 mytmp = {} 00136 00137 mytmp['vis'] = vis 00138 mytmp['otf'] = otf 00139 mytmp['field'] = field 00140 mytmp['scr'] = scr 00141 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00142 trec = casac.casac.utils().torecord(pathname+'delmod.xml') 00143 00144 casalog.origin('delmod') 00145 try : 00146 #if not trec.has_key('delmod') or not casac.casac.utils().verify(mytmp, trec['delmod']) : 00147 #return False 00148 00149 casac.casac.utils().verify(mytmp, trec['delmod'], True) 00150 scriptstr=[''] 00151 saveinputs = self.__globals__['saveinputs'] 00152 saveinputs('delmod', 'delmod.last', myparams, self.__globals__,scriptstr=scriptstr) 00153 if async : 00154 count = 0 00155 keybase = time.strftime("%y%m%d.%H%M%S") 00156 key = keybase + "_" + str(count) 00157 while self.__async__.has_key(key) : 00158 count += 1 00159 key = keybase + "_" + str(count) 00160 result = tm.execute('delmod', vis, otf, field, scr) 00161 print "Use: " 00162 print " tm.retrieve(return_value) # to retrieve the status" 00163 print 00164 self.rkey = key 00165 self.__async__[key] = result 00166 else : 00167 tname = 'delmod' 00168 spaces = ' '*(18-len(tname)) 00169 casalog.post('\n##########################################'+ 00170 '\n##### Begin Task: ' + tname + spaces + ' #####') 00171 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00172 result = delmod(vis, otf, field, scr) 00173 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00174 '\n##########################################') 00175 00176 except Exception, instance: 00177 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00178 raise 00179 else : 00180 #print '**** Error **** ',instance 00181 tname = 'delmod' 00182 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00183 pass 00184 00185 gc.collect() 00186 return result 00187 # 00188 # 00189 # 00190 def paramgui(self, useGlobals=True, ipython_globals=None): 00191 """ 00192 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00193 """ 00194 import paramgui 00195 if not hasattr(self, "__globals__") or self.__globals__ == None : 00196 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00197 00198 if useGlobals: 00199 if ipython_globals == None: 00200 myf=self.__globals__ 00201 else: 00202 myf=ipython_globals 00203 00204 paramgui.setGlobals(myf) 00205 else: 00206 paramgui.setGlobals({}) 00207 00208 paramgui.runTask('delmod', myf['_ip']) 00209 paramgui.setGlobals({}) 00210 00211 # 00212 # 00213 # 00214 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00215 if not hasattr(self, "__globals__") or self.__globals__ == None : 00216 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00217 if ipython_globals == None: 00218 myf=self.__globals__ 00219 else: 00220 myf=ipython_globals 00221 00222 a = odict() 00223 a['vis'] = '' 00224 a['otf'] = True 00225 a['scr'] = False 00226 00227 a['async']=False 00228 a['otf'] = { 00229 0:odict([{'value':True}, {'field':''}]), 00230 1:{'value':False}} 00231 00232 ### This function sets the default values but also will return the list of 00233 ### parameters or the default value of a given parameter 00234 if(param == None): 00235 myf['__set_default_parameters'](a) 00236 elif(param == 'paramkeys'): 00237 return a.keys() 00238 else: 00239 if(paramvalue==None and subparam==None): 00240 if(a.has_key(param)): 00241 return a[param] 00242 else: 00243 return self.itsdefault(param) 00244 else: 00245 retval=a[param] 00246 if(type(a[param])==dict): 00247 for k in range(len(a[param])): 00248 valornotval='value' 00249 if(a[param][k].has_key('notvalue')): 00250 valornotval='notvalue' 00251 if((a[param][k][valornotval])==paramvalue): 00252 retval=a[param][k].copy() 00253 retval.pop(valornotval) 00254 if(subparam != None): 00255 if(retval.has_key(subparam)): 00256 retval=retval[subparam] 00257 else: 00258 retval=self.itsdefault(subparam) 00259 else: 00260 retval=self.itsdefault(subparam) 00261 return retval 00262 00263 00264 # 00265 # 00266 def check_params(self, param=None, value=None, ipython_globals=None): 00267 if ipython_globals == None: 00268 myf=self.__globals__ 00269 else: 00270 myf=ipython_globals 00271 # print 'param:', param, 'value:', value 00272 try : 00273 if str(type(value)) != "<type 'instance'>" : 00274 value0 = value 00275 value = myf['cu'].expandparam(param, value) 00276 matchtype = False 00277 if(type(value) == numpy.ndarray): 00278 if(type(value) == type(value0)): 00279 myf[param] = value.tolist() 00280 else: 00281 #print 'value:', value, 'value0:', value0 00282 #print 'type(value):', type(value), 'type(value0):', type(value0) 00283 myf[param] = value0 00284 if type(value0) != list : 00285 matchtype = True 00286 else : 00287 myf[param] = value 00288 value = myf['cu'].verifyparam({param:value}) 00289 if matchtype: 00290 value = False 00291 except Exception, instance: 00292 #ignore the exception and just return it unchecked 00293 myf[param] = value 00294 return value 00295 # 00296 # 00297 def description(self, key='delmod', subkey=None): 00298 desc={'delmod': 'Deletes model representations in the MS', 00299 'vis': 'Name of input visibility file (MS)', 00300 'otf': 'Delete the on-the-fly model data keywords', 00301 'field': 'Select field using field id(s) or field name(s)', 00302 'scr': 'Delete the MODEL_DATA scr col (if it exists)', 00303 00304 'async': 'If true the taskname must be started using delmod(...)' 00305 } 00306 00307 # 00308 # Set subfields defaults if needed 00309 # 00310 00311 if(desc.has_key(key)) : 00312 return desc[key] 00313 00314 def itsdefault(self, paramname) : 00315 a = {} 00316 a['vis'] = '' 00317 a['otf'] = True 00318 a['field'] = '' 00319 a['scr'] = False 00320 00321 #a = sys._getframe(len(inspect.stack())-1).f_globals 00322 00323 if self.parameters['otf'] == True: 00324 a['field'] = '' 00325 00326 if a.has_key(paramname) : 00327 return a[paramname] 00328 delmod_cli = delmod_cli_()