casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Flagger.h
Go to the documentation of this file.
00001 //# Flagger.h: this defines Flagger
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 //# $Id$
00027 #ifndef FLAGGING_FLAGGER_H
00028 #define FLAGGING_FLAGGER_H
00029 
00030 #include <flagging/Flagging/RFCommon.h>
00031 #include <flagging/Flagging/RFABase.h>
00032 #include <flagging/Flagging/FlagVersion.h>
00033 
00034 #include <ms/MeasurementSets/MeasurementSet.h>
00035 #include <ms/MeasurementSets/MSSelection.h>
00036 #include <measures/Measures/MDirection.h>
00037 #include <measures/Measures/MPosition.h>
00038 #include <measures/Measures/MRadialVelocity.h>
00039 #include <casa/Logging/LogIO.h>
00040 #include <casa/Arrays/Vector.h>
00041 #include <casa/Containers/Record.h>
00042 #include <casa/Quanta/Quantum.h>
00043 
00044 #include <boost/smart_ptr.hpp>
00045 
00046 namespace casa { //# NAMESPACE CASA - BEGIN
00047 
00048 class VisSet;
00049 class RFChunkStats;
00050         
00051 // <summary>
00052 // Flagger: high-performance automated flagging
00053 // </summary>
00054 
00055 // <use visibility=global>
00056 
00057 // <reviewed reviewer="" date="" tests="" demos="">
00058 // </reviewed>
00059 
00060 // <prerequisite>
00061 //   <li> implement/Flagger
00062 // </prerequisite>
00063 //
00064 // <etymology>
00065 // MSFlagger and plain flagger were already taken.
00066 // </etymology>
00067 //
00068 // <synopsis>
00069 // Flagger performs automated flagging operations on a measurement set.
00070 // The class is constructed from an MS. After that, the run method may be used
00071 // to run any number of flagging agents.
00072 // </synopsis>
00073 //
00074 // <example>
00075 //        // construct MS and flagger
00076 //        MeasurementSet ms("test.MS2",Table::Update);
00077 //        Flagger flagger(ms);
00078 //        // build record of global flagging options
00079 //        Record opt(Record::Variable);
00080 //        // build record of flagging agents to be run
00081 //        Record selopt( flagger.defaultAgents().asRecord("select") );
00082 //        selopt.define(RF_POLICY,"RESET");
00083 //        selopt.define(RF_AUTOCORR,True);
00084 //        Record agents(Record::Variable);
00085 //        agents.defineRecord("select",selopt);
00086 //        // perform the flagging
00087 //        flagger.run(agents,opt);
00088 // </example>
00089 //
00090 // <motivation>
00091 // We need an automated flagging tool. Existing tools (MSFlagger and flagger.g)
00092 // were too slow. Hence, Flagger was developed.
00093 // </motivation>
00094 //
00095 // <todo asof="2001/04/16">
00096 //   <li> add this feature
00097 //   <li> fix this bug
00098 //   <li> start discussion of this possible extension
00099 // </todo>
00100 
00101 
00102 class Flagger : public FlaggerEnums
00103 {
00104 protected:
00105 // creates an agent by name
00106   boost::shared_ptr<RFABase> createAgent ( const String &name,RFChunkStats &chunk,const RecordInterface &parms, bool &only_selector );
00107 
00108 // sets up record of agents and default parameters
00109   const RecordInterface & setupAgentDefaults ();
00110 
00111 // print flagging reports from individual agents
00112   void printAgentReports  ( );
00113 
00114   void printSummaryReport ( RFChunkStats &chunk );
00115   Bool selectDataChannel();
00116  
00117   MeasurementSet   ms;
00118   MeasurementSet   originalms;
00119   std::vector<boost::shared_ptr<RFABase> > acc;
00120 
00121   //new added
00122   MeasurementSet *mssel_p;
00123   VisSet *vs_p;
00124   bool scan_looping;     /* Is scan number part of visiter looping? */
00125   String msname_p;
00126   Bool nullSelect_p;
00127   Bool setdata_p;
00128   Bool selectdata_p;
00129   String dataMode_p;  
00130 
00131 //  Vector<Int> dataEnd_p;
00132 //  Vector<Int> dataStart_p, dataStep_p;
00133 //  Vector<Int> dataspectralwindowids_p;
00134 //  Vector<Int> spwidnchans_p;
00135   Vector<String> correlations_p;
00136   Vector<Int> datafieldids_p;
00137   Vector<Int> datadescids_p;
00138   MRadialVelocity mDataStart_p;
00139   MRadialVelocity mDataStep_p;
00140 
00141   //
00142   uInt nant,nifr,nfeed,nfeedcorr;
00143   Vector<Int> ifr2ant1,ifr2ant2;
00144   Vector<String> antnames;
00145   Vector<Double> spwfreqs;
00146     
00147   Record agent_defaults;
00148 
00149   static LogIO os;
00150 
00151 public:  
00152 // default constructor 
00153   Flagger  ();
00154 // construct and attach to a measurement set
00155   Flagger  ( MeasurementSet &ms );
00156   
00157   ~Flagger ();
00158   
00159 // Change or set the MS this Flagger refers to.
00160   bool attach( MeasurementSet &ms, Bool setupAgentDefaults=True );
00161 
00162   // Set the data selection parameters
00163   Bool selectdata(Bool useoriginalms=False, 
00164                   String field="", String spw="", String array="", String feed="", String scan="",
00165                   String baseline="", String uvrange="", String time="",
00166                   String correlation="", String intent="", String observation="");
00167 
00168   // Make a data selection
00169   Bool setdata(String field, String spw, String array, String feed, String scan,
00170                String baseline, String uvrange, String time,
00171                String correlation, String intent, String observation="");
00172   
00173   // Make a selection for manual flagging
00174   Bool setmanualflags(Bool autocorr,
00175                       Bool unflag, 
00176                       String clipexpr, 
00177                       Vector<Double> cliprange, 
00178                       String clipcolumn, 
00179                       Bool outside, 
00180                       Bool channel_average,
00181                       Double quackinterval=0.0, 
00182                       String quackmode=String("beg"),
00183                       Bool quackincrement=Bool(false),
00184                       String opmode=String("flag"),
00185                       Double diameter = -1.0,
00186                       Double lowerlimit = -1.0,
00187                       Double upperlimit = 91.0);
00188 
00189   // Clean up all agents of type "select".
00190   //Bool clearflagselections(Vector<Int> &recordlist,Vector<String> &agentlist);
00191   Bool clearflagselections(Int recordindex);
00192   Bool printflagselections();
00193 
00194   // Fill the selection record to attach to the list of agents
00195   Bool fillSelections(Record &rec);
00196 
00197   // Set autoflag params
00198   Bool setautoflagparams(String algorithm, Record &parameters);
00199 
00200   // Get default autoflag params
00201   Record getautoflagparams(String algorithm);
00202 
00203   Bool addAgent(RecordInterface &newAgent);
00204   
00205   // Detaches from the MS  
00206   void detach();
00207   
00208   Record run(Bool trial, Bool reset);    
00209 
00210   void summary ( const RecordInterface &agents ); 
00211 
00212     // flag version support.
00213   Bool  saveFlagVersion(String versionname, String comment, String merge);
00214   Bool  restoreFlagVersion(Vector<String> versionname, String merge);
00215   Bool  deleteFlagVersion(Vector<String> versionname);
00216   Bool  getFlagVersionList( Vector<String> &verlist);
00217  
00218   
00219   // returns current MS
00220   const MeasurementSet & measSet() const { return ms; }
00221     
00222 // number of antennas in MS
00223   uInt numAnt    () const 
00224       { return nant; };
00225 
00226 // number of IFRs in MS
00227   uInt numIfr    () const 
00228       { return nifr; };
00229 
00230 // number of feeds in MS
00231   uInt numFeed    () const 
00232       { return nfeed; };
00233 
00234 // number of feed correlations in MS
00235   uInt numFeedCorr    () const 
00236       { return nfeedcorr; };
00237 
00238 // names of antennas
00239   const Vector<String> & antNames() const 
00240       { return antnames; };
00241   
00242 // derives a flat IFR index from two antenna indices
00243   uInt ifrNumber ( Int ant1,Int ant2 ) const;
00244 
00245 // vector version of above
00246   Vector<Int> ifrNumbers ( Vector<Int> ant1,Vector<Int> ant2 ) const;
00247 
00248 // derives antenna indices from a flat IFR index
00249   void ifrToAnt ( uInt &ant1,uInt &ant2,uInt ifr ) const;
00250 
00251 // returns a record with all available agents and their default options
00252   const RecordInterface & defaultAgents () const 
00253       { return agent_defaults; }
00254 
00255 // returns a record of available options
00256   static const RecordInterface & defaultOptions ();
00257 
00258 // returns the log sink 
00259   static LogIO & logSink ()       { return os; }
00260 
00261   /* Get rid of negative indices (meaning negation of antenna) in baselinelist */
00262   static void reform_baselinelist(Matrix<Int> &baselinelist, unsigned nant);
00263   
00264   static int my_aipspp_sum(const Array<Bool> &a);
00265 
00266 private:
00267     
00268   Flagger( const Flagger & )          {};
00269 
00270   Flagger& operator=(const Flagger &)  { return *this; };
00271 
00272   void printAgentRecord(String &, uInt, const RecordInterface &);
00273 
00274   // Sink used to store history
00275   LogSink logSink_p;
00276 
00277   // Hold the original ms 
00278   MeasurementSet *originalms_p;
00279   
00280   // MS Selection
00281   MSSelection *msselection_p;
00282   bool spw_selection;  //non-trivial spw-selection
00283 
00284   // List of Agents
00285   Record *agents_p;
00286   Int agentCount_p;
00287 
00288   // List of extra options
00289   Record *opts_p;
00290 
00291   // Debug Message flag
00292   static const bool dbg;
00293 
00294   Bool quack_agent_exists;
00295   /* More initialization is required, if there exists a quacking agent */
00296 };
00297 
00298 
00299 } //# NAMESPACE CASA - END
00300 
00301 #endif
00302