Index: /trunk/FACT++/src/Fits.cc
===================================================================
--- /trunk/FACT++/src/Fits.cc	(revision 10724)
+++ /trunk/FACT++/src/Fits.cc	(revision 10725)
@@ -80,11 +80,13 @@
 //! @param tableName the name of the table that will receive the logged data.
 //! @param file a pointer to an existing FITS file. If NULL, file will be opened and managed internally
-//
-void Fits::Open(const std::string& fileName, const std::string& tableName, FITS* file, int* fitsCounter, std::ostream& out)
+//! @param fitsCounter a pointer to the integer keeping track of the opened FITS files
+//! @param out a pointer to the MessageImp that should be used to log errors
+//
+void Fits::Open(const std::string& fileName, const std::string& tableName, FITS* file, int* fitsCounter, MessageImp* out)//std::ostream& out)
 {		
-	if (fMess)
-		delete fMess;
-	fMess = new MessageImp(out);
-	
+//	if (fMess)
+//		delete fMess;
+//	fMess = new MessageImp(out);
+	fMess = out;
 	fFileName = fileName;
 	if (file == NULL)
@@ -183,7 +185,22 @@
 	}
 			
-	fRefMjD = * static_cast<double*>(fStandardPointers[0]);
+	//As requested by Roland, the reference MjD is 0.0
+	fRefMjD = 0;//* static_cast<double*>(fStandardPointers[0]);
 	if (!updating)
 		WriteHeaderKeys();
+}
+template <typename T>
+void Fits::WriteSingleHeaderKey(string name, T value, string comment)
+{
+	try
+	{
+		fTable->addKey(name, value, comment);
+	}
+	catch (CCfits::FitsError e)
+	{
+		std::stringstream str;
+		str << "Could not add header keys in file " << fFileName << " reason: " << e.message();
+		fMess->Error(str);
+	}
 }
 // --------------------------------------------------------------------------
@@ -198,67 +215,19 @@
 	std::string comment;
 	
-	float floatValue;
-	double doubleValue;
+//	float floatValue;
+//	double doubleValue;
 	std::string stringValue;
-	try
-	{	
-		name = "EXTREL";
-		comment = "Release Number";
-		floatValue = 1.0f;
-		fTable->addKey(name, floatValue, comment);
-				
-		name = "BASETYPE";
-		comment = "Base type of the data structure";
-		stringValue = "??????";
-		fTable->addKey(name, stringValue, comment);
-				
-		name = "TELESCOP";
-		comment = "Telescope that acquired this data";
-		stringValue = "FACT";
-		fTable->addKey(name, stringValue, comment);
-				
-		name = "ORIGIN";
-		comment = "Institution that wrote the file";
-		stringValue = "ISDC";
-		fTable->addKey(name, stringValue, comment);
-				
-		name = "CREATOR";
-		comment = "Program that wrote this file";
-		stringValue = "FACT++_DataLogger";
-		fTable->addKey(name, stringValue, comment);
-				
-		name = "DATE";
-		stringValue = Time().GetAsStr();
-		stringValue[10] = 'T';
-		comment = "File creation date";
-		fTable->addKey(name, stringValue, comment);
-		
-		name = "TIMESYS";
-		stringValue = "TT";
-		comment = "Time frame system";
-		fTable->addKey(name, stringValue, comment);
-		
-		name = "TIMEUNIT";
-		stringValue = "d";
-		comment = "Time unit";
-		fTable->addKey(name, stringValue, comment);
-		
-		name = "TIMEREF";
-		stringValue = "UTC";
-		comment = "Time reference frame";
-		fTable->addKey(name, stringValue, comment);
-			
-		name = "MJDREF";
-		doubleValue = fRefMjD;
-		comment = "Modified Julian Date of origin";
-		fTable->addKey(name, doubleValue, comment);
-	}
-	catch (CCfits::FitsError e)
-	{
-		std::stringstream str;
-		str << "Could not add header keys in file " << fFileName << " reason: " << e.message();
-		fMess->Error(str);
-	}			
-	//More ?
+
+	WriteSingleHeaderKey("EXTREL", 1.0f, "Release Number");
+	WriteSingleHeaderKey("TELESCOP", "FACT", "Telescope that acquired this data");
+	WriteSingleHeaderKey("ORIGIN", "ISDC", "Institution that wrote the file");
+	WriteSingleHeaderKey("CREATOR", "FACT++ DataLogger", "Program that wrote this file");
+	stringValue = Time().GetAsStr();
+	stringValue[10]= 'T';
+	WriteSingleHeaderKey("DATE", stringValue, "File creation data");
+	WriteSingleHeaderKey("TIMESYS", "TT", "Time frame system");
+	WriteSingleHeaderKey("TIMEUNIT", "d", "Time unit");
+	WriteSingleHeaderKey("TIMEREF", "UTC", "Time reference frame");
+	WriteSingleHeaderKey("MJDREF", fRefMjD, "Modified Julian Date of origin");
 }
 // --------------------------------------------------------------------------
@@ -270,24 +239,29 @@
 {
 
-	try
-	{
-		fTable->makeThisCurrent();
-		fTable->insertRows(fNumRows);
-	}
-	catch(CCfits::FitsError e)
-	{
-		std::stringstream str;
-		str << "Could not insert row in file " << fFileName << " reason: " << e.message();
+//	try
+//	{
+	fTable->makeThisCurrent();
+	int status(0);
+	if (fits_insert_rows(fTable->fitsPointer(), fNumRows, 1, &status))
+	{
+		std::stringstream str;
+		str << "Could not insert row in file " << fFileName << ". cfitsio error code: " << status;
 		fMess->Error(str);
 	}
+//		fTable->insertRows(fNumRows);
+//	}
+//	catch(CCfits::FitsError e)
+//	{
+//		std::stringstream str;
+//		str << "Could not insert row in file " << fFileName << " reason: " << e.message();
+//		fMess->Error(str);
+//	}
 	fNumRows++;
-			
+
 	//the first standard variable is the current MjD
 	if (fEndMjD == 0)
 	{
-		std::string name = "TSTART";
-		double doubleValue = *static_cast<double*>(fStandardPointers[0]) - fRefMjD;
-		std::string comment = "Time of the first received data";
-		fTable->addKey(name, doubleValue, comment);	
+		double doubleValue = *static_cast<double*>(fStandardPointers[0]);// - fRefMjD;
+		WriteSingleHeaderKey("TSTART", doubleValue, "Time of the first received data");
 	}
 	fEndMjD = * static_cast<double*>(fStandardPointers[0]);
@@ -298,6 +272,9 @@
 	for (unsigned int i=0;i<fStandardNumBytes.size();i++)
 	{
-		for (int j=0; j<fStandardNumBytes[i]; j++)
-			fCopyBuffer[shift+j] = static_cast<char*>(fStandardPointers[i])[fStandardNumBytes[i]-(j+1)];
+		const char * charSrc = static_cast<char*>(fStandardPointers[i]);
+
+		reverse_copy(charSrc, charSrc+fStandardNumBytes[i], &fCopyBuffer[shift]);
+//		for (int j=0; j<fStandardNumBytes[i]; j++)
+//			fCopyBuffer[shift+j] = static_cast<char*>(fStandardPointers[i])[fStandardNumBytes[i]-(j+1)];
 		shift+= fStandardNumBytes[i];	
 	}
@@ -307,5 +284,5 @@
 			 
 	//data copied to buffer, can write to fits
-	int status = 0;
+//	int status = 0;
 	//TODO check the status after the write operation
    	fits_write_tblbytes(fFile->fitsPointer(), fNumRows, 1, fTotalNumBytes, fCopyBuffer, &status);
@@ -320,5 +297,5 @@
 	//This forces the writting of each row to the disk. Otherwise all rows are written when the file is closed.
 	///TODO check whether this consumes too much resources or not. If it does, flush every N write operations instead
-	try
+/*	try
 	{
 		fFile->flush();
@@ -330,5 +307,5 @@
 		fMess->Error(str);	
 	}
-}
+*/}
 // --------------------------------------------------------------------------
 //
@@ -341,9 +318,6 @@
 //			if (fTable != NULL)
 //				delete fTable;
-	std::string name = "TEND";
-	double doubleValue = fEndMjD - fRefMjD;
-	std::string comment = "Time of the last received data";
-	fTable->addKey(name, doubleValue, comment);
-	
+
+	WriteSingleHeaderKey("TSTOP", fEndMjD, "Time of the last receied data");
 	if (fFile != NULL && fOwner)
 		delete fFile;
@@ -354,6 +328,7 @@
 	if (fOwner && fNumOpenFitsFiles != NULL)
 		(*fNumOpenFitsFiles)--;
-	if (fMess)
-		delete fMess;
+//fMess is the MessageImp part of the dataLogger itself. Thus it should NOT be deleted by the Fits files destructor.
+//	if (fMess)
+//		delete fMess;
 	fMess = NULL;
 }
Index: /trunk/FACT++/src/Fits.h
===================================================================
--- /trunk/FACT++/src/Fits.h	(revision 10724)
+++ /trunk/FACT++/src/Fits.h	(revision 10725)
@@ -89,5 +89,5 @@
 
 		///Opens a FITS file
-                void Open(const std::string& fileName, const std::string& tableName, CCfits::FITS* file, int* fitsCounter, std::ostream& out);
+        void Open(const std::string& fileName, const std::string& tableName, CCfits::FITS* file, int* fitsCounter, MessageImp* out);//std::ostream& out);
 
 		///Write one line of data. Use the given converter.
@@ -99,4 +99,7 @@
 		///Get the size currently written on the disk
 		int GetWrittenSize();
+private:
+		template <typename T>
+		void WriteSingleHeaderKey(string name, T value, string comment);
 
 };//Fits
Index: /trunk/FACT++/src/dataLogger.cc
===================================================================
--- /trunk/FACT++/src/dataLogger.cc	(revision 10724)
+++ /trunk/FACT++/src/dataLogger.cc	(revision 10725)
@@ -66,4 +66,5 @@
 #include <sys/stat.h>
 
+//#define HAVE_FITS
 //#define ONE_RUN_FITS_ONLY
 
@@ -977,4 +978,6 @@
 //! @param I
 //! 	The current DimInfo 
+//! @param sub
+//!		The dataLogger's subscription corresponding to this DimInfo
 //
 void DataLogger::ReportPlease(DimInfo* I, SubscriptionType& sub)
@@ -1088,5 +1091,5 @@
 		std::string n = I->getName();
 		std::stringstream msg;
-		msg << n.substr(0, n.find_first_of('/')) << ": " << I->getString();
+		msg << n << ": " << I->getString();//n.substr(0, n.find_first_of('/')) << ": " << I->getString();
 
 		if (fNightlyLogFile.is_open())
@@ -1699,5 +1702,5 @@
 			fFileSizesMap[partialName] = 0;
 		}
-		sub.nightlyFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, Out());
+		sub.nightlyFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this);//Out());
 		//notify the opening
 		std::string actualTargetDir;
@@ -1771,7 +1774,7 @@
 #ifdef ONE_RUN_FITS_ONLY
 		}
-		sub.runFile.Open(partialName, serviceName, fRunFitsFile, &fNumSubAndFitsData.numOpenFits, Out());
+		sub.runFile.Open(partialName, serviceName, fRunFitsFile, &fNumSubAndFitsData.numOpenFits, this);//Out());
 #else
-		sub.runFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, Out());
+		sub.runFile.Open(partialName, serviceName, NULL, &fNumSubAndFitsData.numOpenFits, this);//Out());
 #endif //one_run_fits_only
 	   if (fNumSubAndFitsIsOn)
