casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
concat_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_concat import concat
00018 class concat_cli_:
00019     __name__ = "concat"
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__ = (concat_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'vis':None, 'concatvis':None, 'freqtol':None, 'dirtol':None, 'respectname':None, 'timesort':None, 'copypointing':None, 'visweightscale':None, 'createmms':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, concatvis=None, freqtol=None, dirtol=None, respectname=None, timesort=None, copypointing=None, visweightscale=None, createmms=None,  async=None):
00047 
00048         """Concatenate several visibility data sets.
00049 
00050 The list of data sets given in the vis argument are concatenated into an output
00051 data set in concatvis.  If concatvis already exists (e.g., it is the same as the
00052 first input data set), then the other input data sets will be appended to the
00053 concatvis data set.  There is no limit to the number of input data sets.
00054 
00055 If none of the input data sets have any scratch columns (model and corrected
00056 columns), none are created in the concatvis.  Otherwise these columns are
00057 created on output and initialized to their default value (1 in model column,
00058 data in corrected column) for those data with no input columns.
00059 
00060 Spectral windows for each data set with the same chanelization, and within a
00061 specified frequency tolerance of another data set will be combined into one
00062 spectral window.
00063 
00064 A field position in one data set that is within a specified direction tolerance
00065 of another field position in any other data set will be combined into one
00066 field.  The field names need not be the same---only their position is used.
00067 
00068 Each appended dataset is assigned a new observation id.
00069 
00070 Keyword arguments:
00071 vis -- Name of input visibility files to be combined
00072         default: none; example: vis = ['src2.ms','ngc5921.ms','ngc315.ms']
00073 concatvis -- Name of visibility file that will contain the concatenated data
00074         note: if this file exits on disk then the input files are 
00075               added to this file.  Otherwise the new file contains  
00076               the concatenated data.  Be careful here when concatenating to
00077               an existing file.
00078         default: none; example: concatvis='src2.ms'
00079                  example: concatvis='outvis.ms'
00080 
00081         other examples: 
00082            concat(vis=['src2.ms','ngc5921.ms'], concatvis='src2.ms')
00083                will concatenate 'ngc5921.ms' into 'src2.ms', and the original
00084                src2.ms is lost
00085 
00086            concat(vis=['src2.ms','ngc5921.ms'], concatvis='out.ms') 
00087                will concatenate 'ngc5921.ms' and 'src2.ms' into a file named 
00088                'out.ms'; the original 'ngc5921.ms' and 'src2.ms' are untouched.
00089 
00090            concat(vis=['v1.ms','v2.ms'], concatvis = 'vall.ms')
00091               then
00092            concat(vis=['v3.ms','v4.ms'], concatvis = 'vall.ms')
00093              vall.ms will contains v1.ms+v2.ms+v3.ms+v4.ms
00094 
00095      Note: run flagmanager to save flags in the concatvis
00096 
00097 freqtol -- Frequency shift tolerance for considering data to be in the same
00098            spwid.  The number of channels must also be the same.
00099         default: ''  do not combine unless frequencies are equal
00100         example: freqtol='10MHz' will not combine spwid unless they are
00101            within 10 MHz.
00102         Note: This option is useful to conbine spectral windows with very slight
00103            frequency differences caused by Doppler tracking, for example.
00104 
00105 dirtol -- Direction shift tolerance for considering data as the same field
00106         default: '' means always combine.
00107         example: dirtol='1.arcsec' will not combine data for a field unless
00108            their phase center differ by less than 1 arcsec.  If the field names
00109            are different in the input data sets, the name in the output data
00110            set will be the first relevant data set in the list.
00111 
00112 respectname -- If true, fields with a different name are not merged even if their 
00113         direction agrees (within dirtol)
00114         default: False
00115 
00116 timesort -- If true, the output visibility table will be sorted in time.
00117         default: false.  Data in order as read in.
00118         example: timesort=true
00119      Note: There is no constraint on data that is simultaneously observed for
00120         more than one field; for example multi-source correlation of VLBA data.
00121 
00122 copypointing -- Make a proper copy of the POINTING subtable (can be time consuming).
00123         If False, the result is an empty POINTING table.
00124         default: True
00125 
00126 visweightscale -- The weights of the individual MSs will be scaled in the concatenated
00127         output MS by the factors in this list. Useful for handling heterogeneous arrays.
00128         Use plotms to inspect the "Wt" column as a reference for determining the scaling 
00129         factors. See the cookbook for more details.
00130         example: [1.,3.,3.] - scale the weights of the second and third MS by a factor 3.
00131         default: [] (empty list) - no scaling
00132 
00133 createmms -- disabled. Please use task virtualconcat.
00134          default: False
00135 
00136 
00137         """
00138         if not hasattr(self, "__globals__") or self.__globals__ == None :
00139            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00140         #casac = self.__globals__['casac']
00141         casalog = self.__globals__['casalog']
00142         #casalog = casac.casac.logsink()
00143         self.__globals__['__last_task'] = 'concat'
00144         self.__globals__['taskname'] = 'concat'
00145         ###
00146         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00147         ###
00148         ###
00149         #Handle globals or user over-ride of arguments
00150         #
00151         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00152         useLocalDefaults = False
00153 
00154         for item in function_signature_defaults.iteritems():
00155                 key,val = item
00156                 keyVal = eval(key)
00157                 if (keyVal == None):
00158                         #user hasn't set it - use global/default
00159                         pass
00160                 else:
00161                         #user has set it - use over-ride
00162                         if (key != 'self') :
00163                            useLocalDefaults = True
00164 
00165         myparams = {}
00166         if useLocalDefaults :
00167            for item in function_signature_defaults.iteritems():
00168                key,val = item
00169                keyVal = eval(key)
00170                exec('myparams[key] = keyVal')
00171                self.parameters[key] = keyVal
00172                if (keyVal == None):
00173                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00174                    keyVal = eval(key)
00175                    if(type(keyVal) == dict) :
00176                       if len(keyVal) > 0 :
00177                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00178                       else :
00179                          exec('myparams[key] = ' + key + ' = {}')
00180 
00181         else :
00182             async = self.parameters['async']
00183             myparams['vis'] = vis = self.parameters['vis']
00184             myparams['concatvis'] = concatvis = self.parameters['concatvis']
00185             myparams['freqtol'] = freqtol = self.parameters['freqtol']
00186             myparams['dirtol'] = dirtol = self.parameters['dirtol']
00187             myparams['respectname'] = respectname = self.parameters['respectname']
00188             myparams['timesort'] = timesort = self.parameters['timesort']
00189             myparams['copypointing'] = copypointing = self.parameters['copypointing']
00190             myparams['visweightscale'] = visweightscale = self.parameters['visweightscale']
00191             myparams['createmms'] = createmms = self.parameters['createmms']
00192 
00193         if type(vis)==str: vis=[vis]
00194         if type(visweightscale)==float: visweightscale=[visweightscale]
00195 
00196         result = None
00197 
00198 #
00199 #    The following is work around to avoid a bug with current python translation
00200 #
00201         mytmp = {}
00202 
00203         mytmp['vis'] = vis
00204         mytmp['concatvis'] = concatvis
00205         mytmp['freqtol'] = freqtol
00206         mytmp['dirtol'] = dirtol
00207         mytmp['respectname'] = respectname
00208         mytmp['timesort'] = timesort
00209         mytmp['copypointing'] = copypointing
00210         mytmp['visweightscale'] = visweightscale
00211         mytmp['createmms'] = createmms
00212         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00213         trec = casac.casac.utils().torecord(pathname+'concat.xml')
00214 
00215         casalog.origin('concat')
00216         try :
00217           #if not trec.has_key('concat') or not casac.casac.utils().verify(mytmp, trec['concat']) :
00218             #return False
00219 
00220           casac.casac.utils().verify(mytmp, trec['concat'], True)
00221           scriptstr=['']
00222           saveinputs = self.__globals__['saveinputs']
00223           saveinputs('concat', 'concat.last', myparams, self.__globals__,scriptstr=scriptstr)
00224           if async :
00225             count = 0
00226             keybase =  time.strftime("%y%m%d.%H%M%S")
00227             key = keybase + "_" + str(count)
00228             while self.__async__.has_key(key) :
00229                count += 1
00230                key = keybase + "_" + str(count)
00231             result = tm.execute('concat', vis, concatvis, freqtol, dirtol, respectname, timesort, copypointing, visweightscale, createmms)
00232             print "Use: "
00233             print "      tm.retrieve(return_value) # to retrieve the status"
00234             print 
00235             self.rkey = key
00236             self.__async__[key] = result
00237           else :
00238               tname = 'concat'
00239               spaces = ' '*(18-len(tname))
00240               casalog.post('\n##########################################'+
00241                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00242               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00243               result = concat(vis, concatvis, freqtol, dirtol, respectname, timesort, copypointing, visweightscale, createmms)
00244               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00245                            '\n##########################################')
00246 
00247         except Exception, instance:
00248           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00249              raise
00250           else :
00251              #print '**** Error **** ',instance
00252              tname = 'concat'
00253              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00254              pass
00255 
00256         gc.collect()
00257         return result
00258 #
00259 #
00260 #
00261     def paramgui(self, useGlobals=True, ipython_globals=None):
00262         """
00263         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00264         """
00265         import paramgui
00266         if not hasattr(self, "__globals__") or self.__globals__ == None :
00267            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00268 
00269         if useGlobals:
00270             if ipython_globals == None:
00271                 myf=self.__globals__
00272             else:
00273                 myf=ipython_globals
00274 
00275             paramgui.setGlobals(myf)
00276         else:
00277             paramgui.setGlobals({})
00278 
00279         paramgui.runTask('concat', myf['_ip'])
00280         paramgui.setGlobals({})
00281 
00282 #
00283 #
00284 #
00285     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00286         if not hasattr(self, "__globals__") or self.__globals__ == None :
00287            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00288         if ipython_globals == None:
00289             myf=self.__globals__
00290         else:
00291             myf=ipython_globals
00292 
00293         a = odict()
00294         a['vis']  = ['']
00295         a['concatvis']  = ''
00296         a['freqtol']  = ''
00297         a['dirtol']  = ''
00298         a['respectname']  = False
00299         a['timesort']  = False
00300         a['copypointing']  = True
00301         a['visweightscale']  = []
00302         a['createmms']  = False
00303 
00304         a['async']=False
00305 
00306 ### This function sets the default values but also will return the list of
00307 ### parameters or the default value of a given parameter
00308         if(param == None):
00309                 myf['__set_default_parameters'](a)
00310         elif(param == 'paramkeys'):
00311                 return a.keys()
00312         else:
00313             if(paramvalue==None and subparam==None):
00314                if(a.has_key(param)):
00315                   return a[param]
00316                else:
00317                   return self.itsdefault(param)
00318             else:
00319                retval=a[param]
00320                if(type(a[param])==dict):
00321                   for k in range(len(a[param])):
00322                      valornotval='value'
00323                      if(a[param][k].has_key('notvalue')):
00324                         valornotval='notvalue'
00325                      if((a[param][k][valornotval])==paramvalue):
00326                         retval=a[param][k].copy()
00327                         retval.pop(valornotval)
00328                         if(subparam != None):
00329                            if(retval.has_key(subparam)):
00330                               retval=retval[subparam]
00331                            else:
00332                               retval=self.itsdefault(subparam)
00333                      else:
00334                         retval=self.itsdefault(subparam)
00335                return retval
00336 
00337 
00338 #
00339 #
00340     def check_params(self, param=None, value=None, ipython_globals=None):
00341       if ipython_globals == None:
00342           myf=self.__globals__
00343       else:
00344           myf=ipython_globals
00345 #      print 'param:', param, 'value:', value
00346       try :
00347          if str(type(value)) != "<type 'instance'>" :
00348             value0 = value
00349             value = myf['cu'].expandparam(param, value)
00350             matchtype = False
00351             if(type(value) == numpy.ndarray):
00352                if(type(value) == type(value0)):
00353                   myf[param] = value.tolist()
00354                else:
00355                   #print 'value:', value, 'value0:', value0
00356                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00357                   myf[param] = value0
00358                   if type(value0) != list :
00359                      matchtype = True
00360             else :
00361                myf[param] = value
00362             value = myf['cu'].verifyparam({param:value})
00363             if matchtype:
00364                value = False
00365       except Exception, instance:
00366          #ignore the exception and just return it unchecked
00367          myf[param] = value
00368       return value
00369 #
00370 #
00371     def description(self, key='concat', subkey=None):
00372         desc={'concat': 'Concatenate several visibility data sets.',
00373                'vis': 'Name of input visibility files to be concatenated',
00374                'concatvis': 'Name of output visibility file',
00375                'freqtol': 'Frequency shift tolerance for considering data as the same spwid',
00376                'dirtol': 'Direction shift tolerance for considering data as the same field',
00377                'respectname': 'If true, fields with a different name are not merged even if their direction agrees',
00378                'timesort': 'If true, sort by TIME in ascending order',
00379                'copypointing': 'Copy all rows of the POINTING table.',
00380                'visweightscale': 'List of the weight scaling factors to be applied to the individual MSs',
00381                'createmms': 'Should this create a multi-MS output',
00382 
00383                'async': 'If true the taskname must be started using concat(...)'
00384               }
00385 
00386         if(desc.has_key(key)) :
00387            return desc[key]
00388 
00389     def itsdefault(self, paramname) :
00390         a = {}
00391         a['vis']  = ['']
00392         a['concatvis']  = ''
00393         a['freqtol']  = ''
00394         a['dirtol']  = ''
00395         a['respectname']  = False
00396         a['timesort']  = False
00397         a['copypointing']  = True
00398         a['visweightscale']  = []
00399         a['createmms']  = False
00400 
00401         #a = sys._getframe(len(inspect.stack())-1).f_globals
00402 
00403         if a.has_key(paramname) :
00404               return a[paramname]
00405 concat_cli = concat_cli_()