casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
virtualconcat_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_virtualconcat import virtualconcat
00016 from task_virtualconcat import casalog
00017 
00018 class virtualconcat_pg_:
00019     __name__ = "virtualconcat"
00020 
00021     def __init__(self) :
00022        self.__bases__ = (virtualconcat_pg_,)
00023        self.__doc__ = self.__call__.__doc__
00024 
00025 
00026     def __call__(self, vis=None, concatvis=None, freqtol=None, dirtol=None, respectname=None, visweightscale=None, keepcopy=None, copypointing=None, async=None):
00027 
00028         """Concatenate several visibility data sets into a multi-MS
00029 
00030 The list of data sets given in the vis argument are moved into an output
00031 multi-MS data set concatvis and virtually concatenated. 
00032 
00033 NOTE: This task will modify the input datasets by moving them and reindexing them.
00034 If you want to keep a copy of your original data, please set the parameter 
00035 keepcopy to True.
00036 
00037 There is no limit to the number of input data sets.
00038 
00039 If none of the input data sets have any scratch columns (model and corrected
00040 columns), none are created in the concatvis.  Otherwise these columns are
00041 created on output and initialized to their default value (1 in model column,
00042 data in corrected column) for those data with no input columns.
00043 
00044 Spectral windows for each data set with the same chanelization, and within a
00045 specified frequency tolerance of another data set will be combined into one
00046 spectral window.
00047 
00048 A field position in one data set that is within a specified direction tolerance
00049 of another field position in any other data set will be combined into one
00050 field.  The field names need not be the same---only their position is used.
00051 
00052 Each appended dataset is assigned a new observation id if the corresponding
00053 rows in the observation table are not the same.
00054 
00055 Keyword arguments:
00056 vis -- Name of input visibility files to be combined
00057         default: none; example: vis = ['src2.ms','ngc5921.ms','ngc315.ms']
00058 concatvis -- Name of visibility file that will contain the concatenated data
00059         note: if this file exits on disk then the input files are 
00060               added to this file.  Otherwise the new file contains  
00061               the concatenated data.  Be careful here when concatenating to
00062               an existing file.
00063         default: none; example: concatvis='src2.ms'
00064                  example: concatvis='outvis.ms'
00065 
00066         other examples: 
00067            virtualconcat(vis=['src2.ms','ngc5921.ms'], concatvis='out.mms') 
00068                will concatenate 'ngc5921.ms' and 'src2.ms' into a file named 
00069                'out.mms'; the original 'ngc5921.ms' and 'src2.ms' are gone.
00070                'out.mms' is a multims. As most of the data is only moved, not 
00071                copied, this is faster and subsequent tasks can run in parallel
00072                on the subMSs of out.mms.
00073            virtualconcat(vis=['src2.ms','ngc5921.ms'], concatvis='out.mms', keepcopy=True) 
00074                will concatenate 'ngc5921.ms' and 'src2.ms' into a file named 
00075                'out.mms'; the original 'ngc5921.ms' and 'src2.ms' are as before
00076                but you consume more disk space and time for the copy.
00077                .
00078 
00079      Note: run flagmanager to save flags in the concatvis
00080 
00081 freqtol -- Frequency shift tolerance for considering data to be in the same
00082            spwid.  The number of channels must also be the same.
00083         default: ''  do not combine unless frequencies are equal
00084         example: freqtol='10MHz' will not combine spwid unless they are
00085            within 10 MHz.
00086         Note: This option is useful to conbine spectral windows with very slight
00087            frequency differences caused by Doppler tracking, for example.
00088 
00089 dirtol -- Direction shift tolerance for considering data as the same field
00090         default: '' means always combine.
00091         example: dirtol='1.arcsec' will not combine data for a field unless
00092            their phase center differ by less than 1 arcsec.  If the field names
00093            are different in the input data sets, the name in the output data
00094            set will be the first relevant data set in the list.
00095 
00096 respectname -- If true, fields with a different name are not merged even if their 
00097         direction agrees (within dirtol). 
00098         default: True
00099 
00100 visweightscale -- The weights of the individual MSs will be scaled in the concatenated
00101         output MS by the factors in this list. Useful for handling heterogeneous arrays.
00102         Use plotms to inspect the "Wt" column as a reference for determining the scaling 
00103         factors. See the cookbook for more details.
00104         example: [1.,3.,3.] - scale the weights of the second and third MS by a factor 3.
00105         default: [] (empty list) - no scaling
00106 
00107 keepcopy -- If true, a copy of the input MSs is kept in their original place.
00108         default: false
00109 
00110 copypointing -- If true, the POINTING table information will be present in the output.
00111                 If false, the result is an empty POINTING table.
00112          default: true
00113 
00114 
00115         """
00116         a=inspect.stack()
00117         stacklevel=0
00118         for k in range(len(a)):
00119           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00120                 stacklevel=k
00121                 break
00122         myf=sys._getframe(stacklevel).f_globals
00123         myf['__last_task'] = 'virtualconcat'
00124         myf['taskname'] = 'virtualconcat'
00125         ###
00126         myf['update_params'](func=myf['taskname'],printtext=False)
00127         ###
00128         ###
00129         #Handle globals or user over-ride of arguments
00130         #
00131         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00132         useLocalDefaults = False
00133 
00134         for item in function_signature_defaults.iteritems():
00135                 key,val = item
00136                 keyVal = eval(key)
00137                 if (keyVal == None):
00138                         #user hasn't set it - use global/default
00139                         pass
00140                 else:
00141                         #user has set it - use over-ride
00142                         if (key != 'self') :
00143                            useLocalDefaults = True
00144                         #myf[key]=keyVal
00145 
00146         myparams = {}
00147         if useLocalDefaults :
00148            for item in function_signature_defaults.iteritems():
00149                key,val = item
00150                keyVal = eval(key)
00151                exec('myparams[key] = keyVal')
00152                if (keyVal == None):
00153                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00154                    keyVal = eval(key)
00155                    if(type(keyVal) == dict) :
00156                       exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00157 
00158         else :
00159             uselessvariable = None 
00160             myparams['vis'] = vis = myf['vis']
00161             myparams['concatvis'] = concatvis = myf['concatvis']
00162             myparams['freqtol'] = freqtol = myf['freqtol']
00163             myparams['dirtol'] = dirtol = myf['dirtol']
00164             myparams['respectname'] = respectname = myf['respectname']
00165             myparams['visweightscale'] = visweightscale = myf['visweightscale']
00166             myparams['keepcopy'] = keepcopy = myf['keepcopy']
00167             myparams['copypointing'] = copypointing = myf['copypointing']
00168 
00169         if type(vis)==str: vis=[vis]
00170         if type(visweightscale)==float: visweightscale=[visweightscale]
00171 
00172         result = None
00173 
00174 #
00175 #    The following is work around to avoid a bug with current python translation
00176 #
00177         mytmp = {}
00178 
00179         mytmp['vis'] = vis
00180         mytmp['concatvis'] = concatvis
00181         mytmp['freqtol'] = freqtol
00182         mytmp['dirtol'] = dirtol
00183         mytmp['respectname'] = respectname
00184         mytmp['visweightscale'] = visweightscale
00185         mytmp['keepcopy'] = keepcopy
00186         mytmp['copypointing'] = copypointing
00187         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00188         trec = casac.utils().torecord(pathname+'virtualconcat.xml')
00189 
00190         casalog.origin('virtualconcat')
00191         if not trec.has_key('virtualconcat') or not casac.utils().verify(mytmp, trec['virtualconcat']) :
00192             return False
00193 
00194 
00195         try :
00196           casalog.post('')
00197           casalog.post('##########################################')
00198           casalog.post('##### Begin Task: virtualconcat           #####')
00199           casalog.post('')
00200           result = virtualconcat(vis, concatvis, freqtol, dirtol, respectname, visweightscale, keepcopy, copypointing)
00201           casalog.post('')
00202           casalog.post('##### End Task: virtualconcat           #####')
00203           casalog.post('##########################################')
00204 
00205 
00206 # saveinputs for individule engine has no use
00207 # saveinputs should alos be removed from casa_in_py.py
00208 #
00209 #
00210 #          saveinputs = myf['saveinputs']
00211 #          saveinputs('virtualconcat', 'virtualconcat.last', myparams)
00212 #
00213 #
00214         except Exception, instance:
00215           #print '**** Error **** ',instance
00216           pass
00217 
00218         gc.collect()
00219         return result
00220 #
00221 #
00222 ##
00223 #    def paramgui(self, useGlobals=True):
00224 #        """
00225 #        Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00226 #        """
00227 #        import paramgui
00228 #
00229 #        a=inspect.stack()
00230 #        stacklevel=0
00231 #        for k in range(len(a)):
00232 #          if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00233 #            stacklevel=k
00234 #            break
00235 #        myf = sys._getframe(stacklevel).f_globals
00236 #
00237 #        if useGlobals:
00238 #            paramgui.setGlobals(myf)
00239 #        else:
00240 #            paramgui.setGlobals({})
00241 #
00242 #        paramgui.runTask('virtualconcat', myf['_ip'])
00243 #        paramgui.setGlobals({})
00244 #
00245 #
00246 #
00247 #
00248     def defaults(self, param=None):
00249         a=inspect.stack()
00250         stacklevel=0
00251         for k in range(len(a)):
00252           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00253                 stacklevel=k
00254                 break
00255         myf=sys._getframe(stacklevel).f_globals
00256         a = odict()
00257         a['vis']  = ['']
00258         a['concatvis']  = ''
00259         a['freqtol']  = ''
00260         a['dirtol']  = ''
00261         a['respectname']  = True
00262         a['visweightscale']  = []
00263         a['keepcopy']  = False
00264         a['copypointing']  = True
00265 
00266         a['async']=False
00267 
00268 ### This function sets the default values but also will return the list of
00269 ### parameters or the default value of a given parameter
00270         if(param == None):
00271                 myf['__set_default_parameters'](a)
00272         elif(param == 'paramkeys'):
00273                 return a.keys()
00274         else:
00275                 if(a.has_key(param)):
00276                    #if(type(a[param]) == dict) :
00277                    #   return a[param][len(a[param])-1]['value']
00278                    #else :
00279                       return a[param]
00280 
00281 
00282 #
00283 #
00284     def check_params(self, param=None, value=None):
00285       a=inspect.stack() 
00286       stacklevel=0
00287       for k in range(len(a)):
00288         if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00289             stacklevel=k
00290             break
00291       myf=sys._getframe(stacklevel).f_globals
00292 
00293 #      print 'param:', param, 'value:', value
00294       try :
00295          if str(type(value)) != "<type 'instance'>" :
00296             value0 = value
00297             value = myf['cu'].expandparam(param, value)
00298             matchtype = False
00299             if(type(value) == numpy.ndarray):
00300                if(type(value) == type(value0)):
00301                   myf[param] = value.tolist()
00302                else:
00303                   #print 'value:', value, 'value0:', value0
00304                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00305                   myf[param] = value0
00306                   if type(value0) != list :
00307                      matchtype = True
00308             else :
00309                myf[param] = value
00310             value = myf['cu'].verifyparam({param:value})
00311             if matchtype:
00312                value = False
00313       except Exception, instance:
00314          #ignore the exception and just return it unchecked
00315          myf[param] = value
00316       return value
00317 
00318 #
00319 #
00320     def description(self, key='virtualconcat', subkey=None):
00321         desc={'virtualconcat': 'Concatenate several visibility data sets into a multi-MS',
00322                'vis': 'List of names of input visibility files to be concatenated',
00323                'concatvis': 'Name of the output visibility file (a multi-MS)',
00324                'freqtol': 'Frequency shift tolerance for considering data as the same spwid',
00325                'dirtol': 'Direction shift tolerance for considering data as the same field',
00326                'respectname': 'If true, fields with a different name are not merged even if their direction agrees',
00327                'visweightscale': 'List of the weight scaling factors to be applied to the individual MSs',
00328                'keepcopy': 'If true, a copy of the input MSs is kept in their original place.',
00329                'copypointing': 'If true, keep the POINTING table information in the output MMS. If false, don\'t.',
00330 
00331                'async': 'If true the taskname must be started using virtualconcat(...)'
00332               }
00333 
00334         if(desc.has_key(key)) :
00335            return desc[key]
00336 
00337     def itsdefault(self, paramname) :
00338         a = {}
00339         a['vis']  = ['']
00340         a['concatvis']  = ''
00341         a['freqtol']  = ''
00342         a['dirtol']  = ''
00343         a['respectname']  = True
00344         a['visweightscale']  = []
00345         a['keepcopy']  = False
00346         a['copypointing']  = True
00347 
00348         if a.has_key(paramname) :
00349               return a[paramname]
00350 virtualconcat_pg = virtualconcat_pg_()