Index: trunk/FACT++/src/datalogger.cc
===================================================================
--- trunk/FACT++/src/datalogger.cc	(revision 15150)
+++ trunk/FACT++/src/datalogger.cc	(revision 15153)
@@ -85,4 +85,7 @@
 
 #include "DimState.h"
+
+#include <libnova/solar.h>
+#include <libnova/rise_set.h>
 
 //Dim structures
@@ -239,4 +242,6 @@
     //overloading of DIM's infoHandler function
     int infoCallback(const EventImp& evt, unsigned int infoIndex);
+
+    Time GetSunRise(const Time &time=Time());
 
     /***************************************************
@@ -386,5 +391,5 @@
 
     //Current day variable. Used to close nightly files when night changes
-    int fCurrentDay;
+    Time fCurrentDay;
     Time lastFlush;
 
@@ -849,5 +854,6 @@
 
     //calculate time suitable for naming files.
-    const Time ftime = time-boost::posix_time::hours(12);
+    //fCurrentDay is 30 minutes after upcoming sunrise. So just use 12 hours before then
+    const Time ftime = fCurrentDay-boost::posix_time::hours(12);
 
     //output it
@@ -858,5 +864,5 @@
         CreateDirectory(str.str());
 
-    str << '/' << CompileFileName(service, extension, time);
+    str << '/' << CompileFileName(service, extension, fCurrentDay);
 
     return str.str();
@@ -916,6 +922,9 @@
     //calculate time "centered" around noon instead of midnight
     const Time timeNow;
-    const Time nowMinusTwelve = timeNow-boost::posix_time::hours(12);
-    fCurrentDay = (int)(nowMinusTwelve.Mjd());//nowMinusTwelve.M()*31 + nowMinusTwelve.D();//assume 31 days per month. we do not really care, only want unique number per day of the year
+//    const Time nowMinusTwelve = timeNow-boost::posix_time::hours(12);
+    //the "current day" is actually the next closing time of nightly files
+    //the next closing time is 30 minutes after upcoming sunrise.
+    //If we are within 30 minutes after sunrise, closing time is soon
+    fCurrentDay = GetSunRise(Time()-boost::posix_time::minutes(30)) + boost::posix_time::minutes(30);//(int)(nowMinusTwelve.Mjd());//nowMinusTwelve.M()*31 + nowMinusTwelve.D();//assume 31 days per month. we do not really care, only want unique number per day of the year
     lastFlush = Time();
 
@@ -1255,4 +1264,37 @@
     AddNewRunNumber(evt.GetXtra(), evt.GetTime());
 }
+// --------------------------------------------------------------------------
+//
+//! Get SunRise. Copied from drivectrl.cc
+//! Used to know when to close and reopen files
+//!
+Time DataLogger::GetSunRise(const Time &time)
+{
+    const double lon = -(17.+53./60+26.525/3600);
+    const double lat =   28.+45./60+42.462/3600;
+
+    ln_lnlat_posn observer;
+    observer.lng = lon;
+    observer.lat = lat;
+
+    // This caluclates the sun-rise of the next day after 12:00 noon
+    ln_rst_time sun_day;
+    if (ln_get_solar_rst(time.JD(), &observer, &sun_day)==1)
+    {
+        Fatal("GetSunRise reported the sun to be circumpolar!");
+        return Time(Time::none);
+    }
+
+    if (Time(sun_day.rise)>=time)
+        return Time(sun_day.rise);
+
+    if (ln_get_solar_rst(time.JD()+0.5, &observer, &sun_day)==1)
+    {
+        Fatal("GetSunRise reported the sun to be circumpolar!");
+        return Time(Time::none);
+    }
+
+    return Time(sun_day.rise);
+}
 
 // --------------------------------------------------------------------------
@@ -1285,6 +1327,6 @@
     // if number of days has changed, then files should be closed and reopenned.
     const Time timeNow;
-    const Time nowMinusTwelve = timeNow-boost::posix_time::hours(12);
-    int newDayNumber = (int)(nowMinusTwelve.Mjd());//nowMinusTwelve.M()*31 + nowMinusTwelve.D();//assume 31 days per month. we do not really care, only want unique number per day of the year
+//    const Time nowMinusTwelve = timeNow-boost::posix_time::hours(12);
+//    int newDayNumber = (int)(nowMinusTwelve.Mjd());
 
     //also check if we should flush the nightly files
@@ -1305,8 +1347,9 @@
             Debug("Just flushed nightly fits files to the disk");
     }
-
-    if (newDayNumber !=  fCurrentDay)
-    {
-        fCurrentDay = newDayNumber;
+    //check if we should close and reopen the nightly files
+    if (timeNow > fCurrentDay)//GetSunRise(fCurrentDay)+boost::posix_time::minutes(30)) //if we went past 30 minutes after sunrise
+    {
+        //set the next closing time. If we are here, we have passed 30 minutes after sunrise.
+        fCurrentDay = GetSunRise(timeNow-boost::posix_time::minutes(30))+boost::posix_time::minutes(30);
         //crawl through the subcriptions and close any open nightly file
         SubscriptionsListType::iterator x;
@@ -1374,6 +1417,7 @@
     if (isItaReport)
     {
+//DISABLED REPORT WRITING BY THOMAS REQUEST
         //write text header
-        string serviceName = (sub.service == "MESSAGE") ? "" : "_"+sub.service;
+/*        string serviceName = (sub.service == "MESSAGE") ? "" : "_"+sub.service;
         header << sub.server << serviceName << " " << fQuality << " ";
         header << evt.GetTime() << " ";
@@ -1414,4 +1458,5 @@
                 return;
         }
+*/
 #ifdef HAVE_FITS
         //check if the last received event was before noon and if current one is after noon.
