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_imregrid import imregrid 00018 class imregrid_cli_: 00019 __name__ = "imregrid" 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__ = (imregrid_cli_,) 00028 self.__doc__ = self.__call__.__doc__ 00029 00030 self.parameters={'imagename':None, 'template':None, 'output':None, 'asvelocity':None, 'axes':None, 'shape':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, template=None, output=None, asvelocity=None, axes=None, shape=None, async=None): 00047 00048 """regrid an image onto a template image 00049 00050 imregrid: regrid an image to a new coordinate system. 00051 00052 Keyword arguments: 00053 imagename Name of the image that needs to be regridded 00054 default: none; example: imagename='orion.image' 00055 template Dictionary, reference code, or imagename defining the new 00056 shape and coordinate system, or 'get' to return the template 00057 dictionary for imagename. Recognized reference codes are: 00058 'J2000', 'B1950', 'B1950_VLA', 'GALACTIC', 'HADEC', 'AZEL', 00059 'AZELSW', 'AZELNE', 'ECLIPTIC', 'MECLIPTIC', 'TECLIPTIC', 00060 and 'SUPERGAL'. 00061 default: 'get'; example: template='orion_j2000.im' 00062 shape Shape of the output image. Only used if template is an image. 00063 If not specified (-1), the output image shape is set equal to 00064 the input image shape. 00065 output Name for the regridded image 00066 default: imagename + '.regridded'; example: imagename='orion_shifted.im' 00067 asvelocity Regrid spectral axis with respect to velocity, not frequency? 00068 default: False 00069 axes The pixel axes to regrid. -1 => all. 00070 async Run task in a separate process (return CASA prompt) 00071 default: False; example: async=True 00072 00073 The new coordinate system is defined by the template parameter, 00074 which can be: 00075 * a recognized reference code string (see below), 00076 * a {'csys': csys_record, 'shap': shape} dictionary, 00077 * 'get', which does not regrid but returns the template dictionary 00078 for imagename, suitable for modification and reuse, or 00079 * the name of an image to get the coordinate system and shape 00080 from. The input and template images must have the same 00081 coordinate structure. 00082 00083 If one of the axes to be regridded is a spectral axis and asvelocity=T, 00084 the axis will be regridded to match the velocity, not the frequency, 00085 description of the template coordinate system. Thus the output pixel 00086 values will correspond only to the velocity, not the frequency, of the 00087 output axis. 00088 00089 If an image has per-plane beams and one attempts to regrid the spectral axis, 00090 an exception is thrown. 00091 00092 00093 00094 """ 00095 if not hasattr(self, "__globals__") or self.__globals__ == None : 00096 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00097 #casac = self.__globals__['casac'] 00098 casalog = self.__globals__['casalog'] 00099 #casalog = casac.casac.logsink() 00100 self.__globals__['__last_task'] = 'imregrid' 00101 self.__globals__['taskname'] = 'imregrid' 00102 ### 00103 self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__) 00104 ### 00105 ### 00106 #Handle globals or user over-ride of arguments 00107 # 00108 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00109 useLocalDefaults = False 00110 00111 for item in function_signature_defaults.iteritems(): 00112 key,val = item 00113 keyVal = eval(key) 00114 if (keyVal == None): 00115 #user hasn't set it - use global/default 00116 pass 00117 else: 00118 #user has set it - use over-ride 00119 if (key != 'self') : 00120 useLocalDefaults = True 00121 00122 myparams = {} 00123 if useLocalDefaults : 00124 for item in function_signature_defaults.iteritems(): 00125 key,val = item 00126 keyVal = eval(key) 00127 exec('myparams[key] = keyVal') 00128 self.parameters[key] = keyVal 00129 if (keyVal == None): 00130 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00131 keyVal = eval(key) 00132 if(type(keyVal) == dict) : 00133 if len(keyVal) > 0 : 00134 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00135 else : 00136 exec('myparams[key] = ' + key + ' = {}') 00137 00138 else : 00139 async = self.parameters['async'] 00140 myparams['imagename'] = imagename = self.parameters['imagename'] 00141 myparams['template'] = template = self.parameters['template'] 00142 myparams['output'] = output = self.parameters['output'] 00143 myparams['asvelocity'] = asvelocity = self.parameters['asvelocity'] 00144 myparams['axes'] = axes = self.parameters['axes'] 00145 myparams['shape'] = shape = self.parameters['shape'] 00146 00147 if type(axes)==int: axes=[axes] 00148 if type(shape)==int: shape=[shape] 00149 00150 result = None 00151 00152 # 00153 # The following is work around to avoid a bug with current python translation 00154 # 00155 mytmp = {} 00156 00157 mytmp['imagename'] = imagename 00158 mytmp['template'] = template 00159 mytmp['output'] = output 00160 mytmp['asvelocity'] = asvelocity 00161 mytmp['axes'] = axes 00162 mytmp['shape'] = shape 00163 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00164 trec = casac.casac.utils().torecord(pathname+'imregrid.xml') 00165 00166 casalog.origin('imregrid') 00167 try : 00168 #if not trec.has_key('imregrid') or not casac.casac.utils().verify(mytmp, trec['imregrid']) : 00169 #return False 00170 00171 casac.casac.utils().verify(mytmp, trec['imregrid'], True) 00172 scriptstr=[''] 00173 saveinputs = self.__globals__['saveinputs'] 00174 saveinputs('imregrid', 'imregrid.last', myparams, self.__globals__,scriptstr=scriptstr) 00175 if async : 00176 count = 0 00177 keybase = time.strftime("%y%m%d.%H%M%S") 00178 key = keybase + "_" + str(count) 00179 while self.__async__.has_key(key) : 00180 count += 1 00181 key = keybase + "_" + str(count) 00182 result = tm.execute('imregrid', imagename, template, output, asvelocity, axes, shape) 00183 print "Use: " 00184 print " tm.retrieve(return_value) # to retrieve the status" 00185 print 00186 self.rkey = key 00187 self.__async__[key] = result 00188 else : 00189 tname = 'imregrid' 00190 spaces = ' '*(18-len(tname)) 00191 casalog.post('\n##########################################'+ 00192 '\n##### Begin Task: ' + tname + spaces + ' #####') 00193 casalog.post(scriptstr[1][1:]+'\n', 'INFO') 00194 result = imregrid(imagename, template, output, asvelocity, axes, shape) 00195 casalog.post('##### End Task: ' + tname + ' ' + spaces + ' #####'+ 00196 '\n##########################################') 00197 00198 except Exception, instance: 00199 if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) : 00200 raise 00201 else : 00202 #print '**** Error **** ',instance 00203 tname = 'imregrid' 00204 casalog.post('An error occurred running task '+tname+'.', 'ERROR') 00205 pass 00206 00207 gc.collect() 00208 return result 00209 # 00210 # 00211 # 00212 def paramgui(self, useGlobals=True, ipython_globals=None): 00213 """ 00214 Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00215 """ 00216 import paramgui 00217 if not hasattr(self, "__globals__") or self.__globals__ == None : 00218 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00219 00220 if useGlobals: 00221 if ipython_globals == None: 00222 myf=self.__globals__ 00223 else: 00224 myf=ipython_globals 00225 00226 paramgui.setGlobals(myf) 00227 else: 00228 paramgui.setGlobals({}) 00229 00230 paramgui.runTask('imregrid', myf['_ip']) 00231 paramgui.setGlobals({}) 00232 00233 # 00234 # 00235 # 00236 def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None): 00237 if not hasattr(self, "__globals__") or self.__globals__ == None : 00238 self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals 00239 if ipython_globals == None: 00240 myf=self.__globals__ 00241 else: 00242 myf=ipython_globals 00243 00244 a = odict() 00245 a['imagename'] = '' 00246 a['template'] = 'get' 00247 a['output'] = '' 00248 a['asvelocity'] = False 00249 a['axes'] = [-1] 00250 00251 a['async']=False 00252 a['template'] = { 00253 0:odict([{'notvalue':'get'}, {'shape':[-1]}])} 00254 00255 ### This function sets the default values but also will return the list of 00256 ### parameters or the default value of a given parameter 00257 if(param == None): 00258 myf['__set_default_parameters'](a) 00259 elif(param == 'paramkeys'): 00260 return a.keys() 00261 else: 00262 if(paramvalue==None and subparam==None): 00263 if(a.has_key(param)): 00264 return a[param] 00265 else: 00266 return self.itsdefault(param) 00267 else: 00268 retval=a[param] 00269 if(type(a[param])==dict): 00270 for k in range(len(a[param])): 00271 valornotval='value' 00272 if(a[param][k].has_key('notvalue')): 00273 valornotval='notvalue' 00274 if((a[param][k][valornotval])==paramvalue): 00275 retval=a[param][k].copy() 00276 retval.pop(valornotval) 00277 if(subparam != None): 00278 if(retval.has_key(subparam)): 00279 retval=retval[subparam] 00280 else: 00281 retval=self.itsdefault(subparam) 00282 else: 00283 retval=self.itsdefault(subparam) 00284 return retval 00285 00286 00287 # 00288 # 00289 def check_params(self, param=None, value=None, ipython_globals=None): 00290 if ipython_globals == None: 00291 myf=self.__globals__ 00292 else: 00293 myf=ipython_globals 00294 # print 'param:', param, 'value:', value 00295 try : 00296 if str(type(value)) != "<type 'instance'>" : 00297 value0 = value 00298 value = myf['cu'].expandparam(param, value) 00299 matchtype = False 00300 if(type(value) == numpy.ndarray): 00301 if(type(value) == type(value0)): 00302 myf[param] = value.tolist() 00303 else: 00304 #print 'value:', value, 'value0:', value0 00305 #print 'type(value):', type(value), 'type(value0):', type(value0) 00306 myf[param] = value0 00307 if type(value0) != list : 00308 matchtype = True 00309 else : 00310 myf[param] = value 00311 value = myf['cu'].verifyparam({param:value}) 00312 if matchtype: 00313 value = False 00314 except Exception, instance: 00315 #ignore the exception and just return it unchecked 00316 myf[param] = value 00317 return value 00318 # 00319 # 00320 def description(self, key='imregrid', subkey=None): 00321 desc={'imregrid': 'regrid an image onto a template image', 00322 'imagename': 'Name of the source image', 00323 'template': 'A dictionary, refcode, or name of an image that provides the output shape and coordinate system', 00324 'output': 'Name for the regridded image', 00325 'asvelocity': 'Regrid spectral axis in velocity space rather than frequency space?', 00326 'axes': 'The pixel axes to regrid. -1 => all.', 00327 'shape': 'Shape of the output image. Only used if template is an image. If not specified (-1), the output image shape is set equal to the input image shape.', 00328 00329 'async': 'If true the taskname must be started using imregrid(...)' 00330 } 00331 00332 # 00333 # Set subfields defaults if needed 00334 # 00335 00336 if(desc.has_key(key)) : 00337 return desc[key] 00338 00339 def itsdefault(self, paramname) : 00340 a = {} 00341 a['imagename'] = '' 00342 a['template'] = 'get' 00343 a['output'] = '' 00344 a['asvelocity'] = False 00345 a['axes'] = [-1] 00346 a['shape'] = [-1] 00347 00348 #a = sys._getframe(len(inspect.stack())-1).f_globals 00349 00350 if self.parameters['template'] != 'get': 00351 a['shape'] = [-1] 00352 00353 if a.has_key(paramname) : 00354 return a[paramname] 00355 imregrid_cli = imregrid_cli_()