casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
visstat_cli.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 casac
00010 import string
00011 import time
00012 import inspect
00013 import gc
00014 import numpy
00015 from odict import odict
00016 from taskmanager import tm
00017 from task_visstat import visstat
00018 class visstat_cli_:
00019     __name__ = "visstat"
00020     __async__ = {}
00021     rkey = None
00022     i_am_a_casapy_task = None
00023     # The existence of the i_am_a_casapy_task attribute allows help()
00024     # (and other) to treat casapy tasks as a special case.
00025 
00026     def __init__(self) :
00027        self.__bases__ = (visstat_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'vis':None, 'axis':None, 'datacolumn':None, 'useflags':None, 'spw':None, 'field':None, 'selectdata':None, 'antenna':None, 'uvrange':None, 'timerange':None, 'correlation':None, 'scan':None, 'array':None, 'observation':None,  'async':None}
00031 
00032 
00033     def result(self, key=None):
00034             #### here we will scan the task-ids in __async__
00035             #### and add any that have completed...
00036             if key is not None and self.__async__.has_key(key) and self.__async__[key] is not None:
00037                ret = tm.retrieve(self.__async__[key])
00038                if ret['state'] == "done" :
00039                   self.__async__[key] = None
00040                elif ret['state'] == 'crashed' :
00041                   self.__async__[key] = None
00042                return ret
00043             return None
00044 
00045 
00046     def __call__(self, vis=None, axis=None, datacolumn=None, useflags=None, spw=None, field=None, selectdata=None, antenna=None, uvrange=None, timerange=None, correlation=None, scan=None, array=None, observation=None,  async=None):
00047 
00048         """Displays statistical information from a measurement set, or from a measurement set selection
00049 
00050         This task returns statistical information about data in a measurement
00051         set.
00052 
00053         The following values are computed: mean value, sum of values,
00054         sum of squared values, median, median absolute deviation, quartile,
00055         minimum, maximum, variance, standard deviation, and root mean square.
00056 
00057         The following axes are supported: uvw, flag, weight, sigma, antenna1,
00058         antenna2, feed1, feed2, field_id, array_id, data_desc_id, flag_row,
00059         interval, scan, scan_number, time, weight_spectrum, amp, amplitude,
00060         phase, real, imag, imaginary, and uvrange.
00061 
00062         Optionally, the statistical information can be computed based only
00063         on a given subset of the measurement set.
00064 
00065         Note: If the MS consists of inhomogeneous data, for example several
00066         spectral windows each having a different number of channels, it may be
00067         necessary to use selection parameters to select a homogeneous subset of
00068         the MS, e.g. spw='2'.
00069 
00070         Keyword arguments:
00071 
00072         vis  --- Name of input visibility file
00073                   default: '', example: vis='my.ms'
00074 
00075         axis -- Which data to analyze.
00076 
00077                 default: 'amplitude'
00078                 axis='phase'
00079                 axis='imag'
00080                 axis='scan_number'
00081                 axis='flag'
00082 
00083                 The phase of a complex number is in radians in the range [-pi; pi[.
00084 
00085 
00086         datacolumn -- Which data column to use for complex data.
00087                 default: 'data'
00088                 datacolumn='data'
00089                 datacolumn='corrected'
00090                 datacolumn='model'
00091 
00092         useflags -- Take MS flags into account?
00093                 default: True
00094                 useflag=False
00095                 useflag=True
00096         If useflags=False, flagged values are included in the statistics.
00097         If useflags=True, any flagged values are not used in the statistics.
00098 
00099         spw -- Select data based on spectral window and channels
00100                 default: '' (all); example: spw='1'
00101                 spw='<2' #spectral windows less than 2
00102                 spw='>1' #spectral windows greater than 1
00103                 spw='0:0~10' # first 10 channels from spw 0
00104                 spw='0:0~5;56~60' # multiple separated channel chunks.
00105 
00106         field -- Select data based on field id(s) or name(s)
00107                 default: '' (all); example: field='1'
00108                 field='0~2' # field ids inclusive from 0 to 2
00109                 field='3C*' # all field names starting with 3C
00110 
00111         selectdata -- Other data selection parameters
00112                 default: True
00113         antenna -- Select data based on baseline
00114                 default: '' (all); example: antenna='5&6' baseline 5-6
00115                 antenna='5&6;7&8' #baseline 5-6 and 7-8
00116                 antenna='5' # all baselines with antenna 5
00117                 antenna='5,6' # all baselines with antennas 5 and 6
00118         correlation -- Correlation types
00119                 default: '' (all);
00120                 example: correlation='RR LL'
00121         uvrange -- Select data within uvrange (default units meters)
00122                 default: '' (all); example:
00123                 uvrange='0~1000klambda'; uvrange from 0-1000 kilo-lambda
00124                 uvrange='>4klambda';uvranges greater than 4 kilo-lambda
00125                 uvrange='0~1000km'; uvrange in kilometers
00126         timerange  -- Select data based on time range:
00127                 default = '' (all); example,
00128                 timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
00129                 Note: YYYY/MM/DD can be dropped as needed:
00130                 timerange='09:14:0~09:54:0' # this time range
00131                 timerange='09:44:00' # data within one integration of time
00132                 timerange='>10:24:00' # data after this time
00133                 timerange='09:44:00+00:13:00' #data 13 minutes after time
00134         scan -- Select data based on scan number
00135                 default: '' (all); example: scan='>3'
00136         array -- Selection based on the antenna array
00137         observation -- Selection by observation ID(s).
00138                        default: '' (all); example: observation='1~3'
00139 
00140 
00141         """
00142         if not hasattr(self, "__globals__") or self.__globals__ == None :
00143            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00144         #casac = self.__globals__['casac']
00145         casalog = self.__globals__['casalog']
00146         #casalog = casac.casac.logsink()
00147         self.__globals__['__last_task'] = 'visstat'
00148         self.__globals__['taskname'] = 'visstat'
00149         ###
00150         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00151         ###
00152         ###
00153         #Handle globals or user over-ride of arguments
00154         #
00155         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00156         useLocalDefaults = False
00157 
00158         for item in function_signature_defaults.iteritems():
00159                 key,val = item
00160                 keyVal = eval(key)
00161                 if (keyVal == None):
00162                         #user hasn't set it - use global/default
00163                         pass
00164                 else:
00165                         #user has set it - use over-ride
00166                         if (key != 'self') :
00167                            useLocalDefaults = True
00168 
00169         myparams = {}
00170         if useLocalDefaults :
00171            for item in function_signature_defaults.iteritems():
00172                key,val = item
00173                keyVal = eval(key)
00174                exec('myparams[key] = keyVal')
00175                self.parameters[key] = keyVal
00176                if (keyVal == None):
00177                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00178                    keyVal = eval(key)
00179                    if(type(keyVal) == dict) :
00180                       if len(keyVal) > 0 :
00181                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00182                       else :
00183                          exec('myparams[key] = ' + key + ' = {}')
00184 
00185         else :
00186             async = self.parameters['async']
00187             myparams['vis'] = vis = self.parameters['vis']
00188             myparams['axis'] = axis = self.parameters['axis']
00189             myparams['datacolumn'] = datacolumn = self.parameters['datacolumn']
00190             myparams['useflags'] = useflags = self.parameters['useflags']
00191             myparams['spw'] = spw = self.parameters['spw']
00192             myparams['field'] = field = self.parameters['field']
00193             myparams['selectdata'] = selectdata = self.parameters['selectdata']
00194             myparams['antenna'] = antenna = self.parameters['antenna']
00195             myparams['uvrange'] = uvrange = self.parameters['uvrange']
00196             myparams['timerange'] = timerange = self.parameters['timerange']
00197             myparams['correlation'] = correlation = self.parameters['correlation']
00198             myparams['scan'] = scan = self.parameters['scan']
00199             myparams['array'] = array = self.parameters['array']
00200             myparams['observation'] = observation = self.parameters['observation']
00201 
00202 
00203         result = None
00204 
00205 #
00206 #    The following is work around to avoid a bug with current python translation
00207 #
00208         mytmp = {}
00209 
00210         mytmp['vis'] = vis
00211         mytmp['axis'] = axis
00212         mytmp['datacolumn'] = datacolumn
00213         mytmp['useflags'] = useflags
00214         mytmp['spw'] = spw
00215         mytmp['field'] = field
00216         mytmp['selectdata'] = selectdata
00217         mytmp['antenna'] = antenna
00218         mytmp['uvrange'] = uvrange
00219         mytmp['timerange'] = timerange
00220         mytmp['correlation'] = correlation
00221         mytmp['scan'] = scan
00222         mytmp['array'] = array
00223         mytmp['observation'] = observation
00224         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00225         trec = casac.casac.utils().torecord(pathname+'visstat.xml')
00226 
00227         casalog.origin('visstat')
00228         try :
00229           #if not trec.has_key('visstat') or not casac.casac.utils().verify(mytmp, trec['visstat']) :
00230             #return False
00231 
00232           casac.casac.utils().verify(mytmp, trec['visstat'], True)
00233           scriptstr=['']
00234           saveinputs = self.__globals__['saveinputs']
00235           saveinputs('visstat', 'visstat.last', myparams, self.__globals__,scriptstr=scriptstr)
00236           if async :
00237             count = 0
00238             keybase =  time.strftime("%y%m%d.%H%M%S")
00239             key = keybase + "_" + str(count)
00240             while self.__async__.has_key(key) :
00241                count += 1
00242                key = keybase + "_" + str(count)
00243             result = tm.execute('visstat', vis, axis, datacolumn, useflags, spw, field, selectdata, antenna, uvrange, timerange, correlation, scan, array, observation)
00244             print "Use: "
00245             print "      tm.retrieve(return_value) # to retrieve the status"
00246             print 
00247             self.rkey = key
00248             self.__async__[key] = result
00249           else :
00250               tname = 'visstat'
00251               spaces = ' '*(18-len(tname))
00252               casalog.post('\n##########################################'+
00253                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00254               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00255               result = visstat(vis, axis, datacolumn, useflags, spw, field, selectdata, antenna, uvrange, timerange, correlation, scan, array, observation)
00256               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00257                            '\n##########################################')
00258 
00259         except Exception, instance:
00260           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00261              raise
00262           else :
00263              #print '**** Error **** ',instance
00264              tname = 'visstat'
00265              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00266              pass
00267 
00268         gc.collect()
00269         return result
00270 #
00271 #
00272 #
00273     def paramgui(self, useGlobals=True, ipython_globals=None):
00274         """
00275         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00276         """
00277         import paramgui
00278         if not hasattr(self, "__globals__") or self.__globals__ == None :
00279            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00280 
00281         if useGlobals:
00282             if ipython_globals == None:
00283                 myf=self.__globals__
00284             else:
00285                 myf=ipython_globals
00286 
00287             paramgui.setGlobals(myf)
00288         else:
00289             paramgui.setGlobals({})
00290 
00291         paramgui.runTask('visstat', myf['_ip'])
00292         paramgui.setGlobals({})
00293 
00294 #
00295 #
00296 #
00297     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00298         if not hasattr(self, "__globals__") or self.__globals__ == None :
00299            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00300         if ipython_globals == None:
00301             myf=self.__globals__
00302         else:
00303             myf=ipython_globals
00304 
00305         a = odict()
00306         a['vis']  = ''
00307         a['axis']  = 'amplitude'
00308         a['useflags']  = True
00309         a['spw']  = ''
00310         a['field']  = ''
00311         a['selectdata']  = True
00312 
00313         a['async']=False
00314         a['axis'] = {
00315                     0:odict([{'value':'amp'}, {'datacolumn':'data'}]), 
00316                     1:odict([{'value':'amplitude'}, {'datacolumn':'data'}]), 
00317                     2:odict([{'value':'phase'}, {'datacolumn':'data'}]), 
00318                     3:odict([{'value':'real'}, {'datacolumn':'data'}]), 
00319                     4:odict([{'value':'imag'}, {'datacolumn':'data'}]), 
00320                     5:odict([{'value':'imaginary'}, {'datacolumn':'data'}])}
00321         a['selectdata'] = {
00322                     0:odict([{'value':True}, {'antenna':''}, {'timerange':''}, {'correlation':''}, {'scan':''}, {'array':''}, {'observation':''}, {'uvrange':''}]), 
00323                     1:{'value':False}}
00324 
00325 ### This function sets the default values but also will return the list of
00326 ### parameters or the default value of a given parameter
00327         if(param == None):
00328                 myf['__set_default_parameters'](a)
00329         elif(param == 'paramkeys'):
00330                 return a.keys()
00331         else:
00332             if(paramvalue==None and subparam==None):
00333                if(a.has_key(param)):
00334                   return a[param]
00335                else:
00336                   return self.itsdefault(param)
00337             else:
00338                retval=a[param]
00339                if(type(a[param])==dict):
00340                   for k in range(len(a[param])):
00341                      valornotval='value'
00342                      if(a[param][k].has_key('notvalue')):
00343                         valornotval='notvalue'
00344                      if((a[param][k][valornotval])==paramvalue):
00345                         retval=a[param][k].copy()
00346                         retval.pop(valornotval)
00347                         if(subparam != None):
00348                            if(retval.has_key(subparam)):
00349                               retval=retval[subparam]
00350                            else:
00351                               retval=self.itsdefault(subparam)
00352                      else:
00353                         retval=self.itsdefault(subparam)
00354                return retval
00355 
00356 
00357 #
00358 #
00359     def check_params(self, param=None, value=None, ipython_globals=None):
00360       if ipython_globals == None:
00361           myf=self.__globals__
00362       else:
00363           myf=ipython_globals
00364 #      print 'param:', param, 'value:', value
00365       try :
00366          if str(type(value)) != "<type 'instance'>" :
00367             value0 = value
00368             value = myf['cu'].expandparam(param, value)
00369             matchtype = False
00370             if(type(value) == numpy.ndarray):
00371                if(type(value) == type(value0)):
00372                   myf[param] = value.tolist()
00373                else:
00374                   #print 'value:', value, 'value0:', value0
00375                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00376                   myf[param] = value0
00377                   if type(value0) != list :
00378                      matchtype = True
00379             else :
00380                myf[param] = value
00381             value = myf['cu'].verifyparam({param:value})
00382             if matchtype:
00383                value = False
00384       except Exception, instance:
00385          #ignore the exception and just return it unchecked
00386          myf[param] = value
00387       return value
00388 #
00389 #
00390     def description(self, key='visstat', subkey=None):
00391         desc={'visstat': 'Displays statistical information from a measurement set, or from a measurement set selection',
00392                'vis': 'Name of input visibility file',
00393                'axis': 'Which values to use',
00394                'datacolumn': 'Which data column to use (data, corrected, model)',
00395                'useflags': 'Take flagging into account?',
00396                'spw': 'spectral-window/frequency/channel',
00397                'field': 'Field names or field index numbers: \'\'==>all, field=\'0~2,3C286\'',
00398                'selectdata': 'More data selection parameters (antenna, timerange etc)',
00399                'antenna': 'antenna/baselines: \'\'==>all, antenna = \'3,VA04\'',
00400                'uvrange': 'uv range: \'\'==>all; uvrange = \'0~100klambda\', default units=meters',
00401                'timerange': 'time range: \'\'==>all, timerange=\'09:14:0~09:54:0\'',
00402                'correlation': 'Select data based on correlation',
00403                'scan': 'scan numbers: \'\'==>all',
00404                'array': '(sub)array numbers: \'\'==>all',
00405                'observation': 'observation ID number(s): \'\' = all',
00406 
00407                'async': 'If true the taskname must be started using visstat(...)'
00408               }
00409 
00410 #
00411 # Set subfields defaults if needed
00412 #
00413 
00414         if(desc.has_key(key)) :
00415            return desc[key]
00416 
00417     def itsdefault(self, paramname) :
00418         a = {}
00419         a['vis']  = ''
00420         a['axis']  = 'amplitude'
00421         a['datacolumn']  = 'data'
00422         a['useflags']  = True
00423         a['spw']  = ''
00424         a['field']  = ''
00425         a['selectdata']  = True
00426         a['antenna']  = ''
00427         a['uvrange']  = ''
00428         a['timerange']  = ''
00429         a['correlation']  = ''
00430         a['scan']  = ''
00431         a['array']  = ''
00432         a['observation']  = ''
00433 
00434         #a = sys._getframe(len(inspect.stack())-1).f_globals
00435 
00436         if self.parameters['axis']  == 'amp':
00437             a['datacolumn'] = 'data'
00438 
00439         if self.parameters['axis']  == 'amplitude':
00440             a['datacolumn'] = 'data'
00441 
00442         if self.parameters['axis']  == 'phase':
00443             a['datacolumn'] = 'data'
00444 
00445         if self.parameters['axis']  == 'real':
00446             a['datacolumn'] = 'data'
00447 
00448         if self.parameters['axis']  == 'imag':
00449             a['datacolumn'] = 'data'
00450 
00451         if self.parameters['axis']  == 'imaginary':
00452             a['datacolumn'] = 'data'
00453 
00454         if self.parameters['selectdata']  == True:
00455             a['antenna'] = ''
00456             a['timerange'] = ''
00457             a['correlation'] = ''
00458             a['scan'] = ''
00459             a['array'] = ''
00460             a['observation'] = ''
00461             a['uvrange'] = ''
00462 
00463         if a.has_key(paramname) :
00464               return a[paramname]
00465 visstat_cli = visstat_cli_()