casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
sdflagmanager_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_sdflagmanager import sdflagmanager
00016 from task_sdflagmanager import casalog
00017 
00018 class sdflagmanager_pg_:
00019     __name__ = "sdflagmanager"
00020 
00021     def __init__(self) :
00022        self.__bases__ = (sdflagmanager_pg_,)
00023        self.__doc__ = self.__call__.__doc__
00024 
00025 
00026     def __call__(self, infile=None, mode=None, versionname=None, oldname=None, comment=None, merge=None, async=None):
00027 
00028         """ASAP SD task: enable list, save, restore, delete and rename flag version files
00029 
00030 
00031         These flag version files are copies of the flag column for a
00032         measurement set.  They can be restored to the data set to obtain
00033         a previous flag version.  It is wise to
00034         save a flagversion at the beginning or after serious editing.    
00035 
00036         Keyword arguments:
00037         infile -- Name of input SD dataset
00038                 default: ''. example: infile='ngc5921.asap'
00039         mode -- Flag version operation
00040                 default: 'list'; to list existing flagtables
00041                 'save' will save flag column from infile to a specified flag file
00042                 'restore' will place the specified flag file into infile
00043                 'delete' will delete specified flag file
00044                 'rename' will rename a specified flag file
00045 
00046             >>> mode expandable parameters             
00047                  versionname -- Flag version name
00048                          default: none; example: versionname='original_data'
00049                          No imbedded blanks in the versionname
00050                  comment -- Short description of a versionname, when mode is 'save' 
00051                             or 'rename'
00052                          default: ''; example: comment='Clip above 1.85'
00053                          comment = versionname
00054                  oldname -- When mode='rename', the flag file to rename
00055                  merge -- Merge operation
00056                          Options: 'or','and', but not recommended for now.
00057 
00058  
00059         """
00060         a=inspect.stack()
00061         stacklevel=0
00062         for k in range(len(a)):
00063           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00064                 stacklevel=k
00065                 break
00066         myf=sys._getframe(stacklevel).f_globals
00067         myf['__last_task'] = 'sdflagmanager'
00068         myf['taskname'] = 'sdflagmanager'
00069         ###
00070         myf['update_params'](func=myf['taskname'],printtext=False)
00071         ###
00072         ###
00073         #Handle globals or user over-ride of arguments
00074         #
00075         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00076         useLocalDefaults = False
00077 
00078         for item in function_signature_defaults.iteritems():
00079                 key,val = item
00080                 keyVal = eval(key)
00081                 if (keyVal == None):
00082                         #user hasn't set it - use global/default
00083                         pass
00084                 else:
00085                         #user has set it - use over-ride
00086                         if (key != 'self') :
00087                            useLocalDefaults = True
00088                         #myf[key]=keyVal
00089 
00090         myparams = {}
00091         if useLocalDefaults :
00092            for item in function_signature_defaults.iteritems():
00093                key,val = item
00094                keyVal = eval(key)
00095                exec('myparams[key] = keyVal')
00096                if (keyVal == None):
00097                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00098                    keyVal = eval(key)
00099                    if(type(keyVal) == dict) :
00100                       exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00101 
00102         else :
00103             uselessvariable = None 
00104             myparams['infile'] = infile = myf['infile']
00105             myparams['mode'] = mode = myf['mode']
00106             myparams['versionname'] = versionname = myf['versionname']
00107             myparams['oldname'] = oldname = myf['oldname']
00108             myparams['comment'] = comment = myf['comment']
00109             myparams['merge'] = merge = myf['merge']
00110 
00111 
00112         result = None
00113 
00114 #
00115 #    The following is work around to avoid a bug with current python translation
00116 #
00117         mytmp = {}
00118 
00119         mytmp['infile'] = infile
00120         mytmp['mode'] = mode
00121         mytmp['versionname'] = versionname
00122         mytmp['oldname'] = oldname
00123         mytmp['comment'] = comment
00124         mytmp['merge'] = merge
00125         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00126         trec = casac.utils().torecord(pathname+'sdflagmanager.xml')
00127 
00128         casalog.origin('sdflagmanager')
00129         if not trec.has_key('sdflagmanager') or not casac.utils().verify(mytmp, trec['sdflagmanager']) :
00130             return False
00131 
00132 
00133         try :
00134           casalog.post('')
00135           casalog.post('##########################################')
00136           casalog.post('##### Begin Task: sdflagmanager           #####')
00137           casalog.post('')
00138           result = sdflagmanager(infile, mode, versionname, oldname, comment, merge)
00139           casalog.post('')
00140           casalog.post('##### End Task: sdflagmanager           #####')
00141           casalog.post('##########################################')
00142 
00143 
00144 # saveinputs for individule engine has no use
00145 # saveinputs should alos be removed from casa_in_py.py
00146 #
00147 #
00148 #          saveinputs = myf['saveinputs']
00149 #          saveinputs('sdflagmanager', 'sdflagmanager.last', myparams)
00150 #
00151 #
00152         except Exception, instance:
00153           #print '**** Error **** ',instance
00154           pass
00155 
00156         gc.collect()
00157         return result
00158 #
00159 #
00160 ##
00161 #    def paramgui(self, useGlobals=True):
00162 #        """
00163 #        Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00164 #        """
00165 #        import paramgui
00166 #
00167 #        a=inspect.stack()
00168 #        stacklevel=0
00169 #        for k in range(len(a)):
00170 #          if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00171 #            stacklevel=k
00172 #            break
00173 #        myf = sys._getframe(stacklevel).f_globals
00174 #
00175 #        if useGlobals:
00176 #            paramgui.setGlobals(myf)
00177 #        else:
00178 #            paramgui.setGlobals({})
00179 #
00180 #        paramgui.runTask('sdflagmanager', myf['_ip'])
00181 #        paramgui.setGlobals({})
00182 #
00183 #
00184 #
00185 #
00186     def defaults(self, param=None):
00187         a=inspect.stack()
00188         stacklevel=0
00189         for k in range(len(a)):
00190           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00191                 stacklevel=k
00192                 break
00193         myf=sys._getframe(stacklevel).f_globals
00194         a = odict()
00195         a['infile']  = ''
00196         a['mode']  = 'list'
00197 
00198         a['async']=False
00199         a['mode'] = {
00200                     0:{'value':'list'}, 
00201                     1:odict([{'value':'save'}, {'versionname':''}, {'comment':''}, {'merge':'replace'}]), 
00202                     2:odict([{'value':'restore'}, {'versionname':''}, {'merge':'replace'}]), 
00203                     3:odict([{'value':'delete'}, {'versionname':''}]), 
00204                     4:odict([{'value':'rename'}, {'oldname':''}, {'versionname':''}, {'comment':''}])}
00205 
00206 ### This function sets the default values but also will return the list of
00207 ### parameters or the default value of a given parameter
00208         if(param == None):
00209                 myf['__set_default_parameters'](a)
00210         elif(param == 'paramkeys'):
00211                 return a.keys()
00212         else:
00213                 if(a.has_key(param)):
00214                    #if(type(a[param]) == dict) :
00215                    #   return a[param][len(a[param])-1]['value']
00216                    #else :
00217                       return a[param]
00218 
00219 
00220 #
00221 #
00222     def check_params(self, param=None, value=None):
00223       a=inspect.stack() 
00224       stacklevel=0
00225       for k in range(len(a)):
00226         if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00227             stacklevel=k
00228             break
00229       myf=sys._getframe(stacklevel).f_globals
00230 
00231 #      print 'param:', param, 'value:', value
00232       try :
00233          if str(type(value)) != "<type 'instance'>" :
00234             value0 = value
00235             value = myf['cu'].expandparam(param, value)
00236             matchtype = False
00237             if(type(value) == numpy.ndarray):
00238                if(type(value) == type(value0)):
00239                   myf[param] = value.tolist()
00240                else:
00241                   #print 'value:', value, 'value0:', value0
00242                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00243                   myf[param] = value0
00244                   if type(value0) != list :
00245                      matchtype = True
00246             else :
00247                myf[param] = value
00248             value = myf['cu'].verifyparam({param:value})
00249             if matchtype:
00250                value = False
00251       except Exception, instance:
00252          #ignore the exception and just return it unchecked
00253          myf[param] = value
00254       return value
00255 
00256 #
00257 #
00258     def description(self, key='sdflagmanager', subkey=None):
00259         desc={'sdflagmanager': 'ASAP SD task: enable list, save, restore, delete and rename flag version files',
00260                'infile': 'Name of input SD dataset (ASAP scantable)',
00261                'mode': 'Operation: list, save, restore, delete, rename',
00262                'versionname': 'Flag version name',
00263                'oldname': 'Flag version to rename',
00264                'comment': 'Short description of a versionname',
00265                'merge': 'Merge option: replace will save or over-write the flags',
00266 
00267                'async': 'If true the taskname must be started using sdflagmanager(...)'
00268               }
00269 
00270 #
00271 # Set subfields defaults if needed
00272 #
00273 
00274         if(desc.has_key(key)) :
00275            return desc[key]
00276 
00277     def itsdefault(self, paramname) :
00278         a = {}
00279         a['infile']  = ''
00280         a['mode']  = 'list'
00281         a['versionname']  = ''
00282         a['oldname']  = ''
00283         a['comment']  = ''
00284         a['merge']  = 'replace'
00285 
00286         if a.has_key(paramname) :
00287               return a[paramname]
00288 sdflagmanager_pg = sdflagmanager_pg_()