From 84d10ce3879b5cf4f5f9d4c3de9f7c70e6e32b78 Mon Sep 17 00:00:00 2001
From: Adriaan Renting <renting@astron.nl>
Date: Thu, 18 Oct 2007 03:03:24 +0000
Subject: [PATCH] BugId: 1114 Initial check in

---
 .../CS1_BinningFlagger.spec.in                | 160 +++++++
 Appl/CEP/CS1/CS1_BinningFlagger/Makefile.am   |  15 +
 Appl/CEP/CS1/CS1_BinningFlagger/configure.in  |  61 +++
 .../FlaggerProcessControl.h                   |  84 ++++
 .../include/CS1_BinningFlagger/Makefile.am    |   3 +
 .../CS1_BinningFlagger/include/Makefile.am    |   3 +
 Appl/CEP/CS1/CS1_BinningFlagger/package.dox   |   2 +
 .../CS1_BinningFlagger/src/BinningFlagger.cc  | 404 ++++++++++++++++++
 .../CS1_BinningFlagger/src/BinningFlagger.h   |  94 ++++
 .../CEP/CS1/CS1_BinningFlagger/src/Flagger.cc |  48 +++
 .../src/FlaggerProcessControl.cc              | 147 +++++++
 .../CEP/CS1/CS1_BinningFlagger/src/MS_File.cc | 182 ++++++++
 Appl/CEP/CS1/CS1_BinningFlagger/src/MS_File.h |  68 +++
 .../CS1/CS1_BinningFlagger/src/Makefile.am    |  12 +
 .../test/CS1_BinningFlagger.parset            |   8 +
 .../CS1/CS1_BinningFlagger/test/Makefile.am   |  25 ++
 16 files changed, 1316 insertions(+)
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/CS1_BinningFlagger.spec.in
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/Makefile.am
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/configure.in
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/include/CS1_BinningFlagger/FlaggerProcessControl.h
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/include/CS1_BinningFlagger/Makefile.am
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/include/Makefile.am
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/package.dox
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/src/BinningFlagger.cc
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/src/BinningFlagger.h
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/src/Flagger.cc
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/src/FlaggerProcessControl.cc
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/src/MS_File.cc
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/src/MS_File.h
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/src/Makefile.am
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/test/CS1_BinningFlagger.parset
 create mode 100644 Appl/CEP/CS1/CS1_BinningFlagger/test/Makefile.am

diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/CS1_BinningFlagger.spec.in b/Appl/CEP/CS1/CS1_BinningFlagger/CS1_BinningFlagger.spec.in
new file mode 100644
index 00000000000..6fe5095bc3c
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/CS1_BinningFlagger.spec.in
@@ -0,0 +1,160 @@
+# -*- Mode:rpm-spec -*-
+# CS1_Generator.spec.in
+#
+
+##############################################################################
+#
+# Preamble
+#
+##############################################################################
+
+Summary: CS1_Flagger is a small program to do flagging for the offline pipeline of CS1
+
+%define release @RPM_RELEASE@
+%define version @VERSION@
+%define pkgname @PACKAGE@
+%define pkgdir %{pkgname}-%{version}-%{release}
+%define prefix /opt/lofar
+%define configure_args @RPM_CONFIGURE_ARGS@
+##define build_kernel_version @BUILD_KERNEL_VERSION@
+
+Name: %{pkgname}
+Version: %{version}
+Release: %{release}
+Copyright: LGPL
+Group: Application/System
+Source: %{pkgname}-%{version}.tar.gz
+BuildRoot: %{_tmppath}/%{pkgdir}-root
+URL: http://www.astron.nl/~renting
+Prefix: %{prefix}
+BuildArchitectures: i386 # Target platforms, i.e., i586
+##Requires: Common = 1.2   ## define dependent packages here
+Packager: %{packager}
+Distribution: The LOFAR project
+Vendor: ASTRON
+
+AutoReqProv: no
+
+%description
+
+CS1_Flagger More detailed desciprion will follow.
+
+##############################################################################
+#
+# prep
+#
+##############################################################################
+%prep
+echo $prefix
+
+# create the build directory, untar the source
+%setup
+
+##############################################################################
+#
+# build
+#
+##############################################################################
+%build
+./configure %{configure_args} --prefix=%{prefix} && make
+
+##############################################################################
+#
+# install
+#
+##############################################################################
+%install
+# To make things work with BUILDROOT
+if [ "$RPM_BUILD_ROOT" != "%{_tmppath}/%{pkgdir}-root" ]
+then
+  echo
+  echo @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+  echo @                                                                    @
+  echo @  RPM_BUILD_ROOT is not what I expected.  Please clean it yourself. @
+  echo @                                                                    @
+  echo @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+  echo
+else
+  echo Cleaning RPM_BUILD_ROOT: "$RPM_BUILD_ROOT"
+  rm -rf "$RPM_BUILD_ROOT"
+fi
+mkdir -p $RPM_BUILD_ROOT%{prefix}
+make DESTDIR="$RPM_BUILD_ROOT" install
+
+#uninstall
+
+##############################################################################
+#
+# verify
+#
+##############################################################################
+#verify
+
+##############################################################################
+#
+# clean
+#
+##############################################################################
+%clean
+# Call me paranoid, but I do not want to be responsible for nuking
+# someone's harddrive!
+if [ "$RPM_BUILD_ROOT" != "%{_tmppath}/%{pkgdir}-root" ]
+then
+  echo
+  echo @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+  echo @                                                                    @
+  echo @  RPM_BUILD_ROOT is not what I expected.  Please clean it yourself. @
+  echo @                                                                    @
+  echo @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
+  echo
+else
+  echo Cleaning RPM_BUILD_ROOT: "$RPM_BUILD_ROOT"
+  rm -rf "$RPM_BUILD_ROOT"
+fi
+
+##############################################################################
+#
+# files
+#
+##############################################################################
+
+# empty 'files' means all distributed files
+%files
+%defattr(-, root, root)
+%{prefix}
+
+# Your application file list goes here
+# %{prefix}/lib/lib*.so*
+
+# Documentation
+# doc COPYING ChangeLog README AUTHORS NEWS
+# doc doc/*
+
+# link the module to the correct path
+%post 
+
+# before uninstall
+%preun
+
+# after uninstall
+%postun
+
+##############################################################################
+#
+# package devel
+#
+##############################################################################
+
+#package devel
+#Summary: Development files for %{pkgname}
+#Group: Applications/System
+#description devel
+#Development files for %{pkgname}.
+
+#files devel
+
+# Your development files go here
+# Programmers documentation goes here
+#doc doc
+
+# end of file
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/Makefile.am b/Appl/CEP/CS1/CS1_BinningFlagger/Makefile.am
new file mode 100644
index 00000000000..f3f5bdd622c
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/Makefile.am
@@ -0,0 +1,15 @@
+SUBDIRS=src test include
+
+pkgextdir     = $(prefix)/config/$(PACKAGE)
+pkgext_DATA   = pkgext pkgextcppflags pkgextcxxflags pkgextldflags
+
+DISTCHECK_CONFIGURE_FLAGS= \
+      --with-common=$(prefix) \
+      --with-aips++ 
+
+EXTRA_DIST = \
+      Makefile.common \
+      CS1_BinningFlagger.spec \
+      autoconf_share/compiletool
+
+include $(top_srcdir)/Makefile.common
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/configure.in b/Appl/CEP/CS1/CS1_BinningFlagger/configure.in
new file mode 100644
index 00000000000..d39ea993207
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/configure.in
@@ -0,0 +1,61 @@
+dnl
+dnl Process this file with autoconf to produce a configure script.
+dnl
+AC_INIT
+AM_CONFIG_HEADER(config.h)
+AM_INIT_AUTOMAKE(CS1_BinningFlagger, 1.0, no-define)
+
+dnl Initialize for LOFAR (may set compilers)
+lofar_INIT
+
+dnl Checks for programs.
+AC_LANG_CPLUSPLUS
+AC_PROG_CXX
+AC_PROG_LIBTOOL
+
+dnl Checks for libraries.
+
+dnl dnl Replace `main' with a function in -lfl:
+dnl AC_CHECK_LIB(fl, main)
+dnl dnl Replace `main' with a function in -lcosev_r:
+dnl AC_CHECK_LIB(cosev_r, main)
+dnl dnl Replace `main' with a function in -lcosnm_r:
+dnl AC_CHECK_LIB(cosnm_r, main)
+dnl dnl Replace `main' with a function in -lorb_r:
+dnl AC_CHECK_LIB(orb_r, main)
+dnl dnl Replace `main' with a function in -lpthread:
+dnl AC_CHECK_LIB(pthread, main)
+dnl dnl Replace `main' with a function in -lvport_r:
+dnl AC_CHECK_LIB(vport_r, main)
+
+dnl Checks for header files.
+AC_HEADER_STDC
+AC_CHECK_HEADERS(unistd.h)
+
+dnl Checks for typedefs, structures, and compiler characteristics.
+AC_C_CONST
+AC_TYPE_SIZE_T
+
+dnl Checks for library functions.
+AC_FUNC_VPRINTF
+
+dnl
+dnl Check for LOFAR specific things
+dnl
+lofar_GENERAL(1)
+lofar_AIPSPP(1)
+lofar_INTERNAL(LCS/ACC/PLC,PLC,,1,PLC/ProcessControl.h)
+lofar_INTERNAL(LCS/ACC/APS,APS,,1,APS/ParameterSet.h)
+lofar_EXTERNAL(boost,1,boost/logic/tribool.hpp,"")
+
+dnl
+dnl Output Makefiles
+dnl
+AC_OUTPUT(
+include/Makefile
+include/CS1_BinningFlagger/Makefile
+src/Makefile
+test/Makefile
+Makefile
+CS1_BinningFlagger.spec
+)
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/include/CS1_BinningFlagger/FlaggerProcessControl.h b/Appl/CEP/CS1/CS1_BinningFlagger/include/CS1_BinningFlagger/FlaggerProcessControl.h
new file mode 100644
index 00000000000..8d7c9765cc8
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/include/CS1_BinningFlagger/FlaggerProcessControl.h
@@ -0,0 +1,84 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by Adriaan Renting                                 *
+ *   renting@astron.nl                                                     *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+#ifndef LOFARFLAGGERPROCESSCONTROL_H
+#define LOFARFLAGGERPROCESSCONTROL_H
+
+#include <PLC/ProcessControl.h>
+#include <APS/ParameterSet.h>
+
+/**
+@author Adriaan Renting
+*/
+
+namespace LOFAR
+{
+  namespace CS1
+  {
+    class MS_File; //foreward declaration
+    class FrequencyFlagger;
+
+    class FlaggerProcessControl : public LOFAR::ACC::PLC::ProcessControl
+    {
+    private:
+      std::string  itsMS;
+      bool         itsExisting;
+      double       itsThreshold;
+      MS_File*          myMS;
+      FrequencyFlagger* itsFlagger;
+    public:
+      FlaggerProcessControl(void);
+
+      ~FlaggerProcessControl(void);
+      // \name Command to control the processes.
+      // There are a dozen commands that can be sent to a application process
+      // to control its flow. The return values for these command are:<br>
+      // - True   - Command executed succesfully.
+      // - False  - Command could not be executed.
+      //
+      // @{
+
+      // During the \c define state the process check the contents of the
+      // ParameterSet it received during start-up. When everthing seems ok the
+      // process constructs the communication channels for exchanging data
+      // with the other processes. The connection are NOT made in the stage.
+      tribool define   (void);
+
+      // When a process receives an \c init command it allocates the buffers it
+      // needs an makes the connections with the other processes. When the
+      // process succeeds in this it is ready for dataprocessing (or whatever
+      // task the process has).
+      tribool init     (void);
+
+      // During the \c run phase the process does the work it is designed for.
+      // The run phase stays active until another command is send.
+      tribool run      (void);
+
+      tribool pause(const std::string&);
+      tribool quit(void);
+      tribool recover(const std::string&);
+      tribool reinit(const  std::string&);
+      tribool snapshot(const std::string&);
+      std::string askInfo(const std::string&);
+
+    }; //class FlaggerProcessControl
+  } //namespace CS1_Flagger
+}; //namespace LOFAR
+
+#endif
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/include/CS1_BinningFlagger/Makefile.am b/Appl/CEP/CS1/CS1_BinningFlagger/include/CS1_BinningFlagger/Makefile.am
new file mode 100644
index 00000000000..c471295b641
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/include/CS1_BinningFlagger/Makefile.am
@@ -0,0 +1,3 @@
+pkginclude_HEADERS = FlaggerProcessControl.h 
+
+include $(top_srcdir)/Makefile.common
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/include/Makefile.am b/Appl/CEP/CS1/CS1_BinningFlagger/include/Makefile.am
new file mode 100644
index 00000000000..dbb8180464d
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/include/Makefile.am
@@ -0,0 +1,3 @@
+SUBDIRS	= CS1_FrequencyFlagger
+
+include $(top_srcdir)/Makefile.common
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/package.dox b/Appl/CEP/CS1/CS1_BinningFlagger/package.dox
new file mode 100644
index 00000000000..15e4659f8a0
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/package.dox
@@ -0,0 +1,2 @@
+// \ingroup CS1
+// \defgroup CS1_BinningFlagger CS1_BinningFlagger Description
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/src/BinningFlagger.cc b/Appl/CEP/CS1/CS1_BinningFlagger/src/BinningFlagger.cc
new file mode 100644
index 00000000000..908b70f329d
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/src/BinningFlagger.cc
@@ -0,0 +1,404 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by ASTRON, Adriaan Renting                         *
+ *   renting@astron.nl                                                     *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+#include <tables/Tables.h>
+#include <tables/Tables/TableIter.h>
+#include "FrequencyFlagger.h"
+#include <casa/Quanta/MVEpoch.h>
+
+using namespace casa;
+
+enum CorrelationTypes {None=0,I=1,Q=2,U=3,V=4,RR=5,RL=6,LR=7,LL=8,XX=9,XY=10,YX=11,YY=12}; //found somewhere in AIPS++, don't remember where
+
+//===============>>>  itoa  <<<===============
+//ANSI C++ doesn't seem to have a decent function for this, or I'm not aware of it. Need to rename it to IntToStr(), to avoid confusion
+std::string itoa(int value, int base=10)
+{ // found on http://www.jb.man.ac.uk/~slowe/cpp/itoa.html
+  //maybe copyright Robert Jan Schaper, Ray-Yuan Sheu, Rodrigo de Salvo Braz, Wes Garland and John Maloney
+  enum { kMaxDigits = 35 };
+  std::string buf;
+  buf.reserve( kMaxDigits ); // Pre-allocate enough space.
+        // check that the base if valid
+  if (base < 2 || base > 16) return buf;
+  int quotient = value;
+        // Translating number to string with base:
+  do {
+    buf += "0123456789abcdef"[ std::abs( quotient % base ) ];
+    quotient /= base;
+  } while ( quotient );
+        // Append the negative sign for base 10
+  if ( value < 0 && base == 10) buf += '-';
+  std::reverse( buf.begin(), buf.end() );
+  return buf;
+}
+namespace LOFAR
+{
+  namespace CS1
+  {
+    //===============>>>  FrequencyFlagger::FrequencyFlagger  <<<===============
+    /* initialize some meta data and get the datastorage the right size. */
+    FrequencyFlagger::FrequencyFlagger(MS_File* InputMSfile,
+                                       double InputThreshold)
+    {
+      MSfile           = InputMSfile;
+      Threshold        = InputThreshold;
+      NumAntennae      = (*MSfile).itsNumAntennae;
+      NumPairs         = (*MSfile).itsNumPairs;
+      NumBands         = (*MSfile).itsNumBands;
+      NumChannels      = (*MSfile).itsNumChannels;
+      NumPolarizations = (*MSfile).itsNumPolarizations;
+      NoiseLevel       = (*MSfile).itsNoiseLevel;
+      NumTimeslots     = (*MSfile).itsNumTimeslots;
+      AntennaNames     = (*MSfile).itsAntennaNames;
+
+      PairsIndex.resize(NumPairs);
+      Statistics = Cube<int>(NumBands, NumAntennae, NumAntennae, 0);
+
+      TimeslotData.resize(NumPolarizations, NumChannels, NumPairs*NumBands);
+      Flags.resize(NumPolarizations, NumChannels, NumPairs*NumBands);
+
+      int index = 0;
+      for (int i = 0; i < NumAntennae; i++)
+      { for(int j = i; j < NumAntennae; j++)
+        { PairsIndex[index]           = pairii(i, j);
+          BaselineIndex[pairii(i, j)] = index++;
+        }
+      }
+      ComputeBaselineLengths();
+    }
+
+    //===============>>>  FrequencyFlagger::~FrequencyFlagger  <<<===============
+
+    FrequencyFlagger::~FrequencyFlagger()
+    {
+    }
+
+    //===============>>> FrequencyFlagger::ComputeBaselineLengths  <<<===============
+    /* compute baseline lengths, and determine the longest one.*/
+    void FrequencyFlagger::ComputeBaselineLengths()
+    {
+      MaxBaselineLength = 0.0;
+      BaselineLengths.resize(NumPairs);
+      //Antenna positions
+      MSAntenna antenna     = (*MSfile).antenna();
+      ROArrayColumn<Double>  position(antenna, "POSITION");
+      for (int i = 0; i < NumAntennae; i++ )
+      {
+        for (int j = i; j < NumAntennae; j++)
+        {
+          Vector<Double> p(position(i) - position(j));
+          double temp   = sqrt(p(0)*p(0) + p(1)*p(1) + p(2)*p(2));
+          BaselineLengths[BaselineIndex[pairii(i, j)]] = temp;
+          if (temp > MaxBaselineLength && temp < 3000000) //radius of the Earth in meters? WSRT sometimes has fake telescopes at 3854243
+          { MaxBaselineLength = temp;                     // non-existent antenna's can have position (0,0,0)
+          }
+        }
+      }
+    }
+
+    //===============>>> FrequencyFlagger::FlagDataOrBaselines  <<<===============
+    /*This function outputs the gathered statistics.*/
+    void FrequencyFlagger::ProcessStatistics()
+    {
+      vector<int>  bands(NumBands);
+      vector<int>  antennae(NumAntennae);
+      unsigned int namelength = 6;
+      for(int i = 0; i < NumAntennae; i++)
+      {
+        if (namelength < AntennaNames[i].size())
+        { namelength = AntennaNames[i].size();
+        }
+      }
+      for (int i = 0; i < NumBands; i++)
+      {
+        cout << "Band: " << i+1 << endl;
+        cout << string(namelength+1,' ');
+        for(int j = 0; j < NumAntennae; j++)
+        {
+          string out = AntennaNames[j];
+          out.resize(namelength+1,' ');
+          cout << out;
+        }
+        cout << endl;
+        for(int j = 0; j < NumAntennae; j++)
+        {
+          string out = AntennaNames[j];
+          out.resize(namelength+1,' ');
+          cout << out;
+          for(int k = 0; k < NumAntennae; k++)
+          {
+            if (k < j) //We print a complete array, but we have inspected only those where k >= j
+            {
+              int val = 100 * Statistics(i,k,j) / (NumChannels * NumTimeslots * NumPolarizations);
+              bands[i]    += val;
+              antennae[j] += val;
+              antennae[k] += val;
+              string out = itoa(val) + "%";
+              out.resize(namelength+1,' ');
+              cout << out;
+            }
+            else
+            {
+              int val = 100 * Statistics(i,j,k) / (NumChannels * NumTimeslots * NumPolarizations);
+              bands[i]    += val;
+              antennae[j] += val;
+              antennae[k] += val;
+              string out = itoa(val) + "%";
+              out.resize(namelength+1,' ');
+              cout << out;
+            }
+              /*{ cout << rms << "Faulty baseline detected: Antenna " << j+1
+                    << ":" << k+1 << " SpectralWindow: "<< i+1 << endl;
+              }
+            }*/
+          }
+          cout << endl;
+        }
+      }
+      cout << "Bands (flagged %):    ";
+      for (int i = 0; i < NumBands; i++)
+      {
+        string out = string("Band") + itoa(i);
+        out.resize(namelength+1,' ');
+        cout << out;
+      }
+      cout << endl << "                      ";
+      for (int i = 0; i < NumBands; i++)
+      {
+        string out = itoa(bands[i] / (NumAntennae*NumAntennae)) + "%";
+        out.resize(namelength+1,' ');
+        cout << out;
+      }
+      cout << endl << "Antennae (flagged %): " ;
+      for(int j = 0; j < NumAntennae; j++)
+      {
+        string out = AntennaNames[j];
+        out.resize(namelength+1,' ');
+        cout << out;
+      }
+      cout << endl << "                       ";
+      for (int i = 0; i < NumAntennae; i++)
+      {
+        string out = itoa(antennae[i] / (NumBands*NumAntennae*2)) + "%";
+        out.resize(namelength+1,' ');
+        cout << out;
+      }
+      cout << endl;
+    }
+    //===============>>> FrequencyFlagger::FlagTimeslot  <<<===============
+    /* This function inspects each visibility in a cetain baseline-band
+    and flags on complexe distance, then determines to flag the entire baseline-band
+    based on the RMS of the points it didn't flag.*/
+    bool FrequencyFlagger::FlagBaselineBand(Matrix<Bool> Flags,
+                                            Matrix<Complex> Timeslots,
+                                            int* flagCounter,
+                                            double FlagThreshold)
+    {
+      vector<double> MS(NumPolarizations, 0.0);
+      vector<double> RMS(NumPolarizations);
+      vector<int>    RMSCounter(NumPolarizations, 0);
+      bool           FlagCompleteRow = true;
+      int            flagcount       = 0;
+      for (int j = NumPolarizations-1; j >= 0; j--)
+      {
+        for (int i = NumChannels-1; i >= 0; i--) //calculata RMS of unflagged datapoints
+        {
+          if (!ExistingFlags || !Flags(j, i))
+          { double temp = pow(abs(Timeslots(j, i)), 2);
+            if (!isNaN(temp))
+            {
+              MS[j] += temp;
+              RMSCounter[j] += 1;
+            }
+          }
+        }
+        if (RMSCounter[j])
+        { RMS[j] = sqrt(MS[j] /RMSCounter[j]);
+          for (int i = NumChannels-1; i >= 0; i--)
+          {
+            if (!ExistingFlags || !Flags(j, i))
+            { double temp = abs(Timeslots(j, i));
+              bool flag   = isNaN(temp) || RMS[j] * FlagThreshold < temp;
+              //cout << RMS[j] << " " << (RMS[j] * FlagThreshold) << " " << temp << " " << (flag) << endl;
+              if (flag)
+              { flagcount++;
+              }
+              else
+              { FlagCompleteRow = false;
+              }
+              Flags(j, i) = flag || (ExistingFlags && Flags(j, i));
+            }
+          }
+        }
+        else
+        {
+          flagcount += NumChannels;
+          Flags.row(j) = true;
+        }
+      }
+      //these need to be separated out into a different function for clarity
+      if (flagcount > 0.9 * NumChannels * NumPolarizations) //more as 90% bad
+      {
+        FlagCompleteRow = true;
+        Flags           = true;
+        flagcount       = NumChannels * NumPolarizations;
+      }
+      (*flagCounter) += flagcount;
+      return FlagCompleteRow;
+    }
+    //===============>>> FrequencyFlagger::FlagBaseline  <<<===============
+    /* This function iterates over baseline and band and uses FlagBaselineBand() to determine
+      for each one if it needs to be flagged. It treats the autocorrelations separately,
+      to detect entire malfunctioning telescopes. Finally it writes the flags.
+    */
+    void FrequencyFlagger::FlagTimeslot(TableIterator* flag_iter,
+                                            bool ExistingFlags)
+    {
+      vector<bool>   FlagCompleteRow(NumPairs*NumBands);
+      vector<int>    stats(NumPairs*NumBands);
+      for (int i = 0; i < NumBands; i++)
+      {
+        for(int j = 0; j < NumAntennae; j++)
+        {
+          for(int k = j; k < NumAntennae; k++)
+          {
+            int index = i*NumPairs + BaselineIndex[pairii(j, k)];
+            if ((BaselineLengths[BaselineIndex[pairii(j, k)]] < 3000000))//radius of the Earth in meters? WSRT sometimes has fake telescopes at 3854243 m
+            { //we skip the non-existent telescopes
+              FlagCompleteRow[index] = FlagBaselineBand(Flags.xyPlane(index),
+                                                        TimeslotData.xyPlane(index),
+                                                        &(stats[index]),
+                                                        Threshold);
+            }
+          }
+        }
+        //this code could be separated in to different functions
+        for(int j = 0; j < NumAntennae; j++) //write the data
+        {
+          for(int k = j; k < NumAntennae; k++)
+          {
+            int index = i*NumPairs + BaselineIndex[pairii(j, k)];
+            Matrix<Bool> flags = Flags.xyPlane(index);
+            if (FlagCompleteRow[index])
+            {
+              Matrix<Bool> flags     = Flags.xyPlane(index);
+              flags                  = true;
+              Statistics(i,j,k)     += NumChannels * NumPolarizations;
+            }
+            else
+            { Statistics(i,j,k)     += stats[index];
+            }
+            (*MSfile).WriteDataPointFlags(flag_iter, &flags, FlagCompleteRow[index], ExistingFlags);
+            (*flag_iter)++;
+          }
+        }
+      }
+    }
+
+    //===============>>> FrequencyFlagger::UpdateTimeslotData  <<<===============
+    /* This function reads the visibility data for one timeslot and checks if
+      a mosaicing mode is active*/
+    /* The datastructure Timeslotdata is rather complex because it flattens
+      half-filled antenna x antenna x bands matrix into a vector of NumPairs X bands length. */
+    bool FrequencyFlagger::UpdateTimeslotData(vector<int>* OldFields,
+                                                  vector<int>* OldBands,
+                                                  int* TimeCounter,
+                                                  Table* TimeslotTable,
+                                                  double* Time)
+    {
+      int           rowcount = (*TimeslotTable).nrow();
+      ROTableVector<Int>     antenna1((*TimeslotTable), "ANTENNA1");
+      ROTableVector<Int>     antenna2((*TimeslotTable), "ANTENNA2");
+      ROTableVector<Int>     bandnr  ((*TimeslotTable), "DATA_DESC_ID");
+      ROTableVector<Int>     fieldid ((*TimeslotTable), "FIELD_ID");
+      ROTableVector<Double>  time    ((*TimeslotTable), "TIME_CENTROID");//for testing purposes
+      ROArrayColumn<Complex> data    ((*TimeslotTable), "DATA");
+      ROArrayColumn<Bool>    flags    ((*TimeslotTable), "FLAG");
+      Cube<Complex>          Data(NumPolarizations, NumChannels, rowcount);
+      Cube<Bool>             Flags(NumPolarizations, NumChannels, rowcount);
+
+      data.getColumn(Data); //We're not checking Data.nrow() Data.ncolumn(), assuming all data is the same size.
+      flags.getColumn(Flags); //We're not checking Data.nrow() Data.ncolumn(), assuming all data is the same size.
+      (*Time) = time(0);//for testing purposes, might be useful in the future
+
+      bool NewField_or_Frequency = false;
+
+      for (int i = 0; i < rowcount; i++)
+      {
+        int bi    = BaselineIndex[pairii(antenna1(i), antenna2(i))];
+        int field = fieldid(i);
+        int band  = bandnr(i);
+        int index = (band % NumBands) * NumPairs + bi;
+        if ((*TimeCounter))
+        {
+          if (field != (*OldFields)[bi]) //pointing mosaicing
+          { NewField_or_Frequency = true;
+          }
+          if (band  != (*OldBands)[index]) //frequency mosaicing
+          { NewField_or_Frequency = true;
+          }
+        }
+        (*OldFields)[bi]   = field;
+        (*OldBands)[index] = band;
+
+        TimeslotData.xyPlane(index) = Data.xyPlane(i);
+        Flags.xyPlane(index)    = Flags.xyPlane(i); //TimeslotData is only WindowSize size!
+      }
+      return NewField_or_Frequency; //assuming everybody is changing field or frequency at the same time!
+    }
+
+    //===============>>> FrequencyFlagger::FlagDataOrBaselines  <<<===============
+    /* This function iterates over the data per timeslot and uses Flagtimeslot()
+      to actually flag datapoints (if flagDatapoints), and entire baselines (if flagRMS)*/
+    void FrequencyFlagger::FlagDataOrBaselines(bool existing)
+    {
+      TableIterator timeslot_iter = (*MSfile).TimeslotIterator();
+      TableIterator flag_iter     = (*MSfile).TimeAntennaIterator();
+      double        Time          = 0.0;//for testing purposes
+      vector<int>   OldFields(NumPairs);         //to check on multipointing and mosaicing
+      vector<int>   OldBands(NumPairs*NumBands); //to check on multifrequency and freq mosaicing
+      ExistingFlags = existing;
+      int           step          = NumTimeslots / 10 + 1; //not exact but it'll do
+      int           row           = 0;
+
+      while (!timeslot_iter.pastEnd())
+      {
+        Table TimeslotTable  = timeslot_iter.table();
+        bool  NewFieldorFreq = UpdateTimeslotData(&OldFields,
+                                                  &OldBands,
+                                                  &row,
+                                                  &TimeslotTable,
+                                                  &Time);
+        //cout << "Processing: " << MVTime(Time/(24*3600)).string(MVTime::YMD) << endl; //for testing purposes
+
+        FlagTimeslot(&flag_iter, ExistingFlags);
+        timeslot_iter++;
+        if (row++ % step == 0) // to tell the user how much % we have processed,
+        { cout << 10*(row/step) << "%" << endl; //not very accurate for low numbers of timeslots, but it'll do for now
+        }
+        if (NewFieldorFreq)
+        { // at the moment we can't realy handle this?
+          cout << "Error new field or frequency detected" << endl;
+        }
+      }
+      ProcessStatistics(); //is there a baseline that should be flagged?
+    }
+    //===============>>> FrequencyFlagger  <<<===============
+  }; // namespace CS1
+}; // namespace LOFAR
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/src/BinningFlagger.h b/Appl/CEP/CS1/CS1_BinningFlagger/src/BinningFlagger.h
new file mode 100644
index 00000000000..b4a924d9d77
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/src/BinningFlagger.h
@@ -0,0 +1,94 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by ASTRON, Adriaan Renting                         *
+ *   renting@astron.nl                                                     *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+#ifndef __FLAGGER_COMPLEXMEDIANFLAGGER_H__
+#define __FLAGGER_COMPLEXMEDIANFLAGGER_H__
+
+#include <casa/Arrays.h>
+#include <utility>
+#include <vector>
+#include <list>
+#include <map>
+#include "MS_File.h"
+
+namespace LOFAR
+{
+  namespace CS1
+  {
+    using casa::Cube;
+    using casa::Matrix;
+    using casa::Complex;
+    using casa::Int;
+    using casa::Double;
+    using casa::Bool;
+    using casa::Table;
+    using std::list;
+    using std::vector;
+
+    typedef pair<int, int> pairii;
+
+    class FrequencyFlagger
+    {
+      public:
+        FrequencyFlagger(MS_File* MSfile,
+                         double Threshold);
+        ~FrequencyFlagger();
+
+        void FlagDataOrBaselines(bool ExistingFlags);
+
+      protected:
+        int                     NumAntennae;
+        int                     NumPairs;
+        int                     NumBands;
+        int                     NumChannels;
+        int                     NumPolarizations;
+        int                     NumTimeslots;
+        double                  Threshold;
+        double                  MaxBaselineLength;
+        double                  NoiseLevel;
+        bool ExistingFlags;
+        vector<double>          BaselineLengths;
+        vector<pairii>          PairsIndex;
+        map<pairii, int>        BaselineIndex;
+        Cube<Complex>           TimeslotData;
+        Cube<Bool>              Flags;
+        vector<casa::String>    AntennaNames;
+        Cube< int >             Statistics;
+        MS_File* MSfile;
+
+        void DeterminePolarizationsToCheck(Bool UseOnlyXpolarizations);
+        void ComputeBaselineLengths();
+        void ProcessStatistics();
+        bool FlagBaselineBand(Matrix<Bool> Flags,
+                              Matrix<Complex> Timeslots,
+                              int* flagCounter,
+                              double FlagThreshold);
+        void FlagTimeslot(casa::TableIterator* flag_iter,
+                          bool ExistingFlags);
+        bool UpdateTimeslotData(vector<int>* OldFields,
+                                vector<int>* OldBands,
+                                int* TimeCounter,
+                                Table* TimeslotTable,
+                                double* Time);
+      private:
+    }; // FrequencyFlagger
+  }; // namespace CS1
+}; // namespace LOFAR
+
+#endif //  __FLAGGER_COMPLEXMEDIANFLAGGER_H__
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/src/Flagger.cc b/Appl/CEP/CS1/CS1_BinningFlagger/src/Flagger.cc
new file mode 100644
index 00000000000..de83a958788
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/src/Flagger.cc
@@ -0,0 +1,48 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by ASTRON, Adriaan Renting                         *
+ *   renting@astron.nl                                                     *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+#include <libgen.h>
+#include <PLC/ACCmain.h>
+#include <casa/Exceptions.h>
+#include <CS1_Flagger/FlaggerProcessControl.h>
+
+int main(int argc, char *argv[])
+{
+  try
+  {
+    INIT_LOGGER(basename(argv[0]));
+    LOFAR::CS1::FlaggerProcessControl myProcess;
+    return LOFAR::ACC::PLC::ACCmain(argc, argv, &myProcess);
+  } //try
+  catch(casa::AipsError& err)
+  {
+    std::cerr << "Aips++ error detected: " << err.getMesg() << std::endl;
+    return -2;
+  }
+  catch(...)
+  {
+    std::cerr << "** PROBLEM **: Unhandled exception caught." << std::endl;
+    return -3;
+  }
+}
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/src/FlaggerProcessControl.cc b/Appl/CEP/CS1/CS1_BinningFlagger/src/FlaggerProcessControl.cc
new file mode 100644
index 00000000000..233d87ef381
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/src/FlaggerProcessControl.cc
@@ -0,0 +1,147 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by ASTRON, Adriaan Renting                         *
+ *   renting@astron.nl                                                     *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+
+#include <iostream>
+#include <cstdlib>
+#include <iostream>
+#include <casa/Inputs/Input.h>
+#include <ms/MeasurementSets.h>
+
+#include <CS1_FrequencyFlagger/FlaggerProcessControl.h>
+#include "MS_File.h"
+#include "FrequencyFlagger.h"
+
+#define FLAGGER_VERSION "0.20"
+// 0.10 initial version
+// 0.20 added more algorithms
+
+namespace LOFAR
+{
+  namespace CS1
+  {
+    //===============>>> FlaggerProcessControl::FlaggerProcessControl  <<<===============
+    FlaggerProcessControl::FlaggerProcessControl()
+    : ProcessControl()
+    {
+      myMS       = NULL;
+      itsFlagger = NULL;
+    }
+
+    //===============>>> FlaggerProcessControl::~FlaggerProcessControl  <<<==============
+    FlaggerProcessControl::~FlaggerProcessControl()
+    {
+      if (myMS)
+      { delete myMS;
+      }
+      if (itsFlagger)
+      { delete itsFlagger;
+      }
+    }
+
+    //===============>>> FlaggerProcessControl::define  <<<==============================
+    tribool FlaggerProcessControl::define()
+    {
+      LOFAR::ACC::APS::ParameterSet* ParamSet = LOFAR::ACC::APS::globalParameterSet();
+      itsMS        = ParamSet->getString("ms");
+      itsExisting  = ParamSet->getBool("existing");
+      itsThreshold = ParamSet->getDouble("threshold");
+      return true;
+    }
+
+    //===============>>> FlaggerProcessControl::run  <<<=================================
+    tribool FlaggerProcessControl::run()
+    {
+      try{
+      std::cout << "Runnning flagger please wait..." << std::endl;
+      itsFlagger->FlagDataOrBaselines(itsExisting);
+      }
+      catch(casa::AipsError& err)
+      {
+        std::cerr << "Aips++ error detected: " << err.getMesg() << std::endl;
+        return false;
+      }
+      return true;
+    }
+
+    //===============>>> FlaggerProcessControl::init  <<<================================
+    tribool FlaggerProcessControl::init()
+    {
+      try {
+      using std::cout;
+      using std::cerr;
+      using std::endl;
+
+      cout  << string(FLAGGER_VERSION) + string(" autoflagging by Adriaan Renting for LOFAR CS1 data\n") +
+              string("This is experimental software, please report errors or requests to renting@astron.nl\n") +
+              string("Documentation can be found at: www.lofar.org/operations/doku.php?id=engineering:software:postprocessing_software\n");
+      cout << itsMS << endl;
+      myMS       = new MS_File(itsMS);
+      itsFlagger = new FrequencyFlagger (myMS, itsThreshold);
+      }
+      catch(casa::AipsError& err)
+      {
+        std::cerr << "Aips++ error detected: " << err.getMesg() << std::endl;
+        return false;
+      }
+      return true;
+    }
+
+    //===============>>> FlaggerProcessControl::pause  <<<===============================
+    tribool FlaggerProcessControl::pause(const std::string&)
+    { return false;
+    }
+
+    //===============>>> FlaggerProcessControl::quit  <<<================================
+    tribool FlaggerProcessControl::quit()
+    {
+      if (myMS)
+      {
+        delete myMS;
+        myMS = NULL;
+      }
+      if (itsFlagger)
+      {
+        delete itsFlagger;
+        itsFlagger = NULL;
+      }
+      return true;
+    }
+
+    //===============>>> FlaggerProcessControl::recover  <<<=============================
+    tribool FlaggerProcessControl::recover(const std::string&)
+    { return false;
+    }
+
+    //===============>>> FlaggerProcessControl::reinit  <<<==============================
+    tribool FlaggerProcessControl::reinit(const  std::string&)
+    { return false;
+    }
+
+    //===============>>> FlaggerProcessControl::askInfo  <<<=============================
+    std::string FlaggerProcessControl::askInfo(const std::string&)
+    { return std::string("");
+    }
+
+    //===============>>> FlaggerProcessControl::snapshot  <<<============================
+    tribool FlaggerProcessControl::snapshot(const std::string&)
+    { return false;
+    }
+  } //namespace CS1
+}; //namespace LOFAR
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/src/MS_File.cc b/Appl/CEP/CS1/CS1_BinningFlagger/src/MS_File.cc
new file mode 100644
index 00000000000..92968f9ed91
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/src/MS_File.cc
@@ -0,0 +1,182 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by ASTRON, Adriaan Renting                         *
+ *   renting@astron.nl                                                     *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+#include <casa/BasicMath/Math.h>
+#include <casa/Arrays.h>
+//#include <casa/Quanta/MVTime.h>
+
+#include <iostream>
+
+#include "MS_File.h"
+
+using namespace casa;
+
+namespace LOFAR
+{
+  namespace CS1
+  {
+    //===============>>>  MS_File::MS_File  <<<===============
+
+    MS_File::MS_File(const std::string& msname)
+    {
+      MSName = msname;
+      MS     = new MeasurementSet(MSName, Table::Update);
+      init();
+    }
+
+    //===============>>>  MS_File::~MS_File  <<<===============
+
+    MS_File::~MS_File()
+    {
+      delete MS;
+    }
+
+    //===============>>> MS_File::init  <<<===============
+
+    void MS_File::init()
+    {
+      //Number of samples
+      itsNumSamples                    = (*MS).nrow();
+      std::cout << "NumSamples" << itsNumSamples << std::endl;
+      //Number of Fields
+      MSField fields                   = (*MS).field();
+      itsNumFields                     = fields.nrow();
+      std::cout << "NumFields" << itsNumFields << std::endl;
+
+      //Number of Antennae
+      MSAntenna antennae               = (*MS).antenna();
+      itsNumAntennae                   = antennae.nrow();
+      std::cout << "NumAntennae" << itsNumAntennae << std::endl;
+
+      //Antenna Names
+      ROScalarColumn<String>           ANT_NAME_col(antennae, "NAME");
+      Vector<String>         ant_names = ANT_NAME_col.getColumn();
+      ant_names.tovector(itsAntennaNames);
+
+      //Number of channels in the Band
+      MSSpectralWindow spectral_window = (*MS).spectralWindow();
+      ROScalarColumn<Int>              NUM_CHAN_col(spectral_window, "NUM_CHAN");
+      itsNumChannels                   = NUM_CHAN_col(0);
+      std::cout << "NumChannels" << itsNumChannels << std::endl;
+
+      //Number of polarizations
+      MSPolarization      polarization = (*MS).polarization();
+      ROScalarColumn<Int>              NUM_CORR_col(polarization, "NUM_CORR");
+      itsNumPolarizations              = NUM_CORR_col(0);
+      ROArrayColumn<Int>               CORR_TYPE_col(polarization, "CORR_TYPE");
+      itsPolarizations.resize(itsNumPolarizations);
+      CORR_TYPE_col.get(0, itsPolarizations);
+      std::cout << "NumPolarizations" << itsNumPolarizations << std::endl;
+
+      //calculate theoretical noise level
+      ROScalarColumn<Double>           EXPOSURE_col((*MS), "EXPOSURE");
+      Double exposure                  = EXPOSURE_col(0);
+
+      ROScalarColumn<Double>           TOTAL_BANDWIDTH_col(spectral_window, "TOTAL_BANDWIDTH");
+      Double bandwidth                 = TOTAL_BANDWIDTH_col(0) / itsNumChannels;
+
+      itsNoiseLevel                    = 1.0 / sqrt(bandwidth * exposure);
+      std::cout << "Noiselevel" << itsNoiseLevel << std::endl;
+
+      //calculate number of timeslots
+      ROScalarColumn<Double>           INTERVAL_col((*MS), "INTERVAL");
+      Double interval                  = INTERVAL_col(0);
+
+      //Number of timeslots
+      ROScalarColumn<Double>            TIME_CENTROID_col((*MS), "TIME_CENTROID");
+      Double firstdate                 = TIME_CENTROID_col(0);
+      Double lastdate                  = TIME_CENTROID_col(itsNumSamples-1);
+      std::cout << "interval" << interval << std::endl;
+
+      itsNumTimeslots                  = (int)((lastdate-firstdate)/interval) + 1;
+      std::cout << "Numtimeslots" << itsNumTimeslots << std::endl;
+
+      //calculate number of baselines.
+      itsNumPairs = (itsNumAntennae) * (itsNumAntennae + 1) / 2; //Triangular numbers formula
+      std::cout << "NumPairs" << itsNumPairs << std::endl;
+
+      //calculate number of Bands
+      itsNumBands                      = itsNumSamples / (itsNumPairs * itsNumTimeslots);
+      std::cout << "NumBands" << itsNumBands << std::endl;
+
+    }
+
+    //===============>>> MS_File::BaselineIterator  <<<===============
+
+    MSAntenna MS_File::antenna()
+    {
+      return (*MS).antenna();
+    }
+
+    //===============>>> MS_File::BaselineIterator  <<<===============
+
+    TableIterator MS_File::TimeslotIterator()
+    {
+      Block<String> ms_iteration_variables(1);
+      ms_iteration_variables[0] = "TIME_CENTROID";
+
+      return TableIterator((*MS), ms_iteration_variables);
+    }
+
+    //===============>>> MS_File::BaselineIterator  <<<===============
+
+    TableIterator MS_File::TimeAntennaIterator()
+    {
+      Block<String> ms_iteration_variables(4);
+      ms_iteration_variables[0] = "TIME_CENTROID";
+      ms_iteration_variables[1] = "DATA_DESC_ID";
+      ms_iteration_variables[2] = "ANTENNA1";
+      ms_iteration_variables[3] = "ANTENNA2";
+
+      return TableIterator((*MS), ms_iteration_variables);
+    }
+
+    //===============>>> MS_File::WriteDataPointFlags  <<<===============
+
+    void   MS_File::WriteDataPointFlags(TableIterator* flag_iter,
+                                        Matrix<Bool>* Flags,
+                                        Bool FlagCompleteRow,
+                                        bool ExistingFlags)
+    {
+      Table         FlagTable = (*flag_iter).table();
+    //  ROTableVector<Int>      antenna1(FlagTable, "ANTENNA1");
+    //  ROTableVector<Int>      antenna2(FlagTable, "ANTENNA2");
+    //  ROTableVector<Int>      bandnr  (FlagTable, "DATA_DESC_ID");
+    //  ROTableVector<Double>   time    (FlagTable, "TIME_CENTROID");
+      ArrayColumn  <Bool>     flags   (FlagTable, "FLAG");
+      ScalarColumn <Bool>     flag_row(FlagTable, "FLAG_ROW");
+    //  ArrayColumn<String> flag_desc((*TimeslotTable), "FLAG_CATEGORY");
+      if (ExistingFlags)
+      {
+        Matrix<Bool> OldFlags;
+        Bool         OldFlagCompleteRow;
+        flags.get(0, OldFlags);
+        flag_row.get(0, OldFlagCompleteRow);
+        flags.put(0, OldFlags || (*Flags));
+      }
+      else
+      {
+        flags.put(0, (*Flags));
+      }
+      flag_row.put(0, FlagCompleteRow);
+    }
+    //===============>>> MS_File  <<<===============
+  }; // namespace CS1
+}; // namespace LOFAR
+
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/src/MS_File.h b/Appl/CEP/CS1/CS1_BinningFlagger/src/MS_File.h
new file mode 100644
index 00000000000..fce2744b04e
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/src/MS_File.h
@@ -0,0 +1,68 @@
+/***************************************************************************
+ *   Copyright (C) 2006 by ASTRON, Adriaan Renting                         *
+ *   renting@astron.nl                                                     *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
+ ***************************************************************************/
+#ifndef __FLAGGER_MS_FILE_H__
+#define __FLAGGER_MS_FILE_H__
+
+#include <ms/MeasurementSets.h>
+#include <vector>
+#include <string>
+#include <tables/Tables.h>
+#include <tables/Tables/TableIter.h>
+
+namespace LOFAR
+{
+  namespace CS1
+  {
+    class MS_File
+    {
+    public:
+      MS_File(const std::string& msname);
+      ~MS_File();
+
+      int                     itsNumSamples;
+      int                     itsNumAntennae;
+      int                     itsNumFields;
+      int                     itsNumBands;
+      int                     itsNumChannels;
+      int                     itsNumPolarizations;
+      int                     itsNumPairs;
+      int                     itsNumTimeslots;
+      double                  itsNoiseLevel;
+      std::vector<casa::String> itsAntennaNames;
+      casa::Vector<casa::Int>   itsPolarizations;
+
+      casa::TableIterator TimeslotIterator();
+      casa::TableIterator TimeAntennaIterator();
+      casa::MSAntenna     antenna();
+      void                WriteDataPointFlags(casa::TableIterator* flag_iter,
+                                              casa::Matrix<casa::Bool>* Flags,
+                                              bool FlagcompleteRow,
+                                              bool ExistingFlags);
+
+    protected:
+      string                MSName;
+      casa::MeasurementSet* MS;
+      void init();
+    private:
+    }; // MS_File
+  }; // namespace CS1
+}; // namespace LOFAR
+
+#endif // __FLAGGER_MS_FILE_H__
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/src/Makefile.am b/Appl/CEP/CS1/CS1_BinningFlagger/src/Makefile.am
new file mode 100644
index 00000000000..12c73792218
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/src/Makefile.am
@@ -0,0 +1,12 @@
+bin_PROGRAMS			= CS1_FrequencyFlagger
+
+CS1_FrequencyFlagger_SOURCES 		= Flagger.cc \
+				  FrequencyFlagger.cc \
+				  FrequencyFlagger.h \
+				  MS_File.cc \
+				  MS_File.h \
+				  FlaggerProcessControl.cc
+CS1_FrequencyFlagger_LDADD		=
+CS1_FrequencyFlagger_DEPENDENCIES	= $(LOFAR_DEPEND)
+
+include $(top_srcdir)/Makefile.common
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/test/CS1_BinningFlagger.parset b/Appl/CEP/CS1/CS1_BinningFlagger/test/CS1_BinningFlagger.parset
new file mode 100644
index 00000000000..5635bb6c229
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/test/CS1_BinningFlagger.parset
@@ -0,0 +1,8 @@
+ms=/mnt/flaggerdata/test.MS
+window=13
+crosspol=false
+min=15
+max=15
+flagrms=true
+flagdata=true
+existing=false
diff --git a/Appl/CEP/CS1/CS1_BinningFlagger/test/Makefile.am b/Appl/CEP/CS1/CS1_BinningFlagger/test/Makefile.am
new file mode 100644
index 00000000000..7d336cc2beb
--- /dev/null
+++ b/Appl/CEP/CS1/CS1_BinningFlagger/test/Makefile.am
@@ -0,0 +1,25 @@
+check_PROGRAMS        =
+
+# programs to run through supplied checktools
+CHECKTOOLPROGS        =
+#ENDCHECKTOOLPROGS
+
+# scripts used to run tests
+TESTSCRIPTS           =
+
+# scripts and possible programs to run tests
+TESTS                 = $(TESTSCRIPTS)
+
+XFAIL_TESTS           =
+
+# all files (.run, .stdout, .in, .log_prop, etc.) needed to run tests
+EXTRA_DIST            = $(TESTSCRIPTS) CS1_BinningFlagger.parset
+
+# Lines to build a test program testprg
+#testprg_SOURCES                = testprg.cc
+#testprg_LDADD                  = ../src/libcs1_inputsection.la
+#testprg_DEPENDENCIES           = ../src/libcs1_inputsection.la $(LOFAR_DEPEND)
+
+
+
+include $(top_srcdir)/Makefile.common
-- 
GitLab