casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
wvrgcal_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_wvrgcal import wvrgcal
00018 class wvrgcal_cli_:
00019     __name__ = "wvrgcal"
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__ = (wvrgcal_cli_,)
00028        self.__doc__ = self.__call__.__doc__
00029 
00030        self.parameters={'vis':None, 'caltable':None, 'toffset':None, 'segsource':None, 'sourceflag':None, 'tie':None, 'nsol':None, 'disperse':None, 'wvrflag':None, 'statfield':None, 'statsource':None, 'smooth':None, 'scale':None, 'reversespw':None, 'cont':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, toffset=None, segsource=None, sourceflag=None, tie=None, nsol=None, disperse=None, wvrflag=None, statfield=None, statsource=None, smooth=None, scale=None, reversespw=None, cont=None,  async=None):
00047 
00048         """Generate a gain table based on Water Vapour Radiometer data
00049 
00050 Information about the observation and the performance of WVRGCAL is written to the CASA logger; 
00051 see the CASA cookbook for a more detailed description of these parameters.
00052 
00053 Of particular note is the discrepancy parameter (Disc): high values (> a few hundred microns) 
00054 may indicate some levels of cloud contamination and the effect of applying the WVRGCAL correction 
00055 should be checked; values > 1000 um in all antennas have currently been found to indicate that 
00056 WVRGCAL correction should not be used.
00057 
00058       
00059   vis -- Name of input visibility file
00060               default: none; example: vis='ngc5921.ms'
00061 
00062   caltable -- Name of output gain calibration table
00063               default: none; example: caltable='ngc5921.wvr'
00064 
00065   toffset -- Time offset (sec) between interferometric and WVR data
00066              default: -1 (ALMA default)
00067 
00068   segsource -- Do a new coefficient calculation for each source
00069              default: True
00070 
00071   tie -- Prioritise tieing the phase of these sources as well as possible
00072          (requires segsource=True)
00073              default: [] example: ['3C273,NGC253', 'IC433,3C279']
00074 
00075   sourceflag -- Flag the WVR data for these source(s) as bad and do not produce corrections for it
00076                (requires segsource=True)
00077                default: [] (none) example: ['3C273']
00078 
00079   nsol -- Number of solutions for phase correction coefficients during this observation.
00080           By default only one set of coefficients is generated for the entire observation. 
00081           If more sets are requested, then they will be evenly distributed in time throughout 
00082           the observation. Values > 1 require segsource=False.
00083              default: 1
00084 
00085   disperse -- Apply correction for dispersion
00086              default: False
00087 
00088   wvrflag -- Flag the WVR data for these antenna(s) as bad and replace its data with interpolated values
00089                default: [] (none) example: ['DV03','DA05','PM02']           
00090 
00091   statfield -- Compute the statistics (Phase RMS, Disc) on this field only
00092                default: '' (all) 
00093 
00094   statsource -- Compute the statistics (Phase RMS, Disc) on this source only
00095                default: '' (all)             
00096 
00097   smooth -- Smooth WVR data by this many samples before applying the correction
00098              default: 1 (no smoothing) example: 3
00099 
00100   scale -- Scale the entire phase correction by this factor
00101              default: 1. (no scaling)
00102 
00103   reversespw -- Reverse the sign of the correction for the listed SPWs
00104                 (only neede for early ALMA data before Cycle 0)
00105              default: '' (none), example: reversespw='0~2,4'; spectral windows 0,1,2,4
00106 
00107   cont -- Estimate the continuum (e.g., due to clouds)
00108              default: False
00109 
00110   
00111         """
00112         if not hasattr(self, "__globals__") or self.__globals__ == None :
00113            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00114         #casac = self.__globals__['casac']
00115         casalog = self.__globals__['casalog']
00116         #casalog = casac.casac.logsink()
00117         self.__globals__['__last_task'] = 'wvrgcal'
00118         self.__globals__['taskname'] = 'wvrgcal'
00119         ###
00120         self.__globals__['update_params'](func=self.__globals__['taskname'],printtext=False,ipython_globals=self.__globals__)
00121         ###
00122         ###
00123         #Handle globals or user over-ride of arguments
00124         #
00125         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00126         useLocalDefaults = False
00127 
00128         for item in function_signature_defaults.iteritems():
00129                 key,val = item
00130                 keyVal = eval(key)
00131                 if (keyVal == None):
00132                         #user hasn't set it - use global/default
00133                         pass
00134                 else:
00135                         #user has set it - use over-ride
00136                         if (key != 'self') :
00137                            useLocalDefaults = True
00138 
00139         myparams = {}
00140         if useLocalDefaults :
00141            for item in function_signature_defaults.iteritems():
00142                key,val = item
00143                keyVal = eval(key)
00144                exec('myparams[key] = keyVal')
00145                self.parameters[key] = keyVal
00146                if (keyVal == None):
00147                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00148                    keyVal = eval(key)
00149                    if(type(keyVal) == dict) :
00150                       if len(keyVal) > 0 :
00151                          exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00152                       else :
00153                          exec('myparams[key] = ' + key + ' = {}')
00154 
00155         else :
00156             async = self.parameters['async']
00157             myparams['vis'] = vis = self.parameters['vis']
00158             myparams['caltable'] = caltable = self.parameters['caltable']
00159             myparams['toffset'] = toffset = self.parameters['toffset']
00160             myparams['segsource'] = segsource = self.parameters['segsource']
00161             myparams['sourceflag'] = sourceflag = self.parameters['sourceflag']
00162             myparams['tie'] = tie = self.parameters['tie']
00163             myparams['nsol'] = nsol = self.parameters['nsol']
00164             myparams['disperse'] = disperse = self.parameters['disperse']
00165             myparams['wvrflag'] = wvrflag = self.parameters['wvrflag']
00166             myparams['statfield'] = statfield = self.parameters['statfield']
00167             myparams['statsource'] = statsource = self.parameters['statsource']
00168             myparams['smooth'] = smooth = self.parameters['smooth']
00169             myparams['scale'] = scale = self.parameters['scale']
00170             myparams['reversespw'] = reversespw = self.parameters['reversespw']
00171             myparams['cont'] = cont = self.parameters['cont']
00172 
00173         if type(sourceflag)==str: sourceflag=[sourceflag]
00174         if type(tie)==str: tie=[tie]
00175         if type(wvrflag)==str: wvrflag=[wvrflag]
00176 
00177         result = None
00178 
00179 #
00180 #    The following is work around to avoid a bug with current python translation
00181 #
00182         mytmp = {}
00183 
00184         mytmp['vis'] = vis
00185         mytmp['caltable'] = caltable
00186         mytmp['toffset'] = toffset
00187         mytmp['segsource'] = segsource
00188         mytmp['sourceflag'] = sourceflag
00189         mytmp['tie'] = tie
00190         mytmp['nsol'] = nsol
00191         mytmp['disperse'] = disperse
00192         mytmp['wvrflag'] = wvrflag
00193         mytmp['statfield'] = statfield
00194         mytmp['statsource'] = statsource
00195         mytmp['smooth'] = smooth
00196         mytmp['scale'] = scale
00197         mytmp['reversespw'] = reversespw
00198         mytmp['cont'] = cont
00199         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00200         trec = casac.casac.utils().torecord(pathname+'wvrgcal.xml')
00201 
00202         casalog.origin('wvrgcal')
00203         try :
00204           #if not trec.has_key('wvrgcal') or not casac.casac.utils().verify(mytmp, trec['wvrgcal']) :
00205             #return False
00206 
00207           casac.casac.utils().verify(mytmp, trec['wvrgcal'], True)
00208           scriptstr=['']
00209           saveinputs = self.__globals__['saveinputs']
00210           saveinputs('wvrgcal', 'wvrgcal.last', myparams, self.__globals__,scriptstr=scriptstr)
00211           if async :
00212             count = 0
00213             keybase =  time.strftime("%y%m%d.%H%M%S")
00214             key = keybase + "_" + str(count)
00215             while self.__async__.has_key(key) :
00216                count += 1
00217                key = keybase + "_" + str(count)
00218             result = tm.execute('wvrgcal', vis, caltable, toffset, segsource, sourceflag, tie, nsol, disperse, wvrflag, statfield, statsource, smooth, scale, reversespw, cont)
00219             print "Use: "
00220             print "      tm.retrieve(return_value) # to retrieve the status"
00221             print 
00222             self.rkey = key
00223             self.__async__[key] = result
00224           else :
00225               tname = 'wvrgcal'
00226               spaces = ' '*(18-len(tname))
00227               casalog.post('\n##########################################'+
00228                            '\n##### Begin Task: ' + tname + spaces + ' #####')
00229               casalog.post(scriptstr[1][1:]+'\n', 'INFO')
00230               result = wvrgcal(vis, caltable, toffset, segsource, sourceflag, tie, nsol, disperse, wvrflag, statfield, statsource, smooth, scale, reversespw, cont)
00231               casalog.post('##### End Task: ' + tname + '  ' + spaces + ' #####'+
00232                            '\n##########################################')
00233 
00234         except Exception, instance:
00235           if(self.__globals__.has_key('__rethrow_casa_exceptions') and self.__globals__['__rethrow_casa_exceptions']) :
00236              raise
00237           else :
00238              #print '**** Error **** ',instance
00239              tname = 'wvrgcal'
00240              casalog.post('An error occurred running task '+tname+'.', 'ERROR')
00241              pass
00242 
00243         gc.collect()
00244         return result
00245 #
00246 #
00247 #
00248     def paramgui(self, useGlobals=True, ipython_globals=None):
00249         """
00250         Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00251         """
00252         import paramgui
00253         if not hasattr(self, "__globals__") or self.__globals__ == None :
00254            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00255 
00256         if useGlobals:
00257             if ipython_globals == None:
00258                 myf=self.__globals__
00259             else:
00260                 myf=ipython_globals
00261 
00262             paramgui.setGlobals(myf)
00263         else:
00264             paramgui.setGlobals({})
00265 
00266         paramgui.runTask('wvrgcal', myf['_ip'])
00267         paramgui.setGlobals({})
00268 
00269 #
00270 #
00271 #
00272     def defaults(self, param=None, ipython_globals=None, paramvalue=None, subparam=None):
00273         if not hasattr(self, "__globals__") or self.__globals__ == None :
00274            self.__globals__=sys._getframe(len(inspect.stack())-1).f_globals
00275         if ipython_globals == None:
00276             myf=self.__globals__
00277         else:
00278             myf=ipython_globals
00279 
00280         a = odict()
00281         a['vis']  = ''
00282         a['caltable']  = ''
00283         a['toffset']  = -1
00284         a['segsource']  = True
00285         a['disperse']  = False
00286         a['wvrflag']  = ['']
00287         a['statfield']  = ''
00288         a['statsource']  = ''
00289         a['smooth']  = 1
00290         a['scale']  = 1.
00291         a['reversespw']  = ''
00292         a['cont']  = False
00293 
00294         a['async']=False
00295         a['segsource'] = {
00296                     0:odict([{'value':True}, {'tie':[]}, {'sourceflag':[]}]), 
00297                     1:odict([{'value':False}, {'nsol':1}])}
00298 
00299 ### This function sets the default values but also will return the list of
00300 ### parameters or the default value of a given parameter
00301         if(param == None):
00302                 myf['__set_default_parameters'](a)
00303         elif(param == 'paramkeys'):
00304                 return a.keys()
00305         else:
00306             if(paramvalue==None and subparam==None):
00307                if(a.has_key(param)):
00308                   return a[param]
00309                else:
00310                   return self.itsdefault(param)
00311             else:
00312                retval=a[param]
00313                if(type(a[param])==dict):
00314                   for k in range(len(a[param])):
00315                      valornotval='value'
00316                      if(a[param][k].has_key('notvalue')):
00317                         valornotval='notvalue'
00318                      if((a[param][k][valornotval])==paramvalue):
00319                         retval=a[param][k].copy()
00320                         retval.pop(valornotval)
00321                         if(subparam != None):
00322                            if(retval.has_key(subparam)):
00323                               retval=retval[subparam]
00324                            else:
00325                               retval=self.itsdefault(subparam)
00326                      else:
00327                         retval=self.itsdefault(subparam)
00328                return retval
00329 
00330 
00331 #
00332 #
00333     def check_params(self, param=None, value=None, ipython_globals=None):
00334       if ipython_globals == None:
00335           myf=self.__globals__
00336       else:
00337           myf=ipython_globals
00338 #      print 'param:', param, 'value:', value
00339       try :
00340          if str(type(value)) != "<type 'instance'>" :
00341             value0 = value
00342             value = myf['cu'].expandparam(param, value)
00343             matchtype = False
00344             if(type(value) == numpy.ndarray):
00345                if(type(value) == type(value0)):
00346                   myf[param] = value.tolist()
00347                else:
00348                   #print 'value:', value, 'value0:', value0
00349                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00350                   myf[param] = value0
00351                   if type(value0) != list :
00352                      matchtype = True
00353             else :
00354                myf[param] = value
00355             value = myf['cu'].verifyparam({param:value})
00356             if matchtype:
00357                value = False
00358       except Exception, instance:
00359          #ignore the exception and just return it unchecked
00360          myf[param] = value
00361       return value
00362 #
00363 #
00364     def description(self, key='wvrgcal', subkey=None):
00365         desc={'wvrgcal': 'Generate a gain table based on Water Vapour Radiometer data',
00366                'vis': 'Name of input visibility file',
00367                'caltable': 'Name of output gain calibration table',
00368                'toffset': 'Time offset (sec) between interferometric and WVR data',
00369                'segsource': 'Do a new coefficient calculation for each source',
00370                'sourceflag': 'Flag the WVR data for these source(s) as bad and do not produce corrections for it (requires segsource=True)',
00371                'tie': 'Prioritise tieing the phase of these sources as well as possible (requires segsource=True)',
00372                'nsol': 'Number of solutions for phase correction coefficients (nsol>1 requires segsource=False)',
00373                'disperse': 'Apply correction for dispersion',
00374                'wvrflag': 'Flag the WVR data for these antenna(s) as bad and replace its data with interpolated values',
00375                'statfield': 'Compute the statistics (Phase RMS, Disc) on this field only',
00376                'statsource': 'Compute the statistics (Phase RMS, Disc) on this source only',
00377                'smooth': 'Smooth WVR data by this many samples before applying the correction',
00378                'scale': 'Scale the entire phase correction by this factor',
00379                'reversespw': 'Reverse the sign of the correction for the listed SPWs (only needed for early ALMA data before Cycle 0)',
00380                'cont': 'Estimate the continuum (e.g., due to clouds) (experimental)',
00381 
00382                'async': 'If true the taskname must be started using wvrgcal(...)'
00383               }
00384 
00385 #
00386 # Set subfields defaults if needed
00387 #
00388 
00389         if(desc.has_key(key)) :
00390            return desc[key]
00391 
00392     def itsdefault(self, paramname) :
00393         a = {}
00394         a['vis']  = ''
00395         a['caltable']  = ''
00396         a['toffset']  = -1
00397         a['segsource']  = True
00398         a['sourceflag']  = ['']
00399         a['tie']  = ['']
00400         a['nsol']  = 1
00401         a['disperse']  = False
00402         a['wvrflag']  = ['']
00403         a['statfield']  = ''
00404         a['statsource']  = ''
00405         a['smooth']  = 1
00406         a['scale']  = 1.
00407         a['reversespw']  = ''
00408         a['cont']  = False
00409 
00410         #a = sys._getframe(len(inspect.stack())-1).f_globals
00411 
00412         if self.parameters['segsource']  == True:
00413             a['tie'] = []
00414             a['sourceflag'] = []
00415 
00416         if self.parameters['segsource']  == False:
00417             a['nsol'] = 1
00418 
00419         if a.has_key(paramname) :
00420               return a[paramname]
00421 wvrgcal_cli = wvrgcal_cli_()