casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
uvcontsub_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_uvcontsub import uvcontsub
00016 from task_uvcontsub import casalog
00017 
00018 class uvcontsub_pg_:
00019     __name__ = "uvcontsub"
00020 
00021     def __init__(self) :
00022        self.__bases__ = (uvcontsub_pg_,)
00023        self.__doc__ = self.__call__.__doc__
00024 
00025 
00026     def __call__(self, vis=None, field=None, fitspw=None, combine=None, solint=None, fitorder=None, spw=None, want_cont=None, async=None):
00027 
00028         """Continuum fitting and subtraction in the uv plane
00029 
00030         Continuum fitting and subtraction in the uv plane:
00031         
00032         This task estimates the continuum emission by fitting polynomials to
00033         the real and imaginary parts of the spectral windows and channels
00034         selected by fitspw.  This fit represents a model of the continuum in 
00035         all channels.
00036         
00037         The fitted continuum spectrum is subtracted from all channels 
00038         selected in spw, and the result (presumably only line emission)
00039         is stored in a new MS (vis + ".contsub").
00040         It will read from the CORRECTED_DATA column of vis if it is present,
00041         or DATA if it is not.  Whichever column is read is presumed to have
00042         already been calibrated.
00043 
00044         If want_cont is True, the continuum fit is placed in a second new MS
00045         (vis + '.cont').  N.B. because the continuum model is necessarily a
00046         smoothed fit, images made with it are liable to have their field of
00047         view reduced in some strange way.  Images of the continuum should be
00048         made by simply excluding the line channels (and probably averaging the
00049         remaining ones) in clean.
00050 
00051         Keyword arguments:
00052         vis -- Name of input visibility file
00053                 default: none; example: vis='ngc5921.ms'
00054         field -- Field selection for continuum estimation and subtraction.
00055                  The estimation and subtraction is done for each selected field
00056                  in turn.  (Run listobs to get lists of the ID and names.)
00057                 default: field = '' means select all fields
00058                 field = 1 # will get field_id=1 (if you give it an 
00059                         integer, it will retrieve the source with that index.
00060                 field = '1328+307'  specifies source '1328+307'
00061                 field = '13*' will retrieve '1328+307' and any other fields
00062                    beginning with '13'
00063         fitspw -- Selection of spectral windows and channels to use in the
00064                   fit for the continuum, using general spw:chan syntax.
00065                   See the note under combine.
00066                 default: '' (all)
00067                 example: fitspw='0:5~30;40~55'
00068         combine -- Data axes to combine for the continuum estimate.
00069                 It must include 'spw' if spw contains spws that are not in
00070                 fitspw!
00071                 default: '' --> solutions will break at scan, field, and spw
00072                       boundaries according to solint
00073               Options: '', 'spw'', 'scan', or 'spw, scan'
00074               example: combine='spw' --> form spw-merged continuum estimate
00075         solint -- Timescale for per-baseline fit (units optional)
00076                 default (recommended): 'int' --> no time averaging, do a
00077                                        fit for each integration and let the
00078                                        noisy fits average out in the image.
00079 
00080                 example: solint='10s'  --> average to 10s before fitting
00081                          10 or '10' --> '10s' (unitless: assumes seconds)
00082                 options: 'int' --> per integration
00083                          'inf' --> per scan
00084 
00085                 If solint is longer than 'int', the continuum estimate can be
00086                 corrupted by time smearing!
00087 
00088         fitorder -- Polynomial order for the fits of the continuum w.r.t.
00089                     frequency.  fitorders > 1 are strongly discouraged
00090                     because high order polynomials have more flexibility, may
00091                     absorb line emission, and tend go wild at the edges of
00092                     fitspw, which is not what you want.
00093 
00094                 default: 0 (constant); example: fitorder=1
00095 
00096         spw -- Optional per spectral window selection of channels to include
00097                in the output.  See the note under combine.
00098 
00099                The spectral windows will be renumbered to start from 0, as in
00100                split.
00101         want_cont -- Create vis + '.cont' to hold the continuum estimate.
00102                 default: 'False'; example: want_cont=True
00103                 The continuum estimate will be placed in vis + '.cont'
00104         async -- Run task in a separate process (return CASA prompt)
00105                 default: False; example: async=True
00106 
00107 
00108         """
00109         a=inspect.stack()
00110         stacklevel=0
00111         for k in range(len(a)):
00112           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00113                 stacklevel=k
00114                 break
00115         myf=sys._getframe(stacklevel).f_globals
00116         myf['__last_task'] = 'uvcontsub'
00117         myf['taskname'] = 'uvcontsub'
00118         ###
00119         myf['update_params'](func=myf['taskname'],printtext=False)
00120         ###
00121         ###
00122         #Handle globals or user over-ride of arguments
00123         #
00124         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00125         useLocalDefaults = False
00126 
00127         for item in function_signature_defaults.iteritems():
00128                 key,val = item
00129                 keyVal = eval(key)
00130                 if (keyVal == None):
00131                         #user hasn't set it - use global/default
00132                         pass
00133                 else:
00134                         #user has set it - use over-ride
00135                         if (key != 'self') :
00136                            useLocalDefaults = True
00137                         #myf[key]=keyVal
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                if (keyVal == None):
00146                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00147                    keyVal = eval(key)
00148                    if(type(keyVal) == dict) :
00149                       exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00150 
00151         else :
00152             uselessvariable = None 
00153             myparams['vis'] = vis = myf['vis']
00154             myparams['field'] = field = myf['field']
00155             myparams['fitspw'] = fitspw = myf['fitspw']
00156             myparams['combine'] = combine = myf['combine']
00157             myparams['solint'] = solint = myf['solint']
00158             myparams['fitorder'] = fitorder = myf['fitorder']
00159             myparams['spw'] = spw = myf['spw']
00160             myparams['want_cont'] = want_cont = myf['want_cont']
00161 
00162 
00163         result = None
00164 
00165 #
00166 #    The following is work around to avoid a bug with current python translation
00167 #
00168         mytmp = {}
00169 
00170         mytmp['vis'] = vis
00171         mytmp['field'] = field
00172         mytmp['fitspw'] = fitspw
00173         mytmp['combine'] = combine
00174         mytmp['solint'] = solint
00175         mytmp['fitorder'] = fitorder
00176         mytmp['spw'] = spw
00177         mytmp['want_cont'] = want_cont
00178         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00179         trec = casac.utils().torecord(pathname+'uvcontsub.xml')
00180 
00181         casalog.origin('uvcontsub')
00182         if not trec.has_key('uvcontsub') or not casac.utils().verify(mytmp, trec['uvcontsub']) :
00183             return False
00184 
00185 
00186         try :
00187           casalog.post('')
00188           casalog.post('##########################################')
00189           casalog.post('##### Begin Task: uvcontsub           #####')
00190           casalog.post('')
00191           result = uvcontsub(vis, field, fitspw, combine, solint, fitorder, spw, want_cont)
00192           casalog.post('')
00193           casalog.post('##### End Task: uvcontsub           #####')
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('uvcontsub', 'uvcontsub.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('uvcontsub', 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['field']  = ''
00250         a['fitspw']  = ''
00251         a['combine']  = ''
00252         a['solint']  = 'int'
00253         a['fitorder']  = 0
00254         a['spw']  = ''
00255         a['want_cont']  = False
00256 
00257         a['async']=False
00258 
00259 ### This function sets the default values but also will return the list of
00260 ### parameters or the default value of a given parameter
00261         if(param == None):
00262                 myf['__set_default_parameters'](a)
00263         elif(param == 'paramkeys'):
00264                 return a.keys()
00265         else:
00266                 if(a.has_key(param)):
00267                    #if(type(a[param]) == dict) :
00268                    #   return a[param][len(a[param])-1]['value']
00269                    #else :
00270                       return a[param]
00271 
00272 
00273 #
00274 #
00275     def check_params(self, param=None, value=None):
00276       a=inspect.stack() 
00277       stacklevel=0
00278       for k in range(len(a)):
00279         if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00280             stacklevel=k
00281             break
00282       myf=sys._getframe(stacklevel).f_globals
00283 
00284 #      print 'param:', param, 'value:', value
00285       try :
00286          if str(type(value)) != "<type 'instance'>" :
00287             value0 = value
00288             value = myf['cu'].expandparam(param, value)
00289             matchtype = False
00290             if(type(value) == numpy.ndarray):
00291                if(type(value) == type(value0)):
00292                   myf[param] = value.tolist()
00293                else:
00294                   #print 'value:', value, 'value0:', value0
00295                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00296                   myf[param] = value0
00297                   if type(value0) != list :
00298                      matchtype = True
00299             else :
00300                myf[param] = value
00301             value = myf['cu'].verifyparam({param:value})
00302             if matchtype:
00303                value = False
00304       except Exception, instance:
00305          #ignore the exception and just return it unchecked
00306          myf[param] = value
00307       return value
00308 
00309 #
00310 #
00311     def description(self, key='uvcontsub', subkey=None):
00312         desc={'uvcontsub': 'Continuum fitting and subtraction in the uv plane',
00313                'vis': 'Name of input MS.  Output goes to vis + ".contsub"',
00314                'field': 'Select field(s) using id(s) or name(s)',
00315                'fitspw': 'Spectral window:channel selection for fitting the continuum',
00316                'combine': 'Data axes to combine for the continuum estimation (none, or spw and/or scan)',
00317                'solint': 'Continuum fit timescale (int recommended!)',
00318                'fitorder': 'Polynomial order for the fits',
00319                'spw': 'Spectral window selection for output',
00320                'want_cont': 'Create vis + ".cont" to hold the continuum estimate.',
00321 
00322                'async': 'If true the taskname must be started using uvcontsub(...)'
00323               }
00324 
00325         if(desc.has_key(key)) :
00326            return desc[key]
00327 
00328     def itsdefault(self, paramname) :
00329         a = {}
00330         a['vis']  = ''
00331         a['field']  = ''
00332         a['fitspw']  = ''
00333         a['combine']  = ''
00334         a['solint']  = 'int'
00335         a['fitorder']  = 0
00336         a['spw']  = ''
00337         a['want_cont']  = False
00338 
00339         if a.has_key(paramname) :
00340               return a[paramname]
00341 uvcontsub_pg = uvcontsub_pg_()