casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
sdflag_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_sdflag import sdflag
00016 from task_sdflag import casalog
00017 
00018 class sdflag_pg_:
00019     __name__ = "sdflag"
00020 
00021     def __init__(self) :
00022        self.__bases__ = (sdflag_pg_,)
00023        self.__doc__ = self.__call__.__doc__
00024 
00025 
00026     def __call__(self, infile=None, antenna=None, specunit=None, restfreq=None, frame=None, doppler=None, scanlist=None, field=None, iflist=None, pollist=None, maskflag=None, flagrow=None, clip=None, clipminmax=None, clipoutside=None, flagmode=None, interactive=None, showflagged=None, outfile=None, outform=None, overwrite=None, plotlevel=None, async=None):
00027 
00028         """ASAP SD spectral flagging task 
00029         Keyword arguments:
00030         infile -- name of input SD dataset
00031         antenna -- antenna name or id (only effective for MS input). 
00032         specunit -- units for spectral axis
00033                 options: (str) 'channel','km/s','GHz','MHz','kHz','Hz',''
00034                 default: '' (=current)
00035                 example: this will be the units for maskflag
00036             >>> specunit expandable parameters
00037                  restfreq -- rest frequency
00038                          available type includes float, int, string, list of float, 
00039                          list of int, list of string, and list of dictionary. the 
00040                          default unit of restfreq in case of float, int, or string 
00041                          without unit is Hz. string input can be a value only 
00042                          (treated as Hz) or a value followed by unit for which 'GHz',
00043                          'MHz','kHz',and 'Hz' are available. 
00044                          a list can be used to set different rest frequencies for 
00045                          each IF. the length of list input must be nIF. dictionary 
00046                          input should be a pair of molecule name and frequency with 
00047                          keys of 'name' and 'value', respectively. values in the 
00048                          dictionary input follows the same manner as for single 
00049                          float or string input. 
00050                          example: 345.796
00051                                   '1420MHz'
00052                                   [345.8, 347.0, 356.7]
00053                                   ['345.8MHz', '347.0MHz', '356.7MHz']
00054                                   [{'name':'CO','value':345}]
00055         frame -- frequency frame for spectral axis
00056                 options: (str) 'LSRK','REST','TOPO','LSRD','BARY',
00057                          'GEO','GALACTO','LGROUP','CMB'
00058                 default: currently set frame in scantable
00059                 WARNING: frame='REST' not yet implemented
00060         doppler -- doppler mode
00061                 options: (str) 'RADIO','OPTICAL','Z','BETA','GAMMA'
00062                 default: currently set doppler in scantable
00063         scanlist -- list of scan numbers to process
00064                 default: [] (use all scans)
00065                 example: [21,22,23,24]
00066                 this selection is in addition to field and iflist
00067         field -- selection string for selecting scans by name
00068                 default: '' (no name selection)
00069                 example: 'FLS3a*'
00070                 this selection is in addition to scanlist, iflist, and pollist
00071         iflist -- list of IF id numbers to select
00072                 default: [] (use all IFs)
00073                 example: [15]
00074                 this selection is in addition to scanlist, field, and pollist
00075         pollist -- list of polarization id numbers to select
00076                 default: [] (use all polarizations)
00077                 example: [1]
00078                 this selection is in addition to scanlist, field, and iflist
00079         maskflag -- list of mask regions to apply flag/unflag 
00080                     Note, this parameter is ignored if one or more rows are 
00081                     given in flagrow, or clip=True.
00082                 default: [] (entire spectrum)
00083                 example: [[1000,3000],[5000,7000]]
00084         flagrow -- list of row numbers to apply flag/unflag
00085                    Note, this parameter is effective only when one or more row 
00086                    numbers are given explicitly and also clip=False
00087                 default: [] (no row selection)
00088                 example: [0, 2, 3]
00089         clip -- flag data that are outside a specified range
00090                 options: (bool)True,False
00091                 default: False
00092              >>> clip expandable parameters
00093                 clipminmax -- range of data that will NOT be flagged
00094                         default: [] means do not use clip option
00095                         example: [0.0,1.5]
00096                 clipoutside -- clip OUTSIDE the range ?
00097                         options: (bool)True,False
00098                         default: True
00099                         example: clipoutside=False means flag data WITHIN the range.
00100         flagmode -- flag mode
00101                 default: 'flag'
00102                 options: 'flag','unflag'
00103         interactive -- determines interactive flagging
00104                 options: (bool) True,False
00105                 default: False
00106              >>> interactive expandable parameters
00107                 showflagged -- show flagged data on plots
00108                         default: False
00109         outfile -- Name of output file
00110                 default: ''
00111                 Note: by default (outfile=''), actual output file name is set as follows: 
00112                       (1) if overwrite=True (default), infile (input) will be overwritten.
00113                       WARNING: If the formats of input and ouput files are different, 
00114                                this causes complete loss of input file.
00115                       (2) if overwrite=False, outfile will be <infile>_f. 
00116         outform -- format of output file
00117                 options: 'ASCII','SDFITS','MS','ASAP'
00118                 default: 'ASAP'
00119                 example: the ASAP format is easiest for further sd
00120                          processing; use MS for CASA imaging.
00121                 WARNING: Be sure outform is same as the input file format when you 
00122                          overwrite the input file by overwrite=True and outfile='' (default).
00123         overwrite -- overwrite the output file if already exists
00124                 options: (bool) True,False
00125                 default: True
00126                 WARNING: input file is overwritten if overwrite=True and outfile='' (default). 
00127                          This causes the complete loss of input file if the formats of
00128                          input and ouput files are different.
00129         plotlevel -- control for plotting of results
00130                 options: (int) 0=none, 1=some, 2=more, <0=hardcopy
00131                 default: 0 (no plotting)
00132                 example: plotlevel<0 as abs(plotlevel), e.g.
00133                          -1 => hardcopy of final plot (will be named
00134                          <outfile>_flag.eps)
00135                 WARNING: be careful plotting in fsotf mode!
00136 
00137         -------------------------------------------------------------------
00138 
00139         DESCRIPTION:
00140 
00141         Task sdflag performs both interactive and non-interactive channel/row 
00142         based flagging on spectra.
00143         Currently, the available ways of non-interactive flagging include: 
00144         (1) channel based flagging by specifying a range of spectral values 
00145         with clip=True, (2) row based flagging by specifying a list of row 
00146         numbers to the flagrow parameter, and (3) channel based flagging by 
00147         specifying regions in channel to the maskflag parameter. 
00148         These three ways of flagging can not be executed simultaneously. 
00149         If more than one parameter above are specified, the task looks for 
00150         them in the above order and operates the first specified way of 
00151         flagging operation.
00152 
00153         Interactive flagging is available when interactive=True. 
00154         The available ways of interactive flagging include: 
00155         (1) row based flagging by selecting 'panel' and (2) channel
00156         based flagging by selecting 'region's of channels on Flag plotter. 
00157         Note that the Flag plotter is loaded after carrying out the 
00158         non-interactive flag operation if any specified. See the cookbook for
00159         details of how to select channel regions and spectra on the plotter.
00160 
00161         If plotlevel>=1, the task asks you if you really apply the 
00162         flags before it is actually written to the data with a plot 
00163         indicating flagged regions.
00164         Please note that this task is still experimental.
00165 
00166         WARNING for overwrite option:
00167         Be sure 'outform' is the same as data format of input file when you
00168         overwrite it. Since CASA 3.1, the default value of the option 'overwrite'
00169         has been changed to True, thereby the current dataset (infile) is 
00170         overwritten unless a different file name is set to outfile. 
00171         There is a known issue in overwriting infile. If 'outform' differs to the
00172         data format of infile, the data is overwritten with the new data format 
00173         (specified by 'outform') and the data in the original format will be lost.
00174 
00175         WARNING for the GBT raw SDFITS format data as input:
00176         SDtasks are able to handle GBT raw SDFITS format data since the 
00177         data filler is available. However, the functionality is not well 
00178         tested yet, so that there may be unknown bugs.  
00179  
00180   
00181         """
00182         a=inspect.stack()
00183         stacklevel=0
00184         for k in range(len(a)):
00185           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00186                 stacklevel=k
00187                 break
00188         myf=sys._getframe(stacklevel).f_globals
00189         myf['__last_task'] = 'sdflag'
00190         myf['taskname'] = 'sdflag'
00191         ###
00192         myf['update_params'](func=myf['taskname'],printtext=False)
00193         ###
00194         ###
00195         #Handle globals or user over-ride of arguments
00196         #
00197         function_signature_defaults=dict(zip(self.__call__.func_code.co_varnames,self.__call__.func_defaults))
00198         useLocalDefaults = False
00199 
00200         for item in function_signature_defaults.iteritems():
00201                 key,val = item
00202                 keyVal = eval(key)
00203                 if (keyVal == None):
00204                         #user hasn't set it - use global/default
00205                         pass
00206                 else:
00207                         #user has set it - use over-ride
00208                         if (key != 'self') :
00209                            useLocalDefaults = True
00210                         #myf[key]=keyVal
00211 
00212         myparams = {}
00213         if useLocalDefaults :
00214            for item in function_signature_defaults.iteritems():
00215                key,val = item
00216                keyVal = eval(key)
00217                exec('myparams[key] = keyVal')
00218                if (keyVal == None):
00219                    exec('myparams[key] = '+ key + ' = self.itsdefault(key)')
00220                    keyVal = eval(key)
00221                    if(type(keyVal) == dict) :
00222                       exec('myparams[key] = ' + key + ' = keyVal[len(keyVal)-1][\'value\']')
00223 
00224         else :
00225             uselessvariable = None 
00226             myparams['infile'] = infile = myf['infile']
00227             myparams['antenna'] = antenna = myf['antenna']
00228             myparams['specunit'] = specunit = myf['specunit']
00229             myparams['restfreq'] = restfreq = myf['restfreq']
00230             myparams['frame'] = frame = myf['frame']
00231             myparams['doppler'] = doppler = myf['doppler']
00232             myparams['scanlist'] = scanlist = myf['scanlist']
00233             myparams['field'] = field = myf['field']
00234             myparams['iflist'] = iflist = myf['iflist']
00235             myparams['pollist'] = pollist = myf['pollist']
00236             myparams['maskflag'] = maskflag = myf['maskflag']
00237             myparams['flagrow'] = flagrow = myf['flagrow']
00238             myparams['clip'] = clip = myf['clip']
00239             myparams['clipminmax'] = clipminmax = myf['clipminmax']
00240             myparams['clipoutside'] = clipoutside = myf['clipoutside']
00241             myparams['flagmode'] = flagmode = myf['flagmode']
00242             myparams['interactive'] = interactive = myf['interactive']
00243             myparams['showflagged'] = showflagged = myf['showflagged']
00244             myparams['outfile'] = outfile = myf['outfile']
00245             myparams['outform'] = outform = myf['outform']
00246             myparams['overwrite'] = overwrite = myf['overwrite']
00247             myparams['plotlevel'] = plotlevel = myf['plotlevel']
00248 
00249         if type(scanlist)==int: scanlist=[scanlist]
00250         if type(iflist)==int: iflist=[iflist]
00251         if type(maskflag)==int: maskflag=[maskflag]
00252         if type(flagrow)==int: flagrow=[flagrow]
00253 
00254         result = None
00255 
00256 #
00257 #    The following is work around to avoid a bug with current python translation
00258 #
00259         mytmp = {}
00260 
00261         mytmp['infile'] = infile
00262         mytmp['antenna'] = antenna
00263         mytmp['specunit'] = specunit
00264         mytmp['restfreq'] = restfreq
00265         mytmp['frame'] = frame
00266         mytmp['doppler'] = doppler
00267         mytmp['scanlist'] = scanlist
00268         mytmp['field'] = field
00269         mytmp['iflist'] = iflist
00270         mytmp['pollist'] = pollist
00271         mytmp['maskflag'] = maskflag
00272         mytmp['flagrow'] = flagrow
00273         mytmp['clip'] = clip
00274         mytmp['clipminmax'] = clipminmax
00275         mytmp['clipoutside'] = clipoutside
00276         mytmp['flagmode'] = flagmode
00277         mytmp['interactive'] = interactive
00278         mytmp['showflagged'] = showflagged
00279         mytmp['outfile'] = outfile
00280         mytmp['outform'] = outform
00281         mytmp['overwrite'] = overwrite
00282         mytmp['plotlevel'] = plotlevel
00283         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00284         trec = casac.utils().torecord(pathname+'sdflag.xml')
00285 
00286         casalog.origin('sdflag')
00287         if not trec.has_key('sdflag') or not casac.utils().verify(mytmp, trec['sdflag']) :
00288             return False
00289 
00290 
00291         try :
00292           casalog.post('')
00293           casalog.post('##########################################')
00294           casalog.post('##### Begin Task: sdflag           #####')
00295           casalog.post('')
00296           result = sdflag(infile, antenna, specunit, restfreq, frame, doppler, scanlist, field, iflist, pollist, maskflag, flagrow, clip, clipminmax, clipoutside, flagmode, interactive, showflagged, outfile, outform, overwrite, plotlevel)
00297           casalog.post('')
00298           casalog.post('##### End Task: sdflag           #####')
00299           casalog.post('##########################################')
00300 
00301 
00302 # saveinputs for individule engine has no use
00303 # saveinputs should alos be removed from casa_in_py.py
00304 #
00305 #
00306 #          saveinputs = myf['saveinputs']
00307 #          saveinputs('sdflag', 'sdflag.last', myparams)
00308 #
00309 #
00310         except Exception, instance:
00311           #print '**** Error **** ',instance
00312           pass
00313 
00314         gc.collect()
00315         return result
00316 #
00317 #
00318 ##
00319 #    def paramgui(self, useGlobals=True):
00320 #        """
00321 #        Opens a parameter GUI for this task.  If useGlobals is true, then any relevant global parameter settings are used.
00322 #        """
00323 #        import paramgui
00324 #
00325 #        a=inspect.stack()
00326 #        stacklevel=0
00327 #        for k in range(len(a)):
00328 #          if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00329 #            stacklevel=k
00330 #            break
00331 #        myf = sys._getframe(stacklevel).f_globals
00332 #
00333 #        if useGlobals:
00334 #            paramgui.setGlobals(myf)
00335 #        else:
00336 #            paramgui.setGlobals({})
00337 #
00338 #        paramgui.runTask('sdflag', myf['_ip'])
00339 #        paramgui.setGlobals({})
00340 #
00341 #
00342 #
00343 #
00344     def defaults(self, param=None):
00345         a=inspect.stack()
00346         stacklevel=0
00347         for k in range(len(a)):
00348           if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00349                 stacklevel=k
00350                 break
00351         myf=sys._getframe(stacklevel).f_globals
00352         a = odict()
00353         a['infile']  = ''
00354         a['antenna']  = 0
00355         a['specunit']  = ''
00356         a['frame']  = ''
00357         a['doppler']  = ''
00358         a['scanlist']  = []
00359         a['field']  = ''
00360         a['iflist']  = []
00361         a['pollist']  = []
00362         a['maskflag']  = []
00363         a['flagrow']  = []
00364         a['clip']  = False
00365         a['flagmode']  = 'flag'
00366         a['interactive']  = False
00367         a['outfile']  = ''
00368         a['outform']  = 'ASAP'
00369         a['overwrite']  = True
00370         a['plotlevel']  = 0
00371 
00372         a['async']=False
00373         a['specunit'] = {
00374                     0:{'value':''}, 
00375                     1:{'value':'channel'}, 
00376                     2:{'value':'GHz'}, 
00377                     3:{'value':'MHz'}, 
00378                     4:{'value':'kHz'}, 
00379                     5:{'value':'Hz'}, 
00380                     6:odict([{'value':'km/s'}, {'restfreq':''}])}
00381         a['clip'] = {
00382                     0:{'value':False}, 
00383                     1:odict([{'value':True}, {'clipminmax':[]}, {'clipoutside':True}])}
00384         a['interactive'] = {
00385                     0:{'value':False}, 
00386                     1:odict([{'value':True}, {'showflagged':False}])}
00387 
00388 ### This function sets the default values but also will return the list of
00389 ### parameters or the default value of a given parameter
00390         if(param == None):
00391                 myf['__set_default_parameters'](a)
00392         elif(param == 'paramkeys'):
00393                 return a.keys()
00394         else:
00395                 if(a.has_key(param)):
00396                    #if(type(a[param]) == dict) :
00397                    #   return a[param][len(a[param])-1]['value']
00398                    #else :
00399                       return a[param]
00400 
00401 
00402 #
00403 #
00404     def check_params(self, param=None, value=None):
00405       a=inspect.stack() 
00406       stacklevel=0
00407       for k in range(len(a)):
00408         if (string.find(a[k][1], 'ipython console') > 0) or (string.find(a[k][1], '<string>') >= 0):
00409             stacklevel=k
00410             break
00411       myf=sys._getframe(stacklevel).f_globals
00412 
00413 #      print 'param:', param, 'value:', value
00414       try :
00415          if str(type(value)) != "<type 'instance'>" :
00416             value0 = value
00417             value = myf['cu'].expandparam(param, value)
00418             matchtype = False
00419             if(type(value) == numpy.ndarray):
00420                if(type(value) == type(value0)):
00421                   myf[param] = value.tolist()
00422                else:
00423                   #print 'value:', value, 'value0:', value0
00424                   #print 'type(value):', type(value), 'type(value0):', type(value0)
00425                   myf[param] = value0
00426                   if type(value0) != list :
00427                      matchtype = True
00428             else :
00429                myf[param] = value
00430             value = myf['cu'].verifyparam({param:value})
00431             if matchtype:
00432                value = False
00433       except Exception, instance:
00434          #ignore the exception and just return it unchecked
00435          myf[param] = value
00436       return value
00437 
00438 #
00439 #
00440     def description(self, key='sdflag', subkey=None):
00441         desc={'sdflag': 'ASAP SD spectral flagging task ',
00442                'infile': 'name of input SD dataset',
00443                'antenna': 'antenna name or id (only effective for MS input)',
00444                'specunit': 'units for spectral axis (channel,km/s,GHz)',
00445                'restfreq': 'rest frequency (default unit: Hz)',
00446                'frame': 'frequency reference frame, e.g. LSRK (''=current)',
00447                'doppler': 'doppler convention, e.g. RADIO (''=current)',
00448                'scanlist': 'list of scans to use (e.g. [1,2,3,4])',
00449                'field': 'string for selection by source name',
00450                'iflist': 'list of IF ids to select (e.g. [0,1])',
00451                'pollist': 'list of polarization by ids or names to select (e.g. [0,1])',
00452                'maskflag': 'list of mask regions to flag/unflag',
00453                'flagrow': 'list of row numbers to apply row-based flagging/unflagging',
00454                'clip': 'flag data outside a specified range',
00455                'clipminmax': 'range of data that will NOT be flagged',
00456                'clipoutside': 'clip outside the range, or within it',
00457                'flagmode': 'flag mode (flag,unflag)',
00458                'interactive': 'determines interactive flagging',
00459                'showflagged': 'show flagged data on plots',
00460                'outfile': 'name of output file (See a WARNING in help document)',
00461                'outform': 'output file format (ASCII,MS,SDFITS,ASAP) (See a WARNING in help document)',
00462                'overwrite': 'overwrite the output file if already exists (See a WARNING in help document)',
00463                'plotlevel': 'control for plotting of results',
00464 
00465                'async': 'If true the taskname must be started using sdflag(...)'
00466               }
00467 
00468 #
00469 # Set subfields defaults if needed
00470 #
00471 
00472         if(desc.has_key(key)) :
00473            return desc[key]
00474 
00475     def itsdefault(self, paramname) :
00476         a = {}
00477         a['infile']  = ''
00478         a['antenna']  = 0
00479         a['specunit']  = ''
00480         a['restfreq']  = ''
00481         a['frame']  = ''
00482         a['doppler']  = ''
00483         a['scanlist']  = []
00484         a['field']  = ''
00485         a['iflist']  = []
00486         a['pollist']  = []
00487         a['maskflag']  = []
00488         a['flagrow']  = []
00489         a['clip']  = False
00490         a['clipminmax']  = []
00491         a['clipoutside']  = True
00492         a['flagmode']  = 'flag'
00493         a['interactive']  = False
00494         a['showflagged']  = False
00495         a['outfile']  = ''
00496         a['outform']  = 'ASAP'
00497         a['overwrite']  = True
00498         a['plotlevel']  = 0
00499 
00500         if a.has_key(paramname) :
00501               return a[paramname]
00502 sdflag_pg = sdflag_pg_()