diff --git a/.gitattributes b/.gitattributes
index 7ce4bbdb0d6fca776c353cd8a69a82b13d21c660..4c7633cb43f4816537bac0fa90a5fc6972308339 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -36,10 +36,11 @@ CEP/Calibration/BBSKernel/include/BBSKernel/Apply.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/BaselineMask.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/Correlation.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/CorrelationMask.h -text
-CEP/Calibration/BBSKernel/include/BBSKernel/ElementBeamExpr.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/Estimate.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/EstimateUtil.h -text
-CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaFieldAzEl.h -text
+CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaElementHBA.h -text
+CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaElementLBA.h -text
+CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaFieldThetaPhi.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/Expr/CachePolicy.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/Expr/Delay.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/Expr/ElevationCut.h -text
@@ -48,6 +49,7 @@ CEP/Calibration/BBSKernel/include/BBSKernel/Expr/FaradayRotation.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/Expr/ITRFDirection.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/Expr/IonPhaseShift.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/Expr/LinearToCircularRL.h -text
+CEP/Calibration/BBSKernel/include/BBSKernel/Expr/ParallacticRotation.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/Expr/Scope.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/Expr/ShapeletCoherence.h -text
 CEP/Calibration/BBSKernel/include/BBSKernel/Expr/ShapeletSource.h -text
@@ -75,10 +77,11 @@ CEP/Calibration/BBSKernel/src/Contrib/SBY_DipoleBeam/hba_beam_phi.c -text
 CEP/Calibration/BBSKernel/src/Contrib/SBY_DipoleBeam/hba_beam_theta.c -text
 CEP/Calibration/BBSKernel/src/Correlation.cc -text
 CEP/Calibration/BBSKernel/src/CorrelationMask.cc -text
-CEP/Calibration/BBSKernel/src/ElementBeamExpr.cc -text
 CEP/Calibration/BBSKernel/src/Estimate.cc -text
 CEP/Calibration/BBSKernel/src/EstimateUtil.cc -text
-CEP/Calibration/BBSKernel/src/Expr/AntennaFieldAzEl.cc -text
+CEP/Calibration/BBSKernel/src/Expr/AntennaElementHBA.cc -text
+CEP/Calibration/BBSKernel/src/Expr/AntennaElementLBA.cc -text
+CEP/Calibration/BBSKernel/src/Expr/AntennaFieldThetaPhi.cc -text
 CEP/Calibration/BBSKernel/src/Expr/CachePolicy.cc -text
 CEP/Calibration/BBSKernel/src/Expr/Delay.cc -text
 CEP/Calibration/BBSKernel/src/Expr/ElevationCut.cc -text
@@ -87,6 +90,7 @@ CEP/Calibration/BBSKernel/src/Expr/FaradayRotation.cc -text
 CEP/Calibration/BBSKernel/src/Expr/ITRFDirection.cc -text
 CEP/Calibration/BBSKernel/src/Expr/IonPhaseShift.cc -text
 CEP/Calibration/BBSKernel/src/Expr/LinearToCircularRL.cc -text
+CEP/Calibration/BBSKernel/src/Expr/ParallacticRotation.cc -text
 CEP/Calibration/BBSKernel/src/Expr/Scope.cc -text
 CEP/Calibration/BBSKernel/src/Expr/ShapeletCoherence.cc -text
 CEP/Calibration/BBSKernel/src/Expr/ShapeletSource.cc -text
@@ -227,6 +231,7 @@ CEP/DP3/AOFlagger/include/AOFlagger/gui/complexplaneplotwindow.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/gui/editstrategywindow.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/gui/gotowindow.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/gui/highlightwindow.h -text
+CEP/DP3/AOFlagger/include/AOFlagger/gui/histogramwindow.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/gui/imagecomparisonwidget.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/gui/imageplanewindow.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/gui/imagepropertieswindow.h -text
@@ -559,6 +564,7 @@ CEP/DP3/AOFlagger/src/gui/plotframe.cpp -text
 CEP/DP3/AOFlagger/src/gui/progresswindow.cpp -text
 CEP/DP3/AOFlagger/src/gui/quality/aoqplotwindow.cpp -text
 CEP/DP3/AOFlagger/src/gui/quality/baselineplotpage.cpp -text
+CEP/DP3/AOFlagger/src/gui/quality/datawindow.cpp -text
 CEP/DP3/AOFlagger/src/gui/quality/grayscaleplotpage.cpp -text
 CEP/DP3/AOFlagger/src/gui/quality/histogrampage.cpp -text
 CEP/DP3/AOFlagger/src/gui/quality/timefrequencyplotpage.cpp -text
diff --git a/CEP/Calibration/BBSControl/src/Step.cc b/CEP/Calibration/BBSControl/src/Step.cc
index 61e40ce06e204894f0000ff1a700afa72c47664a..fa07affa90e7d3e9d1367b5a6da85b5130b11de8 100644
--- a/CEP/Calibration/BBSControl/src/Step.cc
+++ b/CEP/Calibration/BBSControl/src/Step.cc
@@ -157,8 +157,6 @@ namespace LOFAR
         ps.add(prefix + "Model.Beam.Mode", BeamConfig::asString(config.mode()));
         ps.add(prefix + "Model.Beam.ConjugateAF",
           toString(config.conjugateAF()));
-        ps.add(prefix + "Model.Beam.Element.Path",
-          config.getElementPath().originalName());
       }
 
       ps.add(prefix + "Model.DirectionalTEC.Enable",
@@ -243,18 +241,7 @@ namespace LOFAR
         bool conjugateAF = ps.getBool("Model.Beam.ConjugateAF",
             parentConfig.conjugateAF());
 
-        string defaultPath;
-        if(itsModelConfig.useBeam()) {
-          defaultPath = parentConfig.getElementPath().originalName();
-        } else {
-          defaultPath = "$LOFARROOT/share";
-        }
-
-        string elementPath = ps.getString("Model.Beam.Element.Path",
-          defaultPath);
-
-        itsModelConfig.setBeamConfig(BeamConfig(mode, conjugateAF,
-          casa::Path(elementPath)));
+        itsModelConfig.setBeamConfig(BeamConfig(mode, conjugateAF));
       } else {
         itsModelConfig.clearBeamConfig();
       }
diff --git a/CEP/Calibration/BBSKernel/CMakeLists.txt b/CEP/Calibration/BBSKernel/CMakeLists.txt
index 2cc46bb4c2fb0307248344b4f5d4c5145c5f9ef8..7abae3e6495d9754c3e35032827e9aa93bc60376 100644
--- a/CEP/Calibration/BBSKernel/CMakeLists.txt
+++ b/CEP/Calibration/BBSKernel/CMakeLists.txt
@@ -1,6 +1,6 @@
 # $Id$
 
-lofar_package(BBSKernel 1.0 DEPENDS Blob Common ParmDB)
+lofar_package(BBSKernel 1.0 DEPENDS Blob Common ParmDB ElementResponse)
 
 include(LofarFindPackage)
 lofar_find_package(Boost REQUIRED)
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/CMakeLists.txt b/CEP/Calibration/BBSKernel/include/BBSKernel/CMakeLists.txt
index 9a181070de34869725e4a02b500db569ff635e24..45cc134da39cc2e1e8584425e8b3a1a4d6585b00 100644
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/CMakeLists.txt
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/CMakeLists.txt
@@ -39,7 +39,9 @@ install(FILES
   VisSelection.h
   DESTINATION include/${PACKAGE_NAME})
 install(FILES
-  Expr/AntennaFieldAzEl.h
+  Expr/AntennaElementLBA.h
+  Expr/AntennaElementHBA.h
+  Expr/AntennaFieldThetaPhi.h
   Expr/AzEl.h
   Expr/BasicExpr.h
   Expr/Cache.h
@@ -63,7 +65,6 @@ install(FILES
   Expr/FlagIf.h
   Expr/GaussianCoherence.h
   Expr/GaussianSource.h
-  Expr/HamakerDipole.h
   Expr/IonPhaseShift.h
   Expr/ITRFDirection.h
   Expr/LinearToCircularRL.h
@@ -81,6 +82,7 @@ install(FILES
   Expr/MatrixSum.h
   Expr/MatrixTmp.h
   Expr/MergeFlags.h
+  Expr/ParallacticRotation.h
   Expr/PhaseShift.h
   Expr/PiercePoint.h
   Expr/PointCoherence.h
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/ElementBeamExpr.h b/CEP/Calibration/BBSKernel/include/BBSKernel/ElementBeamExpr.h
deleted file mode 100644
index a25f2cc9e2cef8eca1af84fc473814d1f35dd6dd..0000000000000000000000000000000000000000
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/ElementBeamExpr.h
+++ /dev/null
@@ -1,103 +0,0 @@
-//# ElementBeamExpr.h: Wrapper class that constructs the correct beam expr based
-//# on the BeamConfig instance provided in the constructor. It also caches any
-//# shared auxilliary data.
-//#
-//# Copyright (C) 2009
-//# ASTRON (Netherlands Institute for Radio Astronomy)
-//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
-//#
-//# This file is part of the LOFAR software suite.
-//# The LOFAR software suite 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 3 of the License, or
-//# (at your option) any later version.
-//#
-//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
-//#
-//# $Id$
-
-#ifndef LOFAR_BBSKERNEL_ELEMENTBEAMEXPR_H
-#define LOFAR_BBSKERNEL_ELEMENTBEAMEXPR_H
-
-// \file
-// Wrapper class that constructs the correct beam expr based on the BeamConfig
-// instance provided in the constructor. It also caches any shared auxilliary
-// data.
-
-#include <BBSKernel/Expr/Expr.h>
-#include <BBSKernel/Expr/HamakerDipole.h>
-
-#include <Common/lofar_smartptr.h>
-
-#include <casa/OS/Path.h>
-
-namespace LOFAR
-{
-namespace BBS
-{
-
-class BeamConfig;
-class Scope;
-
-// \addtogroup BBSKernel
-// @{
-
-class ElementBeamExpr
-{
-public:
-    typedef shared_ptr<ElementBeamExpr>         Ptr;
-    typedef shared_ptr<const ElementBeamExpr>   ConstPtr;
-
-    static ElementBeamExpr::Ptr create(const BeamConfig &config, Scope &scope);
-
-    virtual ~ElementBeamExpr();
-
-    virtual Expr<JonesMatrix>::Ptr
-        construct(const Expr<Vector<2> >::ConstPtr &direction,
-            const Expr<Scalar>::ConstPtr &orientation) const = 0;
-};
-
-class HamakerBeamExpr: public ElementBeamExpr
-{
-public:
-    typedef shared_ptr<HamakerBeamExpr>         Ptr;
-    typedef shared_ptr<const HamakerBeamExpr>   ConstPtr;
-
-    HamakerBeamExpr(const BeamConfig &config, Scope &scope);
-
-    virtual Expr<JonesMatrix>::Ptr
-        construct(const Expr<Vector<2> >::ConstPtr &direction,
-            const Expr<Scalar>::ConstPtr &orientation) const;
-
-private:
-    HamakerBeamCoeff    itsCoeff;
-};
-
-class YatawattaBeamExpr: public ElementBeamExpr
-{
-public:
-    typedef shared_ptr<YatawattaBeamExpr>       Ptr;
-    typedef shared_ptr<const YatawattaBeamExpr> ConstPtr;
-
-    YatawattaBeamExpr(const BeamConfig &config, Scope &scope);
-
-    virtual Expr<JonesMatrix>::Ptr
-        construct(const Expr<Vector<2> >::ConstPtr &direction,
-            const Expr<Scalar>::ConstPtr &orientation) const;
-
-private:
-    casa::Path  itsModulePath[2];
-};
-
-// @}
-
-} //# namespace BBS
-} //# namespace LOFAR
-
-#endif
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/YatawattaDipole.h b/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaElementHBA.h
similarity index 62%
rename from CEP/Calibration/BBSKernel/include/BBSKernel/Expr/YatawattaDipole.h
rename to CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaElementHBA.h
index 404baf3ce39ebb552463e49ce4c88d4982b0356a..4e46bae67ae78e0a273b407ebe00d5cb557775f7 100644
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/YatawattaDipole.h
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaElementHBA.h
@@ -1,7 +1,7 @@
-//# YatawattaDipole.h: Dipole voltage beam using Sarod Yatawatta's analytical
-//# model.
+//# AntennaElementHBA.h: Model of an idealized LOFAR HBA dual dipole antenna
+//# element.
 //#
-//# Copyright (C) 2008
+//# Copyright (C) 2011
 //# ASTRON (Netherlands Institute for Radio Astronomy)
 //# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
 //#
@@ -21,16 +21,13 @@
 //#
 //# $Id$
 
-#ifndef LOFAR_BBSKERNEL_EXPR_YATAWATTADIPOLE_H
-#define LOFAR_BBSKERNEL_EXPR_YATAWATTADIPOLE_H
+#ifndef LOFAR_BBSKERNEL_EXPR_ANTENNAELEMENTHBA_H
+#define LOFAR_BBSKERNEL_EXPR_ANTENNAELEMENTHBA_H
 
-#include <BBSKernel/Expr/BasicExpr.h>
-#include <BBSKernel/Expr/ExternalFunction.h>
+// \file
+// Model of an idealized LOFAR HBA dual dipole antenna element.
 
-namespace casa
-{
-    class Path;
-}
+#include <BBSKernel/Expr/BasicExpr.h>
 
 namespace LOFAR
 {
@@ -40,19 +37,17 @@ namespace BBS
 // \addtogroup Expr
 // @{
 
-class YatawattaDipole: public BasicBinaryExpr<Vector<2>, Scalar, JonesMatrix>
+class AntennaElementHBA: public BasicUnaryExpr<Vector<2>, JonesMatrix>
 {
 public:
-    YatawattaDipole(const casa::Path &moduleTheta, const casa::Path &modulePhi,
-        const Expr<Vector<2> >::ConstPtr &azel,
-        const Expr<Scalar>::ConstPtr &orientation);
+    typedef shared_ptr<AntennaElementHBA>       Ptr;
+    typedef shared_ptr<const AntennaElementHBA> ConstPtr;
+
+    AntennaElementHBA(const Expr<Vector<2> >::ConstPtr &target);
 
 protected:
     virtual const JonesMatrix::View evaluateImpl(const Grid &grid,
-        const Vector<2>::View &azel, const Scalar::View &orientation) const;
-
-private:
-    ExternalFunction    itsThetaFunction, itsPhiFunction;
+        const Vector<2>::View &target) const;
 };
 
 // @}
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaElementLBA.h b/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaElementLBA.h
new file mode 100644
index 0000000000000000000000000000000000000000..ce9fbefda8a97870dcc72739210e5b0f5a6d7696
--- /dev/null
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaElementLBA.h
@@ -0,0 +1,58 @@
+//# AntennaElementLBA.h: Model of an idealized LOFAR LBA dual dipole antenna
+//# element.
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#ifndef LOFAR_BBSKERNEL_EXPR_ANTENNAELEMENTLBA_H
+#define LOFAR_BBSKERNEL_EXPR_ANTENNAELEMENTLBA_H
+
+// \file
+// Model of an idealized LOFAR LBA dual dipole antenna element.
+
+#include <BBSKernel/Expr/BasicExpr.h>
+
+namespace LOFAR
+{
+namespace BBS
+{
+
+// \addtogroup Expr
+// @{
+
+class AntennaElementLBA: public BasicUnaryExpr<Vector<2>, JonesMatrix>
+{
+public:
+    typedef shared_ptr<AntennaElementLBA>       Ptr;
+    typedef shared_ptr<const AntennaElementLBA> ConstPtr;
+
+    AntennaElementLBA(const Expr<Vector<2> >::ConstPtr &target);
+
+protected:
+    virtual const JonesMatrix::View evaluateImpl(const Grid &grid,
+        const Vector<2>::View &target) const;
+};
+
+// @}
+
+} //# namespace BBS
+} //# namespace LOFAR
+
+#endif
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaFieldAzEl.h b/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaFieldAzEl.h
deleted file mode 100644
index 7c00e7ad35c3293f23e6a06e02110c946f62e4bd..0000000000000000000000000000000000000000
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaFieldAzEl.h
+++ /dev/null
@@ -1,72 +0,0 @@
-//# AntennaFieldAzEl.h: Compute azimuth and elevation in radians relevative to
-//# the antenna field coordinate system (P, Q, R). The input direction as well
-//# as the positive coordinate axes are assumed to be unit vectors expressed in
-//# ITRF. Zero azimuth corresponds to the positive Q axis and positive azimuth
-//# runs from the positive Q axis to the positive P axis. Elevation is the angle
-//# the direction makes with the (P, Q) plane.
-//#
-//# Copyright (C) 2011
-//# ASTRON (Netherlands Institute for Radio Astronomy)
-//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
-//#
-//# This file is part of the LOFAR software suite.
-//# The LOFAR software suite 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 3 of the License, or
-//# (at your option) any later version.
-//#
-//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
-//#
-//# $Id$
-
-#ifndef LOFAR_BBSKERNEL_EXPR_ANTENNAFIELDAZEL_H
-#define LOFAR_BBSKERNEL_EXPR_ANTENNAFIELDAZEL_H
-
-// \file
-// Compute azimuth and elevation in radians relevative to the antenna field
-// coordinate system (P, Q, R). The input direction as well as the positive
-// coordinate axes are assumed to be unit vectors expressed in ITRF. Zero
-// azimuth corresponds to the positive Q axis and positive azimuth runs from the
-// positive Q axis to the positive P axis. Elevation is the angle the direction
-// makes with the (P, Q) plane.
-
-#include <BBSKernel/Expr/BasicExpr.h>
-#include <BBSKernel/Instrument.h>
-
-namespace LOFAR
-{
-namespace BBS
-{
-
-// \addtogroup Expr
-// @{
-
-class AntennaFieldAzEl: public BasicUnaryExpr<Vector<3>, Vector<2> >
-{
-public:
-    typedef shared_ptr<AntennaFieldAzEl>        Ptr;
-    typedef shared_ptr<const AntennaFieldAzEl>  ConstPtr;
-
-    AntennaFieldAzEl(const Expr<Vector<3> >::ConstPtr &direction,
-        const AntennaField::ConstPtr &field);
-
-protected:
-    virtual const Vector<2>::View evaluateImpl(const Grid &grid,
-        const Vector<3>::View &direction) const;
-
-private:
-    AntennaField::ConstPtr  itsField;
-};
-
-// @}
-
-} //# namespace BBS
-} //# namespace LOFAR
-
-#endif
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaFieldThetaPhi.h b/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaFieldThetaPhi.h
new file mode 100644
index 0000000000000000000000000000000000000000..bee80c9b4eda48993df0afb1962e033c409a6a40
--- /dev/null
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/AntennaFieldThetaPhi.h
@@ -0,0 +1,76 @@
+//# AntennaFieldThetaPhi.h: Compute topocentric (local) theta and phi spherical
+//# coordinates (in radians) relative to the Cartesian antenna field coordinate
+//# system (PQR), for a given target direction. The target direction is assumed
+//# to be an ITRF unit vector in the direction of arrival. The positive
+//# coordinate axes are assumed to be ITRF unit vectors. Zero phi corresponds to
+//# the positive P axis, and positive phi runs from the positive P axis towards
+//# the positive Q axis (roughly East over North). Theta or zenith angle is the
+//# angle the target direction makes with the positive R axis.
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#ifndef LOFAR_BBSKERNEL_EXPR_ANTENNAFIELDTHETAPHI_H
+#define LOFAR_BBSKERNEL_EXPR_ANTENNAFIELDTHETAPHI_H
+
+// \file
+// Compute topocentric (local) theta and phi spherical coordinates (in radians)
+// relative to the Cartesian antenna field coordinate system (PQR), for a given
+// target direction. The target direction is assumed to be an ITRF unit vector
+// in the direction of arrival. The positive coordinate axes are assumed to be
+// ITRF unit vectors. Zero phi corresponds to the positive P axis, and positive
+// phi runs from the positive P axis towards the positive Q axis (roughly East
+// over North). Theta or zenith angle is the angle the target direction makes
+// with the positive R axis.
+
+#include <BBSKernel/Expr/BasicExpr.h>
+#include <BBSKernel/Instrument.h>
+
+namespace LOFAR
+{
+namespace BBS
+{
+
+// \addtogroup Expr
+// @{
+
+class AntennaFieldThetaPhi: public BasicUnaryExpr<Vector<3>, Vector<2> >
+{
+public:
+    typedef shared_ptr<AntennaFieldThetaPhi>        Ptr;
+    typedef shared_ptr<const AntennaFieldThetaPhi>  ConstPtr;
+
+    AntennaFieldThetaPhi(const Expr<Vector<3> >::ConstPtr &direction,
+        const AntennaField::ConstPtr &field);
+
+protected:
+    virtual const Vector<2>::View evaluateImpl(const Grid &grid,
+        const Vector<3>::View &direction) const;
+
+private:
+    AntennaField::ConstPtr  itsField;
+};
+
+// @}
+
+} //# namespace BBS
+} //# namespace LOFAR
+
+#endif
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/HamakerDipole.h b/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/HamakerDipole.h
deleted file mode 100644
index 1471e126210786004cd157d60eed4571c47aa8c7..0000000000000000000000000000000000000000
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/HamakerDipole.h
+++ /dev/null
@@ -1,122 +0,0 @@
-//# HamakerDipole.h: Implementation of J.P. Hamaker's memo
-//# "Mathematical-physical analysis of the generic dual-dipole antenna".
-//#
-//# Copyright (C) 2008
-//# ASTRON (Netherlands Institute for Radio Astronomy)
-//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
-//#
-//# This file is part of the LOFAR software suite.
-//# The LOFAR software suite 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 3 of the License, or
-//# (at your option) any later version.
-//#
-//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
-//#
-//# $Id$
-
-#ifndef LOFAR_BBSKERNEL_EXPR_HAMAKERDIPOLE_H
-#define LOFAR_BBSKERNEL_EXPR_HAMAKERDIPOLE_H
-
-// \file
-// Implementation of J.P. Hamaker's memo "Mathematical-physical analysis of the
-// generic dual-dipole antenna".
-
-#include <BBSKernel/Expr/BasicExpr.h>
-#include <Common/lofar_complex.h>
-
-#include <casa/Arrays.h>
-
-namespace casa
-{
-    class Path;
-}
-
-namespace LOFAR
-{
-namespace BBS
-{
-
-// \addtogroup Expr
-// @{
-
-class HamakerBeamCoeff
-{
-public:
-    HamakerBeamCoeff();
-
-    void init(const casa::Path &coeffFile);
-
-    // Center frequency used to scale frequency to range [-1.0, 1.0].
-    double center() const;
-    // Width used to scale frequency to range [-1.0, 1.0].
-    double width() const;
-
-    unsigned int shape(unsigned int i) const;
-
-    dcomplex operator()(unsigned int element, unsigned int harmonic,
-        unsigned int powTheta, unsigned int powFreq) const;
-
-private:
-    double                  itsCenter, itsWidth;
-    casa::Array<dcomplex>   itsCoeff;
-};
-
-class HamakerDipole: public BasicBinaryExpr<Vector<2>, Scalar, JonesMatrix>
-{
-public:
-    typedef shared_ptr<HamakerDipole>       Ptr;
-    typedef shared_ptr<const HamakerDipole> ConstPtr;
-
-    HamakerDipole(const HamakerBeamCoeff &coeff,
-        const Expr<Vector<2> >::ConstPtr &azel,
-        const Expr<Scalar>::ConstPtr &orientation);
-
-protected:
-    virtual const JonesMatrix::View evaluateImpl(const Grid &grid,
-        const Vector<2>::View &azel, const Scalar::View &orientation) const;
-
-private:
-    HamakerBeamCoeff    itsCoeff;
-};
-
-// @}
-
-// -------------------------------------------------------------------------- //
-// - Implementation: HamakerBeamCoeff                                       - //
-// -------------------------------------------------------------------------- //
-
-inline double HamakerBeamCoeff::center() const
-{
-    return itsCenter;
-}
-
-inline double HamakerBeamCoeff::width() const
-{
-    return itsWidth;
-}
-
-inline unsigned int HamakerBeamCoeff::shape(unsigned int i) const
-{
-    // Reverse axes because casa::Array<> uses fortran order.
-    DBGASSERT(i < 4);
-    return itsCoeff.shape()(3 - i);
-}
-
-inline dcomplex HamakerBeamCoeff::operator()(unsigned int element,
-    unsigned int harmonic, unsigned int powTheta, unsigned int powFreq) const
-{
-    // Reverse axes because casa::Array<> uses fortran order.
-    return itsCoeff(casa::IPosition(4, powFreq, powTheta, harmonic, element));
-}
-
-} //# namespace BBS
-} //# namespace LOFAR
-
-#endif
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/ParallacticRotation.h b/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/ParallacticRotation.h
new file mode 100644
index 0000000000000000000000000000000000000000..32642f5ddab4f468991ce9ea585142e17c5e7399
--- /dev/null
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/Expr/ParallacticRotation.h
@@ -0,0 +1,71 @@
+//# ParallacticRotation.h: Jones matrix that relates the (X,Y)-frame used to
+//# express polarization on the sky (according to the IAU definition) to the
+//# topocentric (theta,phi)-frame. Both are Cartesian frames defined on the
+//# tangent plane, with +X towards the North, +Y towards the East, +theta away
+//# from the (pseudo) zenith, and +phi East over North around the (pseudo)
+//# zenith.
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#ifndef LOFAR_BBSKERNEL_EXPR_PARALLACTICROTATION_H
+#define LOFAR_BBSKERNEL_EXPR_PARALLACTICROTATION_H
+
+// \file
+// Jones matrix that relates the (X,Y)-frame used to express polarization on the
+// sky (according to the IAU definition) to the topocentric (theta,phi)-frame.
+// Both are Cartesian frames defined on the tangent plane, with +X towards the
+// North, +Y towards the East, +theta away from the (pseudo) zenith, and +phi
+// East over North around the (pseudo) zenith.
+
+#include <BBSKernel/Expr/BasicExpr.h>
+#include <BBSKernel/Instrument.h>
+
+namespace LOFAR
+{
+namespace BBS
+{
+
+// \addtogroup Expr
+// @{
+
+class ParallacticRotation: public BasicUnaryExpr<Vector<3>, JonesMatrix>
+{
+public:
+    typedef shared_ptr<ParallacticRotation>         Ptr;
+    typedef shared_ptr<const ParallacticRotation>   ConstPtr;
+
+    ParallacticRotation(const Expr<Vector<3> >::ConstPtr &target,
+        const AntennaField::ConstPtr &field);
+
+protected:
+    virtual const JonesMatrix::View evaluateImpl(const Grid &grid,
+        const Vector<3>::View &target) const;
+
+private:
+    AntennaField::ConstPtr  itsField;
+};
+
+// @}
+
+} //# namespace BBS
+} //# namespace LOFAR
+
+#endif
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/Instrument.h b/CEP/Calibration/BBSKernel/include/BBSKernel/Instrument.h
index a90cf6839318c6259dea27d0f0ba2b1022b8a5ee..d442b93df93be7034b58cd7b39b817b0d4eb3389 100644
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/Instrument.h
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/Instrument.h
@@ -50,8 +50,8 @@ namespace BBS
 class AntennaField
 {
 public:
-    typedef shared_ptr<AntennaField>    Ptr;
-    typedef shared_ptr<AntennaField>    ConstPtr;
+    typedef shared_ptr<AntennaField>        Ptr;
+    typedef shared_ptr<const AntennaField>  ConstPtr;
 
     enum Axis
     {
@@ -72,7 +72,7 @@ public:
 
     const string &name() const;
     const Vector3 &position() const;
-    const Vector3 &axis(Axis axis);
+    const Vector3 &axis(Axis axis) const;
 
     bool isHBA() const;
 
@@ -82,11 +82,13 @@ public:
 
     void appendElement(const Element &element);
     inline size_t nElement() const;
+    inline size_t nActiveElement() const;
     inline const Element &element(size_t i) const;
 
 private:
     string                  itsName;
     Vector3                 itsPosition;
+    size_t                  itsActiveElementCount;
     Vector3                 itsAxes[N_Axis];
     vector<Vector3>         itsTileElements;
     vector<Element>         itsElements;
@@ -95,8 +97,8 @@ private:
 class Station
 {
 public:
-    typedef shared_ptr<Station> Ptr;
-    typedef shared_ptr<Station> ConstPtr;
+    typedef shared_ptr<Station>       Ptr;
+    typedef shared_ptr<const Station> ConstPtr;
 
     Station(const string &name, const casa::MPosition &position);
     Station(const string &name, const casa::MPosition &position,
@@ -109,6 +111,9 @@ public:
 
     bool isPhasedArray() const;
     unsigned int nField() const;
+    size_t nElement() const;
+    size_t nActiveElement() const;
+
     AntennaField::ConstPtr field(unsigned int i) const;
 
 private:
@@ -120,8 +125,8 @@ private:
 class Instrument
 {
 public:
-    typedef shared_ptr<Instrument>  Ptr;
-    typedef shared_ptr<Instrument>  ConstPtr;
+    typedef shared_ptr<Instrument>        Ptr;
+    typedef shared_ptr<const Instrument>  ConstPtr;
 
     Instrument(const string &name, const casa::MPosition &position);
 
@@ -166,6 +171,11 @@ inline size_t AntennaField::nElement() const
     return itsElements.size();
 }
 
+inline size_t AntennaField::nActiveElement() const
+{
+    return itsActiveElementCount;
+}
+
 inline const AntennaField::Element &AntennaField::element(size_t i) const
 {
     return itsElements[i];
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementAIPS.h b/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementAIPS.h
index ad8d4809256d34fd9825216a8b0fd9bfdeb1be15..bdba8e923cbde5f2491d4a10de06f9ed85f2a90a 100644
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementAIPS.h
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementAIPS.h
@@ -72,24 +72,15 @@ public:
     virtual void writeHistory(const ParameterSet &parset) const;
 
     virtual BaselineMask asMask(const string &filter) const;
-
     // @}
 
 private:
-    void initInstrument();
-    Station::Ptr initStation(unsigned int id, const string &name,
-        const casa::MPosition &position) const;
     void initReferenceDirections();
     void initDimensions();
 
-    bool hasColumn(const string &column) const;
-    bool hasColumn(const casa::Table &table, const string &column) const;
     void createVisibilityColumn(const string &name);
     void createCovarianceColumn(const string &name);
 
-    bool hasSubTable(const string &table) const;
-    casa::Table getSubTable(const string &table) const;
-
     casa::MDirection getColumnPhaseReference(const string &column) const;
 
     casa::Table getVisSelection(casa::Table table,
@@ -126,6 +117,16 @@ private:
     unsigned int            itsIdDataDescription;
 };
 
+Instrument::Ptr readInstrument(const casa::MeasurementSet &ms,
+    unsigned int idObservation = 0);
+casa::MDirection readPhaseReference(const casa::MeasurementSet &ms,
+    unsigned int idField = 0);
+casa::MDirection readDelayReference(const casa::MeasurementSet &ms,
+    unsigned int idField = 0);
+casa::MDirection readTileReference(const casa::MeasurementSet &ms,
+    unsigned int idField = 0);
+double readFreqReference(const casa::MeasurementSet &ms,
+    unsigned int idDataDescription = 0);
 // @}
 
 } //# namespace BBS
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementExprLOFAR.h b/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementExprLOFAR.h
index d9e4ad4d7ef0561424c9286c45e4f6dc742ec5fc..e6f0a273780563468ea7035ea105a27c1fb8cf2b 100644
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementExprLOFAR.h
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementExprLOFAR.h
@@ -38,7 +38,6 @@
 #include <BBSKernel/VisBuffer.h>
 #include <BBSKernel/Expr/CachePolicy.h>
 #include <BBSKernel/Expr/Expr.h>
-#include <BBSKernel/Expr/HamakerDipole.h>
 #include <BBSKernel/Expr/Scope.h>
 #include <BBSKernel/Expr/Source.h>
 #include <ParmDB/ParmDB.h>
@@ -64,7 +63,7 @@ public:
     MeasurementExprLOFAR(SourceDB &sourceDB,
         const BufferMap &buffers,
         const ModelConfig &config,
-        const Instrument::Ptr &instrument,
+        const Instrument::ConstPtr &instrument,
         const BaselineSeq &baselines,
         double refFreq,
         const casa::MDirection &refPhase,
@@ -105,7 +104,7 @@ private:
     void makeForwardExpr(SourceDB &sourceDB,
         const BufferMap &buffers,
         const ModelConfig &config,
-        const Instrument::Ptr &instrument,
+        const Instrument::ConstPtr &instrument,
         double refFreq,
         const casa::MDirection &refPhase,
         const casa::MDirection &refDelay,
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementExprLOFARUtil.h b/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementExprLOFARUtil.h
index f7bcc096fcf0e4572879518e00839961693014c0..a95ed4e5afb3bd8f3aec9e462b39a5ba62da0637 100644
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementExprLOFARUtil.h
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/MeasurementExprLOFARUtil.h
@@ -43,8 +43,6 @@ namespace LOFAR
 {
 namespace BBS
 {
-class HamakerBeamCoeff;
-
 // \addtogroup BBSKernel
 // @{
 
@@ -108,9 +106,7 @@ makeBeamExpr(Scope &scope,
     const Expr<Vector<3> >::Ptr &exprITRF,
     const Expr<Vector<3> >::Ptr &exprRefDelayITRF,
     const Expr<Vector<3> >::Ptr &exprRefTileITRF,
-    const BeamConfig &config,
-    const HamakerBeamCoeff &coeffLBA,
-    const HamakerBeamCoeff &coeffHBA);
+    const BeamConfig &config);
 
 Expr<JonesMatrix>::Ptr
 makeDirectionalTECExpr(Scope &scope,
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/ModelConfig.h b/CEP/Calibration/BBSKernel/include/BBSKernel/ModelConfig.h
index 82d922dc16f86f936dcc7df4e0e422075bca6ec0..ef63b0b05bdaa84ddcde72a416d16a9537562959 100644
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/ModelConfig.h
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/ModelConfig.h
@@ -31,8 +31,6 @@
 #include <Common/lofar_vector.h>
 #include <Common/lofar_iosfwd.h>
 
-#include <casa/OS/Path.h>
-
 namespace LOFAR
 {
 namespace BBS
@@ -54,11 +52,10 @@ public:
     };
 
     BeamConfig();
-    BeamConfig(Mode mode, bool conjugateAF, const casa::Path &elementPath);
+    BeamConfig(Mode mode, bool conjugateAF);
 
     Mode mode() const;
     bool conjugateAF() const;
-    const casa::Path &getElementPath() const;
 
     static bool isDefined(Mode in);
     static Mode asMode(const string &in);
@@ -67,7 +64,6 @@ public:
 private:
     Mode            itsMode;
     bool            itsConjugateAF;
-    casa::Path      itsElementPath;
 };
 
 // Configuration options specific to the ionospheric model.
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/StationExprLOFAR.h b/CEP/Calibration/BBSKernel/include/BBSKernel/StationExprLOFAR.h
index e62521134e70881651f4fb69d4eee2ccc8bef5c9..ab8556c6b3056db594b3825efc3cfd8e9fa9c94d 100644
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/StationExprLOFAR.h
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/StationExprLOFAR.h
@@ -36,7 +36,6 @@
 #include <BBSKernel/Expr/CachePolicy.h>
 #include <BBSKernel/Expr/Expr.h>
 #include <BBSKernel/Expr/ExprValue.h>
-#include <BBSKernel/Expr/HamakerDipole.h>
 #include <BBSKernel/Expr/Scope.h>
 #include <ParmDB/SourceDB.h>
 #include <measures/Measures/MDirection.h>
@@ -87,7 +86,7 @@ private:
     void initialize(SourceDB &sourceDB,
         const BufferMap &buffers,
         const ModelConfig &config,
-        const Instrument::Ptr &instrument,
+        const Instrument::ConstPtr &instrument,
         double refFreq,
         const casa::MDirection &refPhase,
         const casa::MDirection &refDelay,
diff --git a/CEP/Calibration/BBSKernel/include/BBSKernel/StationResponse.h b/CEP/Calibration/BBSKernel/include/BBSKernel/StationResponse.h
index 38336154120064855b4ff0813b997c99d5b7472c..5541def25a4d392c1cfa8d80ec2d3dae266f0a18 100644
--- a/CEP/Calibration/BBSKernel/include/BBSKernel/StationResponse.h
+++ b/CEP/Calibration/BBSKernel/include/BBSKernel/StationResponse.h
@@ -38,7 +38,6 @@
 
 #include <ms/MeasurementSets/MeasurementSet.h>
 #include <measures/Measures/MDirection.h>
-#include <BBSKernel/Expr/HamakerDipole.h>
 
 namespace LOFAR
 {
@@ -108,9 +107,6 @@ public:
     // Set the tile delay reference direction (used by the tile beamformer).
     void setRefTile(const casa::MDirection &reference);
 
-    // Set the reference orientation of the +X dipole.
-    void setRefOrientation(double orientation);
-
     // Set the direction of interest.
     void setDirection(const casa::MDirection &direction);
 
@@ -125,11 +121,6 @@ public:
     const JonesMatrix::View evaluate(unsigned int i);
 
 private:
-    Instrument::Ptr initInstrument(const casa::MeasurementSet &ms) const;
-    Station::Ptr initStation(const casa::MeasurementSet &ms, unsigned int id,
-        const string &name, const casa::MPosition &position) const;
-    double getReferenceFreq(const casa::MeasurementSet &ms) const;
-
     // Right multiply \p lhs by \p rhs. Return \p rhs if \p lhs is
     // uninitialized.
     Expr<JonesMatrix>::Ptr compose(const Expr<JonesMatrix>::Ptr &lhs,
@@ -137,7 +128,6 @@ private:
 
     Dummy<Vector<2> >::Ptr          itsRefDelay;
     Dummy<Vector<2> >::Ptr          itsRefTile;
-    Dummy<Scalar>::Ptr              itsRefOrientation;
     Dummy<Vector<2> >::Ptr          itsDirection;
     vector<Expr<JonesMatrix>::Ptr>  itsExpr;
     Request                         itsRequest;
diff --git a/CEP/Calibration/BBSKernel/src/CMakeLists.txt b/CEP/Calibration/BBSKernel/src/CMakeLists.txt
index 7608e00f472ce181e3d709defe7680c2cfbe7fe8..153c6abe659478855401920d2d520bcfdc01ad0a 100644
--- a/CEP/Calibration/BBSKernel/src/CMakeLists.txt
+++ b/CEP/Calibration/BBSKernel/src/CMakeLists.txt
@@ -2,7 +2,7 @@
 
 include(LofarPackageVersion)
 
-add_subdirectory(Contrib)
+#add_subdirectory(Contrib)
 
 lofar_add_library(bbskernel
   Package__Version.cc
@@ -31,7 +31,9 @@ lofar_add_library(bbskernel
   VisDimensions.cc
   VisEquator.cc
   VisSelection.cc
-  Expr/AntennaFieldAzEl.cc
+  Expr/AntennaElementLBA.cc
+  Expr/AntennaElementHBA.cc
+  Expr/AntennaFieldThetaPhi.cc
   Expr/AzEl.cc
   Expr/CachePolicy.cc
   Expr/Cache.cc
@@ -52,7 +54,6 @@ lofar_add_library(bbskernel
   Expr/FlagArray.cc
   Expr/GaussianCoherence.cc
   Expr/GaussianSource.cc
-  Expr/HamakerDipole.cc
   Expr/IonPhaseShift.cc
   Expr/ITRFDirection.cc
   Expr/LinearToCircularRL.cc
@@ -69,6 +70,7 @@ lofar_add_library(bbskernel
   Expr/MatrixRep.cc
   Expr/MatrixSum.cc
   Expr/MatrixTmp.cc
+  Expr/ParallacticRotation.cc
   Expr/PhaseShift.cc
   Expr/PiercePoint.cc
   Expr/PointCoherence.cc
@@ -91,4 +93,4 @@ lofar_add_library(bbskernel
   Expr/Timer.cc)
 
 # Because we're using dlopen()
-target_link_libraries(bbskernel ${CMAKE_DL_LIBS})
+#target_link_libraries(bbskernel ${CMAKE_DL_LIBS})
diff --git a/CEP/Calibration/BBSKernel/src/Contrib/CMakeLists.txt b/CEP/Calibration/BBSKernel/src/Contrib/CMakeLists.txt
index 36a016d81e5ef8c4e341085fe9573e0a9142ce62..c633d91530b9ee6bb67194c89698cc894929d421 100644
--- a/CEP/Calibration/BBSKernel/src/Contrib/CMakeLists.txt
+++ b/CEP/Calibration/BBSKernel/src/Contrib/CMakeLists.txt
@@ -1,4 +1,4 @@
 # $Id$
 
-#add_subdirectory(SBY_DipoleBeam)
+add_subdirectory(SBY_DipoleBeam)
 add_subdirectory(JPH_DipoleBeam)
diff --git a/CEP/Calibration/BBSKernel/src/ElementBeamExpr.cc b/CEP/Calibration/BBSKernel/src/ElementBeamExpr.cc
deleted file mode 100644
index d286b894bef634ef875345fb048caafe650cef53..0000000000000000000000000000000000000000
--- a/CEP/Calibration/BBSKernel/src/ElementBeamExpr.cc
+++ /dev/null
@@ -1,115 +0,0 @@
-//# ElementBeamExpr.cc: Wrapper class that constructs the correct beam expr based on the BeamConfig instance provided in the constructor. It also caches any shared auxilliary data.
-//#
-//# Copyright (C) 2009
-//# ASTRON (Netherlands Institute for Radio Astronomy)
-//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
-//#
-//# This file is part of the LOFAR software suite.
-//# The LOFAR software suite 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 3 of the License, or
-//# (at your option) any later version.
-//#
-//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
-//#
-//# $Id$
-
-#include <lofar_config.h>
-#include <BBSKernel/ElementBeamExpr.h>
-#include <BBSKernel/Exceptions.h>
-#include <BBSKernel/ModelConfig.h>
-#include <BBSKernel/Expr/YatawattaDipole.h>
-
-namespace LOFAR
-{
-namespace BBS
-{
-
-ElementBeamExpr::~ElementBeamExpr()
-{
-}
-
-ElementBeamExpr::Ptr ElementBeamExpr::create(const BeamConfig &config,
-    Scope &scope)
-{
-    LOG_INFO_STR("Using element beam type: "
-        << BeamConfig::asString(config.getElementType()));
-    ASSERT(BeamConfig::isDefined(config.getElementType()));
-
-    switch(config.getElementType())
-    {
-        case BeamConfig::HAMAKER_LBA:
-        case BeamConfig::HAMAKER_HBA:
-        {
-            return ElementBeamExpr::Ptr(new HamakerBeamExpr(config, scope));
-        }
-
-        case BeamConfig::YATAWATTA_LBA:
-        case BeamConfig::YATAWATTA_HBA:
-        {
-            return ElementBeamExpr::Ptr(new YatawattaBeamExpr(config, scope));
-        }
-
-        default:
-            THROW(BBSKernelException, "Unsupported element type encountered.");
-    }
-}
-
-HamakerBeamExpr::HamakerBeamExpr(const BeamConfig &config, Scope&)
-{
-    ASSERT(config.getElementType() == BeamConfig::HAMAKER_LBA
-        || config.getElementType() == BeamConfig::HAMAKER_HBA);
-
-    casa::Path path = config.getElementPath();
-    path.append("element_beam_" + BeamConfig::asString(config.getElementType())
-        + ".coeff");
-    LOG_INFO_STR("Element beam config file: " << path.expandedName());
-
-    // Read beam coefficients from file.
-    itsCoeff.init(path);
-}
-
-Expr<JonesMatrix>::Ptr
-HamakerBeamExpr::construct(const Expr<Vector<2> >::ConstPtr &direction,
-    const Expr<Scalar>::ConstPtr &orientation) const
-{
-    return Expr<JonesMatrix>::Ptr(new HamakerDipole(itsCoeff, direction,
-        orientation));
-}
-
-YatawattaBeamExpr::YatawattaBeamExpr(const BeamConfig &config, Scope&)
-{
-    ASSERT(config.getElementType() == BeamConfig::YATAWATTA_LBA
-        || config.getElementType() == BeamConfig::YATAWATTA_HBA);
-
-    // TODO: Transparantly handle platforms that use a different extension for
-    // loadable modules.
-    itsModulePath[0] = config.getElementPath();
-    itsModulePath[0].append("element_beam_"
-        + BeamConfig::asString(config.getElementType()) + "_theta.so");
-
-    itsModulePath[1] = config.getElementPath();
-    itsModulePath[1].append("element_beam_"
-        + BeamConfig::asString(config.getElementType()) + "_phi.so");
-
-    LOG_INFO_STR("Element beam loadable modules: ["
-        << itsModulePath[0].expandedName() << ","
-        << itsModulePath[1].expandedName() << "]");
-}
-
-Expr<JonesMatrix>::Ptr
-YatawattaBeamExpr::construct(const Expr<Vector<2> >::ConstPtr &direction,
-    const Expr<Scalar>::ConstPtr &orientation) const
-{
-    return Expr<JonesMatrix>::Ptr(new YatawattaDipole(itsModulePath[0],
-        itsModulePath[1], direction, orientation));
-}
-
-} //# namespace BBS
-} //# namespace LOFAR
diff --git a/CEP/Calibration/BBSKernel/src/Expr/AntennaElementHBA.cc b/CEP/Calibration/BBSKernel/src/Expr/AntennaElementHBA.cc
new file mode 100644
index 0000000000000000000000000000000000000000..90ec9c88fa4fb7635167a4835ebc12707ad2d8a8
--- /dev/null
+++ b/CEP/Calibration/BBSKernel/src/Expr/AntennaElementHBA.cc
@@ -0,0 +1,100 @@
+//# AntennaElementHBA.cc: Model of an idealized LOFAR HBA dual dipole antenna
+//# element.
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#include <lofar_config.h>
+#include <BBSKernel/Expr/AntennaElementHBA.h>
+#include <ElementResponse/ElementResponse.h>
+#include <casa/BasicSL/Constants.h>
+
+namespace LOFAR
+{
+namespace BBS
+{
+
+AntennaElementHBA::AntennaElementHBA(const Expr<Vector<2> >::ConstPtr &target)
+    :   BasicUnaryExpr<Vector<2>, JonesMatrix>(target)
+{
+}
+
+const JonesMatrix::View AntennaElementHBA::evaluateImpl(const Grid &grid,
+    const Vector<2>::View &target) const
+{
+    const size_t nFreq = grid[FREQ]->size();
+    const size_t nTime = grid[TIME]->size();
+
+    // Check preconditions.
+    ASSERT(!target(0).isComplex() && target(0).nx() == 1
+        && static_cast<size_t>(target(0).ny()) == nTime);
+    ASSERT(!target(1).isComplex() && target(1).nx() == 1
+        && static_cast<size_t>(target(1).ny()) == nTime);
+
+    // Get pointers to input and output data.
+    const double *theta = target(0).doubleStorage();
+    const double *phi = target(1).doubleStorage();
+
+    Matrix E00, E01, E10, E11;
+    double *E00_re, *E00_im;
+    E00.setDCMat(nFreq, nTime);
+    E00.dcomplexStorage(E00_re, E00_im);
+
+    double *E01_re, *E01_im;
+    E01.setDCMat(nFreq, nTime);
+    E01.dcomplexStorage(E01_re, E01_im);
+
+    double *E10_re, *E10_im;
+    E10.setDCMat(nFreq, nTime);
+    E10.dcomplexStorage(E10_re, E10_im);
+
+    double *E11_re, *E11_im;
+    E11.setDCMat(nFreq, nTime);
+    E11.dcomplexStorage(E11_re, E11_im);
+
+    // Evaluate antenna model.
+    dcomplex J[2][2];
+    for(size_t i = 0; i < nTime; ++i)
+    {
+        for(size_t j = 0; j < nFreq; ++j)
+        {
+            // The positive X dipole direction is SW of the reference
+            // orientation, which translates to a phi coordinate of 5/4*pi in
+            // the topocentric spherical coordinate system. The phi coordinate
+            // is corrected for this offset before evaluating the antenna model.
+            element_response_hba(grid[FREQ]->center(j), theta[i],
+                phi[i] - 5.0 * casa::C::pi_4, J);
+
+            *E00_re++ = real(J[0][0]);
+            *E00_im++ = imag(J[0][0]);
+            *E01_re++ = real(J[0][1]);
+            *E01_im++ = imag(J[0][1]);
+            *E10_re++ = real(J[1][0]);
+            *E10_im++ = imag(J[1][0]);
+            *E11_re++ = real(J[1][1]);
+            *E11_im++ = imag(J[1][1]);
+        }
+    }
+
+    return JonesMatrix::View(E00, E01, E10, E11);
+}
+
+} //# namespace BBS
+} //# namespace LOFAR
diff --git a/CEP/Calibration/BBSKernel/src/Expr/AntennaElementLBA.cc b/CEP/Calibration/BBSKernel/src/Expr/AntennaElementLBA.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d8446801832a4b44ae00f32dd0446a6b0be3c0b3
--- /dev/null
+++ b/CEP/Calibration/BBSKernel/src/Expr/AntennaElementLBA.cc
@@ -0,0 +1,100 @@
+//# AntennaElementLBA.cc: Model of an idealized LOFAR LBA dual dipole antenna
+//# element.
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#include <lofar_config.h>
+#include <BBSKernel/Expr/AntennaElementLBA.h>
+#include <ElementResponse/ElementResponse.h>
+#include <casa/BasicSL/Constants.h>
+
+namespace LOFAR
+{
+namespace BBS
+{
+
+AntennaElementLBA::AntennaElementLBA(const Expr<Vector<2> >::ConstPtr &target)
+    :   BasicUnaryExpr<Vector<2>, JonesMatrix>(target)
+{
+}
+
+const JonesMatrix::View AntennaElementLBA::evaluateImpl(const Grid &grid,
+    const Vector<2>::View &target) const
+{
+    const size_t nFreq = grid[FREQ]->size();
+    const size_t nTime = grid[TIME]->size();
+
+    // Check preconditions.
+    ASSERT(!target(0).isComplex() && target(0).nx() == 1
+        && static_cast<size_t>(target(0).ny()) == nTime);
+    ASSERT(!target(1).isComplex() && target(1).nx() == 1
+        && static_cast<size_t>(target(1).ny()) == nTime);
+
+    // Get pointers to input and output data.
+    const double *theta = target(0).doubleStorage();
+    const double *phi = target(1).doubleStorage();
+
+    Matrix E00, E01, E10, E11;
+    double *E00_re, *E00_im;
+    E00.setDCMat(nFreq, nTime);
+    E00.dcomplexStorage(E00_re, E00_im);
+
+    double *E01_re, *E01_im;
+    E01.setDCMat(nFreq, nTime);
+    E01.dcomplexStorage(E01_re, E01_im);
+
+    double *E10_re, *E10_im;
+    E10.setDCMat(nFreq, nTime);
+    E10.dcomplexStorage(E10_re, E10_im);
+
+    double *E11_re, *E11_im;
+    E11.setDCMat(nFreq, nTime);
+    E11.dcomplexStorage(E11_re, E11_im);
+
+    // Evaluate antenna model.
+    dcomplex J[2][2];
+    for(size_t i = 0; i < nTime; ++i)
+    {
+        for(size_t j = 0; j < nFreq; ++j)
+        {
+            // The positive X dipole direction is SW of the reference
+            // orientation, which translates to a phi coordinate of 5/4*pi in
+            // the topocentric spherical coordinate system. The phi coordinate
+            // is corrected for this offset before evaluating the antenna model.
+            element_response_lba(grid[FREQ]->center(j), theta[i],
+                phi[i] - 5.0 * casa::C::pi_4, J);
+
+            *E00_re++ = real(J[0][0]);
+            *E00_im++ = imag(J[0][0]);
+            *E01_re++ = real(J[0][1]);
+            *E01_im++ = imag(J[0][1]);
+            *E10_re++ = real(J[1][0]);
+            *E10_im++ = imag(J[1][0]);
+            *E11_re++ = real(J[1][1]);
+            *E11_im++ = imag(J[1][1]);
+        }
+    }
+
+    return JonesMatrix::View(E00, E01, E10, E11);
+}
+
+} //# namespace BBS
+} //# namespace LOFAR
diff --git a/CEP/Calibration/BBSKernel/src/Expr/AntennaFieldAzEl.cc b/CEP/Calibration/BBSKernel/src/Expr/AntennaFieldThetaPhi.cc
similarity index 53%
rename from CEP/Calibration/BBSKernel/src/Expr/AntennaFieldAzEl.cc
rename to CEP/Calibration/BBSKernel/src/Expr/AntennaFieldThetaPhi.cc
index 567ac7932f0a61491e2f9a876359c5c3d2eef116..3455e296c705d1429c8aa6c1dd691e1fff55986a 100644
--- a/CEP/Calibration/BBSKernel/src/Expr/AntennaFieldAzEl.cc
+++ b/CEP/Calibration/BBSKernel/src/Expr/AntennaFieldThetaPhi.cc
@@ -1,9 +1,11 @@
-//# AntennaFieldAzEl.cc: Compute azimuth and elevation in radians relevative to
-//# the antenna field coordinate system (P, Q, R). The input direction as well
-//# as the positive coordinate axes are assumed to be unit vectors expressed in
-//# ITRF. Zero azimuth corresponds to the positive Q axis and positive azimuth
-//# runs from the positive Q axis to the positive P axis. Elevation is the angle
-//# the direction makes with the (P, Q) plane.
+//# AntennaFieldThetaPhi.cc: Compute topocentric (local) theta and phi spherical
+//# coordinates (in radians) relative to the Cartesian antenna field coordinate
+//# system (PQR), for a given target direction. The target direction is assumed
+//# to be an ITRF unit vector in the direction of arrival. The positive
+//# coordinate axes are assumed to be ITRF unit vectors. Zero phi corresponds to
+//# the positive P axis, and positive phi runs from the positive P axis towards
+//# the positive Q axis (roughly East over North). Theta or zenith angle is the
+//# angle the target direction makes with the positive R axis.
 //#
 //# Copyright (C) 2011
 //# ASTRON (Netherlands Institute for Radio Astronomy)
@@ -26,7 +28,7 @@
 //# $Id$
 
 #include <lofar_config.h>
-#include <BBSKernel/Expr/AntennaFieldAzEl.h>
+#include <BBSKernel/Expr/AntennaFieldThetaPhi.h>
 #include <casa/BasicSL/Constants.h>
 
 namespace LOFAR
@@ -34,14 +36,14 @@ namespace LOFAR
 namespace BBS
 {
 
-AntennaFieldAzEl::AntennaFieldAzEl(const Expr<Vector<3> >::ConstPtr &direction,
+AntennaFieldThetaPhi::AntennaFieldThetaPhi(const Expr<Vector<3> >::ConstPtr &direction,
     const AntennaField::ConstPtr &field)
     :   BasicUnaryExpr<Vector<3>, Vector<2> >(direction),
         itsField(field)
 {
 }
 
-const Vector<2>::View AntennaFieldAzEl::evaluateImpl(const Grid&,
+const Vector<2>::View AntennaFieldThetaPhi::evaluateImpl(const Grid&,
     const Vector<3>::View &direction) const
 {
     // Check preconditions.
@@ -61,19 +63,27 @@ const Vector<2>::View AntennaFieldAzEl::evaluateImpl(const Grid&,
         + direction(2) * q[2];
 
     // Compute the inner product between the antenna field normal (R) and the
-    // direction vector to get the sine of the elevation (cosine of the zenith
-    // angle).
-    Matrix sinEl = direction(0) * r[0] + direction(1) * r[1]
+    // direction vector to get the cosine of the zenith angle (sine of the
+    // elevation).
+    Matrix projectionR = direction(0) * r[0] + direction(1) * r[1]
         + direction(2) * r[2];
 
-    // Compute azimuth and elevation. Zero azimuth corresponds to the positive Q
-    // axis and positive azimuth runs from the positive Q axis to the positive P
-    // axis. Elevation is computed by taking the arcsine of the angle computed
-    // earlier.
-    Vector<2>::View azel;
-    azel.assign(0, atan2(projectionP, projectionQ));
-    azel.assign(1, asin(sinEl));
-    return azel;
+    // Compute theta and phi. Zero phi corresponds to the positive P axis and
+    // positive phi runs from the positive P axis to the positive Q axis.
+    Vector<2>::View result;
+    result.assign(0, acos(projectionR));
+    result.assign(1, atan2(projectionQ, projectionP));
+
+//    LOG_DEBUG_STR("EP: [" << p[0] << ", " << p[1] << ", " << p[2] << "]");
+//    LOG_DEBUG_STR("EQ: [" << q[0] << ", " << q[1] << ", " << q[2] << "]");
+//    LOG_DEBUG_STR("ER: [" << r[0] << ", " << r[1] << ", " << r[2] << "]");
+//    LOG_DEBUG_STR("X: " << direction(0));
+//    LOG_DEBUG_STR("Y: " << direction(1));
+//    LOG_DEBUG_STR("Z: " << direction(2));
+//    LOG_DEBUG_STR("THETA: " << result(0));
+//    LOG_DEBUG_STR("PHI: " << result(1));
+
+    return result;
 }
 
 } //# namespace BBS
diff --git a/CEP/Calibration/BBSKernel/src/Expr/HamakerDipole.cc b/CEP/Calibration/BBSKernel/src/Expr/HamakerDipole.cc
deleted file mode 100644
index c157c52a1981601cf80c8161d4e7f82968f41f7e..0000000000000000000000000000000000000000
--- a/CEP/Calibration/BBSKernel/src/Expr/HamakerDipole.cc
+++ /dev/null
@@ -1,261 +0,0 @@
-//# HamakerDipole.cc: Implementation of J.P. Hamaker's memo
-//# "Mathematical-physical analysis of the generic dual-dipole antenna".
-//#
-//# Copyright (C) 2008
-//# ASTRON (Netherlands Institute for Radio Astronomy)
-//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
-//#
-//# This file is part of the LOFAR software suite.
-//# The LOFAR software suite 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 3 of the License, or
-//# (at your option) any later version.
-//#
-//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
-//#
-//# $Id$
-
-#include <lofar_config.h>
-
-#include <BBSKernel/Expr/HamakerDipole.h>
-#include <BBSKernel/Exceptions.h>
-
-#include <casa/BasicSL/Constants.h>
-#include <casa/OS/Path.h>
-
-namespace LOFAR
-{
-namespace BBS
-{
-
-HamakerBeamCoeff::HamakerBeamCoeff()
-    :   itsCenter(0.0),
-        itsWidth(1.0)
-{
-}
-
-void HamakerBeamCoeff::init(const casa::Path &coeffFile)
-{
-    // Open file.
-    casa::String path = coeffFile.expandedName();
-    ifstream in(path.c_str());
-    if(!in)
-    {
-        THROW(BBSKernelException, "" << path << ": unable to open file");
-    }
-
-    // Read file header.
-    string header, token0, token1, token2, token3, token4, token5;
-    getline(in, header);
-
-    size_t nElements, nHarmonics, nPowerTime, nPowerFreq;
-    double freqAvg, freqRange;
-
-    istringstream iss(header);
-    iss >> token0 >> nElements >> token1 >> nHarmonics >> token2 >> nPowerTime
-        >> token3 >> nPowerFreq >> token4 >> freqAvg >> token5 >> freqRange;
-
-    if(!in || !iss || token0 != "d" || token1 != "k" || token2 != "pwrT"
-        || token3 != "pwrF" || token4 != "freqAvg" || token5 != "freqRange")
-    {
-        THROW(BBSKernelException, "" << path << ": unable to parse header");
-    }
-
-    if(nElements * nHarmonics * nPowerTime * nPowerFreq == 0)
-    {
-        THROW(BBSKernelException, "" << path << ": the number of coefficients"
-            " should be larger than zero.");
-    }
-
-    LOG_DEBUG_STR("" << path << ": nElements: " << nElements << " nHarmonics: "
-        << nHarmonics << " nPowerTime: " << nPowerTime << " nPowerFreq: "
-        << nPowerFreq);
-
-    // Allocate coefficient matrix.
-    itsCenter = freqAvg;
-    itsWidth = freqRange;
-    itsCoeff = casa::Array<dcomplex>(casa::IPosition(4, nPowerFreq, nPowerTime,
-        nHarmonics, nElements));
-
-    size_t nCoeff = 0;
-    while(in.good())
-    {
-        // Read line from file.
-        string line;
-        getline(in, line);
-
-        // Skip lines that contain only whitespace.
-        if(line.find_last_not_of(" ") == string::npos)
-        {
-            continue;
-        }
-
-        // Parse line.
-        size_t element, harmonic, powerTime, powerFreq;
-        double re, im;
-
-        iss.clear();
-        iss.str(line);
-        iss >> element >> harmonic >> powerTime >> powerFreq >> re >> im;
-
-        if(!iss || element >= nElements || harmonic >= nHarmonics
-            || powerTime >= nPowerTime || powerFreq >= nPowerFreq)
-        {
-            THROW(BBSKernelException, "" << path << ": errror reading file.");
-        }
-
-        // Store coefficient.
-        itsCoeff(casa::IPosition(4, powerFreq, powerTime, harmonic, element)) =
-            makedcomplex(re, im);
-
-        // Update coefficient counter.
-        ++nCoeff;
-    }
-
-    if(!in.eof())
-    {
-        THROW(BBSKernelException, "" << path << ": error reading file.");
-    }
-
-    if(nCoeff != nElements * nHarmonics * nPowerTime * nPowerFreq)
-    {
-        THROW(BBSKernelException, "" << path << ": the number of coefficients"
-            " specified in the header does not match the number of coefficients"
-            " in the file.");
-    }
-}
-
-HamakerDipole::HamakerDipole(const HamakerBeamCoeff &coeff,
-    const Expr<Vector<2> >::ConstPtr &azel,
-    const Expr<Scalar>::ConstPtr &orientation)
-    :   BasicBinaryExpr<Vector<2>, Scalar, JonesMatrix>(azel, orientation),
-        itsCoeff(coeff)
-{
-}
-
-const JonesMatrix::View HamakerDipole::evaluateImpl(const Grid &grid,
-    const Vector<2>::View &azel, const Scalar::View &orientation) const
-{
-    const size_t nFreq = grid[FREQ]->size();
-    const size_t nTime = grid[TIME]->size();
-
-    // Check preconditions.
-    ASSERT(static_cast<size_t>(azel(0).nelements()) == nTime);
-    ASSERT(static_cast<size_t>(azel(1).nelements()) == nTime);
-    ASSERT(static_cast<size_t>(orientation().nelements()) == 1);
-
-    // Get pointers to input and output data.
-    const double *az = azel(0).doubleStorage();
-    const double *el = azel(1).doubleStorage();
-    const double angle = orientation().getDouble(0, 0);
-
-    Matrix E00, E01, E10, E11;
-    double *E00_re, *E00_im;
-    E00.setDCMat(nFreq, nTime);
-    E00.dcomplexStorage(E00_re, E00_im);
-
-    double *E01_re, *E01_im;
-    E01.setDCMat(nFreq, nTime);
-    E01.dcomplexStorage(E01_re, E01_im);
-
-    double *E10_re, *E10_im;
-    E10.setDCMat(nFreq, nTime);
-    E10.dcomplexStorage(E10_re, E10_im);
-
-    double *E11_re, *E11_im;
-    E11.setDCMat(nFreq, nTime);
-    E11.dcomplexStorage(E11_re, E11_im);
-
-    // Evaluate beam.
-    const unsigned int nHarmonics = itsCoeff.shape(1);
-    const unsigned int nPowTheta = itsCoeff.shape(2);
-    const unsigned int nPowFreq = itsCoeff.shape(3);
-
-    for(size_t t = 0; t < nTime; ++t)
-    {
-        // Correct azimuth for dipole orientation.
-        const double phi = az[t] - angle;
-
-        // NB: The model is parameterized in terms of zenith angle. The
-        // appropriate conversion is taken care of below.
-        const double theta = casa::C::pi_2 - el[t];
-
-        for(size_t f = 0; f < nFreq; ++f)
-        {
-            // J-jones matrix (2x2 complex matrix)
-            dcomplex J[2][2] = {{0.0, 0.0}, {0.0, 0.0}};
-
-            // Only compute the beam response for directions above the horizon.
-            if(theta < casa::C::pi_2)
-            {
-                // NB: The model is parameterized in terms of a normalized
-                // frequency in the range [-1, 1]. The appropriate conversion is
-                // taken care of below.
-                const double normFreq = (grid[FREQ]->center(f)
-                    - itsCoeff.center()) / itsCoeff.width();
-
-                for(size_t k = 0; k < nHarmonics; ++k)
-                {
-                    // Compute diagonal projection matrix P for the current
-                    // harmonic.
-                    dcomplex P[2] = {0.0, 0.0};
-
-                    dcomplex inner[2];
-                    for(long i = nPowTheta - 1; i >= 0; --i)
-                    {
-                        inner[0] = itsCoeff(0, k, i, nPowFreq - 1);
-                        inner[1] = itsCoeff(1, k, i, nPowFreq - 1);
-
-                        for(long j = nPowFreq - 2; j >= 0; --j)
-                        {
-                            inner[0] =
-                                inner[0] * normFreq + itsCoeff(0, k, i, j);
-                            inner[1] =
-                                inner[1] * normFreq + itsCoeff(1, k, i, j);
-                        }
-                        P[0] = P[0] * theta + inner[0];
-                        P[1] = P[1] * theta + inner[1];
-                    }
-
-                    // Compute Jones matrix for this harmonic by rotating P over
-                    // kappa * phi and add it to the result.
-                    const double kappa =
-                        ((k & 1) == 0 ? 1.0 : -1.0) * (2.0 * k + 1.0);
-                    const double cphi = std::cos(kappa * phi);
-                    const double sphi = std::sin(kappa * phi);
-
-                    J[0][0] += cphi * P[0];
-                    J[0][1] += -sphi * P[1];
-                    J[1][0] += sphi * P[0];
-                    J[1][1] += cphi * P[1];
-                }
-            }
-
-            *E00_re++ = real(J[0][0]);
-            *E00_im++ = imag(J[0][0]);
-            *E01_re++ = real(J[0][1]);
-            *E01_im++ = imag(J[0][1]);
-            *E10_re++ = real(J[1][0]);
-            *E10_im++ = imag(J[1][0]);
-            *E11_re++ = real(J[1][1]);
-            *E11_im++ = imag(J[1][1]);
-        }
-    }
-
-    JonesMatrix::View result;
-    result.assign(0, 0, E00);
-    result.assign(0, 1, E01);
-    result.assign(1, 0, E10);
-    result.assign(1, 1, E11);
-
-    return result;
-}
-
-} //# namespace BBS
-} //# namespace LOFAR
diff --git a/CEP/Calibration/BBSKernel/src/Expr/ParallacticRotation.cc b/CEP/Calibration/BBSKernel/src/Expr/ParallacticRotation.cc
new file mode 100644
index 0000000000000000000000000000000000000000..50fe91836a62c5c9164e02766d424a72125fa86f
--- /dev/null
+++ b/CEP/Calibration/BBSKernel/src/Expr/ParallacticRotation.cc
@@ -0,0 +1,166 @@
+//# ParallacticRotation.cc: Jones matrix that relates the (X,Y)-frame used to
+//# express polarization on the sky (according to the IAU definition) to the
+//# topocentric (theta,phi)-frame. Both are Cartesian frames defined on the
+//# tangent plane, with +X towards the North, +Y towards the East, +theta away
+//# from the (pseudo) zenith, and +phi East over North around the (pseudo)
+//# zenith.
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#include <lofar_config.h>
+#include <BBSKernel/Expr/ParallacticRotation.h>
+#include <Common/LofarLogger.h>
+
+namespace LOFAR
+{
+namespace BBS
+{
+
+ParallacticRotation::ParallacticRotation(
+    const Expr<Vector<3> >::ConstPtr &target,
+    const AntennaField::ConstPtr &field)
+    :   BasicUnaryExpr<Vector<3>, JonesMatrix>(target),
+        itsField(field)
+{
+}
+
+const JonesMatrix::View ParallacticRotation::evaluateImpl(const Grid &grid,
+    const Vector<3>::View &target) const
+{
+    const size_t nFreq = grid[FREQ]->size();
+    const size_t nTime = grid[TIME]->size();
+
+    // Check preconditions.
+    ASSERT(!target(0).isComplex() && target(0).nx() == 1
+        && static_cast<size_t>(target(0).ny()) == nTime);
+    ASSERT(!target(1).isComplex() && target(1).nx() == 1
+        && static_cast<size_t>(target(1).ny()) == nTime);
+    ASSERT(!target(2).isComplex() && target(2).nx() == 1
+        && static_cast<size_t>(target(2).ny()) == nTime);
+
+    // Compute the cross product of the NCP and the target direction. This
+    // yields a vector tangent to the celestial sphere at the target direction,
+    // pointing towards the East (the direction of +Y in the IAU definition,
+    // or positive right ascension).
+    Matrix v1x = -target(1);
+    Matrix v1y = target(0);
+    Matrix v1z = Matrix(0.0);
+
+    Matrix v1norm = sqrt(sqr(v1x) + sqr(v1y) + sqr(v1z));
+    v1x = v1x / v1norm;
+    v1y = v1y / v1norm;
+    v1z = v1z / v1norm;
+
+    // Compute the cross product of the antenna field normal (R) and the target
+    // direction. This yields a vector tangent to the topocentric spherical
+    // coordinate system at the target direction, pointing towards the direction
+    // of positive phi (which runs East over North around the pseudo zenith).
+    const Vector3 &r = itsField->axis(AntennaField::R);
+    Matrix v2x = target(2) * r[1] - target(1) * r[2];
+    Matrix v2y = target(0) * r[2] - target(2) * r[0];
+    Matrix v2z = target(1) * r[0] - target(0) * r[1];
+
+    Matrix v2norm = sqrt(sqr(v2x) + sqr(v2y) + sqr(v2z));
+    v2x = v2x / v2norm;
+    v2y = v2y / v2norm;
+    v2z = v2z / v2norm;
+
+    // Compute the cosine and sine of the parallactic angle, i.e. the angle
+    // between v1 and v2, both tangent to a latitude circle of their respective
+    // spherical coordinate systems.
+    Matrix coschi = v1x * v2x + v1y * v2y + v1z * v2z;
+    Matrix sinchi = (v1y * v2z - v1z * v2y) * target(0)
+        + (v1z * v2x - v1x * v2z) * target(1)
+        + (v1x * v2y - v1y * v2x) * target(2);
+
+    // The input coordinate system is a right handed system with its third axis
+    // along the direction of propagation (IAU +Z). The output coordinate system
+    // is right handed as well, but its third axis points in the direction of
+    // arrival (i.e. exactly opposite).
+    //
+    // Because the electromagnetic field is always perpendicular to the
+    // direction of propagation, we only need to relate the (X, Y) axes of the
+    // input system to the corresponding (theta, phi) axes of the output system.
+    //
+    // To this end, we first rotate the input system around its third axis to
+    // align the Y axis with the phi axis. The X and theta axis are parallel
+    // after this rotation, but point in opposite directions. To align the X
+    // axis with the theta axis, we flip it.
+    //
+    // The Jones matrix to align the Y axis with the phi axis when these are
+    // separated by an angle phi (measured counter-clockwise around the
+    // direction of propagation, looking towards the origin), is given by:
+    //
+    // [ cos(phi)  sin(phi)]
+    // [-sin(phi)  cos(phi)]
+    //
+    // Here, cos(phi) and sin(phi) can be computed directly, without having to
+    // compute phi first (see the computation of coschi and sinchi above).
+    //
+    // Now, sinchi as computed above is opposite to sin(phi), because the
+    // direction used in the computation is the direction of arrival instead
+    // of the direction of propagation. Therefore, the sign of sinchi needs to
+    // be reversed. Furthermore, as explained above, the X axis has to be
+    // flipped to align with the theta axis. The Jones matrix returned from this
+    // function is therefore given by:
+    //
+    // [-coschi  sinchi]
+    // [ sinchi  coschi]
+    //
+    // This is an improper rotation, or rotoreflection.
+
+//    LOG_DEBUG_STR("COS: " << coschi);
+//    LOG_DEBUG_STR("SIN: " << sinchi);
+
+    ASSERT(!coschi.isComplex() && coschi.nx() == 1
+        && static_cast<size_t>(coschi.ny()) == nTime);
+    ASSERT(!sinchi.isComplex() && sinchi.nx() == 1
+        && static_cast<size_t>(sinchi.ny()) == nTime);
+
+    if(nFreq == 1)
+    {
+        return JonesMatrix::View(-coschi, sinchi, sinchi, coschi);
+    }
+
+    // Create 2D arrays because MeqMatrix does not support 1D.
+    const double *p_coschi = coschi.doubleStorage();
+    const double *p_sinchi = sinchi.doubleStorage();
+
+    Matrix coschi2, sinchi2;
+    double *p_coschi2 = coschi2.setDoubleFormat(nFreq, nTime);
+    double *p_sinchi2 = sinchi2.setDoubleFormat(nFreq, nTime);
+
+    for(unsigned int t = 0; t < nTime; ++t)
+    {
+        for(unsigned int f = 0; f < nFreq; ++f)
+        {
+            *p_coschi2++ = *p_coschi;
+            *p_sinchi2++ = *p_sinchi;
+        }
+        ++p_coschi;
+        ++p_sinchi;
+    }
+
+    return JonesMatrix::View(-coschi2, sinchi2, sinchi2, coschi2);
+}
+
+} //# namespace BBS
+} //# namespace LOFAR
diff --git a/CEP/Calibration/BBSKernel/src/Expr/StationBeamFormer.cc b/CEP/Calibration/BBSKernel/src/Expr/StationBeamFormer.cc
index fde5e02b52349a81390c0158651b3c0d2360dfe4..15dd68fa90e03318d4196f01b3182e52658a562f 100644
--- a/CEP/Calibration/BBSKernel/src/Expr/StationBeamFormer.cc
+++ b/CEP/Calibration/BBSKernel/src/Expr/StationBeamFormer.cc
@@ -221,7 +221,6 @@ const JonesMatrix::View StationBeamFormer::evaluateImpl(const Grid &grid,
     Matrix E[2][2];
     Matrix AF(makedcomplex(0.0, 0.0), nFreq, nTime);
 
-    size_t countX = 0, countY = 0;
     for(size_t i = 0; i < itsStation->nField(); ++i)
     {
         AntennaField::ConstPtr field = itsStation->field(i);
@@ -285,13 +284,11 @@ const JonesMatrix::View StationBeamFormer::evaluateImpl(const Grid &grid,
             if(!element.flag[0])
             {
                 AFX += AF;
-                ++countX;
             }
 
             if(!element.flag[1])
             {
                 AFY += AF;
-                ++countY;
             }
         }
 
@@ -319,16 +316,13 @@ const JonesMatrix::View StationBeamFormer::evaluateImpl(const Grid &grid,
     }
 
     // Normalize.
-    if(countX > 0)
+    const size_t nActiveElement = itsStation->nActiveElement();
+    if(nActiveElement > 0)
     {
-        E[0][0] /= countX;
-        E[0][1] /= countX;
-    }
-
-    if(countY > 0)
-    {
-        E[1][0] /= countY;
-        E[1][1] /= countY;
+        E[0][0] /= nActiveElement;
+        E[0][1] /= nActiveElement;
+        E[1][0] /= nActiveElement;
+        E[1][1] /= nActiveElement;
     }
 
     return JonesMatrix::View(E[0][0], E[0][1], E[1][0], E[1][1]);
diff --git a/CEP/Calibration/BBSKernel/src/Expr/YatawattaDipole.cc b/CEP/Calibration/BBSKernel/src/Expr/YatawattaDipole.cc
deleted file mode 100644
index 6e53ed87da5de1f8c85aa0ea38c16747c069cc37..0000000000000000000000000000000000000000
--- a/CEP/Calibration/BBSKernel/src/Expr/YatawattaDipole.cc
+++ /dev/null
@@ -1,150 +0,0 @@
-//# YatawattaDipole.cc: Dipole voltage beam based on external functions.
-//#
-//# Copyright (C) 2008
-//# ASTRON (Netherlands Institute for Radio Astronomy)
-//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
-//#
-//# This file is part of the LOFAR software suite.
-//# The LOFAR software suite 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 3 of the License, or
-//# (at your option) any later version.
-//#
-//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
-//#
-//# $Id$
-
-#include <lofar_config.h>
-
-#include <BBSKernel/Expr/YatawattaDipole.h>
-
-#include <Common/lofar_complex.h>
-#include <casa/BasicSL/Constants.h>
-
-namespace LOFAR
-{
-namespace BBS
-{
-
-YatawattaDipole::YatawattaDipole(const casa::Path &moduleTheta,
-    const casa::Path &modulePhi, const Expr<Vector<2> >::ConstPtr &azel,
-    const Expr<Scalar>::ConstPtr &orientation)
-    :   BasicBinaryExpr<Vector<2>, Scalar, JonesMatrix>(azel, orientation),
-        itsThetaFunction(moduleTheta, "test"),
-        itsPhiFunction(modulePhi, "test")
-{
-    ASSERT(itsThetaFunction.nArguments() == 5);
-    ASSERT(itsPhiFunction.nArguments() == 5);
-}
-
-const JonesMatrix::View YatawattaDipole::evaluateImpl(const Grid &grid,
-    const Vector<2>::View &azel, const Scalar::View &orientation) const
-{
-    const size_t nFreq = grid[FREQ]->size();
-    const size_t nTime = grid[TIME]->size();
-
-    // Check preconditions.
-    ASSERT(static_cast<size_t>(azel(0).nelements()) == nTime);
-    ASSERT(static_cast<size_t>(azel(1).nelements()) == nTime);
-    ASSERT(static_cast<size_t>(orientation().nelements()) == 1);
-
-    // Get pointers to input and output data.
-    const double *az = azel(0).doubleStorage();
-    const double *el = azel(1).doubleStorage();
-    const double angle = orientation().getDouble(0, 0);
-
-    Matrix E00, E01, E10, E11;
-    double *E00_re, *E00_im;
-    E00.setDCMat(nFreq, nTime);
-    E00.dcomplexStorage(E00_re, E00_im);
-
-    double *E01_re, *E01_im;
-    E01.setDCMat(nFreq, nTime);
-    E01.dcomplexStorage(E01_re, E01_im);
-
-    double *E10_re, *E10_im;
-    E10.setDCMat(nFreq, nTime);
-    E10.dcomplexStorage(E10_re, E10_im);
-
-    double *E11_re, *E11_im;
-    E11.setDCMat(nFreq, nTime);
-    E11.dcomplexStorage(E11_re, E11_im);
-
-    //  Parameters for external functions:
-    //      0: time
-    //          (NOTE: ignored)
-    //      1: frequency
-    //      2: az
-    //      3: el
-    //          (NOTE: incorrectly labelled zenith angle in implementation!)
-    //      4: orientation (phi0)
-
-    // Create argument vectors for the X and Y dipole (used for calling
-    // external functions).
-    vector<dcomplex> xArgs(5, makedcomplex(0.0, 0.0));
-    vector<dcomplex> yArgs(5, makedcomplex(0.0, 0.0));
-
-    // TODO: Inside external function, these parameters are added to the
-    // azimuth. The resulting azimuth is therefore:
-    //
-    // az = az + orientation (- pi / 2.0)
-    //
-    // Whereas it seems to me that the orientation should be subtracted
-    // instead of added. It probably does not matter much, because the
-    // beam pattern is symmetric with respect to azimuth.
-    xArgs[4] = makedcomplex(angle, 0.0);
-    yArgs[4] = makedcomplex(angle - casa::C::pi_2, 0.0);
-
-    // Evaluate beam.
-    for(size_t t = 0; t < nTime; ++t)
-    {
-        // TODO: Where does the -pi/4 term in azimuth come from (see
-        // global_model.py in EJones_HBA)? Is this just the default dipole
-        // orientation? If so, the term should be removed in favor of setting
-        // a correct dipole orientation in the parameter database (such that
-        // the orientation in the parameter database corresponds 1:1 with the
-        // real orientation).
-//        xArgs[2] = yArgs[2] = makedcomplex(az[t] - casa::C::pi_4, 0.0);
-        xArgs[2] = yArgs[2] = makedcomplex(az[t], 0.0);
-        xArgs[3] = yArgs[3] = makedcomplex(el[t], 0.0);
-
-        for(size_t f = 0; f < nFreq; ++f)
-        {
-            // Update frequency.
-            xArgs[1] = yArgs[1] = makedcomplex(grid[FREQ]->center(f), 0.0);
-
-            // Compute dipole beam value.
-            const dcomplex xTheta = itsThetaFunction(xArgs);
-            const dcomplex xPhi = itsPhiFunction(xArgs);
-            *E00_re++ = real(xTheta);
-            *E00_im++ = imag(xTheta);
-            *E01_re++ = real(xPhi);
-            *E01_im++ = imag(xPhi);
-
-            const dcomplex yTheta = itsThetaFunction(yArgs);
-            const dcomplex yPhi = itsPhiFunction(yArgs);
-            *E10_re++ = real(yTheta);
-            *E10_im++ = imag(yTheta);
-            *E11_re++ = real(yPhi);
-            *E11_im++ = imag(yPhi);
-        }
-    }
-
-    JonesMatrix::View result;
-    result.assign(0, 0, E00);
-    result.assign(0, 1, E01);
-    result.assign(1, 0, E10);
-    result.assign(1, 1, E11);
-
-    return result;
-}
-
-
-} //# namespace BBS
-} //# namespace LOFAR
diff --git a/CEP/Calibration/BBSKernel/src/Instrument.cc b/CEP/Calibration/BBSKernel/src/Instrument.cc
index f151d2acee3e455d00c332ff75e092b60ea9c916..f3031d5dda5d56b5929d7de0614c35225d841601 100644
--- a/CEP/Calibration/BBSKernel/src/Instrument.cc
+++ b/CEP/Calibration/BBSKernel/src/Instrument.cc
@@ -37,7 +37,8 @@ namespace BBS
 AntennaField::AntennaField(const string &name, const Vector3 &position,
     const Vector3 &p, const Vector3 &q, const Vector3 &r)
     :   itsName(name),
-        itsPosition(position)
+        itsPosition(position),
+        itsActiveElementCount(0)
 {
     itsAxes[P] = p;
     itsAxes[Q] = q;
@@ -54,7 +55,7 @@ const Vector3 &AntennaField::position() const
     return itsPosition;
 }
 
-const Vector3 &AntennaField::axis(Axis axis)
+const Vector3 &AntennaField::axis(Axis axis) const
 {
     return itsAxes[axis];
 }
@@ -72,6 +73,11 @@ void AntennaField::appendTileElement(const Vector3 &offset)
 void AntennaField::appendElement(const Element &element)
 {
     itsElements.push_back(element);
+
+    if(!element.flag[0] || !element.flag[1])
+    {
+        ++itsActiveElementCount;
+    }
 }
 
 Station::Station(const string &name, const casa::MPosition &position)
@@ -117,6 +123,26 @@ unsigned int Station::nField() const
     return itsFields.size();
 }
 
+size_t Station::nElement() const
+{
+    size_t count = 0;
+    for(unsigned int i = 0; i < nField(); ++i)
+    {
+        count += field(i)->nElement();
+    }
+    return count;
+}
+
+size_t Station::nActiveElement() const
+{
+    size_t count = 0;
+    for(unsigned int i = 0; i < nField(); ++i)
+    {
+        count += field(i)->nActiveElement();
+    }
+    return count;
+}
+
 AntennaField::ConstPtr Station::field(unsigned int i) const
 {
     return itsFields[i];
diff --git a/CEP/Calibration/BBSKernel/src/MeasurementAIPS.cc b/CEP/Calibration/BBSKernel/src/MeasurementAIPS.cc
index 886b61e50dc30a94daa36e878b969dd998a2b945..ace130b686bac7654750f999b1d68799ba47d784 100644
--- a/CEP/Calibration/BBSKernel/src/MeasurementAIPS.cc
+++ b/CEP/Calibration/BBSKernel/src/MeasurementAIPS.cc
@@ -25,7 +25,7 @@
 #include <BBSKernel/MeasurementAIPS.h>
 #include <BBSKernel/Exceptions.h>
 
-#include <cstring>
+//#include <cstring>
 #include <Common/Timer.h>
 #include <Common/lofar_algorithm.h>
 #include <Common/LofarLogger.h>
@@ -79,6 +79,16 @@ namespace LOFAR
 namespace BBS
 {
 
+namespace
+{
+    bool hasColumn(const Table &table, const string &column);
+    bool hasSubTable(const Table &table, const string &name);
+    Table getSubTable(const Table &table, const string &name);
+
+    Station::Ptr readStation(const Table &table, unsigned int id,
+        const string &name, const MPosition &position);
+}
+
 MeasurementAIPS::MeasurementAIPS(const string &filename,
     unsigned int idObservation, unsigned int idField,
     unsigned int idDataDescription)
@@ -89,7 +99,7 @@ MeasurementAIPS::MeasurementAIPS(const string &filename,
         itsIdDataDescription(idDataDescription)
 {
     // Get information about the telescope (instrument).
-    initInstrument();
+    itsInstrument = readInstrument(itsMS, itsIdObservation);
 
     // Get the reference directions for the selected field (i.e. phase center,
     // delay center, tile delay center).
@@ -127,7 +137,7 @@ VisBuffer::Ptr MeasurementAIPS::read(const VisSelection &selection,
     // Find the column with covariance information associated with the specified
     // column.
     string columnCov = getLinkedCovarianceColumn(column,
-        hasColumn("WEIGHT_SPECTRUM") ? "WEIGHT_SPECTRUM" : "WEIGHT");
+        hasColumn(itsMS, "WEIGHT_SPECTRUM") ? "WEIGHT_SPECTRUM" : "WEIGHT");
 
     // Create cell slicers for array columns.
     Slicer slicer = getCellSlicer(selection);
@@ -309,7 +319,7 @@ void MeasurementAIPS::write(VisBuffer::Ptr buffer,
     ASSERT(itsMS.isWritable());
 
     // Add visbility column if it does not exist.
-    if(!hasColumn(column))
+    if(!hasColumn(itsMS, column))
     {
         LOG_INFO_STR("Creating visibility column: " << column);
         createVisibilityColumn(column);
@@ -327,7 +337,7 @@ void MeasurementAIPS::write(VisBuffer::Ptr buffer,
         setLinkedCovarianceColumn(column, columnCov);
 
         // Add covariance column if it does not exist.
-        if(!hasColumn(columnCov))
+        if(!hasColumn(itsMS, columnCov))
         {
             LOG_INFO_STR("Creating covariance column: " << columnCov);
             createCovarianceColumn(columnCov);
@@ -510,7 +520,7 @@ void MeasurementAIPS::write(VisBuffer::Ptr buffer,
 
 void MeasurementAIPS::writeHistory(const ParameterSet &parset) const
 {
-    Table tab_history = getSubTable("HISTORY");
+    Table tab_history = getSubTable(itsMS, "HISTORY");
     tab_history.reopenRW();
 
     ScalarColumn<double> c_time(tab_history, "TIME");
@@ -592,182 +602,14 @@ BaselineMask MeasurementAIPS::asMask(const string &filter) const
     return mask;
 }
 
-void MeasurementAIPS::initInstrument()
-{
-    // Get station names and positions in ITRF coordinates.
-    ROMSAntennaColumns antenna(itsMS.antenna());
-    ROMSObservationColumns observation(itsMS.observation());
-
-    ASSERT(observation.nrow() > itsIdObservation);
-    ASSERT(!observation.flagRow()(itsIdObservation));
-
-    // Get instrument name.
-    string name(observation.telescopeName()(itsIdObservation));
-
-    // Get station positions.
-    MVPosition centroid;
-    vector<Station::Ptr> stations(antenna.nrow());
-    for(unsigned int i = 0; i < stations.size(); ++i)
-    {
-        // Get station name and ITRF position.
-        MPosition position = MPosition::Convert(antenna.positionMeas()(i),
-            MPosition::ITRF)();
-
-        // Store station information.
-        stations[i] = initStation(i, antenna.name()(i), position);
-
-        // Update ITRF centroid.
-        centroid += position.getValue();
-    }
-
-    // Get the instrument position in ITRF coordinates, or use the centroid
-    // of the station positions if the instrument position is unknown.
-    MPosition position;
-    if(MeasTable::Observatory(position, name))
-    {
-        position = MPosition::Convert(position, MPosition::ITRF)();
-    }
-    else
-    {
-        LOG_WARN("Instrument position unknown; will use centroid of stations.");
-        ASSERT(antenna.nrow() != 0);
-        centroid *= 1.0 / static_cast<double>(antenna.nrow());
-        position = MPosition(centroid, MPosition::ITRF);
-    }
-
-    itsInstrument = Instrument::Ptr(new Instrument(name, position,
-        stations.begin(), stations.end()));
-}
-
-Station::Ptr MeasurementAIPS::initStation(unsigned int id, const string &name,
-    const MPosition &position) const
-{
-    if(!hasSubTable("LOFAR_ANTENNA_FIELD"))
-    {
-        return Station::Ptr(new Station(name, position));
-    }
-
-    Table tab_field = getSubTable("LOFAR_ANTENNA_FIELD");
-    tab_field = tab_field(tab_field.col("ANTENNA_ID") == static_cast<Int>(id));
-
-    const size_t nFields = tab_field.nrow();
-    if(nFields < 1 || nFields > 2)
-    {
-        LOG_WARN_STR("Antenna " << name << " consists of an incompatible number"
-            " of antenna fields. Beam model simulation will not work for this"
-            " antenna.");
-        return Station::Ptr(new Station(name, position));
-    }
-
-    ROScalarColumn<String> c_name(tab_field, "NAME");
-    ROArrayQuantColumn<Double> c_position(tab_field, "POSITION", "m");
-    ROArrayQuantColumn<Double> c_axes(tab_field, "COORDINATE_AXES", "m");
-    ROArrayQuantColumn<Double> c_tile_offset(tab_field, "TILE_ELEMENT_OFFSET",
-        "m");
-    ROArrayQuantColumn<Double> c_offset(tab_field, "ELEMENT_OFFSET", "m");
-    ROArrayColumn<Bool> c_flag(tab_field, "ELEMENT_FLAG");
-
-    AntennaField::Ptr field[2];
-    for(size_t i = 0; i < nFields; ++i)
-    {
-        // Read antenna field center (ITRF).
-        Vector<Quantum<Double> > aips_position = c_position(i);
-        ASSERT(aips_position.size() == 3);
-
-        Vector3 position = {{aips_position(0).getValue(),
-            aips_position(1).getValue(), aips_position(2).getValue()}};
-
-        // Read antenna field coordinate axes (ITRF).
-        Matrix<Quantum<Double> > aips_axes = c_axes(i);
-        ASSERT(aips_axes.shape().isEqual(IPosition(2, 3, 3)));
-
-        Vector3 P = {{aips_axes(0, 0).getValue(), aips_axes(1, 0).getValue(),
-            aips_axes(2, 0).getValue()}};
-        Vector3 Q = {{aips_axes(0, 1).getValue(), aips_axes(1, 1).getValue(),
-            aips_axes(2, 1).getValue()}};
-        Vector3 R = {{aips_axes(0, 2).getValue(), aips_axes(1, 2).getValue(),
-            aips_axes(2, 2).getValue()}};
-
-        // Store information as AntennaField.
-        field[i] = AntennaField::Ptr(new AntennaField(c_name(i), position, P,
-            Q, R));
-
-        // Read offsets (ITRF) of the dipoles within a tile for HBA antenna
-        // fields.
-        if(c_name(i) != "LBA")
-        {
-            // Read tile configuration for HBA antenna fields.
-            Matrix<Quantum<Double> > aips_offset = c_tile_offset(i);
-            ASSERT(aips_offset.nrow() == 3);
-
-            const size_t nElement = aips_offset.ncolumn();
-            for(size_t j = 0; j < nElement; ++j)
-            {
-                Vector3 offset = {{aips_offset(0, j).getValue(),
-                    aips_offset(1, j).getValue(),
-                    aips_offset(2, j).getValue()}};
-
-                field[i]->appendTileElement(offset);
-            }
-        }
-
-        // Read element offsets and flags.
-        Matrix<Quantum<Double> > aips_offset = c_offset(i);
-        Matrix<Bool> aips_flag = c_flag(i);
-
-        const size_t nElement = aips_offset.ncolumn();
-        ASSERT(aips_offset.shape().isEqual(IPosition(2, 3, nElement)));
-        ASSERT(aips_flag.shape().isEqual(IPosition(2, 2, nElement)));
-
-        for(size_t j = 0; j < nElement; ++j)
-        {
-            AntennaField::Element element;
-            element.offset[0] = aips_offset(0, j).getValue();
-            element.offset[1] = aips_offset(1, j).getValue();
-            element.offset[2] = aips_offset(2, j).getValue();
-            element.flag[0] = aips_flag(0, j);
-            element.flag[1] = aips_flag(1, j);
-
-            field[i]->appendElement(element);
-        }
-    }
-
-    return (nFields == 1 ? Station::Ptr(new Station(name, position, field[0]))
-        : Station::Ptr(new Station(name, position, field[0], field[1])));
-}
-
 void MeasurementAIPS::initReferenceDirections()
 {
-    // Get phase center as RA and DEC (J2000).
-    ROMSFieldColumns field(itsMS.field());
-    ASSERT(field.nrow() > itsIdField);
-    ASSERT(!field.flagRow()(itsIdField));
-
-    itsPhaseReference = MDirection::Convert(field.phaseDirMeas(itsIdField),
-        MDirection::J2000)();
-    itsDelayReference = MDirection::Convert(field.delayDirMeas(itsIdField),
-        MDirection::J2000)();
-
-    // By default, the tile beam reference direction is assumed to be equal
-    // to the station beam reference direction (for backward compatibility,
-    // and for non-HBA measurements).
-    itsTileReference = itsDelayReference;
-
-    // The MeasurementSet class does not support LOFAR specific columns, so we
-    // use ROArrayMeasColumn to read the tile beam reference direction.
-    Table tab_field = getSubTable("FIELD");
-
-    static const String columnName = "LOFAR_TILE_BEAM_DIR";
-    if(hasColumn(tab_field, columnName))
-    {
-        ROArrayMeasColumn<MDirection> c_direction(tab_field, columnName);
-        if(c_direction.isDefined(itsIdField))
-        {
-            itsTileReference =
-                MDirection::Convert(c_direction(itsIdField)(IPosition(1, 0)),
-                MDirection::J2000)();
-        }
-    }
+    itsPhaseReference = MDirection::Convert(readPhaseReference(itsMS,
+        itsIdField), MDirection::J2000)();
+    itsDelayReference = MDirection::Convert(readDelayReference(itsMS,
+        itsIdField), MDirection::J2000)();
+    itsTileReference = MDirection::Convert(readTileReference(itsMS,
+        itsIdField), MDirection::J2000)();
 }
 
 void MeasurementAIPS::initDimensions()
@@ -886,16 +728,6 @@ void MeasurementAIPS::initDimensions()
     itsDims.setCorrelations(correlations);
 }
 
-bool MeasurementAIPS::hasColumn(const string &column) const
-{
-    return hasColumn(itsMS, column);
-}
-
-bool MeasurementAIPS::hasColumn(const Table &table, const string &column) const
-{
-    return table.tableDesc().isColumn(column);
-}
-
 void MeasurementAIPS::createVisibilityColumn(const string &name)
 {
     // Added column should get the same shape as the other data columns in
@@ -948,7 +780,7 @@ void MeasurementAIPS::createCovarianceColumn(const string &name)
     itsMS.addColumn(columnDescriptor, storageManager);
 
     // Figure out which column to use as input.
-    bool hasSpectrum = hasColumn("WEIGHT_SPECTRUM");
+    bool hasSpectrum = hasColumn(itsMS, "WEIGHT_SPECTRUM");
     string weightColumn = hasSpectrum ? "WEIGHT_SPECTRUM" : "WEIGHT";
 
     // Initialize the covariance column using the weight (assumed to equal one
@@ -1052,16 +884,6 @@ void MeasurementAIPS::createCovarianceColumn(const string &name)
             == static_cast<Int>(itsIdDataDescription));
 }
 
-bool MeasurementAIPS::hasSubTable(const string &table) const
-{
-    return itsMS.keywordSet().isDefined(table);
-}
-
-Table MeasurementAIPS::getSubTable(const string &table) const
-{
-    return itsMS.keywordSet().asTable(table);
-}
-
 MDirection MeasurementAIPS::getColumnPhaseReference(const string &column) const
 {
     static const String keyword = "LOFAR_DIRECTION";
@@ -1166,7 +988,7 @@ BaselineMask MeasurementAIPS::getBaselineMask(const VisSelection &selection)
     return asMask("*&&");
 }
 
-// NOTE: OPTIMIZATION OPPORTUNITY: when reading all channels, do not use a
+// NOTE: OPTIMIZATION OPPORTUNITY: When reading all channels, do not use a
 // slicer at all.
 Interval<size_t>
 MeasurementAIPS::getChannelRange(const VisSelection &selection) const
@@ -1462,5 +1284,228 @@ VisDimensions MeasurementAIPS::getDimensionsImpl(const Table &tab_selection,
     return dims;
 }
 
+Instrument::Ptr readInstrument(const MeasurementSet &ms,
+  unsigned int idObservation)
+{
+    ROMSObservationColumns observation(ms.observation());
+    ASSERT(observation.nrow() > idObservation);
+    ASSERT(!observation.flagRow()(idObservation));
+
+    // Get station names and positions in ITRF coordinates.
+    ROMSAntennaColumns antenna(ms.antenna());
+
+    // Get station positions.
+    MVPosition centroid;
+    vector<Station::Ptr> stations(antenna.nrow());
+    for(unsigned int i = 0; i < stations.size(); ++i)
+    {
+        // Get station name and ITRF position.
+        MPosition position = MPosition::Convert(antenna.positionMeas()(i),
+            MPosition::ITRF)();
+
+        // Store station information.
+        stations[i] = readStation(ms, i, antenna.name()(i), position);
+
+        // Update ITRF centroid.
+        centroid += position.getValue();
+    }
+
+    // Get the instrument position in ITRF coordinates, or use the centroid
+    // of the station positions if the instrument position is unknown.
+    MPosition position;
+
+    // Read observatory name and try to look-up its position.
+    const string observatory = observation.telescopeName()(idObservation);
+    if(MeasTable::Observatory(position, observatory))
+    {
+        position = MPosition::Convert(position, MPosition::ITRF)();
+    }
+    else
+    {
+        LOG_WARN("Instrument position unknown; will use centroid of stations.");
+        ASSERT(antenna.nrow() != 0);
+        centroid *= 1.0 / static_cast<double>(antenna.nrow());
+        position = MPosition(centroid, MPosition::ITRF);
+    }
+
+    return Instrument::Ptr(new Instrument(observatory, position,
+      stations.begin(), stations.end()));
+}
+
+MDirection readPhaseReference(const MeasurementSet &ms, unsigned int idField)
+{
+    ROMSFieldColumns field(ms.field());
+    ASSERT(field.nrow() > idField);
+    ASSERT(!field.flagRow()(idField));
+
+    return field.phaseDirMeas(idField);
+}
+
+MDirection readDelayReference(const MeasurementSet &ms, unsigned int idField)
+{
+    ROMSFieldColumns field(ms.field());
+    ASSERT(field.nrow() > idField);
+    ASSERT(!field.flagRow()(idField));
+
+    return field.delayDirMeas(idField);
+}
+
+MDirection readTileReference(const MeasurementSet &ms, unsigned int idField)
+{
+    // The MeasurementSet class does not support LOFAR specific columns, so we
+    // use ROArrayMeasColumn to read the tile beam reference direction.
+    Table tab_field = getSubTable(ms, "FIELD");
+
+    static const String columnName = "LOFAR_TILE_BEAM_DIR";
+    if(hasColumn(tab_field, columnName))
+    {
+        ROArrayMeasColumn<MDirection> c_direction(tab_field, columnName);
+        if(c_direction.isDefined(idField))
+        {
+            return c_direction(idField)(IPosition(1, 0));
+        }
+    }
+
+    // By default, the tile beam reference direction is assumed to be equal
+    // to the station beam reference direction (for backward compatibility,
+    // and for non-HBA measurements).
+    return readDelayReference(ms, idField);
+}
+
+double readFreqReference(const MeasurementSet &ms,
+    unsigned int idDataDescription)
+{
+    // Read spectral window id.
+    ROMSDataDescColumns desc(ms.dataDescription());
+    ASSERT(desc.nrow() > idDataDescription);
+    ASSERT(!desc.flagRow()(idDataDescription));
+
+    const unsigned int idWindow = desc.spectralWindowId()(idDataDescription);
+
+    // Read reference frequency.
+    ROMSSpWindowColumns window(ms.spectralWindow());
+    ASSERT(window.nrow() > idWindow);
+    ASSERT(!window.flagRow()(idWindow));
+
+    return window.refFrequency()(idWindow);
+}
+
+namespace
+{
+
+bool hasColumn(const Table &table, const string &column)
+{
+    return table.tableDesc().isColumn(column);
+}
+
+bool hasSubTable(const Table &table, const string &name)
+{
+    return table.keywordSet().isDefined(name);
+}
+
+Table getSubTable(const Table &table, const string &name)
+{
+    return table.keywordSet().asTable(name);
+}
+
+Station::Ptr readStation(const Table &table, unsigned int id,
+    const string &name, const MPosition &position)
+{
+    if(!hasSubTable(table, "LOFAR_ANTENNA_FIELD"))
+    {
+        return Station::Ptr(new Station(name, position));
+    }
+
+    Table tab_field = getSubTable(table, "LOFAR_ANTENNA_FIELD");
+    tab_field = tab_field(tab_field.col("ANTENNA_ID") == static_cast<Int>(id));
+
+    const size_t nFields = tab_field.nrow();
+    if(nFields < 1 || nFields > 2)
+    {
+        LOG_WARN_STR("Antenna " << name << " consists of an incompatible number"
+            " of antenna fields. Beam model simulation will not work for this"
+            " antenna.");
+        return Station::Ptr(new Station(name, position));
+    }
+
+    ROScalarColumn<String> c_name(tab_field, "NAME");
+    ROArrayQuantColumn<Double> c_position(tab_field, "POSITION", "m");
+    ROArrayQuantColumn<Double> c_axes(tab_field, "COORDINATE_AXES", "m");
+    ROArrayQuantColumn<Double> c_tile_offset(tab_field, "TILE_ELEMENT_OFFSET",
+        "m");
+    ROArrayQuantColumn<Double> c_offset(tab_field, "ELEMENT_OFFSET", "m");
+    ROArrayColumn<Bool> c_flag(tab_field, "ELEMENT_FLAG");
+
+    AntennaField::Ptr field[2];
+    for(size_t i = 0; i < nFields; ++i)
+    {
+        // Read antenna field center (ITRF).
+        Vector<Quantum<Double> > aips_position = c_position(i);
+        ASSERT(aips_position.size() == 3);
+
+        Vector3 position = {{aips_position(0).getValue(),
+            aips_position(1).getValue(), aips_position(2).getValue()}};
+
+        // Read antenna field coordinate axes (ITRF).
+        Matrix<Quantum<Double> > aips_axes = c_axes(i);
+        ASSERT(aips_axes.shape().isEqual(IPosition(2, 3, 3)));
+
+        Vector3 P = {{aips_axes(0, 0).getValue(), aips_axes(1, 0).getValue(),
+            aips_axes(2, 0).getValue()}};
+        Vector3 Q = {{aips_axes(0, 1).getValue(), aips_axes(1, 1).getValue(),
+            aips_axes(2, 1).getValue()}};
+        Vector3 R = {{aips_axes(0, 2).getValue(), aips_axes(1, 2).getValue(),
+            aips_axes(2, 2).getValue()}};
+
+        // Store information as AntennaField.
+        field[i] = AntennaField::Ptr(new AntennaField(c_name(i), position, P,
+            Q, R));
+
+        // Read offsets (ITRF) of the dipoles within a tile for HBA antenna
+        // fields.
+        if(c_name(i) != "LBA")
+        {
+            // Read tile configuration for HBA antenna fields.
+            Matrix<Quantum<Double> > aips_offset = c_tile_offset(i);
+            ASSERT(aips_offset.nrow() == 3);
+
+            const size_t nElement = aips_offset.ncolumn();
+            for(size_t j = 0; j < nElement; ++j)
+            {
+                Vector3 offset = {{aips_offset(0, j).getValue(),
+                    aips_offset(1, j).getValue(),
+                    aips_offset(2, j).getValue()}};
+
+                field[i]->appendTileElement(offset);
+            }
+        }
+
+        // Read element offsets and flags.
+        Matrix<Quantum<Double> > aips_offset = c_offset(i);
+        Matrix<Bool> aips_flag = c_flag(i);
+
+        const size_t nElement = aips_offset.ncolumn();
+        ASSERT(aips_offset.shape().isEqual(IPosition(2, 3, nElement)));
+        ASSERT(aips_flag.shape().isEqual(IPosition(2, 2, nElement)));
+
+        for(size_t j = 0; j < nElement; ++j)
+        {
+            AntennaField::Element element;
+            element.offset[0] = aips_offset(0, j).getValue();
+            element.offset[1] = aips_offset(1, j).getValue();
+            element.offset[2] = aips_offset(2, j).getValue();
+            element.flag[0] = aips_flag(0, j);
+            element.flag[1] = aips_flag(1, j);
+
+            field[i]->appendElement(element);
+        }
+    }
+
+    return (nFields == 1 ? Station::Ptr(new Station(name, position, field[0]))
+        : Station::Ptr(new Station(name, position, field[0], field[1])));
+}
+
+} //# namespace unnamed
+
 } //# namespace BBS
 } //# namespace LOFAR
diff --git a/CEP/Calibration/BBSKernel/src/MeasurementExprLOFAR.cc b/CEP/Calibration/BBSKernel/src/MeasurementExprLOFAR.cc
index 84e52fb12c1754f35062746660e383ec34f40673..3ebca01db48b09dc20e506d08b97e13566c9e907 100644
--- a/CEP/Calibration/BBSKernel/src/MeasurementExprLOFAR.cc
+++ b/CEP/Calibration/BBSKernel/src/MeasurementExprLOFAR.cc
@@ -60,7 +60,7 @@ namespace BBS
 
 MeasurementExprLOFAR::MeasurementExprLOFAR(SourceDB &sourceDB,
     const BufferMap &buffers, const ModelConfig &config,
-    const Instrument::Ptr &instrument, const BaselineSeq &baselines,
+    const Instrument::ConstPtr &instrument, const BaselineSeq &baselines,
     double refFreq, const casa::MDirection &refPhase,
     const casa::MDirection &refDelay, const casa::MDirection &refTile,
     bool circular)
@@ -104,7 +104,7 @@ void MeasurementExprLOFAR::solvablesChanged()
 
 void MeasurementExprLOFAR::makeForwardExpr(SourceDB &sourceDB,
     const BufferMap &buffers, const ModelConfig &config,
-    const Instrument::Ptr &instrument, double refFreq,
+    const Instrument::ConstPtr &instrument, double refFreq,
     const casa::MDirection &refPhase, const casa::MDirection &refDelay,
     const casa::MDirection &refTile, bool circular)
 {
@@ -149,21 +149,6 @@ void MeasurementExprLOFAR::makeForwardExpr(SourceDB &sourceDB,
             instrument->station(i)->position(), refPhase);
     }
 
-    HamakerBeamCoeff coeffLBA, coeffHBA;
-    if(config.useBeam())
-    {
-        // Read LBA beam model coefficients.
-        casa::Path path;
-        path = config.getBeamConfig().getElementPath();
-        path.append("element_beam_HAMAKER_LBA.coeff");
-        coeffLBA.init(path);
-
-        // Read HBA beam model coefficients.
-        path = config.getBeamConfig().getElementPath();
-        path.append("element_beam_HAMAKER_HBA.coeff");
-        coeffHBA.init(path);
-    }
-
     IonosphereExpr::Ptr exprIonosphere;
     if(config.useIonosphere())
     {
@@ -215,7 +200,7 @@ void MeasurementExprLOFAR::makeForwardExpr(SourceDB &sourceDB,
                 exprDDE[j] = compose(exprDDE[j],
                     makeBeamExpr(itsScope, instrument->station(j), refFreq,
                     exprPatchPositionITRF, exprRefDelayITRF, exprRefTileITRF,
-                    config.getBeamConfig(), coeffLBA, coeffHBA));
+                    config.getBeamConfig()));
             }
 
             // Directional TEC.
@@ -343,7 +328,7 @@ void MeasurementExprLOFAR::makeInverseExpr(SourceDB &sourceDB,
 
     LOG_DEBUG_STR("Building expression tree...");
 
-    Instrument::Ptr instrument = buffer->instrument();
+    Instrument::ConstPtr instrument = buffer->instrument();
 
     // Allocate space for the station response expressions.
     vector<Expr<JonesMatrix>::Ptr> stationExpr(instrument->nStations());
@@ -411,21 +396,6 @@ void MeasurementExprLOFAR::makeInverseExpr(SourceDB &sourceDB,
         Expr<Vector<3> >::Ptr exprRefTileITRF =
             makeITRFExpr(instrument->position(), exprRefTile);
 
-        HamakerBeamCoeff coeffLBA, coeffHBA;
-        if(config.useBeam())
-        {
-            // Read LBA beam model coefficients.
-            casa::Path path;
-            path = config.getBeamConfig().getElementPath();
-            path.append("element_beam_HAMAKER_LBA.coeff");
-            coeffLBA.init(path);
-
-            // Read HBA beam model coefficients.
-            path = config.getBeamConfig().getElementPath();
-            path.append("element_beam_HAMAKER_HBA.coeff");
-            coeffHBA.init(path);
-        }
-
         // Functor for the creation of the ionosphere sub-expression.
         IonosphereExpr::Ptr exprIonosphere;
         if(config.useIonosphere())
@@ -462,7 +432,7 @@ void MeasurementExprLOFAR::makeInverseExpr(SourceDB &sourceDB,
                         makeBeamExpr(itsScope, instrument->station(i),
                         buffer->getReferenceFreq(), exprRefPhaseITRF,
                         exprRefDelayITRF, exprRefTileITRF,
-                        config.getBeamConfig(), coeffLBA, coeffHBA));
+                        config.getBeamConfig()));
                 }
 
                 // Ionosphere.
@@ -510,7 +480,7 @@ void MeasurementExprLOFAR::makeInverseExpr(SourceDB &sourceDB,
                         makeBeamExpr(itsScope, instrument->station(i),
                         buffer->getReferenceFreq(), exprPatchPositionITRF,
                         exprRefDelayITRF, exprRefTileITRF,
-                        config.getBeamConfig(), coeffLBA, coeffHBA));
+                        config.getBeamConfig()));
                 }
 
                 // Directional TEC.
diff --git a/CEP/Calibration/BBSKernel/src/MeasurementExprLOFARUtil.cc b/CEP/Calibration/BBSKernel/src/MeasurementExprLOFARUtil.cc
index 29c844690463962c398790fcb07355908b2aef5d..b99dbc038acec7f37e2cc4d7ec7b246bb93f0cee 100644
--- a/CEP/Calibration/BBSKernel/src/MeasurementExprLOFARUtil.cc
+++ b/CEP/Calibration/BBSKernel/src/MeasurementExprLOFARUtil.cc
@@ -24,20 +24,22 @@
 #include <lofar_config.h>
 #include <BBSKernel/MeasurementExprLOFARUtil.h>
 #include <BBSKernel/Exceptions.h>
-#include <BBSKernel/Expr/AntennaFieldAzEl.h>
+#include <BBSKernel/Expr/AntennaElementLBA.h>
+#include <BBSKernel/Expr/AntennaElementHBA.h>
+#include <BBSKernel/Expr/AntennaFieldThetaPhi.h>
 #include <BBSKernel/Expr/AzEl.h>
 #include <BBSKernel/Expr/Delay.h>
 #include <BBSKernel/Expr/ElevationCut.h>
 #include <BBSKernel/Expr/EquatorialCentroid.h>
 #include <BBSKernel/Expr/ExprAdaptors.h>
 #include <BBSKernel/Expr/FaradayRotation.h>
-#include <BBSKernel/Expr/HamakerDipole.h>
 #include <BBSKernel/Expr/ITRFDirection.h>
 #include <BBSKernel/Expr/Literal.h>
 #include <BBSKernel/Expr/LMN.h>
 #include <BBSKernel/Expr/MatrixMul2.h>
 #include <BBSKernel/Expr/MatrixMul3.h>
 #include <BBSKernel/Expr/MatrixSum.h>
+#include <BBSKernel/Expr/ParallacticRotation.h>
 #include <BBSKernel/Expr/PhaseShift.h>
 #include <BBSKernel/Expr/ScalarMatrixMul.h>
 #include <BBSKernel/Expr/StationBeamFormer.h>
@@ -240,9 +242,7 @@ makeBeamExpr(Scope&,
     const Expr<Vector<3> >::Ptr &exprITRF,
     const Expr<Vector<3> >::Ptr &exprRefDelayITRF,
     const Expr<Vector<3> >::Ptr &exprRefTileITRF,
-    const BeamConfig &config,
-    const HamakerBeamCoeff &coeffLBA,
-    const HamakerBeamCoeff &coeffHBA)
+    const BeamConfig &config)
 {
     // Check if the beam model can be computed for this station.
     if(!station->isPhasedArray())
@@ -252,10 +252,6 @@ makeBeamExpr(Scope&,
             " station beam is missing.");
     }
 
-    // The positive X dipole direction is SE of the reference orientation, which
-    // translates to an azimuth of 3/4*pi.
-    Expr<Scalar>::Ptr exprOrientation(new Literal(3.0 * casa::C::pi_4));
-
     // Build expressions for the dual-dipole or tile beam of each antenna field.
     Expr<JonesMatrix>::Ptr exprElementBeam[2];
     for(size_t i = 0; i < station->nField(); ++i)
@@ -265,21 +261,28 @@ makeBeamExpr(Scope&,
         // Element (dual-dipole) beam expression.
         if(config.mode() != BeamConfig::ARRAY_FACTOR)
         {
-            Expr<Vector<2> >::Ptr exprAzEl(new AntennaFieldAzEl(exprITRF,
+            Expr<Vector<2> >::Ptr exprThetaPhi =
+                Expr<Vector<2> >::Ptr(new AntennaFieldThetaPhi(exprITRF,
                 field));
 
             if(field->isHBA())
             {
                 exprElementBeam[i] =
-                    Expr<JonesMatrix>::Ptr(new HamakerDipole(coeffHBA, exprAzEl,
-                    exprOrientation));
+                    Expr<JonesMatrix>::Ptr(new AntennaElementHBA(exprThetaPhi));
             }
             else
             {
                 exprElementBeam[i] =
-                    Expr<JonesMatrix>::Ptr(new HamakerDipole(coeffLBA, exprAzEl,
-                    exprOrientation));
+                    Expr<JonesMatrix>::Ptr(new AntennaElementLBA(exprThetaPhi));
             }
+
+            Expr<JonesMatrix>::Ptr exprRotation =
+                Expr<JonesMatrix>::Ptr(new ParallacticRotation(exprITRF,
+                field));
+
+            exprElementBeam[i] =
+                Expr<JonesMatrix>::Ptr(new MatrixMul2(exprElementBeam[i],
+                exprRotation));
         }
         else
         {
diff --git a/CEP/Calibration/BBSKernel/src/ModelConfig.cc b/CEP/Calibration/BBSKernel/src/ModelConfig.cc
index 486399574e450bf21c893ce82b356ff8d0377878..33db77ca8e20f752020767be4c4930c15f7abf28 100644
--- a/CEP/Calibration/BBSKernel/src/ModelConfig.cc
+++ b/CEP/Calibration/BBSKernel/src/ModelConfig.cc
@@ -96,11 +96,9 @@ BeamConfig::BeamConfig()
 {
 }
 
-BeamConfig::BeamConfig(Mode mode, bool conjugateAF,
-    const casa::Path &elementPath)
+BeamConfig::BeamConfig(Mode mode, bool conjugateAF)
     :   itsMode(mode),
-        itsConjugateAF(conjugateAF),
-        itsElementPath(elementPath)
+        itsConjugateAF(conjugateAF)
 {
 }
 
@@ -114,11 +112,6 @@ bool BeamConfig::conjugateAF() const
     return itsConjugateAF;
 }
 
-const casa::Path &BeamConfig::getElementPath() const
-{
-    return itsElementPath;
-}
-
 // -------------------------------------------------------------------------- //
 // - IonosphereConfig implementation                                        - //
 // -------------------------------------------------------------------------- //
@@ -421,9 +414,7 @@ ostream &operator<<(ostream &out, const BeamConfig &obj)
 {
     out << indent << "Mode: " << BeamConfig::asString(obj.mode())
         << endl << indent << "Conjugate array factor: " << boolalpha
-        << obj.conjugateAF() << noboolalpha
-        << endl << indent << "Element model path: "
-        << obj.getElementPath().originalName();
+        << obj.conjugateAF() << noboolalpha;
     return out;
 }
 
diff --git a/CEP/Calibration/BBSKernel/src/StationExprLOFAR.cc b/CEP/Calibration/BBSKernel/src/StationExprLOFAR.cc
index e0d660437c6e75c861ab10eb607db5a386e6be2f..99f882f24ee64ba45be4cfb03be738fa113ba0da 100644
--- a/CEP/Calibration/BBSKernel/src/StationExprLOFAR.cc
+++ b/CEP/Calibration/BBSKernel/src/StationExprLOFAR.cc
@@ -25,7 +25,6 @@
 #include <BBSKernel/StationExprLOFAR.h>
 #include <BBSKernel/Exceptions.h>
 #include <BBSKernel/MeasurementExprLOFARUtil.h>
-#include <BBSKernel/Expr/AntennaFieldAzEl.h>
 #include <BBSKernel/Expr/AzEl.h>
 #include <BBSKernel/Expr/CachePolicy.h>
 #include <BBSKernel/Expr/Delay.h>
@@ -64,7 +63,7 @@ StationExprLOFAR::StationExprLOFAR(SourceDB &sourceDB, const BufferMap &buffers,
 }
 
 void StationExprLOFAR::initialize(SourceDB &sourceDB, const BufferMap &buffers,
-    const ModelConfig &config, const Instrument::Ptr &instrument,
+    const ModelConfig &config, const Instrument::ConstPtr &instrument,
     double refFreq, const casa::MDirection &refPhase,
     const casa::MDirection &refDelay, const casa::MDirection &refTile,
     bool inverse)
@@ -128,21 +127,6 @@ void StationExprLOFAR::initialize(SourceDB &sourceDB, const BufferMap &buffers,
         Expr<Vector<3> >::Ptr exprRefTileITRF =
             makeITRFExpr(instrument->position(), exprRefTile);
 
-        HamakerBeamCoeff coeffLBA, coeffHBA;
-        if(config.useBeam())
-        {
-            // Read LBA beam model coefficients.
-            casa::Path path;
-            path = config.getBeamConfig().getElementPath();
-            path.append("element_beam_HAMAKER_LBA.coeff");
-            coeffLBA.init(path);
-
-            // Read HBA beam model coefficients.
-            path = config.getBeamConfig().getElementPath();
-            path.append("element_beam_HAMAKER_HBA.coeff");
-            coeffHBA.init(path);
-        }
-
         // Functor for the creation of the ionosphere sub-expression.
         IonosphereExpr::Ptr exprIonosphere;
         if(config.useIonosphere())
@@ -177,8 +161,7 @@ void StationExprLOFAR::initialize(SourceDB &sourceDB, const BufferMap &buffers,
                     itsExpr[i] = compose(itsExpr[i],
                         makeBeamExpr(itsScope, instrument->station(i),
                         refFreq, exprRefPhaseITRF, exprRefDelayITRF,
-                        exprRefTileITRF, config.getBeamConfig(), coeffLBA,
-                        coeffHBA));
+                        exprRefTileITRF, config.getBeamConfig()));
                 }
 
                 // Ionosphere.
@@ -225,8 +208,7 @@ void StationExprLOFAR::initialize(SourceDB &sourceDB, const BufferMap &buffers,
                     itsExpr[i] = compose(itsExpr[i],
                         makeBeamExpr(itsScope, instrument->station(i), refFreq,
                         exprPatchPositionITRF, exprRefDelayITRF,
-                        exprRefTileITRF, config.getBeamConfig(), coeffLBA,
-                        coeffHBA));
+                        exprRefTileITRF, config.getBeamConfig()));
                 }
 
                 // Directional TEC.
diff --git a/CEP/Calibration/BBSKernel/src/StationResponse.cc b/CEP/Calibration/BBSKernel/src/StationResponse.cc
index 04c95f73b3f1c23ad3bffd874ed7f093cbde4dda..00924ed38d03c6d8b206c30ae57287167c51b96c 100644
--- a/CEP/Calibration/BBSKernel/src/StationResponse.cc
+++ b/CEP/Calibration/BBSKernel/src/StationResponse.cc
@@ -24,36 +24,23 @@
 #include <lofar_config.h>
 #include <BBSKernel/StationResponse.h>
 #include <BBSKernel/Exceptions.h>
+#include <BBSKernel/MeasurementAIPS.h>
+#include <BBSKernel/Expr/AntennaElementLBA.h>
+#include <BBSKernel/Expr/AntennaElementHBA.h>
+#include <BBSKernel/Expr/AntennaFieldThetaPhi.h>
 #include <BBSKernel/Expr/CachePolicy.h>
-#include <BBSKernel/Expr/HamakerDipole.h>
+#include <BBSKernel/Expr/ExprAdaptors.h>
+#include <BBSKernel/Expr/ITRFDirection.h>
 #include <BBSKernel/Expr/Literal.h>
 #include <BBSKernel/Expr/MatrixInverse.h>
 #include <BBSKernel/Expr/MatrixMul2.h>
-#include <BBSKernel/Expr/ExprAdaptors.h>
-#include <BBSKernel/Expr/AntennaFieldAzEl.h>
-#include <BBSKernel/Expr/ITRFDirection.h>
+#include <BBSKernel/Expr/ParallacticRotation.h>
 #include <BBSKernel/Expr/ScalarMatrixMul.h>
 #include <BBSKernel/Expr/StationBeamFormer.h>
 #include <BBSKernel/Expr/TileArrayFactor.h>
 #include <Common/LofarLogger.h>
 #include <measures/Measures/MeasConvert.h>
 #include <measures/Measures/MCDirection.h>
-#include <measures/Measures/MCPosition.h>
-#include <ms/MeasurementSets/MSAntenna.h>
-#include <ms/MeasurementSets/MSAntennaParse.h>
-#include <ms/MeasurementSets/MSAntennaColumns.h>
-#include <ms/MeasurementSets/MSDataDescription.h>
-#include <ms/MeasurementSets/MSDataDescColumns.h>
-#include <ms/MeasurementSets/MSField.h>
-#include <ms/MeasurementSets/MSFieldColumns.h>
-#include <ms/MeasurementSets/MSObservation.h>
-#include <ms/MeasurementSets/MSObsColumns.h>
-#include <ms/MeasurementSets/MSPolarization.h>
-#include <ms/MeasurementSets/MSPolColumns.h>
-#include <ms/MeasurementSets/MSSpectralWindow.h>
-#include <ms/MeasurementSets/MSSpWindowColumns.h>
-#include <ms/MeasurementSets/MSSelection.h>
-#include <measures/Measures/MeasTable.h>
 
 namespace LOFAR
 {
@@ -64,7 +51,6 @@ StationResponse::StationResponse(const casa::MeasurementSet &ms,
     bool inverse, bool useElementBeam, bool useArrayFactor, bool conjugateAF)
     :   itsRefDelay(new Dummy<Vector<2> >()),
         itsRefTile(new Dummy<Vector<2> >()),
-        itsRefOrientation(new Dummy<Scalar>()),
         itsDirection(new Dummy<Vector<2> >())
 {
     // Set pointing and beamformer reference directions towards NCP by default.
@@ -72,13 +58,9 @@ StationResponse::StationResponse(const casa::MeasurementSet &ms,
     setRefTile(casa::MDirection());
     setDirection(casa::MDirection());
 
-    // The positive X dipole direction is SE of the reference orientation, which
-    // translates to an azimuth of 3/4*pi.
-    setRefOrientation(3.0 * casa::C::pi_4);
-
     // Load observation details.
-    Instrument::Ptr instrument = initInstrument(ms);
-    double refFreq = getReferenceFreq(ms);
+    Instrument::Ptr instrument = readInstrument(ms);
+    double refFreq = readFreqReference(ms);
 
     // The ITRF direction vectors for the direction of interest and the
     // reference direction are computed w.r.t. the center of the station
@@ -92,13 +74,6 @@ StationResponse::StationResponse(const casa::MeasurementSet &ms,
         Expr<Vector<3> >::Ptr(new ITRFDirection(instrument->position(),
         itsRefTile));
 
-    // Load beam model coefficients.
-    HamakerBeamCoeff coeffLBA, coeffHBA;
-    coeffLBA.init(casa::Path("$LOFARROOT/share/element_beam_HAMAKER_LBA"
-        ".coeff"));
-    coeffHBA.init(casa::Path("$LOFARROOT/share/element_beam_HAMAKER_HBA"
-        ".coeff"));
-
     itsExpr.reserve(instrument->nStations());
     for(size_t i = 0; i < instrument->nStations(); ++i)
     {
@@ -129,21 +104,28 @@ StationResponse::StationResponse(const casa::MeasurementSet &ms,
             // Element (dual-dipole) beam expression.
             if(useElementBeam)
             {
-                Expr<Vector<2> >::Ptr exprAzEl(new AntennaFieldAzEl(exprDirITRF,
+                Expr<Vector<2> >::Ptr exprThetaPhi =
+                    Expr<Vector<2> >::Ptr(new AntennaFieldThetaPhi(exprDirITRF,
                     field));
 
                 if(field->isHBA())
                 {
                     exprElementBeam[j] =
-                        Expr<JonesMatrix>::Ptr(new HamakerDipole(coeffHBA,
-                        exprAzEl, itsRefOrientation));
+                        Expr<JonesMatrix>::Ptr(new AntennaElementHBA(exprThetaPhi));
                 }
                 else
                 {
                     exprElementBeam[j] =
-                        Expr<JonesMatrix>::Ptr(new HamakerDipole(coeffLBA,
-                        exprAzEl, itsRefOrientation));
+                        Expr<JonesMatrix>::Ptr(new AntennaElementLBA(exprThetaPhi));
                 }
+
+                Expr<JonesMatrix>::Ptr exprRotation =
+                    Expr<JonesMatrix>::Ptr(new ParallacticRotation(exprDirITRF,
+                    field));
+
+                exprElementBeam[j] =
+                    Expr<JonesMatrix>::Ptr(new MatrixMul2(exprElementBeam[j],
+                    exprRotation));
             }
             else
             {
@@ -243,16 +225,6 @@ void StationResponse::setRefTile(const casa::MDirection &reference)
     itsCache.clearStats();
 }
 
-void StationResponse::setRefOrientation(double orientation)
-{
-    // Update dipole reference orientation.
-    itsRefOrientation->setValue(Scalar(Matrix(orientation)));
-
-    // Clear cache.
-    itsCache.clear();
-    itsCache.clearStats();
-}
-
 void StationResponse::setDirection(const casa::MDirection &direction)
 {
     // Convert to ensure the direction is specified with respect to the correct
@@ -306,171 +278,5 @@ StationResponse::compose(const Expr<JonesMatrix>::Ptr &lhs,
     return rhs;
 }
 
-Instrument::Ptr StationResponse::initInstrument(const casa::MeasurementSet &ms)
-    const
-{
-    // Get station names and positions in ITRF coordinates.
-    casa::ROMSAntennaColumns antenna(ms.antenna());
-    casa::ROMSObservationColumns observation(ms.observation());
-    ASSERT(observation.nrow() > 0);
-    ASSERT(!observation.flagRow()(0));
-
-    // Get instrument name.
-    string name = observation.telescopeName()(0);
-
-    // Get station positions.
-    casa::MVPosition centroid;
-    vector<Station::Ptr> stations(antenna.nrow());
-    for(unsigned int i = 0; i < stations.size(); ++i)
-    {
-        // Get station name and ITRF position.
-        casa::MPosition position =
-            casa::MPosition::Convert(antenna.positionMeas()(i),
-            casa::MPosition::ITRF)();
-
-        // Store station information.
-        stations[i] = initStation(ms, i, antenna.name()(i), position);
-
-        // Update ITRF centroid.
-        centroid += position.getValue();
-    }
-
-    // Get the instrument position in ITRF coordinates, or use the centroid
-    // of the station positions if the instrument position is unknown.
-    casa::MPosition position;
-    if(casa::MeasTable::Observatory(position, name))
-    {
-        position = casa::MPosition::Convert(position, casa::MPosition::ITRF)();
-    }
-    else
-    {
-        LOG_WARN("Instrument position unknown; will use centroid of stations.");
-        ASSERT(antenna.nrow() != 0);
-        centroid *= 1.0 / static_cast<double>(antenna.nrow());
-        position = casa::MPosition(centroid, casa::MPosition::ITRF);
-    }
-
-    return Instrument::Ptr(new Instrument(name, position, stations.begin(),
-        stations.end()));
-}
-
-Station::Ptr StationResponse::initStation(const casa::MeasurementSet &ms,
-    unsigned int id, const string &name, const casa::MPosition &position) const
-{
-    if(!ms.keywordSet().isDefined("LOFAR_ANTENNA_FIELD"))
-    {
-        return Station::Ptr(new Station(name, position));
-    }
-
-    casa::Table tab_field(ms.keywordSet().asTable("LOFAR_ANTENNA_FIELD"));
-    tab_field = tab_field(tab_field.col("ANTENNA_ID")
-        == static_cast<casa::Int>(id));
-
-    const size_t nFields = tab_field.nrow();
-    if(nFields < 1 || nFields > 2)
-    {
-        LOG_WARN_STR("Antenna " << name << " consists of an incompatible number"
-            " of antenna fields. Beam model simulation will not work for this"
-            " antenna.");
-        return Station::Ptr(new Station(name, position));
-    }
-
-    casa::ROScalarColumn<casa::String> c_name(tab_field, "NAME");
-    casa::ROArrayQuantColumn<casa::Double> c_position(tab_field, "POSITION",
-        "m");
-    casa::ROArrayQuantColumn<casa::Double> c_axes(tab_field, "COORDINATE_AXES",
-        "m");
-    casa::ROArrayQuantColumn<casa::Double> c_tile_offset(tab_field,
-        "TILE_ELEMENT_OFFSET", "m");
-    casa::ROArrayQuantColumn<casa::Double> c_offset(tab_field, "ELEMENT_OFFSET",
-        "m");
-    casa::ROArrayColumn<casa::Bool> c_flag(tab_field, "ELEMENT_FLAG");
-
-    AntennaField::Ptr field[2];
-    for(size_t i = 0; i < nFields; ++i)
-    {
-        // Read antenna field center.
-        casa::Vector<casa::Quantum<casa::Double> > aips_position =
-            c_position(i);
-        ASSERT(aips_position.size() == 3);
-
-        Vector3 position = {{aips_position(0).getValue(),
-            aips_position(1).getValue(), aips_position(2).getValue()}};
-
-        // Read antenna field coordinate axes.
-        casa::Matrix<casa::Quantum<casa::Double> > aips_axes = c_axes(i);
-        ASSERT(aips_axes.shape().isEqual(casa::IPosition(2, 3, 3)));
-
-        Vector3 P = {{aips_axes(0, 0).getValue(), aips_axes(1, 0).getValue(),
-            aips_axes(2, 0).getValue()}};
-        Vector3 Q = {{aips_axes(0, 1).getValue(), aips_axes(1, 1).getValue(),
-            aips_axes(2, 1).getValue()}};
-        Vector3 R = {{aips_axes(0, 2).getValue(), aips_axes(1, 2).getValue(),
-            aips_axes(2, 2).getValue()}};
-
-        // Store information as AntennaField.
-        field[i] = AntennaField::Ptr(new AntennaField(c_name(i), position, P,
-            Q, R));
-
-        if(c_name(i) != "LBA")
-        {
-            // Read tile configuration for HBA antenna fields.
-            casa::Matrix<casa::Quantum<casa::Double> > aips_offset =
-                c_tile_offset(i);
-            ASSERT(aips_offset.nrow() == 3);
-
-            const size_t nElement = aips_offset.ncolumn();
-            for(size_t j = 0; j < nElement; ++j)
-            {
-                Vector3 offset = {{aips_offset(0, j).getValue(),
-                    aips_offset(1, j).getValue(),
-                    aips_offset(2, j).getValue()}};
-
-                field[i]->appendTileElement(offset);
-            }
-        }
-
-        // Read element position offsets and flags.
-        casa::Matrix<casa::Quantum<casa::Double> > aips_offset = c_offset(i);
-        casa::Matrix<casa::Bool> aips_flag = c_flag(i);
-
-        const size_t nElement = aips_offset.ncolumn();
-        ASSERT(aips_offset.shape().isEqual(casa::IPosition(2, 3, nElement)));
-        ASSERT(aips_flag.shape().isEqual(casa::IPosition(2, 2, nElement)));
-
-        for(size_t j = 0; j < nElement; ++j)
-        {
-            AntennaField::Element element;
-            element.offset[0] = aips_offset(0, j).getValue();
-            element.offset[1] = aips_offset(1, j).getValue();
-            element.offset[2] = aips_offset(2, j).getValue();
-            element.flag[0] = aips_flag(0, j);
-            element.flag[1] = aips_flag(1, j);
-
-            field[i]->appendElement(element);
-        }
-    }
-
-    return (nFields == 1 ? Station::Ptr(new Station(name, position, field[0]))
-        : Station::Ptr(new Station(name, position, field[0], field[1])));
-}
-
-double StationResponse::getReferenceFreq(const casa::MeasurementSet &ms) const
-{
-    // Read polarization id and spectral window id.
-    casa::ROMSDataDescColumns desc(ms.dataDescription());
-    ASSERT(desc.nrow() > 0);
-    ASSERT(!desc.flagRow()(0));
-
-    const unsigned int idWindow = desc.spectralWindowId()(0);
-
-    // Get spectral information.
-    casa::ROMSSpWindowColumns window(ms.spectralWindow());
-    ASSERT(window.nrow() > idWindow);
-    ASSERT(!window.flagRow()(idWindow));
-
-    return window.refFrequency()(idWindow);
-}
-
 } //# namespace BBS
 } //# namespace LOFAR
diff --git a/CEP/Calibration/BBSTools/scripts/bbstests.sh b/CEP/Calibration/BBSTools/scripts/bbstests.sh
index 2d741321a6e64abdd539c9fa5018eaf368489335..e700f6a6e8a05ba88467e1bb3a5891acf9e210fa 100755
--- a/CEP/Calibration/BBSTools/scripts/bbstests.sh
+++ b/CEP/Calibration/BBSTools/scripts/bbstests.sh
@@ -5,14 +5,15 @@
 # File:         bbstests.sh
 # Author:       Sven Duscha (duscha@astron.nl)
 # Date:         2011-07-21
-# Last change:  2011-08-24
+# Last change:  2012-02-21
 
 
 
 #bbstestdir='/globaldata/bbs/tests'
-wd='/data/scratch/bbstests'     # working directory to copy tests to
+wd='/data/scratch/bbstests'     # default working directory to copy tests to
 verbosity=0                     # verbosity of test scripts
 taql=False                      # use TaQl to compare columns
+key='tests'                     # BBS database key to run in
 
 usage()
 {
@@ -20,7 +21,8 @@ usage()
   echo "<options> are "
   echo "--verbosity   display verbose information on test progress"
   echo "--wd <dir>    set working directory to execute tests in (default=/data/scratch/bbstests)"
-  echo "--taql <bool> use TaQL for casa table comparison"
+  echo "--taql <1/0>  use TaQL for casa table comparison"
+  echo "--key <dbkey> BBS database key to run in"
   echo "--help        show this help information"
   echo "<tests> to perform"
   echo "calibration   perform a gain calibration on 3C196"
@@ -63,7 +65,7 @@ setBBSTestdir()
     bbstestdir='/globaldata/bbs/tests'
   elif [ ${CEPCLUSTER} == "Sven-Duschas-Macbook-Pro" ]
   then
-    bbstestdir='/Users/duscha/Cluster/Test'
+    bbstestdir='/Users/duscha/Cluster/BBSTests'
   else
 #    bbstestdir=''
     echo "bbstests.sh: Unknown host environment. Exiting..."
@@ -76,22 +78,22 @@ setBBSTestdir()
 args=$@     # keep the arguments
 while test $# -ne 0
 do
-  if test ${1} = "--wd"; then
+  if test ${1} = "-v" -o "${1}" = "--verbose"; then
+    verbosity=1
+    shift
+  elif test ${1} = "-w" -o "${1}" = "--wd"; then
     if test $# -le 1; then
         error "${1} needs an additional argument"
     fi
     shift
-    wd=$1
-    shift
-  elif test ${1} = "-v" -o "${1}" = "--verbose"; then
-    verbosity=1
+    wd=${1}
     shift
-  elif test ${1} = "-w" -o "${1}" = "--wd"; then
+  elif test ${1} = "-k" -o "${1}" = "--key"; then
     if test $# -le 1; then
         error "${1} needs an additional argument"
     fi
     shift
-    wd=${1}
+    key=${1}
     shift
   elif test ${1} = "-t" -o "${1}" = "--taql"; then
     taql=True
@@ -185,43 +187,45 @@ for arg in ${args}
 do
   if [ ${arg} = "all" ]; then
     if [ ${verbosity} == 1 ]; then 
-      echo "${wd}/calibration/testBBS_3C196_calibration.py --verbose --wd ${wd} --taql ${taql}" 
-      echo "${wd}/simulation/testBBS_3C196_simulation.py --verbose --wd ${wd} --taql ${taql}"
-      echo "${wd}/directional/testBBS_3C196_direction.py --verbose --wd ${wd} --taql ${taql}"
-      ${wd}/simulation/testBBS_3C196_simulation.py --verbose --wd ${wd} --taql ${taql}
-      ${wd}/calibration/testBBS_3C196_calibration.py --verbose --wd ${wd} --taql ${taql}
-      ${wd}/directional/testBBS_3C196_direction.py --verbose --wd ${wd} --taql ${taql}
+      echo "${wd}/calibration/testBBS_3C196_calibration.py --verbose --wd ${wd} --taql ${taql} --key ${key}" 
+      echo "${wd}/simulation/testBBS_3C196_simulation.py --verbose --wd ${wd} --taql ${taql} --key ${key}"
+      echo "${wd}/directional/testBBS_3C196_direction.py --verbose --wd ${wd} --taql ${taql} --key ${key}"
+      ${wd}/simulation/testBBS_3C196_simulation.py --verbose --wd ${wd} --taql ${taql} --key ${key}
+      ${wd}/calibration/testBBS_3C196_calibration.py --verbose --wd ${wd} --taql ${taql} --key ${key}
+      ${wd}/directional/testBBS_3C196_direction.py --verbose --wd ${wd} --taql ${taql} --key ${key}
     fi
-    ${wd}/simulation/testBBS_3C196_simulation.py --wd ${wd} --taql ${taql}
-    ${wd}/calibration/testBBS_3C196_calibration.py --wd ${wd} --taql ${taql}
-    ${wd}/directional/testBBS_3C196_direction.py --wd ${wd} --taql ${taql}
+    ${wd}/simulation/testBBS_3C196_simulation.py --wd ${wd} --taql ${taql} --key ${key}
+    ${wd}/calibration/testBBS_3C196_calibration.py --wd ${wd} --taql ${taql} --key ${key}
+    ${wd}/directional/testBBS_3C196_direction.py --wd ${wd} --taql ${taql} --key ${key}
     break
   elif [ ${arg} == "calibration" ]; then
     if [ ${verbosity} == 1 ]; then 
-      echo "${wd}/calibration/testBBS_3C196_calibration.py --verbose --wd ${wd} --taql ${taql}"
-      ${wd}/calibration/testBBS_3C196_calibration.py --verbose --wd ${wd} --taql ${taql}
+      echo "${wd}/calibration/testBBS_3C196_calibration.py --verbose --wd ${wd} --taql ${taql} --key ${key}"
+      ${wd}/calibration/testBBS_3C196_calibration.py --verbose --wd ${wd} --taql ${taql} --key ${key}
     else
-      ${wd}/calibration/testBBS_3C196_calibration.py --verbose --wd ${wd} --taql ${taql}
+      ${wd}/calibration/testBBS_3C196_calibration.py --verbose --wd ${wd} --taql ${taql} --key ${key}
     fi
   elif [ ${arg} == "simulation" ]; then
     if [ ${verbosity} == 1 ]; then 
-      echo "${wd}/simulation/testBBS_3C196_simulation.py --verbose --wd ${wd} --taql ${taql}"
-      ${wd}/simulation/testBBS_3C196_simulation.py --verbose --wd ${wd} --taql ${taql}
+      echo "${wd}/simulation/testBBS_3C196_simulation.py --verbose --wd ${wd} --taql ${taql} --key ${key}"
+      ${wd}/simulation/testBBS_3C196_simulation.py --verbose --wd ${wd} --taql ${taql} --key ${key}
     else
       ${wd}/simulation/testBBS_3C196_simulation.py
     fi
   elif [ ${arg} == "directional" ]; then
     if [ ${verbosity} == 1 ]; then 
-      echo "${wd}/directional/testBBS_3C196_direction.py --verbose --wd ${wd} --taql ${taql}" 
-      ${wd}/directional/testBBS_3C196_direction.py --verbose --wd ${wd} --taql ${taql}
+      echo "${wd}/directional/testBBS_3C196_direction.py --verbose --wd ${wd} --taql ${taql} --key ${key}" 
+      ${wd}/directional/testBBS_3C196_direction.py --verbose --wd ${wd} --taql ${taql} --key ${key}
     else
-      ${wd}/directional/testBBS_3C196_direction.py --verbose --wd ${wd} --taql ${taql}
+      ${wd}/directional/testBBS_3C196_direction.py --verbose --wd ${wd} --taql ${taql} --key ${key}
     fi
   fi
 done
 
 
 # Cleanup and finish
+rm ${wd}/${key}_*                   # delete local kernel log and parset files
 if [ ${verbosity} == 1 ]; then 
+  echo "rm ${wd}/${key}_*"
   echo "bbstest.sh finished."
 fi
diff --git a/CEP/Calibration/BBSTools/scripts/testbbs.py b/CEP/Calibration/BBSTools/scripts/testbbs.py
index dcbda113992fc7cef66b018150e8b2bde010f500..e6bc4fc6f416d51d5ec00fb00145e652f5d748f1 100755
--- a/CEP/Calibration/BBSTools/scripts/testbbs.py
+++ b/CEP/Calibration/BBSTools/scripts/testbbs.py
@@ -7,7 +7,7 @@
 #
 # File:             testbbs.py
 # Date:             2011-07-27
-# Last change:      2011-07-27
+# Last change:      2012-02-21
 # Author:           Sven Duscha (duscha@astron.nl)
 
 
@@ -23,10 +23,10 @@ class testbbs(testsip):
 
     # Constructor of testbbs class which inherits from testsip baseclass
     #
-    def __init__(self, MS, parset, skymodel, wd='.', verbose=True, taql=True):
-        self.sip=testsip.__init__(self, MS, parset, wd, verbose)   # call baseclass constructor
+    def __init__(self, MS, parset, skymodel, wd='.', verbose=True, taql=True, key='tests'):
+        self.sip=testsip.__init__(self, MS, parset, wd, verbose, taql)   # call baseclass constructor
         self.skymodel = skymodel                              # BBS has in addition a skymodel
-
+        self.key=key
 
     def show(self):
         self.sip.showCommon()                                 # call baseclass show() method first
@@ -145,13 +145,10 @@ class testbbs(testsip):
     #
     def runBBS(self):    
         print bcolors.OKBLUE + "Running BBS through calibrate script." + bcolors.ENDC
-        arguments = '-v -f -n --clean --key bbstest --cluster-desc ' + self.clusterdesc + ' --db ' + self.dbserver + ' --db-user ' + self.dbuser + ' ' + self.gds + ' ' + self.parset + ' ' + self.skymodel + ' ' + self.wd
+        arguments = '-v -f -n --clean --key ' + self.key  + ' --cluster-desc ' + self.clusterdesc + ' --db ' + self.dbserver + ' --db-user ' + self.dbuser + ' ' + self.gds + ' ' + self.parset + ' ' + self.skymodel + ' ' + self.wd
         command = ['calibrate', arguments] # '-v', '-f', '--clean', '--key bbstest', '--cluster-desc ' + self.clusterdesc, 
 #        '--db ' + self.dbserver, '--db-user ' + self.dbuser, self.gds, self.parset, self.skymodel,  self.wd]
     
-        #print "arguments = ", arguments
-        #os.popen('calibrate' + ' ' + arguments)
-        
         proc = subprocess.Popen('calibrate ' + arguments, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
         for line in proc.stdout.readlines():
             print line
@@ -185,6 +182,7 @@ class testbbs(testsip):
         if test=="parms" or test=="all":
             self.sip.compareParms()
         if test=="columns" or test=="all":
+            print "executeTest() self.sip.taql = ", self.sip.taql     # DEBUG
             self.sip.compareColumns(self.columns, self.sip.taql)
 
         if self.verbose:
diff --git a/CEP/Calibration/BBSTools/scripts/testsip.py b/CEP/Calibration/BBSTools/scripts/testsip.py
index 7234acd6f492af04b721636cf793c52783a988cc..6eb39552ab1396cb6b45bad8817e21ee058e5b49 100755
--- a/CEP/Calibration/BBSTools/scripts/testsip.py
+++ b/CEP/Calibration/BBSTools/scripts/testsip.py
@@ -11,7 +11,7 @@
 #
 # File:             testsip.py
 # Date:             2011-07-27
-# Last change:      2011-08-22
+# Last change:      2012-02-20
 # Author:           Sven Duscha (duscha@astron.nl)
 
 
@@ -33,7 +33,7 @@ class testsip:
 
     # Create a testBBS class with MS, parset, skymodel and optional working directory
     #
-    def __init__(self, MS, parset, wd='.', verbose=True, taql=True):
+    def __init__(self, MS, parset, wd='.', verbose=True, taql=True, key="tests", clusterdesc=""):
         self.wd = wd    
         self.passed = False
         self.MS = MS
@@ -43,14 +43,19 @@ class testsip:
         self.gds = ""
         self.host = gethostname()
         self.dbserver = "ldb001"
-        self.clusterdesc = self.getClusterDescription()
+        if clusterdesc=="":
+          self.clusterdesc = self.getClusterDescription()
+        else:
+          self.clusterdesc = clusterdesc
         self.dbuser = "postgres"
+        self.key = key              # database key to be used during test run
         
         self.parms = []
         self.columns = []
-        self.acceptancelimit = 1e-3
+        self.acceptancelimit = 1e-3   # modify this to how accurate you expect your tests to mimic the reference
         self.results = {}                
         self.verbose = verbose
+        self.debug = True             # currently have to set this manually, not visible to the outside
         self.taql = taql              # use TaQL to compare columns
         
         return self
@@ -344,9 +349,8 @@ class testsip:
             columnnames=self.columns
     
         for column in columnnames:
-            ret = self.compareColumn(column, taql)            
+            ret = self.compareColumn(column, taql) # need, taql           
             self.results[column] = ret
-            #print "self.results[" + column + "] = " + str(ret)       # DEBUG
             
     
     # Compare a particular MS column with the reference
@@ -354,46 +358,59 @@ class testsip:
     #
     def compareColumn(self, columnname, taql=False):
         if self.verbose:
-            print "Comparing "+  bcolors.OKBLUE + columnname + bcolors.ENDC + " columns."             # DEBUG
+            print "Comparing "+  bcolors.OKBLUE + columnname + bcolors.ENDC + " columns." # DEBUG
 
         passed=False
         errorcount=0                                # counter that counts rows with differying columns
 
         if taql==False:                             # If taql is not to be used for comparison, use numpy difference
-            reftab=pt.table(self.MS)                # Open reference MS in readonly mode
-            testtab=pt.table(self.test_MS)          # Open test MS in readonly mode     
-       
-            tc_test=testtab.col(columnname)         # get column in test table as numpy array
-            tc_ref=reftab.col(columnname)           # get column in reference table as numpy array
-
-            nrows=testtab.nrows()
-            for i in progressbar( range(0, nrows-1), "comparing " + columnname + " ", 60):
-                difference = abs(tc_test[i] - tc_ref[i])    # Use numpy's ability to substract arrays from each other
-                sum=numpy.sum(difference)
-                if sum > (self.acceptancelimit/len(difference)):     # determine if this failed the test
-                    passed=False
-                else:
-                    passed=True
+          if self.debug:
+            print "compareColumn() using numpy" 
 
-            reftab.close()
-            testtab.close()
+          reftab=pt.table(self.MS)                # Open reference MS in readonly mode
+          testtab=pt.table(self.test_MS)          # Open test MS in readonly mode     
+     
+          tc_ref=reftab.col(columnname)           # get column in reference table as numpy array
+          tc_test=testtab.col(columnname)         # get column in test table as numpy array
+  
+          nrows=testtab.nrows()                  
+          for i in progressbar( range(0, nrows-1), "comparing " + columnname + " ", 60):
+              difference = numpy.max(abs(tc_test[i] - tc_ref[i]))    # Use numpy's ability to substract arrays from each other
+              #sum=numpy.sum(difference)
+              
+              #if sum > (self.acceptancelimit/len(difference)):     # determine if this failed the test
+              if difference > self.acceptancelimit:                 # determine if this failed the test
+                  passed=False
+              else:
+                  passed=True
+  
+          reftab.close()
+          testtab.close()
         else:
+            if self.debug:
+              print "compareColumn() using TaQL"          # DEBUG
+  
             self.addRefColumnToTesttab(columnname)      # add reference table column as forward column
         
             testcolumnname = "test_" + columnname       # create name which is used in test_MS if refcolum was added
         
             # Loop over columns, compute and check difference (must be within self.acceptancelimit)            
             # use TaQL for this? How to select from two tables? TODO: check this!
-            taqlcmd = "SELECT * FROM '" + self.test_MS + "' WHERE !all(NEAR(Real("+columnname+"), Real("+testcolumnname+")) AND NEAR(Imag("+columnname+"), Imag("+testcolumnname+")))"
             
-            result = pt.taql(taqlcmd)
-            errorcount = result.nrows()
+#            taqlcmd = "SELECT * FROM '" + self.test_MS + "' WHERE !all(NEAR(Real("+columnname+"), Real("+testcolumnname+")) AND NEAR(Imag("+columnname+"), Imag("+testcolumnname+")))"
+#            errorcount = result.nrows()
+            taqlcmd = "SELECT * FROM '" + self.test_MS + "' WHERE !all(NEARABS(Real("+columnname+"), Real("+testcolumnname+")," + str(self.acceptancelimit) + ") AND NEARABS(Imag("+columnname+"), Imag("+testcolumnname+"),"+ str(self.acceptancelimit) +"))"
+#            print "taqlcmd = ", taqlcmd     # DEBUG
+            errorcount=pt.taql(taqlcmd).nrows()            
             
+            if self.verbose or self.debug:
+              print "errorcount = ", errorcount         # display number of errors=No. of rows
+
+            # If test_MS COLUMN and reference COLUMN have any discrepancy...            
             if errorcount > 0:
-                passed=False
+                passed=False      # ... the test is failed
             else:
                 passed=True
-       
         return passed
 
  
@@ -532,6 +549,15 @@ class testsip:
   
         return columns
 
+    # Cleanup logfiles generated by calibrate script
+    #
+    def cleanUpLogs(self):
+      if self.debug:
+        print "cleanUpLogs()"      # DEBUG
+
+      logfiles=self.key + "*log*"
+      os.remove(logfiles)
+
     """
   
     ##################################################################
diff --git a/CEP/Calibration/ElementResponse/CMakeLists.txt b/CEP/Calibration/ElementResponse/CMakeLists.txt
index 0fdbb0ff282f6e2a7a57192ca8aedc4d6784ca40..a5bc6b53164ea50c6088698dec00a2c047517a0b 100644
--- a/CEP/Calibration/ElementResponse/CMakeLists.txt
+++ b/CEP/Calibration/ElementResponse/CMakeLists.txt
@@ -1,6 +1,6 @@
 # $Id$
 
-lofar_package(ElementResponse 0.1)
+lofar_package(ElementResponse 0.1 DEPENDS Common)
 
 # Uncomment to check for unsafe conversions (gcc), for example conversion of
 # size_t to unsigned int (truncation).
diff --git a/CEP/Calibration/ElementResponse/include/ElementResponse/ElementResponse.h b/CEP/Calibration/ElementResponse/include/ElementResponse/ElementResponse.h
index c1cf9fb93533fee4461c3e52be877a24eb8e9509..f95ba672544e94f30f6046d752f3cf73ffa1c853 100644
--- a/CEP/Calibration/ElementResponse/include/ElementResponse/ElementResponse.h
+++ b/CEP/Calibration/ElementResponse/include/ElementResponse/ElementResponse.h
@@ -37,61 +37,59 @@ namespace LOFAR
 // @{
 
 // Compute the response of an idealized LOFAR LBA dual dipole antenna to
-// radiation at frequency freq (Hz) arriving from the direction given by
-// az, el (rad). The +X dipole is at azimuth 0.0, the +Y dipole is at azimuth
-// pi / 2.0.
+// radiation at frequency freq (Hz) arriving from the direction given by theta,
+// phi (rad). The antenna model is described in a spherical coordinate system
+// with coordinates theta (zenith angle) and phi (azimith). The +X dipole is at
+// azimuth zero, the +Y dipole is at azimuth PI / 2.0.
 //
 // Preconditions:
 // --------------
 // freq: Frequency in Hz in the range [10 MHz, 100 MHz].
-// az: Azimuth in rad in the range [0.0, 2.0 * pi].
-// el: Elevation in rad in the range [0.0, pi / 2.0].
+// theta: Zenith angle in rad in the range [0.0, PI / 2.0].
+// phi: Azimuth in rad in the range [0.0, 2.0 * PI].
 //
-// NB: Clipping directions below the horizon (el < 0.0) is the responsibility of
-// the caller.
-void element_response_lba(double freq, double az, double el,
+void element_response_lba(double freq, double theta, double phi,
     std::complex<double> (&response)[2][2]);
 
 // Compute the response of an idealized LOFAR HBA dual dipole antenna to
-// radiation at frequency freq (Hz) arriving from the direction given by
-// az, el (rad). The +X dipole is at azimuth 0.0, the +Y dipole is at azimuth
-// pi / 2.0.
+// radiation at frequency freq (Hz) arriving from the direction given by theta,
+// phi (rad). The antenna model is described in a spherical coordinate system
+// with coordinates theta (zenith angle) and phi (azimith). The +X dipole is at
+// azimuth zero, the +Y dipole is at azimuth PI / 2.0.
 //
 // Preconditions:
 // --------------
 // freq: Frequency in Hz in the range [120 MHz, 240 MHz].
-// az: Azimuth in rad in the range [0.0, 2.0 * pi].
-// el: Elevation in rad in the range [0.0, pi / 2.0].
+// theta: Zenith angle in rad in the range [0.0, PI / 2.0].
+// phi: Azimuth in rad in the range [0.0, 2.0 * PI].
 //
-// NB: Clipping directions below the horizon (el < 0.0) is the responsibility of
-// the caller.
-void element_response_hba(double freq, double az, double el,
+void element_response_hba(double freq, double theta, double phi,
     std::complex<double> (&response)[2][2]);
 
 // Compute the response of an idealized LOFAR dual dipole antenna to radiation
-// at frequency freq (Hz) arriving from the direction given by az, el (rad).
-// The +X dipole is at azimuth 0.0, the +Y dipole is at azimuth pi / 2.0.
+// at frequency freq (Hz) arriving from the direction given by theta, phi (rad).
+// The antenna model is described in a spherical coordinate system with
+// coordinates theta (zenith angle) and phi (azimith). The +X dipole is at
+// azimuth zero, the +Y dipole is at azimuth PI / 2.0.
 //
 // This function uses a set of user defined coefficients to evaluate the beam
 // model. The coeff_shape parameter defines the shape of the coefficient array
 // as no. of harmonics x degree in theta x degree in frequency x 2. The last
 // dimension is implicit and always equal to 2. The coeff parameter points to an
-// array of coefficients of the proper size, stored in row-major order (or "C"-
-// order). The freq_center and freq_range parameters define the frequency range
-// covered by the model described by the set of coefficients.
+// array of coefficients of the proper size, stored in row-major order
+// ("C"-order). The freq_center and freq_range parameters define the frequency
+// range covered by the model described by the set of coefficients.
 //
 // Preconditions:
 // --------------
 // freq: Frequency in Hz in the range [freq_center - freq_range,
 //     freq_center + freq_range].
-// az: Azimuth in rad in the range [0.0, 2.0 * pi].
-// el: Elevation in rad in the range [0.0, pi / 2.0].
+// theta: Zenith angle in rad in the range [0.0, PI / 2.0].
+// phi: Azimuth in rad in the range [0.0, 2.0 * PI].
 // freq_range, freq_center: Frequency center and range in Hz, should be > 0.
 // coeff_shape: Shape of the coefficient array, all dimensions should be > 0.
 //
-// NB: Clipping directions below the horizon (el < 0.0) is the responsibility of
-// the caller.
-void element_response(double freq, double az, double el,
+void element_response(double freq, double theta, double phi,
     std::complex<double> (&response)[2][2], double freq_center,
     double freq_range, const unsigned int (&coeff_shape)[3],
     const std::complex<double> coeff[]);
diff --git a/CEP/Calibration/ElementResponse/src/CMakeLists.txt b/CEP/Calibration/ElementResponse/src/CMakeLists.txt
index ad09788de60c25444395006ef59585770ba3064a..fbbf6cf0cc590054491f91aa4ef16c2b2732ccf5 100644
--- a/CEP/Calibration/ElementResponse/src/CMakeLists.txt
+++ b/CEP/Calibration/ElementResponse/src/CMakeLists.txt
@@ -1,7 +1,7 @@
 # $Id: CMakeLists.txt 18775 2011-09-06 13:36:45Z zwieten $
 
-#include(LofarPackageVersion)
+include(LofarPackageVersion)
 
 lofar_add_library(elementresponse
-#  Package__Version.cc
+  Package__Version.cc
   ElementResponse.cc)
diff --git a/CEP/Calibration/ElementResponse/src/ElementResponse.cc b/CEP/Calibration/ElementResponse/src/ElementResponse.cc
index 6008a52ca164ba8357f9f1b0afd2803217cab639..1324bfd4be5479bbadbd685bad2362e4d4c260fc 100644
--- a/CEP/Calibration/ElementResponse/src/ElementResponse.cc
+++ b/CEP/Calibration/ElementResponse/src/ElementResponse.cc
@@ -39,43 +39,45 @@ const double pi_2 = 1.570796326794896619231322;
 namespace LOFAR
 {
 
-void element_response_lba(double freq, double az, double el,
+void element_response_lba(double freq, double theta, double phi,
     std::complex<double> (&response)[2][2])
 {
-    element_response(freq, az, el, response, default_lba_freq_center,
+    element_response(freq, theta, phi, response, default_lba_freq_center,
         default_lba_freq_range, default_lba_coeff_shape, default_lba_coeff);
 }
 
-void element_response_hba(double freq, double az, double el,
+void element_response_hba(double freq, double theta, double phi,
     std::complex<double> (&response)[2][2])
 {
-    element_response(freq, az, el, response, default_hba_freq_center,
+    element_response(freq, theta, phi, response, default_hba_freq_center,
         default_hba_freq_range, default_hba_coeff_shape, default_hba_coeff);
 }
 
-void element_response(double freq, double az, double el,
+void element_response(double freq, double theta, double phi,
     std::complex<double> (&response)[2][2], double freq_center,
     double freq_range, const unsigned int (&coeff_shape)[3],
     const std::complex<double> coeff[])
 {
+    // Initialize the response to zero.
+    response[0][0] = 0.0;
+    response[0][1] = 0.0;
+    response[1][0] = 0.0;
+    response[1][1] = 0.0;
+
+    // Clip directions below the horizon.
+    if(theta >= pi_2)
+    {
+        return;
+    }
+
     const unsigned int nHarmonics  = coeff_shape[0];
     const unsigned int nPowerTheta = coeff_shape[1];
     const unsigned int nPowerFreq  = coeff_shape[2];
 
-    // The model is parameterized in terms of zenith angle. The appropriate
-    // conversion is taken care of below.
-    const double theta = pi_2 - el;
-
     // The model is parameterized in terms of a normalized frequency in the
     // range [-1, 1]. The appropriate conversion is taken care of below.
     freq = (freq - freq_center) / freq_range;
 
-    // Initialize the response to zero.
-    response[0][0] = 0.0;
-    response[0][1] = 0.0;
-    response[1][0] = 0.0;
-    response[1][1] = 0.0;
-
     // The variables sign and kappa are used to compute the value of kappa
     // mentioned in the description of the beam model [kappa = (-1)^k * (2 * k
     //+ 1)] incrementally.
@@ -137,7 +139,7 @@ void element_response(double freq, double az, double el,
 
         // Compute the Jones matrix for the current harmonic, by rotating P over
         // kappa * az, and add it to the result.
-        const double angle = sign * kappa * az;
+        const double angle = sign * kappa * phi;
         const double caz = std::cos(angle);
         const double saz = std::sin(angle);
 
diff --git a/CEP/Calibration/pystationresponse/src/__init__.py b/CEP/Calibration/pystationresponse/src/__init__.py
index c04a4105506eb1ae02bd6473346a5d367592817c..d5bb434c54b1b60677064cf7701d6d41d2319a63 100755
--- a/CEP/Calibration/pystationresponse/src/__init__.py
+++ b/CEP/Calibration/pystationresponse/src/__init__.py
@@ -91,20 +91,6 @@ class stationresponse(StationResponse):
         """
         self._setRefTile(ra, dec)
 
-    def setRefOrientation (self, orientation):
-        """Set the orientation of the +X dipole (azimuth in the antenna field
-        coordinate system). Antenna field azimuth is defined with respect to the
-        positive Q axis, and positive azimuth runs from the positive Q axis to
-        the positive P axis (roughly North over East, depending on the field).
-        The orientation of the +Y dipole is assumed to be +90 degrees away from
-        orientation of the +X dipole.
-
-        `orientation`
-          Orientation of the +X dipole as azimuth North over East, in radians.
-          Defaults to SW, or an azimuth of 3/4*pi.
-        """
-        self._setRefOrientation(orientation)
-
     def setDirection (self, ra, dec):
         """Set the direction of interest (can be and often will be different
         from the pointing). By default, PHASE_DIR of field 0 is used.
diff --git a/CEP/Calibration/pystationresponse/src/pystationresponse.cc b/CEP/Calibration/pystationresponse/src/pystationresponse.cc
index fe58492ecc01a7bad4bf179b667b8b49949a149b..d57fa9a4a9539248b31df3fd5227a872cc5fe3e2 100755
--- a/CEP/Calibration/pystationresponse/src/pystationresponse.cc
+++ b/CEP/Calibration/pystationresponse/src/pystationresponse.cc
@@ -66,14 +66,6 @@ namespace LOFAR { namespace BBS  {
     // relevant only for HBA observations.
     void setRefTile(double ra, double dec);
 
-    // Set the orientation of the +X dipole (azimuth in the antenna field
-    // coordinate system). Antenna field azimuth is defined with respect to the
-    // positive Q axis, and positive azimuth runs from the positive Q axis to
-    // the positive P axis (roughly North over East, depending on the field).
-    // The orientation of the +Y dipole is assumed to be +90 degrees away from
-    // orientation of the +X dipole.
-    void setRefOrientation(double orientation);
-
     // Set the direction of interest in radians, J2000. Can and often will be
     // different than the delay and/or tile reference direction.
     void setDirection(double ra, double dec);
@@ -134,11 +126,6 @@ namespace LOFAR { namespace BBS  {
     itsResponse->setRefTile(MDirection(radec, MDirection::J2000));
   }
 
-  void PyStationResponse::setRefOrientation(double orientation)
-  {
-    itsResponse->setRefOrientation(orientation);
-  }
-
   void PyStationResponse::setDirection(double ra, double dec)
   {
     MVDirection radec(Quantity(ra,"rad"), Quantity(dec,"rad"));
@@ -277,8 +264,6 @@ namespace LOFAR { namespace BBS  {
         (boost::python::arg("ra"), boost::python::arg("dec")))
       .def ("_setRefTile", &PyStationResponse::setRefTile,
         (boost::python::arg("ra"), boost::python::arg("dec")))
-      .def ("_setRefOrientation", &PyStationResponse::setRefOrientation,
-        (boost::python::arg("orientation")))
       .def ("_setDirection", &PyStationResponse::setDirection,
         (boost::python::arg("ra"), boost::python::arg("dec")))
       .def ("_evaluate0", &PyStationResponse::evaluate0,
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/gui/histogramwindow.h b/CEP/DP3/AOFlagger/include/AOFlagger/gui/histogramwindow.h
new file mode 100644
index 0000000000000000000000000000000000000000..77ffbf3d0e168a31e5827db94e281b374f9ebfc3
--- /dev/null
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/gui/histogramwindow.h
@@ -0,0 +1,46 @@
+/***************************************************************************
+ *   Copyright (C) 2008 by A.R. Offringa   *
+ *   offringa@astro.rug.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 GUI___HISTOGRAMWINDOW_H
+#define GUI___HISTOGRAMWINDOW_H
+
+#include <AOFlagger/gui/quality/histogrampage.h>
+
+#include <gtkmm/window.h>
+
+#include <AOFlagger/quality/histogramcollection.h>
+
+class HistogramWindow : public Gtk::Window
+{
+	public:
+		HistogramWindow(HistogramCollection &histograms)
+		{
+			_histogramPage.SetStatistics(histograms);
+			add(_histogramPage);
+			_histogramPage.show();
+		}
+		void SetStatistics(HistogramCollection &histograms)
+		{
+			_histogramPage.SetStatistics(histograms);
+		}
+	private:
+		HistogramPage _histogramPage;
+};
+
+#endif
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/gui/imagewidget.h b/CEP/DP3/AOFlagger/include/AOFlagger/gui/imagewidget.h
index c344f9f62fdc54d8b611163605f6cffa2beca60f..1533d971d65b73550f383badbd0b30603928584d 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/gui/imagewidget.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/gui/imagewidget.h
@@ -102,6 +102,7 @@ class ImageWidget : public Gtk::DrawingArea {
 		void SetMetaData(TimeFrequencyMetaDataCPtr metaData) { _metaData = metaData; }
 
 		sigc::signal<void, size_t, size_t> &OnMouseMovedEvent() { return _onMouseMoved; }
+		sigc::signal<void> &OnMouseLeaveEvent() { return _onMouseLeft; }
 		sigc::signal<void, size_t, size_t> &OnButtonReleasedEvent() { return _onButtonReleased; }
 		
 		num_t Max() const { return _max; }
@@ -188,6 +189,7 @@ class ImageWidget : public Gtk::DrawingArea {
 		bool toUnits(double mouseX, double mouseY, int &posX, int &posY);
 		bool onExposeEvent(GdkEventExpose* ev);
 		bool onMotion(GdkEventMotion *event);
+		bool onLeave(GdkEventCrossing *event);
 		bool onButtonReleased(GdkEventButton *event);
 		class ColorMap *createColorMap();
 
@@ -223,8 +225,10 @@ class ImageWidget : public Gtk::DrawingArea {
 		bool _manualXAxisDescription;
 		bool _manualYAxisDescription;
 		bool _manualZAxisDescription;
+		bool _mouseIsIn;
 
 		sigc::signal<void, size_t, size_t> _onMouseMoved;
+		sigc::signal<void> _onMouseLeft;
 		sigc::signal<void, size_t, size_t> _onButtonReleased;
 };
 
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/gui/mswindow.h b/CEP/DP3/AOFlagger/include/AOFlagger/gui/mswindow.h
index 50c9093f6e7224cd3db8b5c10347308772a75d6b..4c6e48daa8fcd0e9fa4c244ceaf85fd58a833d61 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/gui/mswindow.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/gui/mswindow.h
@@ -168,6 +168,9 @@ class MSWindow : public Gtk::Window {
 		void onOpenTestSetSinusoidalBroadband() { openTestSet(27); }
 		void onOpenTestSetSlewedGaussianBroadband() { openTestSet(28); }
 		void onOpenTestSetBurstBroadband() { openTestSet(29); }
+		void onOpenTestSetRFIDistributionLow() { openTestSet(32); }
+		void onOpenTestSetRFIDistributionMid() { openTestSet(31); }
+		void onOpenTestSetRFIDistributionHigh() { openTestSet(30); }
 		void onGaussianTestSets() { _gaussianTestSets = 1; }
 		void onRayleighTestSets() { _gaussianTestSets = 0; }
 		void onZeroTestSets() { _gaussianTestSets = 2; }
@@ -220,6 +223,7 @@ class MSWindow : public Gtk::Window {
 		void onTimeMergeUnsetValues();
 		
 		void showError(const std::string &description);
+		void setSetNameInStatusBar();
 		
 		DefaultModels::SetLocation getSetLocation(bool empty = false);
 		void loadDefaultModel(DefaultModels::Distortion distortion, bool withNoise, bool empty = false);
@@ -251,6 +255,7 @@ class MSWindow : public Gtk::Window {
 			_sim16ChannelsButton, _sim64ChannelsButton, _sim256ChannelsButton;
 		//std::vector<Gtk::Window*> _subWindows;
 		class ImagePlaneWindow *_imagePlaneWindow;
+		class HistogramWindow *_histogramWindow;
 		Gtk::Window
 			*_optionWindow, *_editStrategyWindow,
 			*_gotoWindow,
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/gui/plotwindow.h b/CEP/DP3/AOFlagger/include/AOFlagger/gui/plotwindow.h
index bcfdebbba2e1eee424159563794c276a7eb38dc9..60e2e8f651b7b73bbfd8f5d8084c4f633386e6c2 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/gui/plotwindow.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/gui/plotwindow.h
@@ -45,10 +45,11 @@ class PlotWindow : public Gtk::Window {
 	private:
 		void handleUpdate()
 		{
-			show();
 			const std::vector<Plot2D*> &plots = _plotManager.Items();
 			Plot2D &lastPlot = **plots.rbegin();
 			_plotWidget.SetPlot(lastPlot);
+			show();
+			raise();
 		}
 		
 		PlotWidget _plotWidget;
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/aoqplotwindow.h b/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/aoqplotwindow.h
index 24fcb5b0ffc68a406daa056e724ae10a11ae5418..6a76042e44423be0c7a68799d42715ff23288f19 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/aoqplotwindow.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/aoqplotwindow.h
@@ -103,6 +103,7 @@ class AOQPlotWindow : public Gtk::Window {
 		bool _isOpen;
 		std::string _filename;
 		class StatisticsCollection *_statCollection;
+		class HistogramCollection *_histCollection;
 		class StatisticsCollection *_fullStats;
 		std::vector<class AntennaInfo> _antennas;
 };
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/datawindow.h b/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/datawindow.h
index c2f44614331fbb4db5bd693bf91ac2ce416e8508..988c3aa9d3999a045dbd2d9ea04f6d87809bccc4 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/datawindow.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/datawindow.h
@@ -20,6 +20,8 @@
 #ifndef GUI_QUALITY__DATA_WINDOW_H
 #define GUI_QUALITY__DATA_WINDOW_H
 
+#include <string>
+
 #include <gtkmm/window.h>
 #include <gtkmm/textview.h>
 #include <gtkmm/scrolledwindow.h>
@@ -36,7 +38,7 @@ class DataWindow : public Gtk::Window {
 			
 			add(_scrolledWindow);
 			_scrolledWindow.show();
-			set_default_size(200, 200);
+			set_default_size(300, 400);
 		}
     ~DataWindow()
     {
@@ -45,6 +47,7 @@ class DataWindow : public Gtk::Window {
 		{
 			_textView.get_buffer()->set_text(data);
 		}
+		void SetData(const class Plot2D &plot);
 	private:
 		Gtk::ScrolledWindow _scrolledWindow;
 		Gtk::TextView _textView;
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/histogrampage.h b/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/histogrampage.h
index 58e163dfc7f5a98afed6ca168b377ce9f2a09e40..9d2cbd6035eeea957bc1d0588bae2029cd0e1df5 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/histogrampage.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/gui/quality/histogrampage.h
@@ -27,6 +27,7 @@
 #include <gtkmm/checkbutton.h>
 #include <gtkmm/entry.h>
 #include <gtkmm/frame.h>
+#include <gtkmm/textview.h>
 
 #include <AOFlagger/quality/qualitytablesformatter.h>
 
@@ -39,30 +40,35 @@
 class HistogramPage : public Gtk::HBox {
 	public:
 		HistogramPage();
-    virtual ~HistogramPage();
+    ~HistogramPage();
 
 		void SetStatistics(const std::string &filename)
 		{
 			_statFilename = filename;
+			readFromFile();
 			updatePlot();
 		}
-		void CloseStatistics()
-		{
-			_statFilename = std::string();
-		}
+		void SetStatistics(class HistogramCollection &collection);
+		void CloseStatistics();
 		bool HasStatistics() const
 		{
-			return !_statFilename.empty();
+			return _histograms != 0;
 		}
 	private:
-		void addHistogramToPlot(class LogHistogram &histogram);
-		void addRayleighToPlot(class LogHistogram &histogram, double sigma, double n);
-		void addRayleighDifferenceToPlot(LogHistogram &histogram, double sigma, double n);
+		void addHistogramToPlot(const class LogHistogram &histogram);
+		void addRayleighToPlot(const class LogHistogram &histogram, double sigma, double n);
+		void addRayleighDifferenceToPlot(const LogHistogram &histogram, double sigma, double n);
 		void updatePlot();
-		void plotPolarization(class HistogramCollection &histograms, unsigned p);
-		void plotFit(class LogHistogram &histogram, const std::string &title);
+		void plotPolarization(const HistogramCollection &histogramCollection, unsigned polarization);
+		void plotPolarization(const class LogHistogram &totalHistogram, const class LogHistogram &rfiHistogram);
+		void plotFit(const class LogHistogram &histogram, const std::string &title);
+		void plotSlope(const class LogHistogram &histogram, const std::string &title);
 		void onPlotPropertiesClicked();
-		void onDataExportClicked() { }
+		void onDataExportClicked();
+		void readFromFile();
+		void updateSlopeFrame();
+		void addSlopeText(std::stringstream &str, const LogHistogram &histogram, bool updateRange);
+		void updateDataWindow();
 		
 		void onAutoRangeClicked()
 		{
@@ -73,6 +79,15 @@ class HistogramPage : public Gtk::HBox {
 				updatePlot();
 		}
 		
+		void onSlopeAutoRangeClicked()
+		{
+			bool autoRange = _slopeAutoRangeButton.get_active();
+			_slopeStartEntry.set_sensitive(!autoRange);
+			_slopeEndEntry.set_sensitive(!autoRange);
+			if(autoRange)
+				updatePlot();
+		}
+		
 		Gtk::VBox _sideBox;
 		
 		Gtk::Frame _histogramTypeFrame;
@@ -81,7 +96,7 @@ class HistogramPage : public Gtk::HBox {
 		
 		Gtk::Frame _polarizationFrame;
 		Gtk::VBox _polarizationBox;
-		Gtk::CheckButton _xxPolarizationButton, _xyPolarizationButton, _yxPolarizationButton, _yyPolarizationButton;
+		Gtk::CheckButton _xxPolarizationButton, _xyPolarizationButton, _yxPolarizationButton, _yyPolarizationButton, _sumPolarizationButton;
 		
 		Gtk::Frame _fitFrame;
 		Gtk::VBox _fitBox;
@@ -92,12 +107,22 @@ class HistogramPage : public Gtk::HBox {
 		Gtk::VBox _functionBox;
 		Gtk::RadioButton _nsButton, _dndsButton;
 		
-		Gtk::Button _plotPropertiesButton;
+		Gtk::Button _plotPropertiesButton, _dataExportButton;
+		
+		Gtk::Frame _slopeFrame;
+		Gtk::VBox _slopeBox;
+		Gtk::TextView _slopeTextView;
+		Gtk::CheckButton _drawSlopeButton;
+		Gtk::CheckButton _slopeAutoRangeButton;
+		Gtk::Entry _slopeStartEntry, _slopeEndEntry, _slopeRFIRatio;
 		
 		std::string _statFilename;
 		Plot2D _plot;
 		PlotWidget _plotWidget;
 		class PlotPropertiesWindow *_plotPropertiesWindow;
+		class DataWindow *_dataWindow;
+		class HistogramCollection *_histograms;
+		class HistogramCollection *_summedPolarizationHistograms;
 };
 
 #endif
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/msio/baselinereader.h b/CEP/DP3/AOFlagger/include/AOFlagger/msio/baselinereader.h
index 82b668439f9e5f39f51fac7eb8c548abc9b86110..720e2d333e192a1c7bd797945750db689f391a5a 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/msio/baselinereader.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/msio/baselinereader.h
@@ -69,7 +69,13 @@ class BaselineReader {
 
 		MeasurementSet &Set() { return _measurementSet; }
 
-		const std::map<double,size_t> &ObservationTimes() const { return _observationTimes; }
+		const std::map<double,size_t> &AllObservationTimes() const { return _observationTimes; }
+		
+		std::vector<double> ObservationTimes(size_t startIndex, size_t endIndex) const {
+			std::vector<double> times;
+			times.insert(times.begin(), _observationTimesVector.begin()+startIndex, _observationTimesVector.begin()+endIndex);
+			return times;
+		}
 
 		void AddReadRequest(size_t antenna1, size_t antenna2, size_t spectralWindow);
 		void AddReadRequest(size_t antenna1, size_t antenna2, size_t spectralWindow, size_t startIndex, size_t endIndex)
@@ -210,6 +216,7 @@ class BaselineReader {
 		bool _readData, _readFlags;
 		
 		std::map<double,size_t> _observationTimes;
+		std::vector<double> _observationTimesVector;
 		size_t _polarizationCount;
 		size_t _frequencyCount;
 };
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/msio/measurementset.h b/CEP/DP3/AOFlagger/include/AOFlagger/msio/measurementset.h
index 063356b2265c2dd2251e6b2aa1358ed0e7fc14dd..11d4c3a98812d6101ffbba8c95a5e15a77984074 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/msio/measurementset.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/msio/measurementset.h
@@ -125,6 +125,7 @@ class MeasurementSet {
 			return _minScanIndex;
 		}
 		size_t GetPolarizationCount();
+		static size_t GetPolarizationCount(const std::string &filename);
 		size_t AntennaCount();
 		size_t FieldCount();
 		size_t BandCount();
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/quality/histogramcollection.h b/CEP/DP3/AOFlagger/include/AOFlagger/quality/histogramcollection.h
index 35509bd7b0781aa1fc05f69455baba3a8a8bff82..ca4517ced0746cbba410c688c61fe0eb3aa16896 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/quality/histogramcollection.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/quality/histogramcollection.h
@@ -29,14 +29,30 @@
 #include <AOFlagger/msio/image2d.h>
 #include <AOFlagger/msio/mask2d.h>
 
-class HistogramCollection
+#include <AOFlagger/util/serializable.h>
+
+class HistogramCollection : public Serializable
 {
 	public:
 		typedef std::pair<unsigned, unsigned> AntennaPair;
 		
-		HistogramCollection(unsigned polarizationCount) : _polarizationCount(polarizationCount)
+		HistogramCollection() : _polarizationCount(0)
+		{
+		}
+		
+		explicit HistogramCollection(unsigned polarizationCount) : _polarizationCount(polarizationCount)
+		{
+			init();
+		}
+		
+		HistogramCollection(const HistogramCollection &source) : _polarizationCount(source._polarizationCount)
 		{
 			init();
+			for(unsigned i=0;i<_polarizationCount;++i)
+			{
+				copy(_totalHistograms[i], source._totalHistograms[i]);
+				copy(_rfiHistograms[i], source._rfiHistograms[i]);
+			}
 		}
 		
 		~HistogramCollection()
@@ -44,6 +60,13 @@ class HistogramCollection
 			destruct();
 		}
 		
+		void SetPolarizationCount(unsigned polarizationCount)
+		{
+			destruct();
+			_polarizationCount = polarizationCount;
+			init();
+		}
+		
 		void Add(const unsigned antenna1, const unsigned antenna2, const unsigned polarization, const std::complex<float> *values, const bool *isRFI, size_t sampleCount)
 		{
 			LogHistogram &totalHistogram = GetTotalHistogram(antenna1, antenna2, polarization);
@@ -58,6 +81,28 @@ class HistogramCollection
 			}
 		}
 		
+		void Add(const HistogramCollection &collection)
+		{
+			if(collection._polarizationCount != _polarizationCount)
+				throw std::runtime_error("Polarization counts of histogram collections don't match");
+			for(unsigned p=0;p<_polarizationCount;++p)
+			{
+				add(collection, p, p);
+			}
+		}
+		
+		bool Empty() const
+		{
+			if(_polarizationCount == 0)
+				return true;
+			for(unsigned p=0;p!=_polarizationCount;++p)
+			{
+				if(!_totalHistograms[p].empty() || !_rfiHistograms[p].empty())
+					return false;
+			}
+			return true;
+		}
+		
 		void Add(const unsigned antenna1, const unsigned antenna2, const unsigned polarization, Image2DCPtr image, Mask2DCPtr mask);
 		
 		LogHistogram &GetTotalHistogram(const unsigned a1, const unsigned a2, const unsigned polarization)
@@ -80,12 +125,12 @@ class HistogramCollection
 			return _rfiHistograms[polarization];
 		}
 		
-		void GetTotalHistogramForCrossCorrelations(const unsigned polarization, LogHistogram &target)
+		void GetTotalHistogramForCrossCorrelations(const unsigned polarization, LogHistogram &target) const
 		{
 			getHistogramForCrossCorrelations(_totalHistograms, polarization, target);
 		}
 		
-		void GetRFIHistogramForCrossCorrelations(const unsigned polarization, LogHistogram &target)
+		void GetRFIHistogramForCrossCorrelations(const unsigned polarization, LogHistogram &target) const
 		{
 			getHistogramForCrossCorrelations(_rfiHistograms, polarization, target);
 		}
@@ -101,6 +146,32 @@ class HistogramCollection
 		void Load(class HistogramTablesFormatter &histogramTables);
 		
 		void Plot(class Plot2D &plot, unsigned polarization);
+		
+		unsigned PolarizationCount() const { return _polarizationCount; }
+		
+		virtual void Serialize(std::ostream &stream) const
+		{
+			SerializeToUInt64(stream, _polarizationCount);
+			serializeMapArray(stream, _totalHistograms);
+			serializeMapArray(stream, _rfiHistograms);
+		}
+		
+		virtual void Unserialize(std::istream &stream)
+		{
+			destruct();
+			_polarizationCount = UnserializeUInt64(stream);
+			init();
+			unserializeMapArray(stream, _totalHistograms);
+			unserializeMapArray(stream, _rfiHistograms);
+		}
+		
+		HistogramCollection *CreateSummedPolarizationCollection() const
+		{
+			HistogramCollection *newCollection = new HistogramCollection(1);
+			for(unsigned p=0;p<_polarizationCount;++p)
+				newCollection->add(*this, p, 0);
+			return newCollection;
+		}
 	private:
 		unsigned _polarizationCount;
 		std::map<AntennaPair, LogHistogram*> *_totalHistograms;
@@ -108,27 +179,100 @@ class HistogramCollection
 		
 		void init()
 		{
-			_totalHistograms = new std::map<AntennaPair, LogHistogram*>[_polarizationCount];
-			_rfiHistograms = new std::map<AntennaPair, LogHistogram*>[_polarizationCount];
+			if(_polarizationCount != 0)
+			{
+				_totalHistograms = new std::map<AntennaPair, LogHistogram*>[_polarizationCount];
+				_rfiHistograms = new std::map<AntennaPair, LogHistogram*>[_polarizationCount];
+			} else {
+				_totalHistograms = 0;
+				_rfiHistograms = 0;
+			}
 		}
 		
 		void destruct()
 		{
-			for(unsigned p=0;p<_polarizationCount;++p)
+			if(_polarizationCount != 0)
 			{
-				for(std::map<AntennaPair, LogHistogram*>::iterator i=_totalHistograms[p].begin(); i!=_totalHistograms[p].end(); ++i)
-				{
-					delete i->second;
-				}
-				for(std::map<AntennaPair, LogHistogram*>::iterator i=_rfiHistograms[p].begin(); i!=_rfiHistograms[p].end(); ++i)
+				for(unsigned p=0;p<_polarizationCount;++p)
 				{
-					delete i->second;
+					for(std::map<AntennaPair, LogHistogram*>::iterator i=_totalHistograms[p].begin(); i!=_totalHistograms[p].end(); ++i)
+					{
+						delete i->second;
+					}
+					for(std::map<AntennaPair, LogHistogram*>::iterator i=_rfiHistograms[p].begin(); i!=_rfiHistograms[p].end(); ++i)
+					{
+						delete i->second;
+					}
 				}
+				delete[] _totalHistograms;
+				delete[] _rfiHistograms;
+			}
+		}
+		
+		void serializeMapArray(std::ostream &stream, const std::map<AntennaPair, LogHistogram*> *map) const
+		{
+			for(unsigned p=0;p<_polarizationCount;++p)
+				serializeMap(stream, map[p]);
+		}
+		
+		void unserializeMapArray(std::istream &stream, std::map<AntennaPair, LogHistogram*> *map)
+		{
+			for(unsigned p=0;p<_polarizationCount;++p)
+				unserializeMap(stream, map[p]);
+		}
+		
+		void serializeMap(std::ostream &stream, const std::map<AntennaPair, LogHistogram*> &map) const
+		{
+			SerializeToUInt64(stream, map.size());
+			for(std::map<AntennaPair, LogHistogram*>::const_iterator i=map.begin();i!=map.end();++i)
+			{
+				const AntennaPair &antennae = i->first;
+				const LogHistogram *histogram = i->second;
+				SerializeToUInt32(stream, antennae.first);
+				SerializeToUInt32(stream, antennae.second);
+				histogram->Serialize(stream);
+			}
+		}
+		
+		void unserializeMap(std::istream &stream, std::map<AntennaPair, LogHistogram*> &map)
+		{
+			map.clear();
+			size_t mapSize = UnserializeUInt64(stream);
+			std::map<AntennaPair, LogHistogram*>::iterator insertPos = map.begin();
+			for(size_t i=0;i!=mapSize;++i)
+			{
+				std::pair<AntennaPair, LogHistogram*> p;
+				p.first.first = UnserializeUInt32(stream);
+				p.first.second = UnserializeUInt32(stream);
+				p.second = new LogHistogram();
+				p.second->Unserialize(stream);
+				insertPos = map.insert(insertPos, p);
+			}
+		}
+		
+		void copy(std::map<AntennaPair, LogHistogram*> &destination, const std::map<AntennaPair, LogHistogram*> &source)
+		{
+			for(std::map<AntennaPair, LogHistogram*>::const_iterator i=source.begin();i!=source.end();++i)
+			{
+				destination.insert(std::pair<AntennaPair, LogHistogram*>(i->first, new LogHistogram(*i->second)));
 			}
-			delete[] _totalHistograms;
-			delete[] _rfiHistograms;
 		}
 
+		void add(const HistogramCollection &collection, unsigned fromPolarization, unsigned toPolarization)
+		{
+			for(std::map<AntennaPair, LogHistogram*>::const_iterator i=collection._totalHistograms[fromPolarization].begin(); i!=collection._totalHistograms[fromPolarization].end(); ++i)
+			{
+				LogHistogram &histogram = GetTotalHistogram(i->first.first, i->first.second, toPolarization);
+				histogram.Add(*i->second);
+			}
+			
+			for(std::map<AntennaPair, LogHistogram*>::const_iterator i=collection._rfiHistograms[fromPolarization].begin(); i!=collection._rfiHistograms[fromPolarization].end(); ++i)
+			{
+				LogHistogram &histogram = GetRFIHistogram(i->first.first, i->first.second, toPolarization);
+				histogram.Add(*i->second);
+			}
+		}
+		
 		LogHistogram &getHistogram(std::map<AntennaPair, LogHistogram*> *histograms, const unsigned a1, const unsigned a2, const unsigned polarization)
 		{
 			const AntennaPair antennae(a1, a2);
@@ -140,9 +284,9 @@ class HistogramCollection
 			return *i->second;
 		}
 		
-		void getHistogramForCrossCorrelations(std::map<AntennaPair, LogHistogram*> *histograms, const unsigned polarization, LogHistogram &target)
+		void getHistogramForCrossCorrelations(std::map<AntennaPair, LogHistogram*> *histograms, const unsigned polarization, LogHistogram &target) const
 		{
-			for(std::map<AntennaPair, LogHistogram*>::iterator i=histograms[polarization].begin(); i!=histograms[polarization].end(); ++i)
+			for(std::map<AntennaPair, LogHistogram*>::const_iterator i=histograms[polarization].begin(); i!=histograms[polarization].end(); ++i)
 			{
 				if(i->first.first != i->first.second)
 					target.Add(*i->second);
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/quality/loghistogram.h b/CEP/DP3/AOFlagger/include/AOFlagger/quality/loghistogram.h
index 8cababa2628af19cbba7025695abfabae038cee7..4f335323d7aeca79ed57eaafc953174ea186ef23 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/quality/loghistogram.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/quality/loghistogram.h
@@ -27,10 +27,12 @@
 
 #include "histogramtablesformatter.h"
 
-class LogHistogram
+#include <AOFlagger/util/serializable.h>
+
+class LogHistogram : public Serializable
 {
 	private:
-		struct AmplitudeBin
+		struct AmplitudeBin : public Serializable
 		{
 			AmplitudeBin() :
 				count(0)
@@ -56,9 +58,26 @@ class LogHistogram
 					count = 0;
 				return *this;
 			}
+			
+			virtual void Serialize(std::ostream &stream) const
+			{
+				SerializeToUInt64(stream, count);
+			}
+			virtual void Unserialize(std::istream &stream)
+			{
+				count = UnserializeUInt64(stream);
+			}
 		};
 		
 	public:
+		LogHistogram()
+		{
+		}
+		
+		LogHistogram(const LogHistogram &source) : _amplitudes(source._amplitudes)
+		{
+		}
+		
 		void Add(const double amplitude)
 		{
 			if(std::isfinite(amplitude))
@@ -87,27 +106,6 @@ class LogHistogram
 			}
 		}
 		
-		double NormalizedSlope(double startAmplitude, double endAmplitude) const
-		{
-			unsigned long n = 0;
-			long double sumX = 0.0, sumXY = 0.0, sumY = 0.0, sumXSquare = 0.0;
-			for(std::map<double, class AmplitudeBin>::const_iterator i=_amplitudes.begin();i!=_amplitudes.end();++i)
-			{
-				if(i->first >= startAmplitude && i->first < endAmplitude)
-				{
-					long unsigned count = i->second.GetCount();
-					double x = log10(i->first);
-					double y = log10((double) count / i->first);
-					++n;
-					sumX += x;
-					sumXSquare += x * x;
-					sumY += y;
-					sumXY += x * y;
-				}
-			}
-			return (sumXY - sumX*sumY/n)/(sumXSquare - (sumX*sumX/n));
-		}
-		
 		double MaxAmplitude() const
 		{
 			if(_amplitudes.empty())
@@ -163,6 +161,55 @@ class LogHistogram
 			return minCount;
 		}
 		
+		double MaxNormalizedCount() const
+		{
+			double maxCount = 0.0;
+			for (LogHistogram::const_iterator i=begin(); i!=end(); ++i)
+			{
+				if(i.normalizedCount() > maxCount && i.value() > 0 && std::isfinite(i.value()))
+					maxCount = i.normalizedCount();
+			}
+			return maxCount;
+		}
+		
+		double NormalizedTotalCount() const
+		{
+			unsigned long count = 0;
+			for (LogHistogram::const_iterator i=begin(); i!=end(); ++i)
+				count += i.unnormalizedCount();
+			return count;
+		}
+		
+		double NormalizedCountAbove(double lowerLimitingAmplitude) const
+		{
+			unsigned long count = 0;
+			LogHistogram::const_iterator i=begin();
+			while(i!=end() && i.value() <= lowerLimitingAmplitude)
+			{
+				++i;
+			}
+			while(i!=end())
+			{
+				count += i.unnormalizedCount();
+				++i;
+			}
+			return count;
+		}
+		
+		double AmplitudeWithMaxNormalizedCount() const
+		{
+			double maxCount = 0.0, maxPosition = 0.0;
+			for (LogHistogram::const_iterator i=begin(); i!=end(); ++i)
+			{
+				if(i.normalizedCount() > maxCount && i.value() > 0 && std::isfinite(i.value()))
+				{
+					maxCount = i.normalizedCount();
+					maxPosition = i.value();
+				}
+			}
+			return maxPosition;
+		}
+		
 		double MinPosNormalizedCount() const
 		{
 			const_iterator i = begin();
@@ -178,6 +225,75 @@ class LogHistogram
 			return minCount;
 		}
 		
+		double NormalizedSlope(double startAmplitude, double endAmplitude) const
+		{
+			unsigned long n = 0;
+			long double sumX = 0.0, sumXY = 0.0, sumY = 0.0, sumXSquare = 0.0;
+			for(const_iterator i=begin();i!=end();++i)
+			{
+				if(i.value() >= startAmplitude && i.value() < endAmplitude)
+				{
+					double x = log10(i.value());
+					double y = log10(i.normalizedCount());
+					++n;
+					sumX += x;
+					sumXSquare += x * x;
+					sumY += y;
+					sumXY += x * y;
+				}
+			}
+			return (sumXY - sumX*sumY/n)/(sumXSquare - (sumX*sumX/n));
+		}
+		
+		double NormalizedSlopeOffset(double startAmplitude, double endAmplitude, double slope) const
+		{
+			unsigned long n = 0;
+			long double sumOffset = 0.0;
+			for(const_iterator i=begin();i!=end();++i)
+			{
+				if(i.value() >= startAmplitude && i.value() < endAmplitude)
+				{
+					double y = log10(i.normalizedCount());
+					double x = log10(i.value());
+					double ySlope = x*slope;
+					++n;
+					sumOffset += (y - ySlope);
+				}
+			}
+			return (double) (sumOffset/(long double) n);
+		}
+		
+		double PowerLawUpperLimit(double constrainingAmplitude, double exponent, double factor) const
+		{
+			const double count = NormalizedCountAbove(constrainingAmplitude);
+			const double term = count * (exponent+1.0)/factor + pow(constrainingAmplitude, exponent+1.0);
+			return pow(term, 1.0/(exponent+1.0));
+		}
+		
+		double PowerLawLowerLimit(double constrainingAmplitude, double exponent, double factor, double rfiRatio) const
+		{
+			const double countPart = NormalizedCountAbove(constrainingAmplitude);
+			const double countTotal = NormalizedTotalCount() * rfiRatio;
+			const double term = (countPart - countTotal) * (exponent+1.0)/factor + pow(constrainingAmplitude, exponent+1.0);
+			return pow(term, 1.0/(exponent+1.0));
+		}
+		
+		void GetRFIRegion(double &start, double &end) const
+		{
+			double sigmaEstimate = AmplitudeWithMaxNormalizedCount();
+			double maxAmplitude = MaxAmplitude();
+			start = sigmaEstimate * 20.0;
+			double halfWay = exp((log(start) + log(maxAmplitude)) * 0.5);
+			end = halfWay;
+		}
+
+		double NormalizedSlopeInRFIRegion() const
+		{
+			double start, end;
+			GetRFIRegion(start ,end);
+			return NormalizedSlope(start, end);
+		}
+		
 		void SetData(std::vector<HistogramTablesFormatter::HistogramItem> &histogramData)
 		{
 			for(std::vector<HistogramTablesFormatter::HistogramItem>::const_iterator i=histogramData.begin(); i!=histogramData.end();++i)
@@ -204,9 +320,10 @@ class LogHistogram
 				bool operator==(const const_iterator &other) const { return other._iterator == _iterator; }
 				bool operator!=(const const_iterator &other) const { return other._iterator != _iterator; }
 				const_iterator &operator++() { ++_iterator; return *this; }
+				const_iterator &operator--() { --_iterator; return *this; }
 				double value() const { return _iterator->first; }
 				double normalizedCount() const { return _iterator->second.GetCount() / (binEnd() - binStart()); }
-				double unnormalizedCount() const { return _iterator->second.GetCount(); }
+				long unsigned unnormalizedCount() const { return _iterator->second.GetCount(); }
 				double binStart() const
 				{
 					return _iterator->first>0.0 ?
@@ -234,7 +351,32 @@ class LogHistogram
 			return const_iterator(*this, _amplitudes.end());
 		}
 		
+		virtual void Serialize(std::ostream &stream) const
+		{
+			SerializeToUInt64(stream, _amplitudes.size());
+			for(std::map<double, AmplitudeBin>::const_iterator i=_amplitudes.begin();i!=_amplitudes.end();++i)
+			{
+				SerializeToDouble(stream, i->first);
+				i->second.Serialize(stream);
+			}
+		}
+		
+		virtual void Unserialize(std::istream &stream)
+		{
+			_amplitudes.clear();
+			size_t mapSize = UnserializeUInt64(stream);
+			std::map<double, AmplitudeBin>::iterator insertPos = _amplitudes.begin();
+			for(size_t i=0;i!=mapSize;++i)
+			{
+				std::pair<double, AmplitudeBin> p;
+				p.first = UnserializeDouble(stream);
+				p.second.Unserialize(stream);
+				insertPos = _amplitudes.insert(insertPos, p);
+			}
+		}
 	private:
+		std::map<double, AmplitudeBin> _amplitudes;
+		
 		AmplitudeBin &getBin(double centralAmplitude)
 		{
 			std::map<double, class AmplitudeBin>::iterator element =
@@ -259,8 +401,6 @@ class LogHistogram
 				-pow10(log10(x)+0.005);
 		}
 		
-		std::map<double, AmplitudeBin> _amplitudes;
-		
 		static double getCentralAmplitude(const double amplitude)
 		{
 			if(amplitude>=0.0)
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/quality/rayleighfitter.h b/CEP/DP3/AOFlagger/include/AOFlagger/quality/rayleighfitter.h
index aad6c031888b885db6cdd68636fd3e8de1975777..0b8fea519f9d204af561802f064dd797d60f41bb 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/quality/rayleighfitter.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/quality/rayleighfitter.h
@@ -6,9 +6,9 @@
 class RayleighFitter
 {
  public:
-	void Fit(double minVal, double maxVal, LogHistogram &hist, double &sigma, double &n);
-	static double SigmaEstimate(LogHistogram &hist);
-	static double NEstimate(LogHistogram &hist, double rangeStart, double rangeEnd);
+	void Fit(double minVal, double maxVal, const LogHistogram &hist, double &sigma, double &n);
+	static double SigmaEstimate(const LogHistogram &hist);
+	static double NEstimate(const LogHistogram &hist, double rangeStart, double rangeEnd);
 	static void FindFitRangeUnderRFIContamination(double minPositiveAmplitude, double sigmaEstimate, double &minValue, double &maxValue);
 	static double RayleighValue(double sigma, double n, double x)
 	{
@@ -16,7 +16,7 @@ class RayleighFitter
 		return n * x / (sigmaP2) * exp(-x*x/(2*sigmaP2));
 	}
 	
-	LogHistogram *_hist;
+	const LogHistogram *_hist;
 	double _minVal, _maxVal;
 };
 
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/quality/statisticscollection.h b/CEP/DP3/AOFlagger/include/AOFlagger/quality/statisticscollection.h
index b314da7f1b08271e33f8ff20bff492fc9f5046d4..54db7169a4c400d3d7cd51c82039729ab694b6de 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/quality/statisticscollection.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/quality/statisticscollection.h
@@ -774,12 +774,13 @@ class StatisticsCollection : public Serializable
 			_timeStatistics.clear();
 			size_t count = (size_t) UnserializeUInt64(stream);
 			
+			std::map<double, DoubleStatMap>::iterator insertPos = _timeStatistics.begin();
 			for(size_t i=0;i<count;++i)
 			{
 				double frequency = UnserializeDouble(stream);
-				std::map<double, DoubleStatMap>::iterator iterator =
-					_timeStatistics.insert(std::pair<double, DoubleStatMap>(frequency, DoubleStatMap())).first;
-				unserializeDoubleStatMap(stream, iterator->second);
+				insertPos =
+					_timeStatistics.insert(insertPos, std::pair<double, DoubleStatMap>(frequency, DoubleStatMap()));
+				unserializeDoubleStatMap(stream, insertPos->second);
 			}
 		}
 		
@@ -813,12 +814,13 @@ class StatisticsCollection : public Serializable
 			_baselineStatistics.clear();
 			size_t count = (size_t) UnserializeUInt64(stream);
 			
+			std::map<double, BaselineStatisticsMap>::iterator insertPos = _baselineStatistics.begin();
 			for(size_t i=0;i<count;++i)
 			{
 				double frequency = UnserializeDouble(stream);
-				std::map<double, BaselineStatisticsMap>::iterator iterator =
-					_baselineStatistics.insert(std::pair<double, BaselineStatisticsMap>(frequency, BaselineStatisticsMap(_polarizationCount))).first;
-				iterator->second.Unserialize(stream);
+				insertPos = _baselineStatistics.insert(
+					insertPos, std::pair<double, BaselineStatisticsMap>(frequency, BaselineStatisticsMap(_polarizationCount)));
+				insertPos->second.Unserialize(stream);
 			}
 		}
 		
@@ -839,12 +841,13 @@ class StatisticsCollection : public Serializable
 		{
 			size_t count = (size_t) UnserializeUInt64(stream);
 			
+			std::map<double, DefaultStatistics>::iterator insertPos = statMap.begin();
 			for(size_t i=0;i<count;++i)
 			{
 				double key = UnserializeDouble(stream);
-				std::map<double, DefaultStatistics>::iterator iterator =
-					statMap.insert(std::pair<double, DefaultStatistics>(key, DefaultStatistics(_polarizationCount))).first;
-				iterator->second.Unserialize(stream);
+				insertPos =
+					statMap.insert(insertPos, std::pair<double, DefaultStatistics>(key, DefaultStatistics(_polarizationCount)));
+				insertPos->second.Unserialize(stream);
 			}
 		}
 		
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/remote/processcommander.h b/CEP/DP3/AOFlagger/include/AOFlagger/remote/processcommander.h
index 7d8e70371a135bd33cab3e95537e4797aaaf3833..4038899a60c2ac5ebf7e01c4001090b519a6c6ee 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/remote/processcommander.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/remote/processcommander.h
@@ -45,14 +45,16 @@ class ProcessCommander
 		void Run();
 		
 		static std::string GetHostName();
-		const StatisticsCollection &Statistics() const { return *_collection; }
+		const StatisticsCollection &Statistics() const { return *_statisticsCollection; }
+		const HistogramCollection &Histograms() const { return *_histogramCollection; }
 		const std::vector<AntennaInfo> &Antennas() const { return _antennas; }
 		const std::vector<std::string> &Errors() const { return _errors; }
 		
-		void PushReadQualityTablesTask(StatisticsCollection *dest)
+		void PushReadQualityTablesTask(StatisticsCollection *dest, HistogramCollection *destHistogram)
 		{
 			_tasks.push_back(ReadQualityTablesTask);
-			_collection = dest;
+			_statisticsCollection = dest;
+			_histogramCollection = destHistogram;
 		}
 		void PushReadAntennaTablesTask() { _tasks.push_back(ReadAntennaTablesTask); }
 	private:
@@ -66,7 +68,7 @@ class ProcessCommander
 		void makeNodeMap(const ClusteredObservation &observation);
 		void onConnectionCreated(ServerConnectionPtr serverConnection, bool &acceptConnection);
 		void onConnectionAwaitingCommand(ServerConnectionPtr serverConnection);
-		void onConnectionFinishReadQualityTables(ServerConnectionPtr serverConnection, StatisticsCollection &collection);
+		void onConnectionFinishReadQualityTables(ServerConnectionPtr serverConnection, StatisticsCollection &statisticsCollection, HistogramCollection &histogramCollection);
 		void onConnectionFinishReadAntennaTables(ServerConnectionPtr serverConnection, std::vector<AntennaInfo> &antennas);
 		void onError(ServerConnectionPtr connection, const std::string &error);
 		void onProcessFinished(RemoteProcess &process, bool error, int status);
@@ -75,7 +77,8 @@ class ProcessCommander
 		typedef std::map<std::string, std::deque<ClusteredObservationItem> > NodeMap;
 		NodeMap _nodeMap;
 		std::vector<RemoteProcess *> _processes;
-		StatisticsCollection *_collection;
+		StatisticsCollection *_statisticsCollection;
+		HistogramCollection *_histogramCollection;
 		std::vector<AntennaInfo> _antennas;
 		const ClusteredObservation _observation;
 		
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/remote/serverconnection.h b/CEP/DP3/AOFlagger/include/AOFlagger/remote/serverconnection.h
index 6529dd102b727d8c8d4a585b3f1a66645eb3d4c8..334f9fd127b037121ff25053503469e4efd051a8 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/remote/serverconnection.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/remote/serverconnection.h
@@ -34,6 +34,7 @@
 #include <AOFlagger/msio/antennainfo.h>
 
 class StatisticsCollection;
+class HistogramCollection;
 
 namespace aoRemote {
 
@@ -50,14 +51,14 @@ class ServerConnection : public boost::enable_shared_from_this<ServerConnection>
 		~ServerConnection();
 		
 		void StopClient();
-		void ReadQualityTables(const std::string &msFilename, class StatisticsCollection &collection);
+		void ReadQualityTables(const std::string &msFilename, class StatisticsCollection &collection, HistogramCollection &histogramCollection);
 		void ReadAntennaTables(const std::string &msFilename, std::vector<AntennaInfo> &antennas);
 		void Start();
 		
 		boost::asio::ip::tcp::socket &Socket() { return _socket; }
 		
 		sigc::signal<void, ServerConnectionPtr> &SignalAwaitingCommand() { return _onAwaitingCommand; }
-		sigc::signal<void, ServerConnectionPtr, StatisticsCollection&> &SignalFinishReadQualityTables() { return _onFinishReadQualityTables; }
+		sigc::signal<void, ServerConnectionPtr, StatisticsCollection&, HistogramCollection&> &SignalFinishReadQualityTables() { return _onFinishReadQualityTables; }
 		sigc::signal<void, ServerConnectionPtr, std::vector<AntennaInfo>&> &SignalFinishReadAntennaTables() { return _onFinishReadAntennaTables; }
 		sigc::signal<void, ServerConnectionPtr, const std::string&> &SignalError() { return _onError; }
 		
@@ -68,7 +69,7 @@ class ServerConnection : public boost::enable_shared_from_this<ServerConnection>
 		std::string _hostname;
 		
 		sigc::signal<void, ServerConnectionPtr> _onAwaitingCommand;
-		sigc::signal<void, ServerConnectionPtr, StatisticsCollection&> _onFinishReadQualityTables;
+		sigc::signal<void, ServerConnectionPtr, StatisticsCollection&, HistogramCollection&> _onFinishReadQualityTables;
 		sigc::signal<void, ServerConnectionPtr, std::vector<AntennaInfo>&> _onFinishReadAntennaTables;
 		sigc::signal<void, ServerConnectionPtr, const std::string&> _onError;
 		
@@ -91,6 +92,7 @@ class ServerConnection : public boost::enable_shared_from_this<ServerConnection>
 		void handleError(const GenericReadResponseHeader &header);
 		
 		StatisticsCollection *_collection;
+		HistogramCollection *_histogramCollection;
 		std::vector<AntennaInfo> *_antennas;
 };
 	
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/strategy/algorithms/mitigationtester.h b/CEP/DP3/AOFlagger/include/AOFlagger/strategy/algorithms/mitigationtester.h
index 2149319237b9f0eaa9e7f6040ef49c1659eb02e4..c81698133f6846780668aef54789335d3bf2d75f 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/strategy/algorithms/mitigationtester.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/strategy/algorithms/mitigationtester.h
@@ -119,6 +119,7 @@ class MitigationTester{
 		static void AddVarBroadbandToTestSet(Image2DPtr image, Mask2DPtr rfi);
 		static void AddModelData(Image2DPtr image, unsigned sources);
 		static void SubtractBackground(Image2DPtr image);
+		static Image2DPtr sampleRFIDistribution(unsigned width, unsigned height, double ig_over_rsq);
 
 		static double Rand(enum NoiseType type) {
 			switch(type) {
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/strategy/imagesets/bandcombinedset.h b/CEP/DP3/AOFlagger/include/AOFlagger/strategy/imagesets/bandcombinedset.h
index 9d74b87716daa8fe5cff5242bcd0a36131446e0c..b03ae6ba2675f285d532c9d30589a2c0cb85accf 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/strategy/imagesets/bandcombinedset.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/strategy/imagesets/bandcombinedset.h
@@ -150,9 +150,7 @@ namespace rfiStrategy {
 				const BandCombinedSetIndex &bcIndex = static_cast<const BandCombinedSetIndex&>(index);
 				TimeFrequencyMetaDataPtr metaData(new TimeFrequencyMetaData());
 				
-				std::vector<double> *obsTimes = _sets[0]->CreateObservationTimesVector();
-				metaData->SetObservationTimes(*obsTimes);
-				delete obsTimes;
+				metaData->SetObservationTimes(_sets[0]->ObservationTimesVector(index));
 				
 				BandInfo bandInfo;
 				bandInfo.windowIndex = 0;
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/strategy/imagesets/msimageset.h b/CEP/DP3/AOFlagger/include/AOFlagger/strategy/imagesets/msimageset.h
index 83cf5833beb43966072f0b374b358a7412899375..3e27405948a7f87831003e30bdb04966bb687dd5 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/strategy/imagesets/msimageset.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/strategy/imagesets/msimageset.h
@@ -197,13 +197,15 @@ namespace rfiStrategy {
 			{
 				return _set.GetBandInfo(bandIndex);
 			}
-			const std::set<double> &GetObservationTimesSet()
+			/*const std::set<double> &GetObservationTimesSet(const ImageSetIndex &index)
 			{
-				return _set.GetObservationTimesSet();
-			}
-			std::vector<double> *CreateObservationTimesVector()
+				const MSImageSetIndex &msIndex = static_cast<const MSImageSetIndex &>(index);
+				return _reader->ObservationTimes(StartIndex(index), EndIndex(index));
+			}*/
+			std::vector<double> ObservationTimesVector(const ImageSetIndex &index)
 			{
-				return _set.CreateObservationTimesVector();
+				const MSImageSetIndex &msIndex = static_cast<const MSImageSetIndex &>(index);
+				return _reader->ObservationTimes(StartIndex(msIndex), EndIndex(msIndex));
 			}
 			const std::vector<std::pair<size_t,size_t> > &Baselines() const { return _baselines; }
 			size_t BandCount() const { return _bandCount; }
diff --git a/CEP/DP3/AOFlagger/src/CMakeLists.txt b/CEP/DP3/AOFlagger/src/CMakeLists.txt
index e25ef75e52b61a7b8c384ff8a72a337210d2b98b..71371e354a4d9e99fef941a3a9c4877fb2d55056 100644
--- a/CEP/DP3/AOFlagger/src/CMakeLists.txt
+++ b/CEP/DP3/AOFlagger/src/CMakeLists.txt
@@ -52,6 +52,7 @@ set(GUI_PLOT_FILES
   gui/plot/colorscale.cpp
   gui/plot/horizontalplotscale.cpp
   gui/plot/plot2d.cpp
+  gui/plot/plotpropertieswindow.cpp
   gui/plot/plotwidget.cpp
   gui/plot/verticalplotscale.cpp)
 
@@ -74,6 +75,8 @@ set(GUI_FILES
   gui/progresswindow.cpp
   gui/rawoptionwindow.cpp
   gui/tfstatoptionwindow.cpp
+  gui/quality/datawindow.cpp
+  gui/quality/histogrampage.cpp
   ${GUI_PLOT_FILES})
 
 set(MSIO_FILES
@@ -221,6 +224,7 @@ if(GTKMM_FOUND)
 		gui/plot/plotpropertieswindow.cpp
 		gui/quality/aoqplotwindow.cpp
 		gui/quality/baselineplotpage.cpp
+		gui/quality/datawindow.cpp
 		gui/quality/grayscaleplotpage.cpp
 		gui/quality/histogrampage.cpp
 		gui/quality/timefrequencyplotpage.cpp
diff --git a/CEP/DP3/AOFlagger/src/aoquality.cpp b/CEP/DP3/AOFlagger/src/aoquality.cpp
index d9cbef4d6b3c974ea9486d2747784b337665968c..3712941f5a6a37a305faaf8575aa06a0ffbc4c8b 100644
--- a/CEP/DP3/AOFlagger/src/aoquality.cpp
+++ b/CEP/DP3/AOFlagger/src/aoquality.cpp
@@ -60,7 +60,7 @@ enum CollectingMode
 	CollectHistograms
 };
 
-void actionCollect(const std::string &filename, enum CollectingMode mode)
+void actionCollect(const std::string &filename, enum CollectingMode mode, StatisticsCollection &statisticsCollection, HistogramCollection &histogramCollection)
 {
 	MeasurementSet *ms = new MeasurementSet(filename);
 	const unsigned polarizationCount = ms->GetPolarizationCount();
@@ -93,19 +93,19 @@ void actionCollect(const std::string &filename, enum CollectingMode mode)
 		std::cout << "Channel zero will be included in the statistics, as it seems that channel 0 is okay.\n";
 	
 	// Initialize statisticscollection
-	StatisticsCollection collection(polarizationCount);
+	statisticsCollection.SetPolarizationCount(polarizationCount);
 	if(mode == CollectDefault)
 	{
 		for(unsigned b=0;b<bandCount;++b)
 		{
 			if(ignoreChannelZero)
-				collection.InitializeBand(b, (frequencies[b]+1), bands[b].channelCount-1);
+				statisticsCollection.InitializeBand(b, (frequencies[b]+1), bands[b].channelCount-1);
 			else
-				collection.InitializeBand(b, frequencies[b], bands[b].channelCount);
+				statisticsCollection.InitializeBand(b, frequencies[b], bands[b].channelCount);
 		}
 	}
 	// Initialize Histograms collection
-	HistogramCollection histogramCollection(polarizationCount);
+	histogramCollection.SetPolarizationCount(polarizationCount);
 
 	// get columns
 	casa::Table table(filename, casa::Table::Update);
@@ -170,7 +170,7 @@ void actionCollect(const std::string &filename, enum CollectingMode mode)
 				case CollectDefault:
 					{
 						const bool origFlags = false;
-						collection.Add(antenna1Index, antenna2Index, time, bandIndex, p, &samples[p]->real(), &samples[p]->imag(), isRFI[p], &origFlags, band.channelCount - startChannel, 2, 1, 0);
+						statisticsCollection.Add(antenna1Index, antenna2Index, time, bandIndex, p, &samples[p]->real(), &samples[p]->imag(), isRFI[p], &origFlags, band.channelCount - startChannel, 2, 1, 0);
 					}
 					break;
 				case CollectHistograms:
@@ -193,6 +193,14 @@ void actionCollect(const std::string &filename, enum CollectingMode mode)
 	delete[] frequencies;
 	delete[] bands;
 	std::cout << "100\n";
+}
+
+void actionCollect(const std::string &filename, enum CollectingMode mode)
+{
+	StatisticsCollection statisticsCollection;
+	HistogramCollection histogramCollection;
+	
+	actionCollect(filename, mode, statisticsCollection, histogramCollection);
 	
 	switch(mode)
 	{
@@ -201,7 +209,7 @@ void actionCollect(const std::string &filename, enum CollectingMode mode)
 				std::cout << "Writing quality tables..." << std::endl;
 				
 				QualityTablesFormatter qualityData(filename);
-				collection.Save(qualityData);
+				statisticsCollection.Save(qualityData);
 			}
 			break;
 		case CollectHistograms:
@@ -212,76 +220,17 @@ void actionCollect(const std::string &filename, enum CollectingMode mode)
 				histogramCollection.Save(histograms);
 			}
 			break;
-			
-		/*const std::map<HistogramCollection::AntennaPair, LogHistogram*> &map = histogramCollection.GetHistograms(0);
-		Plot plotSlopes("histogram-slopes.pdf");
-		plotSlopes.SetYRange(-10.0, 10.0);
-		Plot plotHistograms("histograms.pdf");
-		for(std::map<HistogramCollection::AntennaPair, LogHistogram*>::const_iterator i = map.begin(); i != map.end(); ++i)
-		{
-			if(i->first.first != i->first.second)
-			{
-				const LogHistogram *histogram = i->second;
-				double rangeCentre = histogram->MinPositiveAmplitude();
-				rangeCentre = exp2(floor(log2(rangeCentre)));
-				const double maxAmplitude = histogram->MaxAmplitude();
-				std::cout << "Antennae " << i->first.first << " x " << i->first.second << "\n";
-				std::stringstream s;
-				s << i->first.first << " x " << i->first.second;
-				//plotSlopes.StartLine(s.str());
-				//plotHistograms.StartLine(s.str());
-				plotSlopes.StartLine();
-				plotSlopes.SetLogScale(true, false);
-				plotHistograms.StartLine();
-				plotHistograms.SetLogScale(true, true);
-				while(rangeCentre < maxAmplitude && rangeCentre > 0.0)
-				{
-					const double rangeStart = rangeCentre * 0.75;
-					const double rangeEnd = rangeCentre * 1.5;
-					const double slope = histogram->NormalizedSlope(rangeStart, rangeEnd, LogHistogram::TotalAmplitudeHistogram);
-					std::cout << rangeStart << "-" << rangeEnd << ": " << slope << "\n";
-					rangeCentre *= 2.0;
-					plotSlopes.PushDataPoint(rangeCentre, slope);
-					const double count = histogram->NormalizedCount(rangeStart, rangeEnd, LogHistogram::TotalAmplitudeHistogram);
-					if(count > 0 && std::isfinite(count))
-						plotHistograms.PushDataPoint(rangeCentre, count);
-				}
-			}
-		}
-		Plot plotFine("histogram-fine.pdf");
-		Plot plotGlobalSlopes("histogram-gslopes.pdf");
-		plotFine.SetLogScale(true, true);
-		plotGlobalSlopes.SetLogScale(true, false);
-		plotGlobalSlopes.SetYRange(-5.0, 5.0);
-		LogHistogram intHistogram;
-		histogramCollection.GetHistogramForCrossCorrelations(0, intHistogram);
-		
-		plotFine.StartLine("Total");
-		plotGlobalSlopes.StartLine("Total");
-		for(LogHistogram::iterator i=intHistogram.begin(); i!=intHistogram.end(); ++i)
-		{
-			plotFine.PushDataPoint(i.value(), i.normalizedCount(LogHistogram::TotalAmplitudeHistogram));
-			plotGlobalSlopes.PushDataPoint(i.value(), intHistogram.NormalizedSlope(i.value()*0.5, i.value()*2.0, LogHistogram::TotalAmplitudeHistogram));
-		}
-		plotFine.StartLine("RFI");
-		plotGlobalSlopes.StartLine("RFI");
-		for(LogHistogram::iterator i=intHistogram.begin(); i!=intHistogram.end(); ++i)
-		{
-			plotFine.PushDataPoint(i.value(), i.normalizedCount(LogHistogram::RFIAmplitudeHistogram));
-			plotGlobalSlopes.PushDataPoint(i.value(), intHistogram.NormalizedSlope(i.value()*0.5, i.value()*2.0, LogHistogram::RFIAmplitudeHistogram));
-		}
-		plotFine.StartLine("Data");
-		plotGlobalSlopes.StartLine("Data");
-		for(LogHistogram::iterator i=intHistogram.begin(); i!=intHistogram.end(); ++i)
-		{
-			plotFine.PushDataPoint(i.value(), i.normalizedCount(LogHistogram::DataAmplitudeHistogram));
-			plotGlobalSlopes.PushDataPoint(i.value(), intHistogram.NormalizedSlope(i.value()*0.5, i.value()*2.0, LogHistogram::DataAmplitudeHistogram));
-		}*/
 	}
 	
 	std::cout << "Done.\n";
 }
 
+void actionCollectHistogram(const std::string &filename, HistogramCollection &histogramCollection)
+{
+	StatisticsCollection tempCollection;
+	actionCollect(filename, CollectHistograms, tempCollection, histogramCollection);
+}
+
 void printStatistics(std::complex<long double> *complexStat, unsigned count)
 {
 	if(count != 1)
@@ -362,10 +311,7 @@ void actionQueryBaselines(const std::string &kindName, const std::string &filena
 
 void actionQueryTime(const std::string &kindName, const std::string &filename)
 {
-	MeasurementSet *ms = new MeasurementSet(filename);
-	const unsigned polarizationCount = ms->GetPolarizationCount();
-	delete ms;
-	
+	const unsigned polarizationCount = MeasurementSet::GetPolarizationCount(filename);
 	const QualityTablesFormatter::StatisticKind kind = QualityTablesFormatter::NameToKind(kindName);
 	
 	QualityTablesFormatter formatter(filename);
@@ -394,12 +340,13 @@ void actionQueryTime(const std::string &kindName, const std::string &filename)
 void actionSummarize(const std::string &filename)
 {
 	bool remote = aoRemote::ClusteredObservation::IsClusteredFilename(filename);
-	StatisticsCollection collection;
+	StatisticsCollection statisticsCollection;
+	HistogramCollection histogramCollection;
 	if(remote)
 	{
 		aoRemote::ClusteredObservation *observation = aoRemote::ClusteredObservation::Load(filename);
 		aoRemote::ProcessCommander commander(*observation);
-		commander.PushReadQualityTablesTask(&collection);
+		commander.PushReadQualityTablesTask(&statisticsCollection, &histogramCollection);
 		commander.Run();
 		delete observation;
 	}
@@ -408,26 +355,26 @@ void actionSummarize(const std::string &filename)
 		const unsigned polarizationCount = ms->GetPolarizationCount();
 		delete ms;
 		
-		collection.SetPolarizationCount(polarizationCount);
+		statisticsCollection.SetPolarizationCount(polarizationCount);
 		QualityTablesFormatter qualityData(filename);
-		collection.Load(qualityData);
+		statisticsCollection.Load(qualityData);
 	}
 	
-	DefaultStatistics statistics(collection.PolarizationCount());
+	DefaultStatistics statistics(statisticsCollection.PolarizationCount());
 	
-	collection.GetGlobalTimeStatistics(statistics);
+	statisticsCollection.GetGlobalTimeStatistics(statistics);
 	std::cout << "Time statistics: \n";
 	printStatistics(statistics);
 	
-	collection.GetGlobalFrequencyStatistics(statistics);
+	statisticsCollection.GetGlobalFrequencyStatistics(statistics);
 	std::cout << "\nFrequency statistics: \n";
 	printStatistics(statistics);
 
-	collection.GetGlobalCrossBaselineStatistics(statistics);
+	statisticsCollection.GetGlobalCrossBaselineStatistics(statistics);
 	std::cout << "\nCross-correlated baseline statistics: \n";
 	printStatistics(statistics);
 
-	collection.GetGlobalAutoBaselineStatistics(statistics);
+	statisticsCollection.GetGlobalAutoBaselineStatistics(statistics);
 	std::cout << "\nAuto-correlated baseline: \n";
 	printStatistics(statistics);
 }
@@ -480,6 +427,49 @@ void actionRemove(const std::string &filename)
 	formatter.RemoveAllQualityTables();
 }
 
+void actionHistogram(const std::string &filename, const std::string &query)
+{
+	HistogramTablesFormatter histogramFormatter(filename);
+	const unsigned polarizationCount = MeasurementSet::GetPolarizationCount(filename);
+	if(query == "rfislope")
+	{
+		HistogramCollection collection(polarizationCount);
+		collection.Load(histogramFormatter);
+		MeasurementSet set(filename);
+		std::cout << set.GetBandInfo(0).CenterFrequencyHz();
+		for(unsigned p=0;p<polarizationCount;++p)
+		{
+			LogHistogram histogram;
+			collection.GetRFIHistogramForCrossCorrelations(p, histogram);
+			std::cout <<  '\t' << histogram.NormalizedSlopeInRFIRegion();
+		}
+		std::cout << '\n';
+	} else if(query == "rfislope-per-baseline")
+	{
+		HistogramCollection collection;
+		actionCollectHistogram(filename, collection);
+		MeasurementSet set(filename);
+		size_t antennaCount = set.AntennaCount();
+		AntennaInfo antennae[antennaCount];
+		for(size_t a=0;a<antennaCount;++a)
+			antennae[a] = set.GetAntennaInfo(a);
+		
+		for(unsigned p=0;p<polarizationCount;++p)
+		{
+			const std::map<HistogramCollection::AntennaPair, LogHistogram*> &histogramMap = collection.GetRFIHistogram(p);
+			for(std::map<HistogramCollection::AntennaPair, LogHistogram*>::const_iterator i=histogramMap.begin(); i!=histogramMap.end();++i)
+			{
+				const unsigned a1 = i->first.first, a2 = i->first.second;
+				Baseline baseline(antennae[a1], antennae[a2]);
+				double length = baseline.Distance();
+				const LogHistogram &histogram = *i->second;
+				double slope = histogram.NormalizedSlopeInRFIRegion();
+				std::cout << p << '\t' << a1 << '\t' << a2 << '\t' << length << '\t' << slope << '\n';
+			}
+		}
+	}
+}
+
 void printSyntax(std::ostream &stream, char *argv[])
 {
 	stream << "Syntax: " << argv[0] <<
@@ -488,7 +478,8 @@ void printSyntax(std::ostream &stream, char *argv[])
 		"\thelp        - Get more info about an action (usage: '" << argv[0] << " help <action>')\n"
 		"\tcollect     - Processes the entire measurement set, collects the statistics\n"
 		"\t              and writes them in the quality tables.\n"
-		"\\tcombine     - Combine several tables.\n"
+		"\tcombine     - Combine several tables.\n"
+		"\thistogram   - Various histogram actions.\n"
 		"\tquery_b     - Query baselines.\n"
 		"\tquery_t     - Query time.\n"
 		"\tremove      - Remove all quality tables.\n"
@@ -555,6 +546,13 @@ int main(int argc, char *argv[])
 						"write the results to a target measurement set. The target measurement set should\n"
 						"not exist beforehand.\n";
 				}
+				else if(helpAction == "histogram")
+				{
+					std::cout << "Syntax: " << argv[0] << " histogram <query> <ms>]\n\n"
+						"Query can be:\n"
+						"\trfislope - performs linear regression on the part of the histogram that should contain the RFI.\n"
+						"\t           Reports one value per polarisation.\n";
+				}
 				else if(helpAction == "remove")
 				{
 					std::cout << "Syntax: " << argv[0] << " remove [ms]\n\n"
@@ -594,6 +592,17 @@ int main(int argc, char *argv[])
 				actionCombine(outFilename, inFilenames);
 			}
 		}
+		else if(action == "histogram")
+		{
+			if(argc != 4)
+			{
+				std::cerr << "histogram actions needs two parameters (the query and the measurement set)\n";
+				return -1;
+			}
+			else {
+				actionHistogram(argv[3], argv[2]);
+			}
+		}
 		else if(action == "summarize")
 		{
 			if(argc != 3)
diff --git a/CEP/DP3/AOFlagger/src/gui/imagewidget.cpp b/CEP/DP3/AOFlagger/src/gui/imagewidget.cpp
index 68e11fa64b3c406e39dcd4b8bde310385d3234ca..98c8eb5c012775b83e587b88cdd4a61b52573e3e 100644
--- a/CEP/DP3/AOFlagger/src/gui/imagewidget.cpp
+++ b/CEP/DP3/AOFlagger/src/gui/imagewidget.cpp
@@ -58,13 +58,16 @@ ImageWidget::ImageWidget() :
 	_cairoFilter(Cairo::FILTER_BEST),
 	_manualXAxisDescription(false),
 	_manualYAxisDescription(false),
-	_manualZAxisDescription(false)
+	_manualZAxisDescription(false),
+	_mouseIsIn(false)
 {
 	_highlightConfig = new ThresholdConfig();
 	_highlightConfig->InitializeLengthsSingleSample();
 
-	add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::BUTTON_PRESS_MASK);
+	add_events(Gdk::POINTER_MOTION_MASK | Gdk::BUTTON_RELEASE_MASK |
+		   Gdk::BUTTON_PRESS_MASK | Gdk::LEAVE_NOTIFY_MASK);
 	signal_motion_notify_event().connect(sigc::mem_fun(*this, &ImageWidget::onMotion));
+	signal_leave_notify_event().connect(sigc::mem_fun(*this, &ImageWidget::onLeave));
 	signal_button_release_event().connect(sigc::mem_fun(*this, &ImageWidget::onButtonReleased));
 	signal_expose_event().connect(sigc::mem_fun(*this, &ImageWidget::onExposeEvent) );
 }
@@ -620,7 +623,23 @@ bool ImageWidget::onMotion(GdkEventMotion *event)
 	{
 		int posX, posY;
 		if(toUnits(event->x, event->y, posX, posY))
+		{
+			_mouseIsIn = true;
 			_onMouseMoved(posX, posY);
+		} else if(_mouseIsIn) {
+			_onMouseLeft();
+			_mouseIsIn = false;
+		}
+	}
+	return true;
+}
+
+bool ImageWidget::onLeave(GdkEventCrossing *event)
+{
+	if(_mouseIsIn)
+	{
+		_onMouseLeft();
+		_mouseIsIn = false;
 	}
 	return true;
 }
diff --git a/CEP/DP3/AOFlagger/src/gui/mswindow.cpp b/CEP/DP3/AOFlagger/src/gui/mswindow.cpp
index 8d7fa3019b2a3366fe061b5fa74002394eecf170..e704732ebd1925ad7b89f6cfd6695ce4738a4f74 100644
--- a/CEP/DP3/AOFlagger/src/gui/mswindow.cpp
+++ b/CEP/DP3/AOFlagger/src/gui/mswindow.cpp
@@ -72,6 +72,7 @@
 #include <AOFlagger/gui/editstrategywindow.h>
 #include <AOFlagger/gui/gotowindow.h>
 #include <AOFlagger/gui/highlightwindow.h>
+#include <AOFlagger/gui/histogramwindow.h>
 #include <AOFlagger/gui/imageplanewindow.h>
 #include <AOFlagger/gui/imagepropertieswindow.h>
 #include <AOFlagger/gui/msoptionwindow.h>
@@ -88,12 +89,13 @@
 
 #include <iostream>
 
-MSWindow::MSWindow() : _imagePlaneWindow(0), _optionWindow(0), _editStrategyWindow(0), _gotoWindow(0), _progressWindow(0), _highlightWindow(0), _plotComplexPlaneWindow(0), _imagePropertiesWindow(0), _antennaMapWindow(0), _statistics(new RFIStatistics()),  _imageSet(0), _imageSetIndex(0), _gaussianTestSets(true), _spatialMetaData(0), _plotWindow(_plotManager)
+MSWindow::MSWindow() : _imagePlaneWindow(0), _histogramWindow(0), _optionWindow(0), _editStrategyWindow(0), _gotoWindow(0), _progressWindow(0), _highlightWindow(0), _plotComplexPlaneWindow(0), _imagePropertiesWindow(0), _antennaMapWindow(0), _statistics(new RFIStatistics()),  _imageSet(0), _imageSetIndex(0), _gaussianTestSets(true), _spatialMetaData(0), _plotWindow(_plotManager)
 {
 	createToolbar();
 
 	_mainVBox.pack_start(_timeFrequencyWidget);
 	_timeFrequencyWidget.OnMouseMovedEvent().connect(sigc::mem_fun(*this, &MSWindow::onTFWidgetMouseMoved));
+	_timeFrequencyWidget.OnMouseLeaveEvent().connect(sigc::mem_fun(*this, &MSWindow::setSetNameInStatusBar));
 	_timeFrequencyWidget.OnButtonReleasedEvent().connect(sigc::mem_fun(*this, &MSWindow::onTFWidgetButtonReleased));
 	_timeFrequencyWidget.SetShowXAxisDescription(false);
 	_timeFrequencyWidget.SetShowYAxisDescription(false);
@@ -116,6 +118,8 @@ MSWindow::MSWindow() : _imagePlaneWindow(0), _optionWindow(0), _editStrategyWind
 MSWindow::~MSWindow()
 {
 	delete _imagePlaneWindow;
+	if(_histogramWindow != 0)
+		delete _histogramWindow;
 	if(_optionWindow != 0)
 		delete _optionWindow;
 	if(_editStrategyWindow != 0)
@@ -302,8 +306,7 @@ void MSWindow::loadCurrentTFData()
 				_spatialMetaData = new SpatialMatrixMetaData(static_cast<rfiStrategy::SpatialMSImageSet*>(_imageSet)->SpatialMetaData(*_imageSetIndex));
 			}
 			_timeFrequencyWidget.Update();
-			_statusbar.pop();
-			_statusbar.push(std::string() + _imageSet->Name() + ": " + _imageSetIndex->Description());
+			setSetNameInStatusBar();
 		} catch(std::exception &e)
 		{
 			AOLogger::Error << e.what() << '\n';
@@ -312,6 +315,14 @@ void MSWindow::loadCurrentTFData()
 	}
 }
 
+void MSWindow::setSetNameInStatusBar()
+{
+  if(HasImageSet()) {
+	_statusbar.pop();
+	_statusbar.push(std::string() + _imageSet->Name() + ": " + _imageSetIndex->Description());
+  }
+}
+		
 void MSWindow::onLoadPrevious()
 {
 	if(_imageSet != 0) {
@@ -468,7 +479,12 @@ void MSWindow::SetImageSet(rfiStrategy::ImageSet *newImageSet)
 	_imageSet = newImageSet;
 	_imageSetIndex = _imageSet->StartIndex();
 	
-	loadCurrentTFData();
+	if(dynamic_cast<rfiStrategy::MSImageSet*>(newImageSet) != 0)
+	{
+		onGoToPressed();
+	} else {
+		loadCurrentTFData();
+	}
 }
 
 void MSWindow::SetImageSetIndex(rfiStrategy::ImageSetIndex *newImageSetIndex)
@@ -573,6 +589,12 @@ void MSWindow::createToolbar()
 	sigc::mem_fun(*this, &MSWindow::onOpenTestSetSlewedGaussianBroadband));
 	_actionGroup->add( Gtk::Action::create("OpenTestSetBurstBroadband", "Burst"),
 	sigc::mem_fun(*this, &MSWindow::onOpenTestSetBurstBroadband));
+	_actionGroup->add( Gtk::Action::create("OpenTestSetRFIDistributionLow", "Slope -2 dist low"),
+	sigc::mem_fun(*this, &MSWindow::onOpenTestSetRFIDistributionLow));
+	_actionGroup->add( Gtk::Action::create("OpenTestSetRFIDistributionMid", "Slope -2 dist mid"),
+	sigc::mem_fun(*this, &MSWindow::onOpenTestSetRFIDistributionMid));
+	_actionGroup->add( Gtk::Action::create("OpenTestSetRFIDistributionHigh", "Slope -2 dist high"),
+	sigc::mem_fun(*this, &MSWindow::onOpenTestSetRFIDistributionHigh));
 	_actionGroup->add( Gtk::Action::create("AddTestModification", "Test modify") );
 	_actionGroup->add( Gtk::Action::create("AddStaticFringe", "Static fringe"),
 	sigc::mem_fun(*this, &MSWindow::onAddStaticFringe) );
@@ -813,6 +835,9 @@ void MSWindow::createToolbar()
 		"        <menuitem action='OpenTestSetSinusoidalBroadband'/>"
 		"        <menuitem action='OpenTestSetSlewedGaussianBroadband'/>"
 		"        <menuitem action='OpenTestSetBurstBroadband'/>"
+		"        <menuitem action='OpenTestSetRFIDistributionLow'/>"
+		"        <menuitem action='OpenTestSetRFIDistributionMid'/>"
+		"        <menuitem action='OpenTestSetRFIDistributionHigh'/>"
 		"      </menu>"
 		"      <menu action='AddTestModification'>"
 		"        <menuitem action='AddStaticFringe'/>"
@@ -1165,15 +1190,20 @@ void MSWindow::onPlotLogLogDistPressed()
 {
 	if(_timeFrequencyWidget.HasImage())
 	{
-		Plot2D &plot = _plotManager.NewPlot2D("Log-log distribution");
-
 		TimeFrequencyData activeData = GetActiveData();
-		Image2DCPtr image = activeData.GetSingleImage();
-		Mask2DCPtr mask = Mask2D::CreateCopy(activeData.GetSingleMask());
-		HistogramCollection histograms(1);
-		histograms.Add(0, 1, 0, image, mask);
-		histograms.Plot(plot, 0);
-		_plotManager.Update();
+		HistogramCollection histograms(activeData.PolarisationCount());
+		for(unsigned p=0;p!=activeData.PolarisationCount();++p)
+		{
+			TimeFrequencyData *polData = activeData.CreateTFDataFromPolarisationIndex(p);
+			Image2DCPtr image = polData->GetSingleImage();
+			Mask2DCPtr mask = Mask2D::CreateCopy(polData->GetSingleMask());
+			histograms.Add(0, 1, p, image, mask);
+		}
+		if(_histogramWindow == 0)
+			_histogramWindow = new HistogramWindow(histograms);
+		else
+			_histogramWindow->SetStatistics(histograms);
+		_histogramWindow->show();
 	}
 }
 
diff --git a/CEP/DP3/AOFlagger/src/gui/plot/plot2d.cpp b/CEP/DP3/AOFlagger/src/gui/plot/plot2d.cpp
index 2919ee7e5c463bea7afd8520287016a438d7468a..a3e567ba9dbf0886a1af210b78c5d82dafe8359a 100644
--- a/CEP/DP3/AOFlagger/src/gui/plot/plot2d.cpp
+++ b/CEP/DP3/AOFlagger/src/gui/plot/plot2d.cpp
@@ -238,7 +238,7 @@ void Plot2D::render(Cairo::RefPtr<Cairo::Context> cr, Plot2DPointSet &pointSet)
 				y2Val = (log10(pointSet.GetY(i+1)) - minYLog10) / (maxYLog10 - minYLog10);
 		} else {
 			y1Val = (pointSet.GetY(i) - yMin) / (yMax - yMin);
-			y2Val = (pointSet.GetY(i) - yMin) / (yMax - yMin);
+			y2Val = (pointSet.GetY(i+1) - yMin) / (yMax - yMin);
 		}
 		if(y1Val < 0.0) y1Val = 0.0;
 		if(y1Val > 1.0) y1Val = 1.0;
diff --git a/CEP/DP3/AOFlagger/src/gui/quality/aoqplotwindow.cpp b/CEP/DP3/AOFlagger/src/gui/quality/aoqplotwindow.cpp
index 0cad3a27ffbd021cd11edf013ab0646edd921c5a..d7ded01f276b39c8921bccf68a7635be047c708f 100644
--- a/CEP/DP3/AOFlagger/src/gui/quality/aoqplotwindow.cpp
+++ b/CEP/DP3/AOFlagger/src/gui/quality/aoqplotwindow.cpp
@@ -18,20 +18,21 @@
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
+#include <AOFlagger/gui/quality/aoqplotwindow.h>
+
 #include <limits>
 
+#include <gtkmm/main.h>
 #include <gtkmm/messagedialog.h>
 
-#include <AOFlagger/gui/quality/aoqplotwindow.h>
-
 #include <AOFlagger/msio/measurementset.h>
 
+#include <AOFlagger/quality/histogramtablesformatter.h>
+#include <AOFlagger/quality/histogramcollection.h>
 #include <AOFlagger/quality/statisticscollection.h>
 
 #include <AOFlagger/remote/clusteredobservation.h>
 #include <AOFlagger/remote/processcommander.h>
-#include <gtkmm/main.h>
-#include <AOFlagger/quality/histogramtablesformatter.h>
 
 AOQPlotWindow::AOQPlotWindow() :
 	_isOpen(false)
@@ -94,7 +95,7 @@ void AOQPlotWindow::onOpenOptionsSelected(std::string filename, bool downsampleT
 	_timeFrequencyPlotPage.SetStatistics(_fullStats);
 	_summaryPage.SetStatistics(_statCollection);
 	if(_histogramPage.is_visible())
-		_histogramPage.SetStatistics(_filename);
+		_histogramPage.SetStatistics(*_histCollection);
 	show();
 }
 
@@ -109,12 +110,11 @@ void AOQPlotWindow::close()
 		_frequencyPlotPage.CloseStatistics();
 		_timeFrequencyPlotPage.CloseStatistics();
 		_summaryPage.CloseStatistics();
-		if(_histogramPage.is_visible())
-			_histogramPage.CloseStatistics();
+		_histogramPage.CloseStatistics();
 		delete _statCollection;
+		delete _histCollection;
 		delete _fullStats;
 		_isOpen = false;
-		
 	}
 }
 
@@ -126,9 +126,10 @@ void AOQPlotWindow::readStatistics(bool downsampleTime, bool downsampleFreq, siz
 	{
 		aoRemote::ClusteredObservation *observation = aoRemote::ClusteredObservation::Load(_filename);
 		_statCollection = new StatisticsCollection();
+		_histCollection = new HistogramCollection();
 		aoRemote::ProcessCommander commander(*observation);
 		commander.PushReadAntennaTablesTask();
-		commander.PushReadQualityTablesTask(_statCollection);
+		commander.PushReadQualityTablesTask(_statCollection, _histCollection);
 		commander.Run();
 		if(!commander.Errors().empty())
 		{
@@ -152,8 +153,6 @@ void AOQPlotWindow::readStatistics(bool downsampleTime, bool downsampleFreq, siz
 		delete observation;
 		
 		_antennas = commander.Antennas();
-		
-		setShowHistograms(false);
 	}
 	else {
 		MeasurementSet *ms = new MeasurementSet(_filename);
@@ -169,8 +168,13 @@ void AOQPlotWindow::readStatistics(bool downsampleTime, bool downsampleFreq, siz
 		_statCollection->Load(qualityTables);
 		
 		HistogramTablesFormatter histogramTables(_filename);
-		setShowHistograms(histogramTables.HistogramsExist());
+		_histCollection = new HistogramCollection(polarizationCount);
+		if(histogramTables.HistogramsExist())
+		{
+			_histCollection->Load(histogramTables);
+		}
 	}
+	setShowHistograms(!_histCollection->Empty());
 	if(downsampleTime)
 	{
 		std::cout << "Lowering time resolution..." << std::endl;
diff --git a/CEP/DP3/AOFlagger/src/gui/quality/datawindow.cpp b/CEP/DP3/AOFlagger/src/gui/quality/datawindow.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..337a3bd4ae49cc17c153b4386beab017072fb04a
--- /dev/null
+++ b/CEP/DP3/AOFlagger/src/gui/quality/datawindow.cpp
@@ -0,0 +1,51 @@
+/***************************************************************************
+ *   Copyright (C) 2008 by A.R. Offringa   *
+ *   offringa@astro.rug.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 <AOFlagger/gui/quality/datawindow.h>
+#include <AOFlagger/gui/plot/plot2d.h>
+
+#include <sstream>
+#include <iomanip>
+
+void DataWindow::SetData(const Plot2D &plot)
+{
+	std::stringstream _dataStream;
+	_dataStream << std::setprecision(14);
+	if(plot.PointSetCount() != 0)
+	{
+		const Plot2DPointSet &pointSet = plot.GetPointSet(0);
+		const size_t valueCount = pointSet.Size();
+		for(size_t i=0; i<valueCount; ++i)
+		{
+			const double
+				x = pointSet.GetX(i),
+				y = pointSet.GetY(i);
+			if(pointSet.HasTickLabels())
+			{
+				std::string label = pointSet.TickLabels()[i];
+				_dataStream << i << '\t' << label << '\t' << y << '\n';
+			}
+			else
+				_dataStream << i << '\t' << x << '\t' << y << '\n';
+		}
+	}
+	SetData(_dataStream.str());
+}
+
diff --git a/CEP/DP3/AOFlagger/src/gui/quality/histogrampage.cpp b/CEP/DP3/AOFlagger/src/gui/quality/histogrampage.cpp
index 63247befcc57c41772b1a123b131a46ceb224fdb..3053bae58f951ca74fc852ca080f7b8c65018342 100644
--- a/CEP/DP3/AOFlagger/src/gui/quality/histogrampage.cpp
+++ b/CEP/DP3/AOFlagger/src/gui/quality/histogrampage.cpp
@@ -23,6 +23,7 @@
 #include <boost/bind.hpp>
 
 #include <AOFlagger/gui/plot/plotpropertieswindow.h>
+#include <AOFlagger/gui/quality/datawindow.h>
 
 #include <AOFlagger/quality/histogramtablesformatter.h>
 
@@ -39,6 +40,7 @@ HistogramPage::HistogramPage() :
 	_xyPolarizationButton("XY"),
 	_yxPolarizationButton("YX"),
 	_yyPolarizationButton("YY"),
+	_sumPolarizationButton("Sum"),
 	_fitFrame("Fitting"),
 	_fitButton("Fit"),
 	_subtractFitButton("Subtract"),
@@ -47,7 +49,13 @@ HistogramPage::HistogramPage() :
 	_nsButton("N(S)"),
 	_dndsButton("dN(S)/dS"),
 	_plotPropertiesButton("Properties"),
-	_plotPropertiesWindow(0)
+	_dataExportButton("Data"),
+	_slopeFrame("Slope"),
+	_drawSlopeButton("Draw"),
+	_slopeAutoRangeButton("Auto range"),
+	_plotPropertiesWindow(0),
+	_histograms(0),
+	_summedPolarizationHistograms(0)
 	{
 	_histogramTypeBox.pack_start(_totalHistogramButton, Gtk::PACK_SHRINK);
 	_totalHistogramButton.set_active(true);
@@ -64,7 +72,7 @@ HistogramPage::HistogramPage() :
 	_sideBox.pack_start(_histogramTypeFrame, Gtk::PACK_SHRINK);
 	
 	_polarizationBox.pack_start(_xxPolarizationButton, Gtk::PACK_SHRINK);
-	_xxPolarizationButton.set_active(true);
+	_xxPolarizationButton.set_active(false);
 	_xxPolarizationButton.signal_clicked().connect(sigc::mem_fun(*this, &HistogramPage::updatePlot));
 	_polarizationBox.pack_start(_xyPolarizationButton, Gtk::PACK_SHRINK);
 	_xyPolarizationButton.set_active(false);
@@ -73,8 +81,11 @@ HistogramPage::HistogramPage() :
 	_yxPolarizationButton.set_active(false);
 	_yxPolarizationButton.signal_clicked().connect(sigc::mem_fun(*this, &HistogramPage::updatePlot));
 	_polarizationBox.pack_start(_yyPolarizationButton, Gtk::PACK_SHRINK);
-	_yyPolarizationButton.set_active(true);
+	_yyPolarizationButton.set_active(false);
 	_yyPolarizationButton.signal_clicked().connect(sigc::mem_fun(*this, &HistogramPage::updatePlot));
+	_polarizationBox.pack_start(_sumPolarizationButton, Gtk::PACK_SHRINK);
+	_sumPolarizationButton.set_active(true);
+	_sumPolarizationButton.signal_clicked().connect(sigc::mem_fun(*this, &HistogramPage::updatePlot));
 
 	_polarizationFrame.add(_polarizationBox);
 	
@@ -114,18 +125,84 @@ HistogramPage::HistogramPage() :
 	_plotPropertiesButton.signal_clicked().connect(sigc::mem_fun(*this, &HistogramPage::onPlotPropertiesClicked));
 	_sideBox.pack_start(_plotPropertiesButton, Gtk::PACK_SHRINK);
 	
+	_dataExportButton.signal_clicked().connect(sigc::mem_fun(*this, &HistogramPage::onDataExportClicked));
+	_sideBox.pack_start(_dataExportButton, Gtk::PACK_SHRINK);
+	
+	_slopeBox.pack_start(_slopeTextView, Gtk::PACK_SHRINK);
+	_drawSlopeButton.signal_clicked().connect(sigc::mem_fun(*this, &HistogramPage::updatePlot));
+	_slopeBox.pack_start(_drawSlopeButton, Gtk::PACK_SHRINK);
+
+	_slopeBox.pack_start(_slopeAutoRangeButton, Gtk::PACK_SHRINK);
+	_slopeAutoRangeButton.set_active(true);
+	_slopeAutoRangeButton.signal_clicked().connect(sigc::mem_fun(*this, &HistogramPage::onSlopeAutoRangeClicked));
+	
+	_slopeBox.pack_start(_slopeStartEntry, Gtk::PACK_SHRINK);
+	_slopeStartEntry.set_sensitive(false);
+	_slopeStartEntry.signal_activate().connect(sigc::mem_fun(*this, &HistogramPage::updatePlot));
+	_slopeBox.pack_start(_slopeEndEntry, Gtk::PACK_SHRINK);
+	_slopeEndEntry.set_sensitive(false);
+	_slopeEndEntry.signal_activate().connect(sigc::mem_fun(*this, &HistogramPage::updatePlot));
+	_slopeBox.pack_start(_slopeRFIRatio, Gtk::PACK_SHRINK);
+	_slopeRFIRatio.set_text("1.0");
+	_slopeRFIRatio.signal_activate().connect(sigc::mem_fun(*this, &HistogramPage::updatePlot));
+	
+	_slopeFrame.add(_slopeBox);
+	_sideBox.pack_start(_slopeFrame, Gtk::PACK_SHRINK);
+	
 	pack_start(_sideBox, Gtk::PACK_SHRINK);
 	
 	_plotWidget.SetPlot(_plot);
 	pack_start(_plotWidget, Gtk::PACK_EXPAND_WIDGET);
 	
 	show_all_children();
+	
+	_dataWindow = new DataWindow();
 }
 
 HistogramPage::~HistogramPage()
 {
+	CloseStatistics();
 	if(_plotPropertiesWindow != 0)
 		delete _plotPropertiesWindow;
+	delete _dataWindow;
+}
+
+void HistogramPage::readFromFile()
+{
+	CloseStatistics();
+	HistogramTablesFormatter histogramTables(_statFilename);
+	if(histogramTables.HistogramsExist())
+	{
+		MeasurementSet set(_statFilename);
+		
+		const unsigned polarizationCount = set.GetPolarizationCount();
+
+		_histograms = new HistogramCollection(polarizationCount);
+		_histograms->Load(histogramTables);
+	}
+}
+
+void HistogramPage::CloseStatistics()
+{
+	_statFilename = std::string();
+	if(_histograms != 0)
+	{
+		delete _histograms;
+		_histograms = 0;
+	}
+	if(_summedPolarizationHistograms != 0)
+	{
+		delete _summedPolarizationHistograms;
+		_summedPolarizationHistograms = 0;
+	}
+}
+
+void HistogramPage::SetStatistics(HistogramCollection &collection)
+{
+	CloseStatistics();
+	_histograms = new HistogramCollection(collection);
+	_summedPolarizationHistograms = _histograms->CreateSummedPolarizationCollection();
+	updatePlot();
 }
 
 void HistogramPage::updatePlot()
@@ -134,37 +211,37 @@ void HistogramPage::updatePlot()
 	{
 		_plot.Clear();
 		
-		HistogramTablesFormatter histogramTables(_statFilename);
-		if(histogramTables.HistogramsExist())
-		{
-			MeasurementSet set(_statFilename);
-			
-			const unsigned polarizationCount = set.GetPolarizationCount();
-
-			HistogramCollection histograms(polarizationCount);
-			histograms.Load(histogramTables);
-			
-			if(_xxPolarizationButton.get_active())
-				plotPolarization(histograms, 0);
-			if(_xyPolarizationButton.get_active() && polarizationCount>=1)
-				plotPolarization(histograms, 1);
-			if(_yxPolarizationButton.get_active() && polarizationCount>=2)
-				plotPolarization(histograms, 2);
-			if(_yyPolarizationButton.get_active() && polarizationCount>=3)
-				plotPolarization(histograms, 3);
-		}
+		const unsigned polarizationCount = _histograms->PolarizationCount();
+		if(_xxPolarizationButton.get_active())
+			plotPolarization(*_histograms, 0);
+		if(_xyPolarizationButton.get_active() && polarizationCount>=2)
+			plotPolarization(*_histograms, 1);
+		if(_yxPolarizationButton.get_active() && polarizationCount>=3)
+			plotPolarization(*_histograms, 2);
+		if(_yyPolarizationButton.get_active() && polarizationCount>=4)
+			plotPolarization(*_histograms, 3);
+		if(_sumPolarizationButton.get_active())
+			plotPolarization(*_summedPolarizationHistograms, 0);
 		
 		_plotWidget.Update();
+		updateSlopeFrame();
+		updateDataWindow();
 	}
 }
 
-void HistogramPage::plotPolarization(class HistogramCollection &histograms, unsigned p)
+void HistogramPage::plotPolarization(const HistogramCollection &histogramCollection, unsigned polarization)
+{
+	LogHistogram totalHistogram, rfiHistogram;
+	histogramCollection.GetTotalHistogramForCrossCorrelations(polarization, totalHistogram);
+	histogramCollection.GetRFIHistogramForCrossCorrelations(polarization, rfiHistogram);
+	plotPolarization(totalHistogram, rfiHistogram);
+}
+
+void HistogramPage::plotPolarization(const LogHistogram &totalHistogram, const LogHistogram &rfiHistogram)
 {
 	if(_totalHistogramButton.get_active())
 	{
 		_plot.StartLine("Total histogram", "Amplitude in arbitrary units (log)", "Frequency (log)");
-		LogHistogram totalHistogram;
-		histograms.GetTotalHistogramForCrossCorrelations(p, totalHistogram);
 		addHistogramToPlot(totalHistogram);
 		
 		if(_fitButton.get_active() || _subtractFitButton.get_active())
@@ -176,8 +253,6 @@ void HistogramPage::plotPolarization(class HistogramCollection &histograms, unsi
 	if(_rfiHistogramButton.get_active())
 	{
 		_plot.StartLine("RFI histogram", "Amplitude in arbitrary units (log)", "Frequency (log)");
-		LogHistogram rfiHistogram;
-		histograms.GetRFIHistogramForCrossCorrelations(p, rfiHistogram);
 		addHistogramToPlot(rfiHistogram);
 
 		if(_fitButton.get_active() || _subtractFitButton.get_active())
@@ -185,14 +260,15 @@ void HistogramPage::plotPolarization(class HistogramCollection &histograms, unsi
 			plotFit(rfiHistogram, "Fit to RFI");
 		}
 	}
+	if(_drawSlopeButton.get_active())
+	{
+		plotSlope(rfiHistogram, "Fitted slope");
+	}
 	
 	if(_notRFIHistogramButton.get_active())
 	{
 		_plot.StartLine("Non-RFI histogram", "Amplitude in arbitrary units (log)", "Frequency (log)");
-		LogHistogram histogram;
-		histograms.GetTotalHistogramForCrossCorrelations(p, histogram);
-		LogHistogram rfiHistogram;
-		histograms.GetRFIHistogramForCrossCorrelations(p, rfiHistogram);
+		LogHistogram histogram(totalHistogram);
 		histogram -= rfiHistogram;
 		addHistogramToPlot(histogram);
 
@@ -203,7 +279,7 @@ void HistogramPage::plotPolarization(class HistogramCollection &histograms, unsi
 	}
 }
 
-void HistogramPage::plotFit(class LogHistogram &histogram, const std::string &title)
+void HistogramPage::plotFit(const LogHistogram &histogram, const std::string &title)
 {
 	double minRange, maxRange, sigmaEstimate;
 	sigmaEstimate = RayleighFitter::SigmaEstimate(histogram);
@@ -234,7 +310,7 @@ void HistogramPage::plotFit(class LogHistogram &histogram, const std::string &ti
 	}
 }
 
-void HistogramPage::addHistogramToPlot(LogHistogram &histogram)
+void HistogramPage::addHistogramToPlot(const LogHistogram &histogram)
 {
 	const bool derivative = _dndsButton.get_active();
 	for(LogHistogram::iterator i=histogram.begin();i!=histogram.end();++i)
@@ -256,7 +332,7 @@ void HistogramPage::addHistogramToPlot(LogHistogram &histogram)
 	}
 }
 
-void HistogramPage::addRayleighToPlot(LogHistogram &histogram, double sigma, double n)
+void HistogramPage::addRayleighToPlot(const LogHistogram &histogram, double sigma, double n)
 {
 	const bool derivative = _dndsButton.get_active();
 	double x = histogram.MinPositiveAmplitude();
@@ -279,7 +355,7 @@ void HistogramPage::addRayleighToPlot(LogHistogram &histogram, double sigma, dou
 	}
 }
 
-void HistogramPage::addRayleighDifferenceToPlot(LogHistogram &histogram, double sigma, double n)
+void HistogramPage::addRayleighDifferenceToPlot(const LogHistogram &histogram, double sigma, double n)
 {
 	const double sigmaP2 = sigma*sigma;
 	double minCount = histogram.MinPosNormalizedCount();
@@ -299,6 +375,27 @@ void HistogramPage::addRayleighDifferenceToPlot(LogHistogram &histogram, double
 	}
 }
 
+void HistogramPage::plotSlope(const LogHistogram &histogram, const std::string &title)
+{
+	double start, end;
+	if(_slopeAutoRangeButton.get_active())
+	{
+		histogram.GetRFIRegion(start, end);
+	} else {
+		start = atof(_slopeStartEntry.get_text().c_str());
+		end = atof(_slopeEndEntry.get_text().c_str());
+	}
+	double slope = histogram.NormalizedSlope(start, end);
+	double offset = histogram.NormalizedSlopeOffset(start, end, slope);
+	_plot.StartLine(title, "Amplitude in arbitrary units (log)", "Frequency (log)");
+	double xStart = log10(start / 10.0);
+	double xEnd = log10(histogram.MaxAmplitude());
+	double yStart = xStart*slope + offset;
+	double yEnd = xEnd*slope + offset;
+	_plot.PushDataPoint(xStart, yStart);
+	_plot.PushDataPoint(xEnd, yEnd);
+}
+
 void HistogramPage::onPlotPropertiesClicked()
 {
 	if(_plotPropertiesWindow == 0)
@@ -309,4 +406,62 @@ void HistogramPage::onPlotPropertiesClicked()
 	
 	_plotPropertiesWindow->show();
 	_plotPropertiesWindow->raise();
-}
\ No newline at end of file
+}
+
+void HistogramPage::onDataExportClicked()
+{
+	_dataWindow->show();
+	_dataWindow->raise();
+	updateDataWindow();
+}
+
+void HistogramPage::updateSlopeFrame()
+{
+	std::stringstream str;
+	str << "Slopes:";
+	
+	LogHistogram summedHistogram;
+	_summedPolarizationHistograms->GetRFIHistogramForCrossCorrelations(0, summedHistogram);
+	addSlopeText(str, summedHistogram, true);
+	
+	for(size_t p=0;p<_histograms->PolarizationCount();++p)
+	{
+		LogHistogram histogram;
+		_histograms->GetRFIHistogramForCrossCorrelations(p, histogram);
+		addSlopeText(str, histogram, false);
+	}
+	_slopeTextView.get_buffer()->set_text(str.str());
+}
+
+void HistogramPage::addSlopeText(std::stringstream &str, const LogHistogram &histogram, bool updateRange)
+{
+	double minRange, maxRange;
+	if(_slopeAutoRangeButton.get_active())
+	{
+		histogram.GetRFIRegion(minRange, maxRange);
+		if(updateRange)
+		{
+			std::stringstream minRangeStr, maxRangeStr;
+			minRangeStr << minRange;
+			maxRangeStr << maxRange;
+			_slopeStartEntry.set_text(minRangeStr.str());
+			_slopeEndEntry.set_text(maxRangeStr.str());
+		}
+	} else {
+		minRange = atof(_slopeStartEntry.get_text().c_str());
+		maxRange = atof(_slopeEndEntry.get_text().c_str());
+	}
+	double rfiRatio = atof(_slopeRFIRatio.get_text().c_str());
+
+	double slope = histogram.NormalizedSlope(minRange, maxRange);
+	double offset = histogram.NormalizedSlopeOffset(minRange, maxRange, slope);
+	double upperLimit = histogram.PowerLawUpperLimit(minRange, slope, pow10(offset));
+	double lowerLimit = histogram.PowerLawLowerLimit(minRange, slope, pow10(offset), rfiRatio);
+	str << '\n' << slope << '[' << log10(lowerLimit) << ';' << log10(upperLimit) << ']';
+}
+
+void HistogramPage::updateDataWindow()
+{
+	if(_dataWindow->is_visible())
+		_dataWindow->SetData(_plot);
+}
diff --git a/CEP/DP3/AOFlagger/src/gui/quality/twodimensionalplotpage.cpp b/CEP/DP3/AOFlagger/src/gui/quality/twodimensionalplotpage.cpp
index 5ccf4cc748c07118abf78c2a81ae1727e9005ef6..aa6dc837bec1c1cd6cb4f6fa3accb3d1c7e934c6 100644
--- a/CEP/DP3/AOFlagger/src/gui/quality/twodimensionalplotpage.cpp
+++ b/CEP/DP3/AOFlagger/src/gui/quality/twodimensionalplotpage.cpp
@@ -19,7 +19,6 @@
  ***************************************************************************/
 
 #include <limits>
-#include <sstream>
 
 #include <boost/bind.hpp>
 
@@ -339,27 +338,8 @@ void TwoDimensionalPlotPage::onPlotPropertiesClicked()
 
 void TwoDimensionalPlotPage::updateDataWindow()
 {
-	std::stringstream _dataStream;
-	 _dataStream << std::setprecision(14);
-	if(_plot.PointSetCount() != 0)
-	{
-		const Plot2DPointSet &pointSet = _plot.GetPointSet(0);
-		const size_t valueCount = pointSet.Size();
-		for(size_t i=0; i<valueCount; ++i)
-		{
-			const double
-				x = pointSet.GetX(i),
-				y = pointSet.GetY(i);
-			if(pointSet.HasTickLabels())
-			{
-				std::string label = pointSet.TickLabels()[i];
-				_dataStream << i << '\t' << label << '\t' << y << '\n';
-			}
-			else
-				_dataStream << i << '\t' << x << '\t' << y << '\n';
-		}
-	}
-	_dataWindow->SetData(_dataStream.str());
+	if(_dataWindow->is_visible())
+		_dataWindow->SetData(_plot);
 }
 
 void TwoDimensionalPlotPage::onDataExportClicked()
diff --git a/CEP/DP3/AOFlagger/src/msio/baselinereader.cpp b/CEP/DP3/AOFlagger/src/msio/baselinereader.cpp
index c0e0b8468927353c8bd49ff4faca1d0b4af05090..61ad6d0cc6da15674b3785b85d67531312c995b4 100644
--- a/CEP/DP3/AOFlagger/src/msio/baselinereader.cpp
+++ b/CEP/DP3/AOFlagger/src/msio/baselinereader.cpp
@@ -65,7 +65,8 @@ void BaselineReader::initObservationTimes()
 		for(std::set<double>::const_iterator i=times.begin();i!=times.end();++i)
 		{
 			_observationTimes.insert(std::pair<double,size_t>(*i, index));
-			++index; 
+			_observationTimesVector.push_back(*i);
+			++index;
 		}
 	}
 }
diff --git a/CEP/DP3/AOFlagger/src/msio/directbaselinereader.cpp b/CEP/DP3/AOFlagger/src/msio/directbaselinereader.cpp
index 8aa8c1bfaf6104adaa3381c30eaa7c61ff1d103f..49dcea2e40d6c8a8fe8eddb693c8ba1846868655 100644
--- a/CEP/DP3/AOFlagger/src/msio/directbaselinereader.cpp
+++ b/CEP/DP3/AOFlagger/src/msio/directbaselinereader.cpp
@@ -117,7 +117,7 @@ void DirectBaselineReader::PerformReadRequests()
 		addRequestRows(_readRequests[i], i, rows);
 	std::sort(rows.begin(), rows.end());
 	
-	size_t timeCount = ObservationTimes().size();
+	size_t timeCount = AllObservationTimes().size();
 
 	AOLogger::Debug << "Reading " << _readRequests.size() << " requests with " << rows.size() << " rows total, flags=" << ReadFlags() << ", " << PolarizationCount() << " polarizations.\n";
 	
@@ -180,7 +180,7 @@ void DirectBaselineReader::PerformReadRequests()
 		
 		double time = timeColumn(rowIndex);
 		size_t
-			timeIndex = ObservationTimes().find(time)->second,
+			timeIndex = AllObservationTimes().find(time)->second,
 			startIndex = _readRequests[requestIndex].startIndex,
 			endIndex = _readRequests[requestIndex].endIndex;
 		bool timeIsSelected = timeIndex>=startIndex && timeIndex<endIndex;
@@ -222,7 +222,7 @@ std::vector<UVW> DirectBaselineReader::ReadUVW(unsigned antenna1, unsigned anten
 	initialize();
 	initBaselineCache();
 
-	const std::map<double, size_t> &observationTimes = ObservationTimes();
+	const std::map<double, size_t> &allObservationTimes = AllObservationTimes();
 
 	// Each element contains (row number, corresponding request index)
 	std::vector<std::pair<size_t, size_t> > rows;
@@ -231,11 +231,11 @@ std::vector<UVW> DirectBaselineReader::ReadUVW(unsigned antenna1, unsigned anten
 	request.antenna2 = antenna2;
 	request.spectralWindow = spectralWindow;
 	request.startIndex = 0;
-	request.endIndex = observationTimes.size();
+	request.endIndex = allObservationTimes.size();
 	addRequestRows(request, 0, rows);
 	std::sort(rows.begin(), rows.end());
 	
-	size_t width = observationTimes.size();
+	size_t width = allObservationTimes.size();
 
 	casa::Table &table = *Table();
 	casa::ROScalarColumn<double> timeColumn(table, "TIME");
@@ -249,7 +249,7 @@ std::vector<UVW> DirectBaselineReader::ReadUVW(unsigned antenna1, unsigned anten
 		
 		double time = timeColumn(rowIndex);
 		size_t
-			timeIndex = observationTimes.find(time)->second;
+			timeIndex = allObservationTimes.find(time)->second;
 
 		casa::Array<double> arr = uvwColumn(rowIndex);
 		casa::Array<double>::const_iterator i = arr.begin();
@@ -302,7 +302,7 @@ void DirectBaselineReader::PerformFlagWriteRequests()
 		size_t rowIndex = i->first;
 		WriteRequest &request = _writeRequests[i->second];
 		double time = timeColumn(rowIndex);
-		size_t timeIndex = ObservationTimes().find(time)->second;
+		size_t timeIndex = AllObservationTimes().find(time)->second;
 		if(timeIndex >= request.startIndex + request.leftBorder && timeIndex < request.endIndex - request.rightBorder)
 		{
 			casa::Array<bool> flag = flagColumn(rowIndex);
diff --git a/CEP/DP3/AOFlagger/src/msio/indirectbaselinereader.cpp b/CEP/DP3/AOFlagger/src/msio/indirectbaselinereader.cpp
index 6e33b1d3c2317f59e7e2a5a98a5778cae94315c2..ae06ef8f0fa1866bfd377cf6c6f6de39cf3a400c 100644
--- a/CEP/DP3/AOFlagger/src/msio/indirectbaselinereader.cpp
+++ b/CEP/DP3/AOFlagger/src/msio/indirectbaselinereader.cpp
@@ -64,7 +64,7 @@ void IndirectBaselineReader::PerformReadRequests()
 	{
 		const ReadRequest request = _readRequests[i];
 		_results.push_back(Result());
-		const size_t width = ObservationTimes().size();
+		const size_t width = AllObservationTimes().size();
 		for(size_t p=0;p<PolarizationCount();++p)
 		{
 			if(ReadData()) {
@@ -264,7 +264,7 @@ void IndirectBaselineReader::reorderMS()
 		double time = timeColumn(rowIndex);
 		if(time != prevTime)
 		{
-			timeIndex = ObservationTimes().find(time)->second;
+			timeIndex = AllObservationTimes().find(time)->second;
 			if(timeIndex != prevTimeIndex+1)
 			{
 				std::stringstream s;
@@ -525,7 +525,7 @@ void IndirectBaselineReader::updateOriginalMS()
 		if(time != prevTime)
 		{
 			// This row has a different time value, so search it up in the index table and do sanity check
-			timeIndex = ObservationTimes().find(time)->second;
+			timeIndex = AllObservationTimes().find(time)->second;
 			if(timeIndex != prevTimeIndex+1)
 			{
 				std::stringstream s;
diff --git a/CEP/DP3/AOFlagger/src/msio/measurementset.cpp b/CEP/DP3/AOFlagger/src/msio/measurementset.cpp
index 2e02809d5276838f5842852be526b69018240058..747867dbeb1f919b97a99f87201060016174f13c 100644
--- a/CEP/DP3/AOFlagger/src/msio/measurementset.cpp
+++ b/CEP/DP3/AOFlagger/src/msio/measurementset.cpp
@@ -323,7 +323,12 @@ void MeasurementSet::InitCacheData()
 
 size_t MeasurementSet::GetPolarizationCount()
 {
-	casa::MeasurementSet ms(Location());
+	return GetPolarizationCount(Location());
+}
+
+size_t MeasurementSet::GetPolarizationCount(const std::string &filename)
+{
+	casa::MeasurementSet ms(filename);
 	casa::Table polTable = ms.polarization();
 	casa::ROArrayColumn<int> corTypeColumn(polTable, "CORR_TYPE"); 
 	casa::Array<int> corType = corTypeColumn(0);
diff --git a/CEP/DP3/AOFlagger/src/quality/rayleighfitter.cpp b/CEP/DP3/AOFlagger/src/quality/rayleighfitter.cpp
index 33b43fac393f9d5ee37c7f6030f79853e5a7b0d1..dad9ff2cc1fb19c7e8853b3de4205d7c8fb73a1a 100644
--- a/CEP/DP3/AOFlagger/src/quality/rayleighfitter.cpp
+++ b/CEP/DP3/AOFlagger/src/quality/rayleighfitter.cpp
@@ -17,11 +17,11 @@ static int fit_f(const gsl_vector *xvec, void *data, gsl_vector *f)
 	
 	size_t t = 0;
 	RayleighFitter &fitter = *(RayleighFitter *) data;
-	LogHistogram &hist = *fitter._hist;
+	const LogHistogram &hist = *fitter._hist;
 	const double minVal = fitter._minVal;
 	const double maxVal = fitter._maxVal;
   
-	for (LogHistogram::iterator i=hist.begin(); i!=hist.end(); ++i)
+	for (LogHistogram::const_iterator i=hist.begin(); i!=hist.end(); ++i)
 	{
 		const double x = i.value();
 		if(x >= minVal && x < maxVal && std::isfinite(x))
@@ -48,12 +48,12 @@ int fit_df(const gsl_vector *xvec, void *data, gsl_matrix *J)
 	
 	size_t t = 0;
 	RayleighFitter &fitter = *(RayleighFitter *) data;
-	LogHistogram &hist = *fitter._hist;
+	const LogHistogram &hist = *fitter._hist;
 	const double minVal = fitter._minVal;
 	const double maxVal = fitter._maxVal;
 	const double sigma2P2 = 2.0*sigma*sigma;
   const double sigmaP3 = sigma*sigma*sigma;
-	for (LogHistogram::iterator i=hist.begin(); i!=hist.end(); ++i)
+	for (LogHistogram::const_iterator i=hist.begin(); i!=hist.end(); ++i)
 	{
 		const double x = i.value();
 		if(x >= minVal && x < maxVal && std::isfinite(x))
@@ -92,7 +92,7 @@ void print_state(size_t iter, gsl_multifit_fdfsolver *s)
 	std::cout << "iteration " << iter << ", sigma=" << sigma << ", N=" << N << "\n";
 }
 
-void RayleighFitter::Fit(double minVal, double maxVal, LogHistogram &hist, double &sigma, double &n)
+void RayleighFitter::Fit(double minVal, double maxVal, const LogHistogram &hist, double &sigma, double &n)
 {
 	unsigned int iter = 0;
 	const size_t nVars = 2;
@@ -154,7 +154,7 @@ void RayleighFitter::Fit(double minVal, double maxVal, LogHistogram &hist, doubl
 
 #else // No gsl...
 
-void RayleighFitter::Fit(double minVal, double maxVal, LogHistogram &hist, double &sigma, double &n)
+void RayleighFitter::Fit(double minVal, double maxVal, const LogHistogram &hist, double &sigma, double &n)
 {
 	sigma = 1.0;
 	n = 1.0;
@@ -162,18 +162,9 @@ void RayleighFitter::Fit(double minVal, double maxVal, LogHistogram &hist, doubl
 
 #endif
 
-double RayleighFitter::SigmaEstimate(LogHistogram &hist)
+double RayleighFitter::SigmaEstimate(const LogHistogram &hist)
 {
-	double maxCount = 0.0, maxPosition = 0.0;
-	for (LogHistogram::iterator i=hist.begin(); i!=hist.end(); ++i)
-	{
-		if(i.normalizedCount() > maxCount && i.value() > 0 && std::isfinite(i.value()))
-		{
-			maxCount = i.normalizedCount();
-			maxPosition = i.value();
-		}
-	}
-	return maxPosition;
+	return hist.AmplitudeWithMaxNormalizedCount();
 }
 
 void RayleighFitter::FindFitRangeUnderRFIContamination(double minPositiveAmplitude, double sigmaEstimate, double &minValue, double &maxValue)
@@ -183,11 +174,11 @@ void RayleighFitter::FindFitRangeUnderRFIContamination(double minPositiveAmplitu
 	std::cout << "Found range " << minValue << " -- " << maxValue << "\n";
 }
 
-double RayleighFitter::NEstimate(LogHistogram &hist, double rangeStart, double rangeEnd)
+double RayleighFitter::NEstimate(const LogHistogram &hist, double rangeStart, double rangeEnd)
 {
 	double rangeSum = 0.0;
 	size_t count = 0;
-	for (LogHistogram::iterator i=hist.begin(); i!=hist.end(); ++i)
+	for (LogHistogram::const_iterator i=hist.begin(); i!=hist.end(); ++i)
 	{
 		if(i.value() > rangeStart && i.value() < rangeEnd && std::isfinite(i.value()))
 		{
diff --git a/CEP/DP3/AOFlagger/src/remote/client.cpp b/CEP/DP3/AOFlagger/src/remote/client.cpp
index b5122e91878a4269bad210c0e6049e4040d3fa38..261b79c49b48308133badbe2a7797df2a0ae5259 100644
--- a/CEP/DP3/AOFlagger/src/remote/client.cpp
+++ b/CEP/DP3/AOFlagger/src/remote/client.cpp
@@ -25,6 +25,8 @@
 #include <boost/asio/read.hpp>
 #include <boost/asio/write.hpp>
 
+#include <AOFlagger/quality/histogramcollection.h>
+#include <AOFlagger/quality/histogramtablesformatter.h>
 #include <AOFlagger/quality/qualitytablesformatter.h>
 #include <AOFlagger/quality/statisticscollection.h>
 
@@ -145,12 +147,22 @@ void Client::handleReadQualityTables(unsigned dataSize)
 			// TODO: maybe we want to configure the following parameter at one point
 			collection.LowerTimeResolution(1000);
 			
+			HistogramTablesFormatter histogramFormatter(options.msFilename);
+			HistogramCollection histogramCollection(formatter.GetPolarizationCount());
+			const bool histogramsExist = histogramFormatter.HistogramsExist();
+			if(histogramsExist)
+			{
+				histogramCollection.Load(histogramFormatter);
+			}
+			
 			GenericReadResponseHeader header;
 			header.blockIdentifier = GenericReadResponseHeaderId;
 			header.blockSize = sizeof(header);
 			header.errorCode = NoError;
 			std::ostringstream s;
 			collection.Serialize(s);
+			if(histogramsExist)
+				histogramCollection.Serialize(s);
 			const std::string str = s.str();
 			header.dataSize = str.size();
 			
diff --git a/CEP/DP3/AOFlagger/src/remote/processcommander.cpp b/CEP/DP3/AOFlagger/src/remote/processcommander.cpp
index ac34ad7b0c08e30ed56e1160670ebbc4abd178db..4db99d5620175fb7d6465b759a05d49a2080d518 100644
--- a/CEP/DP3/AOFlagger/src/remote/processcommander.cpp
+++ b/CEP/DP3/AOFlagger/src/remote/processcommander.cpp
@@ -25,6 +25,7 @@
 #include <AOFlagger/remote/serverconnection.h>
 
 #include <AOFlagger/quality/statisticscollection.h>
+#include <AOFlagger/quality/histogramcollection.h>
 
 namespace aoRemote {
 
@@ -94,8 +95,9 @@ void ProcessCommander::continueReadQualityTablesTask(ServerConnectionPtr serverC
 		{
 			const std::string msFilename = items.front().LocalPath();
 			items.pop_front();
-			StatisticsCollection *collection = new StatisticsCollection();
-			serverConnection->ReadQualityTables(msFilename, *collection);
+			StatisticsCollection *statisticsCollection = new StatisticsCollection();
+			HistogramCollection *histogramCollection = new HistogramCollection();
+			serverConnection->ReadQualityTables(msFilename, *statisticsCollection, *histogramCollection);
 		}
 	}
 }
@@ -177,19 +179,28 @@ void ProcessCommander::onConnectionAwaitingCommand(ServerConnectionPtr serverCon
 	}
 }
 
-void ProcessCommander::onConnectionFinishReadQualityTables(ServerConnectionPtr serverConnection, StatisticsCollection &collection)
+void ProcessCommander::onConnectionFinishReadQualityTables(ServerConnectionPtr serverConnection, StatisticsCollection &statisticsCollection, HistogramCollection &histogramCollection)
 {
 	boost::mutex::scoped_lock lock(_mutex);
-	if(collection.PolarizationCount() == 0)
+	if(statisticsCollection.PolarizationCount() == 0)
 		throw std::runtime_error("Client sent StatisticsCollection with 0 polarizations.");
 	
 	// If the collection is still empty, we need to set its polarization count
-	if(_collection->PolarizationCount() == 0)
+	if(_statisticsCollection->PolarizationCount() == 0)
+		_statisticsCollection->SetPolarizationCount(statisticsCollection.PolarizationCount());
+	
+	_statisticsCollection->Add(statisticsCollection);
+	
+	if(!histogramCollection.Empty())
 	{
-		_collection->SetPolarizationCount(collection.PolarizationCount());
+		if(_histogramCollection->PolarizationCount() == 0)
+			_histogramCollection->SetPolarizationCount(histogramCollection.PolarizationCount());
+		
+		_histogramCollection->Add(histogramCollection);
 	}
-	_collection->Add(collection);
-	delete &collection;
+	
+	delete &statisticsCollection;
+	delete &histogramCollection;
 }
 
 void ProcessCommander::onConnectionFinishReadAntennaTables(ServerConnectionPtr serverConnection, std::vector<AntennaInfo> &antennas)
diff --git a/CEP/DP3/AOFlagger/src/remote/serverconnection.cpp b/CEP/DP3/AOFlagger/src/remote/serverconnection.cpp
index 832f7ca003bfce69dbc35508143d32fa863f1fdb..542ee991039442c1d01fa07ecaecc9f77784619d 100644
--- a/CEP/DP3/AOFlagger/src/remote/serverconnection.cpp
+++ b/CEP/DP3/AOFlagger/src/remote/serverconnection.cpp
@@ -31,6 +31,7 @@
 #include <AOFlagger/util/autoarray.h>
 
 #include <AOFlagger/quality/statisticscollection.h>
+#include <AOFlagger/quality/histogramcollection.h>
 
 namespace aoRemote
 {
@@ -91,9 +92,10 @@ void ServerConnection::StopClient()
 	boost::asio::write(_socket, boost::asio::buffer(&requestBlock, sizeof(requestBlock)));
 }
 
-void ServerConnection::ReadQualityTables(const std::string &msFilename, StatisticsCollection &collection)
+void ServerConnection::ReadQualityTables(const std::string &msFilename, StatisticsCollection &collection, HistogramCollection &histogramCollection)
 {
 	_collection = &collection;
+	_histogramCollection = &histogramCollection;
 	
 	std::stringstream reqBuffer;
 	
@@ -189,8 +191,14 @@ void ServerConnection::onReceiveQualityTablesResponseData(size_t dataSize)
 	
 	std::cout << "Received quality table of size " << dataSize << "." << std::endl;
 	_collection->Unserialize(stream);
+	if(stream.tellg() != (std::streampos) dataSize)
+	{
+		size_t histogramTablesSize = dataSize - stream.tellg();
+		std::cout << "Processing histogram tables of size " << histogramTablesSize << "." << std::endl;
+		_histogramCollection->Unserialize(stream);
+	}
 
-	_onFinishReadQualityTables(shared_from_this(), *_collection);
+	_onFinishReadQualityTables(shared_from_this(), *_collection, *_histogramCollection);
 	_onAwaitingCommand(shared_from_this());
 }
 
diff --git a/CEP/DP3/AOFlagger/src/strategy/actions/foreachbaselineaction.cpp b/CEP/DP3/AOFlagger/src/strategy/actions/foreachbaselineaction.cpp
index a25174e5ee7f4b506129ba47b07c1476cbce4d43..02e61b3dbd6570df7c5e8efb53ff7876f0c2d5da 100644
--- a/CEP/DP3/AOFlagger/src/strategy/actions/foreachbaselineaction.cpp
+++ b/CEP/DP3/AOFlagger/src/strategy/actions/foreachbaselineaction.cpp
@@ -53,7 +53,9 @@ namespace rfiStrategy {
 			if(msImageSet != 0)
 			{
 				// Check memory usage
-				size_t timeStepCount = msImageSet->GetObservationTimesSet().size();
+				ImageSetIndex *tempIndex = msImageSet->StartIndex();
+				size_t timeStepCount = msImageSet->ObservationTimesVector(*tempIndex).size();
+				delete tempIndex;
 				size_t channelCount = msImageSet->GetBandInfo(0).channelCount;
 				size_t estMemorySizePerThread = 8/*bp complex*/ * 4 /*polarizations*/ * timeStepCount * channelCount * 3 /* approx copies of the data that will be made in memory*/;
 				AOLogger::Debug << "Estimate of memory each thread will use: " << estMemorySizePerThread/(1024*1024) << " MB.\n";
diff --git a/CEP/DP3/AOFlagger/src/strategy/algorithms/mitigationtester.cpp b/CEP/DP3/AOFlagger/src/strategy/algorithms/mitigationtester.cpp
index 04604bbe1a5f412ac29f0832cc7e8ec0fe37b924..9aa8604c34ccf8898bb08c8f12638602ac6edcd2 100644
--- a/CEP/DP3/AOFlagger/src/strategy/algorithms/mitigationtester.cpp
+++ b/CEP/DP3/AOFlagger/src/strategy/algorithms/mitigationtester.cpp
@@ -474,6 +474,18 @@ Image2DPtr MitigationTester::CreateTestSet(int number, Mask2DPtr rfi, unsigned w
 			image = Image2DPtr(CreateNoise(width, height, gaussianNoise));
 			AddBurstBroadbandToTestSet(image, rfi);
 		} break;
+		case 30: { // noise + RFI ^-2 distribution
+			image = sampleRFIDistribution(width, height, 1.0);
+			rfi->SetAll<true>();
+		} break;
+		case 31: { // noise + RFI ^-2 distribution
+			image = sampleRFIDistribution(width, height, 0.1);
+			rfi->SetAll<true>();
+		} break;
+		case 32: { // noise + RFI ^-2 distribution
+			image = sampleRFIDistribution(width, height, 0.01);
+			rfi->SetAll<true>();
+		} break;
 	}
 	return image;
 }
@@ -617,3 +629,16 @@ void MitigationTester::SubtractBackground(Image2DPtr image)
 		}
 	}
 }
+
+Image2DPtr MitigationTester::sampleRFIDistribution(unsigned width, unsigned height, double ig_over_rsq)
+{
+	Image2DPtr image = Image2D::CreateUnsetImagePtr(width, height);
+	const double sigma = 1.0;
+
+	for(size_t f=0; f<height;++f) {
+		for(size_t t=0; t<width;++t) {
+			image->SetValue(t, f, Rand(Gaussian)*sigma + ig_over_rsq / RNG::Uniform());
+		}
+	}
+	return image;
+}
diff --git a/CEP/DP3/AOFlagger/src/strategy/imagesets/msimageset.cpp b/CEP/DP3/AOFlagger/src/strategy/imagesets/msimageset.cpp
index 6793475cb9fad0a91608a29cc1b811e666a3a710..bd540d7440f0e26e8e5c4df7e5488ae955b474ae 100644
--- a/CEP/DP3/AOFlagger/src/strategy/imagesets/msimageset.cpp
+++ b/CEP/DP3/AOFlagger/src/strategy/imagesets/msimageset.cpp
@@ -173,9 +173,7 @@ namespace rfiStrategy {
 		metaData->SetAntenna2(_set.GetAntennaInfo(GetAntenna2(msIndex)));
 		metaData->SetBand(_set.GetBandInfo(msIndex._band));
 		metaData->SetField(_set.GetFieldInfo(msIndex._field));
-		std::vector<double> *times = _set.CreateObservationTimesVector();
-		metaData->SetObservationTimes(*times);
-		delete times;
+		metaData->SetObservationTimes(ObservationTimesVector(msIndex));
 		if(_reader != 0)
 		{
 			metaData->SetUVW(uvw);
diff --git a/CEP/DP3/DPPP/include/DPPP/BBSExpr.h b/CEP/DP3/DPPP/include/DPPP/BBSExpr.h
index 2c43d8deb7d9091c1d53ae4329c3dac6641b3063..6c95f9365b5eb0ad0874ed996ea57c40864ed6ed 100644
--- a/CEP/DP3/DPPP/include/DPPP/BBSExpr.h
+++ b/CEP/DP3/DPPP/include/DPPP/BBSExpr.h
@@ -34,7 +34,9 @@
 #include <BBSKernel/CorrelationMask.h>
 #include <BBSKernel/ParmManager.h>
 #include <BBSKernel/VisBuffer.h>
+#include <BBSKernel/Estimate.h>
 #include <ParmDB/SourceDB.h>
+#include <ParmDB/Grid.h>
 
 #include <Common/lofar_vector.h>
 #include <Common/lofar_string.h>
@@ -48,25 +50,32 @@ namespace LOFAR {
     class BBSExpr
     {
     public:
-      // Define the shared pointer for this type.
-      typedef shared_ptr<BBSExpr> ShPtr;
-
-      // Construct the expression for the given source.
-      BBSExpr(const DPInput&, const DPInfo&, const string& sourceName);
+      // Construct the expression for the given sky and instrument
+      // parameter databases.
+      BBSExpr (const DPInput& input, const string& skyName,
+               const string& instrumentName);
 
       ~BBSExpr();
 
-      // Get the resulting expression.
-      BBS::MeasurementExprLOFAR::Ptr getModel()
-        { return itsModel; }
+      // Create a model expression for the given source.
+      void addModel (const DPInput& input, const DPInfo&,
+                     const string& sourceName, double refFreq);
+
+      // Estimate the model parameters.
+      void estimate (vector<vector<DPBuffer> >& buffers,
+                     const BBS::Grid& visGrid, const BBS::Grid& solveGrid,
+                     const vector<casa::Array<casa::DComplex> >& factors);
+
+      // Subtract the sources.
+      void subtract (vector<DPBuffer>& buffer, const BBS::Grid& visGrid,
+                     const vector<casa::Array<casa::DComplex> >& factors,
+                     uint target, uint nsources);
 
-      // Get the frequency axis.
-      const BBS::Axis::ShPtr& getFreqAxis() const
-        { return itsFreqAxis; }
+      // Clear the solvables in the model expressions.
+      void clearSolvables();
 
-      // Get the baseline mask.
-      const BBS::BaselineMask& getBaselineMask() const
-        { return itsBaselineMask; }
+      // Set the solvables in the model expressions to the gains.
+      void setSolvables();
 
     private:
       // For now, forbid copy construction and assignment.
@@ -74,17 +83,18 @@ namespace LOFAR {
       BBSExpr& operator= (const BBSExpr& other);
 
       //# Data members
-      boost::shared_ptr<BBS::SourceDB> itsSourceDB;
-      BBS::MeasurementExprLOFAR::Ptr   itsModel;
-      BBS::ParmGroup                   itsParms;
-      BBS::BaselineMask                itsBaselineMask;
-      casa::MDirection                 itsPhaseReference;
-      double                           itsRefFreq;
-      double                           itsTimeInterval;
-      BBS::Instrument::Ptr             itsInstrument;
-      BBS::BaselineSeq                 itsBaselines;
-      BBS::CorrelationSeq              itsCorrelations;
-      BBS::Axis::ShPtr                 itsFreqAxis;
+      boost::shared_ptr<BBS::SourceDB>  itsSourceDB;
+      vector<BBS::MeasurementExpr::Ptr> itsModels;
+      vector<string>                    itsSources;
+      BBS::BaselineMask                 itsBaselineMask;
+      BBS::Instrument::ConstPtr         itsInstrument;
+      BBS::BaselineSeq                  itsBaselines;
+      BBS::CorrelationSeq               itsCorrelations;
+      BBS::CorrelationMask              itsCorrelationMask;
+      BBS::ModelConfig                  itsConfig;
+      vector<BBS::ParmGroup>            itsModelParms;
+      BBS::ParmGroup                    itsParms;
+      BBS::EstimateOptions              itsOptions;
     };
 
 // @}
diff --git a/CEP/DP3/DPPP/include/DPPP/DPStep.h b/CEP/DP3/DPPP/include/DPPP/DPStep.h
index 51fd0195a28adbad1eaa69be7b92805c0934f907..3896e335d4333716f41c20e64b5169ca1a251e1d 100644
--- a/CEP/DP3/DPPP/include/DPPP/DPStep.h
+++ b/CEP/DP3/DPPP/include/DPPP/DPStep.h
@@ -162,6 +162,8 @@ namespace LOFAR {
       // Get the result.
       const DPBuffer& get() const
         { return itsBuffer; }
+      DPBuffer& get()
+        { return itsBuffer; }
 
       // Clear the buffer.
       void clear()
@@ -201,6 +203,8 @@ namespace LOFAR {
       // Get the result.
       const vector<DPBuffer>& get() const
         { return itsBuffers; }
+      vector<DPBuffer>& get()
+        { return itsBuffers; }
 
       // Clear the buffers.
       void clear()
diff --git a/CEP/DP3/DPPP/include/DPPP/Demixer.h b/CEP/DP3/DPPP/include/DPPP/Demixer.h
index 142d04220a69332d292bbebfce52bfaa90b30d0a..bad434ac28166a4f52c5dadfd9a60850ae3312e4 100644
--- a/CEP/DP3/DPPP/include/DPPP/Demixer.h
+++ b/CEP/DP3/DPPP/include/DPPP/Demixer.h
@@ -31,18 +31,11 @@
 #include <DPPP/DPBuffer.h>
 #include <DPPP/PhaseShift.h>
 #include <DPPP/BBSExpr.h>
+#include <ParmDB/Axis.h>
 
 #include <casa/Arrays/Cube.h>
 #include <measures/Measures/MDirection.h>
 
-#include <BBSKernel/MeasurementExprLOFAR.h>
-#include <BBSKernel/BaselineMask.h>
-#include <BBSKernel/CorrelationMask.h>
-#include <BBSKernel/ParmManager.h>
-#include <BBSKernel/VisBuffer.h>
-#include <ParmDB/SourceDB.h>
-#include <DPPP/EstimateNDPPP.h>
-
 namespace LOFAR {
 
   namespace DPPP {
@@ -92,65 +85,84 @@ namespace LOFAR {
       // Solve gains and subtract sources.
       void demix();
 
-//      // Do the subtraction.
-//      void subtract();
-
       // Add the decorrelation factor contribution for each time slot.
-      void addFactors (const DPBuffer& newBuf);
+      void addFactors (const DPBuffer& newBuf,
+                       casa::Array<casa::DComplex>& factorBuf);
 
       // Calculate the decorrelation factors by averaging them.
-      void averageFactors();
+      // Apply the P matrix to deproject the sources without a model.
+      void makeFactors (const casa::Array<casa::DComplex>& bufIn,
+                        casa::Array<casa::DComplex>& bufOut,
+                        const casa::Cube<float>& weightSums,
+                        uint nchanOut);
+
+      // Deproject the sources without a model.
+      void deproject (casa::Array<casa::DComplex>& factors,
+                      vector<MultiResultStep*> avgResults,
+                      uint resultIndex);
+
+      // Calculate the P matrix telling how to deal with sources that will
+      // not be predicted.
+      // Those sources are the last columns in the demixing matrix.
+      vector<casa::Array<casa::DComplex> > getP
+      (const vector<casa::Array<casa::DComplex> >& factors, uint nsources);
+
+      // Make a BBS frequency axis for the given channel average factor.
+      BBS::Axis::ShPtr makeFreqAxis (uint nchanAvg);
 
       //# Data members.
       DPInput*                 itsInput;
       string                   itsName;
+      string                   itsSkyName;
+      string                   itsInstrumentName;
       vector<PhaseShift*>      itsPhaseShifts;
       vector<DPStep::ShPtr>    itsFirstSteps;   //# phaseshift/average steps
-      vector<MultiResultStep*> itsAvgResults;
-      vector<BBSExpr::ShPtr>   itsBBSExpr;
-      vector<BBS::MeasurementExpr::Ptr> itsModels;
+      vector<MultiResultStep*> itsAvgResults;   //# result of phaseshift/average
+      DPStep::ShPtr            itsAvgSubtr;     //# average step for subtract
+      MultiResultStep*         itsAvgResultSubtr; //# result of subtract avg
+      BBSExpr                  itsBBSExpr;
       string                   itsTarget;
-      vector<string>           itsSources;
+      vector<string>           itsSubtrSources;
+      vector<string>           itsModelSources;
       vector<string>           itsExtraSources;
       vector<string>           itsAllSources;
-//      vector<DPBuffer>         itsBuf;
+      BBS::Axis::ShPtr         itsFreqAxisDemix;
+      BBS::Axis::ShPtr         itsFreqAxisSubtr;
       double                   itsTimeStart;
       double                   itsTimeInterval;
       vector<double>           itsTimeCenters;
       vector<double>           itsTimeWidths;
       bool                     itsJointSolve;
+      bool                     itsCalcSubtr;    //# subtract averaging differs
       uint                     itsNrDir;
+      uint                     itsNrModel;
       uint                     itsNrBl;
       uint                     itsNrCorr;
-      uint                     itsNrChanIn;
-      uint                     itsNrChanOut;
+      uint                     itsNChanIn;
+      uint                     itsNTimeIn;
+      uint                     itsNChanOutSubtr;
+      uint                     itsNChanAvgSubtr;
+      uint                     itsNTimeAvgSubtr;
+      uint                     itsNTimeChunkSubtr;
+      uint                     itsNTimeOutSubtr;
+      uint                     itsNChanOut;
       uint                     itsNChanAvg;
       uint                     itsNTimeAvg;
-      uint                     itsResChanAvg;
-      uint                     itsResTimeAvg;
       uint                     itsNTimeChunk;
-      uint                     itsNTimeIn;
       uint                     itsNTimeOut;
+      double                   itsStartTimeChunk;
+      double                   itsTimeIntervalSubtr;
       double                   itsTimeIntervalAvg;
-      double                   itsTimeIntervalRes;
       casa::Array<casa::DComplex> itsFactorBuf; //# ncorr,nchan,nbl,ndir*ndir
       vector<casa::Array<casa::DComplex> > itsFactors; //# demix factors/time
       //# each Array is basically cube(ncorr,nchan,nbl) of matrix(ndir,ndir)
+      casa::Array<casa::DComplex> itsFactorBufSubtr; //# factors for subtract
+      vector<casa::Array<casa::DComplex> > itsFactorsSubtr;
       NSTimer                  itsTimer;
       NSTimer                  itsTimerPhaseShift;
       NSTimer                  itsTimerDemix;
-      NSTimer                  itsTimerBBS;
+      NSTimer                  itsTimerSolve;
       NSTimer                  itsTimerSubtract;
-
-      boost::shared_ptr<BBS::SourceDB> itsSourceDB;
-      vector<BBS::ParmGroup>           itsModelParms;
-      BBS::ParmGroup                   itsParms;
-      BBS::BaselineSeq                 itsBaselines;
-      BBS::BaselineMask                itsBaselineMask;
-      BBS::CorrelationSeq              itsCorrelations;
-      BBS::CorrelationMask             itsCorrelationMask;
-      BBS::Axis::ShPtr                 itsFreqAxisAvg;
-      BBS::EstimateOptions             itsOptions;
     };
 
   } //# end namespace
diff --git a/CEP/DP3/DPPP/include/DPPP/EstimateNDPPP.h b/CEP/DP3/DPPP/include/DPPP/EstimateNDPPP.h
index 4e3ab4035e71b838857f266ec9db59e40728b345..6b81e86c52c5c0aebc28ab64656bd858a94f8bcb 100644
--- a/CEP/DP3/DPPP/include/DPPP/EstimateNDPPP.h
+++ b/CEP/DP3/DPPP/include/DPPP/EstimateNDPPP.h
@@ -93,7 +93,7 @@ void subtract(vector<DPPP::DPBuffer> &buffer,
     const BBS::CorrelationMask &correlationMask,
     const BBS::Grid &visGrid,
     unsigned int target,
-    const vector<unsigned int> &directions);
+    unsigned int nsources);
 
 // @}
 
diff --git a/CEP/DP3/DPPP/include/DPPP/MSReader.h b/CEP/DP3/DPPP/include/DPPP/MSReader.h
index b6ec49245e18b2b86775b7023ef0fe86eb2e06e8..e3e0b4213b08fde49676b8913f9c6507fad89a0f 100644
--- a/CEP/DP3/DPPP/include/DPPP/MSReader.h
+++ b/CEP/DP3/DPPP/include/DPPP/MSReader.h
@@ -267,6 +267,7 @@ namespace LOFAR {
       bool                itsReadVisData;   //# read visibility data?
       bool                itsNeedSort;      //# sort needed on time,baseline?
       bool                itsAutoWeight;    //# calculate weights from autocorr?
+      bool                itsAutoWeightForce; //# always calculate weights?
       bool                itsHasWeightSpectrum;
       bool                itsUseFlags;
       bool                itsUseAllChan;    //# all channels (i.e. no slicer)?
diff --git a/CEP/DP3/DPPP/src/BBSExpr.cc b/CEP/DP3/DPPP/src/BBSExpr.cc
index 65a22be1eb985d8b8f29d6923450ade559d644ec..f69200d39d2a676ed2e7d0cb06b7add6a2c753da 100644
--- a/CEP/DP3/DPPP/src/BBSExpr.cc
+++ b/CEP/DP3/DPPP/src/BBSExpr.cc
@@ -22,10 +22,14 @@
 
 #include <lofar_config.h>
 #include <DPPP/BBSExpr.h>
+#include <DPPP/EstimateNDPPP.h>
+
+#include <BBSKernel/MeasurementAIPS.h>
 
 #include <Common/LofarLogger.h>
 #include <Common/lofar_iostream.h>
 #include <Common/lofar_iomanip.h>
+#include <Common/StreamUtil.h>
 
 #include <measures/Measures/MCDirection.h>
 #include <measures/Measures/MCPosition.h>
@@ -37,62 +41,30 @@ using namespace LOFAR::BBS;
 namespace LOFAR {
   namespace DPPP {
 
-    BBSExpr::BBSExpr(const DPInput& input, const DPInfo& info,
-                     const string& sourceName)
-      : itsBaselineMask (True),
-        itsRefFreq      (0),
-        itsTimeInterval (input.timeInterval())
+    BBSExpr::BBSExpr (const DPInput& input, const string& skyName,
+                      const string& instrumentName)
+      : itsBaselineMask    (true),
+        itsCorrelationMask (true)
     {
       // Open ParmDB and SourceDB.
       try {
         itsSourceDB = boost::shared_ptr<SourceDB>
-          (new SourceDB(ParmDBMeta("casa", "sky")));
+          (new SourceDB(ParmDBMeta("casa", skyName)));
         ParmManager::instance().initCategory(SKY, itsSourceDB->getParmDB());
       } catch (Exception &e) {
         THROW(Exception, "Failed to open sky model parameter database: "
-              << "sky");
+              << skyName);
       }
       try {
         ParmManager::instance().initCategory(INSTRUMENT,
                                              ParmDB(ParmDBMeta("casa",
-                                                               "instrument")));
+                                                               instrumentName)));
       } catch (Exception &e) {
         THROW(Exception, "Failed to open instrument model parameter database: "
-              << "instrument");
+              << instrumentName);
       }
       // Create Instrument instance using information present in DPInput.
-      size_t nStations = input.antennaNames().size();
-      vector<Station::Ptr> stations;
-      stations.reserve(nStations);
-      for (size_t i = 0; i < nStations; ++i) {
-        // Get station name and ITRF position.
-        casa::MPosition position = MPosition::Convert(input.antennaPos()[i],
-                                                      MPosition::ITRF)();
-        // Store station information.
-        stations.push_back(Station::Ptr(new Station(input.antennaNames()(i),
-                                                    position)));
-      }
-      MPosition position = MPosition::Convert(input.arrayPos(),
-                                              MPosition::ITRF)();
-      itsInstrument = Instrument::Ptr
-        (new Instrument("LOFAR", position, stations.begin(), stations.end()));
-      // Get directions and make sure they are in J2000.
-      // Note that the phase center has to be taken from the DPInfo object,
-      // because it can be used from a PhaseShift object.
-      /// What to do if phasecenter is moving (e.g. SUN)?
-      itsPhaseReference = MDirection::Convert(info.phaseCenter(),
-                                              MDirection::J2000)();
-      MDirection delayReference = MDirection::Convert(input.delayCenter(),
-                                                      MDirection::J2000)();
-      MDirection tileBeamDir    = MDirection::Convert(input.tileBeamDir(),
-                                                      MDirection::J2000)();
-      // Construct frequency axis (needs channel width information).
-      double chanWidth = input.chanWidths()[0];
-      ASSERT (allEQ (input.chanWidths(), chanWidth));
-      itsFreqAxis = Axis::ShPtr(new RegularAxis
-                                (input.chanFreqs()(0) - chanWidth*0.5,
-                                 chanWidth, input.chanFreqs().size()));
-      // Construct measurement expression.
+      itsInstrument = MeasurementAIPS(input.msName()).instrument();
       // Ignore auto-correlations.
       ASSERT(input.getAnt1().size() == input.getAnt2().size());
       for(size_t i=0; i<input.getAnt1().size(); ++i) {
@@ -103,35 +75,112 @@ namespace LOFAR {
           itsBaselineMask.clear (ant1, ant2);
         }
       }
+      // Use all correlations.
       ASSERT(input.ncorr() == 4);
       itsCorrelations.append(Correlation::XX);
       itsCorrelations.append(Correlation::XY);
       itsCorrelations.append(Correlation::YX);
       itsCorrelations.append(Correlation::YY);
-      ModelConfig config;
-      config.setDirectionalGain();
-      config.setCache();
-      config.setSources (vector<string>(1, sourceName));
-      /// config.setBeam();
-      /// beaminfo lezen/toevoegen aan itsInstrument
+      // Define the model configuration.
+      /// itsConfig.setDirectionalGain();
+      itsConfig.setGain();
+      BeamConfig beamConfig(BeamConfig::DEFAULT, false);
+      itsConfig.setBeamConfig(beamConfig);
+      itsConfig.setCache();
+    }
+
+    BBSExpr::~BBSExpr()
+    {}
+
+    void BBSExpr::addModel (const DPInput& input, const DPInfo& info,
+                            const string& sourceName, double refFreq)
+    {
       // Model of this source plus directional gain for this source. How?
-      BufferMap bufferMap;
+      // Get directions and make sure they are in J2000.
+      // Note that the phase center has to be taken from the DPInfo object,
+      // because it can be used from a PhaseShift object.
+      /// What to do if phasecenter is moving (e.g. SUN)?
+      MDirection phaseReference = MDirection::Convert(info.phaseCenter(),
+                                                      MDirection::J2000)();
+      MDirection delayReference = MDirection::Convert(input.delayCenter(),
+                                                      MDirection::J2000)();
+      MDirection tileBeamDir    = MDirection::Convert(input.tileBeamDir(),
+                                                      MDirection::J2000)();
+      vector<string> incl, excl;
+      incl.push_back ("Gain.*");
+      /// incl.push_back ("DirectionalGain.*");
       try {
-        itsModel = MeasurementExprLOFAR::Ptr(new MeasurementExprLOFAR
-                                             (*itsSourceDB, bufferMap,
-                                              config, itsInstrument,
-                                              itsBaselines, itsRefFreq,
-                                              itsPhaseReference,
-                                              delayReference,
-                                              tileBeamDir));
-      } catch (Exception &x) {
+        itsConfig.setSources (vector<string>(1, sourceName));
+        itsModels.push_back (MeasurementExpr::Ptr
+          (new MeasurementExprLOFAR (*itsSourceDB, BufferMap(),
+                                     itsConfig, itsInstrument,
+                                     itsBaselines, refFreq,
+                                     phaseReference,
+                                     delayReference,
+                                     tileBeamDir)));
+       } catch (Exception &x) {
         THROW(Exception, "Unable to construct the BBS model expression; " +
               string(x.what()));
       }
+      ParmGroup parms = ParmManager::instance().makeSubset
+        (incl, excl, itsModels.back()->parms());
+      itsModelParms.push_back(parms);
+      itsParms.insert(parms.begin(), parms.end());
+      itsSources.push_back (sourceName);
+      // Set solver options.
+      SolverOptions lsqOptions;
+      lsqOptions.maxIter = 40;
+      lsqOptions.epsValue = 1e-9;
+      lsqOptions.epsDerivative = 1e-9;
+      lsqOptions.colFactor = 1e-9;
+      lsqOptions.lmFactor = 1.0;
+      lsqOptions.balancedEq = false;
+      lsqOptions.useSVD = true;
+      itsOptions = EstimateOptions(EstimateOptions::COMPLEX,
+                                   EstimateOptions::L2, false, 1,
+                                   false, ~flag_t(0), flag_t(4),
+                                   lsqOptions);
     }
 
-    BBSExpr::~BBSExpr()
-    {}
+    void BBSExpr::estimate (vector<vector<DPBuffer> >& buffers,
+                            const Grid& visGrid, const Grid& solveGrid,
+                            const vector<Array<DComplex> >& factors)
+    {
+      // Set parameter domain.
+      ParmManager::instance().setDomain(solveGrid.getBoundingBox());
+      DPPP::estimate (buffers, itsModels, factors, itsBaselines,
+                      itsCorrelations, itsBaselineMask, itsCorrelationMask,
+                      visGrid, solveGrid, itsOptions);
+      // Flush solutions to disk.
+      ParmManager::instance().flush();
+    }
+
+    void BBSExpr::subtract (vector<DPBuffer>& buffer,
+                            const Grid& visGrid,
+                            const vector<Array<DComplex> >& factors,
+                            uint target,
+                            uint nsources)
+    {
+      clearSolvables();
+      LOG_DEBUG_STR("nsources: " << nsources << ",  target: " << target);
+      DPPP::subtract (buffer, itsModels, factors, itsBaselines,
+                      itsCorrelations, itsBaselineMask, itsCorrelationMask,
+                      visGrid, target, nsources);
+    }
+
+    void BBSExpr::clearSolvables()
+    {
+      for (uint i=0; i<itsModels.size(); ++i) {
+        itsModels[i]->clearSolvables();
+      }
+    }
+
+    void BBSExpr::setSolvables()
+    {
+      for (uint i=0; i<itsModels.size(); ++i) {
+        itsModels[i]->setSolvables (itsModelParms[i]);
+      }
+    }
 
   } //# namespace DPPP
 } //# namespace LOFAR
diff --git a/CEP/DP3/DPPP/src/CMakeLists.txt b/CEP/DP3/DPPP/src/CMakeLists.txt
index 415b17ba063882b292eed4394ddb4d25fc92ec4f..9ed56e637705dbf960b33939e4ec80fa0ea6b583 100644
--- a/CEP/DP3/DPPP/src/CMakeLists.txt
+++ b/CEP/DP3/DPPP/src/CMakeLists.txt
@@ -10,8 +10,9 @@ lofar_add_library(dppp
   UVWCalculator.cc
   MSReader.cc MultiMSReader.cc MSWriter.cc MSUpdater.cc Counter.cc
   Averager.cc MedFlagger.cc AORFlagger.cc PreFlagger.cc UVWFlagger.cc
-  PhaseShift.cc Demixer.cc EstimateNDPPP.cc
+  PhaseShift.cc Demixer.cc BBSExpr.cc EstimateNDPPP.cc
   FlaggerStatistics.cc
+# The following is old IDPPP stuff.
 #  MsInfo.cc
 #  MsFile.cc
 #  ComplexMedianFlagger.cc
diff --git a/CEP/DP3/DPPP/src/DPRun.cc b/CEP/DP3/DPPP/src/DPRun.cc
index 1366ee69b0a3ebef08970568f4890126e4b31f64..d9d3c05c6511157bf80750746abac4dad8a91c48 100644
--- a/CEP/DP3/DPPP/src/DPRun.cc
+++ b/CEP/DP3/DPPP/src/DPRun.cc
@@ -64,6 +64,14 @@ namespace LOFAR {
       DPInfo info;
       // Create the steps and fill the DPInfo object.
       DPStep::ShPtr firstStep = makeSteps (parset, info, msName);
+      // Show the steps.
+      DPStep::ShPtr step = firstStep;
+      while (step) {
+        ostringstream os;
+        step->show (os);
+        DPLOG_INFO (os.str(), true);
+        step = step->getNextStep();
+      }
       // Show unused parameters (might be misspelled).
       vector<string> unused = parset.unusedKeys();
       if (! unused.empty()) {
@@ -104,7 +112,6 @@ namespace LOFAR {
       firstStep->finish();
       // Give all steps the option to add something to the MS written.
       // Currently it is used by the AOFlagger to write its statistics.
-      DPStep::ShPtr step;
       if (! msName.empty()) {
         step = firstStep;
         while (step) {
diff --git a/CEP/DP3/DPPP/src/Demixer.cc b/CEP/DP3/DPPP/src/Demixer.cc
index 7978b8e9385b34b9bcd089246d41c50efd81238c..839ffc5ea1c65aca649eeadc304eb288d53b4f37 100644
--- a/CEP/DP3/DPPP/src/Demixer.cc
+++ b/CEP/DP3/DPPP/src/Demixer.cc
@@ -34,8 +34,11 @@
 #include <Common/OpenMP.h>
 #include <BBSKernel/MeasurementAIPS.h>
 
+#include <casa/Arrays/Vector.h>
+#include <casa/Arrays/Matrix.h>
+#include <casa/Arrays/ArrayMath.h>
+#include <casa/Arrays/MatrixMath.h>
 #include <scimath/Mathematics/MatrixMathLA.h>
-#include <casa/Arrays/MatrixIter.h>
 #include <measures/Measures/MeasConvert.h>
 #include <measures/Measures/MCDirection.h>
 #include <measures/Measures/MCPosition.h>
@@ -52,56 +55,64 @@ namespace LOFAR {
 
     Demixer::Demixer (DPInput* input,
                       const ParSet& parset, const string& prefix)
-      : itsInput        (input),
-        itsName         (prefix),
-        itsTarget       (parset.getString(prefix+"target")),
-        itsSources      (parset.getStringVector (prefix+"sources")),
-//        itsExtraSources (parset.getStringVector (prefix+"sources")),
-        itsJointSolve   (parset.getBool  (prefix+"jointsolve", true)),
-        itsNChanAvg     (parset.getUint  (prefix+"freqstep", 1)),
-        itsNTimeAvg     (parset.getUint  (prefix+"timestep", 1)),
-        itsResChanAvg   (parset.getUint  (prefix+"avgfreqstep", itsNChanAvg)),
-        itsResTimeAvg   (parset.getUint  (prefix+"avgtimestep", itsNTimeAvg)),
-        itsNTimeChunk   (parset.getUint  (prefix+"ntimechunk", 0)),
-        itsNTimeIn      (0),
-        itsNTimeOut     (0),
-
-        itsBaselineMask(True),
-        itsCorrelationMask(True)
+      : itsInput         (input),
+        itsName          (prefix),
+        itsSkyName       (parset.getString(prefix+"skymodel", "sky")),
+        itsInstrumentName(parset.getString(prefix+"instrumentmodel",
+                                           "instrument")),
+        itsBBSExpr       (*input, itsSkyName, itsInstrumentName),
+        itsTarget        (parset.getString(prefix+"target", "target")),
+        itsSubtrSources  (parset.getStringVector (prefix+"subtractsources")),
+        itsModelSources  (parset.getStringVector (prefix+"modelsources",
+                                                  vector<string>())),
+        itsExtraSources  (parset.getStringVector (prefix+"othersources",
+                                                  vector<string>())),
+        itsJointSolve    (parset.getBool  (prefix+"jointsolve", true)),
+        itsNTimeIn       (0),
+        itsNChanAvgSubtr (parset.getUint  (prefix+"freqstep", 1)),
+        itsNTimeAvgSubtr (parset.getUint  (prefix+"timestep", 1)),
+        itsNTimeOutSubtr (0),
+        itsNChanAvg      (parset.getUint  (prefix+"demixfreqstep",
+                                           itsNChanAvgSubtr)),
+        itsNTimeAvg      (parset.getUint  (prefix+"demixtimestep",
+                                           itsNTimeAvgSubtr)),
+        itsNTimeChunk    (parset.getUint  (prefix+"ntimechunk", 0)),
+        itsNTimeOut      (0)
     {
-      cout << "PREFIX: " << prefix << endl;
-
+      /// Maybe optionally a parset parameter directions to give the
+      /// directions of unknown sources.
+      /// Or make sources a vector of vectors like [name, ra, dec] where
+      /// ra and dec are optional.
       // Default nr of time chunks is maximum number of threads.
       if (itsNTimeChunk == 0) {
         itsNTimeChunk = OpenMP::maxThreads();
       }
-      cout << "NTIMECHUNK: " << itsNTimeChunk << endl;
-
-      // Check that time and freq windows fit nicely.
-      ASSERTSTR ((itsNTimeChunk * itsNTimeAvg) % itsResTimeAvg == 0,
-                 "time window should fit averaging integrally");
-
-      // JVZ: Added to keep track of time info needed for BBS grid.
-      itsTimeCenters.reserve(itsNTimeChunk);
-      itsTimeWidths.reserve(itsNTimeChunk);
-
+      // Check that time windows fit nicely.
+      ASSERTSTR ((itsNTimeChunk * itsNTimeAvg) % itsNTimeAvgSubtr == 0,
+                 "time window should fit final averaging integrally");
+      itsNTimeChunkSubtr = (itsNTimeChunk * itsNTimeAvg) / itsNTimeAvgSubtr;
+      // See if averaging in demix and subtract differs.
+      itsCalcSubtr = (itsNChanAvg != itsNChanAvgSubtr  ||
+                      itsNTimeAvg != itsNTimeAvgSubtr);
       // Collect all source names.
-      itsNrDir = itsSources.size() + itsExtraSources.size() + 1;
+      itsNrModel = itsSubtrSources.size() + itsModelSources.size();
+      itsNrDir   = itsNrModel + itsExtraSources.size() + 1;
       itsAllSources.reserve (itsNrDir);
       itsAllSources.insert (itsAllSources.end(),
-                            itsSources.begin(), itsSources.end());
+                            itsModelSources.begin(), itsModelSources.end());
+      itsAllSources.insert (itsAllSources.end(),
+                            itsSubtrSources.begin(), itsSubtrSources.end());
       itsAllSources.insert (itsAllSources.end(),
                             itsExtraSources.begin(), itsExtraSources.end());
 //      itsAllSources.push_back("target"); //// probably not needed
       itsAllSources.push_back(itsTarget);
 
       // Size buffers.
-      itsFactors.resize (itsNTimeChunk);
-//      itsBuf.resize (itsNTimeChunk * itsNTimeAvg);
+      itsFactors.resize      (itsNTimeChunk);
+      itsFactorsSubtr.resize (itsNTimeChunkSubtr);
       itsPhaseShifts.reserve (itsNrDir-1);
       itsFirstSteps.reserve  (itsNrDir);
       itsAvgResults.reserve  (itsNrDir);
-//      itsBBSExpr.reserve     (itsNrDir);
 
       // Create the steps for the sources to be removed.
       // Demixing consists of the following steps:
@@ -117,12 +128,11 @@ namespace LOFAR {
         // The resultstep gets the result.
         // The phasecenter can be given in a parameter. Its name is the default.
         // Note the PhaseShift knows about source names CygA, etc.
-        itsPhaseShifts.push_back (new PhaseShift
-                                  (input, parset,
-                                   prefix + itsAllSources[i] + '.',
-                                   itsAllSources[i]));
-        DPStep::ShPtr step1 (itsPhaseShifts[i]);
-        itsFirstSteps.push_back (step1);
+        PhaseShift* step1 = new PhaseShift (input, parset,
+                                            prefix + '.' + itsAllSources[i],
+                                            itsAllSources[i]);
+        itsFirstSteps.push_back (DPStep::ShPtr(step1));
+        itsPhaseShifts.push_back (step1);
         DPStep::ShPtr step2 (new Averager(input, parset, prefix));
         step1->setNextStep (step2);
         MultiResultStep* step3 = new MultiResultStep(itsNTimeChunk);
@@ -138,172 +148,38 @@ namespace LOFAR {
       targetAvg->setNextStep (DPStep::ShPtr(targetAvgRes));
       itsAvgResults.push_back (targetAvgRes);
 
-      // Open ParmDB and SourceDB.
-      try {
-        itsSourceDB = boost::shared_ptr<SourceDB>
-          (new SourceDB(ParmDBMeta("casa", "sky")));
-        ParmManager::instance().initCategory(SKY, itsSourceDB->getParmDB());
-      } catch (Exception &e) {
-        THROW(Exception, "Failed to open sky model parameter database: "
-              << "sky");
-      }
-
-      try {
-        ParmManager::instance().initCategory(INSTRUMENT,
-                                             ParmDB(ParmDBMeta("casa",
-                                                               "instrument")));
-      } catch (Exception &e) {
-        THROW(Exception, "Failed to open instrument model parameter database: "
-              << "instrument");
-      }
-
-      // Create Instrument instance using information present in DPInput.
-      size_t nStations = input->antennaNames().size();
-
-//      vector<Station::Ptr> stations;
-//      stations.reserve(nStations);
-//      for (size_t i = 0; i < nStations; ++i) {
-//        // Get station name and ITRF position.
-//        casa::MPosition position = MPosition::Convert(input->antennaPos()[i],
-//                                                      MPosition::ITRF)();
-//        // Store station information.
-//        stations.push_back(Station::Ptr(new Station(input->antennaNames()(i),
-//                                                    position)));
-//      }
-//      MPosition position = MPosition::Convert(input->arrayPos(),
-//                                              MPosition::ITRF)();
-//      Instrument::Ptr instrument(new Instrument("LOFAR", position,
-//        stations.begin(), stations.end()));
-
-      MeasurementAIPS ___bla(input->msName());
-      Instrument::Ptr instrument = ___bla.instrument();
-
-
-      // Get directions and make sure they are in J2000.
-      MDirection refDelay = MDirection::Convert(input->delayCenter(),
-                                               MDirection::J2000)();
-      MDirection refTile  = MDirection::Convert(input->tileBeamDir(),
-                                               MDirection::J2000)();
-
-      // Construct frequency axis (needs channel width information).
-      double chanWidth = input->chanWidths()[0];
-      ASSERT(allEQ(input->chanWidths(), chanWidth));
-      itsFreqAxisAvg = Axis::ShPtr(new RegularAxis(input->chanFreqs()(0)
-        - 0.5 * chanWidth, chanWidth, input->chanFreqs().size()));
-
-//      double factor = input->chanFreqs().size() / itsNChanAvg;
-//      if(input->chanFreqs().size() % itsNChanAvg > 0)
-//      {
-//        ++factor;
-//      }
-
-//      LOG_DEBUG_STR("factor: " << factor);
-      itsFreqAxisAvg = itsFreqAxisAvg->compress(itsNChanAvg);
-
-      ASSERT(input->getAnt1().size() == input->getAnt2().size());
-      for(size_t i = 0; i < input->getAnt1().size(); ++i)
-      {
-        unsigned int ant1 = input->getAnt1()[i];
-        unsigned int ant2 = input->getAnt2()[i];
-        itsBaselines.append(baseline_t(ant1, ant2));
-      }
-
-      // Deselect auto-correlations.
-      for(size_t i = 0; i < nStations; ++i)
-      {
-          itsBaselineMask.clear(i, i);
+      // Do the same for the subtract if it has different averaging.
+      if (itsCalcSubtr) {
+        itsAvgSubtr = DPStep::ShPtr (new Averager(input, prefix,
+                                                  itsNChanAvgSubtr,
+                                                  itsNTimeAvgSubtr));
+        itsAvgResultSubtr = new MultiResultStep(itsNTimeChunk);
+        itsAvgSubtr->setNextStep (DPStep::ShPtr(itsAvgResultSubtr));
+      } else {
+        // Same averaging, so use demix averaging for the subtract.
+        itsAvgSubtr = DPStep::ShPtr (new NullStep());
+        itsAvgResultSubtr = targetAvgRes;
       }
-
-      ASSERT(input->ncorr() == 4);
-      itsCorrelations.append(Correlation::XX);
-      itsCorrelations.append(Correlation::XY);
-      itsCorrelations.append(Correlation::YX);
-      itsCorrelations.append(Correlation::YY);
-
-      ModelConfig config;
-//      config.setDirectionalGain();
-      config.setGain();
-      BeamConfig beamConfig(BeamConfig::DEFAULT, false,
-        casa::Path("$LOFARROOT/share"));
-      config.setBeamConfig(beamConfig);
-      config.setCache();
-
-      vector<string> incl, excl;
-      incl.push_back("Gain:*");
-//      incl.push_back("DirectionalGain:*");
-
-      // TODO: Need to know reference frequency!!!
-      double refFreq = itsFreqAxisAvg->center(itsFreqAxisAvg->size() / 2);
-      for(size_t i = 0; i < itsAllSources.size(); ++i)
-      {
-        try
-        {
-          // TODO: Find sane way to derive phase center.
-//          MDirection refPhase = MDirection::makeMDirection(itsAllSources[i]);
-
-//          MDirection refPhase;
-//          string keyName = prefix+itsAllSources[i]+".phasecenter";
-//          if(parset.parameterSet().isDefined(keyName))
-//          {
-//            refPhase = handleCenter(parset.getStringVector(prefix+itsAllSources[i]+".phasecenter"));
-//          }
-//          else
-//          {
-//            refPhase = handleCenter(vector<string>(1, itsAllSources[i]));
-//          }
-
-          MDirection refPhase = MDirection::Convert(input->phaseCenter(),
-            MDirection::J2000)();
-
-          cout << "PHASE CENTER BBS: " << i << " " << refPhase << endl;
-//          config.setSources(vector<string>(1, itsAllSources[i]));
-          config.setSources(vector<string>(1, "SB000*"));
-          MeasurementExpr::Ptr model(new MeasurementExprLOFAR(*itsSourceDB,
-            BufferMap(), config, instrument, itsBaselines, refFreq, refPhase,
-            refDelay, refTile));
-          itsModels.push_back(model);
-        }
-        catch(Exception &e)
-        {
-          THROW(Exception, "Unable to construct model expression for source: "
-            << itsAllSources[i] << " (" << e.what() << ")");
-        }
-
-        ParmGroup parms = ParmManager::instance().makeSubset(incl, excl,
-          itsModels.back()->parms());
-        itsModelParms.push_back(parms);
-        itsParms.insert(parms.begin(), parms.end());
-      }
-
-//      itsParms = ParmManager::instance().makeSubset(incl, excl);
-
-      SolverOptions lsqOptions;
-//      lsqOptions.maxIter = 200;
-//      lsqOptions.epsValue = 1e-8;
-//      lsqOptions.epsDerivative = 1e-8;
-//      lsqOptions.colFactor = 1e-6;
-//      lsqOptions.lmFactor = 1e-3;
-//      lsqOptions.balancedEq = false;
-//      lsqOptions.useSVD = true;
-
-      lsqOptions.maxIter = 40;
-      lsqOptions.epsValue = 1e-9;
-      lsqOptions.epsDerivative = 1e-9;
-      lsqOptions.colFactor = 1e-9;
-      lsqOptions.lmFactor = 1.0;
-      lsqOptions.balancedEq = false;
-      lsqOptions.useSVD = true;
-
-      itsOptions = EstimateOptions(EstimateOptions::COMPLEX,
-        EstimateOptions::L2, false, 1, false, ~flag_t(0), flag_t(4),
-        lsqOptions);
-
+      // Construct frequency axis for the demix and subtract averaging.
+      itsFreqAxisDemix = makeFreqAxis (itsNChanAvg);
+      itsFreqAxisSubtr = makeFreqAxis (itsNChanAvgSubtr);
     }
 
     Demixer::~Demixer()
     {
     }
 
+    Axis::ShPtr Demixer::makeFreqAxis (uint nchanAvg)
+    {
+      casa::Vector<double> chanw = itsInput->chanWidths(nchanAvg);
+      double chanWidth = chanw[0];
+      ASSERT (allEQ (chanw, chanWidth));
+      return Axis::ShPtr
+        (new RegularAxis (itsInput->chanFreqs(nchanAvg)[0] - chanWidth*0.5,
+                          chanWidth, chanw.size()));
+    }
+
+/*
     MDirection Demixer::handleCenter(const vector<string> &center) const
     {
       // A case-insensitive name can be given for a moving source (e.g. SUN)
@@ -343,20 +219,19 @@ namespace LOFAR {
       }
       return MDirection(q0, q1, type);
     }
+*/
 
     void Demixer::updateInfo (DPInfo& info)
     {
       info.setNeedVisData();
       info.setNeedWrite();
-
-      itsTimeInterval = info.timeInterval();
-      cout << "itsTimeInterval: " << itsTimeInterval << endl;
-
-      itsNrChanIn = info.nchan();
-      itsNrBl     = info.nbaselines();
-      itsNrCorr   = info.ncorr();
-      itsFactorBuf.resize (IPosition(4, itsNrBl, itsNrChanIn, itsNrCorr,
+      itsNChanIn = info.nchan();
+      itsNrBl    = info.nbaselines();
+      itsNrCorr  = info.ncorr();
+      itsFactorBuf.resize (IPosition(4, itsNrBl, itsNChanIn, itsNrCorr,
                                      itsNrDir*(itsNrDir-1)/2));
+      itsTimeInterval = info.timeInterval();
+      double refFreq = itsFreqAxisDemix->center(itsFreqAxisDemix->size() / 2);
       // Let the internal steps update their data.
       // Use a copy of the DPInfo, otherwise it is updated multiple times.
       DPInfo infocp;
@@ -367,30 +242,35 @@ namespace LOFAR {
           step->updateInfo (infocp);
           step = step->getNextStep();
         }
-        // Create the BBSexpression.
-//        itsBBSExpr.push_back (BBSExpr::ShPtr(new BBSExpr(*itsInput, infocp,
-//                                                         itsAllSources[i])));
-//        itsModels.push_back (itsBBSExpr[i]->getModel());
+        // Create the BBS model expression for sources with a model.
+        if (i < itsNrModel) {
+          itsBBSExpr.addModel (*itsInput, infocp, itsAllSources[i], refFreq);
+        }
       }
       // Keep the averaged time interval.
+      itsNChanOut = infocp.nchan();
       itsTimeIntervalAvg = infocp.timeInterval();
       // Update the info of this object.
-      info.update (itsResChanAvg, itsResTimeAvg);
-      itsNrChanOut = info.nchan();
-      itsTimeIntervalRes = info.timeInterval();
+      info.update (itsNChanAvgSubtr, itsNTimeAvgSubtr);
+      itsNChanOutSubtr = info.nchan();
+      itsTimeIntervalSubtr = info.timeInterval();
     }
 
     void Demixer::show (std::ostream& os) const
     {
       os << "Demixer " << itsName << std::endl;
+      os << "  skymodel:       " << itsSkyName << std::endl;
+      os << "  instrumentmodel:" << itsInstrumentName << std::endl;
       os << "  target:         " << itsTarget << std::endl;
-      os << "  sources:        " << itsSources << std::endl;
+      os << "  subtractsources:" << itsSubtrSources << std::endl;
+      os << "  modelsources:   " << itsModelSources << std::endl;
       os << "  extrasources:   " << itsExtraSources << std::endl;
       os << "  jointsolve:     " << itsJointSolve << std::endl;
-      os << "  freqstep:       " << itsNChanAvg << std::endl;
-      os << "  timestep:       " << itsNTimeAvg << std::endl;
-      os << "  avgfreqstep:    " << itsResChanAvg << std::endl;
-      os << "  avgtimestep:    " << itsResTimeAvg << std::endl;
+      os << "  freqstep:       " << itsNChanAvgSubtr << std::endl;
+      os << "  timestep:       " << itsNTimeAvgSubtr << std::endl;
+      os << "  demixfreqstep:  " << itsNChanAvg << std::endl;
+      os << "  demixtimestep:  " << itsNTimeAvg << std::endl;
+      os << "  timechunk:      " << itsNTimeChunk << std::endl;
     }
 
     void Demixer::showTimings (std::ostream& os, double duration) const
@@ -403,24 +283,27 @@ namespace LOFAR {
 
       os << "          ";
       FlagCounter::showPerc1 (os, itsTimerPhaseShift.getElapsed(), self);
-      os << " Phase shift" << endl;
-
+      os << " of it spent in phase shifting/averaging data" << endl;
       os << "          ";
       FlagCounter::showPerc1 (os, itsTimerDemix.getElapsed(), self);
-      os << " Decorrelation factors" << endl;
+      os << " of it spent in calculating decorrelation factors" << endl;
+      os << "          ";
+      FlagCounter::showPerc1 (os, itsTimerSolve.getElapsed(), self);
+      os << " of it spent in solving source gains" << endl;
+      os << "          ";
+      FlagCounter::showPerc1 (os, itsTimerSubtract.getElapsed(), self);
+      os << " of it spent in subtracting modeled sources" << endl;
     }
 
     bool Demixer::process (const DPBuffer& buf)
     {
       itsTimer.start();
-
-      if(itsNTimeIn == 0)
-      {
-        itsTimeStart = buf.getTime() - 0.5 * itsTimeInterval;
+      // Set start time of the chunk.
+      if (itsNTimeIn == 0) {
+        itsStartTimeChunk = buf.getTime() - itsInput->timeInterval() * 0.5;
       }
-
-      ++itsNTimeIn;
-
+      // Update the count.
+      itsNTimeIn++;
       // Make sure all required data arrays are filled in.
       DPBuffer newBuf(buf);
       RefRows refRows(newBuf.getRowNrs());
@@ -446,20 +329,40 @@ namespace LOFAR {
       // For each itsNTimeAvg times, calculate the
       // phase rotation per direction.
       itsTimerDemix.start();
-      addFactors(newBuf);
+      addFactors (newBuf, itsFactorBuf);
       if (itsNTimeIn % itsNTimeAvg == 0) {
-        // TODO: NB: This call increases itsNTimeOut as a side effect!!!!
-        averageFactors();
-        itsNTimeIn  = 0;
+        makeFactors (itsFactorBuf, itsFactors[itsNTimeOut],
+                     itsAvgResults[0]->get()[itsNTimeOut].getWeights(),
+                     itsNChanOut);
+        // If needed, keep the original factors for subtraction.
+        if (!itsCalcSubtr) {
+          itsFactorsSubtr[itsNTimeOut].reference (itsFactors[itsNTimeOut].copy());
+        }
+        // Deproject sources without a model.
+        deproject (itsFactors[itsNTimeOut], itsAvgResults, itsNTimeOut);
+        itsFactorBuf = Complex();   // clear summation buffer
+        itsNTimeOut++;
+      }
+      if (itsCalcSubtr) {
+        // Subtract is done with different averaging parameters, so
+        // calculate the factors for it. 
+        addFactors (newBuf, itsFactorBufSubtr);
+        if (itsNTimeIn % itsNTimeAvgSubtr == 0) {
+          makeFactors (itsFactorBufSubtr, itsFactorsSubtr[itsNTimeOutSubtr],
+                       itsAvgResultSubtr->get()[itsNTimeOutSubtr].getWeights(),
+                       itsNChanOutSubtr);
+          itsFactorBufSubtr = Complex();   // clear summation buffer
+          itsNTimeOutSubtr++;
+        }
       }
       itsTimerDemix.stop();
 
       // Do BBS solve, etc. when sufficient time slots have been collected.
       if (itsNTimeOut == itsNTimeChunk) {
         demix();
-
-        ASSERT(itsNTimeIn == 0);
-        itsNTimeOut = 0;
+        itsNTimeIn       = 0;
+        itsNTimeOut      = 0;
+        itsNTimeOutSubtr = 0;
       }
 
       itsTimer.stop();
@@ -469,7 +372,6 @@ namespace LOFAR {
     void Demixer::finish()
     {
       // Process remaining entries.
-      // Let the next steps finish.
       if (itsNTimeIn > 0) {
         itsTimer.start();
 
@@ -482,18 +384,33 @@ namespace LOFAR {
         itsTimerPhaseShift.stop();
 
         itsTimerDemix.start();
-        // TODO: NB: This call increases itsNTimeOut as a side effect!!!!
-        averageFactors();
+        makeFactors (itsFactorBuf, itsFactors[itsNTimeOut],
+                     itsAvgResults[0]->get()[itsNTimeOut].getWeights(),
+                     itsNChanOut);
+        // Deproject sources without a model.
+        // If needed, keep the original factors for subtraction.
+        if (!itsCalcSubtr) {
+          itsFactorsSubtr[itsNTimeOut].reference (itsFactors[itsNTimeOut].copy());
+        }
+        deproject (itsFactors[itsNTimeOut], itsAvgResults, itsNTimeOut);
+        itsNTimeOut++;
+        if (itsCalcSubtr) {
+          makeFactors (itsFactorBufSubtr, itsFactorsSubtr[itsNTimeOutSubtr],
+                       itsAvgResultSubtr->get()[itsNTimeOutSubtr].getWeights(),
+                       itsNChanOutSubtr);
+          itsNTimeOutSubtr++;
+        }
         itsTimerDemix.stop();
 
         demix();
         itsTimer.stop();
       }
-
+      // Let the next steps finish.
       getNextStep()->finish();
     }
 
-    void Demixer::addFactors (const DPBuffer& newBuf)
+    void Demixer::addFactors (const DPBuffer& newBuf,
+                              Array<DComplex>& factorBuf)
     {
 ///#pragma omp parallel
       {
@@ -501,7 +418,7 @@ namespace LOFAR {
         uint ncorr  = newBuf.getData().shape()[0];
         uint nchan  = newBuf.getData().shape()[1];
         uint nbl    = newBuf.getData().shape()[2];
-        DComplex* factorPtr = itsFactorBuf.data();
+        DComplex* factorPtr = factorBuf.data();
         //# If ever in the future a time dependent phase center is used,
         //# the machine must be reset for each new time, thus each new call
         //# to process.
@@ -549,26 +466,26 @@ namespace LOFAR {
       }
     }
 
-    void Demixer::averageFactors()
+    void Demixer::makeFactors (const Array<DComplex>& bufIn,
+                               Array<DComplex>& bufOut,
+                               const Cube<float>& weightSums,
+                               uint nchanOut)
     {
-      // The averaged weights are calculated in the Averager, so use those.
-      const Cube<float>& weightSums = itsAvgResults[0]->get()[itsNTimeOut].getWeights();
       ASSERT (! weightSums.empty());
-      itsFactors[itsNTimeOut].resize (IPosition(5, itsNrDir, itsNrDir,
-                                                itsNrCorr, itsNrChanOut,
-                                                itsNrBl));
-      itsFactors[itsNTimeOut] = DComplex(1,0);
-      const DComplex* phin = itsFactorBuf.data();
+      bufOut.resize (IPosition(5, itsNrDir, itsNrDir,
+                               itsNrCorr, nchanOut, itsNrBl));
+      bufOut = DComplex(1,0);
+      const DComplex* phin = bufIn.data();
       for (uint d0=0; d0<itsNrDir; ++d0) {
         for (uint d1=d0+1; d1<itsNrDir; ++d1) {
-          DComplex* ph1 = itsFactors[itsNTimeOut].data() + d0*itsNrDir + d1;
-          DComplex* ph2 = itsFactors[itsNTimeOut].data() + d1*itsNrDir + d0;
+          DComplex* ph1 = bufOut.data() + d0*itsNrDir + d1;
+          DComplex* ph2 = bufOut.data() + d1*itsNrDir + d0;
           // Average for all channels and divide by the summed weights.
           const float* weightPtr = weightSums.data();
           for (uint k=0; k<itsNrBl; ++k) {
-            for (uint c0=0; c0<itsNrChanOut; ++c0) {
+            for (uint c0=0; c0<nchanOut; ++c0) {
               DComplex sum[4];
-              uint nch = std::min(itsNChanAvg, itsNrChanIn-c0*itsNChanAvg);
+              uint nch = std::min(itsNChanAvg, itsNChanIn-c0*itsNChanAvg);
               for (uint c1=0; c1<nch; ++c1) {
                 for (uint j=0; j<itsNrCorr; ++j) {
                   sum[j] += *phin++;
@@ -584,165 +501,133 @@ namespace LOFAR {
           }
         }
       }
-      ///      cout << "factor=" <<itsFactors[itsNTimeOut] << endl;
-      // Clear the summation buffer.
-      itsFactorBuf = DComplex();
-
-      // TODO: Factor this out somehow??
-      unsigned int nTime = itsNTimeIn % itsNTimeAvg;
-      nTime = (nTime == 0 ? itsNTimeAvg : nTime);
-      itsTimeWidths.push_back(nTime * itsTimeInterval);
-      itsTimeCenters.push_back(itsTimeStart + 0.5 * itsTimeWidths.back());
+    }
 
-      itsNTimeOut++;
+    void Demixer::deproject (Array<DComplex>& factors,
+                             vector<MultiResultStep*> avgResults,
+                             uint resultIndex)
+    {
+      // Get pointers to the data for the various directions.
+      vector<Complex*> resultPtr(itsNrDir);
+      for (uint j=0; j<itsNrDir; ++j) {
+        resultPtr[j] = avgResults[j]->get()[resultIndex].getData().data();
+      }
+      // Sources without a model have to be deprojected.
+      uint nrDeproject = itsNrDir - itsNrModel;
+      // The projection matrix is given by
+      //     P = I - A * inv(A.T.conj * A) * A.T.conj
+      // where A is the last column of the demixing matrix M.
+      // The BBS equations get:
+      //     P * M' * v_predict = P * v_averaged
+      // where M' is obtained by removing the last column of demixing matrix M.
+      // The dimensions of the matrices/vectors are:
+      //     P : NxN
+      //     M' : Nx(N-1)
+      //     v_predict : (N-1) x 1
+      //     v_averaged: N x 1
+      // where N is the number of modeled sources to use in demixing.
+      // In the general case S sources might not have a source model.
+      // In that case A is the NxS matrix containing all these columns
+      // from M and M' is the Nx(N-S) matrix without all these columns.
+
+      // Calculate P for all baselines,channels,correlations.
+      IPosition shape = factors.shape();
+      int nvis = shape[2] * shape[3] * shape[4];
+      shape[1] = itsNrModel;
+      Array<DComplex> newFactors (shape);
+      IPosition inShape (2, itsNrDir, itsNrDir);
+      IPosition outShape(2, itsNrDir, itsNrModel);
+      // omp parallel
+      casa::Matrix<DComplex> a(itsNrDir, nrDeproject);
+      casa::Matrix<DComplex> ma(itsNrDir, itsNrModel);
+      vector<DComplex> vec(itsNrDir);
+      // omp for
+      for (int i=0; i<nvis; ++i) {
+        // Split the matrix into the modeled and deprojected sources.
+        // Copy the columns to the individual matrices.
+        const DComplex* inptr  = factors.data() + i*itsNrDir*itsNrDir;
+        DComplex* outptr = newFactors.data() + i*itsNrDir*itsNrModel;
+        casa::Matrix<DComplex> out (outShape, outptr, SHARE);
+        // Copying a bit of data is probably faster than taking a matrix subset.
+        objcopy (ma.data(), inptr, itsNrDir*itsNrModel);
+        objcopy (a.data(), inptr + itsNrDir*itsNrModel, itsNrDir*nrDeproject);
+        // Calculate conjugated transpose of A, multiply with A, and invert.
+        casa::Matrix<DComplex> at(adjoint(a));
+        casa::Matrix<DComplex> ata(invert(product(at, a)));
+        if (ata.empty()) {
+          ata.resize (nrDeproject, nrDeproject);
+        }
+        DBGASSERT(ata.ncolumn()==nrDeproject && ata.nrow()==nrDeproject);
+        // Calculate P = I - A * ata * A.T.conj
+        casa::Matrix<DComplex> aata(product(a,ata));
+        casa::Matrix<DComplex> p (-product(product(a, ata), at));
+        casa::Vector<DComplex> diag(p.diagonal());
+        diag += DComplex(1,0);
+        // Multiply the demixing factors with P (get stored in newFactors).
+        out = product(p, ma);
+        ///        cout << "p matrix: " << p;
+        // Multiply the averaged data point with P.
+        std::fill (vec.begin(), vec.end(), DComplex());
+        for (uint j=0; j<itsNrDir; ++j) {
+          for (uint k=0; k<itsNrDir; ++k) {
+            vec[k] += DComplex(resultPtr[j][i]) * p(k,j);
+          }
+        }
+        // Put result back in averaged data for those sources.
+        for (uint j=0; j<itsNrDir; ++j) {
+          resultPtr[j][i] = vec[j];
+        }
+        ///        cout << vec << endl;
+      }
+      // Set the new demixing factors.
+      factors.reference (newFactors);
     }
 
     void Demixer::demix()
     {
+      itsTimerSolve.start();
       // Collect buffers for each direction.
       vector<vector<DPBuffer> > buffers;
-      for(uint i = 0; i < itsAvgResults.size(); ++i)
-      {
-        buffers.push_back(itsAvgResults[i]->get());
-        itsAvgResults[i]->clear();
+      size_t targetIndex = itsAvgResults.size() - 1;
+      for (size_t i=0; i<itsAvgResults.size(); ++i) {
+        buffers.push_back (itsAvgResults[i]->get());
+        // Do not clear target buffer, because it is shared with
+        // itsAvgResultSubtr if averaging of demix and subtract is the same.
+        if (i != targetIndex) {
+          itsAvgResults[i]->clear();
+        }
       }
-
       // Solve for the gains in the various directions.
-      for(uint i = 0; i < itsModels.size(); ++i)
-      {
-        itsModels[i]->setSolvables(itsModelParms[i]);
-      }
-
-      // Make time axis based on averaged target visibilities.
-      ASSERT(itsTimeCenters.size() == itsNTimeOut
-        && itsTimeWidths.size() == itsNTimeOut);
-
-      Axis::ShPtr timeAxis(new OrderedAxis(itsTimeCenters, itsTimeWidths));
-      itsTimeCenters.clear();
-      itsTimeWidths.clear();
-
+      itsBBSExpr.setSolvables();
       // Make time axis and grid.
-      Grid visGrid(itsFreqAxisAvg, timeAxis);
-
+      Axis::ShPtr timeAxis (new RegularAxis (itsStartTimeChunk,
+                                             itsTimeIntervalAvg,
+                                             itsNTimeOut));
+      Grid visGrid(itsFreqAxisDemix, timeAxis);
       // Solve for each time slot over all channels.
-      Grid solGrid(itsFreqAxisAvg->compress(itsFreqAxisAvg->size()), timeAxis);
-
-      LOG_DEBUG_STR("SHAPES: " << itsFactors[0].shape() << " " << itsFreqAxisAvg->size() << " " << buffers[0][0].getData().shape());
-
-//      double startTime = itsBuf[0].getTime() - itsInput->timeInterval() * 0.5;
-//      Axis::ShPtr timeAxis(new RegularAxis(startTime, itsTimeIntervalAvg,
-//                                             itsNTimeOut));
-//      Grid grid(itsBBSExpr[0]->getFreqAxis(), timeAxis);
+      Grid solGrid(itsFreqAxisDemix->compress(itsFreqAxisDemix->size()),
+                   timeAxis);
 
-      // Set parameter domain.
-      ParmManager::instance().setDomain(solGrid.getBoundingBox());
+      LOG_DEBUG_STR("SHAPES: " << itsFactors[0].shape() << " " << itsFreqAxisDemix->size() << " " << buffers[0][0].getData().shape());
 
       // Estimate model parameters.
-      estimate(buffers, itsModels, itsFactors, itsBaselines, itsCorrelations,
-        itsBaselineMask, itsCorrelationMask, visGrid, solGrid, itsOptions);
-
-      // Flush solutions to disk.
-      ParmManager::instance().flush();
-
-
-      // Subtract the demixed sources.
-      // TODO: As soon as we allow a different output resolution for the target
-      // field, visGrid needs to be re-derived for the subtract.
-      ASSERT(itsNChanAvg == itsResChanAvg && itsNTimeAvg == itsResTimeAvg);
+      itsBBSExpr.estimate(buffers, visGrid, solGrid, itsFactors);
+      itsTimerSolve.stop();
+      // Subtract the modeled sources.
+      itsTimerSubtract.start();
       LOG_DEBUG_STR("subtracting....");
-
-      // Solve for the gains in the various directions.
-      for(uint i = 0; i < itsModels.size(); ++i)
-      {
-        itsModels[i]->clearSolvables();
-      }
-
-      vector<unsigned int> directions(itsSources.size());
-      for(size_t i = 0; i < itsSources.size(); ++i)
-      {
-        directions[i] = i;
-      }
-
-      const unsigned int target = itsAllSources.size() - 1;
-
-      LOG_DEBUG_STR("target: " << target << " directions: " << directions);
-
-      subtract(buffers.back(), itsModels, itsFactors, itsBaselines,
-        itsCorrelations, itsBaselineMask, itsCorrelationMask, visGrid, target,
-        directions);
-
-      // Let the next steps process the data.
-      // TODO: As soon as we allow a different output resolution for the target
-      // field, this has to be adapted.
-      ASSERT(itsNChanAvg == itsResChanAvg && itsNTimeAvg == itsResTimeAvg);
-      for(uint i = 0; i < buffers.back().size(); ++i)
-      {
-        getNextStep()->process(buffers.back()[i]);
-      }
-
-      // Clear the intermediate buffers.
-      // TODO: Could already clear all buffers except for the target field
-      // before flushing buffers down the pipline (above).
-      for(uint i = 0; i < itsAvgResults.size(); ++i)
-      {
-        itsAvgResults[i]->clear();
+      itsBBSExpr.subtract (itsAvgResultSubtr->get(), visGrid, itsFactorsSubtr,
+                           itsNrDir-1, itsSubtrSources.size());
+      itsTimerSubtract.stop();
+      // Let the next step process the data.
+      itsTimer.stop();
+      for (uint i=0; i<itsNTimeOutSubtr; ++i) {
+        getNextStep()->process (itsAvgResultSubtr->get()[i]);
+        itsAvgResultSubtr->get()[i].clear();
+        itsAvgResults[targetIndex]->get()[i].clear();
       }
+      itsTimer.start();
     }
 
-//    void Demixer::subtract()
-//    {
-//      // Set expressions to not solvable.
-//      for (uint i=0; i<itsModels.size(); ++i) {
-//        itsModels[i]->clearSolvables();
-//      }
-//      // Loop through all time windows.
-//      for (uint i=0; i<itsNTimeOut; ++i) {
-//        // Subtract data for each time window.
-//      }
-//    }
-
-
-
-      // array dim: baseline, time, freq, dir1, dir2
-      // NDPPP compares data with predictions in Estimate class
-      // Beam Info lezen en aan BBS doorgeven (evt. via BBS class)
-      //   Joris maakt zijn read functie public
-      // Get RA and DEC of phase center (take care of moving targets).
-      /// MeqExpr for FFT/degrid per baseline met apart (gedeeld) degrid object
-      ///     MeqMatrix[2][2] degrid (uvCoordStat1, uvCoordStat2, times, freqs)
-      /// Joris:
-      //  - publiek maken beam info lezen
-      //  - estimate functie voor demixing
-      /// Ger
-      //  - create demixing matrix
-      //  - multiple predict result with demix matrix (also derivatives)
-      //  - subtract mbv demixing matrix
-      //  awimager
-      //  - for subbands in awimager use correct reffreq
-      //    both for separate windows and for subbands combined in single band
-      //    moet parameter worden in ATerm::evaluate
-      //  - option to treat channels in spw as subbands
-      //  Sven
-      //  - on-the-fly degridding
-      //  Ronald
-      //  - Can Solver solve in block matrices?
-      //  Wim
-      //  - can solve be parallelized?
-      // Cyril:
-      // - separate time window for element beam?
-      // - gridding element beam because small conv.func?
-      // - degridding: apply element beam per time window?
-      // - commit the code
-      // - Joris: new beam model in imager (in Cyril's branch)
-      // - Bas: merge ionosphere in imager (gridding takes most time)
-      // - Sanjay: write paper wide-band MSMFS
-      // -         paper wide-band A-projection
-      //    Cyril: A-projection or LOFAR plus element beam trick
-      //    Bas:   ionosphere
-      //    Johan/Stefan: verify beam model
-      //    George: is MSMFS needed in awimager?
-
-    // awimager: default channels by MFS is channel 0 only.
-
   } //# end namespace
 }
diff --git a/CEP/DP3/DPPP/src/EstimateNDPPP.cc b/CEP/DP3/DPPP/src/EstimateNDPPP.cc
index 660d068cc164c6faafd0800b03290970698221c1..51a05cd91b00db1153313e02f54ac9952f13d922 100644
--- a/CEP/DP3/DPPP/src/EstimateNDPPP.cc
+++ b/CEP/DP3/DPPP/src/EstimateNDPPP.cc
@@ -117,7 +117,7 @@ namespace
     JonesMatrix mix(const vector<JonesMatrix> &in,
         const vector<casa::Array<casa::DComplex> > &coeff,
         unsigned int target,
-        const vector<unsigned int> &directions,
+        unsigned int nsources,
         unsigned int baseline);
 
     void mix(const vector<JonesMatrix> &in, vector<JonesMatrix> &out,
@@ -135,7 +135,7 @@ namespace
         const vector<MeasurementExpr::Ptr> &models,
         const vector<casa::Array<casa::DComplex> > &coeff,
         unsigned int target,
-        const vector<unsigned int> &directions,
+        unsigned int nsources,
         const vector<pair<size_t, size_t> > &baselineMap,
         const vector<pair<size_t, size_t> > &correlationMap);
 
@@ -191,8 +191,12 @@ void estimate(const vector<vector<DPPP::DPBuffer> > &buffers,
     // =========================================================================
     // CHECK PRECONDITIONS
     // =========================================================================
+    const size_t nDirections = buffers.size();
+    const size_t nModels = models.size();
     {
-        ASSERT(buffers.size() == models.size() && buffers.size() > 0);
+        ASSERT(nDirections >= nModels && nModels > 0);
+        ASSERT(nDirections == coeff[0].shape()[0]);
+        ASSERT(nModels == coeff[0].shape()[1]);
 
         CorrelationSeq tmp;
         tmp.append(Correlation::XX);
@@ -200,7 +204,7 @@ void estimate(const vector<vector<DPPP::DPBuffer> > &buffers,
         tmp.append(Correlation::YX);
         tmp.append(Correlation::YY);
 
-        for(size_t i = 0; i < models.size(); ++i)
+        for(size_t i = 0; i < nModels; ++i)
         {
             ASSERT(models[i]->baselines() == models.front()->baselines());
             ASSERT(models[i]->correlations() == tmp);
@@ -209,8 +213,6 @@ void estimate(const vector<vector<DPPP::DPBuffer> > &buffers,
     }
     // =========================================================================
 
-    const size_t nDirections = buffers.size();
-
     // Construct a sequence of pairs of indices of matching baselines (i.e.
     // baselines common to both buffer and model).
     vector<pair<size_t, size_t> > blMap;
@@ -233,7 +235,7 @@ void estimate(const vector<vector<DPPP::DPBuffer> > &buffers,
         back_inserter(cellMap[TIME]));
 
     ParmGroup solvables;
-    for(size_t i = 0; i < nDirections; ++i)
+    for(size_t i = 0; i < nModels; ++i)
     {
         ParmGroup tmp = models[i]->solvables();
         solvables.insert(tmp.begin(), tmp.end());
@@ -316,7 +318,7 @@ void estimate(const vector<vector<DPPP::DPBuffer> > &buffers,
             cellMap[TIME][chunkStart.second].start);
         Location reqEnd(cellMap[FREQ][chunkEnd.first].end,
             cellMap[TIME][chunkEnd.second].end);
-        for(size_t i = 0; i < nDirections; ++i)
+        for(size_t i = 0; i < nModels; ++i)
         {
             models[i]->setEvalGrid(visGrid.subset(reqStart, reqEnd));
         }
@@ -346,7 +348,7 @@ void estimate(const vector<vector<DPPP::DPBuffer> > &buffers,
             timerIterate.stop();
 
             // Notify model that solvables have changed.
-            for(size_t i = 0; i < nDirections; ++i)
+            for(size_t i = 0; i < nModels; ++i)
             {
                 models[i]->solvablesChanged();
             }
@@ -426,21 +428,15 @@ void subtract(vector<DPPP::DPBuffer> &buffer,
     const BBS::CorrelationMask &correlationMask,
     const BBS::Grid &visGrid,
     unsigned int target,
-    const vector<unsigned int> &directions)
+    unsigned int nsources)
 {
     // =========================================================================
     // CHECK PRECONDITIONS
     // =========================================================================
     {
-        ASSERT(target < models.size());
-
-        ASSERT(directions.size() > 0);
-        for(size_t i = 0; i < directions.size(); ++i)
-        {
-            ASSERT(directions[i] < models.size());
-        }
-
+        ASSERT(nsources <= models.size());
         ASSERT(buffer.size() > 0);
+        ASSERT(int(target) < coeff[0].shape()[1]);
 
         CorrelationSeq tmp;
         tmp.append(Correlation::XX);
@@ -448,7 +444,7 @@ void subtract(vector<DPPP::DPBuffer> &buffer,
         tmp.append(Correlation::YX);
         tmp.append(Correlation::YY);
 
-        for(size_t i = 0; i < models.size(); ++i)
+        for(size_t i = 0; i < nsources; ++i)
         {
             ASSERT(models[i]->baselines() == models.front()->baselines());
             ASSERT(models[i]->correlations() == tmp);
@@ -469,12 +465,12 @@ void subtract(vector<DPPP::DPBuffer> &buffer,
     makeIndexMap(correlations, models.front()->correlations(), correlationMask,
         back_inserter(crMap));
 
-    for(size_t i = 0; i < models.size(); ++i)
+    for(size_t i = 0; i < nsources; ++i)
     {
         models[i]->setEvalGrid(visGrid);
     }
 
-    subtract2(buffer, models, coeff, target, directions, blMap, crMap);
+    subtract2(buffer, models, coeff, target, nsources, blMap, crMap);
 }
 
 
@@ -493,11 +489,12 @@ void equate(const Location &start, const Location &end,
     vector<Cell> &cells, Statistics &stats)
 
 {
-    ASSERT(buffers.size() == models.size());
+    ASSERT(buffers.size() >= models.size());
 
     const size_t nDirections = buffers.size();
+    const size_t nModels = models.size();
 
-    vector<JonesMatrix> sim(nDirections);
+    vector<JonesMatrix> sim(nModels);
     vector<JonesMatrix> mixed(nDirections);
 
     typedef vector<pair<size_t, size_t> >::const_iterator bl_iterator;
@@ -506,7 +503,7 @@ void equate(const Location &start, const Location &end,
     {
         // Evaluate models.
         stats.start(Statistics::T_EVALUATE);
-        for(size_t i = 0; i < nDirections; ++i)
+        for(size_t i = 0; i < nModels; ++i)
         {
             sim[i] = models[i]->evaluate(it->second);
         }
@@ -669,11 +666,11 @@ void equate2(const Location &start, const Location &end, size_t blIndex,
         const vector<MeasurementExpr::Ptr> &models,
         const vector<casa::Array<casa::DComplex> > &coeff,
         unsigned int target,
-        const vector<unsigned int> &directions,
+        unsigned int nsources,
         const vector<pair<size_t, size_t> > &baselineMap,
         const vector<pair<size_t, size_t> > &correlationMap)
     {
-        vector<JonesMatrix> sim(directions.size());
+        vector<JonesMatrix> sim(nsources);
 
         typedef vector<pair<size_t, size_t> >::const_iterator
             index_map_iterator;
@@ -682,14 +679,13 @@ void equate2(const Location &start, const Location &end, size_t blIndex,
             bl_end = baselineMap.end(); bl_it != bl_end; ++bl_it)
         {
             // Evaluate models.
-            for(size_t i = 0; i < directions.size(); ++i)
+            for(unsigned int i = 0; i < nsources; ++i)
             {
-                sim[i] = models[directions[i]]->evaluate(bl_it->second);
+                sim[i] = models[i]->evaluate(bl_it->second);
             }
 
             // Mix
-            JonesMatrix mixed = mix(sim, coeff, target, directions,
-                bl_it->first);
+            JonesMatrix mixed = mix(sim, coeff, target, nsources, bl_it->first);
 
             // Subtract.
             for(index_map_iterator cr_it = correlationMap.begin(),
@@ -711,7 +707,7 @@ void equate2(const Location &start, const Location &end, size_t blIndex,
                     ++buffer_it)
                 {
                     casa::Cube<casa::Complex> &data = buffer_it->getData();
-                    ASSERT(data.shape()(1) == nFreq);
+                    ASSERT(data.shape()(1) == int(nFreq));
 
                     for(size_t i = 0; i < nFreq; ++i)
                     {
@@ -726,7 +722,7 @@ void equate2(const Location &start, const Location &end, size_t blIndex,
     JonesMatrix mix(const vector<JonesMatrix> &in,
         const vector<casa::Array<casa::DComplex> > &coeff,
         unsigned int target,
-        const vector<unsigned int> &directions,
+        unsigned int nsources,
         unsigned int baseline)
     {
         const unsigned int nFreq = coeff.front().shape()(3);
@@ -737,12 +733,12 @@ void equate2(const Location &start, const Location &end, size_t blIndex,
         const Location end(nFreq - 1, nTime - 1);
 
         Matrix out[4];
-        for(unsigned int i = 0; i < directions.size(); ++i)
+        for(unsigned int i = 0; i < nsources; ++i)
         {
             for(unsigned int correlation = 0; correlation < 4; ++correlation)
             {
                 Matrix weight = makeMixingFactor(coeff, start, end, baseline,
-                    correlation, target, directions[i]);
+                    correlation, i, target);
 
                 const Matrix sim = in[i].element(correlation).value();
 
@@ -755,7 +751,7 @@ void equate2(const Location &start, const Location &end, size_t blIndex,
                     out[correlation] += weight * sim;
                 }
             } // correlations
-        } // directions
+        } // nsources
 
         JonesMatrix result(out[0], out[1], out[2], out[3]);
         result.setFlags(mergeFlags(in));
@@ -766,19 +762,20 @@ void equate2(const Location &start, const Location &end, size_t blIndex,
         const vector<casa::Array<casa::DComplex> > &coeff,
         const Location &start, const Location &end, unsigned int baseline)
     {
-        // dims array: ndir x ndir x ncorr x nchan x nbl (minor -> major).
-        // beter dims: nbl x ncorr x ndir x ndir x nchan ???
-        // TODO: Diagonal of mixing matrix == 1, so could optimize for this.
+        // dims array: ndir x nmodel x ncorr x nchan x nbl (minor -> major).
+        // better dims: nbl x ncorr x ndir x nmodel x nchan ???
 
-        ASSERT(in.size() == out.size());
+        const unsigned int nModels = in.size();
+        const unsigned int nDirections = coeff[0].shape()[0];
+        ASSERT(nDirections == out.size());
+        ASSERT(int(nModels) == coeff[0].shape()[1]);
 
         FlagArray flags = mergeFlags(in);
 
-        const unsigned int nDirections = in.size();
         for(unsigned int i = 0; i < nDirections; ++i)
         {
             Element element[4];
-            for(unsigned int j = 0; j < nDirections; ++j)
+            for(unsigned int j = 0; j < nModels; ++j)
             {
                 for(unsigned int k = 0; k < 4; ++k)
                 {
@@ -880,12 +877,13 @@ void equate2(const Location &start, const Location &end, size_t blIndex,
         double *re = 0, *im = 0;
         factor.dcomplexStorage(re, im);
 
-        // dims array: ndir x ndir x ncorr x nchan x nbl (minor -> major).
-        casa::IPosition index(5, column, row, correlation, 0, baseline);
+        // dims coeff: ndir x nmodel x ncorr x nchan x nbl (minor -> major).
+        //  nmodel = nr of directions with source model (thus excl. target)
+        casa::IPosition index(5, row, column, correlation, 0, baseline);
         for(unsigned int t = start.second; t <= end.second; ++t)
         {
             const casa::Array<casa::DComplex> &tmp = coeff[t];
-            ASSERT(tmp.shape()(3) == nFreq);
+            ASSERT(tmp.shape()(3) == int(nFreq));
             for(index(3) = start.first; index(3) <= static_cast<int>(end.first);
                 ++index(3))
             {
diff --git a/CEP/DP3/DPPP/src/MSReader.cc b/CEP/DP3/DPPP/src/MSReader.cc
index 2b5082e8e6ab52a5b9d5f7bd04e5d0e26346e33b..fb88ef95df7c6490913cb494f2c5eabaf150e2e2 100644
--- a/CEP/DP3/DPPP/src/MSReader.cc
+++ b/CEP/DP3/DPPP/src/MSReader.cc
@@ -74,6 +74,7 @@ namespace LOFAR {
       itsUseFlags         = parset.getBool   (prefix+"useflag", true);
       itsDataColName      = parset.getString (prefix+"datacolumn", "DATA");
       itsAutoWeight       = parset.getBool   (prefix+"autoweight", false);
+      itsAutoWeightForce  = parset.getBool   (prefix+"forceautoweight", false);
       itsNeedSort         = parset.getBool   (prefix+"sort", false);
       itsSelBL            = parset.getString (prefix+"baseline", string());
       // Try to open the MS and get its full name.
@@ -444,16 +445,22 @@ namespace LOFAR {
       // If so, we know it is in time order.
       // (sorting on TIME with LofarStMan can be expensive).
       bool needSort = itsNeedSort;
-      if (needSort) {
-        Record dminfo = itsMS.dataManagerInfo();
-        for (unsigned i=0; i<dminfo.nfields(); ++i) {
-          Record subrec = dminfo.subRecord(i);
-          if (subrec.asString("TYPE") == "LofarStMan") {
-            needSort = false;
-            break;
-          }
+      bool useRaw   = false;
+      Record dminfo = itsMS.dataManagerInfo();
+      for (unsigned i=0; i<dminfo.nfields(); ++i) {
+        Record subrec = dminfo.subRecord(i);
+        if (subrec.asString("TYPE") == "LofarStMan") {
+          needSort = false;
+          useRaw   = true;
+          break;
         }
       }
+      // Give an error if autoweight is used for a non-raw MS.
+      if (itsAutoWeightForce) {
+        itsAutoWeight = true;
+      } else if (!useRaw && itsAutoWeight) {
+        THROW (Exception, "Using autoweight=true cannot be done on DPPP-ed MS");
+      }
       // If not in order, sort the main table (also on baseline).
       Table sortms(itsMS);
       Block<String> sortCols(3);
diff --git a/CEP/DP3/DPPP/src/PhaseShift.cc b/CEP/DP3/DPPP/src/PhaseShift.cc
index 210ee4e34d62c6a955275fb1444dfc29433fbaa2..8b67bf1f1d87d55e45a50ecf62aabb8abcc7995c 100644
--- a/CEP/DP3/DPPP/src/PhaseShift.cc
+++ b/CEP/DP3/DPPP/src/PhaseShift.cc
@@ -89,7 +89,7 @@ namespace LOFAR {
       itsXYZ[0] = tt(0,0);
       itsXYZ[1] = tt(0,1);
       itsXYZ[2] = tt(0,2);
-      cout << itsXYZ[0]<<' '<<itsXYZ[1]<<' '<<itsXYZ[2]<<" ps"<<endl;
+      ///      cout << itsXYZ[0]<<' '<<itsXYZ[1]<<' '<<itsXYZ[2]<<" ps"<<endl;
 
       info.setPhaseCenter (newDir, original);
       // Calculate 2*pi*freq/C to get correct phase term (in wavelengths).
diff --git a/CEP/Imager/LofarFT/CMakeLists.txt b/CEP/Imager/LofarFT/CMakeLists.txt
index 8b2233b9b20b1668232d425e53bf5c920edf7aaa..16bb5a78907e2cf608ab787ab1b486d47e424f47 100644
--- a/CEP/Imager/LofarFT/CMakeLists.txt
+++ b/CEP/Imager/LofarFT/CMakeLists.txt
@@ -1,11 +1,11 @@
 # $Id$
 
-lofar_package(LofarFT 0.1 DEPENDS BBSKernel Common ParmDB)
+lofar_package(LofarFT 0.1 DEPENDS BBSKernel Common ParmDB ElementResponse)
 
 lofar_find_package(Casarest REQUIRED COMPONENTS synthesis)
 lofar_find_package(Casacore REQUIRED COMPONENTS images msfits)
 lofar_find_package(Boost REQUIRED COMPONENTS thread)
-lofar_find_package (FFTW3 REQUIRED COMPONENTS single double)
+lofar_find_package(FFTW3 REQUIRED COMPONENTS single double)
 
 # Needed for casarest
 add_definitions(-DCASA_STANDALONE)
diff --git a/CEP/Imager/LofarFT/include/LofarFT/LofarATerm.h b/CEP/Imager/LofarFT/include/LofarFT/LofarATerm.h
index 87624dfdd5b815fabda1ce0b21f44c16136cee54..67c229a31fb804f3a8edf14426dec1150b164c8f 100644
--- a/CEP/Imager/LofarFT/include/LofarFT/LofarATerm.h
+++ b/CEP/Imager/LofarFT/include/LofarFT/LofarATerm.h
@@ -24,271 +24,120 @@
 #define LOFAR_LOFARFT_LOFARATERM_H
 
 #include <Common/LofarTypes.h>
-#include <Common/lofar_map.h>
 #include <Common/lofar_vector.h>
+#include <BBSKernel/Instrument.h>
+#include <ParmDB/ParmFacade.h>
 
 #include <casa/Arrays/Array.h>
-#include <casa/BasicSL/String.h>
-#include <measures/Measures/MPosition.h>
+#include <casa/Containers/Record.h>
 #include <measures/Measures/MDirection.h>
+#include <measures/Measures/MEpoch.h>
 
 namespace casa
 {
   class DirectionCoordinate;
-  class MEpoch;
   class MeasurementSet;
-  class Path;
 }
 
 namespace LOFAR
 {
-  struct Vector3
-  {
-    const double &operator[](uint i) const
-    { return __data[i]; }
-
-    double &operator[](uint i)
-    { return __data[i]; }
-
-    double  __data[3];
-  };
-
-  class AntennaField
+  class LofarATerm
   {
   public:
-    enum Axis
-    {
-      P,
-      Q,
-      R,
-      N_Axis
-    };
+    LofarATerm(const casa::MeasurementSet &ms, const casa::Record& parameters);
 
-    struct Element
+    struct ITRFDirectionMap
     {
-      Vector3 offset;
-      bool    flag[2];
+      casa::MEpoch              epoch;
+      BBS::Vector3              refDelay;
+      BBS::Vector3              refTile;
+      casa::Cube<casa::Double>  directions;
     };
-
-    AntennaField()
-    {
-    }
-
-    AntennaField(const casa::String &name, const Vector3 &position,
-                 const Vector3 &p,
-                 const Vector3 &q, const Vector3 &r);
-
-    const casa::String &name() const;
-    const Vector3 &position() const;
-    const Vector3 &axis(Axis axis) const;
-
-    bool isHBA() const;
-
-    void appendTileElement(const Vector3 &offset);
-    inline uint nTileElement() const;
-    inline const Vector3 &tileElement(uint i) const;
-
-    void appendElement(const Element &element);
-    inline uint nElement() const;
-    inline const Element &element(uint i) const;
-
-  private:
-    casa::String    m_name;
-    Vector3         m_position;
-    Vector3         m_axes[N_Axis];
-    vector<Vector3> m_tileElements;
-    vector<Element> m_elements;
-  };
-
-  class Station
-  {
-  public:
-    Station()
-    {
-    }
-
-    Station(const casa::String &name, const casa::MPosition &position);
-    Station(const casa::String &name, const casa::MPosition &position,
-            const AntennaField &field0);
-    Station(const casa::String &name, const casa::MPosition &position,
-            const AntennaField &field0, const AntennaField &field1);
-
-    const casa::String &name() const;
-    const casa::MPosition &position() const;
-
-    bool isPhasedArray() const;
-    uint nField() const;
-    const AntennaField &field(uint i) const;
-
-  private:
-    casa::String            m_name;
-    casa::MPosition         m_position;
-    vector<AntennaField>    m_fields;
-  };
-
-  class Instrument
-  {
-  public:
-    Instrument()
-    {
-    }
-
-    Instrument(const casa::String &name, const casa::MPosition &position);
-
-    template <typename T>
-    Instrument(const casa::String &name, const casa::MPosition &position,
-               T first, T last);
-
-    const casa::String &name() const;
-    const casa::MPosition &position() const;
-
-    uint nStations() const;
-    const Station &station(uint i) const;
-    const Station &station(const casa::String &name) const;
-
-    void append(const Station &station);
-
-  private:
-    casa::String              m_name;
-    casa::MPosition           m_position;
-    map<casa::String, uint>   m_index;
-    vector<Station>           m_stations;
-  };
-
-  inline uint AntennaField::nTileElement() const
-  {
-    return m_tileElements.size();
-  }
-
-  const Vector3 &AntennaField::tileElement(uint i) const
-  {
-    return m_tileElements[i];
-  }
-
-  inline uint AntennaField::nElement() const
-  {
-    return m_elements.size();
-  }
-
-  inline const AntennaField::Element &AntennaField::element(uint i) const
-  {
-    return m_elements[i];
-  }
-
-  template <typename T>
-  Instrument::Instrument(const casa::String &name,
-                         const casa::MPosition &position,
-                         T first, T last)
-    :   m_name(name),
-        m_position(position),
-        m_stations(first, last)
-  {
-  }
-
-  class BeamCoeff
-  {
-  public:
-    BeamCoeff();
-
-    void load(const casa::Path &path);
-
-    // Center frequency used to scale frequency to range [-1.0, 1.0].
-    double center() const
-    {
-      return m_center;
-    }
-
-    // Width used to scale frequency to range [-1.0, 1.0].
-    double width() const
-    {
-      return m_width;
-    }
-
-    uint nElements() const
-    {
-      return m_coeff.shape()(0);
-    }
-
-    uint nPowerFreq() const
-    {
-      return m_coeff.shape()(1);
-    }
-
-    uint nPowerTheta() const
-    {
-      return m_coeff.shape()(2);
-    }
-
-    uint nHarmonics() const
-    {
-      return m_coeff.shape()(3);
-    }
-
-    casa::DComplex operator()(uint i, uint freq, uint theta, uint harmonic) const
-    {
-      return m_coeff(casa::IPosition(4, i, freq, theta, harmonic));
-    }
-
-  private:
-    double                      m_center, m_width;
-    casa::Array<casa::DComplex> m_coeff;
-  };
-
-  class LofarATerm
-  {
-  public:
-    LofarATerm(const casa::MeasurementSet &ms,
-               const casa::String &beamElementPath);
-
-    vector<casa::Cube<casa::Complex> > evaluate(const casa::IPosition &shape,
-      const casa::DirectionCoordinate &coordinates,
-      uint station,
-      const casa::MEpoch &epoch,
-      const casa::Vector<casa::Double> &freq,
-      bool normalize = false) const;
-
-  private:
-    casa::Array<casa::DComplex>
-    normalize(const casa::Array<casa::DComplex> &response) const;
-
-    casa::Cube<casa::Double>
-    computeITRFMap(const casa::DirectionCoordinate &coordinates,
+    
+    void setDirection(const casa::DirectionCoordinate &coordinates, const casa::IPosition &shape);
+    
+    void setEpoch(const casa::MEpoch &epoch);
+
+    // Compute an ITRF direction vector for each pixel at the given epoch. This
+    // map can then be used to call any of the evaluate* functions.
+    ITRFDirectionMap
+    makeDirectionMap(const casa::DirectionCoordinate &coordinates,
       const casa::IPosition &shape,
-      casa::MDirection::Convert convertor) const;
-
-    casa::Array<casa::DComplex> evaluateStationBeam(const Station &station,
-      const Vector3 &refDelay,
-      const Vector3 &refTile,
-      const casa::Cube<casa::Double> &map,
-      const casa::Vector<casa::Double> &freq) const;
-
-    casa::Cube<casa::DComplex> evaluateTileArrayFactor(const AntennaField &field,
-      const Vector3 &reference,
-      const casa::Cube<casa::Double> &map,
-      const casa::Vector<casa::Double> &freq) const;
+      const casa::MEpoch &epoch) const;
+
+    // Compute the LOFAR station response for the given station. This includes
+    // the effects of paralactic rotation, the dual dipole LOFAR antenna, the
+    // tile beam former (HBA only), and the station beam former.
+    //
+    // The freq argument is a list of frequencies at which the response will be
+    // evaluated. The reference argument is a list of station beam former
+    // reference frequencies. The normalize argument, when set to true, causes
+    // the response to be multiplied by the inverse of the response at the
+    // central pixel.
+    vector<casa::Cube<casa::Complex> > evaluate(uint idStation,
+      const casa::Vector<casa::Double> &freq,
+      const casa::Vector<casa::Double> &reference, bool normalize = false)
+      const;
+
+    // Compute the array factor for the given station and polarization (0 = X,
+    // 1 = Y).
+    //
+    // The freq argument is a list of frequencies at which the array factor will
+    // be evaluated. The reference argument is a list of station beam former
+    // reference frequencies. The normalize argument, when set to true, causes
+    // the response to be multiplied by the inverse of the array factor at the
+    // central pixel.
+      
+    vector<casa::Matrix<casa::Complex> > evaluateStationScalarFactor(uint idStation,
+      const casa::Vector<casa::Double> &freq,
+      const casa::Vector<casa::Double> &reference, bool normalize = false)
+      const;
 
-    casa::Array<casa::DComplex> evaluateElementBeam(const BeamCoeff &coeff,
-      const AntennaField &field,
-      const casa::Cube<casa::Double> &map,
+    vector<casa::Matrix<casa::Complex> > evaluateArrayFactor(uint idStation,
+      uint idPolarization,
+      const casa::Vector<casa::Double> &freq,
+      const casa::Vector<casa::Double> &reference, bool normalize = false)
+      const;
+
+    // Compute the LOFAR element response for the given station and antenna
+    // field. This includes the effects of paralactic rotation and the dual
+    // dipole LOFAR antenna.
+    //
+    // The freq argument is a list of frequencies at which the response will be
+    // evaluated. The normalize argument, when set to true, causes the response
+    // to be multiplied by the inverse of the response at the central pixel.
+    vector<casa::Cube<casa::Complex> > evaluateElementResponse(uint idStation,
+      uint idField,
+      const casa::Vector<casa::Double> &freq, bool normalize = false) const;
+
+    casa::Cube<casa::DComplex> evaluateIonosphere(
+      const uint station,
       const casa::Vector<casa::Double> &freq) const;
 
-    void initInstrument(const casa::MeasurementSet &ms);
-
-    Station initStation(const casa::MeasurementSet &ms,
-      uint id,
-      const casa::String &name,
-      const casa::MPosition &position) const;
-
-    void initReferenceDirections(const casa::MeasurementSet &ms, uint idField);
-
-    void initReferenceFreq(const casa::MeasurementSet &ms,
-      uint idDataDescription);
-
-    BeamCoeff        m_coeffLBA, m_coeffHBA;
-    casa::MDirection m_refDelay, m_refTile;
-    double           m_refFreq;
-    Instrument       m_instrument;
+  private:
+    
+    void initParmDB(const casa::String &parmdbname);
+    double get_parmvalue( std::string parmname );
+
+    casa::Record itsParameters;
+
+    BBS::Instrument::Ptr  itsInstrument;
+    const casa::DirectionCoordinate *itsDirectionCoordinates;
+    const casa::IPosition       *itsShape;
+    casa::MDirection      itsRefDelay, itsRefTile;
+    ITRFDirectionMap      itsITRFDirectionMap;
+    
+    // state variables for ionosphere
+    casa::Bool itsApplyBeam;
+    casa::Bool itsApplyIonosphere;
+    LOFAR::BBS::ParmFacade* pdb;
+    double time, r0, beta, height;
+    casa::Vector<casa::String>   cal_pp_names;
+    casa::Matrix<casa::Double> cal_pp;
+    casa::Vector<casa::Double> tec_white;
+    
   };
+
 } // namespace LOFAR
 
 #endif
diff --git a/CEP/Imager/LofarFT/include/LofarFT/LofarATermOld.h b/CEP/Imager/LofarFT/include/LofarFT/LofarATermOld.h
new file mode 100644
index 0000000000000000000000000000000000000000..02d88016369279431e17636f7717d19e89fd15bc
--- /dev/null
+++ b/CEP/Imager/LofarFT/include/LofarFT/LofarATermOld.h
@@ -0,0 +1,294 @@
+//# LofarATermOld.h: Compute the LOFAR beam response on the sky.
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#ifndef LOFAR_LOFARFT_LOFARATERMOLD_H
+#define LOFAR_LOFARFT_LOFARATERMOLD_H
+
+#include <Common/LofarTypes.h>
+#include <Common/lofar_map.h>
+#include <Common/lofar_vector.h>
+
+#include <casa/Arrays/Array.h>
+#include <casa/BasicSL/String.h>
+#include <measures/Measures/MPosition.h>
+#include <measures/Measures/MDirection.h>
+
+namespace casa
+{
+  class DirectionCoordinate;
+  class MEpoch;
+  class MeasurementSet;
+  class Path;
+}
+
+namespace LOFAR
+{
+  struct Vector3
+  {
+    const double &operator[](uint i) const
+    { return __data[i]; }
+
+    double &operator[](uint i)
+    { return __data[i]; }
+
+    double  __data[3];
+  };
+
+  class AntennaField
+  {
+  public:
+    enum Axis
+    {
+      P,
+      Q,
+      R,
+      N_Axis
+    };
+
+    struct Element
+    {
+      Vector3 offset;
+      bool    flag[2];
+    };
+
+    AntennaField()
+    {
+    }
+
+    AntennaField(const casa::String &name, const Vector3 &position,
+                 const Vector3 &p,
+                 const Vector3 &q, const Vector3 &r);
+
+    const casa::String &name() const;
+    const Vector3 &position() const;
+    const Vector3 &axis(Axis axis) const;
+
+    bool isHBA() const;
+
+    void appendTileElement(const Vector3 &offset);
+    inline uint nTileElement() const;
+    inline const Vector3 &tileElement(uint i) const;
+
+    void appendElement(const Element &element);
+    inline uint nElement() const;
+    inline const Element &element(uint i) const;
+
+  private:
+    casa::String    m_name;
+    Vector3         m_position;
+    Vector3         m_axes[N_Axis];
+    vector<Vector3> m_tileElements;
+    vector<Element> m_elements;
+  };
+
+  class Station
+  {
+  public:
+    Station()
+    {
+    }
+
+    Station(const casa::String &name, const casa::MPosition &position);
+    Station(const casa::String &name, const casa::MPosition &position,
+            const AntennaField &field0);
+    Station(const casa::String &name, const casa::MPosition &position,
+            const AntennaField &field0, const AntennaField &field1);
+
+    const casa::String &name() const;
+    const casa::MPosition &position() const;
+
+    bool isPhasedArray() const;
+    uint nField() const;
+    const AntennaField &field(uint i) const;
+
+  private:
+    casa::String            m_name;
+    casa::MPosition         m_position;
+    vector<AntennaField>    m_fields;
+  };
+
+  class Instrument
+  {
+  public:
+    Instrument()
+    {
+    }
+
+    Instrument(const casa::String &name, const casa::MPosition &position);
+
+    template <typename T>
+    Instrument(const casa::String &name, const casa::MPosition &position,
+               T first, T last);
+
+    const casa::String &name() const;
+    const casa::MPosition &position() const;
+
+    uint nStations() const;
+    const Station &station(uint i) const;
+    const Station &station(const casa::String &name) const;
+
+    void append(const Station &station);
+
+  private:
+    casa::String              m_name;
+    casa::MPosition           m_position;
+    map<casa::String, uint>   m_index;
+    vector<Station>           m_stations;
+  };
+
+  inline uint AntennaField::nTileElement() const
+  {
+    return m_tileElements.size();
+  }
+
+  const Vector3 &AntennaField::tileElement(uint i) const
+  {
+    return m_tileElements[i];
+  }
+
+  inline uint AntennaField::nElement() const
+  {
+    return m_elements.size();
+  }
+
+  inline const AntennaField::Element &AntennaField::element(uint i) const
+  {
+    return m_elements[i];
+  }
+
+  template <typename T>
+  Instrument::Instrument(const casa::String &name,
+                         const casa::MPosition &position,
+                         T first, T last)
+    :   m_name(name),
+        m_position(position),
+        m_stations(first, last)
+  {
+  }
+
+  class BeamCoeff
+  {
+  public:
+    BeamCoeff();
+
+    void load(const casa::Path &path);
+
+    // Center frequency used to scale frequency to range [-1.0, 1.0].
+    double center() const
+    {
+      return m_center;
+    }
+
+    // Width used to scale frequency to range [-1.0, 1.0].
+    double width() const
+    {
+      return m_width;
+    }
+
+    uint nElements() const
+    {
+      return m_coeff.shape()(0);
+    }
+
+    uint nPowerFreq() const
+    {
+      return m_coeff.shape()(1);
+    }
+
+    uint nPowerTheta() const
+    {
+      return m_coeff.shape()(2);
+    }
+
+    uint nHarmonics() const
+    {
+      return m_coeff.shape()(3);
+    }
+
+    casa::DComplex operator()(uint i, uint freq, uint theta, uint harmonic) const
+    {
+      return m_coeff(casa::IPosition(4, i, freq, theta, harmonic));
+    }
+
+  private:
+    double                      m_center, m_width;
+    casa::Array<casa::DComplex> m_coeff;
+  };
+
+  class LofarATermOld
+  {
+  public:
+    LofarATermOld(const casa::MeasurementSet &ms,
+               const casa::String &beamElementPath);
+
+    vector<casa::Cube<casa::Complex> > evaluate(const casa::IPosition &shape,
+      const casa::DirectionCoordinate &coordinates,
+      uint station,
+      const casa::MEpoch &epoch,
+      const casa::Vector<casa::Double> &freq,
+      bool normalize = false) const;
+
+  private:
+    casa::Array<casa::DComplex>
+    normalize(const casa::Array<casa::DComplex> &response) const;
+
+    casa::Cube<casa::Double>
+    computeITRFMap(const casa::DirectionCoordinate &coordinates,
+      const casa::IPosition &shape,
+      casa::MDirection::Convert convertor) const;
+
+    casa::Array<casa::DComplex> evaluateStationBeam(const Station &station,
+      const Vector3 &refDelay,
+      const Vector3 &refTile,
+      const casa::Cube<casa::Double> &map,
+      const casa::Vector<casa::Double> &freq) const;
+
+    casa::Cube<casa::DComplex> evaluateTileArrayFactor(const AntennaField &field,
+      const Vector3 &reference,
+      const casa::Cube<casa::Double> &map,
+      const casa::Vector<casa::Double> &freq) const;
+
+    casa::Array<casa::DComplex> evaluateElementBeam(const BeamCoeff &coeff,
+      const AntennaField &field,
+      const casa::Cube<casa::Double> &map,
+      const casa::Vector<casa::Double> &freq) const;
+
+    void initInstrument(const casa::MeasurementSet &ms);
+
+    Station initStation(const casa::MeasurementSet &ms,
+      uint id,
+      const casa::String &name,
+      const casa::MPosition &position) const;
+
+    void initReferenceDirections(const casa::MeasurementSet &ms, uint idField);
+
+    void initReferenceFreq(const casa::MeasurementSet &ms,
+      uint idDataDescription);
+
+    BeamCoeff        m_coeffLBA, m_coeffHBA;
+    casa::MDirection m_refDelay, m_refTile;
+    double           m_refFreq;
+    Instrument       m_instrument;
+  };
+} // namespace LOFAR
+
+#endif
diff --git a/CEP/Imager/LofarFT/include/LofarFT/LofarConvolutionFunction.h b/CEP/Imager/LofarFT/include/LofarFT/LofarConvolutionFunction.h
index 4bf2c5640ffcf674b4865a73fe015b9d38e0fdf9..bdddd38f8e35cae6b7862a464203de2bcfe283f7 100644
--- a/CEP/Imager/LofarFT/include/LofarFT/LofarConvolutionFunction.h
+++ b/CEP/Imager/LofarFT/include/LofarFT/LofarConvolutionFunction.h
@@ -44,6 +44,8 @@
 #include <coordinates/Coordinates/DirectionCoordinate.h>
 #include <casa/OS/PrecTimer.h>
 
+#include <lattices/Lattices/ArrayLattice.h>
+#include <lattices/Lattices/LatticeFFT.h>
 
 using namespace casa;
 
@@ -73,10 +75,16 @@ namespace LOFAR
                              const MeasurementSet& ms,
                              uInt nW, double Wmax,
                              uInt oversample,
-                             const String& beamElementPath,
-			     Int verbose,
-			     Int maxsupport,
-                             const String& imgName);
+                             Int verbose,
+                             Int maxsupport,
+                             const String& imgName,
+			     Bool Use_EJones,
+			     Bool Apply_Element,
+                             const casa::Record& parameters
+                            );
+    //,
+    //			     Int TaylorTerm,
+    //			     Double RefFreq);
 
 //      ~LofarConvolutionFunction ()
 //      {
@@ -118,8 +126,12 @@ namespace LOFAR
                                          bool degridding_step,
                                          double Append_average_PB_CF,
                                          Matrix<Complex>& Stack_PB_CF,
-                                         double& sum_weight_square);
+                                         double& sum_weight_square,
+					 uInt spw, Int TaylorTerm, double RefFreq);
 
+    Array<Complex>  ApplyElementBeam(Array<Complex> input_grid, Double time, uInt spw, const Matrix<bool>& Mask_Mueller_in, bool degridding_step);
+    Array<Complex>  ApplyElementBeam2(Array<Complex>& input_grid, Double time, uInt spw, const Matrix<bool>& Mask_Mueller_in, bool degridding_step, Int UsedMask=-1);
+    
     // Returns the average Primary Beam from the disk
     Matrix<float> Give_avg_pb();
 
@@ -133,20 +145,90 @@ namespace LOFAR
     // Zero padding of a Matrix
     Matrix<Complex> zero_padding(const Matrix<Complex>& Image, int Npixel_Out);
 
+
     // Get the W scale.
     const WScale& wScale() const
       { return m_wScale; }
 
+    vector< Matrix< Bool > > itsVectorMasksDegridElement;
+    void MakeMaskDegrid( const Array<Complex>& gridin, Int NumMask)
+    {
+
+      String MaskName("JAWS_masks_degrid/Mask"+String::toString(NumMask)+".boolim");
+      File MaskFile(MaskName);
+      if(!MaskFile.exists()){
+        //cout<<"... Making Masks ..."<<endl;
+        Matrix<Bool> Mask(IPosition(2,gridin.shape()[0],gridin.shape()[0]),false);
+        Matrix<Int> IntMask(IPosition(2,gridin.shape()[0],gridin.shape()[0]),false);
+        uInt GridSize(gridin.shape()[0]);
+        const Complex* inPtr = gridin.data();
+        Bool* outPtr = Mask.data();
+        for (uInt i=0; i<GridSize; ++i) {
+         for (uInt j=0; j<GridSize; ++j) {
+           if (inPtr->real() != 0  ||  inPtr->imag() != 0) {
+             (*(outPtr)) = true;
+            }
+            inPtr++;
+            outPtr++;
+          }
+        }
+        //itsVectorMasksDegridElement.push_back(Mask);
+
+        store(Mask,MaskName);
+        //cout<<"... Done Making Masks ..."<<endl;
+      }
+    }
+
+    Bool itsFilledVectorMasks;
+      //vector< Matrix< Bool > > itsVectorMasksDegridElement;
+      void ReadMaskDegrid()
+      {
+      	Int NumMask(0);
+      	while(true){
+      	  String MaskName("JAWS_masks_degrid/Mask"+String::toString(NumMask)+".boolim");
+      	  File MaskFile(MaskName);
+      	  if(MaskFile.exists())
+	    {
+	      //cout<<"Reading:"<<MaskName<<endl;
+	      PagedImage<Bool> pim(MaskName);
+	      Array<Bool> arr = pim.get();
+	      Matrix<Bool> Mask;
+	      Mask.reference (arr.nonDegenerate(2));
+	      itsVectorMasksDegridElement.push_back(Mask);
+	      NumMask+=1;
+	    }
+	  else
+	    {
+	      break;
+	    }
+	}
+	itsFilledVectorMasks=true;
+	
+      }
+      
+      Bool VectorMaskIsFilled(){return itsFilledVectorMasks;}
+
   private:
     void normalized_fft (Matrix<Complex>&, bool toFreq=true);
     void normalized_fft (PrecTimer& timer, Matrix<Complex>&, bool toFreq=true);
 
+    Matrix<Complex> give_normalized_fft_lapack(const Matrix<Complex> &im, bool toFreq=true)
+      {
+        Matrix<Complex> result(im.copy());
+        ArrayLattice<Complex> lattice(result);
+        LatticeFFT::cfft2d(lattice, toFreq);
+        if(toFreq){
+          result/=static_cast<Float>(result.shape()(0)*result.shape()(1));
+        }
+        else{
+          result*=static_cast<Float>(result.shape()(0)*result.shape()(1));
+        };
+        return result;
+      }
+
     MEpoch observationStartTime (const MeasurementSet &ms,
                                  uInt idObservation) const;
 
-    Double observationReferenceFreq (const MeasurementSet &ms,
-                                     uInt idDataDescription);
-
     // Estime spheroidal convolution function from the support of the fft
     // of the spheroidal in the image plane
     Double makeSpheroidCut();
@@ -158,11 +240,12 @@ namespace LOFAR
                                Double pixelSize,
                                Double w) const;
 
+
     // Return the angular resolution required for making the image of the
     // angular size determined by coordinates and shape.
     // The resolution is assumed to be the same on both direction axes.
     Double estimateAResolution(const IPosition &shape,
-                               const DirectionCoordinate &coordinates) const;
+                               const DirectionCoordinate &coordinates, double station_diam = 70.) const;
 
     // Apply a spheroidal taper to the input function.
     template <typename T>
@@ -181,7 +264,216 @@ namespace LOFAR
         }
       }
     }
+
+
+    // Linear interpolation
+    template <typename T>
+    Matrix< T > LinearInterpol(Matrix<T> ImageIn, Int  NpixOut)
+      {
+	Matrix<T> ImageOut(IPosition(2,NpixOut,NpixOut),0.);
+	float d0(1./(NpixOut-1.));
+	float d1(1./(ImageIn.shape()[0]-1.));
+	float dd(d0/d1);
+	float dx,dy,dxd,dyd,xin,yin;
+	float onef(1.);
+	uInt NpixOutm(NpixOut-1);
+	for(uInt i=0;i<(NpixOut);++i){
+	  dxd=i*dd;
+	  xin=floor(dxd);
+	  dx=dxd-xin;
+	  for(uInt j=0;j<(NpixOut);++j){
+	    dyd=j*dd;
+	    yin=floor(dyd);
+	    dy=dyd-yin;
+	    ImageOut(i,j)=(onef-dx)*(onef-dy)*ImageIn(xin,yin) + (onef-dx)*(dy)*ImageIn(xin,yin+1) + (dx)*(onef-dy)*ImageIn(xin+1,yin) + (dx)*(dy)*ImageIn(xin+1,yin+1);
+	  }
+	}
+	return ImageOut;
+      }
+
+    void Convolve(Matrix<Complex> gridin, Matrix<Complex> gridout, Matrix<Complex> ConvFunc){
+      Int Support(ConvFunc.shape()[0]);
+      Int GridSize(gridin.shape()[0]);
+      Int off(Support/2);
+      for(uInt i=Support/2;i<GridSize-Support/2;++i){
+	for(uInt j=Support/2;j<GridSize-Support/2;++j){
+	  if((gridin(i,j))!=Complex(0.,0.)){
+	    Complex val(gridin(i,j));
+	    for(uInt ii=0;ii<Support;++ii){
+	      for(uInt jj=0;jj<Support;++jj){
+		gridout(i-off+ii,j-off+jj)+=ConvFunc(ii,jj)*val;
+	      }
+	    }
+	  }
+	}
+      }
+    }
+
+    void ConvolveOpt(Matrix<Complex> gridin, Matrix<Complex> gridout, Matrix<Complex> ConvFunc){
+      Int Support(ConvFunc.shape()[0]);
+      Int GridSize(gridin.shape()[0]);
+      Int off(Support/2);
+
+      Complex* __restrict__ gridInPtr = gridin.data();
+      Complex* __restrict__ gridOutPtr = gridout.data();
+      Complex* __restrict__ ConvFuncPtr = ConvFunc.data();
+
+      for(uInt i=Support/2;i<GridSize-Support/2;++i){
+	for(uInt j=Support/2;j<GridSize-Support/2;++j){
+	  gridInPtr=gridin.data()+GridSize*i+j;
+	  if (gridInPtr->real() != 0  ||  gridInPtr->imag() != 0) {//if((*gridInPtr)!=Complex(0.,0.)){
+	    ConvFuncPtr = ConvFunc.data();
+	    for(uInt jj=0;jj<Support;++jj){
+	      for(uInt ii=0;ii<Support;++ii){
+		gridOutPtr = gridout.data()+(j-off+jj)*GridSize+i-off+ii;
+		(*gridOutPtr) += (*ConvFuncPtr)*(*gridInPtr);
+		ConvFuncPtr++;//=ConvFunc.data()+Support*ii+jj;
+	      }
+	    }
+	  }
+	  //gridInPtr++;
+	}
+      }
+      
+    }
+
+    void ConvolveGer( const Matrix<Complex>& gridin, Matrix<Complex>& gridout,
+		      const Matrix<Complex>& ConvFunc)
+    {
+      int Support(ConvFunc.shape()[0]);
+      int GridSize(gridin.shape()[0]);
+      int off(Support/2);
+      const Complex* inPtr = gridin.data() + off*GridSize + off;
+      for (uInt i=0; i<GridSize-Support; ++i) {
+	for (uInt j=0; j<GridSize-Support; ++j) {
+	  if (inPtr->real() != 0  ||  inPtr->imag() != 0) {
+	    const Complex* cfPtr = ConvFunc.data();
+	    for (uInt ii=0; ii<Support; ++ii) {
+	      Complex* outPtr = gridout.data() + (i+ii)*GridSize + j;
+	      for (uInt jj=0; jj<Support; ++jj) {
+		outPtr[jj] += *cfPtr++ * *inPtr;
+	      }
+	    }
+	  }
+	  inPtr++;
+	}
+	inPtr += Support;
+      }
+    }
+
+    void ConvolveGerArray( const Array<Complex>& gridin, Int ConvPol, Matrix<Complex>& gridout,
+			   const Matrix<Complex>& ConvFunc)
+    {
+      int Support(ConvFunc.shape()[0]);
+      int GridSize(gridin.shape()[0]);
+      int off(Support/2);
+
+      const Complex* inPtr = gridin.data() + ConvPol*GridSize*GridSize + off*GridSize + off;
+      for (uInt i=0; i<GridSize-Support; ++i) {
+	for (uInt j=0; j<GridSize-Support; ++j) {
+	  if (inPtr->real() != 0  ||  inPtr->imag() != 0) {
+	    const Complex* cfPtr = ConvFunc.data();
+	    for (uInt ii=0; ii<Support; ++ii) {
+	      Complex* outPtr = gridout.data() + (i+ii)*GridSize + j;
+	      for (uInt jj=0; jj<Support; ++jj) {
+		outPtr[jj] += *cfPtr++ * *inPtr;
+	      }
+	    }
+	  }
+	  inPtr++;
+	  }
+	inPtr += Support;
+      }
+    }
+    
+    
+
+    void ConvolveGerArrayMask( const Array<Complex>& gridin, Int ConvPol, Matrix<Complex>& gridout,
+			       const Matrix<Complex>& ConvFunc, Int UsedMask)
+    {
+      int Support(ConvFunc.shape()[0]);
+      int GridSize(gridin.shape()[0]);
+      int off(Support/2);
+
+      const Complex* inPtr = gridin.data() + ConvPol*GridSize*GridSize + off*GridSize + off;
+      const Bool* MaskPtr = itsVectorMasksDegridElement[UsedMask].data() + off*GridSize + off;
+      for (uInt i=0; i<GridSize-Support; ++i) {
+	for (uInt j=0; j<GridSize-Support; ++j) {
+	  if ((*MaskPtr)==true) {
+	    const Complex* cfPtr = ConvFunc.data();
+	    for (uInt ii=0; ii<Support; ++ii) {
+	      Complex* outPtr = gridout.data() + (i+ii)*GridSize + j;
+	      for (uInt jj=0; jj<Support; ++jj) {
+		outPtr[jj] += *cfPtr++ * *inPtr;
+	      }
+	    }
+	  }
+	  MaskPtr++;
+	  inPtr++;
+	}
+	inPtr += Support;
+	MaskPtr += Support;
+      }
+    }
     
+    
+    
+    // Linear interpolation
+    template <typename T>
+    Matrix< T > LinearInterpol2(Matrix<T> ImageIn, Int  NpixOut)
+      {
+	Matrix<T> ImageOut(IPosition(2,NpixOut,NpixOut),1e-7);
+	int nd(ImageIn.shape()[0]);
+	int ni(NpixOut);
+	float off(-.5);//-(((1.+1./(nd-1.))-1.)/2.)*(nd-1));
+	float a(nd/(ni-1.));//((1.+1./(nd-1.))/(ni-1.))*(nd-1));
+	float dx,dy,dxd,dyd,xin,yin;
+	float onef(1.);
+	uInt NpixOutm(NpixOut-1);
+	for(uInt i=0;i<(NpixOut);++i){
+	  dxd=i*a+off;
+	  xin=floor(dxd);
+	  dx=dxd-xin;
+	  for(uInt j=0;j<(NpixOut);++j){
+	    dyd=j*a+off;
+	    yin=floor(dyd);
+	    dy=dyd-yin;
+	    if((dxd<0)||((xin+1)>ImageIn.shape()[0]-1.)){continue;}
+	    if((dyd<0)||((yin+1)>ImageIn.shape()[0]-1.)){continue;}
+	    ImageOut(i,j)=(onef-dx)*(onef-dy)*ImageIn(xin,yin) + (onef-dx)*(dy)*ImageIn(xin,yin+1) + (dx)*(onef-dy)*ImageIn(xin+1,yin) + (dx)*(dy)*ImageIn(xin+1,yin+1);
+	  }
+	}
+	/* store(ImageIn,"ImageIn.img"); */
+	/* store(ImageOut,"ImageOut.img"); */
+	/* assert(false); */
+	return ImageOut;
+      }
+
+    void EstimateCoordShape(IPosition shape, DirectionCoordinate coordinate, double station_diameter=70.){
+      coordinate = m_coordinates;
+      Double aPixelAngSize = min(m_pixelSizeSpheroidal,
+				 estimateAResolution(m_shape, m_coordinates, station_diameter));
+      
+      Double pixelSize = abs(m_coordinates.increment()[0]);
+      Double imageDiameter = pixelSize * m_shape(0);
+      Int nPixelsConv = imageDiameter / aPixelAngSize;
+      if (nPixelsConv > itsMaxSupport) {
+          nPixelsConv = itsMaxSupport;
+      }
+      // Make odd and optimal.
+      nPixelsConv = FFTCMatrix::optimalOddFFTSize (nPixelsConv);
+      aPixelAngSize = imageDiameter / nPixelsConv;
+
+      shape=IPosition(2, nPixelsConv, nPixelsConv);
+      Vector<Double> increment_old(coordinate.increment());
+      Vector<Double> increment(2);
+      increment[0] = aPixelAngSize*sign(increment_old[0]);
+      increment[1] = aPixelAngSize*sign(increment_old[1]);
+      coordinate.setIncrement(increment);
+      Vector<Double> refpix(2, 0.5*(nPixelsConv-1));
+      coordinate.setReferencePixel(refpix);
+    }
+
     Double spheroidal(Double nu) const;
 
     template <typename T>
@@ -200,6 +492,7 @@ namespace LOFAR
 
 
     //# Data members.
+    casa::Record       itsParameters;
     IPosition           m_shape;
     DirectionCoordinate m_coordinates;
     WScale              m_wScale;
@@ -217,15 +510,20 @@ namespace LOFAR
     Matrix<Complex>     Spheroid_cut;
     //# Stack of the convolution functions for the average PB calculation
     Matrix<Float>       Spheroid_cut_im;
+    Matrix<Float>       Spheroid_cut_im_element;
     //# List of the ferquencies the CF have to be caluclated for
     Vector< Double >    list_freq;
     vector< Matrix<Complex> > m_WplanesStore;
     //# Aterm_store[double time][antenna][channel]=Cube[Npix,Npix,4]
     map<Double, vector< vector< Cube<Complex> > > > m_AtermStore;
+    map<Double, vector< vector< Cube<Complex> > > > m_AtermStore_element;
+    map<Double, vector< vector< Cube<Complex> > > > m_AtermStore_station;
     //# Average primary beam
     Matrix<Float>       Im_Stack_PB_CF0;
     Int                 itsVerbose;
     Int                 itsMaxSupport;
+    //    Int                 itsTaylorTerm;
+    //Double              itsRefFreq;
     String              itsImgName;
     vector<FFTCMatrix>  itsFFTMachines;
     Double              itsTimeW;
@@ -240,6 +538,17 @@ namespace LOFAR
     Double              itsTimeCFpar;
     Double              itsTimeCFfft;
     unsigned long long  itsTimeCFcnt;
+    Bool                its_Use_EJones;
+    Bool                its_Apply_Element;
+    uInt                its_MaxWSupport;
+    uInt                its_count_time;
+    mutable LogIO       m_logIO;
+    Matrix<Complex>     spheroid_cut_element_fft;
+    vector< vector< Matrix< Complex > > > GridsMueller;
+    LogIO &logIO() const
+      {
+        return m_logIO;
+      }
   };
 
 
@@ -264,7 +573,7 @@ namespace LOFAR
   void store (const DirectionCoordinate &dir, const Matrix<T> &data,
               const string &name)
   {
-    cout<<"Saving... "<<name<<endl;
+    //cout<<"Saving... "<<name<<endl;
     Vector<Int> stokes(1);
     stokes(0) = Stokes::I;
     CoordinateSystem csys;
@@ -295,7 +604,7 @@ namespace LOFAR
              const string &name)
   {
     AlwaysAssert(data.shape()(2) == 4, SynthesisError);
-    cout<<"Saving... "<<name<<endl;
+    //cout<<"Saving... "<<name<<endl;
     Vector<Int> stokes(4);
     stokes(0) = Stokes::XX;
     stokes(1) = Stokes::XY;
diff --git a/CEP/Imager/LofarFT/include/LofarFT/LofarConvolutionFunctionOld.h b/CEP/Imager/LofarFT/include/LofarFT/LofarConvolutionFunctionOld.h
new file mode 100644
index 0000000000000000000000000000000000000000..f6d4a0e6499b5387651df5668a8c2a6ad24582c7
--- /dev/null
+++ b/CEP/Imager/LofarFT/include/LofarFT/LofarConvolutionFunctionOld.h
@@ -0,0 +1,234 @@
+//# LofarConvolutionFunctionOld.h: Compute LOFAR convolution functions on demand.
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#ifndef LOFARFT_LOFARCONVOLUTIONFUNCTIONOLD_H
+#define LOFARFT_LOFARCONVOLUTIONFUNCTIONOLD_H
+
+#include <LofarFT/LofarATermOld.h>
+#include <LofarFT/LofarWTerm.h>
+#include <LofarFT/LofarCFStore.h>
+#include <LofarFT/FFTCMatrix.h>
+#include <Common/Timer.h>
+
+#include <casa/Arrays/Cube.h>
+#include <casa/Arrays/Matrix.h>
+#include <casa/Arrays/MatrixMath.h>
+#include <casa/Arrays/ArrayIter.h>
+#include <casa/Arrays/ArrayMath.h>
+#include <images/Images/PagedImage.h>
+#include <casa/Utilities/Assert.h>
+#include <ms/MeasurementSets/MeasurementSet.h>
+#include <measures/Measures/MDirection.h>
+#include <coordinates/Coordinates/CoordinateSystem.h>
+#include <coordinates/Coordinates/SpectralCoordinate.h>
+#include <coordinates/Coordinates/StokesCoordinate.h>
+#include <coordinates/Coordinates/DirectionCoordinate.h>
+#include <casa/OS/PrecTimer.h>
+
+
+using namespace casa;
+
+namespace LOFAR
+{
+
+  class LofarConvolutionFunctionOld
+  {
+
+  public:
+    LofarConvolutionFunctionOld(const IPosition& shape,
+                             const DirectionCoordinate& coordinates,
+                             const MeasurementSet& ms,
+                             uInt nW, double Wmax,
+                             uInt oversample,
+                             const String& beamElementPath,
+			     Int verbose,
+			     Int maxsupport,
+                             const String& imgName);
+
+//      ~LofarConvolutionFunctionOld ()
+//      {
+//      }
+
+    // Show the relative timings of the various steps.
+    void showTimings (std::ostream&, double duration, double timeCF) const;
+
+    // Show percentage of value in total with 1 decimal.
+    static void showPerc1 (std::ostream& os, double value, double total);
+
+    // Compute and store W-terms and A-terms in the fourier domain
+    void store_all_W_images();
+
+    // Get the spheroidal cut.
+    const Matrix<Float>& getSpheroidCut();
+
+    // Get the spheroidal cut from the file.
+    static Matrix<Float> getSpheroidCut (const String& imgName);
+
+    // Get the average PB from the file.
+    static Matrix<Float> getAveragePB (const String& imgName);
+
+
+    // Compute the fft of the beam at the minimal resolution for all antennas,
+    // and append it to a map object with a (double time) key.
+    void computeAterm(Double time);
+
+    // Compute the convolution function for all channel, for the polarisations
+    // specified in the Mueller_mask matrix
+    // Also specify weither to compute the Mueller matrix for the forward or
+    // the backward step. A dirty way to calculate the average beam has been
+    // implemented, by specifying the beam correcting to the given baseline
+    // and timeslot.
+    // RETURNS in a LofarCFStore: result[channel][Mueller row][Mueller column]
+    LofarCFStore makeConvolutionFunction(uInt stationA, uInt stationB,
+                                         Double time, Double w,
+                                         const Matrix<bool>& Mask_Mueller,
+                                         bool degridding_step,
+                                         double Append_average_PB_CF,
+                                         Matrix<Complex>& Stack_PB_CF,
+                                         double& sum_weight_square);
+
+    // Returns the average Primary Beam from the disk
+    Matrix<float> Give_avg_pb();
+
+    // Compute the average Primary Beam from the Stack of convolution functions
+    Matrix<Float> Compute_avg_pb(Matrix<Complex> &Sum_Stack_PB_CF,
+                                 double sum_weight_square);
+
+    // Zero padding of a Cube
+    Cube<Complex> zero_padding(const Cube<Complex>& Image, int Npixel_Out);
+
+    // Zero padding of a Matrix
+    Matrix<Complex> zero_padding(const Matrix<Complex>& Image, int Npixel_Out);
+
+    // Get the W scale.
+    const WScale& wScale() const
+      { return m_wScale; }
+
+  private:
+    void normalized_fft (Matrix<Complex>&, bool toFreq=true);
+    void normalized_fft (PrecTimer& timer, Matrix<Complex>&, bool toFreq=true);
+
+    MEpoch observationStartTime (const MeasurementSet &ms,
+                                 uInt idObservation) const;
+
+    Double observationReferenceFreq (const MeasurementSet &ms,
+                                     uInt idDataDescription);
+
+    // Estime spheroidal convolution function from the support of the fft
+    // of the spheroidal in the image plane
+    Double makeSpheroidCut();
+
+    // Return the angular resolution required for making the image of the
+    // angular size determined by coordinates and shape.
+    // The resolution is assumed to be the same on both direction axes.
+    Double estimateWResolution(const IPosition &shape,
+                               Double pixelSize,
+                               Double w) const;
+
+    // Return the angular resolution required for making the image of the
+    // angular size determined by coordinates and shape.
+    // The resolution is assumed to be the same on both direction axes.
+    Double estimateAResolution(const IPosition &shape,
+                               const DirectionCoordinate &coordinates) const;
+
+    // Apply a spheroidal taper to the input function.
+    template <typename T>
+    void taper (Matrix<T> &function) const
+    {
+      AlwaysAssert(function.shape()[0] == function.shape()[1], SynthesisError);
+      uInt size = function.shape()[0];
+      Double halfSize = (size-1) / 2.0;
+      Vector<Double> x(size);
+      for (uInt i=0; i<size; ++i) {
+        x[i] = spheroidal(abs(i - halfSize) / halfSize);
+      }
+      for (uInt i=0; i<size; ++i) {
+        for (uInt j=0; j<size; ++j) {
+          function(j, i) *= x[i] * x[j];
+        }
+      }
+    }
+    
+    Double spheroidal(Double nu) const;
+
+    template <typename T>
+    uInt findSupport(Matrix<T> &function, Double threshold) const
+    {
+      ///      Double peak = abs(max(abs(function)));
+      Double peak = max(amplitude(function));
+      threshold *= peak;
+      uInt halfSize = function.shape()[0] / 2;
+      uInt x = 0;
+      while (x < halfSize && abs(function(x, halfSize)) < threshold) {
+        ++x;
+      }
+      return 2 * (halfSize - x);
+    }
+
+
+    //# Data members.
+    IPosition           m_shape;
+    DirectionCoordinate m_coordinates;
+    WScale              m_wScale;
+    LofarWTerm          m_wTerm;
+    LofarATermOld       m_aTerm;
+    Double              m_maxW;
+    Double              m_pixelSizeSpheroidal;
+    uInt                m_nWPlanes;
+    uInt                m_nStations;
+    uInt                m_oversampling;
+    uInt                m_nChannel;
+    Double              m_refFrequency;
+    uInt                m_maxCFSupport;
+    //# Stack of the convolution functions for the average PB calculation
+    Matrix<Complex>     Spheroid_cut;
+    //# Stack of the convolution functions for the average PB calculation
+    Matrix<Float>       Spheroid_cut_im;
+    //# List of the ferquencies the CF have to be caluclated for
+    Vector< Double >    list_freq;
+    vector< Matrix<Complex> > m_WplanesStore;
+    //# Aterm_store[double time][antenna][channel]=Cube[Npix,Npix,4]
+    map<Double, vector< vector< Cube<Complex> > > > m_AtermStore;
+    //# Average primary beam
+    Matrix<Float>       Im_Stack_PB_CF0;
+    Int                 itsVerbose;
+    Int                 itsMaxSupport;
+    String              itsImgName;
+    vector<FFTCMatrix>  itsFFTMachines;
+    Double              itsTimeW;
+    Double              itsTimeWpar;
+    Double              itsTimeWfft;
+    unsigned long long  itsTimeWcnt;
+    Double              itsTimeA;
+    Double              itsTimeApar;
+    Double              itsTimeAfft;
+    unsigned long long  itsTimeAcnt;
+    Double              itsTimeCF;
+    Double              itsTimeCFpar;
+    Double              itsTimeCFfft;
+    unsigned long long  itsTimeCFcnt;
+  };
+
+
+} //# end namespace casa
+
+#endif
diff --git a/CEP/Imager/LofarFT/include/LofarFT/LofarCubeSkyEquation.h b/CEP/Imager/LofarFT/include/LofarFT/LofarCubeSkyEquation.h
index 1a409d92a34089ff8382ca3cd6b9fba73ad6149d..9d82a5163d03cfabdb0f3fce07ef5c5b30d7e4cc 100644
--- a/CEP/Imager/LofarFT/include/LofarFT/LofarCubeSkyEquation.h
+++ b/CEP/Imager/LofarFT/include/LofarFT/LofarCubeSkyEquation.h
@@ -46,36 +46,52 @@ class LofarCubeSkyEquation : public SkyEquation {
 
  public:
   LofarCubeSkyEquation(SkyModel& sm, VisSet& vs, FTMachine& ft, ComponentFTMachine& cft, Bool noModelCol=False);
- 
+
   //Read only iterator...hence no scratch col
   LofarCubeSkyEquation(SkyModel& sm, ROVisibilityIterator& vi, FTMachine& ft, ComponentFTMachine& cft, Bool noModelCol=False);
 
   virtual ~LofarCubeSkyEquation();
   virtual void predict(Bool incremental=False, MS::PredefinedColumns Type=MS::MODEL_DATA);
   virtual void gradientsChiSquared(Bool incremental, Bool commitModel=False);
-  
+
+//  virtual Matrix<Float> GiveAvgPB (Int taylor_order)
+//  {
+//    Matrix< Float > a((ftm_p[taylor_order]->getAveragePB()).copy());
+//    return a;
+//  };
+
+  /* virtual const Matrix<Float>& GiveAvgPB(Int taylor_order) { */
+  /*   Matrix< Float> a(IPosition(2,2,2),0.); */
+  /*   return a; */
+  /* }; */
+
   virtual void initializePutSlice(const VisBuffer& vb, Int cubeSlice=0, Int nCubeSlice=1);
-  virtual void putSlice(VisBuffer& vb, Bool dopsf, 
-			FTMachine::Type col,Int cubeSlice=0, 
+  virtual void putSlice(VisBuffer& vb, Bool dopsf,
+			FTMachine::Type col,Int cubeSlice=0,
 			Int nCubeSlice=1);
-  virtual void finalizePutSlice(const VisBuffer& vb,  
+  virtual void finalizePutSlice(const VisBuffer& vb,
 				Int cubeSlice=0, Int nCubeSlice=1);
   void initializeGetSlice(const VisBuffer& vb, Int row,
-			  Bool incremental, Int cubeSlice=0, 
-			  Int nCubeSlice=1);   
-  virtual VisBuffer& getSlice(VisBuffer& vb, 
+			  Bool incremental, Int cubeSlice=0,
+			  Int nCubeSlice=1);
+  virtual VisBuffer& getSlice(VisBuffer& vb,
 			      Bool incremental, Int cubeSlice=0,
-			      Int nCubeSlice=1); 
+			      Int nCubeSlice=1);
   void finalizeGetSlice();
   void isLargeCube(ImageInterface<Complex>& theIm, Int& nCubeSlice);
   //void makeApproxPSF(Int model, ImageInterface<Float>& psf);
-  //virtual void makeApproxPSF(Int model, ImageInterface<Float>& psf); 
+  //virtual void makeApproxPSF(Int model, ImageInterface<Float>& psf);
   void makeApproxPSF(PtrBlock<TempImage<Float> * >& psfs);
 
   //Get the flux scale that the ftmachines have if they have
   virtual void getCoverageImage(Int model, ImageInterface<Float>& im);
+
+
+
  protected:
 
+
+
   //Different versions of psf making
   void makeSimplePSF(PtrBlock<TempImage<Float> * >& psfs);
   void makeMosaicPSF(PtrBlock<TempImage<Float> * >& psfs);
@@ -85,15 +101,16 @@ class LofarCubeSkyEquation : public SkyEquation {
   Block<Matrix<Float> >weightSlice_p;
   Slicer sl_p;
   Int nchanPerSlice_p;
-  // Type of copy 
+  // Type of copy
   // 0 => a independent image just with coordinates gotten from cImage
   // 1 => a subImage referencing cImage ...no image copy
-  void sliceCube(CountedPtr<ImageInterface<Complex> >& slice,Int model, Int cubeSlice, Int nCubeSlice, Int typeOfCopy=0); 
+  void sliceCube(CountedPtr<ImageInterface<Complex> >& slice,Int model, Int cubeSlice, Int nCubeSlice, Int typeOfCopy=0);
   void sliceCube(SubImage<Float>*& slice,ImageInterface<Float>& image, Int cubeSlice, Int nCubeSlice);
   //frequency range from image
   Bool getFreqRange(ROVisibilityIterator& vi, const CoordinateSystem& coords,
 		  Int slice, Int nslice);
 
+
  private:
   // if skyjones changed in get or put we need to tell put or get respectively
   // about it
@@ -101,6 +118,7 @@ class LofarCubeSkyEquation : public SkyEquation {
 
   Bool destroyVisibilityIterator_p;
 
+
   Bool internalChangesPut_p;
   Bool internalChangesGet_p;
   Bool firstOneChangesPut_p;
diff --git a/CEP/Imager/LofarFT/include/LofarFT/LofarFTMachine.h b/CEP/Imager/LofarFT/include/LofarFT/LofarFTMachine.h
index eac1048497a8e110de7e35af3c38f3eea650c697..ad54ac195d515c23a1b4004c81c5fe5e73b54fc5 100644
--- a/CEP/Imager/LofarFT/include/LofarFT/LofarFTMachine.h
+++ b/CEP/Imager/LofarFT/include/LofarFT/LofarFTMachine.h
@@ -48,6 +48,7 @@
 #include <lattices/Lattices/LatticeCache.h>
 #include <lattices/Lattices/ArrayLattice.h>
 
+#include <Common/OpenMP.h>
 
 using namespace casa;
 
@@ -144,13 +145,25 @@ public:
   LofarFTMachine(Long cachesize, Int tilesize,  CountedPtr<VisibilityResamplerBase>& visResampler, String convType, const MeasurementSet& ms,
                  Int nwPlanes,
                  MPosition mLocation, Float padding, Bool usezero,
-                 Bool useDoublePrec, double wmax, const String& beamPath,
+                 Bool useDoublePrec, double wmax,
                  Int verbose,
-		 Int maxsupport,
+                 Int maxsupport,
                  Int oversample,
                  const String& imageName,
                  const Matrix<Bool>& gridMuellerMask,
-                 const Matrix<Bool>& degridMuellerMask);
+                 const Matrix<Bool>& degridMuellerMask,
+		 Double RefFreq,
+		 Bool Use_Linear_Interp_Gridder,
+		 Bool Use_EJones,
+		 int StepApplyElement,
+		 Double PBCut,
+		 Bool PredictFT,
+		 String PsfOnDisk,
+		 Bool UseMasksDegrid,
+		 Bool ReallyDoPSF,
+                 const casa::Record& parameters
+                );//,
+		 //Double FillFactor);
 //  LofarFTMachine(Long cachesize, Int tilesize,  CountedPtr<VisibilityResamplerBase>& visResampler,String convType,
 //	 MDirection mTangent, Float padding=1.0, Bool usezero=True,
 //	 Bool useDoublePrec=False);
@@ -205,6 +218,8 @@ public:
   void put(const VisBuffer& vb, Int row=-1, Bool dopsf=False,
            FTMachine::Type type=FTMachine::OBSERVED);
 
+  mutable Matrix<Float> itsAvgPB;
+  Bool its_Use_Linear_Interp_Gridder;
 
   // Make the entire image
   void makeImage(FTMachine::Type type,
@@ -217,7 +232,7 @@ public:
   ImageInterface<Complex>& getImage(Matrix<Float>&, Bool normalize=True);
 
   // Get the average primary beam.
-  const Matrix<Float>& getAveragePB() const;
+  virtual const Matrix<Float>& getAveragePB() const;
 
   // Get the spheroidal cut.
   const Matrix<Float>& getSpheroidCut() const
@@ -242,7 +257,7 @@ public:
     // size is not done.  If sumWt is not provided, normalization by
     // the sum of weights is also not done.
     //
-  
+
 
 
     virtual void makeSensitivityImage(Lattice<Complex>&,
@@ -289,18 +304,33 @@ public:
   virtual void setNoPadding(Bool nopad){noPadding_p=nopad;};
 
   virtual String name();
-  virtual void setMiscInfo(const Int qualifier){(void)qualifier;};
+  //virtual void setMiscInfo(const Int qualifier){(void)qualifier;};
+
+  //Cyr: The FTMachine has got to know the order of the Taylor term
+  virtual void setMiscInfo(const Int qualifier){thisterm_p=qualifier;};
   virtual void ComputeResiduals(VisBuffer&vb, Bool useCorrected);
 
-    void makeConjPolMap(const VisBuffer& vb, const Vector<Int> cfPolMap, Vector<Int>& conjPolMap);
-    //    Vector<Int> makeConjPolMap(const VisBuffer& vb);
-    void makeCFPolMap(const VisBuffer& vb, const Vector<Int>& cfstokes, Vector<Int>& polM);
 
+  void makeConjPolMap(const VisBuffer& vb, const Vector<Int> cfPolMap, Vector<Int>& conjPolMap);
+  //    Vector<Int> makeConjPolMap(const VisBuffer& vb);
+  void makeCFPolMap(const VisBuffer& vb, const Vector<Int>& cfstokes, Vector<Int>& polM);
+
+  String itsNamePsfOnDisk;
+  void setPsfOnDisk(String NamePsf){itsNamePsfOnDisk=NamePsf;}
+  virtual String GiveNamePsfOnDisk(){return itsNamePsfOnDisk;}
+  
 
 protected:
   // Padding in FFT
   Float padding_p;
-
+  Int thisterm_p;
+  Double itsRefFreq;
+  Bool itsPredictFT;
+  Int itsTotalStepsGrid;
+  Int itsTotalStepsDeGrid;
+  Bool itsMasksAllDone;
+  Bool its_UseMasksDegrid;
+  //Float its_FillFactor;
   // Get the appropriate data pointer
   Array<Complex>* getDataPointer(const IPosition&, Bool);
 
@@ -322,6 +352,65 @@ protected:
   // Gridder
   ConvolveGridder<Double, Complex>* gridder;
 
+  //Sum Grids
+  void SumGridsOMP(Array<Complex>& grid, const Array<Complex>& GridToAdd){
+    int y,ch,pol,dChan,dPol,dx;
+    int GridSize(grid.shape()[0]);
+    int NPol(grid.shape()[2]);
+    int NChan(grid.shape()[3]);
+    Complex* gridPtr;
+    const Complex* GridToAddPtr;
+    
+#pragma omp parallel for private(y,ch,pol,gridPtr,GridToAddPtr)
+    for(int x=0 ; x<grid.shape()[0] ; ++x){
+      for(ch=0 ; ch<NChan ; ++ch){
+	for(pol=0 ; pol<NPol ; ++pol){
+	  gridPtr = grid.data() + ch*NPol*GridSize*GridSize + pol*GridSize*GridSize+x*GridSize;
+	  GridToAddPtr = GridToAdd.data() + ch*NPol*GridSize*GridSize + pol*GridSize*GridSize+x*GridSize;
+	  for(y=0 ; y<grid.shape()[1] ; ++y){
+	    (*gridPtr++) += *GridToAddPtr++;
+	    //gridPtr++;
+	    //GridToAddPtr++;
+	  }
+	}
+      }
+    }
+    
+  }
+
+  void SumGridsOMP(Array<Complex>& grid, const vector< Array<Complex> >& GridToAdd0 ){
+
+    for(uInt vv=0; vv<GridToAdd0.size();vv++){
+      Array<Complex> GridToAdd(GridToAdd0[vv]);
+      int y,ch,pol,dChan,dPol,dx;
+      int GridSize(grid.shape()[0]);
+      int NPol(grid.shape()[2]);
+      int NChan(grid.shape()[3]);
+      Complex* gridPtr;
+      const Complex* GridToAddPtr;
+      
+#pragma omp parallel for private(y,ch,pol,gridPtr,GridToAddPtr)
+      for(int x=0 ; x<grid.shape()[0] ; ++x){
+	for(ch=0 ; ch<NChan ; ++ch){
+	  for(pol=0 ; pol<NPol ; ++pol){
+	    gridPtr = grid.data() + ch*NPol*GridSize*GridSize + pol*GridSize*GridSize+x*GridSize;
+	    GridToAddPtr = GridToAdd.data() + ch*NPol*GridSize*GridSize + pol*GridSize*GridSize+x*GridSize;
+	    for(y=0 ; y<grid.shape()[1] ; ++y){
+	      (*gridPtr++) += *GridToAddPtr++;
+	      //gridPtr++;
+	      //GridToAddPtr++;
+	    }
+	  }
+	}
+      }
+    }
+
+  }
+
+  
+  
+
+
   // Is this tiled?
   Bool isTiled;
 
@@ -344,15 +433,18 @@ protected:
 
   // Arrays for non-tiled gridding (one per thread).
   vector< Array<Complex> >  itsGriddedData;
+  Array<Complex> its_stacked_GriddedData;
+
   vector< Array<DComplex> > itsGriddedData2;
   vector< Matrix<Complex> > itsSumPB;
   vector< Matrix<Double> >  itsSumWeight;
   vector< double > itsSumCFWeight;
+
+
   ///Array<Complex>  griddedData;
   ///Array<DComplex> griddedData2;
   ///Matrix<Complex> itsSumPB;
   ///double itsSumWeight;
-  mutable Matrix<Float> itsAvgPB;
 
   Int priorCacheSize;
 
@@ -385,17 +477,22 @@ protected:
 
   LofarVisResampler visResamplers_p;
 
+  casa::Record       itsParameters;
   casa::MeasurementSet itsMS;
   Int itsNWPlanes;
   double itsWMax;
+  Double its_PBCut;
   int itsNThread;
-
+  Bool its_Use_EJones;
+  Bool its_Apply_Element;
+  Bool its_Already_Initialized;
+  Bool                its_reallyDoPSF;
   CountedPtr<LofarConvolutionFunction> itsConvFunc;
   Vector<Int> ConjCFMap_p, CFMap_p;
-  String itsBeamPath;
   int itsVerbose;
   int itsMaxSupport;
   Int itsOversample;
+  Vector< Double >    itsListFreq;
   String itsImgName;
   Matrix<Bool> itsGridMuellerMask;
   Matrix<Bool> itsDegridMuellerMask;
@@ -403,12 +500,22 @@ protected:
   double itsDegriddingTime;
   double itsCFTime;
   PrecTimer itsTotalTimer;
+  PrecTimer itsCyrilTimer;
+
+  double itsNextApplyTime;
+  int itsCounterTimes;
+  int itsStepApplyElement;
+  double itsTStartObs;
+  double itsDeltaTime;
+  Array<Complex> itsTmpStackedGriddedData;
+  Array<Complex> itsGridToDegrid;
+
 
 
       template <class T>
         void store(const Cube<T> &data, const string &name)
         {
-          
+
           CoordinateSystem csys;
           Matrix<Double> xform(2, 2);
           xform = 0.0;
@@ -427,7 +534,7 @@ protected:
           stokes(3) = Stokes::YY;
           csys.addCoordinate(StokesCoordinate(stokes));
           csys.addCoordinate(SpectralCoordinate(casa::MFrequency::TOPO, 60e6, 0.0, 0.0, 60e6));
-          
+
           PagedImage<T> im(TiledShape(IPosition(4, data.shape()(0), data.shape()(1), 4, 1)), csys, name);
           im.putSlice(data, IPosition(4, 0, 0, 0, 0));
         }
diff --git a/CEP/Imager/LofarFT/include/LofarFT/LofarFTMachineOld.h b/CEP/Imager/LofarFT/include/LofarFT/LofarFTMachineOld.h
new file mode 100644
index 0000000000000000000000000000000000000000..61ad036b49a5efeae5bb7d7a46c68e4bc9e07bfc
--- /dev/null
+++ b/CEP/Imager/LofarFT/include/LofarFT/LofarFTMachineOld.h
@@ -0,0 +1,440 @@
+//# LofarFTMachineOld.h: Definition for LofarFTMachineOld
+//# Copyright (C) 1996,1997,1998,1999,2000,2002
+//# Associated Universities, Inc. Washington DC, USA.
+//#
+//# This library is free software; you can redistribute it and/or modify it
+//# under the terms of the GNU Library General Public License as published by
+//# the Free Software Foundation; either version 2 of the License, or (at your
+//# option) any later version.
+//#
+//# This library 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 Library General Public
+//# License for more details.
+//#
+//# You should have received a copy of the GNU Library General Public License
+//# along with this library; if not, write to the Free Software Foundation,
+//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
+//#
+//# Correspondence concerning AIPS++ should be adressed as follows:
+//#        Internet email: aips2-request@nrao.edu.
+//#        Postal address: AIPS++ Project Office
+//#                        National Radio Astronomy Observatory
+//#                        520 Edgemont Road
+//#                        Charlottesville, VA 22903-2475 USA
+//#
+//#
+//# $Id$
+
+#ifndef LOFARFT_LOFARFTMACHINEOLD_H
+#define LOFARFT_LOFARFTMACHINEOLD_H
+
+#include <synthesis/MeasurementComponents/FTMachine.h>
+#include <casa/OS/File.h>
+#include <casa/OS/PrecTimer.h>
+#include <LofarFT/LofarVisResamplerOld.h>
+#include <LofarFT/LofarConvolutionFunctionOld.h>
+#include <LofarFT/LofarCFStore.h>
+#include <casa/Arrays/Matrix.h>
+#include <scimath/Mathematics/FFTServer.h>
+#include <msvis/MSVis/VisBuffer.h>
+#include <images/Images/ImageInterface.h>
+#include <images/Images/ImageInterface.h>
+#include <casa/Containers/Block.h>
+#include <casa/Arrays/Array.h>
+#include <casa/Arrays/Vector.h>
+#include <casa/Arrays/Matrix.h>
+#include <scimath/Mathematics/ConvolveGridder.h>
+#include <lattices/Lattices/LatticeCache.h>
+#include <lattices/Lattices/ArrayLattice.h>
+
+
+using namespace casa;
+
+namespace LOFAR {
+
+class casa::UVWMachine;
+// <summary>  An FTMachine for Gridded Fourier transforms </summary>
+
+// <use visibility=export>
+
+// <reviewed reviewer="" date="" tests="" demos="">
+
+// <prerequisite>
+//   <li> <linkto class=FTMachine>FTMachine</linkto> module
+//   <li> <linkto class=SkyEquation>SkyEquation</linkto> module
+//   <li> <linkto class=VisBuffer>VisBuffer</linkto> module
+// </prerequisite>
+//
+// <etymology>
+// FTMachine is a Machine for Fourier Transforms. LofarFTMachine does
+// Grid-based Fourier transforms.
+// </etymology>
+//
+// <synopsis>
+// The <linkto class=SkyEquation>SkyEquation</linkto> needs to be able
+// to perform Fourier transforms on visibility data. LofarFTMachine
+// allows efficient Fourier Transform processing using a
+// <linkto class=VisBuffer>VisBuffer</linkto> which encapsulates
+// a chunk of visibility (typically all baselines for one time)
+// together with all the information needed for processing
+// (e.g. UVW coordinates).
+//
+// Gridding and degridding in LofarFTMachine are performed using a
+// novel sort-less algorithm. In this approach, the gridded plane is
+// divided into small patches, a cache of which is maintained in memory
+// using a general-purpose <linkto class=LatticeCache>LatticeCache</linkto> class. As the (time-sorted)
+// visibility data move around slowly in the Fourier plane, patches are
+// swapped in and out as necessary. Thus, optimally, one would keep at
+// least one patch per baseline.
+//
+// A grid cache is defined on construction. If the gridded uv plane is smaller
+// than this, it is kept entirely in memory and all gridding and
+// degridding is done entirely in memory. Otherwise a cache of tiles is
+// kept an paged in and out as necessary. Optimally the cache should be
+// big enough to hold all polarizations and frequencies for all
+// baselines. The paging rate will then be small. As the cache size is
+// reduced below this critical value, paging increases. The algorithm will
+// work for only one patch but it will be very slow!
+//
+// This scheme works well for arrays having a moderate number of
+// antennas since the saving in space goes as the ratio of
+// baselines to image size. For the ATCA, VLBA and WSRT, this ratio is
+// quite favorable. For the VLA, one requires images of greater than
+// about 200 pixels on a side to make it worthwhile.
+//
+// The FFT step is done plane by plane for images having less than
+// 1024 * 1024 pixels on each plane, and line by line otherwise.
+//
+// The gridding and degridding steps are implemented in Fortran
+// for speed. In gridding, the visibilities are added onto the
+// grid points in the neighborhood using a weighting function.
+// In degridding, the value is derived by a weight summ of the
+// same points, using the same weighting function.
+// </synopsis>
+//
+// <example>
+// See the example for <linkto class=SkyModel>SkyModel</linkto>.
+// </example>
+//
+// <motivation>
+// Define an interface to allow efficient processing of chunks of
+// visibility data
+// </motivation>
+//
+// <todo asof="97/10/01">
+// <ul> Deal with large VLA spectral line case
+// </todo>
+
+class LofarFTMachineOld : public casa::FTMachine {
+public:
+
+  // Constructor: cachesize is the size of the cache in words
+  // (e.g. a few million is a good number), tilesize is the
+  // size of the tile used in gridding (cannot be less than
+  // 12, 16 works in most cases), and convType is the type of
+  // gridding used (SF is prolate spheriodal wavefunction,
+  // and BOX is plain box-car summation). mLocation is
+  // the position to be used in some phase rotations. If
+  // mTangent is specified then the uvw rotation is done for
+  // that location iso the image center.
+  // <group>
+//  LofarFTMachineOld(Long cachesize, Int tilesize, CountedPtr<VisibilityResamplerBase>& visResampler,
+//	  String convType="SF", Float padding=1.0, Bool usezero=True, Bool useDoublePrec=False);
+  LofarFTMachineOld(Long cachesize, Int tilesize,  CountedPtr<VisibilityResamplerBase>& visResampler, String convType, const MeasurementSet& ms,
+                 Int nwPlanes,
+                 MPosition mLocation, Float padding, Bool usezero,
+                 Bool useDoublePrec, double wmax, const String& beamPath,
+                 Int verbose,
+		 Int maxsupport,
+                 Int oversample,
+                 const String& imageName,
+                 const Matrix<Bool>& gridMuellerMask,
+                 const Matrix<Bool>& degridMuellerMask);
+//  LofarFTMachineOld(Long cachesize, Int tilesize,  CountedPtr<VisibilityResamplerBase>& visResampler,String convType,
+//	 MDirection mTangent, Float padding=1.0, Bool usezero=True,
+//	 Bool useDoublePrec=False);
+//  LofarFTMachineOld(Long cachesize, Int tilesize,  CountedPtr<VisibilityResamplerBase>& visResampler,String convType,
+//	 MPosition mLocation, MDirection mTangent, Float passing=1.0,
+//	 Bool usezero=True, Bool useDoublePrec=False);
+  // </group>
+
+  // Construct from a Record containing the LofarFTMachineOld state
+//  LofarFTMachineOld(const RecordInterface& stateRec);
+
+  // Copy constructor
+  LofarFTMachineOld(const LofarFTMachineOld &other);
+
+  // Assignment operator
+  LofarFTMachineOld &operator=(const LofarFTMachineOld &other);
+
+  // Clone
+  LofarFTMachineOld* clone() const;
+
+
+  ~LofarFTMachineOld();
+
+  // Show the relative timings of the various steps.
+  void showTimings (std::ostream&, double duration) const;
+
+  // Initialize transform to Visibility plane using the image
+  // as a template. The image is loaded and Fourier transformed.
+  void initializeToVis(ImageInterface<Complex>& image,
+		       const VisBuffer& vb);
+
+  // Finalize transform to Visibility plane: flushes the image
+  // cache and shows statistics if it is being used.
+  void finalizeToVis();
+
+  // Initialize transform to Sky plane: initializes the image
+  void initializeToSky(ImageInterface<Complex>& image,  Matrix<Float>& weight,
+		       const VisBuffer& vb);
+
+
+  // Finalize transform to Sky plane: flushes the image
+  // cache and shows statistics if it is being used. DOES NOT
+  // DO THE FINAL TRANSFORM!
+  void finalizeToSky();
+
+
+  // Get actual coherence from grid by degridding
+  void get(VisBuffer& vb, Int row=-1);
+
+
+  // Put coherence to grid by gridding.
+  void put(const VisBuffer& vb, Int row=-1, Bool dopsf=False,
+           FTMachine::Type type=FTMachine::OBSERVED);
+
+
+  // Make the entire image
+  void makeImage(FTMachine::Type type,
+		 VisSet& vs,
+		 ImageInterface<Complex>& image,
+		 Matrix<Float>& weight);
+
+  // Get the final image: do the Fourier transform and
+  // grid-correct, then optionally normalize by the summed weights
+  ImageInterface<Complex>& getImage(Matrix<Float>&, Bool normalize=True);
+
+  // Get the average primary beam.
+  const Matrix<Float>& getAveragePB() const;
+
+  // Get the spheroidal cut.
+  const Matrix<Float>& getSpheroidCut() const
+    { return itsConvFunc->getSpheroidCut(); }
+
+
+  ///  virtual void normalizeImage(Lattice<Complex>& skyImage,
+  ///			      const Matrix<Double>& sumOfWts,
+  ///			      Lattice<Float>& sensitivityImage,
+  ///			      Bool fftNorm)
+  ///    {throw(AipsError("LofarFTMachineOld::normalizeImage() called"));}
+
+  void normalizeAvgPB();
+  void normalizeAvgPB(ImageInterface<Complex>& inImage,
+                      ImageInterface<Float>& outImage);
+    //
+    // Make a sensitivity image (sensitivityImage), given the gridded
+    // weights (wtImage).  These are related to each other by a
+    // Fourier transform and normalization by the sum-of-weights
+    // (sumWt) and normalization by the product of the 2D FFT size
+    // along each axis.  If doFFTNorm=False, normalization by the FFT
+    // size is not done.  If sumWt is not provided, normalization by
+    // the sum of weights is also not done.
+    //
+  
+
+
+    virtual void makeSensitivityImage(Lattice<Complex>&,
+				      ImageInterface<Float>&,
+				      const Matrix<Float>& =Matrix<Float>(),
+				      const Bool& =True) {}
+    virtual void makeSensitivityImage(const VisBuffer& vb, const ImageInterface<Complex>& imageTemplate,
+				      ImageInterface<Float>& sensitivityImage);
+
+    inline virtual Float pbFunc(const Float& a, const Float& limit)
+    {if (abs(a) >= limit) return (a);else return 1.0;};
+    inline virtual Complex pbFunc(const Complex& a, const Float& limit)
+    {if (abs(a)>=limit) return (a); else return Complex(1.0,0.0);};
+    //
+    // Given the sky image (Fourier transform of the visibilities),
+    // sum of weights and the sensitivity image, this method replaces
+    // the skyImage with the normalized image of the sky.
+    //
+    virtual void normalizeImage(Lattice<Complex>& skyImage,
+				const Matrix<Double>& sumOfWts,
+				Lattice<Float>& sensitivityImage,
+				Bool fftNorm=True);
+    virtual void normalizeImage(Lattice<Complex>& skyImage,
+				const Matrix<Double>& sumOfWts,
+				Lattice<Float>& sensitivityImage,
+				Lattice<Complex>& sensitivitySqImage,
+				Bool fftNorm=True);
+
+    virtual ImageInterface<Float>& getSensitivityImage() {return *avgPB_p;}
+    virtual Matrix<Double>& getSumOfWeights() {return sumWeight;};
+    virtual Matrix<Double>& getSumOfCFWeights() {return sumCFWeight;};
+
+  // Get the final weights image
+  void getWeightImage(ImageInterface<Float>&, Matrix<Float>&);
+
+  // Save and restore the LofarFTMachineOld to and from a record
+  virtual Bool toRecord(String& error, RecordInterface& outRec,
+			Bool withImage=False);
+  virtual Bool fromRecord(String& error, const RecordInterface& inRec);
+
+  // Can this FTMachine be represented by Fourier convolutions?
+  virtual Bool isFourier() {return True;}
+
+  virtual void setNoPadding(Bool nopad){noPadding_p=nopad;};
+
+  virtual String name();
+  virtual void setMiscInfo(const Int qualifier){(void)qualifier;};
+  virtual void ComputeResiduals(VisBuffer&vb, Bool useCorrected);
+
+    void makeConjPolMap(const VisBuffer& vb, const Vector<Int> cfPolMap, Vector<Int>& conjPolMap);
+    //    Vector<Int> makeConjPolMap(const VisBuffer& vb);
+    void makeCFPolMap(const VisBuffer& vb, const Vector<Int>& cfstokes, Vector<Int>& polM);
+
+
+protected:
+  // Padding in FFT
+  Float padding_p;
+
+  // Get the appropriate data pointer
+  Array<Complex>* getDataPointer(const IPosition&, Bool);
+
+  void ok();
+
+  void init();
+
+  // Is this record on Grid? check both ends. This assumes that the
+  // ends bracket the middle
+  Bool recordOnGrid(const VisBuffer& vb, Int rownr) const;
+
+  // Image cache
+  LatticeCache<Complex> * imageCache;
+
+  // Sizes
+  Long cachesize;
+  Int  tilesize;
+
+  // Gridder
+  ConvolveGridder<Double, Complex>* gridder;
+
+  // Is this tiled?
+  Bool isTiled;
+
+  // Array lattice
+  CountedPtr<Lattice<Complex> > arrayLattice;
+
+  // Lattice. For non-tiled gridding, this will point to arrayLattice,
+  //  whereas for tiled gridding, this points to the image
+  CountedPtr<Lattice<Complex> > lattice;
+
+  String convType;
+
+  Float maxAbsData;
+
+  // Useful IPositions
+  IPosition centerLoc, offsetLoc;
+
+  // Image Scaling and offset
+  Vector<Double> uvScale, uvOffset;
+
+  // Arrays for non-tiled gridding (one per thread).
+  vector< Array<Complex> >  itsGriddedData;
+  vector< Array<DComplex> > itsGriddedData2;
+  vector< Matrix<Complex> > itsSumPB;
+  vector< Matrix<Double> >  itsSumWeight;
+  vector< double > itsSumCFWeight;
+  ///Array<Complex>  griddedData;
+  ///Array<DComplex> griddedData2;
+  ///Matrix<Complex> itsSumPB;
+  ///double itsSumWeight;
+  mutable Matrix<Float> itsAvgPB;
+
+  Int priorCacheSize;
+
+  // Grid/degrid zero spacing points?
+
+  Bool usezero_p;
+
+  //force no padding
+  Bool noPadding_p;
+
+  //Check if using put that avoids non-necessary reads
+  Bool usePut2_p;
+
+  //machine name
+  String machineName_p;
+
+  // Shape of the padded image
+  IPosition padded_shape;
+
+  Int convSampling;
+    Float pbLimit_p;
+    Int sensitivityPatternQualifier_p;
+    String sensitivityPatternQualifierStr_p;
+    Vector<Float> pbPeaks;
+    Bool pbNormalized_p;
+    // The average PB for sky image normalization
+    //
+    CountedPtr<ImageInterface<Float> > avgPB_p;
+    CountedPtr<ImageInterface<Complex> > avgPBSq_p;
+
+  LofarVisResamplerOld visResamplers_p;
+
+  casa::MeasurementSet itsMS;
+  Int itsNWPlanes;
+  double itsWMax;
+  int itsNThread;
+
+  CountedPtr<LofarConvolutionFunctionOld> itsConvFunc;
+  Vector<Int> ConjCFMap_p, CFMap_p;
+  String itsBeamPath;
+  int itsVerbose;
+  int itsMaxSupport;
+  Int itsOversample;
+  String itsImgName;
+  Matrix<Bool> itsGridMuellerMask;
+  Matrix<Bool> itsDegridMuellerMask;
+  double itsGriddingTime;
+  double itsDegriddingTime;
+  double itsCFTime;
+  PrecTimer itsTotalTimer;
+
+
+      template <class T>
+        void store(const Cube<T> &data, const string &name)
+        {
+          
+          CoordinateSystem csys;
+          Matrix<Double> xform(2, 2);
+          xform = 0.0;
+          xform.diagonal() = 1.0;
+          Quantum<Double> incLon((8.0 / data.shape()(0)) * C::pi / 180.0, "rad");
+          Quantum<Double> incLat((8.0 / data.shape()(1)) * C::pi / 180.0, "rad");
+          Quantum<Double> refLatLon(45.0 * C::pi / 180.0, "rad");
+          csys.addCoordinate(DirectionCoordinate(MDirection::J2000, Projection(Projection::SIN),
+                             refLatLon, refLatLon, incLon, incLat,
+                             xform, data.shape()(0) / 2, data.shape()(1) / 2));
+
+          Vector<Int> stokes(4);
+          stokes(0) = Stokes::XX;
+          stokes(1) = Stokes::XY;
+          stokes(2) = Stokes::YX;
+          stokes(3) = Stokes::YY;
+          csys.addCoordinate(StokesCoordinate(stokes));
+          csys.addCoordinate(SpectralCoordinate(casa::MFrequency::TOPO, 60e6, 0.0, 0.0, 60e6));
+          
+          PagedImage<T> im(TiledShape(IPosition(4, data.shape()(0), data.shape()(1), 4, 1)), csys, name);
+          im.putSlice(data, IPosition(4, 0, 0, 0, 0));
+        }
+
+
+};
+
+} //# end namespace
+
+#endif
diff --git a/CEP/Imager/LofarFT/include/LofarFT/LofarImager.h b/CEP/Imager/LofarFT/include/LofarFT/LofarImager.h
index 51a9da81974d9448918d112a1c9673dc537661c0..bb47a0ec822e151206aeeb962d524465af6af340 100644
--- a/CEP/Imager/LofarFT/include/LofarFT/LofarImager.h
+++ b/CEP/Imager/LofarFT/include/LofarFT/LofarImager.h
@@ -26,6 +26,7 @@
 #define LOFAR_LOFARFT_LOFARIMAGER_H
 
 #include <LofarFT/LofarFTMachine.h>
+#include <LofarFT/LofarFTMachineOld.h>
 #include <synthesis/MeasurementEquations/Imager.h>
 #include <casa/Containers/Record.h>
 
@@ -50,19 +51,20 @@ namespace LOFAR
 
     // Get the average primary beam.
     const Matrix<Float>& getAveragePB() const
-      { return itsMachine->getAveragePB(); }
+    { return itsMachine ? itsMachine->getAveragePB() : itsMachineOld->getAveragePB(); }
 
     // Get the spheroidal cut.
     const Matrix<Float>& getSpheroidCut() const
-      { return itsMachine->getSpheroidCut(); }
+    { return itsMachine ? itsMachine->getSpheroidCut() : itsMachineOld->getSpheroidCut(); }
 
     // Show the relative timings of the various steps.
     void showTimings (std::ostream&, double duration) const;
 
   private:
     //# Data members.
-    casa::Record    itsParameters;
-    LofarFTMachine* itsMachine;
+    casa::Record       itsParameters;
+    LofarFTMachine*    itsMachine;
+    LofarFTMachineOld* itsMachineOld;
   };
 
 } //# end namespace
diff --git a/CEP/Imager/LofarFT/include/LofarFT/LofarVisResampler.h b/CEP/Imager/LofarFT/include/LofarFT/LofarVisResampler.h
index 49c7f0bbad33f6cf9ce0d436fe12c21e9d56ff18..9abc2078ec8ec8d4711045027691444385b52dca 100644
--- a/CEP/Imager/LofarFT/include/LofarFT/LofarVisResampler.h
+++ b/CEP/Imager/LofarFT/include/LofarFT/LofarVisResampler.h
@@ -110,6 +110,20 @@ namespace LOFAR { //# NAMESPACE CASA - BEGIN
                           const Bool& dopsf, LofarCFStore& cfs)
     {DataToGridImpl_p(griddedData, vbs, rows, rbeg, rend, sumwt,dopsf,cfs);}
 
+    void lofarDataToGrid_linear (Array<Complex>& griddedData, LofarVBStore& vbs,
+                          const Vector<uInt>& rows,
+                          Int rbeg, Int rend,
+                          Matrix<Double>& sumwt,
+                          const Bool& dopsf, LofarCFStore& cfs)
+    {DataToGridImpl_linear_p(griddedData, vbs, rows, rbeg, rend, sumwt,dopsf,cfs);}
+    void lofarDataToGrid_linear (Array<DComplex>& griddedData, LofarVBStore& vbs,
+                          const Vector<uInt>& rows,
+                          Int rbeg, Int rend,
+                          Matrix<Double>& sumwt,
+                          const Bool& dopsf, LofarCFStore& cfs)
+    {DataToGridImpl_linear_p(griddedData, vbs, rows, rbeg, rend, sumwt,dopsf,cfs);}
+
+
     void lofarGridToData(LofarVBStore& vbs,
                          const Array<Complex>& grid,
                          const Vector<uInt>& rows,
@@ -166,6 +180,13 @@ namespace LOFAR { //# NAMESPACE CASA - BEGIN
 			  Matrix<Double>& sumwt,const Bool& dopsf,
                           LofarCFStore& cfs);
 
+    template <class T>
+    void DataToGridImpl_linear_p(Array<T>& griddedData, LofarVBStore& vb,
+                          const Vector<uInt>& rows,
+                          Int rbeg, Int rend,
+			  Matrix<Double>& sumwt,const Bool& dopsf,
+                          LofarCFStore& cfs);
+
 
     Vector<Int> cfMap_p, conjCFMap_p;
   };
diff --git a/CEP/Imager/LofarFT/include/LofarFT/LofarVisResamplerOld.h b/CEP/Imager/LofarFT/include/LofarFT/LofarVisResamplerOld.h
new file mode 100644
index 0000000000000000000000000000000000000000..4e59cf224803c0d665e21205492aca646666b677
--- /dev/null
+++ b/CEP/Imager/LofarFT/include/LofarFT/LofarVisResamplerOld.h
@@ -0,0 +1,173 @@
+//# LofarVisResamplerOld.h: Convolutional AW resampler for LOFAR data
+//# Copyright (C) 2011
+//# Associated Universities, Inc. Washington DC, USA.
+//#
+//# This library is free software; you can redistribute it and/or modify it
+//# under the terms of the GNU Library General Public License as published by
+//# the Free Software Foundation; either version 2 of the License, or (at your
+//# option) any later version.
+//#
+//# This library 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 Library General Public
+//# License for more details.
+//#
+//# You should have received a copy of the GNU Library General Public License
+//# along with this library; if not, write to the Free Software Foundation,
+//# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
+//#
+//# Correspondence concerning AIPS++ should be addressed as follows:
+//#        Internet email: aips2-request@nrao.edu.
+//#        Postal address: AIPS++ Project Office
+//#                        National Radio Astronomy Observatory
+//#                        520 Edgemont Road
+//#                        Charlottesville, VA 22903-2475 USA
+//#
+//# $Id$
+
+#ifndef LOFARFT_LOFARVISRESAMPLEROLD_H
+#define LOFARFT_LOFARVISRESAMPLEROLD_H
+
+#include <synthesis/MeasurementComponents/AWVisResampler.h>
+#include <LofarFT/LofarCFStore.h>
+#include <LofarFT/LofarVBStore.h>
+//added
+#include <LofarFT/LofarCFStore.h>
+
+#include <casa/Logging/LogIO.h>
+#include <casa/Logging/LogOrigin.h>
+#include <casa/Arrays/Cube.h>
+#include <casa/Arrays/Matrix.h>
+#include <casa/Arrays/ArrayIter.h>
+#include <casa/Arrays/ArrayMath.h>
+#include <images/Images/PagedImage.h>
+#include <casa/Utilities/Assert.h>
+
+#include <coordinates/Coordinates/CoordinateSystem.h>
+#include <coordinates/Coordinates/SpectralCoordinate.h>
+#include <coordinates/Coordinates/StokesCoordinate.h>
+
+#include <ms/MeasurementSets/MeasurementSet.h>
+#include <measures/Measures/MDirection.h>
+#include <measures/Measures/MeasConvert.h>
+#include <measures/Measures/MCDirection.h>
+#include <measures/Measures/MCPosition.h>
+#include <ms/MeasurementSets/MSAntenna.h>
+#include <ms/MeasurementSets/MSAntennaParse.h>
+#include <ms/MeasurementSets/MSAntennaColumns.h>
+#include <ms/MeasurementSets/MSDataDescription.h>
+#include <ms/MeasurementSets/MSDataDescColumns.h>
+#include <ms/MeasurementSets/MSField.h>
+#include <ms/MeasurementSets/MSFieldColumns.h>
+#include <ms/MeasurementSets/MSObservation.h>
+#include <ms/MeasurementSets/MSObsColumns.h>
+#include <ms/MeasurementSets/MSPolarization.h>
+#include <ms/MeasurementSets/MSPolColumns.h>
+#include <ms/MeasurementSets/MSSpectralWindow.h>
+#include <ms/MeasurementSets/MSSpWindowColumns.h>
+#include <ms/MeasurementSets/MSSelection.h>
+#include <measures/Measures/MeasTable.h>
+
+#include <lattices/Lattices/ArrayLattice.h>
+#include <lattices/Lattices/LatticeFFT.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <casa/vector.h>
+#include <casa/OS/Directory.h>
+
+//=========
+
+using namespace casa;
+
+namespace LOFAR { //# NAMESPACE CASA - BEGIN
+
+  class LofarVisResamplerOld: public AWVisResampler
+  {
+  public:
+    LofarVisResamplerOld(): AWVisResampler()  {}
+    LofarVisResamplerOld(const CFStore& cfs): AWVisResampler(cfs)      {}
+    virtual ~LofarVisResamplerOld()                                    {}
+
+    virtual VisibilityResamplerBase* clone()
+    {return new LofarVisResamplerOld(*this);}
+
+    void copy(const LofarVisResamplerOld& other)
+    {AWVisResampler::copy(other); }
+
+    // Re-sample the griddedData on the VisBuffer (a.k.a gridding).
+    void lofarDataToGrid (Array<Complex>& griddedData, LofarVBStore& vbs,
+                          const Vector<uInt>& rows,
+                          Int rbeg, Int rend,
+                          Matrix<Double>& sumwt,
+                          const Bool& dopsf, LofarCFStore& cfs)
+    {DataToGridImpl_p(griddedData, vbs, rows, rbeg, rend, sumwt,dopsf,cfs);}
+    void lofarDataToGrid (Array<DComplex>& griddedData, LofarVBStore& vbs,
+                          const Vector<uInt>& rows,
+                          Int rbeg, Int rend,
+                          Matrix<Double>& sumwt,
+                          const Bool& dopsf, LofarCFStore& cfs)
+    {DataToGridImpl_p(griddedData, vbs, rows, rbeg, rend, sumwt,dopsf,cfs);}
+
+    void lofarGridToData(LofarVBStore& vbs,
+                         const Array<Complex>& grid,
+                         const Vector<uInt>& rows,
+                         Int rbeg, Int rend,
+                         LofarCFStore& cfs);
+
+
+    virtual void setCFMaps(const Vector<Int>& cfMap, const Vector<Int>& conjCFMap)
+    {cfMap_p.assign(cfMap); conjCFMap_p.assign(conjCFMap);}
+
+    void lofarComputeResiduals(LofarVBStore& vbs);
+
+  void sgrid(Vector<Double>& pos, Vector<Int>& loc,
+			     Vector<Int>& off, Complex& phasor,
+			     const Int& irow, const Matrix<Double>& uvw,
+			     const Double& dphase, const Double& freq,
+			     const Vector<Double>& scale,
+			     const Vector<Double>& offset,
+                                const Vector<Float>& sampling);
+
+    /*
+  template <class T>
+    void store2(const Matrix<T> &data, const string &name)
+    {
+      CoordinateSystem csys;
+
+      Matrix<Double> xform(2, 2);
+      xform = 0.0;
+      xform.diagonal() = 1.0;
+      Quantum<Double> incLon((8.0 / data.shape()(0)) * C::pi / 180.0, "rad");
+      Quantum<Double> incLat((8.0 / data.shape()(1)) * C::pi / 180.0, "rad");
+      Quantum<Double> refLatLon(45.0 * C::pi / 180.0, "rad");
+      csys.addCoordinate(DirectionCoordinate(MDirection::J2000, Projection(Projection::SIN),
+					     refLatLon, refLatLon, incLon, incLat,
+					     xform, data.shape()(0) / 2, data.shape()(1) / 2));
+
+      Vector<Int> stokes(1);
+      stokes(0) = Stokes::I;
+      csys.addCoordinate(StokesCoordinate(stokes));
+      csys.addCoordinate(SpectralCoordinate(casa::MFrequency::TOPO, 60e6, 0.0, 0.0, 60e6));
+
+      PagedImage<T> im(TiledShape(IPosition(4, data.shape()(0), data.shape()(1), 1, 1)), csys, name);
+      im.putSlice(data, IPosition(4, 0, 0, 0, 0));
+    };
+    */
+
+  private:
+    // Re-sample the griddedData on the VisBuffer (a.k.a de-gridding).
+    //
+    template <class T>
+    void DataToGridImpl_p(Array<T>& griddedData, LofarVBStore& vb,
+                          const Vector<uInt>& rows,
+                          Int rbeg, Int rend,
+			  Matrix<Double>& sumwt,const Bool& dopsf,
+                          LofarCFStore& cfs);
+
+
+    Vector<Int> cfMap_p, conjCFMap_p;
+  };
+
+} //# NAMESPACE CASA - END
+
+#endif //
diff --git a/CEP/Imager/LofarFT/include/LofarFT/LofarWTerm.h b/CEP/Imager/LofarFT/include/LofarFT/LofarWTerm.h
index d31aa137b983246566c7f04b893a1ed4964b6b8a..7253064ca58311360971494c0ec96f0eef5cdb0e 100644
--- a/CEP/Imager/LofarFT/include/LofarFT/LofarWTerm.h
+++ b/CEP/Imager/LofarFT/include/LofarFT/LofarWTerm.h
@@ -71,12 +71,12 @@ namespace LOFAR {
       if (m_scale == 0.0) {
         return 0;
       }
-
       w = abs(w);
       uint estimate = floor(sqrt(w / m_scale));
       return w > upper(estimate) ? estimate + 1 : estimate;
     }
 
+
   private:
     //# Data members.
     double m_scale;
diff --git a/CEP/Imager/LofarFT/src/CMakeLists.txt b/CEP/Imager/LofarFT/src/CMakeLists.txt
index cf3040123333305a73bb45640e135ecfc11d8c80..462d30cd5ddd87c01184e203637d5486abc24c00 100644
--- a/CEP/Imager/LofarFT/src/CMakeLists.txt
+++ b/CEP/Imager/LofarFT/src/CMakeLists.txt
@@ -6,13 +6,17 @@ lofar_add_library(lofarft
   Package__Version.cc
   FFTCMatrix.cc
   LofarATerm.cc
+  LofarATermOld.cc
   LofarConvolutionFunction.cc
+  LofarConvolutionFunctionOld.cc
   LofarImager.cc
   LofarCFStore.cc
   LofarFTMachine.cc
+  LofarFTMachineOld.cc
   LofarWTerm.cc
 #  LofarVisibilityResamplerBase.cc
   LofarVisResampler.cc
+  LofarVisResamplerOld.cc
   LofarCubeSkyEquation.cc
 )
 
diff --git a/CEP/Imager/LofarFT/src/LofarATerm.cc b/CEP/Imager/LofarFT/src/LofarATerm.cc
index 3c5ec0147eec390e60bfd129cae21f8981ae2d37..5db25d2d58f4cf4d4e8eb2027c525223961b6efe 100644
--- a/CEP/Imager/LofarFT/src/LofarATerm.cc
+++ b/CEP/Imager/LofarFT/src/LofarATerm.cc
@@ -22,1017 +22,864 @@
 
 #include <lofar_config.h>
 #include <LofarFT/LofarATerm.h>
+
 #include <Common/LofarLogger.h>
 #include <Common/Exception.h>
+#include <BBSKernel/MeasurementAIPS.h>
+#include <ElementResponse/ElementResponse.h>
 
-#include <casa/OS/Path.h>
 #include <casa/Arrays/ArrayIter.h>
 #include <casa/Arrays/Cube.h>
 #include <coordinates/Coordinates/DirectionCoordinate.h>
-#include <measures/Measures/MeasTable.h>
 #include <measures/Measures/MeasConvert.h>
 #include <measures/Measures/MCDirection.h>
 #include <measures/Measures/MCPosition.h>
-#include <ms/MeasurementSets/MeasurementSet.h>
-#include <ms/MeasurementSets/MSAntenna.h>
-#include <ms/MeasurementSets/MSAntennaParse.h>
-#include <ms/MeasurementSets/MSAntennaColumns.h>
-#include <ms/MeasurementSets/MSDataDescription.h>
-#include <ms/MeasurementSets/MSDataDescColumns.h>
-#include <ms/MeasurementSets/MSField.h>
-#include <ms/MeasurementSets/MSFieldColumns.h>
-#include <ms/MeasurementSets/MSObservation.h>
-#include <ms/MeasurementSets/MSObsColumns.h>
-#include <ms/MeasurementSets/MSPolarization.h>
-#include <ms/MeasurementSets/MSPolColumns.h>
-#include <ms/MeasurementSets/MSSpectralWindow.h>
-#include <ms/MeasurementSets/MSSpWindowColumns.h>
-#include <ms/MeasurementSets/MSSelection.h>
 #include <synthesis/MeasurementComponents/SynthesisError.h>
 
-// DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
-#include <iomanip>
-// DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
-
 using namespace casa;
 
 namespace LOFAR
 {
-  LofarATerm::LofarATerm(const MeasurementSet& ms,
-                         const String& beamElementPath)
-  {
-    if (beamElementPath.empty()) {
-      m_coeffLBA.load(Path("element_beam_HAMAKER_LBA.coeff"));
-      m_coeffHBA.load(Path("element_beam_HAMAKER_HBA.coeff"));
-    } else {
-      m_coeffLBA.load(Path(beamElementPath + "/element_beam_HAMAKER_LBA.coeff"));
-      m_coeffHBA.load(Path(beamElementPath + "/element_beam_HAMAKER_HBA.coeff"));
-    }
-     //    m_coeffLBA.load(Path("element_beam_LBA.coeff"));
-     //    m_coeffHBA.load(Path("element_beam_HBA.coeff"));
-
-    initInstrument(ms);
-    initReferenceFreq(ms, 0);
-    initReferenceDirections(ms, 0);
-  }
-
-  vector<Cube<Complex> > LofarATerm::evaluate(const IPosition &shape,
-    const DirectionCoordinate &coordinates,
-    uint station,
-    const MEpoch &epoch,
-    const Vector<Double> &freq,
-    bool normalize) const
-  {
-    AlwaysAssert(station < m_instrument.nStations(), SynthesisError);
-    AlwaysAssert(shape[0] > 0 && shape[1] > 0, SynthesisError);
-    AlwaysAssert(freq.size() > 0, SynthesisError);
-
-    // Create conversion engine (from J2000 -> ITRF).
-    MDirection::Convert convertor = MDirection::Convert(MDirection::J2000,
-      MDirection::Ref(MDirection::ITRF,
-      MeasFrame(epoch, m_instrument.position())));
-
-    MVDirection mvRefDelay = convertor(m_refDelay).getValue();
-    Vector3 refDelay = {{mvRefDelay(0), mvRefDelay(1), mvRefDelay(2)}};
-
-    MVDirection mvRefTile = convertor(m_refTile).getValue();
-    Vector3 refTile = {{mvRefTile(0), mvRefTile(1), mvRefTile(2)}};
-
-    // Compute ITRF map.
-    LOG_INFO("LofarATerm::evaluate(): Computing ITRF map...");
-    Cube<double> mapITRF = computeITRFMap(coordinates, shape, convertor);
-    LOG_INFO("LofarATerm::evaluate(): Computing ITRF map... done.");
-
-    // Compute element beam response.
-    LOG_INFO("LofarATerm::evaluate(): Computing station response...");
-    Array<DComplex> response =
-      evaluateStationBeam(m_instrument.station(station), refDelay, refTile,
-        mapITRF, freq);
-
-    // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
-//    MDirection world;
-//    Vector<double> refPixel = coordinates.referencePixel();
 
-//    cout << "shape: " << shape << " ref. pixel: " << refPixel << endl;
-//    coordinates.toWorld(world, refPixel);
-
-//    casa::Quantum<casa::Vector<casa::Double> > refAngles = world.getAngle();
-//    double ra = refAngles.getBaseValue()(0);
-//    double dec = refAngles.getBaseValue()(1);
-//    cout << "ref. world: " << std::setprecision(17) << ra << " " << dec << endl;
-
-//    cout << "station: " << station << endl;
-//    cout << "freq: " << std::setprecision(17) << freq << endl;
-//    cout << "time: " << std::setprecision(17) << epoch.getValue().getTime("s") << endl;
-//    IPosition st(4, refPixel(0), refPixel(1), 0, 0);
-//    IPosition en(4, refPixel(0), refPixel(1), 3, freq.size() - 1);
-//    Array<DComplex> tmpResponse = response(st, en).nonDegenerate();
-//    cout << "response shape: " << tmpResponse.shape() << endl;
-//    cout << "response: " << endl << tmpResponse << endl;
+namespace
+{
+  Array<DComplex> computeFieldArrayFactor(const BBS::Station::ConstPtr &station,
+    uint idField, const LofarATerm::ITRFDirectionMap &map,
+    const Vector<Double> &freq, const Vector<Double> &reference);
 
-//    refPixel = 0.0;
-//    coordinates.toWorld(world, refPixel);
-//    refAngles = world.getAngle();
-//    ra = refAngles.getBaseValue()(0);
-//    dec = refAngles.getBaseValue()(1);
-//    cout << "0 world: " << std::setprecision(17) << ra << " " << dec << endl;
+  Cube<DComplex>
+  computeTileArrayFactor(const BBS::AntennaField::ConstPtr &field,
+    const LofarATerm::ITRFDirectionMap &map, const Vector<Double> &freq);
 
-//    st = IPosition(4, 0, 0, 0, 0);
-//    en = IPosition(4, 0, 0, 3, freq.size() - 1);
-//    Array<DComplex> tmpResponse2 = response(st, en).nonDegenerate();
-//    cout << "response shape: " << tmpResponse2.shape() << endl;
-//    cout << "response: " << endl << tmpResponse2 << endl;
+  struct ElementLBA;
+  struct ElementHBA;
 
-//    AlwaysAssert(false, SynthesisError);
-    // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
+  template <typename T_ELEMENT>
+  Array<DComplex>
+  computeElementResponse(const BBS::AntennaField::ConstPtr &field,
+    const LofarATerm::ITRFDirectionMap &map, const Vector<Double> &freq);
 
+  void rescale(Array<DComplex> &response);
+  void rescale(Cube<DComplex> &response);
 
+  vector<Cube<Complex> > asVector(Array<DComplex> &response);
+  vector<Matrix<Complex> > asVector(Cube<DComplex> &response);
+}
 
-    //Cyril
-    if(normalize)
+  LofarATerm::LofarATerm(const MeasurementSet& ms, const casa::Record& parameters)
+  {
+    itsInstrument = BBS::readInstrument(ms);
+    itsRefDelay = MDirection::Convert(BBS::readDelayReference(ms),
+      MDirection::J2000)();
+    itsRefTile = MDirection::Convert(BBS::readTileReference(ms),
+      MDirection::J2000)();
+     itsDirectionCoordinates = 0;
+    itsParameters = parameters;
+    itsApplyBeam = True;
+    if (itsParameters.fieldNumber("applyBeam") > -1) itsApplyBeam = itsParameters.asBool("applyBeam");
+    itsApplyIonosphere = False;
+    if (itsParameters.fieldNumber("applyIonosphere") > -1) itsApplyIonosphere = itsParameters.asBool("applyIonosphere");
+    if (itsApplyIonosphere) 
     {
-     response = this->normalize(response);
+      String parmdbname = ms.tableName() + "/instrument";
+      cout << parmdbname << endl;
+      initParmDB(parmdbname);
+      cout << cal_pp_names << endl;
     }
-    LOG_INFO("LofarATerm::evaluate(): Computing station response... done.");
+  }
 
-    // Convert an Array<DComplex> to a vector<Cube<Complex> >.
-    vector<Cube<Complex> > tmp;
-    tmp.reserve(freq.size());
-    for (ArrayIterator<DComplex> iter(response, 3);
-         !iter.pastEnd(); iter.next())
+  void LofarATerm::initParmDB(const casa::String  &parmdbname)
+  {
+    this->pdb = new LOFAR::BBS::ParmFacade (parmdbname);
+    std::string prefix = "Piercepoint:X:";
+    std::vector<std::string> v = this->pdb->getNames(prefix + "*");
+    this->cal_pp_names = Vector<String>(v.size());
+    this->cal_pp = Matrix<Double>(3,v.size());
+    this->tec_white = Vector<Double>(v.size());
+    
+    //strip cal_pp_names from prefix
+    for (uint i=0; i<v.size(); i++) 
     {
-      Cube<Complex> planef(iter.array().shape());
-      convertArray (planef, iter.array());
-      tmp.push_back(planef);
+      this->cal_pp_names[i] = v[i].substr(prefix.length());
     }
-
-    // if(normalize)
-    //   MDirection::Convert convertor = MDirection::Convert(MDirection::J2000, MDirection::Ref(MDirection::ITRF, MeasFrame(epoch, m_instrument.position())));
-    // mapITRF = computeITRFMap(coordinates, shape, convertor);
-    // Cube<double> mapITRF_center;
-    // DirectionCoordinate coordinates_center(coordinates);
-    //   Vector<Double> Refpix(2,0.);
-    //   coordinates_center.setReferencePixel(Refpix);
-    //   mapITRF_center = computeITRFMap(coordinates_center, IPosition(2,1,1), convertor);
-    //   for(uInt i = 0; i < freq.size(); ++i){
-    // 	{
-    //   evaluateStationBeam(m_instrument.station(station), refDelay, refTile,
-    //     mapITRF, freq);
-    // 	  Cube<Complex> gain(evaluateStationBeam(mapITRF_center, convertor(m_phaseReference), m_instrument.station(station), freq[i]));
-    // 	  Matrix<Complex> central_gain(gain.xzPlane(0));
-    // 	  // central_gain.resize(2,2,true); //resize does not work:
-    // 	  // Central gain  Axis Lengths: [1, 4]  (NB: Matrix in Row/Column order)
-    // 	  //   [(-0.0235668,-0.000796029), (-0.0345345,-0.000373378), (0.030112,0.000938836), (-0.0268743,-0.000258621)]
-    // 	  // Central gain  Axis Lengths: [2, 2]  (NB: Matrix in Row/Column order)
-    // 	  //   [(-0.0235668,-0.000796029), (-0.0345345,-0.000373378)
-    // 	  //    (0,0), (0,0)]
-    // 	  Matrix<Complex> central_gain_reform(central_gain.reform(IPosition(2,2,2)));
-    // 	  Matrix<Complex> central_gain_invert(invert(central_gain_reform));
-
-    // 	  //Cube<Complex> IM=beams[i];
-    // 	  for(uInt ii=0;ii<shape[0];++ii)
-    // 	    {
-    // 	      for(uInt jj=0;jj<shape[1];++jj)
-    // 	  	{
-    // 	  	  Cube<Complex> pixel(tmp[i](IPosition(3,ii,jj,0),IPosition(3,ii,jj,3)).copy());
-    // 		  // cout<<"================="<<pixel<<endl;
-    // 		  // cout<<"pixel"<<pixel<<endl;
-    // 	  	  Matrix<Complex> pixel_reform(pixel.reform(IPosition(2,2,2)));
-    // 		  // cout<<"pixel_reform"<<pixel_reform<<endl;
-    // 	  	  Matrix<Complex> pixel_product=product(central_gain_invert,pixel_reform);
-    // 		  // cout<<"pixel_product"<<pixel_product<<endl;
-    // 		  Matrix<Complex> pixel_product_reform(pixel_product.reform(IPosition(2,1,4)));
-    // 		  // cout<<"pixel_product_reform"<<pixel_product_reform<<endl;
-
-    // 		  for(uInt ind=0;ind<4;++ind){tmp[i](ii,jj,ind)=pixel_product_reform(0,ind);};
-    // 		    //beams[i](IPosition(3,ii,jj,0),IPosition(3,ii,jj,3))=pixel_product;
-    // 					//IM(ii,jj)=pixel_product;
-    // 	  	}
-    // 	    }
-    // 	};
-
-    return tmp;
+    
   }
-
-  Array<DComplex> LofarATerm::normalize(const Array<DComplex> &response)
-    const
-  {
-    const uint nX = response.shape()[0];
-    const uint nY = response.shape()[1];
-    const uint nFreq = response.shape()[3];
-    AlwaysAssert(response.shape()[2] == 4, SynthesisError);
-    AlwaysAssert(nX > 0 && nY > 0 && nFreq > 0, SynthesisError);
-
-    // Cast away const, to be able to use Array<T>::operator(IPosition,
-    // IPosition) to extract a slice (for reading).
-    Array<DComplex> &__response = const_cast<Array<DComplex>&>(response);
-
-    // Extract beam response for the central pixel at the central frequency.
-    IPosition start(4, floor(nX / 2.), floor(nY / 2.), 0, floor(nFreq / 2.));
-    IPosition end(4, floor(nX / 2.), floor(nY / 2.), 3, floor(nFreq / 2.));
-
-    // Use assignment operator to force a copy.
-    Vector<DComplex> factor;
-    factor = __response(start, end).nonDegenerate();
-
-    // Compute the inverse of the reponse.
-    Vector<DComplex> inverse(4);
-    DComplex determinant = factor(0) * factor(3) - factor(1) * factor(2);
-    inverse(0) = factor(3) / determinant;
-    inverse(1) = -factor(1) / determinant;
-    inverse(2) = -factor(2) / determinant;
-    inverse(3) = factor(0) / determinant;
-
-    // Multiply the beam response for all pixels, at all frequencies, by the
-    // computed inverse.
-    Array<DComplex> XX = __response(IPosition(4, 0, 0, 0, 0),
-      IPosition(4, nX - 1, nY - 1, 0, nFreq - 1));
-    Array<DComplex> XY = __response(IPosition(4, 0, 0, 1, 0),
-      IPosition(4, nX - 1, nY - 1, 1, nFreq - 1));
-    Array<DComplex> YX = __response(IPosition(4, 0, 0, 2, 0),
-      IPosition(4, nX - 1, nY - 1, 2, nFreq - 1));
-    Array<DComplex> YY = __response(IPosition(4, 0, 0, 3, 0),
-      IPosition(4, nX - 1, nY - 1, 3, nFreq - 1));
-
-    Array<DComplex> normal(response.shape());
-    Array<DComplex> nXX = normal(IPosition(4, 0, 0, 0, 0),
-      IPosition(4, nX - 1, nY - 1, 0, nFreq - 1));
-    Array<DComplex> nXY = normal(IPosition(4, 0, 0, 1, 0),
-      IPosition(4, nX - 1, nY - 1, 1, nFreq - 1));
-    Array<DComplex> nYX = normal(IPosition(4, 0, 0, 2, 0),
-      IPosition(4, nX - 1, nY - 1, 2, nFreq - 1));
-    Array<DComplex> nYY = normal(IPosition(4, 0, 0, 3, 0),
-      IPosition(4, nX - 1, nY - 1, 3, nFreq - 1));
-
-    nXX = inverse(0) * XX + inverse(1) * YX;
-    nXY = inverse(0) * XY + inverse(1) * YY;
-    nYX = inverse(2) * XX + inverse(3) * YX;
-    nYY = inverse(2) * XY + inverse(3) * YY;
-
-    return normal;
+  
+  void LofarATerm::setDirection(const casa::DirectionCoordinate &coordinates, const IPosition &shape) {
+    itsDirectionCoordinates = &coordinates;
+    itsShape = &shape;
   }
-
-  Array<DComplex> LofarATerm::evaluateElementBeam(const BeamCoeff &coeff,
-    const AntennaField &field,
-    const Cube<double> &map,
-    const Vector<double> &freq) const
+  
+  
+  void LofarATerm::setEpoch( const MEpoch &epoch )
   {
-    const Vector3 &p = field.axis(AntennaField::P);
-    const Vector3 &q = field.axis(AntennaField::Q);
-    const Vector3 &r = field.axis(AntennaField::R);
-
-    const uint nX = map.shape()[1];
-    const uint nY = map.shape()[2];
-    const uint nFreq = freq.shape()[0];
-
-    Array<DComplex> beam(IPosition(4, nX, nY, 4, nFreq), DComplex(0.0, 0.0));
-    for(uint j = 0; j < nY; ++j)
+    if (this->itsDirectionCoordinates) itsITRFDirectionMap = makeDirectionMap(*itsDirectionCoordinates, *itsShape, epoch);    
+    if (this->itsApplyIonosphere) 
     {
-      for(uint i = 0; i < nX; ++i)
-      {
-        if(map(0, i, j) == 0.0 && map(1, i, j) == 0.0 && map(2, i, j) == 0.0)
-        {
-          // Non-physical pixel.
-          continue;
-        }
+      this->time = epoch.get(casa::Unit("s")).getValue();
+      this->r0 = get_parmvalue("r_0");
+      this->beta = get_parmvalue("beta");
+      this->height = get_parmvalue("height");
+      for(uint i = 0; i < this->cal_pp_names.size(); ++i) {
+        this->cal_pp(0, i) = get_parmvalue("Piercepoint:X:" + this->cal_pp_names(i));
+        this->cal_pp(1, i) = get_parmvalue("Piercepoint:Y:" + this->cal_pp_names(i));
+        this->cal_pp(2, i) = get_parmvalue("Piercepoint:Z:" + this->cal_pp_names(i));
+        this->tec_white(i) = get_parmvalue("TECfit_white:0:" + this->cal_pp_names(i));
+      }
+    }
+  }
+  
+  double LofarATerm::get_parmvalue( std::string parmname ) 
+  {
+    double freq = 50e6; // the ionospheric parameters are not frequency dependent, so just pick an arbitrary frequency
+                         // this frequency should be within the range specified in the parmdbname
+                         // this range is again quite arbitrary
+    casa::Record result = this->pdb->getValues (parmname, freq, freq+0.5, 1.0, this->time, this->time + 0.5, 1.0 );
+    casa::Array<double> parmvalues;
+    result.subRecord(parmname).get("values", parmvalues);
+    return parmvalues(IPosition(2,0,0));
+  }
+  
+  
+  LofarATerm::ITRFDirectionMap
+  LofarATerm::makeDirectionMap(const DirectionCoordinate &coordinates,
+    const IPosition &shape, const MEpoch &epoch) const
+  {
+    AlwaysAssert(shape[0] > 0 && shape[1] > 0, SynthesisError);
 
-        // Compute the P and Q coordinate of the direction vector by projecting
-        // onto the positive P and Q axis.
-        double projectionP = map(0, i, j) * p[0] + map(1, i, j) * p[1] + map(2, i, j) * p[2];
-        double projectionQ = map(0, i, j) * q[0] + map(1, i, j) * q[1] + map(2, i, j) * q[2];
+    ITRFDirectionMap map;
+    map.epoch = epoch;
 
-        // Compute the inner product between the antenna field normal
-        // (R) and the direction vector to get the sine of the elevation
-        // (cosine of the zenith angle).
-        double sinEl = map(0, i, j) * r[0] + map(1, i, j) * r[1] + map(2, i, j) * r[2];
+    // Create conversion engine J2000 -> ITRF at epoch.
+    MDirection::Convert convertor = MDirection::Convert(MDirection::J2000,
+      MDirection::Ref(MDirection::ITRF,
+      MeasFrame(epoch, itsInstrument->position())));
 
-        double az = atan2(projectionP, projectionQ);
-        double el = asin(sinEl);
+    MVDirection mvRefDelay = convertor(itsRefDelay).getValue();
+    map.refDelay[0] = mvRefDelay(0);
+    map.refDelay[1] = mvRefDelay(1);
+    map.refDelay[2] = mvRefDelay(2);
 
-        // Evaluate beam.
-        // Correct azimuth for dipole orientation.
-        const double phi = az - 3.0 * C::pi_4;
+    MVDirection mvRefTile = convertor(itsRefTile).getValue();
+    map.refTile[0] = mvRefTile(0);
+    map.refTile[1] = mvRefTile(1);
+    map.refTile[2] = mvRefTile(2);
 
-        // NB: The model is parameterized in terms of zenith angle. The
-        // appropriate conversion is taken care of below.
-        const double theta = C::pi_2 - el;
+    MDirection world;
+    casa::Vector<Double> pixel = coordinates.referencePixel();
 
-        // Only compute the beam response for directions above the horizon.
-        if(theta < C::pi_2)
+    Cube<Double> mapITRF(3, shape[0], shape[1], 0.0);
+    for(pixel[1] = 0.0; pixel(1) < shape[1]; ++pixel[1])
+    {
+      for(pixel[0] = 0.0; pixel[0] < shape[0]; ++pixel[0])
+      {
+        // CoodinateSystem::toWorld()
+        // DEC range [-pi/2,pi/2]
+        // RA range [-pi,pi]
+        if(coordinates.toWorld(world, pixel))
         {
-          for(uint k = 0; k < nFreq; ++k)
-          {
-            // J-jones matrix (2x2 complex matrix)
-            DComplex J[2][2] = {{0.0, 0.0}, {0.0, 0.0}};
-
-            // NB: The model is parameterized in terms of a normalized
-            // frequency in the range [-1, 1]. The appropriate conversion is
-            // taken care of below.
-            const double normFreq = (freq[k] - coeff.center()) / coeff.width();
-
-            for(uint l = 0; l < coeff.nHarmonics(); ++l)
-            {
-              // Compute diagonal projection matrix P for the current
-              // harmonic.
-              DComplex P[2] = {0.0, 0.0};
-
-              DComplex inner[2];
-              for(int m = coeff.nPowerTheta() - 1; m >= 0; --m)
-              {
-                inner[0] = coeff(0, coeff.nPowerFreq() - 1, m, l);
-                inner[1] = coeff(1, coeff.nPowerFreq() - 1, m, l);
-
-                for(int n = coeff.nPowerFreq() - 2; n >= 0; --n)
-                {
-                  inner[0] = inner[0] * normFreq + coeff(0, n, m, l);
-                  inner[1] = inner[1] * normFreq + coeff(1, n, m, l);
-                }
-
-                P[0] = P[0] * theta + inner[0];
-                P[1] = P[1] * theta + inner[1];
-              }
-
-              // Compute Jones matrix for this harmonic by rotating P over
-              // kappa * phi and add it to the result.
-              const double kappa = ((l & 1) == 0 ? 1.0 : -1.0) * (2.0 * l + 1.0);
-              const double cphi = cos(kappa * phi);
-              const double sphi = sin(kappa * phi);
-
-              J[0][0] += cphi * P[0];
-              J[0][1] += -sphi * P[1];
-              J[1][0] += sphi * P[0];
-              J[1][1] += cphi * P[1];
-            }
-
-            beam(IPosition(4, i, j, 0, k)) = J[0][0];
-            beam(IPosition(4, i, j, 1, k)) = J[0][1];
-            beam(IPosition(4, i, j, 2, k)) = J[1][0];
-            beam(IPosition(4, i, j, 3, k)) = J[1][1];
-          }
+          MVDirection mvITRF = convertor(world).getValue();
+          mapITRF(0, pixel[0], pixel[1]) = mvITRF(0);
+          mapITRF(1, pixel[0], pixel[1]) = mvITRF(1);
+          mapITRF(2, pixel[0], pixel[1]) = mvITRF(2);
         }
       }
     }
 
-    return beam;
+    map.directions.reference(mapITRF);
+    return map;
   }
 
-  Array<DComplex> LofarATerm::evaluateStationBeam(const Station &station,
-    const Vector3 &refDelay,
-    const Vector3 &refTile,
-    const Cube<Double> &map,
-    const Vector<Double> &freq) const
+  vector<Cube<Complex> > LofarATerm::evaluate(uint idStation,
+    const Vector<Double> &freq,
+    const Vector<Double> &reference, bool normalize) const
   {
-    const uint nX = map.shape()[1];
-    const uint nY = map.shape()[2];
-    const uint nFreq = freq.shape()[0];
+    const ITRFDirectionMap &map = itsITRFDirectionMap;
+    AlwaysAssert(idStation < itsInstrument->nStations(), SynthesisError);
+    BBS::Station::ConstPtr station = itsInstrument->station(idStation);
+
+    const uint nX = map.directions.shape()[1];
+    const uint nY = map.directions.shape()[2];
+    const uint nFreq = freq.size();
 
-    uint countX = 0, countY = 0;
     Array<DComplex> E(IPosition(4, nX, nY, 4, nFreq), DComplex(0.0, 0.0));
-    for(uint i = 0; i < station.nField(); ++i)
+    for(uint i = 0; i < station->nField(); ++i)
     {
-      const AntennaField &field = station.field(i);
+      BBS::AntennaField::ConstPtr field = station->field(i);
 
       // Compute element beam.
-      LOG_INFO("LofarATerm::computeStationBeam: Computing element beam...");
-      Array<DComplex> beam;
-      if(field.isHBA())
+      Array<DComplex> element;
+      if(field->isHBA())
       {
-        beam = evaluateElementBeam(m_coeffHBA, field, map, freq);
+        element.reference(computeElementResponse<ElementHBA>(field, map, freq));
       }
       else
       {
-        beam = evaluateElementBeam(m_coeffLBA, field, map, freq);
+        element.reference(computeElementResponse<ElementLBA>(field, map, freq));
       }
-      LOG_INFO("LofarATerm::computeStationBeam: Computing element beam... done.");
 
-      if(field.isHBA())
-      {
-        // Compute tile array factor.
-        LOG_INFO("LofarATerm::computeStationBeam: Computing tile array factor...");
-        Cube<DComplex> tileAF = evaluateTileArrayFactor(field, refTile, map,
-          freq);
-        LOG_INFO("LofarATerm::computeStationBeam: Computing tile array factor... done.");
-
-        Array<DComplex> tileAF4 = tileAF.reform(IPosition(4, nX, nY, 1, nFreq));
+      // Compute antenna field array factor.
+      Array<DComplex> fieldAF = computeFieldArrayFactor(station, i, map, freq,
+        reference);
 
-        // Multiply the element beam by the tile array factor.
-        for(uint j = 0; j < 4; ++j)
-        {
-          IPosition start(4, 0, 0, j, 0);
-          IPosition end(4, nX - 1, nY - 1, j, nFreq - 1);
+      // Multiply the element or tile response by the antenna field array
+      // factor.
+      for(uint j = 0; j < 2; ++j)
+      {
+        IPosition start(4, 0, 0, j, 0);
+        IPosition end(4, nX - 1, nY - 1, j, nFreq - 1);
+        Array<DComplex> slice = E(start, end);
 
-          Array<DComplex> plane = beam(start, end);
-          plane *= tileAF4;
-        }
+        IPosition startAF(4, 0, 0, 0, 0);
+        IPosition endAF(4, nX - 1, nY - 1, 0, nFreq - 1);
+        slice += fieldAF(startAF, endAF) * element(start, end);
       }
 
-      LOG_INFO("LofarATerm::computeStationBeam: Computing station array factor...");
-
-      // Account for the case where the delay reference position is not equal to
-      // the field center (only applies to core HBA fields).
-      const Vector3 &fieldCenter = field.position();
-      MVPosition delayCenter = station.position().getValue();
-      Vector3 offsetShift = {{fieldCenter[0] - delayCenter(0),
-                              fieldCenter[1] - delayCenter(1),
-                              fieldCenter[2] - delayCenter(2)}};
-
-      // Compute field array factors.
-      Cube<DComplex> fieldAFX(nX, nY, nFreq, DComplex(0.0, 0.0));
-      Cube<DComplex> fieldAFY(nX, nY, nFreq, DComplex(0.0, 0.0));
-      Cube<DComplex> phase(nX, nY, nFreq, DComplex(0.0, 0.0));
-
-      for(uint j = 0; j < field.nElement(); ++j)
+      for(uint j = 2; j < 4; ++j)
       {
-        const AntennaField::Element &element = field.element(j);
-        if(element.flag[0] && element.flag[1])
-        {
-          continue;
-        }
+        IPosition startAF(4, 0, 0, 1, 0);
+        IPosition endAF(4, nX - 1, nY - 1, 1, nFreq - 1);
 
-        // Compute the offset relative to the delay center.
-        Vector3 offset = {{element.offset[0] + offsetShift[0],
-                           element.offset[1] + offsetShift[1],
-                           element.offset[2] + offsetShift[2]}};
+        IPosition start(4, 0, 0, j, 0);
+        IPosition end(4, nX - 1, nY - 1, j, nFreq - 1);
+        Array<DComplex> slice = E(start, end);
+        slice += fieldAF(startAF, endAF) * element(start, end);
+      }
+    }
 
-        // Compute the delay for a plane wave approaching from the delay
-        // reference direction with respect to the element position.
-        double delay0 = (refDelay[0] * offset[0] + refDelay[1] * offset[1]
-          + refDelay[2] * offset[2]) / casa::C::c;
-        double shift0 = C::_2pi * m_refFreq * delay0;
+    if(normalize)
+    {
+      rescale(E);
+    }
 
-        for(uint y = 0; y < nY; ++y)
-        {
-          for(uint x = 0; x < nX; ++x)
-          {
-            // Compute the delay for a plane wave approaching from the direction
-            // of interest with respect to the element position.
-            double delay = (map(0, x, y) * offset[0]
-                            + map(1, x, y) * offset[1]
-                            + map(2, x, y) * offset[2]) / casa::C::c;
-
-            for(uint k = 0; k < nFreq; ++k)
-            {
-              double shift = C::_2pi * freq[k] * delay - shift0;
-              phase(x, y, k) = DComplex(cos(shift), sin(shift));
-            }
-          }
-        }
+    return asVector(E);
+  }
 
-        if(!element.flag[0])
-        {
-          fieldAFX += phase;
-          ++countX;
-        }
+  
+  
+  vector<Matrix<Complex> > LofarATerm::evaluateStationScalarFactor(const uint idStation,
+    const Vector<Double> &freq, const Vector<Double> &reference, bool normalize) const
+  {
+    AlwaysAssert(idStation < itsInstrument->nStations(), SynthesisError);
 
-        if(!element.flag[1])
-        {
-          fieldAFY += phase;
-          ++countY;
-        }
-      }
+    const uint idPolarization = 0;
+    const ITRFDirectionMap &map = itsITRFDirectionMap;
 
-      LOG_INFO("LofarATerm::computeStationBeam: Computing station array factor... done.");
-      Array<DComplex> fieldAFX4 = fieldAFX.reform(IPosition(4, nX, nY, 1, nFreq));
-      for(uint k = 0; k < 2; ++k)
-      {
-        IPosition start(4, 0, 0, k, 0);
-        IPosition end(4, nX - 1, nY - 1, k, nFreq - 1);
-        Array<DComplex> plane = E(start, end);
-        plane += fieldAFX4 * beam(start, end);
-      }
+    const uint nX = map.directions.shape()[1];
+    const uint nY = map.directions.shape()[2];
+    const uint nFreq = freq.size();
+    
+    Cube<DComplex> SF(nX, nY, nFreq, DComplex(1.0, 0.0));
+    
+    if (itsApplyBeam) {
+      BBS::Station::ConstPtr station = itsInstrument->station(idStation);
+      Array<DComplex> AF = computeFieldArrayFactor(station, 0, map, freq,
+        reference);
 
-      Array<DComplex> fieldAFY4 = fieldAFY.reform(IPosition(4, nX, nY, 1, nFreq));
-      for(uint k = 2; k < 4; ++k)
+      if(station->nField() > 1)
       {
-        IPosition start(4, 0, 0, k, 0);
-        IPosition end(4, nX - 1, nY - 1, k, nFreq - 1);
-        Array<DComplex> plane = E(start, end);
-        plane += fieldAFY4 * beam(start, end);
+        AF += computeFieldArrayFactor(station, 1, map, freq, reference);
       }
-    } // fields
-
-    // Normalize.
-    if(countX > 0)
+      IPosition start(4, 0, 0, idPolarization, 0);
+      IPosition end(4, nX - 1, nY - 1, idPolarization, nFreq - 1);
+      Cube<DComplex> AFslice = AF(start, end).reform(IPosition(3, nX, nY, nFreq));
+      SF *= AFslice;
+    }
+    
+    if (itsApplyIonosphere) 
     {
-      IPosition start(4, 0, 0, 0, 0);
-      IPosition end(4, nX - 1, nY - 1, 1, nFreq - 1);
-      Array<DComplex> plane = E(start, end);
-      plane /= static_cast<Double>(countX);
+      Cube<DComplex> IF = evaluateIonosphere(idStation, freq);
+      SF *= IF;
     }
-
-    if(countY > 0)
+    
+    if(normalize)
     {
-      IPosition start(4, 0, 0, 2, 0);
-      IPosition end(4, nX - 1, nY - 1, 3, nFreq - 1);
-      Array<DComplex> plane = E(start, end);
-      plane /= static_cast<Double>(countY);
+      rescale(SF);
     }
-
-    return E;
+    
+    return asVector(SF);
   }
 
-  Cube<DComplex> LofarATerm::evaluateTileArrayFactor(const AntennaField &field,
-    const Vector3 &reference,
-    const Cube<Double> &map,
-    const Vector<Double> &freq) const
+  vector<Matrix<Complex> > LofarATerm::evaluateArrayFactor(uint idStation,
+    uint idPolarization,
+    const Vector<Double> &freq, const Vector<Double> &reference, bool normalize)
+    const
   {
-    const uint nX = map.shape()[1];
-    const uint nY = map.shape()[2];
-    const uint nFreq = freq.shape()[0];
+    AlwaysAssert(idStation < itsInstrument->nStations(), SynthesisError);
+    AlwaysAssert(idPolarization  < 2, SynthesisError);
 
-    Cube<DComplex> factor(nX, nY, nFreq, DComplex(0.0, 0.0));
-    for(uint y = 0; y < nY; ++y)
+    const ITRFDirectionMap &map = itsITRFDirectionMap;
+    BBS::Station::ConstPtr station = itsInstrument->station(idStation);
+    Array<DComplex> AF = computeFieldArrayFactor(station, 0, map, freq,
+      reference);
+
+    if(station->nField() > 1)
     {
-      for(uint x = 0; x < nX; ++x)
-      {
-        // Instead of computing a phase shift for the pointing direction and a
-        // phase shift for the direction of interest and then computing the
-        // difference, compute the resultant phase shift in one go. Here we make
-        // use of the relation a . b + a . c = a . (b + c). The sign of k is
-        // related to the sign of the phase shift.
-        double k[3];
-        k[0] = map(0, x, y) - reference[0];
-        k[1] = map(1, x, y) - reference[1];
-        k[2] = map(2, x, y) - reference[2];
+      AF += computeFieldArrayFactor(station, 1, map, freq, reference);
+    }
 
-        for(uint j = 0; j < field.nTileElement(); ++j)
-        {
-          // Compute the effective delay for a plane wave approaching from the
-          // direction of interest with respect to the position of element i
-          // when beam forming in the reference direction using time delays.
-          const Vector3 &offset = field.tileElement(j);
-          double delay = (k[0] * offset[0] + k[1] * offset[1] + k[2] * offset[2])
-            / C::c;
+    const uint nX = AF.shape()[0];
+    const uint nY = AF.shape()[1];
+    const uint nFreq = AF.shape()[3];
 
-          // Turn the delay into a phase shift.
-          for(uint k = 0; k < nFreq; ++k)
-          {
-            double shift = C::_2pi * freq[k] * delay;
-            factor(x, y, k) += DComplex(cos(shift), sin(shift));
-          }
-        }
-      }
-    }
+    IPosition start(4, 0, 0, idPolarization, 0);
+    IPosition end(4, nX - 1, nY - 1, idPolarization, nFreq - 1);
+    Cube<DComplex> slice = AF(start, end).reform(IPosition(3, nX, nY, nFreq));
 
-    // Normalize.
-    if(field.nTileElement() > 0)
+    if(normalize)
     {
-      factor /= static_cast<Double>(field.nTileElement());
+      rescale(slice);
     }
-
-    return factor;
+    return asVector(slice);
   }
 
-  Cube<double> LofarATerm::computeITRFMap(const DirectionCoordinate &coordinates,
-    const IPosition &shape,
-    MDirection::Convert convertor) const
+  vector<Cube<Complex> > LofarATerm::evaluateElementResponse(uint idStation,
+    uint idField, const Vector<Double> &freq,
+    bool normalize) const
   {
-    MDirection world;
-    Vector<double> pixel = coordinates.referencePixel();
-
-    Cube<double> map(3, shape[0], shape[1], 0.0);
-    for(pixel[1] = 0.0; pixel(1) < shape[1]; ++pixel[1])
-      {
-        for(pixel[0] = 0.0; pixel[0] < shape[0]; ++pixel[0])
-          {
-            // CoodinateSystem::toWorld()
-            // DEC range [-pi/2,pi/2]
-            // RA range [-pi,pi]
-            if(coordinates.toWorld(world, pixel))
-              {
-                MVDirection mvITRF(convertor(world).getValue());
-                map(0, pixel[0], pixel[1]) = mvITRF(0);
-                map(1, pixel[0], pixel[1]) = mvITRF(1);
-                map(2, pixel[0], pixel[1]) = mvITRF(2);
-              }
-          }
-      }
+    AlwaysAssert(idStation < itsInstrument->nStations(), SynthesisError);
 
-    return map;
-  }
+    const ITRFDirectionMap &map = itsITRFDirectionMap;
+    BBS::AntennaField::ConstPtr field =
+      itsInstrument->station(idStation)->field(idField);
 
-  void LofarATerm::initInstrument(const MeasurementSet &ms)
-  {
-    // Get station names and positions in ITRF coordinates.
-    ROMSAntennaColumns antenna(ms.antenna());
-    ROMSObservationColumns observation(ms.observation());
-    ASSERT(observation.nrow() > 0);
-    ASSERT(!observation.flagRow()(0));
-
-    // Get instrument name.
-    String name = observation.telescopeName()(0);
-
-    // Get station positions.
-    MVPosition centroid;
-    vector<Station> stations(antenna.nrow());
-    for(uint i = 0; i < stations.size(); ++i)
-      {
-        // Get station name and ITRF position.
-        MPosition position =
-          MPosition::Convert(antenna.positionMeas()(i),
-                             MPosition::ITRF)();
+    Array<DComplex> response;
+    if(field->isHBA())
+    {
+      response.reference(computeElementResponse<ElementHBA>(field, map, freq));
+    }
+    else
+    {
+      response.reference(computeElementResponse<ElementLBA>(field, map, freq));
+    }
 
-        // Store station information.
-        stations[i] = initStation(ms, i, antenna.name()(i), position);
+    if(normalize)
+    {
+      rescale(response);
+    }
 
-        ASSERT(stations[i].nField() > 0);
+    return asVector(response);
+  }
+
+  Cube<DComplex> LofarATerm::evaluateIonosphere(uint station, const Vector<Double> &freq) const
+  {
+    const ITRFDirectionMap &map = itsITRFDirectionMap;
+
+    const uint nX = map.directions.shape()[1];
+    const uint nY = map.directions.shape()[2];
+    
+    const uint nFreq = freq.size();
+    
+    const casa::MVPosition p = this->itsInstrument->station(station)->position().getValue();
+    
+    const double earth_ellipsoid_a = 6378137.0;
+    const double earth_ellipsoid_a2 = earth_ellipsoid_a*earth_ellipsoid_a;
+    const double earth_ellipsoid_b = 6356752.3142;
+    const double earth_ellipsoid_b2 = earth_ellipsoid_b*earth_ellipsoid_b;
+    const double earth_ellipsoid_e2 = (earth_ellipsoid_a2 - earth_ellipsoid_b2) / earth_ellipsoid_a2;
+ 
+    const double ion_ellipsoid_a = earth_ellipsoid_a + height;
+    const double ion_ellipsoid_a2_inv = 1.0 / (ion_ellipsoid_a * ion_ellipsoid_a);
+    const double ion_ellipsoid_b = earth_ellipsoid_b + height;
+    const double ion_ellipsoid_b2_inv = 1.0 / (ion_ellipsoid_b * ion_ellipsoid_b);
+
+
+    double x = p(0)/ion_ellipsoid_a;
+    double y = p(1)/ion_ellipsoid_a;
+    double z = p(2)/ion_ellipsoid_b;
+    double c = x*x + y*y + z*z - 1.0;
+    
+    casa::Cube<double> piercepoints(4, nX, nY, 0.0);
+   
+    for(uint i = 0 ; i < nX; ++i) 
+    {
+      for(uint j = 0 ; j < nY; ++j) 
+      {
+        double dx = map.directions(0,i,j) / ion_ellipsoid_a;
+        double dy = map.directions(1,i,j) / ion_ellipsoid_a;
+        double dz = map.directions(2,i,j) / ion_ellipsoid_b;
+        double a = dx*dx + dy*dy + dz*dz;
+        double b = x*dx + y*dy  + z*dz;
+        double alpha = (-b + std::sqrt(b*b - a*c))/a;
+        piercepoints(0, i, j) = p(0) + alpha*map.directions(0,i,j);
+        piercepoints(1, i, j) = p(1) + alpha*map.directions(1,i,j);
+        piercepoints(2, i, j) = p(2) + alpha*map.directions(2,i,j);
+        double normal_x = piercepoints(0, i, j) * ion_ellipsoid_a2_inv;
+        double normal_y = piercepoints(1, i, j) * ion_ellipsoid_a2_inv;
+        double normal_z = piercepoints(2, i, j) * ion_ellipsoid_b2_inv;
+        double norm_normal2 = normal_x*normal_x + normal_y*normal_y + normal_z*normal_z;
+        double norm_normal = std::sqrt(norm_normal2);
+        double sin_lat2 = normal_z*normal_z / norm_normal2;
+
+        double g = 1.0 - earth_ellipsoid_e2*sin_lat2;
+        double sqrt_g = std::sqrt(g);
+
+        double M = earth_ellipsoid_b2 / ( earth_ellipsoid_a * g * sqrt_g );
+        double N = earth_ellipsoid_a / sqrt_g;
+
+        double local_ion_ellipsoid_e2 = (M-N) / ((M+height)*sin_lat2 - N - height);
+        double local_ion_ellipsoid_a = (N+height) * std::sqrt(1.0 - local_ion_ellipsoid_e2*sin_lat2);
+        double local_ion_ellipsoid_b = local_ion_ellipsoid_a*std::sqrt(1.0 - local_ion_ellipsoid_e2);
+
+        double z_offset = ((1.0-earth_ellipsoid_e2)*N + height - (1.0-local_ion_ellipsoid_e2)*(N+height)) * std::sqrt(sin_lat2);
+
+        double x1 = p(0)/local_ion_ellipsoid_a;
+        double y1 = p(1)/local_ion_ellipsoid_a;
+        double z1 = (p(2)-z_offset)/local_ion_ellipsoid_b;
+        double c1 = x1*x1 + y1*y1 + z1*z1 - 1.0;
+
+        dx = map.directions(0,i,j) / local_ion_ellipsoid_a;
+        dy = map.directions(1,i,j) / local_ion_ellipsoid_a;
+        dz = map.directions(2,i,j) / local_ion_ellipsoid_b;
+        a = dx*dx + dy*dy + dz*dz;
+        b = x1*dx + y1*dy  + z1*dz;
+        alpha = (-b + std::sqrt(b*b - a*c1))/a;
+
+        piercepoints(0, i, j) = p(0) + alpha*map.directions(0,i,j);
+        piercepoints(1, i, j) = p(1) + alpha*map.directions(1,i,j);
+        piercepoints(2, i, j) = p(2) + alpha*map.directions(2,i,j);
+        normal_x = piercepoints(0, i, j) / (local_ion_ellipsoid_a * local_ion_ellipsoid_a);
+        normal_y = piercepoints(1, i, j) / (local_ion_ellipsoid_a * local_ion_ellipsoid_a);
+        normal_z = (piercepoints(2, i, j)-z_offset) / (local_ion_ellipsoid_b * local_ion_ellipsoid_b);
+        norm_normal2 = normal_x*normal_x + normal_y*normal_y + normal_z*normal_z;
+        norm_normal = std::sqrt(norm_normal2);
+        double cos_za_rec = norm_normal / (map.directions(0,i,j)*normal_x + map.directions(1,i,j)*normal_y + map.directions(2,i,j)*normal_z);
+        piercepoints(3, i, j) = cos_za_rec;
 
-        // Update ITRF centroid.
-        centroid += position.getValue();
       }
-
-    // Get the instrument position in ITRF coordinates, or use the centroid
-    // of the station positions if the instrument position is unknown.
-    MPosition position;
-    if(MeasTable::Observatory(position, name))
+    }  
+    Matrix<Double> tec(nX, nY, 0.0);
+    
+    Double r0sqr = r0 * r0;
+    Double beta_2 = 0.5 * beta;
+    for(uint i = 0 ; i < nX; ++i) 
+    {
+      for(uint j = 0 ; j < nY; ++j) 
       {
-        position = MPosition::Convert(position, MPosition::ITRF)();
+        for(uint k = 0 ; k < cal_pp_names.size(); ++k) 
+        {
+          Double dx = cal_pp(0, k) - piercepoints(0,i,j);
+          Double dy = cal_pp(1, k) - piercepoints(1,i,j);
+          Double dz = cal_pp(2, k) - piercepoints(2,i,j);
+          Double weight = pow((dx * dx + dy * dy + dz * dz) / r0sqr, beta_2);
+          tec(i,j) += weight * tec_white(k);
+        }
+        tec(i,j) *= (-0.5 * piercepoints(3,i,j));
       }
-    else
+    }
+    
+    Cube<DComplex> IF(nX, nY, nFreq, DComplex(0.0, 0.0));
+    for (uint i = 0; i < freq.size(); ++i)
+    {
+      Double a = (8.44797245e9 / freq[i]);
+      for(uint j = 0 ; j < nX; ++j) 
       {
-        LOG_INFO("LofarATerm initInstrument "
-                 "Instrument position unknown; will use centroid of stations.");
-        ASSERT(antenna.nrow() != 0);
-        centroid *= 1.0 / static_cast<double>(antenna.nrow());
-        position = MPosition(centroid, MPosition::ITRF);
+        for(uint k = 0 ; k < nY; ++k) 
+        {
+          Double phase = -tec(j,k) * a; // SvdT : removed minus sign, but still believe it should be there
+                                        // put it back again
+          IF(j,k,i) = DComplex(cos(phase), sin(phase));
+        }
       }
-
-    m_instrument = Instrument(name, position, stations.begin(), stations.end());
+    }
+    return IF;
   }
-
-  Station LofarATerm::initStation(const MeasurementSet &ms,
-    uint id,
-    const String &name,
-    const MPosition &position) const
+  
+  
+namespace
+{
+  vector<Cube<Complex> > asVector(Array<DComplex> &response)
   {
-    AlwaysAssert(ms.keywordSet().isDefined("LOFAR_ANTENNA_FIELD"), SynthesisError);
-
-    Table tab_field(ms.keywordSet().asTable("LOFAR_ANTENNA_FIELD"));
-    tab_field = tab_field(tab_field.col("ANTENNA_ID") == static_cast<Int>(id));
-
-    const uLong nFields = tab_field.nrow();
-    AlwaysAssert(nFields == 1 || nFields == 2, SynthesisError);
-
-    ROScalarColumn<String> c_name(tab_field, "NAME");
-    ROArrayQuantColumn<double> c_position(tab_field, "POSITION",
-                                          "m");
-    ROArrayQuantColumn<double> c_axes(tab_field, "COORDINATE_AXES",
-                                      "m");
-    ROArrayQuantColumn<double> c_tile_offset(tab_field,
-                                             "TILE_ELEMENT_OFFSET", "m");
-    ROArrayQuantColumn<double> c_offset(tab_field, "ELEMENT_OFFSET",
-                                        "m");
-    ROArrayColumn<Bool> c_flag(tab_field, "ELEMENT_FLAG");
-
-    AntennaField field[2];
-    for(uLong i = 0; i < nFields; ++i)
-      {
-        // Read antenna field center.
-        Vector<Quantum<double> > aips_position =
-          c_position(i);
-        ASSERT(aips_position.size() == 3);
-
-        Vector3 position = {{aips_position[0].getValue(),
-                             aips_position[1].getValue(),
-                             aips_position[2].getValue()}};
-
-        // Read antenna field coordinate axes.
-        Matrix<Quantum<double> > aips_axes = c_axes(i);
-        ASSERT(aips_axes.shape().isEqual(IPosition(2, 3, 3)));
-
-        Vector3 P = {{aips_axes(0, 0).getValue(), aips_axes(1, 0).getValue(),
-                      aips_axes(2, 0).getValue()}};
-        Vector3 Q = {{aips_axes(0, 1).getValue(), aips_axes(1, 1).getValue(),
-                      aips_axes(2, 1).getValue()}};
-        Vector3 R = {{aips_axes(0, 2).getValue(), aips_axes(1, 2).getValue(),
-                      aips_axes(2, 2).getValue()}};
-
-        // Store information as AntennaField.
-        field[i] = AntennaField(c_name(i), position, P, Q, R);
-
-        if(c_name(i) != "LBA")
-          {
-            // Read tile configuration for HBA antenna fields.
-            Matrix<Quantum<double> > aips_offset =
-              c_tile_offset(i);
-            ASSERT(aips_offset.nrow() == 3);
-
-            const uLong nElement = aips_offset.ncolumn();
-            for(uLong j = 0; j < nElement; ++j)
-              {
-                Vector3 offset = {{aips_offset(0, j).getValue(),
-                                   aips_offset(1, j).getValue(),
-                                   aips_offset(2, j).getValue()}};
-
-                field[i].appendTileElement(offset);
-              }
-          }
-
-        // Read element position offsets and flags.
-        Matrix<Quantum<double> > aips_offset = c_offset(i);
-        Matrix<Bool> aips_flag = c_flag(i);
+    vector<Cube<Complex> > result;
+    for(ArrayIterator<DComplex> it(response, 3); !it.pastEnd(); it.next())
+    {
+      Cube<Complex> slice(it.array().shape());
+      convertArray(slice, it.array());
+      result.push_back(slice);
+    }
 
-        const uLong nElement = aips_offset.ncolumn();
-        ASSERT(aips_offset.shape().isEqual(IPosition(2, 3, nElement)));
-        ASSERT(aips_flag.shape().isEqual(IPosition(2, 2, nElement)));
+    return result;
+  }
 
-        for(uLong j = 0; j < nElement; ++j)
-          {
-            AntennaField::Element element;
-            element.offset[0] = aips_offset(0, j).getValue();
-            element.offset[1] = aips_offset(1, j).getValue();
-            element.offset[2] = aips_offset(2, j).getValue();
-            element.flag[0] = aips_flag(0, j);
-            element.flag[1] = aips_flag(1, j);
-
-            field[i].appendElement(element);
-          }
-      }
+  vector<Matrix<Complex> > asVector(Cube<DComplex> &response)
+  {
+    vector<Matrix<Complex> > result;
+    for(ArrayIterator<DComplex> it(response, 2); !it.pastEnd(); it.next())
+    {
+      Matrix<Complex> slice(it.array().shape());
+      convertArray(slice, it.array());
+      result.push_back(slice);
+    }
 
-    return (nFields == 1 ? Station(name, position, field[0])
-            : Station(name, position, field[0], field[1]));
+    return result;
   }
 
-  void LofarATerm::initReferenceDirections(const MeasurementSet &ms,
-    uint idField)
+  void rescale(Array<DComplex> &response)
   {
-    // Get phase center as RA and DEC (J2000).
-    ROMSFieldColumns field(ms.field());
-    ASSERT(field.nrow() > idField);
-    ASSERT(!field.flagRow()(idField));
-
-    m_refDelay = MDirection::Convert(field.delayDirMeas(idField),
-      MDirection::J2000)();
+    AlwaysAssert(response.ndim() == 4, SynthesisError);
+    AlwaysAssert(response.shape()[2] == 4, SynthesisError);
 
-    // By default, the tile beam reference direction is assumed to be equal
-    // to the station beam reference direction (for backward compatibility,
-    // and for non-HBA measurements).
-    m_refTile = m_refDelay;
+    const uint nX = response.shape()[0];
+    const uint nY = response.shape()[1];
+    const uint centerX = nX / 2;
+    const uint centerY = nY / 2;
 
-    // The MeasurementSet class does not support LOFAR specific columns, so we
-    // use ROArrayMeasColumn to read the tile beam reference direction.
-    Table tab_field(ms.keywordSet().asTable("FIELD"));
-    static const String columnName = "LOFAR_TILE_BEAM_DIR";
-    if(tab_field.tableDesc().isColumn(columnName))
+    DComplex invXX, invXY, invYX, invYY;
+    for(ArrayIterator<DComplex> it(response, 3); !it.pastEnd(); it.next())
     {
-      ROArrayMeasColumn<MDirection> c_direction(tab_field, columnName);
-      if(c_direction.isDefined(idField))
+      Cube<DComplex> slice(it.array());
+
+      // Compute the inverse of the Jones matrix at the central pixel.
+      DComplex det = slice(centerX, centerY, 0) * slice(centerX, centerY, 3)
+        - slice(centerX, centerY, 1) * slice(centerX, centerY, 2);
+      invXX = slice(centerX, centerY, 3) / det;
+      invXY = -slice(centerX, centerY, 1) / det;
+      invYX = -slice(centerX, centerY, 2) / det;
+      invYY = slice(centerX, centerY, 0) / det;
+
+      // Apply the inverse of the Jones matrix at the central pixel to all Jones
+      // matrices.
+      Matrix<DComplex> XX = slice(IPosition(3, 0, 0, 0),
+        IPosition(3, nX - 1, nY - 1, 0)).nonDegenerate();
+      Matrix<DComplex> XY = slice(IPosition(3, 0, 0, 1),
+        IPosition(3, nX - 1, nY - 1, 1)).nonDegenerate();
+      Matrix<DComplex> YX = slice(IPosition(3, 0, 0, 2),
+        IPosition(3, nX - 1, nY - 1, 2)).nonDegenerate();
+      Matrix<DComplex> YY = slice(IPosition(3, 0, 0, 3),
+        IPosition(3, nX - 1, nY - 1, 3)).nonDegenerate();
+
+      DComplex normXX, normXY, normYX, normYY;
+      for(uint j = 0; j < nY; ++j)
       {
-        m_refTile = MDirection::Convert(c_direction(idField)(IPosition(1, 0)),
-          MDirection::J2000)();
+        for(uint i = 0; i < nX; ++i)
+        {
+          normXX = invXX * XX(i, j) + invXY * YX(i, j);
+          normXY = invXX * XY(i, j) + invXY * YY(i, j);
+          normYX = invYX * XX(i, j) + invYY * YX(i, j);
+          normYY = invYX * XY(i, j) + invYY * YY(i, j);
+
+          XX(i, j) = normXX;
+          XY(i, j) = normXY;
+          YX(i, j) = normYX;
+          YY(i, j) = normYY;
+        }
       }
     }
   }
 
-  void LofarATerm::initReferenceFreq(const MeasurementSet &ms,
-    uint idDataDescription)
-  {
-    // Read polarization id and spectral window id.
-    ROMSDataDescColumns desc(ms.dataDescription());
-    ASSERT(desc.nrow() > idDataDescription);
-    ASSERT(!desc.flagRow()(idDataDescription));
-
-    const uint idWindow = desc.spectralWindowId()(idDataDescription);
-
-    // Get spectral information.
-    ROMSSpWindowColumns window(ms.spectralWindow());
-    ASSERT(window.nrow() > idWindow);
-    ASSERT(!window.flagRow()(idWindow));
-
-    m_refFreq = window.refFrequency()(idWindow);
-  }
-
-  BeamCoeff::BeamCoeff()
-    :   m_center(0.0),
-        m_width(1.0)
+  void rescale(Cube<DComplex> &response)
   {
+    const uint centerX = response.shape()[0] / 2;
+    const uint centerY = response.shape()[1] / 2;
+    for(ArrayIterator<DComplex> it(response, 2); !it.pastEnd(); it.next())
+    {
+      Matrix<DComplex> slice(it.array());
+      slice /= slice(centerX, centerY);
+    }
   }
 
-  void BeamCoeff::load(const Path &path)
+  Array<DComplex> computeFieldArrayFactor(const BBS::Station::ConstPtr &station,
+    uint idField, const LofarATerm::ITRFDirectionMap &map,
+    const Vector<Double> &freq, const Vector<Double> &reference)
   {
-    // Open file.
-    String expandedPath = path.expandedName();
-    ifstream in(expandedPath.c_str());
-    cout<<"Reading "<<expandedPath<<endl;
-    if(!in)
-      {
-        THROW (Exception, "Unable to open beam coefficient file.");
-      }
+    const uint nX = map.directions.shape()[1];
+    const uint nY = map.directions.shape()[2];
+    const uint nFreq = freq.size();
 
-    // Read file header.
-    String header, token0, token1, token2, token3, token4, token5;
-    getline(in, header);
+    // Account for the case where the delay reference position is not equal to
+    // the field center (only applies to core HBA fields).
+    BBS::AntennaField::ConstPtr field = station->field(idField);
+    const BBS::Vector3 &fieldCenter = field->position();
 
-    uLong nElements, nHarmonics, nPowerTheta, nPowerFreq;
-    double freqAvg, freqRange;
+    MVPosition delayCenter = station->position().getValue();
+    BBS::Vector3 offsetShift = {{fieldCenter[0] - delayCenter(0),
+      fieldCenter[1] - delayCenter(1),
+      fieldCenter[2] - delayCenter(2)}};
 
-    istringstream iss(header);
-    iss >> token0 >> nElements >> token1 >> nHarmonics >> token2 >> nPowerTheta
-        >> token3 >> nPowerFreq >> token4 >> freqAvg >> token5 >> freqRange;
+    // Compute field array factors.
+    Array<DComplex> AF(IPosition(4, nX, nY, 2, nFreq), DComplex(0.0, 0.0));
+    Cube<DComplex> weight(nX, nY, nFreq);
 
-    if(!in || !iss || token0 != "d" || token1 != "k" || token2 != "pwrT"
-       || token3 != "pwrF" || token4 != "freqAvg" || token5 != "freqRange")
-      {
-        THROW (Exception, "Unable to parse header");
-      }
-
-    if(nElements * nHarmonics * nPowerTheta * nPowerFreq == 0)
+    for(uint i = 0; i < field->nElement(); ++i)
+    {
+      const BBS::AntennaField::Element &element = field->element(i);
+      if(element.flag[0] && element.flag[1])
       {
-        THROW (Exception, "The number of coefficients should be"
-               " larger than zero.");
+        continue;
       }
 
-    ASSERT(nElements == 2);
-    ASSERT(in.good());
+      // Compute the offset relative to the delay center.
+      BBS::Vector3 offset = {{element.offset[0] + offsetShift[0],
+        element.offset[1] + offsetShift[1],
+        element.offset[2] + offsetShift[2]}};
 
-    // Allocate coefficient matrix.
-    m_center = freqAvg;
-    m_width = freqRange;
-    m_coeff = Array<DComplex>(IPosition(4, 2, nPowerFreq, nPowerTheta, nHarmonics));
+      // Compute the delay for a plane wave approaching from the delay
+      // reference direction with respect to the element position.
+      double delay0 = (map.refDelay[0] * offset[0]
+        + map.refDelay[1] * offset[1]
+        + map.refDelay[2] * offset[2]) / C::c;
 
-    uLong nCoeff = 0;
-    while(in.good())
+      for(uint k = 0; k < nY; ++k)
       {
-        // Read line from file.
-        String line;
-        getline(in, line);
-
-        // Skip lines that contain only whitespace.
-        if(line.find_last_not_of(" ", String::npos) == String::npos)
-          {
-            continue;
-          }
-
-        // Parse line.
-        uLong element, harmonic, powerTheta, powerFreq;
-        double re, im;
-
-        iss.clear();
-        iss.str(line);
-        iss >> element >> harmonic >> powerTheta >> powerFreq >> re >> im;
+        for(uint j = 0; j < nX; ++j)
+        {
+          // Compute the delay for a plane wave approaching from the direction
+          // of interest with respect to the element position.
+          double delay = (map.directions(0, j, k) * offset[0]
+            + map.directions(1, j, k) * offset[1]
+            + map.directions(2, j, k) * offset[2]) / C::c;
 
-        if(!iss || element >= nElements || harmonic >= nHarmonics
-           || powerTheta >= nPowerTheta || powerFreq >= nPowerFreq)
+          for(uint l = 0; l < nFreq; ++l)
           {
-            THROW (Exception, "Error reading beam coefficient file.");
+            double shift = C::_2pi * (freq[l] * delay - reference[l] * delay0);
+            weight(j, k, l) = DComplex(cos(shift), sin(shift));
           }
-
-        // Store coefficient.
-        m_coeff(IPosition(4, element, powerFreq, powerTheta, harmonic)) = DComplex(re, im);
-
-        // Update coefficient counter.
-        ++nCoeff;
+        }
       }
 
-    if(!in.eof())
+      if(!element.flag[0])
       {
-        THROW (Exception, "Error reading beam coefficient"
-               " file.");
+        IPosition start(4, 0, 0, 0, 0);
+        IPosition end(4, nX - 1, nY - 1, 0, nFreq - 1);
+        Array<DComplex> slice = AF(start, end).reform(weight.shape());
+        slice += weight;
       }
 
-    if(nCoeff != nElements * nHarmonics * nPowerTheta * nPowerFreq)
+      if(!element.flag[1])
       {
-        THROW (Exception, "The number of coefficients"
-               " specified in the header does not match the number of coefficients"
-               " in the file.");
+        IPosition start(4, 0, 0, 1, 0);
+        IPosition end(4, nX - 1, nY - 1, 1, nFreq - 1);
+        Array<DComplex> slice = AF(start, end).reform(weight.shape());
+        slice += weight;
       }
-  }
-
-  AntennaField::AntennaField(const String &name,
-    const Vector3 &position,
-    const Vector3 &p,
-    const Vector3 &q,
-    const Vector3 &r)
-    :   m_name(name),
-        m_position(position)
-  {
-    m_axes[P] = p;
-    m_axes[Q] = q;
-    m_axes[R] = r;
-  }
-
-  const String &AntennaField::name() const
-  {
-    return m_name;
-  }
-
-  const Vector3 &AntennaField::position() const
-  {
-    return m_position;
-  }
+    }
 
-  const Vector3 &AntennaField::axis(Axis axis) const
-  {
-    return m_axes[axis];
-  }
+    // Normalize.
+    if(station->nActiveElement() > 0)
+    {
+      AF /= static_cast<Double>(station->nActiveElement());
+    }
 
-  Bool AntennaField::isHBA() const
-  {
-    return m_name != "LBA";
-  }
+    if(field->isHBA())
+    {
+      // Compute tile array factor.
+      Cube<DComplex> tileAF = computeTileArrayFactor(field, map, freq);
 
-  void AntennaField::appendTileElement(const Vector3 &offset)
-  {
-    m_tileElements.push_back(offset);
+      // Multiply the station array factor by the tile array factor.
+      for(uint i = 0; i < 2; ++i)
+      {
+        IPosition start(4, 0, 0, i, 0);
+        IPosition end(4, nX - 1, nY - 1, i, nFreq - 1);
+        Array<DComplex> slice = AF(start, end).reform(tileAF.shape());
+        slice *= tileAF;
+      }
+    }
+    
+    return AF;
   }
 
-  void AntennaField::appendElement(const Element &element)
+  Cube<DComplex>
+  computeTileArrayFactor(const BBS::AntennaField::ConstPtr &field,
+    const LofarATerm::ITRFDirectionMap &map, const Vector<Double> &freq)
   {
-    m_elements.push_back(element);
-  }
+    const uint nX = map.directions.shape()[1];
+    const uint nY = map.directions.shape()[2];
+    const uint nFreq = freq.size();
 
-  Station::Station(const String &name,
-    const MPosition &position)
-    :   m_name(name),
-        m_position(position)
-  {
-  }
+    Cube<DComplex> AF(nX, nY, nFreq, DComplex(0.0, 0.0));
+    for(uint j = 0; j < nY; ++j)
+    {
+      for(uint i = 0; i < nX; ++i)
+      {
+        // Instead of computing a phase shift for the pointing direction and a
+        // phase shift for the direction of interest and then computing the
+        // difference, compute the resultant phase shift in one go. Here we make
+        // use of the relation a . b + a . c = a . (b + c). The sign of k is
+        // related to the sign of the phase shift.
+        double k[3];
+        k[0] = map.directions(0, i, j) - map.refTile[0];
+        k[1] = map.directions(1, i, j) - map.refTile[1];
+        k[2] = map.directions(2, i, j) - map.refTile[2];
 
-  Station::Station(const String &name,
-    const MPosition &position,
-    const AntennaField &field0)
-    :   m_name(name),
-        m_position(position)
-  {
-    m_fields.push_back(field0);
-  }
+        for(uint l = 0; l < field->nTileElement(); ++l)
+        {
+          // Compute the effective delay for a plane wave approaching from the
+          // direction of interest with respect to the position of element i
+          // when beam forming in the reference direction using time delays.
+          const BBS::Vector3 &offset = field->tileElement(l);
+          double delay = (k[0] * offset[0] + k[1] * offset[1] + k[2]
+            * offset[2]) / C::c;
 
-  Station::Station(const String &name,
-    const MPosition &position,
-    const AntennaField &field0,
-    const AntennaField &field1)
-    :   m_name(name),
-        m_position(position)
-  {
-    m_fields.push_back(field0);
-    m_fields.push_back(field1);
-  }
+          // Turn the delay into a phase shift.
+          for(uint m = 0; m < nFreq; ++m)
+          {
+            double shift = C::_2pi * freq[m] * delay;
+            AF(i, j, m) += DComplex(cos(shift), sin(shift));
+          }
+        }
+      }
+    }
 
-  const String &Station::name() const
-  {
-    return m_name;
-  }
+    // Normalize.
+    if(field->nTileElement() > 0)
+    {
+      AF /= static_cast<Double>(field->nTileElement());
+    }
 
-  const MPosition &Station::position() const
-  {
-    return m_position;
+    return AF;
   }
 
-  bool Station::isPhasedArray() const
+  struct ElementLBA
   {
-    return !m_fields.empty();
-  }
+    static void response(double freq, double theta, double phi,
+      DComplex (&response)[2][2])
+    {
+      element_response_lba(freq, theta, phi, response);
+    }
+  };
 
-  uint Station::nField() const
+  struct ElementHBA
   {
-    return m_fields.size();
-  }
+    static void response(double freq, double theta, double phi,
+      DComplex (&response)[2][2])
+    {
+      element_response_hba(freq, theta, phi, response);
+    }
+  };
 
-  const AntennaField &Station::field(uint i) const
+  template <typename T_ELEMENT>
+  Array<DComplex>
+  computeElementResponse(const BBS::AntennaField::ConstPtr &field,
+    const LofarATerm::ITRFDirectionMap &map, const Vector<Double> &freq)
   {
-    return m_fields[i];
-  }
+    const BBS::Vector3 &p = field->axis(BBS::AntennaField::P);
+    const BBS::Vector3 &q = field->axis(BBS::AntennaField::Q);
+    const BBS::Vector3 &r = field->axis(BBS::AntennaField::R);
 
-  Instrument::Instrument(const String &name,
-    const MPosition &position)
-    :   m_name(name),
-        m_position(position)
-  {
-  }
+    const uint nX = map.directions.shape()[1];
+    const uint nY = map.directions.shape()[2];
+    const uint nFreq = freq.size();
 
-  const String &Instrument::name() const
-  {
-    return m_name;
-  }
+    DComplex J[2][2];
+    Array<DComplex> E(IPosition(4, nX, nY, 4, nFreq), DComplex(0.0, 0.0));
+    for(uint j = 0; j < nY; ++j)
+    {
+      for(uint i = 0; i < nX; ++i)
+      {
+        BBS::Vector3 target = {{map.directions(0, i, j),
+          map.directions(1, i, j), map.directions(2, i, j)}};
 
-  const MPosition &Instrument::position() const
-  {
-    return m_position;
-  }
+        // Check for non-physical directions (the image is square while the
+        // projected sky is circular, therefore some image pixels may map to
+        // invalid directions.
+        if(target[0] == 0.0 && target[1] == 0.0 && target[2] == 0.0)
+        {
+          continue;
+        }
 
-  uint Instrument::nStations() const
-  {
-    return m_stations.size();
-  }
+        // Compute the cross product of the NCP and the target direction. This
+        // yields a vector tangent to the celestial sphere at the target
+        // direction, pointing towards the East (the direction of +Y in the IAU
+        // definition, or positive right ascension).
+        BBS::Vector3 v1 = {{-target[1], target[0], 0.0}};
+        double normv1 = sqrt(v1[0] * v1[0] + v1[1] * v1[1] + v1[2] * v1[2]);
+        v1[0] /= normv1;
+        v1[1] /= normv1;
+        v1[2] /= normv1;
+
+        // Compute the cross product of the antenna field normal (R) and the
+        // target direction. This yields a vector tangent to the topocentric
+        // spherical coordinate system at the target direction, pointing towards
+        // the direction of positive phi (which runs East over North around the
+        // pseudo zenith).
+        BBS::Vector3 v2 = {{r[1] * target[2] - r[2] * target[1],
+          r[2] * target[0] - r[0] * target[2],
+          r[0] * target[1] - r[1] * target[0]}};
+        double normv2 = sqrt(v2[0] * v2[0] + v2[1] * v2[1] + v2[2] * v2[2]);
+        v2[0] /= normv2;
+        v2[1] /= normv2;
+        v2[2] /= normv2;
+
+        // Compute the cosine and sine of the parallactic angle, i.e. the angle
+        // between v1 and v2, both tangent to a latitude circle of their
+        // respective spherical coordinate systems.
+        double coschi = v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
+        double sinchi = (v1[1] * v2[2] - v1[2] * v2[1]) * target[0]
+          + (v1[2] * v2[0] - v1[0] * v2[2]) * target[1]
+          + (v1[0] * v2[1] - v1[1] * v2[0]) * target[2];
+
+        // The input coordinate system is a right handed system with its third
+        // axis along the direction of propagation (IAU +Z). The output
+        // coordinate system is right handed as well, but its third axis points
+        // in the direction of arrival (i.e. exactly opposite).
+        //
+        // Because the electromagnetic field is always perpendicular to the
+        // direction of propagation, we only need to relate the (X, Y) axes of
+        // the input system to the corresponding (theta, phi) axes of the output
+        // system.
+        //
+        // To this end, we first rotate the input system around its third axis
+        // to align the Y axis with the phi axis. The X and theta axis are
+        // parallel after this rotation, but point in opposite directions. To
+        // align the X axis with the theta axis, we flip it.
+        //
+        // The Jones matrix to align the Y axis with the phi axis when these are
+        // separated by an angle phi (measured counter-clockwise around the
+        // direction of propagation, looking towards the origin), is given by:
+        //
+        // [ cos(phi)  sin(phi)]
+        // [-sin(phi)  cos(phi)]
+        //
+        // Here, cos(phi) and sin(phi) can be computed directly, without having
+        // to compute phi first (see the computation of coschi and sinchi
+        // above).
+        //
+        // Now, sinchi as computed above is opposite to sin(phi), because the
+        // direction used in the computation is the direction of arrival instead
+        // of the direction of propagation. Therefore, the sign of sinchi needs
+        // to be reversed. Furthermore, as explained above, the X axis has to be
+        // flipped to align with the theta axis. The Jones matrix returned from
+        // this function is therefore given by:
+        //
+        // [-coschi  sinchi]
+        // [ sinchi  coschi]
 
-  const Station &Instrument::station(uint i) const
-  {
-    return m_stations[i];
-  }
+        // Compute the P and Q coordinate of the direction vector by projecting
+        // onto the positive P and Q axis.
+        double projectionP = target[0] * p[0] + target[1] * p[1] + target[2]
+          * p[2];
+        double projectionQ = target[0] * q[0] + target[1] * q[1] + target[2]
+          * q[2];
+
+        // Compute the inner product between the antenna field normal (R) and
+        // the direction vector to get the cosine of the zenith angle.
+        double projectionR = target[0] * r[0] + target[1] * r[1] + target[2]
+          * r[2];
+
+        double theta = acos(projectionR);
+        double phi = atan2(projectionQ, projectionP);
+
+        // The positive X dipole direction is SW of the reference orientation,
+        // which translates to a phi coordinate of 5/4*pi in the topocentric
+        // spherical coordinate system. The phi coordinate is corrected for this
+        // offset before evaluating the antenna model.
+        phi -= 5.0 * C::pi_4;
+
+        for(uint k = 0; k < nFreq; ++k)
+        {
+          T_ELEMENT::response(freq[k], theta, phi, J);
 
-  const Station &Instrument::station(const String &name) const
-  {
-    map<String, uint>::const_iterator it = m_index.find(name);
-    if(it == m_index.end())
-      {
-        THROW (Exception, "Unknown station: " + name);
+          E(IPosition(4, i, j, 0, k)) = J[0][0] * -coschi + J[0][1] * sinchi;
+          E(IPosition(4, i, j, 1, k)) = J[0][0] *  sinchi + J[0][1] * coschi;
+          E(IPosition(4, i, j, 2, k)) = J[1][0] * -coschi + J[1][1] * sinchi;
+          E(IPosition(4, i, j, 3, k)) = J[1][0] *  sinchi + J[1][1] * coschi;
+        }
       }
+    }
 
-    return m_stations[it->second];
+    return E;
   }
+  
+} // unnamed namespace
 
-  void Instrument::append(const Station &station)
-  {
-    m_stations.push_back(station);
-  }
 } // namespace LOFAR
diff --git a/CEP/Imager/LofarFT/src/LofarATermOld.cc b/CEP/Imager/LofarFT/src/LofarATermOld.cc
new file mode 100644
index 0000000000000000000000000000000000000000..c2704db3f398a24b29578769cdff1773f3268791
--- /dev/null
+++ b/CEP/Imager/LofarFT/src/LofarATermOld.cc
@@ -0,0 +1,1038 @@
+//# LofarATermOld.cc: Compute the LOFAR beam response on the sky.
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#include <lofar_config.h>
+#include <LofarFT/LofarATermOld.h>
+#include <Common/LofarLogger.h>
+#include <Common/Exception.h>
+
+#include <casa/OS/Path.h>
+#include <casa/Arrays/ArrayIter.h>
+#include <casa/Arrays/Cube.h>
+#include <coordinates/Coordinates/DirectionCoordinate.h>
+#include <measures/Measures/MeasTable.h>
+#include <measures/Measures/MeasConvert.h>
+#include <measures/Measures/MCDirection.h>
+#include <measures/Measures/MCPosition.h>
+#include <ms/MeasurementSets/MeasurementSet.h>
+#include <ms/MeasurementSets/MSAntenna.h>
+#include <ms/MeasurementSets/MSAntennaParse.h>
+#include <ms/MeasurementSets/MSAntennaColumns.h>
+#include <ms/MeasurementSets/MSDataDescription.h>
+#include <ms/MeasurementSets/MSDataDescColumns.h>
+#include <ms/MeasurementSets/MSField.h>
+#include <ms/MeasurementSets/MSFieldColumns.h>
+#include <ms/MeasurementSets/MSObservation.h>
+#include <ms/MeasurementSets/MSObsColumns.h>
+#include <ms/MeasurementSets/MSPolarization.h>
+#include <ms/MeasurementSets/MSPolColumns.h>
+#include <ms/MeasurementSets/MSSpectralWindow.h>
+#include <ms/MeasurementSets/MSSpWindowColumns.h>
+#include <ms/MeasurementSets/MSSelection.h>
+#include <synthesis/MeasurementComponents/SynthesisError.h>
+
+// DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
+#include <iomanip>
+// DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
+
+using namespace casa;
+
+namespace LOFAR
+{
+  LofarATermOld::LofarATermOld(const MeasurementSet& ms,
+                         const String& beamElementPath)
+  {
+    if (beamElementPath.empty()) {
+      m_coeffLBA.load(Path("element_beam_HAMAKER_LBA.coeff"));
+      m_coeffHBA.load(Path("element_beam_HAMAKER_HBA.coeff"));
+    } else {
+      m_coeffLBA.load(Path(beamElementPath + "/element_beam_HAMAKER_LBA.coeff"));
+      m_coeffHBA.load(Path(beamElementPath + "/element_beam_HAMAKER_HBA.coeff"));
+    }
+     //    m_coeffLBA.load(Path("element_beam_LBA.coeff"));
+     //    m_coeffHBA.load(Path("element_beam_HBA.coeff"));
+
+    initInstrument(ms);
+    initReferenceFreq(ms, 0);
+    initReferenceDirections(ms, 0);
+  }
+
+  vector<Cube<Complex> > LofarATermOld::evaluate(const IPosition &shape,
+    const DirectionCoordinate &coordinates,
+    uint station,
+    const MEpoch &epoch,
+    const Vector<Double> &freq,
+    bool normalize) const
+  {
+    AlwaysAssert(station < m_instrument.nStations(), SynthesisError);
+    AlwaysAssert(shape[0] > 0 && shape[1] > 0, SynthesisError);
+    AlwaysAssert(freq.size() > 0, SynthesisError);
+
+    // Create conversion engine (from J2000 -> ITRF).
+    MDirection::Convert convertor = MDirection::Convert(MDirection::J2000,
+      MDirection::Ref(MDirection::ITRF,
+      MeasFrame(epoch, m_instrument.position())));
+
+    MVDirection mvRefDelay = convertor(m_refDelay).getValue();
+    Vector3 refDelay = {{mvRefDelay(0), mvRefDelay(1), mvRefDelay(2)}};
+
+    MVDirection mvRefTile = convertor(m_refTile).getValue();
+    Vector3 refTile = {{mvRefTile(0), mvRefTile(1), mvRefTile(2)}};
+
+    // Compute ITRF map.
+    LOG_INFO("LofarATermOld::evaluate(): Computing ITRF map...");
+    Cube<double> mapITRF = computeITRFMap(coordinates, shape, convertor);
+    LOG_INFO("LofarATermOld::evaluate(): Computing ITRF map... done.");
+
+    // Compute element beam response.
+    LOG_INFO("LofarATermOld::evaluate(): Computing station response...");
+    Array<DComplex> response =
+      evaluateStationBeam(m_instrument.station(station), refDelay, refTile,
+        mapITRF, freq);
+
+    // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
+//    MDirection world;
+//    Vector<double> refPixel = coordinates.referencePixel();
+
+//    cout << "shape: " << shape << " ref. pixel: " << refPixel << endl;
+//    coordinates.toWorld(world, refPixel);
+
+//    casa::Quantum<casa::Vector<casa::Double> > refAngles = world.getAngle();
+//    double ra = refAngles.getBaseValue()(0);
+//    double dec = refAngles.getBaseValue()(1);
+//    cout << "ref. world: " << std::setprecision(17) << ra << " " << dec << endl;
+
+//    cout << "station: " << station << endl;
+//    cout << "freq: " << std::setprecision(17) << freq << endl;
+//    cout << "time: " << std::setprecision(17) << epoch.getValue().getTime("s") << endl;
+//    IPosition st(4, refPixel(0), refPixel(1), 0, 0);
+//    IPosition en(4, refPixel(0), refPixel(1), 3, freq.size() - 1);
+//    Array<DComplex> tmpResponse = response(st, en).nonDegenerate();
+//    cout << "response shape: " << tmpResponse.shape() << endl;
+//    cout << "response: " << endl << tmpResponse << endl;
+
+//    refPixel = 0.0;
+//    coordinates.toWorld(world, refPixel);
+//    refAngles = world.getAngle();
+//    ra = refAngles.getBaseValue()(0);
+//    dec = refAngles.getBaseValue()(1);
+//    cout << "0 world: " << std::setprecision(17) << ra << " " << dec << endl;
+
+//    st = IPosition(4, 0, 0, 0, 0);
+//    en = IPosition(4, 0, 0, 3, freq.size() - 1);
+//    Array<DComplex> tmpResponse2 = response(st, en).nonDegenerate();
+//    cout << "response shape: " << tmpResponse2.shape() << endl;
+//    cout << "response: " << endl << tmpResponse2 << endl;
+
+//    AlwaysAssert(false, SynthesisError);
+    // DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG DEBUG
+
+
+
+    //Cyril
+    if(normalize)
+    {
+     response = this->normalize(response);
+    }
+    LOG_INFO("LofarATermOld::evaluate(): Computing station response... done.");
+
+    // Convert an Array<DComplex> to a vector<Cube<Complex> >.
+    vector<Cube<Complex> > tmp;
+    tmp.reserve(freq.size());
+    for (ArrayIterator<DComplex> iter(response, 3);
+         !iter.pastEnd(); iter.next())
+    {
+      Cube<Complex> planef(iter.array().shape());
+      convertArray (planef, iter.array());
+      tmp.push_back(planef);
+    }
+
+    // if(normalize)
+    //   MDirection::Convert convertor = MDirection::Convert(MDirection::J2000, MDirection::Ref(MDirection::ITRF, MeasFrame(epoch, m_instrument.position())));
+    // mapITRF = computeITRFMap(coordinates, shape, convertor);
+    // Cube<double> mapITRF_center;
+    // DirectionCoordinate coordinates_center(coordinates);
+    //   Vector<Double> Refpix(2,0.);
+    //   coordinates_center.setReferencePixel(Refpix);
+    //   mapITRF_center = computeITRFMap(coordinates_center, IPosition(2,1,1), convertor);
+    //   for(uInt i = 0; i < freq.size(); ++i){
+    // 	{
+    //   evaluateStationBeam(m_instrument.station(station), refDelay, refTile,
+    //     mapITRF, freq);
+    // 	  Cube<Complex> gain(evaluateStationBeam(mapITRF_center, convertor(m_phaseReference), m_instrument.station(station), freq[i]));
+    // 	  Matrix<Complex> central_gain(gain.xzPlane(0));
+    // 	  // central_gain.resize(2,2,true); //resize does not work:
+    // 	  // Central gain  Axis Lengths: [1, 4]  (NB: Matrix in Row/Column order)
+    // 	  //   [(-0.0235668,-0.000796029), (-0.0345345,-0.000373378), (0.030112,0.000938836), (-0.0268743,-0.000258621)]
+    // 	  // Central gain  Axis Lengths: [2, 2]  (NB: Matrix in Row/Column order)
+    // 	  //   [(-0.0235668,-0.000796029), (-0.0345345,-0.000373378)
+    // 	  //    (0,0), (0,0)]
+    // 	  Matrix<Complex> central_gain_reform(central_gain.reform(IPosition(2,2,2)));
+    // 	  Matrix<Complex> central_gain_invert(invert(central_gain_reform));
+
+    // 	  //Cube<Complex> IM=beams[i];
+    // 	  for(uInt ii=0;ii<shape[0];++ii)
+    // 	    {
+    // 	      for(uInt jj=0;jj<shape[1];++jj)
+    // 	  	{
+    // 	  	  Cube<Complex> pixel(tmp[i](IPosition(3,ii,jj,0),IPosition(3,ii,jj,3)).copy());
+    // 		  // cout<<"================="<<pixel<<endl;
+    // 		  // cout<<"pixel"<<pixel<<endl;
+    // 	  	  Matrix<Complex> pixel_reform(pixel.reform(IPosition(2,2,2)));
+    // 		  // cout<<"pixel_reform"<<pixel_reform<<endl;
+    // 	  	  Matrix<Complex> pixel_product=product(central_gain_invert,pixel_reform);
+    // 		  // cout<<"pixel_product"<<pixel_product<<endl;
+    // 		  Matrix<Complex> pixel_product_reform(pixel_product.reform(IPosition(2,1,4)));
+    // 		  // cout<<"pixel_product_reform"<<pixel_product_reform<<endl;
+
+    // 		  for(uInt ind=0;ind<4;++ind){tmp[i](ii,jj,ind)=pixel_product_reform(0,ind);};
+    // 		    //beams[i](IPosition(3,ii,jj,0),IPosition(3,ii,jj,3))=pixel_product;
+    // 					//IM(ii,jj)=pixel_product;
+    // 	  	}
+    // 	    }
+    // 	};
+
+    return tmp;
+  }
+
+  Array<DComplex> LofarATermOld::normalize(const Array<DComplex> &response)
+    const
+  {
+    const uint nX = response.shape()[0];
+    const uint nY = response.shape()[1];
+    const uint nFreq = response.shape()[3];
+    AlwaysAssert(response.shape()[2] == 4, SynthesisError);
+    AlwaysAssert(nX > 0 && nY > 0 && nFreq > 0, SynthesisError);
+
+    // Cast away const, to be able to use Array<T>::operator(IPosition,
+    // IPosition) to extract a slice (for reading).
+    Array<DComplex> &__response = const_cast<Array<DComplex>&>(response);
+
+    // Extract beam response for the central pixel at the central frequency.
+    IPosition start(4, floor(nX / 2.), floor(nY / 2.), 0, floor(nFreq / 2.));
+    IPosition end(4, floor(nX / 2.), floor(nY / 2.), 3, floor(nFreq / 2.));
+
+    // Use assignment operator to force a copy.
+    Vector<DComplex> factor;
+    factor = __response(start, end).nonDegenerate();
+
+    // Compute the inverse of the reponse.
+    Vector<DComplex> inverse(4);
+    DComplex determinant = factor(0) * factor(3) - factor(1) * factor(2);
+    inverse(0) = factor(3) / determinant;
+    inverse(1) = -factor(1) / determinant;
+    inverse(2) = -factor(2) / determinant;
+    inverse(3) = factor(0) / determinant;
+
+    // Multiply the beam response for all pixels, at all frequencies, by the
+    // computed inverse.
+    Array<DComplex> XX = __response(IPosition(4, 0, 0, 0, 0),
+      IPosition(4, nX - 1, nY - 1, 0, nFreq - 1));
+    Array<DComplex> XY = __response(IPosition(4, 0, 0, 1, 0),
+      IPosition(4, nX - 1, nY - 1, 1, nFreq - 1));
+    Array<DComplex> YX = __response(IPosition(4, 0, 0, 2, 0),
+      IPosition(4, nX - 1, nY - 1, 2, nFreq - 1));
+    Array<DComplex> YY = __response(IPosition(4, 0, 0, 3, 0),
+      IPosition(4, nX - 1, nY - 1, 3, nFreq - 1));
+
+    Array<DComplex> normal(response.shape());
+    Array<DComplex> nXX = normal(IPosition(4, 0, 0, 0, 0),
+      IPosition(4, nX - 1, nY - 1, 0, nFreq - 1));
+    Array<DComplex> nXY = normal(IPosition(4, 0, 0, 1, 0),
+      IPosition(4, nX - 1, nY - 1, 1, nFreq - 1));
+    Array<DComplex> nYX = normal(IPosition(4, 0, 0, 2, 0),
+      IPosition(4, nX - 1, nY - 1, 2, nFreq - 1));
+    Array<DComplex> nYY = normal(IPosition(4, 0, 0, 3, 0),
+      IPosition(4, nX - 1, nY - 1, 3, nFreq - 1));
+
+    nXX = inverse(0) * XX + inverse(1) * YX;
+    nXY = inverse(0) * XY + inverse(1) * YY;
+    nYX = inverse(2) * XX + inverse(3) * YX;
+    nYY = inverse(2) * XY + inverse(3) * YY;
+
+    return normal;
+  }
+
+  Array<DComplex> LofarATermOld::evaluateElementBeam(const BeamCoeff &coeff,
+    const AntennaField &field,
+    const Cube<double> &map,
+    const Vector<double> &freq) const
+  {
+    const Vector3 &p = field.axis(AntennaField::P);
+    const Vector3 &q = field.axis(AntennaField::Q);
+    const Vector3 &r = field.axis(AntennaField::R);
+
+    const uint nX = map.shape()[1];
+    const uint nY = map.shape()[2];
+    const uint nFreq = freq.shape()[0];
+
+    Array<DComplex> beam(IPosition(4, nX, nY, 4, nFreq), DComplex(0.0, 0.0));
+    for(uint j = 0; j < nY; ++j)
+    {
+      for(uint i = 0; i < nX; ++i)
+      {
+        if(map(0, i, j) == 0.0 && map(1, i, j) == 0.0 && map(2, i, j) == 0.0)
+        {
+          // Non-physical pixel.
+          continue;
+        }
+
+        // Compute the P and Q coordinate of the direction vector by projecting
+        // onto the positive P and Q axis.
+        double projectionP = map(0, i, j) * p[0] + map(1, i, j) * p[1] + map(2, i, j) * p[2];
+        double projectionQ = map(0, i, j) * q[0] + map(1, i, j) * q[1] + map(2, i, j) * q[2];
+
+        // Compute the inner product between the antenna field normal
+        // (R) and the direction vector to get the sine of the elevation
+        // (cosine of the zenith angle).
+        double sinEl = map(0, i, j) * r[0] + map(1, i, j) * r[1] + map(2, i, j) * r[2];
+
+        double az = atan2(projectionP, projectionQ);
+        double el = asin(sinEl);
+
+        // Evaluate beam.
+        // Correct azimuth for dipole orientation.
+        const double phi = az - 3.0 * C::pi_4;
+
+        // NB: The model is parameterized in terms of zenith angle. The
+        // appropriate conversion is taken care of below.
+        const double theta = C::pi_2 - el;
+
+        // Only compute the beam response for directions above the horizon.
+        if(theta < C::pi_2)
+        {
+          for(uint k = 0; k < nFreq; ++k)
+          {
+            // J-jones matrix (2x2 complex matrix)
+            DComplex J[2][2] = {{0.0, 0.0}, {0.0, 0.0}};
+
+            // NB: The model is parameterized in terms of a normalized
+            // frequency in the range [-1, 1]. The appropriate conversion is
+            // taken care of below.
+            const double normFreq = (freq[k] - coeff.center()) / coeff.width();
+
+            for(uint l = 0; l < coeff.nHarmonics(); ++l)
+            {
+              // Compute diagonal projection matrix P for the current
+              // harmonic.
+              DComplex P[2] = {0.0, 0.0};
+
+              DComplex inner[2];
+              for(int m = coeff.nPowerTheta() - 1; m >= 0; --m)
+              {
+                inner[0] = coeff(0, coeff.nPowerFreq() - 1, m, l);
+                inner[1] = coeff(1, coeff.nPowerFreq() - 1, m, l);
+
+                for(int n = coeff.nPowerFreq() - 2; n >= 0; --n)
+                {
+                  inner[0] = inner[0] * normFreq + coeff(0, n, m, l);
+                  inner[1] = inner[1] * normFreq + coeff(1, n, m, l);
+                }
+
+                P[0] = P[0] * theta + inner[0];
+                P[1] = P[1] * theta + inner[1];
+              }
+
+              // Compute Jones matrix for this harmonic by rotating P over
+              // kappa * phi and add it to the result.
+              const double kappa = ((l & 1) == 0 ? 1.0 : -1.0) * (2.0 * l + 1.0);
+              const double cphi = cos(kappa * phi);
+              const double sphi = sin(kappa * phi);
+
+              J[0][0] += cphi * P[0];
+              J[0][1] += -sphi * P[1];
+              J[1][0] += sphi * P[0];
+              J[1][1] += cphi * P[1];
+            }
+
+            beam(IPosition(4, i, j, 0, k)) = J[0][0];
+            beam(IPosition(4, i, j, 1, k)) = J[0][1];
+            beam(IPosition(4, i, j, 2, k)) = J[1][0];
+            beam(IPosition(4, i, j, 3, k)) = J[1][1];
+          }
+        }
+      }
+    }
+
+    return beam;
+  }
+
+  Array<DComplex> LofarATermOld::evaluateStationBeam(const Station &station,
+    const Vector3 &refDelay,
+    const Vector3 &refTile,
+    const Cube<Double> &map,
+    const Vector<Double> &freq) const
+  {
+    const uint nX = map.shape()[1];
+    const uint nY = map.shape()[2];
+    const uint nFreq = freq.shape()[0];
+
+    uint countX = 0, countY = 0;
+    Array<DComplex> E(IPosition(4, nX, nY, 4, nFreq), DComplex(0.0, 0.0));
+    for(uint i = 0; i < station.nField(); ++i)
+    {
+      const AntennaField &field = station.field(i);
+
+      // Compute element beam.
+      LOG_INFO("LofarATermOld::computeStationBeam: Computing element beam...");
+      Array<DComplex> beam;
+      if(field.isHBA())
+      {
+        beam = evaluateElementBeam(m_coeffHBA, field, map, freq);
+      }
+      else
+      {
+        beam = evaluateElementBeam(m_coeffLBA, field, map, freq);
+      }
+      LOG_INFO("LofarATermOld::computeStationBeam: Computing element beam... done.");
+
+      if(field.isHBA())
+      {
+        // Compute tile array factor.
+        LOG_INFO("LofarATermOld::computeStationBeam: Computing tile array factor...");
+        Cube<DComplex> tileAF = evaluateTileArrayFactor(field, refTile, map,
+          freq);
+        LOG_INFO("LofarATermOld::computeStationBeam: Computing tile array factor... done.");
+
+        Array<DComplex> tileAF4 = tileAF.reform(IPosition(4, nX, nY, 1, nFreq));
+
+        // Multiply the element beam by the tile array factor.
+        for(uint j = 0; j < 4; ++j)
+        {
+          IPosition start(4, 0, 0, j, 0);
+          IPosition end(4, nX - 1, nY - 1, j, nFreq - 1);
+
+          Array<DComplex> plane = beam(start, end);
+          plane *= tileAF4;
+        }
+      }
+
+      LOG_INFO("LofarATermOld::computeStationBeam: Computing station array factor...");
+
+      // Account for the case where the delay reference position is not equal to
+      // the field center (only applies to core HBA fields).
+      const Vector3 &fieldCenter = field.position();
+      MVPosition delayCenter = station.position().getValue();
+      Vector3 offsetShift = {{fieldCenter[0] - delayCenter(0),
+                              fieldCenter[1] - delayCenter(1),
+                              fieldCenter[2] - delayCenter(2)}};
+
+      // Compute field array factors.
+      Cube<DComplex> fieldAFX(nX, nY, nFreq, DComplex(0.0, 0.0));
+      Cube<DComplex> fieldAFY(nX, nY, nFreq, DComplex(0.0, 0.0));
+      Cube<DComplex> phase(nX, nY, nFreq, DComplex(0.0, 0.0));
+
+      for(uint j = 0; j < field.nElement(); ++j)
+      {
+        const AntennaField::Element &element = field.element(j);
+        if(element.flag[0] && element.flag[1])
+        {
+          continue;
+        }
+
+        // Compute the offset relative to the delay center.
+        Vector3 offset = {{element.offset[0] + offsetShift[0],
+                           element.offset[1] + offsetShift[1],
+                           element.offset[2] + offsetShift[2]}};
+
+        // Compute the delay for a plane wave approaching from the delay
+        // reference direction with respect to the element position.
+        double delay0 = (refDelay[0] * offset[0] + refDelay[1] * offset[1]
+          + refDelay[2] * offset[2]) / casa::C::c;
+        double shift0 = C::_2pi * m_refFreq * delay0;
+
+        for(uint y = 0; y < nY; ++y)
+        {
+          for(uint x = 0; x < nX; ++x)
+          {
+            // Compute the delay for a plane wave approaching from the direction
+            // of interest with respect to the element position.
+            double delay = (map(0, x, y) * offset[0]
+                            + map(1, x, y) * offset[1]
+                            + map(2, x, y) * offset[2]) / casa::C::c;
+
+            for(uint k = 0; k < nFreq; ++k)
+            {
+              double shift = C::_2pi * freq[k] * delay - shift0;
+              phase(x, y, k) = DComplex(cos(shift), sin(shift));
+            }
+          }
+        }
+
+        if(!element.flag[0])
+        {
+          fieldAFX += phase;
+          ++countX;
+        }
+
+        if(!element.flag[1])
+        {
+          fieldAFY += phase;
+          ++countY;
+        }
+      }
+
+      LOG_INFO("LofarATermOld::computeStationBeam: Computing station array factor... done.");
+      Array<DComplex> fieldAFX4 = fieldAFX.reform(IPosition(4, nX, nY, 1, nFreq));
+      for(uint k = 0; k < 2; ++k)
+      {
+        IPosition start(4, 0, 0, k, 0);
+        IPosition end(4, nX - 1, nY - 1, k, nFreq - 1);
+        Array<DComplex> plane = E(start, end);
+        plane += fieldAFX4 * beam(start, end);
+      }
+
+      Array<DComplex> fieldAFY4 = fieldAFY.reform(IPosition(4, nX, nY, 1, nFreq));
+      for(uint k = 2; k < 4; ++k)
+      {
+        IPosition start(4, 0, 0, k, 0);
+        IPosition end(4, nX - 1, nY - 1, k, nFreq - 1);
+        Array<DComplex> plane = E(start, end);
+        plane += fieldAFY4 * beam(start, end);
+      }
+    } // fields
+
+    // Normalize.
+    if(countX > 0)
+    {
+      IPosition start(4, 0, 0, 0, 0);
+      IPosition end(4, nX - 1, nY - 1, 1, nFreq - 1);
+      Array<DComplex> plane = E(start, end);
+      plane /= static_cast<Double>(countX);
+    }
+
+    if(countY > 0)
+    {
+      IPosition start(4, 0, 0, 2, 0);
+      IPosition end(4, nX - 1, nY - 1, 3, nFreq - 1);
+      Array<DComplex> plane = E(start, end);
+      plane /= static_cast<Double>(countY);
+    }
+
+    return E;
+  }
+
+  Cube<DComplex> LofarATermOld::evaluateTileArrayFactor(const AntennaField &field,
+    const Vector3 &reference,
+    const Cube<Double> &map,
+    const Vector<Double> &freq) const
+  {
+    const uint nX = map.shape()[1];
+    const uint nY = map.shape()[2];
+    const uint nFreq = freq.shape()[0];
+
+    Cube<DComplex> factor(nX, nY, nFreq, DComplex(0.0, 0.0));
+    for(uint y = 0; y < nY; ++y)
+    {
+      for(uint x = 0; x < nX; ++x)
+      {
+        // Instead of computing a phase shift for the pointing direction and a
+        // phase shift for the direction of interest and then computing the
+        // difference, compute the resultant phase shift in one go. Here we make
+        // use of the relation a . b + a . c = a . (b + c). The sign of k is
+        // related to the sign of the phase shift.
+        double k[3];
+        k[0] = map(0, x, y) - reference[0];
+        k[1] = map(1, x, y) - reference[1];
+        k[2] = map(2, x, y) - reference[2];
+
+        for(uint j = 0; j < field.nTileElement(); ++j)
+        {
+          // Compute the effective delay for a plane wave approaching from the
+          // direction of interest with respect to the position of element i
+          // when beam forming in the reference direction using time delays.
+          const Vector3 &offset = field.tileElement(j);
+          double delay = (k[0] * offset[0] + k[1] * offset[1] + k[2] * offset[2])
+            / C::c;
+
+          // Turn the delay into a phase shift.
+          for(uint k = 0; k < nFreq; ++k)
+          {
+            double shift = C::_2pi * freq[k] * delay;
+            factor(x, y, k) += DComplex(cos(shift), sin(shift));
+          }
+        }
+      }
+    }
+
+    // Normalize.
+    if(field.nTileElement() > 0)
+    {
+      factor /= static_cast<Double>(field.nTileElement());
+    }
+
+    return factor;
+  }
+
+  Cube<double> LofarATermOld::computeITRFMap(const DirectionCoordinate &coordinates,
+    const IPosition &shape,
+    MDirection::Convert convertor) const
+  {
+    MDirection world;
+    Vector<double> pixel = coordinates.referencePixel();
+
+    Cube<double> map(3, shape[0], shape[1], 0.0);
+    for(pixel[1] = 0.0; pixel(1) < shape[1]; ++pixel[1])
+      {
+        for(pixel[0] = 0.0; pixel[0] < shape[0]; ++pixel[0])
+          {
+            // CoodinateSystem::toWorld()
+            // DEC range [-pi/2,pi/2]
+            // RA range [-pi,pi]
+            if(coordinates.toWorld(world, pixel))
+              {
+                MVDirection mvITRF(convertor(world).getValue());
+                map(0, pixel[0], pixel[1]) = mvITRF(0);
+                map(1, pixel[0], pixel[1]) = mvITRF(1);
+                map(2, pixel[0], pixel[1]) = mvITRF(2);
+              }
+          }
+      }
+
+    return map;
+  }
+
+  void LofarATermOld::initInstrument(const MeasurementSet &ms)
+  {
+    // Get station names and positions in ITRF coordinates.
+    ROMSAntennaColumns antenna(ms.antenna());
+    ROMSObservationColumns observation(ms.observation());
+    ASSERT(observation.nrow() > 0);
+    ASSERT(!observation.flagRow()(0));
+
+    // Get instrument name.
+    String name = observation.telescopeName()(0);
+
+    // Get station positions.
+    MVPosition centroid;
+    vector<Station> stations(antenna.nrow());
+    for(uint i = 0; i < stations.size(); ++i)
+      {
+        // Get station name and ITRF position.
+        MPosition position =
+          MPosition::Convert(antenna.positionMeas()(i),
+                             MPosition::ITRF)();
+
+        // Store station information.
+        stations[i] = initStation(ms, i, antenna.name()(i), position);
+
+        ASSERT(stations[i].nField() > 0);
+
+        // Update ITRF centroid.
+        centroid += position.getValue();
+      }
+
+    // Get the instrument position in ITRF coordinates, or use the centroid
+    // of the station positions if the instrument position is unknown.
+    MPosition position;
+    if(MeasTable::Observatory(position, name))
+      {
+        position = MPosition::Convert(position, MPosition::ITRF)();
+      }
+    else
+      {
+        LOG_INFO("LofarATermOld initInstrument "
+                 "Instrument position unknown; will use centroid of stations.");
+        ASSERT(antenna.nrow() != 0);
+        centroid *= 1.0 / static_cast<double>(antenna.nrow());
+        position = MPosition(centroid, MPosition::ITRF);
+      }
+
+    m_instrument = Instrument(name, position, stations.begin(), stations.end());
+  }
+
+  Station LofarATermOld::initStation(const MeasurementSet &ms,
+    uint id,
+    const String &name,
+    const MPosition &position) const
+  {
+    AlwaysAssert(ms.keywordSet().isDefined("LOFAR_ANTENNA_FIELD"), SynthesisError);
+
+    Table tab_field(ms.keywordSet().asTable("LOFAR_ANTENNA_FIELD"));
+    tab_field = tab_field(tab_field.col("ANTENNA_ID") == static_cast<Int>(id));
+
+    const uLong nFields = tab_field.nrow();
+    AlwaysAssert(nFields == 1 || nFields == 2, SynthesisError);
+
+    ROScalarColumn<String> c_name(tab_field, "NAME");
+    ROArrayQuantColumn<double> c_position(tab_field, "POSITION",
+                                          "m");
+    ROArrayQuantColumn<double> c_axes(tab_field, "COORDINATE_AXES",
+                                      "m");
+    ROArrayQuantColumn<double> c_tile_offset(tab_field,
+                                             "TILE_ELEMENT_OFFSET", "m");
+    ROArrayQuantColumn<double> c_offset(tab_field, "ELEMENT_OFFSET",
+                                        "m");
+    ROArrayColumn<Bool> c_flag(tab_field, "ELEMENT_FLAG");
+
+    AntennaField field[2];
+    for(uLong i = 0; i < nFields; ++i)
+      {
+        // Read antenna field center.
+        Vector<Quantum<double> > aips_position =
+          c_position(i);
+        ASSERT(aips_position.size() == 3);
+
+        Vector3 position = {{aips_position[0].getValue(),
+                             aips_position[1].getValue(),
+                             aips_position[2].getValue()}};
+
+        // Read antenna field coordinate axes.
+        Matrix<Quantum<double> > aips_axes = c_axes(i);
+        ASSERT(aips_axes.shape().isEqual(IPosition(2, 3, 3)));
+
+        Vector3 P = {{aips_axes(0, 0).getValue(), aips_axes(1, 0).getValue(),
+                      aips_axes(2, 0).getValue()}};
+        Vector3 Q = {{aips_axes(0, 1).getValue(), aips_axes(1, 1).getValue(),
+                      aips_axes(2, 1).getValue()}};
+        Vector3 R = {{aips_axes(0, 2).getValue(), aips_axes(1, 2).getValue(),
+                      aips_axes(2, 2).getValue()}};
+
+        // Store information as AntennaField.
+        field[i] = AntennaField(c_name(i), position, P, Q, R);
+
+        if(c_name(i) != "LBA")
+          {
+            // Read tile configuration for HBA antenna fields.
+            Matrix<Quantum<double> > aips_offset =
+              c_tile_offset(i);
+            ASSERT(aips_offset.nrow() == 3);
+
+            const uLong nElement = aips_offset.ncolumn();
+            for(uLong j = 0; j < nElement; ++j)
+              {
+                Vector3 offset = {{aips_offset(0, j).getValue(),
+                                   aips_offset(1, j).getValue(),
+                                   aips_offset(2, j).getValue()}};
+
+                field[i].appendTileElement(offset);
+              }
+          }
+
+        // Read element position offsets and flags.
+        Matrix<Quantum<double> > aips_offset = c_offset(i);
+        Matrix<Bool> aips_flag = c_flag(i);
+
+        const uLong nElement = aips_offset.ncolumn();
+        ASSERT(aips_offset.shape().isEqual(IPosition(2, 3, nElement)));
+        ASSERT(aips_flag.shape().isEqual(IPosition(2, 2, nElement)));
+
+        for(uLong j = 0; j < nElement; ++j)
+          {
+            AntennaField::Element element;
+            element.offset[0] = aips_offset(0, j).getValue();
+            element.offset[1] = aips_offset(1, j).getValue();
+            element.offset[2] = aips_offset(2, j).getValue();
+            element.flag[0] = aips_flag(0, j);
+            element.flag[1] = aips_flag(1, j);
+
+            field[i].appendElement(element);
+          }
+      }
+
+    return (nFields == 1 ? Station(name, position, field[0])
+            : Station(name, position, field[0], field[1]));
+  }
+
+  void LofarATermOld::initReferenceDirections(const MeasurementSet &ms,
+    uint idField)
+  {
+    // Get phase center as RA and DEC (J2000).
+    ROMSFieldColumns field(ms.field());
+    ASSERT(field.nrow() > idField);
+    ASSERT(!field.flagRow()(idField));
+
+    m_refDelay = MDirection::Convert(field.delayDirMeas(idField),
+      MDirection::J2000)();
+
+    // By default, the tile beam reference direction is assumed to be equal
+    // to the station beam reference direction (for backward compatibility,
+    // and for non-HBA measurements).
+    m_refTile = m_refDelay;
+
+    // The MeasurementSet class does not support LOFAR specific columns, so we
+    // use ROArrayMeasColumn to read the tile beam reference direction.
+    Table tab_field(ms.keywordSet().asTable("FIELD"));
+    static const String columnName = "LOFAR_TILE_BEAM_DIR";
+    if(tab_field.tableDesc().isColumn(columnName))
+    {
+      ROArrayMeasColumn<MDirection> c_direction(tab_field, columnName);
+      if(c_direction.isDefined(idField))
+      {
+        m_refTile = MDirection::Convert(c_direction(idField)(IPosition(1, 0)),
+          MDirection::J2000)();
+      }
+    }
+  }
+
+  void LofarATermOld::initReferenceFreq(const MeasurementSet &ms,
+    uint idDataDescription)
+  {
+    // Read polarization id and spectral window id.
+    ROMSDataDescColumns desc(ms.dataDescription());
+    ASSERT(desc.nrow() > idDataDescription);
+    ASSERT(!desc.flagRow()(idDataDescription));
+
+    const uint idWindow = desc.spectralWindowId()(idDataDescription);
+
+    // Get spectral information.
+    ROMSSpWindowColumns window(ms.spectralWindow());
+    ASSERT(window.nrow() > idWindow);
+    ASSERT(!window.flagRow()(idWindow));
+
+    m_refFreq = window.refFrequency()(idWindow);
+  }
+
+  BeamCoeff::BeamCoeff()
+    :   m_center(0.0),
+        m_width(1.0)
+  {
+  }
+
+  void BeamCoeff::load(const Path &path)
+  {
+    // Open file.
+    String expandedPath = path.expandedName();
+    ifstream in(expandedPath.c_str());
+    cout<<"Reading "<<expandedPath<<endl;
+    if(!in)
+      {
+        THROW (Exception, "Unable to open beam coefficient file.");
+      }
+
+    // Read file header.
+    String header, token0, token1, token2, token3, token4, token5;
+    getline(in, header);
+
+    uLong nElements, nHarmonics, nPowerTheta, nPowerFreq;
+    double freqAvg, freqRange;
+
+    istringstream iss(header);
+    iss >> token0 >> nElements >> token1 >> nHarmonics >> token2 >> nPowerTheta
+        >> token3 >> nPowerFreq >> token4 >> freqAvg >> token5 >> freqRange;
+
+    if(!in || !iss || token0 != "d" || token1 != "k" || token2 != "pwrT"
+       || token3 != "pwrF" || token4 != "freqAvg" || token5 != "freqRange")
+      {
+        THROW (Exception, "Unable to parse header");
+      }
+
+    if(nElements * nHarmonics * nPowerTheta * nPowerFreq == 0)
+      {
+        THROW (Exception, "The number of coefficients should be"
+               " larger than zero.");
+      }
+
+    ASSERT(nElements == 2);
+    ASSERT(in.good());
+
+    // Allocate coefficient matrix.
+    m_center = freqAvg;
+    m_width = freqRange;
+    m_coeff = Array<DComplex>(IPosition(4, 2, nPowerFreq, nPowerTheta, nHarmonics));
+
+    uLong nCoeff = 0;
+    while(in.good())
+      {
+        // Read line from file.
+        String line;
+        getline(in, line);
+
+        // Skip lines that contain only whitespace.
+        if(line.find_last_not_of(" ", String::npos) == String::npos)
+          {
+            continue;
+          }
+
+        // Parse line.
+        uLong element, harmonic, powerTheta, powerFreq;
+        double re, im;
+
+        iss.clear();
+        iss.str(line);
+        iss >> element >> harmonic >> powerTheta >> powerFreq >> re >> im;
+
+        if(!iss || element >= nElements || harmonic >= nHarmonics
+           || powerTheta >= nPowerTheta || powerFreq >= nPowerFreq)
+          {
+            THROW (Exception, "Error reading beam coefficient file.");
+          }
+
+        // Store coefficient.
+        m_coeff(IPosition(4, element, powerFreq, powerTheta, harmonic)) = DComplex(re, im);
+
+        // Update coefficient counter.
+        ++nCoeff;
+      }
+
+    if(!in.eof())
+      {
+        THROW (Exception, "Error reading beam coefficient"
+               " file.");
+      }
+
+    if(nCoeff != nElements * nHarmonics * nPowerTheta * nPowerFreq)
+      {
+        THROW (Exception, "The number of coefficients"
+               " specified in the header does not match the number of coefficients"
+               " in the file.");
+      }
+  }
+
+  AntennaField::AntennaField(const String &name,
+    const Vector3 &position,
+    const Vector3 &p,
+    const Vector3 &q,
+    const Vector3 &r)
+    :   m_name(name),
+        m_position(position)
+  {
+    m_axes[P] = p;
+    m_axes[Q] = q;
+    m_axes[R] = r;
+  }
+
+  const String &AntennaField::name() const
+  {
+    return m_name;
+  }
+
+  const Vector3 &AntennaField::position() const
+  {
+    return m_position;
+  }
+
+  const Vector3 &AntennaField::axis(Axis axis) const
+  {
+    return m_axes[axis];
+  }
+
+  Bool AntennaField::isHBA() const
+  {
+    return m_name != "LBA";
+  }
+
+  void AntennaField::appendTileElement(const Vector3 &offset)
+  {
+    m_tileElements.push_back(offset);
+  }
+
+  void AntennaField::appendElement(const Element &element)
+  {
+    m_elements.push_back(element);
+  }
+
+  Station::Station(const String &name,
+    const MPosition &position)
+    :   m_name(name),
+        m_position(position)
+  {
+  }
+
+  Station::Station(const String &name,
+    const MPosition &position,
+    const AntennaField &field0)
+    :   m_name(name),
+        m_position(position)
+  {
+    m_fields.push_back(field0);
+  }
+
+  Station::Station(const String &name,
+    const MPosition &position,
+    const AntennaField &field0,
+    const AntennaField &field1)
+    :   m_name(name),
+        m_position(position)
+  {
+    m_fields.push_back(field0);
+    m_fields.push_back(field1);
+  }
+
+  const String &Station::name() const
+  {
+    return m_name;
+  }
+
+  const MPosition &Station::position() const
+  {
+    return m_position;
+  }
+
+  bool Station::isPhasedArray() const
+  {
+    return !m_fields.empty();
+  }
+
+  uint Station::nField() const
+  {
+    return m_fields.size();
+  }
+
+  const AntennaField &Station::field(uint i) const
+  {
+    return m_fields[i];
+  }
+
+  Instrument::Instrument(const String &name,
+    const MPosition &position)
+    :   m_name(name),
+        m_position(position)
+  {
+  }
+
+  const String &Instrument::name() const
+  {
+    return m_name;
+  }
+
+  const MPosition &Instrument::position() const
+  {
+    return m_position;
+  }
+
+  uint Instrument::nStations() const
+  {
+    return m_stations.size();
+  }
+
+  const Station &Instrument::station(uint i) const
+  {
+    return m_stations[i];
+  }
+
+  const Station &Instrument::station(const String &name) const
+  {
+    map<String, uint>::const_iterator it = m_index.find(name);
+    if(it == m_index.end())
+      {
+        THROW (Exception, "Unknown station: " + name);
+      }
+
+    return m_stations[it->second];
+  }
+
+  void Instrument::append(const Station &station)
+  {
+    m_stations.push_back(station);
+  }
+} // namespace LOFAR
diff --git a/CEP/Imager/LofarFT/src/LofarConvolutionFunction.cc b/CEP/Imager/LofarFT/src/LofarConvolutionFunction.cc
index 226fda025dea172629ae5f81ec81636f5b1a5259..663be1f1ddc5fc5798fd516a52a9ac9f18e2948a 100644
--- a/CEP/Imager/LofarFT/src/LofarConvolutionFunction.cc
+++ b/CEP/Imager/LofarFT/src/LofarConvolutionFunction.cc
@@ -25,12 +25,16 @@
 #include <Common/LofarLogger.h>
 #include <Common/OpenMP.h>
 
+#include <BBSKernel/MeasurementAIPS.h>
+
 #include <casa/Logging/LogIO.h>
 #include <casa/Logging/LogOrigin.h>
 #include <casa/Arrays/Cube.h>
 #include <casa/Arrays/Matrix.h>
 #include <casa/Arrays/MatrixMath.h>
 #include <casa/Arrays/ArrayMath.h>
+#include <casa/Arrays/ArrayUtil.h>
+#include <casa/Arrays/ArrayIter.h>
 
 #include <ms/MeasurementSets/MeasurementSet.h>
 #include <measures/Measures/MDirection.h>
@@ -59,6 +63,12 @@
 #include <casa/sstream.h>
 #include <iomanip>
 
+#include <lattices/Lattices/ArrayLattice.h>
+#include <lattices/Lattices/LatticeFFT.h>
+
+// SvdT: to get rid of warnings for unused variables
+#define UNUSED(expr) (void)(expr);
+
 namespace LOFAR
 {
 
@@ -68,13 +78,20 @@ namespace LOFAR
    const MeasurementSet& ms,
    uInt nW, double Wmax,
    uInt oversample,
-   const String& beamElementPath,
    Int verbose,
    Int maxsupport,
-   const String& imgName)
+   const String& imgName,
+   Bool Use_EJones,
+   Bool Apply_Element,
+   const casa::Record& parameters
+  )
+  // ,
+  //Int TaylorTerm,
+    //Double RefFreq
     : m_shape(shape),
       m_coordinates(coordinates),
-      m_aTerm(ms, beamElementPath),
+      itsParameters(parameters),
+      m_aTerm(ms, parameters),
       m_maxW(Wmax), //maximum W set by ft machine to flag the w>wmax
       m_nWPlanes(nW),
       m_oversampling(oversample),
@@ -92,7 +109,6 @@ namespace LOFAR
       itsTimeCFpar(0),
       itsTimeCFfft(0),
       itsTimeCFcnt(0)
-      //Not sure how useful that is
   {
     if (itsVerbose > 0) {
       cout<<"LofarConvolutionFunction:shape  "<<shape<<endl;
@@ -101,17 +117,29 @@ namespace LOFAR
 
     //    m_maxCFSupport=0; //need this parameter to stack all the CF for average PB estimate
 
-    m_wScale = WScale(m_maxW, m_nWPlanes);
+    //itsTaylorTerm=TaylorTerm;
+    //itsRefFreq=RefFreq;
+    //cout<<"itsTaylorTerm itsRefFreq "<<itsTaylorTerm<<" "<<itsRefFreq<<endl;
+      m_wScale = WScale(m_maxW, m_nWPlanes);
     MEpoch start = observationStartTime(ms, 0);
 
-    m_refFrequency = observationReferenceFreq(ms, 0);
-
+    m_refFrequency = BBS::readFreqReference(ms, 0);
+    its_Use_EJones=Use_EJones;
+    its_Apply_Element=Apply_Element;
+    its_count_time=0;
+    //if(!its_Use_EJones){cout<<"Not using the beam in the calculation of the CFs...."<<endl;}
     if (m_oversampling%2 == 0) {
       // Make OverSampling an odd number
       m_oversampling++;
     }
 
-    list_freq   = Vector<Double>(1, m_refFrequency);
+    //list_freq   = Vector<Double>(1, m_refFrequency);
+    ROMSSpWindowColumns window(ms.spectralWindow());
+    list_freq.resize(window.nrow());
+    for(uInt i=0; i<window.nrow();++i){
+      list_freq[i]=window.refFrequency()(i);
+    };
+
     m_nChannel  = list_freq.size();
     ROMSAntennaColumns antenna(ms.antenna());
     m_nStations = antenna.nrow();
@@ -131,6 +159,35 @@ namespace LOFAR
 
     // Precalculate the Wtwerm fft for all w-planes.
     store_all_W_images();
+    itsFilledVectorMasks=false;
+
+    // Build the cutted spheroidal for the element beam image
+    Double pixelSize = abs(m_coordinates.increment()[0]);
+    Double imageDiameter = pixelSize * m_shape(0);
+    DirectionCoordinate coordinate = m_coordinates;
+    Double aPixelAngSize = min(m_pixelSizeSpheroidal,
+    			       estimateAResolution(m_shape, m_coordinates));
+    //Double aPixelAngSize = estimateAResolution(m_shape, m_coordinates, 30);
+    Int nPixelsConv = imageDiameter / aPixelAngSize;
+    
+    Matrix<Complex> spheroid_cut_element(IPosition(2,nPixelsConv,nPixelsConv),1.);
+    taper(spheroid_cut_element);
+    //Matrix<Complex> spheroid_cut_element_fft=give_normalized_fft_lapack(spheroid_cut_element, true);
+    normalized_fft(spheroid_cut_element, true);
+    spheroid_cut_element_fft=spheroid_cut_element;
+    Matrix<Complex> spheroid_cut_element_padfft(zero_padding(spheroid_cut_element_fft, m_shape(0)));
+    //Matrix<Complex> spheroid_cut_element_padfft_fft=give_normalized_fft_lapack (spheroid_cut_element_padfft, false);
+    normalized_fft (spheroid_cut_element_padfft, false);
+    Matrix<Complex> spheroid_cut_element_padfft_fft(spheroid_cut_element_padfft);
+    float threshold = 1.e-6;
+    for (Int jj=0; jj<m_shape[1]; ++jj) {
+      for (Int ii=0; ii<m_shape[0]; ++ii) {
+	Float absVal = abs(spheroid_cut_element_padfft_fft(ii,jj));
+	spheroid_cut_element_padfft_fft(ii,jj) = std::max (absVal, threshold);
+      }
+    }
+    Spheroid_cut_im_element.reference (real(spheroid_cut_element_padfft_fft));
+    store(m_coordinates,Spheroid_cut_im_element,"Spheroid_cut_im_element.img");
   }
 
   //      ~LofarConvolutionFunction ()
@@ -140,6 +197,7 @@ namespace LOFAR
   // Precalculate all W-terms in the fourier domain
   void LofarConvolutionFunction::store_all_W_images()
   {
+    logIO()<<"LofarConvolutionFunction::store_all_W_images() "<<"Computing the Wterms..."<< LogIO::POST;//<<endl;
     PrecTimer wTimer;
     wTimer.start();
     Double pixelSize = abs(m_coordinates.increment()[0]);
@@ -163,10 +221,11 @@ namespace LOFAR
                                    estimateWResolution(m_shape,
                                                        pixelSize, w));
         Int nPixelsConv = imageDiameter / wPixelAngSize;
+        nPixelsConv *= 2; // SvdT : temporary hack for testing
         if (itsVerbose > 0) {
           cout<<"Number of pixel in the "<<i<<"-wplane: "<<nPixelsConv
               <<"  (w="<<w<<")"<<endl;
-        }
+	}
         if (nPixelsConv > itsMaxSupport) {
           nPixelsConv = itsMaxSupport;
         }
@@ -194,8 +253,17 @@ namespace LOFAR
 #pragma omp atomic
       itsTimeWpar += ptime;
     } // end omp parallel
+
+    its_MaxWSupport=0;
+    for (uInt i=0; i<m_nWPlanes; ++i) {
+      if(m_WplanesStore[i].shape()[0]>its_MaxWSupport){its_MaxWSupport=m_WplanesStore[i].shape()[0];};
+    }
+
+
+
     wTimer.stop();
     itsTimeW = wTimer.getReal();
+    logIO()<<"LofarConvolutionFunction::store_all_W_images() "<<"... Done!"<< LogIO::POST;//<<endl;
   }
 
 
@@ -204,6 +272,9 @@ namespace LOFAR
   // Put it in a map object with a (double time) key.
   void LofarConvolutionFunction::computeAterm (Double time)
   {
+    //logIO()<<"LofarConvolutionFunction::computeAterm "<<"Computing the Aterms for t="<<time<<", and maximum Wupport="<<its_MaxWSupport<< LogIO::POST;//<<endl;
+    PrecTimer timerCyril;
+    timerCyril.start();
     if (m_AtermStore.find(time) != m_AtermStore.end()) {
       // Already done.
       return;
@@ -215,73 +286,173 @@ namespace LOFAR
     // Try to avoid making copies when inserting elements in vector or map.
     // Therefore first create the elements and resize them.
     m_AtermStore[time] = vector< vector< Cube<Complex> > >();
+    m_AtermStore_element[time] = vector< vector< Cube<Complex> > >();
+    m_AtermStore_station[time] = vector< vector< Cube<Complex> > >();
     vector< vector< Cube<Complex> > >& aTermList = m_AtermStore[time];
+    vector< vector< Cube<Complex> > >& aTermList_element = m_AtermStore_element[time];
+    vector< vector< Cube<Complex> > >& aTermList_station = m_AtermStore_station[time];
     // Calculate the A-term and fill the vector for all stations.
     aTermList.resize (m_nStations);
+    aTermList_element.resize (m_nStations);
+    aTermList_station.resize (m_nStations);
     ///#pragma omp parallel
     {
       // Thread private variables.
       PrecTimer timerFFT;
       PrecTimer timerPar;
+
       ///#pragma omp for
+      DirectionCoordinate coordinate = m_coordinates;
+      Double aPixelAngSize = min(m_pixelSizeSpheroidal, estimateAResolution(m_shape, m_coordinates));
+      Int nPixelsConv = imageDiameter / aPixelAngSize;
+      if (nPixelsConv > itsMaxSupport) {
+	nPixelsConv = itsMaxSupport;
+      }
+      // Make odd and optimal.
+      nPixelsConv = FFTCMatrix::optimalOddFFTSize (nPixelsConv);
+      aPixelAngSize = imageDiameter / nPixelsConv;
+      IPosition shape(2, nPixelsConv, nPixelsConv);
+      Vector<Double> increment_old(coordinate.increment());
+      Vector<Double> increment(2);
+      increment[0] = aPixelAngSize*sign(increment_old[0]);
+      increment[1] = aPixelAngSize*sign(increment_old[1]);
+      coordinate.setIncrement(increment);
+      Vector<Double> refpix(2, 0.5*(nPixelsConv-1));
+      coordinate.setReferencePixel(refpix);
+      
+      DirectionCoordinate coordinate_element = m_coordinates;
+      //Double aPixelAngSize_element = estimateAResolution(m_shape, m_coordinates, 30.);
+      Double aPixelAngSize_element = min(m_pixelSizeSpheroidal, estimateAResolution(m_shape, m_coordinates));
+      Int nPixelsConv_element = imageDiameter / aPixelAngSize_element;
+      //cout<<"Element_beam size:"<<"1 "<<nPixelsConv_element<<", 2 "<<aPixelAngSize_element<<endl;
+      nPixelsConv_element = FFTCMatrix::optimalOddFFTSize (nPixelsConv_element);
+      aPixelAngSize_element = imageDiameter / nPixelsConv_element;
+      //cout<<"Element_beam size:"<<"1 "<<nPixelsConv_element<<", 2 "<<aPixelAngSize_element<<endl;
+      IPosition shape_element(2, nPixelsConv_element, nPixelsConv_element);
+      Vector<Double> increment_element(2);
+      increment_element[0] = aPixelAngSize_element*sign(increment_old[0]);
+      increment_element[1] = aPixelAngSize_element*sign(increment_old[1]);
+      coordinate_element.setIncrement(increment_element);
+      Vector<Double> refpix_element(2, 0.5*(nPixelsConv_element-1));
+      coordinate_element.setReferencePixel(refpix_element);
+      
+      //hier is het
+      
+      m_aTerm.setDirection(coordinate, shape);
+      
+      MEpoch binEpoch;
+      binEpoch.set(Quantity(time, "s"));
+      
+      m_aTerm.setEpoch(binEpoch);
+//       LofarATerm::ITRFDirectionMap dirMap = m_aTerm.makeDirectionMap(coordinate, shape, binEpoch);
+
       for (uInt i=0; i<m_nStations; ++i) {
         timerPar.start();
-	DirectionCoordinate coordinate = m_coordinates;
-        Double aPixelAngSize = min(m_pixelSizeSpheroidal,
-                                   estimateAResolution(m_shape, m_coordinates));
-        Int nPixelsConv = imageDiameter / aPixelAngSize;
-        if (nPixelsConv > itsMaxSupport) {
-          nPixelsConv = itsMaxSupport;
-        }
-        // Make odd and optimal.
-        nPixelsConv = FFTCMatrix::optimalOddFFTSize (nPixelsConv);
-        aPixelAngSize = imageDiameter / nPixelsConv;
-        if (itsVerbose > 0) {
-          cout.precision(20);
-          cout<<"Number of pixel in the Aplane of "<<i<<": "<<nPixelsConv
-              <<", time="<<fixed<<time<<endl;
-        }
-        IPosition shape(2, nPixelsConv, nPixelsConv);
-        Vector<Double> increment_old(coordinate.increment());
-        Vector<Double> increment(2);
-        increment[0] = aPixelAngSize*sign(increment_old[0]);
-        increment[1] = aPixelAngSize*sign(increment_old[1]);
-        coordinate.setIncrement(increment);
-        Vector<Double> refpix(2, 0.5*(nPixelsConv-1));
-        coordinate.setReferencePixel(refpix);
 
         //======================================
-        // Disable the beam
+        // Separated element and station
         //======================================
-        //Cube<Complex> aterm_cube(IPosition(3,nPixels_Conv,nPixels_Conv,4),1.);
-        //for (uInt iiii=0;iiii<nPixels_Conv;++iiii) {
-        //  for (uInt iiiii=0;iiiii<nPixels_Conv;++iiiii) {
-        //    aterm_cube(iiii,iiiii,1)=0.;
-        //    aterm_cube(iiii,iiiii,2)=0.;
-        //  }
-        //}
-        //vector< Cube<Complex> > aTermA;
-        //aTermA.push_back(aterm_cube);
+	vector< Cube<Complex> > aTermA_element;
+	vector< Cube<Complex> > aTermA_array;
+
+	vector< Matrix<Complex> > aTermA_array_plane(m_aTerm.evaluateStationScalarFactor(i, list_freq , list_freq , true));
+	aTermA_array.resize(m_nChannel);
+        for (uInt ch=0; ch<m_nChannel; ++ch) {
+	  aTermA_array[ch].resize(IPosition(3,shape[0],shape[0],4));
+	  aTermA_array[ch]=0.;
+	}
+        for (uInt ch=0; ch<m_nChannel; ++ch) {
+	  Matrix<Complex> plane(aTermA_array[ch].xyPlane(0));
+	  plane=aTermA_array_plane[ch].copy();
+	  Matrix<Complex> plane2(aTermA_array[ch].xyPlane(3));
+	  plane2=aTermA_array_plane[ch].copy();
+	}
+
+	aTermA_element=m_aTerm.evaluateElementResponse(i, 0, list_freq, true);
+
+	//store(coordinate,aTermA_element[0],"aTermA_element."+String::toString(i)+".img");
+	//store(coordinate,aTermA_array[0],"aTermA_array."+String::toString(i)+".img");
+
+        //vector< Cube<Complex> > aTermA = m_aTerm.evaluate(i, dirMap, list_freq, list_freq, false);
+	//store(coordinate,aTermA[0],"aTermA."+String::toString(i)+".orig.img");
+
+//	aTermAtmp = m_aTerm.evaluateSeparated(shape_element,
+//					   coordinate_element,
+//					   i, binEpoch,
+//					   list_freq, true);
+//	aTermA_element=aTermAtmp[0];
+//	aTermAtmp2 = m_aTerm.evaluateSeparated(shape,
+//					   coordinate,
+//					   i, binEpoch,
+//					   list_freq, true);
+//	aTermA_station=aTermAtmp2[1];
+	//store(aTermA_element[0],"aTermA_element.img");
+	//store(aTermA_station[0],"aTermA_station.img");
+
+	//vector< Cube<Complex> > aTermA;
+	// if(!its_Use_EJones){
+
+        // for (uInt ch=0; ch<m_nChannel; ++ch) {
+	//   Matrix<Complex> slice0=aTermA_element[ch].xyPlane(0);
+	//   slice0=1.;
+	//   Matrix<Complex> slice1=aTermA_element[ch].xyPlane(1);
+	//   slice1=0.;
+	//   Matrix<Complex> slice2=aTermA_element[ch].xyPlane(2);
+	//   slice2=0.;
+	//   Matrix<Complex> slice3=aTermA_element[ch].xyPlane(3);
+	//   slice3=1.;
+	// }
+        // for (uInt ch=0; ch<m_nChannel; ++ch) {
+	//   Matrix<Complex> slice0=aTermA_array[ch].xyPlane(0);
+	//   slice0=1.;
+	//   Matrix<Complex> slice1=aTermA_array[ch].xyPlane(1);
+	//   slice1=0.;
+	//   Matrix<Complex> slice2=aTermA_array[ch].xyPlane(2);
+	//   slice2=0.;
+	//   Matrix<Complex> slice3=aTermA_array[ch].xyPlane(3);
+	//   slice3=1.;
+	// }
+
+	// }
+	// else{
         //======================================
         // Enable the beam
         //======================================
-        MEpoch binEpoch;
-        binEpoch.set(Quantity(time, "s"));
-        vector< Cube<Complex> > aTermA = m_aTerm.evaluate(shape,
-                                                          coordinate,
-                                                          i, binEpoch,
-                                                          list_freq, true);
+//	aTermA = m_aTerm.evaluate(shape,
+//				  coordinate,
+//				  i, binEpoch,
+//				  list_freq, true);
+	// }
+
+
+//        // JVZ: Direction map should be computed only once for all stations.
+//        // However, this requires the DirectionCoordinate instance and shape to
+//        // be exactly equal for all stations.
+
+//        vector< Cube<Complex> > aTermA = m_aTerm.evaluate(i, dirMap, list_freq,
+//          list_freq, true);
+
         // Compute the fft on the beam
+	vector< Cube<Complex> > aTermAs;
+	aTermAs.resize(m_nChannel);
+	aTermAs[0].resize(IPosition(3,its_MaxWSupport,its_MaxWSupport,4));
         for (uInt ch=0; ch<m_nChannel; ++ch) {
           for (uInt pol=0; pol<4; ++pol) {
-            Matrix<Complex> plane (aTermA[ch].xyPlane(pol));
-            AlwaysAssert (plane.contiguousStorage(), AipsError);
-            normalized_fft (timerFFT, plane);
+
+            Matrix<Complex> plane1 (aTermA_array[ch].xyPlane(pol));
+	    //Matrix< Complex > plane0int = LinearInterpol2(plane1,200.);
+            normalized_fft (timerFFT, plane1);
+
+
           }
         }
+	// store(coordinate,aTermA_array[0],"aTermA_array.fft."+String::toString(i)+".img");
+
         // Note that push_back uses the copy constructor, so for the Cubes
         // in the vector the copy constructor is called too (which is cheap).
-        aTermList[i] = aTermA;
+        //aTermList[i] = aTermA;
+        aTermList_element[i] = aTermA_element;
+        aTermList_station[i] = aTermA_array;
         timerPar.stop();
       } // end omp for
       // Update the timing info.
@@ -297,8 +468,427 @@ namespace LOFAR
     } // end omp parallel
     aTimer.stop();
     itsTimeA = aTimer.getReal();
+    //logIO()<<"LofarConvolutionFunction::computeAterm "<<"...Done!"<< LogIO::POST;//<<endl;
+    timerCyril.stop();
+    //cout.precision(20);
+    //cout<<"For time: "<<time<<endl;
+    //assert(false);
+    //timerCyril.show(cout,"LofarConvolutionFunction::computeAterm");
+  }
+
+
+  Array<Complex> LofarConvolutionFunction::ApplyElementBeam(Array<Complex> input_grid, Double time, uInt spw, const Matrix<bool>& Mask_Mueller_in, bool degridding_step)
+  {
+
+    map<Double, vector< vector< Cube<Complex> > > >::const_iterator aiter_element = m_AtermStore_element.find(time);
+    AlwaysAssert (aiter_element!=m_AtermStore_element.end(), AipsError);
+    const vector< vector< Cube<Complex> > >& aterm_element = aiter_element->second;
+
+
+
+    vector< vector< IPosition > > Mueller_Coordinates;
+    Mueller_Coordinates.resize(4);
+    for(uInt i=0;i<4;++i){
+      Mueller_Coordinates[i].resize(4);
+      IPosition pos(2,2,1);
+      for(uInt j=0;j<4;++j){
+	Mueller_Coordinates[i][j]=pos;
+      }
+    }
+
+
+    uInt ind0;
+    uInt ind1;
+    uInt ii = 0;
+    IPosition cfShape;
+    Bool allElem = True;
+    UNUSED(allElem);
+    for (uInt row0=0; row0<=1; ++row0) {
+      for (uInt col0=0; col0<=1; ++col0) {
+	vector < Matrix<Complex> > Row(4);
+	vector < Matrix<Complex> > Row_non_padded(4);
+	uInt jj = 0;
+	for (uInt row1=0; row1<=1; ++row1) {
+	  for (uInt col1=0; col1<=1; ++col1) {
+	    // This Mueller ordering is for polarisation given as XX,XY,YX YY
+	    ind0 = row0 + 2*row1;
+	    ind1 = col0 + 2*col1;
+	    IPosition pos(2,2,1);
+	    pos[0]=ind0;
+	    pos[1]=ind1;
+	    Mueller_Coordinates[ii][jj]=pos;
+	    ++jj;
+	  }
+	}
+	++ii;
+      }
+    }
+
+
+
+    vector< vector< Matrix<Complex> > > vec_element_product;
+    vec_element_product.resize(4);
+    vector< vector< Matrix<Complex> > > vec_plane_product;
+    vec_plane_product.resize(4);
+    if (!degridding_step) {
+      for (uInt i=0; i<4; ++i) {
+    	for (uInt j=i; j<4; ++j) {
+	  IPosition pos_tmp(Mueller_Coordinates[i][j]);
+	  Mueller_Coordinates[i][j]=Mueller_Coordinates[j][i];
+	  Mueller_Coordinates[j][i]=pos_tmp;
+        }
+      }
+    }
+
+    uInt nx(input_grid.shape()[0]);
+    uInt ny(input_grid.shape()[1]);
+    UNUSED(ny);
+    uInt npol(input_grid.shape()[2]);
+
+    Cube<Complex> aTermA(aterm_element[0][spw].copy());
+    Array<Complex> grid_out(input_grid.shape(),0.);
+
+
+    if(!degridding_step){
+
+      logIO() <<"LofarConvolutionFunction::ApplyElementBeam "<<"FFT of the gridded data for this timeslot" << LogIO::POST;//<<endl;
+
+      for(uInt channel=0;channel< input_grid.shape()[3];++channel){
+	{
+#pragma omp parallel for
+	for(uInt jj=0;jj<npol;++jj){
+	  //cout<<"jj="<<jj<<endl;
+	  Matrix<Complex> plane_array_in  = input_grid(Slicer(IPosition(4, 0, 0, jj, 0),
+							      IPosition(4, nx, nx, 1, 1))).nonDegenerate();
+	  //store(plane_array_in,"plane_array_in"+String::toString(jj)+".img");
+	  normalized_fft (plane_array_in, false);
+	  //store(plane_array_in,"plane_array_in"+String::toString(jj)+".img");
+	}
+	}
+      }
+    }
+
+
+
+    //cout<<"LofarConvolutionFunction::ApplyElementBeam "<<"Calculate element beams"<<endl;
+    logIO()<<"LofarConvolutionFunction::ApplyElementBeam "<<"Calculate element beams"<< LogIO::POST;//<<endl;
+    for(uInt ii=0;ii<4;++ii){
+      vec_element_product[ii].resize(4);
+      vec_plane_product[ii].resize(4);
+      for(uInt jj=0;jj<4;++jj){
+	if(Mask_Mueller_in(ii,jj)==true){
+	  vec_element_product[ii][jj].resize(IPosition(2, nx, nx));
+	  vec_plane_product[ii][jj].resize(aTermA.xyPlane(0).shape());
+	  vec_plane_product[ii][jj]=aTermA.xyPlane((Mueller_Coordinates[ii][jj])[0]) * aTermA.xyPlane((Mueller_Coordinates[ii][jj])[1]);
+	  taper(vec_plane_product[ii][jj]);
+	  if(!degridding_step){vec_plane_product[ii][jj]=conj(vec_plane_product[ii][jj]);};
+	}
+	//store(vec_plane_product[ii][jj],"vec_plane_product"+String::toString(ii)+String::toString(jj)+".img");
+      }
+    }
+
+
+    logIO()<<"LofarConvolutionFunction::ApplyElementBeam "<<"FFT - Zero Pad - IFFT"<< LogIO::POST;//<<endl;
+    //#pragma omp parallel
+    {
+      Int ii;
+      Int jj;
+#pragma omp parallel for private(ii,jj)
+      for(uInt iii=0;iii<16;++iii){
+	jj=floor(float(iii)/4.);
+	ii=floor((float(iii)/4.-jj)*4.);
+	//cout<<"iii"<<iii<<" "<<ii<<" "<<jj<<endl;
+	if(Mask_Mueller_in(ii,jj)==true){
+	  normalized_fft (vec_plane_product[ii][jj], true);
+	  vec_element_product[ii][jj]=zero_padding(vec_plane_product[ii][jj], nx);
+	  normalized_fft (vec_element_product[ii][jj], false);
+	  //store(vec_element_product[ii][jj],"vec_element_product"+String::toString(ii)+String::toString(jj)+".img");
+	}
+
+      }
+    }
+
+    //    assert(false);
+
+
+    //#pragma omp parallel
+    if(npol==4)
+      {
+	logIO()<<"LofarConvolutionFunction::ApplyElementBeam "<<"Multiply element and data in the image plane"<< LogIO::POST;//<<endl;
+	int y=0;
+	uInt ii=0;
+	uInt jj=0;
+#pragma omp parallel for private(y,ii,jj)
+	for(int x=0 ; x<nx ; ++x){
+	  //cout<<"x="<<x<<endl;
+	  for(y=0 ; y<nx ; ++y){
+
+	    for(ii=0;ii<4;++ii){
+	      for(jj=0;jj<4;++jj){
+		if(Mask_Mueller_in(ii,jj)==true){
+		  grid_out(IPosition(4,x,y,jj,0)) += vec_element_product[ii][jj](x,y) * input_grid(IPosition(4,x,y,ii,0))/Spheroid_cut_im_element(x,y);
+
+		}
+	      }
+	    }
+	  }
+	}
+      }
+
+    if(npol==1)
+      {
+	logIO()<<"LofarConvolutionFunction::ApplyElementBeam "<<"Multiply element and data in the image plane"<< LogIO::POST;//<<endl;
+	int y=0;
+	uInt ii=0;
+	uInt jj=0;
+#pragma omp parallel for private(y,ii,jj)
+	for(int x=0 ; x<nx ; ++x){
+	  //cout<<"x="<<x<<endl;
+	  for(y=0 ; y<nx ; ++y){
+
+	    for(ii=0;ii<4;++ii){
+	      for(jj=0;jj<4;++jj){
+		if(Mask_Mueller_in(ii,jj)==true){
+		  grid_out(IPosition(4,x,y,0,0)) += vec_element_product[ii][jj](x,y) * input_grid(IPosition(4,x,y,0,0))/(2.*Spheroid_cut_im_element(x,y));
+		}
+	      }
+	    }
+	  }
+	}
+
+      }
+
+    logIO()<<"LofarConvolutionFunction::ApplyElementBeam "<<"Shapes InputGrid:"<<input_grid.shape()<<", Shapes OutputGrid:"<< LogIO::POST;//<<grid_out.shape()<<endl;
+
+
+    return grid_out;
+
+  }
+
+  //==================================================================
+  //==================================================================
+  Array<Complex> LofarConvolutionFunction::ApplyElementBeam2(Array<Complex>& input_grid, Double time, uInt spw, const Matrix<bool>& Mask_Mueller_in2, bool degridding_step, Int UsedMask)
+  {
+
+    Matrix<bool> Mask_Mueller_in(Mask_Mueller_in2.copy());
+    for(uInt i=0;i<4;++i){
+      for(uInt j=0;j<4;++j){
+    	Mask_Mueller_in(i,j)=true;
+      }
+    }
+    map<Double, vector< vector< Cube<Complex> > > >::const_iterator aiter_element = m_AtermStore_element.find(time);
+    AlwaysAssert (aiter_element!=m_AtermStore_element.end(), AipsError);
+    const vector< vector< Cube<Complex> > >& aterm_element = aiter_element->second;
+
+
+
+    vector< vector< IPosition > > Mueller_Coordinates;
+    Mueller_Coordinates.resize(4);
+    for(uInt i=0;i<4;++i){
+      Mueller_Coordinates[i].resize(4);
+      IPosition pos(2,2,1);
+      for(uInt j=0;j<4;++j){
+	Mueller_Coordinates[i][j]=pos;
+      }
+    }
+
+    {
+    uInt ind0;
+    uInt ind1;
+    uInt ii = 0;
+    IPosition cfShape;
+    Bool allElem = True;
+    UNUSED(allElem);
+    for (uInt row0=0; row0<=1; ++row0) {
+      for (uInt col0=0; col0<=1; ++col0) {
+	vector < Matrix<Complex> > Row(4);
+	vector < Matrix<Complex> > Row_non_padded(4);
+	uInt jj = 0;
+	for (uInt row1=0; row1<=1; ++row1) {
+	  for (uInt col1=0; col1<=1; ++col1) {
+	    // This Mueller ordering is for polarisation given as XX,XY,YX YY
+	    ind0 = row0 + 2*row1;
+	    ind1 = col0 + 2*col1;
+	    //ind0 = 2.*row0 + row1;
+	    //ind1 = 2.*col0 + col1;
+	    IPosition pos(2,2,1);
+	    pos[0]=ind0;
+	    pos[1]=ind1;
+	    Mueller_Coordinates[ii][jj]=pos;
+	    ++jj;
+	  }
+	}
+	++ii;
+      }
+    }
+    }
+
+    if (!degridding_step) {
+      for (uInt i=0; i<4; ++i) {
+	for (uInt j=i; j<4; ++j) {
+	  IPosition pos_tmp(Mueller_Coordinates[i][j]);
+	  Mueller_Coordinates[i][j]=Mueller_Coordinates[j][i];
+	  Mueller_Coordinates[j][i]=pos_tmp;
+	  Bool bool_tmp(Mask_Mueller_in(i,j));
+	  Mask_Mueller_in(i,j)=Mask_Mueller_in(j,i);
+	  Mask_Mueller_in(i,j)=bool_tmp;
+	}
+      }
+    }
+
+    Cube<Complex> aTermA(aterm_element[0][spw].copy());
+    Array<Complex> grid_out(input_grid.shape(),0.);
+    Int nx(input_grid.shape()[0]);
+    Int ny(input_grid.shape()[1]);
+    UNUSED(ny);
+    Int npol(input_grid.shape()[2]);
+
+    vector< vector< Matrix<Complex> > > vec_plane_product;
+    vec_plane_product.resize(4);
+
+
+    //logIO()<<"LofarConvolutionFunction::ApplyElementBeam "<<"Calculate element beams"<< LogIO::POST;//<<endl;
+    for(uInt ii=0;ii<4;++ii){
+      vec_plane_product[ii].resize(4);
+      for(uInt jj=0;jj<4;++jj){
+	if(Mask_Mueller_in(ii,jj)==true){
+	  vec_plane_product[ii][jj].resize(aTermA.xyPlane(0).shape());
+	  vec_plane_product[ii][jj]=aTermA.xyPlane((Mueller_Coordinates[ii][jj])[0]) * conj(aTermA.xyPlane((Mueller_Coordinates[ii][jj])[1]));
+	  taper(vec_plane_product[ii][jj]);
+	  if(!degridding_step){vec_plane_product[ii][jj]=conj(vec_plane_product[ii][jj]);};
+	  //store(vec_plane_product[ii][jj],"vec_plane_product."+String::toString(ii)+"."+String::toString(jj)+".img");
+	  normalized_fft(vec_plane_product[ii][jj],true);
+	}
+      }
+    }
+
+    //assert(false);
+    //#pragma omp parallel
+    if(GridsMueller.size()==0){
+      //logIO()<<"LofarConvolutionFunction::ApplyElementBeam "<<"...Declare GridsMueller Matrix"<< LogIO::POST;//<<endl;
+      GridsMueller.resize(4);
+      for(uInt ii=0;ii<4;++ii){
+	GridsMueller[ii].resize(4);
+	for(uInt jj=0;jj<4;++jj){
+	  if(Mask_Mueller_in(ii,jj)==true){
+	    GridsMueller[ii][jj].resize(IPosition(2,nx,nx));
+	    GridsMueller[ii][jj]=Complex();
+	  }
+	}
+      }
+    } else {
+      for(uInt ii=0;ii<4;++ii){
+    	for(uInt jj=0;jj<4;++jj){
+    	  if(Mask_Mueller_in(ii,jj)==true){
+    	    GridsMueller[ii][jj]=Complex();
+    	  }
+    	}
+      }
+    }
+
+    //logIO()<<"LofarConvolutionFunction::ApplyElementBeam "<<"Convolve..."<< LogIO::POST;//<<endl;
+    {
+      Int ii;
+      Int jj;
+#pragma omp parallel for private(ii,jj)
+      for(uInt iii=0;iii<16;++iii){
+	ii=floor(float(iii)/4.);
+	jj=floor((float(iii)/4.-ii)*4.);
+	//cout<<"iii"<<iii<<" "<<ii<<" "<<jj<<" M="<<Mask_Mueller_in(jj,ii)<<endl;
+	if(Mask_Mueller_in(ii,jj)==true){
+	  Matrix<Complex> ConvFunc(vec_plane_product[ii][jj]);
+
+	  //ConvolveGerArray(input_grid, ii, GridsMueller[ii][jj], ConvFunc);
+
+	  if(npol==1){
+	    if(!(UsedMask>-1)){
+	      ConvolveGerArray(input_grid, 0, GridsMueller[ii][jj], ConvFunc);
+	    } else {
+	      ConvolveGerArrayMask(input_grid, 0, GridsMueller[ii][jj], ConvFunc, UsedMask);
+	    }
+	  }
+	  if(npol==4){
+	    if(!(UsedMask>-1)){
+	      ConvolveGerArray(input_grid, ii, GridsMueller[ii][jj], ConvFunc);
+	    } else {
+	      ConvolveGerArrayMask(input_grid, ii, GridsMueller[ii][jj], ConvFunc, UsedMask);
+	    }
+	  }
+
+
+	}
+
+      }
+    }
+
+    //logIO()<<"LofarConvolutionFunction::ApplyElementBeam "<<"Convolve ... Done!"<< LogIO::POST;//<<endl;
+    // Int ii;
+    // Int jj;
+    // for(uInt iii=0;iii<16;++iii){
+    //   jj=floor(float(iii)/4.);
+    //   ii=floor((float(iii)/4.-jj)*4.);
+    //   //cout<<"iii"<<iii<<" "<<ii<<" "<<jj<<endl;
+    //   if(Mask_Mueller_in(ii,jj)==true){
+    // 	store(GridsMueller[ii][jj],"grid_out"+String::toString(ii)+String::toString(jj)+".img");
+    //   }
+    // }
+
+
+
+
+
+    //    #pragma omp parallel
+    if(npol==4)
+      {
+	int y=0;
+	uInt ii=0;
+	uInt jj=0;
+	#pragma omp parallel for private(y,ii,jj)
+	for(int x=0 ; x<nx ; ++x){
+	  //cout<<"x="<<x<<endl;
+	  for(y=0 ; y<nx ; ++y){
+
+	    for(ii=0;ii<4;++ii){
+	      for(jj=0;jj<4;++jj){
+		//if(Mask_Mueller_in(ii,jj)==true){
+		  grid_out(IPosition(4,x,y,jj,0)) += GridsMueller[ii][jj](x,y) ;///Spheroid_cut_im_element(x,y);
+
+		//}
+	      }
+	    }
+	  }
+	}
+      }
+
+
+    if(npol==1)
+      {
+    	int y=0;
+    	uInt ii=0;
+    	#pragma omp parallel for private(y,ii)
+    	for(int x=0 ; x<nx ; ++x){
+    	  for(y=0 ; y<nx ; ++y){
+    	    for(ii=0;ii<4;++ii){
+    	      grid_out(IPosition(4,x,y,0,0)) += 0.5*(GridsMueller[0][ii](x,y) + GridsMueller[3][ii](x,y));///Spheroid_cut_im_element(x,y);
+
+    	    }
+    	  }
+    	}
+      }
+
+
+
+    //logIO()<<"LofarConvolutionFunction::ApplyElementBeam "<<"Shapes InputGrid:"<<input_grid.shape()<<", Shapes OutputGrid:"<< LogIO::POST;//<<grid_out.shape()<<endl;
+
+
+    return grid_out;
   }
 
+  //==================================================================
+  //==================================================================
+
+
+
   //================================================
   // Compute the convolution function for all channel, for the polarisations specified in the Mueller_mask matrix
   // Also specify weither to compute the Mueller matrix for the forward or the backward step. A dirty way to calculate
@@ -307,34 +897,52 @@ namespace LOFAR
 
   LofarCFStore LofarConvolutionFunction::makeConvolutionFunction
   (uInt stationA, uInt stationB, Double time, Double w,
-   const Matrix<bool>& Mask_Mueller, bool degridding_step,
+   const Matrix<bool>& /*Mask_Mueller_in*/, bool degridding_step,
    double Append_average_PB_CF, Matrix<Complex>& Stack_PB_CF,
-   double& sum_weight_square)
+   double& sum_weight_square, uInt spw, Int /*TaylorTerm*/, double /*RefFreq*/)
   {
     // Initialize timers.
     PrecTimer timerFFT;
     PrecTimer timerPar;
+    PrecTimer timerCyril;
     timerPar.start();
 
+    Matrix<bool> Mask_Mueller(IPosition(2,4,4),false);
+    Mask_Mueller(0,0)=true;
+
     // Stack_PB_CF should be called Sum_PB_CF (it is a sum, no stack).
     CountedPtr<CFTypeVec> res (new vector< vector< vector < Matrix<Complex> > > >());
     CFTypeVec& result = *res;
     vector< vector< vector < Matrix<Complex> > > > result_non_padded;
 
+
     // Stack the convolution function if averagepb.img don't exist
-    Matrix<Complex> Stack_PB_CF_fft(IPosition(2,m_shape(0),m_shape(0)),0.);
+    //Matrix<Complex> Stack_PB_CF_fft(IPosition(2,m_shape(0),m_shape(0)),0.);
     Bool Stack = (Append_average_PB_CF != 0.);
 
+
+
     // If the beam is not in memory, compute it
-        
-    map<Double, vector< vector< Cube<Complex> > > >::const_iterator aiter =
-      m_AtermStore.find(time);
-    AlwaysAssert (aiter!=m_AtermStore.end(), AipsError);
-    const vector< vector< Cube<Complex> > >& aterm = aiter->second;
+
+    // map<Double, vector< vector< Cube<Complex> > > >::const_iterator aiter =
+    //   m_AtermStore.find(time);
+    // AlwaysAssert (aiter!=m_AtermStore.end(), AipsError);
+    // const vector< vector< Cube<Complex> > >& aterm = aiter->second;
+
+
+    map<Double, vector< vector< Cube<Complex> > > >::const_iterator aiter_station =
+      m_AtermStore_station.find(time);
+    AlwaysAssert (aiter_station!=m_AtermStore_station.end(), AipsError);
+    const vector< vector< Cube<Complex> > >& aterm_station = aiter_station->second;
+
+
+
+
     ///        if(m_AtermStore.find(time)==m_AtermStore.end()){computeAterm(time);}
 
     // Load the Wterm
-    uInt w_index = m_wScale.plane(w);
+    double ratio_freqs=list_freq[0]/list_freq[spw];
+    uInt w_index = m_wScale.plane(w*ratio_freqs);
     Matrix<Complex> wTerm;
     wTerm = m_WplanesStore[w_index];
     Int Npix_out = 0;
@@ -353,17 +961,36 @@ namespace LOFAR
       wTerm.reference (conj(wTerm));
     }
 
-    for (uInt ch=0; ch<m_nChannel; ++ch) {
+    uInt ch(spw);
+    //for (uInt ch=0; ch<m_nChannel; ++ch) {
       // Load the Aterm
-      const Cube<Complex>& aTermA(aterm[stationA][ch]);
-      const Cube<Complex>& aTermB(aterm[stationB][ch]);
+    //const Cube<Complex> aTermA(aterm_station[stationA][ch].copy());
+    Cube<Complex> aTermA(aterm_station[stationA][ch].copy());
+
+
+    //const Cube<Complex>& aTermB(aterm_station[stationB][ch]);
+    Cube<Complex> aTermB(aterm_station[stationB][ch].copy());
+      //==============================
+      //==============================
+      // Cyr: MFS
+      //==============================
+      //==============================
+      // if( TaylorTerm > 0 )
+      // 	{
+      // 	  Float freq=0.0,mulfactor=1.0;
+      // 	  freq = list_freq[ch];
+      // 	  mulfactor = ((freq-RefFreq)/RefFreq);
+      // 	  //cout<<"mulfactor "<<mulfactor<<endl;
+      // 	  Cube<Complex> slice(aTermA);
+      // 	  slice *= pow(mulfactor,TaylorTerm);//mulfactor;
+      // 	}
+      //==============================
+      //==============================
       // Determine maximum support of A, W, and Spheroidal function for zero padding
       Npix_out = std::max(std::max(aTermA.shape()[0], aTermB.shape()[0]),
                           std::max(wTerm.shape()[0], Spheroid_cut.shape()[0]));
-      if (itsVerbose > 0) {
-        cout<<"Number of pixel in the final conv function for baseline ["<< stationA<<", "<<stationB<<"] = "<<Npix_out
-            <<" "<<aTermA.shape()[0]<<" "<<aTermB.shape()[0]<<" "<<wTerm.shape()[0]<<endl;
-      }
+
+      //cout << "CF Shapes, Wterm:" << wTerm.shape()[0] << ", Beam " << aTermA.shape()[0] << ", Spheroid: " << Spheroid_cut.shape()[0] << endl;
 
       // Zero pad to make the image planes of the A1, A2, and W term have the same resolution in the image plane
       Matrix<Complex> Spheroid_cut_paddedf(zero_padding(Spheroid_cut,Npix_out));
@@ -378,10 +1005,9 @@ namespace LOFAR
         cout << "fft shapes " << wTerm_paddedf.shape() << ' ' << Spheroid_cut_paddedf.shape()
              << ' ' << aTermA_padded.shape() << ' ' << aTermB_padded.shape() << endl;
       }
+
+
       for (uInt i=0; i<4; ++i) {
-        //Matrix<Complex> planeAf(aTermA_padded.xyPlane(i));
-        //Matrix<Complex> planeBf(aTermB_padded.xyPlane(i));
-        // AlwaysAssert(planeAf.contiguousStorage(), AipsError);
         // Make a matrix referencing the data in the cube's plane.
         Matrix<Complex> planeAf(aTermA_padded.xyPlane(i));
         Matrix<Complex> planeBf(aTermB_padded.xyPlane(i));
@@ -390,30 +1016,62 @@ namespace LOFAR
         normalized_fft (timerFFT, planeBf, false);
       }
 
+      //if (itsVerbose > 0) {
+      // if((stationA==3)&( stationB==0)){
+      //   cout<<"Number of pixel in the final conv function for baseline ["<< stationA<<", "<<stationB<<"] = "<<Npix_out
+      //       <<" "<<aTermA.shape()[0]<<" "<<aTermB.shape()[0]<<" "<<wTerm.shape()[0]<<endl;
+      // 	store(aTermA,"aTermAfft."+String::toString(its_count_time)+".img");
+      // 	store(aTermB,"aTermBfft."+String::toString(its_count_time)+".img");
+      // 	store(aTermA_padded,"aTermAfft."+String::toString(its_count_time)+".fft.img");
+      // 	store(aTermB_padded,"aTermBfft."+String::toString(its_count_time)+".fft.img");
+      // 	its_count_time+=1;
+      // 	//assert(false);
+      // }
       // Create the vectors of Matrices giving the convolution functions
       // for each Mueller element.
       vector< vector < Matrix<Complex> > > Kron_Product;
       Kron_Product.reserve(4);
 
-      // Something I still don't completely understand: for the average PB calculation.
+      // timerCyril.reset();
+      // timerCyril.start();
+      // //TEST!!!!
+      // Matrix<Complex> Spheroid_cut_paddedf(LinearInterpol(Spheroid_cut,Npix_out));
+      // Matrix<Complex> wTerm_paddedf(LinearInterpol(wTerm, Npix_out));
+      // Cube<Complex> aTermA_padded(IPosition(3,Npix_out,Npix_out,4),0.);
+      // Cube<Complex> aTermB_padded(IPosition(3,Npix_out,Npix_out,4),0.);
+      // for (uInt i=0; i<4; ++i) {
+      // 	Matrix<Complex> planeAf=aTermA_padded.xyPlane(i);
+      // 	Matrix<Complex> planeAf=aTermA_padded.xyPlane(i);
+      //   planeAf=LinearInterpol(aTermA.xyPlane(i), Npix_out);
+      //   planeBf=LinearInterpol(aTermA.xyPlane(i), Npix_out);
+      // }
+      // timerCyril.stop();
+      // timerCyril.show(cout,"linear");
+
+
+
+     // Something I still don't completely understand: for the average PB calculation.
       // The convolution functions padded with a higher value than the minimum one give a
       // better result in the end. If you try Npix_out2=Npix_out, then the average PB shows
       // structure like aliasing, producing high values in the devided disrty map... This
       // is likely to be due to the way fft works?...
-      // FIX: I now do the average of the PB by stacking the CF, FFT the result and square 
-      // it in the end. This is not the way to do in principle but the result is almost the 
+      // FIX: I now do the average of the PB by stacking the CF, FFT the result and square
+      // it in the end. This is not the way to do in principle but the result is almost the
       // same. It should pose no problem I think.
       Matrix<Complex> Spheroid_cut_padded2f;
+      Matrix<Complex> spheroid_cut_element_fft2;
       Cube<Complex> aTermA_padded2;
       Cube<Complex> aTermB_padded2;
 
       // Keep the non-padded convolution functions for average PB calculation.
       vector< vector < Matrix<Complex> > > Kron_Product_non_padded;
       Kron_Product_non_padded.reserve(4);
-	    
+
       if (Stack) {
         Npix_out2 = Npix_out;
         Spheroid_cut_padded2f = zero_padding(Spheroid_cut, Npix_out2);
+	spheroid_cut_element_fft2 = zero_padding(spheroid_cut_element_fft, Npix_out2);
+	normalized_fft (timerFFT, spheroid_cut_element_fft2, false);
         aTermA_padded2 = zero_padding(aTermA, Npix_out2);
         aTermB_padded2 = zero_padding(aTermB, Npix_out2);
         normalized_fft (timerFFT, Spheroid_cut_padded2f, false);
@@ -444,13 +1102,16 @@ namespace LOFAR
               // Compute the convolution function for the given Mueller element
               if (Mask_Mueller(ii,jj)) {
                 // Padded version for oversampling the convolution function
-                Matrix<Complex> plane_product (aTermB_padded.xyPlane(ind0) *
+
+                Matrix<Complex> plane_product (conj(aTermB_padded.xyPlane(ind0)) *
                                                aTermA_padded.xyPlane(ind1));
                 plane_product *= wTerm_paddedf;
                 plane_product *= Spheroid_cut_paddedf;
+
                 Matrix<Complex> plane_product_paddedf
                   (zero_padding(plane_product,
                                 plane_product.shape()[0] * m_oversampling));
+
                 normalized_fft (timerFFT, plane_product_paddedf);
 
                 plane_product_paddedf *= static_cast<Float>(m_oversampling *
@@ -471,6 +1132,7 @@ namespace LOFAR
                   Matrix<Complex> plane_productf(aTermB_padded2.xyPlane(ind0)*
                                                  aTermA_padded2.xyPlane(ind1));
                   plane_productf *= Spheroid_cut_padded2f;
+		  if(its_Apply_Element){plane_productf *= spheroid_cut_element_fft2;}
                   normalized_fft (timerFFT, plane_productf);
                   Row_non_padded[jj].reference (plane_productf);
                 }
@@ -493,7 +1155,12 @@ namespace LOFAR
       if (degridding_step) {
         for (uInt i=0; i<4; ++i) {
           for (uInt j=i; j<4; ++j) {
-            AlwaysAssert (Mask_Mueller(i,j) == Mask_Mueller(j,i), AipsError);
+            //AlwaysAssert (Mask_Mueller(i,j) == Mask_Mueller(j,i), AipsError);
+	    if ((Mask_Mueller(i,j)==false)&&(Mask_Mueller(j,i)==true)){
+	      Matrix<Complex> a(Kron_Product[i][j].copy());
+	      a=0.;
+	      Kron_Product[i][j]=a.copy();
+	    };
             if (Mask_Mueller(i,j)) {
               if (i!=j) {
                 Matrix<Complex> conj_product(conj(Kron_Product[i][j]));
@@ -523,8 +1190,8 @@ namespace LOFAR
       if (Stack) {
         result_non_padded.push_back(Kron_Product_non_padded);
       }
-    }
-          
+      //}
+
     // Stacks the weighted quadratic sum of the convolution function of
     // average PB estimate (!!!!! done for channel 0 only!!!)
     if (Stack) {
@@ -552,7 +1219,7 @@ namespace LOFAR
         }
       }
     }
-	
+
     // Put the resulting vec(vec(vec))) in a LofarCFStore object
     CoordinateSystem csys;
     Vector<Float> samp(2, m_oversampling);
@@ -575,12 +1242,14 @@ namespace LOFAR
 #pragma omp atomic
     itsTimeCFpar += ptime;
 
+
     return LofarCFStore (res, csys, samp,  xsup, ysup, maxXSup, maxYSup,
                          PA, mosPointing, Mask_Mueller);
   }
 
+
   //================================================
-      
+
   // Returns the average Primary Beam from the disk
   Matrix<Float> LofarConvolutionFunction::Give_avg_pb()
   {
@@ -615,21 +1284,35 @@ namespace LOFAR
         cout<<"..... Compute average PB"<<endl;
       }
       Sum_Stack_PB_CF /= float(sum_weight_square);
-      //store(Stack_PB_CF,"Stack_PB_CF.img");
+      //store(Sum_Stack_PB_CF,"Stack_PB_CF.img");
 
       normalized_fft(Sum_Stack_PB_CF, false);
-      //store(Im_Stack_PB_CF00,"Im_Stack_PB_CF00.img");
+      //store(Sum_Stack_PB_CF,"Im_Stack_PB_CF00.img");
+      //store(Sum_Stack_PB_CF, itsImgName + ".before");
       Im_Stack_PB_CF0.resize (IPosition(2, m_shape[0], m_shape[1]));
-	
-      float threshold = 1.e-6;
+
+      float maxPB(0.);
+      float maxPB_noabs(0.);
+      UNUSED(maxPB_noabs);
+      for(uInt i=0;i<m_shape[1];++i){
+	for(uInt j=0;j<m_shape[1];++j){
+	    Complex pixel(Sum_Stack_PB_CF(i,j));
+	    if(abs(pixel)>maxPB){
+	      maxPB=abs(pixel);
+	      //maxPB_noabs=pixel;
+	    };
+	}
+      }
+      float threshold = 1.e-8;
       for (Int jj=0; jj<m_shape[1]; ++jj) {
         for (Int ii=0; ii<m_shape[0]; ++ii) {
           Float absVal = abs(Sum_Stack_PB_CF(ii,jj));
-          Im_Stack_PB_CF0(ii,jj) = std::max (absVal*absVal, threshold);
+          Im_Stack_PB_CF0(ii,jj) = std::max (absVal*absVal, threshold*maxPB);
+	  //Im_Stack_PB_CF0(ii,jj) = sqrt(Im_Stack_PB_CF0(ii,jj))*sign(maxPB_noabs);
         }
       }
       // Make it persistent.
-      store(Im_Stack_PB_CF0, itsImgName + ".avgpb");
+      store(m_coordinates,Im_Stack_PB_CF0, itsImgName + ".avgpb");
     }
     return Im_Stack_PB_CF0;
   }
@@ -642,15 +1325,17 @@ namespace LOFAR
     if (Image.shape()[0] == Npixel_Out) {
       return Image.copy();
     }
-    if ((Npixel_Out%2) != 1) {
-      Npixel_Out++;
-    }
+
+    // if ((Npixel_Out%2) != 1) {
+    //   Npixel_Out++;
+    // }
+
     Cube<Complex> Image_Enlarged(Npixel_Out,Npixel_Out,Image.shape()[2]);
     uInt Dii = Image.shape()(0)/2;
     uInt Start_image_enlarged=Npixel_Out/2-Dii; //Is an even number, Assume square image
-    if ((Start_image_enlarged-floor(Start_image_enlarged))!=0.) {
-      Start_image_enlarged += 0.5; //If number of pixel odd then 0th order at the center, shifted by one otherwise
-    }
+    // if ((Start_image_enlarged-floor(Start_image_enlarged))!=0.) {
+    //   Start_image_enlarged += 0.5; //If number of pixel odd then 0th order at the center, shifted by one otherwise
+    // }
     /* cout<<Start_image_enlarged<<"  "<<floor(Start_image_enlarged)<<endl; */
     /* if((Start_image_enlarged-floor(Start_image_enlarged))!=0.){ */
     /*   cout<<"Not even!!!"<<endl; */
@@ -681,9 +1366,6 @@ namespace LOFAR
     if (Image.shape()[0] == Npixel_Out) {
       return Image.copy();
     }
-    if (Npixel_Out%2 != 1) {
-      Npixel_Out++;
-    }
     IPosition shape_im_out(2, Npixel_Out, Npixel_Out);
     Matrix<Complex> Image_Enlarged(shape_im_out, 0.);
 
@@ -695,9 +1377,9 @@ namespace LOFAR
     uInt Start_image_enlarged = shape_im_out[0]/2-Dii;
     //Is an even number, Assume square image
     //If number of pixel odd then 0th order at the center, shifted by one otherwise
-    if ((Start_image_enlarged-floor(Start_image_enlarged))!=0.) {
-      Start_image_enlarged += 0.5;
-    }
+    // if ((Start_image_enlarged-floor(Start_image_enlarged))!=0.) {
+    //   Start_image_enlarged += 0.5;
+    // }
 
     /* cout<<Start_image_enlarged<<"  "<<floor(Start_image_enlarged)<<endl; */
     /* if((Start_image_enlarged-floor(Start_image_enlarged))!=0.){ */
@@ -705,7 +1387,7 @@ namespace LOFAR
     /*   Start_image_enlarged+=0.5;} */
     for (Int jj=0; jj<Image.shape()[1]; ++jj) {
       for (Int ii=0; ii<Image.shape()[0]; ++ii) {
-        Image_Enlarged(Start_image_enlarged+ii,Start_image_enlarged+jj) = 
+        Image_Enlarged(Start_image_enlarged+ii,Start_image_enlarged+jj) =
           ratio*Image(ii,jj);
       }
     }
@@ -716,6 +1398,7 @@ namespace LOFAR
   void LofarConvolutionFunction::normalized_fft
   (Matrix<Complex> &im, bool toFreq)
   {
+    //cout<<" "<<im.ncolumn()<<" "<<im.nrow()<<" "<<im.size()<<" "<<im.contiguousStorage()<<endl;
     AlwaysAssert (im.ncolumn() == im.nrow()  &&  im.size() > 0  &&
                   im.contiguousStorage(), AipsError);
     int tnr = OpenMP::threadNum();
@@ -746,28 +1429,6 @@ namespace LOFAR
     return observation.timeRangeMeas()(0)(IPosition(1, 0));
   }
 
-  //=================================================
-  Double LofarConvolutionFunction::observationReferenceFreq
-  (const MeasurementSet &ms, uInt idDataDescription)
-  {
-    // Read polarization id and spectral window id.
-    ROMSDataDescColumns desc(ms.dataDescription());
-    AlwaysAssert(desc.nrow() > idDataDescription, SynthesisError);
-    AlwaysAssert(!desc.flagRow()(idDataDescription), SynthesisError);
-
-    const uInt idWindow = desc.spectralWindowId()(idDataDescription);
-
-    /*        logIO() << LogOrigin("LofarATerm", "initReferenceFreq") << LogIO::NORMAL
-              << "spectral window: " << desc.spectralWindowId()(idDataDescription) << LogIO::POST;*/
-    //            << "spectral window: " << desc.spectralWindowId() << LogIO::POST;
-    // Get spectral information.
-    ROMSSpWindowColumns window(ms.spectralWindow());
-    AlwaysAssert(window.nrow() > idWindow, SynthesisError);
-    AlwaysAssert(!window.flagRow()(idWindow), SynthesisError);
-
-    return window.refFrequency()(idWindow);
-  }
-
   //=================================================
   // Estime spheroidal convolution function from the support of the fft of the spheroidal in the image plane
 
@@ -816,7 +1477,7 @@ namespace LOFAR
     store(Spheroid_cut_im, itsImgName + ".spheroid_cut_im");
     if (itsVerbose > 0) {
       store(Spheroid_cut, itsImgName + ".spheroid_cut");
-    }	
+    }
     return Pixel_Size_Spheroidal;
   }
 
@@ -869,12 +1530,12 @@ namespace LOFAR
   // Return the angular resolution required for making the image of the angular size determined by
   // coordinates and shape. The resolution is assumed to be the same on both direction axes.
   Double LofarConvolutionFunction::estimateAResolution
-  (const IPosition &shape, const DirectionCoordinate &coordinates) const
+  (const IPosition &shape, const DirectionCoordinate &coordinates, double station_diam) const
   {
     Double res_ini=abs(coordinates.increment()(0));                      // pixel size in image in radian
     Double diam_image=res_ini*shape(0);                                  // image diameter in radian
-    Double station_diam = 70.;                                           // station diameter in meters: To be adapted to the individual station size.
-    Double Res_beam_image= ((C::c/m_refFrequency)/station_diam)/2.;      // pixel size in A-term image in radian
+    //Double station_diam = 70.;                                           // station diameter in meters: To be adapted to the individual station size.
+    Double Res_beam_image= 0.5*((C::c/m_refFrequency)/station_diam)/2.;      // pixel size in A-term image in radian
     uInt Npix=floor(diam_image/Res_beam_image);                         // Number of pixel size in A-term image
     Res_beam_image=diam_image/Npix;
     if (Npix%2 != 1) {
@@ -920,7 +1581,7 @@ namespace LOFAR
       bot += Q[part][k] * delnusqPow;
       delnusqPow *= delnusq;
     }
-	
+
     double result = (bot == 0  ?  0 : (1.0 - nusq) * (top / bot));
     //if(result<1.e-3){result=1.e-3;}
     return result;
diff --git a/CEP/Imager/LofarFT/src/LofarConvolutionFunctionOld.cc b/CEP/Imager/LofarFT/src/LofarConvolutionFunctionOld.cc
new file mode 100644
index 0000000000000000000000000000000000000000..de9946a8da70cd2c4ff4853c8d6773be83944b9f
--- /dev/null
+++ b/CEP/Imager/LofarFT/src/LofarConvolutionFunctionOld.cc
@@ -0,0 +1,965 @@
+//# LofarConvolutionFunctionOld.cc: Compute the LOFAR convolution function
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#include <lofar_config.h>
+#include <LofarFT/LofarConvolutionFunctionOld.h>
+#include <LofarFT/LofarConvolutionFunction.h>   // needed for store function
+#include <Common/LofarLogger.h>
+#include <Common/OpenMP.h>
+
+#include <casa/Logging/LogIO.h>
+#include <casa/Logging/LogOrigin.h>
+#include <casa/Arrays/Cube.h>
+#include <casa/Arrays/Matrix.h>
+#include <casa/Arrays/MatrixMath.h>
+#include <casa/Arrays/ArrayMath.h>
+
+#include <ms/MeasurementSets/MeasurementSet.h>
+#include <measures/Measures/MDirection.h>
+#include <measures/Measures/MeasConvert.h>
+#include <measures/Measures/MCDirection.h>
+#include <measures/Measures/MCPosition.h>
+#include <ms/MeasurementSets/MSAntenna.h>
+#include <ms/MeasurementSets/MSAntennaParse.h>
+#include <ms/MeasurementSets/MSAntennaColumns.h>
+#include <ms/MeasurementSets/MSDataDescription.h>
+#include <ms/MeasurementSets/MSDataDescColumns.h>
+#include <ms/MeasurementSets/MSField.h>
+#include <ms/MeasurementSets/MSFieldColumns.h>
+#include <ms/MeasurementSets/MSObservation.h>
+#include <ms/MeasurementSets/MSObsColumns.h>
+#include <ms/MeasurementSets/MSPolarization.h>
+#include <ms/MeasurementSets/MSPolColumns.h>
+#include <ms/MeasurementSets/MSSpectralWindow.h>
+#include <ms/MeasurementSets/MSSpWindowColumns.h>
+#include <ms/MeasurementSets/MSSelection.h>
+#include <measures/Measures/MeasTable.h>
+#include <coordinates/Coordinates/CoordinateSystem.h>
+#include <coordinates/Coordinates/SpectralCoordinate.h>
+#include <coordinates/Coordinates/StokesCoordinate.h>
+#include <casa/OS/PrecTimer.h>
+#include <casa/sstream.h>
+#include <iomanip>
+
+namespace LOFAR
+{
+
+  LofarConvolutionFunctionOld::LofarConvolutionFunctionOld
+  (const IPosition& shape,
+   const DirectionCoordinate& coordinates,
+   const MeasurementSet& ms,
+   uInt nW, double Wmax,
+   uInt oversample,
+   const String& beamElementPath,
+   Int verbose,
+   Int maxsupport,
+   const String& imgName)
+    : m_shape(shape),
+      m_coordinates(coordinates),
+      m_aTerm(ms, beamElementPath),
+      m_maxW(Wmax), //maximum W set by ft machine to flag the w>wmax
+      m_nWPlanes(nW),
+      m_oversampling(oversample),
+      itsVerbose (verbose),
+      itsMaxSupport(maxsupport),
+      itsImgName(imgName),
+      itsTimeW    (0),
+      itsTimeWpar (0),
+      itsTimeWfft (0),
+      itsTimeWcnt (0),
+      itsTimeA    (0),
+      itsTimeApar (0),
+      itsTimeAfft (0),
+      itsTimeAcnt (0),
+      itsTimeCFpar(0),
+      itsTimeCFfft(0),
+      itsTimeCFcnt(0)
+      //Not sure how useful that is
+  {
+    if (itsVerbose > 0) {
+      cout<<"LofarConvolutionFunctionOld:shape  "<<shape<<endl;
+    }
+    itsFFTMachines.resize (OpenMP::maxThreads());
+
+    //    m_maxCFSupport=0; //need this parameter to stack all the CF for average PB estimate
+
+    m_wScale = WScale(m_maxW, m_nWPlanes);
+    MEpoch start = observationStartTime(ms, 0);
+
+    m_refFrequency = observationReferenceFreq(ms, 0);
+
+    if (m_oversampling%2 == 0) {
+      // Make OverSampling an odd number
+      m_oversampling++;
+    }
+
+    list_freq   = Vector<Double>(1, m_refFrequency);
+    m_nChannel  = list_freq.size();
+    ROMSAntennaColumns antenna(ms.antenna());
+    m_nStations = antenna.nrow();
+
+    m_pixelSizeSpheroidal = makeSpheroidCut();
+    //Double PixelSize=abs(m_coordinates.increment()(0));
+    //Double ImageDiameter=PixelSize * m_shape(0);
+    //Double W_Pixel_Ang_Size=min(Pixel_Size_Spheroidal,estimateWResolution(m_shape, m_coordinates, m_maxW));
+    //m_maxCFSupport= ImageDiameter / W_Pixel_Ang_Size;
+    //Matrix<Complex> Stack_pb_cf0(IPosition(2,m_maxCFSupport,m_maxCFSupport),0.);
+    Matrix<Complex> Stack_pb_cf0(IPosition(2,m_shape(0),m_shape(0)),0.);
+    Matrix<float> Stack_pb_cf1(IPosition(2,m_shape(0),m_shape(0)),0.);
+
+    //Stack_pb_cf0(256,300)=1.;
+    //Matrix<Complex> Avg_PB_padded00(give_normalized_fft(Stack_pb_cf0,false));
+    //store(Avg_PB_padded00,"Avg_PB_padded00.img");
+
+    // Precalculate the Wtwerm fft for all w-planes.
+    store_all_W_images();
+  }
+
+  //      ~LofarConvolutionFunctionOld ()
+  //      {
+  //      }
+
+  // Precalculate all W-terms in the fourier domain
+  void LofarConvolutionFunctionOld::store_all_W_images()
+  {
+    PrecTimer wTimer;
+    wTimer.start();
+    Double pixelSize = abs(m_coordinates.increment()[0]);
+    Double imageDiameter = pixelSize * m_shape(0);
+    // Size the vector, but give each element its own default matrix,
+    // so the vector can be safely filled in parallel.
+    m_WplanesStore.reserve (m_nWPlanes);
+    for (uInt i=0; i<m_nWPlanes; ++i) {
+      m_WplanesStore.push_back (Matrix<Complex>());
+    }
+#pragma omp parallel
+    {
+      // Thread private variables.
+      PrecTimer timerFFT;
+      PrecTimer timerPar;
+#pragma omp for schedule(dynamic)
+      for (uInt i=0; i<m_nWPlanes; ++i) {
+        timerPar.start();
+        Double w = m_wScale.center(i);
+        Double wPixelAngSize = min(m_pixelSizeSpheroidal,
+                                   estimateWResolution(m_shape,
+                                                       pixelSize, w));
+        Int nPixelsConv = imageDiameter / wPixelAngSize;
+        if (itsVerbose > 0) {
+          cout<<"Number of pixel in the "<<i<<"-wplane: "<<nPixelsConv
+              <<"  (w="<<w<<")"<<endl;
+        }
+        if (nPixelsConv > itsMaxSupport) {
+          nPixelsConv = itsMaxSupport;
+        }
+        // Make odd and optimal.
+        nPixelsConv = FFTCMatrix::optimalOddFFTSize (nPixelsConv);
+        wPixelAngSize = imageDiameter / nPixelsConv;
+        IPosition shape(2, nPixelsConv, nPixelsConv);
+        //Careful with the sign of increment!!!! To check!!!!!!!
+        Vector<Double> increment(2, wPixelAngSize);
+        double wavelength(C::c / list_freq[0]);
+        Matrix<Complex> wTerm = m_wTerm.evaluate(shape, increment,
+                                                 w/wavelength);
+        normalized_fft(timerFFT, wTerm);
+        m_WplanesStore[i].reference (wTerm);
+        timerPar.stop();
+      }
+      // Update the timing info.
+      double ftime = timerFFT.getReal();
+#pragma omp atomic
+      itsTimeWfft += ftime;
+      unsigned long long cnt = timerFFT.getCount();
+#pragma omp atomic
+      itsTimeWcnt += cnt;
+      double ptime = timerPar.getReal();
+#pragma omp atomic
+      itsTimeWpar += ptime;
+    } // end omp parallel
+    wTimer.stop();
+    itsTimeW = wTimer.getReal();
+  }
+
+
+  // Compute the fft of the beam at the minimal resolution for all antennas
+  // if not done yet.
+  // Put it in a map object with a (double time) key.
+  void LofarConvolutionFunctionOld::computeAterm (Double time)
+  {
+    if (m_AtermStore.find(time) != m_AtermStore.end()) {
+      // Already done.
+      return;
+    }
+    PrecTimer aTimer;
+    aTimer.start();
+    Double pixelSize = abs(m_coordinates.increment()[0]);
+    Double imageDiameter = pixelSize * m_shape(0);
+    // Try to avoid making copies when inserting elements in vector or map.
+    // Therefore first create the elements and resize them.
+    m_AtermStore[time] = vector< vector< Cube<Complex> > >();
+    vector< vector< Cube<Complex> > >& aTermList = m_AtermStore[time];
+    // Calculate the A-term and fill the vector for all stations.
+    aTermList.resize (m_nStations);
+    ///#pragma omp parallel
+    {
+      // Thread private variables.
+      PrecTimer timerFFT;
+      PrecTimer timerPar;
+      ///#pragma omp for
+      for (uInt i=0; i<m_nStations; ++i) {
+        timerPar.start();
+	DirectionCoordinate coordinate = m_coordinates;
+        Double aPixelAngSize = min(m_pixelSizeSpheroidal,
+                                   estimateAResolution(m_shape, m_coordinates));
+        Int nPixelsConv = imageDiameter / aPixelAngSize;
+        if (nPixelsConv > itsMaxSupport) {
+          nPixelsConv = itsMaxSupport;
+        }
+        // Make odd and optimal.
+        nPixelsConv = FFTCMatrix::optimalOddFFTSize (nPixelsConv);
+        aPixelAngSize = imageDiameter / nPixelsConv;
+        if (itsVerbose > 0) {
+          cout.precision(20);
+          cout<<"Number of pixel in the Aplane of "<<i<<": "<<nPixelsConv
+              <<", time="<<fixed<<time<<endl;
+        }
+        IPosition shape(2, nPixelsConv, nPixelsConv);
+        Vector<Double> increment_old(coordinate.increment());
+        Vector<Double> increment(2);
+        increment[0] = aPixelAngSize*sign(increment_old[0]);
+        increment[1] = aPixelAngSize*sign(increment_old[1]);
+        coordinate.setIncrement(increment);
+        Vector<Double> refpix(2, 0.5*(nPixelsConv-1));
+        coordinate.setReferencePixel(refpix);
+
+        //======================================
+        // Disable the beam
+        //======================================
+        //Cube<Complex> aterm_cube(IPosition(3,nPixels_Conv,nPixels_Conv,4),1.);
+        //for (uInt iiii=0;iiii<nPixels_Conv;++iiii) {
+        //  for (uInt iiiii=0;iiiii<nPixels_Conv;++iiiii) {
+        //    aterm_cube(iiii,iiiii,1)=0.;
+        //    aterm_cube(iiii,iiiii,2)=0.;
+        //  }
+        //}
+        //vector< Cube<Complex> > aTermA;
+        //aTermA.push_back(aterm_cube);
+        //======================================
+        // Enable the beam
+        //======================================
+        MEpoch binEpoch;
+        binEpoch.set(Quantity(time, "s"));
+        vector< Cube<Complex> > aTermA = m_aTerm.evaluate(shape,
+                                                          coordinate,
+                                                          i, binEpoch,
+                                                          list_freq, true);
+        // Compute the fft on the beam
+        for (uInt ch=0; ch<m_nChannel; ++ch) {
+          for (uInt pol=0; pol<4; ++pol) {
+            Matrix<Complex> plane (aTermA[ch].xyPlane(pol));
+            AlwaysAssert (plane.contiguousStorage(), AipsError);
+            normalized_fft (timerFFT, plane);
+          }
+        }
+        // Note that push_back uses the copy constructor, so for the Cubes
+        // in the vector the copy constructor is called too (which is cheap).
+        aTermList[i] = aTermA;
+        timerPar.stop();
+      } // end omp for
+      // Update the timing info.
+      double ftime = timerFFT.getReal();
+      ///#pragma omp atomic
+      itsTimeAfft += ftime;
+      unsigned long long cnt = timerFFT.getCount();
+      ///#pragma omp atomic
+      itsTimeAcnt += cnt;
+      double ptime = timerPar.getReal();
+      ///#pragma omp atomic
+      itsTimeApar += ptime;
+    } // end omp parallel
+    aTimer.stop();
+    itsTimeA = aTimer.getReal();
+  }
+
+  //================================================
+  // Compute the convolution function for all channel, for the polarisations specified in the Mueller_mask matrix
+  // Also specify weither to compute the Mueller matrix for the forward or the backward step. A dirty way to calculate
+  // the average beam has been implemented, by specifying the beam correcping to the given baseline and timeslot.
+  // RETURNS in a LofarCFStore: result[channel][Mueller row][Mueller column]
+
+  LofarCFStore LofarConvolutionFunctionOld::makeConvolutionFunction
+  (uInt stationA, uInt stationB, Double time, Double w,
+   const Matrix<bool>& Mask_Mueller, bool degridding_step,
+   double Append_average_PB_CF, Matrix<Complex>& Stack_PB_CF,
+   double& sum_weight_square)
+  {
+    // Initialize timers.
+    PrecTimer timerFFT;
+    PrecTimer timerPar;
+    timerPar.start();
+
+    // Stack_PB_CF should be called Sum_PB_CF (it is a sum, no stack).
+    CountedPtr<CFTypeVec> res (new vector< vector< vector < Matrix<Complex> > > >());
+    CFTypeVec& result = *res;
+    vector< vector< vector < Matrix<Complex> > > > result_non_padded;
+
+    // Stack the convolution function if averagepb.img don't exist
+    Matrix<Complex> Stack_PB_CF_fft(IPosition(2,m_shape(0),m_shape(0)),0.);
+    Bool Stack = (Append_average_PB_CF != 0.);
+
+    // If the beam is not in memory, compute it
+        
+    map<Double, vector< vector< Cube<Complex> > > >::const_iterator aiter =
+      m_AtermStore.find(time);
+    AlwaysAssert (aiter!=m_AtermStore.end(), AipsError);
+    const vector< vector< Cube<Complex> > >& aterm = aiter->second;
+    ///        if(m_AtermStore.find(time)==m_AtermStore.end()){computeAterm(time);}
+
+    // Load the Wterm
+    uInt w_index = m_wScale.plane(w);
+    Matrix<Complex> wTerm;
+    wTerm = m_WplanesStore[w_index];
+    Int Npix_out = 0;
+    Int Npix_out2 = 0;
+
+    // Matrix<Complex> Term_test(IPosition(2,101,101),1.);
+    // normalized_fft(Term_test);
+    // store (Term_test,"Term_test.img");
+    // normalized_fft(Term_test,false);
+    // store (Term_test,"Term_test_0.img");
+    // normalized_fft(Term_test);
+    // store (Term_test,"Term_test_1.img");
+    // assert(false);
+
+    if (w > 0.) {
+      wTerm.reference (conj(wTerm));
+    }
+
+    for (uInt ch=0; ch<m_nChannel; ++ch) {
+      // Load the Aterm
+      const Cube<Complex>& aTermA(aterm[stationA][ch]);
+      const Cube<Complex>& aTermB(aterm[stationB][ch]);
+      // Determine maximum support of A, W, and Spheroidal function for zero padding
+      Npix_out = std::max(std::max(aTermA.shape()[0], aTermB.shape()[0]),
+                          std::max(wTerm.shape()[0], Spheroid_cut.shape()[0]));
+      if (itsVerbose > 0) {
+        cout<<"Number of pixel in the final conv function for baseline ["<< stationA<<", "<<stationB<<"] = "<<Npix_out
+            <<" "<<aTermA.shape()[0]<<" "<<aTermB.shape()[0]<<" "<<wTerm.shape()[0]<<endl;
+      }
+
+      // Zero pad to make the image planes of the A1, A2, and W term have the same resolution in the image plane
+      Matrix<Complex> Spheroid_cut_paddedf(zero_padding(Spheroid_cut,Npix_out));
+      Matrix<Complex> wTerm_paddedf(zero_padding(wTerm, Npix_out));
+      Cube<Complex> aTermA_padded(zero_padding(aTermA, Npix_out));
+      Cube<Complex> aTermB_padded(zero_padding(aTermB, Npix_out));
+
+      // FFT (backward) the A and W terms
+      normalized_fft (timerFFT, wTerm_paddedf, false);
+      normalized_fft (timerFFT, Spheroid_cut_paddedf, false);
+      if (itsVerbose > 0) {
+        cout << "fft shapes " << wTerm_paddedf.shape() << ' ' << Spheroid_cut_paddedf.shape()
+             << ' ' << aTermA_padded.shape() << ' ' << aTermB_padded.shape() << endl;
+      }
+      for (uInt i=0; i<4; ++i) {
+        //Matrix<Complex> planeAf(aTermA_padded.xyPlane(i));
+        //Matrix<Complex> planeBf(aTermB_padded.xyPlane(i));
+        // AlwaysAssert(planeAf.contiguousStorage(), AipsError);
+        // Make a matrix referencing the data in the cube's plane.
+        Matrix<Complex> planeAf(aTermA_padded.xyPlane(i));
+        Matrix<Complex> planeBf(aTermB_padded.xyPlane(i));
+        AlwaysAssert(planeAf.contiguousStorage(), AipsError);
+        normalized_fft (timerFFT, planeAf, false);
+        normalized_fft (timerFFT, planeBf, false);
+      }
+
+      // Create the vectors of Matrices giving the convolution functions
+      // for each Mueller element.
+      vector< vector < Matrix<Complex> > > Kron_Product;
+      Kron_Product.reserve(4);
+
+      // Something I still don't completely understand: for the average PB calculation.
+      // The convolution functions padded with a higher value than the minimum one give a
+      // better result in the end. If you try Npix_out2=Npix_out, then the average PB shows
+      // structure like aliasing, producing high values in the devided disrty map... This
+      // is likely to be due to the way fft works?...
+      // FIX: I now do the average of the PB by stacking the CF, FFT the result and square 
+      // it in the end. This is not the way to do in principle but the result is almost the 
+      // same. It should pose no problem I think.
+      Matrix<Complex> Spheroid_cut_padded2f;
+      Cube<Complex> aTermA_padded2;
+      Cube<Complex> aTermB_padded2;
+
+      // Keep the non-padded convolution functions for average PB calculation.
+      vector< vector < Matrix<Complex> > > Kron_Product_non_padded;
+      Kron_Product_non_padded.reserve(4);
+	    
+      if (Stack) {
+        Npix_out2 = Npix_out;
+        Spheroid_cut_padded2f = zero_padding(Spheroid_cut, Npix_out2);
+        aTermA_padded2 = zero_padding(aTermA, Npix_out2);
+        aTermB_padded2 = zero_padding(aTermB, Npix_out2);
+        normalized_fft (timerFFT, Spheroid_cut_padded2f, false);
+        for (uInt i=0; i<4; ++i) {
+          Matrix<Complex> planeA2f(aTermA_padded2.xyPlane(i));
+          Matrix<Complex> planeB2f(aTermB_padded2.xyPlane(i));
+          normalized_fft (timerFFT, planeA2f, false);
+          normalized_fft (timerFFT, planeB2f, false);
+        }
+      }
+
+      // Compute the Mueller matrix considering the Mueller Mask
+      uInt ind0;
+      uInt ind1;
+      uInt ii = 0;
+      IPosition cfShape;
+      Bool allElem = True;
+      for (uInt row0=0; row0<=1; ++row0) {
+        for (uInt col0=0; col0<=1; ++col0) {
+          vector < Matrix<Complex> > Row(4);
+          vector < Matrix<Complex> > Row_non_padded(4);
+          uInt jj = 0;
+          for (uInt row1=0; row1<=1; ++row1) {
+            for (uInt col1=0; col1<=1; ++col1) {
+              // This Mueller ordering is for polarisation given as XX,XY,YX YY
+              ind0 = row0 + 2*row1;
+              ind1 = col0 + 2*col1;
+              // Compute the convolution function for the given Mueller element
+              if (Mask_Mueller(ii,jj)) {
+                // Padded version for oversampling the convolution function
+                Matrix<Complex> plane_product (aTermB_padded.xyPlane(ind0) *
+                                               aTermA_padded.xyPlane(ind1));
+                plane_product *= wTerm_paddedf;
+                plane_product *= Spheroid_cut_paddedf;
+                Matrix<Complex> plane_product_paddedf
+                  (zero_padding(plane_product,
+                                plane_product.shape()[0] * m_oversampling));
+                normalized_fft (timerFFT, plane_product_paddedf);
+
+                plane_product_paddedf *= static_cast<Float>(m_oversampling *
+                                                            m_oversampling);
+		if (itsVerbose>3 && row0==0 && col0==0 && row1==0 && col1==0) {
+		  store (plane_product_paddedf, "awfft"+String::toString(stationA)+'-'+String::toString(stationB));
+		}
+
+                // Maybe to do:
+                // Find circle (from outside to inside) until value > peak*1e-3.
+                // Cut out that box to use as the convolution function.
+                // See nPBWProjectFT.cc (findSupport).
+
+                Row[jj].reference (plane_product_paddedf);
+                cfShape = plane_product_paddedf.shape();
+                // Non padded version for PB calculation (no W-term)
+                if (Stack) {
+                  Matrix<Complex> plane_productf(aTermB_padded2.xyPlane(ind0)*
+                                                 aTermA_padded2.xyPlane(ind1));
+                  plane_productf *= Spheroid_cut_padded2f;
+                  normalized_fft (timerFFT, plane_productf);
+                  Row_non_padded[jj].reference (plane_productf);
+                }
+              } else {
+                allElem = False;
+              }
+              ++jj;
+            }
+          }
+          ++ii;
+          Kron_Product.push_back(Row);
+          if (Stack) {
+            // Keep non-padded for primary beam calculation.
+            Kron_Product_non_padded.push_back(Row_non_padded);
+          }
+        }
+      }
+
+      // When degridding, transpose and use conjugate.
+      if (degridding_step) {
+        for (uInt i=0; i<4; ++i) {
+          for (uInt j=i; j<4; ++j) {
+            AlwaysAssert (Mask_Mueller(i,j) == Mask_Mueller(j,i), AipsError);
+            if (Mask_Mueller(i,j)) {
+              if (i!=j) {
+                Matrix<Complex> conj_product(conj(Kron_Product[i][j]));
+                Kron_Product[i][j].reference (conj(Kron_Product[j][i]));
+                Kron_Product[j][i].reference (conj_product);
+              } else {
+                Kron_Product[i][j].reference (conj(Kron_Product[i][j]));
+              }
+            }
+          }
+        }
+      }
+
+      // Put similarly shaped matrix with zeroes for missing Mueller elements.
+      if (!allElem) {
+        Matrix<Complex> zeroCF(cfShape);
+        for (uInt i=0; i<4; ++i) {
+          for (uInt j=0; j<4; ++j) {
+            if (! Mask_Mueller(i,j)) {
+              Kron_Product[i][j].reference (zeroCF);
+            }
+          }
+        }
+      }
+      // Add the conv.func. for this channel to the result.
+      result.push_back(Kron_Product);
+      if (Stack) {
+        result_non_padded.push_back(Kron_Product_non_padded);
+      }
+    }
+          
+    // Stacks the weighted quadratic sum of the convolution function of
+    // average PB estimate (!!!!! done for channel 0 only!!!)
+    if (Stack) {
+      //	  cout<<"...Stack CF for PB estimate"<<endl;
+      double weight_square = 4. * Append_average_PB_CF * Append_average_PB_CF;
+      double weight_sqsq = weight_square * weight_square;
+      for (uInt i=0; i<4; ++i) {
+        //if((i==2)||(i==1)) break;
+        for (uInt j=0; j<4; ++j) {
+          // Only use diagonal terms for average primary beam.
+          if (i==j  &&  Mask_Mueller(i,j)) {
+            //Stack_PB_CF=0.;
+            double istart = 0.5 * (m_shape[0] - Npix_out2);
+            if (istart-floor(istart) != 0.) {
+              istart += 0.5; //If number of pixel odd then 0th order at the center, shifted by one otherwise
+            }
+            for (Int jj=0; jj<Npix_out2; ++jj) {
+              for (Int ii=0; ii<Npix_out2; ++ii) {
+                Complex gain = result_non_padded[0][i][j](ii,jj);
+                Stack_PB_CF(istart+ii,istart+jj) += gain*weight_sqsq;
+              }
+            }
+            sum_weight_square += weight_sqsq;
+          }
+        }
+      }
+    }
+	
+    // Put the resulting vec(vec(vec))) in a LofarCFStore object
+    CoordinateSystem csys;
+    Vector<Float> samp(2, m_oversampling);
+    Vector<Int> xsup(1, Npix_out/2);
+    Vector<Int> ysup(1, Npix_out/2);
+    Int maxXSup(Npix_out);///2);
+    Int maxYSup(Npix_out);///2);
+    Quantity PA(0., "deg");
+    Int mosPointing(0);
+
+    // Update the timing info.
+    timerPar.stop();
+    double ftime = timerFFT.getReal();
+#pragma omp atomic
+    itsTimeCFfft += ftime;
+    unsigned long long cnt = timerFFT.getCount();
+#pragma omp atomic
+    itsTimeCFcnt += cnt;
+    double ptime = timerPar.getReal();
+#pragma omp atomic
+    itsTimeCFpar += ptime;
+
+    return LofarCFStore (res, csys, samp,  xsup, ysup, maxXSup, maxYSup,
+                         PA, mosPointing, Mask_Mueller);
+  }
+
+  //================================================
+      
+  // Returns the average Primary Beam from the disk
+  Matrix<Float> LofarConvolutionFunctionOld::Give_avg_pb()
+  {
+    // Only read if not available.
+    if (Im_Stack_PB_CF0.empty()) {
+      if (itsVerbose > 0) {
+        cout<<"==============Give_avg_pb()"<<endl;
+      }
+      String PBFile_name(itsImgName + ".avgpb");
+      File PBFile(PBFile_name);
+      if (! PBFile.exists()) {
+        throw SynthesisError (PBFile_name + " not found");
+      }
+      if (itsVerbose > 0) {
+        cout<<"..... loading Primary Beam image from disk ....."<<endl;
+      }
+      PagedImage<Float> tmp(PBFile_name);
+      IPosition shape(tmp.shape());
+      AlwaysAssert (shape[0]==m_shape[0] && shape[1]==m_shape[1], AipsError);
+      tmp.get (Im_Stack_PB_CF0, True);   // remove degenerate axes.
+    }
+    return Im_Stack_PB_CF0;
+  }
+
+  // Compute the average Primary Beam from the Stack of convolution functions
+  Matrix<Float> LofarConvolutionFunctionOld::Compute_avg_pb
+  (Matrix<Complex>& Sum_Stack_PB_CF, double sum_weight_square)
+  {
+    // Only calculate if not done yet.
+    if (Im_Stack_PB_CF0.empty()) {
+      if (itsVerbose > 0) {
+        cout<<"..... Compute average PB"<<endl;
+      }
+      Sum_Stack_PB_CF /= float(sum_weight_square);
+      //store(Stack_PB_CF,"Stack_PB_CF.img");
+
+      normalized_fft(Sum_Stack_PB_CF, false);
+      //store(Im_Stack_PB_CF00,"Im_Stack_PB_CF00.img");
+      Im_Stack_PB_CF0.resize (IPosition(2, m_shape[0], m_shape[1]));
+	
+      float threshold = 1.e-6;
+      for (Int jj=0; jj<m_shape[1]; ++jj) {
+        for (Int ii=0; ii<m_shape[0]; ++ii) {
+          Float absVal = abs(Sum_Stack_PB_CF(ii,jj));
+          Im_Stack_PB_CF0(ii,jj) = std::max (absVal*absVal, threshold);
+        }
+      }
+      // Make it persistent.
+      store(Im_Stack_PB_CF0, itsImgName + ".avgpb");
+    }
+    return Im_Stack_PB_CF0;
+  }
+
+  //================================================
+  // Does Zeros padding of a Cube
+  Cube<Complex> LofarConvolutionFunctionOld::zero_padding
+  (const Cube<Complex>& Image, int Npixel_Out)
+  {
+    if (Image.shape()[0] == Npixel_Out) {
+      return Image.copy();
+    }
+    if ((Npixel_Out%2) != 1) {
+      Npixel_Out++;
+    }
+    Cube<Complex> Image_Enlarged(Npixel_Out,Npixel_Out,Image.shape()[2]);
+    uInt Dii = Image.shape()(0)/2;
+    uInt Start_image_enlarged=Npixel_Out/2-Dii; //Is an even number, Assume square image
+    if ((Start_image_enlarged-floor(Start_image_enlarged))!=0.) {
+      Start_image_enlarged += 0.5; //If number of pixel odd then 0th order at the center, shifted by one otherwise
+    }
+    /* cout<<Start_image_enlarged<<"  "<<floor(Start_image_enlarged)<<endl; */
+    /* if((Start_image_enlarged-floor(Start_image_enlarged))!=0.){ */
+    /*   cout<<"Not even!!!"<<endl; */
+    /*   Start_image_enlarged+=0.5;} */
+
+    //double ratio(double(Npixel_Out)*double(Npixel_Out)/(Image.shape()(0)*Image.shape()(0)));
+    //if(!toFrequency){ratio=1./ratio;}
+    double ratio=1.;
+
+    for (Int pol=0; pol<Image.shape()[2]; ++pol) {
+      //cout<<"pol: "<<pol<<endl;
+      for (Int jj=0; jj<Image.shape()[1]; ++jj) {
+        for (Int ii=0; ii<Image.shape()[0]; ++ii) {
+          Image_Enlarged(Start_image_enlarged+ii,
+                         Start_image_enlarged+jj,pol) = ratio*Image(ii,jj,pol);
+        }
+      }
+    }
+    return Image_Enlarged;
+  }
+
+  //================================================
+  // Zeros padding of a Matrix
+
+  Matrix<Complex> LofarConvolutionFunctionOld::zero_padding
+  (const Matrix<Complex>& Image, int Npixel_Out)
+  {
+    if (Image.shape()[0] == Npixel_Out) {
+      return Image.copy();
+    }
+    if (Npixel_Out%2 != 1) {
+      Npixel_Out++;
+    }
+    IPosition shape_im_out(2, Npixel_Out, Npixel_Out);
+    Matrix<Complex> Image_Enlarged(shape_im_out, 0.);
+
+    double ratio=1.;
+
+    //if(!toFrequency){ratio=double(Npixel_Out)*double(Npixel_Out)/(Image.shape()(0)*Image.shape()(0));}
+
+    uInt Dii = Image.shape()[0]/2;
+    uInt Start_image_enlarged = shape_im_out[0]/2-Dii;
+    //Is an even number, Assume square image
+    //If number of pixel odd then 0th order at the center, shifted by one otherwise
+    if ((Start_image_enlarged-floor(Start_image_enlarged))!=0.) {
+      Start_image_enlarged += 0.5;
+    }
+
+    /* cout<<Start_image_enlarged<<"  "<<floor(Start_image_enlarged)<<endl; */
+    /* if((Start_image_enlarged-floor(Start_image_enlarged))!=0.){ */
+    /*   cout<<"Not even!!!"<<endl; */
+    /*   Start_image_enlarged+=0.5;} */
+    for (Int jj=0; jj<Image.shape()[1]; ++jj) {
+      for (Int ii=0; ii<Image.shape()[0]; ++ii) {
+        Image_Enlarged(Start_image_enlarged+ii,Start_image_enlarged+jj) = 
+          ratio*Image(ii,jj);
+      }
+    }
+    return Image_Enlarged;
+  }
+
+  //================================================
+  void LofarConvolutionFunctionOld::normalized_fft
+  (Matrix<Complex> &im, bool toFreq)
+  {
+    AlwaysAssert (im.ncolumn() == im.nrow()  &&  im.size() > 0  &&
+                  im.contiguousStorage(), AipsError);
+    int tnr = OpenMP::threadNum();
+    if (toFreq) {
+      itsFFTMachines[tnr].normalized_forward (im.nrow(), im.data());
+    } else {
+      itsFFTMachines[tnr].normalized_backward (im.nrow(), im.data());
+    }
+  }
+
+  void LofarConvolutionFunctionOld::normalized_fft
+  (PrecTimer& timer, Matrix<Complex> &im, bool toFreq)
+  {
+    timer.start();
+    normalized_fft (im, toFreq);
+    timer.stop();
+  }
+
+  //=================================================
+  MEpoch LofarConvolutionFunctionOld::observationStartTime
+  (const MeasurementSet &ms, uInt idObservation) const
+  {
+    // Get phase center as RA and DEC (J2000).
+    ROMSObservationColumns observation(ms.observation());
+    AlwaysAssert(observation.nrow() > idObservation, SynthesisError);
+    AlwaysAssert(!observation.flagRow()(idObservation), SynthesisError);
+
+    return observation.timeRangeMeas()(0)(IPosition(1, 0));
+  }
+
+  //=================================================
+  Double LofarConvolutionFunctionOld::observationReferenceFreq
+  (const MeasurementSet &ms, uInt idDataDescription)
+  {
+    // Read polarization id and spectral window id.
+    ROMSDataDescColumns desc(ms.dataDescription());
+    AlwaysAssert(desc.nrow() > idDataDescription, SynthesisError);
+    AlwaysAssert(!desc.flagRow()(idDataDescription), SynthesisError);
+
+    const uInt idWindow = desc.spectralWindowId()(idDataDescription);
+
+    /*        logIO() << LogOrigin("LofarATerm", "initReferenceFreq") << LogIO::NORMAL
+              << "spectral window: " << desc.spectralWindowId()(idDataDescription) << LogIO::POST;*/
+    //            << "spectral window: " << desc.spectralWindowId() << LogIO::POST;
+    // Get spectral information.
+    ROMSSpWindowColumns window(ms.spectralWindow());
+    AlwaysAssert(window.nrow() > idWindow, SynthesisError);
+    AlwaysAssert(!window.flagRow()(idWindow), SynthesisError);
+
+    return window.refFrequency()(idWindow);
+  }
+
+  //=================================================
+  // Estime spheroidal convolution function from the support of the fft of the spheroidal in the image plane
+
+  Double LofarConvolutionFunctionOld::makeSpheroidCut()
+  {
+    // Only calculate if not done yet.
+    if (! Spheroid_cut_im.empty()) {
+      return m_pixelSizeSpheroidal;
+    }
+    Matrix<Complex> spheroidal(m_shape[0], m_shape[1], 1.);
+    taper(spheroidal);
+    if (itsVerbose > 0) {
+      store(spheroidal, itsImgName + ".spheroidal");
+    }
+    normalized_fft(spheroidal);
+    Double Support_Speroidal = findSupport(spheroidal, 0.0001);
+    if (itsVerbose > 0) {
+      store(spheroidal, itsImgName + ".spheroidal_fft");
+    }
+
+    Double res_ini = abs(m_coordinates.increment()(0));
+    Double diam_image = res_ini*m_shape[0];
+    Double Pixel_Size_Spheroidal = diam_image/Support_Speroidal;
+    uInt Npix = floor(diam_image/Pixel_Size_Spheroidal);
+    if (Npix%2 != 1) {
+    // Make the resulting image have an even number of pixel (to make the zeros padding step easier)
+      ++Npix;
+      Pixel_Size_Spheroidal = diam_image/Npix;
+    }
+    Matrix<Complex> Spheroid_cut0(IPosition(2,Npix,Npix),0.);
+    Spheroid_cut=Spheroid_cut0;
+    double istart(m_shape[0]/2.-Npix/2.);
+    if ((istart-floor(istart))!=0.) {
+      //If number of pixel odd then 0th order at the center, shifted by one otherwise
+      istart += 0.5;
+    }
+    for (uInt j=0; j<Npix; ++j) {
+      for (uInt i=0; i<Npix; ++i) {
+        Spheroid_cut(i,j) = spheroidal(istart+i,istart+j);
+      }
+    }
+    Matrix<Complex> Spheroid_cut_paddedf=zero_padding(Spheroid_cut,m_shape[0]);
+    normalized_fft(Spheroid_cut_paddedf, false);
+    Spheroid_cut_im.reference (real(Spheroid_cut_paddedf));
+    // Only this one is really needed.
+    store(Spheroid_cut_im, itsImgName + ".spheroid_cut_im");
+    if (itsVerbose > 0) {
+      store(Spheroid_cut, itsImgName + ".spheroid_cut");
+    }	
+    return Pixel_Size_Spheroidal;
+  }
+
+  const Matrix<Float>& LofarConvolutionFunctionOld::getSpheroidCut()
+  {
+    if (Spheroid_cut_im.empty()) {
+      makeSpheroidCut();
+    }
+    return Spheroid_cut_im;
+  }
+
+  Matrix<Float> LofarConvolutionFunctionOld::getSpheroidCut (const String& imgName)
+  {
+    PagedImage<Float> im(imgName+".spheroid_cut_im");
+    return im.get (True);
+  }
+
+  Matrix<Float> LofarConvolutionFunctionOld::getAveragePB (const String& imgName)
+  {
+    PagedImage<Float> im(imgName+".avgpb");
+    return im.get (True);
+  }
+
+  //=================================================
+  // Return the angular resolution required for making the image of the angular size determined by
+  // coordinates and shape. The resolution is assumed to be the same on both direction axes.
+  Double LofarConvolutionFunctionOld::estimateWResolution
+  (const IPosition &shape, Double pixelSize,
+   Double w) const
+  {
+    Double diam_image = pixelSize*shape[0];         // image diameter in radian
+    if (w == 0.) {
+      return diam_image;
+    }
+    // Get pixel size in W-term image in radian
+    Double Res_w_image = 0.5/(sqrt(2.)*w*(shape[0]/2.)*pixelSize);
+    // Get number of pixel size in W-term image
+    uInt Npix=floor(diam_image/Res_w_image);
+    Res_w_image = diam_image/Npix;
+    if (Npix%2 != 1) {
+      // Make the resulting image have an even number of pixel
+      // (to make the zeros padding step easier)
+      ++Npix;
+      Res_w_image = diam_image/Npix;
+    }
+    return Res_w_image;
+  }
+
+  //=================================================
+  // Return the angular resolution required for making the image of the angular size determined by
+  // coordinates and shape. The resolution is assumed to be the same on both direction axes.
+  Double LofarConvolutionFunctionOld::estimateAResolution
+  (const IPosition &shape, const DirectionCoordinate &coordinates) const
+  {
+    Double res_ini=abs(coordinates.increment()(0));                      // pixel size in image in radian
+    Double diam_image=res_ini*shape(0);                                  // image diameter in radian
+    Double station_diam = 70.;                                           // station diameter in meters: To be adapted to the individual station size.
+    Double Res_beam_image= ((C::c/m_refFrequency)/station_diam)/2.;      // pixel size in A-term image in radian
+    uInt Npix=floor(diam_image/Res_beam_image);                         // Number of pixel size in A-term image
+    Res_beam_image=diam_image/Npix;
+    if (Npix%2 != 1) {
+      // Make the resulting image have an even number of pixel (to make the zeros padding step easier)
+      ++Npix;
+      Res_beam_image = diam_image/Npix;
+    }
+    return Res_beam_image;
+  }
+
+  //=================================================
+  Double LofarConvolutionFunctionOld::spheroidal(Double nu) const
+  {
+    static Double P[2][5] = {{ 8.203343e-2, -3.644705e-1, 6.278660e-1,
+                              -5.335581e-1,  2.312756e-1},
+                             { 4.028559e-3, -3.697768e-2, 1.021332e-1,
+                              -1.201436e-1, 6.412774e-2}};
+    static Double Q[2][3] = {{1.0000000e0, 8.212018e-1, 2.078043e-1},
+                             {1.0000000e0, 9.599102e-1, 2.918724e-1}};
+    uInt part = 0;
+    Double end = 0.0;
+    if (nu >= 0.0 && nu < 0.75) {
+      part = 0;
+      end = 0.75;
+    } else if (nu >= 0.75 && nu <= 1.00) {
+      part = 1;
+      end = 1.00;
+    } else {
+      return 0.0;
+    }
+    Double nusq = nu * nu;
+    Double delnusq = nusq - end * end;
+    Double delnusqPow = delnusq;
+    Double top = P[part][0];
+    for (uInt k=1; k<5; ++k) {
+      top += P[part][k] * delnusqPow;
+      delnusqPow *= delnusq;
+    }
+
+    Double bot = Q[part][0];
+    delnusqPow = delnusq;
+    for (uInt k=1; k<3; ++k) {
+      bot += Q[part][k] * delnusqPow;
+      delnusqPow *= delnusq;
+    }
+	
+    double result = (bot == 0  ?  0 : (1.0 - nusq) * (top / bot));
+    //if(result<1.e-3){result=1.e-3;}
+    return result;
+  }
+
+  void LofarConvolutionFunctionOld::showTimings (ostream& os,
+                                              double duration,
+					      double timeCF) const
+  {
+    os << "  Wterm calculation ";
+    showPerc1 (os, itsTimeW, duration);
+    os << "    fft-part ";
+    showPerc1 (os, itsTimeWfft, itsTimeW);
+    os << "  (";
+    showPerc1 (os, itsTimeWfft, duration);
+    os << " of total;   #ffts=" << itsTimeWcnt << ')' << endl;
+    os << "  Aterm calculation ";
+    showPerc1 (os, itsTimeA, duration);
+    os << "    fft-part ";
+    showPerc1 (os, itsTimeAfft, itsTimeA);
+    os << "  (";
+    showPerc1 (os, itsTimeAfft, duration);
+    os << " of total;   #ffts=" << itsTimeAcnt << ')' << endl;
+    os << "  CFunc calculation ";
+    showPerc1 (os, timeCF, duration);
+    os << "    fft-part ";
+    showPerc1 (os, itsTimeCFfft, timeCF);
+    os << "  (";
+    showPerc1 (os, itsTimeCFfft, duration);
+    os << " of total;   #ffts=" << itsTimeCFcnt << ')' << endl;
+  }
+
+  void LofarConvolutionFunctionOld::showPerc1 (ostream& os,
+                                            double value, double total)
+  {
+    int perc = (total==0  ?  0 : int(1000. * value / total + 0.5));
+    os << std::setw(3) << perc/10 << '.' << perc%10 << '%';
+  }
+
+
+} //# end namespace casa
diff --git a/CEP/Imager/LofarFT/src/LofarCubeSkyEquation.cc b/CEP/Imager/LofarFT/src/LofarCubeSkyEquation.cc
index de087459d25efce5f28cbf85cbbe14fd71607a43..64450723415e81e25056d257e816fedfb88ded81 100644
--- a/CEP/Imager/LofarFT/src/LofarCubeSkyEquation.cc
+++ b/CEP/Imager/LofarFT/src/LofarCubeSkyEquation.cc
@@ -25,6 +25,7 @@
 //#
 //# $Id$
 
+#include <lofar_config.h>
 #include <casa/iostream.h>
 #include <casa/Exceptions/Error.h>
 #include <casa/Utilities/Assert.h>
@@ -35,6 +36,7 @@
 #include <casa/OS/HostInfo.h>
 #include <casa/System/ProgressMeter.h>
 #include <casa/Utilities/CountedPtr.h>
+#include <lattices/Lattices/ArrayLattice.h>
 
 #include <coordinates/Coordinates/CoordinateSystem.h>
 #include <coordinates/Coordinates/DirectionCoordinate.h>
@@ -74,6 +76,8 @@
 #include <msvis/MSVis/VisBufferAsync.h>
 //#include <synthesis/Utilities/ThreadTimers.h>
 
+#include <casa/OS/PrecTimer.h>
+
 namespace casa { //# NAMESPACE CASA - BEGIN
 
 LofarCubeSkyEquation::LofarCubeSkyEquation(SkyModel& sm, VisSet& vs, FTMachine& ft,
@@ -85,7 +89,6 @@ LofarCubeSkyEquation::LofarCubeSkyEquation(SkyModel& sm, VisSet& vs, FTMachine&
   firstOneChangesPut_p(False),
   firstOneChangesGet_p(False)
 {
-
     init(ft);
 
 }
@@ -107,19 +110,19 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
 
   doflat_p=False;
   nchanPerSlice_p = 1;
-  
-   if(sm_->numberOfTaylorTerms()>1) 
+
+   if(sm_->numberOfTaylorTerms()>1)
     {
       nmod = (sm_->numberOfModels()/sm_->numberOfTaylorTerms()) * (2 * sm_->numberOfTaylorTerms() - 1);
     }
-  
+
   //case of component ft only
   if(nmod==0)
     nmod=1;
-  
+
   ftm_p.resize(nmod, True);
   iftm_p.resize(nmod, True);
-  
+
   //make a distinct ift_ as gridding and degridding can occur simultaneously
   if(ft.name() == "MosaicFT"){
     ft_=new MosaicFT(static_cast<MosaicFT &>(ft));
@@ -127,9 +130,9 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
     ftm_p[0]=ft_;
     iftm_p[0]=ift_;
     //For mosaic ...outlier fields get normal GridFT's
-    
+
     MPosition loc=ift_->getLocation();
-    for (Int k=1; k < (nmod); ++k){ 
+    for (Int k=1; k < (nmod); ++k){
       ftm_p[k]=new GridFT(1000000, 16, "SF", loc, 1.0, False);
       iftm_p[k]=new GridFT(1000000, 16, "SF", loc, 1.0, False);
     }
@@ -144,9 +147,9 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
     static_cast<WProjectFT &>(*ftm_p[0]).setConvFunc(sharedconvFunc);
     static_cast<WProjectFT &>(*iftm_p[0]).setConvFunc(sharedconvFunc);
     // For now have all the fields have WProjectFt machines....
-    //but should be seperated between GridFT's for the outliers and 
+    //but should be seperated between GridFT's for the outliers and
     //WProject for the facets.
-    for (Int k=1; k < (nmod); ++k){ 
+    for (Int k=1; k < (nmod); ++k){
       ftm_p[k]=new WProjectFT(static_cast<WProjectFT &>(*ft_));
       iftm_p[k]=new WProjectFT(static_cast<WProjectFT &>(*ift_));
       // Give each pair of FTMachine a convolution function set to share
@@ -171,7 +174,7 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
      iftm_p[0]=ift_;
      if(nmod != (2 * sm_->numberOfTaylorTerms() - 1)) /* MFS */
        throw(AipsError("No multifield with pb-projection allowed"));
-     for (Int k=1; k < (nmod); ++k){ 
+     for (Int k=1; k < (nmod); ++k){
       ftm_p[k]=new nPBWProjectFT(static_cast<nPBWProjectFT &>(*ft_));
       iftm_p[k]=new nPBWProjectFT(static_cast<nPBWProjectFT &>(*ift_));
     }
@@ -184,7 +187,7 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
      iftm_p[0]=ift_;
      if(nmod != (2 * sm_->numberOfTaylorTerms() - 1)) /* MFS */
        throw(AipsError("No multifield with a-projection allowed"));
-     for (Int k=1; k < (nmod); ++k){ 
+     for (Int k=1; k < (nmod); ++k){
       ftm_p[k]=new AWProjectFT(static_cast<AWProjectFT &>(*ft_));
       iftm_p[k]=new AWProjectFT(static_cast<AWProjectFT &>(*ift_));
       //      iftm_p[k]=ftm_p[k];
@@ -198,10 +201,10 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
      iftm_p[0]=ift_;
      // if(nmod != (2 * sm_->numberOfTaylorTerms() - 1)) /* MFS */
      //   throw(AipsError("No multifield with a-projection allowed"));
-     for (Int k=1; k < (nmod); ++k){ 
+     for (Int k=1; k < (nmod); ++k){
       ftm_p[k]=new AWProjectWBFT(static_cast<AWProjectWBFT &>(*ft_));
       iftm_p[k]=new AWProjectWBFT(static_cast<AWProjectWBFT &>(*ift_));
-      if(sm_->numberOfTaylorTerms()>1) 
+      if(sm_->numberOfTaylorTerms()>1)
 	{
 	  for (Int model=0; model < (sm_->numberOfModels()) ; ++model)
 	    {
@@ -221,7 +224,7 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
      iftm_p[0]=ift_;
      if(nmod != (2 * sm_->numberOfTaylorTerms() - 1)) /* MFS */
        throw(AipsError("No multifield with pb-mosaic allowed"));
-     for (Int k=1; k < (nmod); ++k){ 
+     for (Int k=1; k < (nmod); ++k){
       ftm_p[k]=new PBMosaicFT(static_cast<PBMosaicFT &>(*ft_));
       iftm_p[k]=new PBMosaicFT(static_cast<PBMosaicFT &>(*ift_));
     }
@@ -232,7 +235,7 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
     // ftm_p[0]=CountedPtr<FTMachine>(ft_, False);
     ftm_p[0]=ft_;
     iftm_p[0]=ift_;
-    for (Int k=1; k < (nmod); ++k){ 
+    for (Int k=1; k < (nmod); ++k){
       ftm_p[k]=new rGridFT(static_cast<rGridFT &>(*ft_));
       iftm_p[k]=new rGridFT(static_cast<rGridFT &>(*ift_));
     }
@@ -242,11 +245,11 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
     ift_=new MultiTermFT(static_cast<MultiTermFT &>(ft));
     ftm_p[0]=ft_;
     iftm_p[0]=ift_;
-    for (Int k=1; k < (nmod); ++k){ 
+    for (Int k=1; k < (nmod); ++k){
       ftm_p[k]=new MultiTermFT(static_cast<MultiTermFT &>(*ft_));
       iftm_p[k]=new MultiTermFT(static_cast<MultiTermFT &>(*ift_));
     }
-     for (Int k=0; k < (nmod); ++k){ 
+     for (Int k=0; k < (nmod); ++k){
       ftm_p[k]->setMiscInfo(sm_->getTaylorIndex(k));
       iftm_p[k]->setMiscInfo(sm_->getTaylorIndex(k));
     }
@@ -257,13 +260,30 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
      //     ift_=ft_;
      ftm_p[0]=ft_;
      iftm_p[0]=ift_;
-     if(nmod != (2 * sm_->numberOfTaylorTerms() - 1)) /* MFS */
-       throw(AipsError("No multifield with a-projection allowed"));
-     for (Int k=1; k < (nmod); ++k){ 
+     ftm_p[0]->setMiscInfo(0);
+     iftm_p[0]->setMiscInfo(0);
+     cout<<"nmod="<<nmod<<endl;
+     //if(nmod != (2 * sm_->numberOfTaylorTerms() - 1)) /* MFS */
+     //  throw(AipsError("No multifield with a-projection allowed"));
+     for (Int k=1; k < (nmod); ++k){
       ftm_p[k]=new LOFAR::LofarFTMachine(static_cast<LOFAR::LofarFTMachine &>(*ft_));
       iftm_p[k]=new LOFAR::LofarFTMachine(static_cast<LOFAR::LofarFTMachine &>(*ift_));
+      // test MSMFT for LOFAR
+      ftm_p[k]->setMiscInfo(0);//sm_->getTaylorIndex(k));
+      iftm_p[k]->setMiscInfo(0);//sm_->getTaylorIndex(k));
+
+     }
+      // if(sm_->numberOfTaylorTerms()>1)
+      // 	{
+      // 	  for (Int model=0; model < (sm_->numberOfModels()) ; ++model)
+      // 	    {
+      // 	      ftm_p[model]->setMiscInfo(sm_->getTaylorIndex(model));
+      // 	      iftm_p[model]->setMiscInfo(sm_->getTaylorIndex(model));
+
+      // 	    }
+      // 	}
       //      iftm_p[k]=ftm_p[k];
-    }
+     //}
   }
   else {
     ft_=new GridFT(static_cast<GridFT &>(ft));
@@ -271,7 +291,7 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
     // ftm_p[0]=CountedPtr<FTMachine>(ft_, False);
     ftm_p[0]=ft_;
     iftm_p[0]=ift_;
-    for (Int k=1; k < (nmod); ++k){ 
+    for (Int k=1; k < (nmod); ++k){
       ftm_p[k]=new GridFT(static_cast<GridFT &>(*ft_));
       iftm_p[k]=new GridFT(static_cast<GridFT &>(*ift_));
     }
@@ -284,7 +304,7 @@ void LofarCubeSkyEquation::init(FTMachine& ft){
 }
 
 LofarCubeSkyEquation::~LofarCubeSkyEquation(){
-  //As we  make an explicit ift_ in the constructor we need 
+  //As we  make an explicit ift_ in the constructor we need
   //to take care of it here...
   //if(ift_ && (ift_ != ft_))
   //  delete ift_;
@@ -300,7 +320,7 @@ void  LofarCubeSkyEquation::predict(Bool incremental, MS::PredefinedColumns col)
   VisibilityIterator::DataColumn visCol=VisibilityIterator::Model;
   if(col==MS::DATA){
     visCol=VisibilityIterator::Observed;
-  } 
+  }
   if(col==MS::CORRECTED_DATA){
     visCol=VisibilityIterator::Corrected;
   }
@@ -310,7 +330,7 @@ void  LofarCubeSkyEquation::predict(Bool incremental, MS::PredefinedColumns col)
   if(sm_->numberOfModels()!= 0)  AlwaysAssert(ok(),AipsError);
   if(noModelCol_p)
     throw(AipsError("Cannot predict visibilities without using scratch columns yet"));
-  // Initialize 
+  // Initialize
   VisIter& vi=*wvi_p;
   //Lets get the channel selection for later use
   vi.getChannelSelection(blockNumChanGroup_p, blockChanStart_p,
@@ -323,16 +343,16 @@ void  LofarCubeSkyEquation::predict(Bool incremental, MS::PredefinedColumns col)
   Bool initialized=False;
   predictComponents(incremental, initialized);
   //set to zero then loop over model...check for size...subimage then loop over  subimages
-  
-  
+
+
   Bool isEmpty=True;
   for (Int model=0; model < (sm_->numberOfModels());++model){
-    isEmpty=isEmpty &&  (sm_->isEmpty(model));                
-    
+    isEmpty=isEmpty &&  (sm_->isEmpty(model));
+
   }
-  
-  
-  if( (sm_->numberOfModels() >0) && isEmpty  && !initialized && !incremental){ 
+
+
+  if( (sm_->numberOfModels() >0) && isEmpty  && !initialized && !incremental){
     // We are at the begining with an empty model as starting point
     for (vi.originChunks();vi.moreChunks();vi.nextChunk()) {
       for (vi.origin(); vi.more(); vi++) {
@@ -341,15 +361,15 @@ void  LofarCubeSkyEquation::predict(Bool incremental, MS::PredefinedColumns col)
       }
     }
   }
-  
+
     //If all model is zero...no need to continue
-  if(isEmpty) 
+  if(isEmpty)
     return;
-  
-  
-  
+
+
+
   // Now do the images
-  for (Int model=0; model < (sm_->numberOfModels());++model){ 
+  for (Int model=0; model < (sm_->numberOfModels());++model){
     // Change the model polarization frame
     if(vb->polFrame()==MSIter::Linear) {
       StokesImageUtil::changeCStokesRep(sm_->cImage(model),
@@ -389,22 +409,22 @@ void  LofarCubeSkyEquation::predict(Bool incremental, MS::PredefinedColumns col)
     finalizeGetSlice();
     if(!incremental&&!initialized) initialized=True;
   }
-  
+
   for(Int model=0; model < sm_->numberOfModels(); ++model){
       //For now unscale test on name of ft_
     ft_=&(*ftm_p[model]);
     unScaleImage(model, incremental);
   }
   ft_=&(*ftm_p[0]);
-  
+
   //lets return original selection back to iterator
   if(changedVI)
-    vi.selectChannel(blockNumChanGroup_p, blockChanStart_p, 
-		     blockChanWidth_p, blockChanInc_p, blockSpw_p); 
-  
+    vi.selectChannel(blockNumChanGroup_p, blockChanStart_p,
+		     blockChanWidth_p, blockChanInc_p, blockSpw_p);
+
 }
 
-void LofarCubeSkyEquation::makeApproxPSF(PtrBlock<TempImage<Float> * >& psfs) 
+void LofarCubeSkyEquation::makeApproxPSF(PtrBlock<TempImage<Float> * >& psfs)
 {
 
   if(iftm_p[0]->name()=="MosaicFT")
@@ -451,7 +471,7 @@ void LofarCubeSkyEquation::makeMosaicPSF(PtrBlock<TempImage<Float> * >& psfs){
 	planeMax =  LEN.getFloat();
 	if( (planeMax >0.0) && (planeMax < 0.8 *peak)){
 	  psfSub.put(goodplane);
-	  
+
 	}
       }
     }
@@ -480,6 +500,17 @@ void LofarCubeSkyEquation::makeMosaicPSF(PtrBlock<TempImage<Float> * >& psfs){
 
 void LofarCubeSkyEquation::makeSimplePSF(PtrBlock<TempImage<Float> * >& psfs) {
 
+  //  PrecTimer TimerCyril;
+  //  TimerCyril.start();
+
+//  cout<<"psfs[0].name() "<<psfs[0]->name()<<endl;
+//File myFile("Cube_dirty.img"+String::toString(count_cycle));
+//  if(!myFile.exists()){
+//       }
+
+
+
+
   Int nmodels=psfs.nelements();
     LogIO os(LogOrigin("LofarCubeSkyEquation", "makeSimplePSF"));
     ft_->setNoPadding(noModelCol_p);
@@ -499,6 +530,11 @@ void LofarCubeSkyEquation::makeSimplePSF(PtrBlock<TempImage<Float> * >& psfs) {
     VisBufferAutoPtr vb (vi);
     vi.originChunks();
     vi.origin();
+    //  TimerCyril.stop();
+    //  TimerCyril.show(cout,"1");
+    //  TimerCyril.reset();
+    //  TimerCyril.start();
+
     // Change the model polarization frame
     for (Int model=0; model < nmodels; ++model){
         if(vb->polFrame()==MSIter::Linear) {
@@ -514,6 +550,10 @@ void LofarCubeSkyEquation::makeSimplePSF(PtrBlock<TempImage<Float> * >& psfs) {
 
     Int nCubeSlice=1;
     isLargeCube(sm_->cImage(0), nCubeSlice);
+    //  TimerCyril.stop();
+    //  TimerCyril.show(cout,"2");
+    //  TimerCyril.reset();
+    //  TimerCyril.start();
     for (Int cubeSlice=0; cubeSlice< nCubeSlice; ++cubeSlice){
         changedVI= getFreqRange(vi, sm_->cImage(0).coordinates(),
                                 cubeSlice, nCubeSlice) || changedVI;
@@ -527,8 +567,16 @@ void LofarCubeSkyEquation::makeSimplePSF(PtrBlock<TempImage<Float> * >& psfs) {
 
         initializePutSlice(* vb, cubeSlice, nCubeSlice);
 
+  	//  TimerCyril.stop();
+  	//  TimerCyril.show(cout,"3");
+  	//  TimerCyril.reset();
+  	//  TimerCyril.start();
         for (vi.originChunks();vi.moreChunks();vi.nextChunk()) {
             for (vi.origin(); vi.more(); vi++) {
+  	      //  TimerCyril.stop();
+  	      //  TimerCyril.show(cout,"4a");
+  	      //  TimerCyril.reset();
+  	      //  TimerCyril.start();
                 if(noModelCol_p) {
                     //This here forces the modelVisCube shape and prevents reading model column
                     vb->setModelVisCube(Complex(0.0,0.0));
@@ -537,12 +585,16 @@ void LofarCubeSkyEquation::makeSimplePSF(PtrBlock<TempImage<Float> * >& psfs) {
                 cohDone+=vb->nRow();
                 pm.update(Double(cohDone));
 
+  		//  TimerCyril.stop();
+  		//  TimerCyril.show(cout,"4b");
+  		//  TimerCyril.reset();
+  		//  TimerCyril.start();
             }
         }
         finalizePutSlice(* vb, cubeSlice, nCubeSlice);
     }
 
-    //lets return original selection back to iterator
+   //lets return original selection back to iterator
 
 
     if(changedVI)
@@ -552,6 +604,10 @@ void LofarCubeSkyEquation::makeSimplePSF(PtrBlock<TempImage<Float> * >& psfs) {
     fixImageScale();
     for(Int model=0; model < nmodels; ++model){
         {
+  	  //  TimerCyril.stop();
+  	  //  TimerCyril.show(cout,"5a");
+  	  //  TimerCyril.reset();
+  	  //  TimerCyril.start();
             //Normalize the gS image
             Int nXX=sm_->ggS(model).shape()(0);
             Int nYY=sm_->ggS(model).shape()(1);
@@ -561,6 +617,10 @@ void LofarCubeSkyEquation::makeSimplePSF(PtrBlock<TempImage<Float> * >& psfs) {
             IPosition trc(4, nXX, nYY, npola, nchana);
             blc(0)=0; blc(1)=0; trc(0)=nXX-1; trc(1)=nYY-1;
             //max weights per plane
+  	    //  TimerCyril.stop();
+  	    //  TimerCyril.show(cout,"5b");
+  	    //  TimerCyril.reset();
+  	    //  TimerCyril.start();
             for (Int j=0; j < npola; ++j){
                 for (Int k=0; k < nchana ; ++k){
 
@@ -583,40 +643,119 @@ void LofarCubeSkyEquation::makeSimplePSF(PtrBlock<TempImage<Float> * >& psfs) {
                     }
                 }
             }
+  	    //  TimerCyril.stop();
+  	    //  TimerCyril.show(cout,"6");
+  	    //  TimerCyril.reset();
+  	    //  TimerCyril.start();
             //
         }
 
+  //PtrBlock<TempImage<Float> * >& psfs
+
+
+	// Cyril: Gets the PSF from disk
+	// String nameii("PSF.keep");
+	// ostringstream nameiii(nameii);
+	// PagedImage<Float> tmpi(nameiii.str().c_str());
+	// Slicer slicei(IPosition(4,0,0,0,0), tmpi.shape(), IPosition(4,1,1,1,1));
+	// Array<Float> PSF_Disk;
+	// tmpi.doGetSlice(PSF_Disk, slicei);
+	// CountedPtr<Lattice<Float> > PSF_Lattice;
+	// PSF_Lattice = new ArrayLattice<Float>(PSF_Disk);
+	// SubImage<Float> psfSub(*(psfs[0]), slicei, True);
+	// psfSub.copyData(*PSF_Lattice);
+
         /*
     if(0){
       PagedImage<Float> thisScreen(psfs[model]->shape(), psfs[model]->coordinates(), String("ELPSF).psf"));
 	LatticeExpr<Float> le(*psfs[model]);
 	thisScreen.copyData(le);
-      } 
+      }
          */
-        LatticeExprNode maxPSF=max(*psfs[model]);
-        Float maxpsf=maxPSF.getFloat();
-        if(abs(maxpsf-1.0) > 1e-3) {
-            os << "Maximum of approximate PSF for field " << model << " = "
-                    << maxpsf << " : renormalizing to unity" <<  LogIO::POST;
-        }
-        if(maxpsf > 0.0 ){
-            LatticeExpr<Float> len((*psfs[model])/maxpsf);
-            psfs[model]->copyData(len);
-        }
-        else{
-            if(sm_->numberOfTaylorTerms()>1) { /* MFS */
-                os << "PSF calculation resulted in a PSF with its peak being 0 or less. This is ok for MS-MFS." << LogIO::POST;
-            }
-            else{
-                throw(PSFZero("SkyEquation:: PSF calculation resulted in a PSF with its peak being 0 or less!"));
-            }
-        }
+
+	//===============================================
+	//Cyril: For MF cleaning, Itry take this of
+	//===============================================
+	// LatticeExprNode maxPSF=max(*psfs[model]);
+        // Float maxpsf=maxPSF.getFloat();
+        // if(abs(maxpsf-1.0) > 1e-3) {
+        //    os << "Maximum of approximate PSF for field " << model << " = "
+        //            << maxpsf << " : renormalizing to unity" <<  LogIO::POST;
+        // }
+        // if(maxpsf > 0.0 ){
+	//   LatticeExpr<Float> len((*psfs[model])/maxpsf);
+	//   psfs[model]->copyData(len);
+        // }
+        // else{
+        //     if(sm_->numberOfTaylorTerms()>1) { /* MFS */
+        //         os << "PSF calculation resulted in a PSF with its peak being 0 or less. This is ok for MS-MFS." << LogIO::POST;
+        //     }
+        //     else{
+        //         throw(PSFZero("SkyEquation:: PSF calculation resulted in a PSF with its peak being 0 or less!"));
+        //     }
+        // }
+	//===============================================
+
+
+
     }
 
     isPSFWork_p=False; // resetting this flag so that subsequent calculation uses
     // the right SkyJones correction;
 }
 
+
+
+
+// //============================ ADDED by Cyril
+
+// void LofarCubeSkyEquation::setExistingPSF(PtrBlock<TempImage<Float> * >& psfs) {
+
+//   Int nmodels=psfs.nelements();
+//   String FileName("test.img.psf");
+//   PagedImage<Float> myimage (FileName);
+//   for(Int model=0; model < nmodels; ++model){
+//     {
+//       Int nXX=sm_->ggS(model).shape()(0);
+//       Int nYY=sm_->ggS(model).shape()(1);
+//       Int npola= sm_->ggS(model).shape()(2);
+//       Int nchana= sm_->ggS(model).shape()(3);
+//       IPosition blc(4,nXX, nYY, npola, nchana);
+//       IPosition trc(4, nXX, nYY, npola, nchana);
+//       blc(0)=0; blc(1)=0; trc(0)=nXX-1; trc(1)=nYY-1;
+//       for (Int j=0; j < npola; ++j){
+// 	for (Int k=0; k < nchana ; ++k){
+
+// 	  blc(2)=j; trc(2)=j;
+// 	  blc(3)=k; trc(3)=k;
+// 	  Slicer sl(blc, trc, Slicer::endIsLast);
+// 	  SubImage<Float> gSSub(sm_->gS(model), sl, False);
+// 	  SubImage<Float> ggSSub(sm_->ggS(model), sl, False);
+// 	  SubImage<Float> psfSub(*(psfs[model]), sl, True);
+// 	  Float planeMax;
+// 	  LatticeExprNode LEN = max( ggSSub );
+// 	  planeMax =  LEN.getFloat();
+// 	  if(planeMax !=0){
+// 	    psfSub.copyData( (LatticeExpr<Float>)
+// 			     (iif(ggSSub > (0.0),
+// 				  (gSSub/planeMax),0.0)));
+// 	  }
+// 	  else{
+// 	    psfSub.set(0.0);
+// 	  }
+// 	}
+//       }
+
+//     }
+
+//     isPSFWork_p=False;
+//   }
+
+// // ======================== END added by Cyril
+
+
+
+
 void LofarCubeSkyEquation::gradientsChiSquared(Bool /*incr*/, Bool commitModel){
 
 
@@ -687,9 +826,9 @@ void LofarCubeSkyEquation::gradientsChiSquared(Bool /*incr*/, Bool commitModel){
     checkVisIterNumRows(*rvi_p);
     VisBufferAutoPtr vb (rvi_p);
     //    Timers tVisAutoPtr=Timers::getTime();
-    
+
     /**** Do we need to do this
-  if( (sm_->isEmpty(0))  && !initialized && !incremental){ 
+  if( (sm_->isEmpty(0))  && !initialized && !incremental){
     // We are at the begining with an empty model as starting point
     for (vi.originChunks();vi.moreChunks();vi.nextChunk()) {
       for (vi.origin(); vi.more(); vi++) {
@@ -777,11 +916,12 @@ void LofarCubeSkyEquation::gradientsChiSquared(Bool /*incr*/, Bool commitModel){
             for (rvi_p->origin(); rvi_p->more(); (*rvi_p)++) {
 
 	      //	      Timers tInitModel=Timers::getTime();
+
                 if(!incremental && !predictedComp) {
                     //This here forces the modelVisCube shape and prevents reading model column
                     vb->setModelVisCube(Complex(0.0,0.0));
                 }
-                // get the model visibility and write it to the model MS
+                 // get the model visibility and write it to the model MS
 		//	Timers tGetSlice=Timers::getTime();
 		//		Timers tgetSlice=Timers::getTime();
                 if(!isEmpty)
@@ -794,7 +934,7 @@ void LofarCubeSkyEquation::gradientsChiSquared(Bool /*incr*/, Bool commitModel){
                 // Now lets grid the -ve of residual
                 // use visCube if there is no correctedData
 		//		Timers tGetRes=Timers::getTime();
-		
+
 		if(!iftm_p[0]->canComputeResiduals()){
 		  cout<<"CANNOT!!!!"<<endl;
 		  if(!useCorrected) vb->modelVisCube()-=vb->visCube();
@@ -805,10 +945,11 @@ void LofarCubeSkyEquation::gradientsChiSquared(Bool /*incr*/, Bool commitModel){
 		//		Timers tPutSlice = Timers::getTime();
                 //putSlice(* vb, False, FTMachine::MODEL, cubeSlice, nCubeSlice);
 
-                putSlice(* vb, False, FTMachine::MODEL, cubeSlice, nCubeSlice);		
+                putSlice(* vb, False, FTMachine::MODEL, cubeSlice, nCubeSlice);
 
                 cohDone+=vb->nRow();
                 pm.update(Double(cohDone));
+
 		// Timers tDoneGridding=Timers::getTime();
 		// aInitModel += tgetSlice - tInitModel;
 		// aGetSlice += tsetModel - tgetSlice;
@@ -825,7 +966,7 @@ void LofarCubeSkyEquation::gradientsChiSquared(Bool /*incr*/, Bool commitModel){
 	//	Timers tFinalizePutSlice=Timers::getTime();
         finalizePutSlice(* vb, cubeSlice, nCubeSlice);
 	//	Timers tDoneFinalizePutSlice=Timers::getTime();
-	
+
 	// aFinalizeGetSlice += tFinalizePutSlice - tFinalizeGetSlice;
 	// aFinalizePutSlice += tDoneFinalizePutSlice - tFinalizePutSlice;
     }
@@ -853,31 +994,31 @@ void LofarCubeSkyEquation::gradientsChiSquared(Bool /*incr*/, Bool commitModel){
         rvi_p = oldRvi;      // make the old vi the current vi
     }
    // cerr << "gradChiSq: "
-   // 	<< "InitGrad = " << aInitGrad.formatAverage().c_str() << " " 
-   // 	<< "GetChanSel = " << aGetChanSel.formatAverage().c_str() << " " 
-   // 	<< "ChangeStokes = " << aChangeStokes.formatAverage().c_str() << " " 
-   // 	<< "CheckVisRows = " << aCheckVisRows.formatAverage().c_str() << " " 
-   // 	<< "GetFreq = " << aGetFreq.formatAverage().c_str() << " " 
-   // 	<< "OrigChunks = " << aOrigChunks.formatAverage().c_str() << " " 
-   // 	<< "VBInValid = " << aVBInValid.formatAverage().c_str() << " " 
-   // 	<< "InitGetSlice = " << aInitGetSlice.formatAverage().c_str() << " " 
-   // 	<< "InitPutSlice = " << aInitPutSlice.formatAverage().c_str() << " " 
-   // 	<< "PutSlice = " << aPutSlice.formatAverage().c_str() << " " 
-   // 	<< "FinalGetSlice = " << aFinalizeGetSlice.formatAverage().c_str() << " " 
-   // 	<< "FinalPutSlice = " << aFinalizePutSlice.formatAverage().c_str() << " " 
+   // 	<< "InitGrad = " << aInitGrad.formatAverage().c_str() << " "
+   // 	<< "GetChanSel = " << aGetChanSel.formatAverage().c_str() << " "
+   // 	<< "ChangeStokes = " << aChangeStokes.formatAverage().c_str() << " "
+   // 	<< "CheckVisRows = " << aCheckVisRows.formatAverage().c_str() << " "
+   // 	<< "GetFreq = " << aGetFreq.formatAverage().c_str() << " "
+   // 	<< "OrigChunks = " << aOrigChunks.formatAverage().c_str() << " "
+   // 	<< "VBInValid = " << aVBInValid.formatAverage().c_str() << " "
+   // 	<< "InitGetSlice = " << aInitGetSlice.formatAverage().c_str() << " "
+   // 	<< "InitPutSlice = " << aInitPutSlice.formatAverage().c_str() << " "
+   // 	<< "PutSlice = " << aPutSlice.formatAverage().c_str() << " "
+   // 	<< "FinalGetSlice = " << aFinalizeGetSlice.formatAverage().c_str() << " "
+   // 	<< "FinalPutSlice = " << aFinalizePutSlice.formatAverage().c_str() << " "
    // 	<< endl;
-   
-   // cerr << "VB loop: " 
-   // 	<< "InitModel = " << aInitModel.formatAverage().c_str() << " " 
-   // 	<< "GetSlice = " << aGetSlice.formatAverage().c_str() << " " 
-   // 	<< "SetModel = " << aSetModel.formatAverage().c_str() << " " 
-   // 	<< "GetRes = " << aGetRes.formatAverage().c_str() << " " 
-   // 	<< "PutSlice = " << aPutSlice.formatAverage().c_str() << " " 
-   // 	<< "Extra = " << aExtra.formatAverage().c_str() << " " 
+
+   // cerr << "VB loop: "
+   // 	<< "InitModel = " << aInitModel.formatAverage().c_str() << " "
+   // 	<< "GetSlice = " << aGetSlice.formatAverage().c_str() << " "
+   // 	<< "SetModel = " << aSetModel.formatAverage().c_str() << " "
+   // 	<< "GetRes = " << aGetRes.formatAverage().c_str() << " "
+   // 	<< "PutSlice = " << aPutSlice.formatAverage().c_str() << " "
+   // 	<< "Extra = " << aExtra.formatAverage().c_str() << " "
    // 	<< endl;
 }
 
-void  LofarCubeSkyEquation::isLargeCube(ImageInterface<Complex>& theIm, 
+void  LofarCubeSkyEquation::isLargeCube(ImageInterface<Complex>& theIm,
 				   Int& nslice) {
 
   //non-cube
@@ -896,7 +1037,7 @@ void  LofarCubeSkyEquation::isLargeCube(ImageInterface<Complex>& theIm,
     if(memtot < 512000){
       ostringstream oss;
       oss << "The amount of memory reported " << memtot << " kB is too small to work with" << endl;
-      throw(AipsError(String(oss))); 
+      throw(AipsError(String(oss)));
 
     }
     Long pixInMem=(memtot/8)*1024;
@@ -923,9 +1064,10 @@ void  LofarCubeSkyEquation::isLargeCube(ImageInterface<Complex>& theIm,
   }
 }
 
-void LofarCubeSkyEquation::initializePutSlice(const VisBuffer& vb, 
+void LofarCubeSkyEquation::initializePutSlice(const VisBuffer& vb,
 					 Int cubeSlice, Int nCubeSlice) {
 
+
   AlwaysAssert(ok(),AipsError);
   Bool dirDep= (ej_ != NULL);
   for(Int model=0; model < (sm_->numberOfModels()) ; ++model){
@@ -960,7 +1102,7 @@ LofarCubeSkyEquation::putSlice(VisBuffer & vb, Bool dopsf, FTMachine::Type col,
     firstOneChangesPut_p=False;  // Has this VB changed from the previous one?
     if((ftm_p[0]->name() != "MosaicFT")    && (ftm_p[0]->name() != "PBWProjectFT") &&
        (ftm_p[0]->name() != "AWProjectFT") && (ftm_p[0]->name() != "AWProjectWBFT") &&
-       (ftm_p[0]->name() != "LofarFTMachine") ) 
+       (ftm_p[0]->name() != "LofarFTMachine") )
     {
         changedSkyJonesLogic(vb, firstOneChangesPut_p, internalChangesPut_p);
     }
@@ -988,6 +1130,7 @@ LofarCubeSkyEquation::putSlice(VisBuffer & vb, Bool dopsf, FTMachine::Type col,
             }
 
             for (Int model=0; model<sm_->numberOfModels(); ++model){
+	      //  cout<<"model = "<<model<<endl;
                      iftm_p[model]->put(vb, row, dopsf, col);
             }
         }
@@ -1003,11 +1146,13 @@ LofarCubeSkyEquation::putSlice(VisBuffer & vb, Bool dopsf, FTMachine::Type col,
         initializePutSlice(vb, cubeSlice, nCubeSlice);
         isBeginingOfSkyJonesCache_p=False;
         for (Int model=0; model<sm_->numberOfModels(); ++model){
+	  //cout<<"modelb = "<<model<<endl;
                  iftm_p[model]->put(vb, -1, dopsf, col);
         }
     }
     else {
         for (Int model=0; model<sm_->numberOfModels(); ++model){
+	  //  cout<<"modelc = "<<model<<endl;
                 iftm_p[model]->put(vb, -1, dopsf, col);
         }
     }
@@ -1016,7 +1161,7 @@ LofarCubeSkyEquation::putSlice(VisBuffer & vb, Bool dopsf, FTMachine::Type col,
 
 }
 
-void LofarCubeSkyEquation::finalizePutSlice(const VisBuffer& vb,  
+void LofarCubeSkyEquation::finalizePutSlice(const VisBuffer& vb,
 				       Int cubeSlice, Int nCubeSlice) {
   for (Int model=0; model < sm_->numberOfModels(); ++model){
     //the different apply...jones use ft_ and ift_
@@ -1024,11 +1169,11 @@ void LofarCubeSkyEquation::finalizePutSlice(const VisBuffer& vb,
     ift_=&(*iftm_p[model]);
     // Actually do the transform. Update weights as we do so.
     iftm_p[model]->finalizeToSky();
-    // 1. Now get the (unnormalized) image and add the 
+    // 1. Now get the (unnormalized) image and add the
     // weight to the summed weight
     Matrix<Float> delta;
     imPutSlice_p[model]->copyData(iftm_p[model]->getImage(delta, False));
-    
+
 
 
 
@@ -1052,11 +1197,11 @@ void LofarCubeSkyEquation::finalizePutSlice(const VisBuffer& vb,
 		     *gSSlice);
     SubImage<Float> *ggSSlice;
     sliceCube(ggSSlice, sm_->ggS(model), cubeSlice, nCubeSlice);
-  
+
     // 3. Apply the square of the SkyJones and add this to gradgrad chisquared
     applySkyJonesSquare(vb, -1, weightSlice_p[model], *workSlice,
 			*ggSSlice);
-  
+
 
     delete workSlice;
     delete gSSlice;
@@ -1068,9 +1213,9 @@ void LofarCubeSkyEquation::finalizePutSlice(const VisBuffer& vb,
   sm_->addStatistics(sumwt, chisq);
 }
 
-void LofarCubeSkyEquation::initializeGetSlice(const VisBuffer& vb, 
-					   Int row, 
-					   Bool incremental, Int cubeSlice, 
+void LofarCubeSkyEquation::initializeGetSlice(const VisBuffer& vb,
+					   Int row,
+					   Bool incremental, Int cubeSlice,
 					   Int nCubeSlice){
   imGetSlice_p.resize(sm_->numberOfModels(), True, False);
   for(Int model=0; model < sm_->numberOfModels(); ++model){
@@ -1090,11 +1235,11 @@ void LofarCubeSkyEquation::initializeGetSlice(const VisBuffer& vb,
   }
   ft_=&(*ftm_p[0]);
   ift_=&(*iftm_p[0]);
-  
+
 
 }
 
-void LofarCubeSkyEquation::sliceCube(CountedPtr<ImageInterface<Complex> >& slice,Int model, Int cubeSlice, 
+void LofarCubeSkyEquation::sliceCube(CountedPtr<ImageInterface<Complex> >& slice,Int model, Int cubeSlice,
 				Int nCubeSlice, Int typeOfSlice){
 
   IPosition blc(4,0,0,0,0);
@@ -1110,8 +1255,8 @@ void LofarCubeSkyEquation::sliceCube(CountedPtr<ImageInterface<Complex> >& slice
   sl_p=Slicer (blc, trc, Slicer::endIsLast);
   SubImage<Complex>* sliceIm= new SubImage<Complex>(sm_->cImage(model), sl_p, False);
   //  cerr << "SliceCube: " << beginChannel << " " << endChannel << endl;
-  if(typeOfSlice==0){    
-    
+  if(typeOfSlice==0){
+
     Double memoryMB=HostInfo::memoryTotal(true)/1024.0/(8.0*(sm_->numberOfModels()));
     slice=new TempImage<Complex> (sliceIm->shape(), sliceIm->coordinates(), memoryMB);
     //slice.copyData(sliceIm);
@@ -1126,7 +1271,7 @@ void LofarCubeSkyEquation::sliceCube(CountedPtr<ImageInterface<Complex> >& slice
 }
 
 void LofarCubeSkyEquation::sliceCube(SubImage<Float>*& slice,
-				  ImageInterface<Float>& image, Int cubeSlice, 
+				  ImageInterface<Float>& image, Int cubeSlice,
 				  Int nCubeSlice){
   IPosition blc(4,0,0,0,0);
   IPosition trc(4,image.shape()(0)-1,
@@ -1143,7 +1288,7 @@ void LofarCubeSkyEquation::sliceCube(SubImage<Float>*& slice,
   slice=  new SubImage<Float> (image, sl_p, True);
 }
 
-VisBuffer& LofarCubeSkyEquation::getSlice(VisBuffer& result,  
+VisBuffer& LofarCubeSkyEquation::getSlice(VisBuffer& result,
 				     Bool incremental,
 				     Int cubeSlice, Int nCubeSlice) {
 
@@ -1175,7 +1320,7 @@ VisBuffer& LofarCubeSkyEquation::getSlice(VisBuffer& result,
     Matrix<Complex> refvb;
     for (Int row=0; row<nRow; row++) {
       finalizeGetSlice();
-      initializeGetSlice(result, row, False, cubeSlice, 
+      initializeGetSlice(result, row, False, cubeSlice,
 			 nCubeSlice);
       if(incremental || (nmodels > 1)){
 	for (Int model=0; model < nmodels; ++model){
@@ -1228,6 +1373,7 @@ LofarCubeSkyEquation::finalizeGetSlice(){
   //        ftm_p[model]->finalizeToVis();
 }
 
+
 Bool
 LofarCubeSkyEquation::getFreqRange(ROVisibilityIterator& vi,
                               const CoordinateSystem& coords,
@@ -1259,7 +1405,7 @@ LofarCubeSkyEquation::getFreqRange(ROVisibilityIterator& vi,
     if(nslice==1)
         return False;
 
-    Double start=0.0; 
+    Double start=0.0;
     Double end=0.0;
     Double chanwidth=1.0;
     Int specIndex=coords.findCoordinate(Coordinate::SPECTRAL);
@@ -1286,8 +1432,8 @@ LofarCubeSkyEquation::getFreqRange(ROVisibilityIterator& vi,
     if(spwb.nelements()==0)
         return False;
 
-    //vi.selectChannel(1, startb[0][0], nchanb[0][0], 1, spwb[0][0]); 
-    vi.selectChannel(blockNumChanGroup_p, startb, nchanb, incrb, spwb); 
+    //vi.selectChannel(1, startb[0][0], nchanb[0][0], 1, spwb[0][0]);
+    vi.selectChannel(blockNumChanGroup_p, startb, nchanb, incrb, spwb);
 
     return True;
 
@@ -1298,27 +1444,27 @@ void LofarCubeSkyEquation::fixImageScale()
   LogIO os(LogOrigin("LofarCubeSkyEquation", "fixImageScale"));
 
   // make a minimum value to ggS
-  // This has the same effect as Sault Weighting, but 
+  // This has the same effect as Sault Weighting, but
   // is implemented somewhat differently.
   // We also keep the fluxScale(mod) images around to
   // undo the weighting.
   Float ggSMax=0.0;
   for (Int model=0;model<sm_->numberOfModels();model++) {
-    
+
     LatticeExprNode LEN = max( sm_->ggS(model) );
     ggSMax =  max(ggSMax,LEN.getFloat());
   }
   ggSMax_p=ggSMax;
   Float ggSMin1;
   Float ggSMin2;
-  
+
   ggSMin1 = ggSMax * constPB_p * constPB_p;
   ggSMin2 = ggSMax * minPB_p * minPB_p;
-    
+
   for (Int model=0;model<sm_->numberOfModels();model++) {
     if(ej_ || (ftm_p[model]->name() == "MosaicFT") ) {
-      
-      
+
+
 
     /*Don't print this for now
       if (scaleType_p == "SAULT") {
@@ -1331,7 +1477,7 @@ void LofarCubeSkyEquation::fixImageScale()
     sm_->fluxScale(model).removeRegion ("mask0", RegionHandler::Any, False);
     if ((ftm_p[model]->name()!="MosaicFT")) {
       if(scaleType_p=="SAULT"){
-	
+
 	  // Adjust flux scale to account for ggS being truncated at ggSMin1
 	  // Below ggSMin2, set flux scale to 0.0
 	  // FluxScale * image => true brightness distribution, but
@@ -1339,47 +1485,47 @@ void LofarCubeSkyEquation::fixImageScale()
 	  // if ggS < ggSMin2, set to Zero;
 	  // if ggS > ggSMin2 && < ggSMin1, set to ggSMin1/ggS
 	  // if ggS > ggSMin1, set to 1.0
-	
-	sm_->fluxScale(model).copyData( (LatticeExpr<Float>) 
+
+	sm_->fluxScale(model).copyData( (LatticeExpr<Float>)
 					(iif(sm_->ggS(model) < (ggSMin2), 0.0,
 					     sqrt((sm_->ggS(model))/ggSMin1) )) );
-	sm_->fluxScale(model).copyData( (LatticeExpr<Float>) 
+	sm_->fluxScale(model).copyData( (LatticeExpr<Float>)
 					(iif(sm_->ggS(model) > (ggSMin1), 1.0,
 					     (sm_->fluxScale(model)) )) );
 	// truncate ggS at ggSMin1
-	sm_->ggS(model).copyData( (LatticeExpr<Float>) 
-				  (iif(sm_->ggS(model) < (ggSMin1), ggSMin1*(sm_->fluxScale(model)), 
+	sm_->ggS(model).copyData( (LatticeExpr<Float>)
+				  (iif(sm_->ggS(model) < (ggSMin1), ggSMin1*(sm_->fluxScale(model)),
 				       sm_->ggS(model)) )
 				  );
-	
+
 	}
 
 	else{
 
-	  sm_->fluxScale(model).copyData( (LatticeExpr<Float>) 
+	  sm_->fluxScale(model).copyData( (LatticeExpr<Float>)
 					  (iif(sm_->ggS(model) < (ggSMin2), 0.0,
 					       sqrt((sm_->ggS(model))/ggSMax) )) );
-	  sm_->ggS(model).copyData( (LatticeExpr<Float>) 
+	  sm_->ggS(model).copyData( (LatticeExpr<Float>)
 					  (iif(sm_->ggS(model) < (ggSMin2), 0.0,
 					       sqrt((sm_->ggS(model))*ggSMax) )) );
 
 	}
 
       } else {
-	
+
 	  Int nXX=sm_->ggS(model).shape()(0);
 	  Int nYY=sm_->ggS(model).shape()(1);
 	  Int npola= sm_->ggS(model).shape()(2);
 	  Int nchana= sm_->ggS(model).shape()(3);
 	  IPosition blc(4,nXX, nYY, npola, nchana);
 	  IPosition trc(4, nXX, nYY, npola, nchana);
-	  blc(0)=0; blc(1)=0; trc(0)=nXX-1; trc(1)=nYY-1; 
+	  blc(0)=0; blc(1)=0; trc(0)=nXX-1; trc(1)=nYY-1;
 
-	  //Those damn weights per plane can be wildly different so 
+	  //Those damn weights per plane can be wildly different so
 	  //deal with it properly here
 	  for (Int j=0; j < npola; ++j){
 	    for (Int k=0; k < nchana ; ++k){
-	      
+
 	      blc(2)=j; trc(2)=j;
 	      blc(3)=k; trc(3)=k;
 	      Slicer sl(blc, trc, Slicer::endIsLast);
@@ -1399,49 +1545,49 @@ void LofarCubeSkyEquation::fixImageScale()
 	      ///lets be conservative and go to 1% of ggsMin2
 	      if(planeMax !=0){
 		if(doflat_p){
-		  fscalesub.copyData( (LatticeExpr<Float>) 
-				      (iif(ggSSub < (ggSMin2/100.0), 
+		  fscalesub.copyData( (LatticeExpr<Float>)
+				      (iif(ggSSub < (ggSMin2/100.0),
 					   0.0, sqrt(ggSSub/planeMax))));
-		  ggSSub.copyData( (LatticeExpr<Float>) 
-				   (iif(ggSSub < (ggSMin2/100.0), 0.0, 
+		  ggSSub.copyData( (LatticeExpr<Float>)
+				   (iif(ggSSub < (ggSMin2/100.0), 0.0,
 					sqrt(planeMax*ggSSub))));
 		}
 		else{
-		  fscalesub.copyData( (LatticeExpr<Float>) 
-				      (iif(ggSSub < (ggSMin2/100.0), 
+		  fscalesub.copyData( (LatticeExpr<Float>)
+				      (iif(ggSSub < (ggSMin2/100.0),
 					   0.0, (ggSSub/planeMax))));
-		  ggSSub.copyData( (LatticeExpr<Float>) 
-				   (iif(ggSSub < (ggSMin2/100.0), 0.0, 
+		  ggSSub.copyData( (LatticeExpr<Float>)
+				   (iif(ggSSub < (ggSMin2/100.0), 0.0,
 					(planeMax))));
 		}
 
-		//ggSSub.copyData( (LatticeExpr<Float>) 
-		//		 (iif(ggSSub < (ggSMin2/100.0), 0.0, 
+		//ggSSub.copyData( (LatticeExpr<Float>)
+		//		 (iif(ggSSub < (ggSMin2/100.0), 0.0,
 		//		      planeMax)));
-	
+
 
 	      }
 	    }
 
 	  }
 	  /*
-	    
+
 	  ftm_p[model]->getFluxImage(sm_->fluxScale(model));
-	  
-	  sm_->fluxScale(model).copyData( (LatticeExpr<Float>) 
+
+	  sm_->fluxScale(model).copyData( (LatticeExpr<Float>)
 					  (iif(sm_->ggS(model) < (ggSMin2), 0.0,
 					  (sm_->ggS(model)/ggSMax) )) );
 
 	  */
-	  //}	
+	  //}
       }
-    
+
       //because for usual ft machines a applySJoneInv is done on the gS
       //in the finalizeput stage...need to understand if its necessary
       /*need to understand that square business
       if( (ft_->name() != "MosaicFT") && (!isPSFWork_p)){
-	sm_->gS(model).copyData( (LatticeExpr<Float>) 
-				 (iif(sm_->fluxScale(model) > 0.0, 
+	sm_->gS(model).copyData( (LatticeExpr<Float>)
+				 (iif(sm_->fluxScale(model) > 0.0,
 				      ((sm_->gS(model))/(sm_->fluxScale(model))), 0.0 )) );
 
       }
diff --git a/CEP/Imager/LofarFT/src/LofarFTMachine.cc b/CEP/Imager/LofarFT/src/LofarFTMachine.cc
index cc1bc5188a31ed334fffc71793aae94aa70a2bb0..413d5461746e44dce0645228c3b97beb48078e24 100644
--- a/CEP/Imager/LofarFT/src/LofarFTMachine.cc
+++ b/CEP/Imager/LofarFT/src/LofarFTMachine.cc
@@ -21,9 +21,9 @@
 //# $Id$
 
 #include <lofar_config.h>
-// #include <Common/LofarLogger.h>
-// #include <Common/Exception.h>
-#include <Common/OpenMP.h>
+// #include <Common/OpenMP.h>
+// #include <omp.h>
+
 #include <msvis/MSVis/VisibilityIterator.h>
 #include <casa/Quanta/UnitMap.h>
 #include <casa/Quanta/UnitVal.h>
@@ -76,6 +76,8 @@
 #include <casa/Utilities/CompositeNumber.h>
 #include <casa/OS/PrecTimer.h>
 #include <casa/sstream.h>
+#include <casa/OS/HostInfo.h>
+#include <casa/BasicMath/Random.h>
 #define DORES True
 
 
@@ -107,23 +109,39 @@ LofarFTMachine::LofarFTMachine(Long icachesize, Int itilesize,
                                const MeasurementSet& ms, Int nwPlanes,
                                MPosition mLocation, Float padding, Bool usezero,
                                Bool useDoublePrec, double wmax,
-			       const String& beamPath, Int verbose,
+                               Int verbose,
                                Int maxsupport, Int oversample,
                                const String& imgName,
                                const Matrix<bool>& gridMuellerMask,
-                               const Matrix<bool>& degridMuellerMask)
+                               const Matrix<bool>& degridMuellerMask,
+			       Double RefFreq,
+			       Bool Use_Linear_Interp_Gridder, 
+			       Bool Use_EJones, 
+			       int StepApplyElement, 
+			       Double PBCut, 
+			       Bool PredictFT, 
+			       String PsfOnDisk, 
+			       Bool UseMasksDegrid,
+			       Bool reallyDoPSF, 
+                               const Record& parameters
+                              )//, 
+			       //Double FillFactor)
   : FTMachine(), padding_p(padding), imageCache(0), cachesize(icachesize),
     tilesize(itilesize), gridder(0), isTiled(False), convType(iconvType),
     maxAbsData(0.0), centerLoc(IPosition(4,0)),
     offsetLoc(IPosition(4,0)), usezero_p(usezero), noPadding_p(False),
     usePut2_p(False), machineName_p("LofarFTMachine"), itsMS(ms),
-    itsNWPlanes(nwPlanes), itsWMax(wmax), itsConvFunc(0), itsBeamPath(beamPath),
+    itsNWPlanes(nwPlanes), itsWMax(wmax), itsConvFunc(0),
     itsVerbose(verbose),
     itsMaxSupport(maxsupport), itsOversample(oversample), itsImgName(imgName),
     itsGridMuellerMask(gridMuellerMask),
     itsDegridMuellerMask(degridMuellerMask),
-    itsGriddingTime(0), itsDegriddingTime(0), itsCFTime(0)
+    itsGriddingTime(0), itsDegriddingTime(0), itsCFTime(0), itsParameters(parameters)
 {
+  cout << "=======LofarFTMachine====================================" << endl;
+  cout << itsParameters << endl;
+  cout << "=========================================================" << endl;
+  
   logIO() << LogOrigin("LofarFTMachine", "LofarFTMachine")  << LogIO::NORMAL;
   logIO() << "You are using a non-standard FTMachine" << LogIO::WARN << LogIO::POST;
   mLocation_p=mLocation;
@@ -137,6 +155,32 @@ LofarFTMachine::LofarFTMachine(Long icachesize, Int itilesize,
   itsSumPB.resize (itsNThread);
   itsSumCFWeight.resize (itsNThread);
   itsSumWeight.resize (itsNThread);
+  itsRefFreq=RefFreq;
+  itsNamePsfOnDisk=PsfOnDisk;
+  its_Use_Linear_Interp_Gridder=Use_Linear_Interp_Gridder;
+  its_Use_EJones=Use_EJones;
+  its_UseMasksDegrid=UseMasksDegrid;
+  its_PBCut=PBCut;
+  its_reallyDoPSF=reallyDoPSF;
+  //its_FillFactor=FillFactor;
+  itsStepApplyElement=StepApplyElement;
+  its_Apply_Element=false;
+  itsPredictFT=PredictFT;
+  if(itsStepApplyElement>0){its_Apply_Element=true;}
+
+  if(its_Use_Linear_Interp_Gridder){
+    cout<<"Gridding using oversampling of 1 only"<<endl;
+    itsOversample=1;
+  };
+  //cout<<"FTMahin: itsRefFreq "<<itsRefFreq<<endl;
+
+  ROMSSpWindowColumns window(ms.spectralWindow());
+  itsListFreq.resize(window.nrow());
+  for(uInt i=0; i<window.nrow();++i){
+    itsListFreq[i]=window.refFrequency()(i);
+    cout<<"SPW"<<i<<", freq="<<itsListFreq[i]<<endl;
+  };
+  its_Already_Initialized=false;
 }
 
 //LofarFTMachine::LofarFTMachine(Long icachesize, Int itilesize,
@@ -224,6 +268,20 @@ LofarFTMachine& LofarFTMachine::operator=(const LofarFTMachine& other)
     itsNWPlanes = other.itsNWPlanes;
     itsWMax = other.itsWMax;
     itsConvFunc = other.itsConvFunc;
+    //cyrr: mfs
+    itsRefFreq=other.itsRefFreq;
+    thisterm_p=other.thisterm_p;
+    its_Use_Linear_Interp_Gridder= other.its_Use_Linear_Interp_Gridder;
+    its_Use_EJones= other.its_Use_EJones;
+    its_UseMasksDegrid=other.its_UseMasksDegrid;
+    its_Apply_Element= other.its_Apply_Element;
+    itsStepApplyElement=other.itsStepApplyElement;
+    its_Already_Initialized= other.its_Already_Initialized;
+    its_reallyDoPSF = other.its_reallyDoPSF;
+    its_PBCut= other.its_PBCut;
+    //its_FillFactor=other.its_FillFactor;
+     //cyrr: mfs
+
     ConjCFMap_p = other.ConjCFMap_p;
     CFMap_p = other.CFMap_p;
     itsNThread = other.itsNThread;
@@ -232,16 +290,17 @@ LofarFTMachine& LofarFTMachine::operator=(const LofarFTMachine& other)
     itsSumPB.resize (itsNThread);
     itsSumCFWeight.resize (itsNThread);
     itsSumWeight.resize (itsNThread);
-    itsBeamPath = other.itsBeamPath;
     itsVerbose = other.itsVerbose;
     itsMaxSupport = other.itsMaxSupport;
     itsOversample = other.itsOversample;
+    itsPredictFT = other.itsPredictFT;
     itsImgName = other.itsImgName;
     itsGridMuellerMask = other.itsGridMuellerMask;
     itsDegridMuellerMask = other.itsDegridMuellerMask;
     itsGriddingTime = other.itsGriddingTime;
     itsDegriddingTime = other.itsDegriddingTime;
     itsCFTime = other.itsCFTime;
+    itsParameters = other.itsParameters;
   }
   return *this;
 }
@@ -267,6 +326,7 @@ void LofarFTMachine::init() {
   logIO() << LogOrigin("LofarFTMachine", "init")  << LogIO::NORMAL;
   canComputeResiduals_p = DORES;
   ok();
+  //  cout<<"LofarFTMachine::init()" <<endl;
 
   /* hardwiring isTiled is False
   // Padding is possible only for non-tiled processing
@@ -280,6 +340,9 @@ void LofarFTMachine::init() {
   else {
   */
     // We are padding.
+
+  //cout<<"padding_p!!!!! "<<padding_p<<endl;
+
   isTiled=False;
   if(!noPadding_p){
     CompositeNumber cn(uInt(image->shape()(0)*2));
@@ -334,14 +397,19 @@ void LofarFTMachine::init() {
   itsConvFunc = new LofarConvolutionFunction(padded_shape,
                                              image->coordinates().directionCoordinate (image->coordinates().findCoordinate(Coordinate::DIRECTION)),
                                              itsMS, itsNWPlanes, itsWMax,
-                                             itsOversample, itsBeamPath,
-					     itsVerbose, itsMaxSupport,
-                                             itsImgName);
+                                             itsOversample,
+                                             itsVerbose, itsMaxSupport,
+                                             itsImgName+String::toString(thisterm_p),
+					     its_Use_EJones,
+					     its_Apply_Element,
+                                             itsParameters);
 
   // Set up image cache needed for gridding. For BOX-car convolution
   // we can use non-overlapped tiles. Otherwise we need to use
   // overlapped tiles and additive gridding so that only increments
   // to a tile are written.
+  its_Already_Initialized=true;
+
   if(imageCache) delete imageCache; imageCache=0;
 
   if(isTiled) {
@@ -364,6 +432,12 @@ void LofarFTMachine::init() {
 					   (tileOverlap>0.0));
 
   }
+  itsCyrilTimer.start();
+  itsTStartObs=1.e30;
+  itsDeltaTime=0.;
+  itsNextApplyTime=0.;;
+  itsCounterTimes=0;
+
 }
 
 // This is nasty, we should use CountedPointers here.
@@ -377,9 +451,11 @@ LofarFTMachine::~LofarFTMachine()
 
 const Matrix<Float>& LofarFTMachine::getAveragePB() const
 {
+  //cout<<"return beam"<<endl;
   // Read average beam from disk if not present.
   if (itsAvgPB.empty()) {
-    PagedImage<Float> pim(itsImgName + ".avgpb");
+    //cout<<"...read beam "<<itsImgName+String::toString(thisterm_p) + ".avgpb"<<endl;
+    PagedImage<Float> pim(itsImgName+String::toString(thisterm_p) + ".avgpb");
     Array<Float> arr = pim.get();
     itsAvgPB.reference (arr.nonDegenerate(2));
   }
@@ -391,13 +467,10 @@ const Matrix<Float>& LofarFTMachine::getAveragePB() const
 void LofarFTMachine::initializeToVis(ImageInterface<Complex>& iimage,
                                      const VisBuffer& vb)
 {
-  if (itsVerbose > 0) {
-    cout<<"---------------------------> initializeToVis"<<endl;
-  }
   image=&iimage;
 
   ok();
-  init();
+  if(!its_Already_Initialized){init();};//init();
 
   // Initialize the maps for polarization and channel. These maps
   // translate visibility indices into image indices
@@ -425,6 +498,8 @@ void LofarFTMachine::initializeToVis(ImageInterface<Complex>& iimage,
   // Note the other itsGriddedData buffers are assigned later.
   itsGriddedData[0].resize (gridShape);
   itsGriddedData[0] = Complex();
+  its_stacked_GriddedData.resize (gridShape);
+  its_stacked_GriddedData = Complex();
   for (int i=0; i<itsNThread; ++i) {
     itsSumPB[i].resize (padded_shape[0], padded_shape[1]);
     itsSumPB[i] = Complex();
@@ -432,6 +507,11 @@ void LofarFTMachine::initializeToVis(ImageInterface<Complex>& iimage,
     itsSumWeight[i].resize(npol, nchan);
     itsSumWeight[i] = 0.;
   }
+  itsCounterTimes=0;
+  itsTStartObs=1.e30;
+  itsDeltaTime=0.;
+  itsTotalStepsGrid=0;
+  itsTotalStepsDeGrid=0;
 
   //griddedData can be a reference of image data...if not using model col
   //hence using an undocumented feature of resize that if
@@ -446,10 +526,10 @@ void LofarFTMachine::initializeToVis(ImageInterface<Complex>& iimage,
         <<blc<<" "<<trc<<" "<<nx<<" "<<ny<<" "<<image->shape()<<endl;
   }
   IPosition start(4, 0);
-  itsGriddedData[0](blc, trc) = image->getSlice(start, image->shape());
+  its_stacked_GriddedData(blc, trc) = image->getSlice(start, image->shape());
   //if(arrayLattice) delete arrayLattice; arrayLattice=0;
   //======================CHANGED
-  arrayLattice = new ArrayLattice<Complex>(itsGriddedData[0]);
+  arrayLattice = new ArrayLattice<Complex>(its_stacked_GriddedData);
   // Array<Complex> result(IPosition(4, nx, ny, npol, nchan),0.);
   // griddedData=result;
   // arrayLattice = new ArrayLattice<Complex>(griddedData);
@@ -480,10 +560,9 @@ void LofarFTMachine::initializeToVis(ImageInterface<Complex>& iimage,
     // }
 
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-    // Normalising clean components by the beam 
-    
-    // const Matrix<Float>& datai = getSpheroidCut();
+    // Normalising clean components by the beam
 
+    // const Matrix<Float>& datai = getSpheroidCut();
     const Matrix<Float>& data = getAveragePB();
     //    cout<<"tmp.shape() "<<data.shape()<<"  "<<lattice->shape()<<endl;
     IPosition pos(4,lattice->shape()[0],lattice->shape()[1],1,1);
@@ -493,17 +572,43 @@ void LofarFTMachine::initializeToVis(ImageInterface<Complex>& iimage,
     pos2[2]=0.;
     pos2[3]=0.;
     Int offset_pad(floor(data.shape()[0]-lattice->shape()[0])/2.);
-    
+
     //    cout<<"LofarFTMachine::initializeToVis lattice->shape() == "<<lattice->shape()<<endl;
+    String nameii(itsImgName+String::toString(thisterm_p) + ".spheroid_cut_im");
+    ostringstream nameiii(nameii);
+    PagedImage<Float> tmpi(nameiii.str().c_str());
+    Slicer slicei(IPosition(4,0,0,0,0), tmpi.shape(), IPosition(4,1,1,1,1));
+    Array<Float> datai;
+    tmpi.doGetSlice(datai, slicei);
+
+    String nameii_element("Spheroid_cut_im_element.img");
+    ostringstream nameiii_element(nameii_element);
+    PagedImage<Float> tmpi_element(nameiii_element.str().c_str());
+    Slicer slicei_element(IPosition(4,0,0,0,0), tmpi_element.shape(), IPosition(4,1,1,1,1));
+    Array<Float> spheroidCutElement;
+    tmpi_element.doGetSlice(spheroidCutElement, slicei_element);
 
     Complex ff;
     double I=100.;
-    double Q=40.;
-    double U=20.;
-    double V=10.;
+    double Q=0.;
+    double U=0.;
+    double V=0.;
+
+    double maxPB(0.);
+    double minPB(1e10);
+    for(uInt i=0;i<lattice->shape()[0];++i){
+      for(uInt j=0;j<lattice->shape()[0];++j){
+	double pixel(data(i+offset_pad,j+offset_pad));
+	if(abs(pixel)>maxPB){maxPB=abs(pixel);};
+	if(abs(pixel)<minPB){minPB=abs(pixel);};
+      }
+    }
+
     for(Int k=0;k<lattice->shape()[2];++k){
       ff=0.;
+      //cout<<"k="<<k<<endl;
       if(k==0){ff=I+Q;}
+      // if(k==1){ff=I-Q;}
       if(k==1){ff=Complex(U,0.)+Complex(0.,V);}
       if(k==2){ff=Complex(U,0.)-Complex(0.,V);}
       if(k==3){ff=I-Q;}
@@ -518,27 +623,38 @@ void LofarFTMachine::initializeToVis(ImageInterface<Complex>& iimage,
 	  double fact(1.);
 
 	  // pixel=0.;
-	  // if((pos[0]==351.)&&(pos[1]==319.)){//319
+	  // if((pos[0]==372.)&&(pos[1]==370.)){//319
 	  //   pixel=ff;//*139./143;//-100.;
-	  //   if(datai(pos2)>1e-6){fact/=datai(pos2);};//*datai(pos2);};
+	  //   //if(datai(pos2)>1e-6){fact/=datai(pos2)*datai(pos2);};//*datai(pos2);};
+	  //   //if(datai(pos2)>1e-6){fact*=sqrt(maxPB)/sqrt(data(pos2));};
+	  //   fact*=sqrt(maxPB)/sqrt(data(pos2));
 	  //   //if(data(pos2)>1e-6){fact/=sqrt(data(pos2));};//*datai(pos2);};
 	  //   pixel*=Complex(fact);
 	  // }
-	  
-	  fact/=sqrt(data(pos2));
+
+	  if(!itsPredictFT){
+	    fact*=sqrt(maxPB)/sqrt(data(pos2));
+	  } else {
+	    fact/=datai(pos2); //*datai(pos2); 
+	    if(its_Apply_Element){fact/=spheroidCutElement(pos2);}
+	  }
 	  pixel*=Complex(fact);
-	  
-	  lattice->putAt(pixel,pos);
+
+	  if((data(pos2)>=(minPB))&&(abs(pixel)>0.)){   // SvdT: Had to make comparison great _or equal_ because of fake PB consisting of all ones
+	    lattice->putAt(pixel,pos);
+	  };
 	}
       }
     }
 
-
     //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 
     // Now do the FFT2D in place
     LatticeFFT::cfft2d(*lattice);
 
+    if((!(itsConvFunc->itsFilledVectorMasks))&&(its_Apply_Element)){itsConvFunc->ReadMaskDegrid();}
+    //if((!(itsConvFunc->VectorMaskIsFilled()))&&(its_Apply_Element)){itsConvFunc->ReadMaskDegrid();}
+
     logIO() << LogIO::DEBUGGING
 	    << "Finished grid correction and FFT of image" << LogIO::POST;
 
@@ -550,7 +666,7 @@ void LofarFTMachine::initializeToVis(ImageInterface<Complex>& iimage,
     // 	  pos[2]=k;
     // 	  Complex pixel(lattice->getAt(pos));
     // 	  //cout<<"i,j,pixel value: "<<i<<" "<<j<<" "<<pixel<<endl;
-	  
+
     // 	};
     //   };
     // };
@@ -574,10 +690,10 @@ void LofarFTMachine::initializeToSky(ImageInterface<Complex>& iimage,
 {
   // image always points to the image
   image=&iimage;
-  if (itsVerbose > 0) {
-    cout<<"---------------------------> initializeToSky"<<endl;
-  }
-  init();
+  //if (itsVerbose > 0) {
+  //cout<<"---------------------------> initializeToSky"<<" its_Already_Initialized : "<<its_Already_Initialized<<endl;
+  //}
+    if(!its_Already_Initialized){init();};
 
   // Initialize the maps for polarization and channel. These maps
   // translate visibility indices into image indices
@@ -593,6 +709,8 @@ void LofarFTMachine::initializeToSky(ImageInterface<Complex>& iimage,
   AlwaysAssert (!isTiled, AipsError);
   IPosition gridShape(4, nx, ny, npol, nchan);
   // Size and initialize the grid buffer per thread.
+  its_stacked_GriddedData.resize (gridShape);
+  its_stacked_GriddedData = Complex();
   for (int i=0; i<itsNThread; ++i) {
     itsGriddedData[i].resize (gridShape);
     itsGriddedData[i] = Complex();
@@ -604,10 +722,15 @@ void LofarFTMachine::initializeToSky(ImageInterface<Complex>& iimage,
   }
   weight.resize(itsSumWeight[0].shape());
   weight=0.0;
+  itsCounterTimes=0;
+  itsTStartObs=1.e30;
+  itsDeltaTime=0.;
+  itsTotalStepsGrid=0;
+  itsTotalStepsDeGrid=0;
 
   //iimage.get(griddedData, False);
   //if(arrayLattice) delete arrayLattice; arrayLattice=0;
-  arrayLattice = new ArrayLattice<Complex>(itsGriddedData[0]);
+  arrayLattice = new ArrayLattice<Complex>(its_stacked_GriddedData);
   lattice=arrayLattice;
   // if(useDoubleGrid_p) visResampler_p->initializePutBuffers(griddedData2, sumWeight);
   // else                visResampler_p->initializePutBuffers(griddedData, sumWeight);
@@ -628,41 +751,61 @@ void LofarFTMachine::finalizeToSky()
     cout<<"---------------------------> finalizeToSky"<<endl;
   }
   // DEBUG: Store the grid per thread
-  uInt nx(itsGriddedData[0].shape()[0]);
-  IPosition shapecube(3,nx,nx,4);
-  for (int ii=0; ii<itsNThread; ++ii) {
-    Cube<Complex> tempimage(shapecube,0.);
-    for(Int k=0;k<4;++k){
-      for(uInt i=0;i<nx;++i){
-  	for(uInt j=0;j<nx;++j){
-	  IPosition pos(4,i,j,k,0);
-	  Complex pixel(itsGriddedData[ii](pos));
-	  tempimage(i,j,k)=pixel;
-  	}
-      }
+  // uInt nx(itsGriddedData[0].shape()[0]);
+  // IPosition shapecube(3,nx,nx,4);
+  // for (int ii=0; ii<itsNThread; ++ii) {
+  //   Cube<Complex> tempimage(shapecube,0.);
+  //   for(Int k=0;k<itsGriddedData[0].shape()[2];++k){
+  //     for(uInt i=0;i<nx;++i){
+  // 	for(uInt j=0;j<nx;++j){
+  // 	  IPosition pos(4,i,j,k,0);
+  // 	  Complex pixel(itsGriddedData[ii](pos));
+  // 	  tempimage(i,j,k)=pixel;
+  // 	}
+  //     }
+  //   }
+  //   store(tempimage,"Grid"+String::toString(ii)+".img");
+  // }
+
+  // Add all buffers into the first one.
+
+  // for(uInt channel=0;channel< its_stacked_GriddedData.shape()[3];++channel){
+  //   for(uInt jj=0;jj<its_stacked_GriddedData.shape()[2];++jj){
+  //     cout<<"Add all buffers into the first one. jj="<<jj<<endl;
+  //     Matrix<Complex> plane_array_out  = its_stacked_GriddedData(Slicer(IPosition(4, 0, 0, jj, 0),
+  // 									IPosition(4, nx, nx, 1, 1))).nonDegenerate();
+  //     ArrayLattice<Complex> lattice(plane_array_out);
+  //     LatticeFFT::cfft2d(lattice, true);
+  //     plane_array_out/=static_cast<Float>(plane_array_out.shape()(0)*plane_array_out.shape()(1));
+  //   }
+  // }
+
+  if(!its_Apply_Element){
+    SumGridsOMP(its_stacked_GriddedData, itsGriddedData);
+    for (int i=0; i<itsNThread; ++i) {
+      itsGriddedData[i]=Complex();
     }
-    store(tempimage,"Grid"+String::toString(ii)+".img");
   }
 
-  // Add all buffers into the first one.
+
   for (int i=1; i<itsNThread; ++i) {
-    itsGriddedData[0] += itsGriddedData[i];
+    //itsGriddedData[0] += itsGriddedData[i];
     itsSumWeight[0]   += itsSumWeight[i];
     itsSumCFWeight[0] += itsSumCFWeight[i];
     itsSumPB[0]       += itsSumPB[i];
   }
 
-  Cube<Complex> tempimage(shapecube,0.);
-  for(Int k=0;k<4;++k){
-    for(uInt i=0;i<nx;++i){
-      for(uInt j=0;j<nx;++j){
-	IPosition pos(4,i,j,k,0);
-	Complex pixel(itsGriddedData[0](pos));
-	tempimage(i,j,k)=pixel;
-      }
-    }
-  }
-  store(tempimage,"Grid00.img");
+  // Cube<Complex> tempimage(IPosition(3,nx,nx,4),0.);
+  // for(Int k=0;k<4;++k){
+  //   for(uInt i=0;i<nx;++i){
+  //     for(uInt j=0;j<nx;++j){
+  // 	IPosition pos(4,i,j,k,0);
+  // 	Complex pixel(its_stacked_GriddedData(pos));
+  // 	tempimage(i,j,k)=pixel;
+  //     }
+  //   }
+  // }
+  // store(tempimage,"Grid00.img");
 
   // if(useDoubleGrid_p) visResamplers_p[0].GatherGrids(griddedData2, sumWeight);
   // else                visResamplers_p[0].GatherGrids(griddedData, sumWeight);
@@ -687,6 +830,11 @@ Array<Complex>* LofarFTMachine::getDataPointer(const IPosition& centerLoc2D,
 void LofarFTMachine::put(const VisBuffer& vb, Int row, Bool dopsf,
                          FTMachine::Type type)
 {
+
+  itsCyrilTimer.stop();
+  //PrecTimer TimerCyril;
+  //TimerCyril.start();
+
   if (itsVerbose > 0) {
     logIO() << LogOrigin("LofarFTMachine", "put") << LogIO::NORMAL
             << "I am gridding " << vb.nRow() << " row(s)."  << LogIO::POST;
@@ -709,16 +857,21 @@ void LofarFTMachine::put(const VisBuffer& vb, Int row, Bool dopsf,
     chanMap=multiChanMap_p[vb.spectralWindow()];
   }
 
+
+
+
+  //cout<<"... Gridding Spectral Window:    "<<vb.spectralWindow()<<", with Taylor Term: "<< thisterm_p<<endl;
+
+  uInt spw(vb.spectralWindow());
+
   //No point in reading data if it's not matching in frequency
   if(max(chanMap)==-1) return;
 
   const Matrix<Float> *imagingweight;
   imagingweight=&(vb.imagingWeight());
 
-  // dopsf=true;
-
+  if(its_reallyDoPSF) {dopsf=true;}
   if(dopsf) {type=FTMachine::PSF;}
-
   Cube<Complex> data;
   //Fortran gridder need the flag as ints
   Cube<Int> flags;
@@ -737,10 +890,27 @@ void LofarFTMachine::put(const VisBuffer& vb, Int row, Bool dopsf,
   // irrelevant for other cases.
   Matrix<Double> uvw(3, vb.uvw().nelements());  uvw=0.0;
   Vector<Double> dphase(vb.uvw().nelements());  dphase=0.0;
+
+
+  // // const Vector<Double>& times = vb.timeCentroid();
+  // // double time = 0.5 * (times[times.size()-1] + times[0]);
+  // const Vector<Double>& freq = vb.lsrFrequency();
+  // const Vector<Int>& obs = vb.observationId();
+  // Vector<Double> lsrFreq(0);
+  // Bool condoo=False;
+  // vb.lsrFrequency(0, lsrFreq, condoo);
+  // cout<<"mmm " <<lsrFreq<<" "<<condoo<<endl;
+  // vb.lsrFrequency(1, lsrFreq, condoo);
+  // cout<<"mmmmm " <<lsrFreq<<" "<<condoo<<endl;
+  //const Vector<Double>& timess = vb.timeCentroid();
+
   //NEGATING to correct for an image inversion problem
   for (Int i=startRow;i<=endRow;i++) {
     for (Int idim=0;idim<2;idim++) uvw(idim,i)=-vb.uvw()(i)(idim);
     uvw(2,i)=vb.uvw()(i)(2);
+    // cout << "freq  "<< freq[i]   << endl;
+    // cout << "obsid "<< obs[i]    << vb.dataDescriptionId() <<endl;
+    // cout << "times "<< timess[i] << endl;
   }
 
   rotateUVW(uvw, dphase, vb);
@@ -784,6 +954,7 @@ void LofarFTMachine::put(const VisBuffer& vb, Int row, Bool dopsf,
 	  blEnd.push_back (i-1);
 	}
       }
+
       // Skip auto-correlations and high W-values.
       // All w values are close, so if first w is too high, skip baseline.
       usebl = false;
@@ -826,6 +997,8 @@ void LofarFTMachine::put(const VisBuffer& vb, Int row, Bool dopsf,
   // However the VBS objects should ultimately be references
   // directly to bool cubes.
   //**************
+
+
   vbs.flagCube_p.resize(flags.shape());    vbs.flagCube_p = False; vbs.flagCube_p(flags!=0) = True;
   //  vbs.flagCube_p.reference(vb.flagCube());
   //**************
@@ -835,36 +1008,73 @@ void LofarFTMachine::put(const VisBuffer& vb, Int row, Bool dopsf,
   visResamplers_p.setMaps(chanMap, polMap);
 
   // First compute the A-terms for all stations (if needed).
+  PrecTimer CyrilTimer2Aterm;
+  CyrilTimer2Aterm.start();
   itsConvFunc->computeAterm (time);
+  CyrilTimer2Aterm.stop();
+  double Taterm=CyrilTimer2Aterm.getReal();
 
   uInt Nchannels = vb.nChannel();
 
   itsTotalTimer.start();
-#pragma omp parallel 
+
+  vector< Bool> done;
+  done.resize(int(blStart.size()));
+  for(int i=0; i<int(blStart.size()); ++i) {done[i]=false;};
+
+  Bool all_done(false);
+  Int doagain(0);
+
+  ///  Int Max_Num_Threads(itsNThread);
+  ///  omp_set_num_threads(Max_Num_Threads);
+
+
+  //logIO() <<"============================== Gridding data " << LogIO::POST;
+  //cout<<"... gridding with t= "<<time<<endl;
+  PrecTimer CyrilTimer2grid;
+    PrecTimer CyrilTimer2conv;
+    PrecTimer CyrilTimer2gridconv;
+    CyrilTimer2gridconv.start();
+    //    CyrilTimer2conv.reset();
+
+  while(!all_done){
+
+#pragma omp parallel
   {
     // Thread-private variables.
     PrecTimer gridTimer;
     PrecTimer cfTimer;
+    PrecTimer CyrilTimer;
     // The for loop can be parallellized. This must be done dynamically,
     // because the execution times of iterations can vary greatly.
+
+
 #pragma omp for schedule(dynamic)
     for (int i=0; i<int(blStart.size()); ++i) {
       Int ist  = blIndex[blStart[i]];
       Int iend = blIndex[blEnd[i]];
+      if(done[i]==true){continue;};
+      //if(doagain>0){
+	//cout<<"Doing again (doagain) baseline: A1="<<ant1[ist]<<", A2="<<ant2[ist]<<endl;
+      //}
+
+      try{
 
       // compute average weight for baseline for CF averaging
-      double average_weight(0.);
-      uInt Nvis(0);
+	double average_weight=0.;
+      uInt Nvis=0;
       for(Int j=ist; j<iend; ++j){
         uInt row=blIndex[j];
         if(!vbs.rowFlag()[row]){
           Nvis+=1;
-          for(uint k=0; k<Nchannels; ++k) {
+          for(uInt k=0; k<Nchannels; ++k) {
             average_weight=average_weight+vbs.imagingWeight()(k,row);
           }
         }
       }
-      average_weight=average_weight/Nvis;
+      if(Nvis>0){
+	average_weight=average_weight/Nvis;
+      } else {average_weight=0.;}
       ///        itsSumWeight += average_weight * average_weight;
       if (itsVerbose > 1) {
         cout<<"average weights= "<<average_weight<<", Nvis="<<Nvis<<endl;
@@ -877,27 +1087,31 @@ void LofarFTMachine::put(const VisBuffer& vb, Int row, Bool dopsf,
         cout.precision(20);
         cout<<"A1="<<ant1[ist]<<", A2="<<ant2[ist]<<", time="<<fixed<<time<<endl;
       }
-      LofarCFStore cfStore;
       //#pragma omp critical(LofarFTMachine_makeConvolutionFunction)
       //{
+      CyrilTimer2conv.start();
       cfTimer.start();
-      cfStore =
+      Double Wmean=0.5*(vbs.uvw()(2,ist) + vbs.uvw()(2,iend));
+      //cout<< Wmean<<endl;
+      LofarCFStore cfStore =
         itsConvFunc->makeConvolutionFunction (ant1[ist], ant2[ist], time,
-                                              0.5*(vbs.uvw()(2,ist) + vbs.uvw()(2,iend)),
+                                              Wmean,
                                               itsGridMuellerMask, false,
                                               average_weight,
                                               itsSumPB[threadNum],
-                                              itsSumCFWeight[threadNum]);
-      cfTimer.stop();
-      //};
+                                              itsSumCFWeight[threadNum],
+					      spw,thisterm_p,itsRefFreq
+					      );
+      
 
 
-      //cout<<"DONE LOADING CF..."<<endl;
-      //Double or single precision gridding.
-      //	cout<<"============================================"<<endl;
-      //	cout<<"Antenna "<<ant1[ist]<<" and "<<ant2[ist]<<endl;
-      //#pragma omp critical(LofarFTMachine_makeConvolutionFunction)
-      //{
+      //cfTimer.stop();
+      CyrilTimer2conv.stop();
+
+      Int nConvX = (*(cfStore.vdata))[0][0][0].shape()[0];
+      //cout<<ant1[ist]<<" "<<ant2[ist]<<" " <<nConvX/5<<endl;
+      //double cfstep=CyrilTimer2conv.getReal();
+      CyrilTimer2grid.start();
       if (useDoubleGrid_p) {
         visResamplers_p.lofarDataToGrid(itsGriddedData2[threadNum], vbs, blIndex,
                                         blStart[i], blEnd[i],
@@ -907,13 +1121,35 @@ void LofarFTMachine::put(const VisBuffer& vb, Int row, Bool dopsf,
           cout<<"  gridding"<<" thread="<<threadNum<<'('<<itsNThread<<"), A1="<<ant1[ist]<<", A2="<<ant2[ist]<<", time=" <<time<<endl;
         }
         gridTimer.start();
-        visResamplers_p.lofarDataToGrid
-          (itsGriddedData[threadNum], vbs, blIndex, blStart[i],
-           blEnd[i], itsSumWeight[threadNum], dopsf, cfStore);
-        gridTimer.stop();
+	if(!its_Use_Linear_Interp_Gridder){
+	  //cout<<"itsGriddedData[threadNum] "<<itsGriddedData[threadNum].shape()<<endl;
+	  visResamplers_p.lofarDataToGrid
+	    (itsGriddedData[threadNum], vbs, blIndex, blStart[i],
+	     blEnd[i], itsSumWeight[threadNum], dopsf, cfStore);
+	} else{
+	  visResamplers_p.lofarDataToGrid_linear
+	    (itsGriddedData[threadNum], vbs, blIndex, blStart[i],
+	     blEnd[i], itsSumWeight[threadNum], dopsf, cfStore);
+
+	};
+	  gridTimer.stop();
       }
+      CyrilTimer2grid.stop();
+      //cout<<"Gridding calculation: "<<nConvX<<" "<<cfstep<<" "<<CyrilTimer2grid.getReal()<<endl;
+      //CyrilTimer2grid.reset();
+      //CyrilTimer2conv.reset();
+      //CyrilTimer.reset();
+      done[i]=true;
+      } catch (std::bad_alloc &)
+	{
+	  cout<<"-----------------------------------------"<<endl;
+	  cout<<"!!!!!!! GRIDDING: Skipping baseline: "<<ant1[ist]<<" | "<<ant2[ist]<<endl;
+	  cout<<"memoryUsed() "<< HostInfo::memoryUsed()<< ", Free: "<<HostInfo::memoryFree()<<endl;
+	  cout<<"-----------------------------------------"<<endl;
+	};
       // } // end omp critical
     } // end omp for
+
     double cftime = cfTimer.getReal();
 #pragma omp atomic
     itsCFTime += cftime;
@@ -921,7 +1157,67 @@ void LofarFTMachine::put(const VisBuffer& vb, Int row, Bool dopsf,
 #pragma omp atomic
     itsGriddingTime += gtime;
   } // end omp parallel
+
+    all_done=true;
+    int number_missed(0);
+    for (int i=0; i<int(blStart.size()); ++i) {
+      if(done[i]==false){all_done=false;number_missed+=1;};
+    };
+    if(all_done==false){
+      //cout<<"================================"<<endl;
+      //cout<<"Memory exception returned by "<<number_missed<<" threads"<<endl;
+      //cout<<"Reducing number of threads to: "<<int(omp_get_num_threads()/2.)<<endl;
+      //cout<<"================================"<<endl;
+      doagain+=1;
+      //omp_set_num_threads(int(omp_get_num_threads()/2.));
+    };
+
+  }//end While loop
+
+  CyrilTimer2gridconv.stop();
+  double Tgridconv=CyrilTimer2gridconv.getReal();
+
+  PrecTimer CyrilTimer2elem;
+  if(itsDeltaTime<(times[times.size()-1] - times[0])){itsDeltaTime=(times[times.size()-1] - times[0]);};
+  Bool lastchunk(false);
+  if((times[times.size()-1] - times[0])<0.95*itsDeltaTime){
+    lastchunk=true;
+    itsDeltaTime=0.;
+  }
+
+  //cout<<"time: "<<time<<" "<<itsStepApplyElement<<" "<<its_Apply_Element<<endl;
+  CyrilTimer2elem.start();
+
+  if(itsCounterTimes==(itsStepApplyElement-1)/2){itsNextApplyTime=time;}
+  if(its_Apply_Element){
+    if((itsCounterTimes==itsStepApplyElement-1)||(lastchunk)){
+      Array<Complex> tmp_stacked_GriddedData;
+      tmp_stacked_GriddedData.resize (itsGriddedData[0].shape());
+      tmp_stacked_GriddedData = Complex();
+      SumGridsOMP(tmp_stacked_GriddedData, itsGriddedData);
+      //itsConvFunc->MakeMaskDegrid(tmp_stacked_GriddedData, itsTotalStepsGrid);
+      Array<Complex> tmp_stacked_GriddedData_appliedelement=itsConvFunc->ApplyElementBeam2 (tmp_stacked_GriddedData, itsNextApplyTime, spw, itsGridMuellerMask, false);
+      if(its_UseMasksDegrid){
+	itsConvFunc->MakeMaskDegrid(tmp_stacked_GriddedData_appliedelement, itsTotalStepsGrid);
+      }
+      SumGridsOMP(its_stacked_GriddedData, tmp_stacked_GriddedData_appliedelement);
+      CyrilTimer2elem.stop();
+      itsCounterTimes=0;
+      for (int i=0; i<itsNThread; ++i) {
+	itsGriddedData[i]=Complex();
+      }
+      itsTotalStepsGrid+=1;
+    } else {
+      itsCounterTimes+=1;
+    }
+  }
+
+  CyrilTimer2elem.stop();
+  //cout<<"times: aterm:"<<Taterm<<", conv: "<<CyrilTimer2conv.getReal()<<", grid: "<<CyrilTimer2grid.getReal()<<", gridconv: "<<Tgridconv<<", sum: "<<CyrilTimer2elem.getReal()<<", other: "<<itsCyrilTimer.getReal()<<endl;
+  //cout<<"times: conv:"<<CyrilTimer2conv.getReal()<<", grid:"<<CyrilTimer2grid.getReal()<<", element:"<<CyrilTimer2elem.getReal()<<endl;
   itsTotalTimer.stop();
+  itsCyrilTimer.reset();
+  itsCyrilTimer.start();
 }
 
 
@@ -929,7 +1225,9 @@ void LofarFTMachine::put(const VisBuffer& vb, Int row, Bool dopsf,
 void LofarFTMachine::get(VisBuffer& vb, Int row)
 {
   if (itsVerbose > 0) {
-    cout<<"///////////////////// GET!!!!!!!!!!!!!!!!!!"<<endl;
+    logIO() << LogOrigin("LofarFTMachine", "get") << LogIO::NORMAL
+            << "I am degridding " << vb.nRow() << " row(s)."  << LogIO::POST;
+    logIO() << LogIO::NORMAL << "Padding is " << padding_p  << LogIO::POST;
   }
   gridOk(gridder->cSupport()(0));
   // If row is -1 then we pass through all rows
@@ -950,6 +1248,8 @@ void LofarFTMachine::get(VisBuffer& vb, Int row)
 
   //Check if ms has changed then cache new spw and chan selection
   if(vb.newMS())  matchAllSpwChans(vb);
+  uInt spw(vb.spectralWindow());
+  //cout<<"... De-Gridding Spectral Window: "<<vb.spectralWindow()<<", with Taylor Term: "<< thisterm_p<<endl;
 
 
   //Channel matching for the actual spectral window of buffer
@@ -980,7 +1280,7 @@ void LofarFTMachine::get(VisBuffer& vb, Int row)
   vbs.uvw_p.reference(uvw);
   //    vbs.imagingWeight.reference(elWeight);
   vbs.visCube_p.reference(data);
-  
+
   vbs.freq_p.reference(interpVisFreq_p);
   vbs.rowFlag_p.resize(0); vbs.rowFlag_p = vb.flagRow();
   if(!usezero_p)
@@ -1016,19 +1316,23 @@ void LofarFTMachine::get(VisBuffer& vb, Int row)
   bool usebl      = false;
   bool allFlagged = true;
   const Vector<Bool>& flagRow = vb.flagRow();
-  for (uint i=0; i<blnr.size(); ++i) {
+  for (uint i=0; i<blnr.size(); ++i) 
+  {
     Int inx = blIndex[i];
     Int bl = blnr[inx];
-    if (bl != lastbl) {
+    if (bl != lastbl) 
+    {
       // New baseline. Write the previous end index if applicable.
-      if (usebl  &&  !allFlagged) {
+      if (usebl  &&  !allFlagged) 
+      {
         double Wmean(0.5*(vb.uvw()[blIndex[lastIndex]](2) + vb.uvw()[blIndex[i-1]](2)));
-        if (abs(Wmean) <= itsWMax) {
-	  if (itsVerbose > 1) {
-	    cout<<"using w="<<Wmean<<endl;
-	  }
-	  blStart.push_back (lastIndex);
-	  blEnd.push_back (i-1);
+        if (abs(Wmean) <= itsWMax) 
+        {
+          if (itsVerbose > 1) {
+            cout<<"using w="<<Wmean<<endl;
+          }
+          blStart.push_back (lastIndex);
+          blEnd.push_back (i-1);
         }
       }
       // Skip auto-correlations and high W-values.
@@ -1045,13 +1349,17 @@ void LofarFTMachine::get(VisBuffer& vb, Int row)
     if (! flagRow[inx]) {
       allFlagged = false;
     }
+    
   }
   // Write the last end index if applicable.
-  if (usebl  &&  !allFlagged) {
+  if (usebl  &&  !allFlagged) 
+  {
     double Wmean(0.5*(vb.uvw()[blIndex[lastIndex]](2) + vb.uvw()[blIndex[blnr.size()-1]](2)));
-    if (abs(Wmean) <= itsWMax) {
-      if (itsVerbose > 1) {
-	cout<<"...using w="<<Wmean<<endl;
+    if (abs(Wmean) <= itsWMax) 
+    {
+      if (itsVerbose > 1) 
+      {
+        cout<<"...using w="<<Wmean<<endl;
       }
       blStart.push_back (lastIndex);
       blEnd.push_back (blnr.size()-1);
@@ -1066,7 +1374,71 @@ void LofarFTMachine::get(VisBuffer& vb, Int row)
   // First compute the A-terms for all stations (if needed).
   itsConvFunc->computeAterm (time);
 
+  if(times[0]<itsTStartObs){itsTStartObs=times[0];}
+  if(itsDeltaTime<(times[times.size()-1] - times[0])){itsDeltaTime=(times[times.size()-1] - times[0]);};
+  if(itsDeltaTime<(times[times.size()-1] - times[0])){itsDeltaTime=(times[times.size()-1] - times[0]);};
+
   itsTotalTimer.start();
+
+  vector< Bool> done;
+  done.resize(int(blStart.size()));
+  for(int i=0; i<int(blStart.size()); ++i) {done[i]=false;};
+
+  Bool all_done(false);
+  ///  Int Max_Num_Threads(itsNThread);
+  ///  omp_set_num_threads(Max_Num_Threads);
+
+  PrecTimer CyrilElement;
+  CyrilElement.start();
+  cout.precision(20);
+  //cout<<" ======================= De-Grid ... time="<<time<<", at "<<itsCounterTimes<<endl;
+  if(its_Apply_Element){
+    //cout<<"itsCounterTimes= "<<itsCounterTimes<<endl;
+    if(itsCounterTimes==0){
+     double TimeElement(itsTStartObs+itsDeltaTime*itsStepApplyElement/2.);
+     //cout<<"... Appying element with t="<<TimeElement<<", itsTStartObs="<<itsTStartObs<<", itsDeltaTime="<<itsDeltaTime<<endl;
+     itsConvFunc->computeAterm(TimeElement);
+     if(its_UseMasksDegrid){
+       itsGridToDegrid.reference(itsConvFunc->ApplyElementBeam2(its_stacked_GriddedData, TimeElement, spw, itsGridMuellerMask, true, itsTotalStepsDeGrid));
+     }else{
+       itsGridToDegrid.reference(itsConvFunc->ApplyElementBeam2(its_stacked_GriddedData, TimeElement, spw, itsGridMuellerMask, true));
+     }
+     itsTotalStepsDeGrid+=1;
+    }
+    itsCounterTimes+=1;
+    if(itsCounterTimes==itsStepApplyElement){
+      itsTStartObs=1.e30;
+      itsCounterTimes=0;
+    }
+    Bool lastchunk(false);
+    if((times[times.size()-1] - times[0])<0.95*itsDeltaTime){
+      //cout<<"Last Chunk Degrid!!!"<<endl;
+      lastchunk=true;
+      itsDeltaTime=0.;
+      itsTStartObs=1e12;
+    }
+
+
+  } else{
+    itsGridToDegrid.reference(its_stacked_GriddedData);
+  }
+  CyrilElement.stop();
+
+  // arrayLattice = new ArrayLattice<Complex>(tmp_stacked_GriddedData2);
+  // cout<<"LofarConvolutionFunction::ApplyElementBeam "<<"FFT the element corrected model image"<<endl;
+  // lattice=arrayLattice;
+  // LatticeFFT::cfft2d(*lattice);
+
+  //logIO() <<"============================== De-Gridding data " << LogIO::POST;
+  PrecTimer CyrilConv;
+  PrecTimer CyrilGrid;
+
+
+  while(!all_done){
+
+
+
+
 #pragma omp parallel
   {
     // Thread-private variables.
@@ -1080,12 +1452,16 @@ void LofarFTMachine::get(VisBuffer& vb, Int row)
       // {
       Int ist  = blIndex[blStart[i]];
       Int iend = blIndex[blEnd[i]];
+      if(done[i]==true){continue;};
+      try {
       int threadNum = OpenMP::threadNum();
       // Get the convolution function for degridding.
-      if (itsVerbose > 1) {
-	cout<<"ANTENNA "<<ant1[ist]<<" "<<ant2[ist]<<endl;
-      }
+     if (itsVerbose > 1) {
+        cout<<"ANTENNA "<<ant1[ist]<<" "<<ant2[ist]<<endl;
+     }
       cfTimer.start();
+      CyrilConv.start();
+
       LofarCFStore cfStore =
         itsConvFunc->makeConvolutionFunction (ant1[ist], ant2[ist], time,
                                               0.5*(vbs.uvw()(2,ist) + vbs.uvw()(2,iend)),
@@ -1093,15 +1469,29 @@ void LofarFTMachine::get(VisBuffer& vb, Int row)
                                               true,
                                               0.0,
                                               itsSumPB[threadNum],
-                                              itsSumCFWeight[threadNum]);
+                                              itsSumCFWeight[threadNum]
+					      ,spw,thisterm_p,itsRefFreq);
       cfTimer.stop();
 
-      //Double or single precision gridding.
-      //      cout<<"GRID "<<ant1[ist]<<" "<<ant2[ist]<<endl;
+      CyrilConv.stop();
+      CyrilGrid.start();
+
       degridTimer.start();
-      visResamplers_p.lofarGridToData(vbs, itsGriddedData[0],
+      visResamplers_p.lofarGridToData(vbs, itsGridToDegrid,//its_stacked_GriddedData,//itsGriddedData[0],
                                       blIndex, blStart[i], blEnd[i], cfStore);
+      CyrilGrid.stop();
+
       degridTimer.stop();
+      done[i]=true;
+
+      } catch (std::bad_alloc &)
+	{
+	  cout<<"-----------------------------------------"<<endl;
+	  cout<<"!!!!!!! DE-GRIDDING: Skipping baseline: "<<ant1[ist]<<" | "<<ant2[ist]<<endl;
+	  cout<<"memoryUsed() "<< HostInfo::memoryUsed()<< ", Free: "<<HostInfo::memoryFree()<<endl;
+	  cout<<"-----------------------------------------"<<endl;
+	}
+
     } // end omp for
     double cftime = cfTimer.getReal();
 #pragma omp atomic
@@ -1110,6 +1500,27 @@ void LofarFTMachine::get(VisBuffer& vb, Int row)
 #pragma omp atomic
     itsGriddingTime += gtime;
   } // end omp parallel
+
+    all_done=true;
+    int number_missed(0);
+    for (int i=0; i<int(blStart.size()); ++i) {
+      //cout<<"done: "<<i<<" "<<done[i]<<endl;
+      if(done[i]==false){all_done=false;number_missed+=1;};
+    };
+    if(all_done==false){
+      //cout<<"================================"<<endl;
+      //cout<<"Memory exception returned by "<<number_missed<<" threads"<<endl;
+      //cout<<"Reducing number of threads to: "<<int(omp_get_num_threads()/2.)<<endl;
+      //cout<<"================================"<<endl;
+      //omp_set_num_threads(int(omp_get_num_threads()/2.));
+    };
+
+  }//end While loop
+
+  //cout<<"Element: "<<CyrilElement.getReal()<<", Conv: "<<CyrilConv.getReal()<<", Grid: "<<CyrilGrid.getReal()<<endl;;
+
+
+
   itsTotalTimer.stop();
   interpolateFrequencyFromgrid(vb, data, FTMachine::MODEL);
 }
@@ -1125,9 +1536,6 @@ ImageInterface<Complex>& LofarFTMachine::getImage(Matrix<Float>& weights, Bool n
   AlwaysAssert(image, AipsError);
   logIO() << LogOrigin("LofarFTMachine", "getImage") << LogIO::NORMAL;
 
-  if (itsVerbose > 0) {
-    cout<<"GETIMAGE"<<endl;
-  }
   itsAvgPB.reference (itsConvFunc->Compute_avg_pb(itsSumPB[0], itsSumCFWeight[0]));
 
   //cout<<"weights.shape() "<<weights.shape()<<"  "<<sumWeight<<endl;
@@ -1236,10 +1644,10 @@ ImageInterface<Complex>& LofarFTMachine::getImage(Matrix<Float>& weights, Bool n
     // posi[2]=0.;
     // posi[3]=0.;
     // posi2[2]=0.;
-    // posi2[3]=0.;    
+    // posi2[3]=0.;
     // Int offset_pad(floor(data.shape()[0]-lattice->shape()[0])/2.);
 
-    
+
 
 
     // for(uInt k=0;k<lattice->shape()[2];++k){
@@ -1267,6 +1675,19 @@ ImageInterface<Complex>& LofarFTMachine::getImage(Matrix<Float>& weights, Bool n
     Cube<Complex> tempimage(IPosition(3,shapeout,shapeout,lattice->shape()[2]));
 
     pos[3]=0.;
+    double minPB(1e10);
+    double maxPB(0.);
+    for(uInt i=0;i<shapeout;++i){
+      for(uInt j=0;j<shapeout;++j){
+	double pixel(itsAvgPB(i+istart,j+istart));
+	if(abs(pixel)>maxPB){maxPB=abs(pixel);};
+	if(abs(pixel)<minPB){minPB=abs(pixel);};
+      }
+    }
+
+    const Matrix<Float>& sphe = getSpheroidCut();
+
+    //maxPB=1.;
     for(Int k=0;k<lattice->shape()[2];++k){
       for(uInt i=0;i<shapeout;++i){
     	for(uInt j=0;j<shapeout;++j){
@@ -1274,26 +1695,40 @@ ImageInterface<Complex>& LofarFTMachine::getImage(Matrix<Float>& weights, Bool n
     	  pos[1]=j+istart;
     	  pos[2]=k;
     	  Complex pixel(lattice->getAt(pos));
-    	  //cout<<"pixel value: "<<pixel<<", Primary beam: "<<avg_PB(i,j)<<endl;
-    	  pixel/=sqrt(itsAvgPB(i+istart,j+istart));//*sqrt(avg_PB(i+istart,j+istart));
-    	  //pixel*=(lattice->shape()[0]*lattice->shape()[0]);
+
+    	  pixel*=sqrt(maxPB)/sqrt(itsAvgPB(i+istart,j+istart));
+
+
+	  //if(itsAvgPB(pos)<1e-6*maxPB){pixel=0.;}
+	  if((sqrt(itsAvgPB(pos))/sphe(pos)<its_PBCut)||(itsAvgPB(pos)<2.*minPB)){pixel=0.;}
     	  lattice->putAt(pixel,pos);
-    	  //tempimage(i,j,k)=pixel/weights(0,0);
+    	  tempimage(i,j,k)=pixel;///weights(0,0);
     	}
       }
     }
+
     // uInt count_cycle(0);
     // Bool written(false);
 
     // while(!written){
+    //   Cube<Complex> tempimagePB(IPosition(3,shapeout,shapeout,lattice->shape()[2]));
+    //   for(Int k=0;k<lattice->shape()[2];++k){
+    // 	for(uInt i=0;i<shapeout;++i){
+    // 	  for(uInt j=0;j<shapeout;++j){
+    // 	    tempimagePB(i,j,k)=itsAvgPB(i,j);
+    // 	  };
+    // 	};
+    //   };
     //   cout<<"count_cycle ======================= "<<count_cycle<<" "<<normalize<<endl;
     //   File myFile("Cube_dirty.img"+String::toString(count_cycle));
     //   if(!myFile.exists()){
-    // 	written=true;
-    // 	store(tempimage,"Cube_dirty.img"+String::toString(count_cycle));
+    //   	written=true;
+    //   	store(tempimage,"Cube_dirty.img"+String::toString(count_cycle));
+    //   	store(tempimagePB,"Cube_dirty.img"+String::toString(count_cycle)+".pb");
+
     //   }
     //   else{
-    // 	count_cycle++;
+    //   	count_cycle++;
     //   };
     // };
 
@@ -1310,7 +1745,7 @@ ImageInterface<Complex>& LofarFTMachine::getImage(Matrix<Float>& weights, Bool n
       IPosition trc(blc+image->shape()-stride);
       // Do the copy
       IPosition start(4, 0);
-      image->put(itsGriddedData[0](blc, trc));
+      image->put(its_stacked_GriddedData(blc, trc));
     }
   }
 
@@ -1448,16 +1883,16 @@ Bool LofarFTMachine::fromRecord(String& error, const RecordInterface& inRec)
       // Make the grid the correct shape and turn it into an array lattice
       // Check the section from the image BEFORE converting to a lattice
       IPosition gridShape(4, nx, ny, npol, nchan);
-      itsGriddedData[0].resize(gridShape);
-      itsGriddedData[0]=Complex(0.0);
+      its_stacked_GriddedData.resize(gridShape);
+      its_stacked_GriddedData=Complex(0.0);
       IPosition blc(4, (nx-image->shape()(0)+(nx%2==0))/2, (ny-image->shape()(1)+(ny%2==0))/2, 0, 0);
       IPosition start(4, 0);
       IPosition stride(4, 1);
       IPosition trc(blc+image->shape()-stride);
-      itsGriddedData[0](blc, trc)=image->getSlice(start, image->shape());
+      its_stacked_GriddedData(blc, trc)=image->getSlice(start, image->shape());
 
       //if(arrayLattice) delete arrayLattice; arrayLattice=0;
-      arrayLattice = new ArrayLattice<Complex>(itsGriddedData[0]);
+      arrayLattice = new ArrayLattice<Complex>(its_stacked_GriddedData);
       lattice=arrayLattice;
     }
 
@@ -1555,7 +1990,11 @@ String LofarFTMachine::name(){
 
 void LofarFTMachine::ComputeResiduals(VisBuffer&vb, Bool useCorrected)
 {
+
+  //cout<<"LofarFTMachine::ComputeResiduals "<<vb.corrType()<<endl;
   LofarVBStore vbs;
+  PrecTimer TimerResid;
+  TimerResid.start();
   vbs.nRow_p = vb.nRow();
   vbs.beginRow_p = 0;
   vbs.endRow_p = vbs.nRow_p;
@@ -1563,12 +2002,15 @@ void LofarFTMachine::ComputeResiduals(VisBuffer&vb, Bool useCorrected)
   if (useCorrected) vbs.correctedCube_p.reference(vb.correctedVisCube());
   else vbs.visCube_p.reference(vb.visCube());
   //  cout<<"BLA===="<<vb.visCube()<<"    "<<useCorrected<<endl;
-  
+
   //for(uInt i=0;i<vbs.nRow_p;++i){cout<<"ROW "<<i<<" "<<vb.antenna1()(i)<<" "<<vb.antenna2()(i)<<endl;};
 
   vbs.useCorrected_p = useCorrected;
   visResamplers_p.lofarComputeResiduals(vbs);
 
+  TimerResid.stop();
+  //cout<<"Residuals: "<<TimerResid.getReal()<<endl;
+
   //  vb.correctedVisCube()=0.;//vb.modelVisCube();
 }
 
@@ -1576,6 +2018,7 @@ void LofarFTMachine::ComputeResiduals(VisBuffer&vb, Bool useCorrected)
 					 const ImageInterface<Complex>& imageTemplate,
 					 ImageInterface<Float>& sensitivityImage)
   {
+    cout<<"============================== makeSensitivityImage"<<endl;
     if (convFuncCtor_p->makeAverageResponse(vb, imageTemplate, sensitivityImage))
       cfCache_p->flush(sensitivityImage,sensitivityPatternQualifierStr_p);
   }
@@ -1928,6 +2371,7 @@ void LofarFTMachine::ComputeResiduals(VisBuffer&vb, Bool useCorrected)
   {
     LogIO log_l(LogOrigin("LofarFTMachine", "findPointingOffsets"));
     Vector<Int> msStokes = vb.corrType();
+    //cout<<"LofarFTMachine findPointingOffsets "<< msStokes << " "<<locCfStokes<<endl;
     Int nPol = msStokes.nelements();
     polM.resize(polMap.shape());
     polM = -1;
@@ -1978,7 +2422,7 @@ void LofarFTMachine::ComputeResiduals(VisBuffer&vb, Bool useCorrected)
     conjPolMap = cfPolMap;
 
     Int i,j,N = cfPolMap.nelements();
-    
+
     for(i=0;i<N;i++)
       if (cfPolMap[i] > -1)
 	{
diff --git a/CEP/Imager/LofarFT/src/LofarFTMachineOld.cc b/CEP/Imager/LofarFT/src/LofarFTMachineOld.cc
new file mode 100644
index 0000000000000000000000000000000000000000..63de319cb5653155980cdc93d330b6db028bb0bf
--- /dev/null
+++ b/CEP/Imager/LofarFT/src/LofarFTMachineOld.cc
@@ -0,0 +1,2037 @@
+//# LofarFTMachineOld.cc: Gridder for LOFAR data correcting for DD effects
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#include <lofar_config.h>
+// #include <Common/LofarLogger.h>
+// #include <Common/Exception.h>
+#include <Common/OpenMP.h>
+#include <msvis/MSVis/VisibilityIterator.h>
+#include <casa/Quanta/UnitMap.h>
+#include <casa/Quanta/UnitVal.h>
+#include <measures/Measures/Stokes.h>
+#include <coordinates/Coordinates/CoordinateSystem.h>
+#include <coordinates/Coordinates/DirectionCoordinate.h>
+#include <coordinates/Coordinates/SpectralCoordinate.h>
+#include <coordinates/Coordinates/StokesCoordinate.h>
+#include <coordinates/Coordinates/Projection.h>
+#include <ms/MeasurementSets/MSColumns.h>
+#include <casa/BasicSL/Constants.h>
+#include <scimath/Mathematics/FFTServer.h>
+#include <LofarFT/LofarFTMachineOld.h>
+#include <LofarFT/LofarCFStore.h>
+#include <LofarFT/LofarConvolutionFunctionOld.h>
+#include <synthesis/MeasurementComponents/Utils.h>
+#include <LofarFT/LofarVisResamplerOld.h>
+#include <synthesis/MeasurementComponents/CFStore.h>
+#include <LofarFT/LofarVBStore.h>
+#include <scimath/Mathematics/RigidVector.h>
+#include <msvis/MSVis/StokesVector.h>
+#include <synthesis/MeasurementEquations/StokesImageUtil.h>
+#include <msvis/MSVis/VisBuffer.h>
+#include <msvis/MSVis/VisSet.h>
+#include <images/Images/ImageInterface.h>
+#include <images/Images/PagedImage.h>
+#include <casa/Containers/Block.h>
+#include <casa/Containers/Record.h>
+#include <casa/Arrays/ArrayLogical.h>
+#include <casa/Arrays/ArrayMath.h>
+#include <casa/Arrays/Array.h>
+#include <casa/Arrays/MaskedArray.h>
+#include <casa/Arrays/Vector.h>
+#include <casa/Arrays/Slicer.h>
+#include <casa/Arrays/Matrix.h>
+#include <casa/Arrays/Cube.h>
+#include <casa/Arrays/MatrixIter.h>
+#include <casa/BasicSL/String.h>
+#include <casa/Utilities/Assert.h>
+#include <casa/Exceptions/Error.h>
+#include <lattices/Lattices/ArrayLattice.h>
+#include <measures/Measures/UVWMachine.h>
+#include <lattices/Lattices/SubLattice.h>
+#include <lattices/Lattices/LCBox.h>
+#include <lattices/Lattices/LatticeCache.h>
+#include <lattices/Lattices/LatticeFFT.h>
+#include <lattices/Lattices/LatticeIterator.h>
+#include <lattices/Lattices/LatticeStepper.h>
+#include <scimath/Mathematics/ConvolveGridder.h>
+#include <casa/Utilities/CompositeNumber.h>
+#include <casa/OS/PrecTimer.h>
+#include <casa/sstream.h>
+#define DORES True
+
+
+using namespace casa;
+
+namespace LOFAR { //# NAMESPACE CASA - BEGIN
+
+//  LofarFTMachineOld::LofarFTMachineOld(Long icachesize, Int itilesize,
+//                                 CountedPtr<VisibilityResamplerBase>&,
+//                                 String iconvType, Float padding,
+//                                 Bool usezero, Bool useDoublePrec)
+//: FTMachine(), padding_p(padding), imageCache(0), cachesize(icachesize), tilesize(itilesize),
+//  gridder(0), isTiled(False), convType(iconvType),
+//  maxAbsData(0.0), centerLoc(IPosition(4,0)), offsetLoc(IPosition(4,0)),
+//  usezero_p(usezero), noPadding_p(False), usePut2_p(False),
+//  machineName_p("LofarFTMachineOld")
+
+//{
+////   LOG_INFO ("LofarFTMachineOld::LofarFTMachineOld" << 1.0);
+////   logIO() << LogOrigin("LofarFTMachineOld", "LofarFTMachineOld")  << LogIO::NORMAL;
+//  logIO() << "You are using a non-standard FTMachine" << LogIO::WARN << LogIO::POST;
+//  useDoubleGrid_p=useDoublePrec;
+//  canComputeResiduals_p=DORES;
+//}
+
+LofarFTMachineOld::LofarFTMachineOld(Long icachesize, Int itilesize,
+                               CountedPtr<VisibilityResamplerBase>&,
+                               String iconvType,
+                               const MeasurementSet& ms, Int nwPlanes,
+                               MPosition mLocation, Float padding, Bool usezero,
+                               Bool useDoublePrec, double wmax,
+			       const String& beamPath, Int verbose,
+                               Int maxsupport, Int oversample,
+                               const String& imgName,
+                               const Matrix<bool>& gridMuellerMask,
+                               const Matrix<bool>& degridMuellerMask)
+  : FTMachine(), padding_p(padding), imageCache(0), cachesize(icachesize),
+    tilesize(itilesize), gridder(0), isTiled(False), convType(iconvType),
+    maxAbsData(0.0), centerLoc(IPosition(4,0)),
+    offsetLoc(IPosition(4,0)), usezero_p(usezero), noPadding_p(False),
+    usePut2_p(False), machineName_p("LofarFTMachineOld"), itsMS(ms),
+    itsNWPlanes(nwPlanes), itsWMax(wmax), itsConvFunc(0), itsBeamPath(beamPath),
+    itsVerbose(verbose),
+    itsMaxSupport(maxsupport), itsOversample(oversample), itsImgName(imgName),
+    itsGridMuellerMask(gridMuellerMask),
+    itsDegridMuellerMask(degridMuellerMask),
+    itsGriddingTime(0), itsDegriddingTime(0), itsCFTime(0)
+{
+  logIO() << LogOrigin("LofarFTMachineOld", "LofarFTMachineOld")  << LogIO::NORMAL;
+  logIO() << "You are using a non-standard FTMachine" << LogIO::WARN << LogIO::POST;
+  mLocation_p=mLocation;
+  tangentSpecified_p=False;
+  useDoubleGrid_p=useDoublePrec;
+  canComputeResiduals_p=DORES;
+  itsNThread = OpenMP::maxThreads();
+  AlwaysAssert (itsNThread>0, AipsError);
+  itsGriddedData.resize (itsNThread);
+  itsGriddedData2.resize (itsNThread);
+  itsSumPB.resize (itsNThread);
+  itsSumCFWeight.resize (itsNThread);
+  itsSumWeight.resize (itsNThread);
+}
+
+//LofarFTMachineOld::LofarFTMachineOld(Long icachesize, Int itilesize,
+//		 CountedPtr<VisibilityResamplerBase>&,
+//		 String iconvType,
+//		 MDirection mTangent, Float padding, Bool usezero, Bool useDoublePrec)
+//: FTMachine(), padding_p(padding), imageCache(0), cachesize(icachesize),
+//  tilesize(itilesize), gridder(0), isTiled(False), convType(iconvType), maxAbsData(0.0), centerLoc(IPosition(4,0)),
+//  offsetLoc(IPosition(4,0)), usezero_p(usezero), noPadding_p(False),
+//  usePut2_p(False), machineName_p("LofarFTMachineOld")
+//{
+//  logIO() << LogOrigin("LofarFTMachineOld", "LofarFTMachineOld")  << LogIO::NORMAL;
+//  logIO() << "You are using a non-standard FTMachine" << LogIO::WARN << LogIO::POST;
+//  mTangent_p=mTangent;
+//  tangentSpecified_p=True;
+//  useDoubleGrid_p=useDoublePrec;
+//  canComputeResiduals_p=DORES;
+//}
+
+//LofarFTMachineOld::LofarFTMachineOld(Long icachesize, Int itilesize,
+//		 CountedPtr<VisibilityResamplerBase>&,
+//		 String iconvType, MPosition mLocation, MDirection mTangent, Float padding,
+//		 Bool usezero, Bool useDoublePrec)
+//: FTMachine(), padding_p(padding), imageCache(0), cachesize(icachesize),
+//  tilesize(itilesize), gridder(0), isTiled(False), convType(iconvType), maxAbsData(0.0), centerLoc(IPosition(4,0)),
+//  offsetLoc(IPosition(4,0)), usezero_p(usezero), noPadding_p(False),
+//  usePut2_p(False),machineName_p("LofarFTMachineOld")
+//{
+//  logIO() << LogOrigin("LofarFTMachineOld", "LofarFTMachineOld")  << LogIO::NORMAL;
+//  logIO() << "You are using a non-standard FTMachine" << LogIO::WARN << LogIO::POST;
+//  mLocation_p=mLocation;
+//  mTangent_p=mTangent;
+//  tangentSpecified_p=True;
+//  useDoubleGrid_p=useDoublePrec;
+//  canComputeResiduals_p=DORES;
+//}
+
+//LofarFTMachineOld::LofarFTMachineOld(const RecordInterface& stateRec)
+//  : FTMachine()
+//{
+//  // Construct from the input state record
+//  logIO() << LogOrigin("LofarFTMachineOld", "LofarFTMachineOld(RecordInterface)")  << LogIO::NORMAL;
+//  logIO() << "You are using a non-standard FTMachine" << LogIO::WARN << LogIO::POST;
+//  String error;
+//  if (!fromRecord(error, stateRec))
+//    throw (AipsError("Failed to create gridder: " + error));
+//  canComputeResiduals_p=DORES;
+//}
+
+//----------------------------------------------------------------------
+LofarFTMachineOld& LofarFTMachineOld::operator=(const LofarFTMachineOld& other)
+{
+  if(this!=&other) {
+    //Do the base parameters
+    FTMachine::operator=(other);
+
+    //private params
+    imageCache=other.imageCache;
+    cachesize=other.cachesize;
+    tilesize=other.tilesize;
+    convType=other.convType;
+    uvScale.resize();
+    uvOffset.resize();
+    uvScale=other.uvScale;
+    uvOffset=other.uvOffset;
+    if(other.gridder==0)
+      gridder=0;
+    else{
+      gridder = new ConvolveGridder<Double, Complex>(IPosition(2, nx, ny),
+						     uvScale, uvOffset,
+						     convType);
+    }
+    isTiled=other.isTiled;
+    //lattice=other.lattice;
+    lattice=0;
+    tilesize=other.tilesize;
+    arrayLattice=0;
+    maxAbsData=other.maxAbsData;
+    centerLoc=other.centerLoc;
+    offsetLoc=other.offsetLoc;
+    padding_p=other.padding_p;
+    usezero_p=other.usezero_p;
+    noPadding_p=other.noPadding_p;
+    itsMS = other.itsMS;
+    itsNWPlanes = other.itsNWPlanes;
+    itsWMax = other.itsWMax;
+    itsConvFunc = other.itsConvFunc;
+    ConjCFMap_p = other.ConjCFMap_p;
+    CFMap_p = other.CFMap_p;
+    itsNThread = other.itsNThread;
+    itsGriddedData.resize (itsNThread);
+    itsGriddedData2.resize (itsNThread);
+    itsSumPB.resize (itsNThread);
+    itsSumCFWeight.resize (itsNThread);
+    itsSumWeight.resize (itsNThread);
+    itsBeamPath = other.itsBeamPath;
+    itsVerbose = other.itsVerbose;
+    itsMaxSupport = other.itsMaxSupport;
+    itsOversample = other.itsOversample;
+    itsImgName = other.itsImgName;
+    itsGridMuellerMask = other.itsGridMuellerMask;
+    itsDegridMuellerMask = other.itsDegridMuellerMask;
+    itsGriddingTime = other.itsGriddingTime;
+    itsDegriddingTime = other.itsDegriddingTime;
+    itsCFTime = other.itsCFTime;
+  }
+  return *this;
+}
+
+//----------------------------------------------------------------------
+  LofarFTMachineOld::LofarFTMachineOld(const LofarFTMachineOld& other) : FTMachine(), machineName_p("LofarFTMachineOld")
+  {
+    //  visResampler_p.init(useDoubleGrid_p);
+    operator=(other);
+  }
+
+//----------------------------------------------------------------------
+//  CountedPtr<LofarFTMachineOld> LofarFTMachineOld::clone() const
+  LofarFTMachineOld* LofarFTMachineOld::clone() const
+  {
+    LofarFTMachineOld* newftm = new LofarFTMachineOld(*this);
+    return newftm;
+  }
+
+//----------------------------------------------------------------------
+void LofarFTMachineOld::init() {
+
+  logIO() << LogOrigin("LofarFTMachineOld", "init")  << LogIO::NORMAL;
+  canComputeResiduals_p = DORES;
+  ok();
+
+  /* hardwiring isTiled is False
+  // Padding is possible only for non-tiled processing
+  if((padding_p*padding_p*image->shape().product())>cachesize) {
+    isTiled=True;
+    nx    = image->shape()(0);
+    ny    = image->shape()(1);
+    npol  = image->shape()(2);
+    nchan = image->shape()(3);
+  }
+  else {
+  */
+    // We are padding.
+  isTiled=False;
+  if(!noPadding_p){
+    CompositeNumber cn(uInt(image->shape()(0)*2));
+    nx    = cn.nextLargerEven(Int(padding_p*Float(image->shape()(0))-0.5));
+    ny    = cn.nextLargerEven(Int(padding_p*Float(image->shape()(1))-0.5));
+  }
+  else{
+    nx    = image->shape()(0);
+    ny    = image->shape()(1);
+  }
+  npol  = image->shape()(2);
+  nchan = image->shape()(3);
+    // }
+
+  uvScale.resize(3);
+  uvScale=0.0;
+  uvScale(0)=Float(nx)*image->coordinates().increment()(0);
+  uvScale(1)=Float(ny)*image->coordinates().increment()(1);
+  uvScale(2)=Float(1)*abs(image->coordinates().increment()(0));
+
+  uvOffset.resize(3);
+  uvOffset(0)=nx/2;
+  uvOffset(1)=ny/2;
+  uvOffset(2)=0;
+
+  // Now set up the gridder. The possibilities are BOX and SF
+  if(gridder) delete gridder; gridder=0;
+  gridder = new ConvolveGridder<Double, Complex>(IPosition(2, nx, ny),
+						 uvScale, uvOffset,
+						 convType);
+
+  // Setup the CFStore object to carry relavent info. of the Conv. Func.
+  cfs_p.xSupport = gridder->cSupport();
+  cfs_p.ySupport = gridder->cSupport();
+  cfs_p.sampling.resize(2);
+  cfs_p.sampling = gridder->cSampling();
+  if (cfs_p.rdata.null())
+    cfs_p.rdata = new Array<Double>(gridder->cFunction());
+  // else
+  //   (*cfs_p.rdata) = gridder->cFunction();
+
+  padded_shape = image->shape();
+  padded_shape(0) = nx;
+  padded_shape(1) = ny;
+  if (itsVerbose > 0) {
+    cout << "Original shape " << image->shape()(0) << ","
+         << image->shape()(1) << endl;
+    cout << "Padded shape " << padded_shape(0) << ","
+         << padded_shape(1) << endl;
+  }
+  //assert(padded_shape(0)!=image->shape()(0));
+  itsConvFunc = new LofarConvolutionFunctionOld(padded_shape,
+                                             image->coordinates().directionCoordinate (image->coordinates().findCoordinate(Coordinate::DIRECTION)),
+                                             itsMS, itsNWPlanes, itsWMax,
+                                             itsOversample, itsBeamPath,
+					     itsVerbose, itsMaxSupport,
+                                             itsImgName);
+
+  // Set up image cache needed for gridding. For BOX-car convolution
+  // we can use non-overlapped tiles. Otherwise we need to use
+  // overlapped tiles and additive gridding so that only increments
+  // to a tile are written.
+  if(imageCache) delete imageCache; imageCache=0;
+
+  if(isTiled) {
+    Float tileOverlap=0.5;
+    if(convType=="box") {
+      tileOverlap=0.0;
+    }
+    else {
+      tileOverlap=0.5;
+      tilesize=max(12,tilesize);
+    }
+    IPosition tileShape=IPosition(4,tilesize,tilesize,npol,nchan);
+    Vector<Float> tileOverlapVec(4);
+    tileOverlapVec=0.0;
+    tileOverlapVec(0)=tileOverlap;
+    tileOverlapVec(1)=tileOverlap;
+    Int tmpCacheVal=static_cast<Int>(cachesize);
+    imageCache=new LatticeCache <Complex> (*image, tmpCacheVal, tileShape,
+					   tileOverlapVec,
+					   (tileOverlap>0.0));
+
+  }
+}
+
+// This is nasty, we should use CountedPointers here.
+LofarFTMachineOld::~LofarFTMachineOld()
+{
+  if(imageCache) delete imageCache; imageCache=0;
+  //if(arrayLattice) delete arrayLattice; arrayLattice=0;
+  if(gridder) delete gridder; gridder=0;
+//  delete itsConvFunc;
+}
+
+const Matrix<Float>& LofarFTMachineOld::getAveragePB() const
+{
+  // Read average beam from disk if not present.
+  if (itsAvgPB.empty()) {
+    PagedImage<Float> pim(itsImgName + ".avgpb");
+    Array<Float> arr = pim.get();
+    itsAvgPB.reference (arr.nonDegenerate(2));
+  }
+  return itsAvgPB;
+}
+
+// Initialize for a transform from the Sky domain. This means that
+// we grid-correct, and FFT the image
+void LofarFTMachineOld::initializeToVis(ImageInterface<Complex>& iimage,
+                                     const VisBuffer& vb)
+{
+  if (itsVerbose > 0) {
+    cout<<"---------------------------> initializeToVis"<<endl;
+  }
+  image=&iimage;
+
+  ok();
+  init();
+
+  // Initialize the maps for polarization and channel. These maps
+  // translate visibility indices into image indices
+  initMaps(vb);
+
+  visResamplers_p.init(useDoubleGrid_p);
+  visResamplers_p.setMaps(chanMap, polMap);
+  visResamplers_p.setCFMaps(CFMap_p, ConjCFMap_p);
+
+  // Need to reset nx, ny for padding
+  // Padding is possible only for non-tiled processing
+
+  // If we are memory-based then read the image in and create an
+  // ArrayLattice otherwise just use the PagedImage
+  AlwaysAssert (!isTiled, AipsError);
+
+  //  cout<<"LofarFTMachineOld::initializeToVis === is_NOT_Tiled!"<<endl;
+  //======================CHANGED
+  //nx=640;
+  //ny=640;
+  //======================END CHANGED
+  //cout << "npol="<<npol<<endl;
+  IPosition gridShape(4, nx, ny, npol, nchan);
+  // Size and initialize the grid buffer per thread.
+  // Note the other itsGriddedData buffers are assigned later.
+  itsGriddedData[0].resize (gridShape);
+  itsGriddedData[0] = Complex();
+  for (int i=0; i<itsNThread; ++i) {
+    itsSumPB[i].resize (padded_shape[0], padded_shape[1]);
+    itsSumPB[i] = Complex();
+    itsSumCFWeight[i] = 0.;
+    itsSumWeight[i].resize(npol, nchan);
+    itsSumWeight[i] = 0.;
+  }
+
+  //griddedData can be a reference of image data...if not using model col
+  //hence using an undocumented feature of resize that if
+  //the size is the same as old data it is not changed.
+  //if(!usePut2_p) griddedData.set(0);
+
+  IPosition stride(4, 1);
+  IPosition blc(4, (nx-image->shape()(0)+(nx%2==0))/2, (ny-image->shape()(1)+(ny%2==0))/2, 0, 0);
+  IPosition trc(blc+image->shape()-stride);
+  if (itsVerbose > 0) {
+    cout<<"LofarFTMachineOld::initializeToVis === blc,trc,nx,ny,image->shape()"
+        <<blc<<" "<<trc<<" "<<nx<<" "<<ny<<" "<<image->shape()<<endl;
+  }
+  IPosition start(4, 0);
+  itsGriddedData[0](blc, trc) = image->getSlice(start, image->shape());
+  //if(arrayLattice) delete arrayLattice; arrayLattice=0;
+  //======================CHANGED
+  arrayLattice = new ArrayLattice<Complex>(itsGriddedData[0]);
+  // Array<Complex> result(IPosition(4, nx, ny, npol, nchan),0.);
+  // griddedData=result;
+  // arrayLattice = new ArrayLattice<Complex>(griddedData);
+  //======================END CHANGED
+  lattice=arrayLattice;
+
+  //AlwaysAssert(lattice, AipsError);
+
+  logIO() << LogIO::DEBUGGING
+	  << "Starting grid correction and FFT of image" << LogIO::POST;
+
+  //==========================
+  // Cyr: I have commeneted that part which does the spheroidal correction of the clean components in the image plane.
+  // We do this based on our estimate of the spheroidal function, stored in an image
+  // Do the Grid-correction.
+    // {
+    //   Vector<Complex> correction(nx);
+    //   correction=Complex(1.0, 0.0);
+    //   // Do the Grid-correction
+    //   IPosition cursorShape(4, nx, 1, 1, 1);
+    //   IPosition axisPath(4, 0, 1, 2, 3);
+    //   LatticeStepper lsx(lattice->shape(), cursorShape, axisPath);
+    //   LatticeIterator<Complex> lix(*lattice, lsx);
+    //   for(lix.reset();!lix.atEnd();lix++) {
+    //     gridder->correctX1D(correction, lix.position()(1));
+    // 	lix.rwVectorCursor()/=correction;
+    //   }
+    // }
+
+    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+    // Normalising clean components by the beam 
+    
+    // const Matrix<Float>& datai = getSpheroidCut();
+
+    const Matrix<Float>& data = getAveragePB();
+    //    cout<<"tmp.shape() "<<data.shape()<<"  "<<lattice->shape()<<endl;
+    IPosition pos(4,lattice->shape()[0],lattice->shape()[1],1,1);
+    IPosition pos2(2,lattice->shape()[0],lattice->shape()[1]);
+    pos[2]=0.;
+    pos[3]=0.;
+    pos2[2]=0.;
+    pos2[3]=0.;
+    Int offset_pad(floor(data.shape()[0]-lattice->shape()[0])/2.);
+    
+    //    cout<<"LofarFTMachineOld::initializeToVis lattice->shape() == "<<lattice->shape()<<endl;
+
+    Complex ff;
+    double I=100.;
+    double Q=40.;
+    double U=20.;
+    double V=10.;
+    for(Int k=0;k<lattice->shape()[2];++k){
+      ff=0.;
+      if(k==0){ff=I+Q;}
+      if(k==1){ff=Complex(U,0.)+Complex(0.,V);}
+      if(k==2){ff=Complex(U,0.)-Complex(0.,V);}
+      if(k==3){ff=I-Q;}
+      for(Int i=0;i<lattice->shape()[0];++i){
+	for(Int j=0;j<lattice->shape()[0];++j){
+	  pos[0]=i;
+	  pos[1]=j;
+	  pos[2]=k;
+	  pos2[0]=i+offset_pad;
+	  pos2[1]=j+offset_pad;
+	  Complex pixel(lattice->getAt(pos));
+	  double fact(1.);
+
+	  // pixel=0.;
+	  // if((pos[0]==351.)&&(pos[1]==319.)){//319
+	  //   pixel=ff;//*139./143;//-100.;
+	  //   if(datai(pos2)>1e-6){fact/=datai(pos2);};//*datai(pos2);};
+	  //   //if(data(pos2)>1e-6){fact/=sqrt(data(pos2));};//*datai(pos2);};
+	  //   pixel*=Complex(fact);
+	  // }
+	  
+	  fact/=sqrt(data(pos2));
+	  pixel*=Complex(fact);
+	  
+	  lattice->putAt(pixel,pos);
+	}
+      }
+    }
+
+
+    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+
+    // Now do the FFT2D in place
+    LatticeFFT::cfft2d(*lattice);
+
+    logIO() << LogIO::DEBUGGING
+	    << "Finished grid correction and FFT of image" << LogIO::POST;
+
+    // for(uInt k=0;k<lattice->shape()[2];++k){
+    //   for(uInt i=0;i<lattice->shape()[0];++i){
+    // 	for(uInt j=0;j<lattice->shape()[0];++j){
+    // 	  pos[0]=i;
+    // 	  pos[1]=j;
+    // 	  pos[2]=k;
+    // 	  Complex pixel(lattice->getAt(pos));
+    // 	  //cout<<"i,j,pixel value: "<<i<<" "<<j<<" "<<pixel<<endl;
+	  
+    // 	};
+    //   };
+    // };
+}
+
+
+
+
+void LofarFTMachineOld::finalizeToVis()
+{
+  if (itsVerbose > 0) {
+    cout<<"---------------------------> finalizeToVis"<<endl;
+  }
+}
+
+
+// Initialize the FFT to the Sky. Here we have to setup and initialize the
+// grid.
+void LofarFTMachineOld::initializeToSky(ImageInterface<Complex>& iimage,
+			     Matrix<Float>& weight, const VisBuffer& vb)
+{
+  // image always points to the image
+  image=&iimage;
+  if (itsVerbose > 0) {
+    cout<<"---------------------------> initializeToSky"<<endl;
+  }
+  init();
+
+  // Initialize the maps for polarization and channel. These maps
+  // translate visibility indices into image indices
+  initMaps(vb);
+
+  visResamplers_p.init(useDoubleGrid_p);
+  visResamplers_p.setMaps(chanMap, polMap);
+  visResamplers_p.setCFMaps(CFMap_p, ConjCFMap_p);
+
+  // Initialize for in memory or to disk gridding. lattice will
+  // point to the appropriate Lattice, either the ArrayLattice for
+  // in memory gridding or to the image for to disk gridding.
+  AlwaysAssert (!isTiled, AipsError);
+  IPosition gridShape(4, nx, ny, npol, nchan);
+  // Size and initialize the grid buffer per thread.
+  for (int i=0; i<itsNThread; ++i) {
+    itsGriddedData[i].resize (gridShape);
+    itsGriddedData[i] = Complex();
+    itsSumPB[i].resize (padded_shape[0], padded_shape[1]);
+    itsSumPB[i] = Complex();
+    itsSumCFWeight[i] = 0.;
+    itsSumWeight[i].resize (npol, nchan);
+    itsSumWeight[i] = 0.;
+  }
+  weight.resize(itsSumWeight[0].shape());
+  weight=0.0;
+
+  //iimage.get(griddedData, False);
+  //if(arrayLattice) delete arrayLattice; arrayLattice=0;
+  arrayLattice = new ArrayLattice<Complex>(itsGriddedData[0]);
+  lattice=arrayLattice;
+  // if(useDoubleGrid_p) visResampler_p->initializePutBuffers(griddedData2, sumWeight);
+  // else                visResampler_p->initializePutBuffers(griddedData, sumWeight);
+//// Are the following calls needed for LOFAR?
+///  if(useDoubleGrid_p) visResamplers_p.initializeToSky(griddedData2, sumWeight);
+///  else                visResamplers_p.initializeToSky(griddedData, sumWeight);
+  //AlwaysAssert(lattice, AipsError);
+}
+
+
+
+void LofarFTMachineOld::finalizeToSky()
+{
+  //AlwaysAssert(lattice, AipsError);
+  // Now we flush the cache and report statistics
+  // For memory based, we don't write anything out yet.
+  if (itsVerbose > 0) {
+    cout<<"---------------------------> finalizeToSky"<<endl;
+  }
+  // DEBUG: Store the grid per thread
+  uInt nx(itsGriddedData[0].shape()[0]);
+  IPosition shapecube(3,nx,nx,4);
+  for (int ii=0; ii<itsNThread; ++ii) {
+    Cube<Complex> tempimage(shapecube,0.);
+    for(Int k=0;k<4;++k){
+      for(uInt i=0;i<nx;++i){
+  	for(uInt j=0;j<nx;++j){
+	  IPosition pos(4,i,j,k,0);
+	  Complex pixel(itsGriddedData[ii](pos));
+	  tempimage(i,j,k)=pixel;
+  	}
+      }
+    }
+    store(tempimage,"Grid"+String::toString(ii)+".img");
+  }
+
+  // Add all buffers into the first one.
+  for (int i=1; i<itsNThread; ++i) {
+    itsGriddedData[0] += itsGriddedData[i];
+    itsSumWeight[0]   += itsSumWeight[i];
+    itsSumCFWeight[0] += itsSumCFWeight[i];
+    itsSumPB[0]       += itsSumPB[i];
+  }
+
+  Cube<Complex> tempimage(shapecube,0.);
+  for(Int k=0;k<4;++k){
+    for(uInt i=0;i<nx;++i){
+      for(uInt j=0;j<nx;++j){
+	IPosition pos(4,i,j,k,0);
+	Complex pixel(itsGriddedData[0](pos));
+	tempimage(i,j,k)=pixel;
+      }
+    }
+  }
+  store(tempimage,"Grid00.img");
+
+  // if(useDoubleGrid_p) visResamplers_p[0].GatherGrids(griddedData2, sumWeight);
+  // else                visResamplers_p[0].GatherGrids(griddedData, sumWeight);
+//// Are the following calls needed for LOFAR?
+///  if(useDoubleGrid_p) visResamplers_p.finalizeToSky(griddedData2, sumWeight);
+///  else                visResamplers_p.finalizeToSky(griddedData, sumWeight);
+}
+
+
+
+Array<Complex>* LofarFTMachineOld::getDataPointer(const IPosition& centerLoc2D,
+				       Bool readonly) {
+  Array<Complex>* result;
+  // Is tiled: get tiles and set up offsets
+  centerLoc(0)=centerLoc2D(0);
+  centerLoc(1)=centerLoc2D(1);
+  result=&imageCache->tile(offsetLoc,centerLoc, readonly);
+  gridder->setOffset(IPosition(2, offsetLoc(0), offsetLoc(1)));
+  return result;
+}
+
+void LofarFTMachineOld::put(const VisBuffer& vb, Int row, Bool dopsf,
+                         FTMachine::Type type)
+{
+  if (itsVerbose > 0) {
+    logIO() << LogOrigin("LofarFTMachineOld", "put") << LogIO::NORMAL
+            << "I am gridding " << vb.nRow() << " row(s)."  << LogIO::POST;
+    logIO() << LogIO::NORMAL << "Padding is " << padding_p  << LogIO::POST;
+  }
+
+
+  gridOk(gridder->cSupport()(0));
+
+  //Check if ms has changed then cache new spw and chan selection
+  if(vb.newMS())   matchAllSpwChans(vb);
+
+  //Here we redo the match or use previous match
+
+  //Channel matching for the actual spectral window of buffer
+  if (doConversion_p[vb.spectralWindow()]) {
+    matchChannel(vb.spectralWindow(), vb);
+  } else {
+    chanMap.resize();
+    chanMap=multiChanMap_p[vb.spectralWindow()];
+  }
+
+  //No point in reading data if it's not matching in frequency
+  if(max(chanMap)==-1) return;
+
+  const Matrix<Float> *imagingweight;
+  imagingweight=&(vb.imagingWeight());
+
+  // dopsf=true;
+
+  if(dopsf) {type=FTMachine::PSF;}
+
+  Cube<Complex> data;
+  //Fortran gridder need the flag as ints
+  Cube<Int> flags;
+  Matrix<Float> elWeight;
+  interpolateFrequencyTogrid(vb, *imagingweight,data, flags, elWeight, type);
+
+
+  Int startRow, endRow, nRow;
+  if (row==-1) { nRow=vb.nRow(); startRow=0; endRow=nRow-1; }
+  else         { nRow=1; startRow=row; endRow=row; }
+
+  // Get the uvws in a form that Fortran can use and do that
+  // necessary phase rotation. On a Pentium Pro 200 MHz
+  // when null, this step takes about 50us per uvw point. This
+  // is just barely noticeable for Stokes I continuum and
+  // irrelevant for other cases.
+  Matrix<Double> uvw(3, vb.uvw().nelements());  uvw=0.0;
+  Vector<Double> dphase(vb.uvw().nelements());  dphase=0.0;
+  //NEGATING to correct for an image inversion problem
+  for (Int i=startRow;i<=endRow;i++) {
+    for (Int idim=0;idim<2;idim++) uvw(idim,i)=-vb.uvw()(i)(idim);
+    uvw(2,i)=vb.uvw()(i)(2);
+  }
+
+  rotateUVW(uvw, dphase, vb);
+  refocus(uvw, vb.antenna1(), vb.antenna2(), dphase, vb);
+
+  // Set up VBStore object to point to the relevant info of the VB.
+  LofarVBStore vbs;
+  makeCFPolMap(vb,cfStokes_p,CFMap_p);
+  makeConjPolMap(vb,CFMap_p,ConjCFMap_p);
+
+  // Determine the baselines in the VisBuffer.
+  const Vector<Int>& ant1 = vb.antenna1();
+  const Vector<Int>& ant2 = vb.antenna2();
+  int nrant = 1 + max(max(ant1), max(ant2));
+  // Sort on baseline (use a baseline nr which is faster to sort).
+  Vector<Int> blnr(nrant*ant1);
+  blnr += ant2;  // This is faster than nrant*ant1+ant2 in a single line
+  Vector<uInt> blIndex;
+  GenSortIndirect<Int>::sort (blIndex, blnr);
+  // Now determine nr of unique baselines and their start index.
+  vector<int> blStart, blEnd;
+  blStart.reserve (nrant*(nrant+1)/2);
+  blEnd.reserve   (nrant*(nrant+1)/2);
+  Int  lastbl     = -1;
+  Int  lastIndex  = 0;
+  bool usebl      = false;
+  bool allFlagged = true;
+  const Vector<Bool>& flagRow = vb.flagRow();
+  for (uint i=0; i<blnr.size(); ++i) {
+    Int inx = blIndex[i];
+    Int bl = blnr[inx];
+    if (bl != lastbl) {
+      // New baseline. Write the previous end index if applicable.
+      if (usebl  &&  !allFlagged) {
+	double Wmean(0.5*(vb.uvw()[blIndex[lastIndex]](2) + vb.uvw()[blIndex[i-1]](2)));
+	if (abs(Wmean) <= itsWMax) {
+	  if (itsVerbose > 1) {
+	    cout<<"using w="<<Wmean<<endl;
+	  }
+	  blStart.push_back (lastIndex);
+	  blEnd.push_back (i-1);
+	}
+      }
+      // Skip auto-correlations and high W-values.
+      // All w values are close, so if first w is too high, skip baseline.
+      usebl = false;
+
+      if (ant1[inx] != ant2[inx]) {
+	usebl = true;
+      }
+      lastbl=bl;
+      lastIndex=i;
+    }
+    // Test if the row is flagged.
+    if (! flagRow[inx]) {
+      allFlagged = false;
+    }
+  }
+  // Write the last end index if applicable.
+  if (usebl  &&  !allFlagged) {
+    double Wmean(0.5*(vb.uvw()[blIndex[lastIndex]](2) + vb.uvw()[blIndex[blnr.size()-1]](2)));
+    if (abs(Wmean) <= itsWMax) {
+      if (itsVerbose > 1) {
+	cout<<"...using w="<<Wmean<<endl;
+      }
+      blStart.push_back (lastIndex);
+      blEnd.push_back (blnr.size()-1);
+    }
+  }
+  // Determine the time center of this data chunk.
+  const Vector<Double>& times = vb.timeCentroid();
+  double time = 0.5 * (times[times.size()-1] + times[0]);
+
+  vbs.nRow_p = vb.nRow();
+  vbs.uvw_p.reference(uvw);
+  vbs.imagingWeight_p.reference(elWeight);
+  vbs.visCube_p.reference(data);
+  //  vbs.visCube_p.reference(vb.modelVisCube());
+  vbs.freq_p.reference(interpVisFreq_p);
+  vbs.rowFlag_p.reference(vb.flagRow());
+
+  // Really nice way of converting a Cube<Int> to Cube<Bool>.
+  // However the VBS objects should ultimately be references
+  // directly to bool cubes.
+  //**************
+  vbs.flagCube_p.resize(flags.shape());    vbs.flagCube_p = False; vbs.flagCube_p(flags!=0) = True;
+  //  vbs.flagCube_p.reference(vb.flagCube());
+  //**************
+
+   // Determine the terms of the Mueller matrix that should be calculated
+  visResamplers_p.setParams(uvScale,uvOffset,dphase);
+  visResamplers_p.setMaps(chanMap, polMap);
+
+  // First compute the A-terms for all stations (if needed).
+  itsConvFunc->computeAterm (time);
+
+  uInt Nchannels = vb.nChannel();
+
+  itsTotalTimer.start();
+#pragma omp parallel 
+  {
+    // Thread-private variables.
+    PrecTimer gridTimer;
+    PrecTimer cfTimer;
+    // The for loop can be parallellized. This must be done dynamically,
+    // because the execution times of iterations can vary greatly.
+#pragma omp for schedule(dynamic)
+    for (int i=0; i<int(blStart.size()); ++i) {
+      Int ist  = blIndex[blStart[i]];
+      Int iend = blIndex[blEnd[i]];
+
+      // compute average weight for baseline for CF averaging
+      double average_weight(0.);
+      uInt Nvis(0);
+      for(Int j=ist; j<iend; ++j){
+        uInt row=blIndex[j];
+        if(!vbs.rowFlag()[row]){
+          Nvis+=1;
+          for(uint k=0; k<Nchannels; ++k) {
+            average_weight=average_weight+vbs.imagingWeight()(k,row);
+          }
+        }
+      }
+      average_weight=average_weight/Nvis;
+      ///        itsSumWeight += average_weight * average_weight;
+      if (itsVerbose > 1) {
+        cout<<"average weights= "<<average_weight<<", Nvis="<<Nvis<<endl;
+      }
+
+      int threadNum = OpenMP::threadNum();
+
+      // Get the convolution function.
+      if (itsVerbose > 1) {
+        cout.precision(20);
+        cout<<"A1="<<ant1[ist]<<", A2="<<ant2[ist]<<", time="<<fixed<<time<<endl;
+      }
+      LofarCFStore cfStore;
+      //#pragma omp critical(LofarFTMachineOld_makeConvolutionFunction)
+      //{
+      cfTimer.start();
+      cfStore =
+        itsConvFunc->makeConvolutionFunction (ant1[ist], ant2[ist], time,
+                                              0.5*(vbs.uvw()(2,ist) + vbs.uvw()(2,iend)),
+                                              itsGridMuellerMask, false,
+                                              average_weight,
+                                              itsSumPB[threadNum],
+                                              itsSumCFWeight[threadNum]);
+      cfTimer.stop();
+      //};
+
+
+      //cout<<"DONE LOADING CF..."<<endl;
+      //Double or single precision gridding.
+      //	cout<<"============================================"<<endl;
+      //	cout<<"Antenna "<<ant1[ist]<<" and "<<ant2[ist]<<endl;
+      //#pragma omp critical(LofarFTMachineOld_makeConvolutionFunction)
+      //{
+      if (useDoubleGrid_p) {
+        visResamplers_p.lofarDataToGrid(itsGriddedData2[threadNum], vbs, blIndex,
+                                        blStart[i], blEnd[i],
+                                        itsSumWeight[threadNum], dopsf, cfStore);
+      } else {
+        if (itsVerbose > 1) {
+          cout<<"  gridding"<<" thread="<<threadNum<<'('<<itsNThread<<"), A1="<<ant1[ist]<<", A2="<<ant2[ist]<<", time=" <<time<<endl;
+        }
+        gridTimer.start();
+        visResamplers_p.lofarDataToGrid
+          (itsGriddedData[threadNum], vbs, blIndex, blStart[i],
+           blEnd[i], itsSumWeight[threadNum], dopsf, cfStore);
+        gridTimer.stop();
+      }
+      // } // end omp critical
+    } // end omp for
+    double cftime = cfTimer.getReal();
+#pragma omp atomic
+    itsCFTime += cftime;
+    double gtime = gridTimer.getReal();
+#pragma omp atomic
+    itsGriddingTime += gtime;
+  } // end omp parallel
+  itsTotalTimer.stop();
+}
+
+
+// Degrid
+void LofarFTMachineOld::get(VisBuffer& vb, Int row)
+{
+  if (itsVerbose > 0) {
+    cout<<"///////////////////// GET!!!!!!!!!!!!!!!!!!"<<endl;
+  }
+  gridOk(gridder->cSupport()(0));
+  // If row is -1 then we pass through all rows
+  Int startRow, endRow, nRow;
+  if (row < 0) { nRow=vb.nRow(); startRow=0; endRow=nRow-1;}
+  else         { nRow=1; startRow=row; endRow=row; }
+
+  // Get the uvws in a form that Fortran can use
+  Matrix<Double> uvw(3, vb.uvw().nelements());  uvw=0.0;
+  Vector<Double> dphase(vb.uvw().nelements());  dphase=0.0;
+  //NEGATING to correct for an image inversion problem
+  for (Int i=startRow;i<=endRow;i++) {
+    for (Int idim=0;idim<2;idim++) uvw(idim,i)=-vb.uvw()(i)(idim);
+    uvw(2,i)=vb.uvw()(i)(2);
+  }
+  rotateUVW(uvw, dphase, vb);
+  refocus(uvw, vb.antenna1(), vb.antenna2(), dphase, vb);
+
+  //Check if ms has changed then cache new spw and chan selection
+  if(vb.newMS())  matchAllSpwChans(vb);
+
+
+  //Channel matching for the actual spectral window of buffer
+  if(doConversion_p[vb.spectralWindow()])
+    matchChannel(vb.spectralWindow(), vb);
+  else
+    {
+      chanMap.resize();
+      chanMap=multiChanMap_p[vb.spectralWindow()];
+    }
+
+  //No point in reading data if its not matching in frequency
+  if(max(chanMap)==-1)    return;
+
+  Cube<Complex> data;
+  Cube<Int> flags;
+  getInterpolateArrays(vb, data, flags);
+
+  // Apparently we don't support "tiled gridding" any more (good! :)).
+  if(isTiled)
+    throw(SynthesisFTMachineError("LofarFTMachineOld::get(): Internal error.  isTiled is True. "));
+
+  LofarVBStore vbs;
+  vbs.nRow_p = vb.nRow();
+  vbs.beginRow_p = 0;
+  vbs.endRow_p = vbs.nRow_p;
+
+  vbs.uvw_p.reference(uvw);
+  //    vbs.imagingWeight.reference(elWeight);
+  vbs.visCube_p.reference(data);
+  
+  vbs.freq_p.reference(interpVisFreq_p);
+  vbs.rowFlag_p.resize(0); vbs.rowFlag_p = vb.flagRow();
+  if(!usezero_p)
+    for (Int rownr=startRow; rownr<=endRow; rownr++)
+      if(vb.antenna1()(rownr)==vb.antenna2()(rownr)) vbs.rowFlag_p(rownr)=True;
+
+  // Really nice way of converting a Cube<Int> to Cube<Bool>.
+  // However these should ultimately be references directly to bool
+  // cubes.
+  vbs.flagCube_p.resize(flags.shape());    vbs.flagCube_p = False; vbs.flagCube_p(flags!=0) = True;
+  //    vbs.rowFlag.resize(rowFlags.shape());  vbs.rowFlag  = False; vbs.rowFlag(rowFlags) = True;
+
+  // Determine the terms of the Mueller matrix that should be calculated
+  visResamplers_p.setParams(uvScale,uvOffset,dphase);
+  visResamplers_p.setMaps(chanMap, polMap);
+
+
+  // Determine the baselines in the VisBuffer.
+  const Vector<Int>& ant1 = vb.antenna1();
+  const Vector<Int>& ant2 = vb.antenna2();
+  int nrant = 1 + max(max(ant1), max(ant2));
+  // Sort on baseline (use a baseline nr which is faster to sort).
+  Vector<Int> blnr(nrant*ant1);
+  blnr += ant2;  // This is faster than nrant*ant1+ant2 in a single line
+  Vector<uInt> blIndex;
+  GenSortIndirect<Int>::sort (blIndex, blnr);
+  // Now determine nr of unique baselines and their start index.
+  vector<int> blStart, blEnd;
+  blStart.reserve (nrant*(nrant+1)/2);
+  blEnd.reserve   (nrant*(nrant+1)/2);
+  Int  lastbl     = -1;
+  Int  lastIndex  = 0;
+  bool usebl      = false;
+  bool allFlagged = true;
+  const Vector<Bool>& flagRow = vb.flagRow();
+  for (uint i=0; i<blnr.size(); ++i) {
+    Int inx = blIndex[i];
+    Int bl = blnr[inx];
+    if (bl != lastbl) {
+      // New baseline. Write the previous end index if applicable.
+      if (usebl  &&  !allFlagged) {
+        double Wmean(0.5*(vb.uvw()[blIndex[lastIndex]](2) + vb.uvw()[blIndex[i-1]](2)));
+        if (abs(Wmean) <= itsWMax) {
+	  if (itsVerbose > 1) {
+	    cout<<"using w="<<Wmean<<endl;
+	  }
+	  blStart.push_back (lastIndex);
+	  blEnd.push_back (i-1);
+        }
+      }
+      // Skip auto-correlations and high W-values.
+      // All w values are close, so if first w is too high, skip baseline.
+      usebl = false;
+
+      if (ant1[inx] != ant2[inx]) {
+        usebl = true;
+      }
+      lastbl=bl;
+      lastIndex=i;
+    }
+    // Test if the row is flagged.
+    if (! flagRow[inx]) {
+      allFlagged = false;
+    }
+  }
+  // Write the last end index if applicable.
+  if (usebl  &&  !allFlagged) {
+    double Wmean(0.5*(vb.uvw()[blIndex[lastIndex]](2) + vb.uvw()[blIndex[blnr.size()-1]](2)));
+    if (abs(Wmean) <= itsWMax) {
+      if (itsVerbose > 1) {
+	cout<<"...using w="<<Wmean<<endl;
+      }
+      blStart.push_back (lastIndex);
+      blEnd.push_back (blnr.size()-1);
+    }
+  }
+
+  // Determine the time center of this data chunk.
+  const Vector<Double>& times = vb.timeCentroid();
+  double time = 0.5 * (times[times.size()-1] + times[0]);
+  //ROVisIter& via(vb.iter());
+
+  // First compute the A-terms for all stations (if needed).
+  itsConvFunc->computeAterm (time);
+
+  itsTotalTimer.start();
+#pragma omp parallel
+  {
+    // Thread-private variables.
+    PrecTimer degridTimer;
+    PrecTimer cfTimer;
+    // The for loop can be parallellized. This must be done dynamically,
+    // because the execution times of iterations can vary greatly.
+    #pragma omp for schedule(dynamic)
+    for (int i=0; i<int(blStart.size()); ++i) {
+      // #pragma omp critical(LofarFTMachineOld_lofarGridToData)
+      // {
+      Int ist  = blIndex[blStart[i]];
+      Int iend = blIndex[blEnd[i]];
+      int threadNum = OpenMP::threadNum();
+      // Get the convolution function for degridding.
+      if (itsVerbose > 1) {
+	cout<<"ANTENNA "<<ant1[ist]<<" "<<ant2[ist]<<endl;
+      }
+      cfTimer.start();
+      LofarCFStore cfStore =
+        itsConvFunc->makeConvolutionFunction (ant1[ist], ant2[ist], time,
+                                              0.5*(vbs.uvw()(2,ist) + vbs.uvw()(2,iend)),
+                                              itsDegridMuellerMask,
+                                              true,
+                                              0.0,
+                                              itsSumPB[threadNum],
+                                              itsSumCFWeight[threadNum]);
+      cfTimer.stop();
+
+      //Double or single precision gridding.
+      //      cout<<"GRID "<<ant1[ist]<<" "<<ant2[ist]<<endl;
+      degridTimer.start();
+      visResamplers_p.lofarGridToData(vbs, itsGriddedData[0],
+                                      blIndex, blStart[i], blEnd[i], cfStore);
+      degridTimer.stop();
+    } // end omp for
+    double cftime = cfTimer.getReal();
+#pragma omp atomic
+    itsCFTime += cftime;
+    double gtime = degridTimer.getReal();
+#pragma omp atomic
+    itsGriddingTime += gtime;
+  } // end omp parallel
+  itsTotalTimer.stop();
+  interpolateFrequencyFromgrid(vb, data, FTMachine::MODEL);
+}
+
+
+
+// Finalize the FFT to the Sky. Here we actually do the FFT and
+// return the resulting image
+ImageInterface<Complex>& LofarFTMachineOld::getImage(Matrix<Float>& weights, Bool normalize)
+{
+  //AlwaysAssert(lattice, AipsError);
+  AlwaysAssert(gridder, AipsError);
+  AlwaysAssert(image, AipsError);
+  logIO() << LogOrigin("LofarFTMachineOld", "getImage") << LogIO::NORMAL;
+
+  if (itsVerbose > 0) {
+    cout<<"GETIMAGE"<<endl;
+  }
+  itsAvgPB.reference (itsConvFunc->Compute_avg_pb(itsSumPB[0], itsSumCFWeight[0]));
+
+  //cout<<"weights.shape() "<<weights.shape()<<"  "<<sumWeight<<endl;
+
+  weights.resize(itsSumWeight[0].shape());
+
+  convertArray(weights, itsSumWeight[0]);
+  // If the weights are all zero then we cannot normalize
+  // otherwise we don't care.
+  if(normalize&&max(weights)==0.0) {
+    logIO() << LogIO::SEVERE << "No useful data in LofarFTMachineOld: weights all zero"
+	    << LogIO::POST;
+  }
+  else {
+
+    const IPosition latticeShape = lattice->shape();
+
+    logIO() << LogIO::DEBUGGING
+	    << "Starting FFT and scaling of image" << LogIO::POST;
+
+
+
+    // if(useDoubleGrid_p){
+    //   convertArray(griddedData, griddedData2);
+    //   //Don't need the double-prec grid anymore...
+    //   griddedData2.resize();
+    // }
+
+    // x and y transforms
+    //    LatticeFFT::cfft2d(*lattice,False);
+    //
+    // Retain the double precision grid for FFT as well.  Convert it
+    // to single precision just after (since images are still single
+    // precision).
+    //
+    if(useDoubleGrid_p) {
+      ArrayLattice<DComplex> darrayLattice(itsGriddedData2[0]);
+      LatticeFFT::cfft2d(darrayLattice,False);
+      convertArray(itsGriddedData[0], itsGriddedData2[0]);
+      //Don't need the double-prec grid anymore...
+      ///griddedData2.resize();
+    } else {
+      LatticeFFT::cfft2d(*lattice, False);
+    }
+
+    if (itsVerbose > 0) {
+      cout<<"POLMAP:::::::  "<<polMap<<endl;
+      cout<<"POLMAP:::::::  "<<CFMap_p<<endl;
+    }
+    //cout<<"CFPOLMAP:::::::  "<<cfPolMap<<endl;
+    //Int i,j,N = cfPolMap.nelements();
+    //for(i=0;i<N;i++){
+    //  if (cfPolMap[i] > -1){cout<<"cfPolMap[i]<<visStokes[i]"<<cfPolMap[i]<<" "<<visStokes[i]<<endl;};
+    //};
+
+
+    // Cyr: This does a normalisation by the number of pixel and spheroidal
+    // function in the dirty image.
+    // I have commented out the spheroidal normalisation (correctX1D part)
+    {
+      Int inx = lattice->shape()(0);
+      Int iny = lattice->shape()(1);
+      Vector<Complex> correction(inx);
+      correction=Complex(1.0, 0.0);
+      // Do the Grid-correction
+      IPosition cursorShape(4, inx, 1, 1, 1);
+      IPosition axisPath(4, 0, 1, 2, 3);
+      LatticeStepper lsx(lattice->shape(), cursorShape, axisPath);
+      LatticeIterator<Complex> lix(*lattice, lsx);
+      for(lix.reset();!lix.atEnd();lix++) {
+        Int pol=lix.position()(2);
+	//cout<<"pol "<<pol<<endl;
+        Int chan=lix.position()(3);
+        if(weights(pol, chan)!=0.0) {
+          //gridder->correctX1D(correction, lix.position()(1));
+	  //cout<<"correction "<<correction<<endl;
+          //lix.rwVectorCursor()/=correction;
+          if(normalize) {
+            Complex rnorm(Float(inx)*Float(iny)/weights(pol,chan));
+            lix.rwCursor()*=rnorm;
+	    //cout<<"rnorm "<<rnorm<<endl;
+          }
+          else {
+            Complex rnorm(Float(inx)*Float(iny));
+            lix.rwCursor()*=rnorm;
+	    //cout<<"rnorm "<<rnorm<<endl;
+          }
+        }
+        else {
+          lix.woCursor()=0.0;
+        }
+      }
+    }
+
+    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
+    // Normalising dirty image by the spheroidal function
+
+    // String namei("sphe.img");
+    // ostringstream name(namei);
+    // PagedImage<Float> tmp(name.str().c_str());
+    // Slicer slice(IPosition(4,0,0,0,0), tmp.shape(), IPosition(4,1,1,1,1));
+    // Array<Float> data;
+    // tmp.doGetSlice(data, slice);
+    // IPosition posi(4,lattice->shape()[0],lattice->shape()[1],1,1);
+    // IPosition posi2(4,lattice->shape()[0],lattice->shape()[1],1,1);
+    // posi[2]=0.;
+    // posi[3]=0.;
+    // posi2[2]=0.;
+    // posi2[3]=0.;    
+    // Int offset_pad(floor(data.shape()[0]-lattice->shape()[0])/2.);
+
+    
+
+
+    // for(uInt k=0;k<lattice->shape()[2];++k){
+    //   for(uInt i=0;i<lattice->shape()[0];++i){
+    // 	for(uInt j=0;j<lattice->shape()[0];++j){
+    // 	  posi[0]=i;
+    // 	  posi[1]=j;
+    // 	  posi[2]=k;
+    // 	  posi2[0]=i+offset_pad;
+    // 	  posi2[1]=j+offset_pad;
+    // 	  Complex pixel(lattice->getAt(posi));
+    // 	  //pixel/=data(posi2);//*data(posi2);
+    // 	  lattice->putAt(pixel,posi);
+    // 	};
+    //   };
+    // };
+    //====================================================================================================================
+    //====================================================================================================================
+    //====================================================================================================================
+    // Cyr: Normalisation by the beam!!!!!
+    //cout<<"lattice shape: "<<lattice->shape()<<endl;
+    IPosition pos(4,lattice->shape()[0],lattice->shape()[1],1,1);
+    uInt shapeout(floor(lattice->shape()[0]/padding_p));
+    uInt istart(floor((lattice->shape()[0]-shapeout)/2.));
+    Cube<Complex> tempimage(IPosition(3,shapeout,shapeout,lattice->shape()[2]));
+
+    pos[3]=0.;
+    for(Int k=0;k<lattice->shape()[2];++k){
+      for(uInt i=0;i<shapeout;++i){
+    	for(uInt j=0;j<shapeout;++j){
+    	  pos[0]=i+istart;
+    	  pos[1]=j+istart;
+    	  pos[2]=k;
+    	  Complex pixel(lattice->getAt(pos));
+    	  //cout<<"pixel value: "<<pixel<<", Primary beam: "<<avg_PB(i,j)<<endl;
+    	  pixel/=sqrt(itsAvgPB(i+istart,j+istart));//*sqrt(avg_PB(i+istart,j+istart));
+    	  //pixel*=(lattice->shape()[0]*lattice->shape()[0]);
+    	  lattice->putAt(pixel,pos);
+    	  //tempimage(i,j,k)=pixel/weights(0,0);
+    	}
+      }
+    }
+    // uInt count_cycle(0);
+    // Bool written(false);
+
+    // while(!written){
+    //   cout<<"count_cycle ======================= "<<count_cycle<<" "<<normalize<<endl;
+    //   File myFile("Cube_dirty.img"+String::toString(count_cycle));
+    //   if(!myFile.exists()){
+    // 	written=true;
+    // 	store(tempimage,"Cube_dirty.img"+String::toString(count_cycle));
+    //   }
+    //   else{
+    // 	count_cycle++;
+    //   };
+    // };
+
+    //====================================================================================================================
+    //====================================================================================================================
+    //====================================================================================================================
+
+
+
+    if(!isTiled) {
+      // Check the section from the image BEFORE converting to a lattice
+      IPosition blc(4, (nx-image->shape()(0)+(nx%2==0))/2, (ny-image->shape()(1)+(ny%2==0))/2, 0, 0);
+      IPosition stride(4, 1);
+      IPosition trc(blc+image->shape()-stride);
+      // Do the copy
+      IPosition start(4, 0);
+      image->put(itsGriddedData[0](blc, trc));
+    }
+  }
+
+  //store(*image,"last.img");
+  return *image;
+}
+
+// Get weight image
+void LofarFTMachineOld::getWeightImage(ImageInterface<Float>& weightImage, Matrix<Float>& weights)
+{
+
+  logIO() << LogOrigin("LofarFTMachineOld", "getWeightImage") << LogIO::NORMAL;
+
+  weights.resize(itsSumWeight[0].shape());
+  convertArray(weights,itsSumWeight[0]);
+
+  const IPosition latticeShape = weightImage.shape();
+
+  Int nx=latticeShape(0);
+  Int ny=latticeShape(1);
+
+  IPosition loc(2, 0);
+  IPosition cursorShape(4, nx, ny, 1, 1);
+  IPosition axisPath(4, 0, 1, 2, 3);
+  LatticeStepper lsx(latticeShape, cursorShape, axisPath);
+  LatticeIterator<Float> lix(weightImage, lsx);
+  for(lix.reset();!lix.atEnd();lix++) {
+    Int pol=lix.position()(2);
+    Int chan=lix.position()(3);
+    lix.rwCursor()=weights(pol,chan);
+  }
+}
+
+Bool LofarFTMachineOld::toRecord(String& error, RecordInterface& outRec,
+			Bool withImage) {
+
+  // Save the current LofarFTMachineOld object to an output state record
+  Bool retval = True;
+
+  Double cacheVal=(Double)cachesize;
+  outRec.define("cache", cacheVal);
+  outRec.define("tile", tilesize);
+  outRec.define("gridfunction", convType);
+
+  Vector<Double> phaseValue(2);
+  String phaseUnit;
+  phaseValue=mTangent_p.getAngle().getValue();
+  phaseUnit= mTangent_p.getAngle().getUnit();
+  outRec.define("phasevalue", phaseValue);
+  outRec.define("phaseunit", phaseUnit);
+
+  Vector<Double> dirValue(3);
+  String dirUnit;
+  dirValue=mLocation_p.get("m").getValue();
+  dirUnit=mLocation_p.get("m").getUnit();
+  outRec.define("dirvalue", dirValue);
+  outRec.define("dirunit", dirUnit);
+
+  outRec.define("padding", padding_p);
+  outRec.define("maxdataval", maxAbsData);
+
+  Vector<Int> center_loc(4), offset_loc(4);
+  for (Int k=0; k<4 ; k++){
+    center_loc(k)=centerLoc(k);
+    offset_loc(k)=offsetLoc(k);
+  }
+  outRec.define("centerloc", center_loc);
+  outRec.define("offsetloc", offset_loc);
+  outRec.define("sumofweights", itsSumWeight[0]);
+  if(withImage && image){
+    ImageInterface<Complex>& tempimage(*image);
+    Record imageContainer;
+    retval = (retval || tempimage.toRecord(error, imageContainer));
+    outRec.defineRecord("image", imageContainer);
+  }
+  return retval;
+}
+
+Bool LofarFTMachineOld::fromRecord(String& error, const RecordInterface& inRec)
+{
+  Bool retval = True;
+  gridder=0; imageCache=0; lattice=0; arrayLattice=0;
+  Double cacheVal;
+  inRec.get("cache", cacheVal);
+  cachesize=(Long)cacheVal;
+  inRec.get("tile", tilesize);
+  inRec.get("gridfunction", convType);
+
+  Vector<Double> phaseValue(2);
+  inRec.get("phasevalue",phaseValue);
+  String phaseUnit;
+  inRec.get("phaseunit",phaseUnit);
+  Quantity val1(phaseValue(0), phaseUnit);
+  Quantity val2(phaseValue(1), phaseUnit);
+  MDirection phasecenter(val1, val2);
+
+  mTangent_p=phasecenter;
+  // This should be passed down too but the tangent plane is
+  // expected to be specified in all meaningful cases.
+  tangentSpecified_p=True;
+  Vector<Double> dirValue(3);
+  String dirUnit;
+  inRec.get("dirvalue", dirValue);
+  inRec.get("dirunit", dirUnit);
+  MVPosition dummyMVPos(dirValue(0), dirValue(1), dirValue(2));
+  MPosition mLocation(dummyMVPos, MPosition::ITRF);
+  mLocation_p=mLocation;
+
+  inRec.get("padding", padding_p);
+  inRec.get("maxdataval", maxAbsData);
+
+  Vector<Int> center_loc(4), offset_loc(4);
+  inRec.get("centerloc", center_loc);
+  inRec.get("offsetloc", offset_loc);
+  uInt ndim4 = 4;
+  centerLoc=IPosition(ndim4, center_loc(0), center_loc(1), center_loc(2),
+		      center_loc(3));
+  offsetLoc=IPosition(ndim4, offset_loc(0), offset_loc(1), offset_loc(2),
+		      offset_loc(3));
+  inRec.get("sumofweights", itsSumWeight[0]);
+  if(inRec.nfields() > 12 ){
+    Record imageAsRec=inRec.asRecord("image");
+    if(!image) {
+      image= new TempImage<Complex>();
+    }
+    retval = (retval || image->fromRecord(error, imageAsRec));
+
+    // Might be changing the shape of sumWeight
+    init();
+
+    if(isTiled) {
+      lattice=CountedPtr<Lattice<Complex> >(image, False);
+    }
+    else {
+      // Make the grid the correct shape and turn it into an array lattice
+      // Check the section from the image BEFORE converting to a lattice
+      IPosition gridShape(4, nx, ny, npol, nchan);
+      itsGriddedData[0].resize(gridShape);
+      itsGriddedData[0]=Complex(0.0);
+      IPosition blc(4, (nx-image->shape()(0)+(nx%2==0))/2, (ny-image->shape()(1)+(ny%2==0))/2, 0, 0);
+      IPosition start(4, 0);
+      IPosition stride(4, 1);
+      IPosition trc(blc+image->shape()-stride);
+      itsGriddedData[0](blc, trc)=image->getSlice(start, image->shape());
+
+      //if(arrayLattice) delete arrayLattice; arrayLattice=0;
+      arrayLattice = new ArrayLattice<Complex>(itsGriddedData[0]);
+      lattice=arrayLattice;
+    }
+
+    //AlwaysAssert(lattice, AipsError);
+    AlwaysAssert(gridder, AipsError);
+    AlwaysAssert(image, AipsError);
+  }
+  return retval;
+}
+
+void LofarFTMachineOld::ok() {
+  AlwaysAssert(image, AipsError);
+}
+
+// Make a plain straightforward honest-to-God image. This returns
+// a complex image, without conversion to Stokes. The representation
+// is that required for the visibilities.
+//----------------------------------------------------------------------
+void LofarFTMachineOld::makeImage(FTMachine::Type type,
+		       VisSet& vs,
+		       ImageInterface<Complex>& theImage,
+		       Matrix<Float>& weight) {
+
+
+  logIO() << LogOrigin("LofarFTMachineOld", "makeImage") << LogIO::NORMAL;
+
+  if(type==FTMachine::COVERAGE) {
+    logIO() << "Type COVERAGE not defined for Fourier transforms" << LogIO::EXCEPTION;
+  }
+
+
+  // Initialize the gradients
+  ROVisIter& vi(vs.iter());
+
+  // Loop over all visibilities and pixels
+  VisBuffer vb(vi);
+
+  // Initialize put (i.e. transform to Sky) for this model
+  vi.origin();
+
+  if(vb.polFrame()==MSIter::Linear) {
+    StokesImageUtil::changeCStokesRep(theImage, SkyModel::LINEAR);
+  }
+  else {
+    StokesImageUtil::changeCStokesRep(theImage, SkyModel::CIRCULAR);
+  }
+
+  initializeToSky(theImage,weight,vb);
+
+  // Loop over the visibilities, putting VisBuffers
+  for (vi.originChunks();vi.moreChunks();vi.nextChunk()) {
+    for (vi.origin(); vi.more(); vi++) {
+
+      switch(type) {
+      case FTMachine::RESIDUAL:
+        if (itsVerbose > 0) cout<<"FTMachine::RESIDUAL"<<endl;
+	vb.visCube()=vb.correctedVisCube();
+	vb.visCube()-=vb.modelVisCube();
+        put(vb, -1, False);
+        break;
+      case FTMachine::MODEL:
+	if (itsVerbose > 0) cout<<"FTMachine::MODEL"<<endl;
+	vb.visCube()=vb.modelVisCube();
+        put(vb, -1, False);
+        break;
+      case FTMachine::CORRECTED:
+	if (itsVerbose > 0) cout<<"FTMachine::CORRECTED"<<endl;
+	vb.visCube()=vb.correctedVisCube();
+        put(vb, -1, False);
+        break;
+      case FTMachine::PSF:
+	if (itsVerbose > 0) cout<<"FTMachine::PSF"<<endl;
+	vb.visCube()=Complex(1.0,0.0);
+        put(vb, -1, True);
+        break;
+      case FTMachine::OBSERVED:
+      default:
+	if (itsVerbose > 0) cout<<"FTMachine::OBSERVED"<<endl;
+        put(vb, -1, False);
+        break;
+      }
+    }
+  }
+  finalizeToSky();
+  // Normalize by dividing out weights, etc.
+  getImage(weight, True);
+}
+
+String LofarFTMachineOld::name(){
+
+  return machineName_p;
+
+
+}
+
+void LofarFTMachineOld::ComputeResiduals(VisBuffer&vb, Bool useCorrected)
+{
+  LofarVBStore vbs;
+  vbs.nRow_p = vb.nRow();
+  vbs.beginRow_p = 0;
+  vbs.endRow_p = vbs.nRow_p;
+  vbs.modelCube_p.reference(vb.modelVisCube());
+  if (useCorrected) vbs.correctedCube_p.reference(vb.correctedVisCube());
+  else vbs.visCube_p.reference(vb.visCube());
+  //  cout<<"BLA===="<<vb.visCube()<<"    "<<useCorrected<<endl;
+  
+  //for(uInt i=0;i<vbs.nRow_p;++i){cout<<"ROW "<<i<<" "<<vb.antenna1()(i)<<" "<<vb.antenna2()(i)<<endl;};
+
+  vbs.useCorrected_p = useCorrected;
+  visResamplers_p.lofarComputeResiduals(vbs);
+
+  //  vb.correctedVisCube()=0.;//vb.modelVisCube();
+}
+
+  void LofarFTMachineOld::makeSensitivityImage(const VisBuffer& vb,
+					 const ImageInterface<Complex>& imageTemplate,
+					 ImageInterface<Float>& sensitivityImage)
+  {
+    if (convFuncCtor_p->makeAverageResponse(vb, imageTemplate, sensitivityImage))
+      cfCache_p->flush(sensitivityImage,sensitivityPatternQualifierStr_p);
+  }
+  //
+  //---------------------------------------------------------------
+  //
+  void LofarFTMachineOld::normalizeAvgPB(ImageInterface<Complex>& inImage,
+				   ImageInterface<Float>& outImage)
+  {
+    LogIO log_l(LogOrigin("LofarFTMachineOld", "normalizeAvgPB"));
+    if (pbNormalized_p) return;
+    IPosition inShape(inImage.shape()),ndx(4,0,0,0,0);
+    Vector<Complex> peak(inShape(2));
+
+    outImage.resize(inShape);
+    outImage.setCoordinateInfo(inImage.coordinates());
+
+    Bool isRefIn, isRefOut;
+    Array<Complex> inBuf;
+    Array<Float> outBuf;
+
+    isRefIn  = inImage.get(inBuf);
+    isRefOut = outImage.get(outBuf);
+    log_l << "Normalizing the average PBs to unity"
+	  << LogIO::NORMAL << LogIO::POST;
+    //
+    // Normalize each plane of the inImage separately to unity.
+    //
+    Complex inMax = max(inBuf);
+    if (abs(inMax)-1.0 > 1E-3)
+      {
+	for(ndx(3)=0;ndx(3)<inShape(3);ndx(3)++)
+	  for(ndx(2)=0;ndx(2)<inShape(2);ndx(2)++)
+	    {
+	      peak(ndx(2)) = 0;
+	      for(ndx(1)=0;ndx(1)<inShape(1);ndx(1)++)
+		for(ndx(0)=0;ndx(0)<inShape(0);ndx(0)++)
+		  if (abs(inBuf(ndx)) > peak(ndx(2)))
+		    peak(ndx(2)) = inBuf(ndx);
+
+	      for(ndx(1)=0;ndx(1)<inShape(1);ndx(1)++)
+		for(ndx(0)=0;ndx(0)<inShape(0);ndx(0)++)
+		  //		      avgPBBuf(ndx) *= (pbPeaks(ndx(2))/peak(ndx(2)));
+		  inBuf(ndx) /= peak(ndx(2));
+	    }
+	if (isRefIn) inImage.put(inBuf);
+      }
+
+    ndx=0;
+    for(ndx(1)=0;ndx(1)<inShape(1);ndx(1)++)
+      for(ndx(0)=0;ndx(0)<inShape(0);ndx(0)++)
+	{
+	  IPosition plane1(ndx);
+	  plane1=ndx;
+	  plane1(2)=1; // The other poln. plane
+	  //	  avgPBBuf(ndx) = (avgPBBuf(ndx) + avgPBBuf(plane1))/2.0;
+	  outBuf(ndx) = sqrt(real(inBuf(ndx) * inBuf(plane1)));
+	}
+    //
+    // Rather convoluted way of copying Pol. plane-0 to Pol. plane-1!!!
+    //
+    for(ndx(1)=0;ndx(1)<inShape(1);ndx(1)++)
+      for(ndx(0)=0;ndx(0)<inShape(0);ndx(0)++)
+	{
+	  IPosition plane1(ndx);
+	  plane1=ndx;
+	  plane1(2)=1; // The other poln. plane
+	  outBuf(plane1) = outBuf(ndx);
+	}
+
+    pbNormalized_p = True;
+  }
+  //
+  //---------------------------------------------------------------
+  //
+  void LofarFTMachineOld::normalizeAvgPB()
+  {
+    LogIO log_l(LogOrigin("LofarFTMachineOld", "normalizeAvgPB"));
+    if (pbNormalized_p) return;
+    Bool isRefF;
+    Array<Float> avgPBBuf;
+    isRefF=avgPB_p->get(avgPBBuf);
+    //    Float pbMax = max(avgPBBuf);
+      {
+	pbPeaks.resize(avgPB_p->shape()(2),True);
+	// if (makingPSF) pbPeaks = 1.0;
+	// else pbPeaks /= (Float)noOfPASteps;
+	pbPeaks = 1.0;
+	log_l << "Normalizing the average PBs to " << 1.0
+	      << LogIO::NORMAL << LogIO::POST;
+
+	IPosition avgPBShape(avgPB_p->shape()),ndx(4,0,0,0,0);
+	Vector<Float> peak(avgPBShape(2));
+
+
+	Float pbMax = max(avgPBBuf);
+	if (fabs(pbMax-1.0) > 1E-3)
+	  {
+	    //	    avgPBBuf = avgPBBuf/noOfPASteps;
+	    for(ndx(3)=0;ndx(3)<avgPBShape(3);ndx(3)++)
+	      for(ndx(2)=0;ndx(2)<avgPBShape(2);ndx(2)++)
+		{
+		  peak(ndx(2)) = 0;
+		  for(ndx(1)=0;ndx(1)<avgPBShape(1);ndx(1)++)
+		    for(ndx(0)=0;ndx(0)<avgPBShape(0);ndx(0)++)
+		      if (abs(avgPBBuf(ndx)) > peak(ndx(2)))
+			peak(ndx(2)) = avgPBBuf(ndx);
+
+		  for(ndx(1)=0;ndx(1)<avgPBShape(1);ndx(1)++)
+		    for(ndx(0)=0;ndx(0)<avgPBShape(0);ndx(0)++)
+		      //		      avgPBBuf(ndx) *= (pbPeaks(ndx(2))/peak(ndx(2)));
+		      avgPBBuf(ndx) /= peak(ndx(2));
+		}
+	    if (isRefF) avgPB_p->put(avgPBBuf);
+	  }
+
+	ndx=0;
+	for(ndx(1)=0;ndx(1)<avgPBShape(1);ndx(1)++)
+	  for(ndx(0)=0;ndx(0)<avgPBShape(0);ndx(0)++)
+	    {
+	      IPosition plane1(ndx);
+	      plane1=ndx;
+	      plane1(2)=1; // The other poln. plane
+	      avgPBBuf(ndx) = (avgPBBuf(ndx) + avgPBBuf(plane1))/2.0;
+	      //	      avgPBBuf(ndx) = (avgPBBuf(ndx) * avgPBBuf(plane1));
+	    }
+	for(ndx(1)=0;ndx(1)<avgPBShape(1);ndx(1)++)
+	  for(ndx(0)=0;ndx(0)<avgPBShape(0);ndx(0)++)
+	    {
+	      IPosition plane1(ndx);
+	      plane1=ndx;
+	      plane1(2)=1; // The other poln. plane
+	      avgPBBuf(plane1) = avgPBBuf(ndx);
+	    }
+      }
+      pbNormalized_p = True;
+  }
+
+  void LofarFTMachineOld::normalizeImage(Lattice<Complex>& skyImage,
+				   const Matrix<Double>& sumOfWts,
+				   Lattice<Float>& sensitivityImage,
+				   Lattice<Complex>& sensitivitySqImage,
+				   Bool fftNorm)
+  {
+    //
+    // Apply the gridding correction
+    //
+    if (itsVerbose > 0) {
+      cout<<"LofarFTMachineOld::normalizeImage"<<endl;
+    }
+    Int inx = skyImage.shape()(0);
+    Int iny = skyImage.shape()(1);
+    Vector<Complex> correction(inx);
+
+    Vector<Float> sincConv(nx);
+    Float centerX=nx/2;
+    for (Int ix=0;ix<nx;ix++)
+      {
+	Float x=C::pi*Float(ix-centerX)/(Float(nx)*Float(convSampling));
+	if(ix==centerX) sincConv(ix)=1.0;
+	else 	    sincConv(ix)=sin(x)/x;
+      }
+
+    IPosition cursorShape(4, inx, 1, 1, 1);
+    IPosition axisPath(4, 0, 1, 2, 3);
+    LatticeStepper lsx(skyImage.shape(), cursorShape, axisPath);
+    LatticeIterator<Complex> lix(skyImage, lsx);
+
+    LatticeStepper lavgpb(sensitivityImage.shape(),cursorShape,axisPath);
+    LatticeIterator<Float> liavgpb(sensitivityImage, lavgpb);
+    LatticeStepper lavgpbSq(sensitivitySqImage.shape(),cursorShape,axisPath);
+    LatticeIterator<Complex> liavgpbSq(sensitivitySqImage, lavgpbSq);
+
+    for(lix.reset(),liavgpb.reset(),liavgpbSq.reset();
+	!lix.atEnd();
+	lix++,liavgpb++,liavgpbSq++)
+      {
+	Int pol=lix.position()(2);
+	Int chan=lix.position()(3);
+
+	if(sumOfWts(pol, chan)>0.0)
+	  {
+	    Int iy=lix.position()(1);
+	    gridder->correctX1D(correction,iy);
+
+	    Vector<Complex> PBCorrection(liavgpb.rwVectorCursor().shape());
+	    Vector<Float> avgPBVec(liavgpb.rwVectorCursor().shape());
+	    Vector<Complex> avgPBSqVec(liavgpbSq.rwVectorCursor().shape());
+
+	    avgPBSqVec= liavgpbSq.rwVectorCursor();
+	    avgPBVec = liavgpb.rwVectorCursor();
+
+	    for(int i=0;i<PBCorrection.shape();i++)
+	      {
+		//
+		// This with the PS functions
+		//
+		// PBCorrection(i)=FUNC(avgPBVec(i))*sincConv(i)*sincConv(iy);
+		// if ((abs(PBCorrection(i)*correction(i))) >= pbLimit_p)
+		// 	lix.rwVectorCursor()(i) /= PBCorrection(i)*correction(i);
+		// else if (!makingPSF)
+		// 	lix.rwVectorCursor()(i) /= correction(i)*sincConv(i)*sincConv(iy);
+		//
+		// This without the PS functions
+		//
+
+
+
+		// if (makingPSF)
+		PBCorrection(i)=(avgPBSqVec(i)/avgPBVec(i));///(sincConv(i)*sincConv(iy));
+		//		PBCorrection(i)=(avgPBSqVec(i));///(sincConv(i)*sincConv(iy));
+		//		PBCorrection(i)=avgPBVec(i);///(sincConv(i)*sincConv(iy));
+
+		// else
+		//		PBCorrection(i)=(avgPBVec(i));//*sincConv(i)*sincConv(iy);
+		//		if ((abs(avgPBSqVec(i))) >= pbLimit_p)
+		if ((abs(avgPBVec(i))) >= pbLimit_p)
+		  lix.rwVectorCursor()(i) /= PBCorrection(i);
+
+		// if ((abs(PBCorrection(i))) >= pbLimit_p)
+		//   lix.rwVectorCursor()(i) /= PBCorrection(i);
+		// else if (!makingPSF)
+		//   lix.rwVectorCursor()(i) /= sincConv(i)*sincConv(iy);
+
+
+		// PBCorrection(i)=FUNC(avgPBVec(i)/avgPBSqVec(i))/(sincConv(i)*sincConv(iy));
+		// lix.rwVectorCursor()(i) *= PBCorrection(i);
+
+		// if ((abs(avgPBSqVec(i))) >= pbLimit_p)
+		//   lix.rwVectorCursor()(i) *= PBCorrection(i);
+		// else if (!makingPSF)
+		//   lix.rwVectorCursor()(i) /= sincConv(i)*sincConv(iy);
+	      }
+
+	    if(fftNorm)
+	      {
+		Complex rnorm(Float(inx)*Float(iny)/sumOfWts(pol,chan));
+		lix.rwCursor()*=rnorm;
+	      }
+	    else
+	      {
+		Complex rnorm(Float(inx)*Float(iny));
+		lix.rwCursor()*=rnorm;
+	      }
+	  }
+	else
+	  lix.woCursor()=0.0;
+      }
+  }
+  //
+  //---------------------------------------------------------------
+  //
+  void LofarFTMachineOld::normalizeImage(Lattice<Complex>& skyImage,
+				   const Matrix<Double>& sumOfWts,
+				   Lattice<Float>& sensitivityImage,
+				   Bool fftNorm)
+  {
+    //
+    // Apply the gridding correction
+    //
+    if (itsVerbose > 0) {
+      cout<<"LofarFTMachineOld::normalizeImage<"<<endl;
+    }
+    Int inx = skyImage.shape()(0);
+    Int iny = skyImage.shape()(1);
+    Vector<Complex> correction(inx);
+
+    Vector<Float> sincConv(nx);
+    Float centerX=nx/2;
+    for (Int ix=0;ix<nx;ix++)
+      {
+	Float x=C::pi*Float(ix-centerX)/(Float(nx)*Float(convSampling));
+	if(ix==centerX) sincConv(ix)=1.0;
+	else 	    sincConv(ix)=sin(x)/x;
+      }
+
+    IPosition cursorShape(4, inx, 1, 1, 1);
+    IPosition axisPath(4, 0, 1, 2, 3);
+    LatticeStepper lsx(skyImage.shape(), cursorShape, axisPath);
+    //    LatticeIterator<Complex> lix(skyImage, lsx);
+    LatticeIterator<Complex> lix(skyImage, lsx);
+
+    LatticeStepper lavgpb(sensitivityImage.shape(),cursorShape,axisPath);
+    // Array<Float> senArray;sensitivityImage.get(senArray,True);
+    // ArrayLattice<Float> senLat(senArray,True);
+    //    LatticeIterator<Float> liavgpb(senLat, lavgpb);
+    LatticeIterator<Float> liavgpb(sensitivityImage, lavgpb);
+
+    for(lix.reset(),liavgpb.reset();
+	!lix.atEnd();
+	lix++,liavgpb++)
+      {
+	Int pol=lix.position()(2);
+	Int chan=lix.position()(3);
+
+	if(sumOfWts(pol, chan)>0.0)
+	  {
+	    Int iy=lix.position()(1);
+	    gridder->correctX1D(correction,iy);
+
+	    Vector<Float> avgPBVec(liavgpb.rwVectorCursor().shape());
+
+	    avgPBVec = liavgpb.rwVectorCursor();
+
+	    for(int i=0;i<avgPBVec.shape();i++)
+	      {
+		//
+		// This with the PS functions
+		//
+		// PBCorrection(i)=FUNC(avgPBVec(i))*sincConv(i)*sincConv(iy);
+		// if ((abs(PBCorrection(i)*correction(i))) >= pbLimit_p)
+		// 	lix.rwVectorCursor()(i) /= PBCorrection(i)*correction(i);
+		// else if (!makingPSF)
+		// 	lix.rwVectorCursor()(i) /= correction(i)*sincConv(i)*sincConv(iy);
+		//
+		// This without the PS functions
+		//
+		//                Float tt=sqrt(avgPBVec(i))/avgPBVec(i);
+		Float tt = pbFunc(avgPBVec(i),pbLimit_p);
+                  //		PBCorrection(i)=pbFunc(avgPBVec(i),pbLimit_p)*sincConv(i)*sincConv(iy);
+                  //                lix.rwVectorCursor()(i) /= PBCorrection(i);
+		//                lix.rwVectorCursor()(i) *= tt;
+
+		lix.rwVectorCursor()(i) /= tt;
+		// if ((abs(tt) >= pbLimit_p))
+		//   lix.rwVectorCursor()(i) /= tt;
+		// else if (!makingPSF)
+		//   lix.rwVectorCursor()(i) /= sincConv(i)*sincConv(iy);
+	      }
+
+	    if(fftNorm)
+	      {
+		Complex rnorm(Float(inx)*Float(iny)/sumOfWts(pol,chan));
+		lix.rwCursor()*=rnorm;
+	      }
+	    else
+	      {
+		Complex rnorm(Float(inx)*Float(iny));
+		lix.rwCursor()*=rnorm;
+	      }
+	  }
+	else
+	  lix.woCursor()=0.0;
+      }
+  }
+
+
+  void LofarFTMachineOld::makeCFPolMap(const VisBuffer& vb, const Vector<Int>& locCfStokes,
+				 Vector<Int>& polM)
+  {
+    LogIO log_l(LogOrigin("LofarFTMachineOld", "findPointingOffsets"));
+    Vector<Int> msStokes = vb.corrType();
+    Int nPol = msStokes.nelements();
+    polM.resize(polMap.shape());
+    polM = -1;
+
+    for(Int i=0;i<nPol;i++)
+      for(uInt j=0;j<locCfStokes.nelements();j++)
+	if (locCfStokes(j) == msStokes(i))
+	    {polM(i) = j;break;}
+  }
+  //
+  //---------------------------------------------------------------
+  //
+  // Given a polMap (mapping of which Visibility polarization is
+  // gridded onto which grid plane), make a map of the conjugate
+  // planes of the grid E.g, for Stokes-I and -V imaging, the two
+  // planes of the uv-grid are [LL,RR].  For input VisBuffer
+  // visibilites in order [RR,RL,LR,LL], polMap = [1,-1,-1,0].  The
+  // conjugate map will be [0,-1,-1,1].
+  //
+  void LofarFTMachineOld::makeConjPolMap(const VisBuffer& vb,
+				     const Vector<Int> cfPolMap,
+				     Vector<Int>& conjPolMap)
+  {
+    LogIO log_l(LogOrigin("LofarFTMachineOld", "makConjPolMap"));
+    //
+    // All the Natak (Drama) below with slicers etc. is to extract the
+    // Poln. info. for the first IF only (not much "information
+    // hiding" for the code to slice arrays in a general fashion).
+    //
+    // Extract the shape of the array to be sliced.
+    //
+    Array<Int> stokesForAllIFs = vb.msColumns().polarization().corrType().getColumn();
+    IPosition stokesShape(stokesForAllIFs.shape());
+    IPosition firstIFStart(stokesShape),firstIFLength(stokesShape);
+    //
+    // Set up the start and length IPositions to extract only the
+    // first column of the array.  The following is required since the
+    // array could have only one column as well.
+    //
+    firstIFStart(0)=0;firstIFLength(0)=stokesShape(0);
+    for(uInt i=1;i<stokesShape.nelements();i++) {firstIFStart(i)=0;firstIFLength(i)=1;}
+    //
+    // Construct the slicer and produce the slice.  .nonDegenerate
+    // required to ensure the result of slice is a pure vector.
+    //
+    Vector<Int> visStokes = stokesForAllIFs(Slicer(firstIFStart,firstIFLength)).nonDegenerate();
+
+    conjPolMap = cfPolMap;
+
+    Int i,j,N = cfPolMap.nelements();
+    
+    for(i=0;i<N;i++)
+      if (cfPolMap[i] > -1)
+	{
+	if      (visStokes[i] == Stokes::XX)
+	  {
+	    conjPolMap[i]=-1;
+	    for(j=0;j<N;j++) if (visStokes[j] == Stokes::YY) break;
+	    conjPolMap[i]=cfPolMap[j];
+	  }
+	else if (visStokes[i] == Stokes::YY)
+	  {
+	    conjPolMap[i]=-1;
+	    for(j=0;j<N;j++) if (visStokes[j] == Stokes::XX) break;
+	    conjPolMap[i]=cfPolMap[j];
+	  }
+	else if (visStokes[i] == Stokes::YX)
+	  {
+	    conjPolMap[i]=-1;
+	    for(j=0;j<N;j++) if (visStokes[j] == Stokes::XY) break;
+	    conjPolMap[i]=cfPolMap[j];
+	  }
+	else if (visStokes[i] == Stokes::XY)
+	  {
+	    conjPolMap[i]=-1;
+	    for(j=0;j<N;j++) if (visStokes[j] == Stokes::YX) break;
+	    conjPolMap[i]=cfPolMap[j];
+	  }
+	}
+  }
+
+  void LofarFTMachineOld::showTimings (ostream& os, double duration) const
+  {
+    // The total time is the real elapsed time.
+    // The cf and (de)gridding time is the sum of all threads, so scale
+    // them back to real time.
+    double total = itsCFTime + itsGriddingTime + itsDegriddingTime;
+    double scale = 1;
+    if (total > 0) {
+      scale = itsTotalTimer.getReal() / total;
+    }
+    itsConvFunc->showTimings (os, duration, itsCFTime*scale);
+    if (itsGriddingTime > 0) {
+      os << "  gridding          ";
+      LofarConvolutionFunctionOld::showPerc1 (os, itsGriddingTime*scale,
+                                           duration);
+      os << endl;
+    }
+    if (itsDegriddingTime > 0) {
+      os << "  degridding        ";
+      LofarConvolutionFunctionOld::showPerc1 (os, itsDegriddingTime*scale,
+                                           duration);
+      os << endl;
+    }
+  }
+
+} //# end namespace
diff --git a/CEP/Imager/LofarFT/src/LofarImager.cc b/CEP/Imager/LofarFT/src/LofarImager.cc
index 08068444b85b24d8fe605f6394c7a7ab61b38146..34fa5eb10411462a0e0c5f728a840f1d1f341624 100644
--- a/CEP/Imager/LofarFT/src/LofarImager.cc
+++ b/CEP/Imager/LofarFT/src/LofarImager.cc
@@ -40,10 +40,11 @@ namespace LOFAR
   // @brief Imager for LOFAR data correcting for DD effects
 
   LofarImager::LofarImager (MeasurementSet& ms, const Record& parameters)
-    : Imager(ms),
-      itsParameters (parameters)
+    : Imager(ms,false, true),
+      itsParameters (parameters),
+      itsMachine    (0),
+      itsMachineOld (0)
   {
-    cout << itsParameters<<endl;
   }
 
   LofarImager::~LofarImager()
@@ -53,7 +54,36 @@ namespace LOFAR
   {
     CountedPtr<VisibilityResamplerBase> visResampler;
     Bool useDoublePrecGrid = False;
-    itsMachine = new LofarFTMachine(cache_p/2, tile_p,
+    Double RefFreq((*sm_p).getReferenceFrequency());
+
+    if (itsParameters.asBool("splitbeam")) {
+      cout << itsParameters<<endl;
+      itsMachine = new LofarFTMachine(cache_p/2, tile_p,
+                                      visResampler, gridfunction_p,
+                                      *ms_p, wprojPlanes_p, mLocation_p,
+                                      padding_p, false, useDoublePrecGrid,
+                                      itsParameters.asDouble("wmax"),
+                                      itsParameters.asInt("verbose"),
+                                      itsParameters.asInt("maxsupport"),
+                                      itsParameters.asInt("oversample"),
+                                      itsParameters.asString("imagename"),
+                                      itsParameters.asArrayBool("mueller.grid"),
+                                      itsParameters.asArrayBool("mueller.degrid"),
+                                      RefFreq,
+                                      itsParameters.asBool("UseLIG"),
+                                      itsParameters.asBool("UseEJones"),
+                                      itsParameters.asInt("StepApplyElement"),
+                                      itsParameters.asDouble("PBCut"),
+                                      itsParameters.asBool("PredictFT"),
+                                      itsParameters.asString("PsfImage"),
+                                      itsParameters.asBool("UseMasksDegrid"),
+                                      itsParameters.asBool("doPSF"),
+                                      itsParameters);//,
+                                      //itsParameters.asDouble("FillFactor"));
+    
+      ft_p  = itsMachine;
+    } else {
+    itsMachineOld = new LofarFTMachineOld(cache_p/2, tile_p,
                                     visResampler, gridfunction_p,
                                     *ms_p, wprojPlanes_p, mLocation_p,
                                     padding_p, false, useDoublePrecGrid,
@@ -65,7 +95,9 @@ namespace LOFAR
                                     itsParameters.asString("imagename"),
                                     itsParameters.asArrayBool("mueller.grid"),
                                     itsParameters.asArrayBool("mueller.degrid"));
-    ft_p  = itsMachine;
+      ft_p  = itsMachineOld;
+    }
+
     cft_p = new SimpleComponentFTMachine();
 
     //setClarkCleanImageSkyModel();
@@ -79,9 +111,13 @@ namespace LOFAR
     Int nrowBlock = nrowPerTime * max(1,ntime);
     // Set row blocking in VisIter.
     rvi_p->setRowBlocking (nrowBlock);
+    if(itsParameters.asInt("RowBlock")>0){
+      rvi_p->setRowBlocking (itsParameters.asInt("RowBlock"));
+    };
 /*    os << LogIO::NORMAL
        << "vi.setRowBlocking(" << nrowBlock << ")"
        << LogIO::POST;*/
+    
     return True;
   }
 
@@ -89,6 +125,7 @@ namespace LOFAR
   {
     se_p = new LofarCubeSkyEquation(*sm_p, *rvi_p, *ft_p, *cft_p,
                                     !useModelCol_p);
+
     return;
   }
 
@@ -102,9 +139,12 @@ namespace LOFAR
   // Show the relative timings of the various steps.
   void LofarImager::showTimings (std::ostream&, double duration) const
   {
-    itsMachine->showTimings (cout, duration);
+    if (itsMachine) {
+      itsMachine->showTimings (cout, duration);
+    } else if (itsMachineOld) {
+      itsMachineOld->showTimings (cout, duration);
+    }
   }
 
 
 } //# end namespace
-
diff --git a/CEP/Imager/LofarFT/src/LofarVisResampler.cc b/CEP/Imager/LofarFT/src/LofarVisResampler.cc
index 5533e971bd620abf3fcca4e9e7e88ea5ffff31c7..a9249dd8ab1bbd2e7054eb2b1bd83b948bef0c41 100644
--- a/CEP/Imager/LofarFT/src/LofarVisResampler.cc
+++ b/CEP/Imager/LofarFT/src/LofarVisResampler.cc
@@ -20,15 +20,224 @@
 //#
 //# $Id$
 
+#include <lofar_config.h>
 #include <LofarFT/LofarVisResampler.h>
 #include <synthesis/MeasurementComponents/Utils.h>
 #include <coordinates/Coordinates/SpectralCoordinate.h>
 #include <coordinates/Coordinates/CoordinateSystem.h>
-#include<cassert>
+#include <cassert>
+#include <Common/OpenMP.h>
 
 namespace LOFAR {
 
     // Instantiate both templates.
+
+
+  template
+  void LofarVisResampler::DataToGridImpl_linear_p(Array<DComplex>& grid, LofarVBStore& vbs,
+                                           const Vector<uInt>& rows,
+                                           Int rbeg, Int rend,
+					Matrix<Double>& sumwt,const Bool& dopsf,
+					LofarCFStore& cfs) __restrict__;
+  template
+  void LofarVisResampler::DataToGridImpl_linear_p(Array<Complex>& grid, LofarVBStore& vbs,
+                                           const Vector<uInt>& rows,
+                                           Int rbeg, Int rend,
+					Matrix<Double>& sumwt,const Bool& dopsf,
+					LofarCFStore& cfs) __restrict__;
+
+
+  template <class T>
+  void LofarVisResampler::DataToGridImpl_linear_p(Array<T>& grid,  LofarVBStore& vbs,
+                                           const Vector<uInt>& rows,
+                                           Int rbeg, Int rend,
+                                           Matrix<Double>& sumwt,
+                                           const Bool& dopsf,
+                                           LofarCFStore& cfs) __restrict__
+  {
+    // grid[nx,ny,np,nf]
+    // vbs.data[np,nf,nrow]
+    // cfs[nmx,nmy,nf,ncx,ncy]   (mueller,freq,convsize)
+
+    // Get size of convolution functions.
+    Int nConvX = (*(cfs.vdata))[0][0][0].shape()[0];
+    Int nConvY = (*(cfs.vdata))[0][0][0].shape()[1];
+    // Get size of grid.
+    Int nGridX    = grid.shape()[0];
+    Int nGridY    = grid.shape()[1];
+    Int nGridPol  = grid.shape()[2];
+    Int nGridChan = grid.shape()[3];
+    //cout<<"nGridPol<<nGridChan "<<nGridPol<<" "<<nGridChan<<endl;
+
+    // Get visibility data size.
+    Int nVisPol   = vbs.flagCube_p.shape()[0];
+    Int nVisChan  = vbs.flagCube_p.shape()[1];
+    //cout<<"nVisPol<<nVisChan "<<nVisPol<<" "<<nVisChan<<endl;
+    // Get oversampling and support size.
+    Int sampx = SynthesisUtils::nint (cfs.sampling[0]);
+    Int sampy = SynthesisUtils::nint (cfs.sampling[1]);
+    Int supx = cfs.xSupport[0];
+    Int supy = cfs.ySupport[0];
+    ///AlwaysAssert ((2*supx+1)*sampx == nConvX, AipsError);
+    ///AlwaysAssert ((2*supy+1)*sampy == nConvY, AipsError);
+
+    Double* __restrict__ sumWtPtr = sumwt.data();
+    Complex psfValues[4];
+    psfValues[0] = psfValues[1] = psfValues[2] = psfValues[3] = Complex(1,0);
+
+    vector< Float > Weights_Lin_Interp;
+    Weights_Lin_Interp.resize(4);
+    vector< Int > deltax_pix_interp;
+    deltax_pix_interp.resize(4);
+    vector< Int > deltay_pix_interp;
+    deltay_pix_interp.resize(4);
+    for(uInt i=0;i<4;i++){
+      deltax_pix_interp[i]=0;
+      deltay_pix_interp[i]=0;
+    }
+
+    // Loop over all visibility rows to process.
+    for (Int inx=rbeg; inx<=rend; ++inx) {
+      Int irow = rows[inx];
+      const Double*  __restrict__ uvwPtr   = vbs.uvw_p.data() + irow*3;
+      const Float*   __restrict__ imgWtPtr = vbs.imagingWeight_p.data() +
+                                             irow * nVisChan;
+      // Loop over all channels in the visibility data.
+      // Map the visibility channel to the grid channel.
+      // Skip channel if data are not needed.
+      for (Int visChan=0; visChan<nVisChan; ++visChan) {
+        Int gridChan = chanMap_p[visChan];
+
+	//cout<<"visChan "<<visChan<<endl;
+        if (gridChan >= 0  &&  gridChan < nGridChan) {
+          // Determine the grid position from the UV coordinates in wavelengths.
+          Double recipWvl = vbs.freq_p[visChan] / C::c;
+          Double posx = uvwScale_p[0] * uvwPtr[0] * recipWvl + offset_p[0];
+          Double posy = uvwScale_p[1] * uvwPtr[1] * recipWvl + offset_p[1];
+          Int locx = floor(posx);//SynthesisUtils::nint (posx);    // location in grid
+          Int locy = floor(posy);//SynthesisUtils::nint (posy);
+          Double diffx = locx - posx;
+          //if (diffx < 0) diffx += 1;
+          Double diffy = locy - posy;
+
+ 	  if(diffx>0){
+	    deltax_pix_interp[0]=-1;
+	    deltax_pix_interp[1]=0;
+	    deltax_pix_interp[2]=-1;
+	    deltax_pix_interp[3]=0;
+	  } else {
+	    deltax_pix_interp[0]=0;
+	    deltax_pix_interp[1]=1;
+	    deltax_pix_interp[2]=0;
+	    deltax_pix_interp[3]=1;
+	  }
+	  if(diffy>0){
+	    deltay_pix_interp[0]=-1;
+	    deltay_pix_interp[1]=-1;
+	    deltay_pix_interp[2]=0;
+	    deltay_pix_interp[3]=0;
+	  } else {
+	    deltay_pix_interp[0]=0;
+	    deltay_pix_interp[1]=0;
+	    deltay_pix_interp[2]=1;
+	    deltay_pix_interp[3]=1;
+	  }
+
+	  Double diff_floor_x(abs(posx-floor(posx)));
+	  Double diff_floor_y(abs(posy-floor(posy)));
+
+
+	  Weights_Lin_Interp[0]=(1.-diff_floor_x)*(1.-diff_floor_y);
+	  Weights_Lin_Interp[1]=    diff_floor_x *(1.-diff_floor_y);
+	  Weights_Lin_Interp[2]=(1.-diff_floor_x)*    diff_floor_y ;
+	  Weights_Lin_Interp[3]=    diff_floor_x *    diff_floor_y ;
+
+	  //cout<<"diff_floor_x<<diff_floor_y "<<diff_floor_x<<" "<<diff_floor_y<<" "<<Weights_Lin_Interp[0]<<" "<<Weights_Lin_Interp[1]<<" "<<Weights_Lin_Interp[2]<<" "<<Weights_Lin_Interp[3]<<" "<<endl;
+
+          ///if (diffy < 0) diffy += 1;
+          Int offx = 0;//SynthesisUtils::nint (diffx * sampx); // location in
+          Int offy = 0;//SynthesisUtils::nint (diffy * sampy); // oversampling
+          ///          cout<<"  pos= ["<<posx<<", "<<posy<<"]"
+          ///              <<", loc= ["<<locx<<", "<<locy<<"]"
+          ///              <<", off= ["<<offx<<", "<<offy<<"]" << endl;
+          offx += (nConvX-1)/2;
+          offy += (nConvY-1)/2;
+
+          // Scaling with frequency is not necessary (according to Cyril).
+          Double freqFact = 1;   // = cfFreq / vbs.freq_p[visChan];
+          Int fsampx = SynthesisUtils::nint (sampx * freqFact);
+          Int fsampy = SynthesisUtils::nint (sampy * freqFact);
+          Int fsupx  = SynthesisUtils::nint (supx / freqFact);
+          Int fsupy  = SynthesisUtils::nint (supy / freqFact);
+
+          // Only use visibility point if the full support is within grid.
+          if (locx-supx >= 0  &&  locx+supx < nGridX  &&
+              locy-supy >= 0  &&  locy+supy < nGridY) {
+            ///            cout << "in grid"<<endl;
+            // Get pointer to data and flags for this channel.
+            Int doff = (irow * nVisChan + visChan) * nVisPol;
+            const Complex* __restrict__ visPtr  = vbs.visCube_p.data()  + doff;
+            const Bool*    __restrict__ flagPtr = vbs.flagCube_p.data() + doff;
+            if (dopsf) {
+              visPtr = psfValues;
+            }
+
+            // Handle a visibility if not flagged.
+	    for (Int w=0; w<4; w++) {
+	      Double weight_interp(Weights_Lin_Interp[w]);
+            for (Int ipol=0; ipol<nVisPol; ++ipol) {
+              if (! flagPtr[ipol]) {
+                // Map to grid polarization. Only use pol if needed.
+                Int gridPol = polMap_p(ipol);
+                if (gridPol >= 0  &&  gridPol < nGridPol) {
+		  //cout<<"ipol: "<<ipol<<endl;
+                  // Get the offset in the grid data array.
+                  Int goff = (gridChan*nGridPol + gridPol) * nGridX * nGridY;
+                  // Loop over the scaled support.
+                  for (Int sy=-fsupy; sy<=fsupy; ++sy) {
+                    // Get the pointer in the grid for the first x in this y.
+                    T* __restrict__ gridPtr = grid.data() + goff +
+                                              (locy+sy+deltay_pix_interp[w])*nGridX + locx-supx+deltax_pix_interp[w];
+		    //cout<<"goff<<locy<<sy<<nGridX<<locx<<supx "<<goff<<" "<<locy<<" "<<sy<<" "<<nGridX<<" "<<locx<<" "<<supx<<endl;
+                    // Get pointers to the first element to use in the 4
+                    // convolution functions for this channel,pol.
+                    const Complex* __restrict__ cf[4];
+
+                    Int cfoff = (offy + sy*fsampy)*nConvX + offx - fsupx*fsampx;
+		    //cout<<"cfoff<<offy<<fsampy<<nConvX<<offx<<fsupx<<fsampx "<<cfoff<<" "<<offy<<" "<<fsampy<<" "<<nConvX<<" "<<offx<<" "<<fsupx<<" "<<fsampx<<endl;
+                    for (int i=0; i<4; ++i) {
+                      cf[i] = (*cfs.vdata)[gridChan][i][ipol].data() + cfoff;
+                    }
+                    for (Int sx=-fsupx; sx<=fsupx; ++sx) {
+                      // Loop over polarizations to correct for leakage.
+                      Complex polSum(0,0);
+                      for (Int i=0; i<4; ++i) {
+                        ///                        cout<<"cf="<< cf[i]-(*cfs.vdata)[gridChan][ipol][i].data()<<',';
+			//cout<<"visPtr[i] <<" "<< *cf[i] == "<<visPtr[i] <<" "<< *cf[i]<<endl;
+                        polSum += (visPtr[i] * *cf[i])* weight_interp ;
+                        cf[i] += fsampx;
+		      }
+                      ///                      cout<<"  g="<<gridPtr-grid.data()<<' '<<visPtr[i]<<endl;
+		      polSum *= (*imgWtPtr);
+                      *gridPtr++ += polSum ;
+                    }
+                  }
+                  sumWtPtr[gridPol+gridChan*nGridPol] += (*imgWtPtr) * weight_interp;
+                } // end if gridPol
+              } // end if !flagPtr
+            } // end for ipol
+	    }
+          } // end if ongrid
+        } // end if gridChan
+        imgWtPtr++;
+      } // end for visChan
+    } // end for inx
+  }
+
+
+  // ================================
+  // "Traditional" gridder
+
   template
   void LofarVisResampler::DataToGridImpl_p(Array<DComplex>& grid, LofarVBStore& vbs,
                                            const Vector<uInt>& rows,
@@ -42,7 +251,6 @@ namespace LOFAR {
 					Matrix<Double>& sumwt,const Bool& dopsf,
 					LofarCFStore& cfs) __restrict__;
 
-
   template <class T>
   void LofarVisResampler::DataToGridImpl_p(Array<T>& grid,  LofarVBStore& vbs,
                                            const Vector<uInt>& rows,
@@ -63,20 +271,29 @@ namespace LOFAR {
     Int nGridY    = grid.shape()[1];
     Int nGridPol  = grid.shape()[2];
     Int nGridChan = grid.shape()[3];
+    //cout<<"nGridPol<<nGridChan "<<nGridPol<<" "<<nGridChan<<endl;
+
     // Get visibility data size.
     Int nVisPol   = vbs.flagCube_p.shape()[0];
     Int nVisChan  = vbs.flagCube_p.shape()[1];
+    //cout<<"nVisPol<<nVisChan "<<nVisPol<<" "<<nVisChan<<endl;
     // Get oversampling and support size.
     Int sampx = SynthesisUtils::nint (cfs.sampling[0]);
     Int sampy = SynthesisUtils::nint (cfs.sampling[1]);
     Int supx = cfs.xSupport[0];
     Int supy = cfs.ySupport[0];
+
     ///AlwaysAssert ((2*supx+1)*sampx == nConvX, AipsError);
     ///AlwaysAssert ((2*supy+1)*sampy == nConvY, AipsError);
 
     Double* __restrict__ sumWtPtr = sumwt.data();
     Complex psfValues[4];
     psfValues[0] = psfValues[1] = psfValues[2] = psfValues[3] = Complex(1,0);
+    // psfValues[0] = -Complex(2,0);
+    // psfValues[1] = -Complex(1,1);
+    // psfValues[2] = -Complex(1,-1);
+    // psfValues[3] = -Complex(0,0);
+
 
     // Loop over all visibility rows to process.
     for (Int inx=rbeg; inx<=rend; ++inx) {
@@ -89,7 +306,14 @@ namespace LOFAR {
       // Skip channel if data are not needed.
       for (Int visChan=0; visChan<nVisChan; ++visChan) {
         Int gridChan = chanMap_p[visChan];
+
+
+	// !! dirty trick to select all channels
+	chanMap_p[visChan]=0;
+
+
         if (gridChan >= 0  &&  gridChan < nGridChan) {
+
           // Determine the grid position from the UV coordinates in wavelengths.
           Double recipWvl = vbs.freq_p[visChan] / C::c;
           Double posx = uvwScale_p[0] * uvwPtr[0] * recipWvl + offset_p[0];
@@ -115,8 +339,11 @@ namespace LOFAR {
           Int fsupy  = SynthesisUtils::nint (supy / freqFact);
 
           // Only use visibility point if the full support is within grid.
+
           if (locx-supx >= 0  &&  locx+supx < nGridX  &&
               locy-supy >= 0  &&  locy+supy < nGridY) {
+
+
             ///            cout << "in grid"<<endl;
             // Get pointer to data and flags for this channel.
             Int doff = (irow * nVisChan + visChan) * nVisPol;
@@ -128,9 +355,12 @@ namespace LOFAR {
             // Handle a visibility if not flagged.
             for (Int ipol=0; ipol<nVisPol; ++ipol) {
               if (! flagPtr[ipol]) {
+
                 // Map to grid polarization. Only use pol if needed.
                 Int gridPol = polMap_p(ipol);
                 if (gridPol >= 0  &&  gridPol < nGridPol) {
+
+  		  //cout<<"ipol: "<<ipol<<endl;
                   // Get the offset in the grid data array.
                   Int goff = (gridChan*nGridPol + gridPol) * nGridX * nGridY;
                   // Loop over the scaled support.
@@ -138,25 +368,41 @@ namespace LOFAR {
                     // Get the pointer in the grid for the first x in this y.
                     T* __restrict__ gridPtr = grid.data() + goff +
                                               (locy+sy)*nGridX + locx-supx;
+  		    //cout<<"goff<<locy<<sy<<nGridX<<locx<<supx "<<goff<<" "<<locy<<" "<<sy<<" "<<nGridX<<" "<<locx<<" "<<supx<<endl;
                     // Get pointers to the first element to use in the 4
                     // convolution functions for this channel,pol.
-                    const Complex* __restrict__ cf[4];
+
+		    // Fast version
+
+                    const Complex* __restrict__ cf[1];
                     Int cfoff = (offy + sy*fsampy)*nConvX + offx - fsupx*fsampx;
-                    for (int i=0; i<4; ++i) {
-                      cf[i] = (*cfs.vdata)[gridChan][ipol][i].data() + cfoff;
-                    }
+		    cf[0] = (*cfs.vdata)[gridChan][0][0].data() + cfoff;
                     for (Int sx=-fsupx; sx<=fsupx; ++sx) {
                       // Loop over polarizations to correct for leakage.
                       Complex polSum(0,0);
-                      for (Int i=0; i<nVisPol; ++i) {
-                        ///                        cout<<"cf="<< cf[i]-(*cfs.vdata)[gridChan][ipol][i].data()<<',';
-                        polSum += visPtr[i] * *cf[i];
-                        cf[i] += fsampx;
-                      }
-                      ///                      cout<<"  g="<<gridPtr-grid.data()<<' '<<visPtr[i]<<endl;
-		      polSum *= *imgWtPtr;
+		      polSum += visPtr[ipol] * *cf[0];
+		      cf[0] += fsampx;
+  		      polSum *= *imgWtPtr;
                       *gridPtr++ += polSum;
                     }
+
+		    // // Full version
+                    // const Complex* __restrict__ cf[4];
+                    // Int cfoff = (offy + sy*fsampy)*nConvX + offx - fsupx*fsampx;
+                    // for (int i=0; i<4; ++i) {
+                    //   cf[i] = (*cfs.vdata)[gridChan][i][ipol].data() + cfoff;
+                    // }
+                    // for (Int sx=-fsupx; sx<=fsupx; ++sx) {
+                    //   // Loop over polarizations to correct for leakage.
+                    //   Complex polSum(0,0);
+                    //   for (Int i=0; i<4; ++i) {
+                    //     polSum += visPtr[i] * *cf[i];
+                    //     cf[i] += fsampx;
+                    //   }
+  		    //   polSum *= *imgWtPtr;
+                    //   *gridPtr++ += polSum;
+                    // }
+
                   }
                   sumWtPtr[gridPol+gridChan*nGridPol] += *imgWtPtr;
                 } // end if gridPol
@@ -169,6 +415,9 @@ namespace LOFAR {
     } // end for inx
   }
 
+
+
+
   /*
   template <class T>
   void LofarVisResampler::DataToGridImpl_p(Array<T>& grid,  LofarVBStore& vbs,
@@ -232,7 +481,7 @@ namespace LOFAR {
       }
     }
     //cout<<"cfs.vdata= "<<&cfs<<endl;
-      
+
     const Double *freq  = vbs.freq_p.data();
 
     // Cache increment values for adding to grid in gridInc.  This is
@@ -256,7 +505,7 @@ namespace LOFAR {
     // 	      grid(tt)*=1.0;
     // }
 
-    for(Int inx=rbeg; inx< rend; inx++){
+    for(Int inx=rbeg; inx<=rend; inx++){
         Int irow = rows[inx];
 	for(Int ichan=0; ichan< nDataChan; ichan++){
 	    Int achan=chanMap_p[ichan];
@@ -357,6 +606,17 @@ namespace LOFAR {
   */
 
 
+  //=====================================================================================================================================
+  //=====================================================================================================================================
+  //=====================================================================================================================================
+  //=====================================================================================================================================
+  //=====================================================================================================================================
+  //=====================================================================================================================================
+  //=====================================================================================================================================
+
+
+
+
   //
   //-----------------------------------------------------------------------------------
   // Re-sample VisBuffer from a regular grid (griddedData) (a.k.a. de-gridding)
@@ -390,6 +650,13 @@ namespace LOFAR {
     ///AlwaysAssert ((2*supx+1)*sampx == nConvX, AipsError);
     ///AlwaysAssert ((2*supy+1)*sampy == nConvY, AipsError);
 
+    vector< Float > Weights_Lin_Interp;
+    Weights_Lin_Interp.resize(4);
+    vector< Int > deltax_pix_interp;
+    deltax_pix_interp.resize(4);
+    vector< Int > deltay_pix_interp;
+    deltay_pix_interp.resize(4);
+
     // Loop over all visibility rows to process.
     for (Int inx=rbeg; inx<=rend; ++inx) {
       Int irow = rows[inx];
@@ -399,6 +666,11 @@ namespace LOFAR {
       // Skip channel if data are not needed.
       for (Int visChan=0; visChan<nVisChan; ++visChan) {
         Int gridChan = chanMap_p[visChan];
+
+
+	// !! dirty trick to select all channels
+	chanMap_p[visChan]=0;
+
         if (gridChan >= 0  &&  gridChan < nGridChan) {
           // Determine the grid position from the UV coordinates in wavelengths.
           Double recipWvl = vbs.freq_p[visChan] / C::c;
@@ -410,6 +682,12 @@ namespace LOFAR {
           ///if (diffx < 0) diffx += 1;
           Double diffy = locy - posy;
           ///if (diffy < 0) diffy += 1;
+
+	  Weights_Lin_Interp[0]=(1.-diffx)*(1.-diffy);
+	  Weights_Lin_Interp[1]=(1.-diffx)*diffy;
+	  Weights_Lin_Interp[2]=diffx*(1.-diffy);
+	  Weights_Lin_Interp[3]=diffx*diffy;
+
           Int offx = SynthesisUtils::nint (diffx * sampx); // location in
           Int offy = SynthesisUtils::nint (diffy * sampy); // oversampling
           ///          cout<<"  pos= ["<<posx<<", "<<posy<<"]"
@@ -435,9 +713,11 @@ namespace LOFAR {
             // Handle a visibility if not flagged.
             for (Int ipol=0; ipol<nVisPol; ++ipol) {
               if (! flagPtr[ipol]) {
-                visPtr[ipol] = Complex(0,0);
+		visPtr[ipol] = Complex(0,0);
               }
             }
+	    //for (Int w=0; w<4; ++w) {
+	    //  Double weight_interp(Weights_Lin_Interp[w]);
             for (Int ipol=0; ipol<nVisPol; ++ipol) {
               if (! flagPtr[ipol]) {
                 // Map to grid polarization. Only use pol if needed.
@@ -453,31 +733,160 @@ namespace LOFAR {
                                                   (locy+sy)*nGridX + locx-supx;
                     // Get pointers to the first element to use in the 4
                     // convolution functions for this channel,pol.
-                    const Complex* __restrict__ cf[4];
+
+		    // fast version
+                    const Complex* __restrict__ cf[1];
                     Int cfoff = (offy + sy*fsampy)*nConvX + offx - fsupx*fsampx;
-                    for (int i=0; i<4; ++i) {
-                      cf[i] = (*cfs.vdata)[gridChan][i][ipol].data() + cfoff;
-                    }
+		    cf[0] = (*cfs.vdata)[gridChan][0][0].data() + cfoff;
                     for (Int sx=-fsupx; sx<=fsupx; ++sx) {
-                      // Loop over polarizations to correct for leakage.
-                      for (Int i=0; i<nGridPol; ++i) {
-                        ///                        cout<<"cf="<< cf[i]-(*cfs.vdata)[gridChan][ipol][i].data()<<',';
-                        visPtr[i] += *gridPtr * *cf[i];
-                        cf[i] += fsampx;
-                      }
-                      ///                      cout<<"  g="<<gridPtr-grid.data()<<' '<<nvalue<<endl;
+		      visPtr[ipol] += *gridPtr * *cf[0];
+		      cf[0] += fsampx;
                       gridPtr++;
                     }
+
+		    // // Full version
+                    // const Complex* __restrict__ cf[4];
+                    // Int cfoff = (offy + sy*fsampy)*nConvX + offx - fsupx*fsampx;
+                    // for (int i=0; i<4; ++i) {
+                    //   cf[i] = (*cfs.vdata)[gridChan][i][ipol].data() + cfoff;
+                    // }
+                    // for (Int sx=-fsupx; sx<=fsupx; ++sx) {
+                    //   for (Int i=0; i<nVisPol; ++i) {
+                    //     visPtr[i] += *gridPtr * *cf[i];
+                    //     cf[i] += fsampx;
+                    //   }
+                    //   gridPtr++;
+                    // }
+
                   }
                 } // end if gridPol
               } // end if !flagPtr
             } // end for ipol
           } // end if ongrid
         } // end if gridChan
+	//}
       } // end for visChan
     } // end for inx
   }
 
+  // void LofarVisResampler::lofarGridToData(LofarVBStore& vbs,
+  //                                         const Array<Complex>& grid,
+  //                                         const Vector<uInt>& rows,
+  //                                         Int rbeg, Int rend,
+  //                                         LofarCFStore& cfs)
+  // {
+  //   // grid[nx,ny,np,nf]
+  //   // vbs.data[np,nf,nrow]
+  //   // cfs[nmx,nmy,nf,ncx,ncy]   (mueller,freq,convsize)
+
+  //   // Get size of convolution functions.
+  //   Int nConvX = (*(cfs.vdata))[0][0][0].shape()[0];
+  //   Int nConvY = (*(cfs.vdata))[0][0][0].shape()[1];
+  //   // Get size of grid.
+  //   Int nGridX    = grid.shape()[0];
+  //   Int nGridY    = grid.shape()[1];
+  //   Int nGridPol  = grid.shape()[2];
+  //   Int nGridChan = grid.shape()[3];
+  //   // Get visibility data size.
+  //   Int nVisPol   = vbs.flagCube_p.shape()[0];
+  //   Int nVisChan  = vbs.flagCube_p.shape()[1];
+  //   // Get oversampling and support size.
+  //   Int sampx = SynthesisUtils::nint (cfs.sampling[0]);
+  //   Int sampy = SynthesisUtils::nint (cfs.sampling[1]);
+  //   Int supx = cfs.xSupport[0];
+  //   Int supy = cfs.ySupport[0];
+  //   ///AlwaysAssert ((2*supx+1)*sampx == nConvX, AipsError);
+  //   ///AlwaysAssert ((2*supy+1)*sampy == nConvY, AipsError);
+
+  //   // Loop over all visibility rows to process.
+  //   for (Int inx=rbeg; inx<=rend; ++inx) {
+  //     Int irow = rows[inx];
+  //     const Double*  __restrict__ uvwPtr   = vbs.uvw_p.data() + irow*3;
+  //     // Loop over all channels in the visibility data.
+  //     // Map the visibility channel to the grid channel.
+  //     // Skip channel if data are not needed.
+  //     for (Int visChan=0; visChan<nVisChan; ++visChan) {
+  //       Int gridChan = chanMap_p[visChan];
+  //       if (gridChan >= 0  &&  gridChan < nGridChan) {
+  //         // Determine the grid position from the UV coordinates in wavelengths.
+  //         Double recipWvl = vbs.freq_p[visChan] / C::c;
+  //         Double posx = uvwScale_p[0] * uvwPtr[0] * recipWvl + offset_p[0];
+  //         Double posy = uvwScale_p[1] * uvwPtr[1] * recipWvl + offset_p[1];
+  //         Int locx = SynthesisUtils::nint (posx);    // location in grid
+  //         Int locy = SynthesisUtils::nint (posy);
+  //         Double diffx = locx - posx;
+  //         ///if (diffx < 0) diffx += 1;
+  //         Double diffy = locy - posy;
+  //         ///if (diffy < 0) diffy += 1;
+  //         Int offx = SynthesisUtils::nint (diffx * sampx); // location in
+  //         Int offy = SynthesisUtils::nint (diffy * sampy); // oversampling
+  //         ///          cout<<"  pos= ["<<posx<<", "<<posy<<"]"
+  //         ///              <<", loc= ["<<locx<<", "<<locy<<"]"
+  //         ///              <<", off= ["<<offx<<", "<<offy<<"]" << endl;
+  //         offx += (nConvX-1)/2;
+  //         offy += (nConvY-1)/2;
+  //         // Scaling with frequency is not necessary (according to Cyril).
+  //         Double freqFact = 1;   // = cfFreq / vbs.freq_p[visChan];
+  //         Int fsampx = SynthesisUtils::nint (sampx * freqFact);
+  //         Int fsampy = SynthesisUtils::nint (sampy * freqFact);
+  //         Int fsupx  = SynthesisUtils::nint (supx / freqFact);
+  //         Int fsupy  = SynthesisUtils::nint (supy / freqFact);
+
+  //         // Only use visibility point if the full support is within grid.
+  //         if (locx-supx >= 0  &&  locx+supx < nGridX  &&
+  //             locy-supy >= 0  &&  locy+supy < nGridY) {
+  //           ///            cout << "in grid"<<endl;
+  //           // Get pointer to data and flags for this channel.
+  //           Int doff = (irow * nVisChan + visChan) * nVisPol;
+  //           Complex* __restrict__ visPtr  = vbs.visCube_p.data()  + doff;
+  //           const Bool*    __restrict__ flagPtr = vbs.flagCube_p.data() + doff;
+  //           // Handle a visibility if not flagged.
+  //           for (Int ipol=0; ipol<nVisPol; ++ipol) {
+  //             if (! flagPtr[ipol]) {
+  //               visPtr[ipol] = Complex(0,0);
+  //             }
+  //           }
+  //           for (Int ipol=0; ipol<nVisPol; ++ipol) {
+  //             if (! flagPtr[ipol]) {
+  //               // Map to grid polarization. Only use pol if needed.
+  //               Int gridPol = polMap_p(ipol);
+  //               if (gridPol >= 0  &&  gridPol < nGridPol) {
+  //                 /// Complex norm(0,0);
+  //                 // Get the offset in the grid data array.
+  //                 Int goff = (gridChan*nGridPol + gridPol) * nGridX * nGridY;
+  //                 // Loop over the scaled support.
+  //                 for (Int sy=-fsupy; sy<=fsupy; ++sy) {
+  //                   // Get the pointer in the grid for the first x in this y.
+  //                   const Complex* __restrict__ gridPtr = grid.data() + goff +
+  //                                                 (locy+sy)*nGridX + locx-supx;
+  //                   // Get pointers to the first element to use in the 4
+  //                   // convolution functions for this channel,pol.
+  //                   const Complex* __restrict__ cf[4];
+  //                   Int cfoff = (offy + sy*fsampy)*nConvX + offx - fsupx*fsampx;
+  //                   for (int i=0; i<4; ++i) {
+  //                     cf[i] = (*cfs.vdata)[gridChan][i][ipol].data() + cfoff;
+  //                   }
+  //                   for (Int sx=-fsupx; sx<=fsupx; ++sx) {
+  //                     // Loop over polarizations to correct for leakage.
+  //                     for (Int i=0; i<nVisPol; ++i) {
+  //                       ///                        cout<<"cf="<< cf[i]-(*cfs.vdata)[gridChan][ipol][i].data()<<',';
+  //                       visPtr[i] += *gridPtr * *cf[i];
+  //                       cf[i] += fsampx;
+  //                     }
+  //                     ///                      cout<<"  g="<<gridPtr-grid.data()<<' '<<nvalue<<endl;
+  //                     gridPtr++;
+  //                   }
+  //                 }
+  //               } // end if gridPol
+  //             } // end if !flagPtr
+  //           } // end for ipol
+  //         } // end if ongrid
+  //       } // end if gridChan
+  //     } // end for visChan
+  //   } // end for inx
+  // }
+
+
   /*
   void LofarVisResampler::lofarGridToData(LofarVBStore& vbs,
                                           const Array<Complex>& grid,
@@ -536,14 +945,14 @@ namespace LOFAR {
       //store2(im,"Aterm-ch"+String::toString(i)+".img");
       }
     }
-    
-    
+
+
     // Vector<Double> UVWSCALE_MOI(uvwScale_p*1.4);
     // Vector<Double> UVWOFF_MOI(offset_p);
     // UVWOFF_MOI(0)=320;
     // UVWOFF_MOI(1)=320;
     // UVWOFF_MOI(2)=0;
-    
+
     Double *freq=vbs.freq_p.getStorage(Dummy);
 
     Matrix<Float>&  imagingWeight=vbs.imagingWeight_p;
@@ -557,7 +966,7 @@ namespace LOFAR {
     cacheAxisIncrements(grid.shape().asVector(), gridInc_p);
     cacheAxisIncrements(cfShape, cfInc_p);
 
-    for(Int inx=rbeg; inx<rend; inx++) {
+    for(Int inx=rbeg; inx<=rend; inx++) {
         Int irow = rows[inx];
 
 	for (Int ichan=0; ichan < nDataChan; ichan++) {
@@ -574,7 +983,7 @@ namespace LOFAR {
 	    uvwScale_p,offset_p,sampling);
 	    //sgrid(pos,loc,off,phasor,irow,uvw,dphase_p[irow],freq[ichan],
 	//	  UVWSCALE_MOI,UVWOFF_MOI,sampling);
-	    
+
 
 
 	    iloc[2]=max(0, min(nw-1, loc[2]));
@@ -651,31 +1060,36 @@ namespace LOFAR {
     IPosition start(vbDataShape), last(vbDataShape);
     start=0; start(2)=rbeg;
     last(2)=rend; //last=last-1;
+    //cout<<"vbDataShape "<<vbDataShape<<" ,last "<<" ,start "<<start<<" ,vbs.modelCube_p "<< (vbs.modelCube_p).shape() << endl;
 
     //cout<<"//////////////////// Compuute residual!!!!!!"<<"vbs.useCorrected_p"<<vbs.useCorrected_p<<endl;
 
+
+
     if (!vbs.useCorrected_p)
       {
     	for(uInt ichan = start(0); ichan < last(0); ichan++)
     	  for(uInt ipol = start(1); ipol < last(1); ipol++)
     	    for(uInt irow = start(2); irow < last(2); irow++)
     	      vbs.modelCube_p(ichan,ipol,irow) = vbs.modelCube_p(ichan,ipol,irow) - vbs.visCube_p(ichan,ipol,irow);
-	
+
       }
     else
       {
-    	for(uInt ichan = start(0); ichan < last(0); ichan++)
-    	  for(uInt ipol = start(1); ipol < last(1); ipol++)
-    	    for(uInt irow = start(2); irow < last(2); irow++){
+    	for(uInt ichan = start(0); ichan < last(0); ichan++){
+	  //cout<< ichan << vbs.modelCube_p(ichan,0,347) <<vbs.correctedCube_p(ichan,0,347)<< endl;
+    	  for(uInt ipol = start(1); ipol < last(1); ipol++){
+	    for(uInt irow = start(2); irow < last(2); irow++){
     	      //cout<<"===="<<endl;
 	      //if(!(abs(vbs.modelCube_p(ichan,ipol,irow))==0.)){cout<<ipol<<" "<<ichan<<" "<<irow<<" "<<vbs.modelCube_p(ichan,ipol,irow)<<"  "<<vbs.correctedCube_p(ichan,ipol,irow)<<endl;};
-    	      
+
 	      //if(!(abs(vbs.modelCube_p(ichan,ipol,irow))==0.)){cout<<"data "<<ipol<<" "<<ichan<<" "<<irow<<" "<<vbs.modelCube_p(ichan,ipol,irow)<<" "<<vbs.correctedCube_p(ichan,ipol,irow)<<endl;};
-	 
+
     	      vbs.modelCube_p(ichan,ipol,irow) = vbs.modelCube_p(ichan,ipol,irow) - vbs.correctedCube_p(ichan,ipol,irow);
     	      //cout<<vbs.modelCube_p(ichan,ipol,irow)<<"  "<<vbs.modelCube_p(ichan,ipol,irow)<<"  "<<vbs.correctedCube_p(ichan,ipol,irow)<<endl;
-    	    };
-	
+    	    }
+	  }
+	}
       }
 
     // if (!vbs.useCorrected_p)
@@ -684,7 +1098,7 @@ namespace LOFAR {
     // 	  for(uInt ipol = start(1); ipol < last(1); ipol++)
     // 	    for(uInt irow = start(2); irow < last(2); irow++)
     // 	      vbs.modelCube_p(ichan,ipol,irow) = vbs.modelCube_p(ichan,ipol,irow) - vbs.visCube_p(ichan,ipol,irow);
-	
+
     //   }
     // else
     //   {
@@ -696,7 +1110,7 @@ namespace LOFAR {
     // 	      vbs.correctedCube_p(ichan,ipol,irow) = vbs.correctedCube_p(ichan,ipol,irow) - vbs.modelCube_p(ichan,ipol,irow);
     // 	      //cout<<vbs.correctedCube_p(ichan,ipol,irow)<<"  "<< vbs.correctedCube_p(ichan,ipol,irow)<<"  "<< vbs.modelCube_p(ichan,ipol,irow)<<endl;
     // 	    };
-	
+
     //   }
 
 
diff --git a/CEP/Imager/LofarFT/src/LofarVisResamplerOld.cc b/CEP/Imager/LofarFT/src/LofarVisResamplerOld.cc
new file mode 100644
index 0000000000000000000000000000000000000000..15808792852dcfdc173ed1373d15ced6aa5ead86
--- /dev/null
+++ b/CEP/Imager/LofarFT/src/LofarVisResamplerOld.cc
@@ -0,0 +1,741 @@
+//# LofarVisResamplerOld.cc: Implementation of the LofarVisResamplerOld class
+//#
+//# Copyright (C) 2011
+//# ASTRON (Netherlands Institute for Radio Astronomy)
+//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
+//#
+//# This file is part of the LOFAR software suite.
+//# The LOFAR software suite 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 3 of the License, or
+//# (at your option) any later version.
+//#
+//# The LOFAR software suite 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 the LOFAR software suite. If not, see <http://www.gnu.org/licenses/>.
+//#
+//# $Id$
+
+#include <LofarFT/LofarVisResamplerOld.h>
+#include <synthesis/MeasurementComponents/Utils.h>
+#include <coordinates/Coordinates/SpectralCoordinate.h>
+#include <coordinates/Coordinates/CoordinateSystem.h>
+#include<cassert>
+
+namespace LOFAR {
+
+    // Instantiate both templates.
+  template
+  void LofarVisResamplerOld::DataToGridImpl_p(Array<DComplex>& grid, LofarVBStore& vbs,
+                                           const Vector<uInt>& rows,
+                                           Int rbeg, Int rend,
+					Matrix<Double>& sumwt,const Bool& dopsf,
+					LofarCFStore& cfs) __restrict__;
+  template
+  void LofarVisResamplerOld::DataToGridImpl_p(Array<Complex>& grid, LofarVBStore& vbs,
+                                           const Vector<uInt>& rows,
+                                           Int rbeg, Int rend,
+					Matrix<Double>& sumwt,const Bool& dopsf,
+					LofarCFStore& cfs) __restrict__;
+
+
+  template <class T>
+  void LofarVisResamplerOld::DataToGridImpl_p(Array<T>& grid,  LofarVBStore& vbs,
+                                           const Vector<uInt>& rows,
+                                           Int rbeg, Int rend,
+                                           Matrix<Double>& sumwt,
+                                           const Bool& dopsf,
+                                           LofarCFStore& cfs) __restrict__
+  {
+    // grid[nx,ny,np,nf]
+    // vbs.data[np,nf,nrow]
+    // cfs[nmx,nmy,nf,ncx,ncy]   (mueller,freq,convsize)
+
+    // Get size of convolution functions.
+    Int nConvX = (*(cfs.vdata))[0][0][0].shape()[0];
+    Int nConvY = (*(cfs.vdata))[0][0][0].shape()[1];
+    // Get size of grid.
+    Int nGridX    = grid.shape()[0];
+    Int nGridY    = grid.shape()[1];
+    Int nGridPol  = grid.shape()[2];
+    Int nGridChan = grid.shape()[3];
+    // Get visibility data size.
+    Int nVisPol   = vbs.flagCube_p.shape()[0];
+    Int nVisChan  = vbs.flagCube_p.shape()[1];
+    // Get oversampling and support size.
+    Int sampx = SynthesisUtils::nint (cfs.sampling[0]);
+    Int sampy = SynthesisUtils::nint (cfs.sampling[1]);
+    Int supx = cfs.xSupport[0];
+    Int supy = cfs.ySupport[0];
+    ///AlwaysAssert ((2*supx+1)*sampx == nConvX, AipsError);
+    ///AlwaysAssert ((2*supy+1)*sampy == nConvY, AipsError);
+
+    Double* __restrict__ sumWtPtr = sumwt.data();
+    Complex psfValues[4];
+    psfValues[0] = psfValues[1] = psfValues[2] = psfValues[3] = Complex(1,0);
+
+    // Loop over all visibility rows to process.
+    for (Int inx=rbeg; inx<=rend; ++inx) {
+      Int irow = rows[inx];
+      const Double*  __restrict__ uvwPtr   = vbs.uvw_p.data() + irow*3;
+      const Float*   __restrict__ imgWtPtr = vbs.imagingWeight_p.data() +
+                                             irow * nVisChan;
+      // Loop over all channels in the visibility data.
+      // Map the visibility channel to the grid channel.
+      // Skip channel if data are not needed.
+      for (Int visChan=0; visChan<nVisChan; ++visChan) {
+        Int gridChan = chanMap_p[visChan];
+        if (gridChan >= 0  &&  gridChan < nGridChan) {
+          // Determine the grid position from the UV coordinates in wavelengths.
+          Double recipWvl = vbs.freq_p[visChan] / C::c;
+          Double posx = uvwScale_p[0] * uvwPtr[0] * recipWvl + offset_p[0];
+          Double posy = uvwScale_p[1] * uvwPtr[1] * recipWvl + offset_p[1];
+          Int locx = SynthesisUtils::nint (posx);    // location in grid
+          Int locy = SynthesisUtils::nint (posy);
+          Double diffx = locx - posx;
+          ///if (diffx < 0) diffx += 1;
+          Double diffy = locy - posy;
+          ///if (diffy < 0) diffy += 1;
+          Int offx = SynthesisUtils::nint (diffx * sampx); // location in
+          Int offy = SynthesisUtils::nint (diffy * sampy); // oversampling
+          ///          cout<<"  pos= ["<<posx<<", "<<posy<<"]"
+          ///              <<", loc= ["<<locx<<", "<<locy<<"]"
+          ///              <<", off= ["<<offx<<", "<<offy<<"]" << endl;
+          offx += (nConvX-1)/2;
+          offy += (nConvY-1)/2;
+          // Scaling with frequency is not necessary (according to Cyril).
+          Double freqFact = 1;   // = cfFreq / vbs.freq_p[visChan];
+          Int fsampx = SynthesisUtils::nint (sampx * freqFact);
+          Int fsampy = SynthesisUtils::nint (sampy * freqFact);
+          Int fsupx  = SynthesisUtils::nint (supx / freqFact);
+          Int fsupy  = SynthesisUtils::nint (supy / freqFact);
+
+          // Only use visibility point if the full support is within grid.
+          if (locx-supx >= 0  &&  locx+supx < nGridX  &&
+              locy-supy >= 0  &&  locy+supy < nGridY) {
+            ///            cout << "in grid"<<endl;
+            // Get pointer to data and flags for this channel.
+            Int doff = (irow * nVisChan + visChan) * nVisPol;
+            const Complex* __restrict__ visPtr  = vbs.visCube_p.data()  + doff;
+            const Bool*    __restrict__ flagPtr = vbs.flagCube_p.data() + doff;
+            if (dopsf) {
+              visPtr = psfValues;
+            }
+            // Handle a visibility if not flagged.
+            for (Int ipol=0; ipol<nVisPol; ++ipol) {
+              if (! flagPtr[ipol]) {
+                // Map to grid polarization. Only use pol if needed.
+                Int gridPol = polMap_p(ipol);
+                if (gridPol >= 0  &&  gridPol < nGridPol) {
+                  // Get the offset in the grid data array.
+                  Int goff = (gridChan*nGridPol + gridPol) * nGridX * nGridY;
+                  // Loop over the scaled support.
+                  for (Int sy=-fsupy; sy<=fsupy; ++sy) {
+                    // Get the pointer in the grid for the first x in this y.
+                    T* __restrict__ gridPtr = grid.data() + goff +
+                                              (locy+sy)*nGridX + locx-supx;
+                    // Get pointers to the first element to use in the 4
+                    // convolution functions for this channel,pol.
+                    const Complex* __restrict__ cf[4];
+                    Int cfoff = (offy + sy*fsampy)*nConvX + offx - fsupx*fsampx;
+                    for (int i=0; i<4; ++i) {
+                      cf[i] = (*cfs.vdata)[gridChan][ipol][i].data() + cfoff;
+                    }
+                    for (Int sx=-fsupx; sx<=fsupx; ++sx) {
+                      // Loop over polarizations to correct for leakage.
+                      Complex polSum(0,0);
+                      for (Int i=0; i<nVisPol; ++i) {
+                        ///                        cout<<"cf="<< cf[i]-(*cfs.vdata)[gridChan][ipol][i].data()<<',';
+                        polSum += visPtr[i] * *cf[i];
+                        cf[i] += fsampx;
+                      }
+                      ///                      cout<<"  g="<<gridPtr-grid.data()<<' '<<visPtr[i]<<endl;
+		      polSum *= *imgWtPtr;
+                      *gridPtr++ += polSum;
+                    }
+                  }
+                  sumWtPtr[gridPol+gridChan*nGridPol] += *imgWtPtr;
+                } // end if gridPol
+              } // end if !flagPtr
+            } // end for ipol
+          } // end if ongrid
+        } // end if gridChan
+        imgWtPtr++;
+      } // end for visChan
+    } // end for inx
+  }
+
+  /*
+  template <class T>
+  void LofarVisResamplerOld::DataToGridImpl_p(Array<T>& grid,  LofarVBStore& vbs,
+                                           const Vector<uInt>& rows,
+                                           Int rbeg, Int rend,
+                                           Matrix<Double>& sumwt,
+                                           const Bool& dopsf,
+                                           LofarCFStore& cfs)
+  {
+    Vector<Float> sampling(2);
+    Vector<Double> pos(3);
+    Vector<Int> support(2),loc(3), off(3), iloc(4),tiloc(4);
+    Vector<Int> scaledSampling(2), scaledSupport(2);
+    Vector<Int> igrdpos(4);
+    //Float sampling[2];
+    //Double pos[3]
+    //  Int support[2], loc[3], off[3], iloc[4], tiloc[4];
+    //Int scaledSampling[2], scledSupport[2];
+    //Int igrdpos[4];
+
+    Double norm=0.0;
+    Complex phasor, nvalue, wt;
+    // A conv. fucntion per pol.
+    // For time being only one channel.
+    Vector<Int> cfShape(4,1);
+    cfShape[0] = (*(cfs.vdata))[0][0][0].shape()[0];
+    cfShape[1] = (*(cfs.vdata))[0][0][0].shape()[1];
+    Vector<Int> convOrigin = (cfShape-1)/2;
+
+    Int nx = grid.shape()[0];
+    Int ny = grid.shape()[1];
+    Int nw = 1;
+    Int nGridPol = grid.shape()[2];
+    Int nGridChan = grid.shape()[3];
+
+    Int nDataPol  = vbs.flagCube_p.shape()[0];
+    Int nDataChan = vbs.flagCube_p.shape()[1];
+
+    sampling[0] = sampling[1] = cfs.sampling[0];
+    support[0] = cfs.xSupport[0];
+    support[1] = cfs.ySupport[0];
+
+    T* __restrict__ gridStore = grid.data();
+    const Int * __restrict__ iPosPtr = igrdpos.data();
+    // const Complex* __restrict__ convFuncV[4];
+    // for (int i=0; i<4; ++i) {
+    //   convFuncV[i] = (*(cfs.vdata))[0][i][i].data();
+    // }
+
+    const Complex* __restrict__ convFuncV[4][4];
+    for (int i=0; i<4; ++i) {
+      for (int j=0; j<4; ++j) {
+        const Array<Complex>& conv = (*(cfs.vdata))[0][j][i];
+        if (conv.empty()) {
+          convFuncV[j][i] = 0;
+        } else {
+          convFuncV[j][i] = conv.data();
+        }
+      //Matrix<Complex> im((*(cfs.vdata))[0][i][i]);
+      //store2(im,"Aterm-ch"+String::toString(i)+".img");
+      }
+    }
+    //cout<<"cfs.vdata= "<<&cfs<<endl;
+      
+    const Double *freq  = vbs.freq_p.data();
+
+    // Cache increment values for adding to grid in gridInc.  This is
+    // supplied to addTo4DArray later.
+    cacheAxisIncrements(grid.shape().asVector(), gridInc_p);
+    // Cache the CF related increments internally in
+    // VisibilityResamplerBase for use in getFrom4DArray later.
+    cacheAxisIncrements(cfShape, cfInc_p);
+
+    const Bool * __restrict__ flagCube_ptr=vbs.flagCube_p.data();
+    const Float * __restrict__ imgWts_ptr = vbs.imagingWeight_p.data();
+    const Complex * __restrict__ visCube_ptr = vbs.visCube_p.data();
+    Double * __restrict__ sumWt_ptr = sumwt.data();
+
+    // {
+    //   IPosition tt(4);
+    //   for(tt(0)=0;tt(0)<grid.shape()(0);tt(0)++)
+    // 	for(tt(1)=0;tt(1)<grid.shape()(1);tt(1)++)
+    // 	  for(tt(2)=0;tt(2)<grid.shape()(2);tt(2)++)
+    // 	    for(tt(3)=0;tt(3)<grid.shape()(3);tt(3)++)
+    // 	      grid(tt)*=1.0;
+    // }
+
+    for(Int inx=rbeg; inx< rend; inx++){
+        Int irow = rows[inx];
+	for(Int ichan=0; ichan< nDataChan; ichan++){
+	    Int achan=chanMap_p[ichan];
+
+	    if((achan>=0) && (achan<nGridChan)) {
+
+	      scaledSampling[0] = SynthesisUtils::nint(sampling[0]);
+	      scaledSampling[1] = SynthesisUtils::nint(sampling[1]);
+	      scaledSupport[0]  = support[0];
+	      scaledSupport[1]  = support[1];
+
+	      sgrid(pos,loc,off, phasor, irow,
+		    vbs.uvw_p, dphase_p[irow], freq[ichan],
+		    uvwScale_p, offset_p, sampling);
+
+///	      cout<<"  pos= ["<<pos[0]<<", "<<pos[1]<<"]"
+///                  <<", loc= ["<<loc[0]<<", "<<loc[1]<<", "<<loc[2]<<"]"
+///                  <<", off= ["<<off[0]<<", "<<off[1]<<", "<<off[2]<<"] "
+///                  << nx << ' '<<ny<<' '<<support[0]<<' '<<support[1]
+///                  <<endl;
+
+	      iloc[2]=max(0, min(nw-1, loc[2]));
+
+	      //cout<< nx <<" "<<ny<<" "<<nw<<" "<<loc<<" "<< support<<" "<<onGrid(nx, ny, nw, loc, support)<<endl;
+	      //assert(false);
+	      if (onGrid(nx, ny, nw, loc, support)) {
+///                cout << "on grid"<<endl;
+
+		for(Int ipol=0; ipol< nDataPol; ipol++) {
+		  //		  if((!flagCube(ipol,ichan,irow))){
+		  if((!(*(flagCube_ptr + ipol + ichan*nDataPol + irow*nDataPol*nDataChan)))){
+		    Int apol=polMap_p(ipol);
+		    if ((apol>=0) && (apol<nGridPol)) {
+		      igrdpos[2]=apol; igrdpos[3]=achan;
+
+		      norm=0.0;
+		      //int ConjPlane = cfMap_p[ipol];
+		      //int PolnPlane = conjCFMap_p[ipol];
+
+		      iloc[3]=ipol; //PolnPlane;
+
+		      //cout<<"Weight= "<<Complex(*(imgWts_ptr + ichan + irow*nDataChan))<<", Vis= "<<(*(visCube_ptr+ipol+ichan*nDataPol+irow*nDataChan*nDataPol)*phasor)<<endl;
+
+
+		      if(dopsf)  nvalue=Complex(*(imgWts_ptr + ichan + irow*nDataChan));
+		      else	 nvalue= *(imgWts_ptr+ichan+irow*nDataChan)*
+		      		   (*(visCube_ptr+ipol+ichan*nDataPol+irow*nDataChan*nDataPol)*phasor);
+
+
+		      //cout<<"nvalue: "<<nvalue<<endl;
+
+		      for(Int iy=-scaledSupport[1]; iy <= scaledSupport[1]; iy++)
+			{
+			  iloc[1]=(Int)(scaledSampling[1]*iy+off[1]);
+			  igrdpos[1]=loc[1]+iy;
+			  for(Int ix=-scaledSupport[0]; ix <= scaledSupport[0]; ix++)
+			    {
+			      iloc[0]=(Int)(scaledSampling[0]*ix+off[0]);
+			      tiloc=iloc;
+                              if (reindex(iloc,tiloc, 0, 1,
+                                          convOrigin, cfShape)) {
+                                //cout << "reindexed"<<iloc<<tiloc<<cfShape<<endl;
+
+                                for (int ic=0; ic<4; ++ic) {
+                                  if (convFuncV[iloc[3]][ic]) {
+				    //cout<<ic<<" "<<iloc[3]<<" "<<tiloc[0]<<endl;
+                                    wt = convFuncV[iloc[3]][ic][tiloc[1]*cfInc_p[1]+tiloc[0]];
+///                                    cout<<"cf="<<iloc[3]<<' '<<tiloc[1]*cfInc_p[1]+tiloc[0]<<',';
+				    //cout<<wt<<endl;
+				    //wt=convFuncV[iloc[3]][tiloc[1]*cfInc_p[1]+tiloc[0]];
+				//wt = (*(cfs.vdata))[0][iloc[3]][iloc[3]](tiloc[0],tiloc[1]);
+                                ///                              wt = getFrom4DArray(convFuncV, tiloc,cfInc_p);
+				    igrdpos[0]=loc[0]+ix;
+				//				  grid(igrdpos) += nvalue*wt;
+				    //cout<<igrdpos[0]<<endl;
+				    //cout<<"ipol="<<ipol<<", iloc[1]="<<iloc[1]<<", cfInc_p[1]="<<cfInc_p[1]<<", iloc[0]="<<iloc[0]<<", wt="<<wt<<", vis="<<nvalue<<endl;
+                                //assert (wt > 1e-10  &&  wt < 1);
+				// The following uses raw index on the 4D grid
+///                                    cout << "add " << igrdpos<< gridInc_p
+///                                         << igrdpos[0] + igrdpos[1]*gridInc_p[1] + igrdpos[2]*gridInc_p[2] +igrdpos[3]*gridInc_p[3]<<endl;
+                                    addTo4DArray(gridStore,iPosPtr,gridInc_p, nvalue,wt);
+				//				  norm+=real(wt);
+				  }
+				}
+                              }
+			    }
+			}
+		      //		      sumwt(apol,achan)+=imagingWeight(ichan,irow);// *norm;
+		      *(sumWt_ptr+apol+achan*nGridChan)+= *(imgWts_ptr+ichan+irow*nDataChan);
+		    }
+		  }
+		}
+	      }
+	    }
+        }
+    }
+  }
+  */
+
+
+  //
+  //-----------------------------------------------------------------------------------
+  // Re-sample VisBuffer from a regular grid (griddedData) (a.k.a. de-gridding)
+  //
+  void LofarVisResamplerOld::lofarGridToData(LofarVBStore& vbs,
+                                          const Array<Complex>& grid,
+                                          const Vector<uInt>& rows,
+                                          Int rbeg, Int rend,
+                                          LofarCFStore& cfs)
+  {
+    // grid[nx,ny,np,nf]
+    // vbs.data[np,nf,nrow]
+    // cfs[nmx,nmy,nf,ncx,ncy]   (mueller,freq,convsize)
+
+    // Get size of convolution functions.
+    Int nConvX = (*(cfs.vdata))[0][0][0].shape()[0];
+    Int nConvY = (*(cfs.vdata))[0][0][0].shape()[1];
+    // Get size of grid.
+    Int nGridX    = grid.shape()[0];
+    Int nGridY    = grid.shape()[1];
+    Int nGridPol  = grid.shape()[2];
+    Int nGridChan = grid.shape()[3];
+    // Get visibility data size.
+    Int nVisPol   = vbs.flagCube_p.shape()[0];
+    Int nVisChan  = vbs.flagCube_p.shape()[1];
+    // Get oversampling and support size.
+    Int sampx = SynthesisUtils::nint (cfs.sampling[0]);
+    Int sampy = SynthesisUtils::nint (cfs.sampling[1]);
+    Int supx = cfs.xSupport[0];
+    Int supy = cfs.ySupport[0];
+    ///AlwaysAssert ((2*supx+1)*sampx == nConvX, AipsError);
+    ///AlwaysAssert ((2*supy+1)*sampy == nConvY, AipsError);
+
+    // Loop over all visibility rows to process.
+    for (Int inx=rbeg; inx<=rend; ++inx) {
+      Int irow = rows[inx];
+      const Double*  __restrict__ uvwPtr   = vbs.uvw_p.data() + irow*3;
+      // Loop over all channels in the visibility data.
+      // Map the visibility channel to the grid channel.
+      // Skip channel if data are not needed.
+      for (Int visChan=0; visChan<nVisChan; ++visChan) {
+        Int gridChan = chanMap_p[visChan];
+        if (gridChan >= 0  &&  gridChan < nGridChan) {
+          // Determine the grid position from the UV coordinates in wavelengths.
+          Double recipWvl = vbs.freq_p[visChan] / C::c;
+          Double posx = uvwScale_p[0] * uvwPtr[0] * recipWvl + offset_p[0];
+          Double posy = uvwScale_p[1] * uvwPtr[1] * recipWvl + offset_p[1];
+          Int locx = SynthesisUtils::nint (posx);    // location in grid
+          Int locy = SynthesisUtils::nint (posy);
+          Double diffx = locx - posx;
+          ///if (diffx < 0) diffx += 1;
+          Double diffy = locy - posy;
+          ///if (diffy < 0) diffy += 1;
+          Int offx = SynthesisUtils::nint (diffx * sampx); // location in
+          Int offy = SynthesisUtils::nint (diffy * sampy); // oversampling
+          ///          cout<<"  pos= ["<<posx<<", "<<posy<<"]"
+          ///              <<", loc= ["<<locx<<", "<<locy<<"]"
+          ///              <<", off= ["<<offx<<", "<<offy<<"]" << endl;
+          offx += (nConvX-1)/2;
+          offy += (nConvY-1)/2;
+          // Scaling with frequency is not necessary (according to Cyril).
+          Double freqFact = 1;   // = cfFreq / vbs.freq_p[visChan];
+          Int fsampx = SynthesisUtils::nint (sampx * freqFact);
+          Int fsampy = SynthesisUtils::nint (sampy * freqFact);
+          Int fsupx  = SynthesisUtils::nint (supx / freqFact);
+          Int fsupy  = SynthesisUtils::nint (supy / freqFact);
+
+          // Only use visibility point if the full support is within grid.
+          if (locx-supx >= 0  &&  locx+supx < nGridX  &&
+              locy-supy >= 0  &&  locy+supy < nGridY) {
+            ///            cout << "in grid"<<endl;
+            // Get pointer to data and flags for this channel.
+            Int doff = (irow * nVisChan + visChan) * nVisPol;
+            Complex* __restrict__ visPtr  = vbs.visCube_p.data()  + doff;
+            const Bool*    __restrict__ flagPtr = vbs.flagCube_p.data() + doff;
+            // Handle a visibility if not flagged.
+            for (Int ipol=0; ipol<nVisPol; ++ipol) {
+              if (! flagPtr[ipol]) {
+                visPtr[ipol] = Complex(0,0);
+              }
+            }
+            for (Int ipol=0; ipol<nVisPol; ++ipol) {
+              if (! flagPtr[ipol]) {
+                // Map to grid polarization. Only use pol if needed.
+                Int gridPol = polMap_p(ipol);
+                if (gridPol >= 0  &&  gridPol < nGridPol) {
+                  /// Complex norm(0,0);
+                  // Get the offset in the grid data array.
+                  Int goff = (gridChan*nGridPol + gridPol) * nGridX * nGridY;
+                  // Loop over the scaled support.
+                  for (Int sy=-fsupy; sy<=fsupy; ++sy) {
+                    // Get the pointer in the grid for the first x in this y.
+                    const Complex* __restrict__ gridPtr = grid.data() + goff +
+                                                  (locy+sy)*nGridX + locx-supx;
+                    // Get pointers to the first element to use in the 4
+                    // convolution functions for this channel,pol.
+                    const Complex* __restrict__ cf[4];
+                    Int cfoff = (offy + sy*fsampy)*nConvX + offx - fsupx*fsampx;
+                    for (int i=0; i<4; ++i) {
+                      cf[i] = (*cfs.vdata)[gridChan][i][ipol].data() + cfoff;
+                    }
+                    for (Int sx=-fsupx; sx<=fsupx; ++sx) {
+                      // Loop over polarizations to correct for leakage.
+                      for (Int i=0; i<nGridPol; ++i) {
+                        ///                        cout<<"cf="<< cf[i]-(*cfs.vdata)[gridChan][ipol][i].data()<<',';
+                        visPtr[i] += *gridPtr * *cf[i];
+                        cf[i] += fsampx;
+                      }
+                      ///                      cout<<"  g="<<gridPtr-grid.data()<<' '<<nvalue<<endl;
+                      gridPtr++;
+                    }
+                  }
+                } // end if gridPol
+              } // end if !flagPtr
+            } // end for ipol
+          } // end if ongrid
+        } // end if gridChan
+      } // end for visChan
+    } // end for inx
+  }
+
+  /*
+  void LofarVisResamplerOld::lofarGridToData(LofarVBStore& vbs,
+                                          const Array<Complex>& grid,
+                                          const Vector<uInt>& rows,
+                                          Int rbeg, Int rend,
+                                          LofarCFStore& cfs)
+  {
+    Int nDataChan, nDataPol, nGridPol, nGridChan, nx, ny,nw;
+    Int achan, apol, PolnPlane, ConjPlane;
+    Vector<Float> sampling(2);
+    Vector<Int> support(2),loc(3), off(3), iloc(4),tiloc(4), scaledSampling(2), scaledSupport(2);
+    Vector<Double> pos(3);
+
+    IPosition grdpos(4);
+
+    Complex phasor, nvalue, norm, wt;
+    Vector<Int> cfShape(4,1);
+    cfShape[0] = (*(cfs.vdata))[0][0][0].shape()[0];
+    cfShape[1] = (*(cfs.vdata))[0][0][0].shape()[1];
+    //    Vector<Int> convOrigin = (cfShape-1)/2;
+    Vector<Int> convOrigin = (cfShape-1)/2;
+    Double sinDPA=0.0, cosDPA=1.0;
+
+    nx       = grid.shape()[0]; ny        = grid.shape()[1];
+    nw       = cfShape[2];
+    nGridPol = grid.shape()[2]; nGridChan = grid.shape()[3];
+
+    nDataPol  = vbs.flagCube_p.shape()[0];
+    nDataChan = vbs.flagCube_p.shape()[1];
+
+    sampling[0] = sampling[1] = cfs.sampling[0];
+    support(0) = cfs.xSupport[0];
+    support(1) = cfs.ySupport[0];
+    //
+    // The following code reduces most array accesses to the simplest
+    // possible to improve performance.  However this made no
+    // difference in the run-time performance compared to Vector,
+    // Matrix and Cube indexing.
+    //
+    Bool Dummy;
+    const Complex *gridStore = grid.getStorage(Dummy);
+    Vector<Int> igrdpos(4);
+    const Int *iPosPtr = igrdpos.getStorage(Dummy);
+
+    // Take all Mueller elements into account.
+    const Complex* __restrict__ convFuncV[4][4];
+    for (int i=0; i<4; ++i) {
+      for (int j=0; j<4; ++j) {
+        const Array<Complex>& conv = (*(cfs.vdata))[0][j][i];
+        if (conv.empty()) {
+          convFuncV[j][i] = 0;
+        } else {
+          convFuncV[j][i] = conv.data();
+        }
+      //Matrix<Complex> im((*(cfs.vdata))[0][i][i]);
+      //store2(im,"Aterm-ch"+String::toString(i)+".img");
+      }
+    }
+    
+    
+    // Vector<Double> UVWSCALE_MOI(uvwScale_p*1.4);
+    // Vector<Double> UVWOFF_MOI(offset_p);
+    // UVWOFF_MOI(0)=320;
+    // UVWOFF_MOI(1)=320;
+    // UVWOFF_MOI(2)=0;
+    
+    Double *freq=vbs.freq_p.getStorage(Dummy);
+
+    Matrix<Float>&  imagingWeight=vbs.imagingWeight_p;
+    Matrix<Double>& uvw=vbs.uvw_p;
+    Cube<Complex>&  visCube=vbs.visCube_p;
+    Cube<Bool>&     flagCube=vbs.flagCube_p;
+
+    Vector<Int> gridInc, cfInc;
+
+    //    cacheAxisIncrements(nx,ny,nGridPol, nGridChan);
+    cacheAxisIncrements(grid.shape().asVector(), gridInc_p);
+    cacheAxisIncrements(cfShape, cfInc_p);
+
+    for(Int inx=rbeg; inx<rend; inx++) {
+        Int irow = rows[inx];
+
+	for (Int ichan=0; ichan < nDataChan; ichan++) {
+	  achan=chanMap_p[ichan];
+
+	  if((achan>=0) && (achan<nGridChan)) {
+
+	    scaledSampling[0] = SynthesisUtils::nint(sampling[0]);
+	    scaledSampling[1] = SynthesisUtils::nint(sampling[1]);
+	    scaledSupport[0]  = support[0];
+	    scaledSupport[1]  = support[1];
+
+	    sgrid(pos,loc,off,phasor,irow,uvw,dphase_p[irow],freq[ichan],
+	    uvwScale_p,offset_p,sampling);
+	    //sgrid(pos,loc,off,phasor,irow,uvw,dphase_p[irow],freq[ichan],
+	//	  UVWSCALE_MOI,UVWOFF_MOI,sampling);
+	    
+
+
+	    iloc[2]=max(0, min(nw-1, loc[2]));
+	    //cout<<"-----------------------"<<endl;
+	    if (onGrid(nx, ny, nw, loc, support)) {
+	      for(Int ipol=0; ipol < nDataPol; ipol++) {
+		//cout<<"ipol "<<ipol<<endl;
+		if(!flagCube(ipol,ichan,irow)) {
+		  apol=polMap_p[ipol];
+
+		  if((apol>=0) && (apol<nGridPol)) {
+		    igrdpos[2]=apol; igrdpos[3]=achan;
+                    nvalue=0.0;
+                    norm  =0.0;
+
+		    //ConjPlane = cfMap_p(ipol);
+		    //PolnPlane = conjCFMap_p(ipol);
+
+		    iloc[3]=ipol;
+
+		    for(Int iy=-scaledSupport[1]; iy <= scaledSupport[1]; iy++)
+		      {
+			iloc(1)=(Int)(scaledSampling[1]*iy+off[1]);
+			igrdpos[1]=loc[1]+iy;
+
+			for(Int ix=-scaledSupport[0]; ix <= scaledSupport[0]; ix++)
+			  {
+			    iloc(0)=(Int)(scaledSampling[0]*ix+off[0]);
+			    igrdpos[0]=loc[0]+ix;
+			    tiloc=iloc;
+			    if (reindex(iloc,tiloc,sinDPA, cosDPA,
+			    		convOrigin, cfShape))
+			      {
+                                // Use polarization leakage terms if defined.
+                                for (int ic=0; ic<4; ++ic) {
+                                  if (convFuncV[ic][iloc[3]]) {
+				    //cout<<"iloc[3]<<ic "<<iloc[3]<<" "<<ic<<endl;
+                                    wt = convFuncV[iloc[3]][ic][tiloc[1]*cfInc_p[1]+tiloc[0]];
+                                    //wt = conj(convFuncV[ic][iloc[3]][tiloc[1]*cfInc_p[1]+tiloc[0]]);
+                                    norm+=(wt);
+				    //			    nvalue+=wt*grid(grdpos);
+				// The following uses raw index on the 4D grid
+				//				nvalue+=wt*getFrom4DArray(gridStore,iPosPtr,gridInc);
+                                    igrdpos[2] = ic;
+                                    //igrdpos[2] = iloc[3];//ic;
+				    Complex wt2=getFrom4DArray(gridStore,igrdpos,gridInc_p);
+				    //cout<<"Resampler : ic iloc cf sumcf vis: "<<ic<<" "<<iloc[3]<<" "<<wt<<" "<<nvalue<<" "<<wt2<<endl;
+                                    nvalue+=wt*wt2;
+				    //cout<<"wt wt2 wt*wt2 = "<<wt<<" "<<wt2<<" "<<wt*wt2<<endl;
+                                  }
+                                }
+			      }
+			  }
+		      }
+		    //cout<<"nvalue<<" "<<conj(phasor)<<" "<<norm= "<<nvalue<<" "<<conj(phasor)<<" "<<norm<<endl;
+		    visCube(ipol,ichan,irow)=nvalue;//(nvalue*conj(phasor))/norm;
+        	    //cout<<"Vis  "<<visCube(ipol,ichan,irow)<<"  "<<ipol<<"  "<<ichan<<"  "<<irow<<endl;
+		    //modelCube(ipol,ichan,irow)=(nvalue*conj(phasor))/norm;
+		    //cout<<"modelCube  = "<<modelCube(ipol,ichan,irow)<<endl;
+		  }
+		}
+	      }
+	    }
+	  }
+	}
+    }
+  }
+  */
+
+  void LofarVisResamplerOld::lofarComputeResiduals(LofarVBStore& vbs)
+  {
+    Int rbeg = vbs.beginRow_p, rend = vbs.endRow_p;
+    IPosition vbDataShape=vbs.modelCube_p.shape();
+    IPosition start(vbDataShape), last(vbDataShape);
+    start=0; start(2)=rbeg;
+    last(2)=rend; //last=last-1;
+
+    //cout<<"//////////////////// Compuute residual!!!!!!"<<"vbs.useCorrected_p"<<vbs.useCorrected_p<<endl;
+
+    if (!vbs.useCorrected_p)
+      {
+    	for(uInt ichan = start(0); ichan < last(0); ichan++)
+    	  for(uInt ipol = start(1); ipol < last(1); ipol++)
+    	    for(uInt irow = start(2); irow < last(2); irow++)
+    	      vbs.modelCube_p(ichan,ipol,irow) = vbs.modelCube_p(ichan,ipol,irow) - vbs.visCube_p(ichan,ipol,irow);
+	
+      }
+    else
+      {
+    	for(uInt ichan = start(0); ichan < last(0); ichan++)
+    	  for(uInt ipol = start(1); ipol < last(1); ipol++)
+    	    for(uInt irow = start(2); irow < last(2); irow++){
+    	      //cout<<"===="<<endl;
+	      //if(!(abs(vbs.modelCube_p(ichan,ipol,irow))==0.)){cout<<ipol<<" "<<ichan<<" "<<irow<<" "<<vbs.modelCube_p(ichan,ipol,irow)<<"  "<<vbs.correctedCube_p(ichan,ipol,irow)<<endl;};
+    	      
+	      //if(!(abs(vbs.modelCube_p(ichan,ipol,irow))==0.)){cout<<"data "<<ipol<<" "<<ichan<<" "<<irow<<" "<<vbs.modelCube_p(ichan,ipol,irow)<<" "<<vbs.correctedCube_p(ichan,ipol,irow)<<endl;};
+	 
+    	      vbs.modelCube_p(ichan,ipol,irow) = vbs.modelCube_p(ichan,ipol,irow) - vbs.correctedCube_p(ichan,ipol,irow);
+    	      //cout<<vbs.modelCube_p(ichan,ipol,irow)<<"  "<<vbs.modelCube_p(ichan,ipol,irow)<<"  "<<vbs.correctedCube_p(ichan,ipol,irow)<<endl;
+    	    };
+	
+      }
+
+    // if (!vbs.useCorrected_p)
+    //   {
+    // 	for(uInt ichan = start(0); ichan < last(0); ichan++)
+    // 	  for(uInt ipol = start(1); ipol < last(1); ipol++)
+    // 	    for(uInt irow = start(2); irow < last(2); irow++)
+    // 	      vbs.modelCube_p(ichan,ipol,irow) = vbs.modelCube_p(ichan,ipol,irow) - vbs.visCube_p(ichan,ipol,irow);
+	
+    //   }
+    // else
+    //   {
+    // 	for(uInt ichan = start(0); ichan < last(0); ichan++)
+    // 	  for(uInt ipol = start(1); ipol < last(1); ipol++)
+    // 	    for(uInt irow = start(2); irow < last(2); irow++){
+    // 	      //cout<<"===="<<endl;
+    // 	      //cout<<vbs.correctedCube_p(ichan,ipol,irow)<<"  "<< vbs.correctedCube_p(ichan,ipol,irow)<<"  "<< vbs.modelCube_p(ichan,ipol,irow)<<endl;
+    // 	      vbs.correctedCube_p(ichan,ipol,irow) = vbs.correctedCube_p(ichan,ipol,irow) - vbs.modelCube_p(ichan,ipol,irow);
+    // 	      //cout<<vbs.correctedCube_p(ichan,ipol,irow)<<"  "<< vbs.correctedCube_p(ichan,ipol,irow)<<"  "<< vbs.modelCube_p(ichan,ipol,irow)<<endl;
+    // 	    };
+	
+    //   }
+
+
+  }
+
+  void LofarVisResamplerOld::sgrid(Vector<Double>& pos, Vector<Int>& loc,
+			     Vector<Int>& off, Complex& phasor,
+			     const Int& irow, const Matrix<Double>& uvw,
+			     const Double& dphase, const Double& freq,
+			     const Vector<Double>& scale,
+			     const Vector<Double>& offset,
+			     const Vector<Float>& sampling)
+  {
+    Double phase;
+    Vector<Double> uvw_l(3,0); // This allows gridding of weights
+			       // centered on the uv-origin
+    if (uvw.nelements() > 0) for(Int i=0;i<3;i++) uvw_l[i]=uvw(i,irow);
+
+    pos(2)=0;//sqrt(abs(scale[2]*uvw_l(2)*freq/C::c))+offset[2];
+    loc(2)=0;//SynthesisUtils::nint(pos[2]);
+    off(2)=0;
+
+    for(Int idim=0;idim<2;idim++)
+      {
+	pos[idim]=scale[idim]*uvw_l(idim)*freq/C::c+offset[idim];
+        ///        cout<<scale[idim]<<' '<<uvw_l[idim]<<' '<<offset[idim]<<' '<<pos[idim]<<endl;
+	loc[idim]=SynthesisUtils::nint(pos[idim]);
+	//off[idim]=SynthesisUtils::nint((loc[idim]-pos[idim])*sampling[idim]); // Cyr: I've added "+1" next line, and it solves a difficult problem, i don't know why
+	////off[idim]=SynthesisUtils::nint((loc[idim]-pos[idim])*sampling[idim]+1);
+	off[idim]=SynthesisUtils::nint((loc[idim]-pos[idim])*sampling[idim]);
+      }
+
+    //if (dphase != 0.0)
+    //  {
+//	phase=-2.0*C::pi*dphase*freq/C::c;
+//	phasor=Complex(cos(phase), sin(phase));
+    //  }
+    //else
+      phasor=Complex(1.0);
+  }
+
+} // end namespace
diff --git a/CEP/Imager/LofarFT/src/awimager.cc b/CEP/Imager/LofarFT/src/awimager.cc
index 18f38d6d6f876086e3abf1d8bed5790362cfba4e..062065020d4dfe95049a8c499653b8eaa4d7a5c9 100644
--- a/CEP/Imager/LofarFT/src/awimager.cc
+++ b/CEP/Imager/LofarFT/src/awimager.cc
@@ -1,4 +1,4 @@
-//# awzim.cc: Program to create and/or clean a LOFAR image
+//# awimager.cc: Program to create and/or clean a LOFAR image
 //# Copyright (C) 2011
 //# Associated Universities, Inc. Washington DC, USA.
 //#
@@ -39,6 +39,7 @@
 #include <casa/Utilities/Regex.h>
 #include <casa/Utilities/Assert.h>
 #include <casa/OS/Directory.h>
+#include <casa/OS/File.h>
 #include <casa/Exceptions/Error.h>
 #include <casa/OS/Timer.h>
 #include <casa/OS/PrecTimer.h>
@@ -128,7 +129,7 @@ void readFilter (const String& filter,
   }
 }
 
-Matrix<Bool> readMueller (const String& str)
+Matrix<Bool> readMueller (const String& str, String stokes, Bool grid)
 {
   Matrix<Bool> mat(4,4, True);
   String s(str);
@@ -147,6 +148,20 @@ Matrix<Bool> readMueller (const String& str)
       throw AipsError (str + " is an invalid Mueller specification");
     }
   }
+  if((stokes=="I")&&(grid)){
+    for(uInt i=0;i<4;++i){
+      mat(1,i)=0;
+      mat(2,i)=0;
+    };
+  }
+  if((stokes=="I")&&(!grid)){
+    for(uInt i=0;i<4;++i){
+      mat(1,i)=0;
+      mat(2,i)=0;
+      // mat(i,1)=0;
+      // mat(i,2)=0;
+    };
+  };
   return mat;
 }
 
@@ -163,6 +178,7 @@ void applyFactors (PagedImage<Float>& image, const Array<Float>& factors)
 {
   Array<Float> data;
   image.get (data);
+  ///  cout << "apply factor to " << data.data()[0] << ' ' << factors.data()[0]<<endl;
   // Loop over channels
   for (ArrayIterator<Float> iter1(data, 3); !iter1.pastEnd(); iter1.next()) {
     // Loop over Stokes.
@@ -173,10 +189,12 @@ void applyFactors (PagedImage<Float>& image, const Array<Float>& factors)
     }
   }
   image.put (data);
+  ///  cout << "applied factor to " << data.data()[0] << ' ' << factors.data()[0]<<endl;
 }
 
 void correctImages (const String& restoName, const String& modelName,
-                    const String& residName, const String& imgName)
+                    const String& residName, const String& imgName,
+                    LOFAR::LofarImager& imager, Bool CorrectElement)
 {
   // Copy the images to .corr ones.
   {
@@ -195,15 +213,14 @@ void correctImages (const String& restoName, const String& modelName,
                 restoredImage.shape() == modelImage.shape(), SynthesisError);
 
   // Get average primary beam and spheroidal.
-  Matrix<Float> avgPB = LOFAR::LofarConvolutionFunction::getAveragePB(imgName);
-  Matrix<Float> spheroidCut = LOFAR::LofarConvolutionFunction::getSpheroidCut(imgName);
-  //  String nameii(imgName + ".spheroid_cut_im");
-  //ostringstream nameiii(nameii);
-  //PagedImage<Float> restoredImageiii(nameiii.str().c_str());
-  //Slicer sliceiiii(IPosition(4,0,0,0,0), restoredImageiii.shape(), IPosition(4,1,1,1,1));
-  //Array<Float> spheroidCut;
-  //restoredImageiii.doGetSlice(spheroidCut , sliceiiii);
-
+  Matrix<Float> avgPB = LOFAR::LofarConvolutionFunction::getAveragePB(imgName+"0");
+  Matrix<Float> spheroidCut = LOFAR::LofarConvolutionFunction::getSpheroidCut(imgName+"0");
+  String nameii("Spheroid_cut_im_element.img");
+  ostringstream nameiii(nameii);
+  PagedImage<Float> tmpi(nameiii.str().c_str());
+  Slicer slicei(IPosition(4,0,0,0,0), tmpi.shape(), IPosition(4,1,1,1,1));
+  Array<Float> spheroidCutElement;
+  tmpi.doGetSlice(spheroidCutElement, slicei);
   // Use the inner part of the beam and spheroidal.
   Int nximg = restoredImage.shape()[0];
   Int nxpb  = avgPB.shape()[0];
@@ -219,7 +236,15 @@ void correctImages (const String& restoName, const String& modelName,
                                        IPosition(2, nximg, nximg)));
   Array<Float> sphinner = spheroidCut(Slicer(IPosition(2, offsph, offsph),
                                              IPosition(2, nximg, nximg)));
-  Array<Float> factors = sphinner / sqrt(pbinner);
+  Array<Float> factors;
+  if(CorrectElement){
+    Array<Float> sphinner_el = (spheroidCutElement(Slicer(IPosition(4, offsph, offsph,0,0),
+							  IPosition(4, nximg, nximg,1,1)))).nonDegenerate();
+    factors = sphinner_el *sphinner / sqrt(pbinner);//sphinner_el * sphinner / sqrt(pbinner);
+  } else{
+    
+    factors = sphinner / sqrt(pbinner);//sphinner_el * sphinner / sqrt(pbinner);
+  }
   applyFactors (restoredImage, factors);
   applyFactors (modelImage, factors);
   applyFactors (residualImage, factors);
@@ -292,14 +317,11 @@ int main (Int argc, char** argv)
     inputs.create ("wmax", "500.0",
 		   "omit data with w-term > wmax (in meters)",
 		   "float");
-    inputs.create ("beamelementpath", "$LOFARROOT/share",
-		   "directory where the Hamaker beam element files reside",
-		   "string");
     inputs.create ("muellergrid", "all",
-		   "Nueller elements to use when gridding (all,diagonal,band1,band2)",
+		   "Mueller elements to use when gridding (all,diagonal,band1,band2)",
 		   "string");
     inputs.create ("muellerdegrid", "all",
-		   "Nueller elements to use when degridding (all,diagonal,band1,band2)",
+		   "Mueller elements to use when degridding (all,diagonal,band1,band2)",
 		   "string");
     inputs.create ("cachesize", "512",
 		   "maximum size of gridding cache (in MBytes)",
@@ -354,7 +376,7 @@ int main (Int argc, char** argv)
 		   "string");
     inputs.create ("operation", "image",
                    ///		   "Operation (empty,image,clark,hogbom,csclean,multiscale,entropy)",
-		   "Operation (empty,image,csclean)",
+		   "Operation (empty,image,csclean,predict,psf)",
 		   "string");
     inputs.create ("niter", "1000",
 		   "Number of clean iterations",
@@ -404,12 +426,69 @@ int main (Int argc, char** argv)
     inputs.create ("oversample", "8",
 		   "oversampling for convolution functions",
 		   "int");
-
+    inputs.create ("uvdist", "",
+		   "UV Range",
+		   "string");
+    inputs.create ("RefFreq", "",
+		   "Reference Frequency (Hz)",
+		   "Double");
+    inputs.create ("nterms", "1",
+		   "Number of Taylor terms",
+		   "int");
+    inputs.create ("UseLIG", "false",
+		   "Use gridder using linear interpolation (not working yet, never to be)",
+		   "bool");
+    inputs.create ("UseEJones", "true",
+                   "Use the beam for the calculation of the convolution function (not working yet)",
+                   "bool");
+    inputs.create ("applyIonosphere", "false",
+                   "apply ionospheric correction",
+                   "bool");
+    inputs.create ("applyBeam", "true",
+                   "apply beam (array factor)",
+                   "bool");
+    inputs.create ("splitbeam", "true",
+                   "Evaluate station beam and element beam separately (splitbeam = true is faster)",
+                   "bool");
+    // inputs.create ("ApplyElement", "false",
+    // 		   "Apply the element beam",
+    // 		   "bool");
+    inputs.create ("PBCut", "1e-2",
+		   "Level below which the dirty images will be set to zero. Expressed in units of peak primary beam.",
+		   "Double");
+    inputs.create ("cyclefactor", "1.5",
+		   "Cycle Factor. See Casa definition.",
+		   "Double");
+    inputs.create ("cyclespeedup", "-1",
+		   "Cycle Factor. See Casa definition.",
+		   "Double");
+    inputs.create ("PsfImage", "",
+		   "Input PSF image for the cleaning",
+		   "string");
+    inputs.create ("StepApplyElement", "0",
+		   "If turned to >0, apply the element beam every N number of timewindows.",
+		   "int");
+    inputs.create ("UseMasks", "true",
+		   "When the element beam is applied (StepApplyElement), the addictional step of convolving the grid can be made more efficient by computing masks. If true, it will create a directory in which it stores the masks.",
+		   "bool");
+    inputs.create ("RowBlock", "0",
+		   "In certain obscure circounstances (taql, selection using uvdist), the RowBlocking used by the imager calculated from the timewindow value is not correct. This parameter can be used to specify the RowBlocking.",
+		   "int");
+    // inputs.create ("FillFactor", "1",
+    // 		   "Fraction of the data that will be selected from the selected MS. (don't use it yet)",
+    // 		   "Double");
+ 
     // Fill the input structure from the command line.
     inputs.readArguments (argc, argv);
 
     // Get the input specification.
     Bool fixed          = inputs.getBool("fixed");
+    Bool UseLIG         = inputs.getBool("UseLIG");
+    Bool UseEJones      = inputs.getBool("UseEJones");
+    Bool applyIonosphere = inputs.getBool("applyIonosphere");
+    Bool applyBeam = inputs.getBool("applyBeam");
+    Bool splitbeam = inputs.getBool("splitbeam");
+    Bool ApplyElement   ;//= inputs.getBool("ApplyElement");
     Bool constrainFlux  = inputs.getBool("constrainflux");
     Bool preferVelocity = inputs.getBool("prefervelocity");
     Bool displayProgress= inputs.getBool("displayprogress");
@@ -430,11 +509,15 @@ int main (Int argc, char** argv)
     Int verbose      = inputs.getInt("verbose");
     Int maxsupport   = inputs.getInt("maxsupport");
     Int oversample   = inputs.getInt("oversample");
+    Int StepApplyElement   = inputs.getInt("StepApplyElement");
+    if ((StepApplyElement%2 == 0)&&((StepApplyElement%2 != 0))) {
+      StepApplyElement++;
+    }
+    Int nterms   = inputs.getInt("nterms");
     Vector<Double> userScaleSizes(inputs.getDoubleVector("uservector"));
     Double padding   = inputs.getDouble("padding");
     Double gain      = inputs.getDouble("gain");
     Double maskValue = inputs.getDouble("maskvalue");
-    String beamDir   = inputs.getString("beamelementpath");
     String mode      = inputs.getString("mode");
     String operation = inputs.getString("operation");
     String weight    = inputs.getString("weight");
@@ -461,11 +544,20 @@ int main (Int argc, char** argv)
     String psfName   = inputs.getString("psf");
     String imageType = inputs.getString("data");
     String select    = inputs.getString("select");
+    String uvdist    = inputs.getString("uvdist");
     String maskName  = inputs.getString("mask");
     String mstrBlc   = inputs.getString("maskblc");
     String mstrTrc   = inputs.getString("masktrc");
-    Matrix<Bool> muelgrid   = readMueller (inputs.getString("muellergrid"));
-    Matrix<Bool> mueldegrid = readMueller (inputs.getString("muellerdegrid"));
+    Double RefFreq   = inputs.getDouble("RefFreq");
+    Double PBCut   = inputs.getDouble("PBCut");
+    Double cyclefactor   = inputs.getDouble("cyclefactor");
+    Double cyclespeedup  = inputs.getDouble("cyclespeedup");
+    Matrix<Bool> muelgrid   = readMueller (inputs.getString("muellergrid"), stokes, true);
+    Matrix<Bool> mueldegrid = readMueller (inputs.getString("muellerdegrid"), stokes, false);
+    String PsfImage    = inputs.getString("PsfImage");
+    Bool Use_masks    = inputs.getBool("UseMasks");
+    Int RowBlock   = inputs.getInt("RowBlock");
+    //Double FillFactor= 1.;//inputs.getDouble("FillFactor");
 
     // Check and interpret input values.
     Quantity qcellsize = readQuantity (cellsize);
@@ -534,13 +626,13 @@ int main (Int argc, char** argv)
       phaseCenter = readDirection (phasectr);
     }
     operation.downcase();
-    AlwaysAssertExit (operation=="empty" || operation=="image" || operation=="csclean");
+    AlwaysAssertExit (operation=="empty" || operation=="image" || operation=="csclean"|| operation=="msmfs"||operation=="predict"||operation=="psf");
     ///AlwaysAssertExit (operation=="empty" || operation=="image" || operation=="hogbom" || operation=="clark" || operation=="csclean" || operation=="multiscale" || operation =="entropy");
     IPosition maskBlc, maskTrc;
     Quantity threshold;
     Quantity sigma;
     Quantity targetFlux;
-    Bool doClean = (operation != "empty"  &&  operation != "image");
+    Bool doClean = (operation != "empty"  &&  operation != "image"&&  operation != "psf");
     if (doClean) {
       maskBlc = readIPosition (mstrBlc);
       maskTrc = readIPosition (mstrTrc);
@@ -548,6 +640,11 @@ int main (Int argc, char** argv)
       sigma = readQuantity (sigmaStr);
       targetFlux = readQuantity (targetStr);
     }
+    Bool doPSF =(operation=="psf");
+    if(doPSF==true){
+      operation="csclean";
+      niter=0;
+    }
     // Get axis specification from filter.
     Quantity bmajor, bminor, bpa;
     readFilter (filter, bmajor, bminor, bpa);
@@ -558,21 +655,55 @@ int main (Int argc, char** argv)
     Record params;
     params.define ("timewindow", timewindow);
     params.define ("wmax", wmax);
-    params.define ("beam.element.path", beamDir);
     params.define ("mueller.grid", muelgrid);
     params.define ("mueller.degrid", mueldegrid);
     params.define ("verbose", verbose);
     params.define ("maxsupport", maxsupport);
     params.define ("oversample", oversample);
     params.define ("imagename", imgName);
+    params.define ("UseLIG", UseLIG);
+    params.define ("UseEJones", UseEJones);
+    //params.define ("ApplyElement", ApplyElement);
+    params.define ("PBCut", PBCut);
+    params.define ("StepApplyElement", StepApplyElement);
+    Bool PredictFT(false);
+    if(operation=="predict"){PredictFT=true;}
+    params.define ("PredictFT", PredictFT);
+    params.define ("PsfImage", PsfImage);
+    params.define ("UseMasksDegrid", Use_masks);
+    params.define ("RowBlock", RowBlock);
+    params.define ("doPSF", doPSF);
+    params.define ("applyIonosphere", applyIonosphere);
+    params.define ("applyBeam", applyBeam);
+    params.define ("splitbeam", splitbeam);
+    //params.define ("FillFactor", FillFactor);
+    
     LOFAR::LofarImager imager(ms, params);
+
+    MSSpWindowColumns window(ms.spectralWindow());
+    // ROMSObservationColumns timerange(ms.observation());
+    // cout<<"timerange"<<timerange.timerange()<<endl;
+    Vector<Int> wind(window.nrow());
+    for(uInt iii=0;iii<window.nrow();++iii){wind(iii)=iii;};
+    cout<<"... Windows is shit"<<endl;
+
+    ROArrayColumn<Double> chfreq(window.chanFreq());
+
+    cout<<"Number of channels: "<<chfreq(0).shape()[0]<<endl;
+
+    Vector<Int> chansel(1);
+    chansel(0)=chfreq(0).shape()[0];
+    //chansel(1)=1;
+    //chansel(2)=2;
+    //chansel(3)=3;
+
     imager.setdata (chanmode,                       // mode
-		    nchan,
+		    chansel,//nchan,
 		    chanstart,
                     chanstep,
 		    MRadialVelocity(),              // mStart
 		    MRadialVelocity(),              // mStep
-		    spwid,
+		    wind,//spwid,
 		    Vector<Int>(1,fieldid),
 		    select,                         // msSelect
                     String(),                       // timerng
@@ -580,10 +711,22 @@ int main (Int argc, char** argv)
                     Vector<Int>(),                  // antIndex
                     String(),                       // antnames
                     String(),                       // spwstring
-                    String(),                       // uvdist
+                    uvdist,                       // uvdist
                     String(),                       // scan
                     True);                          // useModelCol
 
+
+    imager.setmfcontrol(cyclefactor,          //Float cyclefactor,
+  			cyclespeedup,         //Float cyclespeedup,
+  			0.8,                        //Float cyclemaxpsffraction, 
+  			2,                          //Int stoplargenegatives, 
+  			-1,                         //Int stoppointmode,
+  			"",                         //String& scaleType,
+  			0.1,                        //Float minPB,
+  			0.4,                        //loat constPB,
+  			Vector<String>(1, ""),      //Vector<String>& fluxscale,
+  			true);                      //Bool flatnoise);
+
     imager.defineImage (npix,                       // nx
                         npix,                       // ny
                         qcellsize,                  // cellx
@@ -598,9 +741,25 @@ int main (Int argc, char** argv)
                         MFrequency(),               // mFreqstart
                         MRadialVelocity(),          // mStart
                         Quantity(1,"km/s"),         // qstep, Def=1 km/s
-                        spwid,                      // spectralwindowids
+			wind,//spwid,                      // spectralwindowids
                         nfacet);                    // facets
 
+    if (operation=="predict"){
+      String ftmachine("ft");
+      if (wplanes > 0) {
+        ftmachine = "wproject";
+      }
+      imager.setoptions(ftmachine,                    // ftmachine
+                        cachesize*1024*(1024/8),      // cache
+                        16,                           // tile
+                        "SF",                         // gridfunction
+                        MPosition(),                  // mLocation
+                        padding,                      // padding
+                        wplanes);                     // wprojplanes
+      imager.ft(Vector<String>(1, modelName), "",
+		False);
+    } else{
+
     // Create empty image?
     if (operation == "empty" ) {
       makeEmpty (imager, imgName, fieldid);
@@ -707,25 +866,109 @@ int main (Int argc, char** argv)
                      Vector<String>(1, maskName),     // mask
                      Vector<String>(1, restoName),    // restored
                      Vector<String>(1, residName));   // residual
-        } else {
+        }
+        if (operation == "msmfs") {
+	  //uInt nterms(2);
+
+	  //imager.settaylorterms(nterms,5.95e+07);
+	  imager.settaylorterms(nterms,RefFreq);
+	  String scaleMethod;
+	  Vector<Float> userVector(1); userVector(0)=0;
+	  convertArray (userVector, userScaleSizes);
+	  if (userScaleSizes.size() > 1) {
+	    scaleMethod = "uservector";
+	  } else {
+	    scaleMethod = "nscales";
+	  }
+	  imager.setscales(scaleMethod, 1, userVector);
+	  cout<<imgName<<endl;
+	  makeEmpty (imager, imgName, 0);
+	  Directory filee(imgName);
+
+	  
+	  Vector<String> modelNames(nterms);
+	  for(uInt i=0;i<nterms;++i){
+	    modelNames(i)="test.img.model.tt"+String::toString(i);
+	    
+	    Directory filee0(modelNames(i));
+	    File file_model0(modelNames(i));
+	    if(file_model0.exists()){filee0.removeRecursive();};
+	    Path model0(modelNames(i)); 
+	    filee.copy(model0);
+	  };
+
+	  //	  assert(false);
+
+          imager.clean("msmfs",                     // algorithm,
+                       niter,                         // niter
+                       gain,                          // gain
+                       threshold,                     // threshold
+                       displayProgress,               // displayProgress
+                       //Vector<String>(1, modelName),  // model
+                       modelNames,
+                       Vector<Bool>(1, fixed),        // fixed
+                       "",                            // complist
+                       Vector<String>(1, maskName),   // mask
+                       Vector<String>(1, restoName),  // restored
+                       Vector<String>(1, residName),  // residual
+                       Vector<String>(1, psfName));   // psf
+	  
+	}
+	else {
+	  Vector<String> modelNames(2);
+	  modelNames[0]="model.main";
+	  modelNames[1]="model.outlier";
+	  File Dir_masks_file("JAWS_masks_degrid");
+	  Directory Dir_masks("JAWS_masks_degrid");
+	  if(Dir_masks_file.exists()){
+	    Dir_masks.removeRecursive();
+	  }
+	  if(Use_masks){
+	    Dir_masks.create();
+	  }
+	  // Vector<String> Namelist(5);
+	  // Namelist[0]=".spheroid_cut_im";
+ 	  // Namelist[1]=".residual";
+	  // Namelist[2]=".residual.corr";
+	  // Namelist[3]=".restored";
+	  // Namelist[4]=".restored.corr";
+	  // //Namelist[5]="0.avgpb";
+	  // for(uInt i=0; i<Namelist.size(); ++i){
+	  //   String avgpb_name(imgName + Namelist[i]);
+	  //   File avgpb_name_file(avgpb_name);
+	  //   Directory avgpb_name_dir(avgpb_name);
+	  //   cout<<avgpb_name<<endl;
+	  //   if(avgpb_name_file.exists()){
+	  //     cout<<"... remove"<<endl;
+	  //     avgpb_name_dir.removeRecursive();
+	  //   }
+	  // }
+	  // Regex rx1 (Regex::fromPattern ("*.avgpb"));
+	  // Vector<String> avgpbfiles;
+	  // avgpbfiles=Directory::find(rx1);
+
           imager.clean(operation,                     // algorithm,
                        niter,                         // niter
                        gain,                          // gain
                        threshold,                     // threshold
                        displayProgress,               // displayProgress
                        Vector<String>(1, modelName),  // model
+		      //  modelNames,
                        Vector<Bool>(1, fixed),        // fixed
                        "",                            // complist
                        Vector<String>(1, maskName),   // mask
                        Vector<String>(1, restoName),  // restored
                        Vector<String>(1, residName),  // residual
                        Vector<String>(1, psfName));   // psf
+
         }
         // Do the final correction for primary beam and spheroidal.
-        correctImages (restoName, modelName, residName, imgName);
+	ApplyElement=false;
+	if(StepApplyElement>0){ApplyElement=true;}
+        correctImages (restoName, modelName, residName, imgName, imager, ApplyElement);
         precTimer.stop();
         timer.show ("clean");
-        ///imager.showTimings (cout, precTimer.getReal());
+	///        imager.showTimings (cout, precTimer.getReal());
         // Convert result to fits if needed.
         if (! fitsName.empty()) {
           String error;
@@ -740,10 +983,10 @@ int main (Int argc, char** argv)
         }
       }
     }
-  } catch (AipsError& x) {
+    } }catch (AipsError& x) {
     cout << x.getMesg() << endl;
     return 1;
-  } 
-  cout << "awzim normally ended" << endl;
+  }
+  cout << "awimager normally ended" << endl;
   return 0;
 }
diff --git a/CEP/Imager/LofarFT/src/makefftwisdom2d.cc b/CEP/Imager/LofarFT/src/makefftwisdom2d.cc
index c86442986dabd3617b9c9c6899396aa79d8605a1..9259136d5bec368bcd3a3122b08743c6ef858d6a 100644
--- a/CEP/Imager/LofarFT/src/makefftwisdom2d.cc
+++ b/CEP/Imager/LofarFT/src/makefftwisdom2d.cc
@@ -92,7 +92,7 @@ int main (int argc, char* argv[])
   cerr << endl;
   cout << "Creating 2D FFTW wisdom for some multiples of 1024 (till 16384) ..."
        << endl;
-  int otherSizes[] = {512, 1024, 2048, 4096, 6144, 8192, 10240, 12288, 16384};
+  int otherSizes[] = {512, 1024};//, 2048, 4096, 6144, 8192, 10240, 12288, 16384};
   nsizes = sizeof(otherSizes) / sizeof(int);
   for (int i=0; i<2; ++i) {
     cerr << ' ' << otherSizes[i];
diff --git a/CEP/Imager/MWImager/src/mwimager b/CEP/Imager/MWImager/src/mwimager
index 364b7f74bebd3b0411765224cd57c13df24b9d1d..d3b795cde28433e60f05677f519e9081e83e69da 100755
--- a/CEP/Imager/MWImager/src/mwimager
+++ b/CEP/Imager/MWImager/src/mwimager
@@ -26,8 +26,8 @@
 
 
 # Find the path used to start the script.
-pgmpath=`dirname $0`
-pgmpath=`cd $pgmpath > /dev/null 2>&1  &&  pwd`
+pgmpath=`dirname "$0"`
+pgmpath=`cd "$pgmpath" > /dev/null 2>&1  &&  pwd`
 
 # Handle possible options.
 imager=cimager
@@ -37,7 +37,7 @@ while [ $# != 0 ]
 do
   if [ "$1" = "-casa" ]; then
     imager='casa'
-    casaopt=$1
+    casaopt="$1"
     shift
   elif [ "$1" = "-nocasa" ]; then
     imager=cimager
@@ -78,26 +78,26 @@ if test $# = 0; then
   exit 1
 fi
 
-psn=$1
+psn="$1"
 shift
 cdn=
 if test $# != 0; then
-  cdn=$1
+  cdn="$1"
   shift
 fi
 wd=
 if test $# != 0; then
-  wd=$1
+  wd="$1"
   shift
 fi
 logfile=
 if test $# != 0; then
-  logfile=$1
+  logfile="$1"
   shift
 fi
 dry=
 if test $# != 0; then
-  dry=$1
+  dry="$1"
   shift
 fi
 if test "$dry" = ""; then
@@ -105,7 +105,7 @@ if test "$dry" = ""; then
 fi
 hfn=
 if test $# != 0; then
-  hfn=$1
+  hfn="$1"
   shift
 fi
 
@@ -127,41 +127,41 @@ if test "$dde" = 2; then
 fi
 
 # Get the dataset name.
-msvds=`getparsetvalue $psn dataset` || exit 1
+msvds=`getparsetvalue "$psn" dataset` || exit 1
 if test "$cdn" = ""; then
-  if test ! -d $msvds; then
-    cdn=`getparsetvalue -d '' $msvds ClusterDesc`
+  if test ! -d "$msvds"; then
+    cdn=`getparsetvalue -d '' "$msvds" ClusterDesc`
   fi
 fi
 if test "$cdn" = ""; then
-  cdn=$HOME/CEP.clusterdesc
+  cdn="$HOME/CEP.clusterdesc"
 fi
 
 # Make all file names absolute.
-dn=`dirname $psn`
-psn=`cd $dn > /dev/null; pwd`/`basename $psn`
-dn=`dirname $cdn`
-cdn=`cd $dn > /dev/null; pwd`/`basename $cdn`
+dn=`dirname "$psn"`
+psn=`cd "$dn" > /dev/null; pwd`/`basename "$psn"`
+dn=`dirname "$cdn"`
+cdn=`cd "$dn" > /dev/null; pwd`/`basename "$cdn"`
 if test "$hfn" != ""; then
-  dn=`dirname $hfn`
-  hfn=`cd $dn > /dev/null; pwd`/`basename $hfn`
+  dn=`dirname "$hfn"`
+  hfn=`cd "$dn" > /dev/null; pwd`/`basename "$hfn"`
 fi
 
 # Use mwimager-dd if direction-dependent effect corrections.
 if test $dde = 1; then
-  $pgmpath/mwimager-dd "$psn" "$cdn" "$wd" "$logfile" "$dry" "$hfn" "$casaopt"
+  "$pgmpath/mwimager-dd" "$psn" "$cdn" "$wd" "$logfile" "$dry" "$hfn" "$casaopt"
 else
 
   # Get dataset name from the parset.
   # If it is an MS, operate directly on it (as rank 0).
-  msvds=`getparsetvalue $psn dataset` || exit 1
+  msvds=`getparsetvalue "$psn" dataset` || exit 1
   if test -d "$msvds"  -a  -e "$msvds/table.dat"; then
     echo "$pgmpath/mwimager-part '' '' '' '' 0 '$msvds' '' '$psn' '$imager' '$wd' '$dry'"
-    $pgmpath/mwimager-part "" "" "" "" 0 "$msvds" "" "$psn" "$imager" "$wd" "$dry" 
+    "$pgmpath/mwimager-part" "" "" "" "" 0 "$msvds" "" "$psn" "$imager" "$wd" "$dry" 
 
   else
     # Start the imager processes on the various machines.
-    echo "startdistproc -useenv -mode 0 -nomasterhost -dsn '$msvds' -hfn '$hfn' -cdn '$cdn' -logfile '$logfile' $pgmpath/mwimager-part '$psn' '$imager' '$wd' '$dry'"
-    startdistproc -useenv -mode 0 -nomasterhost -dsn "$msvds" -hfn "$hfn" -cdn "$cdn" -logfile "$logfile" $pgmpath/mwimager-part "$psn" "$imager" "$wd" "$dry"
+    echo "startdistproc -useenv -mode 0 -nomasterhost -dsn '$msvds' -hfn '$hfn' -cdn '$cdn' -logfile '$logfile' '$pgmpath/mwimager-part' '$psn' '$imager' '$wd' '$dry'"
+    startdistproc -useenv -mode 0 -nomasterhost -dsn "$msvds" -hfn "$hfn" -cdn "$cdn" -logfile "$logfile" "$pgmpath/mwimager-part" "$psn" "$imager" "$wd" "$dry"
   fi
 fi
diff --git a/CEP/Imager/MWImager/src/mwimager-askap b/CEP/Imager/MWImager/src/mwimager-askap
index a0513dd7cdf4a28062bfc78f32d5ec15e61e415f..d59b70561d7b44c5891f779ac42e540b02a0ed29 100755
--- a/CEP/Imager/MWImager/src/mwimager-askap
+++ b/CEP/Imager/MWImager/src/mwimager-askap
@@ -26,54 +26,54 @@
 
 
 # Find the path used to start the script.
-pgmpath=`dirname $0`
-pgmpath=`cd $pgmpath > /dev/null 2>&1  &&  pwd`
+pgmpath=`dirname "$0"`
+pgmpath=`cd "$pgmpath" > /dev/null 2>&1  &&  pwd`
 
 if test $# != 5; then
   echo "run as:   mwimager-askap parset-file seqnr ms-part ms-part-vds dry"
   exit 1
 fi
 
-psn=$1
-seqnr=$2
-msn=$3
-vds=$4
-dry=$5
+psn="$1"
+seqnr="$2"
+msn="$3"
+vds="$4"
+dry="$5"
 
-psnbase=`basename $psn`
+psnbase=`basename "$psn"`
 
 # Add channel info to the parset file.
-cp $psn $psnbase.part$seqnr  ||  exit 1
-schan=`getparsetvalue $psnbase.part$seqnr firstchan 2>/dev/null` || schan=0
-echan=`getparsetvalue $psnbase.part$seqnr lastchan  2>/dev/null` || echan=-1
-sfreq=`getparsetvalue $vds StartFreqs schan` || exit 1
-efreq=`getparsetvalue $vds EndFreqs   echan` || exit 1
+cp "$psn" "$psnbase.part$seqnr"  ||  exit 1
+schan=`getparsetvalue "$psnbase.part$seqnr" firstchan 2>/dev/null` || schan=0
+echan=`getparsetvalue "$psnbase.part$seqnr" lastchan  2>/dev/null` || echan=-1
+sfreq=`getparsetvalue "$vds" StartFreqs schan` || exit 1
+efreq=`getparsetvalue "$vds" EndFreqs   echan` || exit 1
 echo "Images.frequency = [$sfreq, $efreq]" >> $psnbase.part$seqnr
-msdRa=`getparsetvalue $vds Extra.FieldDirectionRa 0 2>/dev/null` || extraRa=
-msdDec=`getparsetvalue $vds Extra.FieldDirectionDec 0 2>/dev/null` || extraDec=
-msdType=`getparsetvalue $vds Extra.FieldDirectionType 0 2>/dev/null` || extraType=
+msdRa=`getparsetvalue "$vds" Extra.FieldDirectionRa 0 2>/dev/null` || extraRa=
+msdDec=`getparsetvalue "$vds" Extra.FieldDirectionDec 0 2>/dev/null` || extraDec=
+msdType=`getparsetvalue "$vds" Extra.FieldDirectionType 0 2>/dev/null` || extraType=
 if test "$msdRa" != "" -a "$msdDec" != "" -a "$msdType" != ""; then
-  echo "msDirRa = $msdRa" >> $psnbase.part$seqnr
-  echo "msDirDec = $msdDec" >> $psnbase.part$seqnr
-  echo "msDirType = $msdType" >> $psnbase.part$seqnr
+  echo "msDirRa = $msdRa" >> "$psnbase.part$seqnr"
+  echo "msDirDec = $msdDec" >> "$psnbase.part$seqnr"
+  echo "msDirType = $msdType" >> "$psnbase.part$seqnr"
 fi
 
 # Put the MS name into the parset file.
-sed -e "s%^ *dataset *=.*%dataset = $msn%" $psnbase.part$seqnr > $psnbase.part$seqnr-tmp  ||  exit 1
-mv $psnbase.part$seqnr-tmp $psnbase.part$seqnr  ||  exit 1
+sed -e "s%^ *dataset *=.*%dataset = $msn%" "$psnbase.part$seqnr" > "$psnbase.part$seqnr-tmp"  ||  exit 1
+mv "$psnbase.part$seqnr-tmp" "$psnbase.part$seqnr"  ||  exit 1
 
 # Convert parset file from SAS format to cimager format.
-convertimagerparset $psnbase.part$seqnr $psnbase.part$seqnr.cvt  ||  exit 1
+convertimagerparset "$psnbase.part$seqnr" "$psnbase.part$seqnr.cvt"  ||  exit 1
 
 # Get a possible baseline selection string.
 # If given, the selection has to be done before cimager, hence its input MS
 # gets a different name.
-selstr=`getparsetvalue $psnbase.part$seqnr baseline 2>/dev/null` || selstr=
+selstr=`getparsetvalue "$psnbase.part$seqnr" baseline 2>/dev/null` || selstr=
 if test "$selstr" != ""; then
-  msnsel=${msn}_sel
-  sed -e "s%^ *Cimager.dataset *=.*%Cimager.dataset=$msnsel%" -e "s%^ *Cimager.baseline *=%baseline=%" $psnbase.part$seqnr.cvt > $psnbase.part$seqnr-tmp  ||  exit 1
-  mv $psnbase.part$seqnr-tmp $psnbase.part$seqnr.cvt  ||  exit 1
-  echo "msselect in=$msn 'baseline=$selstr' out=$msnsel"
+  msnsel="${msn}_sel"
+  sed -e "s%^ *Cimager.dataset *=.*%Cimager.dataset=$msnsel%" -e "s%^ *Cimager.baseline *=%baseline=%" "$psnbase.part$seqnr.cvt" > "$psnbase.part$seqnr-tmp"  ||  exit 1
+  mv "$psnbase.part$seqnr-tmp" "$psnbase.part$seqnr.cvt"  ||  exit 1
+  echo "msselect 'in=$msn' 'baseline=$selstr' 'out=$msnsel'"
   if test "$dry" != dry; then
     msselect "in=$msn" "baseline=$selstr" "out=$msnsel"
   fi
@@ -81,39 +81,39 @@ fi
 
 
 if test "$dry" = dry; then
-  cat $psnbase.part$seqnr.cvt
+  cat "$psnbase.part$seqnr.cvt"
   exit 0
 fi
 
 # Run the imager.
 echo "cimager -inputs $psnbase.part$seqnr.cvt"
-cimager -inputs $psnbase.part$seqnr.cvt
+cimager -inputs "$psnbase.part$seqnr.cvt"
 
 # Create a symlink using the same name as cimager would create.
-imgname=`getparsetvalue $psnbase.part$seqnr.cvt Cimager.Images.Names`
-newname=`getparsetvalue $psnbase.part$seqnr.cvt imgname`
+imgname=`getparsetvalue "$psnbase.part$seqnr.cvt" Cimager.Images.Names`
+newname=`getparsetvalue "$psnbase.part$seqnr.cvt" imgname`
 # Do not do it if it already exists as a directory without table.dat.
 # Thus an already existing regular .img is overwritten.
 makelink=1
-if test ! -L $newname  -a  -d $newname; then
-  if test ! -e $newname/table.dat; then
+if test ! -L "$newname"  -a  -d "$newname"; then
+  if test ! -e "$newname/table.dat"; then
     echo "Warning, directory $newname already exists, but not as a table"
     makelink=0
   fi
 fi
 nm=
-if test -e $imgname.restored; then
-  nm=$imgname.restored
-  if test -e $imgname  -a  $imgname -nt $nm; then
-    nm=$imgname
+if test -e "$imgname.restored"; then
+  nm="$imgname.restored"
+  if test -e "$imgname"  -a  "$imgname" -nt "$nm"; then
+    nm="$imgname"
   fi
-elif test -e $imgname; then
-  nm=$imgname
+elif test -e "$imgname"; then
+  nm="$imgname"
 fi
 if test $makelink = 1  -a  "$nm" != ""; then
-  echo "ln -s $nm $newname"
-  rm -rf $newname
-  ln -s $nm $newname
+  echo "ln -s '$nm' '$newname'"
+  rm -rf "$newname"
+  ln -s "$nm" "$newname"
 fi
 
 # Convert to FITS.
diff --git a/CEP/Imager/MWImager/src/mwimager-part b/CEP/Imager/MWImager/src/mwimager-part
index 0ea7ca2124b38feba989c45d79fa25215d8e5bf4..3860945b6bb9f20589a8972c7dd09f5b3098ca69 100755
--- a/CEP/Imager/MWImager/src/mwimager-part
+++ b/CEP/Imager/MWImager/src/mwimager-part
@@ -26,8 +26,8 @@
 
 
 # Find the path used to start the script.
-pgmpath=`dirname $0`
-pgmpath=`cd $pgmpath > /dev/null 2>&1  &&  pwd`
+pgmpath=`dirname "$0"`
+pgmpath=`cd "$pgmpath" > /dev/null 2>&1  &&  pwd`
 
 if test $# != 13; then
   echo "run as:   mwimager-part dummy dummy dummy dummy rank"
@@ -40,20 +40,20 @@ shift
 shift
 shift
 shift
-seqnr=$1
-msn=$2
-vds=$4
-envfile=$5
-psn=$6
-img=$7
-wd=$8
-dry=$9
+seqnr="$1"
+msn="$2"
+vds="$4"
+envfile="$5"
+psn="$6"
+img="$7"
+wd="$8"
+dry="$9"
 
-psnbase=`basename $psn`
+psnbase=`basename "$psn"`
 
 # Initialize environment.
 if test "$envfile" != ""; then
-  . $envfile
+  . "$envfile"
 fi
 
 # Get the data set name and the directory name of it.
@@ -62,19 +62,19 @@ if test "$wd" = ""  -o  "$wd" = "..."; then
 fi
 
 # Set to working directory.
-cd $wd  ||  exit 1
+cd "$wd"  ||  exit 1
 
 # If an MS name is given, create a temporary description file for it.
 if test -d "$vds"  -a  -e "$vds/table.dat"; then
   makevds "" "$vds" "$vds.tmpvds"  ||  exit 1
-  msn=$vds
+  msn="$vds"
   vds="$msn.tmpvds"
 fi
 
 if test "$img" = "casa"; then
-  $pgmpath/mwimager-casa $psn $seqnr $msn $vds $dry || exit 1
+  "$pgmpath/mwimager-casa" "$psn" "$seqnr" "$msn" "$vds" "$dry" || exit 1
 else
-  $pgmpath/mwimager-askap $psn $seqnr $msn $vds $dry || exit 1
+  "$pgmpath/mwimager-askap" "$psn" "$seqnr" "$msn" "$vds" "$dry" || exit 1
 fi
 
 # Convert to FITS.
diff --git a/CEP/Imager/MWImager/test/tmwimager.run b/CEP/Imager/MWImager/test/tmwimager.run
index 249568dcf89c2a929608ac83edb461db6faf0b5e..32b2cb9a753b9e5ede666cf9abbc507ae81573af 100755
--- a/CEP/Imager/MWImager/test/tmwimager.run
+++ b/CEP/Imager/MWImager/test/tmwimager.run
@@ -23,56 +23,73 @@
 # $Id$
 
 
+curdir=`pwd`
 # Set srcdir if undefined (if run outside 'make check')
 if test "$srcdir" = ""; then
-  srcdir=../../../test
+  srcdir=../../../../../../LOFAR/CEP/Imager/MWImager/test
 fi
-srcdir=`cd $srcdir/../src > /dev/null && pwd`
-exedir=`cd ../test > /dev/null && pwd`
+srcdir=`cd "$srcdir/../src" > /dev/null 2>&1  &&  pwd`
+exedir=`cd ../test > /dev/null 2>&1  &&  pwd`
+# Also define paths for getparsetvalue, startdistproc, finddproc.
+gpvdir=`cd ../../../../LCS/Common/src > /dev/null 2>&1  &&  pwd`
+sdpdir=`cd "$srcdir/../../../LMWCommon/src" > /dev/null 2>&1  &&  pwd`
+fdpdir=`cd ../../../LMWCommon/src > /dev/null 2>&1  &&  pwd`
+ppddir=`cd ../../../../LCS/pyparameterset/src > /dev/null 2>&1  &&  pwd`
+libdir=`cd ../../../../installed/lib > /dev/null 2>&1  &&  pwd`
 
 # Extend PATH.
-PATH=`pwd`:$srcdir:$exedir:$PATH
+PATH="`pwd`:$srcdir:$exedir:$gpvdir:$sdpdir:$fdpdir:$PATH"
 export PATH
 
+# Make it possible to find lofar.parameterset by setting path to installed
+# because _parameterset.so is only built when installed.
+PYTHONPATH="$libdir/python2.5/site-packages:$PYTHONPATH"
+export PYTHONPATH
+LD_LIBRARY_PATH="$libdir:$LD_LIBRARY_PATH"
+export LD_LIBRARY_PATH
+DYLD_LIBRARY_PATH="$libdir:$DYLD_LIBRARY_PATH"
+export DYLD_LIBRARY_PATH
+
 # Do a test using the askap imager.
 # Enclose output in angle brackets to exclude it from the assay diff.
 echo ">>>"
-$srcdir/../src/mwimager tmwimager.parset tmwimager.in_cd `pwd` tmwimager_tmp.log dry tmwimager_tmp.hfn
+"$srcdir/../src/mwimager" tmwimager.parset tmwimager.in_cd "`pwd`" tmwimager_tmp.log dry tmwimager_tmp.hfn
 echo "<<<"
 # Append the three log files to the output.
 # Wait for the subprocesses to finish.
 # Remove disturbing log4cxx lines
 sleep 5
-grep -v 'log4cxx:' tmwimager_tmp.log-0 | grep -v 'Debug: '
-grep -v 'log4cxx:' tmwimager_tmp.log-1 | grep -v 'Debug: '
-grep -v 'log4cxx:' tmwimager_tmp.log-2 | grep -v 'Debug: '
+grep -v 'log4cxx:' tmwimager_tmp.log-0 | grep -v 'Debug: ' | sed -e "s%$curdir%%g"
+
+grep -v 'log4cxx:' tmwimager_tmp.log-1 | grep -v 'Debug: ' | sed -e "s%$curdir%%g"
+grep -v 'log4cxx:' tmwimager_tmp.log-2 | grep -v 'Debug: ' | sed -e "s%$curdir%%g"
 # Remove the temporary parset part files.
 rm -f tmwimager.parset.part*
 
 # Do a test using the casa imager.
 # Enclose output in angle brackets to exclude it from the assay diff.
 echo ">>>"
-$srcdir/../src/mwimager -casa tmwimager.parset tmwimager.in_cd `pwd` tmwimager_tmp.log dry tmwimager_tmp.hfn
+"$srcdir/../src/mwimager" -casa tmwimager.parset tmwimager.in_cd "`pwd`" tmwimager_tmp.log dry tmwimager_tmp.hfn
 echo "<<<"
 # Append the three log files to the output.
 # Wait for the subprocesses to finish.
 # Remove disturbing log4cxx lines
 sleep 5
 echo ""
-grep -v 'log4cxx:' tmwimager_tmp.log-0 | grep -v 'Debug: '
-grep -v 'log4cxx:' tmwimager_tmp.log-1 | grep -v 'Debug: '
-grep -v 'log4cxx:' tmwimager_tmp.log-2 | grep -v 'Debug: '
+grep -v 'log4cxx:' tmwimager_tmp.log-0 | grep -v 'Debug: ' | sed -e "s%$curdir%%g"
+grep -v 'log4cxx:' tmwimager_tmp.log-1 | grep -v 'Debug: ' | sed -e "s%$curdir%%g"
+grep -v 'log4cxx:' tmwimager_tmp.log-2 | grep -v 'Debug: ' | sed -e "s%$curdir%%g"
 
 # Do a test using the casa imager with clean parameters.
 # Enclose output in angle brackets to exclude it from the assay diff.
 echo ">>>"
-$srcdir/../src/mwimager -casa tmwimager.in_parset2 tmwimager.in_cd `pwd` tmwimager_tmp.log dry tmwimager_tmp.hfn
+"$srcdir/../src/mwimager" -casa tmwimager.in_parset2 tmwimager.in_cd "`pwd`" tmwimager_tmp.log dry tmwimager_tmp.hfn
 echo "<<<"
 # Append the three log files to the output.
 # Wait for the subprocesses to finish.
 # Remove disturbing log4cxx lines
 sleep 5
 echo ""
-grep -v 'log4cxx:' tmwimager_tmp.log-0 | grep -v 'Debug: '
-grep -v 'log4cxx:' tmwimager_tmp.log-1 | grep -v 'Debug: '
-grep -v 'log4cxx:' tmwimager_tmp.log-2 | grep -v 'Debug: '
+grep -v 'log4cxx:' tmwimager_tmp.log-0 | grep -v 'Debug: ' | sed -e "s%$curdir%%g"
+grep -v 'log4cxx:' tmwimager_tmp.log-1 | grep -v 'Debug: ' | sed -e "s%$curdir%%g"
+grep -v 'log4cxx:' tmwimager_tmp.log-2 | grep -v 'Debug: ' | sed -e "s%$curdir%%g"
diff --git a/CEP/Imager/MWImager/test/tmwimager.stdout b/CEP/Imager/MWImager/test/tmwimager.stdout
index 12844f7f1269973505b9b80aabb2ebdb9e0f6ef5..25ba116d497c30a429fbf01a5aaa3b3d106317df 100644
--- a/CEP/Imager/MWImager/test/tmwimager.stdout
+++ b/CEP/Imager/MWImager/test/tmwimager.stdout
@@ -1,11 +1,11 @@
-msselect in=tmwimager.in_ms0 'baseline=CS* & RS*' out=tmwimager.in_ms0_sel
 >>>
-startdistproc -mode 0 -nomasterhost -dsn tmwimager.in_vd -hfn tmwimager_tmp.hfn -cdn tmwimager.in_cd -logfile tmwimager_tmp.log /Users/diepen/sim/LOFAR/CEP/BB/MWImager/src/mwimager-part /Users/diepen/sim/LOFAR/installed/gnu_opt /Users/diepen/sim/LOFAR/CEP/BB/MWImager/build/gnu_opt/test/tmwimager.parset /Users/diepen/sim/LOFAR/CEP/BB/MWImager/build/gnu_opt/test dry
-socketrun 0 tmwimager_tmp.hfn 0 tmwimager_tmp.log /Users/diepen/sim/LOFAR/CEP/BB/MWImager/src/mwimager-part /Users/diepen/sim/LOFAR/installed/gnu_opt /Users/diepen/sim/LOFAR/CEP/BB/MWImager/build/gnu_opt/test/tmwimager.parset /Users/diepen/sim/LOFAR/CEP/BB/MWImager/build/gnu_opt/test dry
-ssh localhost /Users/diepen/sim/LOFAR/CEP/BB/MWImager/src/mwimager-part socket localhost 0 3 0 tmwimager.in_ms0 /Users/diepen/sim/LOFAR/installed/gnu_opt /Users/diepen/sim/LOFAR/CEP/BB/MWImager/build/gnu_opt/test/tmwimager.parset /Users/diepen/sim/LOFAR/CEP/BB/MWImager/build/gnu_opt/test dry
-ssh localhost /Users/diepen/sim/LOFAR/CEP/BB/MWImager/src/mwimager-part socket localhost 0 3 1 tmwimager.in_ms1 /Users/diepen/sim/LOFAR/installed/gnu_opt /Users/diepen/sim/LOFAR/CEP/BB/MWImager/build/gnu_opt/test/tmwimager.parset /Users/diepen/sim/LOFAR/CEP/BB/MWImager/build/gnu_opt/test dry
-ssh localhost /Users/diepen/sim/LOFAR/CEP/BB/MWImager/src/mwimager-part socket localhost 0 3 2 tmwimager.in_ms2 /Users/diepen/sim/LOFAR/installed/gnu_opt /Users/diepen/sim/LOFAR/CEP/BB/MWImager/build/gnu_opt/test/tmwimager.parset /Users/diepen/sim/LOFAR/CEP/BB/MWImager/build/gnu_opt/test dry
+startdistproc -useenv -mode 0 -nomasterhost -dsn 'tmwimager.in_vd' -hfn '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager_tmp.hfn' -cdn '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.in_cd' -logfile 'tmwimager_tmp.log' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.parset' 'cimager' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'
+socketrun 0 /Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager_tmp.hfn '' 0 1 0 tmwimager_tmp.log /Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part /Users/diepen/startdistproc_14518.envfile /Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.parset cimager /Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test dry
+ssh -x -n 'localhost' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' socket 'localhost' 0 3 0 'tmwimager.in_ms0' 'node1:/usr' 'tmwimager.in_ms0.vds' '/Users/diepen/startdistproc_14518.envfile'  '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.parset' 'cimager' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'  &
+ssh -x -n 'localhost' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' socket 'localhost' 0 3 1 'tmwimager.in_ms1' 'node1:/usr' 'tmwimager.in_ms1.vds' '/Users/diepen/startdistproc_14518.envfile'  '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.parset' 'cimager' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'  &
+ssh -x -n 'localhost' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' socket 'localhost' 0 3 2 'tmwimager.in_ms2' 'node1:/usr' 'tmwimager.in_ms2.vds' '/Users/diepen/startdistproc_14518.envfile'  '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.parset' 'cimager' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'  &
 <<<
+msselect 'in=tmwimager.in_ms0' 'baseline=CS* & RS*' 'out=tmwimager.in_ms0_sel'
 Cimager.Images.Names=image.tmwimager_Dirty
 Cimager.Images.cellsize=[240arcsec,240arcsec]
 Cimager.Images.image.tmwimager_Dirty.direction=[230.35,-10.345,J2000]
@@ -33,7 +33,7 @@ Cimager.solver.Dirty.niter=10
 Cimager.solver.Dirty.scales=[0, 3]
 Cimager.solver.Dirty.verbose=True
 imgname=tmwimager.img
-msselect in=tmwimager.in_ms1 'baseline=CS* & RS*' out=tmwimager.in_ms1_sel
+msselect 'in=tmwimager.in_ms1' 'baseline=CS* & RS*' 'out=tmwimager.in_ms1_sel'
 Cimager.Images.Names=image.tmwimager_Dirty
 Cimager.Images.cellsize=[240arcsec,240arcsec]
 Cimager.Images.image.tmwimager_Dirty.direction=[230.35,-10.345,J2000]
@@ -61,7 +61,7 @@ Cimager.solver.Dirty.niter=10
 Cimager.solver.Dirty.scales=[0, 3]
 Cimager.solver.Dirty.verbose=True
 imgname=tmwimager.img
-msselect in=tmwimager.in_ms2 'baseline=CS* & RS*' out=tmwimager.in_ms2_sel
+msselect 'in=tmwimager.in_ms2' 'baseline=CS* & RS*' 'out=tmwimager.in_ms2_sel'
 Cimager.Images.Names=image.tmwimager_Dirty
 Cimager.Images.cellsize=[240arcsec,240arcsec]
 Cimager.Images.image.tmwimager_Dirty.direction=[230.35,-10.345,J2000]
@@ -89,6 +89,13 @@ Cimager.solver.Dirty.niter=10
 Cimager.solver.Dirty.scales=[0, 3]
 Cimager.solver.Dirty.verbose=True
 imgname=tmwimager.img
+>>>
+startdistproc -useenv -mode 0 -nomasterhost -dsn 'tmwimager.in_vd' -hfn '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager_tmp.hfn' -cdn '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.in_cd' -logfile 'tmwimager_tmp.log' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.parset' 'casa' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'
+socketrun 0 /Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager_tmp.hfn '' 0 1 0 tmwimager_tmp.log /Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part /Users/diepen/startdistproc_14708.envfile /Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.parset casa /Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test dry
+ssh -x -n 'localhost' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' socket 'localhost' 0 3 0 'tmwimager.in_ms0' 'node1:/usr' 'tmwimager.in_ms0.vds' '/Users/diepen/startdistproc_14708.envfile'  '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.parset' 'casa' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'  &
+ssh -x -n 'localhost' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' socket 'localhost' 0 3 1 'tmwimager.in_ms1' 'node1:/usr' 'tmwimager.in_ms1.vds' '/Users/diepen/startdistproc_14708.envfile'  '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.parset' 'casa' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'  &
+ssh -x -n 'localhost' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' socket 'localhost' 0 3 2 'tmwimager.in_ms2' 'node1:/usr' 'tmwimager.in_ms2.vds' '/Users/diepen/startdistproc_14708.envfile'  '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.parset' 'casa' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'  &
+<<<
 
 msselect "in=tmwimager.in_ms0" "baseline=CS* & RS*" "out=tmwimager.in_ms0_sel"
 lwimager "ms=tmwimager.in_ms0_sel" "image=tmwimager.img" "data=CORRECTED_DATA" "mode=channel" "npix=2048" "cellsize=240arcsec" "stokes=IQUV" "nchan=2" "chanstart=0" "img_nchan=1" "wprojplanes=201" "nfacets=1" "padding=1" "cachesize=50000000" "phasecenter=J2000,230.35,-10.345"
@@ -96,6 +103,13 @@ msselect "in=tmwimager.in_ms1" "baseline=CS* & RS*" "out=tmwimager.in_ms1_sel"
 lwimager "ms=tmwimager.in_ms1_sel" "image=tmwimager.img" "data=CORRECTED_DATA" "mode=channel" "npix=2048" "cellsize=240arcsec" "stokes=IQUV" "nchan=2" "chanstart=0" "img_nchan=1" "wprojplanes=201" "nfacets=1" "padding=1" "cachesize=50000000" "phasecenter=J2000,230.35,-10.345"
 msselect "in=tmwimager.in_ms2" "baseline=CS* & RS*" "out=tmwimager.in_ms2_sel"
 lwimager "ms=tmwimager.in_ms2_sel" "image=tmwimager.img" "data=CORRECTED_DATA" "mode=channel" "npix=2048" "cellsize=240arcsec" "stokes=IQUV" "nchan=2" "chanstart=0" "img_nchan=1" "wprojplanes=201" "nfacets=1" "padding=1" "cachesize=50000000" "phasecenter=J2000,230.35,-10.345"
+>>>
+startdistproc -useenv -mode 0 -nomasterhost -dsn 'tmwimager.in_vd' -hfn '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager_tmp.hfn' -cdn '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.in_cd' -logfile 'tmwimager_tmp.log' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.in_parset2' 'casa' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'
+socketrun 0 /Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager_tmp.hfn '' 0 1 0 tmwimager_tmp.log /Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part /Users/diepen/startdistproc_14824.envfile /Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.in_parset2 casa /Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test dry
+ssh -x -n 'localhost' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' socket 'localhost' 0 3 0 'tmwimager.in_ms0' 'node1:/usr' 'tmwimager.in_ms0.vds' '/Users/diepen/startdistproc_14824.envfile'  '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.in_parset2' 'casa' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'  &
+ssh -x -n 'localhost' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' socket 'localhost' 0 3 1 'tmwimager.in_ms1' 'node1:/usr' 'tmwimager.in_ms1.vds' '/Users/diepen/startdistproc_14824.envfile'  '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.in_parset2' 'casa' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'  &
+ssh -x -n 'localhost' '/Users/diepen/sim/space dir/LOFAR/CEP/Imager/MWImager/src/mwimager-part' socket 'localhost' 0 3 2 'tmwimager.in_ms2' 'node1:/usr' 'tmwimager.in_ms2.vds' '/Users/diepen/startdistproc_14824.envfile'  '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test/tmwimager.in_parset2' 'casa' '/Users/diepen/sim/space dir/build/gnu_debug/CEP/Imager/MWImager/test' 'dry'  &
+<<<
 
 lwimager "ms=tmwimager.in_ms0" "image=tmwimager.img" "data=CORRECTED_DATA" "mode=channel" "npix=2048" "cellsize=240arcsec" "stokes=IQUV" "nchan=2" "chanstart=0" "img_nchan=1" "wprojplanes=201" "nfacets=1" "padding=1" "cachesize=50000000" "phasecenter=J2000,230.35,-10.345"
 lwimager "ms=tmwimager.in_ms0" "image=tmwimager.img" "data=CORRECTED_DATA" "mode=channel" "npix=2048" "cellsize=240arcsec" "stokes=IQUV" "nchan=2" "chanstart=0" "img_nchan=1" "wprojplanes=201" "nfacets=1" "padding=1" "cachesize=50000000" "phasecenter=J2000,230.35,-10.345" "operation=multiscale" "niter=10" "gain=1.0" "threshold=0Jy" "fixed=False" "nscales=5" "uservector=[0, 3]" "model=tmwimager.img-model" "restored=tmwimager.img-restored" "residual=tmwimager.img-residual"
diff --git a/CEP/LMWCommon/src/cexecms b/CEP/LMWCommon/src/cexecms
index 3d4d3217ba62837b27f548479df345a5856b2771..1ab5e445fe727ff52b604afe12f0b61a82c4447d 100755
--- a/CEP/LMWCommon/src/cexecms
+++ b/CEP/LMWCommon/src/cexecms
@@ -64,6 +64,7 @@ showhelp()
   echo '                   giving /data/scratch/diepen/<BN.>_sel.MS'"'"'"'
   echo '             "/data/scratch/pipeline/L2011_22663/*SB0##*"'
   echo ' Note that quotes have to be used abundantly.'
+  echo ' Also note that (t)csh requires a ! to be escaped with a backslash.'
   echo ''
   echo ' Sometimes a command can be dangerous or take a long time to run.'
   echo ' In such a case it makes sense to execute it first with the -d option.'
diff --git a/CEP/LMWCommon/src/finddproc.cc b/CEP/LMWCommon/src/finddproc.cc
index 5f627f409308dd330e68491cc8573341041061b5..5f3b353305fa80b8fc39d2118ef240af3759c979 100644
--- a/CEP/LMWCommon/src/finddproc.cc
+++ b/CEP/LMWCommon/src/finddproc.cc
@@ -102,12 +102,24 @@ void makeFromFile (const string& vdsName, WorkersDesc& workers,
   makeFile (fileSys, fileNames, names, workers, cluster, type);
 }
 
-void makeFromDirs (const string& dirStr, WorkersDesc& workers,
+void makeFromDirs (int ndir, const string& dirStr, WorkersDesc& workers,
                    const ClusterDesc& cluster, NodeDesc::NodeType type)
 {
   const vector<NodeDesc>& nodes = cluster.getNodes();
   // Split string.
-  vector<string> dirs = StringUtil::split(dirStr, ',');
+  vector<string> dirnm = StringUtil::split(dirStr, ',');
+  // Extend the string to the nr of directories.
+  vector<string> dirs;
+  if (ndir > 0) {
+    dirs.reserve (ndir);
+    uint inx = 0;
+    for (int i=0; i<ndir; ++i) {
+      dirs.push_back (dirnm[inx++]);
+      if (inx >= dirnm.size()) inx = 0;
+    }
+  } else {
+    dirs = dirnm;
+  }
   // Create a list of FileSys from the dirs.
   vector<string> fileSys, fileNames;
   fileSys.reserve (dirs.size());
@@ -139,6 +151,7 @@ int main (int argc, const char* argv[])
   try {
     int nhead = 0;
     bool useDirs = false;
+    int ndir = 0;
     NodeDesc::NodeType type = NodeDesc::Compute;
     int st = 1;
     if (argc > st  &&  string(argv[st]) == "-storage") {
@@ -150,6 +163,11 @@ int main (int argc, const char* argv[])
       istr >> nhead;
       st += 2;
     }
+    if (argc > st+1  &&  string(argv[st]) == "-ndir") {
+      istringstream istr(argv[st+1]);
+      istr >> ndir;
+      st += 2;
+    }
     if (argc > st  &&  string(argv[st]) == "-dirs") {
       useDirs = true;
       ++st;
@@ -157,7 +175,7 @@ int main (int argc, const char* argv[])
     if (argc < st+2) {
       cerr << "Run as:  finddproc [-storage] [-nhead n] vdsdescname clusterdescname"
 	   << endl;
-      cerr << "    or   finddproc [-storage] [-nhead n] -dirs directories clusterdescname"
+      cerr << "    or   finddproc [-storage] [-nhead n] [-ndir n] -dirs directories clusterdescname"
            << endl;
       cerr << "             directories is a single argument separated by commas."
            << endl;
@@ -194,7 +212,7 @@ int main (int argc, const char* argv[])
     // First list the processes on head nodes.
     makeFromHead (nhead, workers);
     if (useDirs) {
-      makeFromDirs (argv[st], workers, cluster, type);
+      makeFromDirs (ndir, argv[st], workers, cluster, type);
     } else {
       makeFromFile (argv[st], workers, cluster, type);
     }
diff --git a/CEP/LMWCommon/src/socketrun b/CEP/LMWCommon/src/socketrun
index a0b1146b763cf6d79b3b7c7b9fc9401c080cbb76..c1cc0fbbf48627718d4b9909e652d33fba2a0713 100755
--- a/CEP/LMWCommon/src/socketrun
+++ b/CEP/LMWCommon/src/socketrun
@@ -95,42 +95,47 @@ np=`wc -l "$hfn" | awk '{print $1}'`
 # The first host is the master one.
 pidlist=
 rank=0
-for inline in `cat "$hfn"`
+
+# Start a remote process for each line in the hfn file.
+while read inline
 do
-  host=`echo $inline | awk -F'#' '{print $1'}`
-  rest=`echo $inline | awk -F'#' '{print $2'}`
-  part=`echo $rest | awk -F',' '{print $1'}`
-  fsys=`echo $rest | awk -F',' '{print $2'}`
-  partvds=`echo $rest | awk -F',' '{print $3'}`
-  if test "$part" = ""; then
-    part=.
-  fi
-  if test "$fsys" = ""; then
-    fsys=.
-  fi
-  if test "$partvds" = ""; then
-    partvds=.
-  fi
-  if test "$masterhost" = ""; then
-    masterhost="$host"
-  fi
-  # Quote all arguments ("$@" does not work over ssh).
-  arg=
-  for a in "$@"
-  do
-    arg="$arg '$a'"
-  done
-  echo "ssh -x -n $host $program socket $masterhost $port $np $rank '$part' '$fsys' '$partvds' '$envfile' $arg" " &"
-  if test "$dry" = 0; then
-    if test "$logfile" = ""; then
-      ssh -o NoHostAuthenticationForLocalhost=yes -o NumberOfPasswordPrompts=0 -x -n "$host" "$program" socket "$masterhost" "$port" "$np" "$rank" "$part" "$fsys" "$partvds" "$envfile" $arg &
-    else
-      ssh -o NoHostAuthenticationForLocalhost=yes -o NumberOfPasswordPrompts=0 -x -n "$host" "$program" socket "$masterhost" "$port" "$np" "$rank" "$part" "$fsys" "$partvds" "$envfile" $arg > "$logfile-$rank" 2>&1 &
+  if test "$inline" != ""; then
+    host=`echo $inline | awk -F'#' '{print $1'}`
+    rest=`echo $inline | awk -F'#' '{print $2'}`
+    part=`echo $rest | awk -F',' '{print $1'}`
+    fsys=`echo $rest | awk -F',' '{print $2'}`
+    partvds=`echo $rest | awk -F',' '{print $3'}`
+    if test "$part" = ""; then
+      part=.
+    fi
+    if test "$fsys" = ""; then
+      fsys=.
+    fi
+    if test "$partvds" = ""; then
+      partvds=.
+    fi
+    if test "$masterhost" = ""; then
+      masterhost="$host"
+    fi
+    # Quote all arguments ("$@" does not work over ssh).
+    arg=
+    for a in "$@"
+    do
+      arg="$arg '$a'"
+    done
+    echo "ssh -x -n '$host' '$program' socket '$masterhost' $port $np $rank '$part' '$fsys' '$partvds' '$envfile' $arg" " &"
+    if test "$dry" = 0; then
+      if test "$logfile" = ""; then
+        # Use extra quotes in argument that might contain whitespace.
+        ssh -o NoHostAuthenticationForLocalhost=yes -o NumberOfPasswordPrompts=0 -x -n "$host" "'$program'" socket "'$masterhost'" "'$port'" "$np" "$rank" "'$part'" "'$fsys'" "'$partvds'" "'$envfile'" $arg &
+      else
+        ssh -o NoHostAuthenticationForLocalhost=yes -o NumberOfPasswordPrompts=0 -x -n "$host" "'$program'" socket "'$masterhost'" "'$port'" "$np" "$rank" "'$part'" "'$fsys'" "'$partvds'" "'$envfile'" $arg > "$logfile-$rank" 2>&1 &
+      fi
+      pidlist="$pidlist $!"
     fi
-    pidlist="$pidlist $!"
+    rank=`expr $rank + 1`
   fi
-  rank=`expr $rank + 1`
-done
+done < "$hfn"
 
 # If needed, wait for the processes to end.
 # If a process ended unsuccessfully, kill the others if needed.
diff --git a/CEP/LMWCommon/src/startdistproc b/CEP/LMWCommon/src/startdistproc
index 836cb091a6f139f24bcd0007279fa97700aa3fcc..2d6414e391e76da90b9161f9cebbc56989367166 100755
--- a/CEP/LMWCommon/src/startdistproc
+++ b/CEP/LMWCommon/src/startdistproc
@@ -47,6 +47,7 @@ wait=1
 dsn=
 cdn=
 hfn=
+ndir=0
 dirs=
 fdp=
 master=localhost
@@ -102,6 +103,10 @@ do
   elif [ "$1" = "-nohfn" ]; then
     shift
     hfn=
+  elif [ "$1" = "-ndir" ]; then
+    shift
+    ndir="$1"
+    shift
   elif [ "$1" = "-dirs" ]; then
     shift
     dirs="$1"
@@ -180,7 +185,7 @@ if [ $help = 1 ]; then
   echo 'Run as:'
   echo ' startdistproc [-useenv [envfilename]] [-wait] [-killonfail]'
   echo '               [-cdn clusterdescname] [-dsn datasetname]'
-  echo '               [-dirs directories] [-mode mode]'
+  echo '               [-ndir ndirectories] [-dirs directories] [-mode mode]'
   echo '               [-hfn hostfilename] [-fdp finddproc-path]'
   echo '               [-logfile logfilename] [-dry]'
   echo '               [-extrahosts "host1 host2 ..."] [-noextrahosts]'
@@ -204,6 +209,8 @@ if [ $help = 1 ]; then
   echo '                        This is useful if a distributed dataset has'
   echo '                        to be created for which no VDS file exists yet.'
   echo '                        -dsn and dirs cannot be given both.'
+  echo '  -ndir ndirectories    The number of directories to use. If more than'
+  echo '                        length of dirs, dirs is wrapped.'
   echo '  -hfn hostfilename     The name of the generated hostfile.'
   echo '                        It defaults to /tmp/machinefile_${USER}_$$'
   echo '  -fdp finddproc-path   Path where to find finddproc.'
@@ -288,7 +295,7 @@ if test "$dsn" != ""; then
   fi
   "${fdp}finddproc" "$dsn" "$cdn" $masterh $extra > "$hfn" || exit 1
 else
-  "${fdp}finddproc" -dirs "$dirs" "$cdn" "$masterh" $extra > "$hfn" || exit 1
+  "${fdp}finddproc" -ndir "$ndir" -dirs "$dirs" "$cdn" "$masterh" $extra > "$hfn" || exit 1
 fi  
 np=`wc -l "$hfn" | awk '{print $1}'`
 
diff --git a/CEP/LMWCommon/test/tfinddproc.run b/CEP/LMWCommon/test/tfinddproc.run
index 20592019ea41951c64e7d20ded3980cf17b0c1b7..da784e956f6c8926c3da9bb4c5a85b2101a70823 100755
--- a/CEP/LMWCommon/test/tfinddproc.run
+++ b/CEP/LMWCommon/test/tfinddproc.run
@@ -26,3 +26,9 @@
 
 
 ../src/finddproc tfinddproc.in_vd tfinddproc.in_cd
+echo
+../src/finddproc -dirs /usr/local/xyx0,/usr/local/xyx1 tfinddproc.in_cd
+echo
+../src/finddproc -ndir 1 -dirs /usr/local/xyx0,/usr/local/xyx1 tfinddproc.in_cd
+echo
+../src/finddproc -ndir 5 -dirs /usr/local/xyx0,/usr/local/xyx1 tfinddproc.in_cd
diff --git a/CEP/LMWCommon/test/tfinddproc.stdout b/CEP/LMWCommon/test/tfinddproc.stdout
index dfe62a6d0a84c547e5a8e76c91fe362a4761b8f3..7dc648666eb8dafb40b38efd07ee82d76a3043df 100644
--- a/CEP/LMWCommon/test/tfinddproc.stdout
+++ b/CEP/LMWCommon/test/tfinddproc.stdout
@@ -1,3 +1,14 @@
 node1#/usr/local/xyx0,node1:/usr,/usr/local/xyx0.vds
 node2#/usr/local/xyx1,node1:/usr,/usr/local/xyx1.vds
 node1#/usr/local/xyx2,node1:/usr,/usr/local/xyx2.vds
+
+node1#/usr/local/xyx0,node1:/usr,/usr/local/xyx0
+node2#/usr/local/xyx1,node1:/usr,/usr/local/xyx1
+
+node1#/usr/local/xyx0,node1:/usr,/usr/local/xyx0
+
+node1#/usr/local/xyx0,node1:/usr,/usr/local/xyx0
+node2#/usr/local/xyx1,node1:/usr,/usr/local/xyx1
+node1#/usr/local/xyx0,node1:/usr,/usr/local/xyx0
+node2#/usr/local/xyx1,node1:/usr,/usr/local/xyx1
+node1#/usr/local/xyx0,node1:/usr,/usr/local/xyx0
diff --git a/CEP/LMWCommon/test/trundist.run b/CEP/LMWCommon/test/trundist.run
index 92c26da4849c656847e13638a926e6178803c0ce..b7a6ac72d0bb7bb250b6edc11340551b842aacc4 100755
--- a/CEP/LMWCommon/test/trundist.run
+++ b/CEP/LMWCommon/test/trundist.run
@@ -31,13 +31,13 @@ if test "$srcdir" = ""; then
 fi
 curdir=`pwd`
 # Set the src and make it absolute.
-ssdir=`cd $srcdir/../src > /dev/null 2>&1; pwd`
+ssdir=`cd "$srcdir/../src" > /dev/null 2>&1; pwd`
 sedir=`cd ../src > /dev/null 2>&1; pwd`
 
 # Add srcdir to PATH, so runlogpid can be found.
 # Add ../src to PATH, so finddproc can be found
 # Add test to PATH, so tstartdproc can be found.
-PATH=$ssdir:$sedir:$curdir:$PATH
+PATH="$ssdir:$sedir:$curdir:$PATH"
 export PATH
 
 # Run in dry mode.
@@ -45,7 +45,7 @@ echo 'echo "argc=$#"' > trundist_tmp.sh
 echo 'echo "argv=$*"' >> trundist_tmp.sh
 chmod 755 trundist_tmp.sh
 rundist -dry -cdn trundist.in_cd -hfn trundist_tmp.hfn -envfile trundist_tmp.env \
-$curdir/trundist_tmp.sh trundist.in_vd -arg "a b c d" > trundist_tmp.log
+"$curdir/trundist_tmp.sh" trundist.in_vd -arg "a b c d" > trundist_tmp.log
 echo "status=$?"
 cat trundist_tmp.hfn
 # Remove absolute directory path from logfiles.
@@ -57,7 +57,7 @@ echo 'echo "argv=$*"' >> trundist_tmp.sh
 chmod 755 trundist_tmp.sh
 rundist -cdn trundist.in_cd -hfn trundist_tmp.hfn -envfile trundist_tmp.env \
 -logfile trundist_tmp.logx -gdsout img \
-$curdir/trundist_tmp.sh trundist.in_vd -arg "a b c d" > trundist_tmp.log
+"$curdir/trundist_tmp.sh" trundist.in_vd -arg "a b c d" > trundist_tmp.log
 echo "status=$?"
 cat trundist_tmp.hfn
 # Remove absolute directory path from logfile.
diff --git a/CEP/LMWCommon/test/trundist.stdout b/CEP/LMWCommon/test/trundist.stdout
index 8ec709ea386629a068801a91725434435248daaa..e89979b43539a3aa0ce48918b7874e7f6b43e9b3 100644
--- a/CEP/LMWCommon/test/trundist.stdout
+++ b/CEP/LMWCommon/test/trundist.stdout
@@ -4,18 +4,18 @@ localhost#/usr/local/xyx1.ms,node1:/usr,/usr/local/xyx1.ms.vds
 localhost#/usr/local/xyx2.ms,node1:/usr,/usr/local/xyx2.ms.vds
 startdistproc -useenv 'curdir/trundist_tmp.env' -mode 0 -nomasterhost -dsn 'curdir/trundist.in_vd' -hfn 'curdir/trundist_tmp.hfn' -cdn 'curdir/trundist.in_cd' -logfile '' -dry 'curdir/trundist_tmp.sh' '' -arg a b c d
 socketrun 1 curdir/trundist_tmp.hfn '' 0 1 0  ssdir/rundist-part curdir/trundist_tmp.env curdir/trundist_tmp.sh  -arg a b c d
-ssh -x -n localhost ssdir/rundist-part socket localhost 0 3 0 '/usr/local/xyx0.ms' 'node1:/usr' '/usr/local/xyx0.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
-ssh -x -n localhost ssdir/rundist-part socket localhost 0 3 1 '/usr/local/xyx1.ms' 'node1:/usr' '/usr/local/xyx1.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
-ssh -x -n localhost ssdir/rundist-part socket localhost 0 3 2 '/usr/local/xyx2.ms' 'node1:/usr' '/usr/local/xyx2.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
+ssh -x -n 'localhost' 'ssdir/rundist-part' socket 'localhost' 0 3 0 '/usr/local/xyx0.ms' 'node1:/usr' '/usr/local/xyx0.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
+ssh -x -n 'localhost' 'ssdir/rundist-part' socket 'localhost' 0 3 1 '/usr/local/xyx1.ms' 'node1:/usr' '/usr/local/xyx1.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
+ssh -x -n 'localhost' 'ssdir/rundist-part' socket 'localhost' 0 3 2 '/usr/local/xyx2.ms' 'node1:/usr' '/usr/local/xyx2.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
 status=0
 localhost#/usr/local/xyx0.ms,node1:/usr,/usr/local/xyx0.ms.vds
 localhost#/usr/local/xyx1.ms,node1:/usr,/usr/local/xyx1.ms.vds
 localhost#/usr/local/xyx2.ms,node1:/usr,/usr/local/xyx2.ms.vds
 startdistproc -useenv 'curdir/trundist_tmp.env' -mode 0 -nomasterhost -dsn 'curdir/trundist.in_vd' -hfn 'curdir/trundist_tmp.hfn' -cdn 'curdir/trundist.in_cd' -logfile 'trundist_tmp.logx'  'curdir/trundist_tmp.sh' '' -arg a b c d
 socketrun 0 curdir/trundist_tmp.hfn '' 0 1 0 trundist_tmp.logx ssdir/rundist-part curdir/trundist_tmp.env curdir/trundist_tmp.sh  -arg a b c d
-ssh -x -n localhost ssdir/rundist-part socket localhost 0 3 0 '/usr/local/xyx0.ms' 'node1:/usr' '/usr/local/xyx0.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
-ssh -x -n localhost ssdir/rundist-part socket localhost 0 3 1 '/usr/local/xyx1.ms' 'node1:/usr' '/usr/local/xyx1.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
-ssh -x -n localhost ssdir/rundist-part socket localhost 0 3 2 '/usr/local/xyx2.ms' 'node1:/usr' '/usr/local/xyx2.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
+ssh -x -n 'localhost' 'ssdir/rundist-part' socket 'localhost' 0 3 0 '/usr/local/xyx0.ms' 'node1:/usr' '/usr/local/xyx0.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
+ssh -x -n 'localhost' 'ssdir/rundist-part' socket 'localhost' 0 3 1 '/usr/local/xyx1.ms' 'node1:/usr' '/usr/local/xyx1.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
+ssh -x -n 'localhost' 'ssdir/rundist-part' socket 'localhost' 0 3 2 '/usr/local/xyx2.ms' 'node1:/usr' '/usr/local/xyx2.ms.vds' 'curdir/trundist_tmp.env'  'curdir/trundist_tmp.sh' '' '-arg' 'a b c d'  &
 Creating global VDS file trundist.img
 argc=4
 argv=0 /usr/local/xyx0.ms -arg a b c d
diff --git a/CEP/LMWCommon/test/tstartdproc.in_run b/CEP/LMWCommon/test/tstartdproc.in_run
index 1d885ff5fd11405e849fd22281525d54263c7fd0..5be7cdd01833e1be6319fe51c3a7b2e102e84676 100755
--- a/CEP/LMWCommon/test/tstartdproc.in_run
+++ b/CEP/LMWCommon/test/tstartdproc.in_run
@@ -1,13 +1,13 @@
 #!/bin/sh
 
-pgmpath=`dirname $0`
+pgmpath=`dirname "$0"`
 
 # Get the envfile name and rank.
 rank=$5
-envfile=$9
+envfile="$9"
 
 # Source the envfile.
-. $envfile
+. "$envfile"
 
 # Start the test program and log its pid.
 # It will sleep for some seconds and return the given status.
@@ -20,7 +20,7 @@ envfile=$9
 # way the logged (and killed) process pid is the script and not the sleep command
 # which would become a zombie.
 if test $rank = 1; then
-  runlogpid $envfile $rank tstartdproc 5  1
+  runlogpid "$envfile" $rank tstartdproc 5  1
 else
-  runlogpid $envfile $rank tstartdproc 30 0
+  runlogpid "$envfile" $rank tstartdproc 30 0
 fi
diff --git a/CEP/LMWCommon/test/tstartdproc.run b/CEP/LMWCommon/test/tstartdproc.run
index f0e52e16462c4ff01e1f144e0bca807d63e35f2a..88d721939219986b44f6bfa6bba8a1c29abdc84f 100755
--- a/CEP/LMWCommon/test/tstartdproc.run
+++ b/CEP/LMWCommon/test/tstartdproc.run
@@ -31,13 +31,13 @@ if test "$srcdir" = ""; then
 fi
 curdir=`pwd`
 # Set the src and make it absolute.
-ssdir=`cd $srcdir/../src > /dev/null 2>&1; pwd`
+ssdir=`cd "$srcdir/../src" > /dev/null 2>&1; pwd`
 sedir=`cd ../src > /dev/null 2>&1; pwd`
 
 # Add srcdir to PATH, so runlogpid can be found.
 # Add ../src to PATH, so finddproc can be found
 # Add test to PATH, so tstartdproc can be found.
-PATH=$ssdir:$sedir:$curdir:$PATH
+PATH="$ssdir:$sedir:$curdir:$PATH"
 export PATH
 
 startdistproc -dsn tstartdproc.in_vd -cdn tstartdproc.in_cd \
@@ -52,7 +52,7 @@ echo "status=$?"
 cat tstartdproc_tmp.out1
 
 echo ''
-startdistproc -useenv $curdir/tstartdproc_tmp.out2.envfile \
+startdistproc -useenv "$curdir/tstartdproc_tmp.out2.envfile" \
 -dsn tstartdproc.in_vd -cdn tstartdproc.in_cd \
 -mode 3851 -dry -hfn tstartdproc_tmp.out2 -fdp ../src prog1 -arg "a b" \
 > tstartdproc_tmp.log2
@@ -62,10 +62,10 @@ cat tstartdproc_tmp.out2
 sed -e "s%$curdir%%g" tstartdproc_tmp.log2
 
 echo ''
-startdistproc -killonfail -useenv $curdir/tstartdproc_tmp.out3.envfile \
--logfile $curdir/tstartdproc_tmp.log3b \
+startdistproc -killonfail -useenv "$curdir/tstartdproc_tmp.out3.envfile" \
+-logfile "$curdir/tstartdproc_tmp.log3b" \
 -dsn tstartdproc.in_vd -cdn tstartdproc.in_cd1 -nostartmaster \
--mode 3851 -hfn tstartdproc_tmp.out3 -fdp ../src `pwd`/tstartdproc.in_run \
+-mode 3851 -hfn tstartdproc_tmp.out3 -fdp ../src "`pwd`/tstartdproc.in_run" \
 > tstartdproc_tmp.log3a
 echo "status=$?"
 cat tstartdproc_tmp.out3
diff --git a/CEP/LMWCommon/test/tstartdproc.stdout b/CEP/LMWCommon/test/tstartdproc.stdout
index 7a7644699a521b280b73fee0803df75aeb5524fc..47a112f41d5d673d2c0a7d3281897242248094a8 100644
--- a/CEP/LMWCommon/test/tstartdproc.stdout
+++ b/CEP/LMWCommon/test/tstartdproc.stdout
@@ -18,19 +18,19 @@ node1#/usr/local/xyx0,node1:/usr,/usr/local/xyx0.vds
 node2#/usr/local/xyx1,node1:/usr,/usr/local/xyx1.vds
 node1#/usr/local/xyx2,node1:/usr,/usr/local/xyx2.vds
 socketrun 1 tstartdproc_tmp.out2 'localhost' 3851 1 0  prog1 /tstartdproc_tmp.out2.envfile -arg a b
-ssh -x -n localhost prog1 socket localhost 3851 4 0 '.' '.' '.' '/tstartdproc_tmp.out2.envfile'  '-arg' 'a b'  &
-ssh -x -n node1 prog1 socket localhost 3851 4 1 '/usr/local/xyx0' 'node1:/usr' '/usr/local/xyx0.vds' '/tstartdproc_tmp.out2.envfile'  '-arg' 'a b'  &
-ssh -x -n node2 prog1 socket localhost 3851 4 2 '/usr/local/xyx1' 'node1:/usr' '/usr/local/xyx1.vds' '/tstartdproc_tmp.out2.envfile'  '-arg' 'a b'  &
-ssh -x -n node1 prog1 socket localhost 3851 4 3 '/usr/local/xyx2' 'node1:/usr' '/usr/local/xyx2.vds' '/tstartdproc_tmp.out2.envfile'  '-arg' 'a b'  &
+ssh -x -n 'localhost' 'prog1' socket 'localhost' 3851 4 0 '.' '.' '.' '/tstartdproc_tmp.out2.envfile'  '-arg' 'a b'  &
+ssh -x -n 'node1' 'prog1' socket 'localhost' 3851 4 1 '/usr/local/xyx0' 'node1:/usr' '/usr/local/xyx0.vds' '/tstartdproc_tmp.out2.envfile'  '-arg' 'a b'  &
+ssh -x -n 'node2' 'prog1' socket 'localhost' 3851 4 2 '/usr/local/xyx1' 'node1:/usr' '/usr/local/xyx1.vds' '/tstartdproc_tmp.out2.envfile'  '-arg' 'a b'  &
+ssh -x -n 'node1' 'prog1' socket 'localhost' 3851 4 3 '/usr/local/xyx2' 'node1:/usr' '/usr/local/xyx2.vds' '/tstartdproc_tmp.out2.envfile'  '-arg' 'a b'  &
 
 status=1
 localhost#/usr/local/xyx0,node1:/usr,/usr/local/xyx0.vds
 localhost#/usr/local/xyx1,node1:/usr,/usr/local/xyx1.vds
 localhost#/usr/local/xyx2,node1:/usr,/usr/local/xyx2.vds
 socketrun 0 tstartdproc_tmp.out3 'localhost' 3851 1 1 /tstartdproc_tmp.log3b /tstartdproc.in_run /tstartdproc_tmp.out3.envfile
-ssh -x -n localhost /tstartdproc.in_run socket localhost 3851 3 0 '/usr/local/xyx0' 'node1:/usr' '/usr/local/xyx0.vds' '/tstartdproc_tmp.out3.envfile'   &
-ssh -x -n localhost /tstartdproc.in_run socket localhost 3851 3 1 '/usr/local/xyx1' 'node1:/usr' '/usr/local/xyx1.vds' '/tstartdproc_tmp.out3.envfile'   &
-ssh -x -n localhost /tstartdproc.in_run socket localhost 3851 3 2 '/usr/local/xyx2' 'node1:/usr' '/usr/local/xyx2.vds' '/tstartdproc_tmp.out3.envfile'   &
+ssh -x -n 'localhost' '/tstartdproc.in_run' socket 'localhost' 3851 3 0 '/usr/local/xyx0' 'node1:/usr' '/usr/local/xyx0.vds' '/tstartdproc_tmp.out3.envfile'   &
+ssh -x -n 'localhost' '/tstartdproc.in_run' socket 'localhost' 3851 3 1 '/usr/local/xyx1' 'node1:/usr' '/usr/local/xyx1.vds' '/tstartdproc_tmp.out3.envfile'   &
+ssh -x -n 'localhost' '/tstartdproc.in_run' socket 'localhost' 3851 3 2 '/usr/local/xyx2' 'node1:/usr' '/usr/local/xyx2.vds' '/tstartdproc_tmp.out3.envfile'   &
 Remote process (pid=nn) with rank 1 ended with error status 1
 Killing the other remote processes ...
 ssh -x localhost kill -9 >>> 20373 <<<
diff --git a/CEP/MS/src/CMakeLists.txt b/CEP/MS/src/CMakeLists.txt
index ff4cae11ac01b1dc15906602b91c4f098bb87d29..3df832e6492f284d67d8632a081f6fc3ff155450 100644
--- a/CEP/MS/src/CMakeLists.txt
+++ b/CEP/MS/src/CMakeLists.txt
@@ -31,6 +31,9 @@ install(PROGRAMS
   makemsdistr
   makemsdistr-part
   movemss
+  expandcalps
+  expandimageps
+  expandtargetps
   DESTINATION bin)
 
 # Python modules.
diff --git a/CEP/MS/src/combinevds.cc b/CEP/MS/src/combinevds.cc
index 207dc72dd9bed92eb5bbf8b26de95ea29df3b335..7ac57bda2fe5713ce6489a5c16957266f0880b98 100644
--- a/CEP/MS/src/combinevds.cc
+++ b/CEP/MS/src/combinevds.cc
@@ -24,6 +24,7 @@
 #include <MS/VdsMaker.h>
 #include <MS/Package__Version.h>
 #include <Common/LofarLogger.h>
+#include <Common/StringUtil.h>
 #include <Common/SystemUtil.h>
 #include <stdexcept>
 #include <iostream>
@@ -48,7 +49,9 @@ int main (int argc, const char* argv[])
     vector<string> vdsNames;
     vdsNames.reserve (argc-2);
     for (int i=2; i<argc; ++i) {
-      vdsNames.push_back (argv[i]);
+      // Multiple names can be given separated by commas.
+      vector<string> names = StringUtil::split (string(argv[i]), ',');
+      vdsNames.insert (vdsNames.end(), names.begin(), names.end());
     }
     // Combine them.
     VdsMaker::combine (argv[1], vdsNames);
diff --git a/CEP/MS/src/makemsdistr b/CEP/MS/src/makemsdistr
index 1ebb6c8de00283d1f75d0268cd7f4244ecc56528..92f84a6d85077873aa707ed87ece0055f2d9d5d0 100755
--- a/CEP/MS/src/makemsdistr
+++ b/CEP/MS/src/makemsdistr
@@ -22,10 +22,12 @@
 #
 # $Id$
 
+echo $#
+echo $*
 
 # Find the path used to start the script.
-pgmpath=`dirname $0`
-pgmpath=`cd $pgmpath > /dev/null 2>&1  &&  pwd`
+pgmpath=`dirname "$0"`
+pgmpath=`cd "$pgmpath" > /dev/null 2>&1  &&  pwd`
 
 hfn=
 # Handle possible options.
@@ -57,82 +59,80 @@ if test $# = 0  -o  $# = 1; then
   exit 1
 fi
 
-psn=$1
+psn="$1"
 shift
-cdn=$1
+cdn="$1"
 shift
-dirs=$*
+msdirs=
+if [ $# != 0 ]; then
+  msdirs="$1"
+  shift
+fi
+while [ $# != 0 ]
+do
+  msdirs="$msdirs,$1"
+  shift
+done
+
 test "$psn" = ""  &&  psn="makems.cfg"
-parsetcdn=`getparsetvalue $psn ClusterDescName 2>/dev/null` ||  parsetcdn=
-test "$cdn" = ""  &&  cdn=$parsetcdn
+parsetcdn=`getparsetvalue "$psn" ClusterDescName 2>/dev/null` ||  parsetcdn=
+test "$cdn" = ""  &&  cdn="$parsetcdn"
 if test "$cdn" = ""; then
   echo "No ClusterDescName given on command line nor parset file"
   exit 1
 fi
 
 # Find out the number of parts and the nodes to use.
-nparts=`getparsetvalue $psn NParts`  ||  exit 1
+nparts=`getparsetvalue "$psn" NParts`  ||  exit 1
 if test "$nparts" = "0"; then
   nparts=1
 fi
 if test "$dirs" = ""; then
-  dirs=`getparsetvalue $psn Dirs`   ||  dirs=
+  # Get dirs from parset and remove brackets and possible whitespace round comma
+  # Note that [][] in the pattern means ] or [.
+  dirs=`getparsetvalue "$psn" Dirs | sed -e 's/ *, */,/g' -e 's/ *[][] *//g'`  ||  dirs=
+fi
+if test "$dirs" = ""; then
+  dirs=.
 fi
 
 # Get name of MS and path of VDS file.
-msname=`getparsetvalue $psn MSName` ||  exit 1
+msname=`getparsetvalue "$psn" MSName` ||  exit 1
 # Use ms-path if no VDSPath given.
-mspath=`dirname $msname`
+mspath=`dirname "$msname"`
 # Create ms basename without a possible _.%d representing the seqnr.
-msbase=`basename $msname | sed -e 's/_[^_]*%d//'`
-msvdsp=`getparsetvalue $psn VDSPath` ||  msvdsp=$mspath
+msbase=`basename "$msname" | sed -e 's/_[^_]*%d//'`
+msvdsp=`getparsetvalue "$psn" VDSPath` ||  msvdsp=$mspath
 # Make VDS-path absolute.
-msvdsp=`cd $msvdsp > /dev/null  &&  pwd`  ||  exit 1
-
-# Find all directories and turn it into a list separated by commas.
-# Possible whitespace and [] are removed.
-dirs=`echo "$dirs" | sed -e 's/\[//g' -e 's/]//g' -e 's/,/ /g' -e 's/"//g' -e "s/'//g"`
-if test "$dirs" = ""; then
-  dirs=.
-fi
-
-# Make dirs nparts long.
-msdirs=
-np=0
-while test $np != $nparts
-do
-  for dir in $dirs
-  do
-    if test $np = 0; then
-      msdirs=$dir
-    else 
-      msdirs="$msdirs,$dir"
-    fi
-    np=`expr $np + 1`
-    if test $np = $nparts; then
-      break;
-    fi
-  done
-done
+msvdsp=`cd "$msvdsp" > /dev/null  &&  pwd`  ||  exit 1
 
 # Copy parset to the VDS-path that is certainly globally accessible.
-psname=$msvdsp/$msbase.$USER.$$.parset
-cp $psn $psname
+psname="$msvdsp/$msbase.$USER.$$.parset"
+cp "$psn" "$psname"
 # Make clusterdescname absolute.
-dn=`dirname $cdn`
-cdn=`cd $dn > /dev/null; pwd`/`basename $cdn`
+dn=`dirname "$cdn"`
+cdn=`cd "$dn" > /dev/null; pwd`/`basename "$cdn"`
 
 # Start the distributed processes.
 # They write the name of the created VDS-file into $msvdsp/$msbase.gds_*.
-rm -f $msvdsp/$msbase.gds_*
-echo "startdistproc -useenv -mode 0 -nomasterhost -dirs $msdirs -cdn $cdn -hfn '$hfn' $pgmpath/makemsdistr-part $psname $msvdsp $msvdsp/$msbase.gds $cdn"
-startdistproc -useenv -mode 0 -nomasterhost -dirs "$msdirs" -cdn $cdn -hfn "$hfn" $pgmpath/makemsdistr-part $psname $msvdsp $msvdsp/$msbase.gds $cdn
+rm -f "$msvdsp/$msbase".gds_*
+echo "startdistproc -useenv -mode 0 -nomasterhost -ndir "$nparts" -dirs '$msdirs' -cdn '$cdn' -hfn '$hfn' '$pgmpath/makemsdistr-part' '$psname' '$msvdsp' '$msvdsp/$msbase.gds' '$cdn'"
+startdistproc -useenv -mode 0 -nomasterhost -ndir "$nparts" -dirs "$msdirs" -cdn "$cdn" -hfn "$hfn" "$pgmpath/makemsdistr-part" "$psname" "$msvdsp" "$msvdsp/$msbase.gds" "$cdn"
 
 # Combine all VDS files.
-vdsnames=`cat $msvdsp/$msbase.gds_*`
-rm -f $msvdsp/$msbase.gds_*
-rm -f $psname
+rm -f "$psname"
+vdsnames=""
+cat "$msvdsp/$msbase".gds_* > "$msvdsp/$msbase.gdsall"
+while read name
+do
+  if test "$vdsnames" = ""; then
+    vdsnames="$name"
+  else
+    vdsnames="$vdsnames,$name"
+  fi
+done < "$msvdsp/$msbase.gdsall"
+rm -f "$msvdsp/$msbase".gds_* "$msvdsp/$msbase.gdsall"
 
-combinevds $msvdsp/$msbase.gds $vdsnames
+combinevds "$msvdsp/$msbase.gds" "$vdsnames"
 echo "Created $nparts distributed MS parts and their VDS files"
 echo "Created global VDS file $msvdsp/$msbase.gds"
diff --git a/CEP/MS/src/makemsdistr-part b/CEP/MS/src/makemsdistr-part
index 18a02f3550cb85b007700098a29504c7babcda5a..32b8eef485ae745df62908d2d456bf3eea611417 100755
--- a/CEP/MS/src/makemsdistr-part
+++ b/CEP/MS/src/makemsdistr-part
@@ -24,8 +24,8 @@
 
 
 # Find the path used to start the script.
-pgmpath=`dirname $0`
-pgmpath=`cd $pgmpath > /dev/null 2>&1  &&  pwd`
+pgmpath=`dirname "$0"`
+pgmpath=`cd "$pgmpath" > /dev/null 2>&1  &&  pwd`
 
 if test $# != 13; then
   echo "run as:   makemsdistr-part dummy dummy dummy dummy rank"
@@ -38,20 +38,22 @@ shift
 shift
 shift
 shift
-seqnr=$1
-msn=$2
-envfile=$5
-psn=$6
-vdspath=$7
-vdsout=$8
-cdn=$9
+seqnr="$1"
+msn="$2"
+envfile="$5"
+psn="$6"
+vdspath="$7"
+vdsout="$8"
+cdn="$9"
 
 # Initialize environment.
 if test "$envfile" != ""; then
-  . $envfile
+  . "$envfile"
 fi
 
+echo "PATH=$PATH"
+pwd
 # Create the MS.
 # Grep the VDSname from the result.
-echo "$pgmpath/makems $psn $seqnr $msn $vdspath $cdn > ${vdsout}_$seqnr"
-$pgmpath/makems $psn $seqnr $msn $vdspath $cdn | grep "vds=" | sed -e 's/vds=//' > ${vdsout}_$seqnr 
+echo "makems $psn $seqnr $msn $vdspath $cdn > ${vdsout}_$seqnr"
+"makems" "$psn" "$seqnr" "$msn" "$vdspath" "$cdn" | grep "vds=" | sed -e 's/vds=//' > "${vdsout}_$seqnr" 
diff --git a/CEP/MS/test/tmakems.run b/CEP/MS/test/tmakems.run
index 4e73f04aec0ecc27eea2b8c243992203a5bf0353..7252196406a1451de5b41be520badc7c29b1b38e 100755
--- a/CEP/MS/test/tmakems.run
+++ b/CEP/MS/test/tmakems.run
@@ -27,12 +27,14 @@ if test "$srcdir" = ""; then
   srcdir="../../../../../LOFAR/CEP/MS/test"
 fi
 
-# Symlink to scripts/programs, so they can be started from test directory.
-ln -s $srcdir/../src/makemsdistr .
-ln -s $srcdir/../src/makemsdistr-part .
-ln -s ../src/combinevds .
-ln -s ../src/makems .
-PATH=.:$PATH
+# Add path of makemsdistr, combinevds/makems, getparsetvalue, startdistproc.
+# Make all of them absolute.
+p1=`cd "$srcdir/../src" > /dev/null 2>&1  &&  pwd`
+p2=`cd "../src" > /dev/null 2>&1  &&  pwd`
+p3=`cd "../../../LCS/Common/src" > /dev/null 2>&1  &&  pwd`
+p4=`cd "$srcdir/../../LMWCommon/src" > /dev/null 2?&1  &&  pwd`
+p5=`cd "$srcdir/../../LMWCommxxon/src" > /dev/null 2>&1  &&  pwd`
+PATH=".:$p1:$p2:$p3:$p4:$p5:$PATH"
 export PATH
 
 # Create ClusterDesc file using pwd.
@@ -42,7 +44,7 @@ ClusterName = cl
 NNodes = 1
 Node0.NodeName = localhost
 Node0.NodeFileSys = [/usr]
-Node0.NodeMountPoints = [$wd]
+Node0.NodeMountPoints = ["$wd"]
 EOF
 
 # Take care that system-dependent output is excluded.
@@ -56,7 +58,7 @@ echo "AntennaTableName=$wd/tmakems_tmp/WSRT_ANTENNA" >> tmakems.parset
 
 
 # Create MS parts in a distributed way.
-makemsdistr -hfn $wd/tmakems_tmp/hfn tmakems.parset $wd/tmakems_tmp.cd $wd
+makemsdistr -hfn "$wd/tmakems_tmp/hfn" tmakems.parset "$wd/tmakems_tmp.cd" "$wd"
 echo "<<<"
 
 # Print global VDS file without system-dependent names.
@@ -67,6 +69,3 @@ echo ">>>"
 makems tmakems.parset
 echo "<<<"
 grep -v 'Name *=' tmakems_tmp.ms.gds | grep -v "ClusterDesc *="
-
-# Clean up.
-rm makemsdistr makemsdistr-part combinevds makems
diff --git a/CEP/ParmDB/src/makesourcedb.cc b/CEP/ParmDB/src/makesourcedb.cc
index 11b227910083ffb6fc67a9e3fc5b2579dbad053b..8aaebb4b6bf56ad4e0b2f3ac995ff0303ecebe35 100644
--- a/CEP/ParmDB/src/makesourcedb.cc
+++ b/CEP/ParmDB/src/makesourcedb.cc
@@ -973,10 +973,7 @@ void make (const string& in, const string& out,
   int nrsource    = 0;
   int nrpatchfnd  = 0;
   int nrsourcefnd = 0;
-  if (in.empty()) {
-    process (string(), pdb, sdbf, check, nrpatch, nrsource,
-             nrpatchfnd, nrsourcefnd, searchInfo);
-  } else {
+  if (! in.empty()) {
     ifstream infile(in.c_str());
     ASSERTSTR (infile, "File " << in << " could not be opened");
     casa::Regex regexf("^[ \t]*[fF][oO][rR][mM][aA][tT][ \t]*=.*");
@@ -1015,11 +1012,11 @@ void make (const string& in, const string& out,
        << pdb.getParmDBMeta().getTableName() << endl;
   vector<string> dp(pdb.findDuplicatePatches());
   if (dp.size() > 0) {
-    cout << "Duplicate patches: " << dp << endl;
+    cerr << "Duplicate patches: " << dp << endl;
   }
   vector<string> ds(pdb.findDuplicateSources());
   if (ds.size() > 0) {
-    cout << "Duplicate sources: " << ds << endl;
+    cerr << "Duplicate sources: " << ds << endl;
   }
 }
 
@@ -1031,6 +1028,9 @@ string readFormat (string file, const string& catFile)
   if (file.empty()) {
     file = catFile;
   }
+  if (file.empty()) {
+    return string();
+  }
   // Read file until format line is found or until non-comment is found.
   ifstream infile(file.c_str());
   ASSERTSTR (infile, "File " << file
@@ -1075,13 +1075,12 @@ int main (int argc, char* argv[])
   try {
     // Get the inputs.
     Input inputs(1);
-    inputs.version ("GvD 2011-Sep-01");
+    inputs.version ("GvD 2011-Feb-17");
     inputs.create("in", "",
                   "Input file name", "string");
     inputs.create("out", "",
                   "Output sourcedb name", "string");
-    inputs.create("format", "Name,Type,Ra,Dec,I,Q,U,V,MajorAxis,"
-                            "MinorAxis,Orientation",
+    inputs.create("format", "",
                   "Format of the input lines or name of file containing format",
                   "string");
     inputs.create("append", "true",
@@ -1125,10 +1124,15 @@ int main (int argc, char* argv[])
       // Read format from file.
       format = readFormat (format.substr(st), in);
     }
+    // Use default if empty format.
+    if (format.empty()) {
+      cerr << "No format string found; using default format" << endl;
+      format = "Name,Type,Ra,Dec,I,Q,U,V,MajorAxis,MinorAxis,Orientation";
+    }
     make (in, out, format, append, check,
           getSearchInfo (center, radius, width));
   } catch (Exception& x) {
-    std::cerr << "Caught LOFAR exception: " << x << std::endl;
+    cerr << "Caught LOFAR exception: " << x << endl;
     return 1;
   }
   
diff --git a/CEP/ParmDB/src/parmdbremote-scr b/CEP/ParmDB/src/parmdbremote-scr
index 998397fc6cb50aeae617fe2cfaf6d3f2a8736d44..28a08cc86b691a9cdd61b8b6f7c18072520528c2 100755
--- a/CEP/ParmDB/src/parmdbremote-scr
+++ b/CEP/ParmDB/src/parmdbremote-scr
@@ -29,21 +29,21 @@ if test $# != 10; then
   echo "                           wd"
   exit 1
 fi
-envfile=$9
-wd=${10}
+envfile="$9"
+wd="${10}"
 
 # Initialize environment.
 if test "$envfile" != ""; then
-  . $envfile
+  . "$envfile"
 fi
 
 # cd to work directory and add . to PATH for test purposes (make check).
-if test -d $wd; then
-  cd $wd
-  PATH=.:$PATH
+if test -d "$wd"; then
+  cd "$wd"
+  PATH=".:$PATH"
   export PATH
 fi
 
-parmdbremote $2 $3 $6    # host,port,parmdb-part
+parmdbremote "$2" "$3" "$6"    # host,port,parmdb-part
 
-rm -f $envfile
+rm -f "$envfile"
diff --git a/CEP/ParmDB/src/setupparmdb b/CEP/ParmDB/src/setupparmdb
index 6fa3975f49d9f6beeeeece69b010f332e24a7097..46a40eae49852c94ab2f348063721bfb9dc0f649 100755
--- a/CEP/ParmDB/src/setupparmdb
+++ b/CEP/ParmDB/src/setupparmdb
@@ -23,8 +23,8 @@
 # $Id$
 
 # Find the path used to start the script.
-pgmpath=`dirname $0`
-pgmpath=`cd $pgmpath > /dev/null 2>&1  &&  pwd`
+pgmpath=`dirname "$0"`
+pgmpath=`cd "$pgmpath" > /dev/null 2>&1  &&  pwd`
 
 # Get possible options.
 overwrite=0
@@ -81,16 +81,16 @@ if test $help = 1; then
   exit 1
 fi
 
-msvds=$1
+msvds="$1"
 shift
 pdbfile=
 if test $# != 0; then
-  pdbfile=$1
+  pdbfile="$1"
   shift
 fi
 pdb=
 if test $# != 0; then
-  pdb=$1
+  pdb="$1"
   shift
 fi
 if test "$pdb" = ""; then
@@ -98,17 +98,17 @@ if test "$pdb" = ""; then
 fi
 cdn=
 if test $# != 0; then
-  cdn=$1
+  cdn="$1"
   shift
 fi
 logfile=
 if test $# != 0; then
-  logfile=$1
+  logfile="$1"
   shift
 fi
 dry=
 if test $# != 0; then
-  dry=$1
+  dry="$1"
   shift
 fi
 if test "$dry" = ""; then
@@ -116,12 +116,12 @@ if test "$dry" = ""; then
 fi
 hfn=
 if test $# != 0; then
-  hfn=$1
+  hfn="$1"
   shift
 fi
 wd=
 if test $# != 0; then
-  wd=$1
+  wd="$1"
   shift
 fi
 if test "$wd" = ""; then
@@ -131,8 +131,8 @@ fi
 # Generate a VDS file name for the parmdb-s.
 # This is the file that has to be used in pyparmdb.
 # Its name is the MS-vds one with the extension replaced by $pdb.
-vdsname=`echo $msvds | sed -e "s%\([^.\]*\)\.[^/]*$%\1.$pdb%"`
-if test -e $vdsname; then
+vdsname=`echo "$msvds" | sed -e "s%\([^.\]*\)\.[^/]*$%\1.$pdb%"`
+if test -e "$vdsname"; then
   if test $overwrite = 0; then
     echo "Error, distributed parmdb $vdsname already exists"
     exit 1
@@ -146,7 +146,7 @@ madetmpl=0
 if test "$pdbfile" = ""; then
   echo "Creating template parmdb $vdsname.template"
   madetmpl=1
-  pdbfile=$vdsname.template
+  pdbfile="$vdsname.template"
   parmdbm <<EOF
 create tablename="$pdbfile"
 adddef Gain:0:0:Ampl  values=1.0
@@ -160,6 +160,9 @@ adddef DirectionalGain:1:1:Real  values=1.0
 adddef AntennaOrientation values=5.497787144
 quit
 EOF
+  if $? != 0; then
+    exit
+  fi
 fi
 
 # By default do not set steps.
@@ -170,45 +173,45 @@ steptime=.
 # In that case only that MS is handled.
 if test -d "$msvds"  -a  -e "$msvds/table.dat"; then
   echo "setupparmdb-part '' '' '' '' 0 '$msvds' '' '' '$wd' '$pdbfile' '$pdb' '$dry' '$stepfreq' '$steptime'"
-  $pgmpath/setupparmdb-part "" "" "" "" 0 "$msvds" "" "" "$wd" "$pdbfile" "$pdb" "$dry" "$stepfreq" "$steptime"
-  test $madetmpl = 0  ||  rm -rf $vdsname.template
-  exit
+  "$pgmpath/setupparmdb-part" "" "" "" "" 0 "$msvds" "" "" "$wd" "$pdbfile" "$pdb" "$dry" "$stepfreq" "$steptime"  ||  exit 1
+  test $madetmpl = 0  ||  rm -rf "$vdsname.template"
 fi
 
 # Get step in freq and time from the VDS file.
 # They represent the steps in the MS.
 if test $setsteps = 1; then
-  steptime=`getparsetvalue $msvds StepTime`
-  stfreq=`getparsetvalue $msvds StartFreqs 0`
-  endfreq=`getparsetvalue $msvds EndFreqs 0`
+  steptime=`getparsetvalue "$msvds" StepTime`
+  stfreq=`getparsetvalue "$msvds" StartFreqs 0`
+  endfreq=`getparsetvalue "$msvds" EndFreqs 0`
   stepfreq=`python -c "print $endfreq - $stfreq"`
 fi
 
 # Get clusterdesc name from the VDS file if not given.
 if test "$cdn" = ""; then
-  cdn=`getparsetvalue $msvds ClusterDesc`
+  cdn=`getparsetvalue "$msvds" ClusterDesc`
 fi
 
 # Make all file names absolute.
-dn=`dirname $msvds`
-msvds=`cd $dn > /dev/null; pwd`/`basename $msvds`
-dn=`dirname $pdbfile`
-pdbfile=`cd $dn > /dev/null; pwd`/`basename $pdbfile`
-dn=`dirname $cdn`
-cdn=`cd $dn > /dev/null; pwd`/`basename $cdn`
+dn=`dirname "$msvds"`
+msvds=`cd "$dn" > /dev/null; pwd`/`basename "$msvds"`
+dn=`dirname "$pdbfile"`
+pdbfile=`cd "$dn" > /dev/null; pwd`/`basename "$pdbfile"`
+dn=`dirname "$cdn"`
+cdn=`cd "$dn" > /dev/null; pwd`/`basename "$cdn"`
 if test "$hfn" != ""; then
-  dn=`dirname $hfn`
-  hfn=`cd $dn > /dev/null; pwd`/`basename $hfn`
+  dn=`dirname "$hfn"`
+  hfn=`cd "$dn" > /dev/null; pwd`/`basename "$hfn"`
 fi
 
 # Start the processes on the various machines.
-echo "startdistproc -useenv -mode 0 -nomasterhost -dsn '$msvds' -hfn '$hfn' -cdn '$cdn' -logfile '$logfile' $pgmpath/setupparmdb-part '$wd' '$pdbfile' '$pdb' '$dry' '$stepfreq' '$steptime'"
-startdistproc -useenv -mode 0 -nomasterhost -dsn "$msvds" -hfn "$hfn" -cdn "$cdn" -logfile "$logfile" $pgmpath/setupparmdb-part "$wd" "$pdbfile" "$pdb" "$dry" "$stepfreq" "$steptime"  ||  exit 1
+# Exit in case of an error.
+echo "startdistproc -useenv -mode 0 -nomasterhost -dsn '$msvds' -hfn '$hfn' -cdn '$cdn' -logfile '$logfile' '$pgmpath/setupparmdb-part' '$wd' '$pdbfile' '$pdb' '$dry' '$stepfreq' '$steptime'"
+startdistproc -useenv -mode 0 -nomasterhost -dsn "$msvds" -hfn "$hfn" -cdn "$cdn" -logfile "$logfile" "$pgmpath/setupparmdb-part" "$wd" "$pdbfile" "$pdb" "$dry" "$stepfreq" "$steptime"  ||  exit 1
 
 # Create a VDS file for the parmdb-s by copying it.
 # This is the file that has to be used in pyparmdb.
 # Its name is the MS-vds one with the extension replaced by $pdb.
 # Refer in it to the actual pdb filename.
 echo "Create overall VDS $vdsname"
-sed -e "s%\(Part.*\.FileName *= *[^ #]*\)%\1/$pdb%" $msvds > $vdsname
-test $madetmpl = 0  ||  rm -rf $vdsname.template
+sed -e "s%\(Part.*\.FileName *= *[^ #]*\)%\1/$pdb%" "$msvds" > "$vdsname"
+test $madetmpl = 0  ||  rm -rf "$vdsname.template"
diff --git a/CEP/ParmDB/src/setupparmdb-part b/CEP/ParmDB/src/setupparmdb-part
index 15bf208e453f413b35bb592d1fad52a9ebad0212..d323d5084491b6dec2771540ff024cce8ae9f7cd 100755
--- a/CEP/ParmDB/src/setupparmdb-part
+++ b/CEP/ParmDB/src/setupparmdb-part
@@ -23,8 +23,8 @@
 # $Id$
 
 # Find the path used to start the script.
-pgmpath=`dirname $0`
-pgmpath=`cd $pgmpath > /dev/null 2>&1  &&  pwd`
+pgmpath=`dirname "$0"`
+pgmpath=`cd "$pgmpath" > /dev/null 2>&1  &&  pwd`
 
 if test $# != 15; then
   echo "run as:  setupparmdb-part dummy dummy dummy dummy rank"
@@ -34,26 +34,24 @@ if test $# != 15; then
   exit 1
 fi
 
-echo $*
-
 shift
 shift
 shift
 shift
-seqnr=$1
-msn=$2
-envfile=$5
-wd=$6
-pdbfile=$7
-pdb=$8
-dry=$9
-stepfreq=${10}
-steptime=${11}
+seqnr="$1"
+msn="$2"
+envfile="$5"
+wd="$6"
+pdbfile="$7"
+pdb="$8"
+dry="$9"
+stepfreq="${10}"
+steptime="${11}"
 
 
 # Initialize environment.
 if test "$envfile" != ""; then
-  . $envfile
+  . "$envfile"
 fi
 
 # Check if a parmdb name is given (otherwise the MS gets deleted hereafter).
@@ -66,13 +64,13 @@ fi
 if test "$wd" = ""  -o  "$wd" = "..."; then
   wd=`dirname "$msn"`
 fi
-cd $wd  ||  exit 1
+cd "$wd"  ||  exit 1
 
 # Copy the instrument ParmDB.
 echo "Create $msn/$pdb (copy of $pdbfile)"
 if test "$dry" != dry; then
-  rm -rf $msn/$pdb           # this is safe because $pdb is not empty
-  cp -r $pdbfile $msn/$pdb
+  rm -rf "$msn/$pdb"           # this is safe because $pdb is not empty
+  cp -r "$pdbfile" "$msn/$pdb"  ||  exit 1
 
   if test "$stepfreq" != "."  -a  "$steptime" != "."; then
     # Put default steps into the parmdb.
diff --git a/CEP/ParmDB/src/setupsourcedb b/CEP/ParmDB/src/setupsourcedb
index d4626d2e40be6a927741d0973ac19594ef161a59..d1df116715504436e4ae71af668349e126ec2422 100755
--- a/CEP/ParmDB/src/setupsourcedb
+++ b/CEP/ParmDB/src/setupsourcedb
@@ -23,8 +23,8 @@
 # $Id$
 
 # Find the path used to start the script.
-pgmpath=`dirname $0`
-pgmpath=`cd $pgmpath > /dev/null 2>&1  &&  pwd`
+pgmpath=`dirname "$0"`
+pgmpath=`cd "$pgmpath" > /dev/null 2>&1  &&  pwd`
 
 # Get possible options.
 overwrite=0
@@ -50,7 +50,7 @@ do
     shift
   elif [ "$1" = "-makeopt" ]; then
     shift
-    makeopt=$1
+    makeopt="$1"
     shift
   elif [ "$1" = "-nomakeopt" ]; then
     makeopt=
@@ -74,7 +74,7 @@ if test $help = 1; then
   echo "                     -makeopt='format=<'"
   echo "       ms-gds        name of global VDS file of the MS"
   echo "       srccat        name of ASCII file containing sources"
-  echo "                     makesourcedb options can be givn using -makeopt"
+  echo "                     makesourcedb options can be given using -makeopt"
   echo "       sourcedb-name   extension name of new sourcedb (defaults to sky)"
   echo "                     e.g. for 100319.MS the name 100319.sky is used"
   echo "       clusterdesc   name of clusterdesc file"
@@ -91,13 +91,13 @@ if test $help = 1; then
   exit 1
 fi
 
-msvds=$1
+msvds="$1"
 shift
-srccat=$1
+srccat="$1"
 shift
 pdb=
 if test $# != 0; then
-  pdb=$1
+  pdb="$1"
   shift
 fi
 if test "$pdb" = ""; then
@@ -105,17 +105,17 @@ if test "$pdb" = ""; then
 fi
 cdn=
 if test $# != 0; then
-  cdn=$1
+  cdn="$1"
   shift
 fi
 logfile=
 if test $# != 0; then
-  logfile=$1
+  logfile="$1"
   shift
 fi
 dry=
 if test $# != 0; then
-  dry=$1
+  dry="$1"
   shift
 fi
 if test "$dry" = ""; then
@@ -123,27 +123,24 @@ if test "$dry" = ""; then
 fi
 hfn=
 if test $# != 0; then
-  hfn=$1
+  hfn="$1"
   shift
 fi
 wd=
 if test $# != 0; then
-  wd=$1
+  wd="$1"
   shift
 fi
 if test "$wd" = ""; then
   wd=...
 fi
-##if test "$makeopt" = ""; then
-##  makeopt=...
-##fi
 
 # Generate a VDS file name for the sourcedb-s.
 # This is the file that has to be used in pysourcedb.
 # Its name is the MS-vds one with the extension replaced by $pdb.
-vdsname=`echo $msvds | sed -e "s%\([^.\]*\)\.[^/]*$%\1.$pdb%"`
-if test -e $vdsname; then
-  if test $overwrite = 0; then
+vdsname=`echo "$msvds" | sed -e "s%\([^.\]*\)\.[^/]*$%\1.$pdb%"`
+if test -e "$vdsname"; then
+  if test "$overwrite" = 0; then
     echo "Error, distributed sourcedb $vdsname already exists"
     exit 1
   else
@@ -159,42 +156,42 @@ steptime=.
 # In that case only that MS is handled.
 if test -d "$msvds"  -a  -e "$msvds/table.dat"; then
   echo "setupsourcedb-part '' '' '' '' 0 '$msvds' '' '' '$wd' '$srccat' '$makeopt' '$pdb' '$overwrite' '$dry' '$stepfreq' '$steptime'"
-  $pgmpath/setupsourcedb-part "" "" "" "" 0 "$msvds" "" "" "$wd" "$srccat" "$makeopt" "$pdb" "$overwrite" "$dry" "$stepfreq" "$steptime"
+  "$pgmpath/setupsourcedb-part" "" "" "" "" 0 "$msvds" "" "" "$wd" "$srccat" "$makeopt" "$pdb" "$overwrite" "$dry" "$stepfreq" "$steptime"
   exit
 fi
 
 # Get step in freq and time from the VDS file.
 # They represent the steps in the MS.
 if test $setsteps = 1; then
-  steptime=`getparsetvalue $msvds StepTime`
-  stfreq=`getparsetvalue $msvds StartFreqs 0`
-  endfreq=`getparsetvalue $msvds EndFreqs 0`
+  steptime=`getparsetvalue "$msvds" StepTime`
+  stfreq=`getparsetvalue "$msvds" StartFreqs 0`
+  endfreq=`getparsetvalue "$msvds" EndFreqs 0`
   stepfreq=`python -c "print $endfreq - $stfreq"`
 fi
 
 # Get clusterdesc name from the VDS file if not given.
 if test "$cdn" = ""; then
-  cdn=`getparsetvalue $msvds ClusterDesc`
+  cdn=`getparsetvalue "$msvds" ClusterDesc`
 fi
 # Make all file names absolute.
-dn=`dirname $msvds`
-msvds=`cd $dn > /dev/null; pwd`/`basename $msvds`
-dn=`dirname $srccat`
-srccat=`cd $dn > /dev/null; pwd`/`basename $srccat`
-dn=`dirname $cdn`
-cdn=`cd $dn > /dev/null; pwd`/`basename $cdn`
+dn=`dirname "$msvds"`
+msvds=`cd "$dn" > /dev/null; pwd`/`basename "$msvds"`
+dn=`dirname "$srccat"`
+srccat=`cd "$dn" > /dev/null; pwd`/`basename "$srccat"`
+dn=`dirname "$cdn"`
+cdn=`cd "$dn" > /dev/null; pwd`/`basename "$cdn"`
 if test "$hfn" != ""; then
-  dn=`dirname $hfn`
-  hfn=`cd $dn > /dev/null; pwd`/`basename $hfn`
+  dn=`dirname "$hfn"`
+  hfn=`cd "$dn" > /dev/null; pwd`/`basename "$hfn"`
 fi
 
 # Start the processes on the various machines.
-echo "startdistproc -useenv -mode 0 -nomasterhost -dsn '$msvds' -hfn '$hfn' -cdn '$cdn' -logfile '$logfile' $pgmpath/setupsourcedb-part '$wd' '$srccat' '$makeopt' '$pdb' '$overwrite' '$dry' '$stepfreq' '$steptime'"
-startdistproc -useenv -mode 0 -nomasterhost -dsn "$msvds" -hfn "$hfn" -cdn "$cdn" -logfile "$logfile" $pgmpath/setupsourcedb-part "$wd" "$srccat" "$makeopt" "$pdb" "$overwrite" "$dry" "$stepfreq" "$steptime"  ||  exit 1
+echo "startdistproc -useenv -mode 0 -nomasterhost -dsn '$msvds' -hfn '$hfn' -cdn '$cdn' -logfile '$logfile' '$pgmpath/setupsourcedb-part' '$wd' '$srccat' '$makeopt' '$pdb' '$overwrite' '$dry' '$stepfreq' '$steptime'"
+startdistproc -useenv -mode 0 -nomasterhost -dsn "$msvds" -hfn "$hfn" -cdn "$cdn" -logfile "$logfile" "$pgmpath/setupsourcedb-part" "$wd" "$srccat" "$makeopt" "$pdb" "$overwrite" "$dry" "$stepfreq" "$steptime"  ||  exit 1
 
 # Create a VDS file for the sourcedb-s by copying it.
 # This is the file that has to be used in pyparmdb.
 # Its name is the MS-vds one with the extension replaced by $pdb.
 # Refer in it to the actual pdb filename.
 echo "Create overall VDS $vdsname"
-sed -e "s%\(Part.*\.FileName *= *[^ #]*\)%\1/$pdb%" $msvds > $vdsname
+sed -e "s%\(Part.*\.FileName *= *[^ #]*\)%\1/$pdb%" "$msvds" > "$vdsname"
diff --git a/CEP/ParmDB/src/setupsourcedb-part b/CEP/ParmDB/src/setupsourcedb-part
index 3facd1a1e5325c284ff4e3ba7ec0ba7c70961fe7..9c2f10032b3487120b22d8c906fba9bbc1a424d6 100755
--- a/CEP/ParmDB/src/setupsourcedb-part
+++ b/CEP/ParmDB/src/setupsourcedb-part
@@ -23,8 +23,8 @@
 # $Id$
 
 # Find the path used to start the script.
-pgmpath=`dirname $0`
-pgmpath=`cd $pgmpath > /dev/null 2>&1  &&  pwd`
+pgmpath=`dirname "$0"`
+pgmpath=`cd "$pgmpath" > /dev/null 2>&1  &&  pwd`
 
 if test $# != 17; then
   echo "run as:  setupsourcedb-part dummy dummy dummy dummy rank"
@@ -38,22 +38,22 @@ shift
 shift
 shift
 shift
-seqnr=$1
-msn=$2
-envfile=$5
-wd=$6
-srccat=$7
-makeopt=$8
-pdb=$9
-overwrite=${10}
-dry=${11}
-stepfreq=${12}
-steptime=${13}
+seqnr="$1"
+msn="$2"
+envfile="$5"
+wd="$6"
+srccat="$7"
+makeopt="$8"
+pdb="$9"
+overwrite="${10}"
+dry="${11}"
+stepfreq="${12}"
+steptime="${13}"
 
 
 # Initialize environment.
 if test "$envfile" != ""; then
-  . $envfile
+  . "$envfile"
 fi
 
 # Check if a sourcedb name is given.
@@ -62,18 +62,11 @@ if test "$pdb" = ""; then
   exit 1
 fi
 
-# For 'make check' we need to qualify where makesourcedb is.
-#case "$wd" in
-#*/test)
-#  pgmpath=../src/
-#  ;;
-#esac
-
 # Set the working directory; derive it if needed.
 if test "$wd" = ""  -o  "$wd" = "..."; then
   wd=`dirname "$msn"`
 fi
-cd $wd  ||  exit 1
+cd "$wd"  ||  exit 1
 
 # Enclose all options in quotes.
 sdbopt=
@@ -83,13 +76,14 @@ do
 done
 
 # Append to the SourceDB. Delete it first if overwrite is given.
-echo "makesourcedb $sdbopt in=$srccat out=$msn/$pdb"
+echo "makesourcedb $sdbopt 'in=$srccat' 'out=$msn/$pdb'"
 if test "$dry" != dry; then
-  if test $overwrite = 1; then
-    rm -rf $msn/$pdb
+  if test "$overwrite" = 1; then
+    rm -rf "$msn/$pdb"
   fi
   # Use eval to avoid problems with quotes in sdbopt.
-  eval makesourcedb $sdbopt in=$srccat out=$msn/$pdb || exit 1
+  # Use quotes in case filenames contain blanks.
+  eval makesourcedb $sdbopt "'in=$srccat'" "'out=$msn/$pdb'" || exit 1
 
   if test "$stepfreq" != "."  -a  "$steptime" != "."; then
     # Put default steps into the sourcedb.
diff --git a/CEP/ParmDB/src/startparmdbdistr b/CEP/ParmDB/src/startparmdbdistr
index ecc38aa6ad4c40053bd4e113f1d1c6c4a98cb47f..ef2814777ecc46cffc702b48fc15044ec99f5335 100755
--- a/CEP/ParmDB/src/startparmdbdistr
+++ b/CEP/ParmDB/src/startparmdbdistr
@@ -23,19 +23,19 @@
 # $Id$
 
 # Find the path used to start the script.
-pgmpath=`dirname $0`
-pgmpath=`cd $pgmpath > /dev/null 2>&1  &&  pwd`
+pgmpath=`dirname "$0"`
+pgmpath=`cd "$pgmpath" > /dev/null 2>&1  &&  pwd`
 
 # Determine the host name to be used. Default is result of hostname.
 # Try if a connection can be made. If not, set it to localhost.
 # This is merely to run the test tParmFacadeDistr.run on all possible systems.
 host=`hostname`
-ssh -o "ConnectTimeout=10" $host date > /dev/null 2>&1
+ssh -o "ConnectTimeout=10" "$host" date > /dev/null 2>&1
 if test $? != 0; then
   echo "localhost is used because $host is unreachable"
   host=localhost
 fi
 
 # Start parmdbremote for each part.
-startdistproc -useenv -mode "$1" -nowait -masterhost $host -nostartmaster \
-              -cdn "$2" -dsn "$3" $pgmpath/parmdbremote-scr `pwd`
+startdistproc -useenv -mode "$1" -nowait -masterhost "$host" -nostartmaster \
+              -cdn "$2" -dsn "$3" "$pgmpath/parmdbremote-scr" "`pwd`"
diff --git a/CEP/ParmDB/test/tsetupparmdb.run b/CEP/ParmDB/test/tsetupparmdb.run
index 2be429f1b04b3f4eda2f4729e8578d82702791db..fd0f45e5279180f78efd1991ec801f959a865c01 100755
--- a/CEP/ParmDB/test/tsetupparmdb.run
+++ b/CEP/ParmDB/test/tsetupparmdb.run
@@ -25,13 +25,14 @@
 
 # Set srcdir if undefined (if run outside 'make check')
 if test "$srcdir" = ""; then
-  srcdir=../../../src
+  srcdir=../../../../../LOFAR/CEP/ParmDB/test
 fi
 
-# Add src directory to PATH (to find parmdb).
-PATH=../src:$PATH
+# Add src and LMWCommon directory to PATH (to find parmdbm).
+PATH="../src:$srcdir/../src:$PATH"
 export PATH
 which parmdbm
+which startdistproc
 
 # Create directories to create the parmdb-s in.
 # They must match the FileName in in_ms*.vds.
@@ -46,7 +47,7 @@ mkdir tsetupparmdb_tmp.ms2
 # Use _tmp name for the gds, so the resulting parmdb also gets a _tmp name.
 cp tsetupparmdb.in_gds tsetupparmdb_tmp.gds
 echo ">>>"
-$srcdir/../src/setupparmdb -overwrite tsetupparmdb_tmp.gds "" "" "" tsetupparmdb_tmp.log 0 tsetupparmdb_tmp.hfn `pwd`  ||  exit 1
+"$srcdir/../src/setupparmdb" -overwrite tsetupparmdb_tmp.gds "" "" "" tsetupparmdb_tmp.log 0 tsetupparmdb_tmp.hfn "`pwd`"  ||  exit 1
 echo "<<<"
 
 # Append the three log files to the output.
diff --git a/CEP/ParmDB/test/tsetupsourcedb.run b/CEP/ParmDB/test/tsetupsourcedb.run
index 708126cd99e8f80c773df0aa4cd0d0ed42a7b50f..6c76bcc2d18cdbc1bf8f744a080a776769161056 100755
--- a/CEP/ParmDB/test/tsetupsourcedb.run
+++ b/CEP/ParmDB/test/tsetupsourcedb.run
@@ -26,11 +26,11 @@
 # Set srcdir if undefined (if run outside 'make check')
 wd=`pwd`
 if test "$srcdir" = ""; then
-  srcdir=$wd/../../../src
+  srcdir=../../../../../LOFAR/CEP/ParmDB/test
 fi
 
 # Add current (test) and source directory to PATH.
-PATH=$wd:$wd/../src:$srcdir:$PATH
+PATH="$wd:$wd/../src:$srcdir/../src:$PATH"
 export PATH
 
 # Create directories to create the sourcedb-s in.
@@ -46,7 +46,7 @@ mkdir tsetupsourcedb_tmp.ms2
 # Use _tmp name for the gds, so the resulting sourcedb also gets a _tmp name.
 cp tsetupsourcedb.in_gds tsetupsourcedb_tmp.gds
 echo ">>>"
-$srcdir/../src/setupsourcedb -overwrite tsetupsourcedb_tmp.gds tsetupsourcedb.in "" "" tsetupsourcedb_tmp.log 0 tsetupsourcedb_tmp.hfn $wd
+"$srcdir/../src/setupsourcedb" -overwrite tsetupsourcedb_tmp.gds tsetupsourcedb.in "" "" tsetupsourcedb_tmp.log 0 tsetupsourcedb_tmp.hfn "$wd"
 echo "<<<"
 
 # Append the three log files to the output.
diff --git a/CMake/variants/variants.RS005C b/CMake/variants/variants.RS005C
index c110148debcabddac8c38aff8c3096e992056fe7..49f661cb3ce2108bb451d080b0eed7fcb1eb3397 100644
--- a/CMake/variants/variants.RS005C
+++ b/CMake/variants/variants.RS005C
@@ -7,6 +7,9 @@ set(PQ_ROOT_DIR /usr/local/pgsql)
 # Search Lapack in /usr first, because Lapack library in /usr/local is broken!
 set(CPPLAPACK_ROOT_DIR /usr)
 
+# log4cplus-1.0.4 root dir
+set(LOG4CPLUS_ROOT_DIR /usr/local/log4cplus)
+
 # Path to PVSS
 set(PVSS_ROOT_DIR /opt/pvss/pvss2_v3.7)
 set(PVSS_DEFINITIONS "-Wno-deprecated")
diff --git a/CMake/variants/variants.kis001 b/CMake/variants/variants.kis001
index 3ff8ef847f7199fd3775c255dff65e64d04e1ad8..7b56af9f88efb320f44be12bb50d2050330f0317 100644
--- a/CMake/variants/variants.kis001
+++ b/CMake/variants/variants.kis001
@@ -1,6 +1,7 @@
 set(ENV{JAVA_HOME} /usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0)
 set(PVSS_ROOT_DIR /opt/pvss/pvss2_v3.7)
 set(PVSS_DEFINITIONS "-Wno-deprecated")
+set(LOG4CPLUS_ROOT_DIR "/usr/local/log4cplus")
 
 set(CTEST_CUSTOM_WARNING_EXCEPTION 
   "/boost/date_time/time_facet.hpp:[0-9]+: warning: unused parameter"
diff --git a/JAVA/LofarUtils/src/nl/astron/lofar/lofarutils/LofarUtils.java b/JAVA/LofarUtils/src/nl/astron/lofar/lofarutils/LofarUtils.java
index 556660b6de288c2952a5cbffa9ed9033142c7d47..42236b227516251c93ed7b74a2bd9a6b77b408d3 100644
--- a/JAVA/LofarUtils/src/nl/astron/lofar/lofarutils/LofarUtils.java
+++ b/JAVA/LofarUtils/src/nl/astron/lofar/lofarutils/LofarUtils.java
@@ -24,6 +24,9 @@ package nl.astron.lofar.lofarutils;
 
 
 import java.awt.Component;
+import java.awt.KeyboardFocusManager;
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
 import java.text.Collator;
 import java.util.BitSet;
 import java.util.regex.Pattern;
@@ -32,6 +35,7 @@ import javax.swing.Icon;
 import javax.swing.JComboBox;
 import javax.swing.JList;
 import javax.swing.JOptionPane;
+import javax.swing.text.JTextComponent;
 
 /**
  * This panel contains a TreePanel and some textfields that display information
@@ -479,7 +483,6 @@ public abstract class LofarUtils {
 
         } catch (Exception ex) {
             System.out.println("Error in CompactedArrayString: " + ex.getMessage());
-            ex.printStackTrace();
             return orgStr;
         }
 
@@ -533,7 +536,6 @@ public abstract class LofarUtils {
             }
         } catch (Exception ex) {
             System.out.println("Error in CompactedArrayString: " + ex.getMessage());
-            ex.printStackTrace();
             return orgStr;
         }
 
@@ -877,4 +879,54 @@ public abstract class LofarUtils {
             return "";
         }
     }
-}
\ No newline at end of file
+    
+        static public class TextSelector {
+
+            private static FocusHandler installedInstance;
+
+            /**
+             * Install an PropertyChangeList listener to the default focus manager
+             * and selects text when a text component is focused.       
+             */
+            public static void install() {
+                //already installed
+                if (installedInstance != null) {
+                    return;
+                }
+
+                installedInstance = new FocusHandler();
+
+                KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
+
+
+
+                kfm.addPropertyChangeListener("focusOwner", installedInstance);
+            }
+
+            public static void uninstall() {
+                if (installedInstance != null) {
+                    KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
+                    kfm.removePropertyChangeListener("focusOwner", installedInstance);
+                }
+            }
+
+            private static class FocusHandler implements PropertyChangeListener {
+
+            @Override
+                public void propertyChange(PropertyChangeEvent evt) {
+                    if (evt.getNewValue() instanceof JTextComponent) {
+                        JTextComponent text = (JTextComponent) evt.getNewValue();
+                       //select text if the component is editable
+                        //and the caret is at the end of the text
+                        if (text.isEditable()) {
+//                                && text.getDocument().getLength() == text.getCaretPosition()) {
+
+                            text.select(0,text.getDocument().getLength());
+                        }
+                    }
+                }
+            }
+        }
+
+
+}   
\ No newline at end of file
diff --git a/LCS/ApplCommon/include/ApplCommon/Observation.h b/LCS/ApplCommon/include/ApplCommon/Observation.h
index 59aed628f08f85771e8c7888dfc393e7aaeca72d..aa3161f8195d892b2d32af646680557a2a4f4554 100644
--- a/LCS/ApplCommon/include/ApplCommon/Observation.h
+++ b/LCS/ApplCommon/include/ApplCommon/Observation.h
@@ -66,8 +66,6 @@ public:
 
 	// Returns a bitset containing the RCU's requested by the observation.
 	bitset<MAX_RCUS> getRCUbitset(int nrLBAs, int nrHBAs, const string& anAntennaSet);
-	// TEMP HACK
-	string getAntennaArrayName(bool hasSplitters) const;
 
 	// Support for dynamic dataslot allocation
 	vector<int>	getBeamAllocation(const string&	stationName = "") const;
@@ -76,6 +74,9 @@ public:
 	// for operator <<
 	ostream& print (ostream&	os) const;
 
+	// TEMP HACK
+	string getAntennaFieldName(bool hasSplitters, uint32 beamIdx = 0) const;
+
 	// data types
 	typedef bitset<MAX_RCUS> 	  RCUset_t;
 
diff --git a/LCS/ApplCommon/src/Observation.cc b/LCS/ApplCommon/src/Observation.cc
index 482c576d283fea238a75a6b4e853c43cb07d02ff..99ae3fcff757c485425d1196e39bf0d0a331e90a 100644
--- a/LCS/ApplCommon/src/Observation.cc
+++ b/LCS/ApplCommon/src/Observation.cc
@@ -144,7 +144,7 @@ Observation::Observation(const ParameterSet*		aParSet,
 	}
 
 	// determine if DataslotLists are available in this parset
-	itsHasDataslots = aParSet->isDefined(prefix+str(format("Dataslots.%s%s.DataslotList") % stations[0] % antennaArray));
+	itsHasDataslots = !stations.empty() && aParSet->isDefined(prefix+str(format("Dataslots.%s%s.DataslotList") % stations[0] % getAntennaFieldName(itsStnHasDualHBA)));
 	if (itsHasDataslots) {
 		itsDataslotParset = aParSet->makeSubset(prefix+"Dataslots.");		// save subset for later
 	}
@@ -232,9 +232,11 @@ Observation::Observation(const ParameterSet*		aParSet,
 		}
 
 		// finally update vector with beamnumbers
-		int	nrBeamlets = newBeam.subbands.size();
+		int	nrSubbands = newBeam.subbands.size();
 		if (!itsHasDataslots) {		// old situation
 			BeamBeamlets = aParSet->getInt32Vector(beamPrefix+"beamletList", vector<int32>(), true);	// true:expandable
+			int nrBeamlets = BeamBeamlets.size();
+			ASSERTSTR(nrBeamlets == nrSubbands, "Number of beamlets(" << nrBeamlets << ") != nr of subbands(" << nrSubbands << ") for Beam " << beamIdx);
 			for (int  i = 0; i < nrBeamlets; ++i) {
 				if (beamlet2beams[BeamBeamlets[i]] != -1) {
 					stringstream	os;
@@ -246,7 +248,7 @@ Observation::Observation(const ParameterSet*		aParSet,
 			} // for all beamlets
 		}
 		else { // new situation
-			for (int  i = 0; i < nrBeamlets; ++i) {
+			for (int  i = 0; i < nrSubbands; ++i) {	// Note nrBeamlets=nrSubbands
 				itsBeamSlotList.push_back(beamIdx);
 			}
 		} // itsHasDataslots
@@ -500,13 +502,15 @@ vector<int> Observation::getBeamAllocation(const string& stationName) const
 		}
 	}
 	// is DSL for this station available?
-	if (!itsDataslotParset.isDefined(str(format("%s%s.DataslotList") % station % antennaArray)) ||
-	    !itsDataslotParset.isDefined(str(format("%s%s.RSPBoardList") % station % antennaArray))) {
+	string	fieldName = getAntennaFieldName(itsStnHasDualHBA);
+	string	dsl(str(format("%s%s.DataslotList") % station % fieldName));
+	string	rbl(str(format("%s%s.RSPBoardList") % station % fieldName));
+	if (!itsDataslotParset.isDefined(dsl) || !itsDataslotParset.isDefined(rbl)) {
 		LOG_ERROR_STR("No dataslots defined for " << station << antennaArray);
 		return (b2b);
 	}
-	vector<int>	RSPboardList = itsDataslotParset.getIntVector(str(format("%s%s.RSPBoardList") % station % antennaArray),true);
-	vector<int>	DataslotList = itsDataslotParset.getIntVector(str(format("%s%s.DataslotList") % station % antennaArray),true);
+	vector<int>	RSPboardList = itsDataslotParset.getIntVector(rbl,true);
+	vector<int>	DataslotList = itsDataslotParset.getIntVector(dsl,true);
 
 	ASSERTSTR (RSPboardList.size() == DataslotList.size(), "RSPBoardlist (" << RSPboardList << 
 			") differs size of DataslotList(" << DataslotList << ") for station " << station);
@@ -536,8 +540,10 @@ vector<int> Observation::getBeamAllocation(const string& stationName) const
 vector<int>	Observation::getBeamlets (uint beamIdx, const string&	stationName) const
 {
 	uint	parsetIdx = (dualMode && itsStnHasDualHBA) ? beamIdx/2 : beamIdx;
+	string	fieldName = getAntennaFieldName(itsStnHasDualHBA, beamIdx);
 
 	if (!itsHasDataslots) {
+		// both fields use the same beamlet mapping
 		return (itsDataslotParset.getInt32Vector(str(format("Beam[%d].beamletList") % parsetIdx), vector<int32>(), true));	// true:expandable
 	}
 
@@ -552,14 +558,15 @@ vector<int>	Observation::getBeamlets (uint beamIdx, const string&	stationName) c
 	}
 		
 	// is DSL for this station available?
+	// both fields have their own beamlet mapping
+	string	dsl(str(format("%s%s.DataslotList") % station % fieldName));
+	string	rbl(str(format("%s%s.RSPBoardList") % station % fieldName));
 	vector<int>	result;
-	if (!itsDataslotParset.isDefined(str(format("%s%s.DataslotList") % station % antennaArray)) ||
-	    !itsDataslotParset.isDefined(str(format("%s%s.RSPBoardList") % station % antennaArray))) {
+	if (!itsDataslotParset.isDefined(dsl) || !itsDataslotParset.isDefined(rbl)) {
 		return (result);
 	}
-	vector<int>	RSPboardList = itsDataslotParset.getIntVector(str(format("%s%s.RSPBoardList") % station % antennaArray),true);
-	vector<int>	DataslotList = itsDataslotParset.getIntVector(str(format("%s%s.DataslotList") % station % antennaArray),true);
-
+	vector<int>	RSPboardList = itsDataslotParset.getIntVector(rbl,true);
+	vector<int>	DataslotList = itsDataslotParset.getIntVector(dsl,true);
 	uint	nrEntries = itsBeamSlotList.size();
 	for (uint i = 0; i < nrEntries; ++i) {
 		if (itsBeamSlotList[i] == parsetIdx) {
@@ -571,9 +578,11 @@ vector<int>	Observation::getBeamlets (uint beamIdx, const string&	stationName) c
 
 
 //
-// TEMP HACK TO GET THE ANTENNAARRAYNAME
+// TEMP HACK TO GET THE ANTENNAFIELDNAME
+//
+// Except for the beamIdx dependancy we should look in the antennaSet file.
 //
-string Observation::getAntennaArrayName(bool hasSplitters) const
+string Observation::getAntennaFieldName(bool hasSplitters, uint32	beamIdx) const
 {
 	string	result;
 	if (antennaSet.empty()) {
@@ -595,6 +604,7 @@ string Observation::getAntennaArrayName(bool hasSplitters) const
 	if (result == "HBA_ZERO") 	return ("HBA0");
 	if (result == "HBA_ONE") 	return ("HBA1");
 	if (result == "HBA_JOINED")	return ("HBA");
+	if (result == "HBA_DUAL")	return (beamIdx % 2 == 0 ? "HBA0" : "HBA1");
 	return ("HBA");
 }	
 
diff --git a/LCS/ApplCommon/test/tObservation.cc b/LCS/ApplCommon/test/tObservation.cc
index 3a6bea348e6a6ed3ae8e9b0322d8e75188216716..17c3a22ab7ded5c125d4846a6c1c01507f06829d 100644
--- a/LCS/ApplCommon/test/tObservation.cc
+++ b/LCS/ApplCommon/test/tObservation.cc
@@ -45,8 +45,8 @@ int main (int argc, char* argv[])
 			cout << "getRCUbitset(96,96,LBA_XXX) = " << someObs.getRCUbitset(96,96,"LBA_XXX") << endl;	// Core
 			cout << "getRCUbitset(96,48,HBA_XXX) = " << someObs.getRCUbitset(96,48,"HBA_XXX") << endl;	// Core
 			cout << "getRCUbitset(96,96,HBA_XXX) = " << someObs.getRCUbitset(96,96,"HBA_XXX") << endl;	// Core
-			vector<int>	b2b = someObs.getBeamAllocation("RS005");
-			cout << "BeamAlloc for RS005 : " << b2b << endl;
+			vector<int>	b2b = someObs.getBeamAllocation("CS002");
+			cout << "BeamAlloc for CS002 : " << b2b << endl;
 			return (0);
 		}
 
@@ -119,27 +119,27 @@ int main (int argc, char* argv[])
 		
 		// test translation of antennaSetname
 		obs3.antennaSet = "HBA_ZERO";
-		cout << "HBA_ZERO(false) = " << obs3.getAntennaArrayName(false) << endl;
-		cout << "HBA_ZERO(true)  = " << obs3.getAntennaArrayName(true) << endl;
+		cout << "HBA_ZERO(false) = " << obs3.getAntennaFieldName(false) << endl;
+		cout << "HBA_ZERO(true)  = " << obs3.getAntennaFieldName(true) << endl;
 		obs3.antennaSet = "HBA_ONE";
-		cout << "HBA_ONE(false) = " << obs3.getAntennaArrayName(false) << endl;
-		cout << "HBA_ONE(true)  = " << obs3.getAntennaArrayName(true) << endl;
+		cout << "HBA_ONE(false) = " << obs3.getAntennaFieldName(false) << endl;
+		cout << "HBA_ONE(true)  = " << obs3.getAntennaFieldName(true) << endl;
 		obs3.antennaSet = "HBA_DUAL";
-		cout << "HBA_DUAL(false) = " << obs3.getAntennaArrayName(false) << endl;
-		cout << "HBA_DUAL(true)  = " << obs3.getAntennaArrayName(true) << endl;
+		cout << "HBA_DUAL(false) = " << obs3.getAntennaFieldName(false) << endl;
+		cout << "HBA_DUAL(true)  = " << obs3.getAntennaFieldName(true) << endl;
 		obs3.antennaSet = "HBA_JOINED";
-		cout << "HBA_JOINED(false) = " << obs3.getAntennaArrayName(false) << endl;
-		cout << "HBA_JOINED(true)  = " << obs3.getAntennaArrayName(true) << endl;
+		cout << "HBA_JOINED(false) = " << obs3.getAntennaFieldName(false) << endl;
+		cout << "HBA_JOINED(true)  = " << obs3.getAntennaFieldName(true) << endl;
 
 		obs3.antennaSet = "LBA_INNER";
-		cout << "LBA_INNER(false) = " << obs3.getAntennaArrayName(false) << endl;
-		cout << "LBA_INNER(true)  = " << obs3.getAntennaArrayName(true) << endl;
+		cout << "LBA_INNER(false) = " << obs3.getAntennaFieldName(false) << endl;
+		cout << "LBA_INNER(true)  = " << obs3.getAntennaFieldName(true) << endl;
 		obs3.antennaSet = "LBA_OUTER";
-		cout << "LBA_OUTER(false) = " << obs3.getAntennaArrayName(false) << endl;
-		cout << "LBA_OUTER(true)  = " << obs3.getAntennaArrayName(true) << endl;
+		cout << "LBA_OUTER(false) = " << obs3.getAntennaFieldName(false) << endl;
+		cout << "LBA_OUTER(true)  = " << obs3.getAntennaFieldName(true) << endl;
 		obs3.antennaSet = "LBA_X";
-		cout << "LBA_X(false) = " << obs3.getAntennaArrayName(false) << endl;
-		cout << "LBA_X(true)  = " << obs3.getAntennaArrayName(true) << endl;
+		cout << "LBA_X(false) = " << obs3.getAntennaFieldName(false) << endl;
+		cout << "LBA_X(true)  = " << obs3.getAntennaFieldName(true) << endl;
 
 		// test old syntax agains new syntax
 		cout << ">>>" << endl;
diff --git a/LCS/ApplCommon/test/tObservation.in_newParset b/LCS/ApplCommon/test/tObservation.in_newParset
index 3da73e1b91c1688becdae98a717cf37f1f80f154..6965201eb8a21111f20d0e47516387276c58c303 100644
--- a/LCS/ApplCommon/test/tObservation.in_newParset
+++ b/LCS/ApplCommon/test/tObservation.in_newParset
@@ -30,12 +30,18 @@ ObsSW.Observation.VirtualInstrument.partitionList=[R001_128_0,R001_128_1,R001_12
 ObsSW.Observation.VirtualInstrument.stationList=[CS001,RS005,CS004]
 ObsSW.Observation.VirtualInstrument.storageCapacity=760
 ObsSW.Observation.VirtualInstrument.storageNodeList=[list001,list002]
-ObsSW.Observation.Dataslots.CS001HBA.DataslotList=[45..47,0..11]
-ObsSW.Observation.Dataslots.CS001HBA.RSPBoardList=[3*0,12*1]
-ObsSW.Observation.Dataslots.RS005HBA.DataslotList=[0..3,45..47,0..7]
-ObsSW.Observation.Dataslots.RS005HBA.RSPBoardList=[7*0,8*1]
-ObsSW.Observation.Dataslots.CS004HBA.DataslotList=[0..14]
-ObsSW.Observation.Dataslots.CS004HBA.RSPBoardList=[15*0]
+ObsSW.Observation.Dataslots.CS001HBA0.DataslotList=[45..47,0..11]
+ObsSW.Observation.Dataslots.CS001HBA0.RSPBoardList=[3*0,12*1]
+ObsSW.Observation.Dataslots.CS001HBA1.DataslotList=[45..47,0..11]
+ObsSW.Observation.Dataslots.CS001HBA1.RSPBoardList=[3*0,12*1]
+ObsSW.Observation.Dataslots.RS005HBA0.DataslotList=[0..3,45..47,0..7]
+ObsSW.Observation.Dataslots.RS005HBA0.RSPBoardList=[7*0,8*1]
+ObsSW.Observation.Dataslots.RS005HBA1.DataslotList=[0..3,45..47,0..7]
+ObsSW.Observation.Dataslots.RS005HBA1.RSPBoardList=[7*0,8*1]
+ObsSW.Observation.Dataslots.CS004HBA0.DataslotList=[0..14]
+ObsSW.Observation.Dataslots.CS004HBA0.RSPBoardList=[15*0]
+ObsSW.Observation.Dataslots.CS004HBA1.DataslotList=[0..14]
+ObsSW.Observation.Dataslots.CS004HBA1.RSPBoardList=[15*0]
 ObsSW.Observation.antennaArray=HBA
 ObsSW.Observation.antennaSet=HBA_DUAL
 ObsSW.Observation.bandFilter=HBA_210_250
diff --git a/LCS/ApplCommon/test/tObservation.stdout b/LCS/ApplCommon/test/tObservation.stdout
index 7985247fbd4e1772c92e55e202d2ee72ffcae24c..dfd78a1103e353cc59f960eeb7cb71cf1f62d782 100644
--- a/LCS/ApplCommon/test/tObservation.stdout
+++ b/LCS/ApplCommon/test/tObservation.stdout
@@ -1,4 +1,5 @@
 >>>
+DEBUG LCS.ApplCommon - Dataslots.CS016HBA.DataslotList
 
 Observation  : 
 ObsID        : 5025
@@ -35,6 +36,7 @@ Beam[0].TAB[1]: 5.990000, 0.490000, J2000, 20.000000, incoherent
 beamlet2beams   : [0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999]
 nrAnaBeams   : 0
 
+DEBUG LCS.ApplCommon - Dataslots.CS016HBA.DataslotList
 
 Observation  : 
 ObsID        : 5025
@@ -72,6 +74,7 @@ beamlet2beams   : [0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
 nrAnaBeams   : 0
 
 <<<
+DEBUG LCS.ApplCommon - Dataslots.CS016HBA.DataslotList
 
 Observation  : 
 ObsID        : 5025
@@ -119,15 +122,22 @@ TABringsize : 0
 beamlet2beams   : [0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,1,-1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999]
 nrAnaBeams   : 0
 
+DEBUG LCS.ApplCommon - Dataslots.CS016HBA.DataslotList
 >>>
+DEBUG LCS.ApplCommon - Dataslots.CS016HBA.DataslotList
 INFO  LCS.ApplCommon - Clock of observation 5025 and 5026 conflict
+DEBUG LCS.ApplCommon - Dataslots.CS016HBA.DataslotList
 INFO  LCS.ApplCommon - Conflicting use of receivers between observation 5025 and 5027
 DEBUG LCS.ApplCommon - receiverConflict: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111100000000
+DEBUG LCS.ApplCommon - Dataslots.CS016HBA.DataslotList
 INFO  LCS.ApplCommon - Conflict in beamlets between observation 5025 and 5028
 DEBUG LCS.ApplCommon - First conflicting beamlet: 2
+DEBUG LCS.ApplCommon - Dataslots.CS016HBA.DataslotList
 INFO  LCS.ApplCommon - Conflict in nrSlotsInFrame: 48<->54 for resp. observation 5025 and 5029
+DEBUG LCS.ApplCommon - Dataslots.CS016HBA.DataslotList
 <<<
 No conflict found in file 5 which is oke.
+DEBUG LCS.ApplCommon - Dataslots.CS016HBA.DataslotList
 getRCUbitset(96,48,'') = 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111
 getRCUbitset(96,96,'') = 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111
 getRCUbitset(96,48,LBA_XXX) = 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111111
@@ -139,7 +149,7 @@ HBA_ZERO(true)  = HBA0
 HBA_ONE(false) = HBA
 HBA_ONE(true)  = HBA1
 HBA_DUAL(false) = HBA
-HBA_DUAL(true)  = HBA
+HBA_DUAL(true)  = HBA0
 HBA_JOINED(false) = HBA
 HBA_JOINED(true)  = HBA
 LBA_INNER(false) = LBA
@@ -149,6 +159,7 @@ LBA_OUTER(true)  = LBA
 LBA_X(false) = LBA
 LBA_X(true)  = LBA
 >>>
+DEBUG LCS.ApplCommon - Dataslots.CS001HBA0.DataslotList
 OLD SYNTAX
 
 Observation  : 
@@ -164,8 +175,6 @@ filter       : HBA_210_250
 splitter     : ON
 nyquistZone  : 3
 
-Meas.set     : 
-
 (Receivers)  : [0..11]
 Stations     : [CS001,RS005,CS004]
 BLG nodes    : [bgl001..bgl003]
@@ -173,36 +182,53 @@ Storage nodes: [list001,list002]
 
 nrBeams      : 4
 Beam[0].name       : observation[5025]beam[0]_0
+Beam[0].target     : 
 Beam[0].antennaSet : HBA_ZERO
 Beam[0].momID      : 0
 Beam[0].subbandList: [2,3,4,5]
 Beam[0].beamletList: [0,1,2,3]
 nrPointings : 1
 Beam[0].pointing[0]: 6.000000, 0.500000, J2000, 2008-Dec-16 15:30:00
+nrTABs      : 0
+nrTABrings  : 0
+TABringsize : 0
 Beam[1].name       : observation[5025]beam[0]_1
+Beam[1].target     : 
 Beam[1].antennaSet : HBA_ONE
 Beam[1].momID      : 0
 Beam[1].subbandList: [2,3,4,5]
 Beam[1].beamletList: [0,1,2,3]
 nrPointings : 1
 Beam[1].pointing[0]: 6.000000, 0.500000, J2000, 2008-Dec-16 15:30:00
+nrTABs      : 0
+nrTABrings  : 0
+TABringsize : 0
 Beam[2].name       : observation[5025]beam[1]_0
+Beam[2].target     : 
 Beam[2].antennaSet : HBA_ZERO
 Beam[2].momID      : 0
 Beam[2].subbandList: [20,21,22,23,24,25,40,41,42,43,44]
 Beam[2].beamletList: [45,46,47,61,62,63,64,65,66,67,68]
 nrPointings : 1
 Beam[2].pointing[0]: 1.000000, 1.500000, J2000, 2008-Dec-16 15:30:00
+nrTABs      : 0
+nrTABrings  : 0
+TABringsize : 0
 Beam[3].name       : observation[5025]beam[1]_1
+Beam[3].target     : 
 Beam[3].antennaSet : HBA_ONE
 Beam[3].momID      : 0
 Beam[3].subbandList: [20,21,22,23,24,25,40,41,42,43,44]
 Beam[3].beamletList: [45,46,47,61,62,63,64,65,66,67,68]
 nrPointings : 1
 Beam[3].pointing[0]: 1.000000, 1.500000, J2000, 2008-Dec-16 15:30:00
+nrTABs      : 0
+nrTABrings  : 0
+TABringsize : 0
 beamlet2beams   : [0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,1,1,999,999,999,999,999,999,999,999,999,999,999,999,999,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999]
 nrAnaBeams   : 0
 
+DEBUG LCS.ApplCommon - Dataslots.CS001HBA0.DataslotList
 NEW SYNTAX
 
 Observation  : 
@@ -218,8 +244,6 @@ filter       : HBA_210_250
 splitter     : ON
 nyquistZone  : 3
 
-Meas.set     : 
-
 (Receivers)  : [0..11]
 Stations     : [CS001,RS005,CS004]
 BLG nodes    : [bgl001..bgl003]
@@ -227,33 +251,49 @@ Storage nodes: [list001,list002]
 
 nrBeams      : 4
 Beam[0].name       : observation[5025]beam[0]_0
+Beam[0].target     : 
 Beam[0].antennaSet : HBA_ZERO
 Beam[0].momID      : 0
 Beam[0].subbandList: [2,3,4,5]
 Beam[0].beamletList: [0,1,2,3]
 nrPointings : 1
 Beam[0].pointing[0]: 6.000000, 0.500000, J2000, 2008-Dec-16 15:30:00
+nrTABs      : 0
+nrTABrings  : 0
+TABringsize : 0
 Beam[1].name       : observation[5025]beam[0]_1
+Beam[1].target     : 
 Beam[1].antennaSet : HBA_ONE
 Beam[1].momID      : 0
 Beam[1].subbandList: [2,3,4,5]
 Beam[1].beamletList: [0,1,2,3]
 nrPointings : 1
 Beam[1].pointing[0]: 6.000000, 0.500000, J2000, 2008-Dec-16 15:30:00
+nrTABs      : 0
+nrTABrings  : 0
+TABringsize : 0
 Beam[2].name       : observation[5025]beam[1]_0
+Beam[2].target     : 
 Beam[2].antennaSet : HBA_ZERO
 Beam[2].momID      : 0
 Beam[2].subbandList: [20,21,22,23,24,25,40,41,42,43,44]
 Beam[2].beamletList: [45,46,47,61,62,63,64,65,66,67,68]
 nrPointings : 1
 Beam[2].pointing[0]: 1.000000, 1.500000, J2000, 2008-Dec-16 15:30:00
+nrTABs      : 0
+nrTABrings  : 0
+TABringsize : 0
 Beam[3].name       : observation[5025]beam[1]_1
+Beam[3].target     : 
 Beam[3].antennaSet : HBA_ONE
 Beam[3].momID      : 0
 Beam[3].subbandList: [20,21,22,23,24,25,40,41,42,43,44]
 Beam[3].beamletList: [45,46,47,61,62,63,64,65,66,67,68]
 nrPointings : 1
 Beam[3].pointing[0]: 1.000000, 1.500000, J2000, 2008-Dec-16 15:30:00
+nrTABs      : 0
+nrTABrings  : 0
+TABringsize : 0
 beamlet2beams   : [0,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,1,1,999,999,999,999,999,999,999,999,999,999,999,999,999,1,1,1,1,1,1,1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,999,999,999,999,999,999,999,999,999,999,999,999,999]
 nrAnaBeams   : 0
 
diff --git a/LCS/Stream/src/SocketStream.cc b/LCS/Stream/src/SocketStream.cc
index 77d972a26d97fb89d1ca84c54b656190fe5992d4..36809ca238d235ecffb28c2c75db698172bd5877 100644
--- a/LCS/Stream/src/SocketStream.cc
+++ b/LCS/Stream/src/SocketStream.cc
@@ -279,7 +279,7 @@ std::string SocketStream::readkey(const char *nfskey, time_t &timeout)
     char portStr[16];
     ssize_t len;
 
-    len = readlink(nfskey, portStr, sizeof portStr);
+    len = readlink(nfskey, portStr, sizeof portStr - 1); // reserve 1 character to insert \0 below
 
     if (len >= 0) {
       portStr[len] = 0;
diff --git a/MAC/APL/CURTDBDaemons/src/SASGateway/SASGateway.conf b/MAC/APL/CURTDBDaemons/src/SASGateway/SASGateway.conf
index c22934caf8656414cc2044cfb8719090cd46e440..82f960eb7b1e0198ce68803ea4f11328b1fb07c4 100644
--- a/MAC/APL/CURTDBDaemons/src/SASGateway/SASGateway.conf
+++ b/MAC/APL/CURTDBDaemons/src/SASGateway/SASGateway.conf
@@ -1,6 +1,6 @@
 #
 #
 #
-SASGateway.OTDBdatabase = LOFAR_3
+SASGateway.OTDBdatabase = LOFAR_4
 SASGateway.OTDBhostname = sas001
 
diff --git a/MAC/APL/MainCU/src/MACScheduler/MACScheduler.conf.in b/MAC/APL/MainCU/src/MACScheduler/MACScheduler.conf.in
index 09f8999008e17398924ee0c98a87bfc64c4a8fe6..5d9e0988dbb63657677fdb47ad37df6ab113bc04 100644
--- a/MAC/APL/MainCU/src/MACScheduler/MACScheduler.conf.in
+++ b/MAC/APL/MainCU/src/MACScheduler/MACScheduler.conf.in
@@ -1,7 +1,7 @@
 # Startup parameters for the MACScheduler.
 
 # OTDB connection info
-OTDBdatabasename	= LOFAR_3
+OTDBdatabasename	= LOFAR_4
 OTDBhostname		= sas001
 OTDBusername		= paulus
 OTDBpassword		= boskabouter
diff --git a/MAC/APL/MainCU/src/MACScheduler/ObsClaimer.cc b/MAC/APL/MainCU/src/MACScheduler/ObsClaimer.cc
index 25a8d2cbccf43274caba3d69f630139a0be96857..c3570a9f3c8aa0923480c035dba271cd9cb2f4e7 100644
--- a/MAC/APL/MainCU/src/MACScheduler/ObsClaimer.cc
+++ b/MAC/APL/MainCU/src/MACScheduler/ObsClaimer.cc
@@ -292,9 +292,9 @@ GCFEvent::TResult ObsClaimer::preparePVSS_state (GCFEvent& event, GCFPortInterfa
 				for (uint32	b(0); b < theObs.beams.size(); b++) {
 					for (uint32 t(0); t < theObs.beams[b].TABs.size(); t++) {
 						beamIndexArr.push_back  (new GCFPVInteger(b));
-						angle1Arr.push_back	  	(new GCFPVDouble(theObs.beams[b].TABs[t].angle1));
-						angle2Arr.push_back	  	(new GCFPVDouble(theObs.beams[b].TABs[t].angle2));
-						dirTypesArr.push_back 	(new GCFPVString(theObs.beams[b].TABs[t].directionType));
+						TABangle1Arr.push_back	  	(new GCFPVDouble(theObs.beams[b].TABs[t].angle1));
+						TABangle2Arr.push_back	  	(new GCFPVDouble(theObs.beams[b].TABs[t].angle2));
+						TABdirTypesArr.push_back 	(new GCFPVString(theObs.beams[b].TABs[t].directionType));
 						dispersionArr.push_back (new GCFPVDouble(theObs.beams[b].TABs[t].dispersionMeasure));
 						coherentArr.push_back	(new GCFPVBool(theObs.beams[b].TABs[t].coherent));
 					}
@@ -305,7 +305,7 @@ GCFEvent::TResult ObsClaimer::preparePVSS_state (GCFEvent& event, GCFPortInterfa
 				theObsPS->setValue(PN_OBS_TIED_ARRAY_BEAMS_ANGLE1,			GCFPVDynArr(LPT_DYNDOUBLE, TABangle1Arr),   0.0, false);
 				theObsPS->setValue(PN_OBS_TIED_ARRAY_BEAMS_ANGLE2,			GCFPVDynArr(LPT_DYNDOUBLE, TABangle2Arr),   0.0, false);
 				theObsPS->setValue(PN_OBS_TIED_ARRAY_BEAMS_DIRECTION_TYPE,	GCFPVDynArr(LPT_DYNSTRING, TABdirTypesArr), 0.0, false);
-				theObsPS->setValue(PN_OBS_TIED_ARRAY_BEAMS_DISPERSION,		GCFPVDynArr(LPT_DYNSTRING, dispersionArr),  0.0, false);
+				theObsPS->setValue(PN_OBS_TIED_ARRAY_BEAMS_DISPERSION,		GCFPVDynArr(LPT_DYNDOUBLE, dispersionArr),  0.0, false);
 				theObsPS->setValue(PN_OBS_TIED_ARRAY_BEAMS_COHERENT,		GCFPVDynArr(LPT_DYNBOOL, coherentArr),  0.0, false);
 				theObsPS->flush();
 
diff --git a/MAC/Deployment/data/License/CS017C_option.txt b/MAC/Deployment/data/License/CS017C_option.txt
index c90ce92fc6098adfa44b5b916c2febccdedb28cf..b7795abfe2c9fcdff1bd7fe4cb3a8b12aad12970 100644
--- a/MAC/Deployment/data/License/CS017C_option.txt
+++ b/MAC/Deployment/data/License/CS017C_option.txt
@@ -1,5 +1,5 @@
 [license]
-code          = "CS017C 11247311299"
+code          = "CS017C 04214881484"
 version       = 30800002
 comment       = "Core Station CS017"
 sn            = "471_3031_2_Astron_Gen_II_1_38"
diff --git a/MAC/Deployment/data/License/CS401C_option.txt b/MAC/Deployment/data/License/CS401C_option.txt
index fa4d8e2e5b2c16771bd7d87625cd699366528c11..a7ca174b4635c93928432962ca9ee7cad5960837 100644
--- a/MAC/Deployment/data/License/CS401C_option.txt
+++ b/MAC/Deployment/data/License/CS401C_option.txt
@@ -1,5 +1,5 @@
 [license]
-code          = "CS401C 92464652419"
+code          = "CS401C 40501009571"
 version       = 30800002
 comment       = "Core Station CS401"
 sn            = "471_3031_2_Astron_Gen_II_1_38"
diff --git a/MAC/Deployment/data/License/shield.CCU001.txt b/MAC/Deployment/data/License/shield.CCU001.txt
index 486093ad777c7df88891f770d71d8c7bbb843475..62f6a39279e1218d9fc3b0f1665d51041511643f 100644
--- a/MAC/Deployment/data/License/shield.CCU001.txt
+++ b/MAC/Deployment/data/License/shield.CCU001.txt
@@ -1,5 +1,5 @@
 [license]
-code        = "CCU001 10558176559"
+code        = "CCU001 20230388790"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/4"
 date        = 2009.03.30;16:26:20,000 
@@ -14,16 +14,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CCU099.txt b/MAC/Deployment/data/License/shield.CCU099.txt
index cb8d540accd7c289764e2718f25073af9c4fc6ee..5eef98bf1185686d2e6e705a8b5f645601997742 100644
--- a/MAC/Deployment/data/License/shield.CCU099.txt
+++ b/MAC/Deployment/data/License/shield.CCU099.txt
@@ -1,5 +1,5 @@
 [license]
-code        = "CCU099 40448510156"
+code        = "CCU099 00473561460"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/54"
 date        = 2012.02.09;09:59:23,000 
@@ -14,16 +14,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS001C.txt b/MAC/Deployment/data/License/shield.CS001C.txt
index 9e27bbb79177669591b82f72289c1ff36c856a15..883a32ec4da3545b496b1f900691a4e579088b40 100644
--- a/MAC/Deployment/data/License/shield.CS001C.txt
+++ b/MAC/Deployment/data/License/shield.CS001C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS001C 50292755634"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/30"
-date        = 2010.03.25;16:44:37,000 
-comment     = "Core Station CS001"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS001C 81098098283"
+code        = "CS001C 70055309409"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/30"
+date        = 2010.03.25;16:44:37,000 
+comment     = "Core Station CS001"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS001C_lcu037.txt b/MAC/Deployment/data/License/shield.CS001C_lcu037.txt
index bbf36c763a8ba1d7ef978f272d1fe75a4280eeb9..22a89791acc15cdfe7de5ca105cec4cb1c5eb630 100644
--- a/MAC/Deployment/data/License/shield.CS001C_lcu037.txt
+++ b/MAC/Deployment/data/License/shield.CS001C_lcu037.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS001C 10323819506"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/26"
-date        = 2010.02.12;10:04:45,000 
-comment     = "Core Station CS001"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS001C 92464652419"
+code        = "CS001C 20086373281"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/26"
+date        = 2010.02.12;10:04:45,000 
+comment     = "Core Station CS001"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS002C.txt b/MAC/Deployment/data/License/shield.CS002C.txt
index 8a2f87bc7139893511dbc8d0249279997f3ec17f..cad1d21a9f78e34b50b8b9f9e25ff30ff4ac022d 100644
--- a/MAC/Deployment/data/License/shield.CS002C.txt
+++ b/MAC/Deployment/data/License/shield.CS002C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS002C 10507053791"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/15"
-date        = 2009.12.09;14:53:26,000 
-comment     = "Core Station CS002"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS002C 33940646999"
+code        = "CS002C 90269607566"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/15"
+date        = 2009.12.09;14:53:26,000 
+comment     = "Core Station CS002"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS003C.txt b/MAC/Deployment/data/License/shield.CS003C.txt
index 81690d32fb5ec0a809a9894b21a3fbe6d6638177..d831682976ca0ba6fce98f8e0e110d6d4ff55056 100644
--- a/MAC/Deployment/data/License/shield.CS003C.txt
+++ b/MAC/Deployment/data/License/shield.CS003C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS003C 20209925018"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/16"
-date        = 2009.12.09;14:56:42,000 
-comment     = "Core Station CS003"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS003C 31558627779"
+code        = "CS003C 20588377280"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/16"
+date        = 2009.12.09;14:56:42,000 
+comment     = "Core Station CS003"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS004C.txt b/MAC/Deployment/data/License/shield.CS004C.txt
index 494a1c8087a31871cd654d0a86192ed12f48d810..e1b3279a26e744f82e025749a1b02ff95dfdf307 100644
--- a/MAC/Deployment/data/License/shield.CS004C.txt
+++ b/MAC/Deployment/data/License/shield.CS004C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS004C 70362514653"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/17"
-date        = 2009.12.09;14:58:20,000 
-comment     = "Core Station CS004"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS004C 02587602048"
+code        = "CS004C 60125068428"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/17"
+date        = 2009.12.09;14:58:20,000 
+comment     = "Core Station CS004"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS005C.txt b/MAC/Deployment/data/License/shield.CS005C.txt
index 3b0466dec229a1a231bf365e580325aa5f89ecc4..67824ff69246f4cf5a43fd768e6bedba653d4221 100644
--- a/MAC/Deployment/data/License/shield.CS005C.txt
+++ b/MAC/Deployment/data/License/shield.CS005C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS005C 90384279129"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/18"
-date        = 2009.12.09;14:59:50,000 
-comment     = "Core Station CS005"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS005C 33821554483"
+code        = "CS005C 70146832904"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/18"
+date        = 2009.12.09;14:59:50,000 
+comment     = "Core Station CS005"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS006C.txt b/MAC/Deployment/data/License/shield.CS006C.txt
index 4b51e23e44470eeb0370126758bdaa7d88cbb622..a243b6ea679a50d9ac690623a32c93a4bbb47562 100644
--- a/MAC/Deployment/data/License/shield.CS006C.txt
+++ b/MAC/Deployment/data/License/shield.CS006C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS006C 40418214592"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/14"
-date        = 2009.11.26;14:56:12,000 
-comment     = "Core Station CS006"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS006C 03394488018"
+code        = "CS006C 30287713812"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/14"
+date        = 2009.11.26;14:56:12,000 
+comment     = "Core Station CS006"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS007C.txt b/MAC/Deployment/data/License/shield.CS007C.txt
index c1adc8fa6ee9eafad7703df42a323917308b95b5..08d5728303bc057e2616220d42107fa4b09eda68 100644
--- a/MAC/Deployment/data/License/shield.CS007C.txt
+++ b/MAC/Deployment/data/License/shield.CS007C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS007C 20224043076"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/19"
-date        = 2009.12.09;15:01:09,000 
-comment     = "Core Station CS007"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS007C 20478853832"
+code        = "CS007C 40602495338"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/19"
+date        = 2009.12.09;15:01:09,000 
+comment     = "Core Station CS007"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS011C.txt b/MAC/Deployment/data/License/shield.CS011C.txt
index a8652c7b1195b390f16915f3e9751fd08caee622..2a9756c63a7137c201b9cb8beb42c823a26c1302 100644
--- a/MAC/Deployment/data/License/shield.CS011C.txt
+++ b/MAC/Deployment/data/License/shield.CS011C.txt
@@ -1,5 +1,5 @@
 [license]
-code        = "CS011C 80167582799"
+code        = "CS011C 60546035061"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/51"
 date        = 2011.09.19;16:47:21,000 
@@ -14,16 +14,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS013C.txt b/MAC/Deployment/data/License/shield.CS013C.txt
index 2e9ce9ba969c892296616cbf6423da4230eb4697..40c61f20add89936726dd1312f79fea367f50a19 100644
--- a/MAC/Deployment/data/License/shield.CS013C.txt
+++ b/MAC/Deployment/data/License/shield.CS013C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS013C 80033405458"
+#hw         = "CS013C 62902549878"
+code        = "CS013C 80310954332"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/52"
 date        = 2011.09.19;16:47:30,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS017C.txt b/MAC/Deployment/data/License/shield.CS017C.txt
index 8450ac34520f721f9f2b2e18c756a6ca1c6ecc01..4cd5dcb28dc38809ba62e7b769ca210cd15e743d 100644
--- a/MAC/Deployment/data/License/shield.CS017C.txt
+++ b/MAC/Deployment/data/License/shield.CS017C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS017C 10256717685"
+#hw           = "CS017C 04214881484"
+code        = "CS017C 10367727830"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/38"
 date        = 2010.08.18;08:37:47,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS021C.txt b/MAC/Deployment/data/License/shield.CS021C.txt
index 4afa2c34fb836826116f12e7cbe135f445dae73a..8e50bdc720d3495f31673993134247b053aaeae8 100644
--- a/MAC/Deployment/data/License/shield.CS021C.txt
+++ b/MAC/Deployment/data/License/shield.CS021C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS021C 60475149084"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/13"
-date        = 2009.11.26;14:47:57,000 
-comment     = "Core Station CS021"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS021C 12999568290"
+code        = "CS021C 40344648304"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/13"
+date        = 2009.11.26;14:47:57,000 
+comment     = "Core Station CS021"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS024C.txt b/MAC/Deployment/data/License/shield.CS024C.txt
index c67478b7241161f2e77fbff35b050a0abcf4983c..a41d4898a2868a4ccddd5a04683c7050636321e6 100644
--- a/MAC/Deployment/data/License/shield.CS024C.txt
+++ b/MAC/Deployment/data/License/shield.CS024C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS024C 40135530647"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/27"
-date        = 2010.02.25;14:38:35,000 
-comment     = "Core Station CS024"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS024C 93447640285"
+code        = "CS024C 40513982909"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/27"
+date        = 2010.02.25;14:38:35,000 
+comment     = "Core Station CS024"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS026C.txt b/MAC/Deployment/data/License/shield.CS026C.txt
index 0c041c3683cb51d755591cd5ba59f9828bb8d951..09a44420e3dd6214163f06919293297892b66acf 100644
--- a/MAC/Deployment/data/License/shield.CS026C.txt
+++ b/MAC/Deployment/data/License/shield.CS026C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS026C 80339362600"
+#hw         = "CS026C 52302464505"
+code        = "CS026C 50101916375"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/39"
 date        = 2010.10.07;13:43:19,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS028C.txt b/MAC/Deployment/data/License/shield.CS028C.txt
index e96d20dc9eee049bb6e7cc4f37eb5ebd4eb336d4..d4d6f0c202a4c0b10e98d21cbc0d35261d9d2511 100644
--- a/MAC/Deployment/data/License/shield.CS028C.txt
+++ b/MAC/Deployment/data/License/shield.CS028C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS028C 80471969851"
+#hw         = "CS028C 12420659036"
+code        = "CS028C 30234523626"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/45"
 date        = 2011.05.30;11:59:20,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS030.txt b/MAC/Deployment/data/License/shield.CS030.txt
index 6c104c680e8090d281573fff04dc1d7220fca278..94c7c5bc5a566edb85657a4880914ee9f3321784 100644
--- a/MAC/Deployment/data/License/shield.CS030.txt
+++ b/MAC/Deployment/data/License/shield.CS030.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS030C 80503419471"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/11"
-date        = 2009.11.03;11:03:44,000 
-comment     = "Core Station CS030"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS030C 91605312865"
+code        = "CS030C 20265973246"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/11"
+date        = 2009.11.03;11:03:44,000 
+comment     = "Core Station CS030"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS031C.txt b/MAC/Deployment/data/License/shield.CS031C.txt
index 95cc7f83aeb384d27d4164db308a11fbe1534745..5898e18fc9408ddad4f4dd994e0c15f543b96ba5 100644
--- a/MAC/Deployment/data/License/shield.CS031C.txt
+++ b/MAC/Deployment/data/License/shield.CS031C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS031C 90323161184"
+#hw         = "CS031C 23688432570"
+code        = "CS031C 00085714959"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/53"
 date        = 2011.09.19;16:47:39,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS032C.lcu013.txt b/MAC/Deployment/data/License/shield.CS032C.lcu013.txt
index 2d2579333947e9adc075a5ab70db9db8e8867229..1393882de4f796f47cf00fdf81c214b1c816043b 100644
--- a/MAC/Deployment/data/License/shield.CS032C.lcu013.txt
+++ b/MAC/Deployment/data/License/shield.CS032C.lcu013.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS032C 30464888506"
+#hw         = "CS032C 71518917137"
+code        = "CS032C 50489939810"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/29"
 date        = 2010.03.03;13:41:06,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS032C.txt b/MAC/Deployment/data/License/shield.CS032C.txt
index dd50f413f3a40f97422728b630f01b3f983b76f1..5f0b90350c538a9178d2f1b79a4fb87f5417ff61 100644
--- a/MAC/Deployment/data/License/shield.CS032C.txt
+++ b/MAC/Deployment/data/License/shield.CS032C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS032C 30143537309"
+#hw         = "CS032C 92655900141"
+code        = "CS032C 30521989571"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/31"
 date        = 2010.04.29;16:25:00,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS101C.txt b/MAC/Deployment/data/License/shield.CS101C.txt
index 071700eaafd124a6159b5a1b7aca8d62f8947366..02074e1940e6e14f3da97d11c2fc4b4cde780cbf 100644
--- a/MAC/Deployment/data/License/shield.CS101C.txt
+++ b/MAC/Deployment/data/License/shield.CS101C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS101C 70375298533"
+#hw         = "CS101C 42344904008"
+code        = "CS101C 10137852308"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/34"
 date        = 2010.05.28;14:42:30,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS103C.txt b/MAC/Deployment/data/License/shield.CS103C.txt
index 3ef3cd9a69e2546d76d894792c1619b41730c65a..1d96f74262cc9fb79eec5fbbab93a4a427eaeea7 100644
--- a/MAC/Deployment/data/License/shield.CS103C.txt
+++ b/MAC/Deployment/data/License/shield.CS103C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS103C 70140536604"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/28"
-date        = 2010.02.25;14:38:49,000 
-comment     = "Core Station CS103"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS103C 70084677827"
+code        = "CS103C 30518988866"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/28"
+date        = 2010.02.25;14:38:49,000 
+comment     = "Core Station CS103"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS201C.txt b/MAC/Deployment/data/License/shield.CS201C.txt
index 9e86c92250123d504497d8cdfa2a71d67d81fa62..78fb3e16e436fe6dcdaf70eeada57b56cd2eb3c1 100644
--- a/MAC/Deployment/data/License/shield.CS201C.txt
+++ b/MAC/Deployment/data/License/shield.CS201C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS201C 20404962076"
+#hw         = "CS201C 13023438927"
+code        = "CS201C 30167515851"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/37"
 date        = 2010.06.28;15:56:35,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS301C.txt b/MAC/Deployment/data/License/shield.CS301C.txt
index 1cee564e3db49d8d970483c9b7bc9bacba2fd819..e2d10676636fdd928437e5a8ff69290b3fc57833 100644
--- a/MAC/Deployment/data/License/shield.CS301C.txt
+++ b/MAC/Deployment/data/License/shield.CS301C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS301C 40021924673"
+#hw         = "CS301C 42906961546"
+code        = "CS301C 90400376935"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/35"
 date        = 2010.06.22;09:14:47,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS302.txt b/MAC/Deployment/data/License/shield.CS302.txt
index 58109e025146c7a09b45f63333ce56fb3b83a5d4..64ff1234f30bf6f63893ee004c6f400d83fa07fb 100644
--- a/MAC/Deployment/data/License/shield.CS302.txt
+++ b/MAC/Deployment/data/License/shield.CS302.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "CS302C 30388841157"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/5"
-date        = 2009.04.23;11:47:34,000 
-comment     = "Core Station CS302"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "CS302C 94045008620"
+code        = "CS302C 20473128689"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/5"
+date        = 2009.04.23;11:47:34,000 
+comment     = "Core Station CS302"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.CS401C.lcu044.txt b/MAC/Deployment/data/License/shield.CS401C.lcu044.txt
index d90f00f6c1afd64b41d8ff2bb9f1e97beb5cb179..3ddad9e22b698b23f1f3e3e8523ec1e19b87ed6d 100644
--- a/MAC/Deployment/data/License/shield.CS401C.lcu044.txt
+++ b/MAC/Deployment/data/License/shield.CS401C.lcu044.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS401C 20071310778"
+#hw         = "CS401C 40501009571"
+code        = "CS401C 50449763040"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/21"
 date        = 2010.01.18;11:22:12,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS401C.txt b/MAC/Deployment/data/License/shield.CS401C.txt
index 4fbdc4d3f64054626eb5d299b303a8f433e60bc7..ab972fbad2565e96453a8beed4b357972f9b10a0 100644
--- a/MAC/Deployment/data/License/shield.CS401C.txt
+++ b/MAC/Deployment/data/License/shield.CS401C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS401C 50062682456"
+#hw           = "CS401C 40501009571"
+code        = "CS401C 80523523796"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/32"
 date        = 2010.04.29;16:27:19,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.CS501C.txt b/MAC/Deployment/data/License/shield.CS501C.txt
index b290d392211242f7d65f6612521575b2b7458bfd..6044e89401a9f0d07d9a6be58f37548277c91e2e 100644
--- a/MAC/Deployment/data/License/shield.CS501C.txt
+++ b/MAC/Deployment/data/License/shield.CS501C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "CS501C 90356500491"
+#hw         = "CS501C 30123167322"
+code        = "CS501C 00119054266"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/36"
 date        = 2010.06.22;09:15:00,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.DE601C.txt b/MAC/Deployment/data/License/shield.DE601C.txt
index fe7522eaa64a2772e02a63f1e8d197e4a9150510..fb5da0ce4abec5b35ea70d29b6bda504d0977fbe 100644
--- a/MAC/Deployment/data/License/shield.DE601C.txt
+++ b/MAC/Deployment/data/License/shield.DE601C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "DE601C 00612457838"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/12"
-date        = 2009.11.18;16:43:50,000 
-comment     = "European Station DE601"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "DE601C 72425023412"
+code        = "DE601C 30496540564"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/12"
+date        = 2009.11.18;16:43:50,000 
+comment     = "European Station DE601"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.DE602C.txt b/MAC/Deployment/data/License/shield.DE602C.txt
index 0e06c438f857c9e103c0abaec3b72ffafa41e656..644c3b7c160c9c6a262209173783f54629b3cb79 100644
--- a/MAC/Deployment/data/License/shield.DE602C.txt
+++ b/MAC/Deployment/data/License/shield.DE602C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "DE602C 80309964458"
+#hw         = "DE602C 34192162745"
+code        = "DE602C 50582430863"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/33"
 date        = 2010.05.20;13:49:26,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.DE603C.txt b/MAC/Deployment/data/License/shield.DE603C.txt
index 49a076fb039529bdc0c14fc31b48f86bb1c337eb..d78378186537daa91b80a10ddb0d7e1250a4fec1 100644
--- a/MAC/Deployment/data/License/shield.DE603C.txt
+++ b/MAC/Deployment/data/License/shield.DE603C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "DE603C 10002221239"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/25"
-date        = 2010.02.09;14:37:26,000 
-comment     = "European Station DE603"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "DE603C 23550524031"
+code        = "DE603C 40058066232"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/25"
+date        = 2010.02.09;14:37:26,000 
+comment     = "European Station DE603"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.DE604C.txt b/MAC/Deployment/data/License/shield.DE604C.txt
index 5994c907e70a3d0f88c5511ad349282bf1619867..75097fbb028e5543f1905d05b6b8775d5d286ba5 100644
--- a/MAC/Deployment/data/License/shield.DE604C.txt
+++ b/MAC/Deployment/data/License/shield.DE604C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "DE604C 70433674798"
+#hw         = "DE604C 64169956336"
+code        = "DE604C 30090242716"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/44"
 date        = 2011.05.19;12:56:46,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.DE605C.txt b/MAC/Deployment/data/License/shield.DE605C.txt
index 3f09afe53618faad05842406c0109d9376210913..b7ac20ef68812f65daa0027bf4fbf8f4b44d996a 100644
--- a/MAC/Deployment/data/License/shield.DE605C.txt
+++ b/MAC/Deployment/data/License/shield.DE605C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "DE605C 10436351124"
+#hw         = "DE605C 50975472942"
+code        = "DE605C 20092919042"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/48"
 date        = 2011.07.20;10:05:53,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.FR606C.txt b/MAC/Deployment/data/License/shield.FR606C.txt
index fe489a35fd9eddf0a020984aee2f830286e65291..286585dfb2bff363718eae658f3d449b317ea9c3 100644
--- a/MAC/Deployment/data/License/shield.FR606C.txt
+++ b/MAC/Deployment/data/License/shield.FR606C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "FR606C 50351316006"
+#hw         = "FR606C 94042056811"
+code        = "FR606C 20118921167"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/40"
 date        = 2010.10.26;15:44:20,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.MCU001.txt b/MAC/Deployment/data/License/shield.MCU001.txt
index 0d4b78922a6587dead5e7442a031bf7b4f86e5b7..7e5ead4a850e75fb5003fde122bb865d1f5f8d87 100644
--- a/MAC/Deployment/data/License/shield.MCU001.txt
+++ b/MAC/Deployment/data/License/shield.MCU001.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "MCU001 40229760754"
+#hw         = "MCU001 70462893533"
+code        = "MCU001 10168691426"
 version     = 30800002
 sn          = "471_3031_1_Astron_Gen_I_1_38/2"
 date        = 2009.08.31;16:12:45,000 
@@ -14,15 +15,15 @@ ios         = 100000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 15
- infoserver = 5000
-  comcenter = 5
+http        = 15
+infoserver  = 5000
+comcenter   = 5
 maintenance = 0
-  scheduler = 0
+scheduler   = 0
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.MCU099.txt b/MAC/Deployment/data/License/shield.MCU099.txt
index 1b52418ce822c82826eb25ef2fdbffa8f2272a72..abf9a1bb13a8035d92da1ce81f1f6d31e7dea092 100644
--- a/MAC/Deployment/data/License/shield.MCU099.txt
+++ b/MAC/Deployment/data/License/shield.MCU099.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "MCU099 00040690038"
+#hw         = "MCU099 02772147255"
+code        = "MCU099 60315490059"
 version     = 30800002
 sn          = "471_3031_1_Astron_Gen_I_1_38/3"
 date        = 2012.02.09;09:58:38,000 
@@ -14,15 +15,15 @@ ios         = 100000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 15
- infoserver = 5000
-  comcenter = 5
+http        = 15
+infoserver  = 5000
+comcenter   = 5
 maintenance = 0
-  scheduler = 0
+scheduler   = 0
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.RS106C.txt b/MAC/Deployment/data/License/shield.RS106C.txt
index 4f519226183778bb4ee354e88726c305205fb8d2..3cadccd7d6283e514798c4cc8c1e39e9f1a0c558 100644
--- a/MAC/Deployment/data/License/shield.RS106C.txt
+++ b/MAC/Deployment/data/License/shield.RS106C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "RS106C 50573306010"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/8"
-date        = 2009.09.02;15:57:22,000 
-comment     = "Remote Station RS106"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "RS106C 42096275889"
+code        = "RS106C 50112128336"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/8"
+date        = 2009.09.02;15:57:22,000 
+comment     = "Remote Station RS106"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.RS205.txt b/MAC/Deployment/data/License/shield.RS205.txt
index a05cc3ceb7c08a985362330b489852dae664f854..7e49c7eec50492a2446000b4d57384666bbc3bc8 100644
--- a/MAC/Deployment/data/License/shield.RS205.txt
+++ b/MAC/Deployment/data/License/shield.RS205.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "RS205C 50428582765"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/22"
-date        = 2010.01.26;11:53:21,000 
-comment     = "Remote Station RS205"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "RS205C 71221926192"
+code        = "RS205C 40042044938"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/22"
+date        = 2010.01.26;11:53:21,000 
+comment     = "Remote Station RS205"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.RS208C.txt b/MAC/Deployment/data/License/shield.RS208C.txt
index a2426a0d1c1e53abd0171d4041e5cdcaa25378fe..d60c4ce205b4620f9404543b1a4605e7a98fb00d 100644
--- a/MAC/Deployment/data/License/shield.RS208C.txt
+++ b/MAC/Deployment/data/License/shield.RS208C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "RS208C 40551551895"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/10"
-date        = 2009.09.10;13:29:03,000 
-comment     = "Remote Station RS208"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "RS208C 73811290430"
+code        = "RS208C 90165014068"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/10"
+date        = 2009.09.10;13:29:03,000 
+comment     = "Remote Station RS208"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.RS306C.txt b/MAC/Deployment/data/License/shield.RS306C.txt
index c87870a921dc89b91263e29587288196c9f9a470..b90c9e2f3cfc1ff70a4f30a8cf34e408f42fdd93 100644
--- a/MAC/Deployment/data/License/shield.RS306C.txt
+++ b/MAC/Deployment/data/License/shield.RS306C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "RS306C 80417397667"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/24"
-date        = 2010.01.27;12:23:23,000 
-comment     = "Remote Station RS306"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "RS306C 71398563501"
+code        = "RS306C 10030859840"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/24"
+date        = 2010.01.27;12:23:23,000 
+comment     = "Remote Station RS306"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.RS307C.lcu013.txt b/MAC/Deployment/data/License/shield.RS307C.lcu013.txt
index 70797a24b3ac44160c2924b4ecb985dfb4281dd2..9a3824fe506dae6cad38726daa85751ce0dd2751 100644
--- a/MAC/Deployment/data/License/shield.RS307C.lcu013.txt
+++ b/MAC/Deployment/data/License/shield.RS307C.lcu013.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "RS307C 80199658064"
+#hw         = "RS307C 71518917137"
+code        = "RS307C 70429018724"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/43"
 date        = 2011.01.20;14:31:40,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.RS307C_lcu045.txt b/MAC/Deployment/data/License/shield.RS307C_lcu045.txt
index f37e7dcb6d8ef2f3bad7d63c24e2cafb404f93f5..e20659074f7b111fd5b7608869dc7b51e3a47e54 100644
--- a/MAC/Deployment/data/License/shield.RS307C_lcu045.txt
+++ b/MAC/Deployment/data/License/shield.RS307C_lcu045.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "RS307C 00224921370"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/7"
-date        = 2009.06.24;09:28:10,000 
-comment     = "Remote Station RS307"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "RS307C 92121594562"
+code        = "RS307C 40405186033"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/7"
+date        = 2009.06.24;09:28:10,000 
+comment     = "Remote Station RS307"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.RS406C.txt b/MAC/Deployment/data/License/shield.RS406C.txt
index d4f33d415c36aa6ec492ad81c3a3e17afd4715b3..e32d917d6447306b5229daed7241919d3827ceec 100644
--- a/MAC/Deployment/data/License/shield.RS406C.txt
+++ b/MAC/Deployment/data/License/shield.RS406C.txt
@@ -1,5 +1,5 @@
 [license]
-code        = "RS406C 70352277188"
+code        = "RS406C 00581637848"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/41"
 date        = 2010.12.03;16:00:56,000 
@@ -14,16 +14,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.RS503C.txt b/MAC/Deployment/data/License/shield.RS503C.txt
index 793b66836936e4c698ffe5602f7515da05a370da..095d30d7428066e52778ef805766d8149b0ed762 100644
--- a/MAC/Deployment/data/License/shield.RS503C.txt
+++ b/MAC/Deployment/data/License/shield.RS503C.txt
@@ -1,29 +1,30 @@
-[license]
-code        = "RS503C 60337547034"
-version     = 30800002
-sn          = "471_3031_2_Astron_Gen_II_1_38/6"
-date        = 2009.06.24;08:53:32,000 
-comment     = "Remote Station RS503"
-expire      = 0000.00.00;00:00:00,000 
-redundancy  = 0
-ui          = 2
-para        = 1
-dde         = 5
-event       = 1
-ios         = 4000
-ssi         = 0
-api         = 80
-excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
-maintenance = 1
-  scheduler = 1
-     recipe = 1
-distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
-
+[license]
+#hw         = "RS503C 21044372319"
+code        = "RS503C 30517811697"
+version     = 30800002
+sn          = "471_3031_2_Astron_Gen_II_1_38/6"
+date        = 2009.06.24;08:53:32,000 
+comment     = "Remote Station RS503"
+expire      = 0000.00.00;00:00:00,000 
+redundancy  = 0
+ui          = 2
+para        = 1
+dde         = 5
+event       = 1
+ios         = 4000
+ssi         = 0
+api         = 80
+excelreport = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
+maintenance = 1
+scheduler   = 1
+recipe      = 1
+distributed = 255
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
+
diff --git a/MAC/Deployment/data/License/shield.RS508C.txt b/MAC/Deployment/data/License/shield.RS508C.txt
index 97144cafdfe0812498b5dbafffb04267f9762e49..279a0962076e6012631e74d9bc837772704450be 100644
--- a/MAC/Deployment/data/License/shield.RS508C.txt
+++ b/MAC/Deployment/data/License/shield.RS508C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "RS508C 20518742593"
+#hw         = "RS508C 01374926509"
+code        = "RS508C 10132204766"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/49"
 date        = 2011.07.27;15:22:14,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.RS509C.txt b/MAC/Deployment/data/License/shield.RS509C.txt
index 6742d0b6888c5b8e2d583706a749174768a3330e..78c39f159b6b20ea8ea6b8130048bc0a1c6cadd8 100644
--- a/MAC/Deployment/data/License/shield.RS509C.txt
+++ b/MAC/Deployment/data/License/shield.RS509C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "RS509C 00576443799"
+#hw         = "RS509C 82238627624"
+code        = "RS509C 30445943019"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/46"
 date        = 2011.06.08;14:25:21,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.SE607C.txt b/MAC/Deployment/data/License/shield.SE607C.txt
index dccef2785d1f5d91acb170bf16b5663c349dddac..87812495f6f9bb54b82e36031e0bcc9fc575d29b 100644
--- a/MAC/Deployment/data/License/shield.SE607C.txt
+++ b/MAC/Deployment/data/License/shield.SE607C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "SE607C 20592079345"
+#hw         = "SE607C 20243551923"
+code        = "SE607C 20248647263"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/50"
 date        = 2011.08.30;14:22:28,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/License/shield.UK608C.txt b/MAC/Deployment/data/License/shield.UK608C.txt
index 9eb7e3949804e71b03158306ecbeb1d96158880e..6959a94a573e51e35bd7c72f6dbc5fe3bd931478 100644
--- a/MAC/Deployment/data/License/shield.UK608C.txt
+++ b/MAC/Deployment/data/License/shield.UK608C.txt
@@ -1,5 +1,6 @@
 [license]
-code        = "UK608C 10556074073"
+#hw         = "UK608C 21277950322"
+code        = "UK608C 50440156799"
 version     = 30800002
 sn          = "471_3031_2_Astron_Gen_II_1_38/42"
 date        = 2010.12.15;15:20:01,000 
@@ -14,16 +15,16 @@ ios         = 4000
 ssi         = 0
 api         = 80
 excelreport = 5
-       http = 0
- infoserver = 1000
-  comcenter = 5
+http        = 0
+infoserver  = 1000
+comcenter   = 5
 maintenance = 1
-  scheduler = 1
-     recipe = 1
+scheduler   = 1
+recipe      = 1
 distributed = 255
-      uifix = 0
-    parafix = 0
- pararemote = 0
-    ctrlext = 1
-     update = 0
+uifix       = 0
+parafix     = 0
+pararemote  = 0
+ctrlext     = 1
+update      = 1
 
diff --git a/MAC/Deployment/data/OTDB/OLAP.comp b/MAC/Deployment/data/OTDB/OLAP.comp
index 0579289d0ee73cfaf329df5a84c6b52103612c0b..0098827a6e8b8fe12f6efef99a8a6e81e8c8a08b 100644
--- a/MAC/Deployment/data/OTDB/OLAP.comp
+++ b/MAC/Deployment/data/OTDB/OLAP.comp
@@ -30,7 +30,7 @@ node   CNProc_CoherentStokes     4.0.0  development 'node constraint'  "Coherent
 par  which                   I    ptext  -      10    0      "I|IQUV|XXYY;I"                           - "Which Stokes values to compute"
 par  channelsPerSubband      I    int    -      10    0      ">>Observation.channelsPerSubband"        - "Number of channels for Stokes data"
 par  timeIntegrationFactor   I    int    -      10    0      1                                         - "Time-wise integration of Stokes data (in samples)"
-par  subbandsPerFile         I	  int    -	10    0      1                                         - "the number of subbands each Stokes file will hold"
+par  subbandsPerFile         I	  int    -	10    0      512                                       - "the number of subbands each Stokes file will hold"
 
 # -- CNProc_IncoherentStokes --
 #
@@ -43,7 +43,7 @@ node   CNProc_IncoherentStokes   4.0.0  development 'node constraint'  "Incohere
 par  which                   I    ptext  -      10    0      "I|IQUV;I"                                - "Which Stokes values to compute"
 par  channelsPerSubband      I    int    -      10    0      ">>Observation.channelsPerSubband"        - "Number of channels for Stokes data"
 par  timeIntegrationFactor   I    int    -      10    0      1                                         - "Time-wise integration of Stokes data (in samples)"
-par  subbandsPerFile         I    int    -      10    0      1                                         - "the number of subbands each Stokes file will hold"
+par  subbandsPerFile         I    int    -      10    0      512                                       - "the number of subbands each Stokes file will hold"
 
 
 # -- OLAP_Conni --
diff --git a/MAC/Deployment/data/PVSS/Observation.dpdef b/MAC/Deployment/data/PVSS/Observation.dpdef
index 2cc5935c70c138c2624b01b14762392065df03a3..91235ccb84d4f2f892c336a91fd7419e5d5841d0 100644
--- a/MAC/Deployment/data/PVSS/Observation.dpdef
+++ b/MAC/Deployment/data/PVSS/Observation.dpdef
@@ -25,7 +25,7 @@ Beams.directionType	stringArr
 Beams.beamletList	stringArr
 Beams.subbandList	stringArr
 
-TiedArrayBeams.beamIndex		doubleArr
+TiedArrayBeams.beamIndex		intArr
 TiedArrayBeams.angle1			doubleArr
 TiedArrayBeams.angle2			doubleArr
 TiedArrayBeams.directionType 	stringArr
diff --git a/MAC/Deployment/data/StaticMetaData/PPSDelays/CS501-PPSdelays.conf b/MAC/Deployment/data/StaticMetaData/PPSDelays/CS501-PPSdelays.conf
index dd705e38b6786a3b69807d88e2e43d35fe0436e6..7d030b0da2ae34cd0165f7438fc02fbd502a281c 100644
--- a/MAC/Deployment/data/StaticMetaData/PPSDelays/CS501-PPSdelays.conf
+++ b/MAC/Deployment/data/StaticMetaData/PPSDelays/CS501-PPSdelays.conf
@@ -1,10 +1,10 @@
 #
-# APdelay.conf for CS501C
+# PPSDelays.conf for CS501
 #
-# 01-Feb-2012-1010
+# 09-Feb-2012-0828
 #
 48 [ 
-12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 
-12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 12 
-40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 
-]
+31 31 31 31 29 29 29 29 29 29 29 29 28 28 28 28 
+51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 
+29 29 29 29 34 34 34 34 36 36 36 36 35 35 35 35 
+]
\ No newline at end of file
diff --git a/MAC/Deployment/data/StaticMetaData/PPSDelays/RS306-PPSdelays.conf b/MAC/Deployment/data/StaticMetaData/PPSDelays/RS306-PPSdelays.conf
index 92c9ec9f69b5aa9478d54aa6e8897d4065e54ce6..0eaf5163f363ed37be94f870cfe791f670c2f93f 100644
--- a/MAC/Deployment/data/StaticMetaData/PPSDelays/RS306-PPSdelays.conf
+++ b/MAC/Deployment/data/StaticMetaData/PPSDelays/RS306-PPSdelays.conf
@@ -1,10 +1,10 @@
 #
-# APdelay.conf for RS306C
+# PPSDelays.conf for RS306C
 #
-# 01-Feb-2012-1010
+# 08-Feb-2012-1326
 #
 48 [ 
-17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 
-11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 
-23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 
-]
+18 18 18 18 18 18 18 18 19 19 19 19 19 19 19 19 
+14 14 14 14 13 13 13 13 14 14 14 14 13 13 13 13 
+15 15 15 15 15 15 15 15 17 17 17 17 14 14 14 14 
+]
\ No newline at end of file
diff --git a/RTCP/CNProc/src/CN_Processing.cc b/RTCP/CNProc/src/CN_Processing.cc
index 2b7e1383ef7676fec7215f6f4db63d31ae06b0d9..b0429673a4b8b1f998926d230a6176b0598ed7c5 100644
--- a/RTCP/CNProc/src/CN_Processing.cc
+++ b/RTCP/CNProc/src/CN_Processing.cc
@@ -241,9 +241,11 @@ template <typename SAMPLE_TYPE> CN_Processing<SAMPLE_TYPE>::CN_Processing(const
       if (LOG_CONDITION)
         LOG_DEBUG_STR("Considering dedispersion for " << itsTotalNrPencilBeams << " pencil beams");
 
-      itsDMs.resize(itsTotalNrPencilBeams, 0.0);
+      itsCoherentDMs.resize(itsTotalNrPencilBeams, 0.0);
+      itsIncoherentDMs.resize(itsTotalNrPencilBeams, 0.0);
 
-      bool anyNonzeroDM = false;
+      bool dedisperseCoherent = false;
+      bool dedisperseIncoherent = false;
       unsigned i = 0;
       unsigned nrSAPs = parset.nrBeams();
 
@@ -252,18 +254,32 @@ template <typename SAMPLE_TYPE> CN_Processing<SAMPLE_TYPE>::CN_Processing(const
           double DM = parset.dispersionMeasure(sap, pencil);
           if(LOG_CONDITION) LOG_DEBUG_STR("DM for beam " << sap << " pencil " << pencil << " is " << DM);
 
-          if (DM != 0.0)
-            anyNonzeroDM = true;
+          if (DM != 0.0) {
+            if (parset.isCoherent(sap, pencil)) {
+              dedisperseCoherent = true;
+              itsCoherentDMs[i] = DM;
+            } else {
+              dedisperseIncoherent = true;
+              itsIncoherentDMs[i] = DM;
+            }
+          }
 
-          itsDMs[i++] = DM;
+          i++;
         }
       }
 
-      if (anyNonzeroDM) {
-        if(LOG_CONDITION) LOG_DEBUG("Doing dedispersion after beam forming");
-        itsDedispersionAfterBeamForming = new DedispersionAfterBeamForming(parset, itsBeamFormedData, itsCurrentSubband->list(), itsDMs);
+      if (dedisperseCoherent) {
+        if(LOG_CONDITION) LOG_DEBUG("Doing dedispersion for coherent data");
+        itsDedispersionAfterBeamForming = new DedispersionAfterBeamForming(parset, itsBeamFormedData, itsCurrentSubband->list(), itsCoherentDMs);
+      } else {
+        if(LOG_CONDITION) LOG_DEBUG("NOT doing dedispersion for coherent data");
+      }
+
+      if (dedisperseIncoherent) {
+        if(LOG_CONDITION) LOG_DEBUG("Doing dedispersion for incoherent data");
+        itsDedispersionBeforeBeamForming = new DedispersionBeforeBeamForming(parset, itsFilteredData, itsCurrentSubband->list(), itsIncoherentDMs);
       } else {
-        if(LOG_CONDITION) LOG_DEBUG("NOT doing dedispersion after beam forming, because all DMs are 0");
+        if(LOG_CONDITION) LOG_DEBUG("NOT doing dedispersion for incoherent data");
       }
 
       // Our assembly code (BeamFormerAsm) requires groups of beams it processes to
@@ -292,8 +308,10 @@ template <typename SAMPLE_TYPE> CN_Processing<SAMPLE_TYPE>::CN_Processing(const
       itsPreTransposeBeamFormedData.resize(itsMaxNrPencilBeams);
   }
 
-  if (itsHasPhaseTwo || itsHasPhaseThree)
-    itsStokes = new Stokes(itsNrChannels, itsNrSamplesPerIntegration);
+  if (itsHasPhaseTwo || itsHasPhaseThree) {
+    itsCoherentStokes   = new CoherentStokes(itsNrChannels, itsNrSamplesPerIntegration);
+    itsIncoherentStokes = new IncoherentStokes(itsNrChannels, itsNrSamplesPerIntegration, nrMergedStations, parset.nrChannelsPerSubband() / parset.incoherentStokesChannelsPerSubband(), itsDedispersionBeforeBeamForming, itsBigAllocator);
+  }
 
 #if defined HAVE_MPI
   if (itsHasPhaseOne || itsHasPhaseTwo)
@@ -565,19 +583,19 @@ template <typename SAMPLE_TYPE> int CN_Processing<SAMPLE_TYPE>::transposeBeams(u
 
         if (info.coherent) {
           if (itsDedispersionAfterBeamForming != 0)
-            dedisperseAfterBeamForming(i, itsDMs[beam]);
+            dedisperseAfterBeamForming(i, itsCoherentDMs[beam]);
 
           switch (info.stokesType) {
             case STOKES_I:
               if(LOG_CONDITION)
                 LOG_DEBUG_STR(itsLogPrefix << "Calculating coherent Stokes I");
-              itsStokes->calculateCoherent<false>(itsBeamFormedData.get(), itsPreTransposeBeamFormedData[beam].get(), i, info);
+              itsCoherentStokes->calculate<false>(itsBeamFormedData.get(), itsPreTransposeBeamFormedData[beam].get(), i, info);
               break;
 
             case STOKES_IQUV:
               if(LOG_CONDITION)
                 LOG_DEBUG_STR(itsLogPrefix << "Calculating coherent Stokes IQUV");
-              itsStokes->calculateCoherent<true>(itsBeamFormedData.get(), itsPreTransposeBeamFormedData[beam].get(), i, info);
+              itsCoherentStokes->calculate<true>(itsBeamFormedData.get(), itsPreTransposeBeamFormedData[beam].get(), i, info);
               break;
 
             case STOKES_XXYY:
@@ -591,17 +609,19 @@ template <typename SAMPLE_TYPE> int CN_Processing<SAMPLE_TYPE>::transposeBeams(u
               break;
           }
         } else {  
+          // TODO: optimise dedispersion to only do the forwardFFT once
+
           switch (info.stokesType) {
             case STOKES_I:
               if(LOG_CONDITION)
                 LOG_DEBUG_STR(itsLogPrefix << "Calculating incoherent Stokes I");
-              itsStokes->calculateIncoherent<false>(itsFilteredData.get(), itsPreTransposeBeamFormedData[beam].get(), itsBeamFormer->getStationMapping(), info);
+              itsIncoherentStokes->calculate<false>(itsFilteredData.get(), itsPreTransposeBeamFormedData[beam].get(), itsBeamFormer->getStationMapping(), info, subband, itsIncoherentDMs[beam]);
               break;
 
             case STOKES_IQUV:
               if(LOG_CONDITION)
                 LOG_DEBUG_STR(itsLogPrefix << "Calculating incoherent Stokes IQUV");
-              itsStokes->calculateIncoherent<true>(itsFilteredData.get(), itsPreTransposeBeamFormedData[beam].get(), itsBeamFormer->getStationMapping(), info);
+              itsIncoherentStokes->calculate<true>(itsFilteredData.get(), itsPreTransposeBeamFormedData[beam].get(), itsBeamFormer->getStationMapping(), info, subband, itsIncoherentDMs[beam]);
               break;
 
             case STOKES_XXYY:
@@ -680,14 +700,14 @@ template <typename SAMPLE_TYPE> void CN_Processing<SAMPLE_TYPE>::dedisperseAfter
 {
 #if defined HAVE_MPI
   if (LOG_CONDITION)
-    LOG_DEBUG_STR(itsLogPrefix << "Start dedispersion at " << MPI_Wtime());
+    LOG_DEBUG_STR(itsLogPrefix << "Start dedispersion of coherent data at " << MPI_Wtime());
 #endif
 
-  static NSTimer timer("dedispersion (after BF) timer", true, true);
+  static NSTimer timer("dedispersion (coherent) timer", true, true);
 
   computeTimer.start();
   timer.start();
-  itsDedispersionAfterBeamForming->dedisperse(itsBeamFormedData, *itsCurrentSubband, beam, dm);
+  itsDedispersionAfterBeamForming->dedisperse(itsBeamFormedData.get(), *itsCurrentSubband, beam, dm);
   timer.stop();
   computeTimer.stop();
 }
diff --git a/RTCP/CNProc/src/CN_Processing.h b/RTCP/CNProc/src/CN_Processing.h
index bd94585a547f67e1fa8be39bf973d2fd895345ea..f7a0e6ad16b2c824c866562c9ddeb9908199cd7e 100644
--- a/RTCP/CNProc/src/CN_Processing.h
+++ b/RTCP/CNProc/src/CN_Processing.h
@@ -134,7 +134,8 @@ template <typename SAMPLE_TYPE> class CN_Processing : public CN_Processing_Base
     const CN_Transpose2 &itsTranspose2Logic;
     std::vector<double> itsCenterFrequencies;
     SmartPtr<Ring>	itsFirstInputSubband, itsCurrentSubband;
-    std::vector<double> itsDMs;
+    std::vector<double> itsCoherentDMs;
+    std::vector<double> itsIncoherentDMs;
     bool		itsFakeInputData;
     bool		itsHasPhaseOne, itsHasPhaseTwo, itsHasPhaseThree;
 
@@ -169,9 +170,11 @@ template <typename SAMPLE_TYPE> class CN_Processing : public CN_Processing_Base
 
     SmartPtr<PPF<SAMPLE_TYPE> >			itsPPF;
     SmartPtr<BeamFormer>			itsBeamFormer;
-    SmartPtr<Stokes>				itsStokes;
+    SmartPtr<CoherentStokes>			itsCoherentStokes;
+    SmartPtr<IncoherentStokes>			itsIncoherentStokes;
     SmartPtr<Correlator>			itsCorrelator;
     SmartPtr<DedispersionAfterBeamForming>	itsDedispersionAfterBeamForming;
+    SmartPtr<DedispersionBeforeBeamForming>	itsDedispersionBeforeBeamForming;
     SmartPtr<PreCorrelationFlagger>		itsPreCorrelationFlagger;
     SmartPtr<PostCorrelationFlagger>		itsPostCorrelationFlagger;
     SmartPtr<Trigger>				itsTrigger;
diff --git a/RTCP/CNProc/src/Dedispersion.cc b/RTCP/CNProc/src/Dedispersion.cc
index 17b4727f2e320f5673f3ae001a5f4ffde4599cae..d89bfc43262b1b1acc61e43cd2a1740c35d4ac16 100644
--- a/RTCP/CNProc/src/Dedispersion.cc
+++ b/RTCP/CNProc/src/Dedispersion.cc
@@ -115,7 +115,7 @@ void Dedispersion::initFFT(fcomplex *data)
 }
 
 
-void Dedispersion::forwardFFT(fcomplex *data)
+void Dedispersion::forwardFFT(const fcomplex *data)
 {
 #if defined HAVE_FFTW3
   fftwf_execute_dft(itsFFTWforwardPlan, (fftwf_complex *) data, (fftwf_complex *) &itsFFTedBuffer[0][0]);
@@ -195,20 +195,18 @@ void Dedispersion::applyChirp(unsigned subbandIndex, unsigned dmIndex, unsigned
 }
 
 
-void DedispersionBeforeBeamForming::dedisperse(FilteredData *filteredData, unsigned subbandIndex, double dm)
+void DedispersionBeforeBeamForming::dedisperse(const FilteredData *inData, FilteredData *outData, unsigned instat, unsigned outstat, unsigned firstch, unsigned numch, unsigned subbandIndex, double dm)
 {
   if (dm == 0.0)
     return;
 
   unsigned dmIndex = itsDMindices[dm];
 
-  for (unsigned channel = 0; channel < itsNrChannels; channel ++) {
-    for (unsigned station = 0; station < itsNrStations; station ++) {
-      for (unsigned block = 0; block < itsNrSamplesPerIntegration; block += itsFFTsize) {
-	forwardFFT(&filteredData->samples[channel][station][block][0]);
-	applyChirp(subbandIndex, dmIndex, channel);
-	backwardFFT(&filteredData->samples[channel][station][block][0]);
-      }
+  for (unsigned channel = 0; channel < numch; channel ++) {
+    for (unsigned block = 0; block < itsNrSamplesPerIntegration; block += itsFFTsize) {
+      forwardFFT(&inData->samples[firstch + channel][instat][block][0]);
+      applyChirp(subbandIndex, dmIndex, channel);
+      backwardFFT(&outData->samples[channel][outstat][block][0]);
     }
   }
 }
diff --git a/RTCP/CNProc/src/Dedispersion.h b/RTCP/CNProc/src/Dedispersion.h
index 63fa76faf3a6315f61afe88505467597f84a4848..979a705faad2ceb8a892ce6d8579f63bf99820fe 100644
--- a/RTCP/CNProc/src/Dedispersion.h
+++ b/RTCP/CNProc/src/Dedispersion.h
@@ -37,7 +37,7 @@ class Dedispersion
 
   protected:
     void initFFT(fcomplex *data);
-    void forwardFFT(fcomplex *data);
+    void forwardFFT(const fcomplex *data);
     void backwardFFT(fcomplex *data);
 
     const unsigned itsNrChannels, itsNrSamplesPerIntegration, itsFFTsize;
@@ -66,7 +66,7 @@ class DedispersionBeforeBeamForming : public Dedispersion
   public:
     DedispersionBeforeBeamForming(const Parset &, FilteredData *, const std::vector<unsigned> &subbandIndices, std::vector<double> &DMs, Allocator &allocator = heapAllocator);
 
-    void dedisperse(FilteredData *, unsigned subbandIndex, double dm);
+    void dedisperse(const FilteredData *, FilteredData *, unsigned instat, unsigned outstat, unsigned firstch, unsigned numch, unsigned subbandIndex, double dm);
 
   private:
     const unsigned itsNrStations;
diff --git a/RTCP/CNProc/src/Stokes.cc b/RTCP/CNProc/src/Stokes.cc
index 86e7f1503af0ca7cc9f104c01b092a04448aa87b..76729bc58eb2770d782c857bbe150cbb6ebc6562 100644
--- a/RTCP/CNProc/src/Stokes.cc
+++ b/RTCP/CNProc/src/Stokes.cc
@@ -52,10 +52,25 @@ Stokes::Stokes(unsigned nrChannels, unsigned nrSamples)
   itsNrChannels(nrChannels),
   itsNrSamples(nrSamples)
 {
-} 
+}
+CoherentStokes::CoherentStokes(unsigned nrChannels, unsigned nrSamples)
+:
+  Stokes(nrChannels, nrSamples)
+{
+}
+
+IncoherentStokes::IncoherentStokes(unsigned nrChannels, unsigned nrSamples, unsigned nrStations, unsigned maxChannelIntegrations, DedispersionBeforeBeamForming *dedispersion, Allocator &allocator)
+:
+  Stokes(nrChannels, nrSamples),
+  itsAllocator(allocator),
+  itsDedispersedData(dedispersion ? new FilteredData(nrStations, maxChannelIntegrations, itsNrSamples, allocator) : 0),
+  itsDedispersion(dedispersion),
+  itsMaxChannelIntegrations(maxChannelIntegrations)
+{
+}
 
 // Calculate coherent stokes values from pencil beams.
-template <bool ALLSTOKES> void Stokes::calculateCoherent(const BeamFormedData *sampleData, PreTransposeBeamFormedData *stokesData, unsigned inbeam, const StreamInfo &info)
+template <bool ALLSTOKES> void CoherentStokes::calculate(const SampleData<> *sampleData, PreTransposeBeamFormedData *stokesData, unsigned inbeam, const StreamInfo &info)
 {
   // TODO: divide by #valid stations
   ASSERT(sampleData->samples.shape()[0] > inbeam);
@@ -75,7 +90,6 @@ template <bool ALLSTOKES> void Stokes::calculateCoherent(const BeamFormedData *s
   // process flags
   const std::vector<SparseSet<unsigned> > &inflags = sampleData->flags;
   std::vector<SparseSet<unsigned> > &outflags = stokesData->flags;
-
   outflags[0] = inflags[inbeam];
   outflags[0] /= timeIntegrations;
 
@@ -164,8 +178,8 @@ template <bool ALLSTOKES> void Stokes::calculateCoherent(const BeamFormedData *s
   }  
 }
 
-template void Stokes::calculateCoherent<true>(const BeamFormedData *, PreTransposeBeamFormedData *, unsigned, const StreamInfo&);
-template void Stokes::calculateCoherent<false>(const BeamFormedData *, PreTransposeBeamFormedData *, unsigned, const StreamInfo&);
+template void CoherentStokes::calculate<true>(const SampleData<> *, PreTransposeBeamFormedData *, unsigned, const StreamInfo&);
+template void CoherentStokes::calculate<false>(const SampleData<> *, PreTransposeBeamFormedData *, unsigned, const StreamInfo&);
 
 template <bool ALLSTOKES> struct stokes {
   // the sums of stokes values over a number of stations or beams
@@ -215,42 +229,39 @@ template <bool ALLSTOKES> static inline void addStokes(struct stokes<ALLSTOKES>
 }
 
 // Calculate incoherent stokes values from (filtered) station data.
-template <bool ALLSTOKES> void Stokes::calculateIncoherent(const FilteredData *sampleData, PreTransposeBeamFormedData *stokesData, const std::vector<unsigned> &stationMapping, const StreamInfo &info)
+template <bool ALLSTOKES> void IncoherentStokes::calculate(const FilteredData *in, PreTransposeBeamFormedData *out, const std::vector<unsigned> &stationMapping, const StreamInfo &info, unsigned subband, double dm)
 {
   const unsigned nrStations = stationMapping.size();
 
-  ASSERT(sampleData->samples.shape()[0] == itsNrChannels);
-  // sampleData->samples.shape()[1] has to be bigger than all elements in stationMapping
-  ASSERT(sampleData->samples.shape()[2] >= itsNrSamples);
-  ASSERT(sampleData->samples.shape()[3] == NR_POLARIZATIONS);
+  ASSERT(in->samples.shape()[0] == itsNrChannels);
+  // in->samples.shape()[1] has to be bigger than all elements in stationMapping
+  ASSERT(in->samples.shape()[2] >= itsNrSamples);
+  ASSERT(in->samples.shape()[3] == NR_POLARIZATIONS);
 
   const unsigned &timeIntegrations = info.timeIntFactor;
-  const unsigned upperBound = static_cast<unsigned>(itsNrSamples * Stokes::MAX_FLAGGED_PERCENTAGE);
   const unsigned channelIntegrations = itsNrChannels / info.nrChannels;
-  bool validStation[nrStations];
-  unsigned nrValidStations = 0;
-  const MultiDimArray<fcomplex, 4> &in = sampleData->samples;
-  const std::vector< SparseSet<unsigned> > &inflags = sampleData->flags;
-  PreTransposeBeamFormedData *out = stokesData;
   std::vector<unsigned> stationList;
 
+  ASSERT(channelIntegrations <= itsMaxChannelIntegrations);
+
   out->flags[0].reset();
 
   for (unsigned stat = 0; stat < nrStations; stat ++) {
+    const unsigned upperBound = static_cast<unsigned>(itsNrSamples * Stokes::MAX_FLAGGED_PERCENTAGE);
     const unsigned srcStat = stationMapping[stat];
 
-    if(inflags[srcStat].count() > upperBound) {
+    if(in->flags[srcStat].count() > upperBound) {
       // drop station due to too much flagging
-      validStation[stat] = false;
     } else {
-      validStation[stat] = true;
-      nrValidStations ++;
+      stationList.push_back(srcStat);  
 
       // conservative flagging: flag anything that is flagged in one of the stations
-      out->flags[0] |= inflags[srcStat];
+      out->flags[0] |= in->flags[srcStat];
     }
   }
 
+  const unsigned nrValidStations = stationList.size();
+
   if (nrValidStations == 0) {
     /* if no valid samples, insert zeroes */
 
@@ -264,28 +275,36 @@ template <bool ALLSTOKES> void Stokes::calculateIncoherent(const FilteredData *s
     return;
   }
 
-  // enumerate all valid stations
-  for (unsigned stat = 0; stat < nrStations; stat ++) {
-    unsigned srcStat = stationMapping[stat];
-
-    if (!validStation[stat])
-      continue;
-
-    stationList.push_back(srcStat);  
-  }
-
   // shorten the flags over the integration length
   out->flags[0] /= timeIntegrations;
 
+  const bool dedisperse = dm != 0.0 && itsDedispersion;
+  
   for (unsigned inch = 0, outch = 0; inch < itsNrChannels; inch += channelIntegrations, outch++) {
+
+    if (dedisperse) {
+      // dedisperse channelIntegration channels for all stations
+      for (unsigned outstat = 0; outstat < stationList.size(); outstat ++) {
+        unsigned instat = stationList[outstat];
+
+        itsDedispersion->dedisperse( in, itsDedispersedData.get(), instat, outstat, inch, channelIntegrations, subband, dm );
+      }
+    }
+
     for (unsigned inTime = 0, outTime = 0; inTime < itsNrSamples; inTime += timeIntegrations, outTime ++) {
       struct stokes<ALLSTOKES> stokes;
 
-      for (unsigned i = 0; i < stationList.size(); i ++) {
-        unsigned stat = stationList[i];
+      if (dedisperse) {
+         for (unsigned i = 0; i < nrValidStations; i ++)
+          for (unsigned c = 0; c < channelIntegrations; c++)
+            addStokes<ALLSTOKES>(stokes, reinterpret_cast<const fcomplex (*)[2]>(&itsDedispersedData->samples[c][i][inTime][0]), timeIntegrations);
+      } else {
+         for (unsigned i = 0; i < nrValidStations; i ++) {
+          unsigned stat = stationList[i];
 
-        for (unsigned c = 0; c < channelIntegrations; c++)
-          addStokes<ALLSTOKES>(stokes, reinterpret_cast<const fcomplex (*)[2]>(&in[inch + c][stat][inTime][0]), timeIntegrations);
+          for (unsigned c = 0; c < channelIntegrations; c++)
+            addStokes<ALLSTOKES>(stokes, reinterpret_cast<const fcomplex (*)[2]>(&in->samples[inch + c][stat][inTime][0]), timeIntegrations);
+        }  
       }  
 
       #define dest(stokes) out->samples[stokes][outch][outTime]
@@ -301,8 +320,8 @@ template <bool ALLSTOKES> void Stokes::calculateIncoherent(const FilteredData *s
   }
 }
 
-template void Stokes::calculateIncoherent<true>(const FilteredData *, PreTransposeBeamFormedData *, const std::vector<unsigned> &, const StreamInfo&);
-template void Stokes::calculateIncoherent<false>(const FilteredData *, PreTransposeBeamFormedData *, const std::vector<unsigned> &, const StreamInfo&);
+template void IncoherentStokes::calculate<true>(const FilteredData *, PreTransposeBeamFormedData *, const std::vector<unsigned> &, const StreamInfo&, unsigned, double);
+template void IncoherentStokes::calculate<false>(const FilteredData *, PreTransposeBeamFormedData *, const std::vector<unsigned> &, const StreamInfo&, unsigned, double);
 
 } // namespace RTCP
 } // namespace LOFAR
diff --git a/RTCP/CNProc/src/Stokes.h b/RTCP/CNProc/src/Stokes.h
index 442dbdac76c0d5b2e94ec302ada630438163b60a..b2102edce224e4dda1acbda06c0b651f80c6b0dd 100644
--- a/RTCP/CNProc/src/Stokes.h
+++ b/RTCP/CNProc/src/Stokes.h
@@ -6,6 +6,7 @@
 #include <Interface/BeamFormedData.h>
 #include <Interface/MultiDimArray.h>
 #include <Interface/Parset.h>
+#include <Dedispersion.h>
 
 #if 0 || !defined HAVE_BGP
 #define STOKES_C_IMPLEMENTATION
@@ -22,14 +23,33 @@ class Stokes
 
     Stokes(unsigned nrChannels, unsigned nrSamples);
 
-    template <bool ALLSTOKES> void calculateCoherent(const BeamFormedData *sampleData, PreTransposeBeamFormedData *stokesData, unsigned inbeam, const StreamInfo &info);
-    template <bool ALLSTOKES> void calculateIncoherent(const FilteredData *sampleData, PreTransposeBeamFormedData *stokesData, const std::vector<unsigned> &stationMapping, const StreamInfo &info);
-
-  private:
+  protected:
     const unsigned          itsNrChannels;
     const unsigned          itsNrSamples;
 };
 
+class CoherentStokes: public Stokes
+{
+  public:
+    CoherentStokes(unsigned nrChannels, unsigned nrSamples);
+
+    template <bool ALLSTOKES> void calculate(const SampleData<> *sampleData, PreTransposeBeamFormedData *stokesData, unsigned inbeam, const StreamInfo &info);
+};
+
+class IncoherentStokes: public Stokes
+{
+  public:
+    IncoherentStokes(unsigned nrChannels, unsigned nrSamples, unsigned nrStations, unsigned channelIntegrations, DedispersionBeforeBeamForming *dedispersion, Allocator &allocator);
+
+    template <bool ALLSTOKES> void calculate(const FilteredData *sampleData, PreTransposeBeamFormedData *stokesData, const std::vector<unsigned> &stationMapping, const StreamInfo &info, unsigned subband, double dm);
+
+  private:  
+    Allocator                     &itsAllocator;
+    SmartPtr<FilteredData>        itsDedispersedData;
+    DedispersionBeforeBeamForming *itsDedispersion;
+    const unsigned                itsMaxChannelIntegrations;
+};
+
 } // namespace RTCP
 } // namespace LOFAR
 
diff --git a/RTCP/CNProc/test/tStokes.cc b/RTCP/CNProc/test/tStokes.cc
index 2dd7cfca64d136dd86abd696912f8c6e4d41c844..9042f4abedd7458d56eb1e9bc5e4aafa5980784c 100644
--- a/RTCP/CNProc/test/tStokes.cc
+++ b/RTCP/CNProc/test/tStokes.cc
@@ -67,7 +67,7 @@ void test_incoherent_stokes( unsigned NRSTOKES, unsigned INTEGRATION ) {
   }
 
   // calculate
-  Stokes s( NRCHANNELS, NRSAMPLES );
+  IncoherentStokes s( NRCHANNELS, NRSAMPLES, NRSTATIONS, 1, 0, heapAllocator );
 
   struct StreamInfo info;
 
@@ -84,9 +84,9 @@ void test_incoherent_stokes( unsigned NRSTOKES, unsigned INTEGRATION ) {
   info.part = 0;
 
   if (NRSTOKES == 4) {
-    s.calculateIncoherent<true>( &in, &out, stationMapping, info );
+    s.calculate<true>( &in, &out, stationMapping, info, 0, 0.0 );
   } else {
-    s.calculateIncoherent<false>( &in, &out, stationMapping, info );
+    s.calculate<false>( &in, &out, stationMapping, info, 0, 0.0 );
   }
 
   // check
@@ -161,7 +161,7 @@ void test_coherent_stokes( unsigned NRSTOKES, unsigned INTEGRATION, unsigned CHA
     }
   }  
 
-  Stokes s( NRCHANNELS, NRSAMPLES );
+  CoherentStokes s( NRCHANNELS, NRSAMPLES );
 
   for( unsigned b = 0; b < NRPENCILBEAMS; b++ ) {
     struct StreamInfo info;
@@ -191,9 +191,9 @@ void test_coherent_stokes( unsigned NRSTOKES, unsigned INTEGRATION, unsigned CHA
 
     // calculate using Stokes.cc
     if (NRSTOKES == 4) {
-      s.calculateCoherent<true>( &in, &out, b, info );
+      s.calculate<true>( &in, &out, b, info );
     } else {
-      s.calculateCoherent<false>( &in, &out, b, info );
+      s.calculate<false>( &in, &out, b, info );
     }
 
     // calculate our own
diff --git a/RTCP/Run/src/LOFAR/Parset.py b/RTCP/Run/src/LOFAR/Parset.py
index 255a7117127037bbe97e16358084c5dd9bfb6358..c2504eac6c9906840543b383efd1b5d725b7c580 100644
--- a/RTCP/Run/src/LOFAR/Parset.py
+++ b/RTCP/Run/src/LOFAR/Parset.py
@@ -38,6 +38,31 @@ class Parset(util.Parset.Parset):
 
         self.filename = ""
 
+    def applyAntennaSet( self, station, antennaset = None ):
+      if antennaset is None:
+        antennaset = self["Observation.antennaSet"]
+
+      if antennaset == "":
+        # useful for manually entered complete station names like CS302HBA1
+        suffix = [""]
+      elif antennaset in ["LBA_INNER","LBA_OUTER","LBA_X","LBA_Y","LBA_SPARSE_EVEN","LBA_SPARSE_ODD"]:
+        suffix = ["LBA"]
+      elif station.startswith("CS"):
+        if antennaset == "HBA_ZERO":
+          suffix = ["HBA0"]
+        elif antennaset == "HBA_ONE":  
+          suffix = ["HBA1"]
+        elif antennaset == "HBA_JOINED":  
+          suffix = ["HBA"]
+        else: 
+          assert antennaset == "HBA_DUAL", "Unknown antennaSet: %s" % (antennaset,)
+          suffix = ["HBA0","HBA1"]
+      else:  
+        suffix = ["HBA"]
+
+      return "+".join(["%s%s" % (station,s) for s in suffix])
+
+
     def setFilename( self, filename ):
         self.filename = filename
 
@@ -56,28 +81,7 @@ class Parset(util.Parset.Parset):
         # translate station name + antenna set to CEP comprehensable names
         antennaset = self["Observation.antennaSet"]
 
-        def applyAntennaSet( station, antennaset ):
-          if antennaset == "":
-            # useful for manually entered complete station names like CS302HBA1
-            suffix = [""]
-          elif antennaset in ["LBA_INNER","LBA_OUTER","LBA_X","LBA_Y","LBA_SPARSE_EVEN","LBA_SPARSE_ODD"]:
-            suffix = ["LBA"]
-          elif station.startswith("CS"):
-            if antennaset == "HBA_ZERO":
-              suffix = ["HBA0"]
-            elif antennaset == "HBA_ONE":  
-              suffix = ["HBA1"]
-            elif antennaset == "HBA_JOINED":  
-              suffix = ["HBA"]
-            else: 
-              assert antennaset == "HBA_DUAL", "Unknown antennaSet: %s" % (antennaset,)
-              suffix = ["HBA0","HBA1"]
-          else:  
-            suffix = ["HBA"]
-
-          return "+".join(["%s%s" % (station,s) for s in suffix])
-
-        return "+".join( [applyAntennaSet(s, self["Observation.antennaSet"]) for s in self[key]] )
+        return "+".join( [self.applyAntennaSet(s) for s in self[key]] )
 
     def distillPartition(self, key="OLAP.CNProc.partition"):
         """ Distill partition to use from the parset file and return it. """
@@ -250,39 +254,29 @@ class Parset(util.Parset.Parset):
         # SAS specifies beams differently
         if "Observation.subbandList" not in self:
           # convert beam configuration
-          allSubbands = {}
+          allSubbands = []
 
 	  for b in count():
             if "Observation.Beam[%s].angle1" % (b,) not in self:
               break
 
 	    beamSubbands = self.getInt32Vector("Observation.Beam[%s].subbandList" % (b,)) # the actual subband number (0..511)
-            beamBeamlets = self.getInt32Vector("Observation.Beam[%s].beamletList" % (b,)) # the bebamlet index (0..247)
-
-            assert len(beamSubbands) == len(beamBeamlets), "Beam %d has %d subbands but %d beamlets defined" % (b,len(beamSubbands),len(beamBeamlets))
-
-            for subband,beamlet in zip(beamSubbands,beamBeamlets):
-              assert beamlet not in allSubbands, "Beam %d and %d both use beamlet %d" % (allSubbands[beamlet]["beam"],b,beamlet)
 
-              allSubbands[beamlet] = {
+            for subband in beamSubbands:
+              allSubbands.append( {
                 "beam":     b,
                 "subband":  subband,
-                "beamlet":  beamlet,
-
-                # assign rsp board and slot according to beamlet id
-                "rspboard": beamlet // NRBOARBEAMLETS,
-                "rspslot":  beamlet % NRBOARBEAMLETS,
-              }
+              } )
 
 
           # order subbands according to beamlet id, for more human-friendly reading
-          sortedSubbands = [allSubbands[x] for x in sorted( allSubbands )]
+          sortedSubbands = sorted( allSubbands )
 
           # populate OLAP lists
 	  self["Observation.subbandList"]  = [s["subband"] for s in sortedSubbands]
 	  self["Observation.beamList"]     = [s["beam"] for s in sortedSubbands]
-	  self["Observation.rspBoardList"] = [s["rspboard"] for s in sortedSubbands]
-	  self["Observation.rspSlotList"]  = [s["rspslot"] for s in sortedSubbands]
+	  self["Observation.rspBoardList"] = [0 for s in sortedSubbands]
+	  self["Observation.rspSlotList"]  = [0 for s in sortedSubbands]
 
         # The Scheduler creates three lists of files (for beamformed, coherent and incoherent),
         # but we collapse this into one list (beamformed)
@@ -380,46 +374,48 @@ class Parset(util.Parset.Parset):
 	# sloppy to let it pass through here unnoticed.
 
 	# tied-array beam forming
-        tiedArrayStationList = []
-	tabList = []
-	beamFormedStations = []
+        superStations = []
 
         for index in count():
           if "Observation.Beamformer[%s].stationList" % (index,) not in self:
             break
 
-	  curlist = self.getString('Observation.Beamformer[%s].stationList' % (index,))
-
-          # remove any initial or trailing "
-	  curlist = curlist.strip('"').rstrip('"')
-
-          # transform , to +
-	  curlist = curlist.replace(',','+')
+	  stations = self.getStringVector('Observation.Beamformer[%s].stationList' % (index,))
 
-	  tiedArrayStationList.append(curlist)
+          stations = [self.applyAntennaSet(st) for st in stations]
 
-          # extract the individual stations
-	  beamFormedStations += curlist.split('+')
+          superStations.append(stations)
 	
-	if index > 0:
+	if superStations != []:
 	  # tied-array beamforming will occur
 
 	  # add remaining stations to the list
 	  allStationNames = [st.getName() for st in self.stations]
-	  tiedArrayStationList += filter( lambda st: st not in beamFormedStations, allStationNames )
+          beamFormedStations = sum(superStations, [])
+          individualStations = [st for st in allStationNames if st not in beamFormedStations]
 
-          # create a list of indices for all the stations, which by definition occur in
-	  # the tiedArrayStationList
-	  def findTabStation( s ):
-	    for nr,tab in enumerate(tiedArrayStationList):
-	      for st in tab.split('+'):
-	        if s.getName() == st:
-	          return nr
 
-	  tabList = map( findTabStation, self.stations )
+          allTabs = superStations + [[st] for st in individualStations]
+          # sorting is important: because the original station list is sorted, a sorted tabList makes sure that no slot is overwritten before it is needed (data is always generated before or at the slot of the source station)
+          allTabs.sort()
+
+	  def findTabStation( st ):
+	    for nr,tab in enumerate(allTabs):
+              if st in tab:
+                return nr
+
+	  tabList = map( findTabStation, allStationNames )
+
+          # make sure this tabList can be processed by going from element 0 to n-1 (dest slot is always at or after source slot)
+          for st,nr in enumerate(tabList):
+            assert st >= nr, "Station %s is at position %u in the station list but at position %u in the tab list, which could lead to data corruption" % (allStationNames[st],st,nr)
 	   
-	self.setdefault('OLAP.tiedArrayStationNames', tiedArrayStationList)
-	self.setdefault('OLAP.CNProc.tabList', tabList)
+	  self.setdefault('OLAP.tiedArrayStationNames', ["+".join(x) for x in allTabs])
+	  self.setdefault('OLAP.CNProc.tabList', tabList)
+        else:
+          # no super-station beam forming
+	  self.setdefault('OLAP.tiedArrayStationNames', [])
+	  self.setdefault('OLAP.CNProc.tabList', [])
 
 	# input flow configuration
 	for station in self.stations:
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/Main.java b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/Main.java
index b762238dbf78eee3cc35342128c8ab594d794012..6ac2428fa51b69552271284520bbd6091b724255 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/Main.java
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/Main.java
@@ -32,8 +32,7 @@ import java.awt.Rectangle;
 import java.io.File;
 import java.util.Iterator;
 import java.util.Map;
-import java.util.Timer;
-import java.util.TimerTask;
+import nl.astron.lofar.lofarutils.LofarUtils;
 import nl.astron.lofar.sas.otb.exceptions.NoServerConnectionException;
 import nl.astron.lofar.sas.otb.exceptions.NotLoggedInException;
 import org.apache.log4j.Logger;
@@ -58,29 +57,14 @@ public class Main {
             @Override
       public void run() {
         System.out.println("Shutting down OTB");
+        // uninstall tab selector
+        LofarUtils.TextSelector.uninstall();
         if (itsMainFrame != null)  itsMainFrame.exit();
       }
     });
 
   }
     
-//static {
-// 
-//    int delay = 1000 * 60; // delay for 1 minute
-//    int repeat = delay * 2; // repeat every 2 minutes
-// 
-//    Timer gcTimer = new Timer();
-//    gcTimer.scheduleAtFixedRate(new TimerTask() {
-// 
-//    @Override
-//    public void run() {
-////        System.out.println("Running Gargabe-Collector");
-//        System.gc();
-//    }
-//   
-//    }, delay, repeat);
-//  
-// }
 
 
     /**
@@ -139,7 +123,10 @@ public class Main {
             }
             if (errs) {
                 System.err.println("Usage: OTB.jar [-s server] [-p port] [-d database] [-u username] [-l logFile] [-h]");
-            }         
+            }   
+            // install tab focus
+            LofarUtils.TextSelector.install();
+
 
             File f = new File(logConfig);
             if (f.exists()) {
@@ -151,8 +138,11 @@ public class Main {
                     PropertyConfigurator.configure(logConfig);
                 } else {
                     logger.error("OTB.log_prop not found.");
-                }
+                    }
             }
+            // install tab focus
+            LofarUtils.TextSelector.install();
+
             logger.info("OTB started");
 
             try {
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/MainFrame.java b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/MainFrame.java
index dfeeb292440516a7233e0f8522b72e5429889610..f4d0bf82ebc2189eca109e70d5334ac092ece713 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/MainFrame.java
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/MainFrame.java
@@ -137,8 +137,6 @@ public final class MainFrame extends javax.swing.JFrame {
         
 
         
-        
-        
             showPanel(MainPanel.getFriendlyNameStatic());
         } catch(NoServerConnectionException ex ) {
             String aS="No Server Connection "+ex;
@@ -149,7 +147,6 @@ public final class MainFrame extends javax.swing.JFrame {
         } catch (NotLoggedInException ex ) {
             String aS="Not logged in "+ex;
             logger.error(aS);
-            LofarUtils.showErrorPanel(this.getOwner(),aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
             exit();
             throw ex;
         }
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/panels/MainPanel.form b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/panels/MainPanel.form
index 6af2f174f0301679286c76089221b757de1272c4..d46ba8550624ef751927142c2842bbcc6f0e61c7 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/panels/MainPanel.form
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/panels/MainPanel.form
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="UTF-8" ?>
+<?xml version="1.1" encoding="UTF-8" ?>
 
 <Form version="1.2" maxVersion="1.2" type="org.netbeans.modules.form.forminfo.JPanelFormInfo">
   <AuxValues>
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/panels/MainPanel.java b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/panels/MainPanel.java
index 993afe3e4089e8fbe020c5a3c944610d369092cb..f12e47ff71f35e05921d6b0f07655bd8ba57fa4d 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/panels/MainPanel.java
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/panels/MainPanel.java
@@ -28,9 +28,7 @@ import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.rmi.RemoteException;
-import java.util.Iterator;
-import java.util.Vector;
-import java.util.logging.Level;
+import java.util.ArrayList;
 import javax.swing.JLabel;
 import javax.swing.JOptionPane;
 import javax.swing.ListSelectionModel;
@@ -82,109 +80,110 @@ public class MainPanel extends javax.swing.JPanel
      */
     public void initializeButtons() {
         buttonPanel1.removeAllButtons();
-        if (itsTabFocus.equals("PIC")) {
-            buttonPanel1.addButton("State History");
-            buttonPanel1.setButtonIcon("State History",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_info.gif")));
-            buttonPanel1.addButton("Query Panel");
-            buttonPanel1.setButtonIcon("Query Panel",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_help.png")));
-            buttonPanel1.addButton("New");
-            buttonPanel1.setButtonIcon("New",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_new.png")));
-            buttonPanel1.addButton("Delete");
-            buttonPanel1.setButtonIcon("Delete",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_delete.png")));
-            buttonPanel1.addButton("Refresh");
-            buttonPanel1.setButtonIcon("Refresh",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_refresh_e.gif")));
-            buttonPanel1.addButton("View");
-            buttonPanel1.setButtonIcon("View",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif")));
-            buttonPanel1.addButton("Info");
-            buttonPanel1.setButtonIcon("Info",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_fileopen.gif")));
-
-            buttonPanel1.setButtonEnabled("Delete",false);
-            buttonPanel1.setButtonEnabled("View",false);
-            buttonPanel1.setButtonEnabled("Info",false);
-            buttonPanel1.setButtonEnabled("State History",false);
-        } else if (itsTabFocus.equals("VIC")) {
-            buttonPanel1.addButton("State History");
-            buttonPanel1.setButtonIcon("State History",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_info.gif")));
-            buttonPanel1.addButton("Query Panel");
-            buttonPanel1.setButtonIcon("Query Panel",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_help.png")));
-            buttonPanel1.addButton("Delete");
-            buttonPanel1.setButtonIcon("Delete",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_delete.png")));
-            buttonPanel1.addButton("Refresh");
-            buttonPanel1.setButtonIcon("Refresh",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_refresh_e.gif")));
-            buttonPanel1.addButton("View");
-            buttonPanel1.setButtonIcon("View",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif")));
-            buttonPanel1.addButton("Schedule");
-            buttonPanel1.setButtonIcon("Schedule",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_fileopen.gif")));
-
-            buttonPanel1.setButtonEnabled("State History",false);
-            buttonPanel1.setButtonEnabled("Delete",false);
-            buttonPanel1.setButtonEnabled("View",false);
-            buttonPanel1.setButtonEnabled("Schedule",false);
-        } else if (itsTabFocus.equals("Templates")) {
-            buttonPanel1.addButton("State History");
-            buttonPanel1.setButtonIcon("State History",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_info.gif")));
-            buttonPanel1.addButton("Query Panel");
-            buttonPanel1.setButtonIcon("Query Panel",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_help.png")));
-            buttonPanel1.addButton("Duplicate");
-            buttonPanel1.setButtonIcon("Duplicate",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_copy.png")));
-            buttonPanel1.addButton("Modify");
-            buttonPanel1.setButtonIcon("Modify",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif")));
-            buttonPanel1.addButton("Delete");            
-            buttonPanel1.setButtonIcon("Delete",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_delete.png")));
-            buttonPanel1.addButton("Refresh");
-            buttonPanel1.setButtonIcon("Refresh",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_refresh_e.gif")));
-            buttonPanel1.addButton("Build VIC tree");
-            buttonPanel1.setButtonIcon("Build VIC tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_new.png")));
-            buttonPanel1.addButton("Change Status");
-            buttonPanel1.setButtonIcon("Change Status",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_fileopen.gif")));
-            buttonPanel1.addButton("MultiEdit");
-            buttonPanel1.setButtonIcon("MultiEdit",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif" )));
-            buttonPanel1.addButton("Set to Default");
-            buttonPanel1.setButtonIcon("Set to Default",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_redo.png")));
-
-            buttonPanel1.setButtonEnabled("Duplicate",false);
-            buttonPanel1.setButtonEnabled("Modify",false);
-            buttonPanel1.setButtonEnabled("Delete",false);
-            buttonPanel1.setButtonEnabled("Build VIC tree",false);
-            buttonPanel1.setButtonEnabled("Change Status",false);
-            buttonPanel1.setButtonEnabled("MultiEdit",false);
-            buttonPanel1.setButtonEnabled("Set to Default",false);
-            buttonPanel1.setButtonEnabled("State History",false);
-        } else if (itsTabFocus.equals("Default Templates")) {
-            buttonPanel1.addButton("State History");
-            buttonPanel1.setButtonIcon("State History",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_info.gif")));
-            buttonPanel1.addButton("Duplicate");
-            buttonPanel1.setButtonIcon("Duplicate",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_copy.png")));
-            buttonPanel1.addButton("Modify");
-            buttonPanel1.setButtonIcon("Modify",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif")));
-            buttonPanel1.addButton("Refresh");
-            buttonPanel1.setButtonIcon("Refresh",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_refresh_e.gif")));
-            buttonPanel1.addButton("Change Status");
-            buttonPanel1.setButtonIcon("Change Status",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_fileopen.gif")));
-
-            buttonPanel1.setButtonEnabled("Modify",false);
-            buttonPanel1.setButtonEnabled("Duplicate",false);
-            buttonPanel1.setButtonEnabled("Change Status",false);
-            buttonPanel1.setButtonEnabled("State History",false);
-        } else if (itsTabFocus.equals("Components")) {
-            buttonPanel1.addButton("Query Panel");
-            buttonPanel1.setButtonIcon("Query Panel",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_help.png")));
-            buttonPanel1.addButton("New");
-            buttonPanel1.setButtonIcon("New",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_new.png")));
-            buttonPanel1.addButton("Modify");
-            buttonPanel1.setButtonIcon("Modify",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif")));
-            buttonPanel1.addButton("Delete");
-            buttonPanel1.setButtonIcon("Delete",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_delete.png")));
-            buttonPanel1.addButton("Refresh");
-            buttonPanel1.setButtonIcon("Refresh",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_refresh_e.gif")));
-            buttonPanel1.addButton("Build TemplateTree");
-            buttonPanel1.setButtonIcon("Build TemplateTree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_redo.png")));
-
-//            buttonPanel1.setButtonEnabled("Delete",false);
-            buttonPanel1.setButtonEnabled("Modify",false);
-            buttonPanel1.setButtonEnabled("Build TemplateTree",false);
-        } else if (itsTabFocus.equals("Query Results")) {
-        
+        switch (itsTabFocus) {
+            case "PIC":
+                buttonPanel1.addButton("State History");
+                buttonPanel1.setButtonIcon("State History",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_info.gif")));
+                buttonPanel1.addButton("Query Panel");
+                buttonPanel1.setButtonIcon("Query Panel",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_help.png")));
+                buttonPanel1.addButton("New");
+                buttonPanel1.setButtonIcon("New",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_new.png")));
+                buttonPanel1.addButton("Delete");
+                buttonPanel1.setButtonIcon("Delete",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_delete.png")));
+                buttonPanel1.addButton("Refresh");
+                buttonPanel1.setButtonIcon("Refresh",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_refresh_e.gif")));
+                buttonPanel1.addButton("View");
+                buttonPanel1.setButtonIcon("View",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif")));
+                buttonPanel1.addButton("Info");
+                buttonPanel1.setButtonIcon("Info",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_fileopen.gif")));
+                buttonPanel1.setButtonEnabled("Delete",false);
+                buttonPanel1.setButtonEnabled("View",false);
+                buttonPanel1.setButtonEnabled("Info",false);
+                buttonPanel1.setButtonEnabled("State History",false);
+                break;
+            case "VIC":
+                buttonPanel1.addButton("State History");
+                buttonPanel1.setButtonIcon("State History",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_info.gif")));
+                buttonPanel1.addButton("Query Panel");
+                buttonPanel1.setButtonIcon("Query Panel",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_help.png")));
+                buttonPanel1.addButton("Delete");
+                buttonPanel1.setButtonIcon("Delete",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_delete.png")));
+                buttonPanel1.addButton("Refresh");
+                buttonPanel1.setButtonIcon("Refresh",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_refresh_e.gif")));
+                buttonPanel1.addButton("View");
+                buttonPanel1.setButtonIcon("View",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif")));
+                buttonPanel1.addButton("Schedule");
+                buttonPanel1.setButtonIcon("Schedule",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_fileopen.gif")));
+                buttonPanel1.setButtonEnabled("State History",false);
+                buttonPanel1.setButtonEnabled("Delete",false);
+                buttonPanel1.setButtonEnabled("View",false);
+                buttonPanel1.setButtonEnabled("Schedule",false);
+                break;
+            case "Templates":
+                buttonPanel1.addButton("State History");
+                buttonPanel1.setButtonIcon("State History",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_info.gif")));
+                buttonPanel1.addButton("Query Panel");
+                buttonPanel1.setButtonIcon("Query Panel",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_help.png")));
+                buttonPanel1.addButton("Duplicate");
+                buttonPanel1.setButtonIcon("Duplicate",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_copy.png")));
+                buttonPanel1.addButton("Modify");
+                buttonPanel1.setButtonIcon("Modify",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif")));
+                buttonPanel1.addButton("Delete");
+                buttonPanel1.setButtonIcon("Delete",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_delete.png")));
+                buttonPanel1.addButton("Refresh");
+                buttonPanel1.setButtonIcon("Refresh",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_refresh_e.gif")));
+                buttonPanel1.addButton("Build VIC tree");
+                buttonPanel1.setButtonIcon("Build VIC tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_new.png")));
+                buttonPanel1.addButton("Change Status");
+                buttonPanel1.setButtonIcon("Change Status",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_fileopen.gif")));
+                buttonPanel1.addButton("MultiEdit");
+                buttonPanel1.setButtonIcon("MultiEdit",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif" )));
+                buttonPanel1.addButton("Set to Default");
+                buttonPanel1.setButtonIcon("Set to Default",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_redo.png")));
+                buttonPanel1.setButtonEnabled("Duplicate",false);
+                buttonPanel1.setButtonEnabled("Modify",false);
+                buttonPanel1.setButtonEnabled("Delete",false);
+                buttonPanel1.setButtonEnabled("Build VIC tree",false);
+                buttonPanel1.setButtonEnabled("Change Status",false);
+                buttonPanel1.setButtonEnabled("MultiEdit",false);
+                buttonPanel1.setButtonEnabled("Set to Default",false);
+                buttonPanel1.setButtonEnabled("State History",false);
+                break;
+            case "Default Templates":
+                buttonPanel1.addButton("State History");
+                buttonPanel1.setButtonIcon("State History",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_info.gif")));
+                buttonPanel1.addButton("Duplicate");
+                buttonPanel1.setButtonIcon("Duplicate",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_copy.png")));
+                buttonPanel1.addButton("Modify");
+                buttonPanel1.setButtonIcon("Modify",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif")));
+                buttonPanel1.addButton("Refresh");
+                buttonPanel1.setButtonIcon("Refresh",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_refresh_e.gif")));
+                buttonPanel1.addButton("Change Status");
+                buttonPanel1.setButtonIcon("Change Status",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_fileopen.gif")));
+                buttonPanel1.setButtonEnabled("Modify",false);
+                buttonPanel1.setButtonEnabled("Duplicate",false);
+                buttonPanel1.setButtonEnabled("Change Status",false);
+                buttonPanel1.setButtonEnabled("State History",false);
+                break;
+            case "Components":
+                buttonPanel1.addButton("Query Panel");
+                buttonPanel1.setButtonIcon("Query Panel",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_help.png")));
+                buttonPanel1.addButton("New");
+                buttonPanel1.setButtonIcon("New",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_new.png")));
+                buttonPanel1.addButton("Modify");
+                buttonPanel1.setButtonIcon("Modify",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_edit.gif")));
+                buttonPanel1.addButton("Delete");
+                buttonPanel1.setButtonIcon("Delete",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_delete.png")));
+                buttonPanel1.addButton("Refresh");
+                buttonPanel1.setButtonIcon("Refresh",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_refresh_e.gif")));
+                buttonPanel1.addButton("Build TemplateTree");
+                buttonPanel1.setButtonIcon("Build TemplateTree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_redo.png")));
+                //            buttonPanel1.setButtonEnabled("Delete",false);
+                buttonPanel1.setButtonEnabled("Modify",false);
+                buttonPanel1.setButtonEnabled("Build TemplateTree",false);
+                break;
+            case "Query Results":
+                break;
         }
         buttonPanel1.addButton("Quit");
         buttonPanel1.setButtonIcon("Quit",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_exit2.png")));
@@ -308,26 +307,32 @@ public class MainPanel extends javax.swing.JPanel
             // keep selected tree
             int aSavedID=itsMainFrame.getSharedVars().getTreeID();
             itsMainFrame.setHourglassCursor();
-            if (itsTabFocus.equals("PIC")) {
-                if (!((PICtableModel)PICPanel.getTableModel()).fillTable()) {
-                    logger.error("error filling PICtable");
-                }
-            } else if (itsTabFocus.equals("VIC")) {
-                if (!((VICtableModel)VICPanel.getTableModel()).fillTable()) {
-                    logger.error("error filling VICtable");
-                }            
-            } else if (itsTabFocus.equals("Templates")) {
-                if (!((TemplatetableModel)TemplatesPanel.getTableModel()).fillTable()) {
-                    logger.error("error filling templateTable");
-                }
-            } else if (itsTabFocus.equals("Default Templates")) {
-                if (!((DefaultTemplatetableModel)DefaultTemplatesPanel.getTableModel()).fillTable()) {
-                    logger.error("error filling Default templateTable");
-                }
-            } else if (itsTabFocus.equals("Components")) {
-                if (!((ComponentTableModel)ComponentsPanel.getTableModel()).fillTable()) {
-                    logger.error("error filling ComponentsTable");
-                }
+            switch (itsTabFocus) {
+                case "PIC":
+                    if (!((PICtableModel)PICPanel.getTableModel()).fillTable()) {
+                        logger.error("error filling PICtable");
+                    }
+                    break;
+                case "VIC":
+                    if (!((VICtableModel)VICPanel.getTableModel()).fillTable()) {
+                        logger.error("error filling VICtable");
+                    }
+                    break;
+                case "Templates":
+                    if (!((TemplatetableModel)TemplatesPanel.getTableModel()).fillTable()) {
+                        logger.error("error filling templateTable");
+                    }
+                    break;
+                case "Default Templates":
+                    if (!((DefaultTemplatetableModel)DefaultTemplatesPanel.getTableModel()).fillTable()) {
+                        logger.error("error filling Default templateTable");
+                    }
+                    break;
+                case "Components":
+                    if (!((ComponentTableModel)ComponentsPanel.getTableModel()).fillTable()) {
+                        logger.error("error filling ComponentsTable");
+                    }
+                    break;
             }
             if (aSavedID > 0) {
                 itsMainFrame.getSharedVars().setTreeID(aSavedID);
@@ -524,16 +529,22 @@ public class MainPanel extends javax.swing.JPanel
     /** Returns the selected row in the present tree */
     private int getSelectedRow() {
         int aRow=-1;
-        if (itsTabFocus.equals("PIC")) {
-            aRow = PICPanel.getSelectedRow();
-        } else if (itsTabFocus.equals("VIC")) {
-            aRow = VICPanel.getSelectedRow();
-        } else if (itsTabFocus.equals("Templates")) {
-            aRow = TemplatesPanel.getSelectedRow();
-        } else if (itsTabFocus.equals("Default Templates")) {
-            aRow = DefaultTemplatesPanel.getSelectedRow();
-        } else if (itsTabFocus.equals("Components")) {
-            aRow = ComponentsPanel.getSelectedRow();
+        switch (itsTabFocus) {
+            case "PIC":
+                aRow = PICPanel.getSelectedRow();
+                break;
+            case "VIC":
+                aRow = VICPanel.getSelectedRow();
+                break;
+            case "Templates":
+                aRow = TemplatesPanel.getSelectedRow();
+                break;
+            case "Default Templates":
+                aRow = DefaultTemplatesPanel.getSelectedRow();
+                break;
+            case "Components":
+                aRow = ComponentsPanel.getSelectedRow();
+                break;
         }
         return aRow;
     }
@@ -541,16 +552,22 @@ public class MainPanel extends javax.swing.JPanel
     /** Returns the selected rows in the present tree */
     private int[] getSelectedRows() {
         int [] rows=null;
-        if (itsTabFocus.equals("PIC")) {
-            rows = PICPanel.getSelectedRows();
-        } else if (itsTabFocus.equals("VIC")) {
-            rows = VICPanel.getSelectedRows();
-        } else if (itsTabFocus.equals("Templates")) {
-            rows = TemplatesPanel.getSelectedRows();
-        } else if (itsTabFocus.equals("Default Templates")) {
-            rows = DefaultTemplatesPanel.getSelectedRows();
-        } else if (itsTabFocus.equals("Components")) {
-            rows = ComponentsPanel.getSelectedRows();
+        switch (itsTabFocus) {
+            case "PIC":
+                rows = PICPanel.getSelectedRows();
+                break;
+            case "VIC":
+                rows = VICPanel.getSelectedRows();
+                break;
+            case "Templates":
+                rows = TemplatesPanel.getSelectedRows();
+                break;
+            case "Default Templates":
+                rows = DefaultTemplatesPanel.getSelectedRows();
+                break;
+            case "Components":
+                rows = ComponentsPanel.getSelectedRows();
+                break;
         }
         return rows;
     }
@@ -558,16 +575,22 @@ public class MainPanel extends javax.swing.JPanel
     /** Sets the selected row in the present tree */
     private void setSelectedID(int aTreeID) {
         if (aTreeID > -1) {
-            if (itsTabFocus.equals("PIC")) {
-                PICPanel.setSelectedID(aTreeID);
-            } else if (itsTabFocus.equals("VIC")) {
-                VICPanel.setSelectedID(aTreeID);
-            } else if (itsTabFocus.equals("Templates")) {
-                TemplatesPanel.setSelectedID(aTreeID);
-            } else if (itsTabFocus.equals("Default Templates")) {
-                DefaultTemplatesPanel.setSelectedID(aTreeID);
-            } else if (itsTabFocus.equals("Components")) {
-                ComponentsPanel.setSelectedID(aTreeID);
+            switch (itsTabFocus) {
+                case "PIC":
+                    PICPanel.setSelectedID(aTreeID);
+                    break;
+                case "VIC":
+                    VICPanel.setSelectedID(aTreeID);
+                    break;
+                case "Templates":
+                    TemplatesPanel.setSelectedID(aTreeID);
+                    break;
+                case "Default Templates":
+                    DefaultTemplatesPanel.setSelectedID(aTreeID);
+                    break;
+                case "Components":
+                    ComponentsPanel.setSelectedID(aTreeID);
+                    break;
             }
         }
     }
@@ -576,27 +599,33 @@ public class MainPanel extends javax.swing.JPanel
     private int [] getSelectedTreeIDs() {
         int [] rows=this.getSelectedRows();
         int [] treeIDs=new int[rows.length];
-        if (itsTabFocus.equals("PIC")) {
-            for (int i=0; i < rows.length; i++) {
-                treeIDs[i] = ((Integer)PICPanel.getTableModel().getValueAt(rows[i], 0)).intValue();
-            }
-        } else if (itsTabFocus.equals("VIC")) {
-            for (int i=0; i < rows.length; i++) {
-                treeIDs[i] = ((Integer)VICPanel.getTableModel().getValueAt(rows[i], 0)).intValue();
-            }
-        } else if (itsTabFocus.equals("Templates")) {
-            for (int i=0; i < rows.length; i++) {
-                treeIDs[i] = ((Integer)TemplatesPanel.getTableModel().getValueAt(rows[i], 0)).intValue();
-            }
-        } else if (itsTabFocus.equals("Default Templates")) {
-            for (int i=0; i < rows.length; i++) {
-                treeIDs[i] = ((Integer)DefaultTemplatesPanel.getTableModel().getValueAt(rows[i], 0)).intValue();
-            }
-        } else if (itsTabFocus.equals("Components")) {
-            for (int i=0; i < rows.length; i++) {
-                // is the node ID in the case of Components
-                treeIDs[i] = ((Integer)ComponentsPanel.getTableModel().getValueAt(rows[i], 0)).intValue();
-            }
+        switch (itsTabFocus) {
+            case "PIC":
+                for (int i=0; i < rows.length; i++) {
+                    treeIDs[i] = ((Integer)PICPanel.getTableModel().getValueAt(rows[i], 0)).intValue();
+                }
+                break;
+            case "VIC":
+                for (int i=0; i < rows.length; i++) {
+                    treeIDs[i] = ((Integer)VICPanel.getTableModel().getValueAt(rows[i], 0)).intValue();
+                }
+                break;
+            case "Templates":
+                for (int i=0; i < rows.length; i++) {
+                    treeIDs[i] = ((Integer)TemplatesPanel.getTableModel().getValueAt(rows[i], 0)).intValue();
+                }
+                break;
+            case "Default Templates":
+                for (int i=0; i < rows.length; i++) {
+                    treeIDs[i] = ((Integer)DefaultTemplatesPanel.getTableModel().getValueAt(rows[i], 0)).intValue();
+                }
+                break;
+            case "Components":
+                for (int i=0; i < rows.length; i++) {
+                    // is the node ID in the case of Components
+                    treeIDs[i] = ((Integer)ComponentsPanel.getTableModel().getValueAt(rows[i], 0)).intValue();
+                }
+                break;
         }
 
         return treeIDs;   
@@ -606,52 +635,59 @@ public class MainPanel extends javax.swing.JPanel
     private int getSelectedTreeID() {
         int treeID=0;
         int aRow=this.getSelectedRow();
-        if (itsTabFocus.equals("PIC")) {
-            if ( aRow > -1) {
-                treeID = ((Integer)PICPanel.getTableModel().getValueAt(aRow, 0)).intValue();
-                if (treeID > 0) {
-                    itsMainFrame.getSharedVars().setTreeID(treeID);
-                } else {
-                    logger.debug("Tree not found");
+        switch (itsTabFocus) {
+            case "PIC":
+                if ( aRow > -1) {
+                    treeID = ((Integer)PICPanel.getTableModel().getValueAt(aRow, 0)).intValue();
+                    if (treeID > 0) {
+                        itsMainFrame.getSharedVars().setTreeID(treeID);
+                    } else {
+                        logger.debug("Tree not found");
+                    }
                 }
-            }
-        } else if (itsTabFocus.equals("VIC")) {
-            if ( aRow > -1) {
-                treeID = ((Integer)VICPanel.getTableModel().getValueAt(aRow, 0)).intValue();
-                if (treeID > 0) {
-                    itsMainFrame.getSharedVars().setTreeID(treeID);
-                } else {
-                    logger.debug("Tree not found");
+                break;
+            case "VIC":
+                if ( aRow > -1) {
+                    treeID = ((Integer)VICPanel.getTableModel().getValueAt(aRow, 0)).intValue();
+                    if (treeID > 0) {
+                        itsMainFrame.getSharedVars().setTreeID(treeID);
+                    } else {
+                        logger.debug("Tree not found");
+                    }
                 }
-            }
-        } else if (itsTabFocus.equals("Templates")) {
-            if ( aRow > -1) {
-                treeID = ((Integer)TemplatesPanel.getTableModel().getValueAt(aRow, 0)).intValue();
-                if (treeID > 0) {
-                    itsMainFrame.getSharedVars().setTreeID(treeID);
-                } else {
-                    logger.debug("Tree not found");
+                break;
+            case "Templates":
+                if ( aRow > -1) {
+                    treeID = ((Integer)TemplatesPanel.getTableModel().getValueAt(aRow, 0)).intValue();
+                    if (treeID > 0) {
+                        itsMainFrame.getSharedVars().setTreeID(treeID);
+                    } else {
+                        logger.debug("Tree not found");
+                    }
                 }
-            }
-        } else if (itsTabFocus.equals("Default Templates")) {
-            if ( aRow > -1) {
-                treeID = ((Integer)DefaultTemplatesPanel.getTableModel().getValueAt(aRow, 0)).intValue();
-                if (treeID > 0) {
-                    itsMainFrame.getSharedVars().setTreeID(treeID);
-                } else {
-                    logger.debug("DefaultTree not found");
+                break;
+            case "Default Templates":
+                if ( aRow > -1) {
+                    treeID = ((Integer)DefaultTemplatesPanel.getTableModel().getValueAt(aRow, 0)).intValue();
+                    if (treeID > 0) {
+                        itsMainFrame.getSharedVars().setTreeID(treeID);
+                    } else {
+                        logger.debug("DefaultTree not found");
+                    }
                 }
-            }
-        } else if (itsTabFocus.equals("Components")) {
-            if ( aRow > -1) {
-                // is the node ID in the case of Components
-                treeID = ((Integer)ComponentsPanel.getTableModel().getValueAt(aRow, 0)).intValue();
-                if (treeID > 0) {
-                    itsMainFrame.getSharedVars().setComponentID(treeID);
-                } else {
-                    logger.debug("Component not found");
+                break;
+            case "Components":
+                if ( aRow > -1) {
+                    // is the node ID in the case of Components
+                    treeID = ((Integer)ComponentsPanel.getTableModel().getValueAt(aRow, 0)).intValue();
+                    if (treeID > 0) {
+                        itsMainFrame.getSharedVars().setComponentID(treeID);
+                    } else {
+                        logger.debug("Component not found");
+                    }
                 }
-            }        }
+                break;
+        }
         return treeID;
     }
     
@@ -665,38 +701,43 @@ public class MainPanel extends javax.swing.JPanel
             inputFieldBuilder.currentInputField.checkPopup();
         }
         int treeID=getSelectedTreeID();
-        if (aButton.equals("Quit")) {
-            itsMainFrame.exit();
-            return;
-        } else if (aButton.equals("Refresh")) {
-            //set changed flag, we want to refresh the tree
-            itsMainFrame.setChanged(this.getFriendlyName(),true);
-            checkChanged();
-            return;
+        switch (aButton) {
+            case "Quit":
+                itsMainFrame.exit();
+                return;
+            case "Refresh":
+                //set changed flag, we want to refresh the tree
+                itsMainFrame.setChanged(this.getFriendlyName(),true);
+                checkChanged();
+                return;
         }
-        if (itsTabFocus.equals("PIC")) {
-            if (treeID > 0) {
-                itsMainFrame.getSharedVars().setTreeID(treeID);
-            } else if (!aButton.equals("New")) {
-                LofarUtils.showErrorPanel(this,"You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+        switch (itsTabFocus) {
+            case "PIC":
+                if (treeID > 0) {
+                    itsMainFrame.getSharedVars().setTreeID(treeID);
+                } else if (!aButton.equals("New")) {
+                    LofarUtils.showErrorPanel(this,"You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
 
-                return;
-            }
-            if (aButton.equals("Query Panel")) {
+                    return;
+                }
+        switch (aButton) {
+            case "Query Panel":
                 // TODO open Query Panel
                 itsMainFrame.ToDo();
-            } else if (aButton.equals("State History")) {
+                break;
+            case "State History":
                 if (treeID > 0) {
                     viewStateChanges(treeID);
                 }
-            } else if (aButton.equals("New")) {
+                break;
+            case "New":
                 if (getFile("PIC-tree")) {
                     try {
                        // the file obviously resides at the client side, and needs to be transfered to the server side.
                        byte uldata[] = new byte[(int)itsNewFile.length()]; 
-                       BufferedInputStream input = new BufferedInputStream(new FileInputStream(itsNewFile));   
-                       input.read(uldata,0,uldata.length);
-                       input.close();
+                try (BufferedInputStream input = new BufferedInputStream(new FileInputStream(itsNewFile))) {
+                    input.read(uldata,0,uldata.length);
+                }
                        String aFileName= "/tmp/"+itsMainFrame.getUserAccount().getUserName()+"_"+itsNewFile.getName();
                        if (OtdbRmi.getRemoteFileTrans().uploadFile(uldata,aFileName)) {
                            logger.debug("upload finished");                       
@@ -746,9 +787,9 @@ public class MainPanel extends javax.swing.JPanel
                        logger.error(aS);
                        LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                    }
-                }  
-
-            } else if (aButton.equals("Delete")) {
+                }
+                break;
+            case "Delete":
                 if (JOptionPane.showConfirmDialog(this,"Are you sure you want to delete this tree(s): ?","Delete Tree",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION ) {
                     try {
                         int[] treeIDs=getSelectedTreeIDs();
@@ -768,12 +809,14 @@ public class MainPanel extends javax.swing.JPanel
                     ((PICtableModel)PICPanel.getTableModel()).fillTable();
                     itsMainFrame.setNormalCursor();
                 }
-            } else if (aButton.equals("View")) {
+                break;
+            case "View":
                 ResultBrowserPanel aP=(ResultBrowserPanel)itsMainFrame.registerPlugin("nl.astron.lofar.sas.otb.panels.ResultBrowserPanel", true, true);
                 if (aP != null) {
                     itsMainFrame.showPanel(aP.getFriendlyName());
                 }
-            } else if (aButton.equals("Info")) {
+                break;
+            case "Info":
                 if (treeID > 0) {
                     int [] id=new int[1];
                     id[0]=treeID;
@@ -784,22 +827,27 @@ public class MainPanel extends javax.swing.JPanel
                           checkChanged();
                     }
                 }
-            }
-        } else if (itsTabFocus.equals("VIC")) {
-            if (treeID > 0) {
-                itsMainFrame.getSharedVars().setTreeID(treeID);
-            } else {
-                LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                return;
-            }
-            if (aButton.equals("Query Panel")) {
+                break;
+        }
+                break;
+            case "VIC":
+                if (treeID > 0) {
+                    itsMainFrame.getSharedVars().setTreeID(treeID);
+                } else {
+                    LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                    return;
+                }
+        switch (aButton) {
+            case "Query Panel":
                 // TODO open Query Panel
                 itsMainFrame.ToDo();
-            } else if (aButton.equals("State History")) {
+                break;
+            case "State History":
                 if (treeID > 0) {
                     viewStateChanges(treeID);
                 }
-            } else if (aButton.equals("Delete")) {
+                break;
+            case "Delete":
                 if (JOptionPane.showConfirmDialog(this,"Are you sure you want to delete this tree(s): ?","Delete Tree",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION ) {
                     try {
                         int[] treeIDs=getSelectedTreeIDs();
@@ -819,16 +867,16 @@ public class MainPanel extends javax.swing.JPanel
                     ((VICtableModel)VICPanel.getTableModel()).fillTable();
                     itsMainFrame.setNormalCursor();
                 }
-            } else if (aButton.equals("View")) {
+                break;
+            case "View":
                 ResultBrowserPanel aP=(ResultBrowserPanel)itsMainFrame.registerPlugin("nl.astron.lofar.sas.otb.panels.ResultBrowserPanel", true, true);
                 if (aP != null) {
                     itsMainFrame.showPanel(aP.getFriendlyName());
                 }
-            } else if (aButton.equals("Schedule")) {
-                
+                break;
+            case "Schedule":
                 // in case of VICtree we have the possibility of changing a multiple selection
                 // so things like start and/or stoptimes can be set for a few entries at once
-                
                 if (this.VICPanel.getSelectedRowCount() > 0) {
                     if (viewInfo(this.getSelectedTreeIDs()) ) {
                         logger.debug("Tree has been changed, reloading tableline");
@@ -837,279 +885,293 @@ public class MainPanel extends javax.swing.JPanel
                     }
                     
                 }
-            }
-        } else if (itsTabFocus.equals("Templates")) {
-            jOTDBtree aTree=null;
-            String aTreeState="";
-        
-            if (treeID > 0) {
-                itsMainFrame.getSharedVars().setTreeID(treeID);
-                try {
-                    aTree =    OtdbRmi.getRemoteOTDB().getTreeInfo(treeID,false);
-                    aTreeState=OtdbRmi.getRemoteTypes().getTreeState(aTree.state);
-                } catch (RemoteException ex) {
-                    String aS="Error during Remote treeMaintenance";
-                    logger.debug(aS);
-                    LofarUtils.showErrorPanel(this, aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                }
-            } else {
+                break;
+        }
+                break;
+            case "Templates":
+                {
+                    jOTDBtree aTree=null;
+                    String aTreeState="";
+                    if (treeID > 0) {
+                        itsMainFrame.getSharedVars().setTreeID(treeID);
+                        try {
+                            aTree =    OtdbRmi.getRemoteOTDB().getTreeInfo(treeID,false);
+                            aTreeState=OtdbRmi.getRemoteTypes().getTreeState(aTree.state);
+                        } catch (RemoteException ex) {
+                            String aS="Error during Remote treeMaintenance";
+                            logger.debug(aS);
+                            LofarUtils.showErrorPanel(this, aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                        }
+                    } else {
 
-                LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                return;
-            }
-            if (aButton.equals("Query Panel")) {
-                itsMainFrame.ToDo();
-            } else if (aButton.equals("State History")) {
-                if (treeID > 0) {
-                    viewStateChanges(treeID);
-                }
-            } else if (aButton.equals("Duplicate")) {
-                if (treeID < 1) {
-                    LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                } else {
-                    try {
-                        int newTreeID=OtdbRmi.getRemoteMaintenance().copyTemplateTree(treeID);
-                        if (newTreeID > 0) {
-                            JOptionPane.showMessageDialog(this,"New Tree created with ID: "+newTreeID,
-                                "New Tree Message",
-                                JOptionPane.INFORMATION_MESSAGE);
-                            // set back treestate to described
-                            jOTDBtree aT=OtdbRmi.getRemoteOTDB().getTreeInfo(newTreeID, false); 
-                            if (aT.state != OtdbRmi.getRemoteTypes().getTreeState("described") ) {
-                                aT.state=OtdbRmi.getRemoteTypes().getTreeState("described");
-                                if (!OtdbRmi.getRemoteMaintenance().setTreeState(aT.treeID(), aT.state)) {
-                                    String aS="Error during setTreeState: "+OtdbRmi.getRemoteMaintenance().errorMsg();
-                                    logger.error(aS);
-                                    LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                                }
-                            }
-                            // check momID, if not zero set to zero
-                            if (aT.momID() != 0) {
-                                if (!OtdbRmi.getRemoteMaintenance().setMomInfo(aT.treeID(),0,aT.groupID,aT.campaign)) {
-                                    String aS="Error during setMomInfo: "+OtdbRmi.getRemoteMaintenance().errorMsg();
-                                    logger.debug(aS);
-                                    LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                        LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                        return;
+                    }
+            switch (aButton) {
+                case "Query Panel":
+                    itsMainFrame.ToDo();
+                    break;
+                case "State History":
+                    if (treeID > 0) {
+                        viewStateChanges(treeID);
+                    }
+                    break;
+                case "Duplicate":
+                    if (treeID < 1) {
+                        LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                    } else {
+                        try {
+                            int newTreeID=OtdbRmi.getRemoteMaintenance().copyTemplateTree(treeID);
+                            if (newTreeID > 0) {
+                                JOptionPane.showMessageDialog(this,"New Tree created with ID: "+newTreeID,
+                                    "New Tree Message",
+                                    JOptionPane.INFORMATION_MESSAGE);
+                                // set back treestate to described
+                                jOTDBtree aT=OtdbRmi.getRemoteOTDB().getTreeInfo(newTreeID, false); 
+                                if (aT.state != OtdbRmi.getRemoteTypes().getTreeState("described") ) {
+                                    aT.state=OtdbRmi.getRemoteTypes().getTreeState("described");
+                                    if (!OtdbRmi.getRemoteMaintenance().setTreeState(aT.treeID(), aT.state)) {
+                                        String aS="Error during setTreeState: "+OtdbRmi.getRemoteMaintenance().errorMsg();
+                                        logger.error(aS);
+                                        LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                                    }
                                 }
+                                // check momID, if not zero set to zero
+                                if (aT.momID() != 0) {
+                                    if (!OtdbRmi.getRemoteMaintenance().setMomInfo(aT.treeID(),0,aT.groupID,aT.campaign)) {
+                                        String aS="Error during setMomInfo: "+OtdbRmi.getRemoteMaintenance().errorMsg();
+                                        logger.debug(aS);
+                                        LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                                    }
 
+                                }
+                                itsMainFrame.getSharedVars().setTreeID(newTreeID);
+                                // set changed flag to reload mainpanel
+                                itsMainFrame.setChanged(this.getFriendlyName(),true);
+                                checkChanged();
+                            } else {
+                                logger.debug("No Template Tree created!!!");
                             }
-                            itsMainFrame.getSharedVars().setTreeID(newTreeID);
-                            // set changed flag to reload mainpanel
-                            itsMainFrame.setChanged(this.getFriendlyName(),true);
-                            checkChanged();
-                        } else {
-                            logger.debug("No Template Tree created!!!");
+               
+                        } catch (RemoteException ex) {
+                            String aS="Remote error during Build TemplateTree: "+ ex;
+                            logger.error(aS);
+                            LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                         }
-           
-                    } catch (RemoteException ex) {
-                        String aS="Remote error during Build TemplateTree: "+ ex;
-                        logger.error(aS);
-                        LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                     }
-                }
-            } else if (aButton.equals("Modify")) {
-                TemplateMaintenancePanel aP =(TemplateMaintenancePanel)itsMainFrame.registerPlugin("nl.astron.lofar.sas.otb.panels.TemplateMaintenancePanel", true, true);
-                if (aP != null) {
-                    itsMainFrame.showPanel(aP.getFriendlyName());
-                }
-            } else if (aButton.equals("Delete")) {
-                 if (JOptionPane.showConfirmDialog(this,"Are you sure you want to delete this tree(s) ?","Delete Tree",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION ) {
-                     try {
-                        int[] treeIDs=getSelectedTreeIDs();
-                        for (int i=0;i< treeIDs.length;i++) {
-                            if (!OtdbRmi.getRemoteMaintenance().deleteTree(treeIDs[i])) {
-                                String aS="Failed to delete tree: "+treeIDs[i];
+                    break;
+                case "Modify":
+                    TemplateMaintenancePanel aP =(TemplateMaintenancePanel)itsMainFrame.registerPlugin("nl.astron.lofar.sas.otb.panels.TemplateMaintenancePanel", true, true);
+                    if (aP != null) {
+                        itsMainFrame.showPanel(aP.getFriendlyName());
+                    }
+                    break;
+                case "Delete":
+                    if (JOptionPane.showConfirmDialog(this,"Are you sure you want to delete this tree(s) ?","Delete Tree",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION ) {
+                        try {
+                           int[] treeIDs=getSelectedTreeIDs();
+                           for (int i=0;i< treeIDs.length;i++) {
+                               if (!OtdbRmi.getRemoteMaintenance().deleteTree(treeIDs[i])) {
+                                   String aS="Failed to delete tree: "+treeIDs[i];
+                                   logger.error(aS);
+                                   LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                               }
+                           }
+                        } catch (RemoteException ex) {
+                           String aS="Remote error during deleteTree: "+ ex;
+                           logger.error(aS);
+                           LofarUtils.showErrorPanel(this, aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                       }
+                       itsMainFrame.getSharedVars().setTreeID(-1);                              
+                       itsMainFrame.setHourglassCursor();
+                       ((TemplatetableModel)TemplatesPanel.getTableModel()).fillTable();
+                       itsMainFrame.setNormalCursor();
+                       // set changed flag to reload mainpanel
+   //                  itsMainFrame.setChanged(this.getFriendlyName(),true);
+   //                    checkChanged();
+                   }break;
+                case "Build VIC tree":
+                    if (treeID < 1) {
+                        LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                    } else {
+                        try {
+                            int newTreeID=OtdbRmi.getRemoteMaintenance().instanciateTree(itsMainFrame.getSharedVars().getTreeID());
+                            if (newTreeID > 0) {
+                                JOptionPane.showMessageDialog(this,"New VICTree created with ID: "+newTreeID,
+                                    "New Tree Message",
+                                    JOptionPane.INFORMATION_MESSAGE);
+                                itsMainFrame.getSharedVars().setTreeID(newTreeID);
+                                // set changed flag to reload mainpanel
+                                itsMainFrame.setChanged(this.getFriendlyName(),true);
+                                checkChanged();
+                            } else {
+                                String aS="No VIC Tree created!!! : "+ OtdbRmi.getRemoteMaintenance().errorMsg();
                                 logger.error(aS);
                                 LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                             }
-                        }
-                     } catch (RemoteException ex) {
-                        String aS="Remote error during deleteTree: "+ ex;
-                        logger.error(aS);
-                        LofarUtils.showErrorPanel(this, aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                    }
-                    itsMainFrame.getSharedVars().setTreeID(-1);                              
-                    itsMainFrame.setHourglassCursor();
-                    ((TemplatetableModel)TemplatesPanel.getTableModel()).fillTable();
-                    itsMainFrame.setNormalCursor();
-                    // set changed flag to reload mainpanel
-//                  itsMainFrame.setChanged(this.getFriendlyName(),true);
-//                    checkChanged();
-                }
-                
-            } else if (aButton.equals("Build VIC tree")) {
-                if (treeID < 1) {
-                    LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                } else {
-                    try {
-                        int newTreeID=OtdbRmi.getRemoteMaintenance().instanciateTree(itsMainFrame.getSharedVars().getTreeID());
-                        if (newTreeID > 0) {
-                            JOptionPane.showMessageDialog(this,"New VICTree created with ID: "+newTreeID,
-                                "New Tree Message",
-                                JOptionPane.INFORMATION_MESSAGE);
-                            itsMainFrame.getSharedVars().setTreeID(newTreeID);
-                            // set changed flag to reload mainpanel
-                            itsMainFrame.setChanged(this.getFriendlyName(),true);
-                            checkChanged();
-                        } else {
-                            String aS="No VIC Tree created!!! : "+ OtdbRmi.getRemoteMaintenance().errorMsg();
+               
+                        } catch (RemoteException ex) {
+                            String aS="Remote error during Build VICTree: "+ ex;
                             logger.error(aS);
                             LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                         }
-           
-                    } catch (RemoteException ex) {
-                        String aS="Remote error during Build VICTree: "+ ex;
-                        logger.error(aS);
-                        LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                     }
-                }
-                
-            } else if (aButton.equals("Change Status")) {
-
-                // in case of templatetree we have the possibility of changing a multiple selection
-                // so things status can be set for a few entries at once
+                    break;
+                case "Change Status":
+                    // in case of templatetree we have the possibility of changing a multiple selection
+                    // so things status can be set for a few entries at once
+                    if (TemplatesPanel.getSelectedRowCount() > 0) {
+                        if (viewInfo(this.getSelectedTreeIDs()) ) {
+                            logger.debug("Tree has been changed, reloading tableline");
+                              itsMainFrame.setChanged(this.getFriendlyName(),true);
+                              checkChanged();
+                        }
 
-                if (TemplatesPanel.getSelectedRowCount() > 0) {
-                    if (viewInfo(this.getSelectedTreeIDs()) ) {
-                        logger.debug("Tree has been changed, reloading tableline");
-                          itsMainFrame.setChanged(this.getFriendlyName(),true);
-                          checkChanged();
                     }
+                    break;
+                case "MultiEdit":
+                    // in case of templatetree we have the possibility to change a few crucial settings for all trees chosen
+                    if (TemplatesPanel.getSelectedRowCount() > 0) {
+                        if (viewMultiEditDialog(this.getSelectedTreeIDs()) ) {
+                            logger.debug("Trees have been changed, reloading tablelines");
+                              itsMainFrame.setChanged(this.getFriendlyName(),true);
+                              checkChanged();
+                        }
 
-                }
-
-            } else if (aButton.equals("MultiEdit")) {
-
-                // in case of templatetree we have the possibility to change a few crucial settings for all trees chosen
-
-                if (TemplatesPanel.getSelectedRowCount() > 0) {
-                    if (viewMultiEditDialog(this.getSelectedTreeIDs()) ) {
-                        logger.debug("Trees have been changed, reloading tablelines");
-                          itsMainFrame.setChanged(this.getFriendlyName(),true);
-                          checkChanged();
                     }
-
-                }
-
-            } else if (aButton.equals("Set to Default")) {
-                if (itsMainFrame.getSharedVars().getTreeID() > 0) {
-                    try {
-                        Vector<jDefaultTemplate> aDFList = OtdbRmi.getRemoteOTDB().getDefaultTemplates();
-                        if (createDefaultTemplateDialog(aTree, aDFList)) {
-                            // check momID, if not zero set to zero
-                            if (aTree.momID() != 0) {
-                                if (!OtdbRmi.getRemoteMaintenance().setMomInfo(aTree.treeID(), 0, aTree.groupID, aTree.campaign)) {
-                                    String aS = "Error during setMomInfo: " + OtdbRmi.getRemoteMaintenance().errorMsg();
-                                    logger.error(aS);
-                                    LofarUtils.showErrorPanel(this, aS, new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                    break;
+                case "Set to Default":
+                    if (itsMainFrame.getSharedVars().getTreeID() > 0) {
+                        try {
+                            ArrayList<jDefaultTemplate> aDFList = new ArrayList(OtdbRmi.getRemoteOTDB().getDefaultTemplates());
+                            if (createDefaultTemplateDialog(aTree, aDFList)) {
+                                // check momID, if not zero set to zero
+                                if (aTree.momID() != 0) {
+                                    if (!OtdbRmi.getRemoteMaintenance().setMomInfo(aTree.treeID(), 0, aTree.groupID, aTree.campaign)) {
+                                        String aS = "Error during setMomInfo: " + OtdbRmi.getRemoteMaintenance().errorMsg();
+                                        logger.error(aS);
+                                        LofarUtils.showErrorPanel(this, aS, new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                                    }
                                 }
                                 itsMainFrame.setChanged(this.getFriendlyName(), true);
                                 checkChanged();
                             }
+                        } catch (RemoteException ex) {
+                            String aS="Error during Remote treeMaintenance" + ex;
+                            logger.error(aS);
+                            LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                         }
-                    } catch (RemoteException ex) {
-                        String aS="Error during Remote treeMaintenance" + ex;
-                        logger.error(aS);
-                        LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                     }
-                }
+                    break;
             }
-        } else if (itsTabFocus.equals("Default Templates")) {
-            jOTDBtree aTree=null;
-            String aTreeState="";
-
-            if (treeID > 0) {
-                itsMainFrame.getSharedVars().setTreeID(treeID);
-                try {
-                    aTree =    OtdbRmi.getRemoteOTDB().getTreeInfo(treeID,false);
-                    aTreeState=OtdbRmi.getRemoteTypes().getTreeState(aTree.state);
-                } catch (RemoteException ex) {
-                    String aS="Error during Remote treeMaintenance" + ex;
-                    logger.error(aS);
-                    LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                }
-            } else {
-                LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                return;
-            }
-            if (aButton.equals("Query Panel")) {
-                itsMainFrame.ToDo();
-            } else if (aButton.equals("State History")) {
-                if (treeID > 0) {
-                    viewStateChanges(treeID);
+                    break;
                 }
-            } else if (aButton.equals("Duplicate")) {
-                if (treeID < 1) {
-                    LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                } else {
-                    try {
-                        int newTreeID=OtdbRmi.getRemoteMaintenance().copyTemplateTree(treeID);
-                        if (newTreeID > 0) {
-                            JOptionPane.showMessageDialog(this,"New Tree (Not Default!!!) created with ID: "+newTreeID,
-                                "New Tree Message",
-                                JOptionPane.INFORMATION_MESSAGE);
-                            // set back treestate to described
-                            jOTDBtree aT=OtdbRmi.getRemoteOTDB().getTreeInfo(newTreeID, false);
-                            if (aT.state != OtdbRmi.getRemoteTypes().getTreeState("described") ) {
-                                aT.state=OtdbRmi.getRemoteTypes().getTreeState("described");
-                                if (!OtdbRmi.getRemoteMaintenance().setTreeState(aT.treeID(), aT.state)) {
-                                    String aS="Error during setTreeState: "+OtdbRmi.getRemoteMaintenance().errorMsg();
-                                    logger.error(aS);
-                                    LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                                }
-                            }
-                            // check momID, if not zero set to zero
-                            if (aT.momID() != 0) {
-                                if (!OtdbRmi.getRemoteMaintenance().setMomInfo(aT.treeID(),0, aT.groupID, aT.campaign)) {
-                                    String aS="Error during setMomInfo: "+OtdbRmi.getRemoteMaintenance().errorMsg();
-                                    logger.error(aS);
-                                    LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+            case "Default Templates":
+                {
+                    jOTDBtree aTree=null;
+                    String aTreeState="";
+                    if (treeID > 0) {
+                        itsMainFrame.getSharedVars().setTreeID(treeID);
+                        try {
+                            aTree =    OtdbRmi.getRemoteOTDB().getTreeInfo(treeID,false);
+                            aTreeState=OtdbRmi.getRemoteTypes().getTreeState(aTree.state);
+                        } catch (RemoteException ex) {
+                            String aS="Error during Remote treeMaintenance" + ex;
+                            logger.error(aS);
+                            LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                        }
+                    } else {
+                        LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                        return;
+                    }
+            switch (aButton) {
+                case "Query Panel":
+                    itsMainFrame.ToDo();
+                    break;
+                case "State History":
+                    if (treeID > 0) {
+                        viewStateChanges(treeID);
+                    }
+                    break;
+                case "Duplicate":
+                    if (treeID < 1) {
+                        LofarUtils.showErrorPanel(this, "You didn't select a tree",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                    } else {
+                        try {
+                            int newTreeID=OtdbRmi.getRemoteMaintenance().copyTemplateTree(treeID);
+                            if (newTreeID > 0) {
+                                JOptionPane.showMessageDialog(this,"New Tree (Not Default!!!) created with ID: "+newTreeID,
+                                    "New Tree Message",
+                                    JOptionPane.INFORMATION_MESSAGE);
+                                // set back treestate to described
+                                jOTDBtree aT=OtdbRmi.getRemoteOTDB().getTreeInfo(newTreeID, false);
+                                if (aT.state != OtdbRmi.getRemoteTypes().getTreeState("described") ) {
+                                    aT.state=OtdbRmi.getRemoteTypes().getTreeState("described");
+                                    if (!OtdbRmi.getRemoteMaintenance().setTreeState(aT.treeID(), aT.state)) {
+                                        String aS="Error during setTreeState: "+OtdbRmi.getRemoteMaintenance().errorMsg();
+                                        logger.error(aS);
+                                        LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                                    }
                                 }
+                                // check momID, if not zero set to zero
+                                if (aT.momID() != 0) {
+                                    if (!OtdbRmi.getRemoteMaintenance().setMomInfo(aT.treeID(),0, aT.groupID, aT.campaign)) {
+                                        String aS="Error during setMomInfo: "+OtdbRmi.getRemoteMaintenance().errorMsg();
+                                        logger.error(aS);
+                                        LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                                    }
 
+                                }
+                                itsMainFrame.getSharedVars().setTreeID(newTreeID);
+                                // set changed flag to reload mainpanel
+                                itsMainFrame.setChanged(this.getFriendlyName(),true);
+                                checkChanged();
+                            } else {
+                                String aS="No Template Tree created!!!";
+                                logger.error(aS);
+                                LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                             }
-                            itsMainFrame.getSharedVars().setTreeID(newTreeID);
-                            // set changed flag to reload mainpanel
-                            itsMainFrame.setChanged(this.getFriendlyName(),true);
-                            checkChanged();
-                        } else {
-                            String aS="No Template Tree created!!!";
+
+                        } catch (RemoteException ex) {
+                            String aS="Remote error during Build TemplateTree: "+ ex;
                             logger.error(aS);
                             LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                         }
-
-                    } catch (RemoteException ex) {
-                        String aS="Remote error during Build TemplateTree: "+ ex;
-                        logger.error(aS);
-                        LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                     }
-                }
- 
-            } else if (aButton.equals("Modify")) {
-                TemplateMaintenancePanel aP =(TemplateMaintenancePanel)itsMainFrame.registerPlugin("nl.astron.lofar.sas.otb.panels.TemplateMaintenancePanel", true, true);
-                if (aP != null) {
-                    itsMainFrame.showPanel(aP.getFriendlyName());
-                }
-            } else if (aButton.equals("Change Status")) {
-                if (itsMainFrame.getSharedVars().getTreeID() > 0) {
-                    int [] id = new int[1];
-                    id[0]=itsMainFrame.getSharedVars().getTreeID();
-                    if (viewInfo(id)) {
-                        logger.debug("Tree has been changed, reloading table line");
-                          itsMainFrame.setChanged(this.getFriendlyName(),true);
-                          checkChanged();
+                    break;
+                case "Modify":
+                    TemplateMaintenancePanel aP =(TemplateMaintenancePanel)itsMainFrame.registerPlugin("nl.astron.lofar.sas.otb.panels.TemplateMaintenancePanel", true, true);
+                    if (aP != null) {
+                        itsMainFrame.showPanel(aP.getFriendlyName());
                     }
-                }
+                    break;
+                case "Change Status":
+                    if (itsMainFrame.getSharedVars().getTreeID() > 0) {
+                        int [] id = new int[1];
+                        id[0]=itsMainFrame.getSharedVars().getTreeID();
+                        if (viewInfo(id)) {
+                            logger.debug("Tree has been changed, reloading table line");
+                              itsMainFrame.setChanged(this.getFriendlyName(),true);
+                              checkChanged();
+                        }
+                    }
+                    break;
             }
-        } else if (itsTabFocus.equals("Components")) {
-            if (aButton.equals("Query Panel")) {
+                    break;
+                }
+            case "Components":
+        switch (aButton) {
+            case "Query Panel":
                 itsMainFrame.ToDo();
-            } else if (aButton.equals("New")) {
+                break;
+            case "New":
                 if (getFile("VIC-component") ) {
                     try {
                         // the file obviously resides at the client side, and needs to be transfered to the server side.
                         byte uldata[] = new byte[(int)itsNewFile.length()]; 
-                        BufferedInputStream input = new BufferedInputStream(new FileInputStream(itsNewFile));   
-                        input.read(uldata,0,uldata.length);
-                        input.close();
+                try (BufferedInputStream input = new BufferedInputStream(new FileInputStream(itsNewFile))) {
+                    input.read(uldata,0,uldata.length);
+                }
                         String aFileName= "/tmp/"+itsMainFrame.getUserAccount().getUserName()+"_"+itsNewFile.getName();
                         if (OtdbRmi.getRemoteFileTrans().uploadFile(uldata,aFileName)) {
                             logger.debug("upload finished");
@@ -1158,12 +1220,14 @@ public class MainPanel extends javax.swing.JPanel
                         LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                     }
                 }
-            } else if (aButton.equals("Modify")) {
+                break;
+            case "Modify":
                 ComponentMaintenancePanel aP = (ComponentMaintenancePanel)itsMainFrame.registerPlugin("nl.astron.lofar.sas.otb.panels.ComponentMaintenancePanel", true, true);
                 if (aP != null) {
                     itsMainFrame.showPanel(aP.getFriendlyName());
                 }
-            } else if (aButton.equals("Build TemplateTree")) {
+                break;
+            case "Build TemplateTree":
                 int nodeID=itsMainFrame.getSharedVars().getComponentID();
                 short classifID;
                 try {
@@ -1189,7 +1253,8 @@ public class MainPanel extends javax.swing.JPanel
                     logger.error(aS);
                     LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                 }
-            } else if (aButton.equals("Delete")) {
+                break;
+            case "Delete":
                 if (JOptionPane.showConfirmDialog(this,"Are you sure you want to delete this component(s): ?","Delete Component",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION ) {
                     try {
                         int[] componentIDs=getSelectedTreeIDs();
@@ -1213,11 +1278,15 @@ public class MainPanel extends javax.swing.JPanel
                     ((ComponentTableModel)ComponentsPanel.getTableModel()).fillTable();
                     itsMainFrame.setNormalCursor();
                 }
-            }
-        } else if (itsTabFocus.equals("Query Results")) {
-            itsMainFrame.ToDo();
-        } else {
-            logger.debug("Other command found: "+aButton);
+                break;
+        }
+                break;
+            case "Query Results":
+                itsMainFrame.ToDo();
+                break;
+            default:
+                logger.debug("Other command found: "+aButton);
+                break;
         }
     }
 
@@ -1310,7 +1379,7 @@ public class MainPanel extends javax.swing.JPanel
      *
      * @param  aList Vector<jDefaultTemplate> all default template names.
      */
-    private boolean createDefaultTemplateDialog(jOTDBtree aTree,Vector<jDefaultTemplate> aList) {
+    private boolean createDefaultTemplateDialog(jOTDBtree aTree,ArrayList<jDefaultTemplate> aList) {
         logger.debug("createDefaultTemplateDialog started");
 
         if (itsMainFrame.getSharedVars().getTreeID() > 0) {
@@ -1325,6 +1394,7 @@ public class MainPanel extends javax.swing.JPanel
 
             if (defaultTemplateDialog.isOk()) {
                 logger.debug("defaultTemplate is created");
+                setChanged(true);
             } else {
                 logger.debug("defaultTemplate is not created");
             }
@@ -1409,177 +1479,182 @@ public class MainPanel extends javax.swing.JPanel
                 return;
             }
         }
-        if (itsTabFocus.equals("PIC")) {
-            if (treeID>0) {
-                if (aTreeState.equals("active")) {
+        switch (itsTabFocus) {
+            case "PIC":
+                if (treeID>0) {
+                    if (aTreeState.equals("active")) {
+                        buttonPanel1.setButtonEnabled("Delete",false);
+                    } else {
+                        buttonPanel1.setButtonEnabled("Delete",true);                    
+                    }
+                    buttonPanel1.setButtonEnabled("View",true);
+                    buttonPanel1.setButtonEnabled("Info",true);
+                    buttonPanel1.setButtonEnabled("State History",true);
+                } else {
+                    buttonPanel1.setButtonEnabled("State History",false);
                     buttonPanel1.setButtonEnabled("Delete",false);
+                    buttonPanel1.setButtonEnabled("View",false);
+                    buttonPanel1.setButtonEnabled("Info",false);
+                }
+                break;
+            case "VIC":
+                if (VICPanel.getSelectedRowCount() > 1) {
+                    multipleSelection=true;
                 } else {
-                    buttonPanel1.setButtonEnabled("Delete",true);                    
+                    multipleSelection=false;
                 }
-                buttonPanel1.setButtonEnabled("View",true);
-                buttonPanel1.setButtonEnabled("Info",true);
-                buttonPanel1.setButtonEnabled("State History",true);
-            } else {
-                buttonPanel1.setButtonEnabled("State History",false);
-                buttonPanel1.setButtonEnabled("Delete",false);
-                buttonPanel1.setButtonEnabled("View",false);
-                buttonPanel1.setButtonEnabled("Info",false);
-            }
-        } else if (itsTabFocus.equals("VIC")) {
-            if (VICPanel.getSelectedRowCount() > 1) {
-                multipleSelection=true;
-            } else {
-                multipleSelection=false;
-            }
-            if (treeID>0) {
-                // !!!!!!!!!!!!!!
-                // Need to see if buttons need to be invalidated under certain states....
-                //
-                if (!multipleSelection) {
-                    buttonPanel1.setButtonEnabled("State History",true);
-                    buttonPanel1.setButtonEnabled("View",true);
-                    buttonPanel1.setButtonEnabled("Query Panel",true);
-                    buttonPanel1.setButtonEnabled("Refresh",true);
+                if (treeID>0) {
+                    // !!!!!!!!!!!!!!
+                    // Need to see if buttons need to be invalidated under certain states....
+                    //
+                    if (!multipleSelection) {
+                        buttonPanel1.setButtonEnabled("State History",true);
+                        buttonPanel1.setButtonEnabled("View",true);
+                        buttonPanel1.setButtonEnabled("Query Panel",true);
+                        buttonPanel1.setButtonEnabled("Refresh",true);
+                    } else {
+                        buttonPanel1.setButtonEnabled("Query Panel",false);
+                        buttonPanel1.setButtonEnabled("Refresh",false);
+                        buttonPanel1.setButtonEnabled("State History",false);
+                        buttonPanel1.setButtonEnabled("View",false);
+                    }
+                    buttonPanel1.setButtonEnabled("Delete",true);
+                    buttonPanel1.setButtonEnabled("Schedule",true);
                 } else {
-                    buttonPanel1.setButtonEnabled("Query Panel",false);
-                    buttonPanel1.setButtonEnabled("Refresh",false);
-                    buttonPanel1.setButtonEnabled("State History",false);
+                    buttonPanel1.setButtonEnabled("Delete",false);
                     buttonPanel1.setButtonEnabled("View",false);
+                    buttonPanel1.setButtonEnabled("State History",false);
+                    buttonPanel1.setButtonEnabled("Schedule",false);
                 }
-                buttonPanel1.setButtonEnabled("Delete",true);
-                buttonPanel1.setButtonEnabled("Schedule",true);
-            } else {
-                buttonPanel1.setButtonEnabled("Delete",false);
-                buttonPanel1.setButtonEnabled("View",false);
-                buttonPanel1.setButtonEnabled("State History",false);
-                buttonPanel1.setButtonEnabled("Schedule",false);
-            }
-        } else if (itsTabFocus.equals("Templates")) {
-            if (TemplatesPanel.getSelectedRowCount() > 1) {
-                multipleSelection=true;
-            } else {
-                multipleSelection=false;
-            }
-
-            if (treeID > 0) {
-                if ((aTreeState.equals("idle") ||
-                        aTreeState.equals("described") ||
-                        aTreeState.equals("prepared") ||
-                        aTreeState.equals("approved")) && !multipleSelection) {
-                    buttonPanel1.setButtonEnabled("Duplicate",true);
-                    buttonPanel1.setButtonEnabled("Modify",true);
-                    buttonPanel1.setButtonEnabled("Set to Default",true);
-                    buttonPanel1.setButtonEnabled("Query Panel",true);
-                    if (aTreeState.equals("approved") || aTreeState.equals("on_hold") || aTreeState.equals("prescheduled")) {
-                        buttonPanel1.setButtonEnabled("Build VIC tree",true);
+                break;
+            case "Templates":
+                if (TemplatesPanel.getSelectedRowCount() > 1) {
+                    multipleSelection=true;
+                } else {
+                    multipleSelection=false;
+                }
+                if (treeID > 0) {
+                    if ((aTreeState.equals("idle") ||
+                            aTreeState.equals("described") ||
+                            aTreeState.equals("prepared") ||
+                            aTreeState.equals("approved")) && !multipleSelection) {
+                        buttonPanel1.setButtonEnabled("Duplicate",true);
+                        buttonPanel1.setButtonEnabled("Modify",true);
+                        buttonPanel1.setButtonEnabled("Set to Default",true);
+                        buttonPanel1.setButtonEnabled("Query Panel",true);
+                        if (aTreeState.equals("approved") || aTreeState.equals("on_hold") || aTreeState.equals("prescheduled")) {
+                            buttonPanel1.setButtonEnabled("Build VIC tree",true);
+                        } else {
+                            buttonPanel1.setButtonEnabled("Build VIC tree",false);
+                        }
                     } else {
-                        buttonPanel1.setButtonEnabled("Build VIC tree",false);
+                        buttonPanel1.setButtonEnabled("Duplicate",false);
+                        buttonPanel1.setButtonEnabled("Modify",false);                                        
+                        buttonPanel1.setButtonEnabled("Set to Default",false);
                     }
+                    if (multipleSelection) {
+                        buttonPanel1.setButtonEnabled("State History",false);
+                        buttonPanel1.setButtonEnabled("Duplicate",false);
+                        buttonPanel1.setButtonEnabled("Modify",false);
+                        buttonPanel1.setButtonEnabled("MultiEdit",true);
+                        buttonPanel1.setButtonEnabled("Query Panel",false);
+                        buttonPanel1.setButtonEnabled("Refresh",false);
+                    } else {
+                        buttonPanel1.setButtonEnabled("State History",true);
+                        buttonPanel1.setButtonEnabled("Duplicate",true);
+                        buttonPanel1.setButtonEnabled("Modify",true);
+                        buttonPanel1.setButtonEnabled("MultiEdit",false);
+                        buttonPanel1.setButtonEnabled("Query Panel",true);
+                        buttonPanel1.setButtonEnabled("Refresh",true);
+                    }
+                    buttonPanel1.setButtonEnabled("Delete",true);
+                    buttonPanel1.setButtonEnabled("Change Status",true);
                 } else {
                     buttonPanel1.setButtonEnabled("Duplicate",false);
-                    buttonPanel1.setButtonEnabled("Modify",false);                                        
-                    buttonPanel1.setButtonEnabled("Set to Default",false);
-                }
-                if (multipleSelection) {
                     buttonPanel1.setButtonEnabled("State History",false);
-                    buttonPanel1.setButtonEnabled("Duplicate",false);
                     buttonPanel1.setButtonEnabled("Modify",false);
-                    buttonPanel1.setButtonEnabled("MultiEdit",true);
-                    buttonPanel1.setButtonEnabled("Query Panel",false);
-                    buttonPanel1.setButtonEnabled("Refresh",false);
+                    buttonPanel1.setButtonEnabled("Delete",false);                
+                    buttonPanel1.setButtonEnabled("Change Status",false);  
+                    buttonPanel1.setButtonEnabled("Build VIC tree",false);
+                    buttonPanel1.setButtonEnabled("Set to Default",false);
+                }
+                break;
+            case "Default Templates":
+                if (DefaultTemplatesPanel.getSelectedRowCount() > 1) {
+                    multipleSelection=true;
                 } else {
-                    buttonPanel1.setButtonEnabled("State History",true);
-                    buttonPanel1.setButtonEnabled("Duplicate",true);
-                    buttonPanel1.setButtonEnabled("Modify",true);
-                    buttonPanel1.setButtonEnabled("MultiEdit",false);
-                    buttonPanel1.setButtonEnabled("Query Panel",true);
-                    buttonPanel1.setButtonEnabled("Refresh",true);
+                    multipleSelection=false;
                 }
-                buttonPanel1.setButtonEnabled("Delete",true);
-                buttonPanel1.setButtonEnabled("Change Status",true);
-            } else {
-                buttonPanel1.setButtonEnabled("Duplicate",false);
-                buttonPanel1.setButtonEnabled("State History",false);
-                buttonPanel1.setButtonEnabled("Modify",false);
-                buttonPanel1.setButtonEnabled("Delete",false);                
-                buttonPanel1.setButtonEnabled("Change Status",false);  
-                buttonPanel1.setButtonEnabled("Build VIC tree",false);
-                buttonPanel1.setButtonEnabled("Set to Default",false);
-            }
-        } else if (itsTabFocus.equals("Default Templates")) {
-            if (DefaultTemplatesPanel.getSelectedRowCount() > 1) {
-                multipleSelection=true;
-            } else {
-                multipleSelection=false;
-            }
-            if (treeID > 0) {
-                if ((aTreeState.equals("idle") ||
-                        aTreeState.equals("described") ||
-                        aTreeState.equals("prepared") ||
-                        aTreeState.equals("approved")) && !multipleSelection) {
-                    buttonPanel1.setButtonEnabled("Duplicate",true);
-                    buttonPanel1.setButtonEnabled("Modify",true);
+                if (treeID > 0) {
+                    if ((aTreeState.equals("idle") ||
+                            aTreeState.equals("described") ||
+                            aTreeState.equals("prepared") ||
+                            aTreeState.equals("approved")) && !multipleSelection) {
+                        buttonPanel1.setButtonEnabled("Duplicate",true);
+                        buttonPanel1.setButtonEnabled("Modify",true);
+                    } else {
+                        buttonPanel1.setButtonEnabled("Duplicate",false);
+                        buttonPanel1.setButtonEnabled("Modify",false);
+                    }
+                    if (multipleSelection) {
+                        buttonPanel1.setButtonEnabled("State History",false);
+                        buttonPanel1.setButtonEnabled("Duplicate",false);
+                        buttonPanel1.setButtonEnabled("Modify",false);
+                    } else {
+                        buttonPanel1.setButtonEnabled("State History",true);
+                        buttonPanel1.setButtonEnabled("Duplicate",true);
+                        buttonPanel1.setButtonEnabled("Modify",true);
+                    }
+                    buttonPanel1.setButtonEnabled("Change Status",true);
                 } else {
                     buttonPanel1.setButtonEnabled("Duplicate",false);
                     buttonPanel1.setButtonEnabled("Modify",false);
-                }
-                if (multipleSelection) {
                     buttonPanel1.setButtonEnabled("State History",false);
-                    buttonPanel1.setButtonEnabled("Duplicate",false);
-                    buttonPanel1.setButtonEnabled("Modify",false);
+                    buttonPanel1.setButtonEnabled("Change Status",false);
+               }break;
+            case "Components":
+                if (ComponentsPanel.getSelectedRowCount() > 1) {
+                    multipleSelection=true;
                 } else {
-                    buttonPanel1.setButtonEnabled("State History",true);
-                    buttonPanel1.setButtonEnabled("Duplicate",true);
-                    buttonPanel1.setButtonEnabled("Modify",true);
+                    multipleSelection=false;
                 }
-                buttonPanel1.setButtonEnabled("Change Status",true);
-            } else {
-                buttonPanel1.setButtonEnabled("Duplicate",false);
-                buttonPanel1.setButtonEnabled("Modify",false);
-                buttonPanel1.setButtonEnabled("State History",false);
-                buttonPanel1.setButtonEnabled("Change Status",false);
-           }
-        } else if (itsTabFocus.equals("Components")) {
-            if (ComponentsPanel.getSelectedRowCount() > 1) {
-                multipleSelection=true;
-            } else {
-                multipleSelection=false;
-            }
-            if (componentID > 0 ) {
-                if (multipleSelection) {
+                if (componentID > 0 ) {
+                    if (multipleSelection) {
+                        buttonPanel1.setButtonEnabled("Modify",false);
+                        buttonPanel1.setButtonEnabled("Query Panel",false);
+                        buttonPanel1.setButtonEnabled("New",false);
+                        buttonPanel1.setButtonEnabled("Refresh",false);
+                        buttonPanel1.setButtonEnabled("Build TemplateTree",false);
+                    } else {
+                        buttonPanel1.setButtonEnabled("Modify",true);
+                        buttonPanel1.setButtonEnabled("Query Panel",true);
+                        buttonPanel1.setButtonEnabled("New",true);
+                        buttonPanel1.setButtonEnabled("Refresh",true);
+                        buttonPanel1.setButtonEnabled("Build TemplateTree",true);
+                        try {
+                            if (OtdbRmi.getRemoteMaintenance().isTopComponent(componentID)) {
+                                buttonPanel1.setButtonEnabled("Build TemplateTree",true);
+                            } else {
+                                buttonPanel1.setButtonEnabled("Build TemplateTree",false);
+                            }
+                        } catch (RemoteException ex) {
+                            String aS="Error checking isTopComponent";
+                            logger.error(aS);
+                            LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                        }
+                    }
+                    buttonPanel1.setButtonEnabled("Delete",true);
+                } else {
+                    buttonPanel1.setButtonEnabled("Delete",false);
                     buttonPanel1.setButtonEnabled("Modify",false);
                     buttonPanel1.setButtonEnabled("Query Panel",false);
                     buttonPanel1.setButtonEnabled("New",false);
                     buttonPanel1.setButtonEnabled("Refresh",false);
                     buttonPanel1.setButtonEnabled("Build TemplateTree",false);
-                } else {
-                    buttonPanel1.setButtonEnabled("Modify",true);
-                    buttonPanel1.setButtonEnabled("Query Panel",true);
-                    buttonPanel1.setButtonEnabled("New",true);
-                    buttonPanel1.setButtonEnabled("Refresh",true);
-                    buttonPanel1.setButtonEnabled("Build TemplateTree",true);
-                    try {
-                        if (OtdbRmi.getRemoteMaintenance().isTopComponent(componentID)) {
-                            buttonPanel1.setButtonEnabled("Build TemplateTree",true);
-                        } else {
-                            buttonPanel1.setButtonEnabled("Build TemplateTree",false);
-                        }
-                    } catch (RemoteException ex) {
-                        String aS="Error checking isTopComponent";
-                        logger.error(aS);
-                        LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                    }
                 }
-                buttonPanel1.setButtonEnabled("Delete",true);
-            } else {
-                buttonPanel1.setButtonEnabled("Delete",false);
-                buttonPanel1.setButtonEnabled("Modify",false);
-                buttonPanel1.setButtonEnabled("Query Panel",false);
-                buttonPanel1.setButtonEnabled("New",false);
-                buttonPanel1.setButtonEnabled("Refresh",false);
-                buttonPanel1.setButtonEnabled("Build TemplateTree",false);
-            }
-        } else if (itsTabFocus.equals("Query Results")) {
+                break;
+            case "Query Results":
+                break;
         }
     }
     
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/util/tablemodels/DefaultTemplatetableModel.java b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/util/tablemodels/DefaultTemplatetableModel.java
index 80e39ef9e41abfe06f3ff990778955913a3cbef0..c7e51c8173f854c81ba427e215e5b008ff5cd9c3 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/util/tablemodels/DefaultTemplatetableModel.java
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otb/util/tablemodels/DefaultTemplatetableModel.java
@@ -23,6 +23,7 @@
 package nl.astron.lofar.sas.otb.util.tablemodels;
 
 import java.rmi.RemoteException;
+import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.Vector;
 import nl.astron.lofar.sas.otb.jotdb3.jDefaultTemplate;
@@ -71,16 +72,21 @@ public class DefaultTemplatetableModel extends javax.swing.table.AbstractTableMo
 
             // get TreeID that needs 2b refreshed
             // first get all defaulttemplates from the database
-            Vector<jDefaultTemplate> templateList= OtdbRmi.getRemoteOTDB().getDefaultTemplates();
-            Iterator<jDefaultTemplate> it = templateList.iterator();
+            ArrayList<jDefaultTemplate> templateList= new ArrayList(OtdbRmi.getRemoteOTDB().getDefaultTemplates());
             int aTreeID=((Integer)data[row][0]).intValue();
-            jDefaultTemplate aDF=null;
-            while(it.hasNext()) {
-                aDF = it.next();
-                if (aDF.treeID() == aTreeID) break;
+            if (templateList.isEmpty()) {
+                logger.warn("no DefaultTemplates in database");
+                return false;
             }
 
             // if no match, return
+            jDefaultTemplate aDF=null;
+            for (jDefaultTemplate aT : templateList) {
+                if (aT.treeID() == aTreeID) {
+                    aDF=aT;
+                    break;
+                }
+            }
             if (aDF == null) {
                 logger.warn("Couldn't find Matching DefaultTemplate in database");
                 return false;
@@ -92,14 +98,14 @@ public class DefaultTemplatetableModel extends javax.swing.table.AbstractTableMo
                 return false;
             }
             data[row][0]=new Integer(tInfo.treeID());
-            data[row][1]=new String(aDF.name);
-            data[row][2]=new String(OtdbRmi.getTreeState().get(tInfo.state));
-            data[row][3]=new String(tInfo.processType);
-            data[row][4]=new String(tInfo.processSubtype);
-            data[row][5]=new String(tInfo.strategy);
-            data[row][6]=new String(OtdbRmi.getClassif().get(tInfo.classification));
-            data[row][7]=new String(tInfo.campaign);
-            data[row][8]=new String(tInfo.description);
+            data[row][1]=aDF.name;
+            data[row][2]=OtdbRmi.getTreeState().get(tInfo.state);
+            data[row][3]=tInfo.processType;
+            data[row][4]=tInfo.processSubtype;
+            data[row][5]=tInfo.strategy;
+            data[row][6]=OtdbRmi.getClassif().get(tInfo.classification);
+            data[row][7]=tInfo.campaign;
+            data[row][8]=tInfo.description;
             fireTableDataChanged();
         } catch (RemoteException e) {
             logger.debug("Remote OTDB via RMI and JNI failed: " + e);
@@ -119,27 +125,25 @@ public class DefaultTemplatetableModel extends javax.swing.table.AbstractTableMo
                 return false;
             }
             // Get a Treelist of all available VItemplate's
-            Vector<jDefaultTemplate> aTreeList=OtdbRmi.getRemoteOTDB().getDefaultTemplates();
-            Iterator<jDefaultTemplate> it = aTreeList.iterator();
+            ArrayList<jDefaultTemplate> aTreeList=new ArrayList(OtdbRmi.getRemoteOTDB().getDefaultTemplates());
             data = new Object[aTreeList.size()][headers.length];
             logger.debug("DefaultTreelist downloaded. Size: "+aTreeList.size());
             int k=0;
-            while (it.hasNext()) {
-                jDefaultTemplate aDF = it.next();
+            for (jDefaultTemplate aDF : aTreeList) {
                 jOTDBtree tInfo =OtdbRmi.getRemoteOTDB().getTreeInfo(aDF.treeID(), false);
                 if (tInfo.treeID()==0) {
                     logger.warn("Illegal TreeID found!");
                 } else {
                     logger.debug("Gathered info for ID: "+tInfo.treeID());
                     data[k][0]=new Integer(tInfo.treeID());
-                    data[k][1]=new String(aDF.name);
-	            data[k][2]=new String(OtdbRmi.getTreeState().get(tInfo.state));
-                    data[k][3]=new String(tInfo.processType);
-                    data[k][4]=new String(tInfo.processSubtype);
-                    data[k][5]=new String(tInfo.strategy);
-                    data[k][6]=new String(OtdbRmi.getClassif().get(tInfo.classification));
-	            data[k][7]=new String(tInfo.campaign);
-	            data[k][8]=new String(tInfo.description);
+                    data[k][1]=aDF.name;
+	            data[k][2]=OtdbRmi.getTreeState().get(tInfo.state);
+                    data[k][3]=tInfo.processType;
+                    data[k][4]=tInfo.processSubtype;
+                    data[k][5]=tInfo.strategy;
+                    data[k][6]=OtdbRmi.getClassif().get(tInfo.classification);
+	            data[k][7]=tInfo.campaign;
+	            data[k][8]=tInfo.description;
                     k++;
                 }
             }
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/BeamDialog.java b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/BeamDialog.java
index 1060cbae2165fdcfa00f373cfa44183f9ce544a0..f8d77bdf53723d346590c5559874f3928bc098e4 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/BeamDialog.java
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/BeamDialog.java
@@ -53,8 +53,9 @@ public class BeamDialog extends javax.swing.JDialog {
      * @param   selection               Vector of all Beam parameters
      * @param   directionTypeChoices    String with all possible choices + default for combobox
      * @param   edit                    indicates edit or add mode
+     * @param   show                    indicates show only mode
      */
-    public BeamDialog(java.awt.Frame parent, String treeType, boolean modal,BitSet usedBeamlets, Beam aBeam, boolean edit ) {
+    public BeamDialog(java.awt.Frame parent, String treeType, boolean modal,BitSet usedBeamlets, Beam aBeam, boolean edit, boolean show ) {
 
         super(parent, modal);
         initComponents();
@@ -63,6 +64,7 @@ public class BeamDialog extends javax.swing.JDialog {
         itsSavedBeamlets=(BitSet)usedBeamlets.clone();
         itsUsedBeamlets=(BitSet)usedBeamlets.clone();
         editting=edit;
+        showing=show;
         itsTreeType = treeType;
         // need to skip first entry because it is the default (dummy) TBBsetting in other then VHTree's
         if (itsTreeType.equals("VHtree")) {
@@ -157,7 +159,31 @@ public class BeamDialog extends javax.swing.JDialog {
         itsTiedArrayBeams=itsBeam.getTiedArrayBeams();
         itsTiedArrayBeams.trimToSize();
         // fill table with all entries
-        itsTABConfigurationTableModel.fillTable(itsTreeType,itsBeam.getTiedArrayBeams(), true);
+        boolean fillTable = itsTABConfigurationTableModel.fillTable(itsTreeType,itsBeam.getTiedArrayBeams(), true);
+        // if showmode, only view, so disable all buttons
+        if (showing) {
+            enableAll(false);
+            saveButton.setVisible(false);
+            addTiedArrayBeamButton.setVisible(false);
+            editTiedArrayBeamButton.setVisible(false);
+            deleteTiedArrayBeamButton.setVisible(false);
+        }
+    }
+    
+    private void enableAll(boolean flag) {
+        TABConfigurationPanel.setEnabled(flag);
+        inputDirectionTypes.setEnabled(flag);
+        inputAngle1.setEnabled(flag);
+        inputAngle2.setEnabled(flag);
+        coordTypeChange.setEnabled(flag);
+        inputMaxDur.setEnabled(flag);
+        inputSubbandList.setEnabled(flag);
+        inputBeamletList.setEnabled(flag);
+        inputDuration.setEnabled(flag);
+        inputTarget.setEnabled(flag);
+        inputStartTime.setEnabled(flag);
+        inputNrTabRings.setEnabled(flag);
+        inputTabRingSize.setEnabled(flag);
     }
     
     public boolean hasChanged() {
@@ -735,6 +761,7 @@ public class BeamDialog extends javax.swing.JDialog {
     
     private boolean   editTiedArrayBeam = false;
     private boolean   editting          = false;
+    private boolean   showing           = false;
     private BitSet    itsUsedBeamlets   = null;
     private BitSet    itsSavedBeamlets  = null;
     private String    itsTreeType       = null;
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/CreateDefaultTemplateDialog.form b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/CreateDefaultTemplateDialog.form
index bd56e4abb7d81e28a710708641158edb94ddc8b6..85269c318866506307d3ce92ab90a6ce9061750a 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/CreateDefaultTemplateDialog.form
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/CreateDefaultTemplateDialog.form
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="UTF-8" ?>
+<?xml version="1.1" encoding="UTF-8" ?>
 
 <Form version="1.2" maxVersion="1.2" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
   <Properties>
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/CreateDefaultTemplateDialog.java b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/CreateDefaultTemplateDialog.java
index 37ac8f49553a15906f10a887a9b5ca51f72886f6..d94e66c2b686c0fc0cfcedba5bc6f0da7356f251 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/CreateDefaultTemplateDialog.java
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/CreateDefaultTemplateDialog.java
@@ -23,8 +23,7 @@
 package nl.astron.lofar.sas.otbcomponents;
 
 import java.rmi.RemoteException;
-import java.util.Iterator;
-import java.util.Vector;
+import java.util.ArrayList;
 import javax.swing.JOptionPane;
 import nl.astron.lofar.sas.otb.MainFrame;
 import nl.astron.lofar.sas.otb.jotdb3.jDefaultTemplate;
@@ -42,18 +41,18 @@ import nl.astron.lofar.sas.otb.util.OtdbRmi;
 public class CreateDefaultTemplateDialog extends javax.swing.JDialog {
     
     /** Creates new form LoginDialog */
-    public CreateDefaultTemplateDialog(boolean modal, Vector<jDefaultTemplate> aList, jOTDBtree aTree, MainFrame aMainFrame) {
+    public CreateDefaultTemplateDialog(boolean modal, ArrayList<jDefaultTemplate> aList, jOTDBtree aTree, MainFrame aMainFrame) {
         super(aMainFrame, modal);
         initComponents();
         getRootPane().setDefaultButton(jButtonOK);
-        itsDFList=(Vector)aList.clone();
+        itsDFList=(ArrayList)aList.clone();
         itsTree = aTree;
         init();
         ok = true;
     }
 
-    public void setNew(Vector<jDefaultTemplate> aList, jOTDBtree aTree) {
-        itsDFList=(Vector)aList.clone();
+    public void setNew(ArrayList<jDefaultTemplate> aList, jOTDBtree aTree) {
+        itsDFList=(ArrayList)aList.clone();
         itsTree = aTree;
         init();
     }
@@ -174,14 +173,9 @@ public class CreateDefaultTemplateDialog extends javax.swing.JDialog {
             JOptionPane.showMessageDialog(this, "Empty ProcessType Field", "Empty Name error", JOptionPane.ERROR_MESSAGE);
             return false;
         }
-        if (this.processSubtypeInput.getText().equals("")) {
-            JOptionPane.showMessageDialog(this, "Empty ProcessSubtype Field", "Empty Name error", JOptionPane.ERROR_MESSAGE);
-            return false;
-        }
-        Iterator<jDefaultTemplate> it = itsDFList.iterator();
-        while (it.hasNext()) {
-            if (it.next().name.equals(nameInput.getText())) {
-                JOptionPane.showMessageDialog(this, "Empty ProcessType Field", "Empty Name error", JOptionPane.ERROR_MESSAGE);
+        for (jDefaultTemplate it: itsDFList) {
+            if (it.name.equals(nameInput.getText())) {
+                JOptionPane.showMessageDialog(this, "Duplicate ProcessTypename", "Duplicate Name error", JOptionPane.ERROR_MESSAGE);
                 return false;
             }
         }
@@ -231,7 +225,7 @@ public class CreateDefaultTemplateDialog extends javax.swing.JDialog {
      * Holds value of property ok.
      */
     private boolean ok;
-    Vector<jDefaultTemplate> itsDFList;
+    ArrayList<jDefaultTemplate> itsDFList;
     jOTDBtree itsTree;
 
     /**
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TiedArrayBeamDialog.form b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TiedArrayBeamDialog.form
index ad7978e054eda13d190661790334463bc7310f84..4c45370f0f136caccfca3cee67bd66ff78b995f5 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TiedArrayBeamDialog.form
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TiedArrayBeamDialog.form
@@ -51,46 +51,44 @@
       <Layout>
         <DimensionLayout dim="0">
           <Group type="103" groupAlignment="0" attributes="0">
-              <Group type="102" attributes="0">
-                  <EmptySpace max="-2" attributes="0"/>
-                  <Group type="103" groupAlignment="0" attributes="0">
+              <Group type="102" alignment="0" attributes="0">
+                  <Group type="103" groupAlignment="1" attributes="0">
                       <Group type="102" attributes="0">
-                          <Group type="103" groupAlignment="0" attributes="0">
-                              <Component id="labelAngle1" alignment="0" min="-2" max="-2" attributes="1"/>
-                              <Component id="labelDirectionType" alignment="0" min="-2" max="-2" attributes="0"/>
-                              <Component id="labelAngle2" alignment="0" min="-2" pref="52" max="-2" attributes="1"/>
-                          </Group>
-                          <EmptySpace min="-2" pref="20" max="-2" attributes="0"/>
-                          <Group type="103" groupAlignment="0" max="-2" attributes="0">
-                              <Component id="inputDirectionType" alignment="0" min="-2" pref="75" max="-2" attributes="0"/>
-                              <Component id="inputAngle2" alignment="0" pref="185" max="32767" attributes="1"/>
-                              <Component id="inputAngle1" alignment="0" max="32767" attributes="1"/>
-                          </Group>
-                          <EmptySpace min="-2" pref="26" max="-2" attributes="0"/>
-                          <Component id="coordTypeChange" min="-2" max="-2" attributes="0"/>
                           <EmptySpace max="-2" attributes="0"/>
-                      </Group>
-                      <Group type="102" alignment="0" attributes="0">
-                          <Component id="cancelButton" min="-2" max="-2" attributes="0"/>
-                          <EmptySpace max="-2" attributes="0"/>
-                          <Component id="saveButton" min="-2" max="-2" attributes="0"/>
-                          <EmptySpace pref="270" max="32767" attributes="0"/>
-                      </Group>
-                      <Group type="102" attributes="0">
-                          <Component id="labelDispersionMeasure" max="32767" attributes="0"/>
-                          <Group type="103" groupAlignment="0" max="-2" attributes="0">
-                              <Group type="102" attributes="0">
-                                  <EmptySpace min="-2" pref="65" max="-2" attributes="0"/>
-                                  <Component id="inputCoherent" min="-2" max="-2" attributes="0"/>
+                          <Group type="103" groupAlignment="0" attributes="0">
+                              <Group type="102" alignment="0" attributes="0">
+                                  <Component id="cancelButton" min="-2" max="-2" attributes="0"/>
+                                  <EmptySpace max="-2" attributes="0"/>
+                                  <Component id="saveButton" min="-2" max="-2" attributes="0"/>
                               </Group>
-                              <Group type="102" attributes="1">
+                              <Group type="102" attributes="0">
+                                  <Component id="labelDispersionMeasure" min="-2" pref="111" max="-2" attributes="0"/>
                                   <EmptySpace type="separate" max="-2" attributes="0"/>
-                                  <Component id="inputDispersionMeasure" max="32767" attributes="0"/>
+                                  <Component id="inputDispersionMeasure" min="-2" pref="125" max="-2" attributes="0"/>
+                              </Group>
+                              <Group type="102" alignment="0" attributes="0">
+                                  <Group type="103" groupAlignment="0" attributes="0">
+                                      <Component id="labelAngle1" alignment="0" min="-2" max="-2" attributes="1"/>
+                                      <Component id="labelDirectionType" alignment="0" min="-2" max="-2" attributes="0"/>
+                                      <Component id="labelAngle2" alignment="0" min="-2" pref="52" max="-2" attributes="1"/>
+                                  </Group>
+                                  <EmptySpace min="-2" pref="20" max="-2" attributes="0"/>
+                                  <Group type="103" groupAlignment="0" max="-2" attributes="0">
+                                      <Component id="inputDirectionType" alignment="0" min="-2" pref="75" max="-2" attributes="0"/>
+                                      <Component id="inputAngle2" alignment="0" pref="185" max="32767" attributes="1"/>
+                                      <Component id="inputAngle1" alignment="0" max="32767" attributes="1"/>
+                                  </Group>
+                                  <EmptySpace min="-2" pref="26" max="-2" attributes="0"/>
+                                  <Component id="coordTypeChange" min="-2" max="-2" attributes="0"/>
                               </Group>
                           </Group>
-                          <EmptySpace pref="161" max="32767" attributes="0"/>
+                      </Group>
+                      <Group type="102" alignment="0" attributes="0">
+                          <EmptySpace min="-2" pref="135" max="-2" attributes="0"/>
+                          <Component id="inputCoherent" min="-2" pref="145" max="-2" attributes="0"/>
                       </Group>
                   </Group>
+                  <EmptySpace min="-2" pref="168" max="-2" attributes="0"/>
               </Group>
           </Group>
         </DimensionLayout>
@@ -98,7 +96,7 @@
           <Group type="103" groupAlignment="0" attributes="0">
               <Group type="102" attributes="0">
                   <Group type="103" groupAlignment="0" attributes="0">
-                      <Group type="102" attributes="0">
+                      <Group type="102" alignment="0" attributes="0">
                           <Group type="103" groupAlignment="3" attributes="0">
                               <Component id="inputDirectionType" alignment="3" min="-2" max="-2" attributes="0"/>
                               <Component id="labelDirectionType" alignment="3" min="-2" max="-2" attributes="0"/>
@@ -113,18 +111,6 @@
                               <Component id="labelAngle2" alignment="3" min="-2" max="-2" attributes="0"/>
                               <Component id="inputAngle2" alignment="3" min="-2" max="-2" attributes="0"/>
                           </Group>
-                          <EmptySpace min="-2" pref="6" max="-2" attributes="0"/>
-                          <Component id="inputCoherent" min="-2" max="-2" attributes="0"/>
-                          <EmptySpace max="-2" attributes="0"/>
-                          <Group type="103" groupAlignment="3" attributes="0">
-                              <Component id="labelDispersionMeasure" alignment="3" min="-2" max="-2" attributes="0"/>
-                              <Component id="inputDispersionMeasure" alignment="3" min="-2" max="-2" attributes="0"/>
-                          </Group>
-                          <EmptySpace pref="39" max="32767" attributes="0"/>
-                          <Group type="103" groupAlignment="3" attributes="0">
-                              <Component id="cancelButton" alignment="3" min="-2" max="-2" attributes="0"/>
-                              <Component id="saveButton" alignment="3" min="-2" max="-2" attributes="0"/>
-                          </Group>
                       </Group>
                       <Group type="102" alignment="0" attributes="0">
                           <EmptySpace min="-2" pref="35" max="-2" attributes="0"/>
@@ -132,6 +118,18 @@
                       </Group>
                   </Group>
                   <EmptySpace max="-2" attributes="0"/>
+                  <Component id="inputCoherent" min="-2" max="-2" attributes="0"/>
+                  <EmptySpace min="-2" pref="6" max="-2" attributes="0"/>
+                  <Group type="103" groupAlignment="3" attributes="0">
+                      <Component id="labelDispersionMeasure" alignment="3" min="-2" max="-2" attributes="0"/>
+                      <Component id="inputDispersionMeasure" alignment="3" min="-2" max="-2" attributes="0"/>
+                  </Group>
+                  <EmptySpace pref="39" max="32767" attributes="0"/>
+                  <Group type="103" groupAlignment="3" attributes="0">
+                      <Component id="cancelButton" alignment="3" min="-2" max="-2" attributes="0"/>
+                      <Component id="saveButton" alignment="3" min="-2" max="-2" attributes="0"/>
+                  </Group>
+                  <EmptySpace max="-2" attributes="0"/>
               </Group>
           </Group>
         </DimensionLayout>
@@ -210,7 +208,7 @@
         </Component>
         <Component class="javax.swing.JCheckBox" name="inputCoherent">
           <Properties>
-            <Property name="text" type="java.lang.String" value="Maximize  Duration"/>
+            <Property name="text" type="java.lang.String" value="coherent"/>
           </Properties>
         </Component>
         <Component class="javax.swing.JTextField" name="inputDispersionMeasure">
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TiedArrayBeamDialog.java b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TiedArrayBeamDialog.java
index 1d918f13daecec80f0d3037bf50f732f969c3601..92fb8392d68fde2f497a91d672ba026a0caa1d05 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TiedArrayBeamDialog.java
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TiedArrayBeamDialog.java
@@ -155,43 +155,41 @@ public class TiedArrayBeamDialog extends javax.swing.JDialog {
             }
         });
 
-        inputCoherent.setText("Maximize  Duration");
+        inputCoherent.setText("coherent");
 
         org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
         jPanel1.setLayout(jPanel1Layout);
         jPanel1Layout.setHorizontalGroup(
             jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
             .add(jPanel1Layout.createSequentialGroup()
-                .addContainerGap()
-                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
+                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                     .add(jPanel1Layout.createSequentialGroup()
+                        .addContainerGap()
                         .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
-                            .add(labelAngle1)
-                            .add(labelDirectionType)
-                            .add(labelAngle2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
-                        .add(20, 20, 20)
-                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
-                            .add(inputDirectionType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
-                            .add(inputAngle2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
-                            .add(inputAngle1))
-                        .add(26, 26, 26)
-                        .add(coordTypeChange, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
-                        .addContainerGap())
-                    .add(jPanel1Layout.createSequentialGroup()
-                        .add(cancelButton)
-                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
-                        .add(saveButton)
-                        .addContainerGap(270, Short.MAX_VALUE))
-                    .add(jPanel1Layout.createSequentialGroup()
-                        .add(labelDispersionMeasure, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                             .add(jPanel1Layout.createSequentialGroup()
-                                .add(65, 65, 65)
-                                .add(inputCoherent))
+                                .add(cancelButton)
+                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
+                                .add(saveButton))
                             .add(jPanel1Layout.createSequentialGroup()
+                                .add(labelDispersionMeasure, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 111, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                 .add(18, 18, 18)
-                                .add(inputDispersionMeasure)))
-                        .addContainerGap(161, Short.MAX_VALUE))))
+                                .add(inputDispersionMeasure, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 125, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
+                            .add(jPanel1Layout.createSequentialGroup()
+                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
+                                    .add(labelAngle1)
+                                    .add(labelDirectionType)
+                                    .add(labelAngle2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 52, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
+                                .add(20, 20, 20)
+                                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
+                                    .add(inputDirectionType, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
+                                    .add(inputAngle2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
+                                    .add(inputAngle1))
+                                .add(26, 26, 26)
+                                .add(coordTypeChange, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
+                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1Layout.createSequentialGroup()
+                        .add(135, 135, 135)
+                        .add(inputCoherent, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 145, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
+                .add(168, 168, 168))
         );
         jPanel1Layout.setVerticalGroup(
             jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
@@ -208,20 +206,20 @@ public class TiedArrayBeamDialog extends javax.swing.JDialog {
                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                         .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                             .add(labelAngle2)
-                            .add(inputAngle2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
-                        .add(6, 6, 6)
-                        .add(inputCoherent)
-                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
-                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
-                            .add(labelDispersionMeasure)
-                            .add(inputDispersionMeasure, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
-                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 39, Short.MAX_VALUE)
-                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
-                            .add(cancelButton)
-                            .add(saveButton)))
+                            .add(inputAngle2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                     .add(jPanel1Layout.createSequentialGroup()
                         .add(35, 35, 35)
                         .add(coordTypeChange, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
+                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
+                .add(inputCoherent)
+                .add(6, 6, 6)
+                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
+                    .add(labelDispersionMeasure)
+                    .add(inputDispersionMeasure, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
+                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 39, Short.MAX_VALUE)
+                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
+                    .add(cancelButton)
+                    .add(saveButton))
                 .addContainerGap())
         );
 
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TreeInfoDialog.form b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TreeInfoDialog.form
index f17b1dd4c8f9549114ef0fbb1226c78c47b542ab..398c1e3560cc62dd217b5958d08d0e321bdcb697 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TreeInfoDialog.form
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TreeInfoDialog.form
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="UTF-8" ?>
+<?xml version="1.1" encoding="UTF-8" ?>
 
 <Form version="1.2" maxVersion="1.2" type="org.netbeans.modules.form.forminfo.JDialogFormInfo">
   <NonVisualComponents>
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TreeInfoDialog.java b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TreeInfoDialog.java
index 9ca41e48f466a6402e29bc569db1dbcff4f6dcb7..39eb8799cd73a727b3694ed78ceeee08e61be97c 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TreeInfoDialog.java
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/TreeInfoDialog.java
@@ -24,15 +24,17 @@ import com.toedter.components.JSpinField;
 import java.rmi.RemoteException;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.Iterator;
 import java.util.Locale;
 import java.util.TimeZone;
 import java.util.TreeMap;
-import java.util.Vector;
 import javax.swing.DefaultComboBoxModel;
+import javax.swing.Icon;
 import javax.swing.JOptionPane;
+import javax.swing.JTextField;
 import nl.astron.lofar.lofarutils.DateTimeChooser;
 import nl.astron.lofar.lofarutils.LofarUtils;
 import nl.astron.lofar.sas.otb.MainFrame;
@@ -117,10 +119,8 @@ public class TreeInfoDialog extends javax.swing.JDialog {
                     // Get all Beams (if any) from this observation and try to determine the longest duration
                     // try to set the dates
                     itsMaxBeamDuration=0;
-                    Vector<jOTDBnode> beams = OtdbRmi.getRemoteMaintenance().getItemList(itsTree.treeID(), "%.Beam[%.duration");
-                    Iterator<jOTDBnode> itr = beams.iterator();
-                    while (itr.hasNext()){
-                        jOTDBnode aNode=itr.next();
+                    ArrayList<jOTDBnode> beams = new ArrayList(OtdbRmi.getRemoteMaintenance().getItemList(itsTree.treeID(), "%.Beam[%.duration"));
+                    for (jOTDBnode aNode: beams) {
                         try {
                             if (Integer.parseInt(aNode.limits) > itsMaxBeamDuration) {
                                 itsMaxBeamDuration=Integer.parseInt(aNode.limits);
@@ -204,36 +204,36 @@ public class TreeInfoDialog extends javax.swing.JDialog {
     public void composeTimeString(String time) {
         // Set the dateformat OTDB takes
         SimpleDateFormat id = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",itsLocale);
-        if (time.equals("start")) {
-            if (itsStartDate != null) {
-              startTimeInput.setText(id.format(itsStartDate));
-              if (stopTimeInput.getText().equals("") ||
-                      stopTimeInput.getText().equals("not-a-date-time") ||
-                      itsStartDate.after(itsStopDate)) {
-                stopTimeInput.setText(id.format(itsStartDate));
-                itsStopDate=itsStartDate;
-              }
-              itsStarttime=startTimeInput.getText();
-            } else {
-                startTimeInput.setText("not-a-date-time");
-            }
-            
-              
-        } else if (time.equals("stop")) {
-            if (itsStopDate != null) {
-                stopTimeInput.setText(id.format(itsStopDate));
-                itsStoptime=stopTimeInput.getText();
-                saveButton.setEnabled(true);
-            } else {
-                stopTimeInput.setText("not-a-date-time");
-                saveButton.setEnabled(false);
-            }
-
-            if (itsStartDate != null && itsStartDate.after(itsStopDate)) {
-                startTimeInput.setText(id.format(itsStopDate));
-                itsStartDate=itsStopDate;
+        switch (time) {
+            case "start":
+                if (itsStartDate != null) {
+                  startTimeInput.setText(id.format(itsStartDate));
+                  if (stopTimeInput.getText().equals("") ||
+                          stopTimeInput.getText().equals("not-a-date-time") ||
+                          itsStartDate.after(itsStopDate)) {
+                    stopTimeInput.setText(id.format(itsStartDate));
+                    itsStopDate=itsStartDate;
+                  }
+                  itsStarttime=startTimeInput.getText();
+                } else {
+                    startTimeInput.setText("not-a-date-time");
+                }
+                break;
+            case "stop":
+                if (itsStopDate != null) {
+                    stopTimeInput.setText(id.format(itsStopDate));
+                    itsStoptime=stopTimeInput.getText();
+                    saveButton.setEnabled(true);
+                } else {
+                    stopTimeInput.setText("not-a-date-time");
+                    saveButton.setEnabled(false);
+                }
+                if (itsStartDate != null && itsStartDate.after(itsStopDate)) {
+                    startTimeInput.setText(id.format(itsStopDate));
+                    itsStartDate=itsStopDate;
 
-            }
+                }
+                break;
         }
     }
 
@@ -364,106 +364,108 @@ public class TreeInfoDialog extends javax.swing.JDialog {
     }
     
     private void initFocus() {
-        
-            
         // PIC
-        if (itsTreeType.equals("hardware")) {
-            momIDLabel.setVisible(false);
-            momIDInput.setVisible(false);
-            nameLabel.setVisible(false);
-            nameInput.setVisible(false);
-            setNameButton.setVisible(false);
-            originalTreeIDLabel.setVisible(false);
-            originalTreeIDInput.setVisible(false);
-            campaignLabel.setVisible(false);
-            campaignInput.setVisible(false);
-            showCampaignButton.setVisible(false);
-            startTimeLabel.setVisible(false);
-            startTimeInput.setVisible(false);
-            durationLabel.setVisible(false);
-            durationDayLabel.setVisible(false);
-            durationHourLabel.setVisible(false);
-            durationMinuteLabel.setVisible(false);
-            durationSecondLabel.setVisible(false);
-            inputDurationDays.setVisible(false);
-            inputDurationHours.setVisible(false);
-            inputDurationMinutes.setVisible(false);
-            inputDurationSeconds.setVisible(false);
-            setDurationButton.setVisible(false);
-            stopTimeLabel.setVisible(false);
-            stopTimeInput.setVisible(false);
-            setStartDateButton.setVisible(false);
-            setStopDateButton.setVisible(false);
-            descriptionInput.setEnabled(true);
-            // VICtemplate    
-        } else if (itsTreeType.equals("VItemplate")) {
-            campaignLabel.setVisible(false);
-            campaignInput.setVisible(false);        
-            showCampaignButton.setVisible(false);
-            startTimeLabel.setVisible(false);
-            startTimeInput.setVisible(false);
-            durationLabel.setVisible(false);
-            durationDayLabel.setVisible(false);
-            durationHourLabel.setVisible(false);
-            durationMinuteLabel.setVisible(false);
-            durationSecondLabel.setVisible(false);
-            inputDurationDays.setVisible(false);
-            inputDurationHours.setVisible(false);
-            inputDurationMinutes.setVisible(false);
-            inputDurationSeconds.setVisible(false);
-            setDurationButton.setVisible(false);
-            stopTimeLabel.setVisible(false);
-            stopTimeInput.setVisible(false);
-            setStartDateButton.setVisible(false);
-            setStopDateButton.setVisible(false);
-            descriptionInput.setEnabled(true);
-            
-        // VIC
-        } else if (itsTreeType.equals("VHtree")) {
-            nameLabel.setVisible(false);
-            nameInput.setVisible(false);
-            setNameButton.setVisible(false);
-            campaignLabel.setVisible(true);
-            campaignInput.setVisible(true);
-            showCampaignButton.setVisible(true);
-            startTimeLabel.setVisible(true);
-            startTimeInput.setVisible(true);
-            durationLabel.setVisible(true);
-            durationDayLabel.setVisible(true);
-            durationHourLabel.setVisible(true);
-            durationMinuteLabel.setVisible(true);
-            durationSecondLabel.setVisible(true);
-            inputDurationDays.setVisible(true);
-            inputDurationHours.setVisible(true);
-            inputDurationMinutes.setVisible(true);
-            inputDurationSeconds.setVisible(true);
-            setDurationButton.setVisible(true);
-            stopTimeLabel.setVisible(true);
-            stopTimeInput.setVisible(true);
-            setStartDateButton.setVisible(true);
-            setStopDateButton.setVisible(true);
-            if (itsMultiple) {
-                descriptionInput.setEnabled(false);
-                inputDurationDays.setEnabled(false);
-                inputDurationHours.setEnabled(false);
-                inputDurationMinutes.setEnabled(false);
-                inputDurationSeconds.setEnabled(false);
-                setDurationButton.setEnabled(false);
-                setStartDateButton.setEnabled(false);
-                setStopDateButton.setEnabled(false);
-                showCampaignButton.setEnabled(false);
-
-            } else {
+        switch (itsTreeType) {
+            case "hardware":
+                momIDLabel.setVisible(false);
+                momIDInput.setVisible(false);
+                nameLabel.setVisible(false);
+                nameInput.setVisible(false);
+                setNameButton.setVisible(false);
+                originalTreeIDLabel.setVisible(false);
+                originalTreeIDInput.setVisible(false);
+                campaignLabel.setVisible(false);
+                campaignInput.setVisible(false);
+                showCampaignButton.setVisible(false);
+                startTimeLabel.setVisible(false);
+                startTimeInput.setVisible(false);
+                durationLabel.setVisible(false);
+                durationDayLabel.setVisible(false);
+                durationHourLabel.setVisible(false);
+                durationMinuteLabel.setVisible(false);
+                durationSecondLabel.setVisible(false);
+                inputDurationDays.setVisible(false);
+                inputDurationHours.setVisible(false);
+                inputDurationMinutes.setVisible(false);
+                inputDurationSeconds.setVisible(false);
+                setDurationButton.setVisible(false);
+                stopTimeLabel.setVisible(false);
+                stopTimeInput.setVisible(false);
+                setStartDateButton.setVisible(false);
+                setStopDateButton.setVisible(false);
                 descriptionInput.setEnabled(true);
-                inputDurationDays.setEnabled(true);
-                inputDurationHours.setEnabled(true);
-                inputDurationMinutes.setEnabled(true);
-                inputDurationSeconds.setEnabled(true);
-                setDurationButton.setEnabled(true);
-                setStartDateButton.setEnabled(true);
-                setStopDateButton.setEnabled(true);
-                showCampaignButton.setEnabled(true);
-            }
+                // VICtemplate
+                break;
+            case "VItemplate":
+                campaignLabel.setVisible(false);
+                campaignInput.setVisible(false);
+                showCampaignButton.setVisible(false);
+                startTimeLabel.setVisible(false);
+                startTimeInput.setVisible(false);
+                durationLabel.setVisible(false);
+                durationDayLabel.setVisible(false);
+                durationHourLabel.setVisible(false);
+                durationMinuteLabel.setVisible(false);
+                durationSecondLabel.setVisible(false);
+                inputDurationDays.setVisible(false);
+                inputDurationHours.setVisible(false);
+                inputDurationMinutes.setVisible(false);
+                inputDurationSeconds.setVisible(false);
+                setDurationButton.setVisible(false);
+                stopTimeLabel.setVisible(false);
+                stopTimeInput.setVisible(false);
+                setStartDateButton.setVisible(false);
+                setStopDateButton.setVisible(false);
+                descriptionInput.setEnabled(true);
+                
+            // VIC
+                break;
+            case "VHtree":
+                nameLabel.setVisible(false);
+                nameInput.setVisible(false);
+                setNameButton.setVisible(false);
+                campaignLabel.setVisible(true);
+                campaignInput.setVisible(true);
+                showCampaignButton.setVisible(true);
+                startTimeLabel.setVisible(true);
+                startTimeInput.setVisible(true);
+                durationLabel.setVisible(true);
+                durationDayLabel.setVisible(true);
+                durationHourLabel.setVisible(true);
+                durationMinuteLabel.setVisible(true);
+                durationSecondLabel.setVisible(true);
+                inputDurationDays.setVisible(true);
+                inputDurationHours.setVisible(true);
+                inputDurationMinutes.setVisible(true);
+                inputDurationSeconds.setVisible(true);
+                setDurationButton.setVisible(true);
+                stopTimeLabel.setVisible(true);
+                stopTimeInput.setVisible(true);
+                setStartDateButton.setVisible(true);
+                setStopDateButton.setVisible(true);
+                if (itsMultiple) {
+                    descriptionInput.setEnabled(false);
+                    inputDurationDays.setEnabled(false);
+                    inputDurationHours.setEnabled(false);
+                    inputDurationMinutes.setEnabled(false);
+                    inputDurationSeconds.setEnabled(false);
+                    setDurationButton.setEnabled(false);
+                    setStartDateButton.setEnabled(false);
+                    setStopDateButton.setEnabled(false);
+                    showCampaignButton.setEnabled(false);
+
+                } else {
+                    descriptionInput.setEnabled(true);
+                    inputDurationDays.setEnabled(true);
+                    inputDurationHours.setEnabled(true);
+                    inputDurationMinutes.setEnabled(true);
+                    inputDurationSeconds.setEnabled(true);
+                    setDurationButton.setEnabled(true);
+                    setStartDateButton.setEnabled(true);
+                    setStopDateButton.setEnabled(true);
+                    showCampaignButton.setEnabled(true);
+                }
+                break;
         }
         if (isAdministrator) {
             classificationInput.setEnabled(true);
@@ -503,7 +505,7 @@ public class TreeInfoDialog extends javax.swing.JDialog {
         // check if the found tree is a defaulttree
         if (itsTreeType.equals("VItemplate")) {
             try {
-                itsDefaultTemplateList = OtdbRmi.getRemoteOTDB().getDefaultTemplates();
+                itsDefaultTemplateList = new ArrayList(OtdbRmi.getRemoteOTDB().getDefaultTemplates());
                 Iterator<jDefaultTemplate> anI = itsDefaultTemplateList.iterator();
                 while (anI.hasNext()) {
                     // found DefaultTemplate
@@ -1032,7 +1034,7 @@ public class TreeInfoDialog extends javax.swing.JDialog {
         try {
             return aD.parse(aS);
         } catch (ParseException ex) {
-            ex.printStackTrace();
+            logger.error("Parse Exception in time: ", ex);
         }        
         return aGMTDate;
         
@@ -1046,7 +1048,7 @@ public class TreeInfoDialog extends javax.swing.JDialog {
                 SimpleDateFormat aD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", itsLocale);
                 initialDate = aD.parse(aS);
             } catch (ParseException ex) {
-                ex.printStackTrace();
+                logger.error("Parse Exception in time: ", ex);
             }
         }
         DateTimeChooser chooser = new DateTimeChooser(initialDate);
@@ -1067,7 +1069,7 @@ public class TreeInfoDialog extends javax.swing.JDialog {
                 SimpleDateFormat aD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",itsLocale);
                 initialDate = aD.parse(aS);
             } catch (ParseException ex) {
-                ex.printStackTrace();
+                logger.error("Parse Exception in time: ", ex);
             }
         } else {
         }
@@ -1193,19 +1195,30 @@ public class TreeInfoDialog extends javax.swing.JDialog {
     }//GEN-LAST:event_showCampaignButtonMouseClicked
 
     private void setNameButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setNameButtonActionPerformed
-        String aName=JOptionPane.showInputDialog(this, "Give Name for DefaultTree.\n\n !!!!!! Keep in mind that only Default templates who's names are known to MoM can be used by MoM !!!!!!! \n\n","DefaultTree Name", JOptionPane.QUESTION_MESSAGE);
-        if (aName == null || aName.equals("") ){
-            nameInput.setText("");
+        String aName = (String)JOptionPane.showInputDialog(this, 
+                "Give Name for DefaultTree.\n\n !!!!!! Keep in mind that only Default templates who's names are known to MoM can be used by MoM !!!!!!! \n\n",
+                "DefaultTree Name", 
+                JOptionPane.QUESTION_MESSAGE,
+                null,
+                null,
+                nameInput.getText());
+        
+        // cancelled
+        if (aName == null) {
+            return;
+        }
+        
+        if (aName.equals("")) {
+            nameInput.setText("") ;
             return;
         }
 
         if (!aName.equals(nameInput.getText())) {
             boolean found=false;
             try {
-                Vector<jDefaultTemplate> aDFList = OtdbRmi.getRemoteOTDB().getDefaultTemplates();
-                Iterator<jDefaultTemplate> it=aDFList.iterator();
-                while (it.hasNext()) {
-                    if (it.next().name.equals(aName)) {
+                ArrayList<jDefaultTemplate> aDFList = new ArrayList(OtdbRmi.getRemoteOTDB().getDefaultTemplates());
+                for (jDefaultTemplate it: aDFList) {
+                    if (it.name.equals(aName)) {
                         found=true;
                     }
                 }
@@ -1251,7 +1264,7 @@ public class TreeInfoDialog extends javax.swing.JDialog {
     private boolean   isInitialized=false;
     private CampaignInfoDialog campaignInfoDialog=null;
     private int       itsMaxBeamDuration=0;
-    private Vector<jDefaultTemplate> itsDefaultTemplateList=null;
+    private ArrayList<jDefaultTemplate> itsDefaultTemplateList=null;
     
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JTextField campaignInput;
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/ObservationPanel.form b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/ObservationPanel.form
index bdfa841825802989be7df653d4de573904510dac..d9ec7702299457ebad04b6478ae1af65607c29e3 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/ObservationPanel.form
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/ObservationPanel.form
@@ -108,7 +108,7 @@
                                   <Component id="jPanel10" min="-2" max="-2" attributes="1"/>
                               </Group>
                               <Component id="descriptionScrollPane" alignment="0" max="32767" attributes="1"/>
-                              <Component id="jPanel3" alignment="0" pref="1344" max="32767" attributes="1"/>
+                              <Component id="jPanel3" alignment="0" pref="1346" max="32767" attributes="1"/>
                               <Component id="anaBeamConfiguration" alignment="0" max="32767" attributes="1"/>
                               <Component id="treeDescriptionScrollPane" alignment="0" max="32767" attributes="1"/>
                           </Group>
@@ -286,16 +286,18 @@
                       <Group type="103" groupAlignment="0" attributes="0">
                           <Group type="102" attributes="0">
                               <Group type="103" groupAlignment="0" attributes="0">
-                                  <Component id="beamConfigurationPanel" alignment="0" pref="1322" max="32767" attributes="0"/>
+                                  <Component id="beamConfigurationPanel" alignment="0" pref="1324" max="32767" attributes="0"/>
                                   <Group type="102" alignment="0" attributes="0">
                                       <Component id="addBeamButton" min="-2" max="-2" attributes="0"/>
                                       <EmptySpace max="-2" attributes="0"/>
                                       <Component id="editBeamButton" min="-2" max="-2" attributes="0"/>
-                                      <EmptySpace max="-2" attributes="0"/>
+                                      <EmptySpace min="-2" pref="8" max="-2" attributes="0"/>
                                       <Component id="deleteBeamButton" min="-2" max="-2" attributes="0"/>
-                                      <EmptySpace type="unrelated" max="-2" attributes="0"/>
+                                      <EmptySpace max="-2" attributes="0"/>
+                                      <Component id="showBeamButton" min="-2" max="-2" attributes="0"/>
+                                      <EmptySpace max="-2" attributes="0"/>
                                       <Component id="loadBeamsButton" min="-2" max="-2" attributes="0"/>
-                                      <EmptySpace type="separate" max="-2" attributes="0"/>
+                                      <EmptySpace max="-2" attributes="0"/>
                                       <Component id="copyBeamButton" min="-2" pref="155" max="-2" attributes="0"/>
                                   </Group>
                               </Group>
@@ -312,6 +314,7 @@
                                   <Group type="103" groupAlignment="3" attributes="0">
                                       <Component id="editBeamButton" alignment="3" min="-2" max="-2" attributes="0"/>
                                       <Component id="deleteBeamButton" alignment="3" min="-2" max="-2" attributes="0"/>
+                                      <Component id="showBeamButton" alignment="3" min="-2" max="-2" attributes="0"/>
                                       <Component id="loadBeamsButton" alignment="3" min="-2" max="-2" attributes="0"/>
                                       <Component id="copyBeamButton" alignment="3" min="-2" max="-2" attributes="0"/>
                                   </Group>
@@ -351,16 +354,16 @@
                         <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="editBeamButtonActionPerformed"/>
                       </Events>
                     </Component>
-                    <Component class="javax.swing.JButton" name="deleteBeamButton">
+                    <Component class="javax.swing.JButton" name="showBeamButton">
                       <Properties>
                         <Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
-                          <Image iconType="3" name="/nl/astron/lofar/sas/otb/icons/16_delete.png"/>
+                          <Image iconType="3" name="/nl/astron/lofar/sas/otb/icons/16_info.gif"/>
                         </Property>
-                        <Property name="text" type="java.lang.String" value="delete beam"/>
+                        <Property name="text" type="java.lang.String" value="show beam"/>
                         <Property name="enabled" type="boolean" value="false"/>
                       </Properties>
                       <Events>
-                        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="deleteBeamButtonActionPerformed"/>
+                        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="showBeamButtonActionPerformed"/>
                       </Events>
                     </Component>
                     <Component class="javax.swing.JButton" name="loadBeamsButton">
@@ -388,6 +391,18 @@
                         <AuxValue name="JavaCodeGenerator_InitCodePost" type="java.lang.String" value="this.setVisible(false);"/>
                       </AuxValues>
                     </Component>
+                    <Component class="javax.swing.JButton" name="deleteBeamButton">
+                      <Properties>
+                        <Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
+                          <Image iconType="3" name="/nl/astron/lofar/sas/otb/icons/16_delete.png"/>
+                        </Property>
+                        <Property name="text" type="java.lang.String" value="delete beam"/>
+                        <Property name="enabled" type="boolean" value="false"/>
+                      </Properties>
+                      <Events>
+                        <EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="deleteBeamButtonActionPerformed"/>
+                      </Events>
+                    </Component>
                   </SubComponents>
                 </Container>
                 <Container class="javax.swing.JPanel" name="jPanel5">
@@ -660,7 +675,7 @@
                               <Component id="deleteAnaBeamButton" min="-2" max="-2" attributes="0"/>
                               <EmptySpace type="unrelated" max="-2" attributes="0"/>
                               <Component id="loadAnaBeamsButton" min="-2" max="-2" attributes="0"/>
-                              <EmptySpace min="-2" pref="839" max="-2" attributes="0"/>
+                              <EmptySpace min="-2" pref="908" max="-2" attributes="0"/>
                           </Group>
                       </Group>
                     </DimensionLayout>
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/ObservationPanel.java b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/ObservationPanel.java
index 38ef70d04115983e697fc891e8a41adffe668247..8e699e316a91463b52b547d160b68b758093efc2 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/ObservationPanel.java
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/ObservationPanel.java
@@ -34,7 +34,6 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.BitSet;
 import java.util.Collection;
-import java.util.Enumeration;
 import java.util.Vector;
 import javax.swing.DefaultListModel;
 import javax.swing.JFileChooser;
@@ -873,17 +872,16 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
      * @param   enabled     true/false enabled/disabled
      */
     public void enableButtons(boolean enabled) {
-        loadBeamsButton.setEnabled(enabled);
-        loadAnaBeamsButton.setEnabled(enabled);
         addBeamButton.setEnabled(enabled);
         editBeamButton.setEnabled(enabled);
-        copyBeamButton.setEnabled(enabled);
-        loadAnaBeamsButton.setEnabled(enabled);
-        loadBeamsButton.setEnabled(enabled);
         deleteBeamButton.setEnabled(enabled);
+        loadBeamsButton.setEnabled(enabled);
+        copyBeamButton.setEnabled(enabled);
+        
         addAnaBeamButton.setEnabled(enabled);
         editAnaBeamButton.setEnabled(enabled);
         deleteAnaBeamButton.setEnabled(enabled);
+        loadAnaBeamsButton.setEnabled(enabled);
     }
     
     /** Sets the buttons visible/invisible
@@ -892,17 +890,16 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
      */
     @Override
     public void setButtonsVisible(boolean visible) {
-        loadBeamsButton.setVisible(visible);
-        loadAnaBeamsButton.setVisible(visible);
         addBeamButton.setVisible(visible);
         editBeamButton.setVisible(visible);
-        copyBeamButton.setVisible(visible);
-        loadAnaBeamsButton.setVisible(visible);
-        loadBeamsButton.setVisible(visible);
         deleteBeamButton.setVisible(visible);
+        loadBeamsButton.setVisible(visible);
+        copyBeamButton.setVisible(visible);
+
         addAnaBeamButton.setVisible(visible);
         editAnaBeamButton.setVisible(visible);
         deleteAnaBeamButton.setVisible(visible);
+        loadAnaBeamsButton.setVisible(visible);
     }        
     
     /** Enables/disables the complete form
@@ -1464,6 +1461,7 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
                 this.editBeamButton.setEnabled(false);
                 this.deleteBeamButton.setEnabled(false);
                 this.copyBeamButton.setEnabled(false);
+                this.showBeamButton.setEnabled(false);
 
 
             }
@@ -1510,11 +1508,15 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
             BitSet oldBeamlets = selection.getBeamletBitSet();
             aBS.xor(oldBeamlets);
         }
-        beamDialog = new BeamDialog(itsMainFrame,itsTreeType,true,aBS,selection.clone(),editBeam);
+        beamDialog = new BeamDialog(itsMainFrame,itsTreeType,true,aBS,selection.clone(),editBeam,showBeam);
         
         beamDialog.setLocationRelativeTo(this);
         if (editBeam) {
-            beamDialog.setBorderTitle("edit Beam");
+            if (showBeam) {
+                beamDialog.setBorderTitle("show Beam");                
+            } else {
+                beamDialog.setBorderTitle("edit Beam");
+            }
         } else {
             beamDialog.setBorderTitle("add new Beam");            
         }
@@ -1538,6 +1540,7 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
         this.editBeamButton.setEnabled(false);
         this.deleteBeamButton.setEnabled(false);
         this.copyBeamButton.setEnabled(false);
+        this.showBeamButton.setEnabled(false);
         if (beamConfigurationPanel.getTableModel().getRowCount() == 244 ) {
             this.addBeamButton.setEnabled(false);
         } else {
@@ -2042,7 +2045,9 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
 
     private void setAnaBeamConfiguration(boolean flag) {
         this.anaBeamConfiguration.setVisible(flag);
-        this.copyBeamButton.setVisible(flag);
+        if (addBeamButton.isVisible()) {
+            this.copyBeamButton.setVisible(flag);
+        }
    }
 
     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
@@ -2067,9 +2072,10 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
         beamConfigurationPanel = new nl.astron.lofar.sas.otbcomponents.TablePanel();
         addBeamButton = new javax.swing.JButton();
         editBeamButton = new javax.swing.JButton();
-        deleteBeamButton = new javax.swing.JButton();
+        showBeamButton = new javax.swing.JButton();
         loadBeamsButton = new javax.swing.JButton();
         copyBeamButton = new javax.swing.JButton();
+        deleteBeamButton = new javax.swing.JButton();
         jPanel5 = new javax.swing.JPanel();
         stationsPanel = new javax.swing.JPanel();
         stationsScrollPane = new javax.swing.JScrollPane();
@@ -2209,12 +2215,12 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
             }
         });
 
-        deleteBeamButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_delete.png"))); // NOI18N
-        deleteBeamButton.setText("delete beam");
-        deleteBeamButton.setEnabled(false);
-        deleteBeamButton.addActionListener(new java.awt.event.ActionListener() {
+        showBeamButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_info.gif"))); // NOI18N
+        showBeamButton.setText("show beam");
+        showBeamButton.setEnabled(false);
+        showBeamButton.addActionListener(new java.awt.event.ActionListener() {
             public void actionPerformed(java.awt.event.ActionEvent evt) {
-                deleteBeamButtonActionPerformed(evt);
+                showBeamButtonActionPerformed(evt);
             }
         });
 
@@ -2237,22 +2243,33 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
             }
         });
 
+        deleteBeamButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_delete.png"))); // NOI18N
+        deleteBeamButton.setText("delete beam");
+        deleteBeamButton.setEnabled(false);
+        deleteBeamButton.addActionListener(new java.awt.event.ActionListener() {
+            public void actionPerformed(java.awt.event.ActionEvent evt) {
+                deleteBeamButtonActionPerformed(evt);
+            }
+        });
+
         org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
         jPanel3.setLayout(jPanel3Layout);
         jPanel3Layout.setHorizontalGroup(
             jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
             .add(jPanel3Layout.createSequentialGroup()
                 .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
-                    .add(beamConfigurationPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1255, Short.MAX_VALUE)
+                    .add(beamConfigurationPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1324, Short.MAX_VALUE)
                     .add(jPanel3Layout.createSequentialGroup()
                         .add(addBeamButton)
                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                         .add(editBeamButton)
-                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
+                        .add(8, 8, 8)
                         .add(deleteBeamButton)
-                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
+                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
+                        .add(showBeamButton)
+                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                         .add(loadBeamsButton)
-                        .add(18, 18, 18)
+                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                         .add(copyBeamButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 155, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                 .addContainerGap())
         );
@@ -2265,6 +2282,7 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
                     .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                         .add(editBeamButton)
                         .add(deleteBeamButton)
+                        .add(showBeamButton)
                         .add(loadBeamsButton)
                         .add(copyBeamButton))
                     .add(addBeamButton))
@@ -2452,7 +2470,7 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
         anaBeamConfigurationLayout.setHorizontalGroup(
             anaBeamConfigurationLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
             .add(anaBeamConfigurationLayout.createSequentialGroup()
-                .add(anaBeamConfigurationPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1163, Short.MAX_VALUE)
+                .add(anaBeamConfigurationPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1232, Short.MAX_VALUE)
                 .add(102, 102, 102))
             .add(anaBeamConfigurationLayout.createSequentialGroup()
                 .add(addAnaBeamButton)
@@ -2462,7 +2480,7 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
                 .add(deleteAnaBeamButton)
                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                 .add(loadAnaBeamsButton)
-                .add(839, 839, 839))
+                .add(908, 908, 908))
         );
         anaBeamConfigurationLayout.setVerticalGroup(
             anaBeamConfigurationLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
@@ -2490,7 +2508,7 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
                         .add(30, 30, 30)
                         .add(jPanel10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                     .add(descriptionScrollPane)
-                    .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1277, Short.MAX_VALUE)
+                    .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 1346, Short.MAX_VALUE)
                     .add(anaBeamConfiguration, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                     .add(treeDescriptionScrollPane))
                 .addContainerGap(30, Short.MAX_VALUE))
@@ -2532,24 +2550,30 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
         add(buttonPanel1, java.awt.BorderLayout.SOUTH);
     }// </editor-fold>//GEN-END:initComponents
 
-    private void deleteBeamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteBeamButtonActionPerformed
-        deleteBeam();
-    }//GEN-LAST:event_deleteBeamButtonActionPerformed
+    private void showBeamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showBeamButtonActionPerformed
+        editBeam = true;
+        showBeam = true;
+        addBeam();
+    }//GEN-LAST:event_showBeamButtonActionPerformed
 
     private void editBeamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editBeamButtonActionPerformed
         editBeam=true;
+        showBeam = false;
         addBeam();
     }//GEN-LAST:event_editBeamButtonActionPerformed
 
     private void addBeamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addBeamButtonActionPerformed
         editBeam=false;
+        showBeam = false;
         addBeam();
     }//GEN-LAST:event_addBeamButtonActionPerformed
 
     private void beamConfigurationPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_beamConfigurationPanelMouseClicked
         editBeamButton.setEnabled(true);
-        deleteBeamButton.setEnabled(true);
+        showBeamButton.setEnabled(true);
         copyBeamButton.setEnabled(true);
+        deleteBeamButton.setEnabled(true);
+        showBeamButton.setEnabled(true);
     }//GEN-LAST:event_beamConfigurationPanelMouseClicked
 
     private void buttonPanel1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPanel1ActionPerformed
@@ -2674,6 +2698,10 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
            buttonPanel1.setButtonEnabled("Apply", false);
        }
     }//GEN-LAST:event_antennaConfigPanelActionPerformed
+
+    private void deleteBeamButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteBeamButtonActionPerformed
+        // TODO add your handling code here:
+    }//GEN-LAST:event_deleteBeamButtonActionPerformed
     
     private jOTDBnode                         itsNode = null;
     private MainFrame                         itsMainFrame;
@@ -2726,11 +2754,10 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
     private BitSet   itsUsedBeamlets = new BitSet(216);
     private boolean  editBeam = false;
     private boolean  editAnaBeam = false;
+    private boolean  showBeam = false;
     private int      itsSelectedRow = -1;
 
 
-    // Temp
-    private Vector<String>    itsUsedStorageNodes      = new Vector<>();
 
 
 
@@ -2775,6 +2802,7 @@ public class ObservationPanel extends javax.swing.JPanel implements IViewPanel{
     private javax.swing.JLabel labelNrSlotsInFrame;
     private javax.swing.JButton loadAnaBeamsButton;
     private javax.swing.JButton loadBeamsButton;
+    private javax.swing.JButton showBeamButton;
     private javax.swing.JList stationList;
     private javax.swing.JPanel stationsButtonPanel;
     private javax.swing.JPanel stationsModPanel;
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/OlapPanel.form b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/OlapPanel.form
index cdcea94fb65218fde60dfd6f575066792866ae17..c79746fbf5ab041e63b7ee3ea16b97cd4eb76ace 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/OlapPanel.form
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/OlapPanel.form
@@ -33,7 +33,7 @@
     <AuxValue name="FormSettings_listenerGenerationStyle" type="java.lang.Integer" value="0"/>
     <AuxValue name="FormSettings_variablesLocal" type="java.lang.Boolean" value="false"/>
     <AuxValue name="FormSettings_variablesModifier" type="java.lang.Integer" value="2"/>
-    <AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,2,88,0,0,3,-51"/>
+    <AuxValue name="designerSize" type="java.awt.Dimension" value="-84,-19,0,5,115,114,0,18,106,97,118,97,46,97,119,116,46,68,105,109,101,110,115,105,111,110,65,-114,-39,-41,-84,95,68,20,2,0,2,73,0,6,104,101,105,103,104,116,73,0,5,119,105,100,116,104,120,112,0,0,2,-61,0,0,4,61"/>
   </AuxValues>
 
   <Layout class="org.netbeans.modules.form.compat2.layouts.DesignBorderLayout"/>
@@ -100,15 +100,15 @@
                   <Group type="102" alignment="0" attributes="0">
                       <EmptySpace max="-2" attributes="0"/>
                       <Component id="jPanel4" min="-2" max="-2" attributes="2"/>
-                      <EmptySpace pref="4293" max="32767" attributes="0"/>
+                      <EmptySpace pref="4261" max="32767" attributes="0"/>
                   </Group>
               </Group>
             </DimensionLayout>
             <DimensionLayout dim="1">
               <Group type="103" groupAlignment="0" attributes="0">
                   <Group type="102" alignment="0" attributes="0">
-                      <Component id="jPanel4" min="-2" max="-2" attributes="0"/>
-                      <EmptySpace pref="2564" max="32767" attributes="0"/>
+                      <Component id="jPanel4" min="-2" pref="540" max="-2" attributes="0"/>
+                      <EmptySpace pref="2455" max="32767" attributes="0"/>
                   </Group>
               </Group>
             </DimensionLayout>
@@ -119,28 +119,20 @@
               <Layout>
                 <DimensionLayout dim="0">
                   <Group type="103" groupAlignment="0" attributes="0">
-                      <Group type="102" alignment="0" attributes="0">
-                          <Group type="103" groupAlignment="1" max="-2" attributes="0">
-                              <Component id="jPanel8" alignment="0" max="32767" attributes="1"/>
-                              <Group type="102" alignment="0" attributes="0">
+                      <Group type="102" attributes="0">
+                          <Group type="103" groupAlignment="0" attributes="0">
+                              <Group type="102" attributes="0">
+                                  <EmptySpace max="-2" attributes="0"/>
+                                  <Component id="jPanel7" min="-2" max="-2" attributes="1"/>
                                   <EmptySpace max="-2" attributes="0"/>
                                   <Group type="103" groupAlignment="0" attributes="0">
-                                      <Component id="jPanel11" max="32767" attributes="1"/>
-                                      <Component id="jPanel7" alignment="0" min="-2" max="-2" attributes="1"/>
-                                  </Group>
-                                  <EmptySpace type="separate" max="-2" attributes="0"/>
-                                  <Group type="103" groupAlignment="0" max="-2" attributes="0">
-                                      <Component id="PencilInfoPanel" max="32767" attributes="1"/>
-                                      <Component id="CorrelatorPanel" alignment="0" max="32767" attributes="1"/>
-                                  </Group>
-                                  <EmptySpace type="separate" max="-2" attributes="0"/>
-                                  <Group type="103" groupAlignment="0" max="-2" attributes="0">
-                                      <Component id="IncoherentStokesPanel" max="32767" attributes="1"/>
-                                      <Component id="CoherentStokesPanel" alignment="0" max="32767" attributes="1"/>
+                                      <Component id="CorrelatorPanel" min="-2" max="-2" attributes="1"/>
+                                      <Component id="PencilInfoPanel" alignment="0" min="-2" max="-2" attributes="1"/>
                                   </Group>
                               </Group>
+                              <Component id="jPanel8" alignment="0" min="-2" max="-2" attributes="1"/>
                           </Group>
-                          <EmptySpace pref="25" max="32767" attributes="0"/>
+                          <EmptySpace pref="42" max="32767" attributes="0"/>
                       </Group>
                   </Group>
                 </DimensionLayout>
@@ -149,22 +141,19 @@
                       <Group type="102" attributes="0">
                           <EmptySpace max="-2" attributes="0"/>
                           <Component id="jPanel8" min="-2" max="-2" attributes="1"/>
-                          <EmptySpace max="-2" attributes="0"/>
                           <Group type="103" groupAlignment="0" attributes="0">
                               <Group type="102" attributes="0">
-                                  <EmptySpace pref="6" max="32767" attributes="0"/>
-                                  <Component id="CoherentStokesPanel" min="-2" max="-2" attributes="1"/>
+                                  <EmptySpace min="-2" pref="10" max="-2" attributes="0"/>
+                                  <Component id="CorrelatorPanel" min="-2" max="-2" attributes="1"/>
+                                  <EmptySpace type="unrelated" max="-2" attributes="0"/>
+                                  <Component id="PencilInfoPanel" min="-2" max="-2" attributes="1"/>
+                              </Group>
+                              <Group type="102" alignment="0" attributes="0">
+                                  <EmptySpace max="-2" attributes="0"/>
+                                  <Component id="jPanel7" min="-2" max="-2" attributes="1"/>
                               </Group>
-                              <Component id="CorrelatorPanel" max="32767" attributes="1"/>
-                              <Component id="jPanel7" alignment="1" min="-2" max="-2" attributes="1"/>
-                          </Group>
-                          <EmptySpace type="unrelated" max="-2" attributes="0"/>
-                          <Group type="103" groupAlignment="0" attributes="0">
-                              <Component id="IncoherentStokesPanel" alignment="0" max="32767" attributes="1"/>
-                              <Component id="PencilInfoPanel" alignment="0" max="32767" attributes="1"/>
-                              <Component id="jPanel11" alignment="0" max="32767" attributes="1"/>
                           </Group>
-                          <EmptySpace min="-2" pref="24" max="-2" attributes="0"/>
+                          <EmptySpace min="-2" pref="39" max="-2" attributes="0"/>
                       </Group>
                   </Group>
                 </DimensionLayout>
@@ -242,7 +231,7 @@
                                       <Component id="jLabel3" min="-2" max="-2" attributes="0"/>
                                   </Group>
                               </Group>
-                              <EmptySpace pref="127" max="32767" attributes="0"/>
+                              <EmptySpace pref="142" max="32767" attributes="0"/>
                           </Group>
                       </Group>
                     </DimensionLayout>
@@ -451,10 +440,16 @@
                     <DimensionLayout dim="0">
                       <Group type="103" groupAlignment="0" attributes="0">
                           <Group type="102" attributes="0">
-                              <Component id="labelNrPPFTaps" min="-2" max="-2" attributes="1"/>
+                              <Group type="103" groupAlignment="0" attributes="0">
+                                  <Group type="102" attributes="0">
+                                      <Component id="labelNrPPFTaps" min="-2" max="-2" attributes="1"/>
+                                      <EmptySpace max="-2" attributes="0"/>
+                                      <Component id="inputNrPPFTaps" min="-2" pref="154" max="-2" attributes="1"/>
+                                  </Group>
+                                  <Component id="CoherentStokesPanel" alignment="0" max="32767" attributes="1"/>
+                                  <Component id="IncoherentStokesPanel" alignment="1" max="32767" attributes="1"/>
+                              </Group>
                               <EmptySpace max="-2" attributes="0"/>
-                              <Component id="inputNrPPFTaps" min="-2" pref="154" max="-2" attributes="1"/>
-                              <EmptySpace pref="47" max="32767" attributes="0"/>
                           </Group>
                       </Group>
                     </DimensionLayout>
@@ -465,7 +460,11 @@
                                   <Component id="labelNrPPFTaps" alignment="3" min="-2" max="-2" attributes="0"/>
                                   <Component id="inputNrPPFTaps" alignment="3" min="-2" max="-2" attributes="0"/>
                               </Group>
-                              <EmptySpace pref="59" max="32767" attributes="0"/>
+                              <EmptySpace min="-2" pref="26" max="-2" attributes="0"/>
+                              <Component id="CoherentStokesPanel" min="-2" max="-2" attributes="1"/>
+                              <EmptySpace type="unrelated" max="-2" attributes="0"/>
+                              <Component id="IncoherentStokesPanel" max="32767" attributes="1"/>
+                              <EmptySpace max="-2" attributes="0"/>
                           </Group>
                       </Group>
                     </DimensionLayout>
@@ -478,50 +477,195 @@
                     </Component>
                     <Component class="javax.swing.JTextField" name="inputNrPPFTaps">
                     </Component>
-                  </SubComponents>
-                </Container>
-                <Container class="javax.swing.JPanel" name="jPanel11">
-                  <Properties>
-                    <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
-                      <Border info="org.netbeans.modules.form.compat2.border.TitledBorderInfo">
-                        <TitledBorder title="Storage Proc">
-                          <Font PropertyName="font" name="Tahoma" size="11" style="1"/>
-                        </TitledBorder>
-                      </Border>
-                    </Property>
-                  </Properties>
+                    <Container class="javax.swing.JPanel" name="CoherentStokesPanel">
+                      <Properties>
+                        <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
+                          <Border info="org.netbeans.modules.form.compat2.border.TitledBorderInfo">
+                            <TitledBorder title="Coherent Stokes">
+                              <Font PropertyName="font" name="Tahoma" size="11" style="1"/>
+                            </TitledBorder>
+                          </Border>
+                        </Property>
+                        <Property name="toolTipText" type="java.lang.String" value="Stokes"/>
+                      </Properties>
 
-                  <Layout>
-                    <DimensionLayout dim="0">
-                      <Group type="103" groupAlignment="0" attributes="0">
-                          <Group type="102" attributes="0">
-                              <Component id="labelSubbandsPerMs" min="-2" max="-2" attributes="1"/>
-                              <EmptySpace type="separate" max="-2" attributes="0"/>
-                              <Component id="inputSubbandsPerMS" min="-2" pref="125" max="-2" attributes="1"/>
-                              <EmptySpace pref="24" max="32767" attributes="0"/>
+                      <Layout>
+                        <DimensionLayout dim="0">
+                          <Group type="103" groupAlignment="0" attributes="0">
+                              <Group type="102" attributes="0">
+                                  <Group type="103" groupAlignment="0" attributes="0">
+                                      <Component id="labelChannelsPerSubbandCoherent" alignment="0" pref="141" max="32767" attributes="1"/>
+                                      <Component id="timeIntegrationFactorCoherent" pref="141" max="32767" attributes="1"/>
+                                      <Component id="labelWhichCoherent" min="-2" pref="103" max="-2" attributes="1"/>
+                                  </Group>
+                                  <EmptySpace max="-2" attributes="0"/>
+                                  <Group type="103" groupAlignment="1" max="-2" attributes="0">
+                                      <Component id="inputChannelsPerSubbandCoherent" max="32767" attributes="1"/>
+                                      <Component id="inputTimeIntegrationFactorCoherent" alignment="0" max="32767" attributes="1"/>
+                                      <Component id="inputWhichCoherent" alignment="1" pref="173" max="32767" attributes="1"/>
+                                  </Group>
+                                  <EmptySpace max="-2" attributes="0"/>
+                              </Group>
                           </Group>
-                      </Group>
-                    </DimensionLayout>
-                    <DimensionLayout dim="1">
-                      <Group type="103" groupAlignment="0" attributes="0">
-                          <Group type="102" attributes="0">
-                              <Group type="103" alignment="0" groupAlignment="3" attributes="0">
-                                  <Component id="labelSubbandsPerMs" alignment="3" min="-2" max="-2" attributes="0"/>
-                                  <Component id="inputSubbandsPerMS" alignment="3" min="-2" max="-2" attributes="0"/>
+                        </DimensionLayout>
+                        <DimensionLayout dim="1">
+                          <Group type="103" groupAlignment="0" attributes="0">
+                              <Group type="102" attributes="0">
+                                  <Group type="103" groupAlignment="3" attributes="0">
+                                      <Component id="inputWhichCoherent" alignment="3" min="-2" max="-2" attributes="0"/>
+                                      <Component id="labelWhichCoherent" alignment="3" min="-2" pref="14" max="-2" attributes="0"/>
+                                  </Group>
+                                  <EmptySpace min="-2" pref="8" max="-2" attributes="0"/>
+                                  <Group type="103" groupAlignment="3" attributes="0">
+                                      <Component id="inputTimeIntegrationFactorCoherent" alignment="3" min="-2" max="-2" attributes="0"/>
+                                      <Component id="timeIntegrationFactorCoherent" alignment="3" min="-2" max="-2" attributes="0"/>
+                                  </Group>
+                                  <EmptySpace max="-2" attributes="0"/>
+                                  <Group type="103" groupAlignment="3" attributes="0">
+                                      <Component id="inputChannelsPerSubbandCoherent" alignment="3" min="-2" max="-2" attributes="0"/>
+                                      <Component id="labelChannelsPerSubbandCoherent" alignment="3" min="-2" max="-2" attributes="0"/>
+                                  </Group>
+                                  <EmptySpace max="32767" attributes="0"/>
                               </Group>
-                              <EmptySpace pref="52" max="32767" attributes="0"/>
                           </Group>
-                      </Group>
-                    </DimensionLayout>
-                  </Layout>
-                  <SubComponents>
-                    <Component class="javax.swing.JLabel" name="labelSubbandsPerMs">
+                        </DimensionLayout>
+                      </Layout>
+                      <SubComponents>
+                        <Component class="javax.swing.JLabel" name="labelWhichCoherent">
+                          <Properties>
+                            <Property name="text" type="java.lang.String" value="which"/>
+                          </Properties>
+                        </Component>
+                        <Component class="javax.swing.JComboBox" name="inputWhichCoherent">
+                          <Properties>
+                            <Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
+                              <StringArray count="4">
+                                <StringItem index="0" value="Item 1"/>
+                                <StringItem index="1" value="Item 2"/>
+                                <StringItem index="2" value="Item 3"/>
+                                <StringItem index="3" value="Item 4"/>
+                              </StringArray>
+                            </Property>
+                            <Property name="enabled" type="boolean" value="false"/>
+                          </Properties>
+                        </Component>
+                        <Component class="javax.swing.JLabel" name="timeIntegrationFactorCoherent">
+                          <Properties>
+                            <Property name="text" type="java.lang.String" value="time integration factor"/>
+                          </Properties>
+                        </Component>
+                        <Component class="javax.swing.JTextField" name="inputTimeIntegrationFactorCoherent">
+                          <Properties>
+                            <Property name="enabled" type="boolean" value="false"/>
+                          </Properties>
+                        </Component>
+                        <Component class="javax.swing.JLabel" name="labelChannelsPerSubbandCoherent">
+                          <Properties>
+                            <Property name="text" type="java.lang.String" value="# channels per subband"/>
+                          </Properties>
+                        </Component>
+                        <Component class="javax.swing.JTextField" name="inputChannelsPerSubbandCoherent">
+                          <Properties>
+                            <Property name="enabled" type="boolean" value="false"/>
+                          </Properties>
+                        </Component>
+                      </SubComponents>
+                    </Container>
+                    <Container class="javax.swing.JPanel" name="IncoherentStokesPanel">
                       <Properties>
-                        <Property name="text" type="java.lang.String" value="#subbands per MS:"/>
+                        <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
+                          <Border info="org.netbeans.modules.form.compat2.border.TitledBorderInfo">
+                            <TitledBorder title="Incoherent Stokes">
+                              <Font PropertyName="font" name="Tahoma" size="11" style="1"/>
+                            </TitledBorder>
+                          </Border>
+                        </Property>
+                        <Property name="toolTipText" type="java.lang.String" value="Stokes"/>
                       </Properties>
-                    </Component>
-                    <Component class="javax.swing.JTextField" name="inputSubbandsPerMS">
-                    </Component>
+
+                      <Layout>
+                        <DimensionLayout dim="0">
+                          <Group type="103" groupAlignment="0" attributes="0">
+                              <Group type="102" attributes="0">
+                                  <EmptySpace max="-2" attributes="0"/>
+                                  <Group type="103" groupAlignment="0" attributes="0">
+                                      <Component id="labelChannelsPerSubbandIncoherent" alignment="0" pref="131" max="32767" attributes="1"/>
+                                      <Component id="labelTimeIntegrationfactorCoherent" pref="131" max="32767" attributes="1"/>
+                                      <Component id="labelWhichIncoherent" alignment="0" pref="131" max="32767" attributes="1"/>
+                                  </Group>
+                                  <EmptySpace max="-2" attributes="0"/>
+                                  <Group type="103" groupAlignment="1" max="-2" attributes="0">
+                                      <Component id="inputChannelsPerSubbandIncoherent" max="32767" attributes="1"/>
+                                      <Component id="inputTimeIntegrationFactorIncoherent" alignment="0" max="32767" attributes="1"/>
+                                      <Component id="inputWhichIncoherent" alignment="1" pref="173" max="32767" attributes="1"/>
+                                  </Group>
+                                  <EmptySpace max="-2" attributes="0"/>
+                              </Group>
+                          </Group>
+                        </DimensionLayout>
+                        <DimensionLayout dim="1">
+                          <Group type="103" groupAlignment="0" attributes="0">
+                              <Group type="102" attributes="0">
+                                  <Group type="103" groupAlignment="3" attributes="0">
+                                      <Component id="inputWhichIncoherent" alignment="3" min="-2" max="-2" attributes="0"/>
+                                      <Component id="labelWhichIncoherent" alignment="3" min="-2" max="-2" attributes="0"/>
+                                  </Group>
+                                  <EmptySpace min="-2" pref="8" max="-2" attributes="0"/>
+                                  <Group type="103" groupAlignment="3" attributes="0">
+                                      <Component id="inputTimeIntegrationFactorIncoherent" alignment="3" min="-2" max="-2" attributes="0"/>
+                                      <Component id="labelTimeIntegrationfactorCoherent" alignment="3" min="-2" max="-2" attributes="0"/>
+                                  </Group>
+                                  <EmptySpace type="unrelated" max="-2" attributes="0"/>
+                                  <Group type="103" groupAlignment="3" attributes="0">
+                                      <Component id="inputChannelsPerSubbandIncoherent" alignment="3" min="-2" max="-2" attributes="0"/>
+                                      <Component id="labelChannelsPerSubbandIncoherent" alignment="3" min="-2" max="-2" attributes="0"/>
+                                  </Group>
+                                  <EmptySpace max="32767" attributes="0"/>
+                              </Group>
+                          </Group>
+                        </DimensionLayout>
+                      </Layout>
+                      <SubComponents>
+                        <Component class="javax.swing.JLabel" name="labelWhichIncoherent">
+                          <Properties>
+                            <Property name="text" type="java.lang.String" value="which"/>
+                          </Properties>
+                        </Component>
+                        <Component class="javax.swing.JComboBox" name="inputWhichIncoherent">
+                          <Properties>
+                            <Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
+                              <StringArray count="4">
+                                <StringItem index="0" value="Item 1"/>
+                                <StringItem index="1" value="Item 2"/>
+                                <StringItem index="2" value="Item 3"/>
+                                <StringItem index="3" value="Item 4"/>
+                              </StringArray>
+                            </Property>
+                            <Property name="enabled" type="boolean" value="false"/>
+                          </Properties>
+                        </Component>
+                        <Component class="javax.swing.JLabel" name="labelTimeIntegrationfactorCoherent">
+                          <Properties>
+                            <Property name="text" type="java.lang.String" value="time integration factor"/>
+                          </Properties>
+                        </Component>
+                        <Component class="javax.swing.JTextField" name="inputTimeIntegrationFactorIncoherent">
+                          <Properties>
+                            <Property name="enabled" type="boolean" value="false"/>
+                          </Properties>
+                        </Component>
+                        <Component class="javax.swing.JLabel" name="labelChannelsPerSubbandIncoherent">
+                          <Properties>
+                            <Property name="text" type="java.lang.String" value="# channels per subband"/>
+                          </Properties>
+                        </Component>
+                        <Component class="javax.swing.JTextField" name="inputChannelsPerSubbandIncoherent">
+                          <Properties>
+                            <Property name="enabled" type="boolean" value="false"/>
+                          </Properties>
+                        </Component>
+                      </SubComponents>
+                    </Container>
                   </SubComponents>
                 </Container>
                 <Container class="javax.swing.JPanel" name="CorrelatorPanel">
@@ -541,11 +685,10 @@
                       <Group type="103" groupAlignment="0" attributes="0">
                           <Group type="102" attributes="0">
                               <Component id="labelIntegrationTime" min="-2" pref="143" max="-2" attributes="1"/>
-                              <EmptySpace min="-2" pref="24" max="-2" attributes="0"/>
+                              <EmptySpace max="-2" attributes="0"/>
                               <Component id="inputIntegrationTime" min="-2" pref="54" max="-2" attributes="1"/>
                               <EmptySpace type="unrelated" max="-2" attributes="0"/>
                               <Component id="jLabel2" min="-2" max="-2" attributes="0"/>
-                              <EmptySpace pref="28" max="32767" attributes="0"/>
                           </Group>
                       </Group>
                     </DimensionLayout>
@@ -557,7 +700,7 @@
                                   <Component id="inputIntegrationTime" alignment="3" min="-2" max="-2" attributes="0"/>
                                   <Component id="jLabel2" alignment="3" min="-2" max="-2" attributes="0"/>
                               </Group>
-                              <EmptySpace pref="69" max="32767" attributes="0"/>
+                              <EmptySpace pref="49" max="32767" attributes="0"/>
                           </Group>
                       </Group>
                     </DimensionLayout>
@@ -580,201 +723,6 @@
                     </Component>
                   </SubComponents>
                 </Container>
-                <Container class="javax.swing.JPanel" name="CoherentStokesPanel">
-                  <Properties>
-                    <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
-                      <Border info="org.netbeans.modules.form.compat2.border.TitledBorderInfo">
-                        <TitledBorder title="Coherent Stokes">
-                          <Font PropertyName="font" name="Tahoma" size="11" style="1"/>
-                        </TitledBorder>
-                      </Border>
-                    </Property>
-                    <Property name="toolTipText" type="java.lang.String" value="Stokes"/>
-                  </Properties>
-
-                  <Layout>
-                    <DimensionLayout dim="0">
-                      <Group type="103" groupAlignment="0" attributes="0">
-                          <Group type="102" attributes="0">
-                              <Group type="103" groupAlignment="0" attributes="0">
-                                  <Component id="inputIntegrateChannelsCoherent" alignment="0" min="-2" max="-2" attributes="0"/>
-                                  <Group type="102" alignment="0" attributes="0">
-                                      <Group type="103" groupAlignment="1" max="-2" attributes="0">
-                                          <Component id="labelIntegrationstepsCoherent" alignment="0" max="32767" attributes="1"/>
-                                          <Component id="labelWhichCoherent" alignment="0" pref="103" max="32767" attributes="1"/>
-                                      </Group>
-                                      <EmptySpace max="-2" attributes="0"/>
-                                      <Group type="103" groupAlignment="0" max="-2" attributes="0">
-                                          <Component id="inputIntegrationStepsCoherent" max="32767" attributes="1"/>
-                                          <Component id="inputWhichCoherent" alignment="0" pref="173" max="32767" attributes="1"/>
-                                      </Group>
-                                  </Group>
-                              </Group>
-                              <EmptySpace max="32767" attributes="0"/>
-                          </Group>
-                      </Group>
-                    </DimensionLayout>
-                    <DimensionLayout dim="1">
-                      <Group type="103" groupAlignment="0" attributes="0">
-                          <Group type="102" alignment="0" attributes="0">
-                              <Group type="103" groupAlignment="3" attributes="0">
-                                  <Component id="labelWhichCoherent" alignment="3" min="-2" max="-2" attributes="0"/>
-                                  <Component id="inputWhichCoherent" alignment="3" min="-2" max="-2" attributes="0"/>
-                              </Group>
-                              <EmptySpace min="-2" pref="8" max="-2" attributes="0"/>
-                              <Group type="103" groupAlignment="3" attributes="0">
-                                  <Component id="labelIntegrationstepsCoherent" alignment="3" min="-2" max="-2" attributes="0"/>
-                                  <Component id="inputIntegrationStepsCoherent" alignment="3" min="-2" max="-2" attributes="0"/>
-                              </Group>
-                              <EmptySpace type="unrelated" max="-2" attributes="0"/>
-                              <Component id="inputIntegrateChannelsCoherent" min="-2" pref="13" max="-2" attributes="0"/>
-                              <EmptySpace max="32767" attributes="0"/>
-                          </Group>
-                      </Group>
-                    </DimensionLayout>
-                  </Layout>
-                  <SubComponents>
-                    <Component class="javax.swing.JLabel" name="labelWhichCoherent">
-                      <Properties>
-                        <Property name="text" type="java.lang.String" value="which"/>
-                      </Properties>
-                    </Component>
-                    <Component class="javax.swing.JComboBox" name="inputWhichCoherent">
-                      <Properties>
-                        <Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
-                          <StringArray count="4">
-                            <StringItem index="0" value="Item 1"/>
-                            <StringItem index="1" value="Item 2"/>
-                            <StringItem index="2" value="Item 3"/>
-                            <StringItem index="3" value="Item 4"/>
-                          </StringArray>
-                        </Property>
-                        <Property name="enabled" type="boolean" value="false"/>
-                      </Properties>
-                    </Component>
-                    <Component class="javax.swing.JCheckBox" name="inputIntegrateChannelsCoherent">
-                      <Properties>
-                        <Property name="text" type="java.lang.String" value="integrate Channels"/>
-                        <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
-                          <Border info="org.netbeans.modules.form.compat2.border.EmptyBorderInfo">
-                            <EmptyBorder bottom="0" left="0" right="0" top="0"/>
-                          </Border>
-                        </Property>
-                        <Property name="enabled" type="boolean" value="false"/>
-                        <Property name="margin" type="java.awt.Insets" editor="org.netbeans.beaninfo.editors.InsetsEditor">
-                          <Insets value="[0, 0, 0, 0]"/>
-                        </Property>
-                      </Properties>
-                    </Component>
-                    <Component class="javax.swing.JLabel" name="labelIntegrationstepsCoherent">
-                      <Properties>
-                        <Property name="text" type="java.lang.String" value="integration Steps:"/>
-                      </Properties>
-                    </Component>
-                    <Component class="javax.swing.JTextField" name="inputIntegrationStepsCoherent">
-                      <Properties>
-                        <Property name="enabled" type="boolean" value="false"/>
-                      </Properties>
-                    </Component>
-                  </SubComponents>
-                </Container>
-                <Container class="javax.swing.JPanel" name="IncoherentStokesPanel">
-                  <Properties>
-                    <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
-                      <Border info="org.netbeans.modules.form.compat2.border.TitledBorderInfo">
-                        <TitledBorder title="Incoherent Stokes">
-                          <Font PropertyName="font" name="Tahoma" size="11" style="1"/>
-                        </TitledBorder>
-                      </Border>
-                    </Property>
-                    <Property name="toolTipText" type="java.lang.String" value="Stokes"/>
-                  </Properties>
-
-                  <Layout>
-                    <DimensionLayout dim="0">
-                      <Group type="103" groupAlignment="0" attributes="0">
-                          <Group type="102" alignment="0" attributes="0">
-                              <Group type="103" groupAlignment="0" attributes="0">
-                                  <Component id="inputIntegrateChannelsIncoherent" alignment="0" min="-2" max="-2" attributes="0"/>
-                                  <Group type="102" alignment="0" attributes="0">
-                                      <Group type="103" groupAlignment="1" max="-2" attributes="0">
-                                          <Component id="labelIntegrationstepsIncoherent" alignment="0" max="32767" attributes="1"/>
-                                          <Component id="labelWhichIncoherent" alignment="0" pref="103" max="32767" attributes="1"/>
-                                      </Group>
-                                      <EmptySpace max="-2" attributes="0"/>
-                                      <Group type="103" groupAlignment="0" max="-2" attributes="0">
-                                          <Component id="inputIntegrationStepsIncoherent" alignment="0" max="32767" attributes="1"/>
-                                          <Component id="inputWhichIncoherent" alignment="0" pref="173" max="32767" attributes="1"/>
-                                      </Group>
-                                  </Group>
-                              </Group>
-                              <EmptySpace max="32767" attributes="0"/>
-                          </Group>
-                      </Group>
-                    </DimensionLayout>
-                    <DimensionLayout dim="1">
-                      <Group type="103" groupAlignment="0" attributes="0">
-                          <Group type="102" alignment="0" attributes="0">
-                              <Group type="103" groupAlignment="3" attributes="0">
-                                  <Component id="labelWhichIncoherent" alignment="3" min="-2" max="-2" attributes="0"/>
-                                  <Component id="inputWhichIncoherent" alignment="3" min="-2" max="-2" attributes="0"/>
-                              </Group>
-                              <EmptySpace min="-2" pref="8" max="-2" attributes="0"/>
-                              <Group type="103" groupAlignment="3" attributes="0">
-                                  <Component id="labelIntegrationstepsIncoherent" alignment="3" min="-2" max="-2" attributes="0"/>
-                                  <Component id="inputIntegrationStepsIncoherent" alignment="3" min="-2" max="-2" attributes="0"/>
-                              </Group>
-                              <EmptySpace type="unrelated" max="-2" attributes="0"/>
-                              <Component id="inputIntegrateChannelsIncoherent" min="-2" pref="13" max="-2" attributes="0"/>
-                          </Group>
-                      </Group>
-                    </DimensionLayout>
-                  </Layout>
-                  <SubComponents>
-                    <Component class="javax.swing.JLabel" name="labelWhichIncoherent">
-                      <Properties>
-                        <Property name="text" type="java.lang.String" value="which"/>
-                      </Properties>
-                    </Component>
-                    <Component class="javax.swing.JComboBox" name="inputWhichIncoherent">
-                      <Properties>
-                        <Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
-                          <StringArray count="4">
-                            <StringItem index="0" value="Item 1"/>
-                            <StringItem index="1" value="Item 2"/>
-                            <StringItem index="2" value="Item 3"/>
-                            <StringItem index="3" value="Item 4"/>
-                          </StringArray>
-                        </Property>
-                        <Property name="enabled" type="boolean" value="false"/>
-                      </Properties>
-                    </Component>
-                    <Component class="javax.swing.JCheckBox" name="inputIntegrateChannelsIncoherent">
-                      <Properties>
-                        <Property name="text" type="java.lang.String" value="integrate Channels"/>
-                        <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
-                          <Border info="org.netbeans.modules.form.compat2.border.EmptyBorderInfo">
-                            <EmptyBorder bottom="0" left="0" right="0" top="0"/>
-                          </Border>
-                        </Property>
-                        <Property name="enabled" type="boolean" value="false"/>
-                        <Property name="margin" type="java.awt.Insets" editor="org.netbeans.beaninfo.editors.InsetsEditor">
-                          <Insets value="[0, 0, 0, 0]"/>
-                        </Property>
-                      </Properties>
-                    </Component>
-                    <Component class="javax.swing.JLabel" name="labelIntegrationstepsIncoherent">
-                      <Properties>
-                        <Property name="text" type="java.lang.String" value="integration Steps:"/>
-                      </Properties>
-                    </Component>
-                    <Component class="javax.swing.JTextField" name="inputIntegrationStepsIncoherent">
-                      <Properties>
-                        <Property name="enabled" type="boolean" value="false"/>
-                      </Properties>
-                    </Component>
-                  </SubComponents>
-                </Container>
                 <Container class="javax.swing.JPanel" name="PencilInfoPanel">
                   <Properties>
                     <Property name="border" type="javax.swing.border.Border" editor="org.netbeans.modules.form.editors2.BorderEditor">
@@ -793,7 +741,7 @@
                           <Group type="102" attributes="0">
                               <EmptySpace max="-2" attributes="0"/>
                               <Component id="inputFlysEye" min="-2" max="-2" attributes="0"/>
-                              <EmptySpace pref="210" max="32767" attributes="0"/>
+                              <EmptySpace pref="65" max="32767" attributes="0"/>
                           </Group>
                       </Group>
                     </DimensionLayout>
@@ -801,7 +749,7 @@
                       <Group type="103" groupAlignment="0" attributes="0">
                           <Group type="102" attributes="0">
                               <Component id="inputFlysEye" min="-2" pref="13" max="-2" attributes="0"/>
-                              <EmptySpace pref="59" max="32767" attributes="0"/>
+                              <EmptySpace pref="19" max="32767" attributes="0"/>
                           </Group>
                       </Group>
                     </DimensionLayout>
diff --git a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/OlapPanel.java b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/OlapPanel.java
index 614d4805a6ac2cf049f5a3d6c3c4f2173022b2f8..fb46664a151356ba968d951ad80b36267fe5c4d5 100644
--- a/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/OlapPanel.java
+++ b/SAS/OTB/OTB/src/nl/astron/lofar/sas/otbcomponents/userpanels/OlapPanel.java
@@ -18,7 +18,6 @@
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
-
 package nl.astron.lofar.sas.otbcomponents.userpanels;
 
 import java.awt.Component;
@@ -28,8 +27,7 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.rmi.RemoteException;
-import java.util.Enumeration;
-import java.util.Vector;
+import java.util.ArrayList;
 import javax.swing.JFileChooser;
 import javax.swing.JMenuItem;
 import javax.swing.JPanel;
@@ -53,73 +51,68 @@ import org.apache.log4j.Logger;
  *
  * @version $Id$
  */
-public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
+public class OlapPanel extends javax.swing.JPanel implements IViewPanel {
 
-    static Logger logger = Logger.getLogger(OlapPanel.class);    
+    static Logger logger = Logger.getLogger(OlapPanel.class);
     static String name = "OlapPanel";
-    
+
     /** Creates new form OlapPanel */
-    public OlapPanel(MainFrame aMainFrame,jOTDBnode aNode) {
+    public OlapPanel(MainFrame aMainFrame, jOTDBnode aNode) {
         initComponents();
         itsMainFrame = aMainFrame;
-        itsNode=aNode;
+        itsNode = aNode;
         initialize();
         initPanel();
     }
-    
+
     /** Creates new form BeanForm */
     public OlapPanel() {
         initComponents();
         initialize();
-    }   
-    
+    }
+
     @Override
     public void setMainFrame(MainFrame aMainFrame) {
         if (aMainFrame != null) {
-            itsMainFrame=aMainFrame;
+            itsMainFrame = aMainFrame;
         } else {
             logger.debug("No Mainframe supplied");
         }
     }
-    
+
     @Override
     public String getShortName() {
         return name;
     }
-    
+
     @Override
-    public void setContent(Object anObject) { 
-        itsNode=(jOTDBnode)anObject;
-        jOTDBparam aParam=null;
+    public void setContent(Object anObject) {
+        itsNode = (jOTDBnode) anObject;
+        jOTDBparam aParam = null;
 
 
 
         try {
-           
+
             //we need to get all the childs from this node.    
-            Vector<jOTDBnode> childs = OtdbRmi.getRemoteMaintenance().getItemList(itsNode.treeID(), itsNode.nodeID(), 1);
-            
+            ArrayList<jOTDBnode> childs = new ArrayList(OtdbRmi.getRemoteMaintenance().getItemList(itsNode.treeID(), itsNode.nodeID(), 1));
+
             // get all the params per child
-            Enumeration e = childs.elements();
-            while( e.hasMoreElements()  ) {
-                aParam=null;
-            
-                jOTDBnode aNode = (jOTDBnode)e.nextElement();
-                        
+            for (jOTDBnode aNode : childs) {
+                aParam = null;
+
                 // We need to keep all the nodes needed by this panel
                 // if the node is a leaf we need to get the pointed to value via Param.
                 if (aNode.leaf) {
                     aParam = OtdbRmi.getRemoteMaintenance().getParam(aNode);
-                    setField(itsNode,aParam,aNode);
-                    
-                                    
-                //we need to get all the childs from the following nodes as well.
-                }else if (LofarUtils.keyName(aNode.name).equals("CNProc")) {
+                    setField(itsNode, aParam, aNode);
+
+
+                    //we need to get all the childs from the following nodes as well.
+                } else if (LofarUtils.keyName(aNode.name).equals("CNProc")) {
                     this.retrieveAndDisplayChildDataForNode(aNode);
                 } else if (LofarUtils.keyName(aNode.name).equals("IONProc")) {
                     this.retrieveAndDisplayChildDataForNode(aNode);
-                } else if (LofarUtils.keyName(aNode.name).equals("StorageProc")) {
-                    this.retrieveAndDisplayChildDataForNode(aNode);
                 } else if (LofarUtils.keyName(aNode.name).equals("Correlator")) {
                     this.retrieveAndDisplayChildDataForNode(aNode);
                 } else if (LofarUtils.keyName(aNode.name).equals("PencilInfo")) {
@@ -131,77 +124,72 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
                 }
             }
             //we also need the Virtual Instrument Storagenodes here
-            Vector<jOTDBnode> VIchilds = OtdbRmi.getRemoteMaintenance().getItemList(itsNode.treeID(),"%VirtualInstrument");
+            ArrayList<jOTDBnode> VIchilds = new ArrayList(OtdbRmi.getRemoteMaintenance().getItemList(itsNode.treeID(), "%VirtualInstrument"));
 
             // get all the params per child
-            Enumeration eVI = VIchilds.elements();
-            while( eVI.hasMoreElements()  ) {
-                aParam=null;
-
-                jOTDBnode aNode = (jOTDBnode)eVI.nextElement();
+            for (jOTDBnode aNode : VIchilds) {
+                aParam = null;
 
                 // We need to keep all the nodes needed by this panel
                 // if the node is a leaf we need to get the pointed to value via Param.
                 if (aNode.leaf) {
                     aParam = OtdbRmi.getRemoteMaintenance().getParam(aNode);
-                    setField(itsNode,aParam,aNode);
-                }else if (LofarUtils.keyName(aNode.name).equals("VirtualInstrument")) {
+                    setField(itsNode, aParam, aNode);
+                } else if (LofarUtils.keyName(aNode.name).equals("VirtualInstrument")) {
                     this.retrieveAndDisplayChildDataForNode(aNode);
                 }
             }
 
             //we also need the Output_ nodes from the Dataproducts here
-            Vector<jOTDBnode> OUchilds = OtdbRmi.getRemoteMaintenance().getItemList(itsNode.treeID(),"%Output_%");
+            ArrayList<jOTDBnode> OUchilds = new ArrayList(OtdbRmi.getRemoteMaintenance().getItemList(itsNode.treeID(), "%Output_%"));
 
             // get all the params per child
-            Enumeration eOU = OUchilds.elements();
-            while( eOU.hasMoreElements()  ) {
-                aParam=null;
-
-                jOTDBnode aNode = (jOTDBnode)eOU.nextElement();
+            for (jOTDBnode aNode : OUchilds) {
+                aParam = null;
 
                 // We need to keep all the nodes needed by this panel
                 // if the node is a leaf we need to get the pointed to value via Param.
                 if (aNode.leaf) {
                     aParam = OtdbRmi.getRemoteMaintenance().getParam(aNode);
-                    setField(itsNode,aParam,aNode);
-                }else if (LofarUtils.keyName(aNode.name).equals("Output_Beamformed")) {
+                    setField(itsNode, aParam, aNode);
+                } else if (LofarUtils.keyName(aNode.name).equals("Output_Beamformed")) {
                     this.retrieveAndDisplayChildDataForNode(aNode);
-                }else if (LofarUtils.keyName(aNode.name).equals("Output_CoherentStokes")) {
+                } else if (LofarUtils.keyName(aNode.name).equals("Output_CoherentStokes")) {
                     this.retrieveAndDisplayChildDataForNode(aNode);
-                }else if (LofarUtils.keyName(aNode.name).equals("Output_Correlated")) {
+                } else if (LofarUtils.keyName(aNode.name).equals("Output_Correlated")) {
                     this.retrieveAndDisplayChildDataForNode(aNode);
-                }else if (LofarUtils.keyName(aNode.name).equals("Output_Filtered")) {
+                } else if (LofarUtils.keyName(aNode.name).equals("Output_Filtered")) {
                     this.retrieveAndDisplayChildDataForNode(aNode);
-                }else if (LofarUtils.keyName(aNode.name).equals("Output_IncoherentStokes")) {
+                } else if (LofarUtils.keyName(aNode.name).equals("Output_IncoherentStokes")) {
                     this.retrieveAndDisplayChildDataForNode(aNode);
                 }
             }
 
         } catch (RemoteException ex) {
-            String aS="Error during getComponentParam: "+ ex;
+            String aS = "Error during getComponentParam: " + ex;
             logger.error(aS);
-            LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+            LofarUtils.showErrorPanel(this, aS, new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
             return;
         }
-        
+
         initPanel();
     }
-    
+
     @Override
     public boolean isSingleton() {
         return false;
     }
-    
+
     @Override
     public JPanel getInstance() {
         return new OlapPanel();
     }
+
     @Override
     public boolean hasPopupMenu() {
         return true;
-    }    
-    
+    }
+
     /** create popup menu for this panel
      *
      *  // build up the menu
@@ -220,33 +208,36 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
      *  aPopupMenu.show(aComponent, x, y );        
      */
     @Override
-    public void createPopupMenu(Component aComponent,int x, int y) {
-        JPopupMenu aPopupMenu=null;
-        JMenuItem  aMenuItem=null;
-        
-        aPopupMenu= new JPopupMenu();
+    public void createPopupMenu(Component aComponent, int x, int y) {
+        JPopupMenu aPopupMenu = null;
+        JMenuItem aMenuItem = null;
+
+        aPopupMenu = new JPopupMenu();
         // For VIC trees
-        if (itsTreeType.equals("VHtree")) {
-            //  Fill in menu as in the example above
-            aMenuItem=new JMenuItem("Create ParSet File");        
-            aMenuItem.addActionListener(new java.awt.event.ActionListener() {
-                @Override
-                public void actionPerformed(java.awt.event.ActionEvent evt) {
-                    popupMenuHandler(evt);
-                }
-            });
-            aMenuItem.setActionCommand("Create ParSet File");
-            aPopupMenu.add(aMenuItem);
-            
-        // For template trees
-        } else if (itsTreeType.equals("VItemplate")) {
-                
-        }
-        
+        switch (itsTreeType) {
+            case "VHtree":
+                //  Fill in menu as in the example above
+                aMenuItem = new JMenuItem("Create ParSet File");
+                aMenuItem.addActionListener(new java.awt.event.ActionListener() {
+
+                    @Override
+                    public void actionPerformed(java.awt.event.ActionEvent evt) {
+                        popupMenuHandler(evt);
+                    }
+                });
+                aMenuItem.setActionCommand("Create ParSet File");
+                aPopupMenu.add(aMenuItem);
+
+                // For template trees
+                break;
+            case "VItemplate":
+                break;
+        }
+
         aPopupMenu.setOpaque(true);
-        aPopupMenu.show(aComponent, x, y );       
+        aPopupMenu.show(aComponent, x, y);
     }
-    
+
     /** handles the choice from the popupmenu 
      *
      * depending on the choices that are possible for this panel perform the action for it
@@ -257,9 +248,9 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
      */
     @Override
     public void popupMenuHandler(java.awt.event.ActionEvent evt) {
-         if (evt.getActionCommand().equals("Create ParSet File")) {
+        if (evt.getActionCommand().equals("Create ParSet File")) {
             logger.trace("Create ParSet File");
-            int aTreeID=itsMainFrame.getSharedVars().getTreeID();
+            int aTreeID = itsMainFrame.getSharedVars().getTreeID();
             if (fc == null) {
                 fc = new JFileChooser();
                 fc.setApproveButtonText("Apply");
@@ -268,77 +259,71 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
             if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
                 try {
                     File aFile = fc.getSelectedFile();
-                    
+
                     // create filename that can be used at the remote site    
-                    String aRemoteFileName="/tmp/"+aTreeID+"-"+itsNode.name+"_"+itsMainFrame.getUserAccount().getUserName()+".ParSet";
-                    
+                    String aRemoteFileName = "/tmp/" + aTreeID + "-" + itsNode.name + "_" + itsMainFrame.getUserAccount().getUserName() + ".ParSet";
+
                     // write the parset
-                    OtdbRmi.getRemoteMaintenance().exportTree(aTreeID,itsNode.nodeID(),aRemoteFileName,2,false); 
-                    
+                    OtdbRmi.getRemoteMaintenance().exportTree(aTreeID, itsNode.nodeID(), aRemoteFileName, 2, false);
+
                     //obtain the remote file
                     byte[] dldata = OtdbRmi.getRemoteFileTrans().downloadFile(aRemoteFileName);
-
-                    BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(aFile));
-                    output.write(dldata,0,dldata.length);
-                    output.flush();
-                    output.close();
+                    try (BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(aFile))) {
+                        output.write(dldata, 0, dldata.length);
+                        output.flush();
+                    }
                     logger.trace("File written to: " + aFile.getPath());
                 } catch (RemoteException ex) {
-                    String aS="exportTree failed : " + ex;
+                    String aS = "exportTree failed : " + ex;
                     logger.error(aS);
-                    LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                    LofarUtils.showErrorPanel(this, aS, new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                 } catch (FileNotFoundException ex) {
-                    String aS="Error during newPICTree creation: "+ ex;
+                    String aS = "Error during newPICTree creation: " + ex;
                     logger.error(aS);
-                    LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                    LofarUtils.showErrorPanel(this, aS, new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                 } catch (IOException ex) {
-                    String aS="Error during newPICTree creation: "+ ex;
+                    String aS = "Error during newPICTree creation: " + ex;
                     logger.error(aS);
-                    LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                    LofarUtils.showErrorPanel(this, aS, new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
                 }
             }
-        }       
-    } 
-    
+        }
+    }
+
     /** 
      * Helper method that retrieves the child nodes for a given jOTDBnode, 
      * and triggers setField() accordingly.
      * @param aNode the node to retrieve and display child data of.
      */
-    private void retrieveAndDisplayChildDataForNode(jOTDBnode aNode){
-        jOTDBparam aParam=null;
+    private void retrieveAndDisplayChildDataForNode(jOTDBnode aNode) {
+        jOTDBparam aParam = null;
         try {
-            Vector<jOTDBnode> HWchilds = OtdbRmi.getRemoteMaintenance().getItemList(aNode.treeID(), aNode.nodeID(), 1);
+            ArrayList<jOTDBnode> HWchilds = new ArrayList(OtdbRmi.getRemoteMaintenance().getItemList(aNode.treeID(), aNode.nodeID(), 1));
             // get all the params per child
-            Enumeration e1 = HWchilds.elements();
-            while( e1.hasMoreElements()  ) {
-                
-                jOTDBnode aHWNode = (jOTDBnode)e1.nextElement();
-                aParam=null;
+            for (jOTDBnode aHWNode : HWchilds) {
+                aParam = null;
                 // We need to keep all the params needed by this panel
                 if (aHWNode.leaf) {
                     aParam = OtdbRmi.getRemoteMaintenance().getParam(aHWNode);
                 }
-                setField(aNode,aParam,aHWNode);
+                setField(aNode, aParam, aHWNode);
             }
         } catch (RemoteException ex) {
-            String aS="Error during retrieveAndDisplayChildDataForNode: "+ ex;
+            String aS = "Error during retrieveAndDisplayChildDataForNode: " + ex;
             logger.error(aS);
-            LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+            LofarUtils.showErrorPanel(this, aS, new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
             return;
         }
     }
 
-
-    
     /**
      * Sets the different fields in the GUI, using the names of the nodes provided
      * @param parent the parent node of the node to be displayed
      * @param aParam the parameter of the node to be displayed if applicable
      * @param aNode  the node to be displayed
      */
-    private void setField(jOTDBnode parent,jOTDBparam aParam, jOTDBnode aNode) {
-        if (aParam==null) {
+    private void setField(jOTDBnode parent, jOTDBparam aParam, jOTDBnode aNode) {
+        if (aParam == null) {
             return;
         }
         boolean isRef = LofarUtils.isReference(aNode.limits);
@@ -347,309 +332,306 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
         /* Set's the different fields in the GUI */
 
         // Generic OLAP
-        if (aParam==null) {
+        if (aParam == null) {
             return;
         }
-        logger.debug("setField for: "+ aNode.name);
+        logger.debug("setField for: " + aNode.name);
         try {
-            if (OtdbRmi.getRemoteTypes().getParamType(aParam.type).substring(0,1).equals("p")) {
-               // Have to get new param because we need the unresolved limits field.
-               aParam = OtdbRmi.getRemoteMaintenance().getParam(aNode.treeID(),aNode.paramDefID());                
+            if (OtdbRmi.getRemoteTypes().getParamType(aParam.type).substring(0, 1).equals("p")) {
+                // Have to get new param because we need the unresolved limits field.
+                aParam = OtdbRmi.getRemoteMaintenance().getParam(aNode.treeID(), aNode.paramDefID());
             }
         } catch (RemoteException ex) {
-            String aS="Error during getParam: "+ ex;
+            String aS = "Error during getParam: " + ex;
             logger.error(aS);
-            LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-        }
-        
-        if(parentName.equals("CNProc")){
-            // OLAP-CNProc params
-            if (aKeyName.equals("nrPPFTaps")) {
-                inputNrPPFTaps.setToolTipText(aParam.description);
-                itsNrPPFTaps=aNode;
-                if (isRef && aParam != null) {
-                    inputNrPPFTaps.setText(aNode.limits + " : " + aParam.limits);
-                } else {
-                    inputNrPPFTaps.setText(aNode.limits);
-                }
-            }
-            
-        } else if(parentName.equals("Correlator")){
-            // OLAP Correlator params
-
-            if (aKeyName.equals("integrationTime")) {
-                inputIntegrationTime.setToolTipText(aParam.description);
-                itsIntegrationTime=aNode;
-                if (isRef && aParam != null) {
-                    inputIntegrationTime.setText(aNode.limits + " : " + aParam.limits);
-                } else {
-                    inputIntegrationTime.setText(aNode.limits);
-                }
-            }
-        } else if(parentName.equals("PencilInfo")){
-            // OLAP PencilInfo params
-
-            if (aKeyName.equals("flysEye")) {
-                inputFlysEye.setToolTipText(aParam.description);
-                itsFlysEye=aNode;
-                boolean aSelection = false;
-                if (isRef && aParam != null) {
-                    if (aParam.limits.equals("true")||aParam.limits.equals("TRUE")) {
-                        aSelection = true;
-                    }
-                } else {
-                    if (aNode.limits.equals("true")||aNode.limits.equals("TRUE")) {
-                        aSelection = true;
-                    }
-                }
-                inputFlysEye.setSelected(aSelection);
-                checkSettings();
-            }
-        } else if(parentName.equals("CNProc_CoherentStokes")){
-            // OLAP PencilInfo params
-
-            if (aKeyName.equals("which")) {
-                inputWhichCoherent.setToolTipText(aParam.description);
-                LofarUtils.setPopupComboChoices(inputWhichCoherent,aParam.limits);
-                if (!aNode.limits.equals("")) {
-                    inputWhichCoherent.setSelectedItem(aNode.limits);
-                }
-                itsWhichCoherent=aNode;
-            } else if (aKeyName.equals("integrateChannels")) {
-                inputIntegrateChannelsCoherent.setToolTipText(aParam.description);
-                itsIntegrateChannelsCoherent=aNode;
-                boolean aSelection = false;
-                if (isRef && aParam != null) {
-                    if (aParam.limits.equals("true")||aParam.limits.equals("TRUE")) {
-                        aSelection = true;
-                    }
-                } else {
-                    if (aNode.limits.equals("true")||aNode.limits.equals("TRUE")) {
-                        aSelection = true;
+            LofarUtils.showErrorPanel(this, aS, new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+        }
+        switch (parentName) {
+            case "CNProc":
+                // OLAP-CNProc params
+                if (aKeyName.equals("nrPPFTaps")) {
+                    inputNrPPFTaps.setToolTipText(aParam.description);
+                    itsNrPPFTaps = aNode;
+                    if (isRef && aParam != null) {
+                        inputNrPPFTaps.setText(aNode.limits + " : " + aParam.limits);
+                    } else {
+                        inputNrPPFTaps.setText(aNode.limits);
                     }
                 }
-                inputIntegrateChannelsCoherent.setSelected(aSelection);
-
-            } else if (aKeyName.equals("integrationSteps")) {
-                inputIntegrationStepsCoherent.setToolTipText(aParam.description);
-                itsIntegrationStepsCoherent=aNode;
-                if (isRef && aParam != null) {
-                    inputIntegrationStepsCoherent.setText(aNode.limits + " : " + aParam.limits);
-                } else {
-                    inputIntegrationStepsCoherent.setText(aNode.limits);
-                }
-            }
-
-        } else if(parentName.equals("CNProc_IncoherentStokes")){
-            // OLAP PencilInfo params
-
-            if (aKeyName.equals("which")) {
-                inputWhichIncoherent.setToolTipText(aParam.description);
-                LofarUtils.setPopupComboChoices(inputWhichIncoherent,aParam.limits);
-                if (!aNode.limits.equals("")) {
-                    inputWhichIncoherent.setSelectedItem(aNode.limits);
-                }
-                itsWhichIncoherent=aNode;
-            } else if (aKeyName.equals("integrateChannels")) {
-                inputIntegrateChannelsIncoherent.setToolTipText(aParam.description);
-                itsIntegrateChannelsIncoherent=aNode;
-                boolean aSelection = false;
-                if (isRef && aParam != null) {
-                    if (aParam.limits.equals("true")||aParam.limits.equals("TRUE")) {
-                        aSelection = true;
+                break;
+            case "Correlator":
+                // OLAP Correlator params
+                if (aKeyName.equals("integrationTime")) {
+                    inputIntegrationTime.setToolTipText(aParam.description);
+                    itsIntegrationTime = aNode;
+                    if (isRef && aParam != null) {
+                        inputIntegrationTime.setText(aNode.limits + " : " + aParam.limits);
+                    } else {
+                        inputIntegrationTime.setText(aNode.limits);
                     }
-                } else {
-                    if (aNode.limits.equals("true")||aNode.limits.equals("TRUE")) {
-                        aSelection = true;
-                    }
-                }
-                inputIntegrateChannelsIncoherent.setSelected(aSelection);
-
-            } else if (aKeyName.equals("integrationSteps")) {
-                inputIntegrationStepsIncoherent.setToolTipText(aParam.description);
-                itsIntegrationStepsIncoherent=aNode;
-                if (isRef && aParam != null) {
-                    inputIntegrationStepsIncoherent.setText(aNode.limits + " : " + aParam.limits);
-                } else {
-                    inputIntegrationStepsIncoherent.setText(aNode.limits);
                 }
-            }
-        } else if(parentName.equals("StorageProc")){
-            // OLAP StorageProc params
-            if (aKeyName.equals("subbandsPerMS")) {
-                inputSubbandsPerMS.setToolTipText(aParam.description);
-                itsSubbandsPerMS=aNode;
-                if (isRef && aParam != null) {
-                    inputSubbandsPerMS.setText(aNode.limits + " : " + aParam.limits);
-                } else {
-                    inputSubbandsPerMS.setText(aNode.limits);
-                }
-            }
-        } else if(parentName.equals("OLAP")){
-            // Olap Specific parameters
-            if (aKeyName.equals("maxNetworkDelay")) {
-                inputMaxNetworkDelay.setToolTipText(aParam.description);
-                itsMaxNetworkDelay=aNode;
-                if (isRef && aParam != null) {
-                    inputMaxNetworkDelay.setText(aNode.limits + " : " + aParam.limits);
-                } else {
-                    inputMaxNetworkDelay.setText(aNode.limits);
-                }
-            } else if (aKeyName.equals("nrSubbandsPerFrame")) {
-                inputNrSubbandsPerFrame.setToolTipText(aParam.description);
-                itsNrSubbandsPerFrame=aNode;
-                if (isRef && aParam != null) {
-                    inputNrSubbandsPerFrame.setText(aNode.limits);
-                    subbandsPerFrameDerefText.setText(aParam.limits);
-                } else {
-                    inputNrSubbandsPerFrame.setText(aNode.limits);
-                }
-            } else if (aKeyName.equals("delayCompensation")) {
-                inputDelayCompensation.setToolTipText(aParam.description);
-                itsDelayCompensation=aNode;
-                boolean aSelection = false;
-                if (isRef && aParam != null) {
-                    if (aParam.limits.equals("true")||aParam.limits.equals("TRUE")) {
-                        aSelection = true;
+                break;
+            case "PencilInfo":
+                // OLAP PencilInfo params
+                if (aKeyName.equals("flysEye")) {
+                    inputFlysEye.setToolTipText(aParam.description);
+                    itsFlysEye = aNode;
+                    boolean aSelection = false;
+                    if (isRef && aParam != null) {
+                        if (aParam.limits.equals("true") || aParam.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
+                    } else {
+                        if (aNode.limits.equals("true") || aNode.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
                     }
-                } else {
-                    if (aNode.limits.equals("true")||aNode.limits.equals("TRUE")) {
-                        aSelection = true;
-                    }
-                }
-                inputDelayCompensation.setSelected(aSelection);
-            } else if (aKeyName.equals("nrBitsPerSample")) {
-                inputNrBitsPerSample.setToolTipText(aParam.description);
-                itsNrBitsPerSample=aNode;
-                if (isRef && aParam != null) {
-                    inputNrBitsPerSample.setText(aNode.limits + " : " + aParam.limits);
-                } else {
-                    inputNrBitsPerSample.setText(aNode.limits);
+                    inputFlysEye.setSelected(aSelection);
+                    checkSettings();
                 }
-            } else if (aKeyName.equals("nrSecondsOfBuffer")) {
-                inputNrSecondsOfBuffer.setToolTipText(aParam.description);
-                itsNrSecondsOfBuffer=aNode;
-                if (isRef && aParam != null) {
-                    inputNrSecondsOfBuffer.setText(aNode.limits + " : " + aParam.limits);
-                } else {
-                    inputNrSecondsOfBuffer.setText(aNode.limits);
+                break;
+            case "CNProc_CoherentStokes":
+                // OLAP PencilInfo params
+                switch (aKeyName) {
+                    case "which":
+                        inputWhichCoherent.setToolTipText(aParam.description);
+                        LofarUtils.setPopupComboChoices(inputWhichCoherent, aParam.limits);
+                        if (!aNode.limits.equals("")) {
+                            inputWhichCoherent.setSelectedItem(aNode.limits);
+                        }
+                        itsWhichCoherent = aNode;
+                        break;
+                    case "channelsPerSubband":
+                        inputChannelsPerSubbandCoherent.setToolTipText(aParam.description);
+                        itsChannelsPerSubbandCoherent = aNode;
+                        if (isRef && aParam != null) {
+                            inputChannelsPerSubbandCoherent.setText(aNode.limits + " : " + aParam.limits);
+                        } else {
+                            inputChannelsPerSubbandCoherent.setText(aNode.limits);
+                        }
+                        break;
+                    case "timeIntegrationFactor":
+                        inputTimeIntegrationFactorCoherent.setToolTipText(aParam.description);
+                        itsTimeIntegrationFactorCoherent = aNode;
+                        if (isRef && aParam != null) {
+                            inputTimeIntegrationFactorCoherent.setText(aNode.limits + " : " + aParam.limits);
+                        } else {
+                            inputTimeIntegrationFactorCoherent.setText(aNode.limits);
+                        }
+                        break;
                 }
-            } else if (aKeyName.equals("nrTimesInFrame")) {
-                inputNrTimesInFrame.setToolTipText(aParam.description);
-                itsNrTimesInFrame=aNode;
-                if (isRef && aParam != null) {
-                    inputNrTimesInFrame.setText(aNode.limits + " : " + aParam.limits);
-                } else {
-                    inputNrTimesInFrame.setText(aNode.limits);
+                break;
+            case "CNProc_IncoherentStokes":
+                // OLAP PencilInfo params
+                switch (aKeyName) {
+                    case "which":
+                        inputWhichIncoherent.setToolTipText(aParam.description);
+                        LofarUtils.setPopupComboChoices(inputWhichIncoherent, aParam.limits);
+                        if (!aNode.limits.equals("")) {
+                            inputWhichIncoherent.setSelectedItem(aNode.limits);
+                        }
+                        itsWhichIncoherent = aNode;
+                        break;
+                    case "channelsPerSubband":
+                        inputChannelsPerSubbandIncoherent.setToolTipText(aParam.description);
+                        itsChannelsPerSubbandIncoherent = aNode;
+                        if (isRef && aParam != null) {
+                            inputChannelsPerSubbandIncoherent.setText(aNode.limits + " : " + aParam.limits);
+                        } else {
+                            inputChannelsPerSubbandIncoherent.setText(aNode.limits);
+                        }
+                        break;
+                    case "timeIntegrationFactor":
+                        inputTimeIntegrationFactorIncoherent.setToolTipText(aParam.description);
+                        itsTimeIntegrationFactorIncoherent = aNode;
+                        if (isRef && aParam != null) {
+                            inputTimeIntegrationFactorIncoherent.setText(aNode.limits + " : " + aParam.limits);
+                        } else {
+                            inputTimeIntegrationFactorIncoherent.setText(aNode.limits);
+                        }
+                        break;
                 }
-
-            } else if (aKeyName.equals("correctBandPass")) {
-                inputCorrectBandPass.setToolTipText(aParam.description);
-                itsCorrectBandPass=aNode;
-                boolean aSelection = false;
-                if (isRef && aParam != null) {
-                    if (aParam.limits.equals("true")||aParam.limits.equals("TRUE")) {
-                        aSelection = true;
+                break;
+            case "OLAP":
+                // Olap Specific parameters
+                switch (aKeyName) {
+                    case "maxNetworkDelay":
+                        inputMaxNetworkDelay.setToolTipText(aParam.description);
+                        itsMaxNetworkDelay = aNode;
+                        if (isRef && aParam != null) {
+                            inputMaxNetworkDelay.setText(aNode.limits + " : " + aParam.limits);
+                        } else {
+                            inputMaxNetworkDelay.setText(aNode.limits);
+                        }
+                        break;
+                    case "nrSubbandsPerFrame":
+                        inputNrSubbandsPerFrame.setToolTipText(aParam.description);
+                        itsNrSubbandsPerFrame = aNode;
+                        if (isRef && aParam != null) {
+                            inputNrSubbandsPerFrame.setText(aNode.limits);
+                            subbandsPerFrameDerefText.setText(aParam.limits);
+                        } else {
+                            inputNrSubbandsPerFrame.setText(aNode.limits);
+                        }
+                        break;
+                    case "delayCompensation": {
+                        inputDelayCompensation.setToolTipText(aParam.description);
+                        itsDelayCompensation = aNode;
+                        boolean aSelection = false;
+                        if (isRef && aParam != null) {
+                            if (aParam.limits.equals("true") || aParam.limits.equals("TRUE")) {
+                                aSelection = true;
+                            }
+                        } else {
+                            if (aNode.limits.equals("true") || aNode.limits.equals("TRUE")) {
+                                aSelection = true;
+                            }
+                        }
+                        inputDelayCompensation.setSelected(aSelection);
+                        break;
                     }
-                } else {
-                    if (aNode.limits.equals("true")||aNode.limits.equals("TRUE")) {
-                        aSelection = true;
+                    case "nrBitsPerSample":
+                        inputNrBitsPerSample.setToolTipText(aParam.description);
+                        itsNrBitsPerSample = aNode;
+                        if (isRef && aParam != null) {
+                            inputNrBitsPerSample.setText(aNode.limits + " : " + aParam.limits);
+                        } else {
+                            inputNrBitsPerSample.setText(aNode.limits);
+                        }
+                        break;
+                    case "nrSecondsOfBuffer":
+                        inputNrSecondsOfBuffer.setToolTipText(aParam.description);
+                        itsNrSecondsOfBuffer = aNode;
+                        if (isRef && aParam != null) {
+                            inputNrSecondsOfBuffer.setText(aNode.limits + " : " + aParam.limits);
+                        } else {
+                            inputNrSecondsOfBuffer.setText(aNode.limits);
+                        }
+                        break;
+                    case "nrTimesInFrame":
+                        inputNrTimesInFrame.setToolTipText(aParam.description);
+                        itsNrTimesInFrame = aNode;
+                        if (isRef && aParam != null) {
+                            inputNrTimesInFrame.setText(aNode.limits + " : " + aParam.limits);
+                        } else {
+                            inputNrTimesInFrame.setText(aNode.limits);
+                        }
+                        break;
+                    case "correctBandPass": {
+                        inputCorrectBandPass.setToolTipText(aParam.description);
+                        itsCorrectBandPass = aNode;
+                        boolean aSelection = false;
+                        if (isRef && aParam != null) {
+                            if (aParam.limits.equals("true") || aParam.limits.equals("TRUE")) {
+                                aSelection = true;
+                            }
+                        } else {
+                            if (aNode.limits.equals("true") || aNode.limits.equals("TRUE")) {
+                                aSelection = true;
+                            }
+                        }
+                        inputCorrectBandPass.setSelected(aSelection);
+                        break;
                     }
                 }
-                inputCorrectBandPass.setSelected(aSelection);
-            }
-        } else if (parentName.equals("Output_Beamformed")) {
-            if (aKeyName.equals("enabled")) {
-                inputOutputBeamFormedData.setToolTipText(aParam.description);
-                itsOutputBeamFormedData=aNode;
-                boolean aSelection = false;
-                if (isRef && aParam != null) {
-                    if (aParam.limits.equals("true")||aParam.limits.equals("TRUE")) {
-                        aSelection = true;
-                    }
-                } else {
-                    if (aNode.limits.equals("true")||aNode.limits.equals("TRUE")) {
-                        aSelection = true;
+                break;
+            case "Output_Beamformed":
+                if (aKeyName.equals("enabled")) {
+                    inputOutputBeamFormedData.setToolTipText(aParam.description);
+                    itsOutputBeamFormedData = aNode;
+                    boolean aSelection = false;
+                    if (isRef && aParam != null) {
+                        if (aParam.limits.equals("true") || aParam.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
+                    } else {
+                        if (aNode.limits.equals("true") || aNode.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
                     }
+                    inputOutputBeamFormedData.setSelected(aSelection);
+                    checkSettings();
                 }
-                inputOutputBeamFormedData.setSelected(aSelection);
-                checkSettings();
-            }
-        } else if (parentName.equals("Output_Correlated")) {
-            if (aKeyName.equals("enabled")) {
-                inputOutputCorrelatedData.setToolTipText(aParam.description);
-                itsOutputCorrelatedData=aNode;
-                boolean aSelection = false;
-                if (isRef && aParam != null) {
-                    if (aParam.limits.equals("true")||aParam.limits.equals("TRUE")) {
-                        aSelection = true;
-                    }
-                } else {
-                    if (aNode.limits.equals("true")||aNode.limits.equals("TRUE")) {
-                        aSelection = true;
+                break;
+            case "Output_Correlated":
+                if (aKeyName.equals("enabled")) {
+                    inputOutputCorrelatedData.setToolTipText(aParam.description);
+                    itsOutputCorrelatedData = aNode;
+                    boolean aSelection = false;
+                    if (isRef && aParam != null) {
+                        if (aParam.limits.equals("true") || aParam.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
+                    } else {
+                        if (aNode.limits.equals("true") || aNode.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
                     }
+                    inputOutputCorrelatedData.setSelected(aSelection);
+                    inputIntegrationTime.setEnabled(aSelection);
+                    checkSettings();
                 }
-                inputOutputCorrelatedData.setSelected(aSelection);
-                inputIntegrationTime.setEnabled(aSelection);
-                checkSettings();
-            }
-        } else if (parentName.equals("Output_Filtered")) {
-            if (aKeyName.equals("enabled")) {
-                inputOutputFilteredData.setToolTipText(aParam.description);
-                itsOutputFilteredData=aNode;
-                boolean aSelection = false;
-                if (isRef && aParam != null) {
-                    if (aParam.limits.equals("true")||aParam.limits.equals("TRUE")) {
-                        aSelection = true;
-                    }
-                } else {
-                    if (aNode.limits.equals("true")||aNode.limits.equals("TRUE")) {
-                        aSelection = true;
+                break;
+            case "Output_Filtered":
+                if (aKeyName.equals("enabled")) {
+                    inputOutputFilteredData.setToolTipText(aParam.description);
+                    itsOutputFilteredData = aNode;
+                    boolean aSelection = false;
+                    if (isRef && aParam != null) {
+                        if (aParam.limits.equals("true") || aParam.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
+                    } else {
+                        if (aNode.limits.equals("true") || aNode.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
                     }
+                    inputOutputFilteredData.setSelected(aSelection);
+                    checkSettings();
                 }
-                inputOutputFilteredData.setSelected(aSelection);
-                checkSettings();
-            }
-        } else if (parentName.equals("Output_CoherentStokes")) {
-            if (aKeyName.equals("enabled")) {
-                inputOutputCoherentStokes.setToolTipText(aParam.description);
-                itsOutputCoherentStokes=aNode;
-                boolean aSelection = false;
-                if (isRef && aParam != null) {
-                    if (aParam.limits.equals("true")||aParam.limits.equals("TRUE")) {
-                        aSelection = true;
-                    }
-                } else {
-                    if (aNode.limits.equals("true")||aNode.limits.equals("TRUE")) {
-                        aSelection = true;
+                break;
+            case "Output_CoherentStokes":
+                if (aKeyName.equals("enabled")) {
+                    inputOutputCoherentStokes.setToolTipText(aParam.description);
+                    itsOutputCoherentStokes = aNode;
+                    boolean aSelection = false;
+                    if (isRef && aParam != null) {
+                        if (aParam.limits.equals("true") || aParam.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
+                    } else {
+                        if (aNode.limits.equals("true") || aNode.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
                     }
+                    inputOutputCoherentStokes.setSelected(aSelection);
+                    checkSettings();
                 }
-                inputOutputCoherentStokes.setSelected(aSelection);
-                checkSettings();
-            }
-        } else if (parentName.equals("Output_IncoherentStokes")) {
-            if (aKeyName.equals("enabled")) {
-                inputOutputIncoherentStokes.setToolTipText(aParam.description);
-                itsOutputIncoherentStokes=aNode;
-                boolean aSelection = false;
-                if (isRef && aParam != null) {
-                    if (aParam.limits.equals("true")||aParam.limits.equals("TRUE")) {
-                        aSelection = true;
-                    }
-                } else {
-                    if (aNode.limits.equals("true")||aNode.limits.equals("TRUE")) {
-                        aSelection = true;
+                break;
+            case "Output_IncoherentStokes":
+                if (aKeyName.equals("enabled")) {
+                    inputOutputIncoherentStokes.setToolTipText(aParam.description);
+                    itsOutputIncoherentStokes = aNode;
+                    boolean aSelection = false;
+                    if (isRef && aParam != null) {
+                        if (aParam.limits.equals("true") || aParam.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
+                    } else {
+                        if (aNode.limits.equals("true") || aNode.limits.equals("TRUE")) {
+                            aSelection = true;
+                        }
                     }
+                    inputOutputIncoherentStokes.setSelected(aSelection);
+                    checkSettings();
                 }
-                inputOutputIncoherentStokes.setSelected(aSelection);
-                checkSettings();
-            }
+                break;
         }
     }
 
     // check all settings to make a choice about enabled/disables fields
     private void checkSettings() {
-        if (inputOutputCorrelatedData.isSelected() ) {
+        if (inputOutputCorrelatedData.isSelected()) {
             inputIntegrationTime.setEnabled(true);
         } else {
             inputIntegrationTime.setEnabled(false);
@@ -670,200 +652,186 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
             inputFlysEye.setEnabled(false);
         }
 
-        if (inputOutputCoherentStokes.isSelected() ) { 
+        if (inputOutputCoherentStokes.isSelected()) {
             inputWhichCoherent.setEnabled(true);
-            inputIntegrateChannelsCoherent.setEnabled(true);
-            inputIntegrationStepsCoherent.setEnabled(true);
+            inputChannelsPerSubbandCoherent.setEnabled(true);
+            inputTimeIntegrationFactorCoherent.setEnabled(true);
         } else {
             inputWhichCoherent.setEnabled(false);
-            inputIntegrateChannelsCoherent.setEnabled(false);
-            inputIntegrationStepsCoherent.setEnabled(false);
+            inputChannelsPerSubbandCoherent.setEnabled(false);
+            inputTimeIntegrationFactorCoherent.setEnabled(false);
         }
-        if (inputOutputIncoherentStokes.isSelected() ) { 
+        if (inputOutputIncoherentStokes.isSelected()) {
             inputWhichIncoherent.setEnabled(true);
-            inputIntegrateChannelsIncoherent.setEnabled(true);
-            inputIntegrationStepsIncoherent.setEnabled(true);
+            inputChannelsPerSubbandIncoherent.setEnabled(true);
+            inputTimeIntegrationFactorIncoherent.setEnabled(true);
         } else {
             inputWhichIncoherent.setEnabled(false);
-            inputIntegrateChannelsIncoherent.setEnabled(false);
-            inputIntegrationStepsIncoherent.setEnabled(false);
+            inputChannelsPerSubbandIncoherent.setEnabled(false);
+            inputTimeIntegrationFactorIncoherent.setEnabled(false);
         }
 
     }
-    
+
     private void restore() {
-      boolean aB=false;
-
-      // Olap Specific parameters
-      if (itsDelayCompensation!=null) {
-        aB=false;
-        if (itsDelayCompensation.limits.equals("true")||itsDelayCompensation.limits.equals("TRUE")) {
-            aB=true;
-        }
-        inputDelayCompensation.setSelected(aB);
-      }
-      if (itsNrBitsPerSample!=null) {
-        inputNrBitsPerSample.setText(itsNrBitsPerSample.limits);
-      }
-      if (itsNrSecondsOfBuffer!=null) {
-        inputNrSecondsOfBuffer.setText(itsNrSecondsOfBuffer.limits);
-      }
-      if (itsNrTimesInFrame!=null) {
-        inputNrTimesInFrame.setText(itsNrTimesInFrame.limits);
-      }
-      if (itsCorrectBandPass!=null) {
-        aB=false;
-        if (itsCorrectBandPass.limits.equals("true")||itsCorrectBandPass.limits.equals("TRUE")) {
-          aB=true;
-        }
-        inputCorrectBandPass.setSelected(aB);
-      }
-      if (itsOutputCorrelatedData!=null) {
-        aB=false;
-        if (itsOutputCorrelatedData.limits.equals("true")||itsOutputCorrelatedData.limits.equals("TRUE")) {
-          aB=true;
-        }
-        inputOutputCorrelatedData.setSelected(aB);
-      }
-      if (itsOutputFilteredData!=null) {
-        aB=false;
-        if (itsOutputFilteredData.limits.equals("true")||itsOutputFilteredData.limits.equals("TRUE")) {
-          aB=true;
-        }
-        inputOutputFilteredData.setSelected(aB);
-      }
-      if (itsOutputBeamFormedData!=null) {
-        aB=false;
-        if (itsOutputBeamFormedData.limits.equals("true")||itsOutputBeamFormedData.limits.equals("TRUE")) {
-          aB=true;
-        }
-        inputOutputBeamFormedData.setSelected(aB);
-      }
-      if (itsOutputCoherentStokes!=null) {
-        aB=false;
-        if (itsOutputCoherentStokes.limits.equals("true")||itsOutputCoherentStokes.limits.equals("TRUE")) {
-          aB=true;
-        }
-        inputOutputCoherentStokes.setSelected(aB);
-      }
-      if (itsOutputIncoherentStokes!=null) {
-        aB=false;
-        if (itsOutputIncoherentStokes.limits.equals("true")||itsOutputIncoherentStokes.limits.equals("TRUE")) {
-          aB=true;
-        }
-        inputOutputIncoherentStokes.setSelected(aB);
-      }
-      if (itsMaxNetworkDelay!=null) {
-        inputMaxNetworkDelay.setText(itsMaxNetworkDelay.limits);
-      }
-      if (itsNrSubbandsPerFrame!=null) {
-        inputNrSubbandsPerFrame.setText(itsNrSubbandsPerFrame.limits);
-      }
-
-      //OLAP-CNProc
-      if (itsNrPPFTaps!=null) {
-        inputNrPPFTaps.setText(itsNrPPFTaps.limits);
-      }
-      
-      
-      //OLAP StorageProc
-      if (itsSubbandsPerMS!=null) {
-        inputSubbandsPerMS.setText(itsSubbandsPerMS.limits);
-      }
-
-      // Correlator
-      if (itsIntegrationTime!=null) {
-        inputIntegrationTime.setText(itsIntegrationTime.limits);
-      }
-
-      // PencilInfo
-      if (itsFlysEye!=null) {
-        aB=false;
-        if (itsFlysEye.limits.equals("true")||itsFlysEye.limits.equals("TRUE")) {
-          aB=true;
-        }
-        inputFlysEye.setSelected(aB);
-      }
-
-
-      // CNProc_CoherentStokes
-      if(itsWhichCoherent!=null) {
-        inputWhichCoherent.setSelectedItem(itsWhichCoherent.limits);
-      }
-      if (itsIntegrateChannelsCoherent!=null) {
-        aB=false;
-        if (itsIntegrateChannelsCoherent.limits.equals("true")||itsIntegrateChannelsCoherent.limits.equals("TRUE")) {
-          aB=true;
-        }
-        inputIntegrateChannelsCoherent.setSelected(aB);
-      }
-      if (itsIntegrationStepsCoherent!=null) {
-        inputIntegrationStepsCoherent.setText(itsIntegrationStepsCoherent.limits);
-      }
-
-      // CNProc_IncoherentStokes
-      if(itsWhichIncoherent!=null) {
-        inputWhichIncoherent.setSelectedItem(itsWhichIncoherent.limits);
-      }
-      if (itsIntegrateChannelsIncoherent!=null) {
-        aB=false;
-        if (itsIntegrateChannelsIncoherent.limits.equals("true")||itsIntegrateChannelsIncoherent.limits.equals("TRUE")) {
-          aB=true;
-        }
-        inputIntegrateChannelsIncoherent.setSelected(aB);
-      }
-      if (itsIntegrationStepsIncoherent!=null) {
-        inputIntegrationStepsIncoherent.setText(itsIntegrationStepsIncoherent.limits);
-      }
-
-     
-      checkSettings();
+        boolean aB = false;
+
+        // Olap Specific parameters
+        if (itsDelayCompensation != null) {
+            aB = false;
+            if (itsDelayCompensation.limits.equals("true") || itsDelayCompensation.limits.equals("TRUE")) {
+                aB = true;
+            }
+            inputDelayCompensation.setSelected(aB);
+        }
+        if (itsNrBitsPerSample != null) {
+            inputNrBitsPerSample.setText(itsNrBitsPerSample.limits);
+        }
+        if (itsNrSecondsOfBuffer != null) {
+            inputNrSecondsOfBuffer.setText(itsNrSecondsOfBuffer.limits);
+        }
+        if (itsNrTimesInFrame != null) {
+            inputNrTimesInFrame.setText(itsNrTimesInFrame.limits);
+        }
+        if (itsCorrectBandPass != null) {
+            aB = false;
+            if (itsCorrectBandPass.limits.equals("true") || itsCorrectBandPass.limits.equals("TRUE")) {
+                aB = true;
+            }
+            inputCorrectBandPass.setSelected(aB);
+        }
+        if (itsOutputCorrelatedData != null) {
+            aB = false;
+            if (itsOutputCorrelatedData.limits.equals("true") || itsOutputCorrelatedData.limits.equals("TRUE")) {
+                aB = true;
+            }
+            inputOutputCorrelatedData.setSelected(aB);
+        }
+        if (itsOutputFilteredData != null) {
+            aB = false;
+            if (itsOutputFilteredData.limits.equals("true") || itsOutputFilteredData.limits.equals("TRUE")) {
+                aB = true;
+            }
+            inputOutputFilteredData.setSelected(aB);
+        }
+        if (itsOutputBeamFormedData != null) {
+            aB = false;
+            if (itsOutputBeamFormedData.limits.equals("true") || itsOutputBeamFormedData.limits.equals("TRUE")) {
+                aB = true;
+            }
+            inputOutputBeamFormedData.setSelected(aB);
+        }
+        if (itsOutputCoherentStokes != null) {
+            aB = false;
+            if (itsOutputCoherentStokes.limits.equals("true") || itsOutputCoherentStokes.limits.equals("TRUE")) {
+                aB = true;
+            }
+            inputOutputCoherentStokes.setSelected(aB);
+        }
+        if (itsOutputIncoherentStokes != null) {
+            aB = false;
+            if (itsOutputIncoherentStokes.limits.equals("true") || itsOutputIncoherentStokes.limits.equals("TRUE")) {
+                aB = true;
+            }
+            inputOutputIncoherentStokes.setSelected(aB);
+        }
+        if (itsMaxNetworkDelay != null) {
+            inputMaxNetworkDelay.setText(itsMaxNetworkDelay.limits);
+        }
+        if (itsNrSubbandsPerFrame != null) {
+            inputNrSubbandsPerFrame.setText(itsNrSubbandsPerFrame.limits);
+        }
+
+        //OLAP-CNProc
+        if (itsNrPPFTaps != null) {
+            inputNrPPFTaps.setText(itsNrPPFTaps.limits);
+        }
+
+
+        // Correlator
+        if (itsIntegrationTime != null) {
+            inputIntegrationTime.setText(itsIntegrationTime.limits);
+        }
+
+        // PencilInfo
+        if (itsFlysEye != null) {
+            aB = false;
+            if (itsFlysEye.limits.equals("true") || itsFlysEye.limits.equals("TRUE")) {
+                aB = true;
+            }
+            inputFlysEye.setSelected(aB);
+        }
+
+
+        // CNProc_CoherentStokes
+        if (itsWhichCoherent != null) {
+            inputWhichCoherent.setSelectedItem(itsWhichCoherent.limits);
+        }
+        if (itsChannelsPerSubbandCoherent != null) {
+            inputChannelsPerSubbandCoherent.setText(itsChannelsPerSubbandCoherent.limits);
+        }
+        if (itsTimeIntegrationFactorCoherent != null) {
+            inputTimeIntegrationFactorCoherent.setText(itsTimeIntegrationFactorCoherent.limits);
+        }
+
+        // CNProc_IncoherentStokes
+        if (itsWhichIncoherent != null) {
+            inputWhichIncoherent.setSelectedItem(itsWhichIncoherent.limits);
+        }
+        if (itsChannelsPerSubbandIncoherent != null) {
+            inputChannelsPerSubbandIncoherent.setText(itsChannelsPerSubbandIncoherent.limits);
+        }
+        if (itsTimeIntegrationFactorIncoherent != null) {
+            inputTimeIntegrationFactorIncoherent.setText(itsTimeIntegrationFactorIncoherent.limits);
+        }
+
+
+        checkSettings();
     }
-     
+
     private void initialize() {
         buttonPanel1.addButton("Restore");
-        buttonPanel1.setButtonIcon("Restore",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_undo.png")));
+        buttonPanel1.setButtonIcon("Restore", new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_undo.png")));
         buttonPanel1.addButton("Apply");
-        buttonPanel1.setButtonIcon("Apply",new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_apply.png")));
-   }
-    
+        buttonPanel1.setButtonIcon("Apply", new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_apply.png")));
+    }
+
     private void initPanel() {
         // check access
         UserAccount userAccount = itsMainFrame.getUserAccount();
 
         // for now:
         setAllEnabled(true);
-        
-        if(userAccount.isAdministrator()) {
+
+        if (userAccount.isAdministrator()) {
             // enable/disable certain controls
         }
-        if(userAccount.isAstronomer()) {
+        if (userAccount.isAstronomer()) {
             // enable/disable certain controls
         }
-        if(userAccount.isInstrumentScientist()) {
+        if (userAccount.isInstrumentScientist()) {
             // enable/disable certain controls
         }
-        
 
-         if (itsNode != null) {
+
+        if (itsNode != null) {
             try {
                 //figure out the caller
-                jOTDBtree aTree = OtdbRmi.getRemoteOTDB().getTreeInfo(itsNode.treeID(),false);
-                itsTreeType=OtdbRmi.getTreeType().get(aTree.type);
+                jOTDBtree aTree = OtdbRmi.getRemoteOTDB().getTreeInfo(itsNode.treeID(), false);
+                itsTreeType = OtdbRmi.getTreeType().get(aTree.type);
             } catch (RemoteException ex) {
-                String aS="OlapPanel: Error getting treeInfo/treetype" + ex;
+                String aS = "OlapPanel: Error getting treeInfo/treetype" + ex;
                 logger.error(aS);
-                LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-                itsTreeType="";
+                LofarUtils.showErrorPanel(this, aS, new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+                itsTreeType = "";
             }
-         } else {
+        } else {
             logger.error("no node given");
         }
 
         restore();
-    }   
-    
- 
+    }
+
     /** saves the given node back to the database
      */
     private void saveNode(jOTDBnode aNode) {
@@ -871,15 +839,14 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
             return;
         }
         try {
-            OtdbRmi.getRemoteMaintenance().saveNode(aNode); 
+            OtdbRmi.getRemoteMaintenance().saveNode(aNode);
         } catch (RemoteException ex) {
-            String aS="Error: saveNode failed : " + ex;
+            String aS = "Error: saveNode failed : " + ex;
             logger.error(aS);
-            LofarUtils.showErrorPanel(this,aS,new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
-        } 
-    }   
-    
-   
+            LofarUtils.showErrorPanel(this, aS, new javax.swing.ImageIcon(getClass().getResource("/nl/astron/lofar/sas/otb/icons/16_warn.gif")));
+        }
+    }
+
     /** Enables/disables the buttons
      *
      * @param   enabled     true/false enabled/disabled
@@ -890,15 +857,15 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
         // always do this last to keep up with panel settings regardless of user settings
         checkSettings();
     }
-    
+
     /** Sets the buttons visible/invisible
      *
      * @param   visible     true/false visible/invisible
      */
     @Override
     public void setButtonsVisible(boolean visible) {
-    }        
-    
+    }
+
     /** Enables/disables the complete form
      *
      * @param   enabled     true/false enabled/disabled
@@ -909,19 +876,18 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
         // always do this last to keep up with panel settings regardless of user settings
         checkSettings();
     }
-    
+
     private boolean saveInput() {
         boolean hasChanged = false;
 
         // Generic OLAP       
-        if ((!inputDelayCompensation.isSelected() &  
-                (itsDelayCompensation.limits.equals("TRUE") ||itsDelayCompensation.limits.equals("true") )) ||
-            (inputDelayCompensation.isSelected() &  
-                (itsDelayCompensation.limits.equals("FALSE") ||itsDelayCompensation.limits.equals("false") )))        
-        {  
-            String delay="true";
+        if ((!inputDelayCompensation.isSelected()
+                & (itsDelayCompensation.limits.equals("TRUE") || itsDelayCompensation.limits.equals("true")))
+                || (inputDelayCompensation.isSelected()
+                & (itsDelayCompensation.limits.equals("FALSE") || itsDelayCompensation.limits.equals("false")))) {
+            String delay = "true";
             if (!inputDelayCompensation.isSelected()) {
-                delay="false";
+                delay = "false";
             }
             itsDelayCompensation.limits = delay;
             saveNode(itsDelayCompensation);
@@ -933,7 +899,7 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
         if (itsNrSubbandsPerFrame != null && !inputNrSubbandsPerFrame.getText().equals(itsNrSubbandsPerFrame.limits)) {
             itsNrSubbandsPerFrame.limits = inputNrSubbandsPerFrame.getText();
             saveNode(itsNrSubbandsPerFrame);
-        }      
+        }
         if (itsNrBitsPerSample != null && !inputNrBitsPerSample.getText().equals(itsNrBitsPerSample.limits)) {
             itsNrBitsPerSample.limits = inputNrBitsPerSample.getText();
             saveNode(itsNrBitsPerSample);
@@ -946,81 +912,75 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
             itsNrTimesInFrame.limits = inputNrTimesInFrame.getText();
             saveNode(itsNrTimesInFrame);
         }
-        if ((!inputCorrectBandPass.isSelected() &  
-                (itsCorrectBandPass.limits.equals("TRUE") ||itsCorrectBandPass.limits.equals("true") )) ||
-            (inputCorrectBandPass.isSelected() &  
-                (itsCorrectBandPass.limits.equals("FALSE") ||itsCorrectBandPass.limits.equals("false") )))        
-        {  
-            String bp="true";
+        if ((!inputCorrectBandPass.isSelected()
+                & (itsCorrectBandPass.limits.equals("TRUE") || itsCorrectBandPass.limits.equals("true")))
+                || (inputCorrectBandPass.isSelected()
+                & (itsCorrectBandPass.limits.equals("FALSE") || itsCorrectBandPass.limits.equals("false")))) {
+            String bp = "true";
             if (!inputCorrectBandPass.isSelected()) {
-                bp="false";
+                bp = "false";
             }
             itsCorrectBandPass.limits = bp;
             saveNode(itsCorrectBandPass);
         }
 
-        if ((!inputOutputCorrelatedData.isSelected() &
-                (itsOutputCorrelatedData.limits.equals("TRUE") ||itsOutputCorrelatedData.limits.equals("true") )) ||
-            (inputOutputCorrelatedData.isSelected() &
-                (itsOutputCorrelatedData.limits.equals("FALSE") ||itsOutputCorrelatedData.limits.equals("false") )))
-        {
-            String bp="true";
+        if ((!inputOutputCorrelatedData.isSelected()
+                & (itsOutputCorrelatedData.limits.equals("TRUE") || itsOutputCorrelatedData.limits.equals("true")))
+                || (inputOutputCorrelatedData.isSelected()
+                & (itsOutputCorrelatedData.limits.equals("FALSE") || itsOutputCorrelatedData.limits.equals("false")))) {
+            String bp = "true";
             if (!inputOutputCorrelatedData.isSelected()) {
-                bp="false";
+                bp = "false";
             }
             itsOutputCorrelatedData.limits = bp;
             saveNode(itsOutputCorrelatedData);
         }
-        if ((!inputOutputFilteredData.isSelected() &
-                (itsOutputFilteredData.limits.equals("TRUE") ||itsOutputFilteredData.limits.equals("true") )) ||
-            (inputOutputFilteredData.isSelected() &
-                (itsOutputFilteredData.limits.equals("FALSE") ||itsOutputFilteredData.limits.equals("false") )))
-        {
-            String bp="true";
+        if ((!inputOutputFilteredData.isSelected()
+                & (itsOutputFilteredData.limits.equals("TRUE") || itsOutputFilteredData.limits.equals("true")))
+                || (inputOutputFilteredData.isSelected()
+                & (itsOutputFilteredData.limits.equals("FALSE") || itsOutputFilteredData.limits.equals("false")))) {
+            String bp = "true";
             if (!inputOutputFilteredData.isSelected()) {
-                bp="false";
+                bp = "false";
             }
             itsOutputFilteredData.limits = bp;
             saveNode(itsOutputFilteredData);
         }
-        if ((!inputOutputBeamFormedData.isSelected() &
-                (itsOutputBeamFormedData.limits.equals("TRUE") ||itsOutputBeamFormedData.limits.equals("true") )) ||
-            (inputOutputBeamFormedData.isSelected() &
-                (itsOutputBeamFormedData.limits.equals("FALSE") ||itsOutputBeamFormedData.limits.equals("false") )))
-        {
-            String bp="true";
+        if ((!inputOutputBeamFormedData.isSelected()
+                & (itsOutputBeamFormedData.limits.equals("TRUE") || itsOutputBeamFormedData.limits.equals("true")))
+                || (inputOutputBeamFormedData.isSelected()
+                & (itsOutputBeamFormedData.limits.equals("FALSE") || itsOutputBeamFormedData.limits.equals("false")))) {
+            String bp = "true";
             if (!inputOutputBeamFormedData.isSelected()) {
-                bp="false";
+                bp = "false";
             }
             itsOutputBeamFormedData.limits = bp;
             saveNode(itsOutputBeamFormedData);
         }
-        if ((!inputOutputCoherentStokes.isSelected() &
-                (itsOutputCoherentStokes.limits.equals("TRUE") ||itsOutputCoherentStokes.limits.equals("true") )) ||
-            (inputOutputCoherentStokes.isSelected() &
-                (itsOutputCoherentStokes.limits.equals("FALSE") ||itsOutputCoherentStokes.limits.equals("false") )))
-        {
-            String bp="true";
+        if ((!inputOutputCoherentStokes.isSelected()
+                & (itsOutputCoherentStokes.limits.equals("TRUE") || itsOutputCoherentStokes.limits.equals("true")))
+                || (inputOutputCoherentStokes.isSelected()
+                & (itsOutputCoherentStokes.limits.equals("FALSE") || itsOutputCoherentStokes.limits.equals("false")))) {
+            String bp = "true";
             if (!inputOutputCoherentStokes.isSelected()) {
-                bp="false";
+                bp = "false";
             }
             itsOutputCoherentStokes.limits = bp;
             saveNode(itsOutputCoherentStokes);
         }
-        if ((!inputOutputIncoherentStokes.isSelected() &
-                (itsOutputIncoherentStokes.limits.equals("TRUE") ||itsOutputIncoherentStokes.limits.equals("true") )) ||
-            (inputOutputIncoherentStokes.isSelected() &
-                (itsOutputIncoherentStokes.limits.equals("FALSE") ||itsOutputIncoherentStokes.limits.equals("false") )))
-        {
-            String bp="true";
+        if ((!inputOutputIncoherentStokes.isSelected()
+                & (itsOutputIncoherentStokes.limits.equals("TRUE") || itsOutputIncoherentStokes.limits.equals("true")))
+                || (inputOutputIncoherentStokes.isSelected()
+                & (itsOutputIncoherentStokes.limits.equals("FALSE") || itsOutputIncoherentStokes.limits.equals("false")))) {
+            String bp = "true";
             if (!inputOutputIncoherentStokes.isSelected()) {
-                bp="false";
+                bp = "false";
             }
             itsOutputIncoherentStokes.limits = bp;
             saveNode(itsOutputIncoherentStokes);
         }
 
-        
+
         // OLAP-CNProc
         if (itsNrPPFTaps != null && !inputNrPPFTaps.getText().equals(itsNrPPFTaps.limits)) {
             itsNrPPFTaps.limits = inputNrPPFTaps.getText();
@@ -1028,12 +988,6 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
         }
 
 
-        //OLAP-StorageProc
-        if (itsSubbandsPerMS != null && !inputSubbandsPerMS.getText().equals(itsSubbandsPerMS.limits)) {
-            itsSubbandsPerMS.limits = inputSubbandsPerMS.getText();
-            saveNode(itsSubbandsPerMS);
-        }
-
         // Correlator
         if (itsIntegrationTime != null && !inputIntegrationTime.getText().equals(itsIntegrationTime.limits)) {
             itsIntegrationTime.limits = inputIntegrationTime.getText();
@@ -1041,69 +995,51 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
         }
 
         // PencilInfo
-        if ((!inputFlysEye.isSelected() &
-                (itsFlysEye.limits.equals("TRUE") ||itsFlysEye.limits.equals("true") )) ||
-            (inputFlysEye.isSelected() &
-                (itsFlysEye.limits.equals("FALSE") ||itsFlysEye.limits.equals("false") )))
-        {
-            String rt="true";
+        if ((!inputFlysEye.isSelected()
+                & (itsFlysEye.limits.equals("TRUE") || itsFlysEye.limits.equals("true")))
+                || (inputFlysEye.isSelected()
+                & (itsFlysEye.limits.equals("FALSE") || itsFlysEye.limits.equals("false")))) {
+            String rt = "true";
             if (!inputFlysEye.isSelected()) {
-                rt="false";
+                rt = "false";
             }
             itsFlysEye.limits = rt;
             saveNode(itsFlysEye);
         }
 
         // CNProc_CoherentStokes
-        if (itsWhichCoherent!= null && !inputWhichCoherent.getSelectedItem().toString().equals(itsWhichCoherent.limits)) {
+        if (itsWhichCoherent != null && !inputWhichCoherent.getSelectedItem().toString().equals(itsWhichCoherent.limits)) {
             itsWhichCoherent.limits = inputWhichCoherent.getSelectedItem().toString();
             saveNode(itsWhichCoherent);
         }
 
-        if (itsIntegrationStepsCoherent != null && !inputIntegrationStepsCoherent.getText().equals(itsIntegrationStepsCoherent.limits)) {
-            itsIntegrationStepsCoherent.limits = inputIntegrationStepsCoherent.getText();
-            saveNode(itsIntegrationStepsCoherent);
+        if (itsTimeIntegrationFactorCoherent != null && !inputTimeIntegrationFactorCoherent.getText().equals(itsTimeIntegrationFactorCoherent.limits)) {
+            itsTimeIntegrationFactorCoherent.limits = inputTimeIntegrationFactorCoherent.getText();
+            saveNode(itsTimeIntegrationFactorCoherent);
         }
-        if ((!inputIntegrateChannelsCoherent.isSelected() &
-                (itsIntegrateChannelsCoherent.limits.equals("TRUE") ||itsIntegrateChannelsCoherent.limits.equals("true") )) ||
-            (inputIntegrateChannelsCoherent.isSelected() &
-                (itsIntegrateChannelsCoherent.limits.equals("FALSE") ||itsIntegrateChannelsCoherent.limits.equals("false") )))
-        {
-            String rt="true";
-            if (!inputIntegrateChannelsCoherent.isSelected()) {
-                rt="false";
-            }
-            itsIntegrateChannelsCoherent.limits = rt;
-            saveNode(itsIntegrateChannelsCoherent);
+        if (itsChannelsPerSubbandCoherent != null && inputChannelsPerSubbandCoherent.getText().equals(itsChannelsPerSubbandCoherent.limits)) {
+            itsChannelsPerSubbandCoherent.limits = inputChannelsPerSubbandCoherent.getText();
+            saveNode(itsChannelsPerSubbandCoherent);
         }
 
         // CNProc_IncoherentStokes
-        if (itsWhichIncoherent!= null && !inputWhichIncoherent.getSelectedItem().toString().equals(itsWhichIncoherent.limits)) {
+        if (itsWhichIncoherent != null && !inputWhichIncoherent.getSelectedItem().toString().equals(itsWhichIncoherent.limits)) {
             itsWhichIncoherent.limits = inputWhichIncoherent.getSelectedItem().toString();
             saveNode(itsWhichIncoherent);
         }
 
-        if (itsIntegrationStepsIncoherent != null && !inputIntegrationStepsIncoherent.getText().equals(itsIntegrationStepsIncoherent.limits)) {
-            itsIntegrationStepsIncoherent.limits = inputIntegrationStepsIncoherent.getText();
-            saveNode(itsIntegrationStepsIncoherent);
+        if (itsTimeIntegrationFactorIncoherent != null && !inputTimeIntegrationFactorIncoherent.getText().equals(itsTimeIntegrationFactorIncoherent.limits)) {
+            itsTimeIntegrationFactorIncoherent.limits = inputTimeIntegrationFactorIncoherent.getText();
+            saveNode(itsTimeIntegrationFactorIncoherent);
         }
-        if ((!inputIntegrateChannelsIncoherent.isSelected() &
-                (itsIntegrateChannelsIncoherent.limits.equals("TRUE") ||itsIntegrateChannelsIncoherent.limits.equals("true") )) ||
-            (inputIntegrateChannelsIncoherent.isSelected() &
-                (itsIntegrateChannelsIncoherent.limits.equals("FALSE") ||itsIntegrateChannelsIncoherent.limits.equals("false") )))
-        {
-            String rt="true";
-            if (!inputIntegrateChannelsIncoherent.isSelected()) {
-                rt="false";
-            }
-            itsIntegrateChannelsIncoherent.limits = rt;
-            saveNode(itsIntegrateChannelsIncoherent);
+        if (itsChannelsPerSubbandIncoherent != null && inputChannelsPerSubbandIncoherent.getText().equals(itsChannelsPerSubbandIncoherent.limits)) {
+            itsChannelsPerSubbandIncoherent.limits = inputChannelsPerSubbandIncoherent.getText();
+            saveNode(itsChannelsPerSubbandIncoherent);
         }
-        
+
         return true;
     }
 
-    
     /** This method is called from within the constructor to
      * initialize the form.
      * WARNING: Do NOT modify this code. The content of this method is
@@ -1142,25 +1078,24 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
         jPanel7 = new javax.swing.JPanel();
         labelNrPPFTaps = new javax.swing.JLabel();
         inputNrPPFTaps = new javax.swing.JTextField();
-        jPanel11 = new javax.swing.JPanel();
-        labelSubbandsPerMs = new javax.swing.JLabel();
-        inputSubbandsPerMS = new javax.swing.JTextField();
-        CorrelatorPanel = new javax.swing.JPanel();
-        labelIntegrationTime = new javax.swing.JLabel();
-        inputIntegrationTime = new javax.swing.JTextField();
-        jLabel2 = new javax.swing.JLabel();
         CoherentStokesPanel = new javax.swing.JPanel();
         labelWhichCoherent = new javax.swing.JLabel();
         inputWhichCoherent = new javax.swing.JComboBox();
-        inputIntegrateChannelsCoherent = new javax.swing.JCheckBox();
-        labelIntegrationstepsCoherent = new javax.swing.JLabel();
-        inputIntegrationStepsCoherent = new javax.swing.JTextField();
+        timeIntegrationFactorCoherent = new javax.swing.JLabel();
+        inputTimeIntegrationFactorCoherent = new javax.swing.JTextField();
+        labelChannelsPerSubbandCoherent = new javax.swing.JLabel();
+        inputChannelsPerSubbandCoherent = new javax.swing.JTextField();
         IncoherentStokesPanel = new javax.swing.JPanel();
         labelWhichIncoherent = new javax.swing.JLabel();
         inputWhichIncoherent = new javax.swing.JComboBox();
-        inputIntegrateChannelsIncoherent = new javax.swing.JCheckBox();
-        labelIntegrationstepsIncoherent = new javax.swing.JLabel();
-        inputIntegrationStepsIncoherent = new javax.swing.JTextField();
+        labelTimeIntegrationfactorCoherent = new javax.swing.JLabel();
+        inputTimeIntegrationFactorIncoherent = new javax.swing.JTextField();
+        labelChannelsPerSubbandIncoherent = new javax.swing.JLabel();
+        inputChannelsPerSubbandIncoherent = new javax.swing.JTextField();
+        CorrelatorPanel = new javax.swing.JPanel();
+        labelIntegrationTime = new javax.swing.JLabel();
+        inputIntegrationTime = new javax.swing.JTextField();
+        jLabel2 = new javax.swing.JLabel();
         PencilInfoPanel = new javax.swing.JPanel();
         inputFlysEye = new javax.swing.JCheckBox();
         buttonPanel1 = new nl.astron.lofar.sas.otbcomponents.ButtonPanel();
@@ -1183,7 +1118,7 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
         jPanel1.setPreferredSize(new java.awt.Dimension(100, 25));
         jPanel1.setLayout(new java.awt.BorderLayout());
 
-        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
+        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
         jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
         jLabel1.setText("OLAP Details");
         jPanel1.add(jLabel1, java.awt.BorderLayout.CENTER);
@@ -1299,7 +1234,7 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
                     .addGroup(jPanel8Layout.createSequentialGroup()
                         .addGap(43, 43, 43)
                         .addComponent(jLabel3)))
-                .addContainerGap(127, Short.MAX_VALUE))
+                .addContainerGap(142, Short.MAX_VALUE))
         );
         jPanel8Layout.setVerticalGroup(
             jPanel8Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
@@ -1355,79 +1290,6 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
 
         labelNrPPFTaps.setText("# PPFTaps:");
 
-        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
-        jPanel7.setLayout(jPanel7Layout);
-        jPanel7Layout.setHorizontalGroup(
-            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGroup(jPanel7Layout.createSequentialGroup()
-                .addComponent(labelNrPPFTaps)
-                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                .addComponent(inputNrPPFTaps, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
-                .addContainerGap(47, Short.MAX_VALUE))
-        );
-        jPanel7Layout.setVerticalGroup(
-            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGroup(jPanel7Layout.createSequentialGroup()
-                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
-                    .addComponent(labelNrPPFTaps)
-                    .addComponent(inputNrPPFTaps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
-                .addContainerGap(59, Short.MAX_VALUE))
-        );
-
-        jPanel11.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Storage Proc", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
-
-        labelSubbandsPerMs.setText("#subbands per MS:");
-
-        javax.swing.GroupLayout jPanel11Layout = new javax.swing.GroupLayout(jPanel11);
-        jPanel11.setLayout(jPanel11Layout);
-        jPanel11Layout.setHorizontalGroup(
-            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGroup(jPanel11Layout.createSequentialGroup()
-                .addComponent(labelSubbandsPerMs)
-                .addGap(18, 18, 18)
-                .addComponent(inputSubbandsPerMS, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
-                .addContainerGap(24, Short.MAX_VALUE))
-        );
-        jPanel11Layout.setVerticalGroup(
-            jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGroup(jPanel11Layout.createSequentialGroup()
-                .addGroup(jPanel11Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
-                    .addComponent(labelSubbandsPerMs)
-                    .addComponent(inputSubbandsPerMS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
-                .addContainerGap(52, Short.MAX_VALUE))
-        );
-
-        CorrelatorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Correlator", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
-        CorrelatorPanel.setToolTipText("Correlator");
-
-        labelIntegrationTime.setText("integration Time:");
-
-        inputIntegrationTime.setEnabled(false);
-
-        jLabel2.setText("sec");
-
-        javax.swing.GroupLayout CorrelatorPanelLayout = new javax.swing.GroupLayout(CorrelatorPanel);
-        CorrelatorPanel.setLayout(CorrelatorPanelLayout);
-        CorrelatorPanelLayout.setHorizontalGroup(
-            CorrelatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGroup(CorrelatorPanelLayout.createSequentialGroup()
-                .addComponent(labelIntegrationTime, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
-                .addGap(24, 24, 24)
-                .addComponent(inputIntegrationTime, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
-                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
-                .addComponent(jLabel2)
-                .addContainerGap(28, Short.MAX_VALUE))
-        );
-        CorrelatorPanelLayout.setVerticalGroup(
-            CorrelatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-            .addGroup(CorrelatorPanelLayout.createSequentialGroup()
-                .addGroup(CorrelatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
-                    .addComponent(labelIntegrationTime)
-                    .addComponent(inputIntegrationTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
-                    .addComponent(jLabel2))
-                .addContainerGap(69, Short.MAX_VALUE))
-        );
-
         CoherentStokesPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Coherent Stokes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
         CoherentStokesPanel.setToolTipText("Stokes");
 
@@ -1436,14 +1298,13 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
         inputWhichCoherent.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
         inputWhichCoherent.setEnabled(false);
 
-        inputIntegrateChannelsCoherent.setText("integrate Channels");
-        inputIntegrateChannelsCoherent.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
-        inputIntegrateChannelsCoherent.setEnabled(false);
-        inputIntegrateChannelsCoherent.setMargin(new java.awt.Insets(0, 0, 0, 0));
+        timeIntegrationFactorCoherent.setText("time integration factor");
 
-        labelIntegrationstepsCoherent.setText("integration Steps:");
+        inputTimeIntegrationFactorCoherent.setEnabled(false);
 
-        inputIntegrationStepsCoherent.setEnabled(false);
+        labelChannelsPerSubbandCoherent.setText("# channels per subband");
+
+        inputChannelsPerSubbandCoherent.setEnabled(false);
 
         javax.swing.GroupLayout CoherentStokesPanelLayout = new javax.swing.GroupLayout(CoherentStokesPanel);
         CoherentStokesPanel.setLayout(CoherentStokesPanelLayout);
@@ -1451,29 +1312,30 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
             CoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(CoherentStokesPanelLayout.createSequentialGroup()
                 .addGroup(CoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-                    .addComponent(inputIntegrateChannelsCoherent)
-                    .addGroup(CoherentStokesPanelLayout.createSequentialGroup()
-                        .addGroup(CoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
-                            .addComponent(labelIntegrationstepsCoherent, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                            .addComponent(labelWhichCoherent, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
-                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                        .addGroup(CoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
-                            .addComponent(inputIntegrationStepsCoherent)
-                            .addComponent(inputWhichCoherent, 0, 173, Short.MAX_VALUE))))
-                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+                    .addComponent(labelChannelsPerSubbandCoherent, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
+                    .addComponent(timeIntegrationFactorCoherent, javax.swing.GroupLayout.DEFAULT_SIZE, 141, Short.MAX_VALUE)
+                    .addComponent(labelWhichCoherent, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addGroup(CoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
+                    .addComponent(inputChannelsPerSubbandCoherent)
+                    .addComponent(inputTimeIntegrationFactorCoherent, javax.swing.GroupLayout.Alignment.LEADING)
+                    .addComponent(inputWhichCoherent, 0, 173, Short.MAX_VALUE))
+                .addContainerGap())
         );
         CoherentStokesPanelLayout.setVerticalGroup(
             CoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(CoherentStokesPanelLayout.createSequentialGroup()
                 .addGroup(CoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
-                    .addComponent(labelWhichCoherent)
-                    .addComponent(inputWhichCoherent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+                    .addComponent(inputWhichCoherent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                    .addComponent(labelWhichCoherent, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                 .addGap(8, 8, 8)
                 .addGroup(CoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
-                    .addComponent(labelIntegrationstepsCoherent)
-                    .addComponent(inputIntegrationStepsCoherent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
-                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
-                .addComponent(inputIntegrateChannelsCoherent, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
+                    .addComponent(inputTimeIntegrationFactorCoherent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                    .addComponent(timeIntegrationFactorCoherent))
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addGroup(CoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+                    .addComponent(inputChannelsPerSubbandCoherent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                    .addComponent(labelChannelsPerSubbandCoherent))
                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
         );
 
@@ -1485,44 +1347,103 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
         inputWhichIncoherent.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
         inputWhichIncoherent.setEnabled(false);
 
-        inputIntegrateChannelsIncoherent.setText("integrate Channels");
-        inputIntegrateChannelsIncoherent.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
-        inputIntegrateChannelsIncoherent.setEnabled(false);
-        inputIntegrateChannelsIncoherent.setMargin(new java.awt.Insets(0, 0, 0, 0));
+        labelTimeIntegrationfactorCoherent.setText("time integration factor");
+
+        inputTimeIntegrationFactorIncoherent.setEnabled(false);
 
-        labelIntegrationstepsIncoherent.setText("integration Steps:");
+        labelChannelsPerSubbandIncoherent.setText("# channels per subband");
 
-        inputIntegrationStepsIncoherent.setEnabled(false);
+        inputChannelsPerSubbandIncoherent.setEnabled(false);
 
         javax.swing.GroupLayout IncoherentStokesPanelLayout = new javax.swing.GroupLayout(IncoherentStokesPanel);
         IncoherentStokesPanel.setLayout(IncoherentStokesPanelLayout);
         IncoherentStokesPanelLayout.setHorizontalGroup(
             IncoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(IncoherentStokesPanelLayout.createSequentialGroup()
+                .addContainerGap()
                 .addGroup(IncoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-                    .addComponent(inputIntegrateChannelsIncoherent)
-                    .addGroup(IncoherentStokesPanelLayout.createSequentialGroup()
-                        .addGroup(IncoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
-                            .addComponent(labelIntegrationstepsIncoherent, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                            .addComponent(labelWhichIncoherent, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 103, Short.MAX_VALUE))
-                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
-                        .addGroup(IncoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
-                            .addComponent(inputIntegrationStepsIncoherent)
-                            .addComponent(inputWhichIncoherent, 0, 173, Short.MAX_VALUE))))
-                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+                    .addComponent(labelChannelsPerSubbandIncoherent, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
+                    .addComponent(labelTimeIntegrationfactorCoherent, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
+                    .addComponent(labelWhichIncoherent, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE))
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addGroup(IncoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
+                    .addComponent(inputChannelsPerSubbandIncoherent)
+                    .addComponent(inputTimeIntegrationFactorIncoherent, javax.swing.GroupLayout.Alignment.LEADING)
+                    .addComponent(inputWhichIncoherent, 0, 173, Short.MAX_VALUE))
+                .addContainerGap())
         );
         IncoherentStokesPanelLayout.setVerticalGroup(
             IncoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(IncoherentStokesPanelLayout.createSequentialGroup()
                 .addGroup(IncoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
-                    .addComponent(labelWhichIncoherent)
-                    .addComponent(inputWhichIncoherent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+                    .addComponent(inputWhichIncoherent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                    .addComponent(labelWhichIncoherent))
                 .addGap(8, 8, 8)
                 .addGroup(IncoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
-                    .addComponent(labelIntegrationstepsIncoherent)
-                    .addComponent(inputIntegrationStepsIncoherent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+                    .addComponent(inputTimeIntegrationFactorIncoherent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                    .addComponent(labelTimeIntegrationfactorCoherent))
                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
-                .addComponent(inputIntegrateChannelsIncoherent, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE))
+                .addGroup(IncoherentStokesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+                    .addComponent(inputChannelsPerSubbandIncoherent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                    .addComponent(labelChannelsPerSubbandIncoherent))
+                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+        );
+
+        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
+        jPanel7.setLayout(jPanel7Layout);
+        jPanel7Layout.setHorizontalGroup(
+            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addGroup(jPanel7Layout.createSequentialGroup()
+                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+                    .addGroup(jPanel7Layout.createSequentialGroup()
+                        .addComponent(labelNrPPFTaps)
+                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                        .addComponent(inputNrPPFTaps, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE))
+                    .addComponent(CoherentStokesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+                    .addComponent(IncoherentStokesPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+                .addContainerGap())
+        );
+        jPanel7Layout.setVerticalGroup(
+            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addGroup(jPanel7Layout.createSequentialGroup()
+                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+                    .addComponent(labelNrPPFTaps)
+                    .addComponent(inputNrPPFTaps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+                .addGap(26, 26, 26)
+                .addComponent(CoherentStokesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
+                .addComponent(IncoherentStokesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+                .addContainerGap())
+        );
+
+        CorrelatorPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Correlator", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
+        CorrelatorPanel.setToolTipText("Correlator");
+
+        labelIntegrationTime.setText("integration Time:");
+
+        inputIntegrationTime.setEnabled(false);
+
+        jLabel2.setText("sec");
+
+        javax.swing.GroupLayout CorrelatorPanelLayout = new javax.swing.GroupLayout(CorrelatorPanel);
+        CorrelatorPanel.setLayout(CorrelatorPanelLayout);
+        CorrelatorPanelLayout.setHorizontalGroup(
+            CorrelatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addGroup(CorrelatorPanelLayout.createSequentialGroup()
+                .addComponent(labelIntegrationTime, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                .addComponent(inputIntegrationTime, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
+                .addComponent(jLabel2))
+        );
+        CorrelatorPanelLayout.setVerticalGroup(
+            CorrelatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+            .addGroup(CorrelatorPanelLayout.createSequentialGroup()
+                .addGroup(CorrelatorPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
+                    .addComponent(labelIntegrationTime)
+                    .addComponent(inputIntegrationTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                    .addComponent(jLabel2))
+                .addContainerGap(49, Short.MAX_VALUE))
         );
 
         PencilInfoPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "PencilInfo", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N
@@ -1545,13 +1466,13 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
             .addGroup(PencilInfoPanelLayout.createSequentialGroup()
                 .addContainerGap()
                 .addComponent(inputFlysEye)
-                .addContainerGap(210, Short.MAX_VALUE))
+                .addContainerGap(65, Short.MAX_VALUE))
         );
         PencilInfoPanelLayout.setVerticalGroup(
             PencilInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(PencilInfoPanelLayout.createSequentialGroup()
                 .addComponent(inputFlysEye, javax.swing.GroupLayout.PREFERRED_SIZE, 13, javax.swing.GroupLayout.PREFERRED_SIZE)
-                .addContainerGap(59, Short.MAX_VALUE))
+                .addContainerGap(19, Short.MAX_VALUE))
         );
 
         javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
@@ -1559,41 +1480,32 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
         jPanel4Layout.setHorizontalGroup(
             jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(jPanel4Layout.createSequentialGroup()
-                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
-                    .addComponent(jPanel8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel4Layout.createSequentialGroup()
+                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+                    .addGroup(jPanel4Layout.createSequentialGroup()
                         .addContainerGap()
+                        .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                         .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-                            .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                            .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
-                        .addGap(18, 18, 18)
-                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
-                            .addComponent(PencilInfoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                            .addComponent(CorrelatorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
-                        .addGap(18, 18, 18)
-                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
-                            .addComponent(IncoherentStokesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                            .addComponent(CoherentStokesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
-                .addContainerGap(25, Short.MAX_VALUE))
+                            .addComponent(CorrelatorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                            .addComponent(PencilInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
+                    .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+                .addContainerGap(42, Short.MAX_VALUE))
         );
         jPanel4Layout.setVerticalGroup(
             jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(jPanel4Layout.createSequentialGroup()
                 .addContainerGap()
                 .addComponent(jPanel8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
-                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                 .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                     .addGroup(jPanel4Layout.createSequentialGroup()
-                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
-                        .addComponent(CoherentStokesPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
-                    .addComponent(CorrelatorPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                    .addComponent(jPanel7, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
-                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
-                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
-                    .addComponent(IncoherentStokesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                    .addComponent(PencilInfoPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
-                    .addComponent(jPanel11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
-                .addGap(24, 24, 24))
+                        .addGap(10, 10, 10)
+                        .addComponent(CorrelatorPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
+                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
+                        .addComponent(PencilInfoPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
+                    .addGroup(jPanel4Layout.createSequentialGroup()
+                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
+                        .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
+                .addGap(39, 39, 39))
         );
 
         javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
@@ -1603,13 +1515,13 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
             .addGroup(jPanel2Layout.createSequentialGroup()
                 .addContainerGap()
                 .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
-                .addContainerGap(4293, Short.MAX_VALUE))
+                .addContainerGap(4261, Short.MAX_VALUE))
         );
         jPanel2Layout.setVerticalGroup(
             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
             .addGroup(jPanel2Layout.createSequentialGroup()
-                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
-                .addContainerGap(2564, Short.MAX_VALUE))
+                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 540, javax.swing.GroupLayout.PREFERRED_SIZE)
+                .addContainerGap(2455, Short.MAX_VALUE))
         );
 
         jScrollPane1.setViewportView(jPanel2);
@@ -1625,10 +1537,13 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
     }// </editor-fold>//GEN-END:initComponents
 
     private void buttonPanel1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonPanel1ActionPerformed
-        if(evt.getActionCommand().equals("Apply")) {
-            saveInput();
-        } else if(evt.getActionCommand().equals("Restore")) {
-            restore();
+        switch (evt.getActionCommand()) {
+            case "Apply":
+                saveInput();
+                break;
+            case "Restore":
+                restore();
+                break;
         }
     }//GEN-LAST:event_buttonPanel1ActionPerformed
 
@@ -1653,72 +1568,51 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
     }//GEN-LAST:event_inputOutputIncoherentStokesActionPerformed
 
     private void inputFlysEyeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_inputFlysEyeActionPerformed
-       checkSettings();
+        checkSettings();
     }//GEN-LAST:event_inputFlysEyeActionPerformed
-    
-    private jOTDBnode    itsNode      = null;
-    private MainFrame    itsMainFrame = null;
-    private String       itsTreeType  = "";
-    private JFileChooser fc           = null;  
-    private boolean  editting = false;
-    private int      itsSelectedRow = -1;
-
+    private jOTDBnode itsNode = null;
+    private MainFrame itsMainFrame = null;
+    private String itsTreeType = "";
+    private JFileChooser fc = null;
     //Olap specific parameters
-    private jOTDBnode itsDelayCompensation=null;
-    private jOTDBnode itsNrTimesInFrame=null;
-    private jOTDBnode itsNrSubbandsPerFrame=null;
-    private jOTDBnode itsNrBitsPerSample=null;
-    private jOTDBnode itsNrSecondsOfBuffer=null;
-    private jOTDBnode itsMaxNetworkDelay=null;
-    private jOTDBnode itsCorrectBandPass=null;
-    
-    
+    private jOTDBnode itsDelayCompensation = null;
+    private jOTDBnode itsNrTimesInFrame = null;
+    private jOTDBnode itsNrSubbandsPerFrame = null;
+    private jOTDBnode itsNrBitsPerSample = null;
+    private jOTDBnode itsNrSecondsOfBuffer = null;
+    private jOTDBnode itsMaxNetworkDelay = null;
+    private jOTDBnode itsCorrectBandPass = null;
     // _Output params
-    private jOTDBnode itsOutputCorrelatedData=null;
-    private jOTDBnode itsOutputFilteredData=null;
-    private jOTDBnode itsOutputBeamFormedData=null;
-    private jOTDBnode itsOutputCoherentStokes=null;
-    private jOTDBnode itsOutputIncoherentStokes=null;
-
-    
+    private jOTDBnode itsOutputCorrelatedData = null;
+    private jOTDBnode itsOutputFilteredData = null;
+    private jOTDBnode itsOutputBeamFormedData = null;
+    private jOTDBnode itsOutputCoherentStokes = null;
+    private jOTDBnode itsOutputIncoherentStokes = null;
     // OLAP-CNProc parameters
-    private jOTDBnode itsNrPPFTaps=null;
- 
-    // OLAP-StorageProc parameters
-    private jOTDBnode itsSubbandsPerMS=null;
-
+    private jOTDBnode itsNrPPFTaps = null;
     //Correlator
-    private jOTDBnode itsIntegrationTime=null;
-
+    private jOTDBnode itsIntegrationTime = null;
     // CNProc_Coherentstokes
-    private jOTDBnode itsWhichCoherent=null;
-    private jOTDBnode itsIntegrateChannelsCoherent=null;
-    private jOTDBnode itsIntegrationStepsCoherent=null;
-
+    private jOTDBnode itsWhichCoherent = null;
+    private jOTDBnode itsChannelsPerSubbandCoherent = null;
+    private jOTDBnode itsTimeIntegrationFactorCoherent = null;
     // CNProc_Incoherentstokes
-    private jOTDBnode itsWhichIncoherent=null;
-    private jOTDBnode itsIntegrateChannelsIncoherent=null;
-    private jOTDBnode itsIntegrationStepsIncoherent=null;
-
+    private jOTDBnode itsWhichIncoherent = null;
+    private jOTDBnode itsChannelsPerSubbandIncoherent = null;
+    private jOTDBnode itsTimeIntegrationFactorIncoherent = null;
     // PencilInfo
-    private jOTDBnode itsFlysEye=null;
-
-
-    
-    
+    private jOTDBnode itsFlysEye = null;
     // Variables declaration - do not modify//GEN-BEGIN:variables
     private javax.swing.JPanel CoherentStokesPanel;
     private javax.swing.JPanel CorrelatorPanel;
     private javax.swing.JPanel IncoherentStokesPanel;
     private javax.swing.JPanel PencilInfoPanel;
     private nl.astron.lofar.sas.otbcomponents.ButtonPanel buttonPanel1;
+    private javax.swing.JTextField inputChannelsPerSubbandCoherent;
+    private javax.swing.JTextField inputChannelsPerSubbandIncoherent;
     private javax.swing.JCheckBox inputCorrectBandPass;
     private javax.swing.JCheckBox inputDelayCompensation;
     private javax.swing.JCheckBox inputFlysEye;
-    private javax.swing.JCheckBox inputIntegrateChannelsCoherent;
-    private javax.swing.JCheckBox inputIntegrateChannelsIncoherent;
-    private javax.swing.JTextField inputIntegrationStepsCoherent;
-    private javax.swing.JTextField inputIntegrationStepsIncoherent;
     private javax.swing.JTextField inputIntegrationTime;
     private javax.swing.JTextField inputMaxNetworkDelay;
     private javax.swing.JTextField inputNrBitsPerSample;
@@ -1731,39 +1625,39 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
     private javax.swing.JCheckBox inputOutputCorrelatedData;
     private javax.swing.JCheckBox inputOutputFilteredData;
     private javax.swing.JCheckBox inputOutputIncoherentStokes;
-    private javax.swing.JTextField inputSubbandsPerMS;
+    private javax.swing.JTextField inputTimeIntegrationFactorCoherent;
+    private javax.swing.JTextField inputTimeIntegrationFactorIncoherent;
     private javax.swing.JComboBox inputWhichCoherent;
     private javax.swing.JComboBox inputWhichIncoherent;
     private javax.swing.JLabel jLabel1;
     private javax.swing.JLabel jLabel2;
     private javax.swing.JLabel jLabel3;
     private javax.swing.JPanel jPanel1;
-    private javax.swing.JPanel jPanel11;
     private javax.swing.JPanel jPanel2;
     private javax.swing.JPanel jPanel4;
     private javax.swing.JPanel jPanel7;
     private javax.swing.JPanel jPanel8;
     private javax.swing.JPanel jPanel9;
     private javax.swing.JScrollPane jScrollPane1;
+    private javax.swing.JLabel labelChannelsPerSubbandCoherent;
+    private javax.swing.JLabel labelChannelsPerSubbandIncoherent;
     private javax.swing.JLabel labelDelayCompensation;
     private javax.swing.JLabel labelIntegrationTime;
-    private javax.swing.JLabel labelIntegrationstepsCoherent;
-    private javax.swing.JLabel labelIntegrationstepsIncoherent;
     private javax.swing.JLabel labelMaxNetworkDelay;
     private javax.swing.JLabel labelNrBitsPerSample;
     private javax.swing.JLabel labelNrPPFTaps;
     private javax.swing.JLabel labelNrSubbandsPerFrame;
     private javax.swing.JLabel labelNrTimesInFrame;
-    private javax.swing.JLabel labelSubbandsPerMs;
+    private javax.swing.JLabel labelTimeIntegrationfactorCoherent;
     private javax.swing.JLabel labelWhichCoherent;
     private javax.swing.JLabel labelWhichIncoherent;
     private javax.swing.JTextField subbandsPerFrameDerefText;
+    private javax.swing.JLabel timeIntegrationFactorCoherent;
     // End of variables declaration//GEN-END:variables
- 
     /**
      * Utility field used by event firing mechanism.
      */
-    private javax.swing.event.EventListenerList myListenerList =  null;
+    private javax.swing.event.EventListenerList myListenerList = null;
 
     /**
      * Registers ActionListener to receive events.
@@ -1772,10 +1666,10 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
     @Override
     public synchronized void addActionListener(java.awt.event.ActionListener listener) {
 
-        if (myListenerList == null ) {
+        if (myListenerList == null) {
             myListenerList = new javax.swing.event.EventListenerList();
         }
-        myListenerList.add (java.awt.event.ActionListener.class, listener);
+        myListenerList.add(java.awt.event.ActionListener.class, listener);
     }
 
     /**
@@ -1785,7 +1679,7 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
     @Override
     public synchronized void removeActionListener(java.awt.event.ActionListener listener) {
 
-        myListenerList.remove (java.awt.event.ActionListener.class, listener);
+        myListenerList.remove(java.awt.event.ActionListener.class, listener);
     }
 
     /**
@@ -1795,12 +1689,14 @@ public class OlapPanel extends javax.swing.JPanel implements IViewPanel{
      */
     private void fireActionListenerActionPerformed(java.awt.event.ActionEvent event) {
 
-        if (myListenerList == null) return;
-        Object[] listeners = myListenerList.getListenerList ();
+        if (myListenerList == null) {
+            return;
+        }
+        Object[] listeners = myListenerList.getListenerList();
         for (int i = listeners.length - 2; i >= 0; i -= 2) {
-            if (listeners[i]==java.awt.event.ActionListener.class) {
-                ((java.awt.event.ActionListener)listeners[i+1]).actionPerformed (event);
+            if (listeners[i] == java.awt.event.ActionListener.class) {
+                ((java.awt.event.ActionListener) listeners[i + 1]).actionPerformed(event);
             }
         }
-    } 
+    }
 }
diff --git a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jCampaign.cc b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jCampaign.cc
index bf1c83daf324daa8ce4c615912b16428475bfbcf..297a072cf532c0bb65835a227ac81f0e73cc9cac 100644
--- a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jCampaign.cc
+++ b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jCampaign.cc
@@ -52,6 +52,7 @@ JNIEXPORT void JNICALL Java_nl_astron_lofar_sas_otb_jotdb3_jCampaign_initCampaig
   try {
     OTDBconnection* aConn=getConnection(name);
     Campaign* aCampaign = new Campaign(aConn);
+    theirC_ObjectMap.erase(name+"_Campaign");
     theirC_ObjectMap[name+"_Campaign"]=(void*)aCampaign;
 
   } catch (exception &ex) {
diff --git a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jClassifConv.cc b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jClassifConv.cc
index 568b3e422bda5cc72e2c739e48ee199634a12a2d..1719f48aef6c8459b701b6f12e09faac3209ba9a 100644
--- a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jClassifConv.cc
+++ b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jClassifConv.cc
@@ -47,6 +47,7 @@ JNIEXPORT void JNICALL Java_nl_astron_lofar_sas_otb_jotdb3_jClassifConv_initClas
   try {
     OTDBconnection* aConn=getConnection(name);
     ClassifConv* aCConv = new ClassifConv(aConn);
+    theirC_ObjectMap.erase(name+"_ClassifConv");
     theirC_ObjectMap[name+"_ClassifConv"]=(void*)aCConv;
   } catch (exception &ex) {
     cout << "Exception during new ClassifConv " << ex.what() << endl;
diff --git a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jOTDBconnection.cc b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jOTDBconnection.cc
index 22cb15c7760b86fc511b2039c05e7993349bd20e..701c89299de23f4caf1cade3e6d057c23d477c91 100644
--- a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jOTDBconnection.cc
+++ b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jOTDBconnection.cc
@@ -75,6 +75,7 @@ JNIEXPORT void JNICALL Java_nl_astron_lofar_sas_otb_jotdb3_jOTDBconnection_initO
 
     std::map<std::string,void *>::iterator iter;    
 
+    theirC_ObjectMap.erase(name+"_OTDBconnection");
     theirC_ObjectMap[name+"_OTDBconnection"]=(void *)aPtr;
 
     LOG_DEBUG("after connect: theirC_ObjectMap contains: ");
@@ -88,7 +89,6 @@ JNIEXPORT void JNICALL Java_nl_astron_lofar_sas_otb_jotdb3_jOTDBconnection_initO
     env->ReleaseStringUTFChars(passwd, pass);
     env->ReleaseStringUTFChars(database, db);
     env->ReleaseStringUTFChars(hostname, hn);
-    env->ReleaseStringUTFChars(str, n);
   } catch (exception &ex) {
     cout << "Exception during new OTDBconnection(" << u << "," << p << "," << d << "," << h << ") : "<< ex.what() << endl;
     
diff --git a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jParamTypeConv.cc b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jParamTypeConv.cc
index e6ce74b7d176fde4439a8384d5b1efc80f7b1bb5..8237202e9a24bdd874117aae10748849fe0847c8 100644
--- a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jParamTypeConv.cc
+++ b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jParamTypeConv.cc
@@ -47,6 +47,7 @@ JNIEXPORT void JNICALL Java_nl_astron_lofar_sas_otb_jotdb3_jParamTypeConv_initPa
   try {
     OTDBconnection* aConn=getConnection(name);
     ParamTypeConv* aPTConv = new ParamTypeConv(aConn);
+    theirC_ObjectMap.erase(name+"_ParamTypeConv");
     theirC_ObjectMap[name+"_ParamTypeConv"]=(void*)aPTConv;
   } catch (exception &ex) {
     cout << "Exception during new ParamTypeConv) : "<< ex.what() << endl;
diff --git a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeMaintenance.cc b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeMaintenance.cc
index 7ac617a48c73f658b8aee62bbc8fff559fd9da8b..7670ba3f0b709b2cff882a3f716e99af87673fd7 100644
--- a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeMaintenance.cc
+++ b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeMaintenance.cc
@@ -50,6 +50,7 @@ JNIEXPORT void JNICALL Java_nl_astron_lofar_sas_otb_jotdb3_jTreeMaintenance_init
   try {
     OTDBconnection* aConn=getConnection(name);
     TreeMaintenance* aTM = new TreeMaintenance(aConn);
+    theirC_ObjectMap.erase(name+"_TreeMaintenance");
     theirC_ObjectMap[name+"_TreeMaintenance"]=(void*)aTM;
 
   } catch (exception &ex) {
diff --git a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeStateConv.cc b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeStateConv.cc
index d59687b00cfd78b1a1d58506779d184e9d47441b..ef56f298c357328c95da38e956a1017276042a91 100644
--- a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeStateConv.cc
+++ b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeStateConv.cc
@@ -47,6 +47,7 @@ JNIEXPORT void JNICALL Java_nl_astron_lofar_sas_otb_jotdb3_jTreeStateConv_initTr
   try {
     OTDBconnection* aConn=getConnection(name);
     TreeStateConv* aTSConv = new TreeStateConv(aConn);
+    theirC_ObjectMap.erase(name+"_TreeStateConv");
     theirC_ObjectMap[name+"_TreeStateConv"]=(void*)aTSConv;
   } catch (exception &ex) {
     cout << "Exception during new TreeStateConv "<< ex.what() << endl; 
diff --git a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeTypeConv.cc b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeTypeConv.cc
index aa29f6aa33e93f393a4bb8c015a85247af4cbf50..51f15372f394a11c48d8d0c92ed895ac0ff4e8ab 100644
--- a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeTypeConv.cc
+++ b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeTypeConv.cc
@@ -48,6 +48,7 @@ JNIEXPORT void JNICALL Java_nl_astron_lofar_sas_otb_jotdb3_jTreeTypeConv_initTre
     OTDBconnection* aConn=getConnection(name);
     TreeTypeConv* aTTConv = new TreeTypeConv(aConn);
 
+    theirC_ObjectMap.erase(name+"_TreeTypeConv");
     theirC_ObjectMap[name+"_TreeTypeConv"]=(void*)aTTConv;
   } catch (exception &ex) {
     cout << "Exception during new TreeTypeConv::top" << ex.what() << endl; 
diff --git a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeValue.cc b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeValue.cc
index 0bf5491acef4ec2aafdda0b2a1fa1fb86c1235e8..d98152703f97449a19d290e992d74160a8ad4b54 100644
--- a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeValue.cc
+++ b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jTreeValue.cc
@@ -512,10 +512,12 @@ void  setTreeValConnection(JNIEnv *env, jobject jTreeValue) {
       if (treeVal->treeID() != aTreeID) {
 	delete treeVal;
 	treeVal = new TreeValue(aConn,aTreeID);
+        theirC_ObjectMap.erase(name+"_TreeValue");
 	theirC_ObjectMap[name+"_TreeValue"]=(void*)treeVal;
       }
     } else {
       treeVal = new TreeValue(aConn,aTreeID);
+      theirC_ObjectMap.erase(name+"_TreeValue");
       theirC_ObjectMap[name+"_TreeValue"]=(void*)treeVal;
     }
     
diff --git a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jUnitConv.cc b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jUnitConv.cc
index f5b07df2ef522933b7efa024401a9a3eb45e291a..af17d764f96cbee3685499be4453a7df2a03bf50 100644
--- a/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jUnitConv.cc
+++ b/SAS/OTB/jOTDB3/src/nl_astron_lofar_sas_otb_jotdb3_jUnitConv.cc
@@ -47,6 +47,7 @@ JNIEXPORT void JNICALL Java_nl_astron_lofar_sas_otb_jotdb3_jUnitConv_initUnitCon
   try {
     OTDBconnection* aConn=getConnection(name);
     UnitConv* aUConv = new UnitConv(aConn);
+    theirC_ObjectMap.erase(name+"_UnitConv");
     theirC_ObjectMap[name+"_UnitConv"]=(void*)aUConv;
   } catch (exception &ex) {
     cout << "Exception during new UnitConv" << ex.what() << endl;