Index: trunk/Mars/mcore/ofits.h
===================================================================
--- trunk/Mars/mcore/ofits.h	(revision 17777)
+++ trunk/Mars/mcore/ofits.h	(revision 17778)
@@ -2,16 +2,6 @@
 #define MARS_ofits
 
-#include <string>
-#include <string.h>
-#include <algorithm>
-#include <sstream>
-#include <iostream>
-#include <fstream>
-#include <iomanip>
-#include <vector>
-#include <algorithm>
-#include <stdexcept>
-
-#define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
+#include "FITS.h"
+#include "fits.h"
 
 #ifndef PACKAGE_NAME
@@ -31,26 +21,9 @@
 #endif
 
-#ifndef __MARS__
-#ifndef gLog
-#define gLog std::cerr
-#define ___err___   ""
-#define ___warn___  ""
-#define ___all___   ""
-#endif
-#else
-#include "MLog.h"
-#include "MLogManip.h"
-#define ___err___   err
-#define ___warn___  warn
-#define ___all___   all
-#endif
-
-#include "FITS.h"
-#include "checksum.h"
-
 // Sloppy:  allow / <--- left
 //          allow all characters (see specs for what is possible)
 
 // units: m kg s rad sr K A mol cd Hz J W V N Pa C Ohm S F Wb T Hlm lx
+
 
 class ofits : public std::ostream
@@ -72,5 +45,6 @@
         bool changed;   // For closing the file
 
-        Key(const std::string &k="") : key(k), delim(false), fitsString(""), offset(0), changed(true) { }
+        Key() : delim(false), offset(0), changed(true) { }
+        Key(const std::string &s) : delim(false), fitsString(s), offset(0), changed(true) { }
 
         std::string Trim(const std::string &str)
@@ -215,6 +189,5 @@
         std::string Compile()
         {
-
-            if (fitsString != "")
+            if (!fitsString.empty())
                 return fitsString;
 
@@ -318,11 +291,9 @@
         entry.value   = value;
         entry.comment = comment;
-        entry.offset  = 0;
-        entry.changed = true;
 
         if (!entry.check(fCommentTrimming))
             return false;
 
-        fKeys.push_back(entry);
+        fKeys.emplace_back(entry);
         return true;
     }
@@ -455,4 +426,5 @@
         fCommentTrimming = allow;
     }
+
     //Etienne: required to enable 1 to 1 reconstruction of files
     bool SetKeyComment(const std::string& key, const std::string& comment)
@@ -461,8 +433,13 @@
         if (it==fKeys.end())
             return false;
+
         it->comment = comment;
         it->changed = true;
         return true;
     }
+        /* tbretz, I removed that, because it does not comply
+         with the FITS standard, it omits all checks... it
+         neither checks if a row already exists, not
+         checks the chcracter set
     bool SetKeyFromFitsString(const std::string& fitsString)
     {
@@ -484,5 +461,52 @@
         fKeys.push_back(entry);
         return true;
-    }
+}*/
+
+
+    bool CopyKeys(const fits &fin, bool update=false)
+    {
+        if (fTable.num_rows>0)
+        {
+#ifdef __EXCEPTIONS
+            throw std::runtime_error("No header keys can be copied, rows were already written to the file... ignoring CopyKeys().");
+#else
+            gLog << ___err___ << "ERROR - No header key can be copyied, rows were already written to the file... ignoring CopyKeys()." << std::endl;
+            return false;
+#endif
+        }
+
+        const auto &keys = fin.GetKeys();
+
+        // We can assume that the keys are all valid
+        for (auto it=keys.cbegin(); it!=keys.cend(); it++)
+        {
+            const std::string &key = it->first;
+
+            if (FITS::IsReservedKeyWord(key))
+                continue;
+
+            const auto &entry = it->second;
+
+            // If no delimit add the row no matter if it alread exists
+            if (entry.fitsString[9]=='=')
+            {
+                // if the row already exists: remove it
+                const auto it2 = findkey(key);
+                if (it2!=fKeys.end())
+                {
+                    if (!update)
+                        continue;
+
+                    fKeys.erase(it2);
+                }
+            }
+
+            fKeys.emplace_back(entry.fitsString);
+        }
+
+        return true;
+    }
+
+
     bool SetRaw(const std::string &key, const std::string &val, const std::string &comment)
     {
@@ -569,43 +593,4 @@
     }
 
-    std::string CommentFromType(char type)
-    {
-        std::string comment;
-
-        switch (type)
-        {
-        case 'L': comment = "[1-byte BOOL]";  break;
-        case 'A': comment = "[1-byte CHAR]";  break;
-        case 'B': comment = "[1-byte BOOL]";  break;
-        case 'I': comment = "[2-byte INT]";   break;
-        case 'J': comment = "[4-byte INT]";   break;
-        case 'K': comment = "[8-byte INT]";   break;
-        case 'E': comment = "[4-byte FLOAT]"; break;
-        case 'D': comment = "[8-byte FLOAT]"; break;
-        case 'Q': comment = "[var. Length]"; break;
-        }
-
-        return comment;
-    }
-
-    uint32_t SizeFromType(char type)
-    {
-        size_t size = 0;
-
-        switch (type)
-        {
-        case 'L': size = 1; break;
-        case 'A': size = 1; break;
-        case 'B': size = 1; break;
-        case 'I': size = 2; break;
-        case 'J': size = 4; break;
-        case 'K': size = 8; break;
-        case 'E': size = 4; break;
-        case 'D': size = 8; break;
-        case 'Q': size = 16; break;
-        }
-
-        return size;
-    }
     //ETIENNE to be able to restore the file 1 to 1, I must restore the header keys myself
     virtual bool AddColumn(uint32_t cnt, char typechar, const std::string &name, const std::string &unit, const std::string &comment="", bool addHeaderKeys=true)
@@ -682,10 +667,11 @@
             const std::string nc = std::to_string(fTable.num_cols);
 #endif
-            SetStr("TFORM"+nc, type.str(), "format of "+name+" "+CommentFromType(typechar));
+            SetStr("TFORM"+nc, type.str(), "format of "+name+" "+FITS::CommentFromType(typechar));
             SetStr("TTYPE"+nc, name, comment);
             if (!unit.empty())
                 SetStr("TUNIT"+nc, unit, "unit of "+name);
         }
-        size_t size = SizeFromType(typechar);
+
+        const size_t size = FITS::SizeFromType(typechar);
 
         Table::Column col;
@@ -697,5 +683,5 @@
         col.offset = fTable.bytes_per_row;
 
-        fTable.cols.push_back(col);
+        fTable.cols.emplace_back(col);
 
         fTable.bytes_per_row += size*cnt;
