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_testconcat import testconcat 00018 class testconcat_cli_: 00019 __name__ = "testconcat" 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__ = (testconcat_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'vis':None, 'testconcatvis':None, 'freqtol':None, 'dirtol':None, 'copypointing':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, testconcatvis=None, freqtol=None, dirtol=None, copypointing=None, async=None): 00047 00048 """Concatenate the subtables of several visibility data sets, not the MAIN bulk data. 00049 00050 The list of data sets given in the vis argument are concatenated into an output 00051 data set in testconcatvis without the bulk data of the MAIN table. 00052 This is useful for obtaining the information in the merged subtables without 00053 actually performing a time-consuming concatenation of the MAIN tables on disk. 00054 00055 00056 Keyword arguments: 00057 vis -- Name of input visibility files for which the subtables are to be combined 00058 default: none; example: vis = 'mydata.ms', 00059 vis=['src2.ms','ngc5921.ms','ngc315.ms'] 00060 testconcatvis -- Name of MS that will contain the concatenated subtables 00061 default: none; example: testconcatvis='test.ms' 00062 00063 freqtol -- Frequency shift tolerance for considering data to be in the same 00064 spwid. The number of channels must also be the same. 00065 default: '' do not combine unless frequencies are equal 00066 example: freqtol='10MHz' will not combine spwid unless they are 00067 within 10 MHz. 00068 Note: This option is useful to conbine spectral windows with very slight 00069 frequency differences caused by Doppler tracking, for example. 00070 00071 dirtol -- Direction shift tolerance for considering data as the same field 00072 default: '' means always combine. 00073 example: dirtol='1.arcsec' will not combine data for a field unless 00074 their phase center differ by less than 1 arcsec. If the field names 00075 are different in the input data sets, the name in the output data 00076 set will be the first relevant data set in the list. 00077 00078 copypointing -- Make a proper copy of the POINTING subtable (can be time consuming). 00079 If False, the result is an empty POINTING table. 00080 default: True 00081 00082 00083 """ 00084 if not hasattr(self, "__globals__") or self.__globals__ == None : 00085 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00086 #casac = self.__globals__['casac'] 00087 casalog = self.__globals__['casalog'] 00088 #casalog = casac.casac.logsink() 00089 self.__globals__['__last_task'] = 'testconcat' 00090 self.__globals__['taskname'] = 'testconcat' 00091 ### 00092 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00093 ### 00094 ### 00095 #Handle globals or user over-ride of arguments 00096 # 00097 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00098 useLocalDefaults = False 00099 00100 for item in function_signature_defaults.iteritems(): 00101 key,val = item 00102 keyVal = eval(key) 00103 if (keyVal == None): 00104 #user hasn't set it - use global/default 00105 pass 00106 else: 00107 #user has set it - use over-ride 00108 if (key != 'self') : 00109 useLocalDefaults = True 00110 00111 myparams = {} 00112 if useLocalDefaults : 00113 for item in function_signature_defaults.iteritems(): 00114 key,val = item 00115 keyVal = eval(key) 00116 exec('myparams[key] = keyVal') 00117 self.parameters[key] = keyVal 00118 if (keyVal == None): 00119 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00120 keyVal = eval(key) 00121 if(type(keyVal) == dict) : 00122 if len(keyVal) > 0 : 00123 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00124 else : 00125 exec('myparams[key] = ' + key + ' = {}') 00126 00127 else : 00128 async = self.parameters['async'] 00129 myparams['vis'] = vis = self.parameters['vis'] 00130 myparams['testconcatvis'] = testconcatvis = self.parameters['testconcatvis'] 00131 myparams['freqtol'] = freqtol = self.parameters['freqtol'] 00132 myparams['dirtol'] = dirtol = self.parameters['dirtol'] 00133 myparams['copypointing'] = copypointing = self.parameters['copypointing'] 00134 00135 if type(vis)==str: vis=[vis] 00136 00137 result = None 00138 00139 # 00140 # The following is work around to avoid a bug with current python translation 00141 # 00142 mytmp = {} 00143 00144 mytmp['vis'] = vis 00145 mytmp['testconcatvis'] = testconcatvis 00146 mytmp['freqtol'] = freqtol 00147 mytmp['dirtol'] = dirtol 00148 mytmp['copypointing'] = copypointing 00149 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00150 trec = casac.casac.utils().torecord(pathname+'testconcat.xml') 00151 00152 casalog.origin('testconcat') 00153 try : 00154 #if not trec.has_key('testconcat') or not casac.casac.utils().verify(mytmp, trec['testconcat']) : 00155 #return False 00156 00157 casac.casac.utils().verify(mytmp, trec['testconcat'], True) 00158 scriptstr=[''] 00159 saveinputs = self.__globals__['saveinputs'] 00160 saveinputs('testconcat', 'testconcat.last', myparams, self.__globals__,scriptstr=scriptstr) 00161 if async : 00162 count = 0 00163 keybase = time.strftime("%y%m%d.%H%M%S") 00164 key = keybase + "_" + str(count) 00165 while self.__async__.has_key(key) : 00166 count += 1 00167 key = keybase + "_" + str(count) 00168 result = tm.execute('testconcat', vis, testconcatvis, freqtol, dirtol, copypointing) 00169 print "Use: " 00170 print " tm.retrieve(return_value) # to retrieve the status" 00171 print 00172 self.rkey = key 00173 self.__async__[key] = result 00174 else : 00175 tname = 'testconcat' 00176 spaces = ' '*(18-len(tname)) 00177 casalog.post('\n##########################################'+ 00178 '\n##### Begin Task: ' + tname + spaces + ' #####') 00179 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00180 result = testconcat(vis, testconcatvis, freqtol, dirtol, copypointing) 00181 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00182 '\n##########################################') 00183 00184 except Exception, instance: 00185 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00186 raise 00187 else : 00188 #print '**** Error **** ',instance 00189 tname = 'testconcat' 00190 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00191 pass 00192 00193 gc.collect() 00194 return result 00195 # 00196 # 00197 # 00198 def paramgui(self, useGlobals=True, ipython_globals=None): 00199 """ 00200 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00201 """ 00202 import paramgui 00203 if not hasattr(self, "__globals__") or self.__globals__ == None : 00204 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00205 00206 if useGlobals: 00207 if ipython_globals == None: 00208 myf=self.__globals__ 00209 else: 00210 myf=ipython_globals 00211 00212 paramgui.setGlobals(myf) 00213 else: 00214 paramgui.setGlobals({}) 00215 00216 paramgui.runTask('testconcat', myf['_ip']) 00217 paramgui.setGlobals({}) 00218 00219 # 00220 # 00221 # 00222 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00223 if not hasattr(self, "__globals__") or self.__globals__ == None : 00224 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00225 if ipython_globals == None: 00226 myf=self.__globals__ 00227 else: 00228 myf=ipython_globals 00229 00230 a = odict() 00231 a['vis'] = [''] 00232 a['testconcatvis'] = '' 00233 a['freqtol'] = '' 00234 a['dirtol'] = '' 00235 a['copypointing'] = True 00236 00237 a['async']=False 00238 00239 ### This function sets the default values but also will return the list of 00240 ### parameters or the default value of a given parameter 00241 if(param == None): 00242 myf['__set_default_parameters'](a) 00243 elif(param == 'paramkeys'): 00244 return a.keys() 00245 else: 00246 if(paramvalue==None and subparam==None): 00247 if(a.has_key(param)): 00248 return a[param] 00249 else: 00250 return self.itsdefault(param) 00251 else: 00252 retval=a[param] 00253 if(type(a[param])==dict): 00254 for k in range(len(a[param])): 00255 valornotval='value' 00256 if(a[param][k].has_key('notvalue')): 00257 valornotval='notvalue' 00258 if((a[param][k][valornotval])==paramvalue): 00259 retval=a[param][k].copy() 00260 retval.pop(valornotval) 00261 if(subparam != None): 00262 if(retval.has_key(subparam)): 00263 retval=retval[subparam] 00264 else: 00265 retval=self.itsdefault(subparam) 00266 else: 00267 retval=self.itsdefault(subparam) 00268 return retval 00269 00270 00271 # 00272 # 00273 def check_params(self, param=None, value=None, ipython_globals=None): 00274 if ipython_globals == None: 00275 myf=self.__globals__ 00276 else: 00277 myf=ipython_globals 00278 # print 'param:', param, 'value:', value 00279 try : 00280 if str(type(value)) != "<type 'instance'>" : 00281 value0 = value 00282 value = myf['cu'].expandparam(param, value) 00283 matchtype = False 00284 if(type(value) == numpy.ndarray): 00285 if(type(value) == type(value0)): 00286 myf[param] = value.tolist() 00287 else: 00288 #print 'value:', value, 'value0:', value0 00289 #print 'type(value):', type(value), 'type(value0):', type(value0) 00290 myf[param] = value0 00291 if type(value0) != list : 00292 matchtype = True 00293 else : 00294 myf[param] = value 00295 value = myf['cu'].verifyparam({param:value}) 00296 if matchtype: 00297 value = False 00298 except Exception, instance: 00299 #ignore the exception and just return it unchecked 00300 myf[param] = value 00301 return value 00302 # 00303 # 00304 def description(self, key='testconcat', subkey=None): 00305 desc={'testconcat': 'Concatenate the subtables of several visibility data sets, not the MAIN bulk data.', 00306 'vis': 'Name(s) of input visibility files to be test-concatenated', 00307 'testconcatvis': 'Name of output MS containing the merged subtables', 00308 'freqtol': 'Frequency shift tolerance for considering data as the same spwid', 00309 'dirtol': 'Direction shift tolerance for considering data as the same field', 00310 'copypointing': 'Copy all rows of the POINTING table.', 00311 00312 'async': 'If true the taskname must be started using testconcat(...)' 00313 } 00314 00315 if(desc.has_key(key)) : 00316 return desc[key] 00317 00318 def itsdefault(self, paramname) : 00319 a = {} 00320 a['vis'] = [''] 00321 a['testconcatvis'] = '' 00322 a['freqtol'] = '' 00323 a['dirtol'] = '' 00324 a['copypointing'] = True 00325 00326 #a = sys._getframe(len(inspect.stack())-1).f_globals 00327 00328 if a.has_key(paramname) : 00329 return a[paramname] 00330 testconcat_cli = testconcat_cli_()