casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
listcal_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_listcal import listcal
00018 class listcal_cli_:
00019     __name__ = "listcal"
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__ = (listcal_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'vis':None, 'caltable':None, 'field':None, 'antenna':None, 'spw':None, 'listfile':None, 'pagerows':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, caltable=None, field=None, antenna=None, spw=None, listfile=None, pagerows=None,  async=None):
00047 
00048         """List antenna gain solutions
00049 
00050 This task lists antenna gain solutions in tabular form.  The table
00051 is organized as follows.  Solutions are output by
00052     1) Spectral window,
00053     2) Antenna,
00054     3) Time,
00055     4) Channel,
00056     5) and Polarization;
00057 where the inner-most loop is over polarization.
00058 
00059 The listcal output table contains two table headers.  The top-level header
00060 is printed each time the spectral window changes.  This header lists
00061 the spectral window ID (SpwID), the date of observation (Date), 
00062 the calibration table name (CalTable), and the measurement set name (MS name).  
00063 
00064 A lower-level header is printed when the the top-level header is printed, 
00065 when the antenna names change, and every `pagerows' of output.
00066 The lower-level header columns are described here:
00067 
00068 Column Name    Description
00069 -----------    -----------
00070 Ant            Antenna name (contains sub-columns: Amp, Phs, F)
00071 Time           Visibility timestamp corresponding to gain solution
00072 Field          Field name
00073 Chn            Channel number
00074 Amp            Complex solution amplitude
00075 Phs            Complex solution  phase
00076 F              Flag
00077 
00078 Elements of the ``F'' column contain an `F' when the datum is flagged,
00079 and ` ' (whitespace) when the datum is not flagged.
00080 
00081 Presently, the polarization mode names (for example: R, L)
00082 are not given, but the ordering of the polrization modes (left-to-right) is
00083 equivalent to the order output by task listobs (see ``Feeds'' in listobs 
00084 output).
00085 
00086 Input Parameters:
00087 
00088 vis         Name of input visibility file
00089             default: none; example: vis='ngc5921.ms'
00090             
00091 caltable    Name of input calibration table
00092             default: none; example: caltable='ngc5921.gcal'
00093             
00094 field       Select data based on field ID(s) or name(s)
00095             default: ''==>all; example: field='1'
00096             field='0~2' field ids inclusive from 0 to 2
00097             field='3C*' all field names starting with 3C
00098             
00099 antenna     Select calibration data based on antenna name
00100             default: ''-->all; example: antenna='5';
00101             antenna='5,6' antenna index 5 and 6 solutions
00102             antenna='VA05','VA06'  VLA antenna 5 and 6
00103 
00104 spw         Select spectral window(s), channel(s) to list
00105             default: '' --> All spws and channels;
00106             spw='2:34' spectral window 2, channel 34;
00107             spw='1:5,3~5:7~9' spectral window 1, channel
00108             5 and spectral windows 3 thru 5, channels
00109             7 thru 9.
00110 
00111 listfile    write output to disk; will not overwrite
00112             default: '' --> write to screen
00113 
00114 pagerows    rows per page of listing 
00115             default: 50; 0 --> do not paginate 
00116 
00117 Example:
00118 
00119 # Get path to CASA home dir
00120 pathname=os.environ.get('CASAPATH').split()[0] 
00121 # Select uv-data (FITS) file
00122 fitsdata=pathname+'/data/demo/NGC5921.fits' 
00123 # MS name; write to current directory
00124 msdata='NGC5921.ms' 
00125 # import FITS data to MS
00126 importuvfits(fitsfile=fitsdata, vis=msdata) 
00127 # Create model data for flux calibrator
00128 setjy(vis=msdata) 
00129 # Calibration table name
00130 caldata=msdata+'.bcal' 
00131 # Bandpass calibration
00132 bandpass(vis=msdata, caltable=caldata) 
00133 # List a subset of calibration factors
00134 listcal(vis=msdata, caltable=caldata, field='N5921_2, 0, 1', 
00135         antenna='1,2,5;10~14', spw='0:1,0:22~25', pagerows=0)
00136 
00137 Example Output:
00138 
00139 SpwID = 0, Date = 1995/04/13,  CalTable = NGC5921.ms.bcal (B Jones), MS name = /users/jcrossle/NRAO/casa/NGC5921.ms                                    
00140 -------------------------------------------------------------------------------------------------------------------------------------------------------
00141                               | Ant = 1                     | Ant = 2                     | Ant = 5                     | Ant = 10                    |
00142 Time       Field           Chn|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|
00143 ----------|---------------|---|--------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|
00144 09:21:46.0 1331+30500002_0   1|0.165    7.9   0.117   21.3   0.168   98.8   0.161 -116.8   0.146  -24.6   0.153 -109.7   0.163 -158.6   0.139    3.9   
00145 10:05:27.9 1445+09900002_0   1|0.260   10.3   0.185   20.0   0.266  102.3   0.250 -116.1   0.233  -20.4   0.245 -108.6   0.255 -156.5   0.217    4.1   
00146 10:09:05.3         N5921_2   1|0.047   54.2   0.030   50.7   0.057  -64.6   0.041   36.5   0.050   93.5   0.035  -13.9   0.079   97.5   0.048 -107.0   
00147                               | Ant = 11                    | Ant = 12                    | Ant = 13                    | Ant = 14                    |
00148 Time       Field           Chn|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|
00149 ----------|---------------|---|--------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|
00150 09:21:46.0 1331+30500002_0   1|0.156 -112.6   0.128   -5.5   0.156 -178.4   0.169 -146.2   0.160 -177.4   0.148  -89.1   0.173 -117.0   0.145  141.5   
00151 10:05:27.9 1445+09900002_0   1|0.243 -110.6   0.199   -5.7   0.251 -175.4   0.272 -146.9   0.249 -175.0   0.238  -89.5   0.268 -113.5   0.228  142.5   
00152 10:09:05.3         N5921_2   1|0.054   47.1   0.056  105.5   0.042  -84.9   0.043  -18.9   0.058   72.4   0.055  155.6   0.040  -35.0   0.044 -153.6   
00153 SpwID = 0, Date = 1995/04/13,  CalTable = NGC5921.ms.bcal (B Jones), MS name = /users/jcrossle/NRAO/casa/NGC5921.ms                                    
00154 -------------------------------------------------------------------------------------------------------------------------------------------------------
00155                               | Ant = 1                     | Ant = 2                     | Ant = 5                     | Ant = 10                    |
00156 Time       Field           Chn|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|
00157 ----------|---------------|---|--------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|
00158 09:21:46.0 1331+30500002_0  22|0.319    4.6   0.323   -6.8   0.311  109.6   0.315 -109.0   0.286  -26.8   0.324 -106.8   0.303 -146.6   0.303    4.3   
00159 09:21:46.0 1331+30500002_0  23|0.318    4.4   0.323   -6.8   0.309  109.7   0.315 -108.8   0.285  -26.8   0.325 -106.5   0.304 -146.2   0.304    4.6   
00160 09:21:46.0 1331+30500002_0  24|0.318    4.2   0.323   -6.6   0.309  109.8   0.316 -108.6   0.285  -26.8   0.324 -106.6   0.302 -146.1   0.304    5.0   
00161 09:21:46.0 1331+30500002_0  25|0.319    4.3   0.323   -6.6   0.308  109.5   0.315 -108.4   0.285  -26.7   0.323 -106.7   0.301 -145.9   0.303    5.1   
00162 10:05:27.9 1445+09900002_0  22|0.502    7.0   0.508   -7.9   0.483  112.2   0.499 -108.5   0.451  -24.2   0.515 -106.2   0.481 -144.1   0.489    4.6   
00163 10:05:27.9 1445+09900002_0  23|0.498    7.2   0.509   -8.2   0.489  112.6   0.502 -108.8   0.455  -23.9   0.513 -106.2   0.477 -144.0   0.480    5.0   
00164 10:05:27.9 1445+09900002_0  24|0.496    6.3   0.506   -7.1   0.487  111.9   0.502 -108.3   0.450  -23.8   0.517 -106.1   0.473 -144.6   0.478    4.0   
00165 10:05:27.9 1445+09900002_0  25|0.489    6.3   0.512   -8.2   0.483  113.0   0.498 -108.7   0.456  -24.3   0.507 -105.5   0.470 -144.4   0.476    4.3   
00166 10:09:05.3         N5921_2  22|0.089   53.9   0.084   38.8   0.135  -84.0   0.148   54.9   0.100   94.2   0.112    4.4   0.112   90.6   0.115 -124.0   
00167 10:09:05.3         N5921_2  23|0.068   50.4   0.073   31.5   0.117  -80.7   0.150   50.5   0.103   90.3   0.120    2.6   0.104  103.6   0.104 -121.5   
00168 10:09:05.3         N5921_2  24|0.068   51.4   0.080   45.1   0.125  -89.0   0.146   47.3   0.106   99.9   0.122    8.8   0.102   95.9   0.099 -121.4   
00169 10:09:05.3         N5921_2  25|0.060   45.8   0.060   42.5   0.124  -85.4   0.146   47.8   0.110   93.4   0.125    7.2   0.088  100.9   0.096 -115.9   
00170                               | Ant = 11                    | Ant = 12                    | Ant = 13                    | Ant = 14                    |
00171 Time       Field           Chn|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|  Amp    Phs F   Amp    Phs F|
00172 ----------|---------------|---|--------------|--------------|--------------|--------------|--------------|--------------|--------------|--------------|
00173 09:21:46.0 1331+30500002_0  22|0.302  -99.8   0.301  -10.5   0.341  169.8   0.350 -137.6   0.306 -167.6   0.308  -84.8   0.319 -103.6   0.316  143.7   
00174 09:21:46.0 1331+30500002_0  23|0.301  -99.9   0.302  -10.6   0.341  169.7   0.349 -138.0   0.306 -167.4   0.307  -84.5   0.318 -103.6   0.316  143.8   
00175 09:21:46.0 1331+30500002_0  24|0.300 -100.0   0.301  -10.9   0.342  169.6   0.348 -138.4   0.305 -167.4   0.306  -84.3   0.319 -103.4   0.317  143.4   
00176 09:21:46.0 1331+30500002_0  25|0.301 -100.1   0.300  -11.0   0.339  169.9   0.347 -138.5   0.305 -167.4   0.306  -84.0   0.317 -103.2   0.315  143.5   
00177 10:05:27.9 1445+09900002_0  22|0.478  -97.3   0.482   -9.7   0.535  171.3   0.544 -138.1   0.480 -165.1   0.487  -86.0   0.502 -100.2   0.503  144.6   
00178 10:05:27.9 1445+09900002_0  23|0.481  -97.4   0.479  -10.4   0.531  171.4   0.549 -138.9   0.483 -165.3   0.489  -84.3   0.498  -99.7   0.501  144.7   
00179 10:05:27.9 1445+09900002_0  24|0.482  -97.6   0.484  -10.1   0.532  172.7   0.544 -139.3   0.489 -165.3   0.476  -84.6   0.498 -100.3   0.502  144.6   
00180 10:05:27.9 1445+09900002_0  25|0.479  -98.4   0.484  -10.1   0.534  172.4   0.553 -139.0   0.481 -165.4   0.479  -84.3   0.498 -100.3   0.497  145.0   
00181 10:09:05.3         N5921_2  22|0.127   44.8   0.142  128.9   0.090  -94.4   0.090  -48.5   0.112   41.3   0.103  109.0   0.075    7.2   0.095 -120.1   
00182 10:09:05.3         N5921_2  23|0.135   43.1   0.132  126.0   0.087  -89.3   0.103  -38.2   0.112   39.3   0.100  117.8   0.076   -3.4   0.098 -113.5   
00183 10:09:05.3         N5921_2  24|0.135   49.4   0.137  136.1   0.092  -95.9   0.084  -42.7   0.104   49.9   0.120  117.6   0.087    2.9   0.097 -121.2   
00184 10:09:05.3         N5921_2  25|0.144   49.8   0.119  130.0   0.086  -96.5   0.074  -42.8   0.109   41.5   0.124  120.8   0.087    0.4   0.104 -117.3   
00185 
00186 Listed 120 antenna solutions.
00187 
00188   
00189         """
00190         if not hasattr(self, "__globals__") or self.__globals__ == None :
00191            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00192         #casac = self.__globals__['casac']
00193         casalog = self.__globals__['casalog']
00194         #casalog = casac.casac.logsink()
00195         self.__globals__['__last_task'] = 'listcal'
00196         self.__globals__['taskname'] = 'listcal'
00197         ###
00198         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00199         ###
00200         ###
00201         #Handle globals or user over-ride of arguments
00202         #
00203         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00204         useLocalDefaults = False
00205 
00206         for item in function_signature_defaults.iteritems():
00207                 key,val = item
00208                 keyVal = eval(key)
00209                 if (keyVal == None):
00210                         #user hasn't set it - use global/default
00211                         pass
00212                 else:
00213                         #user has set it - use over-ride
00214                         if (key != 'self') :
00215                            useLocalDefaults = True
00216 
00217         myparams = {}
00218         if useLocalDefaults :
00219            for item in function_signature_defaults.iteritems():
00220                key,val = item
00221                keyVal = eval(key)
00222                exec('myparams[key] = keyVal')
00223                self.parameters[key] = keyVal
00224                if (keyVal == None):
00225                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00226                    keyVal = eval(key)
00227                    if(type(keyVal) == dict) :
00228                       if len(keyVal) > 0 :
00229                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00230                       else :
00231                          exec('myparams[key] = ' + key + ' = {}')
00232 
00233         else :
00234             async = self.parameters['async']
00235             myparams['vis'] = vis = self.parameters['vis']
00236             myparams['caltable'] = caltable = self.parameters['caltable']
00237             myparams['field'] = field = self.parameters['field']
00238             myparams['antenna'] = antenna = self.parameters['antenna']
00239             myparams['spw'] = spw = self.parameters['spw']
00240             myparams['listfile'] = listfile = self.parameters['listfile']
00241             myparams['pagerows'] = pagerows = self.parameters['pagerows']
00242 
00243 
00244         result = None
00245 
00246 #
00247 #    The following is work around to avoid a bug with current python translation
00248 #
00249         mytmp = {}
00250 
00251         mytmp['vis'] = vis
00252         mytmp['caltable'] = caltable
00253         mytmp['field'] = field
00254         mytmp['antenna'] = antenna
00255         mytmp['spw'] = spw
00256         mytmp['listfile'] = listfile
00257         mytmp['pagerows'] = pagerows
00258         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00259         trec = casac.casac.utils().torecord(pathname+'listcal.xml')
00260 
00261         casalog.origin('listcal')
00262         try :
00263           #if not trec.has_key('listcal') or not casac.casac.utils().verify(mytmp, trec['listcal']) :
00264             #return False
00265 
00266           casac.casac.utils().verify(mytmp, trec['listcal'], True)
00267           scriptstr=['']
00268           saveinputs = self.__globals__['saveinputs']
00269           saveinputs('listcal', 'listcal.last', myparams, self.__globals__,scriptstr=scriptstr)
00270           if async :
00271             count = 0
00272             keybase =  time.strftime("%y%m%d.%H%M%S")
00273             key = keybase + "_" + str(count)
00274             while self.__async__.has_key(key) :
00275                count += 1
00276                key = keybase + "_" + str(count)
00277             result = tm.execute('listcal', vis, caltable, field, antenna, spw, listfile, pagerows)
00278             print "Use: "
00279             print "      tm.retrieve(return_value) # to retrieve the status"
00280             print 
00281             self.rkey = key
00282             self.__async__[key] = result
00283           else :
00284               tname = 'listcal'
00285               spaces = ' '*(18-len(tname))
00286               casalog.post('\n##########################################'+
00287                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00288               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00289               result = listcal(vis, caltable, field, antenna, spw, listfile, pagerows)
00290               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00291                            '\n##########################################')
00292 
00293         except Exception, instance:
00294           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00295              raise
00296           else :
00297              #print '**** Error **** ',instance
00298              tname = 'listcal'
00299              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00300              pass
00301 
00302         gc.collect()
00303         return result
00304 #
00305 #
00306 #
00307     def paramgui(self, useGlobals=True, ipython_globals=None):
00308         """
00309         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00310         """
00311         import paramgui
00312         if not hasattr(self, "__globals__") or self.__globals__ == None :
00313            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00314 
00315         if useGlobals:
00316             if ipython_globals == None:
00317                 myf=self.__globals__
00318             else:
00319                 myf=ipython_globals
00320 
00321             paramgui.setGlobals(myf)
00322         else:
00323             paramgui.setGlobals({})
00324 
00325         paramgui.runTask('listcal', myf['_ip'])
00326         paramgui.setGlobals({})
00327 
00328 #
00329 #
00330 #
00331     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00332         if not hasattr(self, "__globals__") or self.__globals__ == None :
00333            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00334         if ipython_globals == None:
00335             myf=self.__globals__
00336         else:
00337             myf=ipython_globals
00338 
00339         a = odict()
00340         a['vis']  = ''
00341         a['caltable']  = ''
00342         a['field']  = ''
00343         a['antenna']  = ''
00344         a['spw']  = ''
00345         a['listfile']  = ''
00346         a['pagerows']  = 50
00347 
00348         a['async']=False
00349 
00350 ### This function sets the default values but also will return the list of
00351 ### parameters or the default value of a given parameter
00352         if(param == None):
00353                 myf['__set_default_parameters'](a)
00354         elif(param == 'paramkeys'):
00355                 return a.keys()
00356         else:
00357             if(paramvalue==None and subparam==None):
00358                if(a.has_key(param)):
00359                   return a[param]
00360                else:
00361                   return self.itsdefault(param)
00362             else:
00363                retval=a[param]
00364                if(type(a[param])==dict):
00365                   for k in range(len(a[param])):
00366                      valornotval='value'
00367                      if(a[param][k].has_key('notvalue')):
00368                         valornotval='notvalue'
00369                      if((a[param][k][valornotval])==paramvalue):
00370                         retval=a[param][k].copy()
00371                         retval.pop(valornotval)
00372                         if(subparam != None):
00373                            if(retval.has_key(subparam)):
00374                               retval=retval[subparam]
00375                            else:
00376                               retval=self.itsdefault(subparam)
00377                      else:
00378                         retval=self.itsdefault(subparam)
00379                return retval
00380 
00381 
00382 #
00383 #
00384     def check_params(self, param=None, value=None, ipython_globals=None):
00385       if ipython_globals == None:
00386           myf=self.__globals__
00387       else:
00388           myf=ipython_globals
00389 #      print 'param:', param, 'value:', value
00390       try :
00391          if str(type(value)) != "<type 'instance'>" :
00392             value0 = value
00393             value = myf['cu'].expandparam(param, value)
00394             matchtype = False
00395             if(type(value) == numpy.ndarray):
00396                if(type(value) == type(value0)):
00397                   myf[param] = value.tolist()
00398                else:
00399                   #print 'value:', value, 'value0:', value0
00400                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00401                   myf[param] = value0
00402                   if type(value0) != list :
00403                      matchtype = True
00404             else :
00405                myf[param] = value
00406             value = myf['cu'].verifyparam({param:value})
00407             if matchtype:
00408                value = False
00409       except Exception, instance:
00410          #ignore the exception and just return it unchecked
00411          myf[param] = value
00412       return value
00413 #
00414 #
00415     def description(self, key='listcal', subkey=None):
00416         desc={'listcal': 'List antenna gain solutions',
00417                'vis': 'Name of input visibility file',
00418                'caltable': 'Input calibration table to list',
00419                'field': 'Field name or index; \'\'==>all',
00420                'antenna': 'Antenna name or index; \'\'==>all; antenna=\'3\'',
00421                'spw': 'Spectral window and channel: \'\'==>all; spw=\'5:0~10\'',
00422                'listfile': 'Disk file to write output: \'\'==>to terminal',
00423                'pagerows': 'Rows per page',
00424 
00425                'async': 'If true the taskname must be started using listcal(...)'
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['caltable']  = ''
00435         a['field']  = ''
00436         a['antenna']  = ''
00437         a['spw']  = ''
00438         a['listfile']  = ''
00439         a['pagerows']  = 50
00440 
00441         #a = sys._getframe(len(inspect.stack())-1).f_globals
00442 
00443         if a.has_key(paramname) :
00444               return a[paramname]
00445 listcal_cli = listcal_cli_()