casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
split_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_split import split
00018 class split_cli_:
00019     __name__ = "split"
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__ = (split_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'vis':None, 'outputvis':None, 'datacolumn':None, 'field':None, 'spw':None, 'width':None, 'antenna':None, 'timebin':None, 'timerange':None, 'scan':None, 'intent':None, 'array':None, 'uvrange':None, 'correlation':None, 'observation':None, 'combine':None, 'keepflags':None, 'keepmms':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, outputvis=None, datacolumn=None, field=None, spw=None, width=None, antenna=None, timebin=None, timerange=None, scan=None, intent=None, array=None, uvrange=None, correlation=None, observation=None, combine=None, keepflags=None, keepmms=None,  async=None):
00047 
00048         """Create a visibility subset from an existing visibility set
00049 
00050     Split is the general purpose program to make a new data set that is a
00051     subset or averaged form of an existing data set.  General selection
00052     parameters are included, and one or all of the various data columns
00053     (DATA, LAG_DATA and/or FLOAT_DATA, and possibly MODEL_DATA and/or
00054     CORRECTED_DATA) can be selected.
00055     
00056     Split is often used after the initial calibration of the data to make a
00057     smaller measurement set with only the data that will be used in
00058     further flagging, imaging and/or self-calibration.  split can
00059     average over frequency (channels) and time (integrations).
00060 
00061     With the keepmms parameter, split can be run parallelized on multi-MS
00062     input.
00063 
00064         Keyword arguments:
00065         vis -- Name of input visibility file
00066                 default: none; example: vis='ngc5921.ms'
00067         outputvis -- Name of output visibility file
00068                 default: none; example: outputvis='ngc5921_src.ms'
00069 
00070         datacolumn -- Which data column to split out
00071                 default='corrected'; example: datacolumn='data'
00072                 Options: 'data', 'model', 'corrected', 'all',
00073                 'float_data', 'lag_data', 'float_data,data', and
00074                 'lag_data,data'.
00075                 N.B.: 'all' = whichever of the above that are present.
00076                 Otherwise the selected column will go to DATA (or
00077                 FLOAT_DATA) in the output.
00078                 Splitting with the default datacolumn='corrected'
00079                 before clean is normally required for self-calibration!
00080 
00081         --- Data Selection (see help par.selectdata for more detailed
00082             information)
00083 
00084         field -- Select field using field id(s) or field name(s).
00085                   [run listobs to obtain the list id's or names]
00086                default: ''=all fields If field string is a non-negative
00087                integer, it is assumed to be a field index
00088                otherwise, it is assumed to be a field name
00089                field='0~2'; field ids 0,1,2
00090                field='0,4,5~7'; field ids 0,4,5,6,7
00091                field='3C286,3C295'; fields named 3C286 and 3C295
00092                field = '3,4C*'; field id 3, all names starting with 4C
00093         spw -- Select spectral window/channels
00094                default: ''=all spectral windows and channels
00095                spw='0~2,4'; spectral windows 0,1,2,4 (all channels)
00096                spw='<2';  spectral windows less than 2 (i.e. 0,1)
00097                spw='0:5~61'; spw 0, channels 5 to 61
00098                spw='0,10,3:3~45'; spw 0,10 all channels, spw 3 - chans 3 to 45.
00099                spw='0~2:2~6'; spw 0,1,2 with channels 2 through 6 in each.
00100                spw = '*:3~64'  channels 3 through 64 for all sp id's
00101                        spw = ' :3~64' will NOT work.
00102                split does not support multiple channel ranges per spectral
00103                window (';') because it is not clear whether to keep the ranges
00104                in the original spectral window or make a new spectral window
00105                for each additional range.
00106         width -- Defines the number of channel to average to form the one
00107                  output channel.
00108                  default: '1' => no channel averaging
00109                  example: width=[2,3] => average 2 channels of 1st
00110                           spectral window selected and 3 in the second one.
00111         antenna -- Select data based on antenna/baseline
00112                default: '' (all)
00113                 Non-negative integers are assumed to be antenna indices, and
00114                 anything else is taken as an antenna name.
00115 
00116                 Examples:
00117                 antenna='5&6': baseline between antenna index 5 and index 6.
00118                 antenna='VA05&VA06': baseline between VLA antenna 5 and 6.
00119                 antenna='5&6;7&8': baselines 5-6 and 7-8
00120                 antenna='5': all baselines with antenna 5
00121                 antenna='5,6,10': all baselines including antennas 5, 6, or 10
00122                 antenna='5,6,10&': all baselines with *only* antennas 5, 6, or
00123                                        10.  (cross-correlations only.  Use &&
00124                                        to include autocorrelations, and &&&
00125                                        to get only autocorrelations.)
00126                 antenna='!ea03,ea12,ea17': all baselines except those that
00127                                            include EVLA antennas ea03, ea12, or
00128                                            ea17.
00129         timebin -- Interval width for time averaging.
00130                    default: '0s' or '-1s' (no averaging)
00131                    example: timebin='30s'
00132                             '10' means '10s'
00133         timerange -- Select data based on time range:
00134                default = '' (all); examples,
00135                timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
00136                Note: if YYYY/MM/DD is missing date, timerange defaults to the
00137                first day in the dataset
00138                timerange='09:14:0~09:54:0' picks 40 min on first day
00139                timerange='25:00:00~27:30:00' picks 1 hr to 3 hr 30min
00140                on next day
00141                timerange='09:44:00' data within one integration of time
00142                timerange='>10:24:00' data after this time
00143         array -- (Sub)array number range
00144             default: ''=all
00145         uvrange -- Select data within uvrange (default units meters)
00146             default: ''=all; example:
00147             uvrange='0~1000klambda'; uvrange from 0-1000 kilo-lambda
00148             uvrange='>4klambda';uvranges greater than 4 kilo-lambda
00149             uvrange='0~1000km'; uvrange in kilometers
00150         scan -- Scan number range
00151             default: ''=all
00152         intent -- Select by scan intent (state).  Case sensitive.
00153             default: '' = all
00154             Examples:
00155             intent = 'CALIBRATE_ATMOSPHERE_REFERENCE'
00156             intent = 'calibrate_atmosphere_reference'.upper() # same as above
00157             # Select states that include one or both of CALIBRATE_WVR.REFERENCE
00158             # or OBSERVE_TARGET_ON_SOURCE.
00159             intent = 'CALIBRATE_WVR.REFERENCE, OBSERVE_TARGET_ON_SOURCE'
00160         correlation -- Select correlations, e.g. 'rr, ll' or ['XY', 'YX'].
00161                        default '' (all).
00162         observation -- Select by observation ID(s).
00163                        default: '' = all
00164         combine -- Let time bins span changes in scan and/or state.
00165                    default = '' (separate time bins by both of the above)
00166                    combine = 'scan': Can be useful when the scan number
00167                                      goes up with each integration,
00168                                      as in many WSRT MSes.
00169                    combine = ['scan', 'state']: disregard scan and state
00170                                                 numbers when time averaging.
00171                    combine = 'state,scan': Same as above.
00172         keepflags -- If practical, keep completely flagged rows instead of
00173                      dropping them.  This has absolutely no effect on averaging
00174                      calculations, or partially flagged rows.  All of the
00175                      channels and correlations of a row must be flagged for it
00176                      to be droppable, and a row must be well defined to be
00177                      keepable.  The latter condition means that this option has
00178                      no effect on time averaging - in that case fully flagged
00179                      rows are automatically omitted.  Regardless of this
00180                      parameter, flagged data is never included in averaging
00181                      calculations.
00182 
00183                      The only time keepflags matters is if 
00184                      1. the input MS has some completely flagged rows
00185                      and
00186                      2. time averaging is not being done.
00187 
00188                      Then, if keepflags is False, the completely flagged rows
00189                      will be omitted from the output MS.  Otherwise, they will
00190                      be included (subject to the selection parameters).
00191 
00192         keepmms -- If true and the input is a multi-MS, make the output one, too.
00193                    Otherwise, the output will be a normal MS without partitioning. 
00194                    (experimental)
00195                    Default: False
00196 
00197 
00198         """
00199         if not hasattr(self, "__globals__") or self.__globals__ == None :
00200            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00201         #casac = self.__globals__['casac']
00202         casalog = self.__globals__['casalog']
00203         #casalog = casac.casac.logsink()
00204         self.__globals__['__last_task'] = 'split'
00205         self.__globals__['taskname'] = 'split'
00206         ###
00207         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00208         ###
00209         ###
00210         #Handle globals or user over-ride of arguments
00211         #
00212         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00213         useLocalDefaults = False
00214 
00215         for item in function_signature_defaults.iteritems():
00216                 key,val = item
00217                 keyVal = eval(key)
00218                 if (keyVal == None):
00219                         #user hasn't set it - use global/default
00220                         pass
00221                 else:
00222                         #user has set it - use over-ride
00223                         if (key != 'self') :
00224                            useLocalDefaults = True
00225 
00226         myparams = {}
00227         if useLocalDefaults :
00228            for item in function_signature_defaults.iteritems():
00229                key,val = item
00230                keyVal = eval(key)
00231                exec('myparams[key] = keyVal')
00232                self.parameters[key] = keyVal
00233                if (keyVal == None):
00234                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00235                    keyVal = eval(key)
00236                    if(type(keyVal) == dict) :
00237                       if len(keyVal) > 0 :
00238                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00239                       else :
00240                          exec('myparams[key] = ' + key + ' = {}')
00241 
00242         else :
00243             async = self.parameters['async']
00244             myparams['vis'] = vis = self.parameters['vis']
00245             myparams['outputvis'] = outputvis = self.parameters['outputvis']
00246             myparams['datacolumn'] = datacolumn = self.parameters['datacolumn']
00247             myparams['field'] = field = self.parameters['field']
00248             myparams['spw'] = spw = self.parameters['spw']
00249             myparams['width'] = width = self.parameters['width']
00250             myparams['antenna'] = antenna = self.parameters['antenna']
00251             myparams['timebin'] = timebin = self.parameters['timebin']
00252             myparams['timerange'] = timerange = self.parameters['timerange']
00253             myparams['scan'] = scan = self.parameters['scan']
00254             myparams['intent'] = intent = self.parameters['intent']
00255             myparams['array'] = array = self.parameters['array']
00256             myparams['uvrange'] = uvrange = self.parameters['uvrange']
00257             myparams['correlation'] = correlation = self.parameters['correlation']
00258             myparams['observation'] = observation = self.parameters['observation']
00259             myparams['combine'] = combine = self.parameters['combine']
00260             myparams['keepflags'] = keepflags = self.parameters['keepflags']
00261             myparams['keepmms'] = keepmms = self.parameters['keepmms']
00262 
00263 
00264         result = None
00265 
00266 #
00267 #    The following is work around to avoid a bug with current python translation
00268 #
00269         mytmp = {}
00270 
00271         mytmp['vis'] = vis
00272         mytmp['outputvis'] = outputvis
00273         mytmp['datacolumn'] = datacolumn
00274         mytmp['field'] = field
00275         mytmp['spw'] = spw
00276         mytmp['width'] = width
00277         mytmp['antenna'] = antenna
00278         mytmp['timebin'] = timebin
00279         mytmp['timerange'] = timerange
00280         mytmp['scan'] = scan
00281         mytmp['intent'] = intent
00282         mytmp['array'] = array
00283         mytmp['uvrange'] = uvrange
00284         mytmp['correlation'] = correlation
00285         mytmp['observation'] = observation
00286         mytmp['combine'] = combine
00287         mytmp['keepflags'] = keepflags
00288         mytmp['keepmms'] = keepmms
00289         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00290         trec = casac.casac.utils().torecord(pathname+'split.xml')
00291 
00292         casalog.origin('split')
00293         try :
00294           #if not trec.has_key('split') or not casac.casac.utils().verify(mytmp, trec['split']) :
00295             #return False
00296 
00297           casac.casac.utils().verify(mytmp, trec['split'], True)
00298           scriptstr=['']
00299           saveinputs = self.__globals__['saveinputs']
00300           saveinputs('split', 'split.last', myparams, self.__globals__,scriptstr=scriptstr)
00301           if async :
00302             count = 0
00303             keybase =  time.strftime("%y%m%d.%H%M%S")
00304             key = keybase + "_" + str(count)
00305             while self.__async__.has_key(key) :
00306                count += 1
00307                key = keybase + "_" + str(count)
00308             result = tm.execute('split', vis, outputvis, datacolumn, field, spw, width, antenna, timebin, timerange, scan, intent, array, uvrange, correlation, observation, combine, keepflags, keepmms)
00309             print "Use: "
00310             print "      tm.retrieve(return_value) # to retrieve the status"
00311             print 
00312             self.rkey = key
00313             self.__async__[key] = result
00314           else :
00315               tname = 'split'
00316               spaces = ' '*(18-len(tname))
00317               casalog.post('\n##########################################'+
00318                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00319               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00320               result = split(vis, outputvis, datacolumn, field, spw, width, antenna, timebin, timerange, scan, intent, array, uvrange, correlation, observation, combine, keepflags, keepmms)
00321               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00322                            '\n##########################################')
00323 
00324         except Exception, instance:
00325           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00326              raise
00327           else :
00328              #print '**** Error **** ',instance
00329              tname = 'split'
00330              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00331              pass
00332 
00333         gc.collect()
00334         return result
00335 #
00336 #
00337 #
00338     def paramgui(self, useGlobals=True, ipython_globals=None):
00339         """
00340         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00341         """
00342         import paramgui
00343         if not hasattr(self, "__globals__") or self.__globals__ == None :
00344            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00345 
00346         if useGlobals:
00347             if ipython_globals == None:
00348                 myf=self.__globals__
00349             else:
00350                 myf=ipython_globals
00351 
00352             paramgui.setGlobals(myf)
00353         else:
00354             paramgui.setGlobals({})
00355 
00356         paramgui.runTask('split', myf['_ip'])
00357         paramgui.setGlobals({})
00358 
00359 #
00360 #
00361 #
00362     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00363         if not hasattr(self, "__globals__") or self.__globals__ == None :
00364            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00365         if ipython_globals == None:
00366             myf=self.__globals__
00367         else:
00368             myf=ipython_globals
00369 
00370         a = odict()
00371         a['vis']  = ''
00372         a['outputvis']  = ''
00373         a['datacolumn']  = 'corrected'
00374         a['field']  = ''
00375         a['spw']  = ''
00376         a['width']  = 1
00377         a['antenna']  = ''
00378         a['timebin']  = '0s'
00379         a['timerange']  = ''
00380         a['scan']  = ''
00381         a['intent']  = ''
00382         a['array']  = ''
00383         a['uvrange']  = ''
00384         a['correlation']  = ''
00385         a['observation']  = ''
00386         a['keepflags']  = True
00387         a['keepmms']  = False
00388 
00389         a['async']=False
00390         a['timebin'] = {
00391                     0:odict([{'notvalue':'0s'}, {'combine':''}])}
00392 
00393 ### This function sets the default values but also will return the list of
00394 ### parameters or the default value of a given parameter
00395         if(param == None):
00396                 myf['__set_default_parameters'](a)
00397         elif(param == 'paramkeys'):
00398                 return a.keys()
00399         else:
00400             if(paramvalue==None and subparam==None):
00401                if(a.has_key(param)):
00402                   return a[param]
00403                else:
00404                   return self.itsdefault(param)
00405             else:
00406                retval=a[param]
00407                if(type(a[param])==dict):
00408                   for k in range(len(a[param])):
00409                      valornotval='value'
00410                      if(a[param][k].has_key('notvalue')):
00411                         valornotval='notvalue'
00412                      if((a[param][k][valornotval])==paramvalue):
00413                         retval=a[param][k].copy()
00414                         retval.pop(valornotval)
00415                         if(subparam != None):
00416                            if(retval.has_key(subparam)):
00417                               retval=retval[subparam]
00418                            else:
00419                               retval=self.itsdefault(subparam)
00420                      else:
00421                         retval=self.itsdefault(subparam)
00422                return retval
00423 
00424 
00425 #
00426 #
00427     def check_params(self, param=None, value=None, ipython_globals=None):
00428       if ipython_globals == None:
00429           myf=self.__globals__
00430       else:
00431           myf=ipython_globals
00432 #      print 'param:', param, 'value:', value
00433       try :
00434          if str(type(value)) != "<type 'instance'>" :
00435             value0 = value
00436             value = myf['cu'].expandparam(param, value)
00437             matchtype = False
00438             if(type(value) == numpy.ndarray):
00439                if(type(value) == type(value0)):
00440                   myf[param] = value.tolist()
00441                else:
00442                   #print 'value:', value, 'value0:', value0
00443                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00444                   myf[param] = value0
00445                   if type(value0) != list :
00446                      matchtype = True
00447             else :
00448                myf[param] = value
00449             value = myf['cu'].verifyparam({param:value})
00450             if matchtype:
00451                value = False
00452       except Exception, instance:
00453          #ignore the exception and just return it unchecked
00454          myf[param] = value
00455       return value
00456 #
00457 #
00458     def description(self, key='split', subkey=None):
00459         desc={'split': 'Create a visibility subset from an existing visibility set',
00460                'vis': 'Name of input measurement set',
00461                'outputvis': 'Name of output measurement set',
00462                'datacolumn': 'Which data column(s) to split out',
00463                'field': 'Select field using ID(s) or name(s)',
00464                'spw': 'Select spectral window/channels',
00465                'width': 'Number of channels to average to form one output channel',
00466                'antenna': 'Select data based on antenna/baseline',
00467                'timebin': 'Bin width for time averaging',
00468                'timerange': 'Select data by time range',
00469                'scan': 'Select data by scan numbers',
00470                'intent': 'Select data by scan intents',
00471                'array': 'Select (sub)array(s) by array ID number',
00472                'uvrange': 'Select data by baseline length',
00473                'correlation': 'Select correlations',
00474                'observation': 'Select by observation ID(s)',
00475                'combine': 'Let time bins span changes in scan and/or state',
00476                'keepflags': 'If practical, keep *completely flagged rows* instead of dropping them.',
00477                'keepmms': 'If the input is a multi-MS, make the output one, too. (experimental)',
00478 
00479                'async': 'If true the taskname must be started using split(...)'
00480               }
00481 
00482 #
00483 # Set subfields defaults if needed
00484 #
00485 
00486         if(desc.has_key(key)) :
00487            return desc[key]
00488 
00489     def itsdefault(self, paramname) :
00490         a = {}
00491         a['vis']  = ''
00492         a['outputvis']  = ''
00493         a['datacolumn']  = 'corrected'
00494         a['field']  = ''
00495         a['spw']  = ''
00496         a['width']  = 1
00497         a['antenna']  = ''
00498         a['timebin']  = '0s'
00499         a['timerange']  = ''
00500         a['scan']  = ''
00501         a['intent']  = ''
00502         a['array']  = ''
00503         a['uvrange']  = ''
00504         a['correlation']  = ''
00505         a['observation']  = ''
00506         a['combine']  = ''
00507         a['keepflags']  = True
00508         a['keepmms']  = False
00509 
00510         #a = sys._getframe(len(inspect.stack())-1).f_globals
00511 
00512         if self.parameters['timebin']  != '0s':
00513             a['combine'] = ''
00514 
00515         if a.has_key(paramname) :
00516               return a[paramname]
00517 split_cli = split_cli_()