Index: trunk/FACT++/scripts/Main.js
===================================================================
--- trunk/FACT++/scripts/Main.js	(revision 16797)
+++ trunk/FACT++/scripts/Main.js	(revision 16798)
@@ -92,5 +92,5 @@
 function doDrsCalibration(where)
 {
-    console.out("  Take DRS calibration ["+where+"]");
+    console.out("  "+new Date().toUTCString()+": Starting DRS calibration ["+where+"]");
 
     service_feedback.voltageOff();
@@ -132,5 +132,5 @@
         //                                       4'40 / 2'00
 
-    console.out("  DRS calibration done [%.1f]".$((new Date()-tm)/1000));
+    console.out("  "+new Date().toUTCString()+": DRS calibration done [%.1f]".$((new Date()-tm)/1000));
 }
 
@@ -157,5 +157,5 @@
     if (isClosed)
     {
-        console.out("  Open lid: start");
+        console.out("  "+new Date().toUTCString()+": Opening lid");
         dim.send("LID_CONTROL/OPEN");
     }
@@ -163,5 +163,5 @@
 
     if (isClosed)
-        console.out("  Open lid: done [%.1fs]".$((new Date()-tm)/1000));
+        console.out("  "+new Date().toUTCString()+": Lid open [%.1fs]".$((new Date()-tm)/1000));
 }
 
@@ -175,5 +175,5 @@
     if (isOpen)
     {
-        console.out("  Close lid: start");
+        console.out("  "+new Date().toUTCString()+": Closing lid.");
         dim.send("LID_CONTROL/CLOSE");
     }
@@ -181,5 +181,5 @@
 
     if (isOpen)
-        console.out("  Close lid: end [%.1fs]".$((new Date()-tm)/1000));
+        console.out("  "+new Date().toUTCString()+": Lid closed [%.1fs]".$((new Date()-tm)/1000));
 }
 
@@ -238,5 +238,5 @@
     if (isOn)
     {
-        console.out("  Voltage off: start");
+        console.out("  "+new Date().toUTCString()+": Switching voltage off.");
 
         // Supress the possibility that the bias control is
@@ -246,5 +246,5 @@
         if (isControl)
         {
-            console.out("  Suspending feedback.");
+            //console.out("  Suspending feedback.");
             dim.send("FEEDBACK/ENABLE_OUTPUT", false);
             dim.wait("FEEDBACK", "CurrentCtrlIdle", 3000);
@@ -252,5 +252,5 @@
 
         // Switch voltage off
-        console.out("  "+new Date().toUTCString()+": Voltage off: switch off");
+        //console.out("  "+new Date().toUTCString()+": Voltage off: switch off");
         dim.send("BIAS_CONTROL/SET_ZERO_VOLTAGE");
 
@@ -258,5 +258,5 @@
         if (isControl)
         {
-            console.out("  Resuming feedback.");
+            //console.out("  Resuming feedback.");
             dim.send("FEEDBACK/ENABLE_OUTPUT", true);
             dim.wait("FEEDBACK", "CurrentControl", 3000);
@@ -270,5 +270,5 @@
 
     if (isOn)
-        console.out("  Voltage off: end");
+        console.out("  "+new Date().toUTCString()+": Voltage off.");
 }
 
@@ -302,5 +302,5 @@
     if (isOff)
     {
-        console.out("  "+new Date().toUTCString()+": Voltage on: switch on");
+        console.out("  "+new Date().toUTCString()+": Switching voltage on.");
         //console.out(JSON.stringify(dim.state("BIAS_CONTROL")));
 
@@ -317,5 +317,5 @@
     if (isOff)
     {
-        console.out("  Voltage on: cnt="+this.cnt);
+        console.out("  "+new Date().toUTCString()+": Voltage on [cnt="+this.cnt+"]");
 
         this.previous = undefined;
@@ -344,5 +344,5 @@
 
     // FIXME: timeout missing
-    console.out("  Feedback wait: start");
+    console.out("  "+new Date().toUTCString()+": Waiting for voltage to be stable.");
 
     function func()
@@ -357,5 +357,5 @@
     v8.timeout(4*60000, func, this);
 
-    console.out("  Feedback wait: end [dV=%.3f, cnt=%d, %.2fs]".$(this.voltageStep, this.get().counter, (new Date()-now)/1000));
+    console.out("  "+new Date().toUTCString()+": Voltage stable within limits [dV=%.3f, cnt=%d, %.2fs]".$(this.voltageStep, this.get().counter, (new Date()-now)/1000));
 }
 
@@ -366,5 +366,5 @@
 function Shutdown()
 {
-    console.out("Shutdown: start");
+    console.out("  "+new Date().toUTCString()+": Starting shutdown.");
 
     service_feedback.voltageOff();
@@ -372,5 +372,5 @@
     dim.send("DRIVE_CONTROL/PARK");
 
-    console.out("Waiting for telescope to park. This may take a while.");
+    console.out("","Waiting for telescope to park. This may take a while.");
 
     // FIXME: This might not work is the drive is already close to park position
@@ -401,5 +401,5 @@
     dim.send("BIAS_CONTROL/DISCONNECT");
 
-    dim.wait("FTM_CONTROL",  "Idle",         3000);
+    dim.wait("FTM_CONTROL",  "Valid",        3000);
     dim.wait("BIAS_CONTROL", "Disconnected", 3000);
     dim.wait("FEEDBACK",     "Connected",    3000);
@@ -463,5 +463,4 @@
          "FAD_CONTROL/START_RUN",
          "FAD_CONTROL/STATISTICS1",
- //        "FAD_CONTROL/STATISTICS2",
          "FAD_CONTROL/STATS",
          "FAD_CONTROL/STATUS",
@@ -531,4 +530,15 @@
 
 // ================================================================
+//  Code to monitor clock conditioner
+// ================================================================
+
+var sub_counter = new Subscription("FTM_CONTROL/COUNTER");
+sub_counter.onchange = function(evt)
+{
+    if (evt.qos>0 && evt.qos!=2 && evt.qos&0x100==0)
+        throw new Error("FTM reports: clock conditioner not locked.");
+}
+
+// ================================================================
 //  Code related to monitoring the fad system
 // ================================================================
@@ -547,12 +557,12 @@
 // (FIXME: Should that go to the general CheckState?)
 // ----------------------------------------------------------------
-console.out("Checking send.");
+//console.out("Checking send.");
 checkSend(["MCP", "DRIVE_CONTROL", "LID_CONTROL", "FAD_CONTROL", "FEEDBACK"]);
-console.out("Checking send: done");
+//console.out("Checking send: done");
 
 // ----------------------------------------------------------------
 // Bring feedback into the correct operational state
 // ----------------------------------------------------------------
-console.out("Feedback init: start.");
+//console.out("Feedback init: start.");
 service_feedback.get(5000);
 
@@ -565,5 +575,5 @@
 // Connect to the DRS_RUNS service
 // ----------------------------------------------------------------
-console.out("Drs runs init: start.");
+//console.out("Drs runs init: start.");
 
 var sub_drsruns = new Subscription("FAD_CONTROL/DRS_RUNS");
@@ -582,7 +592,7 @@
 
     if (valid)
-        console.out("  Last DRS calib: %.1fmin ago".$(diff));
+        console.out("  "+new Date().toUTCString()+": Last DRS calib: %.1fmin ago".$(diff));
     else
-        console.out("  No valid drs calibration available");
+        console.out("  "+new Date().toUTCString()+": No valid drs calibration available.");
 
     return valid ? diff : null;
@@ -615,5 +625,5 @@
 // Start main loop
 // ----------------------------------------------------------------
-console.out("Start main loop.");
+console.out("  "+new Date().toUTCString()+": Entering main loop.");
 
 var run = -2; // getObservation never called
@@ -645,5 +655,5 @@
     if (!was_up && sun.isUp)
     {
-        console.out("", "Sun rise detected.... automatic shutdown initiated!");
+        console.out("", "  "+new Date().toUTCString()+": Sun rise detected.... automatic shutdown initiated!");
         // FIXME: State check?
         Shutdown();
@@ -743,5 +753,5 @@
          [ "LID_CONTROL",     [ "Open", "Closed"           ] ],
          [ "DRIVE_CONTROL",   drive_states/*[ "Armed", "Tracking", "OnTrack" ]*/ ],
-         [ "FTM_CONTROL",     [ "Idle", "TriggerOn"        ] ],
+         [ "FTM_CONTROL",     [ "Valid", "TriggerOn"       ] ],
          [ "FAD_CONTROL",     [ "Connected", "RunInProgress" ] ],
          [ "RATE_SCAN",       [ "Connected"                ] ],
@@ -763,5 +773,5 @@
     {
     case "STARTUP":
-        console.out("  STARTUP", "");
+        console.out("  "+new Date().toUTCString()+": New task [STARTUP]", "");
         CloseLid();
 
@@ -772,5 +782,5 @@
 
         // Before we can switch to 3000 we have to make the right DRS calibration
-        console.out("  Take single p.e. run.");
+        console.out("  "+new Date().toUTCString()+": Taking single p.e. run.");
         while (!takeRun("pedestal", 5000));
 
@@ -778,8 +788,9 @@
         service_feedback.voltageOff();
         system_on = true;
+        console.out("  "+new Date().toUTCString()+": Task finished [STARTUP]", "");
         break;
 
     case "SHUTDOWN":
-        console.out("  SHUTDOWN", "");
+        console.out("  "+new Date().toUTCString()+": New task [SHUTDOWN]", "");
         Shutdown();
         system_on = false;
@@ -789,4 +800,5 @@
         console.out("  Waiting for next startup.", "");
         sub++;
+        console.out("  "+new Date().toUTCString()+": Task finished [SHUTDOWN]", "");
         continue;
 
@@ -796,10 +808,11 @@
 
     case "DRSCALIB":
-        console.out("  DRSCALIB", "");
+        console.out("  "+new Date().toUTCString()+": New task [DRSCALIB]", "");
         doDrsCalibration("drscalib");  // will switch the voltage off
+       console.out("  "+new Date().toUTCString()+": Task finished [DRSCALIB]", "");
         break;
 
     case "SINGLEPE":
-        console.out("  SINGLE-PE", "");
+        console.out("  "+new Date().toUTCString()+": New task [SINGLE-PE]", "");
 
         // The lid must be closes
@@ -816,13 +829,14 @@
 
         // Before we can switch to 3000 we have to make the right DRS calibration
-        console.out("  Take single p.e. run.");
+        console.out("  "+new Date().toUTCString()+": Taking single p.e. run.");
         while (!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]", "");
         break;
 
     case "RATESCAN":
-        console.out("  RATESCAN", "");
+        console.out("  "+new Date().toUTCString()+": New task [RATESCAN]", "");
 
         var tm1 = new Date();
@@ -840,7 +854,13 @@
 
         if (obs.source != undefined)
+        {
+            console.out("  "+new Date().toUTCString()+": Moving drive 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.send("DRIVE_CONTROL/TRACK", obs[sub].ra, obs[sub].dec);
+        }
 
         dim.wait("DRIVE_CONTROL", "OnTrack", 150000); // 110s for turning and 30s for stabilizing
@@ -849,4 +869,6 @@
 
         var tm2 = new Date();
+
+        console.out("  "+new Date().toUTCString()+": Starting ratescan.");
 
         // Start rate scan
@@ -864,5 +886,6 @@
         //dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
 
-        console.out("  Ratescan done [%.1fs, %.1fs]".$((tm2-tm1)/1000, (new Date()-tm2)/1000));
+        console.out("  "+new Date().toUTCString()+": Ratescan done [%.1fs, %.1fs]".$((tm2-tm1)/1000, (new Date()-tm2)/1000));
+        console.out("  "+new Date().toUTCString()+": Task finished [RATESCAN]", "");
         break; // case "RATESCAN"
 
@@ -884,5 +907,5 @@
         // ------------------------------------------------------------
 
-        console.out("  Run #"+run+"  (remaining "+parseInt(remaining)+"min)");
+        console.out("  "+new Date().toUTCString()+": Run #"+run+"  (remaining "+parseInt(remaining)+"min)");
 
         // ----- Time since last DRS Calibration [min] ------
@@ -910,5 +933,5 @@
 
             //console.out("  Move telescope to '"+source+"' "+offset+" "+wobble);
-            console.out("  Move telescope to '"+obs[sub].source+"' ["+wobble+"]");
+            console.out("  "+new Date().toUTCString()+": Moving telescope to '"+obs[sub].source+"' [wobble="+wobble+"]");
 
             //var offset = observations[obs][2];
@@ -943,5 +966,5 @@
         if (point)
         {
-            console.out("  Calibration.");
+            console.out("  "+new Date().toUTCString()+": Starting calibration.");
 
             // Calibration (2% of 20')
@@ -956,5 +979,8 @@
         }
 
-        console.out("  Taking data: start [5min]");
+        //console.out("  Taking data: start [5min]");
+
+        // FIXME: What do we do if during calibration something has happened
+        // e.g. drive went to ERROR? Maybe we have to check all states again?
 
         var len = 300;
@@ -968,5 +994,5 @@
         }
 
-        console.out("  Taking data: done");
+        //console.out("  Taking data: done");
         run++;
 
@@ -975,5 +1001,5 @@
 
     if (nextObs!=undefined && sub==obs.length-1)
-        console.out("  Waiting for next observation scheduled for "+nextObs.start.toUTCString(),"");
+        console.out("  "+new Date().toUTCString()+": Waiting for next observation scheduled at "+nextObs.start.toUTCString(),"");
 
     sub++;
