From b59e784608bcf7ba22aec819dccd1801d5d6567d Mon Sep 17 00:00:00 2001
From: Ger van Diepen <diepen@astron.nl>
Date: Fri, 8 Feb 2013 10:20:04 +0000
Subject: [PATCH] Task #4136 Add choice to form summed autocorr from autocorr
 or crosscorr.

---
 CEP/DP3/DPPP/include/DPPP/StationAdder.h |  7 ++--
 CEP/DP3/DPPP/src/StationAdder.cc         | 10 +++---
 CEP/DP3/DPPP/test/tStationAdder.cc       | 44 ++++++++++++++++++------
 3 files changed, 42 insertions(+), 19 deletions(-)

diff --git a/CEP/DP3/DPPP/include/DPPP/StationAdder.h b/CEP/DP3/DPPP/include/DPPP/StationAdder.h
index 3cd95859eae..69c47ab4eab 100644
--- a/CEP/DP3/DPPP/include/DPPP/StationAdder.h
+++ b/CEP/DP3/DPPP/include/DPPP/StationAdder.h
@@ -1,4 +1,4 @@
-//# StationAdder.h: DPPP step class to add station to a superstation
+//# StationAdder.h: DPPP step class to add stations as a superstation
 //# Copyright (C) 2012
 //# ASTRON (Netherlands Institute for Radio Astronomy)
 //# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
@@ -25,7 +25,7 @@
 #define DPPP_STATIONADDER_H
 
 // @file
-// @brief DPPP step class to average in time and/or freq
+// @brief DPPP step class to add stations as a superstation
 
 #include <DPPP/DPInput.h>
 #include <DPPP/DPBuffer.h>
@@ -109,7 +109,8 @@ namespace LOFAR {
       vector<casa::Vector<int> > itsParts;  // the stations in each superstation
       vector<vector<int> > itsBufRows; // old baseline rows in each new baseline
       uint            itsMinNPoint  ;  // flag data if too few unflagged data
-      bool            itsMakeAutoCorr; // also form new autocorrelations?
+      bool            itsMakeAutoCorr; // also form new auto-correlations?
+      bool            itsSumAutoCorr;  // sum auto- or cross-correlations?
       bool            itsUseWeight;    // false = use weight 1 per station
       UVWCalculator   itsUVWCalc;
       NSTimer         itsTimer;
diff --git a/CEP/DP3/DPPP/src/StationAdder.cc b/CEP/DP3/DPPP/src/StationAdder.cc
index 1f6768bd5ac..f4269c5279d 100644
--- a/CEP/DP3/DPPP/src/StationAdder.cc
+++ b/CEP/DP3/DPPP/src/StationAdder.cc
@@ -1,4 +1,4 @@
-//# StationAdder.cc: DPPP step class to add station to a superstation
+//# StationAdder.cc: DPPP step class to add stations as a superstation
 //# Copyright (C) 2012
 //# ASTRON (Netherlands Institute for Radio Astronomy)
 //# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
@@ -55,6 +55,7 @@ namespace LOFAR {
         itsStatRec      (parset.getRecord(prefix+"stations")),
         itsMinNPoint    (parset.getUint  (prefix+"minpoints", 1)),
         itsMakeAutoCorr (parset.getBool  (prefix+"autocorr", false)),
+        itsSumAutoCorr  (parset.getBool  (prefix+"sumauto", true)),
         itsUseWeight    (parset.getBool  (prefix+"useweights", true))
     {
     }
@@ -204,11 +205,10 @@ namespace LOFAR {
           int  take   = 0;
           if (havea1) {
             // If both stations are in same superstation, only use them
-            // if it is an autocorrelation.
+            // if autocorrelations have to be made.
+            // Taking auto or cross depends on summing mode.
             if (havea2) {
-              if (itsMakeAutoCorr  &&  ant1[i] == ant2[i]) {
-                take = 1;
-              }
+              take = itsMakeAutoCorr  &&  itsSumAutoCorr == (ant1[i]==ant2[i]);
             } else {
               ant  = ant2[i];
               take = -1;            // conjugate has to be added
diff --git a/CEP/DP3/DPPP/test/tStationAdder.cc b/CEP/DP3/DPPP/test/tStationAdder.cc
index 5b21004c854..cd601390670 100644
--- a/CEP/DP3/DPPP/test/tStationAdder.cc
+++ b/CEP/DP3/DPPP/test/tStationAdder.cc
@@ -41,7 +41,6 @@ using namespace std;
 
 // Simple class to generate input arrays.
 // It can only set all flags to true or all to false.
-// Weights are always 1.
 // It can be used with different nr of times, channels, etc.
 class TestInput: public DPInput
 {
@@ -141,9 +140,9 @@ private:
 class TestOutput: public DPStep
 {
 public:
-  TestOutput(int ntime, int nbl, int nchan, int ncorr)
+  TestOutput(int ntime, int nbl, int nchan, int ncorr, bool sumauto)
     : itsCount(0), itsNTime(ntime), itsNBl(nbl), itsNChan(nchan),
-      itsNCorr(ncorr)
+      itsNCorr(ncorr), itsSumAuto(sumauto)
   {}
 private:
   void addData (Cube<Complex>& to, const Cube<Complex>& from, int bl)
@@ -164,9 +163,26 @@ private:
     indgen (weights, 0.5f, 0.01f);
     Cube<Complex> databl0 (itsNCorr, itsNChan, 1);
     Cube<Complex> databl1 (itsNCorr, itsNChan, 1);
-    addData (databl0, data, 0);
-    addData (databl0, data, 5);
-    addData (databl0, data, 15);
+    // "{ns:[rs01.s01, rs02.s01, cs01.s02]}" was given resulting in 2 new
+    // baselines (ns-ns and cs01.s01-ns). 
+    // Thus adding the baselines below.
+    float weight=0;
+    if (itsSumAuto) {
+      // add autocorr to form new autocorr
+      addData (databl0, data, 0);
+      addData (databl0, data, 5);
+      addData (databl0, data, 15);
+      weight = 3;
+    } else {
+      // add crosscorr to form new autocorr
+      addData (databl0, data, 1);
+      addData (databl0, data, 3);
+      addData (databl0, data, 4);
+      addData (databl0, data, 7);
+      addData (databl0, data, 12);
+      addData (databl0, data, 13);
+      weight = 6;
+    }
     addData (databl1, data, 8);
     addData (databl1, data, 9);
     addData (databl1, data, 11);
@@ -191,7 +207,7 @@ private:
     // Now check data of new baselines.
     end[2] = itsNBl;
     ASSERT (allNear (buf.getData()(IPosition(3,0,0,itsNBl), end), databl0, 1e-5));
-    ASSERT (allNear (buf.getWeights()(IPosition(3,0,0,itsNBl), end), 3.f, 1e-5));
+    ASSERT (allNear (buf.getWeights()(IPosition(3,0,0,itsNBl), end), weight, 1e-5));
     end[2] = itsNBl+1;
     ASSERT (allNear (buf.getData()(IPosition(3,0,0,itsNBl+1), end), databl1, 1e-5));
     ASSERT (allNear (buf.getWeights()(IPosition(3,0,0,itsNBl+1), end), 6.f, 1e-5));
@@ -234,6 +250,7 @@ private:
 
   int itsCount;
   int itsNTime, itsNBl, itsNChan, itsNCorr, itsNAvgTime, itsNAvgChan;
+  bool itsSumAuto;
 };
 
 // Class to check result of flagged, unaveraged TestInput run by test2.
@@ -275,6 +292,7 @@ private:
     Cube<Float> weightbl2 (itsNCorr, itsNChan, 1, 0.);
     Cube<Float> weightbl3 (itsNCorr, itsNChan, 1, 0.);
     Cube<Float> weightbl4 (itsNCorr, itsNChan, 1, 0.);
+    // "{ns1:[rs01.s01, rs02.s01], ns2:[cs01.s02, cs01.s01]}" was given.
     addData (databl0, data, weightbl0, weights, 8);
     addData (databl0, data, weightbl0, weights, 9);
     addData (databl1, data, weightbl1, weights, 12);
@@ -376,10 +394,10 @@ void execute (const DPStep::ShPtr& step1)
 }
 
 // Test adding 3 stations.
-void test1(int ntime, int nbl, int nchan, int ncorr)
+void test1(int ntime, int nbl, int nchan, int ncorr, bool sumauto)
 {
   cout << "test1: ntime=" << ntime << " nrbl=" << nbl << " nchan=" << nchan
-       << " ncorr=" << ncorr << endl;
+       << " ncorr=" << ncorr << " sumauto=" << sumauto << endl;
   // Create the steps.
   TestInput* in = new TestInput(ntime, nbl, nchan, ncorr);
   DPStep::ShPtr step1(in);
@@ -387,9 +405,12 @@ void test1(int ntime, int nbl, int nchan, int ncorr)
   parset.add ("stations",
               "{ns:[rs01.s01, rs02.s01, cs01.s02]}");
   parset.add ("autocorr", "true");
+  if (!sumauto) {
+    parset.add ("sumauto", "false");
+  }
   parset.add ("useweights", "false");
   DPStep::ShPtr step2(new StationAdder(in, parset, ""));
-  DPStep::ShPtr step3(new TestOutput(ntime, nbl, nchan, ncorr));
+  DPStep::ShPtr step3(new TestOutput(ntime, nbl, nchan, ncorr, sumauto));
   step1->setNextStep (step2);
   step2->setNextStep (step3);
   execute (step1);
@@ -462,7 +483,8 @@ int main()
     // Test the station selection patterns.
     testPatterns();
     // Test must be done with with 16 baselines.
-    test1( 10,  16, 32, 4);
+    test1( 10,  16, 32, 4, true);
+    test1( 10,  16, 32, 4, false);
     test2( 10,  16, 32, 4);
     // Unknown station.
     test3("{ns1:rs01.s1, ns2:[cs01.s02, cs01.s01]}");
-- 
GitLab