casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
smoothcal_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_smoothcal import smoothcal
00016 from task_smoothcal import casalog
00017 
00018 class smoothcal_pg_:
00019     __name__ = "smoothcal"
00020 
00021     def __init__(self) :
00022        self.__bases__ = (smoothcal_pg_,)
00023        self.__doc__ = self.__call__.__doc__
00024 
00025 
00026     def __call__(self, vis=None, tablein=None, caltable=None, field=None, smoothtype=None, smoothtime=None, async=None):
00027 
00028         """Smooth calibration solution(s) derived from one or more sources:
00029 
00030 
00031         A G- or T-type gain calibration can be smoothed.  The amplitude and
00032         phase smoothing times are currently the same.  Calibration values
00033         will be smoothed over all fields designed.
00034 
00035         Keyword arguments:
00036         vis -- Name of input visibility file
00037                 default: none; example: vis='ngc5921.ms'
00038         tablein -- Input calibration table (G or T)
00039                 default: none; example: tablein='ngc5921.gcal'
00040         caltable -- Output calibration table (smoothed)
00041                 default: none; example: caltable='ngc5921_smooth.gcal'
00042         field -- subset of fields to select and smooth
00043                 default: '' means all; example: field='0319_415_1,3C286'
00044         smoothtype -- The smoothing filter to be used for both amp and phase
00045                 default: 'median'; example: smoothtype='mean'
00046                 Options: 'median','mean'
00047         smoothtime -- Smoothing filter time (sec)
00048                 default: 300.0; example: smoothtime=60.
00049  
00050         """
00051         a=inspect.stack()
00052         stacklevel=0
00053         for k in range(len(a)):
00054           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00055                 stacklevel=k
00056                 break
00057         myf=sys._getframe(stacklevel).f_globals
00058         myf['__last_task'] = 'smoothcal'
00059         myf['taskname'] = 'smoothcal'
00060         ###
00061         myf['update_params'](func=myf['taskname'],printtext=False)
00062         ###
00063         ###
00064         #Handle globals or user over-ride of arguments
00065         #
00066         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00067         useLocalDefaults = False
00068 
00069         for item in function_signature_defaults.iteritems():
00070                 key,val = item
00071                 keyVal = eval(key)
00072                 if (keyVal == None):
00073                         #user hasn't set it - use global/default
00074                         pass
00075                 else:
00076                         #user has set it - use over-ride
00077                         if (key != 'self') :
00078                            useLocalDefaults = True
00079                         #myf[key]=keyVal
00080 
00081         myparams = {}
00082         if useLocalDefaults :
00083            for item in function_signature_defaults.iteritems():
00084                key,val = item
00085                keyVal = eval(key)
00086                exec('myparams[key] = keyVal')
00087                if (keyVal == None):
00088                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00089                    keyVal = eval(key)
00090                    if(type(keyVal) == dict) :
00091                       exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00092 
00093         else :
00094             uselessvariable = None 
00095             myparams['vis'] = vis = myf['vis']
00096             myparams['tablein'] = tablein = myf['tablein']
00097             myparams['caltable'] = caltable = myf['caltable']
00098             myparams['field'] = field = myf['field']
00099             myparams['smoothtype'] = smoothtype = myf['smoothtype']
00100             myparams['smoothtime'] = smoothtime = myf['smoothtime']
00101 
00102         if type(field)==str: field=[field]
00103 
00104         result = None
00105 
00106 #
00107 #    The following is work around to avoid a bug with current python translation
00108 #
00109         mytmp = {}
00110 
00111         mytmp['vis'] = vis
00112         mytmp['tablein'] = tablein
00113         mytmp['caltable'] = caltable
00114         mytmp['field'] = field
00115         mytmp['smoothtype'] = smoothtype
00116         mytmp['smoothtime'] = smoothtime
00117         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00118         trec = casac.utils().torecord(pathname+'smoothcal.xml')
00119 
00120         casalog.origin('smoothcal')
00121         if not trec.has_key('smoothcal') or not casac.utils().verify(mytmp, trec['smoothcal']) :
00122             return False
00123 
00124 
00125         try :
00126           casalog.post('')
00127           casalog.post('##########################################')
00128           casalog.post('##### Begin Task: smoothcal           #####')
00129           casalog.post('')
00130           result = smoothcal(vis, tablein, caltable, field, smoothtype, smoothtime)
00131           casalog.post('')
00132           casalog.post('##### End Task: smoothcal           #####')
00133           casalog.post('##########################################')
00134 
00135 
00136 # saveinputs for individule engine has no use
00137 # saveinputs should alos be removed from casa_in_py.py
00138 #
00139 #
00140 #          saveinputs = myf['saveinputs']
00141 #          saveinputs('smoothcal', 'smoothcal.last', myparams)
00142 #
00143 #
00144         except Exception, instance:
00145           #print '**** Error **** ',instance
00146           pass
00147 
00148         gc.collect()
00149         return result
00150 #
00151 #
00152 ##
00153 #    def paramgui(self, useGlobals=True):
00154 #        """
00155 #        Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00156 #        """
00157 #        import paramgui
00158 #
00159 #        a=inspect.stack()
00160 #        stacklevel=0
00161 #        for k in range(len(a)):
00162 #          if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00163 #            stacklevel=k
00164 #            break
00165 #        myf = sys._getframe(stacklevel).f_globals
00166 #
00167 #        if useGlobals:
00168 #            paramgui.setGlobals(myf)
00169 #        else:
00170 #            paramgui.setGlobals({})
00171 #
00172 #        paramgui.runTask('smoothcal', myf['_ip'])
00173 #        paramgui.setGlobals({})
00174 #
00175 #
00176 #
00177 #
00178     def defaults(self, param=None):
00179         a=inspect.stack()
00180         stacklevel=0
00181         for k in range(len(a)):
00182           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00183                 stacklevel=k
00184                 break
00185         myf=sys._getframe(stacklevel).f_globals
00186         a = odict()
00187         a['vis']  = ''
00188         a['tablein']  = ''
00189         a['caltable']  = ''
00190         a['field']  = ['']
00191         a['smoothtype']  = 'median'
00192         a['smoothtime']  = 60.0
00193 
00194         a['async']=False
00195 
00196 ### This function sets the default values but also will return the list of
00197 ### parameters or the default value of a given parameter
00198         if(param == None):
00199                 myf['__set_default_parameters'](a)
00200         elif(param == 'paramkeys'):
00201                 return a.keys()
00202         else:
00203                 if(a.has_key(param)):
00204                    #if(type(a[param]) == dict) :
00205                    #   return a[param][len(a[param])-1]['value']
00206                    #else :
00207                       return a[param]
00208 
00209 
00210 #
00211 #
00212     def check_params(self, param=None, value=None):
00213       a=inspect.stack() 
00214       stacklevel=0
00215       for k in range(len(a)):
00216         if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00217             stacklevel=k
00218             break
00219       myf=sys._getframe(stacklevel).f_globals
00220 
00221 #      print 'param:', param, 'value:', value
00222       try :
00223          if str(type(value)) != "<type 'instance'>" :
00224             value0 = value
00225             value = myf['cu'].expandparam(param, value)
00226             matchtype = False
00227             if(type(value) == numpy.ndarray):
00228                if(type(value) == type(value0)):
00229                   myf[param] = value.tolist()
00230                else:
00231                   #print 'value:', value, 'value0:', value0
00232                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00233                   myf[param] = value0
00234                   if type(value0) != list :
00235                      matchtype = True
00236             else :
00237                myf[param] = value
00238             value = myf['cu'].verifyparam({param:value})
00239             if matchtype:
00240                value = False
00241       except Exception, instance:
00242          #ignore the exception and just return it unchecked
00243          myf[param] = value
00244       return value
00245 
00246 #
00247 #
00248     def description(self, key='smoothcal', subkey=None):
00249         desc={'smoothcal': 'Smooth calibration solution(s) derived from one or more sources:',
00250                'vis': 'Name of input visibility file (MS)',
00251                'tablein': 'Input calibration table',
00252                'caltable': 'Output calibration table',
00253                'field': 'Field name list',
00254                'smoothtype': 'Smoothing filter to use',
00255                'smoothtime': 'Smoothing time (sec)',
00256 
00257                'async': 'If true the taskname must be started using smoothcal(...)'
00258               }
00259 
00260         if(desc.has_key(key)) :
00261            return desc[key]
00262 
00263     def itsdefault(self, paramname) :
00264         a = {}
00265         a['vis']  = ''
00266         a['tablein']  = ''
00267         a['caltable']  = ''
00268         a['field']  = ['']
00269         a['smoothtype']  = 'median'
00270         a['smoothtime']  = 60.0
00271 
00272         if a.has_key(paramname) :
00273               return a[paramname]
00274 smoothcal_pg = smoothcal_pg_()