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