Index: trunk/FACT++/scripts/Main.js
===================================================================
--- trunk/FACT++/scripts/Main.js	(revision 16852)
+++ trunk/FACT++/scripts/Main.js	(revision 16853)
@@ -94,5 +94,5 @@
 function doDrsCalibration(where)
 {
-    console.out("  "+new Date().toUTCString()+": Starting DRS calibration ["+where+"]");
+    dim.log("Starting DRS calibration ["+where+"]");
 
     service_feedback.voltageOff();
@@ -134,7 +134,7 @@
 
     if (irq)
-        console.out("  "+new Date().toUTCString()+": DRS calibration interrupted [%.1fs]".$((new Date()-tm)/1000));
+        dim.log("DRS calibration interrupted [%.1fs]".$((new Date()-tm)/1000));
     else
-        console.out("  "+new Date().toUTCString()+": DRS calibration done [%.1fs]".$((new Date()-tm)/1000));
+        dim.log("DRS calibration done [%.1fs]".$((new Date()-tm)/1000));
 }
 
@@ -161,5 +161,5 @@
     if (isClosed)
     {
-        console.out("  "+new Date().toUTCString()+": Opening lid");
+        dim.log("Opening lid");
         dim.send("LID_CONTROL/OPEN");
     }
@@ -167,5 +167,5 @@
 
     if (isClosed)
-        console.out("  "+new Date().toUTCString()+": Lid open [%.1fs]".$((new Date()-tm)/1000));
+        dim.log("Lid open [%.1fs]".$((new Date()-tm)/1000));
 }
 
@@ -179,5 +179,5 @@
     if (isOpen)
     {
-        console.out("  "+new Date().toUTCString()+": Closing lid.");
+        dim.log("Closing lid.");
         dim.send("LID_CONTROL/CLOSE");
     }
@@ -185,5 +185,5 @@
 
     if (isOpen)
-        console.out("  "+new Date().toUTCString()+": Lid closed [%.1fs]".$((new Date()-tm)/1000));
+        dim.log("Lid closed [%.1fs]".$((new Date()-tm)/1000));
 }
 
@@ -242,5 +242,5 @@
     if (isOn)
     {
-        console.out("  "+new Date().toUTCString()+": Switching voltage off.");
+        dim.log("Switching voltage off.");
 
         // Supress the possibility that the bias control is
@@ -274,5 +274,5 @@
 
     if (isOn)
-        console.out("  "+new Date().toUTCString()+": Voltage off.");
+        dim.log("Voltage off.");
 }
 
@@ -306,5 +306,5 @@
     if (isOff)
     {
-        console.out("  "+new Date().toUTCString()+": Switching voltage on.");
+        dim.log("Switching voltage on.");
         //console.out(JSON.stringify(dim.state("BIAS_CONTROL")));
 
@@ -321,5 +321,5 @@
     if (isOff)
     {
-        console.out("  "+new Date().toUTCString()+": Voltage on [cnt="+this.cnt+"]");
+        dim.log("Voltage on, feedback will start ramping [cnt="+this.cnt+"]");
 
         this.previous = undefined;
@@ -347,12 +347,12 @@
         return;
 
-    // FIXME: timeout missing
-    console.out("  "+new Date().toUTCString()+": Waiting for voltage to be stable.");
-
+    dim.log("Waiting for voltage to be stable.");
+
+    // 0.022 corresponds to 1 DAC count (90V/4096)
     function func()
     {
         if (irq ||
             (this.cnt!=undefined && this.get().counter>this.cnt+10) ||
-            (this.voltageStep && this.voltageStep<0.02))
+            (this.voltageStep && this.voltageStep<0.022))
             return true;
     }
@@ -363,7 +363,7 @@
 
     if (irq)
-        console.out("  "+new Date().toUTCString()+": Waiting for stable voltage interrupted [dU=%.3fV, cnt=%d, %.2fs]".$(this.voltageStep, this.get().counter, (new Date()-now)/1000));
+        dim.log("Waiting for stable voltage interrupted [dU=%.3fV, cnt=%d, %.2fs]".$(this.voltageStep, this.get().counter, (new Date()-now)/1000));
     else
-        console.out("  "+new Date().toUTCString()+": Voltage stable within limits [dV=%.3f, cnt=%d, %.2fs]".$(this.voltageStep, this.get().counter, (new Date()-now)/1000));
+        dim.log("Voltage stable within limits [dV=%.3f, cnt=%d, %.2fs]".$(this.voltageStep, this.get().counter, (new Date()-now)/1000));
 }
 
@@ -374,8 +374,13 @@
 function Shutdown()
 {
-    console.out("  "+new Date().toUTCString()+": Starting shutdown.");
+    dim.log("Starting shutdown.");
+
+    var now1 = new Date();
 
     service_feedback.voltageOff();
     CloseLid(); 
+
+    var now2 = new Date();
+
     dim.send("DRIVE_CONTROL/PARK");
 
@@ -401,6 +406,7 @@
     }
 
-    var now = new Date();
     v8.timeout(150000, func);
+
+    var now3 = new Date();
 
     //dim.send("FEEDBACK/STOP");
@@ -427,5 +433,6 @@
     console.out("    BIAS_CONTROL:  "+dim.state("BIAS_CONTROL").name);
     console.out("");
-    console.out("Shutdown: end ["+(new Date()-now)/1000+"s]");
+    dim.log("Shutdown: end ["+(now2-now1)/1000+"s, "+(now3-now2)/1000+"s, "+(new Date()-now3)/1000+"s]");
+    console.out("");
 
     sub.close();
@@ -600,7 +607,7 @@
 
     if (valid)
-        console.out("  "+new Date().toUTCString()+": Last DRS calibration was %.1fmin ago".$(diff));
+        dim.log("Last DRS calibration was %.1fmin ago".$(diff));
     else
-        console.out("  "+new Date().toUTCString()+": No valid DRS calibration available.");
+        dim.log("No valid DRS calibration available.");
 
     return valid ? diff : null;
@@ -644,11 +651,11 @@
     var n = new Date();
     if (observations.length>0 && test==-1)
-        console.out("  "+n.toUTCString()+": First observation scheduled for "+observations[0].start.toUTCString());
+        dim.log("First observation scheduled for "+observations[0].start.toUTCString()+" [id="+observations[0].id+"]");
     if (test>=0 && test<observations.length)
-        console.out("  "+n.toUTCString()+": First observation should start immediately.");
+        dim.log("First observation should start immediately ["+observations[test].start.toUTCString()+", id="+observations[test].id+"]");
     if (observations.length>0 && observations[0].start>n+12*3600*1000)
-        console.out("  "+n.toUTCString()+": No observations scheduled for the next 12 hours!");
+        dim.log("No observations scheduled for the next 12 hours!");
     if (observations.length==0)
-        console.out("  "+n.toUTCString()+": No observations scheduled!");
+        dim.log("No observations scheduled!");
 }
 
@@ -656,9 +663,10 @@
 // Start main loop
 // ----------------------------------------------------------------
-console.out("  "+new Date().toUTCString()+": Entering main loop.");
+dim.log("Entering main loop.");
 
 var run = -2; // getObservation never called
 var sub;
 var lastId;
+var nextId;
 var sun = Sun.horizon(-12);
 var system_on;  // undefined
@@ -686,5 +694,6 @@
     if (!was_up && sun.isUp)
     {
-        console.out("", "  "+new Date().toUTCString()+": Sun rise detected.... automatic shutdown initiated!");
+        console.out("");
+        dim.log("Sun rise detected.... automatic shutdown initiated!");
         // FIXME: State check?
         Shutdown();
@@ -701,12 +710,5 @@
     {
         console.out("");
-        console.out("  "+new Date().toUTCString()+": Starting new observation [id="+obs.id+"]");
-
-        //console.out("--- "+obs.id+" ---");
-        //console.out("Current time:        "+new Date().toUTCString());
-        //console.out("Current observation: "+obs.start.toUTCString());
-        if (nextObs!=undefined)
-            console.out("  "+new Date().toUTCString()+": Next observation scheduled for "+nextObs.start.toUTCString());
-        console.out("");
+        dim.log("Starting new observation ["+obs.start.toUTCString()+", id="+obs.id+"]");
 
         // This is the first source, but we do not come from
@@ -727,6 +729,20 @@
 
         run = 0;
-    }
-    lastId = obs.id;
+        lastId = obs.id;
+    }
+
+    if (nextObs && nextId!=nextObs.id)
+    {
+        dim.log("Next observation scheduled for "+nextObs.start.toUTCString()+" [id="+nextObs.id+"]");
+        console.out("");
+        nextId = nextObs.id;
+    }
+
+    if (!nextObs && nextId)
+    {
+        dim.log("No further observation scheduled.");
+        console.out("");
+        nextId = undefined;
+    }
 
     //if (nextObs==undefined && obs[obs.length-1].task!="SHUTDOWN")
@@ -758,6 +774,6 @@
 
 
-    if (obs[sub].task!="IDLE" && obs[sub].task!="DATA")
-        console.out("  "+new Date().toUTCString()+": New task ["+obs[sub]+"]");
+    if (obs[sub].task!="IDLE" && (obs[sub].task!="DATA" && run>0))
+        dim.log("New task ["+obs[sub]+"]");
 
     // FIXME: Maybe print a warning if Drive is on during day time!
@@ -813,5 +829,4 @@
 
     case "STARTUP":
-        //console.out("", "  "+new Date().toUTCString()+": New task [STARTUP]");
         CloseLid();
 
@@ -825,5 +840,5 @@
 
         // Before we can switch to 3000 we have to make the right DRS calibration
-        console.out("  "+new Date().toUTCString()+": Taking single p.e. run.");
+        dim.log("Taking single p.e. run.");
         while (!irq && !takeRun("pedestal", 5000));
 
@@ -831,9 +846,9 @@
         service_feedback.voltageOff();
         system_on = true;
-        console.out("  "+new Date().toUTCString()+": Task finished [STARTUP]", "");
+        dim.log("Task finished [STARTUP]");
+        console.out("");
         break;
 
     case "SHUTDOWN":
-        //console.out("  "+new Date().toUTCString()+": New task [SHUTDOWN]", "");
         Shutdown();
         system_on = false;
@@ -841,18 +856,17 @@
         // FIXME: Avoid new observations after a shutdown until
         //        the next startup (set run back to -2?)
-        console.out("  Waiting for next startup.", "");
         sub++;
-        console.out("  "+new Date().toUTCString()+": Task finished [SHUTDOWN]", "");
+        dim.log("Task finished [SHUTDOWN]");
+        console.out("");
+        //console.out("  Waiting for next startup.", "");
         continue;
 
     case "DRSCALIB":
-        //console.out("  "+new Date().toUTCString()+": New task [DRSCALIB]", "");
         doDrsCalibration("drscalib");  // will switch the voltage off
-        console.out("  "+new Date().toUTCString()+": Task finished [DRSCALIB]", "");
+        dim.log("Task finished [DRSCALIB]");
+        console.out("");
         break;
 
     case "SINGLEPE":
-        //console.out("  "+new Date().toUTCString()+": New task [SINGLE-PE]", "");
-
         // The lid must be closes
         CloseLid();
@@ -872,15 +886,14 @@
 
         // Before we can switch to 3000 we have to make the right DRS calibration
-        console.out("  "+new Date().toUTCString()+": Taking single p.e. run.");
+        dim.log("Taking single p.e. run.");
         while (!irq && !takeRun("pedestal", 5000));
 
         // It is unclear what comes next, so we better switch off the voltage
         service_feedback.voltageOff();
-        console.out("  "+new Date().toUTCString()+": Task finished [SINGLE-PE]", "");
+        dim.log("Task finished [SINGLE-PE]");
+        console.out("");
         break;
 
     case "RATESCAN":
-        //console.out("  "+new Date().toUTCString()+": New task [RATESCAN]", "");
-
         var tm1 = new Date();
 
@@ -898,10 +911,10 @@
         if (obs.source != undefined)
         {
-            console.out("  "+new Date().toUTCString()+": Moving drive to '"+obs[cub].source+"'.");
+            dim.log("Pointing telescope to '"+obs[cub].source+"'.");
             dim.send("DRIVE_CONTROL/TRACK_ON", obs[sub].source);
         }
         else
         {
-            console.out("  "+new Date().toUTCString()+": Moving drive to: ra="+obs[sub].ra+" dec="+obs[sub].dec);
+            dim.log("Pointing telescope to ra="+obs[sub].ra+" dec="+obs[sub].dec);
             dim.send("DRIVE_CONTROL/TRACK", obs[sub].ra, obs[sub].dec);
         }
@@ -913,5 +926,5 @@
         var tm2 = new Date();
 
-        console.out("  "+new Date().toUTCString()+": Starting ratescan.");
+        dim.log("Starting ratescan.");
 
         // Start rate scan
@@ -929,6 +942,7 @@
         //dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
 
-        console.out("  "+new Date().toUTCString()+": Ratescan done [%.1fs, %.1fs]".$((tm2-tm1)/1000, (new Date()-tm2)/1000));
-        console.out("  "+new Date().toUTCString()+": Task finished [RATESCAN]", "");
+        dim.log("Ratescan done [%.1fs, %.1fs]".$((tm2-tm1)/1000, (new Date()-tm2)/1000));
+        dim.log("Task finished [RATESCAN]");
+        console.out("");
         break; // case "RATESCAN"
 
@@ -950,5 +964,5 @@
         // ------------------------------------------------------------
 
-        console.out("  "+new Date().toUTCString()+": Run count "+run+" [remaining "+parseInt(remaining)+"min]");
+        dim.log("Run count "+run+" [remaining "+parseInt(remaining)+"min]");
 
         // ----- Time since last DRS Calibration [min] ------
@@ -976,5 +990,5 @@
 
             //console.out("  Move telescope to '"+source+"' "+offset+" "+wobble);
-            console.out("  "+new Date().toUTCString()+": Moving telescope to '"+obs[sub].source+"' [wobble="+wobble+"]");
+            dim.log("Pointing telescope to '"+obs[sub].source+"' [wobble="+wobble+"]");
 
             // This is a workaround to make sure that we really catch
@@ -998,5 +1012,6 @@
             if (!was_up && sun.isUp)
             {
-                console.out("", "  "+new Date().toUTCString()+": Sun rise detected.... automatic shutdown initiated!");
+                console.out("");
+                dim.log("Sun rise detected.... automatic shutdown initiated!");
                 // FIXME: State check?
                 Shutdown();
@@ -1027,5 +1042,5 @@
         if (point)
         {
-            console.out("  "+new Date().toUTCString()+": Starting calibration.");
+            dim.log("Starting calibration.");
 
             // Calibration (2% of 20')
@@ -1045,12 +1060,22 @@
         // e.g. drive went to ERROR? Maybe we have to check all states again?
 
-        var len = 300;
-        while (len>15)
+        var twilight = Sun(-16).isUp;
+
+        if (twilight)
         {
-            var time = new Date();
-            if (takeRun("data", -1, len)) // Take data (5min)
-                break;
-
-            len -= parseInt((new Date()-time)/1000);
+            for (int i=0; i<5 && !irq; i++)
+                takeRun("data", -1, 60); // Take data (1min)
+        }
+        else
+        {
+            var len = 300;
+            while (len>15)
+            {
+                var time = new Date();
+                if (takeRun("data", -1, len)) // Take data (5min)
+                    break;
+
+                len -= parseInt((new Date()-time)/1000);
+            }
         }
 
@@ -1062,5 +1087,8 @@
 
     if (nextObs!=undefined && sub==obs.length-1)
-        console.out("  "+new Date().toUTCString()+": Next observation will start at "+nextObs.start.toUTCString(),"");
+    {
+        dim.log("Next observation will start at "+nextObs.start.toUTCString()+" [id="+nextObs.id+"]");
+        console.out("");
+    }
 
     sub++;
@@ -1069,5 +1097,5 @@
 sub_drsruns.close();
 
-console.out("  "+new Date().toUTCString()+": Left main loop [irq="+irq+"]");
+dim.log("Left main loop [irq="+irq+"]");
 
 if (irq.toUpperCase()=="SHUTDOWN")
