casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
statwt_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_statwt import statwt
00018 class statwt_cli_:
00019     __name__ = "statwt"
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__ = (statwt_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'vis':None, 'dorms':None, 'byantenna':None, 'sepacs':None, 'fitspw':None, 'fitcorr':None, 'combine':None, 'timebin':None, 'minsamp':None, 'field':None, 'spw':None, 'antenna':None, 'timerange':None, 'scan':None, 'intent':None, 'array':None, 'correlation':None, 'observation':None, 'datacolumn':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, dorms=None, byantenna=None, sepacs=None, fitspw=None, fitcorr=None, combine=None, timebin=None, minsamp=None, field=None, spw=None, antenna=None, timerange=None, scan=None, intent=None, array=None, correlation=None, observation=None, datacolumn=None,  async=None):
00047 
00048         """ Reweight visibilities according to their scatter (Experimental)
00049 
00050     The WEIGHT and SIGMA columns of measurement sets are often set to arbitrary
00051     values (e.g. 1), or theoretically estimated from poorly known antenna and
00052     receiver properties.  Many tasks (e.g. clean) are insensitive to an overall
00053     scale error in WEIGHT, but are affected by errors in the relative weights
00054     between visibilities.  Other tasks, such as uvmodelfit, or anything which
00055     depends on theoretical estimates of the noise, require (reasonably) correct
00056     weights and sigmas.  statwt empirically measures the visibility scatter
00057     (typically as a function of time, antenna, and/or baseline) and uses that
00058     to set WEIGHT and SIGMA. It is important that all necessary calibrations 
00059     are applied to the data prior to running this task for correct determination of
00060     weights and sigmas. 
00061     
00062     Note: Some of the parameters (byantenna, sepacs, fitcorr, and timebin) 
00063           are not fully implemeted for CASA 3.4.
00064           
00065 
00066         Keyword arguments:
00067         vis -- Name of the measurement set.
00068                 default: none; example: vis='ngc5921.ms'
00069 
00070         dorms -- Estimate the scatter using rms instead of the standard
00071                  deviation?
00072 
00073                  Ideally the visibilities used to estimate the scatter, as
00074                  selected by fitspw and fitcorr, should be pure noise.  If you
00075                  know for certain that they are, then setting dorms to True
00076                  will give the best result.  Otherwise, use False (standard
00077                  sample standard deviation).  More robust scatter estimates
00078                  like the interquartile range or median absolute deviation from
00079                  the median are not offered because they require sorting by
00080                  value, which is not possible for complex numbers.
00081                default: False
00082 
00083         byantenna -- Assume that the noise is factorable by antenna (feed).
00084                      If false, treat it seperately for each baseline
00085                      (recommended if there is strong signal).
00086                default: False (*** byantenna=True is not yet implemented)
00087 
00088         sepacs -- If solving by antenna, treat autocorrelations separately.
00089                   (Acknowledge that what autocorrelations "see" is very
00090                    different from what crosscorrelations see.)
00091                default: True (*** not yet implemented)
00092 
00093 
00094         --- Data Selection (see help par.selectdata for more detailed
00095             information)
00096 
00097         fitspw -- The (ideally) signal-free spectral window:channels to
00098                   estimate the scatter from.
00099                default: '' (All)
00100 
00101         fitcorr -- The (ideally) signal-free correlations to
00102                    estimate the scatter from.
00103                default: '' (All) 
00104                *** not yet implemented 
00105 
00106         combine -- Let samples span multiple spws, corrs, scans, and/or states.
00107                    combine = 'spw': Recommended when a line spans an entire spw 
00108                                     - set fitspw to the neighboring spws and
00109                                     apply their weight to the line spw(s).
00110                                     However, the effect of the line signal per
00111                                     visibility may be relatively harmless
00112                                     compared to the noise difference between
00113                                     spws.
00114                    combine = 'scan': Can be useful when the scan number
00115                                      goes up with each integration,
00116                                      as in many WSRT MSes.
00117                    combine = ['scan', 'spw']: disregard scan and spw
00118                                               numbers when gathering samples.
00119                    combine = 'spw,scan': Same as above.
00120               default: '' (None)
00121 
00122         timebin -- Sample interval.
00123                    default: '0s' or '-1s' (1 integration at a time)
00124                    example: timebin='30s'
00125                             '10' means '10s'
00126                    *** not yet implemented 
00127 
00128         minsamp -- Minimum number of unflagged visibilities for estimating the
00129                    scatter.  Selected visibilities for which the weight cannot
00130                    be estimated will be flagged.  Note that minsamp is
00131                    effectively at least 2 if dorms is False, and 1 if it is
00132                    True.
00133 
00134         field -- Select field using field id(s) or field name(s).
00135                   [run listobs to obtain the list id's or names]
00136                default: ''=all fields If field string is a non-negative
00137                integer, it is assumed to be a field index
00138                otherwise, it is assumed to be a field name
00139                field='0~2'; field ids 0,1,2
00140                field='0,4,5~7'; field ids 0,4,5,6,7
00141                field='3C286,3C295'; fields named 3C286 and 3C295
00142                field = '3,4C*'; field id 3, all names starting with 4C
00143 
00144         spw -- Select spectral window/channels for changing WEIGHT and SIGMA.
00145                default: ''=all spectral windows and channels
00146                spw='0~2,4'; spectral windows 0,1,2,4 (all channels)
00147                spw='<2';  spectral windows less than 2 (i.e. 0,1)
00148                spw='0:5~61'; spw 0, channels 5 to 61
00149                spw='0,10,3:3~45'; spw 0,10 all channels, spw 3 - chans 3 to 45.
00150                spw='0~2:2~6'; spw 0,1,2 with channels 2 through 6 in each.
00151                spw = '*:3~64'  channels 3 through 64 for all sp id's
00152                        spw = ' :3~64' will NOT work.
00153                statwt does not support multiple channel ranges per spectral
00154                window (';') because it is not clear whether to keep the ranges
00155                in the original spectral window or make a new spectral window
00156                for each additional range.
00157 
00158         antenna -- Select antennas/baselines for changing WEIGHT and SIGMA.
00159                default: '' (all)
00160                 Non-negative integers are assumed to be antenna indices, and
00161                 anything else is taken as an antenna name.
00162 
00163                 Examples:
00164                 antenna='5&6': baseline between antenna index 5 and index 6.
00165                 antenna='VA05&VA06': baseline between VLA antenna 5 and 6.
00166                 antenna='5&6;7&8': baselines 5-6 and 7-8
00167                 antenna='5': all baselines with antenna 5
00168                 antenna='5,6,10': all baselines including antennas 5, 6, or 10
00169                 antenna='5,6,10&': all baselines with *only* antennas 5, 6, or
00170                                        10.  (cross-correlations only.  Use &&
00171                                        to include autocorrelations, and &&&
00172                                        to get only autocorrelations.)
00173                 antenna='!ea03,ea12,ea17': all baselines except those that
00174                                            include EVLA antennas ea03, ea12, or
00175                                            ea17.
00176         timerange -- Select data based on time range:
00177                default = '' (all); examples,
00178                timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
00179                Note: if YYYY/MM/DD is missing date, timerange defaults to the
00180                first day in the dataset
00181                timerange='09:14:0~09:54:0' picks 40 min on first day
00182                timerange='25:00:00~27:30:00' picks 1 hr to 3 hr 30min
00183                on next day
00184                timerange='09:44:00' data within one integration of time
00185                timerange='>10:24:00' data after this time
00186         scan -- Scan number range
00187             default: ''=all
00188         intent -- Select by scan intent (state).  Case sensitive.
00189             default: '' = all
00190             Examples:
00191             intent = 'CALIBRATE_ATMOSPHERE_REFERENCE'
00192             intent = 'calibrate_atmosphere_reference'.upper() # same as above
00193             # Select states that include one or both of CALIBRATE_WVR.REFERENCE
00194             # or OBSERVE_TARGET_ON_SOURCE.
00195             intent = 'CALIBRATE_WVR.REFERENCE, OBSERVE_TARGET_ON_SOURCE'
00196         array -- (Sub)array number range
00197             default: ''=all
00198         correlation -- Select correlations, e.g. 'rr, ll' or ['XY', 'YX'].
00199                        default '' (all).
00200         observation -- Select by observation ID(s).
00201                        default: '' = all
00202        datacolumn -- Which data column to calculate the scatter from
00203                   default='corrected'; example: datacolumn='data'
00204                   Options: 'data', 'corrected', 'model', 'float_data'
00205                   note: 'corrected' will fall back to DATA if CORRECTED_DATA
00206                         is absent.
00207 
00208 
00209         """
00210         if not hasattr(self, "__globals__") or self.__globals__ == None :
00211            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00212         #casac = self.__globals__['casac']
00213         casalog = self.__globals__['casalog']
00214         #casalog = casac.casac.logsink()
00215         self.__globals__['__last_task'] = 'statwt'
00216         self.__globals__['taskname'] = 'statwt'
00217         ###
00218         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00219         ###
00220         ###
00221         #Handle globals or user over-ride of arguments
00222         #
00223         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00224         useLocalDefaults = False
00225 
00226         for item in function_signature_defaults.iteritems():
00227                 key,val = item
00228                 keyVal = eval(key)
00229                 if (keyVal == None):
00230                         #user hasn't set it - use global/default
00231                         pass
00232                 else:
00233                         #user has set it - use over-ride
00234                         if (key != 'self') :
00235                            useLocalDefaults = True
00236 
00237         myparams = {}
00238         if useLocalDefaults :
00239            for item in function_signature_defaults.iteritems():
00240                key,val = item
00241                keyVal = eval(key)
00242                exec('myparams[key] = keyVal')
00243                self.parameters[key] = keyVal
00244                if (keyVal == None):
00245                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00246                    keyVal = eval(key)
00247                    if(type(keyVal) == dict) :
00248                       if len(keyVal) > 0 :
00249                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00250                       else :
00251                          exec('myparams[key] = ' + key + ' = {}')
00252 
00253         else :
00254             async = self.parameters['async']
00255             myparams['vis'] = vis = self.parameters['vis']
00256             myparams['dorms'] = dorms = self.parameters['dorms']
00257             myparams['byantenna'] = byantenna = self.parameters['byantenna']
00258             myparams['sepacs'] = sepacs = self.parameters['sepacs']
00259             myparams['fitspw'] = fitspw = self.parameters['fitspw']
00260             myparams['fitcorr'] = fitcorr = self.parameters['fitcorr']
00261             myparams['combine'] = combine = self.parameters['combine']
00262             myparams['timebin'] = timebin = self.parameters['timebin']
00263             myparams['minsamp'] = minsamp = self.parameters['minsamp']
00264             myparams['field'] = field = self.parameters['field']
00265             myparams['spw'] = spw = self.parameters['spw']
00266             myparams['antenna'] = antenna = self.parameters['antenna']
00267             myparams['timerange'] = timerange = self.parameters['timerange']
00268             myparams['scan'] = scan = self.parameters['scan']
00269             myparams['intent'] = intent = self.parameters['intent']
00270             myparams['array'] = array = self.parameters['array']
00271             myparams['correlation'] = correlation = self.parameters['correlation']
00272             myparams['observation'] = observation = self.parameters['observation']
00273             myparams['datacolumn'] = datacolumn = self.parameters['datacolumn']
00274 
00275 
00276         result = None
00277 
00278 #
00279 #    The following is work around to avoid a bug with current python translation
00280 #
00281         mytmp = {}
00282 
00283         mytmp['vis'] = vis
00284         mytmp['dorms'] = dorms
00285         mytmp['byantenna'] = byantenna
00286         mytmp['sepacs'] = sepacs
00287         mytmp['fitspw'] = fitspw
00288         mytmp['fitcorr'] = fitcorr
00289         mytmp['combine'] = combine
00290         mytmp['timebin'] = timebin
00291         mytmp['minsamp'] = minsamp
00292         mytmp['field'] = field
00293         mytmp['spw'] = spw
00294         mytmp['antenna'] = antenna
00295         mytmp['timerange'] = timerange
00296         mytmp['scan'] = scan
00297         mytmp['intent'] = intent
00298         mytmp['array'] = array
00299         mytmp['correlation'] = correlation
00300         mytmp['observation'] = observation
00301         mytmp['datacolumn'] = datacolumn
00302         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00303         trec = casac.casac.utils().torecord(pathname+'statwt.xml')
00304 
00305         casalog.origin('statwt')
00306         try :
00307           #if not trec.has_key('statwt') or not casac.casac.utils().verify(mytmp, trec['statwt']) :
00308             #return False
00309 
00310           casac.casac.utils().verify(mytmp, trec['statwt'], True)
00311           scriptstr=['']
00312           saveinputs = self.__globals__['saveinputs']
00313           saveinputs('statwt', 'statwt.last', myparams, self.__globals__,scriptstr=scriptstr)
00314           if async :
00315             count = 0
00316             keybase =  time.strftime("%y%m%d.%H%M%S")
00317             key = keybase + "_" + str(count)
00318             while self.__async__.has_key(key) :
00319                count += 1
00320                key = keybase + "_" + str(count)
00321             result = tm.execute('statwt', vis, dorms, byantenna, sepacs, fitspw, fitcorr, combine, timebin, minsamp, field, spw, antenna, timerange, scan, intent, array, correlation, observation, datacolumn)
00322             print "Use: "
00323             print "      tm.retrieve(return_value) # to retrieve the status"
00324             print 
00325             self.rkey = key
00326             self.__async__[key] = result
00327           else :
00328               tname = 'statwt'
00329               spaces = ' '*(18-len(tname))
00330               casalog.post('\n##########################################'+
00331                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00332               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00333               result = statwt(vis, dorms, byantenna, sepacs, fitspw, fitcorr, combine, timebin, minsamp, field, spw, antenna, timerange, scan, intent, array, correlation, observation, datacolumn)
00334               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00335                            '\n##########################################')
00336 
00337         except Exception, instance:
00338           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00339              raise
00340           else :
00341              #print '**** Error **** ',instance
00342              tname = 'statwt'
00343              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00344              pass
00345 
00346         gc.collect()
00347         return result
00348 #
00349 #
00350 #
00351     def paramgui(self, useGlobals=True, ipython_globals=None):
00352         """
00353         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00354         """
00355         import paramgui
00356         if not hasattr(self, "__globals__") or self.__globals__ == None :
00357            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00358 
00359         if useGlobals:
00360             if ipython_globals == None:
00361                 myf=self.__globals__
00362             else:
00363                 myf=ipython_globals
00364 
00365             paramgui.setGlobals(myf)
00366         else:
00367             paramgui.setGlobals({})
00368 
00369         paramgui.runTask('statwt', myf['_ip'])
00370         paramgui.setGlobals({})
00371 
00372 #
00373 #
00374 #
00375     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00376         if not hasattr(self, "__globals__") or self.__globals__ == None :
00377            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00378         if ipython_globals == None:
00379             myf=self.__globals__
00380         else:
00381             myf=ipython_globals
00382 
00383         a = odict()
00384         a['vis']  = ''
00385         a['dorms']  = False
00386         a['byantenna']  = False
00387         a['fitspw']  = ''
00388         a['fitcorr']  = ''
00389         a['combine']  = ''
00390         a['timebin']  = '0s'
00391         a['minsamp']  = 2
00392         a['field']  = ''
00393         a['spw']  = ''
00394         a['antenna']  = ''
00395         a['timerange']  = ''
00396         a['scan']  = ''
00397         a['intent']  = ''
00398         a['array']  = ''
00399         a['correlation']  = ''
00400         a['observation']  = ''
00401         a['datacolumn']  = 'corrected'
00402 
00403         a['async']=False
00404         a['byantenna'] = {
00405                     0:{'value':False}, 
00406                     1:odict([{'value':True}, {'sepacs':True}])}
00407 
00408 ### This function sets the default values but also will return the list of
00409 ### parameters or the default value of a given parameter
00410         if(param == None):
00411                 myf['__set_default_parameters'](a)
00412         elif(param == 'paramkeys'):
00413                 return a.keys()
00414         else:
00415             if(paramvalue==None and subparam==None):
00416                if(a.has_key(param)):
00417                   return a[param]
00418                else:
00419                   return self.itsdefault(param)
00420             else:
00421                retval=a[param]
00422                if(type(a[param])==dict):
00423                   for k in range(len(a[param])):
00424                      valornotval='value'
00425                      if(a[param][k].has_key('notvalue')):
00426                         valornotval='notvalue'
00427                      if((a[param][k][valornotval])==paramvalue):
00428                         retval=a[param][k].copy()
00429                         retval.pop(valornotval)
00430                         if(subparam != None):
00431                            if(retval.has_key(subparam)):
00432                               retval=retval[subparam]
00433                            else:
00434                               retval=self.itsdefault(subparam)
00435                      else:
00436                         retval=self.itsdefault(subparam)
00437                return retval
00438 
00439 
00440 #
00441 #
00442     def check_params(self, param=None, value=None, ipython_globals=None):
00443       if ipython_globals == None:
00444           myf=self.__globals__
00445       else:
00446           myf=ipython_globals
00447 #      print 'param:', param, 'value:', value
00448       try :
00449          if str(type(value)) != "<type 'instance'>" :
00450             value0 = value
00451             value = myf['cu'].expandparam(param, value)
00452             matchtype = False
00453             if(type(value) == numpy.ndarray):
00454                if(type(value) == type(value0)):
00455                   myf[param] = value.tolist()
00456                else:
00457                   #print 'value:', value, 'value0:', value0
00458                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00459                   myf[param] = value0
00460                   if type(value0) != list :
00461                      matchtype = True
00462             else :
00463                myf[param] = value
00464             value = myf['cu'].verifyparam({param:value})
00465             if matchtype:
00466                value = False
00467       except Exception, instance:
00468          #ignore the exception and just return it unchecked
00469          myf[param] = value
00470       return value
00471 #
00472 #
00473     def description(self, key='statwt', subkey=None):
00474         desc={'statwt': ' Reweight visibilities according to their scatter (Experimental)',
00475                'vis': 'Name of measurement set',
00476                'dorms': 'Use rms instead of stddev?',
00477                'byantenna': 'Estimate the noise per antenna -not implemented (vs. per baseline)',
00478                'sepacs': 'If solving by antenna, treat autocorrs separately (not implemented)',
00479                'fitspw': 'The signal-free spectral window:channels to estimate the scatter from',
00480                'fitcorr': 'The signal-free correlation(s) to estimate the scatter from (not implemented)',
00481                'combine': 'Let estimates span changes in spw, corr, scan and/or state',
00482                'timebin': 'Bin length for estimates (not implemented)',
00483                'minsamp': 'Minimum number of unflagged visibilities for estimating the scatter',
00484                'field': 'Select field using ID(s) or name(s)',
00485                'spw': 'Select spectral window/channels',
00486                'antenna': 'Select data based on antenna/baseline',
00487                'timerange': 'Select data by time range',
00488                'scan': 'Select data by scan numbers',
00489                'intent': 'Select data by scan intents',
00490                'array': 'Select (sub)array(s) by array ID number',
00491                'correlation': 'Select correlations to reweight',
00492                'observation': 'Select by observation ID(s)',
00493                'datacolumn': 'Which data column to calculate the scatter from',
00494 
00495                'async': 'If true the taskname must be started using statwt(...)'
00496               }
00497 
00498 #
00499 # Set subfields defaults if needed
00500 #
00501 
00502         if(desc.has_key(key)) :
00503            return desc[key]
00504 
00505     def itsdefault(self, paramname) :
00506         a = {}
00507         a['vis']  = ''
00508         a['dorms']  = False
00509         a['byantenna']  = False
00510         a['sepacs']  = True
00511         a['fitspw']  = ''
00512         a['fitcorr']  = ''
00513         a['combine']  = ''
00514         a['timebin']  = '0s'
00515         a['minsamp']  = 2
00516         a['field']  = ''
00517         a['spw']  = ''
00518         a['antenna']  = ''
00519         a['timerange']  = ''
00520         a['scan']  = ''
00521         a['intent']  = ''
00522         a['array']  = ''
00523         a['correlation']  = ''
00524         a['observation']  = ''
00525         a['datacolumn']  = 'corrected'
00526 
00527         #a = sys._getframe(len(inspect.stack())-1).f_globals
00528 
00529         if self.parameters['byantenna']  == True:
00530             a['sepacs'] = True
00531 
00532         if a.has_key(paramname) :
00533               return a[paramname]
00534 statwt_cli = statwt_cli_()