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