Index: trunk/Mars/mfileio/MWriteFitsFile.cc
===================================================================
--- trunk/Mars/mfileio/MWriteFitsFile.cc	(revision 14935)
+++ trunk/Mars/mfileio/MWriteFitsFile.cc	(revision 15277)
@@ -337,6 +337,20 @@
     return kTRUE;
 }
-
-
+template<>
+std::string MWriteFitsFile::GetFitsString(const double& value)
+{
+    std::ostringstream returnVal;
+    returnVal << std::setprecision(value>1e-100 && value<1e100 ? 15 : 14) << value;
+    std::string temp = returnVal.str();
+    std::replace(temp.begin(), temp.end(), 'e', 'E');
+    if (temp.find_first_of('E')==std::string::npos && temp.find_first_of('.')==std::string::npos)
+        temp += ".";
+    return temp;
+}
+template<>
+std::string MWriteFitsFile::GetFitsString(const float& value)
+{
+    return GetFitsString((double)(value));
+}
 // --------------------------------------------------------------------------
 //                                                                           
@@ -355,9 +369,10 @@
 
    // remove the extension from the filename
+   // this has been disabled for now
    char fileNameNoExt[strlen(GetFileName()) + 1];
    strcpy(fileNameNoExt, GetFileName());
-   char * pos = strrchr(fileNameNoExt, '.');
-   if (pos) *pos = 0;
-
+//   char * pos = strrchr(fileNameNoExt, '.');
+//   if (pos) *pos = 0;
+//*fLog << inf <<"Filename no ext: " << fileNameNoExt << endl;
    // loop over all FITS tables which have to be created.
    map<TString, map<TString, MFitsSubTable> >::iterator i_table =
@@ -373,8 +388,9 @@
       {
           dol = dol(0, dol.Length()-5);
-//          *fLog << err << "New name: " << dol.Data() << endl;
-      }
-//      else
-//          *fLog << err << dol(dol.Length()-5, dol.Length()).Data() << endl;
+      }
+      if (dol(dol.Length()-5, dol.Length()) == ".fits")
+      {
+          dol = dol(0, dol.Length()-5);
+      }
       dol += "_";
       dol += i_table->first;
@@ -1038,4 +1054,6 @@
     if (!fTableHeaderWritten[tableName])
     {
+        for (vector<ofits::Key>::const_iterator it = fHeaderKeys.begin(); it != fHeaderKeys.end(); it++)
+            fFitsTables[tableName]->SetRaw(it->key, it->value, it->comment);
         fFitsTables[tableName]->WriteTableHeader(tableName.Data());
         fTableHeaderWritten[tableName] = true;
@@ -1102,5 +1120,5 @@
    // get new filename
    const TString readFileName = read->GetFullFileName();
-   const TString newname = MWriteRootFile::SubstituteName(fRule, readFileName)+".fits";
+   const TString newname = MWriteRootFile::SubstituteName(fRule, readFileName);
 
    // create new files
Index: trunk/Mars/mfileio/MWriteFitsFile.h
===================================================================
--- trunk/Mars/mfileio/MWriteFitsFile.h	(revision 14935)
+++ trunk/Mars/mfileio/MWriteFitsFile.h	(revision 15277)
@@ -138,4 +138,72 @@
    Int_t       PostProcess();
 
+   //Header keys related stuff
+   template<typename _T>
+   std::string GetFitsString(const _T& value)
+   {
+       std::ostringstream returnVal;
+       std::string typeName = typeid(_T).name();
+       if (typeName == "i" || typeName == "j" || //int
+           typeName == "s" || typeName == "t" || //short
+           typeName == "l" || typeName == "m" || //long
+           typeName == "x" || typeName == "y")   //long long
+           returnVal << value;
+
+       if (typeName == "b")
+       {
+           if (value) returnVal << "T"; else returnVal << "F";
+       }
+       if (typeName == "Ss" || typeName == "PKc" ||
+           (typeName.size() >= 4 && typeName[0] == 'A' && typeName[typeName.size()-1] == 'c' && typeName[typeName.size()-2] == '_'))
+       {
+           returnVal << value;
+           std::string temp = returnVal.str();
+           returnVal.str("");
+           for (std::string::iterator c=temp.begin(); c<temp.end(); c++)
+               if (*c=='\'')
+                   temp.insert(c++, '\'');
+           returnVal << "'" << temp << "'";
+       }
+       if (returnVal.str() == "")
+           *fLog << warn << "WARNING: could not construct fits string from \"" << value << "\" typename: " << typeName << endl;
+       return returnVal.str();
+   }
+
+public:
+   template<typename _T>
+   bool SetHeaderKey(const std::string& key,
+                     const _T& value,
+                     const std::string& comment="")
+   {
+       //check if header was already written
+       for (std::map<TString, bool>::iterator it=fTableHeaderWritten.begin(); it!= fTableHeaderWritten.end(); it++)
+           if (it->second == true)
+               return false;
+       //headers were not written yet. Add one key
+       std::vector<ofits::Key>::iterator it = fHeaderKeys.begin();
+       //check if the value had been set beforel
+       for (;it!=fHeaderKeys.end();it++)
+       {
+           if (it->key == key)
+           {
+               it->value = GetFitsString(value);
+               it->comment = comment;
+               break;
+           }
+       }
+       //did we find the key ?
+       if (it == fHeaderKeys.end())
+       {//no ? add it !
+           ofits::Key temp;
+           temp.key = key;
+           temp.value = GetFitsString(value);
+           temp.comment = comment;
+           fHeaderKeys.push_back(temp);
+       }
+       return true;
+   }
+private:
+   std::vector<ofits::Key> fHeaderKeys;
+
    std::string Trim(const std::string &str);
 
@@ -145,4 +213,5 @@
    std::vector<std::string> fVetoedColumns;
    std::map<std::string, uint32_t> fBytesPerSamples;
+
 
 public:
@@ -186,5 +255,8 @@
    ClassDef(MWriteFitsFile, 0)   
 };
-
-
-#endif
+//Specializations for float and doubles (because of the precision handling, I could not deal with it in the main function
+template<>
+std::string MWriteFitsFile::GetFitsString(const double& value);
+template<>
+std::string MWriteFitsFile::GetFitsString(const float& value);
+#endif
