LCOV - code coverage report
Current view: top level - flagging/Flagging - RFFlagCube.cc (source / functions) Hit Total Coverage
Test: casa_coverage.info Lines: 0 284 0.0 %
Date: 2023-10-25 08:47:59 Functions: 0 15 0.0 %

          Line data    Source code
       1             : 
       2             : //# RFFlagCube.cc: 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: aips2-request@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             : #include <flagging/Flagging/RFFlagCube.h>
      29             : #include <casacore/casa/Exceptions/Error.h>
      30             : #include <msvis/MSVis/VisBuffer.h>
      31             : #include <casacore/casa/Arrays/ArrayLogical.h>
      32             : #include <casacore/casa/Arrays/ArrayMath.h>
      33             : #include <casacore/casa/Arrays/LogiVector.h>
      34             : #include <casacore/casa/Utilities/Regex.h>
      35             : #include <casacore/casa/OS/Time.h>
      36             : #include <casacore/casa/Quanta/MVTime.h>
      37             : #include <memory>
      38             : #include <stdio.h>
      39             : 
      40             : using namespace std;
      41             :         
      42             : using namespace casacore;
      43             : namespace casa { //# NAMESPACE CASA - BEGIN
      44             : 
      45             : const Bool dbg=false;
      46             : const Bool mdbg=false;
      47             : const Bool verbose=false;
      48             :         
      49             : RFCubeLattice<RFlagWord> RFFlagCube::flag; // global flag lattice
      50             : Cube<Bool> RFFlagCube::in_flags;  //global flag array (kiss mode)
      51             : int RFFlagCube::in_flags_time;
      52             : bool RFFlagCube::in_flags_flushed;
      53             : 
      54             : FlagMatrix RFFlagCube::flagrow;     // this data type supports only up to 32 agents (bad design)
      55             : Matrix<vector<bool> > RFFlagCube::flagrow_kiss;  // in kiss mode, support more agents
      56             : Int RFFlagCube::pos_get_flag=-1,RFFlagCube::pos_set_flag=-1;
      57             : 
      58             : RFlagWord RFFlagCube::base_flagmask=1,
      59             :           RFFlagCube::full_corrmask;
      60             : Int RFFlagCube::agent_count=0, RFFlagCube::num_inst=0;
      61             : Vector<RFlagWord> RFFlagCube::corr_flagmask;  
      62             : //This is a map from a set of correlations to a set of agents, i.e.
      63             : // which agents deal with any of the given correlations
      64             : 
      65             : Bool RFFlagCube::reset_preflags;
      66             :   LogIO RFFlagCube::default_sink(LogOrigin("Flagger","FlagCube"));
      67             : 
      68           0 : RFFlagCube::RFFlagCube ( RFChunkStats &ch,Bool ignore,Bool reset,LogIO &sink )
      69           0 :   : chunk(ch),os(sink)
      70             : {
      71           0 :   num_inst++;
      72           0 :   if( reset )
      73             :   {
      74           0 :     pfpolicy = FL_RESET;
      75             :     if(verbose) os<<"Existing flags will be reset\n"<<LogIO::POST;
      76             :   }
      77             :   else
      78             :   {
      79           0 :     if( ignore )
      80             :     {
      81           0 :       pfpolicy = FL_IGNORE;
      82             :       if(verbose) os<<"Existing flags will be ignored, but added to\n"<<LogIO::POST;
      83             :     }
      84             :     else
      85             :     {
      86           0 :       pfpolicy = FL_HONOR;
      87             :       if(verbose) os<<"Existing flags will be honored\n"<<LogIO::POST;
      88             :     }
      89             :   }
      90           0 : }
      91             : 
      92           0 : RFFlagCube::~RFFlagCube ()
      93             : {
      94           0 :     in_flags.resize(0, 0, 0);
      95           0 :     in_flags_time = -1;
      96           0 :     in_flags_flushed = false;
      97           0 :     num_inst--;
      98           0 :     if (num_inst == 0) {
      99           0 :         cleanup();
     100             :     }
     101           0 : }
     102             : 
     103           0 : uInt RFFlagCube::estimateMemoryUse ( const RFChunkStats &ch )
     104             : {
     105             :     return
     106           0 :         ch.num(CHAN) * ch.num(IFR) * ch.num(TIME) * 
     107           0 :         sizeof(RFlagWord) / (1024*1024) + 1;
     108             :     
     109             : }
     110             : 
     111             : // creates flag cube for a given visibility chunk
     112           0 : void RFFlagCube::init( RFlagWord corrmsk, uInt nAgent, bool only_selector, const String &name) 
     113             : {
     114             :     if (dbg) cout << "name=" << name << endl;
     115             : 
     116           0 :     kiss = only_selector; /* Use a Cube<Bool> instead of the
     117             :                              expensive flag lattice in this case */
     118             : 
     119             :     /* Using the 'flagrow_kiss' buffer instead of 'flagrow'
     120             :        allows to have more than 32 agents+correlations, but at a
     121             :        (small, maybe insignificant 1m38s vs 1m32s) runtime cost. 
     122             :        Therefore use it only when necessary.
     123             :      */
     124           0 :     kiss_flagrow = (kiss && nAgent + num(CORR) + 1 > sizeof(RFlagWord)*8);
     125             : 
     126             :     // In order to use flagrow_kiss whenever possible (for testing), define here kiss_flagrow = kiss;
     127             : 
     128             :     // setup some masks
     129           0 :     corrmask = corrmsk;
     130           0 :     check_corrmask = (pfpolicy == FL_HONOR) ? corrmsk : 0;
     131           0 :     check_rowmask  = (pfpolicy == FL_HONOR) ? RowFlagged : 0;
     132             : 
     133             :     // clear stats  
     134           0 :     tot_fl_raised=tot_row_fl_raised=fl_raised=fl_cleared=
     135           0 :         row_fl_raised=row_fl_cleared=0;
     136             :     
     137             :     // init flag cube if it is empty
     138           0 :     if ( !flag.shape().nelements() ) {
     139             :     
     140           0 :         reset_preflags=false;
     141             : 
     142             :         // setup correlation masks. The first NCORR bits of the flag word
     143             :         // are used to store the apriori flags. Basemask is the first bitmask
     144             :         // actually used for flagging
     145           0 :         base_flagmask = num(CORR) >= 2 ? 1 << num(CORR) : 4;  // is this a bug, should base_flagmask be 2 if num(CORR) is 1?
     146             : 
     147             :         // full_corrmask is the mask of all correlations flagged
     148           0 :         full_corrmask = (1<<num(CORR))-1;
     149             : 
     150           0 :         if (!kiss) {
     151             :             // init empty flag lattice
     152             :             // initial state is all pre-flags set; we'll clear them as we go along
     153           0 :             flag.init(num(CHAN),num(IFR),num(TIME),num(CORR), nAgent, full_corrmask);
     154             : 
     155             :         }
     156             :         else {
     157             :             /* Set shape to a dummy value, 
     158             :                only so that we can later use flag.shape().nelements()
     159             :                in the if conditions.
     160             :             */
     161           0 :             flag.shape().resize(1);
     162           0 :             in_flags_time = -1;
     163             : 
     164             :         }
     165             : 
     166           0 :         if (!kiss_flagrow) {
     167             :             // allocate cube of row flags
     168           0 :             flagrow.resize(num(IFR),num(TIME));
     169           0 :             flagrow = RowFlagged|RowAbsent; // 0000 0011
     170             : 
     171           0 :             corr_flagmask.resize(1<<num(CORR));
     172           0 :             corr_flagmask = 0;
     173             :         }
     174             :         else {
     175           0 :             flagrow_kiss.resize(num(IFR),num(TIME));
     176           0 :             unsigned long val = (RowFlagged|RowAbsent);
     177           0 :             flagrow_kiss = bitvec_from_ulong( val, (num(CORR) >= 2 ? num(CORR) : 2) + nAgent );
     178             :         }
     179             : 
     180           0 :         pos_get_flag = pos_set_flag = -1;
     181             : 
     182             :         // reset instance counters 
     183           0 :         agent_count = 0;
     184             :     }
     185           0 :     if (kiss_flagrow) {
     186             :         // basebit plus agent_count
     187           0 :         flagmask_kiss = (num(CORR) >= 2 ? num(CORR) : 2) + agent_count;
     188             : 
     189             :     }
     190             :     else {
     191           0 :         flagmask = base_flagmask << agent_count;
     192             :         if (dbg) cout << "agent_count=" << agent_count 
     193             :                       << " base_flagmask=" << base_flagmask 
     194             :                       << " flagmask=" << (flagmask > UINT_MAX) << endl;
     195           0 :         if( !flagmask  )
     196           0 :             throw(AipsError("Too many flagging agents instantiated"));
     197             :     }
     198           0 :     agent_count++;
     199             : 
     200             :     // raise flag if any one instance has a RESET pre-flag policy
     201           0 :     if ( pfpolicy==FL_RESET )
     202           0 :         reset_preflags=true;
     203             : 
     204             :     // set bits in corr_flagmask, not used in kiss mode
     205           0 :     if (!kiss_flagrow) {
     206           0 :         for ( uInt cm=0; cm < corr_flagmask.nelements(); cm++ )
     207           0 :             if ( cm & corrmask )
     208           0 :                 corr_flagmask(cm) |= flagmask;
     209             :     }
     210             :   
     211             :     if(dbg) cout << "End of init. reset_preflags : " << reset_preflags << endl;
     212           0 : }
     213             : 
     214             : // deallocates flag cube
     215           0 : void RFFlagCube::cleanup ()
     216             : {
     217           0 :     if (flag.shape().nelements()) {
     218           0 :         flag.cleanup();
     219           0 :         if (!kiss_flagrow) {
     220           0 :             flagrow.resize(0,0);
     221           0 :             corr_flagmask.resize(0);
     222             :         }
     223             :         else {
     224           0 :             flagrow_kiss.resize(0, 0);
     225             :         }
     226           0 :         agent_count=0;
     227             :     }
     228           0 : }
     229             : 
     230           0 : void RFFlagCube::reset ()
     231             : {
     232           0 :     fl_raised=fl_cleared=row_fl_raised=row_fl_cleared=0;
     233           0 :     if (!kiss_flagrow) {
     234           0 :         my_corrflagmask = corr_flagmask(corrmask);
     235             :     }
     236           0 :     if (!kiss) {
     237           0 :         flag.reset();
     238             :     }
     239             : 
     240           0 :     return;
     241             : }
     242             : 
     243           0 : String RFFlagCube::getSummary ()
     244             : {
     245             :   char s[128];
     246           0 :   sprintf(s, "%d pixel flags, %d row flags",
     247             :           tot_fl_raised,tot_row_fl_raised);
     248           0 :   return s;
     249             : }
     250             : 
     251             : // prints flagging statistics
     252           0 : void RFFlagCube::printStats ()
     253             : {
     254           0 :   if( tot_fl_raised )
     255           0 :     dprintf(os,"%d pixel flags; %d raised this pass, %d cleared\n",
     256             :         tot_fl_raised,fl_raised,fl_cleared);
     257           0 :   if( tot_row_fl_raised )
     258           0 :     dprintf(os,"%d row flags; %d raised this pass, %d cleared\n",
     259             :         tot_row_fl_raised,row_fl_raised,row_fl_cleared);
     260           0 : }
     261             : 
     262             : // Sets flag at (ich, ifr) to 1. Returns true if the flag has not been raised
     263             : // previously.
     264           0 : Bool RFFlagCube::setFlag ( uInt ich,uInt ifr, FlagCubeIterator &iter )
     265             : {
     266           0 :     if (kiss) {
     267           0 :         uInt c = 1;
     268           0 :         bool raised = false;
     269           0 :         for (uInt icorr = 0; icorr < num(CORR); icorr++, c<<=1) {
     270           0 :             if (c & corrmask) {
     271           0 :                 if (!in_flags(icorr, ich, ifr)) {
     272           0 :                     raised = true;
     273             :                 }
     274           0 :                 in_flags(icorr, ich, ifr) = 1;
     275             :             }
     276             :         }
     277           0 :         if (raised) {
     278           0 :             tot_fl_raised++;
     279           0 :             fl_raised++;
     280             :         }
     281           0 :         return raised;
     282             :     }
     283             : 
     284             :     if (dbg) cerr << "flag for " << ich << "," << ifr << "corrmask = " << corrmask;
     285             : 
     286           0 :     RFlagWord oldfl = iter(ich,ifr);
     287             :     if (dbg) cerr << " : " << oldfl << "," << flagmask;
     288           0 :     if ( !(oldfl&flagmask) ) {
     289           0 :         tot_fl_raised++;
     290           0 :         fl_raised++;
     291             :         if (dbg) cerr << " setting " << oldfl << " | " << flagmask << endl;
     292           0 :         iter.set(ich, ifr, oldfl | flagmask);
     293             :         if (dbg) cerr << " -----> true --> " << iter(ich,ifr) << endl;
     294           0 :         return true;
     295             :     }
     296             :     if (dbg) cerr << " -----> false --> " << iter(ich,ifr) << endl;
     297           0 :     return false;
     298             : }
     299             : 
     300             : // Clears flag at (ich,iifr). Returns true if flag was up before.
     301           0 : Bool RFFlagCube::clearFlag ( uInt ich,uInt ifr,FlagCubeIterator &iter )
     302             : {
     303             :     if(dbg) cerr << "unflag for " << ich << "," << ifr;
     304             : 
     305           0 :     if (kiss) {
     306             :         
     307           0 :         uInt c = 1;
     308           0 :         bool cleared = false;
     309           0 :         for (uInt icorr = 0; icorr < num(CORR); icorr++, c<<=1) {
     310           0 :             if (c & corrmask) {
     311           0 :                 if (in_flags(icorr, ich, ifr)) {
     312           0 :                     cleared = true;
     313           0 :                     in_flags(icorr, ich, ifr) = 0;
     314             :                 }
     315             :             }
     316             :         }
     317           0 :         if (cleared) {
     318           0 :             tot_fl_raised--;
     319           0 :             fl_cleared++;
     320             :         }
     321           0 :         return cleared;
     322             :     }
     323             : 
     324           0 :     RFlagWord oldfl = iter(ich,ifr);
     325             :     if(dbg)cerr << " : " << oldfl << "," << flagmask;
     326             : 
     327             :     // all flags cleared for this point - update global stats
     328             :     
     329           0 :     if( !(oldfl&flagmask) ) {
     330           0 :         tot_fl_raised--;
     331           0 :         fl_cleared++;
     332           0 :         iter.set(ich, ifr, oldfl & flagmask);
     333             :         if(dbg)cerr << " -----> true --> " << iter(ich,ifr) << endl;
     334           0 :         return true;
     335             :     }
     336             :     if(dbg)cerr << " -----> false --> " << iter(ich,ifr) << endl;
     337           0 :     return false;
     338             : }
     339             : 
     340             : // Sets flag at (ifr, itime). Returns true if flag has not been raised
     341             : // previously.
     342           0 : Bool RFFlagCube::setRowFlag ( uInt ifr, uInt itime )
     343             : {
     344           0 :     if (kiss_flagrow){
     345             : 
     346           0 :         const std::vector<bool> &oldfl = flagrow_kiss(ifr, itime);
     347             : 
     348           0 :         if ( ! oldfl[flagmask_kiss] ) {
     349           0 :             tot_row_fl_raised++;
     350           0 :             row_fl_raised++;
     351             : 
     352           0 :             flagrow_kiss(ifr, itime)[flagmask_kiss] = true;
     353             : 
     354           0 :             return true;
     355             :         }
     356           0 :         return false;
     357             :     }
     358             :     else {
     359           0 :         RFlagWord oldfl = flagrow(ifr,itime);
     360             :         
     361             :         // first flag raised for this row - update global stats
     362           0 :         if ( !(oldfl&flagmask) )
     363             :             {
     364           0 :                 tot_row_fl_raised++;
     365           0 :                 row_fl_raised++;
     366           0 :                 flagrow(ifr, itime) = oldfl | flagmask;
     367           0 :                 return true;
     368             :             }
     369           0 :         return false;
     370             :     }
     371             : }
     372             : 
     373             : // Clears row flag for (ifr, itime). Returns true if flag was up before.
     374           0 : Bool RFFlagCube::clearRowFlag ( uInt ifr,uInt itime )
     375             : {
     376           0 :     if (kiss_flagrow){
     377             : 
     378           0 :         const std::vector<bool> &oldfl = flagrow_kiss(ifr, itime);
     379             : 
     380           0 :         if ( oldfl[flagmask_kiss] ) {
     381           0 :             tot_row_fl_raised--;
     382           0 :             row_fl_raised++;
     383             : 
     384           0 :             flagrow_kiss(ifr, itime)[flagmask_kiss] = false;
     385             : 
     386           0 :             return true;
     387             :         }
     388           0 :         return false;
     389             :     }   
     390             :     else {
     391           0 :         RFlagWord oldfl = flagrow(ifr, itime);
     392             :         
     393           0 :         if (oldfl & flagmask)  {
     394           0 :             tot_row_fl_raised--;
     395           0 :             row_fl_cleared++;
     396           0 :             flagrow(ifr,itime) = oldfl & (~flagmask);
     397           0 :             return true;
     398             :         }
     399           0 :         return false;
     400             :     }
     401             : }
     402             : 
     403             : // Advances the global flag lattice iterator to the specified time.
     404             : // If pfr and pfc are specified, fills in data
     405           0 : void RFFlagCube::advance( uInt it,Bool getFlags )
     406             : {
     407           0 :   if( !kiss && flag.position() != (Int)it )
     408           0 :     flag.advance(it);
     409           0 :   if( getFlags )
     410           0 :     getMSFlags(it);
     411           0 :   return;
     412             : }
     413             : 
     414             : // Fills lattice with apriori flags (from VisBuffer in ChunkStats)
     415           0 : void RFFlagCube::getMSFlags(uInt it)
     416             : {
     417             :   // return if already filled at this iterator position
     418           0 :   if( !kiss ) {
     419           0 :       if (pos_get_flag >= flag.position() )
     420           0 :           return;
     421             :       
     422           0 :       pos_get_flag = flag.position();
     423             :   }
     424             :   else {
     425           0 :       pos_get_flag = it;
     426           0 :       if (in_flags_time == (int)it) {
     427           0 :           return;
     428             :       }
     429           0 :       in_flags_time = it;
     430           0 :       in_flags_flushed = false;
     431             :   }
     432             :   
     433           0 :   unique_ptr<FlagVector> fl_row;
     434           0 :   FlagVector *flr = NULL;
     435             : 
     436             :   //  FlagVector fl_row;//(flagrow.column(pos_get_flag));
     437           0 :   if (!kiss) {
     438           0 :       fl_row = unique_ptr<FlagVector>(new FlagVector(flagrow.column(pos_get_flag)));
     439           0 :       flr = fl_row.get();
     440             :   }
     441             : 
     442           0 :   const Vector<Bool> & fr( chunk.visBuf().flagRow() );
     443             : 
     444           0 :   in_flags.resize(num(CORR), num(CHAN), num(IFR));
     445             :     
     446           0 :   if( reset_preflags ) // RESET pre-flag policy: reset the flags
     447             :   {
     448           0 :     for( uInt i=0; i<fr.nelements(); i++ )
     449             :     {
     450           0 :       uInt ifr = chunk.ifrNum(i);
     451             : 
     452           0 :       if (!kiss) {
     453             :           // clear row flag
     454           0 :           (*flr)(ifr) &= ~(RowAbsent|RowFlagged); // 0000 0011 & 1111 1100 = 0000 0000
     455             :           // clear pixel flags
     456           0 :           flag.set_column(ifr, 0); // 0000 0000
     457             :       }
     458             : 
     459           0 :       for (uInt ichan = 0; ichan < num(CHAN); ichan++) {
     460           0 :           for (uInt icorr = 0; icorr < num(CORR); icorr++) {
     461           0 :               in_flags(icorr, ichan, ifr) = 0;
     462             :           }
     463             :       }
     464             :     }
     465             :   }
     466             :   else // HONOR/IGNORE policy: faithfully copy flags from FLAG and FLAG_ROW
     467             :   {
     468           0 :     const Cube<Bool>   & fc( chunk.visBuf().flagCube() );
     469             : 
     470           0 :     if (fc.shape()(0) != (Int) num(CORR) ||
     471           0 :         fc.shape()(1) != (Int) num(CHAN))
     472             :       {
     473           0 :         stringstream ss;
     474           0 :         ss << "The shape of FLAGs has changed (from (" << num(CORR) << ", " << num(CHAN)
     475           0 :            << ") to (" << fc.shape()(0) << ", " << fc.shape()(1) << ")) within the same chunk. "
     476           0 :            << "Invalid MS.";
     477           0 :         throw AipsError(ss.str());
     478             :       }
     479             : 
     480             :     Bool deleteIn, deleteFc;
     481           0 :     Bool *inp = in_flags.getStorage(deleteIn);
     482           0 :     const Bool *fcp = fc.getStorage(deleteFc);
     483             : 
     484           0 :     for( uInt i=0; i < fr.nelements(); i++ )
     485             :     {
     486           0 :       uInt ifr = chunk.ifrNum(i);
     487             : 
     488           0 :       if (fr(i)) {
     489           0 :           unsigned n = num(CHAN)*num(CORR);
     490           0 :           for (unsigned j = 0; j < n; j++) {
     491           0 :               inp[j + ifr * n] = 1; 
     492             :           }
     493             :       }
     494             :       else {
     495           0 :           unsigned n = num(CORR) * num(CHAN);
     496           0 :           for (unsigned j = 0; j < n; j++) {
     497           0 :               inp[j + n * ifr] = fcp[j + n * i];
     498             :           }
     499             :       }
     500             : 
     501           0 :       if (!kiss) {
     502           0 :           (*flr)(ifr) &= ~RowAbsent; // 0000 0011 & 11111101 = 0000 0001
     503             :           // initial state of lattice is all correlations flagged, so we just
     504             :           // ignore flagged rows
     505             :           //if( !fr(i) )  // row not flagged, or we ignore/reset flags
     506             :           //{
     507             :           // clear row flag in internal matrix, if needed
     508           0 :           if( !fr(i) ) 
     509           0 :               (*flr)(ifr) &= ~RowFlagged; // 0000 0001 & 1111 1110 -> 0000 0000
     510             :           /* clear all row flags...so that only new flags are true at the end */
     511             :           
     512             :           ///... read in chan flags for all rows......
     513             :           ///...  because all may need to be written back.
     514             :           
     515             :           
     516             :           /* The lattice was initialized to all flags set,
     517             :              Now clear as appropriate (if not FLAG_ROW and not FLAG)
     518             :           */
     519           0 :           if (num(CORR) == 1) {
     520           0 :               for (uInt ich=0; ich<num(CHAN); ich++ ) {
     521           0 :                   if( !(*flr)(ifr) && !fc(0, ich, i) ) {
     522           0 :                       flag.set(ich, ifr, 0, 0);
     523             :                   }
     524             :               }
     525             :           }
     526             :           else {
     527           0 :               for (uInt ich=0; ich<num(CHAN); ich++ ) {
     528           0 :                   for (uInt icorr=0; icorr<num(CORR); icorr++ ) {
     529             :                       
     530           0 :                       if( !(*flr)(ifr) && !fc(icorr, ich, i) ) {
     531             :                           //(*flag.cursor())(ich,ifr) &= ~(1<<icorr); 
     532             :                           
     533           0 :                           flag.set(ich, ifr, icorr, 0);
     534             :                       }
     535             :                   }
     536             :               }
     537             :           }
     538             :       }
     539             :     }
     540           0 :     in_flags.putStorage(inp, deleteIn);
     541           0 :     fc.freeStorage(fcp, deleteFc);
     542             :   }
     543             : }
     544             : 
     545             : 
     546             : // Moves flags from lattice to VisBuffer
     547             : // ifrnums is a vector of IFR indices (derived from antenna indices)
     548           0 : void RFFlagCube::setMSFlags(uInt itime)
     549             : {
     550             :     //    cerr << itime << " write to visIter" << endl;
     551             :   //jmlarsen: This function should probably use the flagrow member variable.
     552             : 
     553             :     if(mdbg) 
     554             :     {
     555             :       cerr << "RFFlagCube :: setMSFlags for " ;
     556             :       cerr << "itime : " << itime << endl;
     557             :     }
     558             : 
     559             :   // return if already done at this iterator position
     560           0 :   if (!kiss) {
     561           0 :       if( flag.position() <= pos_set_flag )
     562           0 :           return;
     563             :       
     564           0 :       pos_set_flag = flag.position();
     565             :   }
     566             :   else {
     567           0 :       if (in_flags_flushed) {
     568           0 :           return;
     569             :       }
     570             :       else {
     571           0 :           in_flags_flushed = true;
     572             :       }
     573             :   }
     574             : 
     575           0 :   uInt nr = chunk.visBuf().nRow();
     576           0 :   Vector<Bool> out_flagrow( nr,false );
     577           0 :   Cube<Bool>   out_flagcube( num(CORR),num(CHAN),nr,false );
     578             : 
     579           0 :   chunk.nrfTime(itime) = 0;
     580             : 
     581             :   Bool deleteOut, deleteIn;
     582           0 :   Bool *outp = out_flagcube.getStorage(deleteOut);
     583           0 :   const Bool *inp = in_flags.getStorage(deleteIn);
     584             : 
     585             :   Bool deleteNfChanIfr;
     586           0 :   uInt *nfChanIfrp = chunk.nfChanIfr().getStorage(deleteNfChanIfr);
     587             : 
     588           0 :   unsigned ncorr = num(CORR);
     589           0 :   unsigned nchan = num(CHAN);
     590             : 
     591           0 :   for( uInt ir=0; ir<nr; ir++ )
     592             :   {
     593           0 :       uInt ifr = chunk.ifrNum(ir);
     594             : 
     595           0 :       chunk.nrfIfr(ifr) = 0;
     596             : 
     597             :       if (dbg) cerr << "  at " << __FILE__ << " " << __func__ << " " << __LINE__ << " " << __LINE__ << out_flagrow(ir) << endl;
     598             :       
     599             :       // Set data flags
     600           0 :       unsigned n = nchan * ncorr;
     601           0 :       unsigned iout = n*ir;
     602           0 :       unsigned iin = n*ifr;
     603           0 :       unsigned iChanIfr = nchan * ifr;
     604           0 :       uInt &iNfIfrTime = chunk.nfIfrTime(ifr, itime);
     605           0 :       iNfIfrTime = 0;
     606           0 :       for( uInt ich=0; ich < nchan; ich++, iChanIfr++) {
     607           0 :           nfChanIfrp[iChanIfr] = 0;
     608             :       }
     609             : 
     610           0 :       iChanIfr = nchan * ifr;
     611           0 :       for( uInt ich=0; ich < nchan; ich++, iChanIfr++) {
     612             : 
     613           0 :           if (kiss) {
     614             : 
     615           0 :               if (ncorr == 1) {
     616             :                   /* TODO assignment correct? */
     617           0 :                   if ((outp[iout++] = inp[iin++])) {
     618           0 :                       nfChanIfrp[iChanIfr]++;
     619           0 :                       iNfIfrTime++;
     620             :                   }
     621             :               }
     622             :               else {
     623           0 :                   for( uInt icorr = 0; icorr < ncorr; icorr++, iout++, iin++) {
     624             :                           /* TODO assignment correct? */
     625           0 :                       if ((outp[iout] = inp[iin])) {
     626           0 :                           nfChanIfrp[iChanIfr]++;
     627           0 :                           iNfIfrTime++;
     628             :                       }
     629             :                   }
     630             :               }
     631             :           } else {
     632             : 
     633           0 :               RFlagWord fw = flag(ich, ifr);
     634             :               
     635           0 :               if (fw) {
     636             :                   // if anything was raised for this channel
     637             :                   
     638             :                   // loop over correlations and see which are (a) preflagged
     639             :                   // (b) been flagged by agents. 
     640           0 :                   RFlagWord cmask = 1;
     641           0 :                   if (num(CORR) == 1) {
     642             :                       
     643           0 :                       if ((fw & 1) || (fw & corr_flagmask(1))) {
     644           0 :                           out_flagcube(0, ich, ir) = true;
     645           0 :                           chunk.nfChanIfr(ich,ifr)++;
     646           0 :                           chunk.nfIfrTime(ifr,itime)++;
     647             :                       }
     648             :                   }
     649             :                   else {
     650           0 :                       for( uInt  icorr=0; icorr<num(CORR); icorr++, cmask<<=1 ) {
     651             :                           
     652           0 :                           if( (fw & cmask)       // (a) if fw is set for this correlation
     653           0 :                               ||
     654           0 :                               (fw & corr_flagmask(cmask)) // (b) if agent flag in fw
     655             :                               // is set for any agent that 
     656             :                               // deals with this correlation
     657             :                               ) {
     658             :                               
     659           0 :                               out_flagcube(icorr,ich,ir) = true;
     660           0 :                               chunk.nfChanIfr(ich,ifr)++;
     661           0 :                               chunk.nfIfrTime(ifr,itime)++;
     662             :                           }
     663             :                       }
     664             :                   }
     665             :               }
     666             :           }
     667             :       }
     668             :     
     669             :       /* if any dataflags have been unflagged, they already are.
     670             :          if any rowflags have been unflagged, this is already in the dataflags too */
     671             :       /* if any dataflags have been flagged - this info is there in dataflags.
     672             :          if any rowflags have been flagged, this is also there in dataflags */
     673             :       // so make flag_row the AND of the dataflags.
     674             : 
     675             :       /* Set flagrow if everything was flagged */
     676             : 
     677             :       /* Fill in all the flag counts here */
     678             :       // chunk.nf*
     679             :       // nrfIfr(ifr), nrfTime(itime), nfIfrTime(ifr,itime), nfChanIfr(ich,ifr)
     680             : 
     681           0 :       if (chunk.nfIfrTime(ifr, itime) == nchan * ncorr) {
     682             : 
     683           0 :           out_flagrow(ir) = true;
     684             : 
     685           0 :           chunk.nrfIfr(ifr)++;
     686           0 :           chunk.nrfTime(itime)++;
     687             :       }
     688             :   }
     689             : 
     690           0 :   out_flagcube.putStorage(outp, deleteOut);
     691           0 :   in_flags.freeStorage(inp, deleteIn);
     692           0 :   chunk.nfChanIfr().putStorage(nfChanIfrp, deleteNfChanIfr);
     693             : 
     694             :   if(mdbg)
     695             :       {
     696             :           Int cnt1=0,cnt2=0;
     697             :           for( uInt ir=0; ir<nr; ir++ )
     698             :               {
     699             :                   uInt ifr = chunk.ifrNum(ir);
     700             :                   cnt1 += chunk.nrfIfr(ifr);
     701             :                   cnt2 += chunk.nfIfrTime(ifr,itime);
     702             :                   cerr << "Sum of flagrow (ifr) : " << cnt1 << endl;
     703             :                   cerr << "Sum of flags (ifr,itime): " << cnt2 << endl;
     704             :               }
     705             :       }
     706             : 
     707           0 :   chunk.visIter().setFlag(out_flagcube);
     708           0 :   chunk.visIter().setFlagRow(out_flagrow);
     709             : }
     710             : 
     711             : } //# NAMESPACE CASA - END
     712             : 

Generated by: LCOV version 1.16