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 string 00010 import time 00011 import inspect 00012 import gc 00013 import numpy 00014 from odict import odict 00015 from task_imregrid import imregrid 00016 from task_imregrid import casalog 00017 00018 class imregrid_pg_: 00019 __name__ = "imregrid" 00020 00021 def __init__(self) : 00022 self.__bases__ = (imregrid_pg_,) 00023 self.__doc__ = self.__call__.__doc__ 00024 00025 00026 def __call__(self, imagename=None, template=None, output=None, asvelocity=None, axes=None, shape=None, async=None): 00027 00028 """regrid an image onto a template image 00029 00030 imregrid: regrid an image to a new coordinate system. 00031 00032 Keyword arguments: 00033 imagename Name of the image that needs to be regridded 00034 default: none; example: imagename='orion.image' 00035 template Dictionary, reference code, or imagename defining the new 00036 shape and coordinate system, or 'get' to return the template 00037 dictionary for imagename. Recognized reference codes are: 00038 'J2000', 'B1950', 'B1950_VLA', 'GALACTIC', 'HADEC', 'AZEL', 00039 'AZELSW', 'AZELNE', 'ECLIPTIC', 'MECLIPTIC', 'TECLIPTIC', 00040 and 'SUPERGAL'. 00041 default: 'get'; example: template='orion_j2000.im' 00042 shape Shape of the output image. Only used if template is an image. 00043 If not specified (-1), the output image shape is set equal to 00044 the input image shape. 00045 output Name for the regridded image 00046 default: imagename + '.regridded'; example: imagename='orion_shifted.im' 00047 asvelocity Regrid spectral axis with respect to velocity, not frequency? 00048 default: False 00049 axes The pixel axes to regrid. -1 => all. 00050 async Run task in a separate process (return CASA prompt) 00051 default: False; example: async=True 00052 00053 The new coordinate system is defined by the template parameter, 00054 which can be: 00055 * a recognized reference code string (see below), 00056 * a {'csys': csys_record, 'shap': shape} dictionary, 00057 * 'get', which does not regrid but returns the template dictionary 00058 for imagename, suitable for modification and reuse, or 00059 * the name of an image to get the coordinate system and shape 00060 from. The input and template images must have the same 00061 coordinate structure. 00062 00063 If one of the axes to be regridded is a spectral axis and asvelocity=T, 00064 the axis will be regridded to match the velocity, not the frequency, 00065 description of the template coordinate system. Thus the output pixel 00066 values will correspond only to the velocity, not the frequency, of the 00067 output axis. 00068 00069 If an image has per-plane beams and one attempts to regrid the spectral axis, 00070 an exception is thrown. 00071 00072 00073 00074 """ 00075 a=inspect.stack() 00076 stacklevel=0 00077 for k in range(len(a)): 00078 if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0): 00079 stacklevel=k 00080 break 00081 myf=sys._getframe(stacklevel).f_globals 00082 myf['__last_task'] = 'imregrid' 00083 myf['taskname'] = 'imregrid' 00084 ### 00085 myf['update_params'](func=myf['taskname'],printtext=False) 00086 ### 00087 ### 00088 #Handle globals or user over-ride of arguments 00089 # 00090 function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults)) 00091 useLocalDefaults = False 00092 00093 for item in function_signature_defaults.iteritems(): 00094 key,val = item 00095 keyVal = eval(key) 00096 if (keyVal == None): 00097 #user hasn't set it - use global/default 00098 pass 00099 else: 00100 #user has set it - use over-ride 00101 if (key != 'self') : 00102 useLocalDefaults = True 00103 #myf[key]=keyVal 00104 00105 myparams = {} 00106 if useLocalDefaults : 00107 for item in function_signature_defaults.iteritems(): 00108 key,val = item 00109 keyVal = eval(key) 00110 exec('myparams[key] = keyVal') 00111 if (keyVal == None): 00112 exec('myparams[key] = '+ key + ' = self.itsdefault(key)') 00113 keyVal = eval(key) 00114 if(type(keyVal) == dict) : 00115 exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']') 00116 00117 else : 00118 uselessvariable = None 00119 myparams['imagename'] = imagename = myf['imagename'] 00120 myparams['template'] = template = myf['template'] 00121 myparams['output'] = output = myf['output'] 00122 myparams['asvelocity'] = asvelocity = myf['asvelocity'] 00123 myparams['axes'] = axes = myf['axes'] 00124 myparams['shape'] = shape = myf['shape'] 00125 00126 if type(axes)==int: axes=[axes] 00127 if type(shape)==int: shape=[shape] 00128 00129 result = None 00130 00131 # 00132 # The following is work around to avoid a bug with current python translation 00133 # 00134 mytmp = {} 00135 00136 mytmp['imagename'] = imagename 00137 mytmp['template'] = template 00138 mytmp['output'] = output 00139 mytmp['asvelocity'] = asvelocity 00140 mytmp['axes'] = axes 00141 mytmp['shape'] = shape 00142 pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/' 00143 trec = casac.utils().torecord(pathname+'imregrid.xml') 00144 00145 casalog.origin('imregrid') 00146 if not trec.has_key('imregrid') or not casac.utils().verify(mytmp, trec['imregrid']) : 00147 return False 00148 00149 00150 try : 00151 casalog.post('') 00152 casalog.post('##########################################') 00153 casalog.post('##### Begin Task: imregrid #####') 00154 casalog.post('') 00155 result = imregrid(imagename, template, output, asvelocity, axes, shape) 00156 casalog.post('') 00157 casalog.post('##### End Task: imregrid #####') 00158 casalog.post('##########################################') 00159 00160 00161 # saveinputs for individule engine has no use 00162 # saveinputs should alos be removed from casa_in_py.py 00163 # 00164 # 00165 # saveinputs = myf['saveinputs'] 00166 # saveinputs('imregrid', 'imregrid.last', myparams) 00167 # 00168 # 00169 except Exception, instance: 00170 #print '**** Error **** ',instance 00171 pass 00172 00173 gc.collect() 00174 return result 00175 # 00176 # 00177 ## 00178 # def paramgui(self, useGlobals=True): 00179 # """ 00180 # Opens a parameter GUI for this task. If useGlobals is true, then any relevant global parameter settings are used. 00181 # """ 00182 # import paramgui 00183 # 00184 # a=inspect.stack() 00185 # stacklevel=0 00186 # for k in range(len(a)): 00187 # if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0): 00188 # stacklevel=k 00189 # break 00190 # myf = sys._getframe(stacklevel).f_globals 00191 # 00192 # if useGlobals: 00193 # paramgui.setGlobals(myf) 00194 # else: 00195 # paramgui.setGlobals({}) 00196 # 00197 # paramgui.runTask('imregrid', myf['_ip']) 00198 # paramgui.setGlobals({}) 00199 # 00200 # 00201 # 00202 # 00203 def defaults(self, param=None): 00204 a=inspect.stack() 00205 stacklevel=0 00206 for k in range(len(a)): 00207 if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0): 00208 stacklevel=k 00209 break 00210 myf=sys._getframe(stacklevel).f_globals 00211 a = odict() 00212 a['imagename'] = '' 00213 a['template'] = 'get' 00214 a['output'] = '' 00215 a['asvelocity'] = False 00216 a['axes'] = [-1] 00217 00218 a['async']=False 00219 a['template'] = { 00220 0:odict([{'notvalue':'get'}, {'shape':[-1]}])} 00221 00222 ### This function sets the default values but also will return the list of 00223 ### parameters or the default value of a given parameter 00224 if(param == None): 00225 myf['__set_default_parameters'](a) 00226 elif(param == 'paramkeys'): 00227 return a.keys() 00228 else: 00229 if(a.has_key(param)): 00230 #if(type(a[param]) == dict) : 00231 # return a[param][len(a[param])-1]['value'] 00232 #else : 00233 return a[param] 00234 00235 00236 # 00237 # 00238 def check_params(self, param=None, value=None): 00239 a=inspect.stack() 00240 stacklevel=0 00241 for k in range(len(a)): 00242 if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0): 00243 stacklevel=k 00244 break 00245 myf=sys._getframe(stacklevel).f_globals 00246 00247 # print 'param:', param, 'value:', value 00248 try : 00249 if str(type(value)) != "<type 'instance'>" : 00250 value0 = value 00251 value = myf['cu'].expandparam(param, value) 00252 matchtype = False 00253 if(type(value) == numpy.ndarray): 00254 if(type(value) == type(value0)): 00255 myf[param] = value.tolist() 00256 else: 00257 #print 'value:', value, 'value0:', value0 00258 #print 'type(value):', type(value), 'type(value0):', type(value0) 00259 myf[param] = value0 00260 if type(value0) != list : 00261 matchtype = True 00262 else : 00263 myf[param] = value 00264 value = myf['cu'].verifyparam({param:value}) 00265 if matchtype: 00266 value = False 00267 except Exception, instance: 00268 #ignore the exception and just return it unchecked 00269 myf[param] = value 00270 return value 00271 00272 # 00273 # 00274 def description(self, key='imregrid', subkey=None): 00275 desc={'imregrid': 'regrid an image onto a template image', 00276 'imagename': 'Name of the source image', 00277 'template': 'A dictionary, refcode, or name of an image that provides the output shape and coordinate system', 00278 'output': 'Name for the regridded image', 00279 'asvelocity': 'Regrid spectral axis in velocity space rather than frequency space?', 00280 'axes': 'The pixel axes to regrid. -1 => all.', 00281 '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.', 00282 00283 'async': 'If true the taskname must be started using imregrid(...)' 00284 } 00285 00286 # 00287 # Set subfields defaults if needed 00288 # 00289 00290 if(desc.has_key(key)) : 00291 return desc[key] 00292 00293 def itsdefault(self, paramname) : 00294 a = {} 00295 a['imagename'] = '' 00296 a['template'] = 'get' 00297 a['output'] = '' 00298 a['asvelocity'] = False 00299 a['axes'] = [-1] 00300 a['shape'] = [-1] 00301 00302 if a.has_key(paramname) : 00303 return a[paramname] 00304 imregrid_pg = imregrid_pg_()