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_imreframe import imreframe 00018 class imreframe_cli_: 00019 __name__ = "imreframe" 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__ = (imreframe_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'imagename':None, 'output':None, 'outframe':None, 'epoch':None, 'restfreq':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, imagename=None, output=None, outframe=None, epoch=None, restfreq=None, async=None): 00047 00048 """Change the frame in which the image reports its spectral values 00049 00050 imagename -- name of casa image file to process on 00051 output -- name of output image '' means modify the input image itself 00052 default: ''; 00053 outframe -- new spectral frame in which the frequency or 00054 velocity will be reported for. 00055 Options: 'lsrk','lsrd','bary','geo','topo','galacto', 00056 'lgroup','cmb' 00057 default: 'lsrk' 00058 >>> 00059 epoch -- when outframe is 'topo' or 'geo' a time in UTC is needed 00060 to decide when to do the frequency conversion. '' is to use 00061 the epoch of the input image 00062 default= '' 00063 00064 restfreq -- Specify rest frequency to use for output image 00065 default=''; '' means use the restfrequency already in input image 00066 For example for 00067 NH_3 (1,1) put restfreq='23.694496GHz' 00068 00069 00070 """ 00071 if not hasattr(self, "__globals__") or self.__globals__ == None : 00072 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00073 #casac = self.__globals__['casac'] 00074 casalog = self.__globals__['casalog'] 00075 #casalog = casac.casac.logsink() 00076 self.__globals__['__last_task'] = 'imreframe' 00077 self.__globals__['taskname'] = 'imreframe' 00078 ### 00079 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00080 ### 00081 ### 00082 #Handle globals or user over-ride of arguments 00083 # 00084 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00085 useLocalDefaults = False 00086 00087 for item in function_signature_defaults.iteritems(): 00088 key,val = item 00089 keyVal = eval(key) 00090 if (keyVal == None): 00091 #user hasn't set it - use global/default 00092 pass 00093 else: 00094 #user has set it - use over-ride 00095 if (key != 'self') : 00096 useLocalDefaults = True 00097 00098 myparams = {} 00099 if useLocalDefaults : 00100 for item in function_signature_defaults.iteritems(): 00101 key,val = item 00102 keyVal = eval(key) 00103 exec('myparams[key] = keyVal') 00104 self.parameters[key] = keyVal 00105 if (keyVal == None): 00106 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00107 keyVal = eval(key) 00108 if(type(keyVal) == dict) : 00109 if len(keyVal) > 0 : 00110 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00111 else : 00112 exec('myparams[key] = ' + key + ' = {}') 00113 00114 else : 00115 async = self.parameters['async'] 00116 myparams['imagename'] = imagename = self.parameters['imagename'] 00117 myparams['output'] = output = self.parameters['output'] 00118 myparams['outframe'] = outframe = self.parameters['outframe'] 00119 myparams['epoch'] = epoch = self.parameters['epoch'] 00120 myparams['restfreq'] = restfreq = self.parameters['restfreq'] 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['imagename'] = imagename 00131 mytmp['output'] = output 00132 mytmp['outframe'] = outframe 00133 mytmp['epoch'] = epoch 00134 mytmp['restfreq'] = restfreq 00135 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00136 trec = casac.casac.utils().torecord(pathname+'imreframe.xml') 00137 00138 casalog.origin('imreframe') 00139 try : 00140 #if not trec.has_key('imreframe') or not casac.casac.utils().verify(mytmp, trec['imreframe']) : 00141 #return False 00142 00143 casac.casac.utils().verify(mytmp, trec['imreframe'], True) 00144 scriptstr=[''] 00145 saveinputs = self.__globals__['saveinputs'] 00146 saveinputs('imreframe', 'imreframe.last', myparams, self.__globals__,scriptstr=scriptstr) 00147 if async : 00148 count = 0 00149 keybase = time.strftime("%y%m%d.%H%M%S") 00150 key = keybase + "_" + str(count) 00151 while self.__async__.has_key(key) : 00152 count += 1 00153 key = keybase + "_" + str(count) 00154 result = tm.execute('imreframe', imagename, output, outframe, epoch, restfreq) 00155 print "Use: " 00156 print " tm.retrieve(return_value) # to retrieve the status" 00157 print 00158 self.rkey = key 00159 self.__async__[key] = result 00160 else : 00161 tname = 'imreframe' 00162 spaces = ' '*(18-len(tname)) 00163 casalog.post('\n##########################################'+ 00164 '\n##### Begin Task: ' + tname + spaces + ' #####') 00165 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00166 result = imreframe(imagename, output, outframe, epoch, restfreq) 00167 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00168 '\n##########################################') 00169 00170 except Exception, instance: 00171 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00172 raise 00173 else : 00174 #print '**** Error **** ',instance 00175 tname = 'imreframe' 00176 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00177 pass 00178 00179 gc.collect() 00180 return result 00181 # 00182 # 00183 # 00184 def paramgui(self, useGlobals=True, ipython_globals=None): 00185 """ 00186 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00187 """ 00188 import paramgui 00189 if not hasattr(self, "__globals__") or self.__globals__ == None : 00190 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00191 00192 if useGlobals: 00193 if ipython_globals == None: 00194 myf=self.__globals__ 00195 else: 00196 myf=ipython_globals 00197 00198 paramgui.setGlobals(myf) 00199 else: 00200 paramgui.setGlobals({}) 00201 00202 paramgui.runTask('imreframe', myf['_ip']) 00203 paramgui.setGlobals({}) 00204 00205 # 00206 # 00207 # 00208 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00209 if not hasattr(self, "__globals__") or self.__globals__ == None : 00210 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00211 if ipython_globals == None: 00212 myf=self.__globals__ 00213 else: 00214 myf=ipython_globals 00215 00216 a = odict() 00217 a['imagename'] = '' 00218 a['output'] = '' 00219 a['outframe'] = 'lsrk' 00220 a['restfreq'] = '' 00221 00222 a['async']=False 00223 a['outframe'] = { 00224 0:{'value':'lsrk'}, 00225 1:odict([{'value':'topo'}, {'epoch':''}]), 00226 2:odict([{'value':'geo'}, {'epoch':''}])} 00227 00228 ### This function sets the default values but also will return the list of 00229 ### parameters or the default value of a given parameter 00230 if(param == None): 00231 myf['__set_default_parameters'](a) 00232 elif(param == 'paramkeys'): 00233 return a.keys() 00234 else: 00235 if(paramvalue==None and subparam==None): 00236 if(a.has_key(param)): 00237 return a[param] 00238 else: 00239 return self.itsdefault(param) 00240 else: 00241 retval=a[param] 00242 if(type(a[param])==dict): 00243 for k in range(len(a[param])): 00244 valornotval='value' 00245 if(a[param][k].has_key('notvalue')): 00246 valornotval='notvalue' 00247 if((a[param][k][valornotval])==paramvalue): 00248 retval=a[param][k].copy() 00249 retval.pop(valornotval) 00250 if(subparam != None): 00251 if(retval.has_key(subparam)): 00252 retval=retval[subparam] 00253 else: 00254 retval=self.itsdefault(subparam) 00255 else: 00256 retval=self.itsdefault(subparam) 00257 return retval 00258 00259 00260 # 00261 # 00262 def check_params(self, param=None, value=None, ipython_globals=None): 00263 if ipython_globals == None: 00264 myf=self.__globals__ 00265 else: 00266 myf=ipython_globals 00267 # print 'param:', param, 'value:', value 00268 try : 00269 if str(type(value)) != "<type 'instance'>" : 00270 value0 = value 00271 value = myf['cu'].expandparam(param, value) 00272 matchtype = False 00273 if(type(value) == numpy.ndarray): 00274 if(type(value) == type(value0)): 00275 myf[param] = value.tolist() 00276 else: 00277 #print 'value:', value, 'value0:', value0 00278 #print 'type(value):', type(value), 'type(value0):', type(value0) 00279 myf[param] = value0 00280 if type(value0) != list : 00281 matchtype = True 00282 else : 00283 myf[param] = value 00284 value = myf['cu'].verifyparam({param:value}) 00285 if matchtype: 00286 value = False 00287 except Exception, instance: 00288 #ignore the exception and just return it unchecked 00289 myf[param] = value 00290 return value 00291 # 00292 # 00293 def description(self, key='imreframe', subkey=None): 00294 desc={'imreframe': 'Change the frame in which the image reports its spectral values', 00295 'imagename': 'Name of the input image', 00296 'output': 'Name of the output image; \'\' => modify input image', 00297 'outframe': 'Spectral frame in which the frequency or velocity values will be reported by default', 00298 'epoch': 'Epoch to be associated with this image e.g \'2000/12/25/18:30:00.10\'', 00299 'restfreq': 'restfrequency to use for velocity values (e.g "1.420GHz" for the HI line)', 00300 00301 'async': 'If true the taskname must be started using imreframe(...)' 00302 } 00303 00304 # 00305 # Set subfields defaults if needed 00306 # 00307 00308 if(desc.has_key(key)) : 00309 return desc[key] 00310 00311 def itsdefault(self, paramname) : 00312 a = {} 00313 a['imagename'] = '' 00314 a['output'] = '' 00315 a['outframe'] = 'lsrk' 00316 a['epoch'] = '' 00317 a['restfreq'] = '' 00318 00319 #a = sys._getframe(len(inspect.stack())-1).f_globals 00320 00321 if self.parameters['outframe'] == 'topo': 00322 a['epoch'] = '' 00323 00324 if self.parameters['outframe'] == 'geo': 00325 a['epoch'] = '' 00326 00327 if a.has_key(paramname) : 00328 return a[paramname] 00329 imreframe_cli = imreframe_cli_()