casa
$Rev:20696$
|
00001 //# TestFlagger.h: this defines TestFlagger 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 <tableplot/TablePlot/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 // TestFlagger: 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 // TestFlagger performs automated flagging operations on a measurement set. 00071 // The class is constructed from an MS. 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 // TestFlagger *tf = new TestFlagger(); 00086 // tf->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 // tf->selectData(selection); 00099 // 00100 // // 2) Second method: 00101 // tf->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. 00121 // 00122 // // Only for the tfcrop agent, if a correlation ALL is requested, this method will create one 00123 // // agent for each available polarization in the MS. For example, if the MS contains polarizations 00124 // // XX and YY and the parameter is correlation="ABS_ALL", then there will be two tfcrop agents, 00125 // // one with correlation="ABS_XX" and the other with correlation="ABS_YY". The apply parameter 00126 // // is set by default to True to apply the flags. 00127 // 00128 // Record agent_pars = Record(); 00129 // agent_pars.define("mode", "clip"); 00130 // agent_pars.define("clipzeros", true); 00131 // agent_pars.define("apply", true); 00132 // tf->parseAgentParameters(agent_pars); 00133 // 00134 // Record agent_pars = Record(); 00135 // agent_pars.define("mode", "manual"); 00136 // agent_pars.define("autocorr", true); 00137 // tf->parseAgentParameters(agent_pars); 00138 // 00139 // Record agent_pars = Record(); 00140 // agent_pars.define("mode", "summary"); 00141 // agent_pars.define("basecnt", true); 00142 // tf->parseAgentParameters(agent_pars); 00143 // 00144 // // There are convenience functions to parse the agent's parameters, one specific for each agent. 00145 // // The above calls can be done instead using these functions. 00146 // 00147 // tf->parseClipParameters(clipzeros=true, apply=true); 00148 // tf->parseManualParameters(autocorr=true); 00149 // tf->parseSummaryParameters(basecnt=true); 00150 // 00151 // // In either one of the cases, three agents will be created. 00152 // // 00153 // // NOTE: it is possible to add multiple summary agents to the list and gather a list of summary 00154 // // reports when executing the tool. 00155 // 00156 // // We need to initialize the agents, which 00157 // // will call the constructor of each one of them and set the parameters that were given in the previous 00158 // // calls. Some basic checks will be performed at this stage for types and values of the parameters. 00159 // 00160 // // If any tfcrop, rflag, extend or display agent is in the list, the iteration approach will be 00161 // // set to a different value depending on whether combinescans is true or not. When True, the 00162 // // iteration approach will be set to FlagDataHandler::COMBINE_SCANS_MAP_ANTENNA_PAIRS_ONLY, otherwise 00163 // // to FlagDataHandler::COMPLETE_SCAN_MAP_ANTENNA_PAIRS_ONLY. 00164 // 00165 // // This method will create agents and add them to a FlagAgentList. If for any reason, the call to 00166 // // FlagAgentBase::create(fdh_p, agent_rec) fails, an error message will be displayed. Any agents previously 00167 // // added to the FlagAgentList will remain there. A subsequent call to this method can be done to add 00168 // // more agents to the same FlagAgentList. 00169 // 00170 // tf->initAgents(); 00171 // 00172 // // The next step in the chain is to actually process the flags and write them or 00173 // // not to the MS. The run method takes two parameters, writeflags and sequential. 00174 // // The parameter writeflags controls whether to write the flags or not to the MS. 00175 // // By default it is set to True. Setting writeflags to False is useful when one 00176 // // wants to run the tool together with the display agent to see what is going to be 00177 // // flagged before deciding to write or not to the MS. The sequential parameter 00178 // // controls if the order of the agent's list needs to be preserved or not. If set to False, 00179 // // the order will not be preserved and the framework may execute the agent's list in parallel. 00180 // // By default sequential is set to True. 00181 // 00182 // // The run method gathers several reports, depending on which agents are run. The display and summary agents 00183 // // produce reports that can be retrieved from calling the run method. The reports are returned via a Record 00184 // // that may contain multiple reports at the same time. 00185 00186 // Record myReports; 00187 // myReports = tf->run(); 00188 // 00189 // // To destroy the tool, call a method to execute the destructor. 00190 // 00191 // tf->done(); 00192 // 00193 // </srcblock> 00194 // </example> 00195 // 00196 // <motivation> 00197 // To flag data using different algorithms. 00198 // </motivation> 00199 // 00200 00201 00202 class TestFlagger 00203 { 00204 protected: 00205 00206 static LogIO os; 00207 00208 // variables used to initialize the FlagDataHandler 00209 String msname_p; 00210 uShort iterationApproach_p; 00211 Double timeInterval_p; 00212 00213 00214 // members to parse to selectData 00215 String spw_p; 00216 String scan_p; 00217 String field_p; 00218 String antenna_p; 00219 String timerange_p; 00220 String correlation_p; 00221 String intent_p; 00222 String feed_p; 00223 String array_p; 00224 String uvrange_p; 00225 String observation_p; 00226 Record dataselection_p; 00227 00228 // agent's members 00229 String mode_p; 00230 Record agentParams_p; 00231 FlagAgentSummary *summaryAgent_p; 00232 Bool combinescans_p; 00233 00234 // True if there are apply and unapply parameters in the list 00235 Bool mixed_p; 00236 00237 // Display agent parameters 00238 FlagAgentDisplay *displayAgent_p; 00239 00240 // variables for initAgents 00241 FlagDataHandler *fdh_p; 00242 std::vector<Record> agents_config_list_p; 00243 std::vector<Record> agents_config_list_copy_p; 00244 FlagAgentList agents_list_p; 00245 00246 public: 00247 // default constructor 00248 TestFlagger(); 00249 00250 // destructor 00251 ~TestFlagger(); 00252 00253 // reset everything 00254 void done(); 00255 00256 // configure the tool, open the MS 00257 bool open(String msname, Double ntime); 00258 00259 // parse the data selection 00260 bool selectData(Record selrec); 00261 bool selectData(String field, String spw, String array, String feed, String scan, 00262 String antenna, String uvrange, String timerange, 00263 String correlation, String intent, String observation=""); 00264 00265 // parse the parameters of the agent 00266 bool parseAgentParameters(Record agent_params); 00267 String getExpressionFunction(String expression); 00268 bool isExpressionPolarizationAll(String expression); 00269 00270 // initialize the agents list 00271 bool initAgents(); 00272 00273 // Run the tool and write the flags to the MS 00274 Record run(Bool writeflags, Bool sequential=true); 00275 00276 // Flag backup methods 00277 bool printFlagSelections(); 00278 bool saveFlagVersion(String versionname, String comment, String merge); 00279 bool restoreFlagVersion(Vector<String> versionname, String merge); 00280 bool deleteFlagVersion(Vector<String> versionname); 00281 bool getFlagVersionList(Vector<String> &verlist); 00282 00283 // Agent's specific parsing methods (for convenience only) 00284 // Parse parameters for manual 00285 bool parseManualParameters(String field, String spw, String array, String feed, String scan, 00286 String antenna, String uvrange, String timerange,String correlation, 00287 String intent, String observation, Bool autocorr, Bool apply); 00288 00289 // Parse parameters for clip 00290 bool parseClipParameters(String field, String spw, String array, String feed, String scan, 00291 String antenna, String uvrange, String timerange,String correlation, 00292 String intent, String observation, String datacolumn, 00293 Vector<Double> clipminmax, Bool clipoutside, Bool channelavg, 00294 Bool clipzeros, Bool apply); 00295 00296 // Parse parameters for quack 00297 bool parseQuackParameters(String field, String spw, String array, String feed, String scan, 00298 String antenna, String uvrange, String timerange,String correlation, 00299 String intent, String observation, String quackmode, Double quackinterval, 00300 Bool quackincrement, Bool apply); 00301 00302 // Parse parameters for elevation 00303 bool parseElevationParameters(String field, String spw, String array, String feed, 00304 String scan, String antenna, String uvrange, String timerange, String correlation, 00305 String intent, String observation, Double lowerlimit, Double upperlimit, 00306 Bool apply); 00307 00308 00309 // Parse parameters for tfcrop 00310 bool parseTfcropParameters(String field, String spw, String array, String feed, 00311 String scan, String antenna, String uvrange, String timerange, String correlation, 00312 String intent, String observation, Double ntime, Bool combinescans, 00313 String datacolumn, Double timecutoff, Double freqcutoff, String timefit, 00314 String freqfit, Int maxnpieces, String flagdimension, String usewindowstats, 00315 Int halfwin, Bool apply); 00316 00317 // Parse parameters for extend 00318 bool parseExtendParameters(String field, String spw, String array, String feed, 00319 String scan, String antenna, String uvrange, String timerange, String correlation, 00320 String intent, String observation, Double ntime, Bool combinescans, 00321 Bool extendpols, Double growtime, Double growfreq, Bool growaround, 00322 Bool flagneartime, Bool flagnearfreq, Bool apply); 00323 00324 // Parse parameters for summary 00325 bool parseSummaryParameters(String field, String spw, String array, String feed, 00326 String scan, String antenna, String uvrange, String timerange, String correlation, 00327 String intent, String observation, Bool spwchan, Bool spwcorr, Bool basecnt); 00328 00329 private: 00330 00331 TestFlagger(const TestFlagger &) {}; 00332 00333 TestFlagger& operator=(const TestFlagger &) {return *this;}; 00334 00335 // Maximum between two numbers 00336 void getMax(Double value); 00337 00338 // Check if mode is valid against a list of known modes 00339 bool isModeValid(String mode); 00340 00341 // Sink used to store history 00342 LogSink logSink_p; 00343 00344 // Debug message flag 00345 static const bool dbg; 00346 00347 // Store the temporary maximum value 00348 Double max_p; 00349 00350 // Helper members 00351 Bool timeset_p; 00352 Bool iterset_p; 00353 00354 }; 00355 00356 00357 } //# NAMESPACE CASA - END 00358 00359 #endif 00360