casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
AgentFlagger.h
Go to the documentation of this file.
00001 //# AgentFlagger.h: this defines AgentFlagger
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 <iostream>
00031 #include <vector>
00032 
00033 #include <casa/Logging/LogIO.h>
00034 #include <casa/Arrays/Vector.h>
00035 #include <casa/Containers/Record.h>
00036 #include <casa/Quanta/Quantum.h>
00037 #include <flagging/Flagging/FlagVersion.h>
00038 
00039 #include <flagging/Flagging/FlagDataHandler.h>
00040 #include <flagging/Flagging/FlagMSHandler.h>
00041 #include <flagging/Flagging/FlagCalTableHandler.h>
00042 #include <flagging/Flagging/FlagAgentBase.h>
00043 #include <flagging/Flagging/FlagAgentSummary.h>
00044 #include <flagging/Flagging/FlagAgentDisplay.h>
00045 
00046 #include <boost/smart_ptr.hpp>
00047 
00048 namespace casa { //# NAMESPACE CASA - BEGIN
00049 
00050 // <summary>
00051 // AgentFlagger: high-performance automated flagging
00052 // </summary>
00053 
00054 // <use visibility=global>
00055 
00056 // <reviewed reviewer="" date="" tests="" demos="">
00057 // </reviewed>
00058 
00059 // <prerequisite>
00060 //   <li> <linkto class="FlagDataHandler:description">FlagDataHandler</linkto>
00061 //   <li> <linkto class="FlagMSHandlerr:description">FlagMSHandler</linkto>
00062 //   <li> <linkto class="FlagCalTableHandler:description">FlagCalTableHandler</linkto>
00063 // </prerequisite>
00064 //
00065 // <etymology>
00066 //
00067 // </etymology>
00068 //
00069 // <synopsis>
00070 // AgentFlagger performs automated flagging operations on a measurement set or calibration
00071 // table. The class is constructed from an MS or cal table. After that, the run method may be used
00072 // to run any number of flagging agents.
00073 // </synopsis>
00074 //
00075 // <example>
00076 // <srcblock>
00077 // // The following is a typical way of using this class and its methods to perform flagging.
00078 //
00079 // // Open the MS or a calibration file and attach it to the tool. This will create an object of the
00080 // // FlagDataHandler (fdh) type. The constructor of the fdh takes three arguments,
00081 // // the MS or CAL table, the iteration approach to use and the time interval. Only the MS
00082 // // is mandatory to use. By default it will use the FlagDataHandler::SUB_INTEGRATION iteration
00083 // // approach and 0.0 seconds as the time interval.
00084 //
00085 //    AgentFlagger *tf = new AgentFlagger();
00086 //    af->open('my.ms')
00087 //
00088 // // Select the data where to flag. If left blank, the whole MS will be selected. This step
00089 // // will use the MS Selection class. There are two methods to perform the selection. One takes
00090 // // a Record of the parameters, the other takes the individual parameters as arguments.
00091 //
00092 // // 1) First method:
00093 //    String spw = "0:1~10";
00094 //    String scan = "1";
00095 //    Record selection = Record();
00096 //    selection.define("spw", spw);
00097 //    selection.define("scan", scan);
00098 //    af->selectData(selection);
00099 //
00100 // // 2) Second method:
00101 //    af->selectData(spw=spw, scan=scan);
00102 //
00103 // // Now it is time to build a list of the agents that we want to run to process the data. This
00104 // // step will create a list of all the agents that will be executed to flag/unflag the data.
00105 // // This method can be called multiple times. Every call should contain the desired parameters of
00106 // // the agent and optionally data selection parameters. When data selection parameters are present,
00107 // // the agent will loop through only that portion of the data.
00108 //
00109 // // This method will check if the requested agent (mode) is known from the following list
00110 // // (manual, clip, quack, shadow, elevation, tfcrop, rflag, extend, unflag and summary). If
00111 // // empty or unknown, it will give a warning and return.
00112 //
00113 // // If any tfcrop, rflag or extend mode is present, this method will calculate the maximum value
00114 // // of time interval (ntime) from these agents. The maximum value will be used for all agents in
00115 // // the list.
00116 //
00117 // // A similar situation will happen with the combinescans parameter. If any of the combinescans is
00118 // // True, it will be taken as True to all agents.
00119 //
00120 // // Async I/O will be activated if any of the modes clip, tfcrop or rflag is requested. Also for
00121 // // these three modes, there will be a call to a function that will validate the requested
00122 // // datacolumn parameter. It will detect if the input is an MS or a cal table and validate the
00123 // // column. The default is the DATA column. If the input is a cal table, the function will
00124 // // first check if FPARAM is available, then CPARAM. If none of them is available it will return
00125 // // False and the agent will not be created.
00126 //
00127 // // Only for the tfcrop agent, if a correlation ALL is requested, this method will create one
00128 // // agent for each available polarization in the MS. For example, if the MS contains polarizations
00129 // // XX and YY and the parameter is correlation="ABS_ALL", then there will be two tfcrop agents,
00130 // // one with correlation="ABS_XX" and the other with correlation="ABS_YY". The apply parameter
00131 // // is set by default to True to apply the flags.
00132 //
00133 //     Record agent_pars = Record();
00134 //     agent_pars.define("mode", "clip");
00135 //     agent_pars.define("clipzeros", true);
00136 //     agent_pars.define("apply", true);
00137 //     af->parseAgentParameters(agent_pars);
00138 //
00139 //     Record agent_pars = Record();
00140 //     agent_pars.define("mode", "manual");
00141 //     agent_pars.define("autocorr", true);
00142 //     af->parseAgentParameters(agent_pars);
00143 //
00144 //     Record agent_pars = Record();
00145 //     agent_pars.define("mode", "summary");
00146 //     agent_pars.define("basecnt", true);
00147 //     af->parseAgentParameters(agent_pars);
00148 //
00149 // // There are convenience functions to parse the agent's parameters, one specific for each agent.
00150 // // The above calls can also be done using these functions instead.
00151 //
00152 //     af->parseClipParameters(clipzeros=true, apply=true);
00153 //     af->parseManualParameters(autocorr=true);
00154 //     af->parseSummaryParameters(basecnt=true);
00155 //
00156 // // In either one of the cases, three agents will be created.
00157 // //
00158 // // NOTE: it is possible to add multiple summary agents to the list and gather a list of summary
00159 // // reports when executing the tool.
00160 //
00161 // // We need to initialize the agents, which
00162 // // will call the constructor of each one of them and set the parameters that were given in the previous
00163 // // calls. Some basic checks will be performed at this stage for types and values of the parameters.
00164 //
00165 // // If any tfcrop, rflag, extend or display agent is in the list, the iteration approach will be
00166 // // set to a different value depending on whether combinescans is true or not. When True, the
00167 // // iteration approach will be set to FlagDataHandler::COMBINE_SCANS_MAP_ANTENNA_PAIRS_ONLY, otherwise
00168 // // to FlagDataHandler::COMPLETE_SCAN_MAP_ANTENNA_PAIRS_ONLY.
00169 //
00170 // // This method will create agents and add them to a FlagAgentList. If for any reason, the call to
00171 // // FlagAgentBase::create(fdh_p, agent_rec) fails, an error message will be displayed. Any agents previously
00172 // // added to the FlagAgentList will remain there. A subsequent call to this method can be done to add
00173 // // more agents to the same FlagAgentList.
00174 //
00175 //     af->initAgents();
00176 //
00177 // // Before next step which will write the new flags, it is advisable to create a backup of
00178 // // the current flags in the MS.
00179 //
00180 //    af.saveflagversion(versionname='backup_before_manual_1',
00181 //                       comment='Backup of flags before running manual',merge='replace')
00182 //
00183 // // The next step in the chain is to actually process the flags and write them or
00184 // // not to the MS. The run method takes two parameters, writeflags and sequential.
00185 // // The parameter writeflags controls whether to write the flags or not to the MS.
00186 // // By default it is set to True. Setting writeflags to False is useful when one
00187 // // wants to run the tool together with the display agent to see what is going to be
00188 // // flagged before deciding to write or not to the MS. The sequential parameter
00189 // // controls if the order of the agent's list needs to be preserved or not. If set to False,
00190 // // the order will not be preserved and the framework may execute the agent's list in parallel.
00191 // // By default sequential is set to True.
00192 //
00193 // // The run method gathers several reports, depending on which agents are run. The display and summary agents
00194 // // produce reports that can be retrieved from calling the run method. The reports are returned via a Record
00195 // // that may contain multiple reports at the same time.
00196 
00197 //     Record myReports;
00198 //     myReports = af->run();
00199 //
00200 // // To destroy the tool, call a method to execute the destructor.
00201 //
00202 //     af->done();
00203 //
00204 // </srcblock>
00205 // </example>
00206 //
00207 // <motivation>
00208 // To flag data using different algorithms.
00209 // </motivation>
00210 //
00211 
00212 
00213 class AgentFlagger
00214 {
00215 protected:
00216 
00217         static LogIO os;
00218 
00219         // variables used to initialize the FlagDataHandler
00220         String msname_p;
00221         uShort iterationApproach_p;
00222         Double timeInterval_p;
00223         Bool isMS_p;
00224 
00225 
00226         // members to parse to selectData
00227         String spw_p;
00228         String scan_p;
00229         String field_p;
00230         String antenna_p;
00231         String timerange_p;
00232         String correlation_p;
00233         String intent_p;
00234         String feed_p;
00235         String array_p;
00236         String uvrange_p;
00237         String observation_p;
00238         Record dataselection_p;
00239 
00240         // agent's members
00241         String mode_p;
00242         Record agentParams_p;
00243         FlagAgentSummary *summaryAgent_p;
00244         Bool combinescans_p;
00245 
00246         // True if there are apply and unapply parameters in the list
00247         Bool mixed_p;
00248 
00249         // Display agent parameters
00250         FlagAgentDisplay *displayAgent_p;
00251 
00252         // variables for initAgents
00253         FlagDataHandler *fdh_p;
00254         std::vector<Record> agents_config_list_p;
00255         std::vector<Record> agents_config_list_copy_p;
00256         FlagAgentList agents_list_p;
00257 
00258 public:  
00259         // default constructor
00260         AgentFlagger();
00261 
00262         // destructor
00263         ~AgentFlagger();
00264 
00265         // reset everything
00266         void done();
00267 
00268         // configure the tool, open the MS
00269         bool open(String msname, Double ntime);
00270 
00271         // parse the data selection
00272         bool selectData(Record selrec);
00273         bool selectData(String field, String spw, String array, String feed, String scan,
00274                             String antenna, String uvrange, String timerange,
00275                             String correlation, String intent, String observation="");
00276 
00277         // parse the parameters of the agent
00278         bool parseAgentParameters(Record agent_params);
00279         String getExpressionFunction(String expression);
00280         bool isExpressionPolarizationAll(String expression);
00281 
00282         // initialize the agents list
00283         bool initAgents();
00284 
00285         // Run the tool and write the flags to the MS
00286         Record run(Bool writeflags, Bool sequential=true);
00287 
00288         // Flag backup methods
00289         bool printFlagSelections();
00290         bool saveFlagVersion(String versionname, String comment, String merge);
00291         bool restoreFlagVersion(Vector<String> versionname, String merge);
00292         bool deleteFlagVersion(Vector<String> versionname);
00293         bool getFlagVersionList(Vector<String> &verlist);
00294 
00295         // Agent's specific parsing methods (for convenience only)
00296         // Parse parameters for manual
00297         bool parseManualParameters(String field, String spw, String array, String feed, String scan,
00298             String antenna, String uvrange, String timerange,String correlation,
00299             String intent, String observation, Bool autocorr, Bool apply);
00300 
00301         // Parse parameters for clip
00302         bool parseClipParameters(String field, String spw, String array, String feed, String scan,
00303             String antenna, String uvrange, String timerange,String correlation,
00304             String intent, String observation, String datacolumn,
00305             Vector<Double> clipminmax, Bool clipoutside, Bool channelavg,
00306             Bool clipzeros, Bool apply);
00307 
00308         // Parse parameters for quack
00309         bool parseQuackParameters(String field, String spw, String array, String feed, String scan,
00310             String antenna, String uvrange, String timerange,String correlation,
00311             String intent, String observation, String quackmode, Double quackinterval,
00312             Bool quackincrement, Bool apply);
00313 
00314         // Parse parameters for elevation
00315         bool parseElevationParameters(String field, String spw, String array, String feed,
00316                         String scan, String antenna, String uvrange, String timerange, String correlation,
00317             String intent, String observation, Double lowerlimit, Double upperlimit,
00318             Bool apply);
00319 
00320 
00321         // Parse parameters for tfcrop
00322         bool parseTfcropParameters(String field, String spw, String array, String feed,
00323                         String scan, String antenna, String uvrange, String timerange, String correlation,
00324             String intent, String observation, Double ntime, Bool combinescans,
00325             String datacolumn, Double timecutoff, Double freqcutoff, String timefit,
00326             String freqfit, Int maxnpieces, String flagdimension, String usewindowstats,
00327             Int halfwin, Bool apply);
00328 
00329         // Parse parameters for extend
00330         bool parseExtendParameters(String field, String spw, String array, String feed,
00331                         String scan, String antenna, String uvrange, String timerange, String correlation,
00332             String intent, String observation, Double ntime, Bool combinescans,
00333             Bool extendpols, Double growtime, Double growfreq, Bool growaround,
00334             Bool flagneartime, Bool flagnearfreq, Bool apply);
00335 
00336         // Parse parameters for summary
00337         bool parseSummaryParameters(String field, String spw, String array, String feed,
00338                         String scan, String antenna, String uvrange, String timerange, String correlation,
00339             String intent, String observation, Bool spwchan, Bool spwcorr, Bool basecnt);
00340 
00341 private:
00342 
00343         AgentFlagger(const AgentFlagger &) {};
00344 
00345         AgentFlagger& operator=(const AgentFlagger &)  {return *this;};
00346 
00347         // Maximum between two numbers
00348         void getMax(Double value);
00349 
00350         // Check if mode is valid against a list of known modes
00351         bool isModeValid(String mode);
00352 
00353         String validateDataColumn(String datacol);
00354 
00355         // Sink used to store history
00356         LogSink logSink_p;
00357 
00358         // Debug message flag
00359         static const bool dbg;
00360 
00361         // Store the temporary maximum value
00362         Double max_p;
00363 
00364         // Helper members
00365         Bool timeset_p;
00366         Bool iterset_p;
00367 
00368 };
00369 
00370 
00371 } //# NAMESPACE CASA - END
00372 
00373 #endif
00374