Index: /trunk/FACT++/src/fixfits.cc
===================================================================
--- /trunk/FACT++/src/fixfits.cc	(revision 20037)
+++ /trunk/FACT++/src/fixfits.cc	(revision 20038)
@@ -16,5 +16,5 @@
 #include <boost/filesystem.hpp>
 
-#include "FITS.h"
+#include "factfits.h"
 #include "Time.h"
 #include "Configuration.h"
@@ -342,4 +342,5 @@
     const uint64_t theap    = output_catalog_end - output_catalog_start;
 
+    map<string, size_t> header;
 
     if (!dry_run)
@@ -374,4 +375,6 @@
                 if (!rc)
                     return 5;
+
+                header[string(buffer, 8)] = size_t(output.tellp())+10;
             }
 
@@ -472,8 +475,77 @@
     cout << "\nExecution [write] : " << time_write << "s, " << fs::file_size(output_name)/1024./1024/time_write << "Mb/s";
 
-    cout << "\n\nYou should now:\n";
-    cout << "- update TSTOPI and TSTOPF using \"fitsdump -c UnixTimeUTC --minmax --nozero\" and then fv\n";
-    cout << "- update DATE-END using TSTOP given to the MjDtoISO program in ~fact_arc/ingestScripts and then fv again\n";
-    cout << "- update the checksum of the file with \"fchecksum update+ <filename>\"\n" << endl;
+    if (0)
+        return 0;
+
+    factfits fits(output_name);
+
+    cout << "\nNew file contains " << fits.GetNumRows() << " events.";
+    cout << "\nChecking for raw data contents." << endl;
+
+    if (fits.GetStr("TELESCOP")!="FACT" || fits.GetStr("ORIGIN")!="FACT" || fits.GetStr("PACKAGE")!="FACT++" || fits.GetStr("EXTNAME")!="Events" || !fits.HasKey("DRSCALIB"))
+    {
+        cerr << "Header incomplete." << endl;
+        return 0;
+    }
+
+    if (!fits.HasColumn("UnixTimeUTC"))
+    {
+        cerr << "Column UnixTimeUTC missing." << endl;
+        return 0;
+    }
+
+    cout << "\nRaw data file detected.";
+    cout << "\nUpdating run-end with last event time." << endl;
+
+    vector<uint32_t> utime(2);
+    fits.SetVecAddress("UnixTimeUTC", utime);
+    fits.GetRow(fits.GetNumRows()-1);
+
+    const Time stop(utime[0], utime[1]);
+
+    const uint32_t tstopi = floor(stop.UnixDate());
+    const double   tstopf = fmod(stop.UnixDate(), 1);
+
+    fits.close();
+
+    cout << setfill(' ');
+    cout << "\nTSTOPI  = " << right << setw(20) << tstopi << " / Time when last event received (integral part)";
+    cout << "\nTSTOPF  = " << right << setw(20) << tstopf << " / Time when last event received (fractional part)";
+    cout << "\nDATE-END= '" << stop.Iso() << "' / Time when last event received";
+
+    fstream fout(output_name);//, ios::binary);
+    fout << setfill(' ');
+
+    fout.seekp(header["DATE-END"], ios::beg);
+    //fout.seekg(header["DATE-END"], ios::beg);
+    fout << left << setw(20) << string("'" + stop.Iso() + "'") << " / Time when last event received";
+
+    fout.seekp(header["TSTOPI  "], ios::beg);
+    //fout.seekg(header["TSTOPI  "], ios::beg);
+    fout << right << setw(20) << tstopi;
+
+    fout.seekp(header["TSTOPF  "], ios::beg);
+    //fout.seekg(header["TSTOPF  "], ios::beg);
+    fout << right << setw(20) << tstopf;
+    fout.flush();
+
+    cout << "\nUpdating checksum." << endl;
+
+    factfits fits2(output_name);
+    fout.seekp(header["CHECKSUM"], ios::beg);
+    //fout.seekg(header["CHECKSUM"], ios::beg);
+    fout << left << setw(20) << string("'" + fits2.GetChecksum().str() +  "'");
+
+    fout.close();
+    fits2.close();
+
+    factfits fits3(output_name);
+    if (!fits3.IsHeaderOk() || !fits3.IsFileOk())
+    {
+        cerr << "\nFAILED: The checksum is still invalid! :(" << endl;
+        return 7;
+    }
+
+    cout << "\nFile header successfully updated!\n" << endl;
 
     return 0;
