From 770dce2ad41ba1ef2d5e4637b1a70a973914cbba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A9=20Offringa?= <offringa@astron.nl>
Date: Mon, 31 Jan 2011 14:18:18 +0000
Subject: [PATCH] Bug 1491: adding some script creaters

---
 .gitattributes                                |  5 +-
 .../include/AOFlagger/ref/concatenatescript.h | 61 +++++++++++++++++++
 .../include/AOFlagger/ref/copyallscript.h     | 12 +++-
 .../AOFlagger/include/AOFlagger/ref/reffile.h | 14 ++++-
 .../include/AOFlagger/ref/reffileentry.h      |  6 ++
 .../include/AOFlagger/ref/refmovescript.h     | 56 +++++++++++++++++
 CEP/DP3/AOFlagger/package.dox                 | 12 ++++
 CEP/DP3/AOFlagger/src/CMakeLists.txt          |  2 +-
 .../{aocopyallscript.cpp => aorefscript.cpp}  | 17 +++++-
 9 files changed, 176 insertions(+), 9 deletions(-)
 create mode 100644 CEP/DP3/AOFlagger/include/AOFlagger/ref/concatenatescript.h
 create mode 100644 CEP/DP3/AOFlagger/include/AOFlagger/ref/refmovescript.h
 create mode 100644 CEP/DP3/AOFlagger/package.dox
 rename CEP/DP3/AOFlagger/src/{aocopyallscript.cpp => aorefscript.cpp} (73%)

diff --git a/.gitattributes b/.gitattributes
index 2cbdedc39fd..415afbbf2e0 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -227,10 +227,12 @@ CEP/DP3/AOFlagger/include/AOFlagger/msio/timefrequencydata.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/msio/timefrequencyimager.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/msio/timefrequencymetadata.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/msio/types.h -text
+CEP/DP3/AOFlagger/include/AOFlagger/ref/concatenatescript.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/ref/copyallscript.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/ref/reffile.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/ref/reffileentry.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/ref/reffileexception.h -text
+CEP/DP3/AOFlagger/include/AOFlagger/ref/refmovescript.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/rfi/antennaflagcountplot.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/rfi/eigenvalue.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/rfi/frequencyflagcountplot.h -text
@@ -323,6 +325,7 @@ CEP/DP3/AOFlagger/include/AOFlagger/util/stopwatch.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/util/types.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/util/xmlwriter.h -text
 CEP/DP3/AOFlagger/include/AOFlagger/vectortypes.h -text
+CEP/DP3/AOFlagger/package.dox -text
 CEP/DP3/AOFlagger/scripts/CMakeLists-AOFlagger-without-lofarstman.txt -text
 CEP/DP3/AOFlagger/scripts/CMakeLists-CEP.txt -text
 CEP/DP3/AOFlagger/scripts/CMakeLists-Common-src-without-lofarstman.txt -text
@@ -351,8 +354,8 @@ CEP/DP3/AOFlagger/scripts/distributed/statistics.rfis -text
 CEP/DP3/AOFlagger/scripts/package-without-lofarstman.sh -text
 CEP/DP3/AOFlagger/scripts/package.sh -text
 CEP/DP3/AOFlagger/src/CMakeLists.txt -text
-CEP/DP3/AOFlagger/src/aocopyallscript.cpp -text
 CEP/DP3/AOFlagger/src/aofrequencyfilter.cpp -text
+CEP/DP3/AOFlagger/src/aorefscript.cpp -text
 CEP/DP3/AOFlagger/src/cleaner.cpp -text
 CEP/DP3/AOFlagger/src/colormapper.cpp -text
 CEP/DP3/AOFlagger/src/createuv.cpp -text
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/ref/concatenatescript.h b/CEP/DP3/AOFlagger/include/AOFlagger/ref/concatenatescript.h
new file mode 100644
index 00000000000..441ad196a28
--- /dev/null
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/ref/concatenatescript.h
@@ -0,0 +1,61 @@
+/***************************************************************************
+ *   Copyright (C) 2011 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 AO_CONCATENATESCRIPT_H
+#define AO_CONCATENATESCRIPT_H
+
+#include <iostream>
+#include <string>
+
+#include <AOFlagger/ref/reffile.h>
+
+namespace AOTools
+{
+	class ConcatenateScript
+	{
+		public:
+		static void Make(std::ostream &stream, const std::string &refFilePath, const std::string &destination)
+		{
+			RefFile file(refFilePath);
+
+			stream <<
+				"#!/usr/bin/env python\n\n"
+				"import pyrap.tables as pt\n\n"
+				"pt.concatms([";
+
+			RefFile::const_iterator i=file.begin();
+			if(i!=file.end())
+			{
+				stream << '\"' << i->Path() << '\"';
+				++i;
+			}
+
+			while(i!=file.end())
+			{
+				stream << ", \"/net/" << i->Node() << i->Path() << '\"';
+				++i;
+			}
+
+			stream << "], \"" << destination << "\")\n"; 
+		}
+	};
+}
+
+#endif // AO_CONCATENATESCRIPT_H
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/ref/copyallscript.h b/CEP/DP3/AOFlagger/include/AOFlagger/ref/copyallscript.h
index 64361a1efb4..5df56def3bb 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/ref/copyallscript.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/ref/copyallscript.h
@@ -39,6 +39,12 @@ namespace AOTools
 			typedef std::map<std::string, PathList> PathsPerNodeType;
 			typedef std::pair<std::string, PathList> NodeAndPaths;
 
+			std::string correctedDest;
+			if(*destination.rbegin() == '/')
+				correctedDest = destination;
+			else
+				correctedDest = destination + "/";
+
 			RefFile file(refFilePath);
 			
 			PathsPerNodeType pathsPerNode;
@@ -58,7 +64,7 @@ namespace AOTools
 			stream <<
 				"#! /bin/bash\n"
 				"# Created by aocopyallscript to move sets in \n# " << refFilePath << "\n"
-				"# to local path\n# " << destination << "\n"
+				"# to local path\n# " << correctedDest << "\n"
 				"# Set contains " << file.Count() << " MS directories\n"
 				"# Number of nodes: " << pathsPerNode.size() << "\n";
 			for(PathsPerNodeType::const_iterator i=pathsPerNode.begin(); i!=pathsPerNode.end(); ++i)
@@ -68,13 +74,13 @@ namespace AOTools
 
 				stream
 					<< "function copy_" << node << " {\n"
-					<< "  mkdir -p " << destination << "\n";
+					<< "  ssh " << node << " -C \"mkdir -p " << correctedDest << "\"\n";
 				for(PathList::const_iterator p=paths.begin();p!=paths.end();++p)
 				{
 					const std::string &path = *p;
 					stream
 						<< "  echo " << path << " \\(" << node << "\\)\n"
-						<< "  ssh " << node << " -C \"cp -r " << path << " " << destination << "\"\n";
+						<< "  ssh " << node << " -C \"cp -r " << path << " " << correctedDest << "\"\n";
 				}
 				stream
 					<< "}\n\n";
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/ref/reffile.h b/CEP/DP3/AOFlagger/include/AOFlagger/ref/reffile.h
index 075678d6942..f2aa1f45efc 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/ref/reffile.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/ref/reffile.h
@@ -23,6 +23,7 @@
 
 #include <fstream>
 #include <string>
+#include <vector>
 
 #include <AOFlagger/ref/reffileentry.h>
 #include <AOFlagger/ref/reffileexception.h>
@@ -55,6 +56,12 @@ namespace AOTools
 				}
 			}
 
+			void Write(std::ostream &destination) const
+			{
+				for(const_iterator i=begin();i!=end();++i)
+					i->write(destination);
+			}
+
 			size_t Count() const
 			{
 				return _entries.size();
@@ -65,6 +72,11 @@ namespace AOTools
 				return _entries[index];
 			}
 
+			void Add(const RefFileEntry &entry)
+			{
+				_entries.push_back(entry);
+			}
+	
 			const_iterator begin() const
 			{
 				return _entries.begin();
@@ -74,7 +86,7 @@ namespace AOTools
 			{
 				return _entries.end();
 			}
-	
+
 		private:
 			RefFile(const RefFile &) // don't allow copy
 			{
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/ref/reffileentry.h b/CEP/DP3/AOFlagger/include/AOFlagger/ref/reffileentry.h
index 082b42b3497..4525d9f658c 100644
--- a/CEP/DP3/AOFlagger/include/AOFlagger/ref/reffileentry.h
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/ref/reffileentry.h
@@ -58,6 +58,7 @@ namespace AOTools
 			unsigned Size() const { return _size; }
 			const std::string &Node() const { return _node; }
 
+			void SetPath(const std::string &path) { _path = path; }
 		private:
 			std::string _path;
 			std::string _frequency;
@@ -78,6 +79,11 @@ namespace AOTools
 				return true;
 			}
 
+			void write(std::ostream &stream) const
+			{
+				stream << _path << ' ' << _frequency << ' ' << _size << ' ' << _node << "\n";
+			}
+
 			void assignFromString(const std::string &line)
 			{
 				std::string::const_iterator i=line.begin();
diff --git a/CEP/DP3/AOFlagger/include/AOFlagger/ref/refmovescript.h b/CEP/DP3/AOFlagger/include/AOFlagger/ref/refmovescript.h
new file mode 100644
index 00000000000..4ed38ea510a
--- /dev/null
+++ b/CEP/DP3/AOFlagger/include/AOFlagger/ref/refmovescript.h
@@ -0,0 +1,56 @@
+/***************************************************************************
+ *   Copyright (C) 2011 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 AO_REFMOVESCRIPT_H
+#define AO_REFMOVESCRIPT_H
+
+#include <iostream>
+#include <string>
+
+#include <boost/filesystem.hpp>
+
+#include <AOFlagger/ref/reffile.h>
+
+namespace AOTools
+{
+	class RefMoveScript
+	{
+		public:
+		static void Make(std::ostream &stream, const std::string &refFilePath, const std::string &destination)
+		{
+			RefFile file(refFilePath);
+			RefFile destFile;
+
+			const boost::filesystem::path destPath(destination);
+
+			for(RefFile::const_iterator i=file.begin();i!=file.end();++i)
+			{
+				RefFileEntry entry = *i;
+				boost::filesystem::path entryPath(entry.Path());
+				entry.SetPath((destPath / entryPath.filename()).directory_string());
+				destFile.Add(entry);
+			}
+
+			destFile.Write(stream);
+		}
+	};
+}
+
+#endif // AO_REFMOVESCRIPT_H
diff --git a/CEP/DP3/AOFlagger/package.dox b/CEP/DP3/AOFlagger/package.dox
new file mode 100644
index 00000000000..609d8c743cf
--- /dev/null
+++ b/CEP/DP3/AOFlagger/package.dox
@@ -0,0 +1,12 @@
+// \ingroup DP3
+//
+// \defgroup AOFlagger AOFlagger (flagger framework based on the
+// SumThreshold method)
+//
+// This package contains several programs related to flagging,
+// most important the programs "rficonsole" and "rfigui". See
+// the LOFAR cookbook for general info.
+// 
+// @{
+//   \defgroup AOFlagger AOFlagger
+// @}
diff --git a/CEP/DP3/AOFlagger/src/CMakeLists.txt b/CEP/DP3/AOFlagger/src/CMakeLists.txt
index 60ecf1d07b1..73104a9edd4 100644
--- a/CEP/DP3/AOFlagger/src/CMakeLists.txt
+++ b/CEP/DP3/AOFlagger/src/CMakeLists.txt
@@ -6,7 +6,7 @@ lofar_add_bin_program(rfihistory rfihistory.cpp)
 
 lofar_add_bin_program(aofrequencyfilter aofrequencyfilter.cpp rfi/thresholdtools.cpp util/rng.cpp msio/image2d.cpp msio/fitsfile.cpp util/aologger.cpp)
 
-lofar_add_bin_program(aocopyallscript aocopyallscript.cpp)
+lofar_add_bin_program(aorefscript aorefscript.cpp)
 
 set(GUI_PLOT_FILES
   gui/plot/horizontalplotscale.cpp
diff --git a/CEP/DP3/AOFlagger/src/aocopyallscript.cpp b/CEP/DP3/AOFlagger/src/aorefscript.cpp
similarity index 73%
rename from CEP/DP3/AOFlagger/src/aocopyallscript.cpp
rename to CEP/DP3/AOFlagger/src/aorefscript.cpp
index 216d84ec879..938a86e36ad 100644
--- a/CEP/DP3/AOFlagger/src/aocopyallscript.cpp
+++ b/CEP/DP3/AOFlagger/src/aorefscript.cpp
@@ -20,18 +20,29 @@
 
 #include <iostream>
 
+#include <AOFlagger/ref/concatenatescript.h>
 #include <AOFlagger/ref/copyallscript.h>
+#include <AOFlagger/ref/refmovescript.h>
 
 using namespace std;
 
 int main(int argc, char *argv[])
 {
-	if(argc != 3)
+	if(argc != 4)
 	{
-		cerr << "Syntax: " << argv[0] << " <reffile> <local-destination>\n";
+		cerr << "Syntax: " << argv[0] << " <scripttype> <reffile> <local-destination>\n"
+			"script types: concatenate, copyall, refmove\n";
 		return -1;
 	} else {
-		AOTools::CopyAllScript::Make(cout, argv[1], argv[2]);
+		string type(argv[1]);
+		if(type == "concatenate")
+			AOTools::ConcatenateScript::Make(cout, argv[2], argv[3]);
+		else if(type =="copyall")
+			AOTools::CopyAllScript::Make(cout, argv[2], argv[3]);
+		else if(type =="refmove")
+			AOTools::RefMoveScript::Make(cout, argv[2], argv[3]);
+		else
+			cerr << "Incorrect script type given." << "\n";
 		return 0;
 	}
 }
-- 
GitLab