casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
sdsmooth_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_sdsmooth import sdsmooth
00016 from task_sdsmooth import casalog
00017 
00018 class sdsmooth_pg_:
00019     __name__ = "sdsmooth"
00020 
00021     def __init__(self) :
00022        self.__bases__ = (sdsmooth_pg_,)
00023        self.__doc__ = self.__call__.__doc__
00024 
00025 
00026     def __call__(self, infile=None, antenna=None, scanaverage=None, scanlist=None, field=None, iflist=None, pollist=None, kernel=None, kwidth=None, chanwidth=None, verify=None, outfile=None, outform=None, overwrite=None, plotlevel=None, async=None):
00027 
00028         """ASAP SD task: do smoothing of spectra
00029 
00030         Keyword arguments:
00031         infile -- name of input SD dataset
00032         antenna -- antenna name or id (only effective for MS input). 
00033         scanaverage -- average integrations within scans
00034                 options: (bool) True,False
00035                 default: False
00036                 example: if True, this happens in read-in
00037                          For GBT, set False!
00038         scanlist -- list of scan numbers to process
00039                 default: [] (use all scans)
00040                 example: [21,22,23,24]
00041                 this selection is in addition to field, iflist, and pollist
00042         field -- selection string for selecting scans by name
00043                 default: '' (no name selection)
00044                 example: 'FLS3a*'
00045                 this selection is in addition to scanlist, iflist,pollist
00046         iflist -- list of IF id numbers to select
00047                 default: [] (use all IFs)
00048                 example: [15]
00049                 this selection is in addition to scanlist, field, and pollist
00050         pollist -- list of polarization id numbers to select
00051                 default: [] (use all polarizations)
00052                 example: [1]
00053                 this selection is in addition to scanlist, field, and iflist
00054         kernel -- type of spectral smoothing
00055                 options: 'hanning','gaussian','boxcar','regrid'
00056                 default: 'hanning'
00057 
00058             >>>kernel expandable parameter
00059                  kwidth -- width of spectral smoothing kernel
00060                          options: (int) in channels 
00061                          default: 5
00062                          example: 5 or 10 seem to be popular for boxcar
00063                                   ignored for hanning (fixed at 5 chans)
00064                                   (0 will turn off gaussian or boxcar)
00065                  chanwidth -- channel width of regridded spectra
00066                          default: '5' (in channels)
00067                          example: '500MHz', '0.2km/s'
00068 
00069         verify -- verify the results of smoothing
00070                 options: (bool) True,False
00071                 default: False
00072                 WARNING: Currently this just asks whether you accept
00073                          the displayed smoothing and if not, continues
00074                          without smoothing.
00075                 Note: verification is not yet available for kernel='regrid'
00076         outfile -- Name of output ASAP format(scantable) file
00077                 default: '' (<infile>_sm)
00078         outform -- format of output file
00079                 options: 'ASCII','SDFITS','MS','ASAP'
00080                 default: 'ASAP'
00081                 example: the ASAP format is easiest for further sd
00082                          processing; use MS for CASA imaging.
00083                          If ASCII, then will append some stuff to
00084                          the outfile name
00085         overwrite -- overwrite the output file if already exists
00086                 options: (bool) True,False
00087                 default: False
00088                 WARNING: if outform='ASCII', this parameter is ignored
00089         plotlevel -- control for plotting of results
00090                 options: (int) 0=none, 1=some, 2=more, <0=hardcopy
00091                 default: 0 (no plotting)
00092                 example: plotlevel<0 as abs(plotlevel), e.g.
00093                          -1 => hardcopy of final plot (will be named
00094                          <outfile>_smspec.eps)
00095 
00096 
00097         DESCRIPTION:
00098 
00099         Task sdsmooth performs smoothing of the single-dish spectra.
00100         Set plotlevel >= 1 to plot spectrum before and after smoothing.
00101 
00102         WARNING for the GBT raw SDFITS format data as input:
00103         SDtasks are able to handle GBT raw SDFITS format data since the 
00104         data filler is available. However, the functionality is not well 
00105         tested yet, so that there may be unknown bugs.  
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'] = 'sdsmooth'
00117         myf['taskname'] = 'sdsmooth'
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['infile'] = infile = myf['infile']
00154             myparams['antenna'] = antenna = myf['antenna']
00155             myparams['scanaverage'] = scanaverage = myf['scanaverage']
00156             myparams['scanlist'] = scanlist = myf['scanlist']
00157             myparams['field'] = field = myf['field']
00158             myparams['iflist'] = iflist = myf['iflist']
00159             myparams['pollist'] = pollist = myf['pollist']
00160             myparams['kernel'] = kernel = myf['kernel']
00161             myparams['kwidth'] = kwidth = myf['kwidth']
00162             myparams['chanwidth'] = chanwidth = myf['chanwidth']
00163             myparams['verify'] = verify = myf['verify']
00164             myparams['outfile'] = outfile = myf['outfile']
00165             myparams['outform'] = outform = myf['outform']
00166             myparams['overwrite'] = overwrite = myf['overwrite']
00167             myparams['plotlevel'] = plotlevel = myf['plotlevel']
00168 
00169         if type(scanlist)==int: scanlist=[scanlist]
00170         if type(iflist)==int: iflist=[iflist]
00171         if type(pollist)==int: pollist=[pollist]
00172 
00173         result = None
00174 
00175 #
00176 #    The following is work around to avoid a bug with current python translation
00177 #
00178         mytmp = {}
00179 
00180         mytmp['infile'] = infile
00181         mytmp['antenna'] = antenna
00182         mytmp['scanaverage'] = scanaverage
00183         mytmp['scanlist'] = scanlist
00184         mytmp['field'] = field
00185         mytmp['iflist'] = iflist
00186         mytmp['pollist'] = pollist
00187         mytmp['kernel'] = kernel
00188         mytmp['kwidth'] = kwidth
00189         mytmp['chanwidth'] = chanwidth
00190         mytmp['verify'] = verify
00191         mytmp['outfile'] = outfile
00192         mytmp['outform'] = outform
00193         mytmp['overwrite'] = overwrite
00194         mytmp['plotlevel'] = plotlevel
00195         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00196         trec = casac.utils().torecord(pathname+'sdsmooth.xml')
00197 
00198         casalog.origin('sdsmooth')
00199         if not trec.has_key('sdsmooth') or not casac.utils().verify(mytmp, trec['sdsmooth']) :
00200             return False
00201 
00202 
00203         try :
00204           casalog.post('')
00205           casalog.post('##########################################')
00206           casalog.post('##### Begin Task: sdsmooth           #####')
00207           casalog.post('')
00208           result = sdsmooth(infile, antenna, scanaverage, scanlist, field, iflist, pollist, kernel, kwidth, chanwidth, verify, outfile, outform, overwrite, plotlevel)
00209           casalog.post('')
00210           casalog.post('##### End Task: sdsmooth           #####')
00211           casalog.post('##########################################')
00212 
00213 
00214 # saveinputs for individule engine has no use
00215 # saveinputs should alos be removed from casa_in_py.py
00216 #
00217 #
00218 #          saveinputs = myf['saveinputs']
00219 #          saveinputs('sdsmooth', 'sdsmooth.last', myparams)
00220 #
00221 #
00222         except Exception, instance:
00223           #print '**** Error **** ',instance
00224           pass
00225 
00226         gc.collect()
00227         return result
00228 #
00229 #
00230 ##
00231 #    def paramgui(self, useGlobals=True):
00232 #        """
00233 #        Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00234 #        """
00235 #        import paramgui
00236 #
00237 #        a=inspect.stack()
00238 #        stacklevel=0
00239 #        for k in range(len(a)):
00240 #          if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00241 #            stacklevel=k
00242 #            break
00243 #        myf = sys._getframe(stacklevel).f_globals
00244 #
00245 #        if useGlobals:
00246 #            paramgui.setGlobals(myf)
00247 #        else:
00248 #            paramgui.setGlobals({})
00249 #
00250 #        paramgui.runTask('sdsmooth', myf['_ip'])
00251 #        paramgui.setGlobals({})
00252 #
00253 #
00254 #
00255 #
00256     def defaults(self, param=None):
00257         a=inspect.stack()
00258         stacklevel=0
00259         for k in range(len(a)):
00260           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00261                 stacklevel=k
00262                 break
00263         myf=sys._getframe(stacklevel).f_globals
00264         a = odict()
00265         a['infile']  = ''
00266         a['antenna']  = 0
00267         a['scanaverage']  = False
00268         a['scanlist']  = []
00269         a['field']  = ''
00270         a['iflist']  = []
00271         a['pollist']  = []
00272         a['kernel']  = 'hanning'
00273         a['verify']  = False
00274         a['outfile']  = ''
00275         a['outform']  = 'ASAP'
00276         a['overwrite']  = False
00277         a['plotlevel']  = 0
00278 
00279         a['async']=False
00280         a['kernel'] = {
00281                     0:{'value':'hanning'}, 
00282                     1:odict([{'value':'gaussian'}, {'kwidth':5}]), 
00283                     2:odict([{'value':'boxcar'}, {'kwidth':5}]), 
00284                     3:odict([{'value':'regrid'}, {'chanwidth':'5'}])}
00285 
00286 ### This function sets the default values but also will return the list of
00287 ### parameters or the default value of a given parameter
00288         if(param == None):
00289                 myf['__set_default_parameters'](a)
00290         elif(param == 'paramkeys'):
00291                 return a.keys()
00292         else:
00293                 if(a.has_key(param)):
00294                    #if(type(a[param]) == dict) :
00295                    #   return a[param][len(a[param])-1]['value']
00296                    #else :
00297                       return a[param]
00298 
00299 
00300 #
00301 #
00302     def check_params(self, param=None, value=None):
00303       a=inspect.stack() 
00304       stacklevel=0
00305       for k in range(len(a)):
00306         if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00307             stacklevel=k
00308             break
00309       myf=sys._getframe(stacklevel).f_globals
00310 
00311 #      print 'param:', param, 'value:', value
00312       try :
00313          if str(type(value)) != "<type 'instance'>" :
00314             value0 = value
00315             value = myf['cu'].expandparam(param, value)
00316             matchtype = False
00317             if(type(value) == numpy.ndarray):
00318                if(type(value) == type(value0)):
00319                   myf[param] = value.tolist()
00320                else:
00321                   #print 'value:', value, 'value0:', value0
00322                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00323                   myf[param] = value0
00324                   if type(value0) != list :
00325                      matchtype = True
00326             else :
00327                myf[param] = value
00328             value = myf['cu'].verifyparam({param:value})
00329             if matchtype:
00330                value = False
00331       except Exception, instance:
00332          #ignore the exception and just return it unchecked
00333          myf[param] = value
00334       return value
00335 
00336 #
00337 #
00338     def description(self, key='sdsmooth', subkey=None):
00339         desc={'sdsmooth': 'ASAP SD task: do smoothing of spectra',
00340                'infile': 'name of input SD dataset',
00341                'antenna': 'antenna name or id (only effective for MS input)',
00342                'scanaverage': 'average integs within scans (True,False) ',
00343                'scanlist': 'list of scans to use (e.g. [1,2,3,4])',
00344                'field': 'string for selection by source name',
00345                'iflist': 'list of IF ids to select (e.g. [0,1])',
00346                'pollist': 'list of polarization ids to select (e.g. [0,1])',
00347                'kernel': 'type of spectral smoothing',
00348                'kwidth': 'width of spectral kernel in channels',
00349                'chanwidth': 'width of regridded channels',
00350                'verify': 'verify the results of smoothing [not available for kernel="regrid"]',
00351                'outfile': 'output file name',
00352                'outform': 'output file format (ASCII,MS,SDFITS,ASAP)',
00353                'overwrite': 'overwrite the output file if already exists',
00354                'plotlevel': 'plot results (0=none,1+=some,<0=hardcopy)',
00355 
00356                'async': 'If true the taskname must be started using sdsmooth(...)'
00357               }
00358 
00359 #
00360 # Set subfields defaults if needed
00361 #
00362 
00363         if(desc.has_key(key)) :
00364            return desc[key]
00365 
00366     def itsdefault(self, paramname) :
00367         a = {}
00368         a['infile']  = ''
00369         a['antenna']  = 0
00370         a['scanaverage']  = False
00371         a['scanlist']  = []
00372         a['field']  = ''
00373         a['iflist']  = []
00374         a['pollist']  = []
00375         a['kernel']  = 'hanning'
00376         a['kwidth']  = 5
00377         a['chanwidth']  = '5'
00378         a['verify']  = False
00379         a['outfile']  = ''
00380         a['outform']  = 'ASAP'
00381         a['overwrite']  = False
00382         a['plotlevel']  = 0
00383 
00384         if a.has_key(paramname) :
00385               return a[paramname]
00386 sdsmooth_pg = sdsmooth_pg_()