Index: trunk/Mars/mbase/fits.h
===================================================================
--- trunk/Mars/mbase/fits.h	(revision 11552)
+++ trunk/Mars/mbase/fits.h	(revision 11557)
@@ -1,4 +1,6 @@
-#ifndef MARS_MFits
-#define MARS_MFits
+#ifndef MARS_fits
+#define MARS_fits
+
+#include <stdint.h>
 
 #include <map>
@@ -7,5 +9,7 @@
 #include <algorithm>
 
-#include <stdint.h>
+#ifdef __EXCEPTIONS
+#include <stdexcept>
+#endif
 
 #ifdef __CINT__
@@ -14,22 +18,19 @@
 
 #ifndef __MARS__
-#include <vector>
-#include <fstream>
 #include <iomanip>
-#include <iostream>
-#define MZlib ifstream
 #define gLog cerr
 #define err ""
 #define all ""
 #else
-#include "MZlib.h"
-
 #include "MLog.h"
 #include "MLogManip.h"
 #endif
 
-using namespace std;
-
-class MFits : public MZlib
+#include "izstream.h"
+
+namespace std
+{
+
+class fits : public izstream
 {
 public:
@@ -78,12 +79,12 @@
         {
             // Trim Both leading and trailing spaces
-            const size_t start = str.find_first_not_of(c); // Find the first character position after excluding leading blank spaces
-            const size_t end   = str.find_last_not_of(c);  // Find the first character position from reverse af
+            const size_t pstart = str.find_first_not_of(c); // Find the first character position after excluding leading blank spaces
+            const size_t pend   = str.find_last_not_of(c);  // Find the first character position from reverse af
 
             // if all spaces or empty return an empty string
-            if (string::npos==start || string::npos==end)
+            if (string::npos==pstart || string::npos==pend)
                 return string();
 
-            return str.substr(start, end-start+1);
+            return str.substr(pstart, pend-pstart+1);
         }
 
@@ -93,18 +94,36 @@
             if (it==keys.end())
             {
-                gLog << err << "ERROR - Key '" << key << "' not found." << endl;
+                ostringstream str;
+                str << "Key '" << key << "' not found.";
+#ifdef __EXCEPTIONS
+                throw runtime_error(str.str());
+#else
+                gLog << err << "ERROR - " << str.str() << endl;
                 return false;
+#endif
             }
 
             if (it->second.type!=type)
             {
-                gLog << err << "ERROR - Wrong type for key '" << key << "': expected " << type << ", found " << it->second.type << "." << endl;
+                ostringstream str;
+                str << "Wrong type for key '" << key << "': expected " << type << ", found " << it->second.type << ".";
+#ifdef __EXCEPTIONS
+                throw runtime_error(str.str());
+#else
+                gLog << err << "ERROR - " << str.str() << endl;
                 return false;
+#endif
             }
 
             if (!value.empty() && it->second.value!=value)
             {
-                gLog << err << "ERROR - Wrong value for key '" << key << "': expected " << value << ", found " << it->second.value << "." << endl;
+                ostringstream str;
+                str << "Wrong value for key '" << key << "': expected " << value << ", found " << it->second.value << ".";
+#ifdef __EXCEPTIONS
+                throw runtime_error(str.str());
+#else
+                gLog << err << "ERROR - " << str.str() << endl;
                 return false;
+#endif
             }
 
@@ -197,11 +216,11 @@
                     return;
 
-                const string name = Get<string>("TTYPE"+num.str());
-                const string fmt  = Get<string>("TFORM"+num.str());
-
-                istringstream in(fmt);
+                const string id  = Get<string>("TTYPE"+num.str());
+                const string fmt = Get<string>("TFORM"+num.str());
+
+                istringstream sin(fmt);
                 int n = 0;
-                in >> n;
-                if (!in)
+                sin >> n;
+                if (!sin)
                     n = 1;
 
@@ -226,11 +245,19 @@
                 // case 'Q': size = 16; break; // array descriptor (64bit)
                 default:
-                    gLog << err << "FITS format '" << fmt << "' not yet supported." << endl;
-                    continue;
+                    {
+                        ostringstream str;
+                        str << "FITS format TFORM='" << fmt << "' not yet supported.";
+#ifdef __EXCEPTIONS
+                        throw runtime_error(str.str());
+#else
+                        gLog << err << "ERROR - " << str.str() << endl;
+                        return;
+#endif
+                    }
                 }
 
                 const Table::Column col = { bytes, n, size, type };
 
-                cols[name] = col;
+                cols[id] = col;
                 bytes  += n*size;
             }
@@ -238,6 +265,10 @@
             if (bytes!=bytes_per_row)
             {
-                gLog << err << "Column size mismatch" << endl;
+#ifdef __EXCEPTIONS
+                throw runtime_error("Column size mismatch");
+#else
+                gLog << err << "ERROR - Column size mismatch" << endl;
                 return;
+#endif
             }
 
@@ -275,9 +306,15 @@
             T Get(const string &key) const
         {
-            const ::map<string,Entry>::const_iterator it = keys.find(key);
+            const map<string,Entry>::const_iterator it = keys.find(key);
             if (it==keys.end())
             {
-                gLog << err << "Key '" << key << "' not found." << endl;
+                ostringstream str;
+                str << "Key '" << key << "' not found." << endl;
+#ifdef __EXCEPTIONS
+                throw runtime_error(str.str());
+#else
+                gLog << err << "ERROR - " << str.str() << endl;
                 return 0;
+#endif
             }
             return it->second.Get<T>();
@@ -306,5 +343,5 @@
     vector<string> ReadBlock(vector<string> &vec)
     {
-        bool end = false;
+        bool endtag = false;
         for (int i=0; i<36; i++)
         {
@@ -324,7 +361,7 @@
 
             if (str=="END                                                                             ")
-                end = true;
-
-            if (end)
+                endtag = true;
+
+            if (endtag)
                 continue;
 
@@ -346,5 +383,5 @@
 
 public:
-    MFits(const string &fname) : MZlib(fname.c_str())
+    fits(const string &fname) : izstream(fname.c_str())
     {
         char simple[6];
@@ -355,7 +392,11 @@
         if (memcmp(simple, "SIMPLE", 6))
         {
-            gLog << err << "File is not a FITS file." << endl;
             clear(rdstate()|ios::badbit);
+#ifdef __EXCEPTIONS
+            throw runtime_error("File is not a FITS file.");
+#else
+            gLog << err << "ERROR - File is not a FITS file." << endl;
             return;
+#endif
         }
 
@@ -371,7 +412,11 @@
                 if (!good())
                 {
-                    gLog << err << "FITS file corrupted." << endl;
                     clear(rdstate()|ios::badbit);
+#ifdef __EXCEPTIONS
+                    throw runtime_error("FITS file corrupted.");
+#else
+                    gLog << err << "ERROR - FITS file corrupted." << endl;
                     return;
+#endif
                 }
 
@@ -396,9 +441,6 @@
 
                 if (!fTable)
-                    return;
-
-                if (!fTable.Check("TELESCOP", 'T', "FACT"))
                 {
-                    gLog << err << "Not a valid FACT fits file." << endl;
+                    clear(rdstate()|ios::badbit);
                     return;
                 }
@@ -427,5 +469,5 @@
     void ReadRow(size_t row)
     {
-        // if (row!=fRow+1) // Fast seeking is ensured by MZlib
+        // if (row!=fRow+1) // Fast seeking is ensured by izstream
         seekg(fTable.offset+row*fTable.bytes_per_row);
 
@@ -439,6 +481,6 @@
         void revcpy(char *dest, const char *src, int num)
     {
-        const char *end = src + num*N;
-        for (const char *ptr = src; ptr<end; ptr+=N, dest+=N)
+        const char *pend = src + num*N;
+        for (const char *ptr = src; ptr<pend; ptr+=N, dest+=N)
             reverse_copy(ptr, ptr+N, dest);
     }
@@ -492,22 +534,38 @@
         if (fTable.cols.count(name)==0)
         {
-            gLog << err <<"SetPtrAddress('" << name << "') - Column not found." << endl;
+            ostringstream str;
+            str <<"SetPtrAddress('" << name << "') - Column not found." << endl;
+#ifdef __EXCEPTIONS
+            throw runtime_error(str.str());
+#else
+            gLog << err << "ERROR - " << str.str() << endl;
             return false;
+#endif
         }
 
         if (sizeof(T)!=fTable.cols[name].size)
         {
-            gLog << err << "SetPtrAddress('" << name << "') - Element size mismatch: expected "
+            ostringstream str;
+            str << "SetPtrAddress('" << name << "') - Element size mismatch: expected "
                 << fTable.cols[name].size << " from header, got " << sizeof(T) << endl;
-
+#ifdef __EXCEPTIONS
+            throw runtime_error(str.str());
+#else
+            gLog << err << "ERROR - " << str.str() << endl;
             return false;
+#endif
         }
 
         if (cnt!=fTable.cols[name].num)
         {
-            gLog << err << "SetPtrAddress('" << name << "') - Element count mismatch: expected "
+            ostringstream str;
+            str << "SetPtrAddress('" << name << "') - Element count mismatch: expected "
                 << fTable.cols[name].num << " from header, got " << cnt << endl;
-
+#ifdef __EXCEPTIONS
+            throw runtime_error(str.str());
+#else
+            gLog << err << "ERROR - " << str.str() << endl;
             return false;
+#endif
         }
 
@@ -543,6 +601,12 @@
         if (fTable.cols.count(name)==0)
         {
-            gLog << err <<"SetPtrAddress('" << name << "') - Column not found." << endl;
+            ostringstream str;
+            str <<"SetPtrAddress('" << name << "') - Column not found." << endl;
+#ifdef __EXCEPTIONS
+            throw runtime_error(str.str());
+#else
+            gLog << err << "ERROR - " << str.str() << endl;
             return false;
+#endif
         }
 
@@ -560,4 +624,5 @@
     uint64_t GetUInt(const string &key) const { return fTable.Get<uint64_t>(key); }
     double   GetFloat(const string &key) const { return fTable.Get<double>(key); }
+    string   GetStr(const string &key) const { return fTable.Get<string>(key); }
 
     size_t GetN(const string &key) const
@@ -574,3 +639,5 @@
     void PrintColumns() const { fTable.PrintColumns(); }
 };
-#endif
+
+};
+#endif
