casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
vishead_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_vishead import vishead
00016 from task_vishead import casalog
00017 
00018 class vishead_pg_:
00019     __name__ = "vishead"
00020 
00021     def __init__(self) :
00022        self.__bases__ = (vishead_pg_,)
00023        self.__doc__ = self.__call__.__doc__
00024 
00025 
00026     def __call__(self, vis=None, mode=None, listitems=None, hdkey=None, hdindex=None, hdvalue=None, async=None):
00027 
00028         """List, summary, get, and put metadata in a measurement set
00029 
00030         This task allows the user to manipulate some meta-data parameters in a
00031         measurement set.  The mode='list' shows those keywords that are
00032         presently implemented, with their values.  The contents associated
00033         with the keywords can be obtained (get) and changed (put).
00034 
00035         The modes that are available are:
00036 
00037            list    --- List all keywords that are recognized, and list the
00038                        value(s) for each.  Only these keywords can be obtained
00039                        (get) or changed (put) 
00040            summary --- Equivalent to running taskname='listobs'; verbose=F
00041            get     --- Get the specified keyword value(s) from the ms
00042            put     --- Put the specified keyword value(s) into the ms
00043 
00044         Parameters currently implemented are (June 1, 2009):
00045 
00046            cal_grp              
00047            field                 Field names
00048            fld_code              Field Observing codes
00049            freq_group_name       
00050            log                   
00051            observer              Observer name
00052            project               Project name
00053            ptcs                  Phase tracking centers for each field
00054            release_date          
00055            schedule
00056            schedule_type
00057            spw_name              Spectral parameters?
00058            source_name           Source Names (=Field Names?)
00059            telescope             Telescope Name
00060 
00061         Keyword arguments:
00062 
00063         vis  --- Name of input visibility file
00064                   default: none, example: vis='my.ms'
00065 
00066         mode --- Mode of operation for vishead
00067                   default = 'list'; example: mode='get'
00068 
00069         hdkey--- keyword to get or put from the ms (used in get/put mode only)
00070                   ex: hdkey='telescope'
00071 
00072         hdindex--- index (counting from 0) if keyword is an array (used in get/put
00073                   mode only)
00074                   ex: hdindex='2'; hdindex=''->put/get full array; 
00075 
00076         hdvalue   --- value to be put in the MS (used in put mode only)
00077                   ex: hdvalue=array(['MyTelescope'])
00078 
00079 
00080 
00081 
00082         Examples:
00083 
00084            To transfer the parameters to useful python items requires some care.
00085 
00086            taskname = 'vishead'
00087            default() 
00088            vis = '3C84C.ms'
00089            mode = 'get'
00090                       
00091            to get a field name (string),
00092               hdkey = 'field'; hdindex = '2'; hdvalue=vishead(); 
00093                        print hdvalue[0] = the name for field='2'                    
00094                 
00095            to get an phase center (number)
00096               hdkey = 'ptcs'; hdindex = '1'; hdvalue = vishead();
00097                        hdvalue[0][0] gives the ra, hdvalue[0][1] gives the dec in field '1'
00098 
00099 
00100            taskname = 'vishead'
00101            default() 
00102            vis = '3C84C.ms'
00103            mode = 'put'
00104 
00105           To change a string,
00106 
00107               hdkey = 'field'; hdindex = '2'; hdvalue = 'junk'; vishead()
00108                    field='2' is renamed 'junk'
00109 
00110           To change a number, (egs. ra of field=1 to 0.5 radian)
00111               is too complicated to figure out!
00112              
00113 
00114 
00115         """
00116         a=inspect.stack()
00117         stacklevel=0
00118         for k in range(len(a)):
00119           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00120                 stacklevel=k
00121                 break
00122         myf=sys._getframe(stacklevel).f_globals
00123         myf['__last_task'] = 'vishead'
00124         myf['taskname'] = 'vishead'
00125         ###
00126         myf['update_params'](func=myf['taskname'],printtext=False)
00127         ###
00128         ###
00129         #Handle globals or user over-ride of arguments
00130         #
00131         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00132         useLocalDefaults = False
00133 
00134         for item in function_signature_defaults.iteritems():
00135                 key,val = item
00136                 keyVal = eval(key)
00137                 if (keyVal == None):
00138                         #user hasn't set it - use global/default
00139                         pass
00140                 else:
00141                         #user has set it - use over-ride
00142                         if (key != 'self') :
00143                            useLocalDefaults = True
00144                         #myf[key]=keyVal
00145 
00146         myparams = {}
00147         if useLocalDefaults :
00148            for item in function_signature_defaults.iteritems():
00149                key,val = item
00150                keyVal = eval(key)
00151                exec('myparams[key] = keyVal')
00152                if (keyVal == None):
00153                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00154                    keyVal = eval(key)
00155                    if(type(keyVal) == dict) :
00156                       exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00157 
00158         else :
00159             uselessvariable = None 
00160             myparams['vis'] = vis = myf['vis']
00161             myparams['mode'] = mode = myf['mode']
00162             myparams['listitems'] = listitems = myf['listitems']
00163             myparams['hdkey'] = hdkey = myf['hdkey']
00164             myparams['hdindex'] = hdindex = myf['hdindex']
00165             myparams['hdvalue'] = hdvalue = myf['hdvalue']
00166 
00167         if type(listitems)==str: listitems=[listitems]
00168 
00169         result = None
00170 
00171 #
00172 #    The following is work around to avoid a bug with current python translation
00173 #
00174         mytmp = {}
00175 
00176         mytmp['vis'] = vis
00177         mytmp['mode'] = mode
00178         mytmp['listitems'] = listitems
00179         mytmp['hdkey'] = hdkey
00180         mytmp['hdindex'] = hdindex
00181         mytmp['hdvalue'] = hdvalue
00182         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00183         trec = casac.utils().torecord(pathname+'vishead.xml')
00184 
00185         casalog.origin('vishead')
00186         if not trec.has_key('vishead') or not casac.utils().verify(mytmp, trec['vishead']) :
00187             return False
00188 
00189 
00190         try :
00191           casalog.post('')
00192           casalog.post('##########################################')
00193           casalog.post('##### Begin Task: vishead           #####')
00194           casalog.post('')
00195           result = vishead(vis, mode, listitems, hdkey, hdindex, hdvalue)
00196           casalog.post('')
00197           casalog.post('##### End Task: vishead           #####')
00198           casalog.post('##########################################')
00199 
00200 
00201 # saveinputs for individule engine has no use
00202 # saveinputs should alos be removed from casa_in_py.py
00203 #
00204 #
00205 #          saveinputs = myf['saveinputs']
00206 #          saveinputs('vishead', 'vishead.last', myparams)
00207 #
00208 #
00209         except Exception, instance:
00210           #print '**** Error **** ',instance
00211           pass
00212 
00213         gc.collect()
00214         return result
00215 #
00216 #
00217 ##
00218 #    def paramgui(self, useGlobals=True):
00219 #        """
00220 #        Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00221 #        """
00222 #        import paramgui
00223 #
00224 #        a=inspect.stack()
00225 #        stacklevel=0
00226 #        for k in range(len(a)):
00227 #          if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00228 #            stacklevel=k
00229 #            break
00230 #        myf = sys._getframe(stacklevel).f_globals
00231 #
00232 #        if useGlobals:
00233 #            paramgui.setGlobals(myf)
00234 #        else:
00235 #            paramgui.setGlobals({})
00236 #
00237 #        paramgui.runTask('vishead', myf['_ip'])
00238 #        paramgui.setGlobals({})
00239 #
00240 #
00241 #
00242 #
00243     def defaults(self, param=None):
00244         a=inspect.stack()
00245         stacklevel=0
00246         for k in range(len(a)):
00247           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00248                 stacklevel=k
00249                 break
00250         myf=sys._getframe(stacklevel).f_globals
00251         a = odict()
00252         a['vis']  = ''
00253         a['mode']  = 'summary'
00254 
00255         a['async']=False
00256         a['mode'] = {
00257                     0:odict([{'value':'list'}, {'listitems':['telescope', 'observer', 'project', 'field', 'freq_group_name', 'spw_name', 'schedule', 'schedule_type', 'release_date']}]), 
00258                     1:{'value':'summary'}, 
00259                     2:odict([{'value':'get'}, {'hdkey':''}, {'hdindex':''}]), 
00260                     3:odict([{'value':'put'}, {'hdkey':''}, {'hdindex':''}, {'hdvalue':''}])}
00261 
00262 ### This function sets the default values but also will return the list of
00263 ### parameters or the default value of a given parameter
00264         if(param == None):
00265                 myf['__set_default_parameters'](a)
00266         elif(param == 'paramkeys'):
00267                 return a.keys()
00268         else:
00269                 if(a.has_key(param)):
00270                    #if(type(a[param]) == dict) :
00271                    #   return a[param][len(a[param])-1]['value']
00272                    #else :
00273                       return a[param]
00274 
00275 
00276 #
00277 #
00278     def check_params(self, param=None, value=None):
00279       a=inspect.stack() 
00280       stacklevel=0
00281       for k in range(len(a)):
00282         if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00283             stacklevel=k
00284             break
00285       myf=sys._getframe(stacklevel).f_globals
00286 
00287 #      print 'param:', param, 'value:', value
00288       try :
00289          if str(type(value)) != "<type 'instance'>" :
00290             value0 = value
00291             value = myf['cu'].expandparam(param, value)
00292             matchtype = False
00293             if(type(value) == numpy.ndarray):
00294                if(type(value) == type(value0)):
00295                   myf[param] = value.tolist()
00296                else:
00297                   #print 'value:', value, 'value0:', value0
00298                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00299                   myf[param] = value0
00300                   if type(value0) != list :
00301                      matchtype = True
00302             else :
00303                myf[param] = value
00304             value = myf['cu'].verifyparam({param:value})
00305             if matchtype:
00306                value = False
00307       except Exception, instance:
00308          #ignore the exception and just return it unchecked
00309          myf[param] = value
00310       return value
00311 
00312 #
00313 #
00314     def description(self, key='vishead', subkey=None):
00315         desc={'vishead': 'List, summary, get, and put metadata in a measurement set',
00316                'vis': 'Name of input visibility file',
00317                'mode': 'options: list, summary, get, put',
00318                'listitems': 'items to list ([] for all)',
00319                'hdkey': 'keyword to get/put',
00320                'hdindex': 'keyword index to get/put, counting from zero. ''==>all',
00321                'hdvalue': 'value of hdkey',
00322 
00323                'async': 'If true the taskname must be started using vishead(...)'
00324               }
00325 
00326 #
00327 # Set subfields defaults if needed
00328 #
00329 
00330         if(desc.has_key(key)) :
00331            return desc[key]
00332 
00333     def itsdefault(self, paramname) :
00334         a = {}
00335         a['vis']  = ''
00336         a['mode']  = 'summary'
00337         a['listitems']  = ['telescope', 'observer', 'project', 'field', 'freq_group_name', 'spw_name', 'schedule', 'schedule_type', 'release_date']
00338         a['hdkey']  = ''
00339         a['hdindex']  = ''
00340         a['hdvalue']  = ''
00341 
00342         if a.has_key(paramname) :
00343               return a[paramname]
00344 vishead_pg = vishead_pg_()