# HG changeset patch
# Parent 111914973bb62bc4d4fb652d40803d130218a6dc

diff --git a/src/sampling/Make/files b/src/sampling/Make/files
--- a/src/sampling/Make/files
+++ b/src/sampling/Make/files
@@ -19,6 +19,7 @@
 $(setWriters)/jplot/jplotSetWriterRunTime.C
 $(setWriters)/raw/rawSetWriterRunTime.C
 $(setWriters)/xmgrace/xmgraceSetWriterRunTime.C
+$(setWriters)/csv/csvSetWriterRunTime.C
 
 cuttingPlane/cuttingPlane.C
 
diff --git a/src/sampling/sampledSet/writers/raw/rawSetWriter.C b/src/sampling/sampledSet/writers/csv/csvSetWriter.C
copy from src/sampling/sampledSet/writers/raw/rawSetWriter.C
copy to src/sampling/sampledSet/writers/csv/csvSetWriter.C
--- a/src/sampling/sampledSet/writers/raw/rawSetWriter.C
+++ b/src/sampling/sampledSet/writers/csv/csvSetWriter.C
@@ -23,7 +23,7 @@
 
 \*---------------------------------------------------------------------------*/
 
-#include "rawSetWriter.H"
+#include "csvSetWriter.H"
 #include "coordSet.H"
 #include "fileName.H"
 #include "OFstream.H"
@@ -31,34 +31,36 @@
 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
 
 template<class Type>
-Foam::rawSetWriter<Type>::rawSetWriter()
+Foam::csvSetWriter<Type>::csvSetWriter()
 :
     writer<Type>()
-{}
+{
+    this->separator_ = token::COMMA;
+}
 
 
 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
 
 template<class Type>
-Foam::rawSetWriter<Type>::~rawSetWriter()
+Foam::csvSetWriter<Type>::~csvSetWriter()
 {}
 
 
 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
 
 template<class Type>
-Foam::fileName Foam::rawSetWriter<Type>::getFileName
+Foam::fileName Foam::csvSetWriter<Type>::getFileName
 (
     const coordSet& points,
     const wordList& valueSetNames
 ) const
 {
-    return this->getBaseName(points, valueSetNames) + ".xy";
+    return this->getBaseName(points, valueSetNames) + ".csv";
 }
 
 
 template<class Type>
-void Foam::rawSetWriter<Type>::write
+void Foam::csvSetWriter<Type>::write
 (
     const coordSet& points,
     const wordList& valueSetNames,
@@ -66,6 +68,8 @@
     Ostream& os
 ) const
 {
+    writeHeader(points,valueSetNames,os);
+
     // Collect sets into columns
     List<const List<Type>*> columns(valueSets.size());
 
@@ -79,7 +83,7 @@
 
 
 template<class Type>
-void Foam::rawSetWriter<Type>::write
+void Foam::csvSetWriter<Type>::write
 (
     const bool writeTracks,
     const PtrList<coordSet>& points,
@@ -88,9 +92,11 @@
     Ostream& os
 ) const
 {
+    writeHeader(points[0],valueSetNames,os);
+
     if (valueSets.size() != valueSetNames.size())
     {
-        FatalErrorIn("rawSetWriter<Type>::write(..)")
+        FatalErrorIn("csvSetWriter<Type>::write(..)")
             << "Number of variables:" << valueSetNames.size() << endl
             << "Number of valueSets:" << valueSets.size()
             << exit(FatalError);
@@ -111,5 +117,74 @@
     }
 }
 
+template<class Type>
+void Foam::csvSetWriter<Type>::writeComponentSeparator
+(
+    Ostream& os
+) const
+{
+    os << this->separator_;
+}
+
+namespace Foam {
+    // otherwise compiler complains about specialization
+template<>
+void csvSetWriter<scalar>::writeHeader
+(
+    const coordSet& points,
+    const wordList& valueSetNames,
+    Ostream& os
+) const
+{
+    writeCoordHeader(points,os);
+
+    forAll(valueSetNames,i) {
+        if( i>0 ) {
+            os << this->separator_;
+        }
+        os << valueSetNames[i];
+    }
+
+    os << nl;
+}
+} // end namespace
+
+template<class Type>
+void Foam::csvSetWriter<Type>::writeHeader
+(
+    const coordSet& points,
+    const wordList& valueSetNames,
+    Ostream& os
+) const
+{
+    writeCoordHeader(points,os);
+    
+    forAll(valueSetNames,i) {
+        for(label j=0;j<Type::nComponents;j++) {
+            if( i>0 || j>0 ) {
+                os << this->separator_;
+            }
+            os << valueSetNames[i] << "_" << j;
+        }
+    }
+
+    os << nl;
+}
+
+template<class Type>
+void Foam::csvSetWriter<Type>::writeCoordHeader
+(
+    const coordSet& points,
+    Ostream& os
+) const
+{
+    if(points.hasVectorAxis()) {
+        for(unsigned int i=0;i<points.axis().size();i++) {
+            os << points.axis()[i] << this->separator_;
+        }
+    } else {
+        os << points.axis() << this->separator_;
+    }    
+}
 
 // ************************************************************************* //
diff --git a/src/sampling/sampledSet/writers/raw/rawSetWriter.H b/src/sampling/sampledSet/writers/csv/csvSetWriter.H
copy from src/sampling/sampledSet/writers/raw/rawSetWriter.H
copy to src/sampling/sampledSet/writers/csv/csvSetWriter.H
--- a/src/sampling/sampledSet/writers/raw/rawSetWriter.H
+++ b/src/sampling/sampledSet/writers/csv/csvSetWriter.H
@@ -22,17 +22,17 @@
     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
 
 Class
-    Foam::rawSetWriter
+    Foam::csvSetWriter
 
 Description
 
 SourceFiles
-    rawSetWriter.C
+    csvSetWriter.C
 
 \*---------------------------------------------------------------------------*/
 
-#ifndef rawSetWriter_H
-#define rawSetWriter_H
+#ifndef csvSetWriter_H
+#define csvSetWriter_H
 
 #include "writer.H"
 
@@ -42,29 +42,47 @@
 {
 
 /*---------------------------------------------------------------------------*\
-                       Class rawSetWriter Declaration
+                       Class csvSetWriter Declaration
 \*---------------------------------------------------------------------------*/
 
 template<class Type>
-class rawSetWriter
+class csvSetWriter
 :
     public writer<Type>
 {
 
+
+    void writeCoordHeader
+    (
+        const coordSet&,
+        Ostream&
+    ) const;
+
+    void writeHeader
+    (
+        const coordSet&,
+        const wordList&,
+        Ostream&
+    ) const;
+
+protected:
+
+    virtual void writeComponentSeparator(Ostream& os) const;
+
 public:
 
     //- Runtime type information
-    TypeName("raw");
+    TypeName("csv");
 
 
     // Constructors
 
         //- Construct null
-        rawSetWriter();
+        csvSetWriter();
 
 
     //- Destructor
-    virtual ~rawSetWriter();
+    virtual ~csvSetWriter();
 
 
     // Member Functions
@@ -101,7 +119,7 @@
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
 
 #ifdef NoRepository
-#   include "rawSetWriter.C"
+#   include "csvSetWriter.C"
 #endif
 
 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
diff --git a/src/sampling/sampledSet/writers/raw/rawSetWriterRunTime.C b/src/sampling/sampledSet/writers/csv/csvSetWriterRunTime.C
copy from src/sampling/sampledSet/writers/raw/rawSetWriterRunTime.C
copy to src/sampling/sampledSet/writers/csv/csvSetWriterRunTime.C
--- a/src/sampling/sampledSet/writers/raw/rawSetWriterRunTime.C
+++ b/src/sampling/sampledSet/writers/csv/csvSetWriterRunTime.C
@@ -23,7 +23,7 @@
 
 \*---------------------------------------------------------------------------*/
 
-#include "rawSetWriter.H"
+#include "csvSetWriter.H"
 #include "writers.H"
 #include "addToRunTimeSelectionTable.H"
 
@@ -31,7 +31,7 @@
 
 namespace Foam
 {
-    makeSetWriters(rawSetWriter);
+    makeSetWriters(csvSetWriter);
 }
 
 // ************************************************************************* //
diff --git a/src/sampling/sampledSet/writers/writer.C b/src/sampling/sampledSet/writers/writer.C
--- a/src/sampling/sampledSet/writers/writer.C
+++ b/src/sampling/sampledSet/writers/writer.C
@@ -107,7 +107,7 @@
     {
         writeCoord(points, pointI, os);
 
-        os << token::SPACE;
+        os << separator_;
         write(values[pointI], os);
         os << nl;
     }
@@ -128,7 +128,7 @@
 
         forAll(valuesPtrList, i)
         {
-            os << token::SPACE;
+            os << separator_;
             const List<Type>& values = *valuesPtrList[i];
             write(values[pointI], os);
         }
@@ -141,6 +141,8 @@
 
 template<class Type>
 Foam::writer<Type>::writer()
+:
+    separator_(token::SPACE)
 {}
 
 
@@ -174,16 +176,30 @@
 {
     for (direction d=0; d<VSType::nComponents; d++)
     {
+        if (d > 0)
+        {
+            writeComponentSeparator(os);
+        }
+
         os << value.component(d);
 
-        if (d <= VSType::nComponents-1)
-        {
-            os << ' ' << token::TAB;
-        }
+        // throws warning for Spherical tensors
+//         if (d < VSType::nComponents-1)
+//         {
+//             writeComponentSeparator(os);
+//         }
     }
     return os;
 }
 
+template<class Type>
+void Foam::writer<Type>::writeComponentSeparator
+(
+    Ostream& os
+) const
+{
+    os << separator_ << token::TAB;
+}
 
 template<class Type>
 Foam::Ostream& Foam::writer<Type>::write
diff --git a/src/sampling/sampledSet/writers/writer.H b/src/sampling/sampledSet/writers/writer.H
--- a/src/sampling/sampledSet/writers/writer.H
+++ b/src/sampling/sampledSet/writers/writer.H
@@ -86,6 +86,8 @@
 {
 
 protected:
+    //- Separator between entries
+    token separator_;
 
     //- Generates filename from coordSet and sampled fields
     fileName getBaseName(const coordSet&, const wordList&) const;
@@ -107,7 +109,8 @@
         Ostream& os
     ) const;
 
-
+    //- Writes a separator between components for vectors etc
+    virtual void writeComponentSeparator(Ostream& os) const;
 public:
 
     //- Runtime type information
