Index: /trunk/FACT++/scripts/Main.js
===================================================================
--- /trunk/FACT++/scripts/Main.js	(revision 15251)
+++ /trunk/FACT++/scripts/Main.js	(revision 15252)
@@ -109,4 +109,6 @@
 service_con.onchange = function(evt)
 {
+    return;
+
     // This happens, but why?
     if (!evt.obj['status'])
@@ -229,10 +231,16 @@
     catch (e)
     {
+        console.out("");
+        console.out("MCP:         "+dim.state("MCP").name);
+        console.out("FAD_CONTROL: "+dim.state("FAD_CONTROL").name);
+        console.out("FTM_CONTROL: "+dim.state("FTM_CONTROL").name);
+        console.out("");
+
         if (dim.state("MCP").name=="Configuring3" &&
             dim.state("FAD_CONTROL").name=="Configuring2")
         {
-            conssole.out("");
-            conssole.out("Waiting for fadctrl to get configured timed out... checking for FAD loss.");
-            conssole.out("");
+            console.out("");
+            console.out("Waiting for fadctrl to get configured timed out... checking for FAD loss.");
+            console.out("");
 
             var con  = service_con.get();
@@ -245,5 +253,5 @@
                 {
                     console.out("  FAD %2d".$(i)+" not in Configured state.");
-                    reset.push(i/10);
+                    reset.push(parseInt(i/10));
                 }
 
@@ -253,8 +261,10 @@
             {
                 console.out("");
-                console.out("  FADs belong to crates "+reset);
+                console.out("  FADs belong to crate(s): "+reset);
+                console.out("");
             }
         }
 
+        console.out("Sending MCP/RESET");
         dim.send("MCP/RESET");
 
@@ -264,8 +274,5 @@
     }
 
-    if (time>0)
-        dim.wait("MCP", "Idle", time*1250); // run time plus 25%
-    else
-        dim.wait("MCP", "Idle"); 
+    dim.wait("MCP", "Idle", time>0 ? time*1250 : undefined); // run time plus 25%
 
     //console.out("  Take run: end");
@@ -281,8 +288,11 @@
 // ----------------------------------------------------------------
 
-function doDrsCalibration()
-{
-    //console.out("  DRS cal: start");
+function doDrsCalibration(where)
+{
+    console.out("  Take DRS calibration ["+where+"]");
+
     service_feedback.voltageOff();
+
+    var tm = new Date();
 
     while (1)
@@ -315,8 +325,5 @@
     }
 
-    //dim.send("RATE_CONTROL/STOP"); // get out of GlobalThresholdSet
-    //dim.wait("RATE_CONTROL", "Connected", 3000);
-
-    //console.out("  DRS cal: end");
+    console.out("  DRS calibration done [%.1f]".$((new Date()-tm)/1000));
 }
 
@@ -336,4 +343,6 @@
     var isClosed = dim.state("LID_CONTROL").name=="Closed";
 
+    var tm = new Date();
+
     // Wait for lid to be open
     if (isClosed)
@@ -345,5 +354,5 @@
 
     if (isClosed)
-        console.out("  Open lid: done");
+        console.out("  Open lid: done [%.1fs]".$((new Date()-tm)/1000));
 }
 
@@ -351,4 +360,6 @@
 {
     var isOpen = dim.state("LID_CONTROL").name=="Open";
+
+    var tm = new Date();
 
     // Wait for lid to be open
@@ -361,5 +372,5 @@
 
     if (isOpen)
-        console.out("  Close lid: end");
+        console.out("  Close lid: end [%.1fs]".$((new Date()-tm)/1000));
 }
 
@@ -535,40 +546,4 @@
 include('scripts/Startup.js');//Startup();
 
-/*
-include('scripts/CheckStates.js');
-
-var table =
-[
- [ "TNG_WEATHER"   ],
- [ "MAGIC_WEATHER" ],
- [ "CHAT"          ],
- [ "SMART_FACT"     ],
- [ "FSC_CONTROL",     [ "Connected"                    ] ],
- [ "MCP",             [ "Idle"                         ] ],
- [ "TIME_CHECK",      [ "Valid"                        ] ],
- [ "PWR_CONTROL",     [ "SystemOn"                     ] ],
- [ "AGILENT_CONTROL", [ "VoltageOn"                    ] ],
- [ "BIAS_CONTROL",    [ "VoltageOff"                   ] ],
- [ "FEEDBACK",        [ "CurrentControl", "CurrentCtrlIdle", "Connected" ] ],
- [ "RATE_SCAN",       [ "Connected"                    ] ],
- [ "RATE_CONTROL",    [ "Connected"                    ] ],
- [ "LID_CONTROL",     [ "Open", "Closed"               ] ],
- [ "DRIVE_CONTROL",   [ "Armed", "Tracking", "OnTrack" ] ],
- [ "FTM_CONTROL",     [ "Idle", "TriggerOn"            ] ],
- [ "FAD_CONTROL",     [ "Connected", "WritingData"     ] ],
- [ "DATA_LOGGER",     [ "NightlyFileOpen", "WaitForRun", "Logging" ] ],
-];
-
-console.out("Checking states.");
-if (!checkStates(table, 10000))
-{
-    throw new Error"Something unexpected has happened. Although the startup-",
-            "procedure has finished, not all servers are in the state",
-            "in which they ought to be. Please, try to find out what",
-            "happened...");
-}
-
-console.out("Checking states: done.");
-*/
 // ----------------------------------------------------------------
 
@@ -622,4 +597,6 @@
     console.out("Shutdown: start");
 
+    dim.send("FEEDBACK/STOP");
+    dim.send("FEEDBACK/ENABLE_OUTPUT", false);
     dim.send("FTM_CONTROL/STOP_TRIGGER");
     service_feedback.voltageOff();
@@ -659,5 +636,5 @@
     console.out("");
     console.out("Shutdown procedure seems to be finished...");
-    console.out("  Telescope at Zd="+report.obj['Zd']+"deg Az="+report.obj['Az']+"deg");
+    console.out("  Telescope at Zd=%.1fdeg Az=%.1fdeg".$(report.obj['Zd'], report.obj['Az']));
     console.out("  Please make sure the park position was reached");
     console.out("  and the telescope is not moving anymore.");
@@ -743,6 +720,6 @@
         if (run<0)
         {
-            Startup();   // -> Bias On/Off?, Lid open/closed?
-            CloseLid();
+            //Startup();   // -> Bias On/Off?, Lid open/closed?
+            //CloseLid();
         }
 
@@ -768,176 +745,8 @@
     }
 
-    console.out("\n"+(new Date()).toUTCString()+": Current measurement: "+obs[sub]);
-
-    // Check if obs.task is one of the one-time-tasks
-    switch (obs[sub].task)
-    {
-    case "STARTUP":
-        console.out("  STARTUP", "");
-        Startup();  // BiasOn/Off?, Lid open/close?
-        CloseLid();
-
-        console.out("  Take DRS calibration [startup]");
-        doDrsCalibration();  // -> VoltageOff
-        console.out("  DRS calibration done.");
-
-        service_feedback.voltageOn();
-        service_feedback.waitForVoltageOn();
-
-        // Before we can switch to 3000 we have to make the right DRS calibration
-        console.out("  Take single p.e. run.");
-        while (!takeRun("pedestal", 5000));
-
-        service_feedback.voltageOff();
-
-        //dim.send("RATE_CONTROL/STOP"); // get out of GlobalThresholdSet
-        //dim.wait("RATE_CONTROL", "Connected", 3000);
-
-        console.out("  Waiting for first scheduled observation.","");
-        sub++;
-        continue;
-
-    case "SHUTDOWN":
-        console.out("  SHUTDOWN","");
-        Shutdown();
-
-        console.out("  Waiting for next startup.", "");
-        sub++;
-        continue;
-
-    case "IDLE":
-        v8.sleep(1000);
-        continue;
-
-    case "DRSCALIB":
-        console.out("  DRSCALIB", "");
-
-        console.out("  Take DRS calibration [drscalib]");
-        doDrsCalibration();  // -> VoltageOff
-        console.out("  DRS calibration done.");
-
-        //dim.send("RATE_CONTROL/STOP"); // get out of GlobalThresholdSet
-        //dim.wait("RATE_CONTROL", "Connected", 3000);
-
-        sub++;
-        continue;
-
-    case "SINGLEPE":
-
-        // FIXME: Check whether a DRS calib is necessary.
-
-        service_feedback.voltageOn();
-        service_feedback.waitForVoltageOn();
-
-        // Before we can switch to 3000 we have to make the right DRS calibration
-        console.out("  Take single p.e. run.");
-        while (!takeRun("pedestal", 5000));
-
-        service_feedback.voltageOff();
-
-        //dim.send("RATE_CONTROL/STOP"); // get out of GlobalThresholdSet
-        //dim.wait("RATE_CONTROL", "Connected", 3000);
-
-        sub++;
-        continue;
-
-    case "RATESCAN":
-        console.out("  RATESCAN  ");
-        /*
-        if (dim.state("FTM_CONTROL").name=="TriggerOn")
-        {
-            dim.send("FTM_CONTROL/STOP_TRIGGER");
-            dim.wait("FTM_CONTROL", "Idle", 3000);
-        }*/
-
-        // This is a workaround to make sure that we really catch
-        // the new state and not the old one
-        dim.send("DRIVE_CONTROL/STOP");
-        dim.wait("DRIVE_CONTROL", "Armed", 5000);
-
-        OpenLid();
-
-        service_feedback.voltageOn();
-
-        if (obs.source != undefined)
-            dim.send("DRIVE_CONTROL/TRACK_ON", obs[sub].source);
-        else
-            dim.send("DRIVE_CONTROL/TRACK", obs[sub].ra, obs[sub].dec);
-
-        dim.wait("DRIVE_CONTROL", "OnTrack", 150000); // 110s for turning and 30s for stabilizing
-
-        service_feedback.waitForVoltageOn();
-
-        // Checking if system is Ready for Data Taking, which is in this case 
-        // the same as Ready for RateScan.
-        console.out("  Checking states [ratescan]");
-        var table =
-            [
-             [ "TNG_WEATHER"   ],
-             [ "MAGIC_WEATHER" ],
-             [ "CHAT"          ],
-             [ "SMART_FACT"    ],
-             [ "DATA_LOGGER",     [ "NightlyFileOpen", "WaitForRun", "Logging" ] ],
-             [ "FSC_CONTROL",     [ "Connected"                ] ],
-             [ "MCP",             [ "Idle"                     ] ],
-             [ "TIME_CHECK",      [ "Valid"                    ] ],
-             [ "PWR_CONTROL",     [ "SystemOn"                 ] ],
-             [ "AGILENT_CONTROL", [ "VoltageOn"                ] ],
-             [ "BIAS_CONTROL",    [ "VoltageOn", "Ramping"     ] ],
-             [ "FEEDBACK",        [ "CurrentControl"           ] ],
-             [ "RATE_SCAN",       [ "Connected"                ] ],
-             [ "RATE_CONTROL",    [ "Connected", "GlobalThresholdSet", "InProgress"  ] ],
-             [ "LID_CONTROL",     [ "Open"                     ] ],
-             [ "DRIVE_CONTROL",   [ "Tracking", "OnTrack"      ] ],
-             [ "FTM_CONTROL",     [ "Idle", "TriggerOn"        ] ],
-             [ "FAD_CONTROL",     [ "Connected", "WritingData" ] ],
-            ];
-
-        if (!checkStates(table))
-        {
-            throw new Error("Something unexpected has happened. One of the servers"+
-                            "is in a state in which it should not be. Please,"+
-                            "try to find out what happened...");
-        }
-
-        // Start rate scan
-        dim.send("RATE_SCAN/START_THRESHOLD_SCAN", 50, 1000, -10);
-
-        // Lets wait if the ratescan really starts... this might take a few
-        // seconds because RATE_SCAN configures the ftm and is waiting for
-        // it to be configured.
-        dim.wait("RATE_SCAN", "InProgress", 10000);
-        dim.wait("RATE_SCAN", "Connected", 2700000);
-
-        // this line is actually some kind of hack. 
-        // after the Ratescan, no data is written to disk. I don't know why, but it happens all the time
-        // So I decided to put this line here as a kind of patchwork....
-        //dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
-
-        console.out("Ratescan done.");
-        sub++;
-        continue;
-    }
-
-    // ========================== case "DATA" ============================
-/*
-    if (Sun.horizon("FACT").isUp)
-    {
-        console.out("  SHUTDOWN","");
-        Shutdown();
-        console.out("  Exit forced due to broken schedule", "");
-        exit();
-    }
-*/
-    // Calculate remaining time for this observation in minutes
-    var remaining = nextObs==undefined ? 0 : (nextObs.start-new Date())/60000;
-
-    // ------------------------------------------------------------
-
-    // Checking for 'Ramping' in the BIAS_CONTROL is not ideal, but at the moment
-    // it is not possible to distinguish between a real ramping and the short
-    // ramping which takes place whenever the feedback updated the voltages.
-
-    console.out("  Checking states [mainloop]");
+    // A scheduled task was found, lets check if all servers are
+    // still only and in reasonable states. If this is not the case,
+    // something unexpected must have happend and the script is aborted.
+    //console.out("  Checking states [general]");
     var table =
         [
@@ -947,27 +756,171 @@
          [ "SMART_FACT"    ],
          [ "DATA_LOGGER",     [ "NightlyFileOpen", "WaitForRun", "Logging" ] ],
-         [ "FSC_CONTROL",     [ "Connected"                                ] ],
-         [ "MCP",             [ "Idle"                                     ] ],
-         [ "TIME_CHECK",      [ "Valid"                                    ] ],
-         [ "PWR_CONTROL",     [ "SystemOn"                                 ] ],
-         [ "AGILENT_CONTROL", [ "VoltageOn"                                ] ],
-         [ "BIAS_CONTROL",    [ "VoltageOff", "VoltageOn", "Ramping"       ] ],
-         [ "FEEDBACK",        [ "CurrentCtrlIdle", "CurrentControl"        ] ],
-         [ "RATE_SCAN",       [ "Connected"                                ] ],
-         [ "RATE_CONTROL",    [ "Connected", "GlobalThresholdSet", "InProgress" ] ],
-         [ "LID_CONTROL",     [ "Open", "Closed"                           ] ],
-         [ "DRIVE_CONTROL",   [ "Armed", "Tracking", "OnTrack"             ] ],
-         [ "FTM_CONTROL",     [ "Idle", "TriggerOn"                        ] ],
-         [ "FAD_CONTROL",     [ "Connected", "WritingData"                 ] ],
+         [ "FSC_CONTROL",     [ "Connected"                ] ],
+         [ "MCP",             [ "Idle"                     ] ],
+         [ "TIME_CHECK",      [ "Valid"                    ] ],
+         [ "PWR_CONTROL",     [ "SystemOn"                 ] ],
+         [ "AGILENT_CONTROL", [ "VoltageOn"                ] ],
+         [ "BIAS_CONTROL",    [ "VoltageOff", "VoltageOn", "Ramping" ] ],
+         [ "FEEDBACK",        [ "CurrentControl", "CurrentCtrlIdle" ] ],
+         [ "LID_CONTROL",     [ "Open", "Closed"           ] ],
+         [ "DRIVE_CONTROL",   [ "Armed", "Tracking", "OnTrack" ] ],
+         [ "FTM_CONTROL",     [ "Idle", "TriggerOn"        ] ],
+         [ "FAD_CONTROL",     [ "Connected", "WritingData" ] ],
+         [ "RATE_SCAN",       [ "Connected"                ] ],
+         [ "RATE_CONTROL",    [ "Connected", "GlobalThresholdSet", "InProgress"  ] ],
         ];
 
     if (!checkStates(table))
     {
-        //dim.alarm("System inconsistent");
-        //dim.alarm();
-        throw new Error("Something unexpected has happened. One of the servers "+
-                        "is in a state in which it should not be. Please, "+
+        throw new Error("Something unexpected has happened. One of the servers"+
+                        "is in a state in which it should not be. Please,"+
                         "try to find out what happened...");
     }
+
+    console.out("\n"+(new Date()).toUTCString()+": Current measurement: "+obs[sub]);
+
+    // Check if obs.task is one of the one-time-tasks
+    switch (obs[sub].task)
+    {
+    case "STARTUP":
+        console.out("  STARTUP", "");
+        Startup();  // BiasOn/Off?, Lid open/close?
+        CloseLid();
+
+        doDrsCalibration("startup");  // will switch the voltage off
+
+        service_feedback.voltageOn();
+        service_feedback.waitForVoltageOn();
+
+        // Before we can switch to 3000 we have to make the right DRS calibration
+        console.out("  Take single p.e. run.");
+        while (!takeRun("pedestal", 5000));
+
+        service_feedback.voltageOff();
+
+        //dim.send("RATE_CONTROL/STOP"); // get out of GlobalThresholdSet
+        //dim.wait("RATE_CONTROL", "Connected", 3000);
+
+        if (nextObs!=undefined && sub==obs.length-1)
+            console.out("  Waiting for next observation scheduled for "+nextObs.start.toUTCString(),"");
+        sub++;
+        continue;
+
+    case "SHUTDOWN":
+        console.out("  SHUTDOWN", "");
+        Shutdown();
+
+        console.out("  Waiting for next startup.", "");
+        sub++;
+        continue;
+
+    case "IDLE":
+        v8.sleep(1000);
+        continue;
+
+    case "DRSCALIB":
+        console.out("  DRSCALIB", "");
+
+        doDrsCalibration("drscalib");  // will switch the voltage off
+
+        if (nextObs!=undefined && sub==obs.length-1)
+            console.out("  Waiting for next observation scheduled for "+nextObs.start.toUTCString(),"");
+
+        sub++;
+        continue;
+
+    case "SINGLEPE":
+        console.out("  SINGLE-PE", "");
+
+        // FIXME: Check whether a DRS calib is necessary.
+
+        // The lid must be closes
+        CloseLid();
+
+        // The voltage must be on
+        service_feedback.voltageOn();
+        service_feedback.waitForVoltageOn();
+
+        // Before we can switch to 3000 we have to make the right DRS calibration
+        console.out("  Take single p.e. run.");
+        while (!takeRun("pedestal", 5000));
+
+        // It is unclear what comes next, so we better switch off the voltage
+        // service_feedback.voltageOff();
+
+        if (nextObs!=undefined && sub==obs.length-1)
+            console.out("  Waiting for next observation scheduled for "+nextObs.start.toUTCString(),"");
+
+        sub++;
+        continue;
+
+    case "RATESCAN":
+        console.out("  RATESCAN", "");
+
+        var tm1 = new Date();
+
+        // This is a workaround to make sure that we really catch
+        // the new state and not the old one
+        dim.send("DRIVE_CONTROL/STOP");
+        dim.wait("DRIVE_CONTROL", "Armed", 5000);
+
+        // The lid must be open
+        OpenLid();
+
+        // The voltage must be switched on
+        service_feedback.voltageOn();
+
+        if (obs.source != undefined)
+            dim.send("DRIVE_CONTROL/TRACK_ON", obs[sub].source);
+        else
+            dim.send("DRIVE_CONTROL/TRACK", obs[sub].ra, obs[sub].dec);
+
+        dim.wait("DRIVE_CONTROL", "OnTrack", 150000); // 110s for turning and 30s for stabilizing
+
+        service_feedback.waitForVoltageOn();
+
+        var tm2 = new Date();
+
+        // Start rate scan
+        dim.send("RATE_SCAN/START_THRESHOLD_SCAN", 50, 1000, -10);
+
+        // Lets wait if the ratescan really starts... this might take a few
+        // seconds because RATE_SCAN configures the ftm and is waiting for
+        // it to be configured.
+        dim.wait("RATE_SCAN", "InProgress", 10000);
+        dim.wait("RATE_SCAN", "Connected", 2700000);
+
+        // this line is actually some kind of hack. 
+        // after the Ratescan, no data is written to disk. I don't know why, but it happens all the time
+        // So I decided to put this line here as a kind of patchwork....
+        //dim.send("FAD_CONTROL/SET_FILE_FORMAT", 2);
+
+        console.out("  Ratescan done [%.1fs, %.1fs]".$((tm2-tm1)/1000, (new Date()-tm2)/1000));
+
+        if (nextObs!=undefined && sub==obs.length-1)
+            console.out("  Waiting for next observation scheduled for "+nextObs.start.toUTCString(),"");
+
+        sub++;
+        continue;
+    }
+
+    // ========================== case "DATA" ============================
+/*
+    if (Sun.horizon("FACT").isUp)
+    {
+        console.out("  SHUTDOWN","");
+        Shutdown();
+        console.out("  Exit forced due to broken schedule", "");
+        exit();
+    }
+*/
+    // Calculate remaining time for this observation in minutes
+    var remaining = nextObs==undefined ? 0 : (nextObs.start-new Date())/60000;
+
+    // ------------------------------------------------------------
+
+    // Checking for 'Ramping' in the BIAS_CONTROL is not ideal, but at the moment
+    // it is not possible to distinguish between a real ramping and the short
+    // ramping which takes place whenever the feedback updated the voltages.
 
     // ------------------------------------------------------------
@@ -986,5 +939,5 @@
         console.out("  Last DRS calib: %.1fmin ago".$(diff));
     else
-        console.out("  No valid drs calibration");
+        console.out("  No valid drs calibration available");
 
     // Changine pointing position and take calibration...
@@ -1023,9 +976,5 @@
 
     if (drscal)
-    {
-        console.out("  Take DRS calibration [interleaved]");
-        doDrsCalibration();  // -> VoltageOff
-        console.out("  DRS calibration done.");
-    }
+        doDrsCalibration("data");  // will turn voltage off
 
     OpenLid();
