Line data Source code
1 :
2 : //# RFFlagCube.h: this defines RFFlagCube
3 : //# Copyright (C) 2000,2001,2002
4 : //# Associated Universities, Inc. Washington DC, USA.
5 : //#
6 : //# This library is free software; you can redistribute it and/or modify it
7 : //# under the terms of the GNU Library General Public License as published by
8 : //# the Free Software Foundation; either version 2 of the License, or (at your
9 : //# option) any later version.
10 : //#
11 : //# This library is distributed in the hope that it will be useful, but WITHOUT
12 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14 : //# License for more details.
15 : //#
16 : //# You should have received a copy of the GNU Library General Public License
17 : //# along with this library; if not, write to the Free Software Foundation,
18 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
19 : //#
20 : //# Correspondence concerning AIPS++ should be addressed as follows:
21 : //# Internet email: casa-feedback@nrao.edu.
22 : //# Postal address: AIPS++ Project Office
23 : //# National Radio Astronomy Observatory
24 : //# 520 Edgemont Road
25 : //# Charlottesville, VA 22903-2475 USA
26 : //#
27 : //# $Id$
28 : #ifndef FLAGGING_RFFLAGCUBE_H
29 : #define FLAGGING_RFFLAGCUBE_H
30 :
31 : //#include <flagging/Flagging/RedFlagger.h>
32 : #include <flagging/Flagging/RFCubeLattice.h>
33 : #include <flagging/Flagging/RFChunkStats.h>
34 : #include <casacore/casa/Arrays/ArrayLogical.h>
35 : #include <casacore/casa/Arrays/LogiMatrix.h>
36 : #include <casacore/casa/Arrays/LogiVector.h>
37 : #include <casacore/casa/Logging/LogIO.h>
38 : #include <stdexcept>
39 :
40 : namespace casa { //# NAMESPACE CASA - BEGIN
41 :
42 : typedef RFCubeLatticeIterator<RFlagWord> FlagCubeIterator;
43 :
44 : // special row flag masks. RowFlagged for flagged rows,
45 : // RowAbsent for absent rows
46 : const RFlagWord RowFlagged=1,RowAbsent=2;
47 :
48 : // casacore::Function for working with bitmasks. Does a bitwise-AND
49 : // on every element, returns true if !=0 or false if ==0
50 : template<class T> casacore::Array<T> operator & ( const casacore::Array<T> &,const T &);
51 : // returns a casacore::LogicalArray corresponding to (ARR&MASK)!=0
52 : template<class T> casacore::LogicalArray maskBits ( const casacore::Array<T> &,const T &);
53 :
54 : // <summary>
55 : // RFFlagCube: a cube of flags
56 : // </summary>
57 :
58 : // <use visibility=local>
59 :
60 : // <reviewed reviewer="" date="" tests="" demos="">
61 : // </reviewed>
62 :
63 : // <prerequisite>
64 : // <li> RFCubeLattice
65 : // </prerequisite>
66 : //
67 : // <synopsis>
68 : // RFFlagCube implements an [NCHAN,NIFR,NTIME] cube of flags, stored in
69 : // a casacore::TempLattice that is iterated alog the TIME axis. One static
70 : // (i.e. global) cube is used to hold the actual flags. Individual
71 : // instances (instantiated by flagging agents) have individual unique
72 : // bitmasks and, possibly, individual iterators.
73 : //
74 : // It was/is a design mistake to use a global/static buffer to hold the
75 : // shared flags. Instead, every agent should point to the unique dynamically
76 : // allocated buffer.
77 : // </synopsis>
78 : //
79 : // <example>
80 : // </example>
81 : //
82 : // <motivation>
83 : // </motivation>
84 : //
85 : // <todo asof="2001/04/16">
86 : // <li> add this feature
87 : // <li> fix this bug
88 : // <li> start discussion of this possible extension
89 : // </todo>
90 :
91 : class RFFlagCube : public FlaggerEnums
92 : {
93 : public:
94 : // default log sink
95 : static casacore::LogIO default_sink;
96 :
97 : // constructor
98 : RFFlagCube ( RFChunkStats &ch,casacore::Bool ignore=false,casacore::Bool reset=false,casacore::LogIO &os=default_sink );
99 : ~RFFlagCube ();
100 :
101 : // returns reference to logsink
102 : casacore::LogIO & logSink ();
103 :
104 : // returns estimated size of flag cube for a given chunk.
105 : static casacore::uInt estimateMemoryUse ( const RFChunkStats &ch );
106 :
107 : // creates flag cube for current chunk. name is name of agent.
108 : // nAgent is total number of agents
109 : void init ( RFlagWord polmsk, casacore::uInt nAgent, bool is_selector, const casacore::String &name = "" );
110 :
111 : // cleans up at end of chunk
112 : void cleanup ();
113 :
114 : // returns summary of stats in text form
115 : casacore::String getSummary ();
116 :
117 : // prints flagging stats to stderr
118 : void printStats ();
119 :
120 : // resets at start of pass
121 : void reset ();
122 :
123 : // advances global flag iterator to time slot it (if required), sets
124 : // the flag cursor from the iterator (see below). If getflags is true,
125 : // also calls getDataFlags().
126 : void advance ( casacore::uInt it,casacore::Bool getFlags=false );
127 :
128 : // fills global flag lattice with apriori flags from a VisBuffer (if required)
129 : void getMSFlags (casacore::uInt it);
130 :
131 : // transfers all flags from lattice into VisBuffer
132 : void setMSFlags (casacore::uInt itime);
133 :
134 : // creates a custom iterator
135 : FlagCubeIterator newCustomIter ();
136 :
137 : // Returns full flag matrix (i.e. cursor of global iterator)
138 : const FlagMatrix & flagMatrix ();
139 :
140 : // sets or clears a flag at the given flag cursor
141 : casacore::Bool setFlag ( casacore::uInt ich,casacore::uInt ifr,FlagCubeIterator &iter );
142 : casacore::Bool clearFlag ( casacore::uInt ich,casacore::uInt ifr,FlagCubeIterator &iter );
143 :
144 : // Gets full flag word at the given flag cursor.
145 : RFlagWord getFlag ( casacore::uInt ich,casacore::uInt ifr,FlagCubeIterator &iter );
146 :
147 : // Versions of above that use global flag cursor
148 : casacore::Bool setFlag ( casacore::uInt ich,casacore::uInt ifr );
149 : casacore::Bool clearFlag ( casacore::uInt ich,casacore::uInt ifr );
150 : RFlagWord getFlag ( casacore::uInt ich,casacore::uInt ifr );
151 :
152 : // the preFlagged() function uses the corr-mask to tell if any of this
153 : // agent's correlations are pre-flagged. Uses internal cursor.
154 : casacore::Bool preFlagged ( casacore::uInt ich,casacore::uInt ifr );
155 :
156 : // The anyFlagged() uses the corr-flagmask to tell if any of my
157 : // correlations are flagged either by any agent or pre-flagged
158 : // Uses internal cursor.
159 : casacore::Bool anyFlagged ( casacore::uInt ich,casacore::uInt ifr );
160 :
161 : // Sets or clears a row flag
162 : casacore::Bool setRowFlag ( casacore::uInt ifr,casacore::uInt itime );
163 : casacore::Bool clearRowFlag ( casacore::uInt ifr,casacore::uInt itime );
164 :
165 : // Gets full row flag word
166 : RFlagWord getRowFlag ( casacore::uInt ifr,casacore::uInt itime );
167 :
168 : // tells if a row is pre-flagged in the casacore::MS (or does not exist)
169 : casacore::Bool rowPreFlagged ( casacore::uInt ifr,casacore::uInt itime );
170 :
171 : // tells if a row is flagged by any agent
172 : casacore::Bool rowAgentFlagged ( casacore::uInt ifr,casacore::uInt itime );
173 :
174 : // preFlagged OR agentFlagged
175 : casacore::Bool rowFlagged ( casacore::uInt ifr,casacore::uInt itime );
176 :
177 : // returns reference to internal iterator
178 : FlagCubeIterator & iterator ();
179 :
180 : // returns flag mask for this agent
181 : RFlagWord flagMask ();
182 :
183 : // returns correlations mask for this agent
184 : RFlagWord corrMask ();
185 :
186 : // returns the checked-correlations mask for this agent
187 : // (=0 for RESET/IGNORE policies, or =corrMask() for HONOR policy).
188 : RFlagWord checkCorrMask ();
189 :
190 : // returns mask of all correlations
191 : static RFlagWord fullCorrMask ();
192 :
193 : // returns the number of instances of the flag cube
194 : static casacore::Int numInstances ();
195 :
196 : // sets the maximum memory usage for the flag cube
197 : static void setMaxMem ( casacore::Int maxmem );
198 : // returns the current maximum memory usage
199 : static int getMaxMem ();
200 :
201 : private:
202 : RFChunkStats &chunk; // chunk
203 :
204 : bool kiss; // do things simpler (faster) if there is nothing but RFAselector agents
205 : bool kiss_flagrow;
206 :
207 : static casacore::Cube<casacore::Bool> in_flags;
208 : static int in_flags_time; //time stamp that in_flags has reached
209 : static bool in_flags_flushed; // do we need to write the flags back for this time stamp?
210 :
211 : // shortcut to RFChunkStats::num
212 0 : casacore::uInt num ( StatEnums which ) { return chunk.num(which); }
213 :
214 : static RFCubeLattice<RFlagWord> flag; // global flag lattice
215 : static FlagMatrix flagrow; // (nIfr,nTime) matrix of row flags
216 : static casacore::Matrix<std::vector<bool> > flagrow_kiss;
217 : static casacore::Int pos_get_flag,pos_set_flag;
218 :
219 : static casacore::Bool reset_preflags; // flag: RESET policy specified for at least one instance
220 :
221 : static casacore::uInt npol,nchan;
222 :
223 : // Flag mask used by this instance. Each instance has a unique 1-bit mask.
224 : // This is assigned automatically in the constructor, by updating the
225 : // instance count and the nextmask member.
226 : // Note that the low N bits of a mask are assigned to pre-flags (one per
227 : // each correlation in the casacore::MS); so the agents start at bit N+1.
228 : RFlagWord flagmask, // flagmask of this instance
229 : corrmask, // corrmask of this instance (corrs used/flagged by it)
230 : check_corrmask, // mask checked by preFlagged() & co. Set to 0 for
231 : // RESET or IGNORE policy, or to corrmask for HONOR
232 : check_rowmask, // same for row flags: 0 or RowFlagged
233 : my_corrflagmask; // see above
234 : unsigned long flagmask_kiss; // represents a bitmask with only bit number <n> set where
235 : // <n> is the value of this variable
236 : static casacore::Int agent_count; // # of agents instantiated
237 : static RFlagWord base_flagmask, // flagmask of first agent instance
238 : full_corrmask; // bitmask for all correlations in casacore::MS (low N bits)
239 :
240 : // corr_flagmask is a mapping from corrmasks into masks of agents that flag the
241 : // given corrmask
242 : static casacore::Vector<RFlagWord> corr_flagmask;
243 :
244 : // log sink
245 : casacore::LogIO os;
246 :
247 : // pre-flag policy (can be set on a per-instance basis)
248 : PreFlagPolicy pfpolicy;
249 :
250 : // flagging stats for this instance
251 : casacore::uInt tot_fl_raised,fl_raised,fl_cleared,
252 : tot_row_fl_raised,row_fl_raised,row_fl_cleared;
253 :
254 : // local flag cursor used by this instance (setFlag and clearFlag).
255 : // Normally, set to flag.cursor() in advance(), but can be overridden
256 : // by setFlagCursor();
257 : FlagMatrix * flag_curs;
258 : casacore::uInt flag_itime;
259 :
260 : // number of instances in use
261 : static casacore::Int num_inst;
262 : };
263 :
264 0 : inline RFlagWord RFFlagCube::flagMask ()
265 : {
266 0 : if (kiss) {
267 0 : throw std::logic_error("Cannot do this in kiss mode (program bug, please report)");
268 : }
269 0 : return flagmask;
270 : }
271 :
272 : inline RFlagWord RFFlagCube::corrMask ()
273 : {
274 : return corrmask;
275 : }
276 :
277 : inline RFlagWord RFFlagCube::checkCorrMask ()
278 : { return check_corrmask; }
279 :
280 0 : inline RFlagWord RFFlagCube::fullCorrMask ()
281 0 : { return full_corrmask; }
282 :
283 0 : inline RFlagWord RFFlagCube::getFlag ( casacore::uInt ich,casacore::uInt ifr,FlagCubeIterator &iter )
284 : {
285 0 : if (kiss) {
286 : /* Create the bitmap (integer) from the correlation flags
287 : relevant for this agent */
288 0 : RFlagWord f = 0;
289 0 : casacore::uInt c = 1;
290 :
291 0 : for (casacore::uInt icorr = 0; icorr < num(CORR); icorr++, c<<=1) {
292 0 : if ((c & corrmask) &&
293 0 : in_flags(icorr, ich, ifr)) {
294 0 : f |= c;
295 : }
296 : }
297 0 : return f;
298 : }
299 : else {
300 0 : return (iter)(ich,ifr);
301 : }
302 : }
303 :
304 0 : inline casacore::Bool RFFlagCube::setFlag ( casacore::uInt ich,casacore::uInt ifr )
305 0 : { return setFlag(ich,ifr,flag.iterator()); }
306 :
307 0 : inline casacore::Bool RFFlagCube::clearFlag ( casacore::uInt ich,casacore::uInt ifr )
308 0 : { return clearFlag(ich,ifr,flag.iterator()); }
309 :
310 0 : inline RFlagWord RFFlagCube::getFlag ( casacore::uInt ich,casacore::uInt ifr )
311 0 : { return getFlag(ich,ifr,flag.iterator()); }
312 :
313 0 : inline FlagCubeIterator RFFlagCube::newCustomIter ()
314 0 : { return flag.newIter(); }
315 :
316 : inline const FlagMatrix & RFFlagCube::flagMatrix ()
317 : { return *flag_curs; }
318 :
319 0 : inline casacore::Bool RFFlagCube::preFlagged ( casacore::uInt ich,casacore::uInt ifr )
320 0 : { return (getFlag(ich,ifr)&check_corrmask) != 0; }
321 :
322 0 : inline casacore::Bool RFFlagCube::anyFlagged ( casacore::uInt ich,casacore::uInt ifr )
323 : {
324 0 : if (kiss) {
325 0 : throw std::logic_error("Cannot do this in kiss mode (program bug, please report)");
326 : }
327 0 : return (getFlag(ich,ifr)&(check_corrmask|my_corrflagmask)) != 0;
328 : }
329 :
330 : // Gets full row flag word
331 0 : inline RFlagWord RFFlagCube::getRowFlag ( casacore::uInt ifr,casacore::uInt itime )
332 : {
333 0 : if (kiss) {
334 0 : throw std::logic_error("Cannot do this in kiss mode (program bug, please report)");
335 : }
336 0 : return flagrow(ifr,itime);
337 : }
338 :
339 : // tells if a row is pre-flagged in the casacore::MS (or does not exist)
340 0 : inline casacore::Bool RFFlagCube::rowPreFlagged ( casacore::uInt ifr,casacore::uInt itime )
341 0 : { return getRowFlag(ifr,itime)&check_rowmask; }
342 :
343 : // tells if a row is flagged by any agent
344 : inline casacore::Bool RFFlagCube::rowAgentFlagged ( casacore::uInt ifr,casacore::uInt itime )
345 : { return getRowFlag(ifr,itime)&~(RowFlagged|RowAbsent); }
346 :
347 : // preFlagged OR agentFlagged
348 0 : inline casacore::Bool RFFlagCube::rowFlagged ( casacore::uInt ifr,casacore::uInt itime )
349 0 : { return getRowFlag(ifr,itime)&(check_rowmask?~0:~RowFlagged); }
350 :
351 0 : inline FlagCubeIterator & RFFlagCube::iterator ()
352 0 : { return flag.iterator(); }
353 :
354 : inline int RFFlagCube::numInstances ()
355 : { return num_inst; }
356 :
357 0 : inline casacore::LogIO & RFFlagCube::logSink ()
358 0 : { return os; }
359 :
360 :
361 : } //# NAMESPACE CASA - END
362 :
363 : #ifndef AIPS_NO_TEMPLATE_SRC
364 : #include <flagging/Flagging/RFFlagCube.tcc>
365 : #endif //# AIPS_NO_TEMPLATE_SRC
366 : #endif
|