Index: trunk/MagicSoft/Cosy/main/MStarguider.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 9132)
+++ trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 9435)
@@ -9,7 +9,8 @@
 #include <TGraph.h>
 #include <TTimer.h>
+#include <TEnv.h>
 #include <TSystem.h>
 #include <TFile.h> // temp writeout of histos
-#include <TSocket.h>
+//#include <TSocket.h>
 
 #include <TGMenu.h>
@@ -111,6 +112,4 @@
     IDM_kStargAnalysis,
     IDM_kStargCaosFilter,
-    IDM_kStargFindStar,
-    IDM_kRoqueLampAna,
     IDM_kStarguiderMode,
     IDM_kTpointMode
@@ -273,9 +272,7 @@
     fDisplay->AddEntry("Starguider",            IDM_kStarguider);
     fDisplay->AddEntry("Starguider LED Filter", IDM_kStargCaosFilter);
-    fDisplay->AddEntry("Starguider Find Star",  IDM_kStargFindStar);
     fDisplay->AddSeparator();    
     if (channel>=0)
         fDisplay->AddPopup("&Input",   fChannel);
-    fDisplay->DisableEntry(IDM_kStargFindStar);
     fDisplay->CheckEntry(IDM_kStretch);
     fDisplay->Associate(this);
@@ -298,5 +295,4 @@
     fSetup->Associate(this);
 
-    fOperations->AddEntry("Roque Lamp Analysis", IDM_kRoqueLampAna);
     fOperations->AddEntry("Starguider Analysis", IDM_kStargAnalysis);
     fOperations->DisableEntry(IDM_kStargAnalysis);
@@ -400,46 +396,27 @@
     AddFrame(fDZdAzText);
 
-#ifdef EXPERT
-    l = new TGLabel(this, "Mispointing/FindStar (Experts Only!)");
-    l->SetTextJustify(kTextLeft);
-    l->Move(240+12+20, fMenu->GetDefaultHeight()+722+3*20-5);
-    AddFrame(l);
-#endif
-
     // Set input box for rotation angle
-    const Double_t angle = -0.2;
-    fSao->SetRotationAngle(angle);
-
-    TString txt;
-    txt += angle;
-
-    fAngle = new TGTextEntry(this, txt, IDM_kAngle);
+    fAngle = new TGTextEntry(this, "           ", IDM_kAngle);
     fAngle->SetAlignment(kTextCenterX);
     fAngle->Move(547-410, fMenu->GetDefaultHeight()+667);
     AddFrame(fAngle);
 
+    SetRotationAngle(-0.2);
+
     // Set input box for pixel size
-    const Double_t pixsize = 48.9; // used to be 23.4
-
-    fSao->SetPixSize(pixsize);
-
-    txt = "";
-    txt += pixsize;
-
-    fPixSize = new TGTextEntry(this, txt, IDM_kPixSize);
+    fPixSize = new TGTextEntry(this, "           ", IDM_kPixSize);
     fPixSize->SetAlignment(kTextCenterX);
     fPixSize->Move(547-410, fMenu->GetDefaultHeight()+690);
     AddFrame(fPixSize);
 
+    SetPixSize(48.9);
+
     // Set input box for cleaning cut
-    const Double_t cut = 3.0;
-
-    txt = "";
-    txt += cut;
-
-    fCut = new TGTextEntry(this, txt, IDM_kCut);
+    fCut = new TGTextEntry(this, "           ", IDM_kCut);
     fCut->SetAlignment(kTextCenterX);
     fCut->Move(547-410, fMenu->GetDefaultHeight()+713);
     AddFrame(fCut);
+
+    SetCut(3.0);
 
     // TGHorizontal3DLine *fLineSep = new TGHorizontal3DLine(this);
@@ -505,5 +482,7 @@
       fDx((768-kZOOM)/2),
       fDy((512-kZOOM)/2),
-      fStatus(MDriveCom::kStandby)
+      fStatus(MDriveCom::kStandby),
+      fRadius(200),
+      fTPointFromCC(0)
 {
     gLog << warn << " #### FIXME: Make MCaos Thread safe!" << endl;
@@ -513,20 +492,9 @@
     SetCleanup();
 
-    fAmcSocket = new TSocket("amc", 7307);
-
     fSao = new StarCatalog(obs);
     fRaDec = new RaDec(180, 40);
 
-    //    fStargLeds = new MStargLeds;
-    //    fStargLeds->ReadResources();
-
     fCaos = new MCaos;
-    fCaos->ReadResources();
-    fCaos->SetRadii(237.9, 239.9);
-
     fStargCaos = new MCaos;
-    fStargCaos->ReadResources("stargleds.txt");
-    fStargCaos->SetMinNumberRings(3);
-    fStargCaos->SetRadii(158,164);
 
     fStargHistograms = new MStargHistograms();
@@ -539,5 +507,5 @@
     fTime.Now();
 
-    fTimeFromTp.Set(1970,1,1);
+    fTimeFromTp.Clear();
     fAltAzOffsetFromTp = AltAz(-1000,-1000);
                                              
@@ -550,4 +518,45 @@
 }
 
+void MStarguider::SetRotationAngle(Double_t angle)
+{
+    fSao->SetRotationAngle(angle);
+
+    TString txt;
+    txt += angle;
+
+    fAngle->SetText(txt);
+}
+
+void MStarguider::SetPixSize(Double_t size)
+{
+    fSao->SetPixSize(size);
+
+    TString txt;
+    txt += size;
+
+    fPixSize->SetText(txt);
+}
+
+void MStarguider::SetCut(Double_t cut)
+{
+    TString txt;
+    txt += cut;
+
+    fCut->SetText(txt);
+}
+
+void MStarguider::SetupEnv(TEnv &env)
+{
+    fCaos->ReadEnv(env, "TPointLeds", kTRUE);
+    fStargCaos->ReadEnv(env, "StarguiderLeds", kTRUE);
+
+    SetRotationAngle(env.GetValue("Starguider.RotationAngle", fSao->GetRotationAngle()));
+    SetCut(env.GetValue("Starguider.CleaningLevel", atof(fCut->GetText())));
+
+    SetPixSize(env.GetValue("StarguiderLeds.ArcsecPerPixel", fSao->GetPixSize()));
+
+    fRadius = env.GetValue("Leds.Radius", fRadius);
+}
+
 MStarguider::~MStarguider()
 {
@@ -564,5 +573,4 @@
     delete fCaos;
     delete fStargCaos;
-    //    delete fStargLeds;
     delete fStargHistograms;
     delete fSao;
@@ -578,30 +586,5 @@
         delete fOutRq;
 
-    delete fAmcSocket;
-
     gLog << inf2 << "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)
-    {
-        gLog << err << "ERROR - Sending Trigger to Amc" << endl;
-        return false;
-    }
-    if (len!=txt.Length())
-    {
-        gLog << err << "Send wrong number (" << len << ") of Bytes to Amc." << endl;
-        return false;
-    }
-
-    return true;
 }
 
@@ -763,5 +746,4 @@
         SwitchOff(fDisplay, IDM_kStarguider);
         SwitchOff(fDisplay, IDM_kStargCaosFilter);
-        fDisplay->DisableEntry(IDM_kStargFindStar);
     }
     else
@@ -858,21 +840,4 @@
                 return kTRUE;
 
-	   case IDM_kRoqueLampAna:
-  	        Toggle(fOperations, IDM_kRoqueLampAna);
-                if (fOperations->IsEntryChecked(IDM_kRoqueLampAna))
-                    fDisplay->CheckEntry(IDM_kStargCaosFilter);
-                else
-                    fDisplay->UnCheckEntry(IDM_kStargCaosFilter);
-		  
-		return kTRUE;
-
-           case IDM_kStargFindStar:
-	        Toggle(fDisplay, IDM_kStargFindStar);         
-                if (fDisplay->IsEntryChecked(IDM_kStargFindStar))
-                    fSZdAz->MapWindow();
-                else
-                    fSZdAz->UnmapWindow();
-                return kTRUE;
-
             case IDM_kStarguider:
                 Toggle(fDisplay, IDM_kStarguider);
@@ -892,8 +857,6 @@
                     //uncheck not needed items
                     //general
-                    fDisplay->UnCheckEntry(IDM_kStargFindStar);
                     SwitchOff(fDisplay, IDM_kFilter);
                     SwitchOff(fChannel, IDM_kChannel3);
-                    SwitchOff(fOperations, IDM_kRoqueLampAna);
                     SwitchOff(fOperations, IDM_kStargAnalysis);
 
@@ -930,5 +893,4 @@
                     fDisplay->EnableEntry(IDM_kFindStar);
                     fChannel->EnableEntry(IDM_kChannel3);
-                    fOperations->EnableEntry(IDM_kRoqueLampAna);
                 }
                 return kTRUE;
@@ -941,8 +903,6 @@
                     //unchecking not needed items
                     //general
-                    fDisplay->UnCheckEntry(IDM_kStargFindStar);
                     SwitchOff(fDisplay, IDM_kFilter);
                     SwitchOff(fChannel, IDM_kChannel3);
-                    SwitchOff(fOperations, IDM_kRoqueLampAna);
 
                     //from starguider
@@ -981,5 +941,4 @@
                     fChannel->EnableEntry(IDM_kChannel1);
                     fChannel->EnableEntry(IDM_kChannel3);
-                    fOperations->EnableEntry(IDM_kRoqueLampAna);
 
                     //tpoint
@@ -1013,5 +972,4 @@
                 if (fDisplay->IsEntryChecked(IDM_kStargCaosFilter))
                 {
-                    fDisplay->EnableEntry(IDM_kStargFindStar);
                     SwitchOff(fDisplay, IDM_kCaosFilter);
                     SwitchOff(fDisplay, IDM_kFindStar);
@@ -1023,5 +981,4 @@
                     fDisplay->EnableEntry(IDM_kFindStar);
                     fDisplay->EnableEntry(IDM_kCaosFilter);
-                    fDisplay->DisableEntry(IDM_kStargFindStar);
                 }
                 return kTRUE;
@@ -1338,19 +1295,7 @@
     AltAz pos0 = fSao->CalcAltAzFromPix(768/2,    576/2)*kRad2Deg;
     AltAz pos1 = fSao->CalcAltAzFromPix(768/2+mx, 576/2+my)*kRad2Deg;
-/*
-    ofstream fout1("pointingpos.txt");
-    fout1 << setprecision(10) << fSao->GetMjd()-52000 << " ";
-    if (fCosy)
-        fout1 << fCosy->GetPointingPos() << " ";
-    fout1 << -pos1.Alt() << " " << pos1.Az() << endl;
- */
+
     pos1 -= pos0;
-/*
-    ofstream fout2("tracking_error.txt", ios::app);
-    fout2 << setprecision(10) << fSao->GetMjd()-52000 << " ";
-    if (fCosy)
-        fout2 << fCosy->GetPointingPos() << " ";
-    fout2 << -pos1.Alt() << " " << pos1.Az() << endl;
-  */
+
     return ZdAz(-pos1.Alt(), pos1.Az());
 }
@@ -1360,19 +1305,15 @@
 {
     num = leds.GetEntries();
-    //cout << "Num: " << num << endl;
     if (num < 3) //was 1
     {
         gLog << warn << "Sorry, less than 3 detected spot in FOV!" << endl;
-        if (fStargTPoint->IsDown())
-            fStargTPoint->SetDown(kFALSE);
+        fStargTPoint->SetDown(kFALSE);
         return 0;
     }
 
-    //cout << "Cat: " << stars.GetRealEntries() << endl;
     if (stars.GetRealEntries() < 3)
     {
         gLog << warn << "Sorry, less than 3 stars in FOV!" << endl;
-        if (fStargTPoint->IsDown())
-            fStargTPoint->SetDown(kFALSE);
+        fStargTPoint->SetDown(kFALSE);
         return 0;
     }
@@ -1433,19 +1374,10 @@
     d = TrackingError(x, y, mag, numcor);
     if (numcor<1)
+    {
+        fStargTPoint->SetDown(kFALSE);
         return 0;
-
-    //cout << "Cor: " << numcor << endl;
+    }
 
     fDZdAz->SetCoordinates(d);
-
-    //
-    // Calculated offsets
-    //
-
-#ifdef EXPERT
-    // round= floor(x+.5)
-    cout << "Offset-ZdAz: " << d.Zd()*60 << "' / " << d.Az()*60 << "'" << endl;
-    cout << "Offset-ZdAz: " << d.Zd()/360*16384 << " / " << d.Az()/360*16384 << " (SE) " << endl;
-#endif
 
     //
@@ -1476,5 +1408,5 @@
     // Get tracking coordinates
     const XY    xy = fCRaDec->GetCoordinates();
-    const RaDec rd(xy.X()*TMath::DegToRad()*15, xy.Y()*TMath::DegToRad());
+    const RaDec rd(xy.X()*MAstro::HorToRad(), xy.Y()*TMath::DegToRad());
 
     // From the star position in the camera we calculate the Alt/Az
@@ -1485,17 +1417,12 @@
     //correction with offset from last tpoint measurement
     za0 -= fAltAzOffsetFromTp;
-    MTime t2 = fTimeFromTp;
 
     //if the difference between the tpoint and the starguider tpoint
     //is too big, the starguider tpoint is not stored
-    cout << "     mjd difference: " << t.GetMjd()-t2.GetMjd() << endl;
-//        cout << "t: " << setprecision(11) << t.GetMjd() << endl;
-//        cout << "t2: " << setprecision(11) << t2.GetMjd() << endl;
-    if ((t.GetMjd()-t2.GetMjd())>0.001) //1min20sec
-    {
-        cout << "     time difference between tpoint and starguider-tpoint > 1 min *" <<
-            t.GetMjd()-t2.GetMjd() << "s) " << endl;
+    if ((t.GetMjd()-fTimeFromTp.GetMjd())>0.001) //1min20sec
+    {
+        cout << "     time difference between tpoint and starguider-tpoint > 1m20s" << endl;
         cout << "     => starguider tpoint hasn't been stored. " << endl;
-        cout << "     Please repeat whole procedure. " << endl;
+        cout << "        Please repeat whole procedure. " << endl;
         return numcor;
     }
@@ -1503,9 +1430,9 @@
 
     // Write real pointing position
-    cout << "     Alt/Az: " << za0.Alt() << "° " << za0.Az() << "°" << endl;
+    //cout << "     Alt/Az: " << za0.Alt() << "° " << za0.Az() << "°" << endl;
     *fOutStargTp << setprecision(7) << za0.Az() << " " << za0.Alt() << " ";
 
     // Write system pointing position
-    cout << "     SE-Pos: " << 90-cpos.Zd() << "° " << cpos.Az() << "°" << endl;
+    //cout << "     SE-Pos: " << 90-cpos.Zd() << "° " << cpos.Az() << "°" << endl;
     *fOutStargTp << fmod(cpos.Az()+360, 360) << " " << 90-cpos.Zd();
 
@@ -1517,57 +1444,14 @@
     *fOutStargTp << endl;
 
-    fTimeFromTp.Set(1970,1,1);
+    fTimeFromTp.Clear();
 
     return numcor;
 }
 
-XY MStarguider::FindRoqueLamp(FilterLed &f, FilterLed &f2, Ring &CameraCenter, MTime &t, Double_t cut, Int_t box, XY SearchCenter)
-{
-    // Set search Paremeters (FIXME: Get them from user input!)
-    f.SetCut(cut);  // 3.5
-    f.SetBox(box);  // 70
-
-    // Try to find Led in this area
-    Leds leds;
-    f.FindStar(leds, (Int_t)SearchCenter.X(), (Int_t)SearchCenter.Y(), true);
-
-    // Check whether star found
-    Led *star = (Led*)leds.At(0);
-    if (!star || leds.GetEntries()<1)
-        return XY(.0,.0);
-
-//    cout << "Found Roque Lamp @ " << flush;
-    star->Print();
-    f.MarkPoint(star->GetX(), star->GetY(), 500);
-
-//    cout << "RoquePos: " << star->GetX() << "," << star->GetY() << endl;
-
-    XY roquepos(star->GetX(), star->GetY());
-    XY relroquepos(roquepos.X()-CameraCenter.GetX(), roquepos.Y()-CameraCenter.GetY());
-
-    // If no file open: open new Roque Lamp file
-    if (!fOutRq)
-    {
-        const TString name = MCosy::GetFileName("tpoint", "roquelamp", "txt");
-        cout << "Starg_RoqueLamp File ********* " << name << " ********** " << endl;
-        fOutRq = new ofstream(name);
-        *fOutRq << "# Magic Roque Lamp file  " << t << endl;
-    }
-
-    return relroquepos;
-}
-
-ZdAz MStarguider::FindStar(FilterLed &f, FilterLed &f2, Ring &center, MTime &t, Double_t cut, Int_t box, Double_t scalefactor = 1.0)
-{
-    // Set search Paremeters (FIXME: Get them from user input!)
-    f.SetCut(cut);  // 3.5
-    f.SetBox(box);  // 70
-
-    // Try to find Led in this area
+void MStarguider::FindStar(FilterLed &f, FilterLed &f2, Ring &center, MTime &t)
+{
+    // Try to find the star
     Leds leds;
     f.FindStar(leds, (Int_t)center.GetX(), (Int_t)center.GetY(), true);
-
-    if (leds.GetEntries()<0)
-        return ZdAz(0, 0);
 
     // Check whether star found
@@ -1580,11 +1464,12 @@
             gLog << warn << "No star found. Couldn't take a tpoint." << endl;
         }
-        return ZdAz(.0,.0);
-    }
+        return;
+    }
+
     cout << "Found star @ " << flush;
     star->Print();
     f2.MarkPoint(star->GetX(), star->GetY(), 2<<2);
 
-    // Initialize Star Catalog on th camera plane
+    // Initialize Star Catalog on the camera plane
     MGeomCamMagic geom;
     MAstroCamera ac;
@@ -1595,35 +1480,20 @@
 
     // Get tracking coordinates
-    const XY    xy = fCRaDec->GetCoordinates();
-    const RaDec rd(xy.X()*TMath::DegToRad()*15, xy.Y()*TMath::DegToRad());
+    const XY xy = fCRaDec->GetCoordinates();  // [h, deg]
+    const RaDec rd(xy.X()*MAstro::HorToRad(), xy.Y()*TMath::DegToRad());
 
     ac.SetRaDec(rd.Ra(), rd.Dec());
 
+    // Convert from Pixel to millimeter (1pix=2.6mm) [deg/pix / deg/mm = mm/pix]
+    // Correct for abberation.
+    const Double_t conv = fCaos->GetArcsecPerPixel()/3600/geom.GetConvMm2Deg()/ 1.0713;
+
     // Adapt coordinate system (GUIs and humans are counting Y in different directions)
-    Double_t x = star->GetX()-center.GetX();
-    Double_t y = center.GetY()-star->GetY();
-
-#ifdef EXPERT
-    cout << "STAR-Offset: " << MTime(-1) << " dx=" << x << "pix  dy=" << y << "pix" << endl;
-#endif
-
-    // 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);
-
-    // Correct for abberation.
-    x /= 1.0713;
-    y /= 1.0713;
+    const Double_t dx = (star->GetX()-center.GetX())*conv;
+    const Double_t dy = (center.GetY()-star->GetY())*conv;
 
     // Convert offset from camera plane into local ccordinates
     Double_t dzd, daz;
-    ac.GetDiffZdAz(x, y, dzd, daz);
-
-#ifdef EXPERT
-    cout << "STAR-Offset: " << MTime(-1) << " dZd=" << dzd << "d  dAz=" << daz << "d" << endl;
-#endif
+    ac.GetDiffZdAz(dx, dy, dzd, daz);
 
     ZdAz zdaz(dzd,daz);
@@ -1631,5 +1501,6 @@
     // Check TPoint data set request
     if (!fTPoint->IsDown())
-        return zdaz;
+        return;
+
     fTPoint->SetDown(kFALSE);
 
@@ -1641,5 +1512,5 @@
         //
         const TString name = MCosy::GetFileName("tpoint", "tpoint", "txt");
-        cout << "TPoint-Starg File ********* " << name << " ********** " << endl;
+        cout << "TPoint File ********* " << name << " ********** " << endl;
 
         fOutTp = new ofstream(name);
@@ -1652,5 +1523,5 @@
 
     // Output Ra/Dec the drive system thinks that it is currently tracking
-    cout << "TPoint Star: " << xy.X() << "h " << xy.Y() << "°" << endl;
+    //cout << "TPoint Star: " << xy.X() << "h " << xy.Y() << "°" << endl;
 
     // From the star position in the camera we calculate the Alt/Az
@@ -1664,16 +1535,14 @@
     fTimeFromTp=t;
 
-
     // From the Shaftencoders we get the current 'pointing' position
     // as it is seen by the drive system (system pointing position)
-    // FIXME????
-    const ZdAz za1 = fCosy->GetSePos()*TMath::TwoPi();
+    const ZdAz za1 = fCosy->GetSePos()*360; // [deg]
 
     // Write real pointing position
-    cout << "     Alt/Az: " << za0.Alt() << "° " << za0.Az() << "°" << endl;
+    //cout << "     Alt/Az: " << za0.Alt() << "° " << za0.Az() << "°" << endl;
     *fOutTp << setprecision(7) << za0.Az() << " " << za0.Alt() << " ";
 
     // Write system pointing position
-    cout << "     SE-Pos: " << 90-za1.Zd() << "° " << za1.Az() << "°" << endl;
+    //cout << "     SE-Pos: " << 90-za1.Zd() << "° " << za1.Az() << "°" << endl;
     *fOutTp << fmod(za1.Az()+360, 360) << " " << 90-za1.Zd();
 
@@ -1684,5 +1553,5 @@
     *fOutTp << " " << star->GetMag();
     *fOutTp << endl;
-
+/*
     MLog &outrep = *fCosy->GetOutRep();
     if (outrep.Lock("MStarguider::FindStar"))
@@ -1695,10 +1564,10 @@
         outrep << star->GetX() << " " << star->GetY() << " ";
         outrep << center.GetX() << " " << center.GetY() << " ";
-        outrep << x*1.0713/2.58427 << " " << y*1.0713/2.58427 << " " << star->GetMag();
+        outrep << dx/conv << " " << dy/conv << " " << star->GetMag();
         outrep << setprecision(11) << t.GetMjd() << endl;
         outrep.UnLock("MStarguider::FindStar");
-    }
+    }*/
     
-    return zdaz;
+//    return zdaz;
 }
 
@@ -1730,4 +1599,109 @@
 
     return true;
+}
+
+void MStarguider::DrawZoomImage(const byte *img)
+{
+    byte zimg[kZOOM*kZOOM];
+    for (int y=0; y<kZOOM; y++)
+        for (int x=0; x<kZOOM; x++)
+            zimg[x+y*kZOOM] = img[(fDx+(x-kZOOM/2)/2)+(fDy+(y-kZOOM/2)/2)*768];
+
+    fZoomImage->DrawImg(zimg);
+}
+
+void MStarguider::DrawCosyImage(const byte *img)
+{
+    if (!fCosy)
+        return;
+
+    byte simg[(768/2-1)*(576/2-1)];
+    for (int y=0; y<576/2-1; y++)
+        for (int x=0; x<768/2-1; x++)
+            simg[x+y*(768/2-1)] = ((unsigned int)img[2*x+2*y*768]+img[2*x+2*y*768+1]+img[2*x+2*(y+1)*768]+img[2*x+2*(y+1)*768+1])/4;
+
+    fCosy->GetWin()->GetImage()->DrawImg(simg);
+}
+
+void MStarguider::StartStarguider()
+{
+    // Switch to starguider mode
+    cout << " * Switching to Starguider mode" << endl;
+    fMode->UnCheckEntry(IDM_kStarguiderMode);
+    ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStarguiderMode, 0);
+}
+
+Bool_t MStarguider::DoTPoint()
+{
+    if (fTPointFromCC<0)
+        return kTRUE;
+
+    switch (++fTPointFromCC)
+    {
+    case 1:
+        fTimeFromTp.Clear();
+        fNumStarsDetected = 0;
+
+        cout << " * Switching to TPoint mode" << endl;
+        // Switch to tpoint mode
+        fMode->UnCheckEntry(IDM_kTpointMode);
+        ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kTpointMode, 0);
+        return kFALSE;
+
+    case 2:
+        cout << " * Waiting one frame" << endl;
+        // Wait one frame
+        return kFALSE;
+
+    case 3:
+        cout << " * Taking TPoint" << endl;
+        fTPoint->SetDown(); // kTRUE
+        return kTRUE;
+
+    case 4:
+        if (!fTimeFromTp) // TPoint failed
+            break;
+
+        // Switch to starguider mode
+        cout << " * Switching to Starguider mode" << endl;
+        fMode->UnCheckEntry(IDM_kStarguiderMode);
+        ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStarguiderMode, 0);
+        return kFALSE;
+
+    case 5:
+        cout << " * Waiting one frame" << endl;
+        // Wait one frame
+        return kFALSE;
+
+    case 6:
+        cout << " * Taking Starguider TPoint" << endl;
+        fStargTPoint->SetDown(); // kTRUE
+        return kTRUE;
+
+    case 7:
+        cout << " * Send Report" << endl;
+        break;
+    }
+
+    // Send report
+    fTPointFromCC = -1;
+
+    if (!fCosy)
+        return kTRUE;
+
+    MDriveCom *com = fCosy->GetDriveCom();
+    if (!com)
+        return kTRUE;
+
+    // nominalaz, nominalel, realaz, realel, nomra, nomdec,
+    // diffaz, diffel, mjd, numleds, artmag
+
+    //fTimeFromTp.Clear();
+    //fStatus==MDriveCom::kMonitoring
+    //fNumStarsDetected   = numstars;
+    //fNumStarsCorrelated = rc;
+    com->SendTPoint(fNumStarsCorrelated>0);
+
+    return kTRUE;
 }
 
@@ -1778,4 +1752,9 @@
         f.Execute();
 
+    DoTPoint();
+
+    Int_t numleds = 0;
+    Int_t numrings = 0;
+
     // Find Center of Camera for Caos and Tpoints
     Ring center(768/2, 576/2);
@@ -1787,7 +1766,9 @@
         if (fCosy)
             pos = fCosy->GetPointingPos();
-        center = fCaos->Run(img, printl, printr, pos, t, 19, 3.0);
-	cout << "Caos Filter Camera center position: " << center.GetX() << " " << center.GetY() << " (R=" << center.GetR() << ")" << endl;
-
+
+        center = fCaos->Run(img, printl, printr, pos, t);
+
+        numleds  = fCaos->GetNumDetectedLEDs();
+        numrings = fCaos->GetNumDetectedRings();
     }
 
@@ -1796,33 +1777,18 @@
         center.GetX()>0 && center.GetY()>0)
     {
-        // SCALE FACTOR ASSUMED TO BE 70
-        FindStar(f, f2, center, t, 3/*3.5*/, 70);
-        SendAmcTrigger("TPoint");
-    }
-
-    byte zimg[kZOOM*kZOOM];
-    for (int y=0; y<kZOOM; y++)
-        for (int x=0; x<kZOOM; x++)
-            zimg[x+y*kZOOM] = img[(fDx+(x-kZOOM/2)/2)+(fDy+(y-kZOOM/2)/2)*768];
-
-    fZoomImage->DrawImg(zimg);
-
-    if (fCosy)
-    {
-	byte simg[(768/2-1)*(576/2-1)];
-	for (int y=0; y<576/2-1; y++)
-	    for (int x=0; x<768/2-1; x++)
-		simg[x+y*(768/2-1)] = ((unsigned int)img[2*x+2*y*768]+img[2*x+2*y*768+1]+img[2*x+2*(y+1)*768]+img[2*x+2*(y+1)*768+1])/4;	
-
-	fCosy->GetWin()->GetImage()->DrawImg(simg);
-    }
+        // Set search Paremeters (FIXME: Get them from user input!)
+        f.SetCut(3.0);
+        f.SetBox(70);
+
+        FindStar(f, f2, center, t);
+    }
+
+    DrawZoomImage(img);
+    DrawCosyImage(img);
+
+    // Position corresponding to the camera center (53.2, 293.6)
+    Ring sgcenter(53.2, 293.6); // Center of camera in SG picture [px]
 
     // Find Center of Camera in Starfield Camera picture 
-
-    Ring sgcenter(53.2, 293.6); // Center of camera in SG picture [px]
-    //ZdAz sgcenterzdaz(0, 0);    // Center of camera in SG picture [deg]
-    //                            // (0,0)_deg is at (53.2, 293.6)_px
-    ZdAz star(0, 0);            // Star on curtain in [deg]
-
     if (fDisplay->IsEntryChecked(IDM_kStargCaosFilter))
     {
@@ -1831,99 +1797,9 @@
             pos = fCosy->GetPointingPos();
 
-        sgcenter = fStargCaos->Run(img, kFALSE, kFALSE, pos, t, 19, 3.0); // [px]
-
-        //const Float_t pixsize = atof(fPixSize->GetText()); // [arcsec/px]
-
-        // BE CAREFULL: This transformation is WRONG. It is just
-        // a transformation of units, but this implies, that the
-        // coordiante axis in both units look the same. This is
-        // wrong exspecially near the zenith were az-lines are highly
-        // curved around the zenith!
-        //sgcenterzdaz.Zd((sgcenter.GetY()-293.6) * pixsize /3600 );
-	//sgcenterzdaz.Az((sgcenter.GetX()-53.2)  * pixsize /3600 );
-#ifdef EXPERT
-	cout << "- LEDs imply offset of Zd=" 
-	     << sgcenter.GetX()-53.2 << "pix Az=" 
-	     << sgcenter.GetY()-293.6<< "pix" << endl; 	
-#endif
-        if (fDisplay->IsEntryChecked(IDM_kStargFindStar) &&
-            sgcenter.GetX()>0 && sgcenter.GetY()>0)
-        {
-            star = FindStar(f, f2, sgcenter, t, 4.5, 30, 267/161.9); // [deg]
-#ifdef EXPERT
-            cout << "- Star is found to be off Zd=" << star.Zd()*60 << "' Az="
-                << star.Az()*60 << "'" << endl;
-#endif
-            fSZdAz->SetCoordinates(star); // Mispointing found from Camera
-
-            SendAmcTrigger("Starguider");
-        }
-    }
-
-// Find Roque Lamp
-
-    if (fOperations->IsEntryChecked(IDM_kRoqueLampAna))
-    {
-
-        Double_t imageclean = 1.5;
-        Int_t    boxradius = 60;
-	//Double_t scalefactor = 1;
-	XY searchcenter(768/2-1,576/2+25);
-
-	XY roquelamp = FindRoqueLamp(f, f2, sgcenter, t, imageclean, boxradius, searchcenter);
-
-        if (fOutRq)
-        {
-            ZdAz pos = fCosy->GetPointingPos();
-
-            *fOutRq << "RoqueLampDirect:    " << MTime(-1) << "  "
-                << pos.Zd() << " " << pos.Az() << "   "
-                << roquelamp.X() << " " << roquelamp.Y() << endl;
-        }
-
-        cout << "Starguider Camera Center: " << sgcenter.GetX() << "," << sgcenter.GetY() << endl;
-	cout << ">=>=>=> Roque Lamp found at:         >=>=>=> (" << roquelamp.X() << "," 
-	     << roquelamp.Y() << ") <=<=<=<" << endl;
-
-    }
-
-    // Find Spot on Camera Center in Starguider camera
-    if (fOperations->IsEntryChecked(IDM_kRoqueLampAna))
-    {
-        XY cameraspot(0,0);
-
-        Double_t imageclean = 5;
-	Int_t    boxradius = 60;
-	//Double_t scalefactor = 1;
-	//	XY searchcenter(sgcenter.GetX(),sgcenter.GetY());
-	XY searchcenter(60.,290.);
-
-	cameraspot = FindRoqueLamp(f, f2, sgcenter, t, imageclean, boxradius, searchcenter);
-
-        if (fOutRq)
-        {
-            ZdAz pos = fCosy->GetPointingPos();
-
-            *fOutRq << "RoqueLampReflected: "  << MTime(-1) << "  "
-                << pos.Zd() << " " << pos.Az() << "   "
-                << cameraspot.X() << " " << cameraspot.Y() << endl;
-        }
-
-        cout << ">>>>> Spot on Magic camera found at: >>>>> (" << cameraspot.X() << ","
-	     << cameraspot.Y() << ") <<<<<" << endl;
-
-        f2.DrawCircle(sgcenter, 5.0, 0x0fa);
-        f2.DrawCircle(sgcenter, 115.0, 0x0fa);
-    }
-    // we calculate the offset given by the three ETH Leds visible to
-    // the guide camera
-    // This is an (inferior, obsolete) alternative to the StarCaosFilter
-    // Led offset;
-    // if (fDisplay->IsEntryChecked(IDM_kStargLEDFilter))	
-    //	fStargLeds->Run(img,offset);
-
-    // Position corresponding to the camera center (53.2, 293.6)
-    //Ring skycenter(392, 318);
-    // MStarList spots;
+        sgcenter = fStargCaos->Run(img, kFALSE, kFALSE, pos, t); // [px]
+
+        numleds  = fStargCaos->GetNumDetectedLEDs();
+        numrings = fStargCaos->GetNumDetectedRings();
+    }
 
     // we obtain a list of stars in the FOV from the SAO catalog
@@ -1932,6 +1808,6 @@
         MTime time(*tm);
 
-	XY xy = fCRaDec->GetCoordinates();  //[h,  deg]
-	fRaDec->Set(xy.X()*360/24, xy.Y()); //[deg,deg]
+        XY xy = fCRaDec->GetCoordinates();  //[h,  deg]
+	fRaDec->Set(xy.X()*15, xy.Y());     //[deg,deg]
 
 	UpdatePosZoom();
@@ -1941,14 +1817,7 @@
   	fCZdAz->SetCoordinates(fSao->GetZdAz());
 
-  	MStarList stars;
     	fSao->SetBox(230); // Region of interest around center
 
-        // very careful: If center of camera cannot be determined
-	// sgcenter jumps to (0,0)  
-
-        //Please never change this offsets!!!
-        // 53.2 and 293.6 are the "preliminary" camera center
-        // -9 and 28.5 are the offsets of the pointing position in the sky
-
+        // If center of camera cannot be determined sgcenter is (0,0)
         const Bool_t centerisvalid = sgcenter.GetX()>0 && sgcenter.GetY()>0;
 //        if (centerisvalid)
@@ -1956,11 +1825,15 @@
 //                            sgcenter.GetY() - 293.6);
 
+	// We determine the ideal starfield using camera sagging info
+        // from the LEDs
+        //Please never change this offsets!!!
+        // 53.2 and 293.6 are the "preliminary" camera center
+        // -9 and 28.5 are the offsets of the pointing position in the sky
+        const XY off(sgcenter.GetX()- 53.2-9,
+                     sgcenter.GetY()-293.6+28.5);
+
     	// we obtain stars in the effective star FOV and draw them.
     	// coordinates are video frame coords.
-	// We determine the ideal starfield using camera sagging info
-        // from the LEDs
-        const XY off(sgcenter.GetX()- 53.2-9,
-                     sgcenter.GetY()-293.6+28.5);
-
+  	MStarList stars;
         fSao->CalcStars(stars, 530, 292, TMath::FloorNint(off.X()), TMath::FloorNint(off.Y()));
 	fSao->DrawStars(stars, cimg);
@@ -2015,7 +1888,6 @@
 
             if (fOperations->IsEntryChecked(IDM_kStargAnalysis))
-                fStargHistograms->Fill(spots, stars, fD,
-                                       fSao->GetZdAz(), sgcenter, /*sgcenterzdaz,*/
-                                       star, bright, fPos, t);
+                fStargHistograms->Fill(spots, stars, fD, fSao->GetZdAz(),
+                                       sgcenter, bright, fPos, t);
 
             fNumStarsDetected   = numstars;
@@ -2028,5 +1900,5 @@
                     com->SendStargReport(fStatus, fD, fSao->GetZdAz(),
                                          sgcenter, numstars, rc, bright,
-                                         time.GetMjd(), 0, 0);    // Report
+                                         time.GetMjd(), numleds, numrings);    // Report
             }
 
@@ -2068,6 +1940,5 @@
     if (fDisplay->IsEntryChecked(IDM_kCaosFilter) ||
         fDisplay->IsEntryChecked(IDM_kCatalog)    ||
-        fDisplay->IsEntryChecked(IDM_kFindStar)   ||
-        fOperations->IsEntryChecked(IDM_kRoqueLampAna))
+        fDisplay->IsEntryChecked(IDM_kFindStar))
         fImage->DrawColImg(img, cimg);
     else
