Index: trunk/FACT++/drive/MStarguider.cc
===================================================================
--- trunk/FACT++/drive/MStarguider.cc	(revision 18626)
+++ trunk/FACT++/drive/MStarguider.cc	(revision 18627)
@@ -4,13 +4,14 @@
 #include "MStarguider.h"
 
-#include <fstream>    // ifstream
-
-#include <TH2F.h>
-#include <TGraph.h>
-#include <TTimer.h>
+#ifdef HAVE_PNG
+#include <png.h>
+#endif
+#include <iostream>
+#include <fstream>
+
+#include <math.h>
+
 #include <TEnv.h>
 #include <TSystem.h>
-#include <TFile.h> // temp writeout of histos
-//#include <TSocket.h>
 
 #include <TGMenu.h>
@@ -20,40 +21,13 @@
 #include <TGTextEntry.h>
 #include <TGLayout.h>
-#include "MString.h"
-
-#include "MLog.h"
-#include "MLogManip.h"
-
-#include "MAstro.h"
-#include "MString.h"
-
-
-#include "MCosy.h"
+
 #include "MCaos.h"
-// #include "MStargLeds.h"
-#include "MStargHistograms.h"
-#include "MDriveCom.h"
-
-#include "MGStarg.h" 
-#include "MGNumStars.h"
-
 #include "MGImage.h"
-#include "MGCoordinates.h"
-
 #include "Camera.h"
-#include "PngReader.h"
-
+#include "../src/DimSetup.h"
 #include "Led.h"
-#include "Writer.h"
+//#include "Writer.h"
 #include "FilterLed.h"
-#include "MStarList.h"
-#include "CaosFilter.h"
-#include "StarCatalog.h"
-#include "MGeomCamMagic.h"
-#include "MAstroCamera.h"
-
-#include "MGCosy.h"
-
-ClassImp(MStarguider);
+//#include "CaosFilter.h"
 
 using namespace std;
@@ -63,35 +37,8 @@
     IDM_kFindStar,
     IDM_kCaosFilter,
-    IDM_kCatalog,
     IDM_kStarguider,
-    IDM_kStart,
-    IDM_kStop,
-    IDM_kFileType,
-    IDM_kPPM,
-    IDM_kPNG,
-    IDM_kOnce,
     IDM_kStretch,
     IDM_kInput,
-    IDM_kChannel1,
-    IDM_kChannel2,
-    IDM_kChannel3,
-    IDM_kContinous,
-    IDM_kRate25ps,
-    IDM_kRate5ps,
-    IDM_kRate1s,
-    IDM_kRate5s,
-    IDM_kRate30s,
-    IDM_kRate1m,
-    IDM_kRate5m,
     IDM_kSetup,
-    IDM_kLimMag3,
-    IDM_kLimMag4,
-    IDM_kLimMag5,
-    IDM_kLimMag6,
-    IDM_kLimMag7,
-    IDM_kLimMag8,
-    IDM_kLimMag9,
-    IDM_kPixSize,
-    IDM_kAngle,
     IDM_kCut,
     IDM_kInterpol250,
@@ -103,32 +50,15 @@
     IDM_kInterpol2,
     IDM_kInterpol1,
-    IDM_kCaosPrintRings,
-    IDM_kCaosPrintLeds,
-    IDM_kCaosAnalStart,
-    IDM_kCaosAnalStop,
+    //IDM_kCaosPrintRings,
+    //IDM_kCaosPrintLeds,
     IDM_kCaosWriteStart,
     IDM_kCaosWriteStop,
     IDM_kResetHistograms,
-    IDM_kStargAnalysis,
-    IDM_kStargCaosFilter,
-    IDM_kStarguiderMode,
-    IDM_kTpointMode
-
 };
 
-Bool_t MStarguider::HandleTimer(TTimer *t)
+Bool_t MStarguider::HandleTimer(TTimer *)
 {
     if (IsMapped())
-    {
         fImage->DoRedraw();
-        fZoomImage->DoRedraw();
-    }
-
-    if (fCosy && fCosy->GetWin()->IsMapped())
-        fCosy->GetWin()->GetImage()->DoRedraw();
-
-    fGStarg->Update(fPos, fD);   
-    if (fDisplay->IsEntryChecked(IDM_kStarguider))
-        fGNumStars->Update(fNumStarsDetected, fNumStarsCorrelated);
  
     return kTRUE;
@@ -136,24 +66,25 @@
 
 #define kZOOM 96
-
+/*
 XY MStarguider::GetCoordinates() const
 {
     return fPZdAz->GetCoordinates();
 }
-
-void MStarguider::InitGui(Int_t channel)
-{
-    fList = new MGList;
-    fList->SetOwner();
-
-    const TGWindow *p=gClient->GetRoot();
+*/
+void MStarguider::InitGui(Int_t)
+{
+    //fList = new MGList;
+    //fList->SetOwner();
+
+/*
+ const TGWindow *p=gClient->GetRoot();
 
     fChannel = new TGPopupMenu(p);
     fChannel->AddEntry("Starfield Camera", IDM_kChannel1);
     fChannel->AddEntry("TPoint Camera",    IDM_kChannel2);
-    fChannel->AddEntry("Read from File",   IDM_kChannel3);
-    if (channel<0)
-        fChannel->CheckEntry(IDM_kChannel3);
-    else
+    //fChannel->AddEntry("Read from File",   IDM_kChannel3);
+    //if (channel<0)
+    //    fChannel->CheckEntry(IDM_kChannel3);
+    //else
         fChannel->CheckEntry(channel==0?IDM_kChannel1:IDM_kChannel2);
     fChannel->Associate(this);
@@ -199,6 +130,8 @@
     fLimMag->Associate(this);
     fList->Add(fLimMag);
-
-    fSao->SetLimitMag(9.0);
+    */
+    //fSao->SetLimitMag(9.0);
+
+    const TGWindow *p=gClient->GetRoot();
 
     fInterpol = new TGPopupMenu(p);
@@ -212,8 +145,8 @@
     fInterpol->AddEntry("Off", IDM_kInterpol1);
     fInterpol->Associate(this);
-    fList->Add(fInterpol);
+    //fList->Add(fInterpol);
 
     TString disp=gVirtualX->DisplayName();
-    gLog << all << "Display: " << disp << endl;
+    cout << "Display: " << disp << endl;
     if (disp.First(':')>=0)
         disp=disp(0, disp.First(':'));
@@ -221,6 +154,6 @@
     if (disp.IsNull() || disp==(TString)"localhost")
     {
-        fInterpol->CheckEntry(IDM_kInterpol5);
-        fIntRate = 50;
+        fInterpol->CheckEntry(IDM_kInterpol25);
+        fIntRate = 25;
     }
     else
@@ -230,4 +163,5 @@
     }
 
+/*
     fCaosPrint = new TGPopupMenu(p);
     fCaosPrint->AddEntry("&Leds",  IDM_kCaosPrintLeds);
@@ -249,13 +183,13 @@
     fCaosAnalyse->Associate(this);
     fList->Add(fCaosAnalyse);
-
+*/
     fMenu = new TGMenuBar(this, 0, 0, kHorizontalFrame);
     fDisplay       = fMenu->AddPopup("&Display");
-    fMode          = fMenu->AddPopup("&Mode");
-    fWritePictures = fMenu->AddPopup("&WritePics");
+    //fMode          = fMenu->AddPopup("&Mode");
+    //fWritePictures = fMenu->AddPopup("&WritePics");
     fSetup         = fMenu->AddPopup("&Setup");
-    fOperations    = fMenu->AddPopup("&Operations");
+    //fOperations    = fMenu->AddPopup("&Operations");
     fMenu->Resize(fMenu->GetDefaultSize());
-    AddFrame(fMenu); 
+    AddFrame(fMenu);
 
     //
@@ -268,47 +202,37 @@
     fDisplay->AddEntry("Find &Star",            IDM_kFindStar);
     fDisplay->AddEntry("C&aos Filter",          IDM_kCaosFilter);
-    fDisplay->AddSeparator();
-    fDisplay->AddEntry("SAO &Catalog",          IDM_kCatalog);
-    fDisplay->AddEntry("Starguider",            IDM_kStarguider);
-    fDisplay->AddEntry("Starguider LED Filter", IDM_kStargCaosFilter);
-    fDisplay->AddSeparator();    
-    if (channel>=0)
-        fDisplay->AddPopup("&Input",   fChannel);
-    fDisplay->CheckEntry(IDM_kStretch);
+    //fDisplay->AddSeparator();
+    //if (channel>=0)
+    //    fDisplay->AddPopup("&Input",   fChannel);
+    // fDisplay->CheckEntry(IDM_kStretch);
+    fDisplay->CheckEntry(IDM_kFindStar);
+    fDisplay->CheckEntry(IDM_kCaosFilter);
     fDisplay->Associate(this);
 
-    fMode->AddEntry("Starguider", IDM_kStarguiderMode);
-    fMode->AddEntry("Tpoint",     IDM_kTpointMode);
-    fMode->Associate(this);
-
+    //fMode->AddEntry("Tpoint",     IDM_kTpointMode);
+    //fMode->Associate(this);
+/*
     fWritePictures->AddEntry("&Start",      IDM_kStart);
     fWritePictures->AddEntry("Sto&p",       IDM_kStop);
     fWritePictures->AddSeparator();
-    fWritePictures->AddPopup("File &Type",  fFileType);
+    //fWritePictures->AddPopup("File &Type",  fFileType);
     fWritePictures->AddPopup("&Write Type", fWriteType);
     fWritePictures->AddPopup("Write &Rate", fWriteRate);
     fWritePictures->DisableEntry(IDM_kStop);
     fWritePictures->Associate(this);
-
-    fSetup->AddPopup("Lim. &Magnitude",      fLimMag);
+    */
     fSetup->AddPopup("Disp. &Interpolation", fInterpol);
     fSetup->Associate(this);
 
-    fOperations->AddEntry("Starguider Analysis", IDM_kStargAnalysis);
-    fOperations->DisableEntry(IDM_kStargAnalysis);
-    fOperations->Associate(this);
-
+/*
     fCaOs = new TGPopupMenu(p);
-    fCaOs->AddPopup("&Write",   fCaosWrite);
+    //fCaOs->AddPopup("&Write",   fCaosWrite);
     fCaOs->AddPopup("&Print",   fCaosPrint);
-    fCaOs->AddPopup("&Analyse", fCaosAnalyse);
+    //fCaOs->AddPopup("&Analyse", fCaosAnalyse);
     fCaOs->Associate(this);
     fList->Add(fCaOs);
-
-    fGStarg = new MGStarg(this, 235);
-    fGStarg->Move(530,596+5);
-    fList->Add(fGStarg);
-
+*/
     //RA,Dec for catalog
+    /*
     fCRaDec = new MGCoordinates(this, kETypeRaDec);
     fCRaDec->Move(4, fMenu->GetDefaultHeight()+584);
@@ -344,12 +268,11 @@
     fTPoint->AllowStayDown(kTRUE);
     AddFrame(fTPoint);
-    /*
-     fStargTPoint = new TGTextButton(this, "StargTPoint");
+
+    fStargTPoint = new TGTextButton(this, "StargTPoint");
      fStargTPoint->Move(170, fMenu->GetDefaultHeight()+785);
      fStargTPoint->AllowStayDown(kTRUE);
      AddFrame(fStargTPoint);
-    */
-
-    fFps = new TGLabel(this, "---fps");
+
+     fFps = new TGLabel(this, "---fps");
     fFps->SetTextJustify(kTextRight);
     fFps->Move(650-495, fMenu->GetDefaultHeight()+714+23);
@@ -395,6 +318,7 @@
     fDZdAzText->Move(240+12+20+7, fMenu->GetDefaultHeight()+676+2*20-5-46);
     AddFrame(fDZdAzText);
-
+*/
     // Set input box for rotation angle
+    /*
     fAngle = new TGTextEntry(this, "           ", IDM_kAngle);
     fAngle->SetAlignment(kTextCenterX);
@@ -402,5 +326,5 @@
     AddFrame(fAngle);
 
-    SetRotationAngle(-0.2);
+    //SetRotationAngle(-0.2);
 
     // Set input box for pixel size
@@ -409,14 +333,14 @@
     fPixSize->Move(547-410, fMenu->GetDefaultHeight()+690);
     AddFrame(fPixSize);
-
-    SetPixSize(48.9);
+    */
+    //SetPixSize(48.9);
 
     // Set input box for cleaning cut
-    fCut = new TGTextEntry(this, "           ", IDM_kCut);
-    fCut->SetAlignment(kTextCenterX);
-    fCut->Move(547-410, fMenu->GetDefaultHeight()+713);
-    AddFrame(fCut);
-
-    SetCut(3.0);
+    //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);
@@ -426,10 +350,10 @@
     //
     // Create Image Display
-    //
+    /*
     fZoomImage = new MGImage(this, kZOOM, kZOOM);
     // fZoomImage->Move(768-kZOOM-2, 700-kZOOM-2);
     fZoomImage->Move(4, 700-kZOOM-2+85);
     AddFrame(fZoomImage);
-
+    */
     fImage = new MGImage(this, 768, 576);
     fImage->Move(0, fMenu->GetDefaultHeight());
@@ -437,5 +361,5 @@
 
     const Int_t w = 768;
-    const Int_t h = 840;
+    const Int_t h = 576;
     SetWMSizeHints(w, h, w, h, 1, 1);  // set the smallest and biggest size of the Main frame
 
@@ -443,21 +367,21 @@
     // Make everything visible
     //
-    SetWindowName("MStarguider Main Window");
-    SetIconName("MStarguider");
+    SetWindowName("TPoint Main Window");
+    SetIconName("TPoint");
 
     MapSubwindows();
-    fTPoint->UnmapWindow();
+    //fTPoint->UnmapWindow();
     //fStargTPoint->UnmapWindow();
-    fGStarg->UnmapWindow();
-    fGNumStars->UnmapWindow();
-    fCRaDec->UnmapWindow();
-    fCZdAz->UnmapWindow();
-    fCZdAzText->UnmapWindow();
-    fPZdAz->UnmapWindow();
-    fPZdAzText->UnmapWindow();
-    fDZdAz->UnmapWindow();
-    fDZdAzText->UnmapWindow();
-    fSZdAz->UnmapWindow();
-    fSkyBright->UnmapWindow();
+    //fGStarg->UnmapWindow();
+    //fGNumStars->UnmapWindow();
+    //fCRaDec->UnmapWindow();
+    //fCZdAz->UnmapWindow();
+    //fCZdAzText->UnmapWindow();
+    //fPZdAz->UnmapWindow();
+    //fPZdAzText->UnmapWindow();
+    //fDZdAz->UnmapWindow();
+    //fDZdAzText->UnmapWindow();
+    //fSZdAz->UnmapWindow();
+    //fSkyBright->UnmapWindow();
     MapWindow();
 
@@ -472,20 +396,11 @@
 }
 
-MStarguider::MStarguider(MObservatory::LocationName_t obs, Int_t channel)
-    : TGMainFrame(gClient->GetRoot(), 768, 840), 
-      fNumStarsDetected(0),
-      fNumStarsCorrelated(0),
-      fCosy(NULL),
-      fOutTp(0),
-      fOutStargTp(0),
-//      fOutRq(0),
-      fDx((768-kZOOM)/2),
-      fDy((512-kZOOM)/2),
-      fStatus(MDriveCom::kStandby),
-      fRadius(200),
-      fTPointFromCC(-1),
-      fFindStarCut(3.0), fFindStarBox(70)
-{
-    gLog << warn << " #### FIXME: Make MCaos Thread safe!" << endl;
+MStarguider::MStarguider(Int_t channel) : TGMainFrame(gClient->GetRoot(), 768, 840),
+fDimData("TPOINT/DATA", "D:11", (void*)NULL, 0),
+fDimTPoint("TPOINT/EXECUTE", "", this),
+fDimScreenshot("TPOINT/SCREENSHOT", "B:1;C", this),
+fRadius(200), fFindStarCut(2.), fFindStarBox(30), fTPointMode(0)
+{
+    cout << " #### FIXME: Make MCaos Thread safe!" << endl;
 
     // This means that all objects added with AddFrame are deleted
@@ -493,11 +408,6 @@
     SetCleanup();
 
-    fSao = new StarCatalog(obs);
-    fRaDec = new RaDec(180, 40);
-
     fCaos = new MCaos;
-    fStargCaos = new MCaos;
-
-    fStargHistograms = new MStargHistograms();
+    fCaos->ReadResources("leds_fact.txt");
 
     InitGui(channel);
@@ -506,54 +416,42 @@
     fTimer->TurnOn();
 
-    fTime.Now();
-
-    fTimeFromTp.Clear();
-    fAltAzOffsetFromTp = AltAz(-1000,-1000);
-                                             
     gVirtualX->GrabButton(fId, kButton2, 0, 0, 0, 0, kTRUE);
 
-    if (channel<0)
-        fGetter = new PngReader(*this);
-    else
-        fGetter = new Camera(*this, channel);
-}
-
-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);
-}
-
+    fGetter = new Camera(*this, channel);
+
+    DimClient::setNoDataCopy();
+    DimServer::start("TPOINT");
+}
+
+MStarguider::~MStarguider()
+{
+    DimServer::stop();
+
+    fGetter->ExitLoop();
+    delete fGetter;
+
+    gVirtualX->GrabButton(fId, kButton2, 0, 0, 0, 0, kFALSE);
+
+    fTimer->TurnOff();
+    delete fTimer;
+
+    delete fInterpol;
+
+    //delete fList;
+    delete fCaos;
+
+    cout << "Camera Display destroyed." << endl;
+}
+
+/*
 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()));
+    //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);
@@ -570,34 +468,5 @@
     fFindStarCut = env.GetValue("FindStar.CleaningLevel", fFindStarCut);
 }
-
-MStarguider::~MStarguider()
-{
-    fGetter->ExitLoop();
-    delete fGetter;
-
-    gVirtualX->GrabButton(fId, kButton2, 0, 0, 0, 0, kFALSE);
-
-    fTimer->TurnOff();
-    delete fTimer;
-
-    delete fList;
-
-    delete fCaos;
-    delete fStargCaos;
-    delete fStargHistograms;
-    delete fSao;
-    delete fRaDec;
-
-    if (fOutTp)
-        delete fOutTp;
-
-    if (fOutStargTp)
-        delete fOutStargTp;
-
-//    if (fOutRq)
-//        delete fOutRq;
-
-    gLog << inf2 << "Camera Display destroyed." << endl;
-}
+*/
 
 void MStarguider::Layout()
@@ -608,5 +477,5 @@
 void MStarguider::CloseWindow()
 {
-    gLog << inf2 << "EventDisplay::CloseWindow: Exit Application Loop." << endl;
+    cout << "EventDisplay::CloseWindow: Exit Application Loop." << endl;
 
     //fClient.ExitLoop();
@@ -622,4 +491,5 @@
 }
 
+/*
 void MStarguider::SetChannel()
 {
@@ -643,5 +513,5 @@
             fGetter->SetChannel(ch);
     }
-}
+}*/
 
 void MStarguider::Toggle(TGPopupMenu *p, UInt_t id)
@@ -653,151 +523,6 @@
 }
 
-void MStarguider::ToggleStargAnalysis()
-{
-    if (fOperations->IsEntryChecked(IDM_kStargAnalysis))
-        fStargHistograms->OpenFile();
-    else
-        fStargHistograms->CloseFile();
-}
-
-/*
-void MStarguider::ToggleFindStar()
-{
-    if (fDisplay->IsEntryChecked(IDM_kFindStar) && fCosy)
-    fTPoint->MapWindow();
-    else
-    {
-        fTPoint->UnmapWindow();
-        fTPoint->SetDown(kFALSE);
-    }
-}
-*/
-void MStarguider::ToggleStarguider()
-{
-    if (fDisplay->IsEntryChecked(IDM_kStarguider))
-    {
-        fLastBright = 0xff;
-
-        fChannel->EnableEntry(IDM_kChannel1);
-        fDisplay->DisableEntry(IDM_kFindStar);
-        fDisplay->DisableEntry(IDM_kCaosFilter);
-        fOperations->EnableEntry(IDM_kStargAnalysis);
-
-        //fStargTPoint->MapWindow();
-
-        fPZdAz->MapWindow();
-        fDZdAz->MapWindow();
-        fSkyBright->MapWindow();
-
-        for (int i=IDM_kLimMag3; i<=IDM_kLimMag9; i++)
-            fLimMag->UnCheckEntry(i);
-        fLimMag->CheckEntry(IDM_kLimMag9);
-        fSao->SetLimitMag(9.0);
-
-        for (int i=IDM_kInterpol250; i<=IDM_kInterpol1; i++)
-            fInterpol->UnCheckEntry(i);
-        fInterpol->CheckEntry(IDM_kInterpol125);
-        fIntRate = 125;
-
-        //catalog
-        fDisplay->CheckEntry(IDM_kCatalog);
-        fCRaDec->MapWindow();
-        fCZdAz->MapWindow();
-        fCZdAzText->MapWindow();
-
-        fPZdAz->MapWindow();
-        fPZdAzText->MapWindow();
-        fDZdAz->MapWindow();
-        fDZdAzText->MapWindow();
-
-        fGStarg->MapWindow();
-        fGNumStars->MapWindow();
-
-        SwitchOff(fChannel, IDM_kChannel2);
-        fChannel->CheckEntry(IDM_kChannel1);
-    }
-    else
-    {
-        fStatus = MDriveCom::kStandby;
-
-        //fStargTPoint->UnmapWindow();
-        //fStargTPoint->SetDown(kFALSE);
-
-        fPZdAz->UnmapWindow();
-        fPZdAzText->UnmapWindow();
-        fDZdAz->UnmapWindow();
-        fDZdAzText->UnmapWindow();
-        fSkyBright->UnmapWindow();
-        //catalog
-        fCRaDec->UnmapWindow();
-        fCZdAz->UnmapWindow();
-        fCZdAzText->UnmapWindow();
-        fDisplay->UnCheckEntry(IDM_kCatalog);
-
-        fGStarg->UnmapWindow();
-        fGNumStars->UnmapWindow();
-
-        fChannel->EnableEntry(IDM_kChannel2);
-        fDisplay->EnableEntry(IDM_kFindStar);
-        fDisplay->EnableEntry(IDM_kCaosFilter);
-        fOperations->DisableEntry(IDM_kStargAnalysis);
-    }
-
-    SetChannel();
-    //gSystem->Unlink("tracking_error.txt");
-}
-
-void MStarguider::ToggleCaosFilter()
-{
-    if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
-    {
-        if (!fMode->IsEntryChecked(IDM_kStarguiderMode)
-            && !fMode->IsEntryChecked(IDM_kTpointMode))
-            fMenu->AddPopup("&CaOs", fCaOs, NULL);
-
-        SwitchOff(fDisplay, IDM_kStarguider);
-        SwitchOff(fDisplay, IDM_kStargCaosFilter);
-    }
-    else
-    {
-        fCaosWrite->UnCheckEntry(IDM_kCaosPrintLeds);
-        fCaosWrite->UnCheckEntry(IDM_kCaosPrintRings);
-
-        if (fCaosAnalyse->IsEntryEnabled(IDM_kCaosAnalStop))
-        {
-            fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
-            fCaosAnalyse->EnableEntry(IDM_kCaosAnalStart);
-            fCaos->DeleteHistograms();
-        }
-        if (fCaosWrite->IsEntryEnabled(IDM_kCaosWriteStop))
-        {
-            fCaosWrite->DisableEntry(IDM_kCaosWriteStop);
-            fCaosWrite->EnableEntry(IDM_kCaosWriteStart);
-            fCaos->CloseFile();
-        }
-        fMenu->RemovePopup("CaOs");
-
-        fDisplay->EnableEntry(IDM_kStargCaosFilter);
-        fDisplay->EnableEntry(IDM_kStarguider);
-    }
-    fMenu->Resize(fMenu->GetDefaultSize());
-    MapSubwindows(); // maps everything, but we don't want that
-    fTPoint->UnmapWindow();
-    //fStargTPoint->UnmapWindow();
-    fGStarg->UnmapWindow();
-    fGNumStars->UnmapWindow();
-    fCRaDec->UnmapWindow();
-    fCZdAz->UnmapWindow();
-    fCZdAzText->UnmapWindow();
-    fPZdAz->UnmapWindow();
-    fPZdAzText->UnmapWindow();
-    fDZdAz->UnmapWindow();
-    fDZdAzText->UnmapWindow();
-    fSZdAz->UnmapWindow();
-    fSkyBright->UnmapWindow();
-    MapWindow();
-}
-
-Bool_t MStarguider::ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
+
+Bool_t MStarguider::ProcessMessage(Long_t msg, Long_t mp1, Long_t)
 {
     switch (GET_MSG(msg))
@@ -807,4 +532,5 @@
             switch (mp1)
             {
+                /*
             case IDM_kPixSize:
                 {
@@ -826,5 +552,5 @@
                     gLog << all << "Starguider cleaning level changed to " << cut << " sigma." << endl;
                     return kTRUE;
-                }
+                }*/
             }
         return kTRUE;
@@ -836,78 +562,5 @@
             switch (mp1)
             {
-            case IDM_kCatalog:
-                Toggle(fDisplay, IDM_kCatalog);
-                if (fDisplay->IsEntryChecked(IDM_kCatalog))
-                {
-                    fCRaDec->MapWindow();
-                    fCZdAz->MapWindow();
-                    fCZdAzText->MapWindow();
-                }
-                else
-                {
-                    fCRaDec->UnmapWindow();
-                    fCZdAz->UnmapWindow();
-                    fCZdAzText->UnmapWindow();
-                }
-                return kTRUE;
-
-            case IDM_kStarguider:
-                Toggle(fDisplay, IDM_kStarguider);
-                ToggleStarguider();
-                return kTRUE;
-
-            case IDM_kStargAnalysis:
-                Toggle(fOperations, IDM_kStargAnalysis);
-                ToggleStargAnalysis();
-                return kTRUE;
-
-            case IDM_kStarguiderMode:
-                Toggle(fMode, IDM_kStarguiderMode);
-
-                if (fMode->IsEntryChecked(IDM_kStarguiderMode))
-                {
-                    //uncheck not needed items
-                    //general
-                    SwitchOff(fDisplay, IDM_kFilter);
-                    SwitchOff(fChannel, IDM_kChannel3);
-                    SwitchOff(fOperations, IDM_kStargAnalysis);
-
-                    //tpoint
-                    SwitchOff(fDisplay, IDM_kFindStar);
-                    SwitchOff(fDisplay, IDM_kCaosFilter);
-                    ToggleCaosFilter();
-                    fMode->UnCheckEntry(IDM_kTpointMode);
-
-                    //enable starguider items
-                    fDisplay->EnableEntry(IDM_kStargCaosFilter);
-                    fDisplay->EnableEntry(IDM_kStarguider);
-                    fDisplay->EnableEntry(IDM_kCatalog);
-
-                    //check needed items
-                    fDisplay->UnCheckEntry(IDM_kStretch);
-                    fDisplay->CheckEntry(IDM_kStargCaosFilter);
-                    fDisplay->CheckEntry(IDM_kStarguider);
-                    ToggleStarguider();
-
-                    fTPoint->MapWindow();
-                }
-                else
-                {
-                    //uncheck
-                    //starguider items
-                    fDisplay->UnCheckEntry(IDM_kStargCaosFilter);
-                    fDisplay->UnCheckEntry(IDM_kStarguider);
-                    fTPoint->UnmapWindow();
-                    ToggleStarguider();
-
-                    //general
-                    fDisplay->UnCheckEntry(IDM_kCatalog);
-                    fDisplay->EnableEntry(IDM_kFilter);
-                    fDisplay->EnableEntry(IDM_kCaosFilter);
-                    fDisplay->EnableEntry(IDM_kFindStar);
-                    fChannel->EnableEntry(IDM_kChannel3);
-                }
-                return kTRUE;
-
+/*
             case IDM_kTpointMode:
                 Toggle(fMode, IDM_kTpointMode);
@@ -921,11 +574,11 @@
 
                     //from starguider
-                    SwitchOff(fDisplay, IDM_kStargCaosFilter);
-                    SwitchOff(fDisplay, IDM_kCatalog);
-                    SwitchOff(fDisplay, IDM_kStarguider);
-                    ToggleStarguider();
-                    fMode->UnCheckEntry(IDM_kStarguiderMode);
-                    SwitchOff(fOperations, IDM_kStargAnalysis);
-                    ToggleStargAnalysis();
+                    //SwitchOff(fDisplay, IDM_kStargCaosFilter);
+                    //SwitchOff(fDisplay, IDM_kCatalog);
+                    //SwitchOff(fDisplay, IDM_kStarguider);
+                    //ToggleStarguider();
+                    //fMode->UnCheckEntry(IDM_kStarguiderMode);
+                    //SwitchOff(fOperations, IDM_kStargAnalysis);
+                    //ToggleStargAnalysis();
 
                     //switch camera
@@ -946,8 +599,8 @@
                     //enable
                     //starguider items
-                    fDisplay->EnableEntry(IDM_kStargCaosFilter);
-                    fDisplay->EnableEntry(IDM_kCatalog);
-                    fDisplay->EnableEntry(IDM_kStarguider);
-                    fOperations->EnableEntry(IDM_kStargAnalysis);
+                    //fDisplay->EnableEntry(IDM_kStargCaosFilter);
+                    //fDisplay->EnableEntry(IDM_kCatalog);
+                    //fDisplay->EnableEntry(IDM_kStarguider);
+                    //fOperations->EnableEntry(IDM_kStargAnalysis);
 
                     //general
@@ -963,5 +616,5 @@
                 }
                 return kTRUE;
-
+*/
             case IDM_kFilter:
                 Toggle(fDisplay, IDM_kFilter);           
@@ -979,23 +632,7 @@
             case IDM_kCaosFilter:
                 Toggle(fDisplay, IDM_kCaosFilter);
-                ToggleCaosFilter();
-                return kTRUE;
-
-            case IDM_kStargCaosFilter:
-		Toggle(fDisplay, IDM_kStargCaosFilter);                
-                if (fDisplay->IsEntryChecked(IDM_kStargCaosFilter))
-                {
-                    SwitchOff(fDisplay, IDM_kCaosFilter);
-                    SwitchOff(fDisplay, IDM_kFindStar);
-                    SwitchOff(fDisplay, IDM_kFilter);
-                }
-                else
-                {
-                    fDisplay->EnableEntry(IDM_kFilter);
-                    fDisplay->EnableEntry(IDM_kFindStar);
-                    fDisplay->EnableEntry(IDM_kCaosFilter);
-                }
-                return kTRUE;
-
+                //ToggleCaosFilter();
+                return kTRUE;
+/*
             case IDM_kCaosPrintLeds:
             case IDM_kCaosPrintRings:
@@ -1006,5 +643,5 @@
                 fCaosAnalyse->DisableEntry(IDM_kCaosAnalStart);
                 fCaosAnalyse->EnableEntry(IDM_kCaosAnalStop);
-                fCaos->InitHistograms();
+                //fCaos->InitHistograms();
                 return kTRUE;
 
@@ -1111,5 +748,5 @@
                 }
                 return kTRUE;
-
+             */
             case IDM_kInterpol250:
             case IDM_kInterpol125:
@@ -1153,20 +790,4 @@
                 }
                 return kTRUE;
-
-            case IDM_kLimMag3:
-            case IDM_kLimMag4:
-            case IDM_kLimMag5:
-            case IDM_kLimMag6:
-            case IDM_kLimMag7:
-            case IDM_kLimMag8:
-            case IDM_kLimMag9:
-                for (int i=IDM_kLimMag3; i<=IDM_kLimMag9; i++)
-                    if (mp1==i)
-                        fLimMag->CheckEntry(i);
-                    else
-                        fLimMag->UnCheckEntry(i);
-
-                fSao->SetLimitMag(mp1-IDM_kLimMag3+3);
-                return kTRUE;
             }
             break;
@@ -1177,329 +798,38 @@
     return kTRUE;
 }
-
-void MStarguider::SetPointingPosition(RaDec rd)
-{
-    rd.Ra(rd.Ra()*24/360);
-    fCRaDec->SetCoordinates(rd);
-}
-
-ZdAz MStarguider::TrackingError(TArrayF &x, TArrayF &y, TArrayF &mag,
-                                Int_t &num) const
-{
-    num = -1;
-
-    // Width of the final 2D gaussian
-    const Double_t width = 0.8;
-
-    // The binning is made 1.7 sigma (which, in the 1D case, gives the 
-    // highest significance of a gaussian signal)  (1bin equiv 3x3 sigma)
-    const Double_t max =  50;
-    const Int_t    n   = TMath::Nint(2*max/(1.77*1.7*width));
-    //1.77=sqrt(pi) comes from pir=bin
-
-    // Fill a histogram with the dx/dy values from the file
-    TH2F hist("Hist", "dX/dY", n, -max, max, n, -max, max);
-//    hist.SetDirectory(0);
-
-    //
-    // Search for matching Magnitudes
-    //
-    //for (int i=0; i<mag.GetSize(); i++)
-    //{
-    //   if (mag[i]>48-15 && mag[i]<48+15)
-    //        h.Fill(x[i], y[i]);
-    //}
-
-    // fill dx and dy into histogram
-    for (int i=0; i<mag.GetSize(); i++)
-        hist.Fill(x[i], y[i]);
-
-    // Remove all bins which have content of only a single event
-    // including under- and overflow bins
-    for (int i=0; i<hist.GetSize(); i++)
-        if (hist.GetBinContent(i)<1.5)
-            hist.SetBinContent(i, 0);
-
-    // Find the bin containing the maximum
-    Int_t bx, by, bz;
-    hist.GetMaximumBin(bx, by, bz);
-
-    // In the worst case the events are spread through the
-    // bins which are the neighbors of the maximum
-    // Normally neighbors which do not belong to the signal are empty!
-    hist.GetXaxis()->SetRange(bx-1, bx+1); // 3x3bins  ~8x8pix  ~9x9sigma
-    hist.GetYaxis()->SetRange(by-1, by+1); // 3x3bins  ~8x8pix  ~9x9sigma
-
-    // Check whether this region contains events
-    num = TMath::Nint(hist.Integral());
-    if (num<1)
-        return ZdAz();
-
-    // Get Mean for the given axis range
-    Double_t mx = hist.GetMean(1);
-    Double_t my = hist.GetMean(2);
-
-    const Int_t max1 = TMath::Nint(hist.GetBinContent(bx, by));
-
-    // Check if the maximum is unique
-    Int_t bx2, by2, bz2;
-    hist.GetXaxis()->SetRange(-1, 9999);
-    hist.GetYaxis()->SetRange(-1, 9999);
-    hist.SetBinContent(bx, by, 0);
-    hist.GetMaximumBin(bx2, by2, bz2);
-
-    const Int_t max2 = TMath::Nint(hist.GetBinContent(bx2, by2));
-    if (max1==max2 && TMath::Abs(bx2-bx)+TMath::Abs(by2-by)>1)
-        return ZdAz();
-
-    // loop again over the file and fill the histogram again.
-    // to get closer to the correct value
-    Double_t sumx = 0;
-    Double_t sumy = 0;
-             num  = 0;
-    for (int i=0; i<mag.GetSize(); i++)
-    {
-        // only fill values into the histogram which
-        // are inside 2*1.7=3.4 sigma (99.7%)
-        if (TMath::Hypot(x[i]-mx, y[i]-my)>width*3.4)
-            continue;
-
-        sumx += x[i];
-        sumy += y[i];
-        num++;
-    }
-
-    if (num<1)
-        return ZdAz();
-
-    // calc MeanX and MeanY
-    mx = sumx/num;
-    my = sumy/num;
-
-    // loop again to fill the mispointing corrected histograms
-    // and fill another histogram to check the quality of the calculated
-    // mispointing.
-    sumx = 0;
-    sumy = 0;
-    num  = 0;
-    for (int i=0; i<mag.GetSize(); i++)
-    {
-        // only fill values into the histogram which
-        // are inside 1.7=3.4 sigma (92%)
-        // Cut at 1.7 sigma which gives the best background supression
-        if (TMath::Hypot(x[i]-mx, y[i]-my)>width*1.7)
-            continue;
-
-        sumx += x[i];
-        sumy += y[i];
-        num++;
-    }
-
-    if (num<1)
-        return ZdAz(); // FIXME!!!!!!!!!!!!
-
-    // Mispointing
-    mx = sumx/num;
-    my = sumy/num;
-
-#ifdef EXPERT
-    cout << "Offset-XY:    " << mx << " +- " << my << endl;
-#endif
-
-    return fSao->CalcDeltaZdAzFromPix(mx, my)*TMath::RadToDeg();
 /*
-    AltAz pos0 = fSao->CalcAltAzFromPix(768/2,    576/2)*kRad2Deg;
-    AltAz pos1 = fSao->CalcAltAzFromPix(768/2+mx, 576/2+my)*kRad2Deg;
-
-    pos1 -= pos0;
-
-    ZdAz res2 = ZdAz(-pos1.Alt(), pos1.Az());
-
-    return res1;
-*/
-}
-
-Int_t MStarguider::CalcTrackingError(const Leds &leds, MStarList &stars,
-                                     ZdAz &d, const MTime &t, const double &bright,
-                                     Int_t &num, const Ring &center, Int_t numleds, Int_t numrings)
+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);
+}
+
+Led MStarguider::FindStar(const FilterLed &f, const FilterLed &f2, const Ring &center, Int_t numleds, Int_t numrings)
 {
     // Get tracking coordinates
-    const XY xy = fCRaDec->GetCoordinates();
-
-    num = leds.GetEntries();
-    if (num < 3) //was 1
-    {
-        gLog << warn << "Sorry, less than 3 detected spot in FOV!" << endl;
-        if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
-            fCosy->GetDriveCom()->SendTPoint(false, 'S', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings, num);    // Report
-        fTPoint->SetDown(kFALSE);
-        return 0;
-    }
-
-    if (stars.GetRealEntries() < 3)
-    {
-        gLog << warn << "Sorry, less than 3 stars in FOV!" << endl;
-        if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
-            fCosy->GetDriveCom()->SendTPoint(false, 'S', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings, num);    // Report
-        fTPoint->SetDown(kFALSE);
-        return 0;
-    }
-
-    stars.Sort(); // Sort by magnitude
-
-#ifdef EXPERT
-    TString str = "data/tracking_";
-    str += fSao->GetMjd();
-    str += ".txt";
-
-    ofstream fout(str);
-#endif
-
-    TArrayF x, y, mag;
-
-    // FIXME: Is predefined value 3 a good idea?
-    Int_t cnt=0;
-
-    MStar *star=0;
-    MStarListIter NextStar(&stars);
-    while ((star=NextStar()) && cnt<num+5)
-    {
-        TIter NextSp(&leds);
-        Led *spot=NULL;
-        while ((spot=(Led*)NextSp())) 
-	{
-            const XY dpos(spot->GetX()-(768-star->GetX()),
-                          spot->GetY()-star->GetY());
-	    
-            const Int_t idx = x.GetSize();
-
-            x.Set(idx+1);
-            y.Set(idx+1);
-            mag.Set(idx+1);
-
-            x.AddAt(dpos.X(), idx);
-            y.AddAt(dpos.Y(), idx);
-            mag.AddAt(1, idx);
-//            mag.AddAt(log(spot->GetMag())/star->GetMag(), idx);
-/*
-#ifdef EXPERT
-            if (fout)
-            {
-                fout << spot->GetX() << " "
-                    << spot->GetY()  << " "
-                    << -2.5*log(spot->GetMag())  << " "
-                    << star->GetX() << " "
-                    << star->GetY()  << " "
-                    << star->GetMag()  << " ";
-                fout << x[idx] << " " << y[idx] << " " << mag[idx] << endl;
-            }
-#endif
-*/
-        }
-        cnt++;
-    }
-
-    Int_t numcor = 0;
-    d = TrackingError(x, y, mag, numcor);
-    if (numcor<1)
-    {
-        fTPoint->SetDown(kFALSE);
-        if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
-            fCosy->GetDriveCom()->SendTPoint(false, 'S', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings, num, numcor);    // Report
-        return 0;
-    }
-
-    fDZdAz->SetCoordinates(d);
-
-    //
-    // Current Pointing position
-    //
-    ZdAz cpos = fSao->GetZdAz()-d;
-    fPZdAz->SetCoordinates(cpos);
-
-    // Check StargTPoint data set request
-    if (!fMode->IsEntryChecked(IDM_kStarguiderMode) || !fTPoint->IsDown())
-        return numcor;
-    fTPoint->SetDown(kFALSE);
-
-    // If no file open: open new file
-    if (!fOutStargTp)
-    {
-        // open tpoint file
-        const TString name = MCosy::GetFileName("tpoint", "starg", "txt");
-        gLog << all << "TPoint-Starg File ********* " << name << " ********** " << endl;
-
-        fOutStargTp = new ofstream(name);
-        *fOutStargTp << "Magic Model  STARGTPOINT data file" << endl;
-        *fOutStargTp << ": ALTAZ" << endl;
-        *fOutStargTp << "49 48 0 ";
-        *fOutStargTp << t << endl;
-    }
-
-    const RaDec rd(xy.X()*MAstro::HorToRad(), xy.Y()*TMath::DegToRad());
-
-    // From the star position in the camera we calculate the Alt/Az
-    // position we are currently tracking (real pointing position)
-    fSao->SetMjd(t.GetMjd());
-    AltAz za0 = fSao->CalcAltAz(rd)*kRad2Deg;
-
-    //correction with offset from last tpoint measurement
-    za0 -= fAltAzOffsetFromTp;
-
-    //if the difference between the tpoint and the starguider tpoint
-    //is too big, the starguider tpoint is not stored
-    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;
-        return numcor;
-    }
-
-
-    // Write real pointing position
-    *fOutStargTp << setprecision(7) << za0.Az() << " " << za0.Alt() << " ";
-
-    // Write system pointing position
-    *fOutStargTp << fmod(cpos.Az()+360, 360) << " " << 90-cpos.Zd();
-
-    *fOutStargTp << " " << xy.X() << " " << xy.Y();
-    *fOutStargTp << " " << d.Zd() << " " << d.Az();
-    *fOutStargTp << " " << setprecision(11) << t.GetMjd();
-    *fOutStargTp << " " << setprecision(4) << center.GetMag();
-    *fOutStargTp << " 0";// << star->GetMag();
-    *fOutStargTp << " " << center.GetX() << " " << center.GetY();
-    *fOutStargTp << " 0 0";
-    *fOutStargTp << " " << numleds << " " << numrings;
-    *fOutStargTp << " " << num << " " << numcor << " " << bright;
-    *fOutStargTp << " " << fTPointStarMag << " " << fTPointStarName;
-    *fOutStargTp << endl;
-
-    gLog << all << "Starguider TPoint successfully taken." << endl;
-
-    fTimeFromTp.Clear();
-
-    if (!fCosy)
-        return numcor;
-
-    MDriveCom *com = fCosy->GetDriveCom();
-    if (!com)
-        return numcor;
-
-    com->SendTPoint(true, 'S', fTPointStarMag, fTPointStarName, za0, cpos, xy, d.Zd(), d.Az(), t, center, Led(), numleds, numrings, num, numcor);    // Report
-
-    return numcor;
-}
-
-void MStarguider::FindStar(const FilterLed &f, const FilterLed &f2, const Ring &center, const MTime &t, Int_t numleds, Int_t numrings)
-{
-    // Get tracking coordinates
-    const XY xy = fCRaDec->GetCoordinates();  // [h, deg]
+    //const XY xy = fCRaDec->GetCoordinates();  // [h, deg]
 
     if (center.GetX()<=0 && center.GetY()<=0)
     {
-        gLog << warn << "Couldn't determine center of the camera." << endl;
-        if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
-            fCosy->GetDriveCom()->SendTPoint(false, 'T', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings);    // Report
+        cout << "Couldn't determine center of the camera." << endl;
+        //if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
+        //    fCosy->GetDriveCom()->SendTPoint(false, 'T', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings);    // Report
         return;
     }
@@ -1510,16 +840,20 @@
 
     // Check whether star found
-    Led *star = (Led*)leds.At(0);
-    if (!star)
-    {
-        gLog << warn << "No star found." << endl;
-        if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
-            fCosy->GetDriveCom()->SendTPoint(false, 'T', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings);    // Report
+    if (leds.size()==0)
+    {
+        cout << "No star found." << endl;
+        //if (fTPoint->IsDown() && fCosy && fCosy->GetDriveCom())
+        //    fCosy->GetDriveCom()->SendTPoint(false, 'T', fTPointStarMag, fTPointStarName, AltAz(), ZdAz(), xy, 0, 0, t, center, Led(), numleds, numrings);    // Report
         return;
     }
 
     cout << "Found star @ " << flush;
-    star->Print();
-    f2.MarkPoint(star->GetX(), star->GetY(), 2<<2);
+
+    const Led &star = leds.front();
+
+    star.Print();
+    f2.MarkPoint(star.GetX(), star.GetY(), 2<<2);
+
+    return star;
 
     const RaDec rd(xy.X()*MAstro::HorToRad(), xy.Y()*TMath::DegToRad());
@@ -1530,5 +864,5 @@
     ac.SetGeom(geom);
     ac.SetRadiusFOV(3);
-    ac.SetObservatory(*fSao);
+    //ac.SetObservatory(*fSao);
     ac.SetTime(t);
     ac.SetRaDec(rd.Ra(), rd.Dec());
@@ -1536,5 +870,7 @@
     // 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;
+    const double sec_per_pix = 45.311;
+    const double mm_to_deg   = 0.011693;
+    const Double_t conv = sec_per_pix/3600/mm_to_deg / 1.0713;
 
     // Adapt coordinate system (GUIs and humans are counting Y in different directions)
@@ -1558,4 +894,5 @@
         // open tpoint file
         //
+
         const TString name = MCosy::GetFileName("tpoint", "tpoint", "txt");
         cout << "TPoint File ********* " << name << " ********** " << endl;
@@ -1566,5 +903,5 @@
         *fOutTp << "49 48 0 ";
         *fOutTp << t << endl;
-        // temp(°C) pressure(mB) height(m) humidity(1) wavelength(microm) troplapserate(K/m)
+        // temp(°C) pressure(mB) height(m) humidity(1) wavelength(microm) troplapserate(K/m
     }
 
@@ -1608,5 +945,5 @@
 
     gLog << all << "TPoint successfully taken." << endl;
-/*
+
     MLog &outrep = *fCosy->GetOutRep();
     if (outrep.Lock("MStarguider::FindStar"))
@@ -1622,5 +959,5 @@
         outrep << setprecision(11) << t.GetMjd() << endl;
         outrep.UnLock("MStarguider::FindStar");
-    }*/
+    }
     
 //    return zdaz;
@@ -1635,4 +972,124 @@
     com->SendTPoint(true, 'T', fTPointStarMag, fTPointStarName, za0, za1, xy, -dzd, -daz, t, center, *star, numleds, numrings);    // Report
 }
+*/
+
+void MStarguider::WritePNG(const char *name, const byte *gbuf, const byte *cbuf)
+{
+    string fname(name);
+    if (fname.size()<4 || fname.compare(fname.size()-4,4, ".png")==0)
+        fname += ".png";
+
+    cout << "Writing PNG '" << fname << "'" << endl;
+
+#ifdef HAVE_PNG
+    //
+    // open file
+    //
+    FILE *fd = fopen(fname.c_str(), "w");
+    if (!fd)
+    {
+        cout << "Warning: Cannot open file for writing." << endl;
+        return;
+    }
+
+    //
+    // allocate memory
+    //
+    png_structp fPng = png_create_write_struct(PNG_LIBPNG_VER_STRING,
+                                               NULL, NULL, NULL);
+
+    if (!fPng)
+    {
+        cout << "Warning: Unable to create PNG structure" << endl;
+        fclose(fd);
+        return;
+    }
+
+
+    png_infop fInfo = png_create_info_struct(fPng);
+
+    if (!fInfo)
+    {
+        cout << "Warning: Unable to create PNG info structure" << endl;
+        png_destroy_write_struct (&fPng, NULL);
+        fclose(fd);
+        return;
+    }
+
+    fInfo->width      = 768;
+    fInfo->height     = 576;
+    fInfo->bit_depth  = 8;
+    fInfo->color_type = PNG_COLOR_TYPE_RGB;
+//    fInfo->color_type = PNG_COLOR_TYPE_GRAY;
+
+    //
+    // set jump-back point in case of errors
+    //
+    if (setjmp(fPng->jmpbuf))
+    {
+        cout << "longjmp Warning: PNG encounterd an error!" << endl;
+        png_destroy_write_struct (&fPng, &fInfo);
+        fclose(fd);
+        return;
+    }
+
+    //
+    // connect file to PNG-Structure
+    //
+    png_init_io(fPng, fd);
+
+    // png_set_compression_level (fPng, Z_BEST_COMPRESSION);
+
+    //
+    // Write header
+    //
+    png_write_info(fPng, fInfo);
+
+    png_byte buf[768*576*3];
+
+    png_byte *d = buf;
+    const byte *g = gbuf;
+    const byte *c = cbuf;
+
+    // d=destination, s1=source1, s2=source2, e=end
+    while (d<buf+768*576*3)
+    {
+        if (fScreenshotColor && *c)
+        {
+            *d++ = ((*c>>4)&0x3)*85;
+            *d++ = ((*c>>2)&0x3)*85;
+            *d++ = ((*c++ )&0x3)*85;
+            g++;
+        }
+        else
+        {
+            *d++ = *g;
+            *d++ = *g;
+            *d++ = *g++;
+            c++;
+        }
+    }
+
+    //
+    // Write bitmap data
+    //
+    for (unsigned int y=0; y<768*576*3; y+=768*3)
+	png_write_row (fPng, buf+y);
+
+    //
+    // Write footer
+    //
+    png_write_end (fPng, fInfo);
+
+    //
+    // free memory
+    //
+    png_destroy_write_struct (&fPng, &fInfo);
+
+    fclose(fd);
+#else
+    cout << "Sorry, no png support compiled into tpoint." << endl;
+#endif
+}
 
 bool MStarguider::Interpolate(const unsigned long n, byte *img) const
@@ -1641,4 +1098,6 @@
         return true;
 
+    const int rate = fTPointMode>0 ? 5*25 : fIntRate;
+
     static unsigned short myimg[768*576];
 
@@ -1647,5 +1106,5 @@
     const byte *end = img+768*576;
 
-    if (n%fIntRate)
+    if (n%rate)
     {
         while (img<end)
@@ -1657,5 +1116,5 @@
         while (img<end)
         {
-            *img = (*img + *f)/fIntRate;
+            *img = (*img + *f)/rate;
             ++img;
             *f++ = 0;
@@ -1666,418 +1125,158 @@
 }
 
-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::StartTPoint(char *cmd)
-{
-    fTPointStarName = "Dummy";
-    fTPointStarMag  = 0;
-
-    TString str(cmd);
-
-    TObjArray *arr = str.Tokenize(' ');
-
-    if (arr->GetEntries()==2)
-    {
-        fTPointStarName = (*arr)[0]->GetName();
-        fTPointStarMag  = atof((*arr)[1]->GetName());
-    }
-
-    delete arr;
-
-    if (fTPointFromCC<0)
-        fTPointFromCC=0;
-}
-
-void MStarguider::StartStarguider(bool on)
-{
-    // Switch to starguider mode
-    if (on)
-    {
-        cout << " * Switching Starguider mode on" << endl;
-        fMode->UnCheckEntry(IDM_kStarguiderMode);
-        ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStarguiderMode, 0);
-    }
-    else
-    {
-        cout << " * Switching Starguider mode off" << endl;
-        fMode->CheckEntry(IDM_kStarguiderMode);
-        ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStarguiderMode, 0);
-    }
-}
-
-Int_t MStarguider::GetStarguiderMode() const
-{
-    return fMode->IsEntryChecked(IDM_kStarguiderMode) ? 1 : 2;
-
-}
-
-Bool_t MStarguider::DoTPoint()
-{
-    if (fTPoint->IsDown() && fTPointFromCC<0)
-    {
-        fTPointFromCC = 0;
-        fTPoint->SetDown(kFALSE);
-    }
-
-    if (fTPointFromCC<0)
-        return kTRUE;
-
-    switch (++fTPointFromCC)
-    {
-    case 1:
-        fTimeFromTp.Clear();
-        fNumStarsDetected = 0;
-
-        if (!fMode->IsEntryChecked(IDM_kTpointMode))
-        {
-            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;
-        }
-        fTPointFromCC++;
-
-    case 2:
-    case 3:
-        cout << " * Waiting one frame" << endl;
-        // Wait one frame
-        return kFALSE;
-
-    case 4:
-        cout << " * Taking TPoint" << endl;
-        fTPoint->SetDown(); // kTRUE
-        return kTRUE;
-
-    case 5:
-        if (!fTimeFromTp) // TPoint failed
-            break;
-#ifdef FACT
-        fTPointFromCC = -1;
-        return kFALSE;
-#else
-        // 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;
-#endif
-    case 6:
-    case 7:
-        cout << " * Waiting one frame" << endl;
-        // Wait one frame
-        return kFALSE;
-
-    case 8:
-        cout << " * Taking Starguider TPoint" << endl;
-        fTPoint->SetDown(); // kTRUE
-        return kTRUE;
-
-//    case 9:
-//        cout << " * Send Report" << endl;
-//        return kTRUE;
-
-    case 9:
-        cout << " * Switching to TPoint mode" << endl;
-        // Switch to tpoint mode
-        fMode->UnCheckEntry(IDM_kTpointMode);
-        ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kTpointMode, 0);
-        fTPointFromCC = -1;
-        return kFALSE;
-    }
-
-    // Send report
-/*
-    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;
-}
-
-void MStarguider::ProcessFrame(const unsigned long n, byte *img, 
+void MStarguider::ProcessFrame(const unsigned long n, byte *img,
 			       struct timeval *tm)
 {
-    static unsigned long n0 = n;
-
-    MTime t(*tm);
-
-    const Double_t d = t-fTime;
-    if (d>1)
-    {
-        const TString txt = MString::Format("%dfps", (int)((n-n0)/d+.5));
-        fFps->SetText(txt);
-        fTime = t;
-        n0 = n;
-    }
-
     if (!Interpolate(n, img))
         return;
+
+    if (fTPointMode==2)
+    {
+        fTPointMode=1;
+        return;
+    }
 
     byte cimg[768*576];
     memset(cimg, 0, 768*576);
 
-    FilterLed f(img,   768, 576, 2.5); // 2.5
-    FilterLed f2(cimg, 768, 576); // former color 0xb0
-
-    if (fDisplay->IsEntryChecked(IDM_kStretch))
+    FilterLed f (img,  768, 576, 2.5); // 2.5
+    FilterLed f2(cimg, 768, 576);      // former color 0xb0
+
+    if (!fTPointMode && fScreenshotName.empty() && fDisplay->IsEntryChecked(IDM_kStretch))
         f.Stretch();
 
-    if (!fWritePictures->IsEntryEnabled(IDM_kStart) &&
-        (!(n%fWrtRate) || fWriteType->IsEntryChecked(IDM_kOnce)))
-    {
-
-        if (fFileType->IsEntryChecked(IDM_kPNG))
-            Writer::Png("pix/file", img, tm, fCRaDec->GetCoordinates());
-
-        if (fFileType->IsEntryChecked(IDM_kPPM))
-            Writer::Ppm("pix/file", img, tm, fCRaDec->GetCoordinates());
-	
-        if (fWriteType->IsEntryChecked(IDM_kOnce))
-            ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStop, 0);
-    }
-
     // Visual Filter, whole FOV
-    if (fDisplay->IsEntryChecked(IDM_kFilter))
-        f.Execute();
-
-    if (!DoTPoint())
-        return;
-
-    Int_t numleds = 0;
-    Int_t numrings = 0;
+    if (!fTPointMode && fDisplay->IsEntryChecked(IDM_kFilter))
+    {
+        vector<Led> leds;
+        f.Execute(leds, 768/2, 576/2);
+        for (auto it=leds.begin(); it!=leds.end(); it++)
+            f.MarkPoint(*it);
+    }
 
     // Find Center of Camera for Caos and Tpoints
-//    Ring center;//(-1, -1);
-    Ring center(5, 5);//(-1, -1);
-    if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
-    {
-        const bool printl = fCaosPrint->IsEntryChecked(IDM_kCaosPrintLeds);
-        const bool printr = fCaosPrint->IsEntryChecked(IDM_kCaosPrintRings);
-        ZdAz pos;
-        if (fCosy)
-            pos = fCosy->GetPointingPos();
-
-        center = fCaos->Run(img, printl, printr, pos, t);
-
+    int numleds  = 0;
+    int numrings = 0;
+    Ring center(-1, -1);//(5, 5);
+
+    if (fTPointMode || fDisplay->IsEntryChecked(IDM_kCaosFilter))
+    {
+        center   = fCaos->Run(img);
         numleds  = fCaos->GetNumDetectedLEDs();
         numrings = fCaos->GetNumDetectedRings();
     }
 
+    //cout << "cx=" << center.GetX() << "   cy=" << center.GetY() << "   Nled=" << numleds << "   Nrings=" << numrings << endl;
+
     // Find Star at Center---for Tpoint Procedure
-    if (fDisplay->IsEntryChecked(IDM_kFindStar))
-    {
-        // Set search Paremeters (FIXME: Get them from user input!)
-        f.SetCut(fFindStarCut);
-        f.SetBox(fFindStarBox);
-
-        FindStar(f, f2, center, t, numleds, numrings);
-    }
-
-    DrawZoomImage(img);
-    DrawCosyImage(img);
-
-    // Position corresponding to the camera center (53.2, 293.6)
-    Ring sgcenter;//(-1, -1); // Center of camera in SG picture [px]
-
-    // Find Center of Camera in Starfield Camera picture 
-    if (fDisplay->IsEntryChecked(IDM_kStargCaosFilter))
-    {
-        ZdAz pos;
-        if (fCosy)
-            pos = fCosy->GetPointingPos();
-
-        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
-    if (fDisplay->IsEntryChecked(IDM_kCatalog))
-    {
-        MTime time(*tm);
-
-        XY xy = fCRaDec->GetCoordinates();  //[h,  deg]
-	fRaDec->Set(xy.X()*15, xy.Y());     //[deg,deg]
-
-	UpdatePosZoom();
-
-	// Always call SetMjd first!
-        fSao->SetPointing(time.GetMjd(), *fRaDec);
-  	fCZdAz->SetCoordinates(fSao->GetZdAz());
-
-    	fSao->SetBox(fStarguiderW, fStarguiderH); // 280 Region of interest around center
-
-        // If center of camera cannot be determined sgcenter is (0,0)
-        const Bool_t centerisvalid = sgcenter.GetX()>0 && sgcenter.GetY()>0;
-
-	// 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);
-
-        // Skycenter
-        const XY off(sgcenter.GetX() + fSkyOffsetX,
-                     sgcenter.GetY() + fSkyOffsetY);
-
-        MStarList stars;
-        if (centerisvalid)
+    Led star(-1, -1);
+    if (center.GetX()>0 && center.GetY()>0)
+    {
+        if (fTPointMode || fDisplay->IsEntryChecked(IDM_kFindStar))
         {
-            // we obtain stars in the effective star FOV and draw them.
-            // coordinates are video frame coords.
-            fSao->CalcStars(stars, fStarguiderX, fStarguiderY, off.X(), off.Y());
-            fSao->DrawStars(stars, cimg);
+            // Set search Paremeters (FIXME: Get them from user input!)
+            f.SetCut(fFindStarCut+1.5);  // FindStar.CleaningLevel
+            f.SetBox(fFindStarBox+12/*+80*/);  // FindStar.SizeBox
+
+            // Try to find the star
+            vector<Led> leds;
+            f.FindStar(leds, (Int_t)center.GetX(), (Int_t)center.GetY(), true);
+
+            // Check whether star found
+            if (leds.size()>0)
+            {
+                //cout << "Found star @ " << flush;
+                //leds[0].Print();
+                f2.MarkPoint(leds[0].GetX(), leds[0].GetY(), 2<<2);
+                star = leds[0];
+            }
         }
 
-	// There are two corrections to the mispointing
-	// - Sagging of the camera as measured with the LEDs
-	// - Star not ideally centered on MAGIC Camera
-
-	// Next we evaluate the offset given by the LEDs. This we obtain
-        // in Zd/Az and add it to the tracking error.
-	if (fDisplay->IsEntryChecked(IDM_kStarguider))
+        // DrawZoomImage(img);
+        // DrawCosyImage(img);
+
+        // Position corresponding to the camera center (53.2, 293.6)
+        // Draw Circles around center of Camera
+        if (fTPointMode || fDisplay->IsEntryChecked(IDM_kCaosFilter))
         {
-            const Float_t cut = atof(fCut->GetText());
-
-  	    Leds spots;
-            f.SetBox(fStarguiderW, fStarguiderH); // 280
-	    f.SetCut(cut);
-
-            double bright;
- 	    f.ExecuteAndMark(spots, fStarguiderX, fStarguiderY, bright);
-
-            ULong_t color;
-	    gClient->GetColorByName("Green", color);
-            if (bright> 60)
-                gClient->GetColorByName("Yellow", color);
-            if (bright> 85)
-                gClient->GetColorByName("Orange", color);
-            if (bright> 95)
-                gClient->GetColorByName("Red", color);
-            fSkyBright->SetBackgroundColor(color);
-
-            const MString txt = MString::Format("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, sgcenter, numleds, numrings);
-
-            const Bool_t monitoring = brightnessisvalid && centerisvalid && fNumStarsCorrelated>2;
-
-            fStatus = monitoring ? MDriveCom::kMonitoring : MDriveCom::kError;
-
-            if (fCosy)
-                fPos = fCosy->GetPointingPos();
-
-            if (fOperations->IsEntryChecked(IDM_kStargAnalysis))
-                fStargHistograms->Fill(spots, stars, fD, fSao->GetZdAz(),
-                                       sgcenter, bright, fPos, t);
-
-            fNumStarsDetected   = numstars;
-            fNumStarsCorrelated = rc;
-
-            if (fCosy)
-            {
-                MDriveCom *com = fCosy->GetDriveCom();
-                if (com)
-                    com->SendStargReport(fStatus, fD, fSao->GetZdAz(),
-                                         sgcenter, numstars, rc, bright,
-                                         time.GetMjd(), numleds, numrings);    // Report
-            }
-
-        } //kStarguider
-
-        if (centerisvalid && fNumStarsCorrelated>2)
-        {
-            // Position around which the circles are drawn.
-            const Ring skycenter(off.X(), off.Y());
-
-            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(center, 0x0a);
+            f2.DrawCircle(center,   7.0,
+                          fDisplay->IsEntryChecked(IDM_kFindStar)?3:0xb0);
+            //f2.DrawCircle(center, 115.0, 0x0a);
+            //f2.DrawCircle(center, 230.0, 0x0a);
+            //f2.DrawCircle(center, 245.0, 0x0a);
         }
-
-    } //CalcStars
-
-    if (fTPoint->IsDown())
-        fTPoint->SetDown(kFALSE);
-
-    // Draw Circles around center of Camera
-    if (fDisplay->IsEntryChecked(IDM_kCaosFilter) &&
-        center.GetX()>0 && center.GetY()>0)
-    {
-        f2.DrawCircle(center, 0x0a);
-        f2.DrawCircle(center,   7.0,
-		      fDisplay->IsEntryChecked(IDM_kFindStar)?3:0xb0);
-        f2.DrawCircle(center, 115.0, 0x0a);
-        f2.DrawCircle(center, 230.0, 0x0a);
-        f2.DrawCircle(center, 245.0, 0x0a);
-    }
-
-    if (fDisplay->IsEntryChecked(IDM_kStargCaosFilter) &&
-        sgcenter.GetX()>0 && sgcenter.GetY()>0)
-    {
-        f2.DrawCircle(sgcenter, 0x0a);
-        f2.DrawCircle(sgcenter,   5.0,
-                      fDisplay->IsEntryChecked(IDM_kFindStar)?3:0xb0);
-    }
-
-    if (fDisplay->IsEntryChecked(IDM_kCaosFilter) ||
-        fDisplay->IsEntryChecked(IDM_kCatalog)    ||
+    }
+
+    if (fTPointMode ||
+        fDisplay->IsEntryChecked(IDM_kCaosFilter) ||
         fDisplay->IsEntryChecked(IDM_kFindStar))
         fImage->DrawColImg(img, cimg);
     else
         fImage->DrawImg(img);
-}
-
+
+    if (fTPointMode && !fScreenshotName.empty())
+    {
+        WritePNG(fScreenshotName.c_str(), img, cimg);
+        fScreenshotName = "";
+        fTPointMode = 0;
+        return;
+    }
+
+
+    if (star.GetX()<0 || star.GetY()<0 || fTPointMode==0)
+        return;
+
+    if (fTPointMode==1)
+        fTPointMode=0;
+
+    // Convert from Pixel to millimeter (1pix=2.6mm) [deg/pix / deg/mm = mm/pix]
+    // Correct for abberation.
+
+    //const float Dleds =     510; // 5.96344 deg
+    //const float Dpix  = 2*237.58;
+
+    // The DC reflector elongates light from off axis sources
+    // This is a correction. It is 7% for MAGIC (1:1) and
+    // less for FACT (1:1.4). This is an estimate from a
+    // Orbit mode observation at 0.17deg distance to the
+    // camera center [4.90m might also not be very accurate
+    // depending on the position of the CCD camera]
+    const double abberation  = 1.0638; //1.0713;
+    const double sec_per_pix = 45.14;  //45.311;  (atan(510mm/2 / 4.90m) / 237.58)           // FACT LEDs
+
+    const double conv = sec_per_pix/abberation;
+
+    const double dx = star.GetX()-center.GetX();
+    const double dy = star.GetY()-center.GetY();
+
+    //const double dxx = - conv * (star.GetX()-center.GetX());
+    //const double dyy =   conv * (star.GetY()-center.GetY());
+
+    const double dphi = - center.GetPhi() * M_PI/180;
+
+    // The sign is because the pixels are not counted in
+    // both directions in the same direction as Zd/Az
+    const double dxx = - conv * (dx*cos(dphi) - dy*sin(dphi));
+    const double dyy =   conv * (dx*sin(dphi) + dy*cos(dphi));
+
+    double arr[11] = {
+        dxx, dyy,
+        double(numleds), double(numrings),
+        center.GetX(), center.GetY(), center.GetMag(),
+        star.GetX(),   star.GetY(),   star.GetMag(),
+        center.GetPhi()
+    };
+
+    fDimData.setData(arr, 11*sizeof(double));
+    fDimData.setQuality(0);
+    fDimData.setTimestamp(tm->tv_sec, tm->tv_usec/1000);
+    fDimData.updateService();
+}
+
+/*
 void MStarguider::UpdatePosZoom()
 {
-    MString txt;/*
+    MString txt;
     if (fDisplay->IsEntryChecked(IDM_kCatalog))
     {
@@ -2089,5 +1288,5 @@
         txt.Form("(%d, %d) %.1fd/%.1fd", fDx, fDy, -aa.Alt(), aa.Az()-180);
     }
-    else*/
+    else
         txt.Form("(%d, %d)", fDx, fDy);
     fPosZoom->SetText(txt);
@@ -2115,15 +1314,37 @@
     fDy = dy;
 
-    UpdatePosZoom();
+    //UpdatePosZoom();
     return kTRUE;
 }
 
-void MStarguider::Print(TString &str, Double_t deg) const
-{
-    Char_t sgn;
-    UShort_t d, m, s;
-
-    MAstro::Deg2Dms(deg, sgn, d, m, s);
-
-    str += MString::Format("%c %03d %02d %03d ", sgn, d, m, s);
-}
+*/
+
+/// Overwritten DimCommand::commandHandler
+void MStarguider::commandHandler()
+{
+    DimCommand *cmd = getCommand();
+    if (!cmd)
+        return;
+
+    if (cmd==&fDimTPoint)
+    {
+        fTPointMode = 2;
+        fScreenshotName = "";
+        cout << "DimCommand[TPOINT]: " << cmd->itsSize << " " << string((char*)cmd->itsData, cmd->itsSize) << endl;
+        return;
+    }
+
+    if (cmd==&fDimScreenshot && fTPointMode==0)
+    {
+        if (cmd->itsSize<2)
+            return;
+
+        fTPointMode = 2;
+        fScreenshotColor = ((uint8_t*)cmd->itsData)[0];
+        fScreenshotName = string((char*)cmd->itsData+1, cmd->itsSize-1);
+        cout << "DimCommand[SCREENSHOT]: " << fScreenshotName << endl;
+        return;
+    }
+
+    cout << "DimCommand[UNKNOWN]: " << cmd->itsSize << " " << string((char*)cmd->itsData, cmd->itsSize) << endl;
+}
Index: trunk/FACT++/drive/MStarguider.h
===================================================================
--- trunk/FACT++/drive/MStarguider.h	(revision 18626)
+++ trunk/FACT++/drive/MStarguider.h	(revision 18627)
@@ -4,24 +4,11 @@
 #include "PixClient.h"
 #include "MGImage.h"
+#include "Led.h"
+#include "Camera.h"
 
-#include "MPointing.h"
+#include "dic.hxx"
+#include "dis.hxx"
 
-#ifndef MARS_MObservatory
-#include "MObservatory.h"
-#endif
-#ifndef MARS_MTime
-#include "MTime.h"
-#endif
-#ifndef MARS_MGList
-#include "MGList.h"
-#endif
-
-class TArrayF;
-class TGLabel;
-class TGButton;
-class TString;
-//class TSocket;
-
-class TTimer;
+#include <TGButton.h>
 
 class TGMenuBar;
@@ -32,45 +19,24 @@
 
 class MGImage;
-class MGCoordinates;
-
-class StarCatalog;
-class MStarList;
-class MCosy;
 class MCaos;
-// class MStargLeds;
-class MStargHistograms;
-class MGNumStars;
-class MGStarg;
 class FilterLed;
 class Ring;
 
-class Leds;
-#include <TGButton.h>
-class MStarguider : public PixClient, public TGMainFrame
+//class Leds;
+class MStarguider : public PixClient, public TGMainFrame, public DimCommandHandler
 {
 private:
-    PixGetter     *fGetter;
+    DimService fDimData;
+    DimCommand fDimTPoint;
+    DimCommand fDimScreenshot;
 
-    MGList        *fList;
+    Camera        *fGetter;
 
     TGMenuBar     *fMenu;
     MGImage       *fImage;
-    MGImage       *fZoomImage;
-
-    MGStarg       *fGStarg;
-    MGNumStars    *fGNumStars;
 
     TGPopupMenu   *fDisplay;
-    TGPopupMenu   *fMode;
-    TGPopupMenu   *fWritePictures;
     TGPopupMenu   *fSetup;
-    TGPopupMenu   *fOperations;
-
-    TGPopupMenu   *fFileType;
-    TGPopupMenu   *fWriteType;
-    TGPopupMenu   *fWriteRate;
     TGPopupMenu   *fInterpol;
-    TGPopupMenu   *fLimMag;
-    TGPopupMenu   *fChannel;
 
     TGPopupMenu   *fCaosWrite;
@@ -79,47 +45,7 @@
     TGPopupMenu   *fCaOs;
 
-    MGCoordinates *fCRaDec;
-    MGCoordinates *fCZdAz;
-
-    MGCoordinates *fPZdAz;
-    MGCoordinates *fDZdAz;
-    MGCoordinates *fSZdAz;
-
-    TGTextEntry   *fPixSize;
-    TGTextEntry   *fAngle;
-    TGTextEntry   *fCut;
-
-    TGLabel       *fFps;
-    TGLabel       *fPosZoom;
-    TGLabel       *fSkyBright;
-    TGLabel       *fCZdAzText;
-    TGLabel       *fPZdAzText;
-    TGLabel       *fDZdAzText;
-public:
-    TGButton      *fTPoint;
-//    TGButton      *fStargTPoint;
 private:
-    ZdAz          fPos;
-    ZdAz          fD;
-    AltAz         fAltAzOffsetFromTp;
-
-    Int_t          fNumStarsDetected;
-    Int_t          fNumStarsCorrelated;
-
-    StarCatalog   *fSao;
-    MCosy         *fCosy;
     MCaos         *fCaos;
-    MCaos         *fStargCaos;
-    MStargHistograms *fStargHistograms;
-    
-    RaDec *fRaDec;
-
-    TTimer *fTimer;
-    MTime   fTime;
-    MTime   fTimeFromTp;
-
-    std::ofstream *fOutTp;
-    std::ofstream *fOutStargTp;
-//    ofstream *fOutRq;
+    TTimer        *fTimer;
 
     Int_t fDx;
@@ -127,58 +53,37 @@
 
     byte fIntRate;
-    int  fWrtRate;
+//    int  fWrtRate;
 
-    UInt_t fStatus;
     Double_t fLastBright;
-
     Double_t fRadius; // LED radius [cm]
-
-    Int_t fTPointFromCC;
-
-    Int_t fStarguiderW;
-    Int_t fStarguiderH;
-    Int_t fStarguiderX;
-    Int_t fStarguiderY;
-
-    Float_t fSkyOffsetX;  // Offset between camera center and sky position
-    Float_t fSkyOffsetY;  // Offset between camera center and sky position
 
     Float_t fFindStarCut;
     Int_t   fFindStarBox;
 
-    TString fTPointStarName;
-    Float_t fTPointStarMag;
+    int fTPointMode;
+
+    bool fScreenshotColor;
+    std::string fScreenshotName;
+
+    void WritePNG(const char *name, const byte *gbuf, const byte *cbuf);
 
     void Toggle(TGPopupMenu *p, UInt_t id);
     void SwitchOff(TGPopupMenu *p, UInt_t id);
-    void ToggleStargAnalysis();
-//    void ToggleFindStar();
-    void ToggleStarguider();
-    void ToggleCaosFilter();
-    void SetChannel();
-    Int_t CalcTrackingError(const Leds &, MStarList &, ZdAz &, const MTime &, const double &bright, Int_t &num, const Ring &center, Int_t numleds, Int_t numrings);
-    ZdAz TrackingError(TArrayF &alt, TArrayF &az, TArrayF &mag, Int_t &num) const;
     bool Interpolate(const unsigned long n, byte *img) const;
-    void FindStar(const FilterLed &f, const FilterLed &f2, const Ring &center, const MTime &t, Int_t numleds, Int_t numrings);
 
     void InitGui(Int_t channel);
 
-    void UpdatePosZoom();
-    void DrawZoomImage(const byte *img);
-    void DrawCosyImage(const byte *img);
+    //void DrawZoomImage(const byte *img);
+    //void DrawCosyImage(const byte *img);
 
     Bool_t HandleTimer(TTimer *t);
 
-    void SetRotationAngle(Double_t angle);
-    void SetPixSize(Double_t size);
     void SetCut(Double_t cut);
 
-    Bool_t DoTPoint();
-
 public:
-    MStarguider(MObservatory::LocationName_t obs, Int_t channel);
+    MStarguider(Int_t channel);
     virtual ~MStarguider();
 
-    void SetupEnv(TEnv &env);
+    //void SetupEnv(TEnv &env);
 
     void Layout();
@@ -187,10 +92,5 @@
     Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2);
 
-    Bool_t HandleDoubleClick(Event_t *event);
-
-    void SetPointingPosition(RaDec rd);
-    void SetCosy(MCosy *c) { fCosy = c; }
-
-    XY GetCoordinates() const;
+    //Bool_t HandleDoubleClick(Event_t *event);
 
     //
@@ -199,13 +99,5 @@
     void ProcessFrame(const unsigned long n, byte *img, struct timeval *tm);
 
-    void Print(TString &str, Double_t deg) const;
-    void Print(Option_t *o) const { }
-
-    void StartTPoint(char *cmd=0);// { if (fTPointFromCC<0) fTPointFromCC=0; }
-    void StartStarguider(bool on);
-
-    Int_t GetStarguiderMode() const;
-
-    ClassDef(MStarguider, 0)
+    void commandHandler();       /// Overwritten DimCommand::commandHandler
 };
 
