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