Index: trunk/Mars/mcore/ofits.h
===================================================================
--- trunk/Mars/mcore/ofits.h	(revision 16287)
+++ trunk/Mars/mcore/ofits.h	(revision 16331)
@@ -39,4 +39,5 @@
         string value;
         string comment;
+        string fitsString;
 
         off_t offset;   // offset in file
@@ -44,5 +45,5 @@
         bool changed;   // For closing the file
 
-        Key(const string &k="") : key(k), delim(false), offset(0), changed(true) { }
+        Key(const string &k="") : key(k), delim(false), fitsString(""), offset(0), changed(true) { }
 
         string Trim(const string &str)
@@ -180,4 +181,8 @@
         string Compile()
         {
+
+            if (fitsString != "")
+                return fitsString;
+
             ostringstream sout;
             sout << std::left << setw(8) << key;
@@ -331,11 +336,14 @@
 
     bool fCommentTrimming;
+    bool fManualExtName;
 
 public:
-    ofits() : fCommentTrimming(false)
+    ofits() : fCommentTrimming(false),
+              fManualExtName(false)
     {
     }
     ofits(const char *fname) : ofstream(),
-                               fCommentTrimming(false)
+                               fCommentTrimming(false),
+                               fManualExtName(false)
     {
         this->open(fname);
@@ -343,5 +351,5 @@
     ~ofits() { close(); }
 
-    void open(const char * filename)
+    void open(const char * filename, bool addEXTNAMEKey=true)
     {
         fDataSum  = 0;
@@ -359,5 +367,8 @@
         SetInt("GCOUNT",                     1, "one data group (required keyword)");
         SetInt("TFIELDS",                    0, "number of fields in each row");
-        SetStr("EXTNAME", "", "name of extension table");
+        if (addEXTNAMEKey)
+            SetStr("EXTNAME", "", "name of extension table");
+        else
+            fManualExtName = true;
         SetStr("CHECKSUM", "0000000000000000", "Checksum for the whole HDU");
         SetStr("DATASUM",  "         0", "Checksum for the data block");
@@ -370,5 +381,34 @@
         fCommentTrimming = allow;
     }
-
+    //Etienne: required to enable 1 to 1 reconstruction of files
+    bool SetKeyComment(const string& key, const string& comment)
+    {
+        auto it = findkey(key);
+        if (it==fKeys.end())
+            return false;
+        it->comment = comment;
+        it->changed = true;
+        return true;
+    }
+    bool SetKeyFromFitsString(const string& fitsString)
+    {
+        if (fTable.num_rows>0)
+        {
+            ostringstream sout;
+            sout << "No new header key can be defined, rows were already written to the file... ignoring new key '" << fitsString << "'";
+#ifdef __EXCEPTIONS
+                throw runtime_error(sout.str());
+#else
+                gLog << ___err___ << "ERROR - " << sout.str() << endl;
+                return false;
+#endif
+        }
+
+        Key entry;
+        entry.fitsString = fitsString;
+        entry.changed = true;
+        fKeys.push_back(entry);
+        return true;
+    }
     bool SetRaw(const string &key, const string &val, const string &comment)
     {
@@ -455,5 +495,6 @@
     }
 
-    bool AddColumn(uint32_t cnt, char typechar, const string &name, const string &unit, const string &comment="")
+    //ETIENNE to be able to restore the file 1 to 1, I must restore the header keys myself
+    bool AddColumn(uint32_t cnt, char typechar, const string &name, const string &unit, const string &comment="", bool addHeaderKeys=true)
     {
         if (tellp()<0)
@@ -533,10 +574,11 @@
         }
 
-        SetStr(formkey.str(), type.str(), typecom.str());
-        SetStr(typekey.str(), name,       comment);
-
-        if (!unit.empty())
-            SetStr(unitkey.str(), unit, unitcom.str());
-
+        if (addHeaderKeys)
+        {
+            SetStr(formkey.str(), type.str(), typecom.str());
+            SetStr(typekey.str(), name,       comment);
+            if (!unit.empty())
+                SetStr(unitkey.str(), unit, unitcom.str());
+        }
         size_t size = 0;
 
@@ -682,5 +724,6 @@
         fHeaderSum = WriteFitsHeader();
 
-        SetStr("EXTNAME", name);
+        if (!fManualExtName)
+            SetStr("EXTNAME", name);
         SetInt("NAXIS1",  fTable.bytes_per_row);
         SetInt("TFIELDS", fTable.cols.size());
@@ -819,4 +862,21 @@
         return false;
 #endif
+    }
+
+    pair<string, int> GetChecksumData()
+    {
+        string datasum;
+        string checksum;
+        //cannot directly use the Get methods, because they are only in fits.h
+        for (vector<Key>::const_iterator it=fKeys.begin(); it!= fKeys.end(); it++)
+        {
+            if (it->key == "CHECKSUM") checksum = it->value;
+            if (it->key == "DATASUM") datasum = it->value;
+        }
+        if (checksum[0] == '\'')
+            checksum = checksum.substr(1,checksum.size()-2);
+        if (datasum[0] == '\'')
+            datasum = datasum.substr(1, datasum.size()-2);
+        return make_pair(checksum, atoi(datasum.c_str()));
     }
 };
