casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
task_flagdata.py
Go to the documentation of this file.
00001 from taskinit import *
00002 import time
00003 import os
00004 import sys
00005 import flaghelper as fh
00006 from parallel.parallel_task_helper import ParallelTaskHelper
00007 
00008 debug = False
00009 
00010 
00011 def flagdata(vis,
00012              mode,
00013              autocorr,      # mode manual parameter
00014              inpfile,       # mode list parameters
00015              reason,
00016              spw,           # data selection parameters
00017              field,
00018              antenna,
00019              uvrange,
00020              timerange,
00021              correlation,
00022              scan,
00023              intent,
00024              array,
00025              observation,
00026              feed,
00027              clipminmax,    # mode clip parameters
00028              datacolumn,
00029              clipoutside,
00030              channelavg,
00031              clipzeros,
00032              quackinterval, # mode quack parameters
00033              quackmode,
00034              quackincrement,
00035              tolerance,      # mode shadow parameter
00036              addantenna,
00037              lowerlimit,    # mode elevation parameters
00038              upperlimit,
00039              ntime,         # mode tfcrop
00040              combinescans,
00041              timecutoff,    
00042              freqcutoff,
00043              timefit,
00044              freqfit,
00045              maxnpieces,
00046              flagdimension,
00047              usewindowstats,
00048              halfwin,
00049              winsize,    # rflag parameters
00050              timedev,
00051              freqdev,
00052              timedevscale,
00053              freqdevscale,
00054              spectralmax,
00055              spectralmin,
00056              extendpols,    # mode extend
00057              growtime,
00058              growfreq,
00059              growaround,
00060              flagneartime,
00061              flagnearfreq,
00062              minrel,        # mode summary
00063              maxrel,
00064              minabs,
00065              maxabs,
00066              spwchan,
00067              spwcorr,
00068              basecnt,
00069              action,           # run or not the tool
00070              display,
00071              flagbackup,
00072              savepars,      # save the current parameters to FLAG_CMD  or to a file
00073              cmdreason,     # reason to save to flag cmd
00074              outfile):      # output file to save flag commands
00075 
00076     # Global parameters
00077     # vis, mode, action, savepars                      
00078     
00079     #
00080     # Task flagdata
00081     #    Flags data from an MS or calibration table based on data selection in various ways
00082     
00083     casalog.origin('flagdata')
00084     
00085     # jagonzal (CAS-4119): Use absolute paths for input files to ensure that the engines find them
00086     if isinstance(inpfile, str) and inpfile != "":                   
00087         inpfile = os.path.abspath(inpfile)
00088         fh.addAbsPath(inpfile)
00089         
00090     if (outfile != ""):
00091         outfile = os.path.abspath(outfile)        
00092         
00093     if (isinstance(addantenna,str) and addantenna != ""):
00094         addantenna = os.path.abspath(addantenna)
00095         
00096     if (isinstance(timedev,str) and timedev != ""):
00097         timedev = os.path.abspath(timedev)        
00098         
00099     if (isinstance(freqdev,str) and freqdev != ""):
00100         freqdev = os.path.abspath(freqdev)        
00101         
00102     # CAS-4696
00103     if (action == 'none' or action=='' or action=='calculate' and flagbackup):
00104         flagbackup = False
00105         
00106     # SMC: moved the flagbackup to before initializing the cluster.
00107     # Note that with this change, a flag backup will be created even if
00108     # an error happens that prevents the flagger tool from running.    
00109     if (mode != 'summary' and flagbackup):
00110         casalog.post('Backup original flags before applying new flags')
00111         fh.backupFlags(aflocal=None, msfile=vis, prename='flagdata')
00112         # Set flagbackup to False because only the controller
00113         # should create a backup
00114         flagbackup = False
00115 
00116 
00117     # Save all the locals()
00118     orig_locals = locals()
00119     iscal = False
00120     
00121     # Check if vis is a cal table:
00122     # typevis = 1 --> cal table
00123     # typevis = 0 --> MS
00124     # typevis = 2 --> MMS
00125     typevis = fh.isCalTable(vis)
00126     if typevis == 1:
00127         iscal = True  
00128 
00129     if pCASA.is_mms(vis):
00130         pCASA.execute("flagdata", orig_locals)
00131         return
00132 
00133     # Take care of the trivial parallelization
00134     # jagonzal (CAS-4119): If flags are not going to be applied is better to run in sequential mode
00135 #        if ParallelTaskHelper.isParallelMS(vis) and action != '' and action != 'none':
00136     if typevis == 2 and action != '' and action != 'none':
00137         # To be safe convert file names to absolute paths.
00138         helper = ParallelTaskHelper('flagdata', orig_locals)
00139         # jagonzal (CAS-4119): Override summary minabs,maxabs,minrel,maxrel 
00140         # so that it is done after consolidating the summaries
00141         
00142         # By-pass options to filter summary
00143         filterSummary = False
00144         if ((mode == 'summary') and ((minrel != 0.0) or (maxrel != 1.0) or (minabs != 0) or (maxabs != -1))):
00145             filterSummary = True
00146             
00147             myms = mstool()
00148             myms.open(vis)
00149             subMS_list = myms.getreferencedtables()
00150             myms.close()
00151             
00152             if (minrel != 0.0):
00153                 minreal_dict = create_arg_dict(subMS_list,0.0)
00154                 helper.override_arg('minrel',minreal_dict)
00155             if (maxrel != 1.0):
00156                 maxrel_dict = create_arg_dict(subMS_list,1.0)
00157                 helper.override_arg('maxrel',maxrel_dict)
00158             if (minabs != 0):
00159                 minabs_dict = create_arg_dict(subMS_list,0)
00160                 helper.override_arg('minabs',minabs_dict)
00161             if (maxabs != -1):
00162                 maxabs_dict = create_arg_dict(subMS_list,-1)
00163                 helper.override_arg('maxabs',maxabs_dict)
00164                 
00165         # By-pass options to filter summary
00166         if savepars:  
00167             
00168             myms = mstool()
00169             myms.open(vis)
00170             subMS_list = myms.getreferencedtables()
00171             myms.close()
00172             
00173             savepars_dict = create_arg_dict(subMS_list,False)
00174             helper.override_arg('savepars',savepars_dict)
00175             
00176         retVar = helper.go()
00177         
00178         # Filter summary at MMS level
00179         if (mode == 'summary'):
00180             if filterSummary:
00181                 retVar = filter_summary(retVar,minrel,maxrel,minabs,maxabs)
00182             return retVar
00183         # Save parameters at MMS level
00184         elif savepars:
00185             action = 'none'
00186         else:
00187             return retVar
00188     
00189     # Create local tools
00190     aflocal = casac.agentflagger()
00191     mslocal = casac.ms()
00192 
00193     try: 
00194         # Verify the ntime value
00195         newtime = 0.0
00196         if type(ntime) == float or type(ntime) == int:
00197             if ntime <= 0:
00198                 raise Exception, 'Parameter ntime cannot be < = 0'
00199             else:
00200                 # units are seconds
00201                 newtime = float(ntime)
00202         
00203         elif type(ntime) == str:
00204             if ntime == 'scan':
00205                 # iteration time step is a scan
00206                 newtime = 0.0
00207             else:
00208                 # read the units from the string
00209                 qtime = qa.quantity(ntime)
00210                 
00211                 if qtime['unit'] == 'min':
00212                     # convert to seconds
00213                     qtime = qa.convert(qtime, 's')
00214                 elif qtime['unit'] == '':
00215                     qtime['unit'] = 's'
00216                     
00217                 # check units
00218                 if qtime['unit'] == 's':
00219                     newtime = qtime['value']
00220                 else:
00221                     casalog.post('Cannot convert units of ntime. Will use default 0.0s', 'WARN')
00222                                     
00223         casalog.post("New ntime is of type %s and value %s"%(type(newtime),newtime), 'DEBUG')
00224                 
00225         # Open the MS and attach it to the tool
00226         if ((type(vis) == str) & (os.path.exists(vis))):
00227             aflocal.open(vis, newtime)
00228         else:
00229             raise Exception, 'Visibility data set not found - please verify the name'
00230 
00231 
00232         # Get the parameters for the mode
00233         agent_pars = {}
00234 
00235         
00236         # By default, write flags to the MS
00237         writeflags = True
00238         
00239         # Only the apply action writes to the MS
00240         # action=apply     --> write to the MS
00241         # action=calculate --> do not write to the MS
00242         # action=''        --> do not run the tool and do not write to the MS
00243         if action != 'apply':
00244             writeflags = False
00245                                          
00246         # Default mode
00247         if mode == '' or mode == 'manualflag':
00248             mode = 'manual'
00249         
00250         # Read in the list of commands
00251         if mode == 'list':
00252             casalog.post('List mode is active')
00253             # Parse the input file
00254             try:            
00255                 # Is it a file or a Python list?
00256                 if isinstance(inpfile, list):
00257                     # Make a FLAG_CMD compatible dictionary. Select by reason if requested
00258                     casalog.post('Will read commands from a Python list')
00259                     flagcmd = fh.makeDict(inpfile, reason)
00260                     
00261                 elif isinstance(inpfile, str):
00262                     
00263                     if inpfile == '':
00264                          casalog.post('Input file is empty', 'ERROR')
00265                          
00266                     casalog.post('Will read commands from a file')
00267                     flaglist = fh.readFile(inpfile)
00268                     casalog.post('%s'%flaglist,'DEBUG')
00269                     
00270                     # Make a FLAG_CMD compatible dictionary. Select by reason if requested
00271                     flagcmd = fh.makeDict(flaglist, reason)
00272                 
00273                 else:
00274                     casalog.post('Input type is not supported', 'ERROR')
00275                     
00276                 casalog.post('%s'%flagcmd,'DEBUG')
00277                 
00278                 # Update the list of commands with the selection
00279                 flaglist = []
00280                 for k in flagcmd.keys():
00281                     cmdline = flagcmd[k]['command']
00282                     flaglist.append(cmdline)
00283                                     
00284                 # List of command keys in dictionary
00285                 vrows = flagcmd.keys()
00286                 
00287 
00288             except:
00289                 raise Exception, 'Error reading the input list '
00290             
00291             casalog.post('Read ' + str(vrows.__len__())
00292                          + ' lines from input list ')
00293                              
00294         elif mode == 'manual':
00295             agent_pars['autocorr'] = autocorr
00296             casalog.post('Manual mode is active')
00297 
00298             
00299         elif mode == 'clip':
00300                 
00301             agent_pars['datacolumn'] = datacolumn.upper()
00302             agent_pars['clipoutside'] = clipoutside
00303             agent_pars['channelavg'] = channelavg
00304             agent_pars['clipzeros'] = clipzeros
00305             
00306 
00307             if type(clipminmax) != list:
00308                 casalog.post('Error : clipminmax must be a list : [min,max]', 'ERROR')
00309             # If clipminmax = [], do not write it in the dictionary.
00310             # It will be handled by the framework to flag NaNs only
00311             if clipminmax.__len__() == 2:      
00312                 # Cast to float to avoid the missing decimal point                 
00313                 clipmin = float(clipminmax[0])
00314                 clipmax = float(clipminmax[1])
00315                 clipminmax = []
00316                 clipminmax.append(clipmin)
00317                 clipminmax.append(clipmax)     
00318                 agent_pars['clipminmax'] = clipminmax
00319                 
00320             casalog.post('Clip mode is active')
00321                         
00322         elif mode == 'shadow':
00323                 
00324             agent_pars['tolerance'] = tolerance
00325             
00326             if type(addantenna) == str:
00327                 if addantenna != '':
00328                     # it's a filename, create a dictionary
00329                     antdict = fh.readAntennaList(addantenna)
00330                     agent_pars['addantenna'] = antdict
00331                     
00332             elif type(addantenna) == dict:
00333                 if addantenna != {}:
00334                     agent_pars['addantenna'] = addantenna
00335                                                                
00336             casalog.post('Shadow mode is active')
00337             
00338         elif mode == 'quack':
00339             agent_pars['quackmode'] = quackmode
00340             agent_pars['quackinterval'] = quackinterval
00341             agent_pars['quackincrement'] = quackincrement
00342             casalog.post('Quack mode is active')
00343             
00344 
00345         elif mode == 'elevation':
00346 
00347             agent_pars['lowerlimit'] = lowerlimit
00348             agent_pars['upperlimit'] = upperlimit
00349             casalog.post('Elevation mode is active')
00350             
00351 
00352         elif mode == 'tfcrop':
00353                 
00354             agent_pars['ntime'] = newtime
00355             agent_pars['combinescans'] = combinescans            
00356             agent_pars['datacolumn'] = datacolumn.upper()
00357             agent_pars['timecutoff'] = timecutoff
00358             agent_pars['freqcutoff'] = freqcutoff
00359             agent_pars['timefit'] = timefit
00360             agent_pars['freqfit'] = freqfit
00361             agent_pars['maxnpieces'] = maxnpieces
00362             agent_pars['flagdimension'] = flagdimension
00363             agent_pars['usewindowstats'] = usewindowstats
00364             agent_pars['halfwin'] = halfwin
00365             casalog.post('Time and Frequency (tfcrop) mode is active')
00366 
00367                       
00368         elif mode == 'rflag':
00369 
00370             agent_pars['ntime'] = newtime
00371             agent_pars['combinescans'] = combinescans   
00372             agent_pars['datacolumn'] = datacolumn.upper()
00373             agent_pars['winsize'] = winsize
00374             agent_pars['timedevscale'] = timedevscale
00375             agent_pars['freqdevscale'] = freqdevscale
00376             agent_pars['spectralmax'] = spectralmax
00377             agent_pars['spectralmin'] = spectralmin
00378 
00379             # These can be double, doubleArray, or string.
00380             # writeflags=False : calculate and return thresholds.
00381             # writeflags=True : use given thresholds for this run.
00382             if( type(timedev) == str and writeflags == True):
00383                 timedev = fh.readRFlagThresholdFile(timedev,'timedev')
00384             if( type(freqdev) == str and writeflags == True):
00385                 freqdev = fh.readRFlagThresholdFile(freqdev,'freqdev')
00386 
00387             agent_pars['timedev'] = timedev
00388             agent_pars['freqdev'] = freqdev
00389             
00390             agent_pars['writeflags'] = writeflags
00391             agent_pars['display'] = display
00392 
00393             casalog.post('Rflag mode is active')
00394 
00395         elif mode == 'extend':
00396             agent_pars['ntime'] = newtime
00397             agent_pars['combinescans'] = combinescans                            
00398             agent_pars['extendpols'] = extendpols
00399             agent_pars['growtime'] = growtime
00400             agent_pars['growfreq'] = growfreq
00401             agent_pars['growaround'] = growaround
00402             agent_pars['flagneartime'] = flagneartime
00403             agent_pars['flagnearfreq'] = flagnearfreq
00404             casalog.post('Extend mode is active')
00405             
00406             
00407         elif mode == 'unflag':      
00408             casalog.post('Unflag mode is active')                
00409             
00410         elif mode == 'summary':
00411             agent_pars['spwchan'] = spwchan
00412             agent_pars['spwcorr'] = spwcorr
00413             agent_pars['basecnt'] = basecnt
00414             
00415             # Disable writeflags and savepars
00416             writeflags = False
00417             savepars = False
00418             casalog.post('Summary mode is active')
00419 
00420 
00421         # Create a flagcmd dictionary of the interface parameters to save
00422         # when savepars = True
00423         if mode != 'list' and mode != 'summary':
00424 
00425             # CAS-4063: remove any white space in the values of the
00426             # selection parameters before creating the string.
00427             
00428             # Create a dictionary of the selection parameters
00429             seldic = {}
00430             seldic['field'] = field
00431             seldic['spw'] = spw
00432             seldic['array'] = array
00433             seldic['feed'] = feed
00434             seldic['scan'] = scan
00435             seldic['antenna'] = antenna
00436             seldic['uvrange'] = uvrange
00437             seldic['timerange'] = timerange
00438             seldic['intent'] = intent
00439             seldic['observation'] = str(observation)
00440             
00441             # String to hold the selection parameters
00442             sel_pars = []
00443             sel_pars = ' mode='+mode
00444             if correlation != '':
00445                 # Replace an empty space, in case there is one
00446                 expr = delspace(correlation, '_')
00447                 sel_pars = sel_pars +' correlation=' + expr
00448             
00449             # Include only parameters with values in the string
00450             # Remove the white spaces from the values
00451             for k in seldic.keys():
00452                 if seldic[k] != '':
00453                     # Delete any space in the value
00454                     val = delspace(seldic[k], '')
00455                     sel_pars = sel_pars +' ' + k + '=' + val
00456                                
00457             # Add the agent's parameters to the same string 
00458             for k in agent_pars.keys():
00459                 if agent_pars[k] != '':
00460                     # Remove any white space from the string value
00461                     nospace = delspace(str(agent_pars[k]),'')
00462                     sel_pars = sel_pars + ' ' + k + '=' + nospace
00463                 
00464             
00465             # Create a dictionary according to the FLAG_CMD structure
00466             flagcmd = fh.makeDict([sel_pars])
00467                         
00468             # Number of commands in dictionary
00469             vrows = flagcmd.keys()
00470             casalog.post('There are %s cmds in dictionary of mode %s'%(vrows.__len__(),mode),'DEBUG')
00471 
00472 
00473         # Setup global parameters in the agent's dictionary
00474         apply = True
00475                     
00476         # Correlation does not go in selectdata, but in the agent's parameters
00477         if correlation != '':
00478             agent_pars['correlation'] = correlation.upper()
00479         
00480         
00481         # Hold the name of the agent
00482         agent_name = mode.capitalize()
00483         agent_pars['name'] = agent_name
00484         agent_pars['mode'] = mode
00485         agent_pars['apply'] = apply      
00486                           
00487         ##########  Only save the parameters and exit; action = ''     
00488         if (action == '' or action == 'none') and savepars == False:
00489             casalog.post('Parameter action=\'\' is only meaningful with savepars=True.', 'WARN')
00490             return 0
00491         
00492         if (action == '' or action == 'none') and savepars == True:
00493             if iscal:
00494                 if outfile == '':
00495                     casalog.post('Saving to FLAG_CMD is not supported for cal tables', 'WARN')
00496                 else:
00497                     casalog.post('Saving parameters to '+outfile)                            
00498                     fh.writeFlagCmd(vis, flagcmd, vrows, False, cmdreason, outfile) 
00499             else: 
00500                 if outfile == '':
00501                     casalog.post('Saving parameters to FLAG_CMD')
00502                 else:
00503                     casalog.post('Saving parameters to '+outfile)                            
00504                 
00505                 fh.writeFlagCmd(vis, flagcmd, vrows, False, cmdreason, outfile) 
00506                      
00507             return 0
00508 
00509         
00510         ######### From now on it is assumed that action = apply or calculate
00511         
00512         # Select the data and parse the agent's parameters
00513         if mode != 'list':
00514             aflocal.selectdata(field=field, spw=spw, array=array, feed=feed, scan=scan, \
00515                                antenna=antenna, uvrange=uvrange, time=timerange, \
00516                                intent=intent, observation=str(observation))   
00517 
00518             # CAS-3959 Handle channel selection at the FlagAgent level
00519             agent_pars['spw'] = spw
00520             casalog.post('Parsing the parameters for the %s mode'%mode)
00521             if (not aflocal.parseagentparameters(agent_pars)):
00522                 casalog.post('Failed to parse parameters for mode %s' %mode, 'ERROR')
00523                 
00524             casalog.post('%s'%agent_pars, 'DEBUG')
00525        
00526         else:        
00527             # Select a loose union of the data selection from the list
00528             # The loose union will be calculated for field and spw only
00529             # antenna, correlation and timerange should be handled by the agent
00530             if vrows.__len__() == 0:
00531                 raise Exception, 'There are no valid commands in list'
00532             
00533             unionpars = {}
00534             # Do not crete union for a cal table
00535             if iscal:
00536                 if vrows.__len__() == 1:
00537                     cmd0 = flagcmd[vrows[0]]['command']
00538                     unionpars = fh.getSelectionPars(cmd0)
00539                     casalog.post('The selected subset of the cal table will be: ');
00540                     casalog.post('%s'%unionpars);
00541                     
00542             else:
00543                 if vrows.__len__() > 1:
00544                    unionpars = fh.getUnion(vis, flagcmd)
00545                    
00546                    if( len( unionpars.keys() ) > 0 ):
00547                         casalog.post('Pre-selecting a subset of the MS : ');
00548                         casalog.post('%s'%unionpars)
00549                         
00550                    else:
00551                         casalog.post('Iterating through the entire MS');
00552                         
00553     #               mslocal.close()
00554                         
00555                 # Get all the selection parameters, but set correlation to ''
00556                 elif vrows.__len__() == 1:
00557                     cmd0 = flagcmd[vrows[0]]['command']
00558                     unionpars = fh.getSelectionPars(cmd0)
00559                     casalog.post('The selected subset of the MS will be: ');
00560                     casalog.post('%s'%unionpars);
00561                 
00562             aflocal.selectdata(unionpars);
00563 
00564             # Parse the parameters for each agent in the list
00565             list2save = fh.setupAgent(aflocal, flagcmd, [], apply, writeflags, display)
00566 
00567         # Do display if requested
00568         if display != '':
00569             
00570             agent_pars = {}
00571             casalog.post('Parsing the display parameters')
00572                 
00573             agent_pars['mode'] = 'display'
00574             # need to create different parameters for both, data and report.
00575             if display == 'both':
00576                 agent_pars['datadisplay'] = True
00577                 agent_pars['reportdisplay'] = True
00578             
00579             elif display == 'data':
00580                 agent_pars['datadisplay'] = True
00581             
00582             elif display == 'report':
00583                 agent_pars['reportdisplay'] = True
00584                 
00585             # jagonzal: CAS-3966 Add datacolumn to display agent parameters
00586             agent_pars['datacolumn'] = datacolumn.upper()
00587             aflocal.parseagentparameters(agent_pars)
00588             
00589             # Disable saving the parameters to avoid inconsistencies
00590             if savepars:
00591                 casalog.post('Disabling savepars for the display', 'WARN')
00592                 savepars = False
00593                     
00594         # Initialize the agents
00595         casalog.post('Initializing the agents')
00596         aflocal.init()
00597 
00598         # HPC work: moved the flagbackup to before the cluster
00599         # initialization.
00600         # Backup the existing flags before applying new ones
00601 #        if flagbackup and writeflags:
00602 #            casalog.post('Backup original flags before applying new flags')
00603 #            fh.backupFlags(aflocal, 'flagdata')
00604         
00605         # Run the tool
00606         casalog.post('Running the agentflagger tool')
00607         summary_stats_list = aflocal.run(writeflags, True)
00608         
00609         # Inform the user that end of MS summary was not written to the MS
00610         if not writeflags:
00611             casalog.post("Flags are not written to the MS. (action=\'calculate\')")
00612 
00613 
00614         # Now, deal with all the modes that return output.
00615         # Summary : Currently, only one is allowed in the task
00616         # Rflag : There can be many 'rflags' in the list mode.
00617 
00618         ## Pull out RFlag outputs. There will be outputs only if writeflags=False
00619         if (mode == 'rflag' or mode== 'list') and (writeflags==False):  
00620             fh.extractRFlagOutputFromSummary(mode,summary_stats_list, flagcmd)
00621 
00622         # Save the current parameters/list to FLAG_CMD or to output
00623         if savepars:  
00624             # Cal table type
00625             if iscal:
00626                 if outfile == '':
00627                     casalog.post('Saving to FLAG_CMD is not supported for cal tables', 'WARN')
00628                 else:
00629                     casalog.post('Saving parameters to '+outfile)
00630                     if mode != 'list':     
00631                         fh.writeFlagCmd(vis, flagcmd, vrows, writeflags, cmdreason, outfile)  
00632                     else:
00633                         valid_rows = list2save.keys()
00634                         fh.writeFlagCmd(vis, list2save, valid_rows, writeflags, cmdreason, outfile)        
00635                     
00636             # MS type
00637             else:                
00638                 if outfile == '':
00639                     casalog.post('Saving parameters to FLAG_CMD')        
00640                 else:
00641                     casalog.post('Saving parameters to '+outfile)
00642                                           
00643                 if mode != 'list':     
00644                     fh.writeFlagCmd(vis, flagcmd, vrows, writeflags, cmdreason, outfile)  
00645                 else:
00646                     valid_rows = list2save.keys()
00647                     fh.writeFlagCmd(vis, list2save, valid_rows, writeflags, cmdreason, outfile)        
00648             
00649         # Destroy the tool
00650         aflocal.done()
00651 
00652         # Pull out the 'summary' part of summary_stats_list.
00653         # (This is the task, and there will be only one such dictionary.)
00654         # After this step, the only thing left in summary_stats_list are the
00655         # list of reports/views, if any.  Return it, if the user wants it.
00656         summary_stats={};
00657         if mode == 'summary':
00658            if type(summary_stats_list) is dict:
00659                nreps = summary_stats_list['nreport'];
00660                for rep in range(0,nreps):
00661                     repname = "report"+str(rep);
00662                     if summary_stats_list[repname]['type'] == "summary":
00663                           summary_stats = summary_stats_list.pop(repname);
00664                           summary_stats_list[repname] = {'type':'none','name':'none'};
00665                           break;  # pull out only one summary.
00666         
00667            # Filter out baselines/antennas/fields/spws/... from summary_stats
00668            # which do not fall within limits
00669            summary_stats = filter_summary(summary_stats,minrel,maxrel,minabs,maxabs)
00670         
00671         # if (need to return the reports/views as well as summary_stats) :
00672         #      return summary_stats , summary_stats_list;
00673         # else :
00674         #      return summary_stats;
00675         return summary_stats;
00676     
00677     except Exception, instance:
00678         aflocal.done()
00679         casalog.post('%s'%instance,'ERROR')
00680         raise
00681         
00682     # Write history to the MS
00683     if not iscal:
00684         try:
00685             mslocal.open(vis, nomodify=False)
00686             mslocal.writehistory(message='taskname = flagdata', origin='flagdata')
00687             param_names = flagdata.func_code.co_varnames[:flagdata.func_code.co_argcount]
00688             param_vals = [eval(p) for p in param_names]
00689             retval &= write_history(mslocal, vis, 'flagdata', param_names,
00690                                     param_vals, casalog)
00691             
00692             mslocal.close()
00693         except Exception, instance:
00694             mslocal.close()
00695             casalog.post("*** Error \'%s\' updating HISTORY" % (instance),
00696                              'WARN')
00697         
00698     return
00699 
00700 
00701 
00702 def delspace(word, replace):
00703     '''Replace the white space of a string with another character'''
00704     
00705     newword = word
00706     if word.count(' ') > 0:
00707         newword = word.replace(' ', replace)
00708     
00709     return newword
00710 
00711 def filter_summary(summary_stats,minrel,maxrel,minabs,maxabs):
00712     
00713     if type(summary_stats) is dict:
00714         for x in summary_stats.keys():
00715             if type(summary_stats[x]) is dict:
00716                 for xx in summary_stats[x].keys():
00717                     flagged = summary_stats[x][xx]
00718                     assert type(flagged) is dict
00719                     assert flagged.has_key('flagged')
00720                     assert flagged.has_key('total')
00721                     if flagged['flagged'] < minabs or \
00722                        (flagged['flagged'] > maxabs and maxabs >= 0) or \
00723                        flagged['flagged'] * 1.0 / flagged['total'] < minrel or \
00724                        flagged['flagged'] * 1.0 / flagged['total'] > maxrel:
00725                         del summary_stats[x][xx]
00726                         
00727     return summary_stats
00728 
00729 def create_arg_dict(subMS_list,value):
00730     
00731     ret_dict = []
00732     for subMS in subMS_list:
00733         ret_dict.append(value)
00734         
00735     return ret_dict
00736