casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
hanningsmooth_pg.py
Go to the documentation of this file.
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_hanningsmooth import hanningsmooth
00016 from task_hanningsmooth import casalog
00017 
00018 class hanningsmooth_pg_:
00019     __name__ = "hanningsmooth"
00020 
00021     def __init__(self) :
00022        self.__bases__ = (hanningsmooth_pg_,)
00023        self.__doc__ = self.__call__.__doc__
00024 
00025 
00026     def __call__(self, vis=None, datacolumn=None, outputvis=None, async=None):
00027 
00028         """Hanning smooth frequency channel data to remove Gibbs ringing
00029 
00030     This function Hanning smoothes the frequency channels with
00031     a weighted running average. The weights are 0.5 for the central 
00032     channel and 0.25 for each of the two adjacent channels. The first 
00033     and last channels are flagged.
00034     Inclusion of a flagged value in an average causes that data value
00035     to be flagged. 
00036     If an 'outputvis' filename is given, the task will copy the input file to the 
00037     output file name first, including all columns that are present in the input MS. 
00038     After that step it will smooth the column(s) as requested in the 'datacolumn' parameter. 
00039     Alternatively, if no 'outputvis' is specified, hanningsmooth will work directly on the 
00040     input visibility file. 
00041     If the 'CORRECTED' data column is requested for an MS that does not contain this column, 
00042     it will be filled from the 'DATA' column and then smoothed.
00043     
00044     WARNING: by default, all visibility columns will be smoothed. This will 
00045              modify the DATA column of the output MS in order to make sure that
00046              later gaincal will work on the smoothed data, e.g. as part of self-cal.
00047 
00048     Keyword arguments:
00049     vis -- Name of input visibility file (MS)
00050            default: none; example: vis='ngc5921.ms'
00051     datacolumn -- the name of the MS column to be Hanning smoothed
00052                   default='all'; example: datacolumn='corrected'
00053                   options: 'corrected', 'data', 'all'
00054     outputvis -- name of the output visibility file (MS)
00055                  default=none (write to the input MS); example: outputvis='ngc5921_src.ms'
00056 
00057     hanningsmooth(vis='ngc4852.ms', datacolumn='data', outputvis='ngc4852-hs.ms')                
00058 
00059 
00060  
00061         """
00062         a=inspect.stack()
00063         stacklevel=0
00064         for k in range(len(a)):
00065           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00066                 stacklevel=k
00067                 break
00068         myf=sys._getframe(stacklevel).f_globals
00069         myf['__last_task'] = 'hanningsmooth'
00070         myf['taskname'] = 'hanningsmooth'
00071         ###
00072         myf['update_params'](func=myf['taskname'],printtext=False)
00073         ###
00074         ###
00075         #Handle globals or user over-ride of arguments
00076         #
00077         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00078         useLocalDefaults = False
00079 
00080         for item in function_signature_defaults.iteritems():
00081                 key,val = item
00082                 keyVal = eval(key)
00083                 if (keyVal == None):
00084                         #user hasn't set it - use global/default
00085                         pass
00086                 else:
00087                         #user has set it - use over-ride
00088                         if (key != 'self') :
00089                            useLocalDefaults = True
00090                         #myf[key]=keyVal
00091 
00092         myparams = {}
00093         if useLocalDefaults :
00094            for item in function_signature_defaults.iteritems():
00095                key,val = item
00096                keyVal = eval(key)
00097                exec('myparams[key] = keyVal')
00098                if (keyVal == None):
00099                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00100                    keyVal = eval(key)
00101                    if(type(keyVal) == dict) :
00102                       exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00103 
00104         else :
00105             uselessvariable = None 
00106             myparams['vis'] = vis = myf['vis']
00107             myparams['datacolumn'] = datacolumn = myf['datacolumn']
00108             myparams['outputvis'] = outputvis = myf['outputvis']
00109 
00110 
00111         result = None
00112 
00113 #
00114 #    The following is work around to avoid a bug with current python translation
00115 #
00116         mytmp = {}
00117 
00118         mytmp['vis'] = vis
00119         mytmp['datacolumn'] = datacolumn
00120         mytmp['outputvis'] = outputvis
00121         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00122         trec = casac.utils().torecord(pathname+'hanningsmooth.xml')
00123 
00124         casalog.origin('hanningsmooth')
00125         if not trec.has_key('hanningsmooth') or not casac.utils().verify(mytmp, trec['hanningsmooth']) :
00126             return False
00127 
00128 
00129         try :
00130           casalog.post('')
00131           casalog.post('##########################################')
00132           casalog.post('##### Begin Task: hanningsmooth           #####')
00133           casalog.post('')
00134           result = hanningsmooth(vis, datacolumn, outputvis)
00135           casalog.post('')
00136           casalog.post('##### End Task: hanningsmooth           #####')
00137           casalog.post('##########################################')
00138 
00139 
00140 # saveinputs for individule engine has no use
00141 # saveinputs should alos be removed from casa_in_py.py
00142 #
00143 #
00144 #          saveinputs = myf['saveinputs']
00145 #          saveinputs('hanningsmooth', 'hanningsmooth.last', myparams)
00146 #
00147 #
00148         except Exception, instance:
00149           #print '**** Error **** ',instance
00150           pass
00151 
00152         gc.collect()
00153         return result
00154 #
00155 #
00156 ##
00157 #    def paramgui(self, useGlobals=True):
00158 #        """
00159 #        Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00160 #        """
00161 #        import paramgui
00162 #
00163 #        a=inspect.stack()
00164 #        stacklevel=0
00165 #        for k in range(len(a)):
00166 #          if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00167 #            stacklevel=k
00168 #            break
00169 #        myf = sys._getframe(stacklevel).f_globals
00170 #
00171 #        if useGlobals:
00172 #            paramgui.setGlobals(myf)
00173 #        else:
00174 #            paramgui.setGlobals({})
00175 #
00176 #        paramgui.runTask('hanningsmooth', myf['_ip'])
00177 #        paramgui.setGlobals({})
00178 #
00179 #
00180 #
00181 #
00182     def defaults(self, param=None):
00183         a=inspect.stack()
00184         stacklevel=0
00185         for k in range(len(a)):
00186           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00187                 stacklevel=k
00188                 break
00189         myf=sys._getframe(stacklevel).f_globals
00190         a = odict()
00191         a['vis']  = ''
00192         a['datacolumn']  = 'all'
00193         a['outputvis']  = ''
00194 
00195         a['async']=False
00196 
00197 ### This function sets the default values but also will return the list of
00198 ### parameters or the default value of a given parameter
00199         if(param == None):
00200                 myf['__set_default_parameters'](a)
00201         elif(param == 'paramkeys'):
00202                 return a.keys()
00203         else:
00204                 if(a.has_key(param)):
00205                    #if(type(a[param]) == dict) :
00206                    #   return a[param][len(a[param])-1]['value']
00207                    #else :
00208                       return a[param]
00209 
00210 
00211 #
00212 #
00213     def check_params(self, param=None, value=None):
00214       a=inspect.stack() 
00215       stacklevel=0
00216       for k in range(len(a)):
00217         if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00218             stacklevel=k
00219             break
00220       myf=sys._getframe(stacklevel).f_globals
00221 
00222 #      print 'param:', param, 'value:', value
00223       try :
00224          if str(type(value)) != "<type 'instance'>" :
00225             value0 = value
00226             value = myf['cu'].expandparam(param, value)
00227             matchtype = False
00228             if(type(value) == numpy.ndarray):
00229                if(type(value) == type(value0)):
00230                   myf[param] = value.tolist()
00231                else:
00232                   #print 'value:', value, 'value0:', value0
00233                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00234                   myf[param] = value0
00235                   if type(value0) != list :
00236                      matchtype = True
00237             else :
00238                myf[param] = value
00239             value = myf['cu'].verifyparam({param:value})
00240             if matchtype:
00241                value = False
00242       except Exception, instance:
00243          #ignore the exception and just return it unchecked
00244          myf[param] = value
00245       return value
00246 
00247 #
00248 #
00249     def description(self, key='hanningsmooth', subkey=None):
00250         desc={'hanningsmooth': 'Hanning smooth frequency channel data to remove Gibbs ringing',
00251                'vis': 'Name of input visibility file (MS)',
00252                'datacolumn': 'the name of the MS column into which to write the smoothed data',
00253                'outputvis': 'name of the output visibility file (MS)',
00254 
00255                'async': 'If true the taskname must be started using hanningsmooth(...)'
00256               }
00257 
00258         if(desc.has_key(key)) :
00259            return desc[key]
00260 
00261     def itsdefault(self, paramname) :
00262         a = {}
00263         a['vis']  = ''
00264         a['datacolumn']  = 'all'
00265         a['outputvis']  = ''
00266 
00267         if a.has_key(paramname) :
00268               return a[paramname]
00269 hanningsmooth_pg = hanningsmooth_pg_()