casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
gencal_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_gencal import gencal
00018 class gencal_cli_:
00019     __name__ = "gencal"
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__ = (gencal_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'vis':None, 'caltable':None, 'caltype':None, 'spw':None, 'antenna':None, 'pol':None, 'parameter':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, caltype=None, spw=None, antenna=None, pol=None, parameter=None,  async=None):
00047 
00048         """Specify Calibration Values of Various Types
00049 
00050      The gencal task provides a means of specifying antenna-based
00051      calibration values manually.  The values are put in designated
00052      tables and applied to the data using applycal.  Several
00053      specialized calibrations are also generated with gencal.
00054 
00055      Current antenna-based gencal options (caltype) are:
00056         'amp'= amplitude correction
00057         'ph' = phase correction
00058         'sbd'= single-band delay (phase-frequency slope for each spw)
00059         'mbd'= multi-band delay (phase-frequency slope over all spw)
00060         'antpos' = ITRF antenna position corrections
00061         'antposvla' = VLA-centric antenna position corrections 
00062         'tsys' = Tsys from the SYSCAL table (ALMA)
00063         'swpow' = EVLA switched-power gains (experimental)
00064         'evlagain' (='swpow') (this syntax will deprecate)
00065         'rq' = EVLA requantizer gains _only_
00066         'swp/rq' = EVLA switched-power gains divided by requantizer gain
00067         'opac' = Tropospheric opacity
00068         'gc' = Gain curve (zenith-angle-dependent gain) (VLA only)
00069         'eff' = Antenna efficiency (sqrt(K/Jy)) (VLA only)
00070         'gceff' = Gain curve and efficiency (VLA only)
00071 
00072      The calibration specifications are not time-variable in the
00073      present implementation.  Calibration values can be assigned to
00074      each spw, antenna and pol selection, where applicable.  The 
00075      same caltable can be specified for multipl runs of gencal, 
00076      in which case the specified parameters will be incorporated 
00077      cumulatively.  E.g., amplitude parameters (caltype='amp')
00078      multiply and phase-like parameters ('ph', 'sbd','mbd','antpos') 
00079      add.  'amp' and 'ph' parameters can be incorporated into the 
00080      same caltable (in separate runs), but each of the other types
00081      require their own unique caltable.   A mechanism for
00082      specifying manual corrections via a text file will be provided in
00083      the future.
00084 
00085      The caltables are applied to the data by using applycal.  Other
00086      calibration tables must also be present, if applicable.
00087 
00088      For antenna position corrections (caltype='antpos'), the antenna 
00089      position offsets are specified in the ITRF frame. For EVLA, automated 
00090      lookup of the antenna position corrections is enabled when antenna is 
00091      unspecified (antenna='') for this caltype. Note that this requires 
00092      internet connection to access the EVLA antenna position correction 
00093      site. If no corrections are found, it will throw an exception 
00094      with the message, "no offsets found. no caltable created". The task
00095      may terminate with a SEVERE error message and may sound alarming, but 
00096      it simply means that it cannot produce the caltable.
00097      For VLA position corrections in the VLA-centric frame, use 
00098      caltype='antposvla', and gencal will rotate them to ITRF before 
00099      storing them in the output caltable.
00100 
00101      For Tsys (caltype='tsys', for ALMA) and EVLA switched power
00102      corrections (caltype='swpow'), the calibration parameters are
00103      derived from information contained in MS subtables.  In these
00104      cases, specification of spw, antenna, pol, and parameter will be
00105      ignored.
00106 
00107      EVLA switched power calibration is supported in three modes:  
00108      'swpow' (formerly 'evlagain', a syntax which will 
00109        deprecate) yields the formal EVLA switched power calibration 
00110        which describes voltage gain as sqrt(Pdif/Tcal) (used to 
00111        correct the visibility data) and Tsys as Psum*Tcal/Pdif/2 (used 
00112        to correct the weights).  'swpow' implicitly includes any 
00113        requantizer gain scale and adjustments.
00114      'rq' yields only the requantizer voltage gains (Tsys is set to
00115        1.0 to avoid weight adjustments).
00116      'swp/rq' yields the ordinary switched power voltage gains divided
00117        by the requantizer voltage gain (Tsys is calculate normally).
00118      The 'rq' and 'swp/rq' modes are are mainly intended for testing 
00119      and evaluating the EVLA switched power systems.
00120   
00121      For caltype='opac', specify the desired opacity(ies) in the parameter
00122      argument.  At this time, only constant (in time) opacities are
00123      supported via gencal.  
00124 
00125      For gaincurve and efficiency (caltype='gc', 'gceff', or 'eff'), 
00126      observatory-provided factors are determined per spw according
00127      to the observing frequencies.  The parameter argument is 
00128      ignored.  These caltypes are currently only supported
00129      for VLA processing.  (Appropriate factors for ALMA are TBD.)
00130 
00131      Keyword arguments:
00132 
00133      vis -- Name of input visibility file
00134              default: none.  example: vis='ngc5921.ms'
00135      caltable -- Name of input/output caltable.  If it does not
00136                   exist, it will be created.  Specifying an
00137                   existing table will result in the parameters
00138                   being applied cumulatively. Only a single
00139                   time-stamp for all calibrations are supported,
00140                   currently.  Do not use a caltable
00141                   created by gaincal, bandpass, etc.  
00142                   default: none.  example: caltable='test.G'
00143      caltype -- The calibration parameter type being specified.
00144                  Options include:
00145                  'amp' = gain (G) amplitude (1 real parameter per
00146                          pol, antenna, spw)
00147                  'ph'  = gain (G) phase (deg) (1 real parameter per
00148                          pol, antenna, spw)
00149                  'sbd' = single-band delays (nsec) (1 real parameter 
00150                          per pol, antenna, spw)
00151                  'mbd' = multi-band delay (nsec) (1 real parameter 
00152                          per pol, antenna, spw)
00153                  'antpos' = antenna position corrections (m) (3 real
00154                             ITRF offset parameters per antenna; spw, pol
00155                             selection will be ignored)
00156                             With antenna='', this triggers an automated lookup
00157                             of antenna positions for EVLA.
00158                  'antposvla' = antenna position corrections (m) specified
00159                                in the old VLA-centric coordinate system
00160                  'tsys' = Tsys from the SYSCAL table (ALMA)
00161                  'evlagain' = EVLA switched-power gains (experimental)
00162                  'opac' = Tropospheric opacity (1 real parameter 
00163                          per antenna, spw)
00164                  'gc' = Antenna zenith-angle dependent gain curve (auto-lookup)
00165                  'gceff' = Gain curve and efficiency (auto-lookup)
00166                  'eff' = Antenna efficiency (auto-lookup)
00167                  default: none.
00168                  example: caltype='ph'
00169      spw -- Spectral window selection for specified parameters. 
00170              default: spw='' (specified parameters apply to all spws)
00171              example: spw = '2,3,4'
00172      antenna -- Antenna selection for specified parameters. 
00173                  default: antenna='' (specified parameters apply to all antennas)
00174                  example: antenna='ea02, ea03' (specified parameter(s) to
00175                            apply to ea02 and ea03 only)
00176      pol -- Polarization selection for specified parameters.
00177              default: pol='' (specified parameters apply to all polarizations)
00178              example: pol='R' (specified parameters to apply to
00179                                  R only)
00180      parameter -- The calibration parameters, specified as a list, to 
00181                    store in the caltable for the spw, antenna, and pol 
00182                    selection.  The required length of the list is 
00183                    determined by the caltype and the spw, antenna, pol
00184                    selection.  One "set" of parameters (e.g., one value
00185                    for 'amp', 'ph', etc., three values for 'antpos')
00186                    specified the same value for all indicated spw, antenna,
00187                    and pol.
00188                    OR, 
00189                    When specifying a long list of calibration parameter values,
00190                    these should be ordered first (fastest) by pol (if pol!=''),
00191                    then by antenna (if antenna!=''), and finally (sloweset) by 
00192                    spw (if spw!='').  Unspecified selection axes must not be
00193                    enumerated in the parameter list
00194       
00195      Examples:
00196 
00197        gencal(vis='test.ms',caltable='test.G',caltype='amp',
00198               spw='',antenna='',pol='',
00199               parameter=[3])
00200 
00201          --> Antenna-based gain amplitude corrections for all spws, antennas, 
00202               and polarizations will be multiplied by 3.  When applied
00203               to visibility data, this correction will produce a
00204               corrected visibility than is (1/3*1/3) less than the 
00205               uncorrected visibility.
00206 
00207        gencal(vis='test.ms',caltable='test.G',caltype='ph',
00208               spw='',antenna='ea03,ea04',pol='',
00209               parameter=[45,120])
00210 
00211          --> Gain phase corrections for antennas ea03 and ea04
00212               will be adjusted (additive) by 45 and 120 
00213               degrees (respectively), for all spws and polarizations.
00214               When these phases are applied to visibility data, the
00215               visibility phases will decrease or increase by the
00216               specified amount where the selected antennas occur
00217               first or second (respectively) in each baseline.  E.g., 
00218               the phase of baseline ea03-ea04 will change by (-45+120)
00219               = + 75 degrees.  Baseline ea01-ea03's phase will change
00220               by +45 degrees; baseline ea04-ea05's phase will change
00221               by -120 degrees.  The same phase sign convention is
00222               used for delay and antenna position corrections.
00223 
00224        gencal(vis='test.ms',caltable='test.G',caltype='ph',
00225               spw='',antenna='ea05,ea06',pol='R',
00226               parameter=[63,-34])
00227 
00228          --> Gain phase corrections for antennas ea05 and ea06
00229               will be adjusted (additive) by 63 and -34 
00230               degrees (respectively), in R only, for all spws
00231 
00232        gencal(vis='test.ms',caltable='test.G',caltype='ph',
00233               spw='',antenna='ea09,ea10',pol='R,L',
00234               parameter=[14,-23,-130,145])
00235 
00236          --> Gain phase corrections in all spws will be adjusted for 
00237               antenna ea09 by 14 deg in R and -23 deg in L, and for
00238               antenna ea10 by -130 deg in R and 145 deg in L.
00239 
00240        gencal(vis='test.ms',caltable='test.G',caltype='ph',
00241               spw='2,3',antenna='ea09,ea10',pol='',
00242               parameter=[14,-23,-130,145])
00243 
00244          --> Gain phases corrections in both polarizations will be adjusted for 
00245               antenna ea09 by 14 deg in spw 2 and -23 deg in spw 3, and for
00246               antenna ea10 by -130 deg in spw 2 and 145 deg in spw 3.
00247 
00248        gencal(vis='test.ms',caltable='test.G',caltype='sbd',
00249               spw='2,3',antenna='ea09,ea10',pol='',
00250               parameter=[14,-23,-130,145])
00251 
00252          --> Delay corrections in both polarizations will be adjusted for 
00253               antenna ea09 by 14 nsec in spw 2 and -23 nsec in spw 3, and for
00254               antenna ea10 by -130 nsec in spw 2 and 145 nsec in spw
00255               3.  See the above example for caltype='ph' for details
00256               of the sign convention adopted when applying delay corrections.
00257 
00258        gencal(vis='test.ms',caltable='test.G',caltype='antpos',antenna='')
00259      
00260          --> *** Currently EVLA observations only ***
00261              Antenna position corrections will be retrieved automatically
00262              over internet to generate the caltable with antenna=''.
00263 
00264        gencal(vis='test.ms',caltable='test.G',caltype='antpos',
00265               antenna='ea09,ea10',
00266               parameter=[0.01,0.02,0.03, -0.03,-0.01,-0.02])
00267 
00268          --> Antenna position corrections in meters (in ITRF) for
00269               antenna ea09 (dBx=0.01, dBy=0.02, dBz=0.03) and for
00270               antenna ea10 (dBx=-0.03, dBy=-0.01, dBz=-0.02)
00271               See the above example for caltype='ph' for details
00272               of the sign convention adopted when applying antpos 
00273               corrections.
00274 
00275        gencal(vis='test.ms',caltable='test.G',caltype='antposvla',
00276               antenna='ea09,ea10',
00277               parameter=[0.01,0.02,0.03, -0.03,-0.01,-0.02])
00278 
00279          --> Antenna position corrections (in the traditional VLA-centric
00280               frame) will be introduced in meters for
00281               antenna ea09 (dBx=0.01, dBy=0.02, dBz=0.03) and for
00282               antenna ea10 (dBx=-0.03, dBy=-0.01, dBz=-0.02)
00283               These offsets will be rotated to the ITRF frame before
00284               storing them in the caltable.
00285               See the above example for caltype='ph' for details
00286               of the sign convention adopted when applying antpos 
00287               corrections.
00288 
00289  
00290         """
00291         if not hasattr(self, "__globals__") or self.__globals__ == None :
00292            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00293         #casac = self.__globals__['casac']
00294         casalog = self.__globals__['casalog']
00295         #casalog = casac.casac.logsink()
00296         self.__globals__['__last_task'] = 'gencal'
00297         self.__globals__['taskname'] = 'gencal'
00298         ###
00299         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00300         ###
00301         ###
00302         #Handle globals or user over-ride of arguments
00303         #
00304         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00305         useLocalDefaults = False
00306 
00307         for item in function_signature_defaults.iteritems():
00308                 key,val = item
00309                 keyVal = eval(key)
00310                 if (keyVal == None):
00311                         #user hasn't set it - use global/default
00312                         pass
00313                 else:
00314                         #user has set it - use over-ride
00315                         if (key != 'self') :
00316                            useLocalDefaults = True
00317 
00318         myparams = {}
00319         if useLocalDefaults :
00320            for item in function_signature_defaults.iteritems():
00321                key,val = item
00322                keyVal = eval(key)
00323                exec('myparams[key] = keyVal')
00324                self.parameters[key] = keyVal
00325                if (keyVal == None):
00326                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00327                    keyVal = eval(key)
00328                    if(type(keyVal) == dict) :
00329                       if len(keyVal) > 0 :
00330                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00331                       else :
00332                          exec('myparams[key] = ' + key + ' = {}')
00333 
00334         else :
00335             async = self.parameters['async']
00336             myparams['vis'] = vis = self.parameters['vis']
00337             myparams['caltable'] = caltable = self.parameters['caltable']
00338             myparams['caltype'] = caltype = self.parameters['caltype']
00339             myparams['spw'] = spw = self.parameters['spw']
00340             myparams['antenna'] = antenna = self.parameters['antenna']
00341             myparams['pol'] = pol = self.parameters['pol']
00342             myparams['parameter'] = parameter = self.parameters['parameter']
00343 
00344         if type(parameter)==float: parameter=[parameter]
00345 
00346         result = None
00347 
00348 #
00349 #    The following is work around to avoid a bug with current python translation
00350 #
00351         mytmp = {}
00352 
00353         mytmp['vis'] = vis
00354         mytmp['caltable'] = caltable
00355         mytmp['caltype'] = caltype
00356         mytmp['spw'] = spw
00357         mytmp['antenna'] = antenna
00358         mytmp['pol'] = pol
00359         mytmp['parameter'] = parameter
00360         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00361         trec = casac.casac.utils().torecord(pathname+'gencal.xml')
00362 
00363         casalog.origin('gencal')
00364         try :
00365           #if not trec.has_key('gencal') or not casac.casac.utils().verify(mytmp, trec['gencal']) :
00366             #return False
00367 
00368           casac.casac.utils().verify(mytmp, trec['gencal'], True)
00369           scriptstr=['']
00370           saveinputs = self.__globals__['saveinputs']
00371           saveinputs('gencal', 'gencal.last', myparams, self.__globals__,scriptstr=scriptstr)
00372           if async :
00373             count = 0
00374             keybase =  time.strftime("%y%m%d.%H%M%S")
00375             key = keybase + "_" + str(count)
00376             while self.__async__.has_key(key) :
00377                count += 1
00378                key = keybase + "_" + str(count)
00379             result = tm.execute('gencal', vis, caltable, caltype, spw, antenna, pol, parameter)
00380             print "Use: "
00381             print "      tm.retrieve(return_value) # to retrieve the status"
00382             print 
00383             self.rkey = key
00384             self.__async__[key] = result
00385           else :
00386               tname = 'gencal'
00387               spaces = ' '*(18-len(tname))
00388               casalog.post('\n##########################################'+
00389                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00390               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00391               result = gencal(vis, caltable, caltype, spw, antenna, pol, parameter)
00392               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00393                            '\n##########################################')
00394 
00395         except Exception, instance:
00396           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00397              raise
00398           else :
00399              #print '**** Error **** ',instance
00400              tname = 'gencal'
00401              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00402              pass
00403 
00404         gc.collect()
00405         return result
00406 #
00407 #
00408 #
00409     def paramgui(self, useGlobals=True, ipython_globals=None):
00410         """
00411         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00412         """
00413         import paramgui
00414         if not hasattr(self, "__globals__") or self.__globals__ == None :
00415            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00416 
00417         if useGlobals:
00418             if ipython_globals == None:
00419                 myf=self.__globals__
00420             else:
00421                 myf=ipython_globals
00422 
00423             paramgui.setGlobals(myf)
00424         else:
00425             paramgui.setGlobals({})
00426 
00427         paramgui.runTask('gencal', myf['_ip'])
00428         paramgui.setGlobals({})
00429 
00430 #
00431 #
00432 #
00433     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00434         if not hasattr(self, "__globals__") or self.__globals__ == None :
00435            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00436         if ipython_globals == None:
00437             myf=self.__globals__
00438         else:
00439             myf=ipython_globals
00440 
00441         a = odict()
00442         a['vis']  = ''
00443         a['caltable']  = ''
00444         a['caltype']  = ''
00445         a['spw']  = ''
00446         a['antenna']  = ''
00447         a['pol']  = ''
00448         a['parameter']  = []
00449 
00450         a['async']=False
00451 
00452 ### This function sets the default values but also will return the list of
00453 ### parameters or the default value of a given parameter
00454         if(param == None):
00455                 myf['__set_default_parameters'](a)
00456         elif(param == 'paramkeys'):
00457                 return a.keys()
00458         else:
00459             if(paramvalue==None and subparam==None):
00460                if(a.has_key(param)):
00461                   return a[param]
00462                else:
00463                   return self.itsdefault(param)
00464             else:
00465                retval=a[param]
00466                if(type(a[param])==dict):
00467                   for k in range(len(a[param])):
00468                      valornotval='value'
00469                      if(a[param][k].has_key('notvalue')):
00470                         valornotval='notvalue'
00471                      if((a[param][k][valornotval])==paramvalue):
00472                         retval=a[param][k].copy()
00473                         retval.pop(valornotval)
00474                         if(subparam != None):
00475                            if(retval.has_key(subparam)):
00476                               retval=retval[subparam]
00477                            else:
00478                               retval=self.itsdefault(subparam)
00479                      else:
00480                         retval=self.itsdefault(subparam)
00481                return retval
00482 
00483 
00484 #
00485 #
00486     def check_params(self, param=None, value=None, ipython_globals=None):
00487       if ipython_globals == None:
00488           myf=self.__globals__
00489       else:
00490           myf=ipython_globals
00491 #      print 'param:', param, 'value:', value
00492       try :
00493          if str(type(value)) != "<type 'instance'>" :
00494             value0 = value
00495             value = myf['cu'].expandparam(param, value)
00496             matchtype = False
00497             if(type(value) == numpy.ndarray):
00498                if(type(value) == type(value0)):
00499                   myf[param] = value.tolist()
00500                else:
00501                   #print 'value:', value, 'value0:', value0
00502                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00503                   myf[param] = value0
00504                   if type(value0) != list :
00505                      matchtype = True
00506             else :
00507                myf[param] = value
00508             value = myf['cu'].verifyparam({param:value})
00509             if matchtype:
00510                value = False
00511       except Exception, instance:
00512          #ignore the exception and just return it unchecked
00513          myf[param] = value
00514       return value
00515 #
00516 #
00517     def description(self, key='gencal', subkey=None):
00518         desc={'gencal': 'Specify Calibration Values of Various Types',
00519                'vis': 'Name of input visibility file',
00520                'caltable': 'The new/existing calibration table',
00521                'caltype': 'The calibration type: \'amp\',\'ph\', \'sbd\',\'mbd\',\'antpos\',\'antposvla\',\'tsys\',\'evlagain\',\'opac\',\'gc\',\'gceff\',\'eff\'',
00522                'spw': 'Calibration spw(s) selection',
00523                'antenna': 'Calibration antenna(s) selection',
00524                'pol': 'Calibration polarizations(s) selection',
00525                'parameter': 'The calibration values',
00526 
00527                'async': 'If true the taskname must be started using gencal(...)'
00528               }
00529 
00530         if(desc.has_key(key)) :
00531            return desc[key]
00532 
00533     def itsdefault(self, paramname) :
00534         a = {}
00535         a['vis']  = ''
00536         a['caltable']  = ''
00537         a['caltype']  = ''
00538         a['spw']  = ''
00539         a['antenna']  = ''
00540         a['pol']  = ''
00541         a['parameter']  = []
00542 
00543         #a = sys._getframe(len(inspect.stack())-1).f_globals
00544 
00545         if a.has_key(paramname) :
00546               return a[paramname]
00547 gencal_cli = gencal_cli_()