casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables
flagdata.py
Go to the documentation of this file.
00001 #
00002 # This file was generated using xslt from its XML file
00003 #
00004 # Copyright 2009, Associated Universities Inc., Washington DC
00005 #
00006 import sys
00007 import os
00008 from  casac import *
00009 import string
00010 from taskinit import casalog
00011 #from taskmanager import tm
00012 import task_flagdata
00013 def flagdata(vis='', mode='manual', autocorr=False, inpfile='', reason='any', spw='', field='', antenna='', uvrange='', timerange='', correlation='', scan='', intent='', array='', observation='', feed='', clipminmax=[], datacolumn='DATA', clipoutside=True, channelavg=False, clipzeros=False, quackinterval=1.0, quackmode='beg', quackincrement=False, tolerance=0.0, addantenna='', lowerlimit=0.0, upperlimit=90.0, ntime='scan', combinescans=False, timecutoff=4.0, freqcutoff=3.0, timefit='line', freqfit='poly', maxnpieces=7, flagdimension='freqtime', usewindowstats='none', halfwin=1, winsize=3, timedev='', freqdev='', timedevscale=5.0, freqdevscale=5.0, spectralmax=1E6, spectralmin=0.0, extendpols=True, growtime=50.0, growfreq=50.0, growaround=False, flagneartime=False, flagnearfreq=False, minrel=0.0, maxrel=1.0, minabs=0, maxabs=-1, spwchan=False, spwcorr=False, basecnt=False, action='apply', display='', flagbackup=True, savepars=False, cmdreason='', outfile=''):
00014 
00015         """All-purpose flagging task based on data-selections and flagging modes/algorithms.
00016 
00017             This task can flag a Measurement Set or a calibration table. It has 
00018             two main types of operation. One type will read the parameters from 
00019             the interface and flag using any of the various available modes. The 
00020             other type will read the commands from a text file or a Python list of 
00021             strings, containing a list of flag commands (each line containing data 
00022             selection parameters and any parameter specific for the mode being 
00023             requested). Please see examples at the end of this help.
00024             
00025             It is also possible to only save the parameters set in the interface 
00026             without flagging. The parameters can be saved in the FLAG_CMD sub-table 
00027             or in a text file. Note that when saving to an external file, the parameters 
00028             will be appended to the given file.
00029             
00030             The available flagging modes are: manual, clip, shadow, quack, elevation, 
00031             tfcrop, rflag, extend, unflag and summary.
00032 
00033             The current flags can be automatically backed up before applying new 
00034             flags if the parameter flagbackup is set. Previous flag versions can be 
00035             recovered using the flagmanager task.
00036             
00037             NOTE on flagging calibration tables.
00038             -----------------------------------
00039             
00040             When the input is a calibration table, the modes 'elevation' and 'shadow'
00041             will be disabled. Data selection for calibration tables is limited to field,
00042             antenna and spw at the moment. It is only possible to save the parameters 
00043             to an external file. 
00044             
00045 The task will flag a subset of data based on the following modes of operation:
00046 
00047     list        = list of flagging commands to apply to MS/cal table
00048     manual      = flagging based on specific selection parameters
00049     clip        = clip data according to values
00050     quack       = remove/keep specific time range at scan beginning/end
00051     shadow      = remove antenna-shadowed data
00052     elevation   = remove data below/above given elevations
00053     tfcrop      = automatic identification of outliers on the time-freq plane
00054     rflag       = automatic detection of outliers based on sliding-window RMS filters
00055     extend      = extend and/or grow flags beyond what the basic algorithms detect
00056     summary     = report the amount of flagged data
00057     unflag      = unflag the specified data
00058 
00059     Keyword arguments:
00060     vis -- Name of input visibility file or calibration table.
00061         default: '' (none) 
00062         example1: vis='uid___A002_X2a5c2f_X54.ms' or
00063         example2: vis='cal-X54.B1'
00064 
00065         Any flagging will only be applied to the specified selection.
00066 
00067               antenna -- Select data based on baseline
00068                     default: '' (all); example: antenna='DV04&DV06' baseline DV04-DV06
00069                     antenna='DV04&DV06;DV07&DV10' #baselines DV04-DV06 and DV07-DV10
00070                     antenna='DV06' # all cross-correlation baselines between antenna DV06 and 
00071                             all other available antennas
00072                     antenna='DV04,DV06' # all baselines with antennas DV04 and DV06
00073                     antenna='DV06&&DV06' # only the auto-correlation baselines for antenna DV06
00074                     antenna='DV04&&*' # cross and auto-correlation baselines between antenna DV04
00075                                              and all other available antennas
00076                     antenna='0~2&&&' # only the auto-correlation baselines for antennas 
00077                                                    in range 0~2
00078 
00079               spw -- Select data based on spectral window and channels
00080                     default: '' (all); example: spw='1'
00081                     spw='<2' #spectral windows less than 2
00082                     spw='>1' #spectral windows greater than 1
00083                     spw='1:0~10' # first 10 channels from spw 1
00084                     spw='1:0~5;120~128' # multiple separated channel chunks.
00085 
00086                     Note : For modes clip, tfcrop and rflag, channel-ranges can be excluded 
00087                     from flagging by selecting ranges such as spw='0:0~5;10~63'. This is a way to
00088                     protect known spectral-lines from being flagged by the autoflag algorithms.
00089 
00090               correlation -- Correlation types or expression.
00091                     default: '' (all correlations)
00092                             For modes clip, tfcrop or rflag, the default means ABS_ALL. If
00093                             the input is cal table that does not contain a complex data column,
00094                             the default will fall back to REAL_ALL.
00095                     example: correlation='XX,YY' or
00096                     options: Any of 'ABS', 'ARG', 'REAL', 'IMAG', 'NORM' followed by
00097                              any of 'ALL', 'I', 'XX', 'YY', 'RR', 'LL', 'Sol1', 'Sol2', 'WVR'
00098                              'WVR' refers to the water vapour radiometer of ALMA data.
00099                     example: correlation='REAL_XX,XY'
00100                   -->correlation selection is not supported for modes other than clip, tfcrop or
00101                       rflag in cal tables.      
00102                     
00103                     Note that the operators ABS,ARG,REAL, etc. are written only once as the first value.
00104                     if more than one correlation is given, the operator will be applied to all of them.
00105                     
00106                     The expression is used only in modes clip, tfcrop and rflag.
00107 
00108               field -- Select data based on field id(s) or name(s)
00109                     default: '' (all); example: field='1'
00110                     field='0~2' # field ids inclusive from 0 to 2
00111                     field='3C*' # all field names starting with 3C
00112 
00113               uvrange -- Select data within uvrange (default units meters)
00114                     default: '' (all); example:
00115                     uvrange='0~1000klambda'; uvrange from 0-1000 kilo-lambda
00116                     uvrange='>4klamda';uvranges greater than 4 kilo-lambda
00117                     uvrange='0~1000km'; uvrange in kilometers
00118                   -->uvrange selection is not supported for cal tables.                          
00119 
00120               timerange  -- Select data based on time range:
00121                     default = '' (all); example,
00122                     timerange = 'YYYY/MM/DD/hh:mm:ss~YYYY/MM/DD/hh:mm:ss'
00123                     Note: YYYY/MM/DD can be dropped as needed:
00124                     timerange='09:14:0~09:54:0' # this time range
00125                     timerange='09:44:00' # data within one integration of time
00126                     timerange='>10:24:00' # data after this time
00127                     timerange='09:44:00+00:13:00' #data 13 minutes after time
00128                   -->timerange selection is not supported for cal tables.      
00129 
00130               scan -- Select data based on scan number
00131                     default: '' (all); example: scan='>3'
00132                   -->scan selection is not supported for cal tables.                          
00133 
00134               intent -- Select data based on observation intent
00135                     default: '' (all); example: intent='*CAL*,*BAND*'
00136                   -->intent selection is not supported for cal tables.                          
00137 
00138               array -- Selection based on the antenna array
00139                     default: '' (all); 
00140                   -->array selection is not supported for cal tables.                          
00141 
00142               observation -- Selection based on the observation ID
00143                     default: '' (all); example: observation='1' or observation=1
00144                   -->observation selection is not supported for cal tables.                          
00145 
00146               feed -- Selection based on the feed - NOT IMPLEMENTED YET
00147 
00148 
00149     mode -- Mode of operation.
00150         options: 'list', 'manual','clip','quack','shadow','elevation', 'tfcrop', 'extend',
00151                  'unflag', 'summary'
00152         default: 'manual'
00153 
00154         list -- Flag according to the data selection and flag commands specified in the input list.
00155                 The input list may come from a text file or from a Python list of strings.
00156                 Each input line may contain data selection parameters and any parameter
00157                 specific to the mode given in the line. Default values will be used for
00158                 the parameters that are not present in the line. Each line will be taken 
00159                 as a command to the task. If data is pre-selected using any of the selection 
00160                 parameters, then flagging will apply only to that subset of the MS.
00161                   
00162                 The summary mode is not allowed in the input list.
00163                   
00164                 For optimization, the task will create a union of the data selection parameters
00165                 present in the list and select only that portion of the MS.
00166 
00167                 NOTE: the flag commands will be applied only when action='apply'. If
00168                       action='calculate' the flags will be calculated, but not applied.
00169                       This is useful if display is set to something other than 'none'. If
00170                       action='' or 'none', the flag commands will not be applied either.
00171                       An empty action is useful only to save the parameters of the list
00172                       to a file or to the FLAG_CMD sub-table.
00173                   
00174                inpfile -- Input ASCII file or a Python list of command strings.
00175                   default: ''
00176                   options: [] or '' with filename.
00177                   Example1: the following commands can be saved into a file and given
00178                             to the task (e.g. save it to flags.txt).
00179                   
00180                      scan='1~3' mode='manual'
00181                      mode='clip' clipminmax=[0,2] correlation='ABS_XX' clipoutside=False
00182                      spw='9' mode='tfcrop' correlation='ABS_YY' ntime=51.0
00183                      mode='extend' extendpols=True
00184 
00185                     flagdata(vis,mode='list',inpfile='flags.txt')
00186 
00187                   Example2: the same commands can be given in a Python list on the command line
00188                              to the task.
00189                      cmd=["scan='1~3' mode='manual'", 
00190                           "mode='clip' clipminmax=[0,2] correlation='ABS_XX' clipoutside=False",
00191                           "spw='9' mode='tfcrop' correlation='ABS_YY' ntime=51.0",
00192                           "mode='extend' extendpols=True"]
00193 
00194                      flagdata(vis,mode='list',inpfile=cmd)                    
00195         
00196                reason -- select flag commands based on REASON(s) 
00197                    default: 'any' (all flags regardless of reason)
00198                             can be a string, or list of strings
00199                    example: reason='FOCUS_ERROR'
00200                             reason=['FOCUS_ERROR','SUBREFLECTOR_ERROR']
00201     
00202                    NOTE: what is within the string is literally
00203                    matched, e.g. reason='' matches only blank reasons, 
00204                    and reason = 'FOCUS_ERROR,SUBREFLECTOR_ERROR'
00205                    matches this compound reason string only
00206                      
00207                   See the syntax for writing flag commands at the end of this help.                  
00208 
00209 
00210         manual -- Flag according to the data selection specified.
00211                   This is the default mode (used when the mode is not specified).
00212                   
00213                 autocorr -- Flag only the auto-correlations. Note that this parameter is only
00214                             active when set to True. If set to False it does NOT mean "do not
00215                             flag auto-correlations".
00216                     default: False
00217                     options: True,False
00218                   
00219         clip -- Clip data according to values of the following subparameters. The polarization
00220                 expression is given by the correlation parameter.
00221                     
00222                channelavg -- Average data over (selected) channels? Channel
00223                             selections are taken into account. Flagged channels
00224                             are excluded from the average. The average is done
00225                             after applying the specified clipexpr, i.e. scalar
00226                             averaging.
00227                     default: False
00228                     options: True,False
00229                     
00230                clipminmax -- Range of data (Jy) that will NOT be flagged
00231                     default: []; it will flag only NaN and Infs.
00232                     example: [0.0,1.5]
00233                     It will always flag the NaN/Inf data, even when a range is specified.
00234                     
00235                datacolumn -- Column to use for clipping.
00236                     default: 'DATA'
00237                     options: 'DATA', 'CORRECTED','MODEL', 'RESIDUAL, 'RESIDUAL_DATA',
00238                              'FPARAM', 'CPARAM', 'SNR'.
00239                              
00240                     NOTE: by default for calibration tables, the framework will try to use FPARAM; 
00241                           if that does not exist, it will try to use CPARAM. If none of them exist, it
00242                           will give an error and exit.
00243                     
00244                clipoutside -- Clip OUTSIDE the range?
00245                     default: True
00246                     example: False; flag data WITHIN the range.
00247 
00248                clipzeros -- Clip zero-value data.
00249                     default: False
00250 
00251         quack -- Option to remove specified part of scan beginning/end
00252 
00253                quackinterval -- Time in seconds from scan beginning/end to flag. Make time slightly 
00254                                  smaller than the desired time.
00255                     default: 0.0
00256                quackmode -- Quack mode
00257                     default: 'beg'
00258                     options: 'beg'  ==> beginning of scan
00259                              'endb' ==> end of scan. 
00260                              'tail' ==> all but beginning of scan
00261                              'end'  ==> all but end of scan.
00262                quackincrement -- Quack incrementally in time?
00263                     default: False
00264                              False  ==> the quack interval is counted from the
00265                                         beginning of the scan
00266                              True   ==> the quack interval is counted from the
00267                                         first unflagged data in the scan
00268 
00269         shadow -- Option to flag data of shadowed antennas. This mode is not available
00270                     for cal tables.
00271                     
00272                    All antennas in the antenna-subtable of the MS (and the corresponding
00273                    diameters) will be considered for shadow-flag calculations. 
00274                    For a given timestep, an antenna is flagged if any of its baselines 
00275                    (projected onto the uv-plane) is shorter than  radius_1 + radius_2 - tolerance. 
00276                    The value of 'w' is used to determine which antenna is behind the other.
00277                    The phase-reference center is used for antenna-pointing direction. 
00278                      
00279                tolerance -- Amount of shadowing allowed (or tolerated), in meters. 
00280                         A positive number allows antennas to overlap in projection
00281                         A negative number forces antennas apart in projection
00282                         Zero implies a distance of radius_1+radius_2 between antenna centers.
00283                    default: 0.0
00284 
00285                addantenna -- It can be either a file name with additional antenna names, positions 
00286                          and diameters, or a Python dictionary with the same information. 
00287                          You can use the flaghelper functions to create the dictionary from a file.  
00288                    default: ''
00289                    
00290                    To create a dictionary inside casapy.
00291                    > import flaghelper as fh
00292                    > antdic = fh.readAntennaList(antfile)
00293                    
00294                    Where antfile is a text file in disk that contains information such as:
00295                     name=VLA01
00296                     diameter=25.0
00297                     position=[-1601144.96146691, -5041998.01971858, 3554864.76811967]
00298                     name=VLA02
00299                     diameter=25.0
00300                     position=[-1601105.7664601889, -5042022.3917835914, 3554847.245159178]
00301                   
00302 
00303         elevation -- Option to flag based on antenna elevation. This mode is not available
00304                       for cal tables.
00305                  
00306                lowerlimit -- Lower limiting elevation in degrees. Data coming from a baseline
00307                              where one or both antennas were pointing at a strictly lower elevation
00308                              (as function of time), will be flagged.
00309                    default: 0.0
00310                    
00311                upperlimit -- Upper limiting elevation in degrees. Data coming from a baseline
00312                              where one or both antennas were pointing at a strictly higher elevation
00313                              (as function of time), will be flagged.
00314                    default: 90.0
00315 
00316         tfcrop -- Flag using the TFCrop autoflag algorithm. The polarization
00317                   expression is given by the correlation parameter.
00318 
00319                  For each field, spw, timerange (specified by ntime), and baseline, 
00320                  (1) Average visibility amplitudes along time dimension to 
00321                        form an average spectrum
00322                  (2) Calculate a robust piece-wise polynomial fit for the band-shape 
00323                       at the base of RFI spikes. Calculate 'stddev' of (data - fit).
00324                  (3) Flag points deviating from the fit by more than N-stddev
00325                  (4) Repeat (1-3) along the other dimension.
00326 
00327                  This algorithm is designed to operate on un-calibrated data (step (2)), 
00328                  as well as calibrated data.  It is usually helpful to extend the flags along
00329                  time, frequency, and correlation in a second step (via mode='extend)
00330 
00331                ntime -- Timerange (in seconds or minutes) over which to buffer data before 
00332                         running the algorithm. 
00333                     options: 'scan' or any other float value or string containing the units.
00334                     default: 'scan'
00335                     example: '1.5min'
00336                            : 1.2 (taken in seconds)
00337                         The dataset will be iterated through in time-chunks defined here.
00338                 
00339                combinescans -- accumulate data across scans.
00340                     default: False
00341                         This parameter is relevant only ntime is specified as a time-interval (not 'scan')
00342 
00343                datacolumn -- Column to use for flagging.
00344                     default: 'DATA'
00345                     options: 'DATA', 'CORRECTED','MODEL', 'RESIDUAL, 'RESIDUAL_DATA',
00346                              'FPARAM', 'CPARAM', 'SNR'.
00347                              
00348                     NOTE: by default for calibration tables, the framework will try to use FPARAM; 
00349                           if that does not exist, it will try to use CPARAM. If none of them exist, it
00350                           will give an error and exit.
00351                     
00352 
00353                timecutoff -- Flag threshold in time. Flag all data-points further than N-stddev 
00354                              from the fit. This threshold catches time-varying RFI spikes (
00355                              narrow and broad-band), but will not catch RFI that is persistent in time.
00356                     default: 4.0
00357                           Flagging is done in upto 5 iterations. The stddev calculation is adaptive and 
00358                           converges to a value that reflects only the data and no RFI. At each iteration,
00359                           the same relative threshold is applied to detect flags. (Step (3) of the algorithm).
00360 
00361                freqcutoff -- Flag threshold in frequency. Flag all data-points further than N-stddev 
00362                              from the fit.
00363                     default: 3.0
00364                         Same as timecutoff, but along the frequency-dimension. This threshold catches
00365                         narrow-band RFI that may or may not be persistent in time. 
00366 
00367                timefit --  Fitting function for the time direction
00368                     default: 'line'
00369                     options: 'line', 'poly'
00370                       A 'line' fit is a robust straight-line fit across the entire timerange (defined 
00371                       by 'ntime').
00372                       A 'poly' fit is a robust piece-wise polynomial fit across the timerange.
00373                       Note: A robust fit is computed in upto 5 iterations. At each iteration, the stddev
00374                             between the data and the fit is computed, values beyond N-stddev are flagged,
00375                             and the fit and stddev are re-calculated with the remaining points.
00376                             This stddev calculation is adaptive, and converges to a value that reflects 
00377                             only the data and no RFI.  It also provides a varying set of flagging thresholds,
00378                             that allows deep flagging only when the fit best represents the true data.
00379                       Choose 'poly' only if the visibilities are expected to vary significantly over the
00380                       timerange selected by 'ntime', or if there is a lot of strong but intermittent RFI.
00381 
00382                freqfit -- Fitting function for the frequency direction
00383                     default: 'poly'
00384                     options: 'line','poly'
00385                         Same as for the 'timefit' parameter.
00386                         Choose 'line' only if you are operating on bandpass-corrected data, or residuals, 
00387                         and expect that the bandshape is linear. The 'poly' option works better on 
00388                         uncalibrated bandpasses with narrow-band RFI spikes.   
00389 
00390                maxnpieces -- Maxinum number of pieces to allow in the piecewise-polynomial fits 
00391                     default: 7
00392                     options: 1 - 9
00393                         This parameter is used only if 'timefit' or 'freqfit' are chosen as 'poly'.
00394                         If there is significant broad-band RFI, reduce this number. Using too many
00395                         pieces could result in the RFI being fitted in the 'clean' bandpass.    
00396                         In later stages of the fit, a third-order polynomial is fit per piece, so 
00397                         for best results, please ensure that nchan/maxnpieces is at-least 10.
00398 
00399                flagdimension -- Choose the directions along which to perform flagging
00400                     default: 'freqtime'; first flag along frequency, and then along time 
00401                     options: 'time', 'freq', 'timefreq', 'freqtime'
00402                          For most cases, 'freqtime' or 'timefreq' are appropriate, and differences
00403                          between these choices are apparant only if RFI in one dimension is 
00404                          significantly stronger than the other. The goal is to flag the dominant RFI first.
00405                          If there are very few (less than 5) channels of data, then choose 'time'.
00406                          Similarly for 'freq'.
00407 
00408                usewindowstats -- Use sliding-window statistics to find additional flags.
00409                     default: 'none' 
00410                     options: 'none', 'sum', 'std', 'both'   
00411                          Note: This is experimental !
00412                            The 'sum' option chooses to flag a point, if the mean-value in a 
00413                            window centered on that point deviates from the fit by more than
00414                            N-stddev/2.0. 
00415                          Note: stddev is calculated between the data and fit as explained in Step (2).
00416                             This option is an attempt to catch broad-band or 
00417                             time-persistent RFI  that the above polynomial fits will mistakenly fit
00418                             as the clean band. It is an approximation to the sumThreshold
00419                             method found to be effective by Offringa et.al (2010) for LOFAR data. 
00420                             The 'std' option chooses to flag a point, if the 'local' stddev calculated
00421                             in a window centered on that point is larger than N-stddev/2.0. 
00422                             This option is an attempt to catch noisy RFI that is not excluded in the
00423                             polynomial fits, and which increases the global stddev, and results in 
00424                             fewer flags (based on the N-stddev threshold).
00425                                                                 
00426                 halfwin -- Half width of sliding window to use with 'usewindowstats'
00427                     default: 1  (a 3-point window size)
00428                     options: 1,2,3
00429                         Note: This is experimental !
00430 
00431         rflag -- Detect outliers based on the RFLAG algorithm (ref. E.Greisen, AIPS, 2011).
00432                  The polarization expression is given by the correlation parameter.
00433 
00434                  Iterate through the data in chunks of time.  For each chunk, calculate local
00435                  statistics, and apply flags based on user supplied (or auto-calculated) thresholds.
00436 
00437                  Step 1 : Time analysis (for each channel)
00438                    -- calculate local rms of real and imag visibilities, within a sliding time window
00439                    -- calculate the median rms across time windows, deviations of local rms from
00440                       this median, and the median deviation 
00441                    -- flag if local rms is larger than timedevscale x (medianRMS + medianDev)
00442 
00443                  Step 2 : Spectral analysis (for each time)
00444                    -- calculate avg of real and imag visibilities and their rms across channels
00445                    -- calculate the deviation of each channel from this avg, and the median-deviation
00446                    -- flag if deviation is larger than freqdevscale x medianDev
00447 
00448                  Example usage : 
00449 
00450                   (1) Calculate thresholds automatically per scan, and use them to find flags.
00451                       Specify scale-factor for time-analysis thresholds, use default for frequency.
00452 
00453                       flagdata('my.ms', mode='rflag',spw='9',timedevscale=4.0)
00454  
00455                   (2) Supply noise-estimates to be used with default scale-factors. 
00456   
00457                       flagdata(vis='my.ms', mode='rflag', spw='9', timedev=0.1, freqdev=0.5, action='calculate)
00458 
00459                   (3) Two-passes. This replicates the usage pattern in AIPS.
00460 
00461                       -- The first pass saves commands in an output text files, with auto-calculated 
00462                          thresholds. Thresholds are returned from rflag only when action='calculate'. 
00463                          The user can edit this file before doing the second pass, 
00464                          but the python-dictionary structure must be preserved.
00465 
00466                       -- The second pass applies these commands (action='apply').
00467 
00468                           flagdata(vis='my.ms', mode='rflag', spw='9,10', timedev='tdevfile.txt', 
00469                                     freqdev='fdevfile.txt', action='calculate')
00470                           flagdata(vis='my.ms', mode='rflag', spw='9,10', timedev='tdevfile.txt', 
00471                                     freqdev='fdevfile.txt', action='apply')
00472 
00473                         With action='calculate', display='report' will produce diagnostic plots 
00474                         showing data-statistics and thresholds (the same thresholds as those
00475                         written out to 'tdevfile.txt' and 'fdevfile.txt'). 
00476            
00477                         Note : The rflag algorithm was originally developed by Eric Greisen in 
00478                                 AIPS (31DEC11).
00479                         AIPS documentation : Section E.5 of the AIPS cookbook
00480                         (Appendix E : Special Considerations for EVLA data calibration and imaging in AIPS,
00481                         http://www.aips.nrao.edu/cook.html#CEE )
00482 
00483                         Note : Since this algorithm operates with two passes through each
00484                            chunk of data (time and freq axes), some data points get flagged
00485                            twice. This can affect the flag-percentage estimate printed in the
00486                            logger at runtime.  An accurate estimate can be obtained via the
00487                            summary mode.
00488 
00489                ntime -- Timerange (in seconds or minutes) over which to buffer data before running the algorithm.
00490                      options: 'scan' or any other float value or string containing the units.
00491                      default: 'scan'
00492                      example: '1.5min'
00493                             : 1.2 (taken in seconds)
00494                         The dataset will be iterated through in time-chunks defined here.
00495                 
00496                combinescans -- accumulate data across scans.
00497                       default: False
00498                           It only makes sense when ntime not equal 'scan'.                
00499 
00500                datacolumn -- Column to use for flagging.
00501                     default: 'DATA'
00502                     options: 'DATA', 'CORRECTED','MODEL', 'RESIDUAL, 'RESIDUAL_DATA',
00503                              'FPARAM', 'CPARAM', 'SNR'.
00504                              
00505                     NOTE: by default for calibration tables, the framework will try to use FPARAM; 
00506                           if that does not exist, it will try to use CPARAM. If none of them exist, it
00507                           will give an error and exit.
00508                     
00509 
00510                winsize -- number of timesteps in the sliding time window ( fparm(1) in AIPS ). 
00511                     default: 3
00512                     
00513                timedev -- time-series noise estimate ( noise in AIPS ). 
00514                     default: []
00515                     Examples : 
00516                          timedev = 0.5 : Use this noise-estimate to calculate flags. Do not recalculate.
00517                          timedev = [ [1,9,0.2], [1,10,0.5] ] :  Use noise-estimate of 0.2 for field 1, 
00518                                    spw 9, and noise-estimate of 0.5 for field 1, spw 10.
00519                          timedev = [] : Auto-calculate noise estimates.
00520                 
00521                     
00522                freqdev -- spectral noise estimate ( scutoff in AIPS ).
00523                           This step depends on having a relatively-flat bandshape.
00524                           Same parameter-options as 'timedev'. 
00525                     default: []
00526                      
00527 
00528                timedevscale -- For Step 1 (time analysis), flag a point if local rms around it 
00529                                is larger than  'timedevscale' x 'timedev'   ( fparm(0) in AIPS )
00530                     default: 5.0
00531                     
00532                freqdevscale -- For Step 2 (spectral analysis), flag a point if local rms around it
00533                                is larger than 'freqdevscale' x 'freqdev'     ( fparm(10) in AIPS )
00534                     default: 5.0
00535 
00536                spectralmax -- Flag whole spectrum if 'freqdev' is greater than spectralmax ( fparm(6) in AIPS )
00537                     default: 1E6
00538                     
00539                spectralmin -- Flag whole spectrum if 'freqdev' is less than spectralmin ( fparm(5) in AIPS )
00540                     default: 0.0
00541 
00542 
00543 
00544         extend -- Extend and/or grow flags beyond what the basic algorithms detect.
00545                         This mode will extend the accumulated flags available in the MS,
00546                         regardless of which algorithm created them.
00547                          
00548                         Extensions will apply only within the selected data, according to the settings 
00549                         of extendpols,growtime,growfreq,growaround, flagneartime,flagnearfreq.
00550                         
00551                         Note : Runtime summary counts in the logger can sometimes report larger
00552                             flag percentages than what is actually flagged. This is because 
00553                             extensions onto already-flagged data-points are counted as new flags. 
00554                             An accurate flag count can be obtained via the summary mode.
00555 
00556                ntime -- Timerange (in seconds or minutes) over which to buffer data before running 
00557                         the algorithm.
00558                      options: 'scan' or any other float value or string containing the units.
00559                      default: 'scan'
00560                      example: '1.5min'
00561                             : 1.2 (taken in seconds)
00562                         The dataset will be iterated through in time-chunks defined here.
00563                 
00564                combinescans -- accumulate data across scans.
00565                       default: False
00566                           It only makes sense when ntime not equal 'scan'.                
00567 
00568                extendpols -- Extend flags to all selected correlations
00569                       default: True
00570                       options: True/False
00571                          For example, to extend flags from RR to only RL and LR, a data-selection
00572                          of correlation='RR,LR,RL' is required along with extendpols=True. 
00573 
00574                growtime -- For any channel, flag the entire timerange in the current 2D chunk (
00575                            set by 'ntime') if more than X% of the timerange is already flagged.
00576                       default: 50.0
00577                       options: 0.0 - 100.0
00578                          This option catches the low-intensity parts of time-persistent RFI.
00579 
00580                growfreq -- For any timestep, flag all channels in the current 2D chunk (set by 
00581                            data-selection) if more than X% of the channels are already flagged.
00582                       default: 50.0
00583                       options: 0.0 - 100.0
00584                          This option catches broad-band RFI that is partially identified by earlier steps.
00585 
00586                growaround -- Flag a point based on the number of flagged points around it.
00587                       default: False
00588                       options: True/False
00589                             For every un-flagged point on the 2D time/freq plane, if more than four
00590                             surrounding points are already flagged, flag that point. 
00591                             This option catches some wings of strong RFI spikes.
00592 
00593                flagneartime -- Flag points before and after every flagged one, in the time-direction.
00594                       default: False
00595                       options: True/False
00596                       Note: This can result in excessive flagging.
00597 
00598                flagnearfreq -- Flag points before and after every flagged one, in the frequency-direction
00599                     default: False
00600                     options: True/False
00601                     This option allows flagging of wings in the spectral response of strong RFI.
00602                     Note: This can result in excessive flagging.
00603 
00604 
00605         unflag -- Unflag according to the data selection specified.
00606 
00607         summary -- List the number of rows and data points flagged.
00608                   
00609                minrel -- Minimum number of flags (relative) to include in histogram
00610                     default: 0.0
00611 
00612                maxrel -- Maximum number of flags (relative) to include in histogram
00613                     default: 1.0
00614 
00615                minabs -- Minimum number of flags (absolute, inclusive) to include in histogram
00616                     default: 0
00617 
00618                maxabs -- Maximum number of flags (absolute, inclusive) to include in histogram
00619                           To indicate infinity, use any negative number.
00620                     default: -1
00621 
00622                spwchan -- list the number of flags per spw and per channel.
00623                     default: False
00624 
00625                spwcorr -- list the number of flags per spw and per correlation.
00626                     default: False
00627 
00628                basecnt -- list the number of flags per baseline
00629                     default: False
00630 
00631                     In summary mode, the task returns a dictionary of flagging statistics.
00632                     Example:
00633 
00634                         s = flagdata(..., mode='summary')
00635 
00636                      Then s will be a dictionary which contains
00637                     s['total']   : total number of data
00638                     s['flagged'] : amount of flagged data
00639                     
00640         
00641 
00642     action -- Action to perform in MS/cal table or in the input list of parameters. 
00643         options: 'none', 'apply','calculate'
00644         default: 'apply'
00645 
00646         'apply' -- Apply the flags to the MS.
00647         
00648                display -- Display data and/or end-of-MS reports at run-time.        
00649                    default: 'none'
00650                    options: 'none', 'data', 'report', 'both'
00651 
00652                      'none' --> It will not display anything.
00653 
00654                      'data' --> display data and flags per-chunk at run-time, within an interactive GUI.
00655 
00656                       This option opens a GUI to show the 2D time-freq planes of
00657                       the data with old and new flags, for all correlations per baseline.
00658                       -- The GUI allows stepping through all baselines (prev/next) in
00659                       the current chunk (set by 'ntime'), and stepping to the next-chunk.
00660                       -- The 'flagdata' task can be quit from the GUI, in case it becomes
00661                       obvious that the current set of parameters is just wrong.
00662                       -- There is an option to stop the display but continue flagging.
00663 
00664                     'report' --> displays end-of-MS reports on the screen.
00665 
00666                     'both' --> displays data per chunk and end-of-MS reports on the screen
00667 
00668                flagbackup -- Automatically backup flags before running the tool.
00669                             Flagversion names are chosen automatically, and are based on the
00670                             mode being used.
00671                    default: True
00672                    options: True/False                    
00673         
00674         'calculate' -- Only calculate the flags but do not write them to the MS. This is
00675                      useful if used together with the display to analyse the results before
00676                      writing to the MS.
00677 
00678                display -- Display data and/or end-of-MS reports at run-time. See extended description
00679                           above. 
00680                    default: 'none'
00681                    options: 'none', 'data', 'report', 'both'
00682                    
00683         ' ' -- When set to empty, the underlying tool will not be executed and no flags
00684                 will be produced. No data selection will be done either. This is useful
00685                 when used together with the parameter savepars to only save the current
00686                 parameters (or list of parameters) to the FLAG_CMD sub-table or to an
00687                 external file.  
00688                  
00689 
00690     savepars -- Save the current parameters to the FLAG_CMD table of the MS or to an output text file.
00691                 Note that when display is set to anything other than 'none', savepars
00692                 will be disabled. This is done because in an interactive mode, the user
00693                 may skip data which may invalidate the initial input parameters and there
00694                 is no way to save the interactive commands.
00695                 default: False
00696                 options: True/False
00697 
00698                 cmdreason -- A string containing a reason to save to the FLAG_CMD table or to an 
00699                              output text file given by the outfile sub-parameter. If the input 
00700                              contains any reason, they will be replaced with this one. At the 
00701                              moment it is not possible to add more than one reason.
00702                     default: ' '; no reason will be added to output.
00703                     example: cmdreason='CLIP_ZEROS'
00704                 
00705                 outfile -- Name of output file to save the current parameters.
00706                     default: ' '; it will save the parameters to the FLAG_CMD table of the MS.
00707                     example: outfile='flags.txt' will save the parameters in a text file.
00708                     
00709     
00710 ---- EXAMPLES ----
00711 
00712     NOTE: The vector mode of the previous flagdata task can be achieved with this task by using it
00713           with mode='list' and the commands given in a list in inpmode=[]. Example:
00714     
00715        flagdata('my.ms', inpmode='list', inpfile=["mode='clip' clipzeros=True","mode='shadow'])
00716         
00717 
00718     1) Manually flag scans 1~3 and save the parameters to the FLAG_CMD sub-table.
00719     
00720         flagdata('my.ms', scan='1~3, mode='manual', savepars=True)
00721             
00722     2) Save the parameters to a file that is open in append mode.
00723 
00724         flagdata('my.ms', scan='1~3, mode='manual', savepars=True, outfile='flags.txt')
00725             
00726     3a) Flag all the commands given in the Python list of strings.
00727     
00728         cmd = ["scan='1~3' mode='manual'",
00729                "spw='9' mode='tfcrop' correlation='ABS_RR,LL' ntime=51.0",
00730                "mode='extend' extendpols=True"]
00731     
00732         flagdata('my.ms', mode='list', inpfile=cmd)
00733 
00734     3b) Flag all the commands given in the file called flags.txt.
00735     
00736         > cat flags.txt
00737         scan='1~3' mode='manual'
00738         spw='9' mode='tfcrop' correlation='ABS_RR,LL' ntime=51.0
00739         mode='extend' extendpols=True
00740     
00741         flagdata('my.ms', mode='list', inpfile='flags.txt')
00742 
00743     4) Display the data and flags per-chunk and do not write flags to the MS.
00744     
00745         flagdata('my.ms', mode='list', inpfile='flags.txt', action='calculate', display='data')
00746         
00747     5) Flag all the antennas except antenna=5.
00748     
00749         flagdata(vis='my.ms', antenna='!5', mode='manual)
00750             
00751     6) Clip the NaN in the data. An empty clipminmax will flag only NaN.
00752     
00753         flagdata('my.ms', mode='clip')
00754         
00755     7) Clip only the water vapour radiometer data.
00756     
00757         flagdata('my.ms',mode='clip',clipminmax=[0,50], correlation='ABS_WVR')
00758 
00759     8) Clip only zero-value data.
00760 
00761         flagdata('my.ms',mode='clip',clipzeros=True)
00762         
00763     9a) Flag only auto-correlations using the autocorr parameter.
00764        
00765         flagdata('my.ms', autocorr=True)
00766             
00767     9b) Flag only auto-correlations using the antenna selection.
00768        
00769         flagdata('my.ms', mode='manual', antenna='*&&&')
00770 
00771     10a) Flag based on selected reasons from a file.
00772        
00773         > cat flags.txt
00774         scan='1~3' mode='manual' reason='MYREASON'
00775         spw='9' mode='clip' clipzeros=True reason='CLIPZEROS'
00776         mode='manual' scan='4' reason='MYREASON'
00777         
00778         flagdata('my.ms', mode='list', inpfile='flags.txt', reason='MYREASON')
00779 
00780     10b) The same result of 10a can be achieved using the task flagcmd.
00781 
00782         flagcmd('my.ms', inpmode='file', inpfile='flags.txt', action='apply', reason='MYREASON')
00783             
00784     11) Automatic flagging using 'rflag', using auto-thresholds, and specifying 
00785           a threshold scale-factor to use for flagging.
00786 
00787         flagdata('my.ms', mode='rflag',spw='9',timedevscale=4.0,action='apply')
00788 
00789     12) Save the interface parameters to the FLAG_CMD sub-table of the MS. Add a reason
00790         to the flag command. This cmdreason will be added to the REASON column of the
00791         FLAG_CMD sub-table. Apply flags in flagcmd.
00792 
00793         flagdata('my.ms', mode='clip',channelavg=False, clipminmax=[30., 60.], spw='0:0~10', 
00794                   correlation='ABS_XX,XY', action='', savepars=True, cmdreason='CLIPXX_XY')
00795 
00796         > Select based on the reason.
00797         flagcmd('my.ms', action='apply', reason='CLIPXX_XY')
00798 
00799     13) Flag antennas that are shadowed by antennas not present in the MS.
00800 
00801         > Create a text file with information about the antennas.
00802         > cat ant.txt
00803           name=VLA01
00804           diameter=25.0
00805           position=[-1601144.96146691, -5041998.01971858, 3554864.76811967]
00806           name=VLA02
00807           diameter=25.0
00808           position=[-1601105.7664601889, -5042022.3917835914, 3554847.245159178]
00809           name=VLA09
00810           diameter=25.0
00811           position=[-1601197.2182404203, -5041974.3604805721, 3554875.1995636248]
00812           name=VLA10
00813           diameter=25.0
00814           position=[-1601227.3367843349,-5041975.7011900628,3554859.1642644769]    
00815            
00816 
00817          flagdata('my.vis', mode='shadow', tolerance=10.0, addantenna='ant.txt')
00818 
00819         The antenna information can also be given as a Python dictionary. To create the
00820         dictionary using the flaghelper functions, do the following inside casapy:
00821 
00822              > import flaghelper as fh
00823              > antdic = fh.readAntennaList(antfile)
00824 
00825          flagdata('my.vis', mode='shadow', tolerance=10.0, addantenna=antdic)
00826 
00827     14) Apply the online flags that come from importasdm.
00828 
00829         > In importasdm, save the online flags to a file.
00830          importasdm('myasdm', 'asdm.ms', process_flags=True, savecmds=True, outfile='online_flags.txt')
00831 
00832         > You can edit the online_flags.txt to add other flagging commands or apply it directly.
00833          flagdata('asdm.ms', mode='list', inpfile='online_flags.txt')
00834 
00835         > The same result can be achieved using the task flagcmd.
00836          flagcmd('asdm.ms', inpmode='file', inpfile='online_flags.txt', action='apply')
00837 
00838     15) Clip zero data from a calibration table.
00839     
00840         flagdata('cal-X54.B1', mode='clip', clipzeros=True, datacolumn='CPARAM')
00841         
00842     16) Clip data with SNR <4.0.
00843     
00844         flagdata('cal-X54.B1', mode='clip', clipminmax=[0.0,4.0], clipoutside=False, datacolumn='SNR')
00845     
00846 
00847 ---- SYNTAX FOR COMMANDS GIVEN IN A FILE or LIST OF STRINGS ----
00848 
00849     Basic Syntax Rules
00850         
00851           Commands are strings (which may contain internal "strings") consisting of
00852           KEY=VALUE pairs separated by whitespace. 
00853 
00854           NOTE: There should be no whitespace between KEY=VALUE or within each KEY or 
00855           VALUE, since the simple parser first breaks command lines on whitespace, 
00856           then on "=". 
00857 
00858           Use only white spaces to separate the parameters (no commas).
00859         
00860           Each key should only appear once on a given command line/string.
00861         
00862           There is an implicit "mode" for each command, with the default
00863           being 'manual' if not given.
00864 
00865           Comment lines can start with '#' and will be ignored.
00866 
00867           Example:
00868         
00869           scan='1~3' mode='manual'
00870           # this line will ignored
00871           spw='9' mode='tfcrop' correlation='ABS_XX,YY' ntime=51.0
00872           mode='extend' extendpols=True
00873           scan='1~3,10~12' mode='quack' quackinterval=1.0
00874              
00875 
00876         """
00877 
00878 #
00879 #    The following is work around to avoid a bug with current python translation
00880 #
00881         mytmp = {}
00882 
00883         mytmp['vis'] = vis
00884         mytmp['mode'] = mode
00885         mytmp['autocorr'] = autocorr
00886         mytmp['inpfile'] = inpfile
00887         mytmp['reason'] = reason
00888         mytmp['spw'] = spw
00889         mytmp['field'] = field
00890         mytmp['antenna'] = antenna
00891         mytmp['uvrange'] = uvrange
00892         mytmp['timerange'] = timerange
00893         mytmp['correlation'] = correlation
00894         mytmp['scan'] = scan
00895         mytmp['intent'] = intent
00896         mytmp['array'] = array
00897         mytmp['observation'] = observation
00898         mytmp['feed'] = feed
00899         mytmp['clipminmax'] = clipminmax
00900         mytmp['datacolumn'] = datacolumn
00901         mytmp['clipoutside'] = clipoutside
00902         mytmp['channelavg'] = channelavg
00903         mytmp['clipzeros'] = clipzeros
00904         mytmp['quackinterval'] = quackinterval
00905         mytmp['quackmode'] = quackmode
00906         mytmp['quackincrement'] = quackincrement
00907         mytmp['tolerance'] = tolerance
00908         mytmp['addantenna'] = addantenna
00909         mytmp['lowerlimit'] = lowerlimit
00910         mytmp['upperlimit'] = upperlimit
00911         mytmp['ntime'] = ntime
00912         mytmp['combinescans'] = combinescans
00913         mytmp['timecutoff'] = timecutoff
00914         mytmp['freqcutoff'] = freqcutoff
00915         mytmp['timefit'] = timefit
00916         mytmp['freqfit'] = freqfit
00917         mytmp['maxnpieces'] = maxnpieces
00918         mytmp['flagdimension'] = flagdimension
00919         mytmp['usewindowstats'] = usewindowstats
00920         mytmp['halfwin'] = halfwin
00921         mytmp['winsize'] = winsize
00922         mytmp['timedev'] = timedev
00923         mytmp['freqdev'] = freqdev
00924         mytmp['timedevscale'] = timedevscale
00925         mytmp['freqdevscale'] = freqdevscale
00926         mytmp['spectralmax'] = spectralmax
00927         mytmp['spectralmin'] = spectralmin
00928         mytmp['extendpols'] = extendpols
00929         mytmp['growtime'] = growtime
00930         mytmp['growfreq'] = growfreq
00931         mytmp['growaround'] = growaround
00932         mytmp['flagneartime'] = flagneartime
00933         mytmp['flagnearfreq'] = flagnearfreq
00934         mytmp['minrel'] = minrel
00935         mytmp['maxrel'] = maxrel
00936         mytmp['minabs'] = minabs
00937         mytmp['maxabs'] = maxabs
00938         mytmp['spwchan'] = spwchan
00939         mytmp['spwcorr'] = spwcorr
00940         mytmp['basecnt'] = basecnt
00941         mytmp['action'] = action
00942         mytmp['display'] = display
00943         mytmp['flagbackup'] = flagbackup
00944         mytmp['savepars'] = savepars
00945         mytmp['cmdreason'] = cmdreason
00946         mytmp['outfile'] = outfile
00947         pathname='file:///'+os.environ.get('CASAPATH').split()[0]+'/share/xml/'
00948         trec = casac.utils().torecord(pathname+'flagdata.xml')
00949 
00950         casalog.origin('flagdata')
00951         if trec.has_key('flagdata') and casac.utils().verify(mytmp, trec['flagdata']) :
00952             result = task_flagdata.flagdata(vis, mode, autocorr, inpfile, reason, spw, field, antenna, uvrange, timerange, correlation, scan, intent, array, observation, feed, clipminmax, datacolumn, clipoutside, channelavg, clipzeros, quackinterval, quackmode, quackincrement, tolerance, addantenna, lowerlimit, upperlimit, ntime, combinescans, timecutoff, freqcutoff, timefit, freqfit, maxnpieces, flagdimension, usewindowstats, halfwin, winsize, timedev, freqdev, timedevscale, freqdevscale, spectralmax, spectralmin, extendpols, growtime, growfreq, growaround, flagneartime, flagnearfreq, minrel, maxrel, minabs, maxabs, spwchan, spwcorr, basecnt, action, display, flagbackup, savepars, cmdreason, outfile)
00953 
00954         else :
00955           result = False
00956         return result