Index: trunk/MagicSoft/Cosy/main/MCaos.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MCaos.cc	(revision 7788)
+++ trunk/MagicSoft/Cosy/main/MCaos.cc	(revision 7790)
@@ -34,5 +34,4 @@
     cout << " Reading " << name << ":" << endl;
     cout << "------------------------------" << endl;
-
     while (1)
     {
@@ -336,4 +335,16 @@
     leds.Clear();
 
+    /*
+    //the following lines are just to test the new setup
+    static int i=0;
+    i++;
+    i%=2;
+    if (i==0)
+        ReadResources("leds0.txt");
+    else
+        ReadResources("leds1.txt");
+    cout << "LEDs " << i << " " << flush;
+     */
+
     //          img  width height radius sigma
     FilterLed f(img, 768, 576, box, cut);
@@ -378,6 +389,7 @@
 
     const Ring &center = rings.GetCenter();
-    
-    center.Print();
+
+//uncommented for testing
+//    center.Print();
 
     // FIXME!
@@ -423,4 +435,9 @@
     }
 
+    /*
+    //test - give number of rings
+    cout << rings.GetEntries() << " " << flush;
+    */
+
     if (printl)
         leds.Print();
Index: trunk/MagicSoft/Cosy/main/MCosy.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 7788)
+++ trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 7790)
@@ -1537,5 +1537,5 @@
 
         // This is the full qualified date which is part of the name
-        const TString clock = time.GetStringFmt("%y%m%d_%H%M%S");
+        const TString clock = time.GetStringFmt("%Y%m%d_%H%M%S");
 
         // This gives the night in which the date belongs to
Index: trunk/MagicSoft/Cosy/main/MStarguider.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 7788)
+++ trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 7790)
@@ -13,4 +13,6 @@
 #include <TSystem.h>
 #include <TFile.h> // temp writeout of histos
+#include <TSocket.h>
+
 #include "MAstro.h"
 #include "MString.h"
@@ -31,4 +33,5 @@
 
 #include "MGStarg.h" 
+#include "MGNumStars.h"
 #include "TGFrame.h"
 
@@ -90,4 +93,5 @@
     IDM_kPixSize,
     IDM_kAngle,
+    IDM_kCut,
     IDM_kInterpol250,
     IDM_kInterpol125,
@@ -126,4 +130,5 @@
 
     fGStarg->Update(fPos, fD);   
+    fGNumStars->Update(fNumStarsDetected, fNumStarsCorrelated);
  
     return kTRUE;
@@ -237,9 +242,9 @@
     fLimMag->AddEntry("8", IDM_kLimMag8);
     fLimMag->AddEntry("9", IDM_kLimMag9);
-    fLimMag->CheckEntry(IDM_kLimMag8);
+    fLimMag->CheckEntry(IDM_kLimMag9);
     fLimMag->Associate(this);
     fList->Add(fLimMag);
 
-    fSao->SetLimitMag(7.0);
+    fSao->SetLimitMag(9.0);
 
     fInterpol = new MGPopupMenu(p);
@@ -305,5 +310,4 @@
     fMenu->AddPopup("&WritePics",  fWritePictures,   NULL);
     fMenu->AddPopup("&Setup",      fSetup,           NULL);
-
     fMenu->AddPopup("&Operations", fOperations,      NULL);
 
@@ -327,4 +331,6 @@
 
     fGStarg = new MGStarg(this, 235);
+    fGStarg->DrawText("0.75'", "1.50'", "3.00'", "Misspointing [min]");
+    fGStarg->DrawHexagon();
     fGStarg->Move(530,596+5);
     fList->Add(fGStarg);
@@ -335,26 +341,31 @@
     fList->Add(fCRaDec);
 
-    fCZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fCZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+581+20-4);
+    fCZdAz = new MGCoordinates(this, kETypeZdAz, 2);
+    fCZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+597);
     AddFrame(fCZdAz);
     fList->Add(fCZdAz);
 
-    fPZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fPZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+627+2*20-4);
+    fPZdAz = new MGCoordinates(this, kETypeZdAz, 2);
+    fPZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+640);
     AddFrame(fPZdAz);
     fList->Add(fPZdAz);
 
-    fDZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fDZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+673+3*20-4);
+    fDZdAz = new MGCoordinates(this, kETypeZdAz, 2);
+    fDZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+683);
     AddFrame(fDZdAz);
     fList->Add(fDZdAz);
 
-    fSZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fSZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+719+4*20-4);
+    fSZdAz = new MGCoordinates(this, kETypeZdAz, 2);
+    fSZdAz->Move(240+12+28, fMenu->GetDefaultHeight()+795);
     AddFrame(fSZdAz);
     fList->Add(fSZdAz);
 
+    fGNumStars = new MGNumStars(this, 235);
+    fGNumStars->DrawText("Number of stars");
+    fGNumStars->Move(278, fMenu->GetDefaultHeight()+713);
+    fList->Add(fGNumStars);
+
     fTPoint = new TGTextButton(this, "TPoint");
-    fTPoint->Move(4, fMenu->GetDefaultHeight()+722+3*15+15);
+    fTPoint->Move(4, fMenu->GetDefaultHeight()+785);
     fTPoint->AllowStayDown(kTRUE);
     AddFrame(fTPoint);
@@ -362,5 +373,5 @@
 
     fStargTPoint = new TGTextButton(this, "StargTPoint");
-    fStargTPoint->Move(240+12+28, fMenu->GetDefaultHeight()+785);
+    fStargTPoint->Move(170, fMenu->GetDefaultHeight()+785);
     fStargTPoint->AllowStayDown(kTRUE);
     AddFrame(fStargTPoint);
@@ -369,5 +380,5 @@
     fFps = new TGLabel(this, "---fps");
     fFps->SetTextJustify(kTextRight);
-    fFps->Move(650-495, fMenu->GetDefaultHeight()+619+13+60+20+2);
+    fFps->Move(650-495, fMenu->GetDefaultHeight()+714+23);
     AddFrame(fFps);
     fList->Add(fFps);
@@ -385,17 +396,23 @@
     fList->Add(fSkyBright);
 
-    TGLabel *l = new TGLabel(this, "arcsec/pix");
+    TGLabel *l = new TGLabel(this, "deg");
     l->SetTextJustify(kTextLeft);
-    l->Move(606-412, fMenu->GetDefaultHeight()+619+13+60);
+    l->Move(606-412, fMenu->GetDefaultHeight()+669);
     AddFrame(l);
     fList->Add(l);
 
-    l = new TGLabel(this, "deg");
+    l = new TGLabel(this, "arcsec/pix");
     l->SetTextJustify(kTextLeft);
-    l->Move(606-412, fMenu->GetDefaultHeight()+619-10+60);
+    l->Move(606-412, fMenu->GetDefaultHeight()+692);
     AddFrame(l);
     fList->Add(l);
 
-    l = new TGLabel(this, "Telescope pointing at");
+    l = new TGLabel(this, "sigma");
+    l->SetTextJustify(kTextLeft);
+    l->Move(606-412, fMenu->GetDefaultHeight()+715);
+    AddFrame(l);
+    fList->Add(l);
+
+    l = new TGLabel(this, "Zd/Az telescope pointing at");
     l->SetTextJustify(kTextLeft);
     l->Move(240+12+20+7, fMenu->GetDefaultHeight()+584-5);
@@ -403,13 +420,13 @@
     fList->Add(l);
 
-    l = new TGLabel(this, "Starguider position");
+    l = new TGLabel(this, "Zd/Az starguider pointing at");
     l->SetTextJustify(kTextLeft);
-    l->Move(240+12+20+7, fMenu->GetDefaultHeight()+630+20-5);
+    l->Move(240+12+20+7, fMenu->GetDefaultHeight()+630+20-5-23);
     AddFrame(l);
     fList->Add(l);
 
-    l = new TGLabel(this, "Misspointing");
+    l = new TGLabel(this, "Zd/Az misspointing");
     l->SetTextJustify(kTextLeft);
-    l->Move(240+12+20+7, fMenu->GetDefaultHeight()+676+2*20-5);
+    l->Move(240+12+20+7, fMenu->GetDefaultHeight()+676+2*20-5-46);
     AddFrame(l);
     fList->Add(l);
@@ -422,5 +439,19 @@
     fList->Add(l);
 #endif
+
+    // Set input box for rotation angle
+    const Double_t angle = -0.4;
+    fSao->SetRotationAngle(angle);
     
+    TString txt;
+    txt += angle;
+
+    fAngle = new TGTextEntry(this, txt, IDM_kAngle);
+    fAngle->SetAlignment(kTextCenterX);
+    fAngle->Move(547-410, fMenu->GetDefaultHeight()+667);
+    AddFrame(fAngle);
+    fList->Add(fAngle);
+
+    // Set input box for pixel size
     const Double_t pixsize = 48.8; // used to be 23.4
 
@@ -428,24 +459,24 @@
     fSao->SetRotationAngle(0);
 
-    TString txt;
+    txt = "";
     txt += pixsize;
 
     fPixSize = new TGTextEntry(this, txt, IDM_kPixSize);
     fPixSize->SetAlignment(kTextCenterX);
-    fPixSize->Move(547-410, fMenu->GetDefaultHeight()+617+13+60);
+    fPixSize->Move(547-410, fMenu->GetDefaultHeight()+690);
     AddFrame(fPixSize);
     fList->Add(fPixSize);
 
-    const Double_t angle = -0.4;
-    fSao->SetRotationAngle(angle);
+    // Set input box for cleaning cut
+    const Double_t cut = 3.0;
 
     txt = "";
-    txt += angle;
-
-    fAngle = new TGTextEntry(this, txt, IDM_kAngle);
-    fAngle->SetAlignment(kTextCenterX);
-    fAngle->Move(547-410, fMenu->GetDefaultHeight()+617-10+60);
-    AddFrame(fAngle);
-    fList->Add(fAngle);
+    txt += cut;
+
+    fCut = new TGTextEntry(this, txt, IDM_kCut);
+    fCut->SetAlignment(kTextCenterX);
+    fCut->Move(547-410, fMenu->GetDefaultHeight()+713);
+    AddFrame(fCut);
+    fList->Add(fCut);
 
     // TGHorizontal3DLine *fLineSep = new TGHorizontal3DLine(this);
@@ -467,4 +498,8 @@
     fList->Add(fImage);
 
+    const Int_t w = 768;
+    const Int_t h = 840;
+    SetWMSizeHints(w, h, w, h, 1, 1);  // set the smallest and biggest size of the Main frame
+
     //
     // Make everything visible
@@ -477,4 +512,5 @@
     fStargTPoint->UnmapWindow();
     fGStarg->UnmapWindow();
+    fGNumStars->UnmapWindow();
     fPZdAz->UnmapWindow();
     fDZdAz->UnmapWindow();
@@ -503,6 +539,7 @@
       fStatus(MDriveCom::kStandby)
 {
-    
     cout << " #### FIXME: Make MCaos Thread safe!" << endl;
+
+    fAmcSocket = new TSocket("amc", 7307);
 
     fSao = new StarCatalog(obs);
@@ -573,5 +610,30 @@
         delete fOutRq;
 
+    delete fAmcSocket;
+
     cout << "Camera Display destroyed." << endl;
+}
+
+bool MStarguider::SendAmcTrigger(const char *msg)
+{
+    if (!fAmcSocket->IsValid())
+        return false;
+
+    TString txt("TRIGGER ");
+    txt += msg;
+
+    const Int_t len = fAmcSocket->SendRaw(txt.Data(), txt.Length());
+    if (len<0)
+    {
+        cout << "ERROR - Sending Trigger to Amc" << endl;
+        return false;
+    }
+    if (len!=txt.Length())
+    {
+        cout << "Send wrong number (" << len << ") of Bytes to Amc." << endl;
+        return false;
+    }
+
+    return true;
 }
 
@@ -648,4 +710,5 @@
         fDisplay->CheckEntry(IDM_kCatalog);
         fGStarg->MapWindow();
+        fGNumStars->MapWindow();
 
         const Int_t ch0 =
@@ -682,4 +745,5 @@
         fSkyBright->UnmapWindow();
         fGStarg->UnmapWindow();
+        fGNumStars->UnmapWindow();
 
         fChannel->EnableEntry(IDM_kChannel2);
@@ -730,4 +794,5 @@
     fStargTPoint->UnmapWindow();
     fGStarg->UnmapWindow();
+    fGNumStars->UnmapWindow();
     fPZdAz->UnmapWindow();
     fDZdAz->UnmapWindow();
@@ -757,4 +822,10 @@
                     cout << "Rotation Angle changed to " << angle << "deg" << endl;
                     fSao->SetRotationAngle(angle);
+                    return kTRUE;
+                }
+            case IDM_kCut:
+                {
+                    const Float_t cut = atof(fCut->GetText());
+                    cout << "Starguider cleaning level changed to " << cut << " sigma." << endl;
                     return kTRUE;
                 }
@@ -1292,5 +1363,13 @@
                                      ZdAz &d, MTime &t, double &bright, Int_t &num)
 {
-    const Int_t max = leds.GetEntries();
+    num = leds.GetEntries();
+    if (num < 3) //was 1
+    {
+        cout << "Sorry, less than 3 detected spot in FOV!" << endl;
+        if (fStargTPoint->IsDown())
+            fStargTPoint->SetDown(kFALSE);
+        return 0;
+    }
+
     if (stars.GetRealEntries() < 3)
     {
@@ -1300,11 +1379,4 @@
         return 0;
     }
-    if (max < 3) //was 1
-    {
-        cout << "Sorry, less than 3 detected spot in FOV!" << endl;
-        if (fStargTPoint->IsDown())
-            fStargTPoint->SetDown(kFALSE);
-        return 0;
-    }
 
     stars.Sort(); // Sort by magnitude
@@ -1320,11 +1392,9 @@
     TArrayF x, y, mag;
 
-    num = 0;
-
     // FIXME: Is predefined value 3 a good idea?
 
     MStar *star;
     MStarListIter NextStar(&stars);
-    while ((star=NextStar()) && num++<max+3) 
+    while ((star=NextStar()))
     {
         TIter NextSp(&leds);
@@ -1441,5 +1511,5 @@
     *fOutStargTp << " " << d.Zd() << " " << d.Az();
     *fOutStargTp << " " << setprecision(11) << t.GetMjd();
-    *fOutStargTp << " " << max;
+    *fOutStargTp << " " << num;
     *fOutStargTp << " " << bright;
     *fOutStargTp << endl;
@@ -1536,5 +1606,8 @@
 #endif
 
-    // Convert from Pixel to millimeter (1pix=2.9mm)
+    // MAKE SURE THAT THIS VALUE CAN BE SETUP
+    // (Scalefactor describes the difference between the tpoint (=1)
+    //  and the starguider (!=1) camera
+    // Convert from Pixel to millimeter (1pix=2.6mm)
     x *= (2.58427 * scalefactor);
     y *= (2.58427 * scalefactor);
@@ -1544,5 +1617,5 @@
     y /= 1.0713;
 
-    // Calculate Offset
+    // Convert offset from camera plane into local ccordinates
     Double_t dzd, daz;
     ac.GetDiffZdAz(x, y, dzd, daz);
@@ -1719,5 +1792,9 @@
     // Find Star at Center---for Tpoint Procedure
     if (fDisplay->IsEntryChecked(IDM_kFindStar))
+    {
+        // SCALE FACTOR ASSUMED TO BE 70
         FindStar(f, f2, center, t, 3.5, 70);
+        SendAmcTrigger("TPoint");
+    }
 
     byte zimg[kZOOM*kZOOM];
@@ -1751,5 +1828,5 @@
             pos = fCosy->GetPointingPos();
 
-        sgcenter = fStargCaos->Run(img, kFALSE, kFALSE, pos, t, 30, 3.0); // [px]
+        sgcenter = fStargCaos->Run(img, kFALSE, kFALSE, pos, t, 19, 3.0); // [px]
 
         //const Float_t pixsize = atof(fPixSize->GetText()); // [arcsec/px]
@@ -1775,4 +1852,6 @@
 #endif
             fSZdAz->SetCoordinates(star); // Misspointing found from Camera
+
+            SendAmcTrigger("Starguider");
         }
     }
@@ -1891,25 +1970,18 @@
 
 	// Next we evaluate the offset given by the LEDs. This we obtain
-	// in Zd/Az and add it to the tracking error.
+        // in Zd/Az and add it to the tracking error.
 	if (fDisplay->IsEntryChecked(IDM_kStarguider))
         {
+            const Float_t cut = atof(fCut->GetText());
+
   	    Leds spots;
   	    f.SetBox(230);
-	    f.SetCut(2.5);
+	    f.SetCut(cut);
 
             double bright;
   	    f.ExecuteAndMark(spots, 530, 292, bright);
 
-            MString txt;
- 	    txt.Print("Sky Brightness: %.1f", bright);
- 	    fSkyBright->SetText(txt);
-
-            const Bool_t brightnessisvalid = bright<1.75*fLastBright &&
-                bright>30 && bright<110;
-
-            fLastBright = bright;
-
             ULong_t color;
-	    gClient->GetColorByName("Green", color);    
+	    gClient->GetColorByName("Green", color);
             if (bright> 60)
                 gClient->GetColorByName("Yellow", color);
@@ -1920,4 +1992,13 @@
             fSkyBright->SetBackgroundColor(color);
 
+            MString txt;
+ 	    txt.Print("Sky Brightness: %.1f", bright);
+ 	    fSkyBright->SetText(txt);
+
+            const Bool_t brightnessisvalid = bright<1.75*fLastBright &&
+                bright>30 && bright<110;
+
+            fLastBright = bright;
+
             Int_t numstars = 0;
             const Int_t rc = CalcTrackingError(spots, stars, fD, t, bright, numstars);
@@ -1934,4 +2015,7 @@
                                        fSao->GetZdAz(), sgcenter, /*sgcenterzdaz,*/
                                        star, bright, fPos, t);
+
+            fNumStarsDetected   = numstars;
+            fNumStarsCorrelated = rc;
 
             if (fCosy)
@@ -1945,14 +2029,20 @@
         } //kStarguider
 
-        if (centerisvalid)
+        if (centerisvalid && fNumStarsCorrelated>2)
         {
-            f2.DrawCircle(skycenter, 2.0, 0x0a);
-
-            f2.DrawCircle(skycenter, 7.4, 0x0a); //0.1deg
-
-            f2.DrawCircle(skycenter, 2.06*.5*74.0, 0x0a);
-            f2.DrawCircle(skycenter, 2.32*.5*74.0, 0x0a);
-            f2.DrawCircle(skycenter, 3.50*.5*74.0, 0x0a);
-            f2.DrawCircle(skycenter, 3.84*.5*74.0, 0x0a);
+            f2.DrawCircle(skycenter,   2.0,     0x0a);
+
+            const Double_t ap = fSao->GetPixSize()/3600; //[deg/pix]
+
+            f2.DrawCircle(skycenter,  0.10/ap, 0x0a); //0.1deg
+            f2.DrawHexagon(skycenter, 2.06/ap, 0x0a);
+            f2.DrawHexagon(skycenter, 3.50/ap, 0x0a);
+
+            /*
+             f2.DrawCircle(skycenter, 2.06*.5*74.0, 0x0a);
+             f2.DrawCircle(skycenter, 2.32*.5*74.0, 0x0a);
+             f2.DrawCircle(skycenter, 3.50*.5*74.0, 0x0a);
+             f2.DrawCircle(skycenter, 3.84*.5*74.0, 0x0a);
+             */
         }
 
Index: trunk/MagicSoft/Cosy/main/MStarguider.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MStarguider.h	(revision 7788)
+++ trunk/MagicSoft/Cosy/main/MStarguider.h	(revision 7790)
@@ -20,4 +20,5 @@
 class TGButton;
 class TString;
+class TSocket;
 
 class TTimer;
@@ -38,4 +39,5 @@
 // class MStargLeds;
 class MStargHistograms;
+class MGNumStars;
 class MGStarg;
 class FilterLed;
@@ -56,4 +58,5 @@
 
     MGStarg       *fGStarg;
+    MGNumStars    *fGNumStars;
 
     MGPopupMenu   *fDisplay;
@@ -84,4 +87,5 @@
     TGTextEntry   *fPixSize;
     TGTextEntry   *fAngle;
+    TGTextEntry   *fCut;
 
     TGLabel       *fFps;
@@ -95,10 +99,13 @@
     ZdAz          fD;
     AltAz         fAltAzOffsetFromTp;
-    
+
+    Double_t fNumStarsDetected;
+    Double_t fNumStarsCorrelated;
+
     StarCatalog   *fSao;
     MCosy         *fCosy;
     MCaos         *fCaos;
     MCaos         *fStargCaos;
-    //    MStargLeds    *fStargLeds;
+    TSocket       *fAmcSocket;
     MStargHistograms *fStargHistograms;
     
@@ -145,4 +152,6 @@
     void InitGui(Int_t channel);
 
+    bool SendAmcTrigger(const char *msg);
+
     //void OpenFile();
 
