casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
accum_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_accum import accum
00018 class accum_cli_:
00019     __name__ = "accum"
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__ = (accum_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'vis':None, 'tablein':None, 'incrtable':None, 'caltable':None, 'field':None, 'calfield':None, 'interp':None, 'accumtime':None, 'spwmap':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, tablein=None, incrtable=None, caltable=None, field=None, calfield=None, interp=None, accumtime=None, spwmap=None,  async=None):
00047 
00048         """Accumulate incremental calibration solutions into a calibration table
00049 
00050        Accum will interpolate and extrapolate a temporal calibration
00051        table onto a new table that has a regularly-space time grid.
00052 
00053        The first run of accum defines the time grid and fills this
00054        table with the results from the input table.
00055 
00056        Subsequent use of accum will combine additional calibration
00057        tables onto the same grid of the initial accum table to obtain
00058        an output accum table.  See below for a concrete example.
00059 
00060 
00061      Keyword arguments:
00062 
00063      vis -- Name of input visibility file
00064              default: none.  example: vis='ngc5921.ms'
00065      tablein -- Input cumulative calibration table.
00066              default: ''  means none
00067              On first execution of accum, tablein=''
00068              and accumtime is used to generate tablein with
00069              the specified time gridding.
00070      accumtime -- The time separation when making tablein.
00071              default: 1.0  (1 second).  This time should not be
00072              less than the visibiility sampling time, but should
00073              be less than about 30% of a typical scan length.
00074      incrtable -- The calibration data to be interpolated onto the
00075              tablein file.
00076              default: ''.  Must be specified
00077      caltable -- The output cumulated calibration file.
00078              default: ''  means use tablein as the output file
00079              
00080      field -- Select field(s) from tablein to process.
00081               ['go listobs' to obtain the list id's or names]
00082             default: ''= all fields
00083             If field string is a non-negative integer, it is assumed to
00084                be a field index otherwise, it is assumed to be a field name
00085             field='0~2'; field ids 0,1,2
00086             field='0,4,5~7'; field ids 0,4,5,6,7
00087             field='3C286,3C295'; field named 3C286 and 3C295
00088             field = '3,4C*'; field id 3, all names starting with 4C
00089      calfield -- Select field(s) from incrtable to process.
00090             default: '' = all fields
00091      interp -- Interpolation type (in time[,freq]) to use for each gaintable.
00092                 When frequency interpolation is relevant (B, Df, Xf),
00093                 separate time-dependent and freq-dependent interp
00094                 types with a comma (freq _after_ the comma).                
00095                 Specifications for frequency are ignored when the
00096                 calibration table has no channel-dependence.
00097                 Time-dependent interp options ending in 'PD' enable a
00098                 "phase delay" correction per spw for non-channel-dependent
00099                 calibration types.
00100                 default: '' --> 'linear,linear' for all gaintable(s)
00101                 example: interp='nearest'   (in time, freq-dep will be
00102                                              linear, if relevant)
00103                          interp='linear,cubic'  (linear in time, cubic
00104                                                  in freq)
00105                          interp=',spline'  (spline in freq; linear in
00106                                             time by default)
00107                          interp=['nearest,spline','linear']  (for multiple gaintables)
00108                 Options: Time: 'nearest', 'linear'
00109                          Freq: 'nearest', 'linear', 'cubic', 'spline'
00110      spwmap -- Spectral windows combinations to form for gaintable(s)
00111             default: [] (apply solutions from each spw to that spw only)
00112             Example:  spwmap=[0,0,1,1] means apply the caltable solutions
00113                       from spw = 0 to the spw 0,1 and spw 1 to spw 2,3.
00114                       spwmap=[[0,0,1,1],[0,1,0,1]]  (for multiple gaintables)
00115      async -- Run task in a separate process 
00116              default: False; example: async=True
00117 
00118      Examples:
00119 
00120        Create an accum table with 10-sec sampling, filling it with the calibration
00121           in 'first_cal' with the desired interpolation.
00122 
00123            taskname = 'accum'
00124              default()
00125              vis = 'mydata.ms'
00126              tablein = ''
00127              accumtime = 10
00128              incrtable = 'first_cal'
00129              caltable = 'accum1_cal'
00130              accum()
00131 
00132        If you plot 'accum1_cal' with plotcal, you can see how the incrtable was
00133              interpolated.
00134 
00135        Continue accumulating calibrations in accum1_cal from 'second_cal'
00136 
00137            taskname = 'accum'
00138              default()
00139              vis = 'mydata.ms'
00140              tablein = 'accum1_cal'
00141              incrtable = 'second_cal'
00142              caltable = 'accum1_cal'
00143              accum()
00144 
00145        Incorporate gaincurve, opacity, parang once and for all
00146 
00147           taskname= 'gaincal'
00148              vis = 'mydata.ms'
00149              caltable = 'first_cal'
00150                etc
00151              gaincurve = T
00152              opacity = 0.11
00153              parang = T
00154              gaincal()
00155 
00156            taskname = 'accum'
00157              default()
00158              vis = 'mydata.ms'
00159              tablein = ''
00160              accumtime = 10
00161              incrtable = 'first_cal'
00162              caltable = 'accum1_cal'
00163              accum()
00164 
00165          accum1_cal with contain not only the gaincal solution, but the gaincurve,
00166              opacity and parang correction.  Hence, when applying accum1_cal
00167              on the fly for subsequent calibrations (gaincal, bandpass) or applying
00168              the calibrations, then
00169                 gaincurve=F; opacity = 0.0; parang = F  should be used
00170      
00171   
00172  
00173         """
00174         if not hasattr(self, "__globals__") or self.__globals__ == None :
00175            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00176         #casac = self.__globals__['casac']
00177         casalog = self.__globals__['casalog']
00178         #casalog = casac.casac.logsink()
00179         self.__globals__['__last_task'] = 'accum'
00180         self.__globals__['taskname'] = 'accum'
00181         ###
00182         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00183         ###
00184         ###
00185         #Handle globals or user over-ride of arguments
00186         #
00187         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00188         useLocalDefaults = False
00189 
00190         for item in function_signature_defaults.iteritems():
00191                 key,val = item
00192                 keyVal = eval(key)
00193                 if (keyVal == None):
00194                         #user hasn't set it - use global/default
00195                         pass
00196                 else:
00197                         #user has set it - use over-ride
00198                         if (key != 'self') :
00199                            useLocalDefaults = True
00200 
00201         myparams = {}
00202         if useLocalDefaults :
00203            for item in function_signature_defaults.iteritems():
00204                key,val = item
00205                keyVal = eval(key)
00206                exec('myparams[key] = keyVal')
00207                self.parameters[key] = keyVal
00208                if (keyVal == None):
00209                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00210                    keyVal = eval(key)
00211                    if(type(keyVal) == dict) :
00212                       if len(keyVal) > 0 :
00213                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00214                       else :
00215                          exec('myparams[key] = ' + key + ' = {}')
00216 
00217         else :
00218             async = self.parameters['async']
00219             myparams['vis'] = vis = self.parameters['vis']
00220             myparams['tablein'] = tablein = self.parameters['tablein']
00221             myparams['incrtable'] = incrtable = self.parameters['incrtable']
00222             myparams['caltable'] = caltable = self.parameters['caltable']
00223             myparams['field'] = field = self.parameters['field']
00224             myparams['calfield'] = calfield = self.parameters['calfield']
00225             myparams['interp'] = interp = self.parameters['interp']
00226             myparams['accumtime'] = accumtime = self.parameters['accumtime']
00227             myparams['spwmap'] = spwmap = self.parameters['spwmap']
00228 
00229         if type(field)==str: field=[field]
00230         if type(calfield)==str: calfield=[calfield]
00231         if type(spwmap)==int: spwmap=[spwmap]
00232 
00233         result = None
00234 
00235 #
00236 #    The following is work around to avoid a bug with current python translation
00237 #
00238         mytmp = {}
00239 
00240         mytmp['vis'] = vis
00241         mytmp['tablein'] = tablein
00242         mytmp['incrtable'] = incrtable
00243         mytmp['caltable'] = caltable
00244         mytmp['field'] = field
00245         mytmp['calfield'] = calfield
00246         mytmp['interp'] = interp
00247         mytmp['accumtime'] = accumtime
00248         mytmp['spwmap'] = spwmap
00249         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00250         trec = casac.casac.utils().torecord(pathname+'accum.xml')
00251 
00252         casalog.origin('accum')
00253         try :
00254           #if not trec.has_key('accum') or not casac.casac.utils().verify(mytmp, trec['accum']) :
00255             #return False
00256 
00257           casac.casac.utils().verify(mytmp, trec['accum'], True)
00258           scriptstr=['']
00259           saveinputs = self.__globals__['saveinputs']
00260           saveinputs('accum', 'accum.last', myparams, self.__globals__,scriptstr=scriptstr)
00261           if async :
00262             count = 0
00263             keybase =  time.strftime("%y%m%d.%H%M%S")
00264             key = keybase + "_" + str(count)
00265             while self.__async__.has_key(key) :
00266                count += 1
00267                key = keybase + "_" + str(count)
00268             result = tm.execute('accum', vis, tablein, incrtable, caltable, field, calfield, interp, accumtime, spwmap)
00269             print "Use: "
00270             print "      tm.retrieve(return_value) # to retrieve the status"
00271             print 
00272             self.rkey = key
00273             self.__async__[key] = result
00274           else :
00275               tname = 'accum'
00276               spaces = ' '*(18-len(tname))
00277               casalog.post('\n##########################################'+
00278                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00279               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00280               result = accum(vis, tablein, incrtable, caltable, field, calfield, interp, accumtime, spwmap)
00281               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00282                            '\n##########################################')
00283 
00284         except Exception, instance:
00285           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00286              raise
00287           else :
00288              #print '**** Error **** ',instance
00289              tname = 'accum'
00290              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00291              pass
00292 
00293         gc.collect()
00294         return result
00295 #
00296 #
00297 #
00298     def paramgui(self, useGlobals=True, ipython_globals=None):
00299         """
00300         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00301         """
00302         import paramgui
00303         if not hasattr(self, "__globals__") or self.__globals__ == None :
00304            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00305 
00306         if useGlobals:
00307             if ipython_globals == None:
00308                 myf=self.__globals__
00309             else:
00310                 myf=ipython_globals
00311 
00312             paramgui.setGlobals(myf)
00313         else:
00314             paramgui.setGlobals({})
00315 
00316         paramgui.runTask('accum', myf['_ip'])
00317         paramgui.setGlobals({})
00318 
00319 #
00320 #
00321 #
00322     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00323         if not hasattr(self, "__globals__") or self.__globals__ == None :
00324            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00325         if ipython_globals == None:
00326             myf=self.__globals__
00327         else:
00328             myf=ipython_globals
00329 
00330         a = odict()
00331         a['vis']  = ''
00332         a['tablein']  = ''
00333         a['incrtable']  = ''
00334         a['caltable']  = ''
00335         a['field']  = ['']
00336         a['calfield']  = ['']
00337         a['interp']  = 'linear'
00338         a['spwmap']  = [-1]
00339 
00340         a['async']=False
00341         a['tablein'] = {
00342                     0:odict([{'value':''}, {'accumtime':1.0}])}
00343 
00344 ### This function sets the default values but also will return the list of
00345 ### parameters or the default value of a given parameter
00346         if(param == None):
00347                 myf['__set_default_parameters'](a)
00348         elif(param == 'paramkeys'):
00349                 return a.keys()
00350         else:
00351             if(paramvalue==None and subparam==None):
00352                if(a.has_key(param)):
00353                   return a[param]
00354                else:
00355                   return self.itsdefault(param)
00356             else:
00357                retval=a[param]
00358                if(type(a[param])==dict):
00359                   for k in range(len(a[param])):
00360                      valornotval='value'
00361                      if(a[param][k].has_key('notvalue')):
00362                         valornotval='notvalue'
00363                      if((a[param][k][valornotval])==paramvalue):
00364                         retval=a[param][k].copy()
00365                         retval.pop(valornotval)
00366                         if(subparam != None):
00367                            if(retval.has_key(subparam)):
00368                               retval=retval[subparam]
00369                            else:
00370                               retval=self.itsdefault(subparam)
00371                      else:
00372                         retval=self.itsdefault(subparam)
00373                return retval
00374 
00375 
00376 #
00377 #
00378     def check_params(self, param=None, value=None, ipython_globals=None):
00379       if ipython_globals == None:
00380           myf=self.__globals__
00381       else:
00382           myf=ipython_globals
00383 #      print 'param:', param, 'value:', value
00384       try :
00385          if str(type(value)) != "<type 'instance'>" :
00386             value0 = value
00387             value = myf['cu'].expandparam(param, value)
00388             matchtype = False
00389             if(type(value) == numpy.ndarray):
00390                if(type(value) == type(value0)):
00391                   myf[param] = value.tolist()
00392                else:
00393                   #print 'value:', value, 'value0:', value0
00394                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00395                   myf[param] = value0
00396                   if type(value0) != list :
00397                      matchtype = True
00398             else :
00399                myf[param] = value
00400             value = myf['cu'].verifyparam({param:value})
00401             if matchtype:
00402                value = False
00403       except Exception, instance:
00404          #ignore the exception and just return it unchecked
00405          myf[param] = value
00406       return value
00407 #
00408 #
00409     def description(self, key='accum', subkey=None):
00410         desc={'accum': 'Accumulate incremental calibration solutions into a calibration table',
00411                'vis': 'Name of input visibility file',
00412                'tablein': 'Input cumulative calibration table; use \'\' on first run',
00413                'incrtable': 'Input incremental calibration table to add',
00414                'caltable': 'Output (cumulative) calibration table',
00415                'field': 'List of field names to process from tablein',
00416                'calfield': 'List of field names to use from incrtable.',
00417                'interp': 'Interpolation mode to use for resampling incrtable solutions',
00418                'accumtime': 'Time-interval when create cumulative table',
00419                'spwmap': 'Spectral window combinations to apply',
00420 
00421                'async': 'If true the taskname must be started using accum(...)'
00422               }
00423 
00424 #
00425 # Set subfields defaults if needed
00426 #
00427 
00428         if(desc.has_key(key)) :
00429            return desc[key]
00430 
00431     def itsdefault(self, paramname) :
00432         a = {}
00433         a['vis']  = ''
00434         a['tablein']  = ''
00435         a['incrtable']  = ''
00436         a['caltable']  = ''
00437         a['field']  = ['']
00438         a['calfield']  = ['']
00439         a['interp']  = 'linear'
00440         a['accumtime']  = 1.0
00441         a['spwmap']  = [-1]
00442 
00443         #a = sys._getframe(len(inspect.stack())-1).f_globals
00444 
00445         if self.parameters['tablein']  == '':
00446             a['accumtime'] = 1.0
00447 
00448         if a.has_key(paramname) :
00449               return a[paramname]
00450 accum_cli = accum_cli_()