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 00047 namespace casa { //# NAMESPACE CASA - BEGIN 00048 00049 // <summary> 00050 // AgentFlagger: high-performance automated flagging 00051 // </summary> 00052 00053 // <use visibility=global> 00054 00055 // <reviewed reviewer="" date="" tests="" demos=""> 00056 // </reviewed> 00057 00058 // <prerequisite> 00059 // <li> <linkto class="FlagDataHandler:description">FlagDataHandler</linkto> 00060 // <li> <linkto class="FlagMSHandlerr:description">FlagMSHandler</linkto> 00061 // <li> <linkto class="FlagCalTableHandler:description">FlagCalTableHandler</linkto> 00062 // </prerequisite> 00063 // 00064 // <etymology> 00065 // 00066 // </etymology> 00067 // 00068 // <synopsis> 00069 // AgentFlagger performs automated flagging operations on a measurement set or calibration 00070 // table. The class is constructed from an MS or cal table. After that, the run method may be used 00071 // to run any number of flagging agents. 00072 // </synopsis> 00073 // 00074 // <example> 00075 // <srcblock> 00076 // // The following is a typical way of using this class and its methods to perform flagging. 00077 // 00078 // // Open the MS or a calibration file and attach it to the tool. This will create an object of the 00079 // // FlagDataHandler (fdh) type. The constructor of the fdh takes three arguments, 00080 // // the MS or CAL table, the iteration approach to use and the time interval. Only the MS 00081 // // is mandatory to use. By default it will use the FlagDataHandler::SUB_INTEGRATION iteration 00082 // // approach and 0.0 seconds as the time interval. 00083 // 00084 // AgentFlagger *tf = new AgentFlagger(); 00085 // af->open('my.ms') 00086 // 00087 // // Select the data where to flag. If left blank, the whole MS will be selected. This step 00088 // // will use the MS Selection class. There are two methods to perform the selection. One takes 00089 // // a Record of the parameters, the other takes the individual parameters as arguments. 00090 // 00091 // // 1) First method: 00092 // String spw = "0:1~10"; 00093 // String scan = "1"; 00094 // Record selection = Record(); 00095 // selection.define("spw", spw); 00096 // selection.define("scan", scan); 00097 // af->selectData(selection); 00098 // 00099 // // 2) Second method: 00100 // af->selectData(spw=spw, scan=scan); 00101 // 00102 // // Now it is time to build a list of the agents that we want to run to process the data. This 00103 // // step will create a list of all the agents that will be executed to flag/unflag the data. 00104 // // This method can be called multiple times. Every call should contain the desired parameters of 00105 // // the agent and optionally data selection parameters. When data selection parameters are present, 00106 // // the agent will loop through only that portion of the data. 00107 // 00108 // // This method will check if the requested agent (mode) is known from the following list 00109 // // (manual, clip, quack, shadow, elevation, tfcrop, rflag, extend, unflag and summary). If 00110 // // empty or unknown, it will give a warning and return. 00111 // 00112 // // If any tfcrop, rflag or extend mode is present, this method will calculate the maximum value 00113 // // of time interval (ntime) from these agents. The maximum value will be used for all agents in 00114 // // the list. 00115 // 00116 // // A similar situation will happen with the combinescans parameter. If any of the combinescans is 00117 // // True, it will be taken as True to all agents. 00118 // 00119 // // Async I/O will be activated if any of the modes clip, tfcrop or rflag is requested. Also for 00120 // // these three modes, there will be a call to a function that will validate the requested 00121 // // datacolumn parameter. It will detect if the input is an MS or a cal table and validate the 00122 // // column. The default is the DATA column. If the input is a cal table, the function will 00123 // // first check if FPARAM is available, then CPARAM. If none of them is available it will return 00124 // // False and the agent will not be created. 00125 // 00126 // // Only for the tfcrop agent, if a correlation ALL is requested, this method will create one 00127 // // agent for each available polarization in the MS. For example, if the MS contains polarizations 00128 // // XX and YY and the parameter is correlation="ABS_ALL", then there will be two tfcrop agents, 00129 // // one with correlation="ABS_XX" and the other with correlation="ABS_YY". The apply parameter 00130 // // is set by default to True to apply the flags. 00131 // 00132 // Record agent_pars = Record(); 00133 // agent_pars.define("mode", "clip"); 00134 // agent_pars.define("clipzeros", true); 00135 // agent_pars.define("apply", true); 00136 // af->parseAgentParameters(agent_pars); 00137 // 00138 // Record agent_pars = Record(); 00139 // agent_pars.define("mode", "manual"); 00140 // agent_pars.define("autocorr", true); 00141 // af->parseAgentParameters(agent_pars); 00142 // 00143 // Record agent_pars = Record(); 00144 // agent_pars.define("mode", "summary"); 00145 // agent_pars.define("basecnt", true); 00146 // af->parseAgentParameters(agent_pars); 00147 // 00148 // // There are convenience functions to parse the agent's parameters, one specific for each agent. 00149 // // The above calls can also be done using these functions instead. 00150 // 00151 // af->parseClipParameters(clipzeros=true, apply=true); 00152 // af->parseManualParameters(autocorr=true); 00153 // af->parseSummaryParameters(basecnt=true); 00154 // 00155 // // In either one of the cases, three agents will be created. 00156 // // 00157 // // NOTE: it is possible to add multiple summary agents to the list and gather a list of summary 00158 // // reports when executing the tool. 00159 // 00160 // // We need to initialize the agents, which 00161 // // will call the constructor of each one of them and set the parameters that were given in the previous 00162 // // calls. Some basic checks will be performed at this stage for types and values of the parameters. 00163 // 00164 // // If any tfcrop, rflag, extend or display agent is in the list, the iteration approach will be 00165 // // set to a different value depending on whether combinescans is true or not. When True, the 00166 // // iteration approach will be set to FlagDataHandler::COMBINE_SCANS_MAP_ANTENNA_PAIRS_ONLY, otherwise 00167 // // to FlagDataHandler::COMPLETE_SCAN_MAP_ANTENNA_PAIRS_ONLY. 00168 // 00169 // // This method will create agents and add them to a FlagAgentList. If for any reason, the call to 00170 // // FlagAgentBase::create(fdh_p, agent_rec) fails, an error message will be displayed. Any agents previously 00171 // // added to the FlagAgentList will remain there. A subsequent call to this method can be done to add 00172 // // more agents to the same FlagAgentList. 00173 // 00174 // af->initAgents(); 00175 // 00176 // // Before next step which will write the new flags, it is advisable to create a backup of 00177 // // the current flags in the MS. 00178 // 00179 // af.saveflagversion(versionname='backup_before_manual_1', 00180 // comment='Backup of flags before running manual',merge='replace') 00181 // 00182 // // The next step in the chain is to actually process the flags and write them or 00183 // // not to the MS. The run method takes two parameters, writeflags and sequential. 00184 // // The parameter writeflags controls whether to write the flags or not to the MS. 00185 // // By default it is set to True. Setting writeflags to False is useful when one 00186 // // wants to run the tool together with the display agent to see what is going to be 00187 // // flagged before deciding to write or not to the MS. The sequential parameter 00188 // // controls if the order of the agent's list needs to be preserved or not. If set to False, 00189 // // the order will not be preserved and the framework may execute the agent's list in parallel. 00190 // // By default sequential is set to True. 00191 // 00192 // // The run method gathers several reports, depending on which agents are run. The display and summary agents 00193 // // produce reports that can be retrieved from calling the run method. The reports are returned via a Record 00194 // // that may contain multiple reports at the same time. 00195 00196 // Record myReports; 00197 // myReports = af->run(); 00198 // 00199 // // To destroy the tool, call a method to execute the destructor. 00200 // 00201 // af->done(); 00202 // 00203 // </srcblock> 00204 // </example> 00205 // 00206 // <motivation> 00207 // To flag data using different algorithms. 00208 // </motivation> 00209 // 00210 00211 00212 class AgentFlagger 00213 { 00214 protected: 00215 00216 static LogIO os; 00217 00218 // variables used to initialize the FlagDataHandler 00219 String msname_p; 00220 uShort iterationApproach_p; 00221 Double timeInterval_p; 00222 Bool isMS_p; 00223 00224 00225 // members to parse to selectData 00226 String spw_p; 00227 String scan_p; 00228 String field_p; 00229 String antenna_p; 00230 String timerange_p; 00231 String correlation_p; 00232 String intent_p; 00233 String feed_p; 00234 String array_p; 00235 String uvrange_p; 00236 String observation_p; 00237 Record dataselection_p; 00238 00239 // agent's members 00240 String mode_p; 00241 Record agentParams_p; 00242 FlagAgentSummary *summaryAgent_p; 00243 Bool combinescans_p; 00244 Bool extendflags_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, casac::variant chanbin, Bool timeavg, 00306 String timebin, 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 // Parse parameters for tfcrop 00321 bool parseTfcropParameters(String field, String spw, String array, String feed, 00322 String scan, String antenna, String uvrange, String timerange, String correlation, 00323 String intent, String observation, Double ntime, Bool combinescans, 00324 String datacolumn, Double timecutoff, Double freqcutoff, String timefit, 00325 String freqfit, Int maxnpieces, String flagdimension, String usewindowstats, 00326 Int halfwin, Bool extendflags, Bool apply, Bool channelavg, casac::variant chanbin, Bool timeavg, 00327 String timebin); 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=0.0, Bool combinescans=false, 00333 Bool extendpols=true, Double growtime=50.0, Double growfreq=50.0, Bool growaround=false, 00334 Bool flagneartime=false, Bool flagnearfreq=false, Bool apply=true); 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, Bool fieldcnt, 00340 String name); 00341 00342 private: 00343 00344 AgentFlagger(const AgentFlagger &) {}; 00345 00346 AgentFlagger& operator=(const AgentFlagger &) {return *this;}; 00347 00348 // Maximum between two numbers 00349 void getMax(Double value); 00350 00351 // Check if mode is valid against a list of known modes 00352 bool isModeValid(String mode); 00353 00354 Bool validateDataColumn(String datacol); 00355 00356 // Sink used to store history 00357 LogSink logSink_p; 00358 00359 // Debug message flag 00360 static const bool dbg; 00361 00362 // Store the temporary maximum value 00363 Double max_p; 00364 00365 // Helper members 00366 Bool timeset_p; 00367 Bool iterset_p; 00368 00369 // Time average parameter in clip mode 00370 Bool timeAvg_p; 00371 00372 }; 00373 00374 00375 } //# NAMESPACE CASA - END 00376 00377 #endif 00378