RFFlagCube.h

Go to the documentation of this file.
00001 
00002 //# RFFlagCube.h: this defines RFFlagCube
00003 //# Copyright (C) 2000,2001,2002
00004 //# Associated Universities, Inc. Washington DC, USA.
00005 //#
00006 //# This library is free software; you can redistribute it and/or modify it
00007 //# under the terms of the GNU Library General Public License as published by
00008 //# the Free Software Foundation; either version 2 of the License, or (at your
00009 //# option) any later version.
00010 //#
00011 //# This library is distributed in the hope that it will be useful, but WITHOUT
00012 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00013 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00014 //# License for more details.
00015 //#
00016 //# You should have received a copy of the GNU Library General Public License
00017 //# along with this library; if not, write to the Free Software Foundation,
00018 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00019 //#
00020 //# Correspondence concerning AIPS++ should be addressed as follows:
00021 //#        Internet email: aips2-request@nrao.edu.
00022 //#        Postal address: AIPS++ Project Office
00023 //#                        National Radio Astronomy Observatory
00024 //#                        520 Edgemont Road
00025 //#                        Charlottesville, VA 22903-2475 USA
00026 //#
00027 //# $Id$
00028 #ifndef FLAGGING_RFFLAGCUBE_H
00029 #define FLAGGING_RFFLAGCUBE_H
00030     
00031 //#include <flagging/Flagging/RedFlagger.h>
00032 #include <flagging/Flagging/RFCubeLattice.h>
00033 #include <flagging/Flagging/RFChunkStats.h>
00034 #include <casa/Arrays/ArrayLogical.h>
00035 #include <casa/Arrays/LogiMatrix.h>
00036 #include <casa/Arrays/LogiVector.h>
00037 #include <casa/Logging/LogIO.h>
00038 #include <stdexcept>
00039 
00040 namespace casa { //# NAMESPACE CASA - BEGIN
00041 
00042 typedef RFCubeLatticeIterator<RFlagWord> FlagCubeIterator;
00043 
00044 // special row flag masks. RowFlagged for flagged rows, 
00045 // RowAbsent for absent rows
00046 const RFlagWord RowFlagged=1,RowAbsent=2;
00047 
00048 // Function for working with bitmasks. Does a bitwise-AND
00049 // on every element, returns True if !=0 or False if ==0
00050 template<class T> Array<T> operator & ( const Array<T> &,const T &);
00051 // returns a LogicalArray corresponding to (ARR&MASK)!=0
00052 template<class T> LogicalArray  maskBits  ( const Array<T> &,const T &);
00053 
00054 // <summary>
00055 // RFFlagCube: a cube of flags
00056 // </summary>
00057 
00058 // <use visibility=local>
00059 
00060 // <reviewed reviewer="" date="" tests="" demos="">
00061 // </reviewed>
00062 
00063 // <prerequisite>
00064 //   <li> RFCubeLattice
00065 // </prerequisite>
00066 //
00067 // <synopsis>
00068 // RFFlagCube implements an [NCHAN,NIFR,NTIME] cube of flags, stored in
00069 // a TempLattice that is iterated alog the TIME axis.  One static
00070 // (i.e. global) cube is used to hold the actual flags. Individual
00071 // instances (instantiated by flagging agents) have individual unique
00072 // bitmasks and, possibly, individual iterators.
00073 //
00074 // It was/is a design mistake to use a global/static buffer to hold the
00075 // shared flags. Instead, every agent should point to the unique dynamically
00076 // allocated buffer. 
00077 // </synopsis>
00078 //
00079 // <example>
00080 // </example>
00081 //
00082 // <motivation>
00083 // </motivation>
00084 //
00085 // <todo asof="2001/04/16">
00086 //   <li> add this feature
00087 //   <li> fix this bug
00088 //   <li> start discussion of this possible extension
00089 // </todo>
00090 
00091 class RFFlagCube : public FlaggerEnums
00092 {
00093 public:
00094   // default log sink
00095   static LogIO default_sink;
00096     
00097   // constructor
00098   RFFlagCube ( RFChunkStats &ch,Bool ignore=False,Bool reset=False,LogIO &os=default_sink );
00099   ~RFFlagCube ();
00100 
00101   // returns reference to logsink
00102   LogIO & logSink ();
00103 
00104   // returns estimated size of flag cube for a given chunk.
00105   static uInt estimateMemoryUse ( const RFChunkStats &ch );
00106 
00107   // creates flag cube for current chunk. name is name of agent.
00108   // nAgent is total number of agents
00109   void init ( RFlagWord polmsk, uInt nAgent, bool is_selector, const String &name = "" );
00110 
00111   // cleans up at end of chunk
00112   void cleanup ();
00113 
00114   // returns summary of stats in text form
00115   String getSummary ();
00116 
00117   // prints flagging stats to stderr
00118   void printStats ();
00119 
00120   // resets at start of pass
00121   void reset ();
00122 
00123   // advances global flag iterator to time slot it (if required), sets
00124   // the flag cursor from the iterator (see below). If getflags is true,
00125   // also calls getDataFlags().
00126   void advance   ( uInt it,Bool getFlags=False );
00127 
00128   // fills global flag lattice with apriori flags from a VisBuffer (if required)
00129   void getMSFlags  (uInt it);
00130 
00131   // transfers all flags from lattice into VisBuffer
00132   void setMSFlags  (uInt itime);
00133 
00134   // creates a custom iterator
00135   FlagCubeIterator newCustomIter ();
00136 
00137   // Returns full flag matrix (i.e. cursor of global iterator)
00138   const FlagMatrix & flagMatrix ();
00139   
00140   // sets or clears a flag at the given flag cursor
00141   Bool setFlag      ( uInt ich,uInt ifr,FlagCubeIterator &iter );
00142   Bool clearFlag    ( uInt ich,uInt ifr,FlagCubeIterator &iter );
00143 
00144   // Gets full flag word at the given flag cursor.
00145   RFlagWord getFlag ( uInt ich,uInt ifr,FlagCubeIterator &iter );
00146 
00147   // Versions of above that use global flag cursor
00148   Bool setFlag      ( uInt ich,uInt ifr );
00149   Bool clearFlag    ( uInt ich,uInt ifr );
00150   RFlagWord getFlag ( uInt ich,uInt ifr );
00151   
00152   // the preFlagged() function uses the corr-mask to tell if any of this
00153   // agent's correlations are pre-flagged. Uses internal cursor.
00154   Bool preFlagged   ( uInt ich,uInt ifr );
00155 
00156   // The anyFlagged() uses the corr-flagmask to tell if any of my
00157   // correlations are flagged either by any agent or pre-flagged
00158   // Uses internal cursor.
00159   Bool anyFlagged   ( uInt ich,uInt ifr );
00160   
00161   // Sets or clears a row flag
00162   Bool setRowFlag      ( uInt ifr,uInt itime );
00163   Bool clearRowFlag    ( uInt ifr,uInt itime );
00164 
00165   // Gets full row flag word
00166   RFlagWord getRowFlag ( uInt ifr,uInt itime );
00167   
00168   // tells if a row is pre-flagged in the MS (or does not exist)
00169   Bool rowPreFlagged   ( uInt ifr,uInt itime );  
00170 
00171   // tells if a row is flagged by any agent
00172   Bool rowAgentFlagged ( uInt ifr,uInt itime );  
00173 
00174   // preFlagged OR agentFlagged  
00175   Bool rowFlagged      ( uInt ifr,uInt itime );
00176   
00177   // returns reference to internal iterator
00178   FlagCubeIterator &  iterator ();
00179   
00180   // returns flag mask for this agent
00181   RFlagWord flagMask ();      
00182 
00183   // returns correlations mask for this agent
00184   RFlagWord corrMask ();
00185 
00186   // returns the checked-correlations mask for this agent
00187   // (=0 for RESET/IGNORE policies, or =corrMask() for HONOR policy).
00188   RFlagWord checkCorrMask ();
00189 
00190   // returns mask of all correlations
00191   static RFlagWord fullCorrMask ();
00192 
00193   // returns the number of instances of the flag cube
00194   static Int numInstances ();
00195 
00196   // sets the maximum memory usage for the flag cube  
00197   static void setMaxMem ( Int maxmem );
00198   // returns the current maximum memory usage
00199   static int  getMaxMem ();
00200       
00201  private:
00202   RFChunkStats &chunk;                  // chunk
00203 
00204   bool kiss;  // do things simpler (faster) if there is nothing but RFAselector agents
00205   bool kiss_flagrow;
00206 
00207   static Cube<Bool> in_flags;
00208   static int in_flags_time;  //time stamp that in_flags has reached
00209   static bool in_flags_flushed; // do we need to write the flags back for this time stamp?
00210 
00211   // shortcut to RFChunkStats::num
00212   uInt num ( StatEnums which ) { return chunk.num(which); }
00213       
00214   static RFCubeLattice<RFlagWord> flag; // global flag lattice
00215   static FlagMatrix flagrow;             // (nIfr,nTime) matrix of row flags
00216   static Matrix<std::vector<bool> > flagrow_kiss;
00217   static Int pos_get_flag,pos_set_flag; 
00218 
00219   static Bool reset_preflags; // flag: RESET policy specified for at least one instance
00220   
00221   static uInt npol,nchan;
00222   
00223   // Flag mask used by this instance. Each instance has a unique 1-bit mask.
00224   // This is assigned automatically in the constructor, by updating the 
00225   // instance count and the nextmask member.
00226   // Note that the low N bits of a mask are assigned to pre-flags (one per
00227   // each correlation in the MS); so the agents start at bit N+1.
00228   RFlagWord flagmask,       // flagmask of this instance
00229     corrmask,        // corrmask of this instance (corrs used/flagged by it)
00230     check_corrmask,  // mask checked by preFlagged() & co. Set to 0 for
00231     // RESET or IGNORE policy, or to corrmask for HONOR
00232     check_rowmask,   // same for row flags: 0 or RowFlagged
00233     my_corrflagmask; // see above
00234   unsigned long flagmask_kiss; // represents a bitmask with only bit number <n> set where 
00235                           // <n> is the value of this variable
00236   static Int agent_count;    // # of agents instantiated
00237   static RFlagWord base_flagmask, // flagmask of first agent instance
00238     full_corrmask;          // bitmask for all correlations in MS (low N bits)
00239 
00240   // corr_flagmask is a mapping from corrmasks into masks of agents that flag the
00241   // given corrmask
00242   static Vector<RFlagWord> corr_flagmask;
00243   
00244   // log sink
00245   LogIO os;
00246 
00247   // pre-flag policy (can be set on a per-instance basis)
00248   PreFlagPolicy pfpolicy;
00249   
00250   // flagging stats for this instance
00251   uInt tot_fl_raised,fl_raised,fl_cleared,
00252     tot_row_fl_raised,row_fl_raised,row_fl_cleared;
00253     
00254   // local flag cursor used by this instance (setFlag and clearFlag). 
00255   // Normally, set to flag.cursor() in advance(), but can be overridden
00256   // by setFlagCursor();
00257   FlagMatrix * flag_curs;
00258   uInt flag_itime;
00259   
00260   // number of instances in use
00261   static Int num_inst;
00262 };
00263 
00264 inline RFlagWord RFFlagCube::flagMask ()
00265   { 
00266      if (kiss) {
00267        throw std::logic_error("Cannot do this in kiss mode (program bug, please report)");
00268      }
00269      return flagmask; 
00270   }
00271  
00272 inline RFlagWord RFFlagCube::corrMask ()
00273    { 
00274      return corrmask; 
00275    }
00276 
00277 inline RFlagWord RFFlagCube::checkCorrMask ()
00278    { return check_corrmask; }
00279 
00280 inline RFlagWord RFFlagCube::fullCorrMask ()
00281    { return full_corrmask; }
00282 
00283 inline RFlagWord RFFlagCube::getFlag ( uInt ich,uInt ifr,FlagCubeIterator &iter )
00284    { 
00285      if (kiss) {
00286        /* Create the bitmap (integer) from the correlation flags
00287           relevant for this agent */
00288        RFlagWord f = 0;
00289        uInt c = 1;
00290 
00291        for (uInt icorr = 0; icorr < num(CORR); icorr++, c<<=1) {
00292          if ((c & corrmask) && 
00293              in_flags(icorr, ich, ifr)) {
00294            f |= c;
00295          }
00296        }
00297        return f;
00298      }
00299      else {
00300        return (iter)(ich,ifr); 
00301      }
00302    }
00303 
00304 inline Bool RFFlagCube::setFlag ( uInt ich,uInt ifr ) 
00305    { return setFlag(ich,ifr,flag.iterator()); } 
00306 
00307 inline Bool RFFlagCube::clearFlag ( uInt ich,uInt ifr ) 
00308    { return clearFlag(ich,ifr,flag.iterator()); } 
00309 
00310 inline RFlagWord RFFlagCube::getFlag ( uInt ich,uInt ifr ) 
00311    { return getFlag(ich,ifr,flag.iterator()); } 
00312 
00313 inline FlagCubeIterator RFFlagCube::newCustomIter ()
00314    { return flag.newIter(); }
00315 
00316 inline const FlagMatrix & RFFlagCube::flagMatrix ()
00317    { return *flag_curs; }
00318 
00319 inline Bool RFFlagCube::preFlagged ( uInt ich,uInt ifr )
00320    { return (getFlag(ich,ifr)&check_corrmask) != 0; }
00321 
00322 inline Bool RFFlagCube::anyFlagged ( uInt ich,uInt ifr )
00323    { 
00324      if (kiss) {
00325        throw std::logic_error("Cannot do this in kiss mode (program bug, please report)");
00326      }
00327      return (getFlag(ich,ifr)&(check_corrmask|my_corrflagmask)) != 0;
00328    }
00329 
00330 // Gets full row flag word
00331 inline RFlagWord RFFlagCube::getRowFlag ( uInt ifr,uInt itime )
00332   {
00333     if (kiss) {
00334       throw std::logic_error("Cannot do this in kiss mode (program bug, please report)");
00335     }
00336     return flagrow(ifr,itime); 
00337   }
00338 
00339 // tells if a row is pre-flagged in the MS (or does not exist)
00340 inline Bool RFFlagCube::rowPreFlagged   ( uInt ifr,uInt itime )
00341    { return getRowFlag(ifr,itime)&check_rowmask; }
00342 
00343 // tells if a row is flagged by any agent
00344 inline Bool RFFlagCube::rowAgentFlagged ( uInt ifr,uInt itime )
00345    { return getRowFlag(ifr,itime)&~(RowFlagged|RowAbsent); }
00346 
00347 // preFlagged OR agentFlagged  
00348 inline Bool RFFlagCube::rowFlagged      ( uInt ifr,uInt itime )
00349    { return getRowFlag(ifr,itime)&(check_rowmask?~0:~RowFlagged); }
00350 
00351 inline FlagCubeIterator & RFFlagCube::iterator ()
00352    { return flag.iterator(); }
00353 
00354 inline int RFFlagCube::numInstances ()
00355    { return num_inst; }
00356 
00357 inline LogIO & RFFlagCube::logSink ()
00358    { return os; }
00359 
00360 
00361 } //# NAMESPACE CASA - END
00362 
00363 #ifndef AIPS_NO_TEMPLATE_SRC
00364 #include <flagging/Flagging/RFFlagCube.tcc>
00365 #endif //# AIPS_NO_TEMPLATE_SRC
00366 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 31 Aug 2016 for casa by  doxygen 1.6.1