Index: trunk/FACT++/src/fitsdump.cc
===================================================================
--- trunk/FACT++/src/fitsdump.cc	(revision 10836)
+++ trunk/FACT++/src/fitsdump.cc	(revision 10837)
@@ -17,5 +17,4 @@
 class FitsDumper
 {
-
 public:
     FitsDumper();
@@ -38,8 +37,11 @@
 
     template<class T>
-        void Write(ostream &out, const unsigned char* &ptr) const;
+        T PtrToValue(const unsigned char* &ptr) const;
+//    template<class T>
+//        double PtrToDouble(const unsigned char *ptr) const;
+//    double PtrToDouble(const unsigned char *ptr, CCfits::ValueType type) const;
 
     /// Write a single row of the selected data
-    int  WriteRow(ostream &, const vector<string> &, const vector<int> &, unsigned char *) const;
+    int  WriteRow(ostream &, const vector<CCfits::Column*> &, const vector<int> &, unsigned char *) const;
 
     bool OpenFile(const string &);        /// Open a file
@@ -54,4 +56,5 @@
     bool Dump(const string &, const vector<string> &list, int);
 
+//    bool Plot(const vector<string> &list);
 
 public:
@@ -122,13 +125,41 @@
 
 template<class T>
-void FitsDumper::Write(ostream &out, const unsigned char* &ptr) const
+T FitsDumper::PtrToValue(const unsigned char* &ptr) const
 {
     T t;
     reverse_copy(ptr, ptr+sizeof(T), reinterpret_cast<unsigned char*>(&t));
-    out << t;
-
     ptr += sizeof(T);
-}
-
+
+    return t;
+}
+/*
+template<class T>
+double FitsDumper::PtrToDouble(const unsigned char *ptr) const
+{
+    T t;
+    reverse_copy(ptr, ptr+sizeof(T), reinterpret_cast<unsigned char*>(&t));
+    return t;
+}
+
+double FitsDumper::PtrToDouble(const unsigned char *ptr, CCfits::ValueType type) const
+{
+    switch (type)
+    {
+    case CCfits::Tbyte:     return PtrToDouble<uint8_t> (ptr);
+    case CCfits::Tushort:   return PtrToDouble<uint16_t>(ptr);
+    case CCfits::Tuint:
+    case CCfits::Tulong:    return PtrToDouble<uint32_t>(ptr);
+    case CCfits::Tshort:    return PtrToDouble<int16_t> (ptr);
+    case CCfits::Tint:
+    case CCfits::Tlong:     return PtrToDouble<int32_t> (ptr);
+    case CCfits::Tlonglong: return PtrToDouble<int64_t> (ptr);
+    case CCfits::Tfloat:    return PtrToDouble<float>   (ptr);
+    case CCfits::Tdouble:   return PtrToDouble<double>  (ptr);
+
+    default:
+        throw runtime_error("Data type not implemented yet.");
+    }
+}
+*/
 
 // --------------------------------------------------------------------------
@@ -151,12 +182,11 @@
 //!         the memory were the row has been loaded by cfitsio
 //
-int FitsDumper::WriteRow(ostream &out, const vector<string> &list, const vector<int> &offsets, unsigned char* fitsBuffer) const
+int FitsDumper::WriteRow(ostream &out, const vector<CCfits::Column*> &list, const vector<int> &offsets, unsigned char* fitsBuffer) const
 {
     int cnt = 0;
 
-    for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++)
-    {
-        // Loop over all columns in our list of requested columns
-        const CCfits::Column *col = fColMap.find(*it)->second;
+    for (vector<CCfits::Column*>::const_iterator it=list.begin(); it!=list.end(); it++)
+    {
+        const CCfits::Column *col = *it;
 
         // CCfits starts counting at 1 not 0
@@ -171,14 +201,14 @@
             switch (col->type())
             {
-            case CCfits::Tbyte:     Write<uint8_t> (out, ptr); break;
-            case CCfits::Tushort:   Write<uint16_t>(out, ptr); break;
+            case CCfits::Tbyte:     out << PtrToValue<uint8_t> (ptr); break;
+            case CCfits::Tushort:   out << PtrToValue<uint16_t>(ptr); break;
             case CCfits::Tuint:
-            case CCfits::Tulong:    Write<uint32_t>(out, ptr); break;
-            case CCfits::Tshort:    Write<int16_t> (out, ptr); break;
+            case CCfits::Tulong:    out << PtrToValue<uint32_t>(ptr); break;
+            case CCfits::Tshort:    out << PtrToValue<int16_t> (ptr); break;
             case CCfits::Tint:
-            case CCfits::Tlong:     Write<int32_t> (out, ptr); break;
-            case CCfits::Tlonglong: Write<int64_t> (out, ptr); break;
-            case CCfits::Tfloat:    Write<float>   (out, ptr); break;
-            case CCfits::Tdouble:   Write<double>  (out, ptr); break;
+            case CCfits::Tlong:     out << PtrToValue<int32_t> (ptr); break;
+            case CCfits::Tlonglong: out << PtrToValue<int64_t> (ptr); break;
+            case CCfits::Tfloat:    out << PtrToValue<float>   (ptr); break;
+            case CCfits::Tdouble:   out << PtrToValue<double>  (ptr); break;
 
             default:
@@ -189,10 +219,4 @@
             out << " ";
             cnt++;
-
-            if (out.fail())
-            {
-                cerr << "ERROR - writing output: " << strerror(errno) << endl;
-                return 0;
-            }
         }
     }
@@ -200,4 +224,10 @@
     if (cnt>0)
         out << endl;
+
+    if (out.fail())
+    {
+        cerr << "ERROR - writing output: " << strerror(errno) << endl;
+        return -1;
+    }
 
     return cnt;
@@ -420,10 +450,8 @@
         }
 
-
+    // FIXME: Maybe do this when opening a table?
     const vector<int> offsets = CalculateOffsets();
     if (offsets.size()==0)
         return false;
-
-    const int size = offsets[offsets.size()-1];
 
     ofstream out(filename=="-"?"/dev/stdout":filename);
@@ -459,4 +487,12 @@
     out << "#" << endl;
 
+
+    // Loop over all columns in our list of requested columns
+    vector<CCfits::Column*> columns;
+    for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++)
+        columns.push_back(fColMap.find(*it)->second);
+
+
+    const int size = offsets[offsets.size()-1];
     unsigned char* fitsBuffer = new unsigned char[size];
 
@@ -470,10 +506,86 @@
             break;
         }
-        WriteRow(out, list, offsets, fitsBuffer);
+        if (WriteRow(out, columns, offsets, fitsBuffer)<0)
+        {
+            status=1;
+            break;
+        }
     }
     delete[] fitsBuffer;
 
-    return status;
-}
+    return status==0;
+}
+
+// --------------------------------------------------------------------------
+//
+//! Perform the actual dump, based on the current parameters
+//
+/*
+bool FitsDumper::Plot(const vector<string> &list)
+{
+   for (vector<string>::const_iterator it=list.begin(); it!=list.end(); it++)
+        if (fColMap.find(*it) == fColMap.end())
+        {
+            cerr << "WARNING - Column '" << *it << "' not found in table." << endl;
+            return false;
+        }
+
+    // FIXME: Maybe do this when opening a table?
+    const vector<int> offsets = CalculateOffsets();
+    if (offsets.size()==0)
+        return false;
+
+    // Loop over all columns in our list of requested columns
+    const CCfits::Column *col[3] =
+    {
+        list.size()>0 ? fColMap.find(list[0])->second : 0,
+        list.size()>1 ? fColMap.find(list[1])->second : 0,
+        list.size()>2 ? fColMap.find(list[2])->second : 0
+    };
+
+    const int size = offsets[offsets.size()-1];
+    unsigned char* fitsBuffer = new unsigned char[size];
+
+    const int idx = 0;
+
+    // CCfits starts counting at 1 not 0
+    const size_t pos[3] =
+    {
+        col[0] ? offsets[col[0]->index()-1] + idx*col[0]->width()*ValueTypeToSize(col[0]->type()) : 0,
+        col[1] ? offsets[col[1]->index()-1] + idx*col[1]->width()*ValueTypeToSize(col[1]->type()) : 0,
+        col[2] ? offsets[col[2]->index()-1] + idx*col[2]->width()*ValueTypeToSize(col[2]->type()) : 0
+    };
+
+    int status = 0;
+    for (int i=1; i<=fTable->rows(); i++)
+    {
+        fits_read_tblbytes(fFile->fitsPointer(), i, 1, size, fitsBuffer, &status);
+        if (status)
+        {
+            cerr << "An error occurred while reading fits row #" << i << " error code: " << status << endl;
+            break;
+        }
+
+        try
+        {
+            const double x[3] =
+            {
+                col[0] ? PtrToDouble(fitsBuffer+pos[0], col[0]->type()) : 0,
+                col[1] ? PtrToDouble(fitsBuffer+pos[1], col[1]->type()) : 0,
+                col[2] ? PtrToDouble(fitsBuffer+pos[2], col[2]->type()) : 0
+            };
+        }
+        catch (const runtime_error &e)
+        {
+            cerr << e.what() << endl;
+            status=1;
+            break;
+        }
+    }
+    delete[] fitsBuffer;
+
+    return status==0;
+}
+*/
 
 // --------------------------------------------------------------------------
