casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
LightFlagger.h
Go to the documentation of this file.
00001 //# LightFlagger.h: A lighter flagger - for autoflag
00002 //# Copyright (C) 2000,2001
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Jan 28 2011 rurvashi Id$
00027 #ifndef FLAGGING_LIGHTFLAGGER_H
00028 #define FLAGGING_LIGHTFLAGGER_H
00029 
00030 #include <flagging/Flagging/FlagVersion.h>
00031 
00032 #include <flagging/Flagging/LFBase.h>
00033 
00034 #include <ms/MeasurementSets/MeasurementSet.h>
00035 #include <ms/MeasurementSets/MSSelection.h>
00036 #include <casa/Logging/LogIO.h>
00037 #include <casa/Arrays/Vector.h>
00038 #include <casa/Containers/Record.h>
00039 #include <casa/Quanta/Quantum.h>
00040 #include <synthesis/MSVis/VisSet.h>
00041 #include <scimath/Functionals/Polynomial.h>
00042 #include <scimath/Fitting.h>
00043 #include <scimath/Fitting/LinearFit.h>
00044 #include <scimath/Fitting/GenericL2Fit.h>
00045 
00046 namespace casa { //# NAMESPACE CASA - BEGIN
00047 
00048 // <summary>
00049 // LightFlagger: Tool for manual and automatic flagging
00050 // </summary>
00051 
00052 // <use visibility=global>
00053 // <reviewed reviewer="" date="" tests="" demos="">
00054 // </reviewed>
00055 
00056 // <prerequisite>
00057 //   <li> Flagger/LFBase
00058 //   <li> MS/MSSelection
00059 //   <li> casadbus/PlotServerProxy and FlagPlotServerProxy
00060 // </prerequisite>
00061 //
00062 // <etymology>
00063 // A lighter version of the existing flagger. 
00064 // This should eventually replace the Flagger.
00065 // </etymology>
00066 //
00067 // <synopsis>
00068 // LightFlagger performs manual and automated flagging operations on a measurement set.
00069 //  -- Attaches to an MS, with subsets decided via MSSelection. 
00070 //  -- Contains one or more flagging methods of types derived from LFBase
00071 //  -- Iterates through the MS using a VisibilityIterator
00072 //  -- For each visbuffer, send each flagmethod the data and flags (by reference).
00073 //  -- Each flagmethod modifies the flags (in sequence, and in place).
00074 //  -- A FlagDisplay method displays data and flags as the visbuffers iterate by
00075 //  -- A record of summary statistics is returned after all the data has been traversed.
00076 // 
00077 // 
00078 // </synopsis>
00079 //
00080 // <example>
00081 //        // Construct MS
00082 //        MeasurementSet ms("test.MS2",Table::Update);
00083 //
00084 //        // Construct LightFlagger
00085 //        LightFlagger flagger(ms);
00086 //
00087 //        // Build record of global flagging options
00088 //        Record gen_options();
00089 //        gen_options.define("showplots",True);
00090 //        gen_options.define("writeflags",False);
00091 //
00092 //        // Build record of flagging-agent options (can read default params first)
00093 //        Record algo_options( flagger.getparameters("tfcrop") );
00094 //        algo_options.define("freq_amp_cutoff",3.0);
00095 //
00096 //        // Send flagging-agent parameters to the flagger
00097 //        flagger.setparameters("tfcrop",algo_options);
00098 //
00099 //        // Run it.
00100 //        flagger.run(gen_options);
00101 //
00102 //
00103 // </example>
00104 //
00105 // <motivation>
00106 // The existing Flagger infrastructure needs clean-up.
00107 // </motivation>
00108 //
00109 // <todo asof="2011/05/23">
00110 //   <li> Add a new LFManualFlag class for manual flagging and 'quacking'.
00111 //   <li> Add data-expression functionality for the user to control what to flag on.
00112 //   <li> Add a new LFClip for simple amplitude-threshold clipping
00113 //   <li> Add a new LFFilters for 1D sliding-window statistical filters
00114 //   <li> Add flag statistics counts to LFFlagDisplay
00115 //   <li> Allow extensions across polarization, baseline, time,freq in LFExtendFlags
00116 //   <li> Add support for saving/reading parameters to/from Flag_Cmd strings
00117 //   <li> Use async visbuffer I/O (the dominant cost is I/O).
00118 //   <li> Create a task interface to replace flagdata/flagdata2
00119 // </todo>
00120  
00121 
00122   class LightFlagger 
00123   {
00124   public:  
00125     // default constructor 
00126     LightFlagger  ();
00127     // default destructor
00128     ~LightFlagger ();
00129 
00130     // Attach to a measurement se
00131     Bool attach(String msname);    
00132 
00133     // Set the data selection parameters
00134     Bool setdata(String field="", String spw="", String array="", String feed="", String scan="",
00135                     String baseline="", String uvrange="", String time="",
00136                     String correlation="");
00137     
00138     // Set autoflag params
00139     Bool setparameters(String algorithm, Record &parameters);
00140     
00141     // Get default autoflag params
00142     Record getparameters(String algorithm);
00143 
00144     // Run all the algorithms
00145     Record run(Record &parameters);    
00146     
00147     // flag version support.
00148     Bool  saveFlagVersion(String versionname, String comment, String merge);
00149     Bool  restoreFlagVersion(Vector<String> versionname, String merge);
00150     Bool  deleteFlagVersion(Vector<String> versionname);
00151     Bool  getFlagVersionList( Vector<String> &verlist);
00152     
00153   private:
00154 
00155     Record defaultParameters();
00156     void initParameters(Record &parameters);
00157 
00158     Bool setupIterator();
00159     Bool readVisAndFlags(VisBuffer &vb, uInt timecnt);
00160     Bool writeFlags(VisibilityIterator &vi, VisBuffer &vb, uInt timecnt);
00161 
00162      void Ants(uInt bs, uInt *a1, uInt *a2);
00163      uInt BaselineIndex(uInt row, uInt a1, uInt a2);
00164 
00165   void AllocateMemory();  
00166 
00167   void runAllMethods(Int threadindex, Int nmethods, Int nthreads, 
00168                      uInt numT, uInt numAnt, uInt numB, uInt numC, uInt nPol);
00169 
00170     MeasurementSet   ms_p,  mssel_p;
00171     MSSelection mss_p;
00172     VisSet *vs_p;
00173 
00174     // List of flagging agents
00175     Vector<CountedPtr<LFBase> >  flagmethods_p;
00176     Int maxnumthreads_p;  
00177 
00178     // Generic input parameters
00179     Bool ShowPlots, WriteFlagsToMS, StopAndExit, FlagZeros,UsePreFlags;
00180     Int NumTime, FlagLevel;
00181     String Expr, Column;
00182 
00183     Vector<Int> selCorr;
00184     Vector<Bool> baselineFlag;
00185  
00186     // Shapes per chunk
00187   uInt NumT; // Number of timestamps in one block
00188   uInt NumB; // Number of baselines.
00189   uInt NumAnt; // Number of antennas.
00190   uInt NumC; // Number of channels
00191   uInt NumP; // Number of polarizations
00192     uInt Nrow; // Number of rows in current chunk
00193     uInt Ncorr; // Number of correlations
00194     IPosition VisCubeShp;
00195 
00196     // Data Mapper
00197     //    Vector<Bool> dmCorrs;
00198     // Bool SetupDataMapper();
00199 
00200     // Storage arrays
00201   Cube<Float> visc;     // visCube
00202   Cube<Bool> flagc;     // flagCube
00203     Cube<Bool> preflagc; //flagCube before flagging.
00204 
00205    Vector<Int> ant1,ant2;
00206     //   LogIO os;
00207     Bool dbg;
00208     
00209   };
00210   
00211   
00212 } //# NAMESPACE CASA - END
00213 
00214 #endif
00215