Index: /trunk/MagicSoft/Cosy/Changelog
===================================================================
--- /trunk/MagicSoft/Cosy/Changelog	(revision 7787)
+++ /trunk/MagicSoft/Cosy/Changelog	(revision 7788)
@@ -37,4 +37,39 @@
        is neccessary in root 4.04/02g to make cosy work is
        unknown. Anyhow it makes switching the cameras more robust
+
+   * cosy.cc:
+     - updated writing log-files
+     - implemented sanity check for batch mode
+
+   * gui/MGAccuracy.cc, gui/MGStarg.cc:
+     - use GetabsDev from MAstro
+     - scale azimuth deviation to real distance on the sky
+
+   * main/MCosy.[h,cc]:
+     - updated writing log-files
+     - do no perfect position in case of move'n'track
+     - implemented WM_PREPS for predefined positions
+     - unified WM_TRACK and WM_GRB
+     - updated GetFileName
+
+   * main/MStarguider.cc:
+     - undef EXPERT
+     - some layout changes
+     - set rotation angle of starguider camera to -0.4deg
+     - unified FindStar and FindStarCircle
+     - updated writing log-files
+     - to calc skycenter the X-offset must be subtracted
+
+   * tcpip/MDriveCom.[h,cc]:
+     - implemented CommandPREPS
+     - implemented CommandARM
+
+   * videodev/FilterLed.[h,cc]:
+     - unified FindStar and FindStarCircle
+     - fixed algorithm FindStarCircle (it was not working for
+       bright stars and didn't take care of the edge of the
+       image!)
+     - fixed a long outstanding bug which accessed some memory
+       outside the image due to a wrong logical condition
 
 
Index: /trunk/MagicSoft/Cosy/cosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/cosy.cc	(revision 7787)
+++ /trunk/MagicSoft/Cosy/cosy.cc	(revision 7788)
@@ -48,5 +48,5 @@
     // this must move to MGCosy !!!!
     //
-    const TString name = MCosy::GetFileName("log/cosy_%s.log");
+    const TString name = MCosy::GetFileName("log", "cosy", "log");
     cout << "Open Logfile: " << name << endl;
 
@@ -65,4 +65,10 @@
     // FIXME: Fails deleteing something in TGClient::fWlist
     TApplication *app=new TApplication("App", &argc, argv);
+
+    if (gROOT->IsBatch())
+    {
+        cout << "ERROR - Cannot run in Batch mode!" << endl;
+        return 0;
+    }
 
     //
Index: /trunk/MagicSoft/Cosy/gui/MGAccuracy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGAccuracy.cc	(revision 7787)
+++ /trunk/MagicSoft/Cosy/gui/MGAccuracy.cc	(revision 7788)
@@ -20,4 +20,5 @@
 #include "coord.h"
 #include "MTime.h"
+#include "MAstro.h"
 
 ClassImp(MGAccuracy);
@@ -238,29 +239,5 @@
 void MGAccuracy::Update(Float_t pzd, Float_t azd, Float_t aaz)
 {
-    const Float_t d2r = TMath::Pi()/180.;
-
-    pzd *= d2r;
-    azd *= d2r;
-    aaz *= d2r;
-
-    const double el = TMath::Pi()/2-pzd;
-
-    const double dphi2 = aaz/2.;
-    const double cos2  = cos(dphi2)*cos(dphi2);
-    const double sin2  = sin(dphi2)*sin(dphi2);
-    const double d     = cos(azd)*cos2 - cos(2*el)*sin2;
-
-    //
-    // Original:
-    //   cos(Zd1)*cos(Zd2)+sin(Zd1)*sin(Zd2)*cos(dAz)
-    //
-    // Correct:
-    //   const double d = cos(azd)*cos2 - cos(el1+el2)*sin2;
-    //
-    // Estimated:
-    //   const double d = cos(azd)*cos2 - cos(2*el)*sin2;
-    //
-
-    double dist = acos(d)*TMath::RadToDeg();
+    const double dist = MAstro::GetDevAbs(pzd, azd, aaz);
 
     UpdateText(dist);
@@ -303,8 +280,13 @@
 }
 
-void MGAccuracy::Update(ZdAz &pos, ZdAz &acc)
-{
-    UpdateCross(acc.Az()*3600., acc.Zd()*3600.);
-    Update(pos.Zd(), acc.Zd(), acc.Az());
+void MGAccuracy::Update(ZdAz &pos, ZdAz &dev)
+{
+    // Estimate the distance in az direction on the camera plane
+    const double daz = MAstro::GetDevAbs(pos.Zd(), 0, dev.Az())*3600.;
+
+    //cout << "--> T: " << dev.Az()*60 << " " << dev.Zd()*60. << endl;
+
+    UpdateCross(TMath::Sign(daz, dev.Az()), dev.Zd()*3600.);
+    Update(pos.Zd(), dev.Zd(), dev.Az());
 
     UpdateCanvas();
Index: /trunk/MagicSoft/Cosy/gui/MGStarg.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGStarg.cc	(revision 7787)
+++ /trunk/MagicSoft/Cosy/gui/MGStarg.cc	(revision 7788)
@@ -16,4 +16,5 @@
 #include "coord.h"
 #include "MTime.h"
+#include "MAstro.h"
 
 ClassImp(MGStarg);
@@ -36,11 +37,11 @@
     text.SetTextAlign(11);  // left, bottom (s.TAttText)
     text.SetTextColor(3);
-    text.DrawText(160, -220, "0.0125 deg");
+    text.DrawText(220, -220, "0.75'");
 
     text.SetTextColor(5);
-    text.DrawText(160, -250, "0.025 deg");
+    text.DrawText(220, -250, "1.50'");
 
     text.SetTextColor(2);
-    text.DrawText(160, -280, "0.05 deg");
+    text.DrawText(220, -280, "3.00'");
 
 
@@ -218,18 +219,5 @@
 void MGStarg::Update(Float_t pzd, Float_t azd, Float_t aaz)
 {
-    const Float_t d2r = TMath::Pi()/180.;
-
-    pzd *= d2r;
-    azd *= d2r;
-    aaz *= d2r;
-
-    const double el = TMath::Pi()/2-pzd;
-
-    const double dphi2 = aaz/2.;
-    const double cos2  = cos(dphi2)*cos(dphi2);
-    const double sin2  = sin(dphi2)*sin(dphi2);
-    const double d     = cos(azd)*cos2 - cos(2*el)*sin2;
-
-    double dist = acos(d)*TMath::RadToDeg();
+    const double dist = MAstro::GetDevAbs(pzd, azd, aaz);
 
     UpdateText(dist);
@@ -269,20 +257,14 @@
 }
 
-void MGStarg::Update(ZdAz &pos, ZdAz &acc)
-{
-
-    UpdateCross(acc.Az()*3600., acc.Zd()*3600.);
-    Update(pos.Zd(), acc.Zd(), acc.Az());
+void MGStarg::Update(ZdAz &pos, ZdAz &dev)
+{
+    // Estimate the distance in az direction on the camera plane
+    const double daz = MAstro::GetDevAbs(pos.Zd(), 0, dev.Az())*3600.;
+
+    //cout << "--> S: " << dev.Az()*60 << " " << dev.Zd()*60. << endl;
+
+    UpdateCross(TMath::Sign(daz, dev.Az()), dev.Zd()*3600.);
+    Update(pos.Zd(), dev.Zd(), dev.Az());
 
     UpdateCanvas();
 }
-
-
-
-
-
-
-
-
-
-
Index: /trunk/MagicSoft/Cosy/main/MCosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 7787)
+++ /trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 7788)
@@ -401,4 +401,5 @@
 //
     point.SetPointAccDec(0.2, 0.1);
+
     point.SetPointVelocity(0.1);
 
@@ -634,5 +635,5 @@
                 // open tpoint file
                 //
-                const TString name = GetFileName("tpoint/old-tpoint_%s.txt");
+                const TString name = GetFileName("tpoint", "old-tpoint", "txt");
                 cout << "TPoint-Cosy File ********* " << name << " ********** " << endl;
 
@@ -675,5 +676,5 @@
 
             ZdAz dest = *((ZdAz*)mp) * kDeg2Rad;
-            if (!SetPosition(dest))
+            if (!SetPosition(dest, kTRUE))
                 return (void*)0x1234;
 
@@ -686,5 +687,28 @@
         //cout << "WM_TrackPosition: done. (return 0xabcd)" << endl;
         return (void*)0xabcd;
-
+/*
+    case WM_ARM:
+        //cout << "WM_Position: start." << endl;
+        {
+            if (!CheckNetwork())
+                return (void*)0xebb0;
+
+            const bool arm = *((bool*)mp);
+            if (arm)
+            {
+                fMac1->Arm();
+                fMac2->Arm();
+                cout << "ARMED" << endl;
+            }
+            else
+            {
+                fMac1->Disarm();
+                fMac2->Disarm();
+                cout << "DISARMED" << endl;
+            }
+        }
+        //cout << "WM_Position: done. (return 0x7777)" << endl;
+        return (void*)0x9999;
+  */
     case WM_POSITION:
         //cout << "WM_Position: start." << endl;
@@ -711,4 +735,44 @@
         return (void*)0x7777;
 
+    case WM_PREPS:
+        //cout << "WM_Track: START" << endl;
+        {
+            if (!CheckNetwork())
+                return (void*)0xebb0;
+
+            const char *preps = (const char*)mp;
+            cout << "Preposition command to " << preps << " received." << endl;
+
+            ifstream fin("prepos.txt");
+            if (!fin)
+            {
+                cout << "ERROR: cannot open prepos.txt." << endl;
+                return (void*)0xebb1;
+            }
+
+            while (1)
+            {
+                Double_t zd, az;
+                fin >> zd >> az;
+
+                TString str;
+                str.ReadLine(fin);
+                if (!fin)
+                    break;
+
+                str.ToLower();
+
+                if (str.Strip(TString::kBoth)==preps)
+                {
+                    ZdAz dest(zd, az);
+                    SetPosition(dest*kDeg2Rad);
+                    return (void*)0x7979;
+                }
+                cout << "ERROR - Requested preposition not found in file..." << endl;
+            }
+        }
+        //cout << "WM_Track: done. (return 0x8888)" << endl;
+        return (void*)0x7878;
+
     case WM_TESTSE:
         //cout << "WM_TestSe: start." << endl;
@@ -730,5 +794,6 @@
 
     case WM_TRACK:
-        //cout << "WM_Track: START" << endl;
+    case WM_GRB:
+        //cout << "WM_Track/GRB: START" << endl;
         {
             RaDec dest = ((RaDec*)mp)[0];
@@ -737,20 +802,11 @@
             if (!CheckNetwork())
                 return (void*)0xebb0;
-            TrackPosition(dest*kDeg2Rad);
+
+            if (msg==WM_TRACK)
+                TrackPosition(dest*kDeg2Rad);
+            else
+                TrackPositionGRB(dest*kDeg2Rad);
         }
-        //cout << "WM_Track: done. (return 0x8888)" << endl;
-        return (void*)0x8888;
-
-    case WM_GRB:
-        //cout << "WM_Track: START" << endl;
-        {
-            RaDec dest = ((RaDec*)mp)[0];
-            if (fStarguider)
-                fStarguider->SetPointingPosition(((RaDec*)mp)[1]);
-            if (!CheckNetwork())
-                return (void*)0xebb0;
-            TrackPositionGRB(dest*kDeg2Rad);
-        }
-        //cout << "WM_Track: done. (return 0x8888)" << endl;
+        //cout << "WM_Track/GRB: done. (return 0x8888)" << endl;
         return (void*)0x8888;
 
@@ -1178,4 +1234,5 @@
     avail |= (fZd2  && !fZd2->IsZombieNode())  ? 0x10 : 0;
     avail |= (fAz   && !fAz->IsZombieNode())   ? 0x20 : 0;
+//    avail |= (!(fStatus&MDriveCom::kError) && 1 ? 0x40 : 0;
 
     if (HasError())
@@ -1471,14 +1528,31 @@
 */
 
-TString MCosy::GetFileName(const char *fmt)
+TString MCosy::GetFileName(const char *path, const char *name, const char *ext)
 {
     // FIXME: Timeout missing
+
     while (1)
     {
         MTime time(-1);
-        const TString name = Form(fmt, (const char*)time.GetFileName());
-        if (gSystem->AccessPathName(name, kFileExists))
-            return name;
-            break;
+
+        // This is the full qualified date which is part of the name
+        const TString clock = time.GetStringFmt("%y%m%d_%H%M%S");
+
+        // This gives the night in which the date belongs to
+        time.SetMjd(TMath::Nint(time.GetMjd()));
+
+        const TString night = time.GetStringFmt("%Y_%m_%d");
+
+        const TString dir   = Form("%s/%s", path, night.Data());
+        const TString fname = Form("%s_%s.%s", name, clock.Data(), ext);
+
+        const TString full  = Form("%s/%s", dir.Data(), fname.Data());
+
+        gSystem->mkdir(dir, kTRUE);
+
+        if (gSystem->AccessPathName(full, kFileExists))
+            return full;
+
+        break;// !!!!!!!!!!!!!!!!!!!!!!!
 
         usleep(1000);
@@ -1498,5 +1572,5 @@
     const Int_t id6 = env.GetValue("Az_Id-SE",   6); //6
 
-    TString name = GetFileName("rep/cosy_%s.rep");
+    TString name = GetFileName("rep", "cosy", "rep");
     cout << "Open Repfile: " << name << endl;
     fOutRep = new MLog(name, kTRUE);
Index: /trunk/MagicSoft/Cosy/main/MCosy.h
===================================================================
--- /trunk/MagicSoft/Cosy/main/MCosy.h	(revision 7787)
+++ /trunk/MagicSoft/Cosy/main/MCosy.h	(revision 7788)
@@ -38,4 +38,6 @@
 #define WM_ENDSWITCH    0x1010
 #define WM_GRB          0x1011
+#define WM_PREPS        0x1012
+#define WM_ARM          0x1013
 
 class ShaftEncoder;
@@ -187,5 +189,5 @@
     void SetStarguider(MStarguider *s) { fStarguider = s; }
 
-    static TString GetFileName(const char *name);
+    static TString GetFileName(const char *path, const char *name, const char *ext);
 
     MGCosy *GetWin() { return fWin; }
Index: /trunk/MagicSoft/Cosy/main/MStarguider.cc
===================================================================
--- /trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 7787)
+++ /trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 7788)
@@ -1,2 +1,3 @@
+#undef EXPERT
 #undef EXPERT
 
@@ -335,25 +336,25 @@
 
     fCZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fCZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+581+12);
+    fCZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+581+20-4);
     AddFrame(fCZdAz);
     fList->Add(fCZdAz);
 
     fPZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fPZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+627+2*12);
+    fPZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+627+2*20-4);
     AddFrame(fPZdAz);
     fList->Add(fPZdAz);
 
     fDZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fDZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+673+3*12);
+    fDZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+673+3*20-4);
     AddFrame(fDZdAz);
     fList->Add(fDZdAz);
 
     fSZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fSZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+719+4*12);
+    fSZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+719+4*20-4);
     AddFrame(fSZdAz);
     fList->Add(fSZdAz);
 
     fTPoint = new TGTextButton(this, "TPoint");
-    fTPoint->Move(4, fMenu->GetDefaultHeight()+722+3*12+25);
+    fTPoint->Move(4, fMenu->GetDefaultHeight()+722+3*15+15);
     fTPoint->AllowStayDown(kTRUE);
     AddFrame(fTPoint);
@@ -361,5 +362,5 @@
 
     fStargTPoint = new TGTextButton(this, "StargTPoint");
-    fStargTPoint->Move(240+12+20, fMenu->GetDefaultHeight()+785);
+    fStargTPoint->Move(240+12+28, fMenu->GetDefaultHeight()+785);
     fStargTPoint->AllowStayDown(kTRUE);
     AddFrame(fStargTPoint);
@@ -368,10 +369,10 @@
     fFps = new TGLabel(this, "---fps");
     fFps->SetTextJustify(kTextRight);
-    fFps->Move(650-440, fMenu->GetDefaultHeight()+619+13+60+20);
+    fFps->Move(650-495, fMenu->GetDefaultHeight()+619+13+60+20+2);
     AddFrame(fFps);
     fList->Add(fFps);
 
-    fPosZoom = new TGLabel(this, "----.--d/----.--d (----, ----)");
-    fPosZoom->SetTextJustify(kTextRight);
+    fPosZoom = new TGLabel(this, "(----, ----) ----.--d/----.--d");
+    fPosZoom->SetTextJustify(kTextLeft);
     fPosZoom->Move(4, fMenu->GetDefaultHeight()+765);
     AddFrame(fPosZoom);
@@ -386,5 +387,5 @@
     TGLabel *l = new TGLabel(this, "arcsec/pix");
     l->SetTextJustify(kTextLeft);
-    l->Move(605-400, fMenu->GetDefaultHeight()+619+13+60);
+    l->Move(606-412, fMenu->GetDefaultHeight()+619+13+60);
     AddFrame(l);
     fList->Add(l);
@@ -392,5 +393,5 @@
     l = new TGLabel(this, "deg");
     l->SetTextJustify(kTextLeft);
-    l->Move(605-410, fMenu->GetDefaultHeight()+619-10+60);
+    l->Move(606-412, fMenu->GetDefaultHeight()+619-10+60);
     AddFrame(l);
     fList->Add(l);
@@ -398,5 +399,5 @@
     l = new TGLabel(this, "Telescope pointing at");
     l->SetTextJustify(kTextLeft);
-    l->Move(240+12+20, fMenu->GetDefaultHeight()+584-5);
+    l->Move(240+12+20+7, fMenu->GetDefaultHeight()+584-5);
     AddFrame(l);
     fList->Add(l);
@@ -404,5 +405,5 @@
     l = new TGLabel(this, "Starguider position");
     l->SetTextJustify(kTextLeft);
-    l->Move(240+12+20, fMenu->GetDefaultHeight()+630+12-5);
+    l->Move(240+12+20+7, fMenu->GetDefaultHeight()+630+20-5);
     AddFrame(l);
     fList->Add(l);
@@ -410,5 +411,5 @@
     l = new TGLabel(this, "Misspointing");
     l->SetTextJustify(kTextLeft);
-    l->Move(240+12+20, fMenu->GetDefaultHeight()+676+2*12-5);
+    l->Move(240+12+20+7, fMenu->GetDefaultHeight()+676+2*20-5);
     AddFrame(l);
     fList->Add(l);
@@ -417,5 +418,5 @@
     l = new TGLabel(this, "Misspointing/FindStar (Experts Only!)");
     l->SetTextJustify(kTextLeft);
-    l->Move(240+12+20, fMenu->GetDefaultHeight()+722+3*12-5);
+    l->Move(240+12+20, fMenu->GetDefaultHeight()+722+3*20-5);
     AddFrame(l);
     fList->Add(l);
@@ -436,5 +437,11 @@
     fList->Add(fPixSize);
 
-    fAngle = new TGTextEntry(this, "         0", IDM_kAngle);
+    const Double_t angle = -0.4;
+    fSao->SetRotationAngle(angle);
+
+    txt = "";
+    txt += angle;
+
+    fAngle = new TGTextEntry(this, txt, IDM_kAngle);
     fAngle->SetAlignment(kTextCenterX);
     fAngle->Move(547-410, fMenu->GetDefaultHeight()+617-10+60);
@@ -1385,5 +1392,5 @@
     {
         // open tpoint file
-        const TString name = MCosy::GetFileName("tpoint/starg_%s.txt");
+        const TString name = MCosy::GetFileName("tpoint", "starg", "txt");
         cout << "TPoint-Starg File ********* " << name << " ********** " << endl;
 
@@ -1451,5 +1458,5 @@
     // Try to find Led in this area
     Leds leds;
-    f.FindStarCircle(leds, (Int_t)SearchCenter.X(), (Int_t)SearchCenter.Y());
+    f.FindStar(leds, (Int_t)SearchCenter.X(), (Int_t)SearchCenter.Y(), true);
 
     // Check whether star found
@@ -1470,5 +1477,5 @@
     if (!fOutRq)
     {
-        const TString name = MCosy::GetFileName("tpoint/starg_roquelamp_%s.txt");
+        const TString name = MCosy::GetFileName("tpoint", "roquelamp", "txt");
         cout << "Starg_RoqueLamp File ********* " << name << " ********** " << endl;
         fOutRq = new ofstream(name);
@@ -1476,8 +1483,6 @@
     }
 
-
     return relroquepos;
 }
-  
 
 ZdAz MStarguider::FindStar(FilterLed &f, FilterLed &f2, Ring &center, MTime &t, Double_t cut, Double_t box, Double_t scalefactor = 1.0)
@@ -1489,5 +1494,5 @@
     // Try to find Led in this area
     Leds leds;
-    f.FindStarCircle(leds, (Int_t)center.GetX(), (Int_t)center.GetY());
+    f.FindStar(leds, (Int_t)center.GetX(), (Int_t)center.GetY(), true);
 
     if (leds.GetEntries()<0)
@@ -1505,5 +1510,4 @@
         return ZdAz(.0,.0);
     }
-
     cout << "Found star @ " << flush;
     star->Print();
@@ -1561,5 +1565,5 @@
         // open tpoint file
         //
-        const TString name = MCosy::GetFileName("tpoint/tpoint_%s.txt");
+        const TString name = MCosy::GetFileName("tpoint", "tpoint", "txt");
         cout << "TPoint-Starg File ********* " << name << " ********** " << endl;
 
@@ -1715,5 +1719,5 @@
     // Find Star at Center---for Tpoint Procedure
     if (fDisplay->IsEntryChecked(IDM_kFindStar))
-        /*ZdAz zdaz =*/ FindStar(f, f2, center, t, 3.5, 70);
+        FindStar(f, f2, center, t, 3.5, 70);
 
     byte zimg[kZOOM*kZOOM];
@@ -1779,6 +1783,4 @@
     {
 
-        XY roquelamp(0,0);
-
         Double_t imageclean = 1.5;
         Double_t boxradius = 60;
@@ -1786,5 +1788,5 @@
 	XY searchcenter(768/2-1,576/2+25);
 
-	roquelamp = FindRoqueLamp(f, f2, sgcenter, t, imageclean, boxradius, searchcenter);
+	XY roquelamp = FindRoqueLamp(f, f2, sgcenter, t, imageclean, boxradius, searchcenter);
 
         if (fOutRq)
@@ -1804,5 +1806,4 @@
 
     // Find Spot on Camera Center in Starguider camera
-
     if (fOperations->IsEntryChecked(IDM_kRoqueLampAna))
     {
@@ -1832,5 +1833,4 @@
         f2.DrawCircle(sgcenter, 115.0, 0x0fa);
     }
-
     // we calculate the offset given by the three ETH Leds visible to
     // the guide camera
@@ -1884,5 +1884,5 @@
 
         // Position around which the circles are drawn.
-        const Ring skycenter(768/2+off.X(), 576/2+off.Y());
+        const Ring skycenter(768/2-off.X(), 576/2+off.Y());
 
 	// There are two corrections to the misspointing
@@ -1896,8 +1896,10 @@
   	    Leds spots;
   	    f.SetBox(230);
-//	    f.SetCut(1.5);
-	    double bright;
+	    f.SetCut(2.5);
+
+            double bright;
   	    f.ExecuteAndMark(spots, 530, 292, bright);
- 	    MString txt;
+
+            MString txt;
  	    txt.Print("Sky Brightness: %.1f", bright);
  	    fSkyBright->SetText(txt);
@@ -1961,5 +1963,5 @@
     {
         f2.DrawCircle(center, 0x0a);
-        f2.DrawCircle(center,   5.0, 
+        f2.DrawCircle(center,   7.0,
 		      fDisplay->IsEntryChecked(IDM_kFindStar)?3:0xb0);
         f2.DrawCircle(center, 115.0, 0x0a);
@@ -2003,5 +2005,5 @@
         if (aa.Az()<0)
             aa.Az(aa.Az()+360);
-        txt.Print("%.1fd/%.1fd (%d, %d)", -aa.Alt(), aa.Az()-180, fDx, fDy);
+        txt.Print("(%d, %d) %.1fd/%.1fd", fDx, fDy, -aa.Alt(), aa.Az()-180);
     }
     else
Index: /trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 7787)
+++ /trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 7788)
@@ -118,4 +118,57 @@
 }
 
+bool MDriveCom::CommandPREPS(TString &str)
+{
+    str = str.Strip(TString::kBoth);
+    if (str.IsNull())
+    {
+        cout << "ERROR - No identifier for preposition (PREPS) given." << endl;
+        return false;
+    }
+    if (str.First(' ')>=0)
+    {
+        cout << "ERROR - PREPS command syntax error (contains whitespaces)." << endl;
+        return false;
+    }
+
+    str.ToLower();
+
+    cout << "CC-COMMAND " << MTime(-1) << " PREPS '" << str << "'" << endl;
+
+    //cout << "MDriveCom - TRACK... start." << endl;
+    fQueue->PostMsg(WM_PREPS, (void*)str.Data(), str.Length()+1);
+    //cout << "MDriveCom - TRACK... done." << endl;
+    return true;
+}
+
+bool MDriveCom::CommandARM(TString &str)
+{
+    str = str.Strip(TString::kBoth);
+    if (str.IsNull())
+    {
+        cout << "ERROR - No identifier for ARM command." << endl;
+        return false;
+    }
+    if (str.First(' ')>=0)
+    {
+        cout << "ERROR - ARM command syntax error (contains whitespaces)." << endl;
+        return false;
+    }
+
+    str.ToLower();
+    if (str!="lock" && str!="unlock")
+    {
+        cout << "ERROR - ARM command syntax error (neither LOCK nor UNLOCK)." << endl;
+        return false;
+    }
+
+    cout << "CC-COMMAND " << MTime(-1) << " ARM '" << str << "'" << endl;
+
+    bool lock = str=="lock";
+
+    fQueue->PostMsg(WM_ARM, &lock, sizeof(lock));
+    return true;
+}
+
 bool MDriveCom::InterpreteCmd(TString cmd, TString str)
 {
@@ -148,8 +201,5 @@
 
     if (cmd==(TString)"PREPS")
-    {
-        cout << "Prepos: " << str << endl;
-        return true;
-    }
+        return CommandPREPS(str);
 
     if (cmd.IsNull() && str.IsNull())
Index: /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h
===================================================================
--- /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 7787)
+++ /trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 7788)
@@ -25,4 +25,6 @@
     bool CommandGRB(TString &str);
     bool CommandZDAZ(TString &str);
+    bool CommandPREPS(TString &str);
+    bool CommandARM(TString &str);
 
 public:
@@ -46,13 +48,2 @@
 
 #endif
-
-
-
-
-
-
-
-
-
-
-
Index: /trunk/MagicSoft/Cosy/videodev/FilterLed.cc
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 7787)
+++ /trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 7788)
@@ -125,43 +125,59 @@
 				     float &my, unsigned int &sum) const
 {
+    //-------------------------------
+    // Improved algorithm:
+    // 1. Look for the largest five-pixel-cross signal inside the box
+    int x0 = TMath::Max(x-box+1,   0);
+    int y0 = TMath::Max(y-box+1,   0);
+
+    int x1 = TMath::Min(x+box+1-1, fW);
+    int y1 = TMath::Min(y+box+1-1, fH);
+
+    int maxx=0;
+    int maxy=0;
+
+    unsigned int max =0;
+    for (int dx=x0; dx<x1; dx++)
+    {
+        for (int dy=y0; dy<y1; dy++)
+        {
+            const unsigned int sumloc =
+                fImg[(dy+0)*fW + (dx-1)] +
+                fImg[(dy+0)*fW + (dx+1)] +
+                fImg[(dy+1)*fW + dx] +
+                fImg[(dy+0)*fW + dx] +
+                fImg[(dy-1)*fW + dx];
+
+            if(sumloc<=max)
+                continue;
+
+            maxx=dx;
+            maxy=dy;
+            max =sum;
+	}
+    }
+
+    // 2. Calculate mean position inside a circle around
+    // the highst cross-signal with radius of 6 pixels.
     unsigned int sumx=0;
     unsigned int sumy=0;
 
-    //-------------------------------
-    // Improved algorithm:
-    // 1. Look for the largest four-pixel signal inside box
-
-    int thissum=0, maxsum=0;
-    int maxx=0, maxy=0;
-    for (int dx=x-box; dx<x+box+1; dx++)
-        for (int dy=y-box; dy<y+box+1; dy++)
-        {
-            thissum = fImg[dy*fW+dx]+fImg[(dy+1)*fW+dx]+
-	      fImg[dy*fW+(dx+1)]+fImg[(dy+1)*fW+(dx+1)];
-	    if(thissum>maxsum)
-	      {
-		maxx=dx;
-		maxy=dy;
-		maxsum=thissum;
-	      }
-	}
-
-    // 2. Calculate mean position inside a circle around
-    // the highst four-pixel signal with radius of 5 pixels.
+    const int rad = 17;
+
+    x0 = TMath::Max(x-box,   maxx-rad);
+    y0 = TMath::Max(y-box,   maxy-rad);
+
+    x1 = TMath::Min(x+box+1, maxx+rad+1);
+    y1 = TMath::Min(y+box+1, maxy+rad+1);
 
     sum=0;
-    for (int dx=x-box; dx<x+box+1; dx++)
-        for (int dy=y-box; dy<y+box+1; dy++)
+    for (int dx=x0; dx<x1; dx++)
+        for (int dy=y0; dy<y1; dy++)
         {
             const byte &m = fImg[dy*fW+dx];
 
-	    // Circle
-	    if(sqrt((dx-maxx)*(dx-maxx)+
-		    (dy-maxy)*(dy-maxy)) <= 6)
-	      {
-		sumx += m*dx;
-		sumy += m*dy;
-		sum  += m;
-	      }
+            sumx += m*dx;
+            sumy += m*dy;
+            sum  += m;
         }
 
@@ -171,6 +187,4 @@
     return (int)my*fW + (int)mx;
 }
-
-
 
 int FilterLed::GetMeanPositionCircle(const int x, const int y, 
@@ -323,13 +337,8 @@
 void FilterLed::Execute(Leds &leds, int xc, int yc, double &bright) const
 {
-    int x0 = xc-fBox;
-    int x1 = xc+fBox;
-    int y0 = yc-fBox;
-    int y1 = yc+fBox;
-
-    if (x0<0) x0=0;
-    if (y0<0) y0=0;
-    if (x1>fW) x1=fW;
-    if (y1>fH) y1=fH;
+    const int x0 = TMath::Max(xc-fBox, 0);
+    const int y0 = TMath::Max(yc-fBox, 0);
+    const int x1 = TMath::Min(xc+fBox, fW);
+    const int y1 = TMath::Min(yc+fBox, fH);
 
     const int wx = x1-x0;
@@ -430,9 +439,7 @@
 
     RemoveTwinsInterpol(leds, first, 5);
-    
-   
-}
-
-void FilterLed::FindStar(Leds &leds, int xc, int yc) const
+}
+
+void FilterLed::FindStar(Leds &leds, int xc, int yc, bool circle) const
 {
     // fBox: radius of the inner (signal) box
@@ -442,13 +449,8 @@
     // Define inner box in which to search the signal
     //
-    int x0 = xc-fBox;
-    int x1 = xc+fBox;
-    int y0 = yc-fBox;
-    int y1 = yc+fBox;
-
-    if (x0<0) x0=0;
-    if (y0<0) y0=0;
-    if (x1>fW) x1=fW;
-    if (y1>fH) y1=fH;
+    const int x0 = TMath::Max(xc-fBox, 0);
+    const int y0 = TMath::Max(yc-fBox, 0);
+    const int x1 = TMath::Min(xc+fBox, fW);
+    const int y1 = TMath::Min(yc+fBox, fH);
 
     //
@@ -459,13 +461,8 @@
     const double sqrt2 = sqrt(2.);
 
-    int xa = xc-(int)rint(fBox*sqrt2);
-    int xb = xc+(int)rint(fBox*sqrt2);
-    int ya = yc-(int)rint(fBox*sqrt2);
-    int yb = yc+(int)rint(fBox*sqrt2);
-
-    if (xa<0) xa=0;
-    if (ya<0) ya=0;
-    if (xb>fW) xb=fW;
-    if (yb>fH) yb=fH;
+    const int xa = TMath::Max(xc-(int)rint(fBox*sqrt2), 0);
+    const int ya = TMath::Max(yc-(int)rint(fBox*sqrt2), 0);
+    const int xb = TMath::Min(xc+(int)rint(fBox*sqrt2), fW);
+    const int yb = TMath::Min(yc+(int)rint(fBox*sqrt2), fH);
 
     //
@@ -538,7 +535,7 @@
     float mx, my;
     unsigned int mag;
-    int pos = GetMeanPosition(xc, yc, fBox-1, mx, my, mag);
-
-    if (pos<0 || pos>=fW*fH && fImg[pos]<sum+fCut*sdev)
+    int pos = circle ? GetMeanPositionCircle(xc, yc, fBox-1, mx, my, mag) : GetMeanPosition(xc, yc, fBox-1, mx, my, mag);
+
+    if (pos<0 || pos>=fW*fH || fImg[pos]<sum+fCut*sdev)
         return;
 
@@ -549,124 +546,4 @@
 }
 
-void FilterLed::FindStarCircle(Leds &leds, int xc, int yc) const
-{
-    // fBox: radius of the inner (signal) box
-    // Radius of the outer box is fBox*sqrt(2)
-
-    //
-    // Define inner box in which to search the signal
-    //
-    int x0 = xc-fBox;
-    int x1 = xc+fBox;
-    int y0 = yc-fBox;
-    int y1 = yc+fBox;
-
-    if (x0<0) x0=0;
-    if (y0<0) y0=0;
-    if (x1>fW) x1=fW;
-    if (y1>fH) y1=fH;
-
-    //
-    // Define outer box (excluding inner box) having almost
-    // the same number of pixels in which the background
-    // is calculated
-    //
-    const double sqrt2 = sqrt(2.);
-
-    int xa = xc-(int)rint(fBox*sqrt2);
-    int xb = xc+(int)rint(fBox*sqrt2);
-    int ya = yc-(int)rint(fBox*sqrt2);
-    int yb = yc+(int)rint(fBox*sqrt2);
-
-    if (xa<0) xa=0;
-    if (ya<0) ya=0;
-    if (xb>fW) xb=fW;
-    if (yb>fH) yb=fH;
-
-    //
-    // Calculate average and sdev for a square
-    // excluding the inner part were we expect
-    // the signal to be.
-    //
-    double sum = 0;
-    double sq  = 0;
-
-    int n=0;
-    for (int x=xa; x<xb; x++)
-        for (int y=ya; y<yb; y++)
-        {
-            if (x>=x0 && x<x1 && y>=y0 && y<y1)
-                continue;
-
-            byte &b = fImg[y*fW+x];
-
-            sum += b;
-            sq  += b*b;
-            n++;
-        }
-
-    sum /= n;
-    sq  /= n;
-
-    // 254 because b<=max and not b<max
-    const double sdev = sqrt(sq-sum*sum);
-    const byte   max  = sum+fCut*sdev>254 ? 254 : (byte)(sum+fCut*sdev);
-
-    //
-    // clean image from noise
-    // (FIXME: A lookup table could accelerate things...
-    //
-    n=0;
-    for (int x=x0; x<x1; x++)
-        for (int y=y0; y<y1; y++)
-        {
-            byte &b = fImg[y*fW+x];
-            if (b<=max)
-                b = 0;
-            else
-                n++;
-        }
-
-    //
-    // Mark the background region
-    //
-    for (int x=xa; x<xb; x+=2)
-    {
-        fImg[ya*fW+x]=0xf0;
-        fImg[yb*fW+x]=0xf0;
-    }
-    for (int y=ya; y<yb; y+=2)
-    {
-        fImg[y*fW+xa]=0xf0;
-        fImg[y*fW+xb]=0xf0;
-    }
-
-    //
-    // Check if any pixel found...
-    //
-    if (n<5)
-        return;
-
-    //
-    // Get the mean position of the star
-    //
-    float mx, my;
-    unsigned int mag;
-
-    //    int pos = GetMeanPosition(xc, yc, fBox-1, mx, my, mag);
-
-    // try new method
-    int pos = GetMeanPositionCircle(xc, yc, fBox-1, mx, my, mag);
-    
-    if (pos<0 || pos>=fW*fH && fImg[pos]<sum+fCut*sdev)
-        return;
-
-    cout << "Mean=" << sum << "  SDev=" << sdev << "  :  ";
-    cout << "Sum/n = " << sum << "/" << n << " = " << (n==0?0:mag/n) << endl;
-
-    leds.Add(mx, my, 0, 0, -2.5*log10((float)mag)+13.7);
-}
-
-
 void FilterLed::Stretch() const
 {
@@ -698,3 +575,2 @@
     }
 }
-
Index: /trunk/MagicSoft/Cosy/videodev/FilterLed.h
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/FilterLed.h	(revision 7787)
+++ /trunk/MagicSoft/Cosy/videodev/FilterLed.h	(revision 7788)
@@ -49,6 +49,5 @@
     void SetBox(int box)   { fBox = box; }
     void SetCut(float cut) { fCut = cut; } 
-    void FindStar(Leds &leds, int xc, int yc) const;
-    void FindStarCircle(Leds &leds, int xc, int yc) const;
+    void FindStar(Leds &leds, int xc, int yc, bool circle=false) const;
     
     void Execute(Leds &leds, int xc, int yc, double &bright) const;
