Index: /trunk/MagicSoft/Cosy/Changelog
===================================================================
--- /trunk/MagicSoft/Cosy/Changelog	(revision 2277)
+++ /trunk/MagicSoft/Cosy/Changelog	(revision 2278)
@@ -1,3 +1,157 @@
                                                                   -*-*- END -*-*-
+ 2003/07/15 - Thomas Bretz (La Palma)
+
+   * leds.txt:
+     - new configuration file
+     
+   * main/MCaos.[h,cc]:
+     - added
+
+   * main/MStarguider.[h,cc]:
+     - added
+     
+   * videodev/PixGetter.[h,cc]:
+     - added
+     
+   * videodev/FilterLed.[h,cc]:
+     - added
+     - highly optimized for speed!
+     
+   * Makefile.conf.linux:
+     - added -O5 optimization option
+     
+   * bending_magic.txt:
+     - changed to newly determined bending correction
+     
+   * cosy.cc:
+     - included starguider
+     - do not overwrite log file
+     - added EXPERT mode
+     
+   * starg.cc:
+     - changed from MGStarguider to MStarguider
+     - simplified by use of PixGetter
+
+   * aposs/Manual.m:
+     - removed a wrong character
+     
+   * base/coord.h:
+     - changed operator<<
+     
+   * base/timer.[h,cc]:
+     - const argument for copy constructor
+     - added operator<<
+
+   * candrv/network.[h,cc], candrv/nodedrv.[h,cc]:
+     - added data argument to HandleSDOOK
+     
+   * candrv/vmodican.cc:
+     - return from constructor if Module not found
+     
+   * caos/Led.[h,cc]:
+     - included TROOT
+     - formated Print-output
+     - Initialize fDx, fDy and fDphi
+     - added AddOffset
+     
+   * caos/Leds.[h,cc]:
+     - added Add
+     
+   * caos/Ring.[h,cc]:
+     - added constructor
+     - use hypot
+     - devide by n instead of n-1
+     - formated Print-output
+
+   * caos/Rings.[h,cc]:
+     - Do not calculated Center for less than 5 rings
+     - included Radius filter
+     
+   * catalog/StarCatalog.cc:
+     - Do not output Alt/Az, fAzCnt, fAltMin, etc.
+     
+   * devdrv/macs.[h,cc]:
+     - added data argument to HandleSDOOK
+     - implemented StartNode() and check for initialization
+     - changed minimum MACS software version to 0.69
+     - moved SetNoWait to the MACS software
+     - removed StopMotor
+     
+   * devdrv/shaftencoder.[h,cc]:
+     - initialize all values to 0
+
+   * gui/GuiLinkDef.h:
+     - removed MGStarguider
+     
+   * gui/MGAccuracy.cc:
+     - small change
+     
+   * gui/MGCosy.[h,cc]:
+     - added moving in Zd/Az by buttons while tracking (Experts only)
+     - some changes to the menu bar
+     - removed displaying offsets
+     - changed EXPERT mode
+
+   * gui/MGEmbeddedCanvas.[h,cc]:
+     - changed range argument in constructor to double (problems
+       with the optimization - strange!)
+
+   * gui/MGImage.[h,cc]:
+     - switched from my own code completely to tVirtualX code
+       + requires root 3.05./05!
+     - highly optimized for speed!
+     - at the moment only 32 bit screen depth is supported!
+     
+   * gui/MGSkyPosition.[h,cc]:
+     - fixed position of vega
+     - added new bright star
+     - cut displaying position into two. \n not supported by
+       root 3.05/05
+     
+   * gui/MGVelocity.[h,cc]:
+     - removed displaying numbers
+     
+   * gui/Makefile:
+     - removed MGStarguider
+     
+   * main/MCosy.[h,cc]:
+     - added output of time when tracking is started or stopped
+     - added output of time when slewing is started or stopped
+     - removed sign changing limits in LimitSpeed
+     - changed EXPERT mode
+     - added MStarguider support
+     - fixed Calc-Alt/Az
+     - added GetPointingPos     
+     - removed fOffset from Update
+     
+   * main/MainLinkDef.h, main/Makefile:
+     - added MStarguider
+
+   * main/MStarguider.[h,cc]:
+     - added
+     
+   * tpoint/tpointfit.C:
+     - fixed reading of data
+     - removed absolute path
+
+   * videodev/Camera.[h,cc]:
+     - small simplification
+     - derived from new PixGetter
+     
+   * videodev/CaosFilter.[h,cc], videodev/Filter.[h,cc]:
+     - optimized, but not used anymore, will be removed soon
+     
+   * videodev/Makefile, videodev/VideodevLinkDef.h:
+     - added PixGetter to Makefile
+     - removed Filter
+     - removed Filter2
+     - removed CaosFilter
+     - added FilterLed
+     
+   * videodev/PngReader.h:
+     - derived from PixGetter
+
+
+     
  2003/05/05 - Thomas Bretz
 
Index: /trunk/MagicSoft/Cosy/Makefile.conf.linux
===================================================================
--- /trunk/MagicSoft/Cosy/Makefile.conf.linux	(revision 2277)
+++ /trunk/MagicSoft/Cosy/Makefile.conf.linux	(revision 2278)
@@ -12,8 +12,8 @@
 # compilers
 
-CC            = gcc
-CXX           = g++
-F77           = f77
-AR            = ar -rc
+CC  = gcc
+CXX = g++
+F77 = f77
+AR  = ar -rc
 #
 #  ----->>>   settings for compilation
@@ -21,9 +21,9 @@
 
 #-O2
-OPTIM    = -Wall -fno-rtti -fno-exceptions -fPIC -Wtraditional -Wpointer-arith -Wcast-align -Wwrite-strings -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Woverloaded-virtual -D_REENTRANT 
-DEBUG    = -g
+OPTIM = -O5 -Wall -fno-exceptions -fPIC -Wtraditional -Wpointer-arith -Wcast-align -Wwrite-strings -Wconversion -Wstrict-prototypes -Wmissing-prototypes -Wnested-externs -Woverloaded-virtual -D_REENTRANT 
+DEBUG = -g
 
-MARS_LIB  = -Llib $(SUBDIRS:%=-l%)
-INCLUDES  = -I. $(SUBDIRS:%=-I%)
+MARS_LIB = -Llib $(SUBDIRS:%=-l%) -lX11 -lXpm
+INCLUDES = -I. $(SUBDIRS:%=-I%)
 
 # uncomment this for quiet compilation
Index: /trunk/MagicSoft/Cosy/aposs/Manual.m
===================================================================
--- /trunk/MagicSoft/Cosy/aposs/Manual.m	(revision 2277)
+++ /trunk/MagicSoft/Cosy/aposs/Manual.m	(revision 2278)
@@ -1,3 +1,3 @@
-#/* ----------------------------------------------------------------------- */
+/* ----------------------------------------------------------------------- */
 /*                                                                         */               
 /*  Version:                                                               */
Index: /trunk/MagicSoft/Cosy/base/coord.h
===================================================================
--- /trunk/MagicSoft/Cosy/base/coord.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/base/coord.h	(revision 2278)
@@ -74,5 +74,5 @@
 
 inline ifstream& operator>>(ifstream &in,  XY &xy) { in  >> xy.fX; in  >> xy.fY; return in; }
-inline ofstream& operator<<(ofstream &out, XY &xy) { out << xy.fX << " " << xy.fY; return out; }
+inline ofstream& operator<<(ofstream &out, const XY &xy) { out << xy.X() << " " << xy.Y(); return out; }
 
 class AltAz : public XY
Index: /trunk/MagicSoft/Cosy/base/timer.cc
===================================================================
--- /trunk/MagicSoft/Cosy/base/timer.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/base/timer.cc	(revision 2278)
@@ -68,5 +68,5 @@
 }
 
-Timer::Timer(Timer &t)
+Timer::Timer(const Timer &t)
 {
     fMs   = t.fMs;
@@ -124,5 +124,5 @@
 }
 
-Timer::operator double() //[s]
+Timer::operator double() const //[s]
 {
     return fMs+fSecs;
@@ -142,2 +142,12 @@
 }
 
+ostream &operator<<(ostream &out, Timer &t)
+{
+    char text[256];
+
+    sprintf(text, "%d/%02d/%02d %d:%02d:%02d.%01li",
+            t.fYea, t.fMon, t.fDay, t.fHor, t.fMin, t.fSec, (long)(10.0*t.fMs));
+
+    out << text;
+    return out;
+}
Index: /trunk/MagicSoft/Cosy/base/timer.h
===================================================================
--- /trunk/MagicSoft/Cosy/base/timer.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/base/timer.h	(revision 2278)
@@ -1,9 +1,12 @@
-#ifndef TIMER_H
-#define TIMER_H
+#ifndef COSY_Timer
+#define COSY_Timer
 
 struct timeval;
 
+class ostream;
+
 class Timer
 {
+    friend ostream &operator<<(ostream &out, Timer &t);
 private:
     double fMs;
@@ -28,5 +31,5 @@
     Timer(double t);
     Timer(struct timeval *tv);
-    Timer(Timer &t);
+    Timer(const Timer &t);
 
     void SetTimer(int tv_sec, double tv_usec);
@@ -54,6 +57,8 @@
     void Print();
 
-    operator double(); //[s]
+    operator double() const; //[s]
 };
 
+ostream &operator<<(ostream &out, Timer &t);
+
 #endif
Index: /trunk/MagicSoft/Cosy/bending_magic.txt
===================================================================
--- /trunk/MagicSoft/Cosy/bending_magic.txt	(revision 2277)
+++ /trunk/MagicSoft/Cosy/bending_magic.txt	(revision 2278)
@@ -1,10 +1,10 @@
-MAGIC1 2003/03/07 20:40:26.88604 
+MAGIC1 2003/07/15 13:28:46.783326
 S   00   000000   000000  0000000
- IA     499.0188249 -1
- IE     -10.741869 -1
- CA     -0.23939033 -1
- NPAE   2.3247554 -1
- AN     0          -1
- AW     -1.1901258 -1
+ IA     141.81712 -1
+ IE     -9.0736584 -1
+ CA     0 -1
+ NPAE   0.14617499 -1
+ AN     0 -1
+ AW     -1.5469003 -1
  NRX    0 -1
  NRY    0 -1
@@ -15,5 +15,3 @@
  ECEC   0 -1
  ACEC   0 -1
- MAGIC1 0 -1
- MAGIC2 0 -1
 END
Index: /trunk/MagicSoft/Cosy/candrv/network.cc
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/network.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/candrv/network.cc	(revision 2278)
@@ -105,5 +105,5 @@
 
         case kSDO_TX_OK:     // answer to a SDO_TX message
-            fNodes[node]->HandleSDOOK(idx, subidx, tv);
+            fNodes[node]->HandleSDOOK(idx, subidx, data, tv);
             return;
 
Index: /trunk/MagicSoft/Cosy/candrv/nodedrv.cc
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/nodedrv.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/candrv/nodedrv.cc	(revision 2278)
@@ -144,7 +144,9 @@
 //
 // Print an "SDO idx/subidx set." from this device message.
-// This output is never redirected to the GUI
-//
-void NodeDrv::HandleSDOOK(WORD_t idx, BYTE_t subidx, timeval_t *tv)
+// This output is never redirected to the GUI.
+// In standard CANOpen operation data is meaningless (we are using
+// it in the 'non-standard' CANOpen communication with the MACS)
+//
+void NodeDrv::HandleSDOOK(WORD_t idx, BYTE_t subidx, LWORD_t data, timeval_t *tv)
 {
     const Bool_t gui = lout.IsOutputDeviceEnabled(MLog::eGui);
Index: /trunk/MagicSoft/Cosy/candrv/nodedrv.h
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/nodedrv.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/candrv/nodedrv.h	(revision 2278)
@@ -69,5 +69,5 @@
 
     virtual void HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, timeval_t *tv);
-    virtual void HandleSDOOK(WORD_t idx, BYTE_t subidx, timeval_t *tv);
+    virtual void HandleSDOOK(WORD_t idx, BYTE_t subidx, LWORD_t data, timeval_t *tv);
     virtual void HandleSDOError(LWORD_t data);
 
Index: /trunk/MagicSoft/Cosy/candrv/vmodican.cc
===================================================================
--- /trunk/MagicSoft/Cosy/candrv/vmodican.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/candrv/vmodican.cc	(revision 2278)
@@ -950,4 +950,5 @@
         cout << "Cannot open device '" << dev << "'... exit." << endl;
         cout << strerror(errno) << endl;
+        return;
 //        exit(1);                       // open module
     }
Index: /trunk/MagicSoft/Cosy/caos/Led.cc
===================================================================
--- /trunk/MagicSoft/Cosy/caos/Led.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/caos/Led.cc	(revision 2278)
@@ -2,4 +2,6 @@
 
 #include <iostream.h>
+
+#include <TROOT.h>
 #include <TMath.h>
 
@@ -16,7 +18,7 @@
 {
     cout << "Led: ";
-    cout << "x="   << fX   << "+-" << fDx   << ", ";
-    cout << "y="   << fY   << "+-" << fDy   << ", ";
-    cout << "phi=" << fPhi << "+-" << fDphi << ", ";
+    cout << "x="   << Form("%5.1f", fX)   << "+-" << fDx   << ", ";
+    cout << "y="   << Form("%5.1f", fY)   << "+-" << fDy   << ", ";
+    cout << "phi=" << Form("%6.1f", fPhi) << "+-" << fDphi << ", ";
     cout << "mag=" << fMag << endl;
 }
Index: /trunk/MagicSoft/Cosy/caos/Led.h
===================================================================
--- /trunk/MagicSoft/Cosy/caos/Led.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/caos/Led.h	(revision 2278)
@@ -23,5 +23,5 @@
 public:
     Led(Double_t x=0, Double_t y=0, Double_t dx=0, Double_t dy=0, Double_t mag=0) :
-        fX(x), fY(y), fDx(dx), fDy(dy), fMag(mag)
+        fX(x), fY(y), fPhi(0), fDx(dx), fDy(dy), fDphi(-1), fMag(mag)
     {
     }
@@ -48,5 +48,7 @@
     Double_t GetMag() const  { return fMag; }
 
-        Bool_t IsSortable() const { return kTRUE; }
+    void AddOffset(Double_t dx, Double_t dy) { fX+=dx; fY+=dy; }
+
+    Bool_t IsSortable() const { return kTRUE; }
 
     void CalcPhi(const Ring &ring);
Index: /trunk/MagicSoft/Cosy/caos/Leds.cc
===================================================================
--- /trunk/MagicSoft/Cosy/caos/Leds.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/caos/Leds.cc	(revision 2278)
@@ -12,4 +12,9 @@
 }
 
+void Leds::Add(Double_t x, Double_t y, Double_t dx, Double_t dy, Double_t mag)
+{
+    Set(GetEntriesFast(), x, y, dx, dy, mag);
+}
+
 void Leds::Print(Option_t *o=NULL) const
 {
Index: /trunk/MagicSoft/Cosy/caos/Leds.h
===================================================================
--- /trunk/MagicSoft/Cosy/caos/Leds.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/caos/Leds.h	(revision 2278)
@@ -19,5 +19,6 @@
     //
 
-    void Set(Int_t i, Double_t x, Double_t y, Double_t dx, Double_t dy, Double_t mag);
+    void Set(Int_t i, Double_t x, Double_t y, Double_t dx=0, Double_t dy=0, Double_t mag=0);
+    void Add(Double_t x, Double_t y, Double_t dx=0, Double_t dy=0, Double_t mag=0);
 
     //nst Led &GetLed(int i) const { return *(Led*)( (*this)[i] ); }
Index: /trunk/MagicSoft/Cosy/caos/Ring.cc
===================================================================
--- /trunk/MagicSoft/Cosy/caos/Ring.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/caos/Ring.cc	(revision 2278)
@@ -9,4 +9,10 @@
 
 ClassImp(Ring);
+
+Ring::Ring() :
+    fX(0), fY(0), fR(0), fPhi(0), fDx(-1), fDy(-1), fDr(-1), fDphi(-1)
+{
+}
+
 
 bool Ring::CalcCenter(const Leds &leds, Int_t i, Int_t j, Int_t k)
@@ -53,5 +59,5 @@
     fY = ((m2*(leds(i).GetY() + leds(j).GetY()) +m1*m2*(leds(k).GetX() - leds(i).GetX())-m1*(leds(j).GetY() + leds(k).GetY()))/(m2-m1)/2);
 
-    fR = sqrt(sqr(fX-leds(i).GetX())+sqr(fY-leds(i).GetY()));
+    fR = hypot(fX-leds(i).GetX(), fY-leds(i).GetY());
 
     return kTRUE;
@@ -101,7 +107,7 @@
     }
 
-    fDx=sqrt(fDx)/(n-1);
-    fDy=sqrt(fDy)/(n-1);
-    fDr=sqrt(fDr)/(n-1);
+    fDx=sqrt(fDx)/n;
+    fDy=sqrt(fDy)/n;
+    fDr=sqrt(fDr)/n;
 }
 
@@ -109,8 +115,8 @@
 {
     cout << "Ring: ";
-    cout << "x="   << fX   << "+-" << fDx   << ", ";
-    cout << "y="   << fY   << "+-" << fDy   << ", ";
-    cout << "r="   << fR   << "+-" << fDr   << ", ";
-    cout << "phi=" << fPhi << "+-" << fDphi << endl;
+    cout << "x="   << Form("%5.1f", fX) << "+-" << Form("%.1f", fDx) << ", ";
+    cout << "y="   << Form("%5.1f", fY) << "+-" << Form("%.1f", fDy) << ", ";
+    cout << "r="   << Form("%5.1f", fR) << "+-" << Form("%.1f", fDr) << ", ";
+    cout << "phi=" << fPhi              << "+-" << fDphi << endl;
 }
 
Index: /trunk/MagicSoft/Cosy/caos/Ring.h
===================================================================
--- /trunk/MagicSoft/Cosy/caos/Ring.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/caos/Ring.h	(revision 2278)
@@ -32,4 +32,5 @@
 
 public:
+    Ring();
 
     Double_t GetX() const   { return fX; }
Index: /trunk/MagicSoft/Cosy/caos/Rings.cc
===================================================================
--- /trunk/MagicSoft/Cosy/caos/Rings.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/caos/Rings.cc	(revision 2278)
@@ -8,9 +8,12 @@
 ClassImp(Rings);
 
-void Rings::CalcCenters(const Leds &leds)
+void Rings::CalcCenters(const Leds &leds, Float_t min, Float_t max)
 {
     Clear();
 
     int nPoints = leds.GetEntries();
+
+    if (nPoints<5)
+        return;
 
     int n=0;
@@ -27,4 +30,13 @@
                 }
 
+                //
+                //filter and remove rings with too big or too small radius
+                //
+                if ((min>=0&&ring.GetR()<min) || (max>=0&&ring.GetR()>max))
+                {
+                    RemoveAt(n);
+                    continue;
+                }
+
                 n++;
             }
@@ -33,7 +45,7 @@
 }
 
-void Rings::CalcRings(Leds &leds)
+void Rings::CalcRings(Leds &leds, Float_t min, Float_t max)
 {
-    CalcCenters(leds);
+    CalcCenters(leds, min, max);
 
     fCenter.InterpolCenters(*this);
@@ -51,6 +63,9 @@
 {
     cout << "Number of Rings: " << GetEntries() << endl;
-    TClonesArray::Print();
-    cout << "Center: " << endl;
-    fCenter.Print();
+    if (GetEntriesFast()>0)
+    {
+        TClonesArray::Print();
+        cout << "Center: " << endl;
+        fCenter.Print();
+    }
 }
Index: /trunk/MagicSoft/Cosy/caos/Rings.h
===================================================================
--- /trunk/MagicSoft/Cosy/caos/Rings.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/caos/Rings.h	(revision 2278)
@@ -17,10 +17,10 @@
     Ring fCenter;
 
-    void CalcCenters(const Leds &leds);
+    void CalcCenters(const Leds &leds, Float_t min, Float_t max);
 
 public:
     Rings() : TClonesArray("Ring", 1) {}
 
-    void CalcRings(Leds &leds);
+    void CalcRings(Leds &leds, Float_t min=-1, Float_t max=-1);
 
     void Print(Option_t *o=NULL) const;
Index: /trunk/MagicSoft/Cosy/catalog/StarCatalog.cc
===================================================================
--- /trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 2278)
@@ -93,6 +93,6 @@
     fAltAz = CalcAltAz(fRaDec);
 
-    cout << "Alt: " << 360.0/D2PI*fAltAz.Alt() << "  ";
-    cout << "Az: "  << 360.0/D2PI*fAltAz.Az()  << endl;
+    //cout << "Alt: " << 360.0/D2PI*fAltAz.Alt() << "  ";
+    //cout << "Az: "  << 360.0/D2PI*fAltAz.Az()  << endl;
 
     CalcRaDecRange();
@@ -170,5 +170,5 @@
             fAzCnt++;
 
-    cout << "fAzCnt: " << setw(3) << fAzCnt << "  " << flush;
+    //cout << "fAzCnt: " << setw(3) << fAzCnt << "  " << flush;
 
     //
@@ -204,6 +204,6 @@
     }
 
-    cout << "fAltMin: " << setw(3) << fAltMin << "  ";
-    cout << "fAltMax: " << setw(3) << fAltMax << endl;
+    //cout << "fAltMin: " << setw(3) << fAltMin << "  ";
+    //cout << "fAltMax: " << setw(3) << fAltMax << endl;
 }
 
@@ -291,5 +291,5 @@
         if (fRa0[x])
             fRaCnt++;
-    cout << "fRaCnt: " << setw(3) << fRaCnt << "  " << flush;
+    //cout << "fRaCnt: " << setw(3) << fRaCnt << "  " << flush;
 
     //
@@ -323,6 +323,6 @@
     }
 
-    cout << "fDecMin: " << setw(3) << fDecMin << "  ";
-    cout << "fDecMax: " << setw(3) << fDecMax << endl;
+    //cout << "fDecMin: " << setw(3) << fDecMin << "  ";
+    //cout << "fDecMax: " << setw(3) << fDecMax << endl;
 }
 
@@ -707,5 +707,5 @@
     }
 
-    cout << " " << count+deleted << "-" << deleted << "=" << count << " " << endl;
+    cout << "Showing " << count+deleted << "-" << deleted << "=" << count << " stars." << endl;
 }
 
Index: /trunk/MagicSoft/Cosy/cosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/cosy.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/cosy.cc	(revision 2278)
@@ -11,4 +11,10 @@
 #include "base/timer.h"
 
+#include "Camera.h"
+#include "PngReader.h"
+#include "MStarguider.h"
+
+#define EXPERT
+
 #define clog(txt) \
     { \
@@ -20,4 +26,8 @@
 
 TROOT root("Cosy", "Magic Control System");
+
+/* ---------------------------------------------------------------------- */
+//extern void InitGui();
+//VoidFuncPtr_t initfuncs[] = { InitGui, 0 };
 
 int main(int argc, char **argv)
@@ -37,5 +47,14 @@
     // this must move to MGCosy !!!! (or MApplication)
     //
-    MLog *l = new MLog("log/cosy.log", kTRUE);
+    int i=0;
+    char name[100];
+    while (1)
+    {
+        sprintf(name, "log/cosy%03d.log", i++);
+        if (gSystem->AccessPathName(name, kFileExists))
+            break;
+    }
+
+    MLog *l = new MLog(name, kTRUE);
     MLog &lout = *l;
 
@@ -75,10 +94,30 @@
 
     clog("- Starting MCosy.");
+#ifndef EXPERT
     lout.DisableOutputDevice(MLog::eStdout);
+#endif
     cosy->Start();
 
+    clog("- Starting Camera.");
+    MStarguider *client=new MStarguider(MObservatory::kMagic1);
+    Camera *cam = new Camera(*client);
+    cam->Loop(0);
+
+    cosy->SetStarguider(client);
+    client->SetCosy(cosy);
+
     clog("- Starting mainloop.");
+#ifndef EXPERT
     lout.DisableOutputDevice(MLog::eStdout);
+#endif
     app->Run(kTRUE);
+
+    client->SetCosy(NULL);
+    cosy->SetStarguider(NULL);
+
+    clog("- Stopping starg.");
+    cam->ExitLoop();
+    delete cam;
+    delete client;
 
     clog("- Stopping cosy.");
Index: /trunk/MagicSoft/Cosy/devdrv/macs.cc
===================================================================
--- /trunk/MagicSoft/Cosy/devdrv/macs.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/devdrv/macs.cc	(revision 2278)
@@ -127,7 +127,15 @@
 }
 
-void Macs::HandleSDOOK(WORD_t idx, BYTE_t subidx, timeval_t *tv)
-{
-//    cout << "Node #" << dec << (int)GetId() << ": Sdo=" << hex << idx  << "/" << (int)subidx << " set." << endl;
+void Macs::HandleSDOOK(WORD_t idx, BYTE_t subidx, LWORD_t data, timeval_t *tv)
+{
+    //    cout << "Node #" << dec << (int)GetId() << ": Sdo=" << hex << idx  << "/" << (int)subidx << " set." << endl;
+
+    // If a real drive operation is requested from the MACS and
+    // the MACS is not correctly initialized the operation is
+    // rejected. (This is expecially harmfull if the NoWait state
+    // is set incorrectly)
+    if (data)
+        SetZombie();
+
     switch (idx)
     {
@@ -201,5 +209,5 @@
 
     }
-    NodeDrv::HandleSDOOK(idx, subidx, tv);
+    NodeDrv::HandleSDOOK(idx, subidx, data, tv);
 }
 
@@ -223,4 +231,16 @@
     SendSDO(0x1800, 1, (LWORD_t)(flag?0:1)<<31); 
     WaitForSdo(0x1800, 1);           
+}
+
+void Macs::StartNode()
+{
+    //
+    // Switch node from pre-operational state to operational state
+    // (This is not CANOpen compatible)
+    // After this the MACS will react on real movement commands.
+    //
+    lout << "- " << GetNodeName() << ": Starting Node." << endl;
+    SendSDO(0x1000, 1, (LWORD_t)1);
+    WaitForSdo(0x1000, 1);
 }
 
@@ -251,5 +271,5 @@
     StopGuarding();
 
-    usleep(2000*GetGuardTime());
+// ****FIXME***   usleep(2000*GetGuardTime());
 
     lout << "- " << GetNodeName() << ": Requesting Mac Software Version." << endl;
@@ -257,5 +277,6 @@
     WaitForSdo(0x100a);
     // FIXME! Not statically linked!
-    if (fSoftVersion<0x00000044) // 00.68
+    //if (fSoftVersion<0x00000044) // 00.68
+    if (fSoftVersion<0x00000045) // 00.69
     {
         lout << GetNodeName() << " - Software Version too old!" << endl;
@@ -269,19 +290,23 @@
     ReqVelRes(); // Init fVelRes
 
-    lout << "- " << GetNodeName() << ": Motor on." << endl;
-    SendSDO(0x3000, string('o', 'n'));
-    WaitForSdo(0x3000);
+    /* Should not be necessary anymore. This is done by the MACS itself.
+     lout << "- " << GetNodeName() << ": Motor on." << endl;
+     SendSDO(0x3000, string('o', 'n'));
+     WaitForSdo(0x3000);
+     */
 
     SetPDO1On(FALSE); // this is a workaround for the Macs
     SetPDO1On(TRUE);
 
-    SetNoWait(TRUE);
-
-//    StartGuarding(200, 2, kFALSE); // Using PDO1 @ 100ms
-
-//    StartGuarding(250, 4);
-//    StartHostGuarding();
-}
-
+    //This is now standard in the MACS
+    //SetNoWait(TRUE);
+
+    // StartGuarding(200, 2, kFALSE); // Using PDO1 @ 100ms
+    // StartGuarding(250, 4);
+    // StartHostGuarding();
+
+    StartNode();
+}
+/*
 void Macs::StopMotor()
 {
@@ -292,10 +317,11 @@
     WaitForSdo(0x3000);
 }
-
+*/
 void Macs::StopDevice()
 {
     //EnableTimeout(kFALSE);
 
-    SetNoWait(FALSE);
+    //No need to switch it off.
+    //SetNoWait(FALSE);
 
     StopHostGuarding();
@@ -402,4 +428,5 @@
 }
 
+/*
 void Macs::SetNoWait(BYTE_t flag)
 {
@@ -408,4 +435,5 @@
     WaitForSdo(0x3008);
 }
+*/
 
 void Macs::StartVelSync()
Index: /trunk/MagicSoft/Cosy/devdrv/macs.h
===================================================================
--- /trunk/MagicSoft/Cosy/devdrv/macs.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/devdrv/macs.h	(revision 2278)
@@ -34,5 +34,5 @@
 
     void HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, timeval_t *tv);
-    void HandleSDOOK(WORD_t idx, BYTE_t subidx, timeval_t *tv);
+    void HandleSDOOK(WORD_t idx, BYTE_t subidx, LWORD_t data, timeval_t *tv);
     //void HandleSDOError(LWORD_t data) { NodeDrv::HandleSDOError(data); }
 
@@ -84,5 +84,5 @@
     void SetDeceleration(LWORD_t dec);
     void SetVelocity(LWORD_t vel);
-    void SetNoWait(BYTE_t flag=TRUE);
+    //void SetNoWait(BYTE_t flag=TRUE);
     void SetRpmMode(BYTE_t mode=TRUE);
     void SetRpmVelocity(LWORDS_t cvel);
@@ -99,5 +99,7 @@
     void StartAbsPos(LWORDS_t pos);
 
-    void StopMotor();
+    //void StopMotor();
+
+    void StartNode();
 
     int IsPositioning() const { return fPosActive; }
Index: /trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc
===================================================================
--- /trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/devdrv/shaftencoder.cc	(revision 2278)
@@ -16,5 +16,6 @@
 
 ShaftEncoder::ShaftEncoder(const BYTE_t nodeid, const char *name, MLog &out)
-    : NodeDrv(nodeid, name, out), fLabel(NULL), fPosHasChanged(false)
+    : NodeDrv(nodeid, name, out), fPos(0), fVel(0), fAcc(0),
+    fTurn(0), fLabel(NULL), fPosHasChanged(false)
 {
 }
Index: /trunk/MagicSoft/Cosy/gui/GuiLinkDef.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/GuiLinkDef.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/GuiLinkDef.h	(revision 2278)
@@ -18,5 +18,4 @@
 
 #pragma link C++ class MGCosy+;
-#pragma link C++ class MGStarguider+;
 
 #endif
Index: /trunk/MagicSoft/Cosy/gui/MGAccuracy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGAccuracy.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/MGAccuracy.cc	(revision 2278)
@@ -170,5 +170,5 @@
 
 MGAccuracy::MGAccuracy(const TGWindow* p, const UInt_t w)
-: MGEmbeddedCanvas("Accuracy", p, w, 75*4)
+: MGEmbeddedCanvas("Accuracy", p, w, 300)
 {
     DrawCoordinateSystem();
Index: /trunk/MagicSoft/Cosy/gui/MGCosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGCosy.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/MGCosy.cc	(revision 2278)
@@ -55,8 +55,13 @@
     kPB_DECp,
     kPB_DECm,
+    kPB_ZDp,
+    kPB_ZDm,
+    kPB_AZp,
+    kPB_AZm,
     kPB_LoadBending,
     kPB_ResetBending,
     kPB_CALIBSE,
     kCB_PredefPos,
+    kCB_StarList,
 
     // kLog
@@ -85,7 +90,7 @@
     TGPopupMenu *logmenu = new TGPopupMenu(gClient->GetRoot());
     logmenu->AddEntry("&Copy Selected", kLogCopy);
-    logmenu->AddEntry("Clear all",      kLogClear);
+    logmenu->AddEntry("&Select All",    kLogSelect);
     logmenu->AddSeparator();
-    logmenu->AddEntry("Select All",     kLogSelect);
+    logmenu->AddEntry("Clear &all",     kLogClear);
     /*
      logmenu->AddSeparator();
@@ -93,6 +98,6 @@
      */
     logmenu->AddSeparator();
-    logmenu->AddEntry("Save",           kLogSave);
-    logmenu->AddEntry("Save append",    kLogAppend);
+    logmenu->AddEntry("Sa&ve",          kLogSave);
+    logmenu->AddEntry("Save a&ppend",   kLogAppend);
     logmenu->Associate(this);
 
@@ -160,5 +165,5 @@
     fList->Add(fLabel2);
     fList->Add(fLabel3);
-
+/*
     l = new TGLabel(f, "Offset-Zd:");
     l->Move(x-60, y+102);
@@ -177,19 +182,19 @@
     fList->Add(fOffsetZd);
     fList->Add(fOffsetAz);
-
+*/
     l = new TGLabel(f, "Ra (estimated):");
-    l->Move(x-60, y+142);
+    l->Move(x-60, y+142-20);
     fList->Add(l);
 
     l = new TGLabel(f, "Ra (nominal):");
-    l->Move(x-60, y+159);
+    l->Move(x-60, y+159-20);
     fList->Add(l);
 
     l = new TGLabel(f, "Dec (estimated):");
-    l->Move(x-60, y+182);
+    l->Move(x-60, y+182-20);
     fList->Add(l);
 
     l = new TGLabel(f, "Dec (nominal):");
-    l->Move(x-60, y+199);
+    l->Move(x-60, y+199-20);
     fList->Add(l);
 
@@ -214,8 +219,8 @@
     fZdSoll->SetTextJustify(kTextRight);
     fAzSoll->SetTextJustify(kTextRight);
-    fRaEst->Move(x+30, y+142);
-    fRaSoll->Move(x+30, y+159);
-    fDecEst->Move(x+30, y+182);
-    fDecSoll->Move(x+30, y+199);
+    fRaEst->Move(x+30, y+142-20);
+    fRaSoll->Move(x+30, y+159-20);
+    fDecEst->Move(x+30, y+182-20);
+    fDecSoll->Move(x+30, y+199-20);
     fZdSoll->Move(x+30, y+222);
     fAzSoll->Move(x+30, y+239);
@@ -311,5 +316,5 @@
     if (!fin)
     {
-        cout << "ERROR: Predifined posiion in 'prepos.txt' not found." << endl;
+        cout << "ERROR: Predifined positions in 'prepos.txt' not found." << endl;
         return;
     }
@@ -329,10 +334,47 @@
 }
 
-void MGCosy::CreateTabs(TGTab *fTab)
+void MGCosy::CreateStarList(TGCompositeFrame *tf1)
+{
+    /*
+    TGComboBox *box = new TGComboBox(tf1, kCB_StarList);
+    box->Resize(120, 20);
+    box->Associate(this);
+
+    TGLayoutHints *lay = new TGLayoutHints(kLHintsLeft|kLHintsTop,
+                                           27, 0, 200, 0);
+    tf1->AddFrame(box, lay);
+
+    fList->Add(box);
+    fList->Add(lay);
+
+    ifstream fin("stars.txt");
+    if (!fin)
+    {
+        cout << "ERROR: Star List 'stars.txt' not found." << endl;
+        return;
+    }
+
+    int i=0;
+    while (1)
+    {
+        TString str;
+        Int_t h, m, s, d, am, as;
+        fin >> h >> m >> s >> d >> am >> as;
+        if (!fin)
+            break;
+
+        box->AddEntry(str, i++);
+
+        fStarList.Add(zd, az);
+        }
+        */
+}
+
+void MGCosy::CreateTabs()
 {
     TGCompositeFrame *tf1 = fTab->AddTab("Position");
     TGCompositeFrame *tf2 = fTab->AddTab("Track");
+    TGCompositeFrame *tf4 = fTab->AddTab("Calib");
 #ifdef EXPERT
-    TGCompositeFrame *tf4 = fTab->AddTab("Calib");
     TGCompositeFrame *tf5 = fTab->AddTab("Test SE");
     TGCompositeFrame *tf6 = fTab->AddTab("Gear");
@@ -345,4 +387,5 @@
 
     CreatePredefinedPos(tf1);
+    CreateStarList(tf4);
 
     fCRaDec = new MGCoordinates(tf2, kETypeRaDec);
@@ -350,9 +393,7 @@
     fList->Add(fCRaDec);
 
-#ifdef EXPERT
     fCCalib = new MGCoordinates(tf4, kETypeRaDec);
     fCCalib->Move(27, 105);
     fList->Add(fCCalib);
-#endif
 
     const int x=15;
@@ -386,5 +427,4 @@
     fList->Add(l3);
 
-#ifdef EXPERT
     l1 = new TGLabel(tf4, "Start the calibration using the Start button.");
     l2 = new TGLabel(tf4, "Write a coordinate pair to a TPoint file using");
@@ -401,4 +441,5 @@
     fList->Add(l4);
 
+#ifdef EXPERT
     l1 = new TGLabel(tf5, "START starts histograming the differences of");
     l2 = new TGLabel(tf5, "the two shaftencoders at the elevation axis.");
@@ -495,7 +536,8 @@
     fList->Add(but);
     */
+#ifdef EXPERT
     but = new TGTextButton(tf4, "Load", kPB_LoadBending);
-    but->Resize(80, 25);
-    but->Move(25, 190);
+    but->Resize(50, 25);
+    but->Move(151, 185);
     but->SetToolTipText("Load bending corrections from file 'bending.txt'");
     but->Associate(this);
@@ -503,14 +545,40 @@
 
     but = new TGTextButton(tf4, "Reset", kPB_ResetBending);
-    but->Resize(80, 25);
-    but->Move(25, 220);
+    but->Resize(50, 25);
+    but->Move(206, 185);
     but->SetToolTipText("Reset bending correction (coefficients=0)");
     but->Associate(this);
     fList->Add(but);
+#endif EXPERT
 
     but = new TGTextButton(tf4, "TPoint", kPB_TPOINT);
-    but->Resize(80, 25);
-    but->Move(25, 250);
+    but->Resize(50, 25);
+    but->Move(176/*231*/, 213);
     but->SetToolTipText("Trigger writing a tpoint coordinate pair.");
+    but->Associate(this);
+    fList->Add(but);
+
+    but= new TGTextButton(tf4, "Zd -",  kPB_ZDm);
+    but->Resize(50, 25);
+    but->Move(25, 210);
+    but->SetToolTipText("Zenith Distance -= 1SE");
+    but->Associate(this);
+    fList->Add(but);
+    but= new TGTextButton(tf4, "Zd +",  kPB_ZDp);
+    but->Resize(50, 25);
+    but->Move(90, 210);
+    but->SetToolTipText("Zenith Distance += 1SE");
+    but->Associate(this);
+    fList->Add(but);
+    but= new TGTextButton(tf4, "Az +",  kPB_AZp);
+    but->Resize(50, 25);
+    but->Move(55, 185);
+    but->SetToolTipText("Azimuth += 1SE");
+    but->Associate(this);
+    fList->Add(but);
+    but= new TGTextButton(tf4, "Az -",  kPB_AZm);
+    but->Resize(50, 25);
+    but->Move(55, 235);
+    but->SetToolTipText("Azimuth -= 1SE");
     but->Associate(this);
     fList->Add(but);
@@ -728,11 +796,8 @@
     AddFrame(f2, hints2);
 
-    //
-    // FIXME!
-    //
 #ifdef DEBUG
     cout << "MGCosy: Creating Tabs" << endl;
 #endif
-    CreateTabs(fTab);
+    CreateTabs();
 #ifdef DEBUG
     cout << "MGCosy: Creating Label" << endl;
@@ -871,5 +936,5 @@
     }
 }
-
+/*
 void MGCosy::UpdateOffset(ZdAz &off)
 {
@@ -892,5 +957,5 @@
     }
 }
-
+*/
 void MGCosy::UpdateZdAz(ZdAz &soll)
 {
@@ -954,5 +1019,5 @@
 }
 
-void MGCosy::Update(ZdAz pos, ZdAz acc, ZdAz vel, ZdAz off, RaDec radec,
+void MGCosy::Update(ZdAz pos, ZdAz acc, ZdAz vel, /*ZdAz off,*/ RaDec radec,
                     ZdAz soll, UInt_t stat, UInt_t stat2)
 {
@@ -962,7 +1027,5 @@
     double mjd = UpdateTime();
     fSkyPosition->Update(pos, mjd);
-    fAccuracy->Update(pos, acc);
-    fVelocity->Update(vel);
-    UpdateOffset(off);
+    //UpdateOffset(off);
     UpdateRaDec(pos, mjd, radec);
     UpdateZdAz(soll);
@@ -991,6 +1054,16 @@
     SetLabelColor(fAvailSe3,  stat2&0x20);
 
-    stat&kTracking ? fAccuracy->MapWindow() : fAccuracy->UnmapWindow();
-    stat&kTracking ? fVelocity->MapWindow() : fVelocity->UnmapWindow();
+    if (stat&kTracking)
+    {
+        fAccuracy->Update(pos, acc);
+        fVelocity->Update(vel);
+        fAccuracy->MapWindow();
+        fVelocity->MapWindow();
+    }
+    else
+    {
+        fAccuracy->UnmapWindow();
+        fVelocity->UnmapWindow();
+    }
     //    stat&kTracking ? fOffset->MapWindow()   : fOffset->UnmapWindow();
 
@@ -1022,12 +1095,18 @@
 }
 
-void MGCosy::StartTrack()
+void MGCosy::StartTrack(bool track)
 {
     cout << "Start tracking." << endl;
 
-    XY xy = fCRaDec->GetCoordinates();
-    RaDec dest(xy.X()*15., xy.Y()); // xy.X()  [h]->[ø]
-
-    cout << dest.Ra() << "h " << dest.Dec() << "\xb0" << endl;
+    XY xy0 = fCRaDec->GetCoordinates();
+    XY xy1 = track ? xy0 : fCCalib->GetCoordinates();
+
+    RaDec dest0(xy0.X()*15., xy0.Y()); // xy.X()  [h]->[ø]
+    RaDec dest1(xy1.X()*15., xy1.Y()); // xy.X()  [h]->[ø]
+
+    cout << "TrackPos: " << dest0.Ra() << "h " << dest0.Dec() << "\xb0" << endl;
+    cout << "CalibPos: " << dest1.Ra() << "h " << dest1.Dec() << "\xb0" << endl;
+
+    RaDec dest[2] = { dest0, dest1 };
 
     fQueue->PostMsg(WM_TRACK, &dest, sizeof(dest));
@@ -1036,5 +1115,5 @@
 }
 
-void MGCosy::StartTrackPos()
+void MGCosy::StartTrackPos(Long_t mp1)
 {
     cout << "Start tracking Zd/Az pos." << endl;
@@ -1043,4 +1122,20 @@
 
     ZdAz za(xy.X(), xy.Y());
+
+    switch (mp1)
+    {
+    case kPB_ZDm:
+        za.Zd(za.Zd()-360/16384.);
+        break;
+    case kPB_ZDp:
+        za.Zd(za.Zd()+360/16384.);
+        break;
+    case kPB_AZm:
+        za.Az(za.Az()-360/16384.);
+        break;
+    case kPB_AZp:
+        za.Az(za.Az()+360/16384.);
+        break;
+    }
 
     cout << za.Zd() << "\xb0 " << za.Az() << "\xb0" << endl;
@@ -1203,8 +1298,9 @@
                     return kTRUE;
                 case 1:
-                    StartTrack();
+                    StartTrack(true);
                     return kTRUE;
                 case 2:
                     fCRaDec->SetCoordinates(fCCalib->GetCoordinates());
+                    StartTrack(false);
                     return kTRUE;
                 case 3:
@@ -1261,11 +1357,20 @@
                         break;
                     }
-                    RaDec dest(xy.X()*15., xy.Y()); // xy.X()  [h]->[ø]
-                    fQueue->PostMsg(WM_TRACK, &dest, sizeof(dest));
-                    //fQueue->Proc(WM_NEWTRACK, &dest);
                     fCRaDec->SetCoordinates(xy);
-
-                    cout << "New Ra/Dec: " << dest.Ra() << "h " << dest.Dec() << "\xb0" << endl;
+                    StartTrack(false);
+                    /*
+                     RaDec dest(xy.X()*15., xy.Y()); // xy.X()  [h]->[ø]
+                     fQueue->PostMsg(WM_TRACK, &dest, sizeof(dest));
+                     //fQueue->Proc(WM_NEWTRACK, &dest);
+                     */
+                    //cout << "New Ra/Dec: " << dest.Ra() << "h " << dest.Dec() << "\xb0" << endl;
                 }
+                return kTRUE;
+
+            case kPB_ZDp:
+            case kPB_ZDm:
+            case kPB_AZp:
+            case kPB_AZm:
+                StartTrackPos(mp1);
                 return kTRUE;
 
Index: /trunk/MagicSoft/Cosy/gui/MGCosy.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGCosy.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/MGCosy.h	(revision 2278)
@@ -50,6 +50,6 @@
     TGLabel       *fLabel3;
 
-    TGLabel       *fOffsetZd;
-    TGLabel       *fOffsetAz;
+    // TGLabel       *fOffsetZd;
+    // TGLabel       *fOffsetAz;
 
     TGLabel       *fRaEst;
@@ -98,10 +98,11 @@
     void CreateMenu();
     void CreateLabel(TGCompositeFrame *f);
-    void CreateTabs(TGTab *fTab);
+    void CreateTabs();
     void CreatePredefinedPos(TGCompositeFrame *tf1);
+    void CreateStarList(TGCompositeFrame *tf1);
 
     void StartPos();
-    void StartTrack();
-    void StartTrackPos();
+    void StartTrack(bool track);
+    void StartTrackPos(Long_t mp1=0);
     void StartDemo();
     //void StartCalib();
@@ -110,5 +111,5 @@
     void EnableLabel(TGLabel *label, Bool_t stat);
     void SetLabelColor(TGLabel *label, Bool_t col);
-    void UpdateOffset(ZdAz &off);
+    //void UpdateOffset(ZdAz &off);
     void UpdateZdAz(ZdAz &off);
     void UpdateRaDec(ZdAz &pos, double mjd, RaDec &radec);
@@ -127,5 +128,5 @@
     TGTextView *GetLog() const { return fLog; }
 
-    void Update(ZdAz pos, ZdAz acc, ZdAz vel, ZdAz off, RaDec radec, ZdAz soll, UInt_t stat, UInt_t stat2);
+    void Update(ZdAz pos, ZdAz acc, ZdAz vel, /*ZdAz off,*/ RaDec radec, ZdAz soll, UInt_t stat, UInt_t stat2);
 
     Bool_t ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2);
Index: /trunk/MagicSoft/Cosy/gui/MGEmbeddedCanvas.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGEmbeddedCanvas.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/MGEmbeddedCanvas.cc	(revision 2278)
@@ -17,7 +17,7 @@
 
 MGEmbeddedCanvas::MGEmbeddedCanvas(const char *name, const TGWindow* p,
-                                   const UInt_t width, Float_t range)
+                                   UInt_t width, Double_t range)
     : TRootEmbeddedCanvas(name, p, width+1, width+1, 0/*kRaisedFrame*/),
-      fModified(kFALSE), fWidth(width), fRange(range), fPix(2.*range/width)
+      fModified(kFALSE), fWidth(width), fRange(fabs(range)), fPix(2.*fabs(range)/width)
 {
 #ifdef DEBUG
@@ -34,8 +34,8 @@
     fCanvas->SetFillColor(39); // s. TAttFill
 #ifdef DEBUG
-    cout << "MGEmbeddedCanvas: fRange=" << fRange << endl;
+    cout << "MGEmbeddedCanvas." << endl;
 #endif
-    if (fRange>0)
-        fCanvas->Range(-fRange, -fRange, fRange, fRange);
+
+    fCanvas->Range(-fRange, -fRange, fRange, fRange);
 
     fList = new TList;
Index: /trunk/MagicSoft/Cosy/gui/MGEmbeddedCanvas.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGEmbeddedCanvas.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/MGEmbeddedCanvas.h	(revision 2278)
@@ -34,5 +34,5 @@
 public:
     MGEmbeddedCanvas(const char *name, const TGWindow* p,
-                     const UInt_t width, const Float_t range);
+                     UInt_t width, Double_t range);
     ~MGEmbeddedCanvas();
 
Index: /trunk/MagicSoft/Cosy/gui/MGImage.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGImage.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/MGImage.cc	(revision 2278)
@@ -12,4 +12,6 @@
 #include <pthread.h>
 
+#include <X11/Xlib.h>
+
 #include <TTimer.h>
 #include <TSystem.h>
@@ -18,4 +20,14 @@
 
 ClassImp(MGImage);
+/*
+class MyX11 : public TGX11
+{
+public:
+    Display *GetDisplay() { return fDisplay; }
+    Drawable GetRootWin() { return fRootWin; }
+    Drawable GetVisRootWin() { return fVisRootWin; }
+    Int_t    GetDepth() { return fDepth; }
+};
+*/
 
 MGImage::MGImage(const TGWindow* p, UInt_t w, UInt_t h, UInt_t options, ULong_t back)
@@ -27,84 +39,4 @@
 
     //
-    // Set Color Table (Gray scale)
-    //
-    const int cols = 0x100+4*4*4;
-
-    for (int c=0; c<0x100; c++)
-        sprintf(fColors[c], "%02x", c);
-
-    //
-    // create space for the pixmap buffer, initialize pointer to data area
-    //
-    fBuffer = new char*[1+cols+fHeight+1];
-    fBody   = &fBuffer[1+cols];
-
-    //
-    // fill buffer with header informations
-    //
-    fBuffer[0] = new char[14];
-    sprintf(fBuffer[0], "%3d %3d %3d %1d", fWidth, fHeight, cols, 2);
-
-    for (int k=0; k<0x100; k++)
-    {
-        const int l = k+1;
-        fBuffer[l] = new char[13];
-        fBuffer[l][0]  = fColors[k][0];
-        fBuffer[l][1]  = fColors[k][1];
-        fBuffer[l][2]  = '\t';
-        fBuffer[l][3]  = 'c';
-        fBuffer[l][4]  = ' ';
-        fBuffer[l][5]  = '#';
-        fBuffer[l][6]  = fColors[k][0];
-        fBuffer[l][7]  = fColors[k][1];
-        fBuffer[l][8]  = fColors[k][0];
-        fBuffer[l][9]  = fColors[k][1];
-        fBuffer[l][10] = fColors[k][0];
-        fBuffer[l][11] = fColors[k][1];
-        fBuffer[l][12] = '\0';
-    }
-
-    for (int b=0; b<4; b++)
-        for (int g=0; g<4; g++)
-            for (int r=0; r<4; r++)
-            {
-                const int nr = r+(g<<2)+(b<<4);
-                const int l = 0x100+nr+1;
-
-                fBuffer[l] = new char[13];
-                fBuffer[l][0]  = 'f'+nr/8+1;
-                fBuffer[l][1]  = 'f'+nr%8+1;
-                fBuffer[l][2]  = '\t';
-                fBuffer[l][3]  = 'c';
-                fBuffer[l][4]  = ' ';
-                fBuffer[l][5]  = '#';
-                fBuffer[l][6]  = fColors[r*85][0];
-                fBuffer[l][7]  = fColors[r*85][1];
-                fBuffer[l][8]  = fColors[g*85][0];
-                fBuffer[l][9]  = fColors[g*85][1];
-                fBuffer[l][10] = fColors[b*85][0];
-                fBuffer[l][11] = fColors[b*85][1];
-                fBuffer[l][12] = '\0';
-            }
-
-    //
-    // mark end of lines of the data area
-    //
-    for (UInt_t y=0; y<fHeight; y++)
-    {
-        fBody[y] = new char[fWidth*2+1];
-        fBody[y][fWidth*2] = '\0';
-    }
-    //
-    // mark end of buffer
-    //
-    fBuffer[1+cols+fHeight] = '\0';
-
-    //
-    // Create Default Graphic Context (XCreateGC)
-    //
-    fDefGC = gVirtualX->CreateGC(fId, 0); // GetBckgndGC().GetGC(); //
-
-    //
     // Creat drawing semaphore
     //
@@ -112,62 +44,23 @@
     pthread_mutex_init((pthread_mutex_t*)fMuxPixmap, NULL);
 
-    //
-    // create empty (black) pixmap
-    //   return (Pixmap_t) XCreatePixmap(fDisplay, (Drawable) id, w, h,
-    //          DefaultDepth(fDisplay, DefaultScreen(fDisplay)));
-    //
-    fPixmap = kNone; //@@@gVirtualX->CreatePixmap(fId, fWidth, fHeight);
-
     Resize(w, h);
 
-    //fTimer=new TTimer(Form("gClient->ProcessEventsFor((TGWindow*)0x%p);", this), 250, kTRUE);
-//    fTimer=new TTimer("printf(\"Timer!\\n\"); gSystem->ProcessEvents();", 250, kTRUE);
-    //fTimer->Start();
-}
-
-void MGImage::Resize(UInt_t w, UInt_t h)
-{
-//    TGFrame::Resize(w+2*GetBorderWidth(), h+2*GetBorderWidth());
-    // FIXME: RESIZE THE PIXMAP
-}
-
-void MGImage::Resize(TGDimension size)
-{
-//    TGFrame::Resize(size.fWidth+2*GetBorderWidth(), size.fHeight+2*GetBorderWidth());
-    // FIXME: RESIZE THE PIXMAP
-}
-
-void MGImage::MoveResize(Int_t x, Int_t y, UInt_t w, UInt_t h)
-{
-//   TGFrame::MoveResize(x, y, w+2*GetBorderWidth(), h+2*GetBorderWidth());
-   // FIXME: RESIZE THE PIXMAP
+    //
+    // create empty pixmap
+    //
+    fPixmap = gVirtualX->CreatePixmap(fId, fWidth, fHeight);
+    fDefGC  = gVirtualX->CreateGC(fId, 0);
+    fImage  = (XImage*)gVirtualX->CreateImage(fWidth, fHeight);
 }
 
 MGImage::~MGImage()
 {
-    //fTimer->Stop();
-    //delete fTimer;
-
     pthread_mutex_lock((pthread_mutex_t*)fMuxPixmap);
 
-    cout << "Deleting fBuffer..." << endl;
+    cout << "Deleting MGImage..." << endl;
 
-    char **b = fBuffer;
-    while (*b)
-        delete[] *b++;
-    delete[] fBuffer;
-
-    cout << "Deleting Pixmap..." << endl;
-
-    if (fPixmap!=kNone) // @@@
-    {
-        cout << "Delete Pixmap" << endl;
-        gVirtualX->DeletePixmap(fPixmap);  // XFreePixmap(fDisplay, (Pixmap) pmap);
-    }
-
-    cout << "Deleting GC..." << endl;
-
-
-    gVirtualX->DeleteGC(fDefGC);       // XFreeGC(fDisplay, (GC) gc);
+    gVirtualX->DeletePixmap(fPixmap);
+    gVirtualX->DeleteGC(fDefGC);
+    gVirtualX->DeleteImage((Drawable_t)fImage);
 
     pthread_mutex_destroy((pthread_mutex_t*)fMuxPixmap);
@@ -176,77 +69,17 @@
 }
 
-#include <X11/Xlib.h>
-/*
-#include <X11/Xutil.h>
-#include <X11/Intrinsic.h>
-XImage *fPix=NULL;
-*/
-
 void MGImage::DoRedraw()
 {
-//    TGFrame::DrawBorder();
     pthread_mutex_lock((pthread_mutex_t*)fMuxPixmap);
 
-    //===========================
-    if (TestBit(kNeedRecreate))
+    if (TestBit(kNeedRedraw))
     {
-        Pixmap_t mask = kNone;
-        PictureAttributes_t attr;
-        attr.fMask = kNone;
-
-        if (fPixmap!=kNone)
-            gVirtualX->DeletePixmap(fPixmap);
-
-        fPixmap=kNone;
-
-        if (!gVirtualX->CreatePictureFromData(fId, fBuffer, fPixmap,
-                                              mask, attr))
-        {
-            cout << "Warning: Error in CreatePictureFromData" << endl;
-            fPixmap=kNone;
-        }
-        ResetBit(kNeedRecreate);
+        gVirtualX->PutImage(fId, fDefGC, (Drawable_t)fImage, 0, 0, 0, 0, fWidth, fHeight);
+        ResetBit(kNeedRedraw);
     }
 
-    //===========================
-
-    // Copy a drawable (i.e. pixmap) to another drawable (pixmap, window).
-    // The graphics context gc will be used and the source will be copied
-    // from src_x,src_y,src_x+width,src_y+height to dest_x,dest_y.
-    // XCopyArea(fDisplay, src, dest, (GC) gc, src_x, src_y, width, height,
-    //           dest_x, dest_y);
-    if (fPixmap!=kNone) //@@@
-    {
-//        gVirtualX->DrawString(fId, fDefGC, 20, 20, "HELLO WORLD!", 12);
-//        gVirtualX->FillRectangle(fId, fDefGC, 10, 10, 50, 50);
-
-        //        cout << gVirtualX->IsA()->GetName() << endl;
-        gVirtualX->CopyArea(fPixmap, fId, fDefGC,
-                            0, 0, fWidth, fHeight,
-                            GetBorderWidth(), GetBorderWidth());
-
-        /*
-        XCopyArea((Display*)gVirtualX->GetDisplay(), pm, fId,
-                  (GC)fDefGC, 0, 0, fWidth, fHeight,
-                  GetBorderWidth(), GetBorderWidth());
-                  */
-    }
-/*
-     if (fPix)
-    {
-        cout << "put" << flush;
-        XPutImage((Display*)gVirtualX->GetDisplay(), fId,
-                  fDefGC, fPix,
-                  0, 0, GetBorderWidth(), GetBorderWidth(),
-                  fWidth, fHeight);
-
-    cout << "done " << endl;
-    }
-*/
     pthread_mutex_unlock((pthread_mutex_t*)fMuxPixmap);
 }
 
-
-//#include <TGClient.h>
 void MGImage::DrawImg(const byte *buffer)
 {
@@ -254,115 +87,20 @@
         return;
 
-    for (UInt_t y=0; y<fHeight; y++)
+    char *d = fImage->data;
+    char *e = (char*)(buffer+fWidth*fHeight);
+    char *s = (char*)buffer;
+
+    // FIXME: This loop depends on the screen color depth
+    while (s<e)
     {
-        for (UInt_t x=0; x<fWidth; x++)
-        {
-            const byte col = buffer[y*fWidth+x];
-
-            fBody[y][x*2]   = fColors[col][0];
-            fBody[y][x*2+1] = fColors[col][1];
-        }
+        *d++ = *s;
+        *d++ = *s;
+        *d++ = *s++;
+        d++;
     }
 
-/*
-    cout << "CreateImage" << flush;
-    if (!fPix)
-    {
-        Display *dsp = (Display*)gVirtualX->GetDisplay();
-        Screen  *scr = DefaultScreenOfDisplay(dsp);
-        Visual  *vis = DefaultVisualOfScreen(scr);
+    SetBit(kNeedRedraw);
 
-        cout << vis->visualid << endl;
-        cout << vis->c_class << endl;
-        cout << vis->bits_per_rgb << endl;
-        cout << vis->map_entries << endl;
-        Visual visual;
-        visual.c_class = StaticGray;
-
-        int n;
-        XPixmapFormatValues *fmt = XListPixmapFormats(dsp, &n);
-
-        cout << "N: " << n << endl;
-        for (int i=0; i<n; i++)
-        {
-            cout << fmt[i].dww.epth << " " << fmt[i].bits_per_pixel << " "
-                << fmt[i].scanline_pad << endl;
-        }
-
-        Colormap colormap = XCreateColormap(dsp, fId, vis, AllocNone);
-
-        for (int i=0; i<vis->map_entries; i++)
-        {
-            XColor color;
-            char data[4];
-
-            color.flags = DoRed | DoGreen | DoBlue;
-            color.pixel = i;
-            color.red   = (256*i/vis->map_entries) << 8;
-            color.green = (256*i/vis->map_entries) << 8;
-            color.blue  = (256*i/vis->map_entries) << 8;
-
-            XAllocColor(dsp, colormap, &color);
-
-            cout << color.pixel <<" " << flush;
-        }
-        fPix = XCreateImage(dsp, vis, 8, ZPixmap, 0,
-                            buffer, 768, 576, 32, 0);
-
-        cout << "Colors" << visual.visualid << flush;
-
-    }
-    cout << "Done " << (void*)fPix << endl;
-    */
-
-/*
-    Pixmap_t mask = kNone;
-    PictureAttributes_t attr;
-    attr.fMask = kNone;
-    if (fPixmap!=kNone) // @@@
-        gVirtualX->DeletePixmap(fPixmap); // XFreePixmap(fDisplay, (Pixmap) pmap);
-
-    // Create a pixture pixmap from data. The picture attributes
-    // are used for input and output. Returns kTRUE in case of success,
-    // kFALSE otherwise. If mask does not exist it is set to kNone.
-    //
-    // XpmAttributes xpmattr;
-    //
-    // MapPictureAttributes(attr, xpmattr);
-    //
-    // Int_t res = XpmCreatePixmapFromData(fDisplay, id, data, (Pixmap*)&pict,
-    //                                     (Pixmap*)&pict_mask, &xpmattr);
-    //
-    // MapPictureAttributes(attr, xpmattr, kFALSE);
-    // XpmFreeAttributes(&xpmattr);
-    //
-    // if (res == XpmSuccess || res == XpmColorError)
-    //   return kTRUE;
-    //
-    // if (pict) {
-    //    XFreePixmap(fDisplay, (Pixmap)pict);
-    //    pict = kNone;
-    // }
-    // if (pict_mask) {
-    //    XFreePixmap(fDisplay, (Pixmap)pict_mask);
-    //    pict_mask = kNone;
-    // }
-    // return kFALSE;
-    fPixmap=kNone;
-    if (!gVirtualX->CreatePictureFromData(fId, fBuffer, fPixmap,
-                                          mask, attr))
-    {
-        cout << "Warning: Error in CreatePictureFromData" << endl;
-        fPixmap=kNone;
-    }
-*/
-    SetBit(kNeedRecreate);
     pthread_mutex_unlock((pthread_mutex_t*)fMuxPixmap);
-    //gClient->NeedRedraw(this);
-    //fTimer->Start(10, kTRUE);
-    //gVirtualX->UpdateWindow(1);
-    //    ((TGX11*)gVirtualX)->Sync(1);
-    //new TTimer(Form("gClient->ProcessEventsFor((TGWindow*)0x%p);", this), 1, kFALSE);
-    //    DoRedraw();
 }
 
@@ -372,48 +110,44 @@
         return;
 
-    for (UInt_t y=0; y<fHeight; y++)
+    char *d  = fImage->data;
+    char *e  = (char*)(gbuf+fWidth*fHeight);
+    char *s1 = (char*)gbuf;
+    char *s2 = (char*)cbuf;
+
+    // FROM libAfterImage:
+    // -------------------
+    //#define ALPHA_TRANSPARENT      	0x00
+    //#define ALPHA_SEMI_TRANSPARENT 	0x7F
+    //#define ALPHA_SOLID            	0xFF
+    // * Lowermost 8 bits - Blue channel
+    // * bits 8 to 15     - Green channel
+    // * bits 16 to 23    - Red channel
+    // * bits 24 to 31    - Alpha channel
+    //#define ARGB32_White    		0xFFFFFFFF
+    //#define ARGB32_Black    		0xFF000000
+
+    // FIXME: This loop depends on the screen color depth
+    while (s1<e)
     {
-        for (UInt_t x=0; x<fWidth; x++)
+        if (*s2)
         {
-            const byte ccol = cbuf[y*fWidth+x];
-
-            if (ccol)
-            {
-                fBody[y][x*2]   = 'f'+ccol/8+1;
-                fBody[y][x*2+1] = 'f'+ccol%8+1;
-            }
-            else
-            {
-                const byte gcol = gbuf[y*fWidth+x];
-                fBody[y][x*2]   = fColors[gcol][0];
-                fBody[y][x*2+1] = fColors[gcol][1];
-            }
+            *d++ = ((*s2>>4)&0x3)*85;
+            *d++ = ((*s2>>2)&0x3)*85;
+            *d++ = ((*s2++ )&0x3)*85;
+            d++;
+            s1++;
+        }
+        else
+        {
+            *d++ = *s1;
+            *d++ = *s1;
+            *d++ = *s1++;
+            d++;
+            s2++;
         }
     }
-/*
-    Pixmap_t mask = kNone;
-    PictureAttributes_t attr;
-    attr.fMask = kNone;
 
-    if (fPixmap!=kNone)
-        gVirtualX->DeletePixmap(fPixmap);
+    SetBit(kNeedRedraw);
 
-    fPixmap=kNone;
-
-    if (!gVirtualX->CreatePictureFromData(fId, fBuffer, fPixmap,
-                                          mask, attr))
-    {
-        cout << "Warning: Error in CreatePictureFromData" << endl;
-        fPixmap=kNone;
-    }
-    */
-    SetBit(kNeedRecreate);
     pthread_mutex_unlock((pthread_mutex_t*)fMuxPixmap);
-    //gClient->NeedRedraw(this);
-    //fTimer->Start(10, kTRUE);
-    //gVirtualX->UpdateWindow(1);
-    //    ((TGX11*)gVirtualX)->Sync(1);
-    //new TTimer(Form("gClient->ProcessEventsFor((TGWindow*)0x%p);", this), 1, kFALSE);
-    //    DoRedraw();
 }
-
Index: /trunk/MagicSoft/Cosy/gui/MGImage.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGImage.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/MGImage.h	(revision 2278)
@@ -8,5 +8,10 @@
 //   Version: V1.0 (1-8-2000)
 
+#ifndef ROOT_TGFrame
 #include <TGFrame.h>
+#endif
+#ifndef ROOT_TGX11
+#include <TGX11.h>
+#endif
 
 class TTimer;
@@ -16,8 +21,5 @@
 class MGImage : public TGFrame
 {
-    char fColors[0x100][3];
-
-    char **fBuffer;
-    char **fBody;
+    XImage *fImage;
 
     GContext_t fDefGC;
@@ -31,5 +33,5 @@
     TTimer *fTimer;
 
-    enum { kNeedRecreate = BIT(17) };
+    enum { kNeedRedraw = BIT(17) };
 
 public:
@@ -38,8 +40,4 @@
 
     void DoRedraw();
-
-    void Resize(UInt_t w, UInt_t h);
-    void Resize(TGDimension size);
-    void MoveResize(Int_t x, Int_t y, UInt_t w, UInt_t h);
 
     void DrawImg(const byte *buffer);
Index: /trunk/MagicSoft/Cosy/gui/MGSkyPosition.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGSkyPosition.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/MGSkyPosition.cc	(revision 2278)
@@ -69,5 +69,5 @@
     InitArc(fSaturn,  1001, kYellow/*17*/, 16);
 
-    for (int i=0; i<13; i++)
+    for (int i=0; i<14; i++)
     {
         fStars[i] = new TArc(0, 0, 1);
@@ -110,27 +110,33 @@
 }
 
-static const RaDec stars[13] =
+static const RaDec stars[14] =
 {
     // 32349   Sirius           -1.44 1.45    2.64      0.009   A0m
-    (RaDec(Slalib::Hms2Rad(  6, 45, 0), Slalib::Dms2Rad(-16, 42, 0))),
+    (RaDec(Slalib::Hms2Rad(  6, 45,  0), Slalib::Dms2Rad(-16, 42,  0))),
     // 30438   Canopus          -0.62 -5.53   95.88     0.164   F0Ib
-    (RaDec(Slalib::Hms2Rad(  6, 23, 0), Slalib::Dms2Rad(-52, 41, 0))),
+    (RaDec(Slalib::Hms2Rad(  6, 23,  0), Slalib::Dms2Rad(-52, 41,  0))),
     // 69673   Arcturus         -0.05 -0.31   11.25     1.239   K2IIIp
-    (RaDec(Slalib::Hms2Rad( 14, 15, 0), Slalib::Dms2Rad( 19, 10, 0))),
-    (RaDec(Slalib::Hms2Rad( 01, 37, 6), Slalib::Dms2Rad(-57, 14, 0))),
-    (RaDec(Slalib::Hms2Rad( 12, 26, 6), Slalib::Dms2Rad(-63, 06, 0))),
-    (RaDec(Slalib::Hms2Rad( 13, 25, 2), Slalib::Dms2Rad(-11, 10, 0))),
-    (RaDec(Slalib::Hms2Rad(  5, 14, 0), Slalib::Dms2Rad(- 8, 12, 5))),
-    (RaDec(Slalib::Hms2Rad( 19, 50, 0), Slalib::Dms2Rad(- 8, 52, 6))),
-    (RaDec(Slalib::Hms2Rad( 18, 37, 0), Slalib::Dms2Rad( 38, 47, 0))),
-    (RaDec(Slalib::Hms2Rad(  5, 16, 0), Slalib::Dms2Rad( 46, 00, 0))),
-    (RaDec(Slalib::Hms2Rad(  7, 39, 0), Slalib::Dms2Rad(  5, 14, 0))),
-    (RaDec(Slalib::Hms2Rad(  5, 55, 0), Slalib::Dms2Rad(  7, 24, 0))),
-    (RaDec(Slalib::Hms2Rad( 19, 50, 0), Slalib::Dms2Rad(  8, 52, 0)))
+    (RaDec(Slalib::Hms2Rad( 14, 15,  0), Slalib::Dms2Rad( 19, 10,  0))),
+    //         Vega              0.03
+    (RaDec(Slalib::Hms2Rad( 18, 37,  0), Slalib::Dms2Rad( 38, 47,  0))),
+    //         Altair            0.77
+    (RaDec(Slalib::Hms2Rad( 19, 50,  0), Slalib::Dms2Rad(  8, 52,  6))),
+    //         Antares           0.96
+    (RaDec(Slalib::Hms2Rad( 16, 29, 25), Slalib::Dms2Rad(-26, 25, 55))),
+    //         Spica             0.98
+    (RaDec(Slalib::Hms2Rad( 13, 25, 13), Slalib::Dms2Rad(-11,  9, 41))),
+    //         Deneb             1.25
+    (RaDec(Slalib::Hms2Rad( 20, 41, 26), Slalib::Dms2Rad( 45, 16, 49))),
+    (RaDec(Slalib::Hms2Rad(  1, 37,  6), Slalib::Dms2Rad(-57, 14,  0))),
+    (RaDec(Slalib::Hms2Rad( 12, 26,  6), Slalib::Dms2Rad(-63, 06,  0))),
+    (RaDec(Slalib::Hms2Rad(  5, 14,  0), Slalib::Dms2Rad(- 8, 12,  5))),
+    (RaDec(Slalib::Hms2Rad(  5, 16,  0), Slalib::Dms2Rad( 46, 00,  0))),
+    (RaDec(Slalib::Hms2Rad(  7, 39,  0), Slalib::Dms2Rad(  5, 14,  0))),
+    (RaDec(Slalib::Hms2Rad(  5, 55,  0), Slalib::Dms2Rad(  7, 24,  0)))
 };
 
 void MGSkyPosition::UpdateStars()
 {
-    for (int i=0; i<13; i++)
+    for (int i=0; i<14; i++)
         SetDot(fStars[i], stars[i], 0);
 }
@@ -138,11 +144,18 @@
 void MGSkyPosition::InitText()
 {
-    fText = new TText(105, 105, "");
-    //fText->SetFillStyle(4000);  // transparent
-    fText->SetTextAlign(33);  // right, top
-    fText->SetTextColor(10);  // white
-    fText->Draw();
-
-    fList->Add(fText);
+    fText1 = new TText(-105, 105, "");
+    fText2 = new TText(105, 105, "");
+
+    fText1->SetTextAlign(13);  // left, top
+    fText2->SetTextAlign(33);  // right, top
+
+    fText1->SetTextColor(10);  // white
+    fText2->SetTextColor(10);  // white
+
+    fText1->Draw();
+    fText2->Draw();
+
+    fList->Add(fText1);
+    fList->Add(fText2);
 }
 
@@ -339,8 +352,9 @@
 
     char txt[100];
-    sprintf(txt, "Zd=%s%d\xb0%02.1f'\nAz=%s%d\xb0%02.1f'",
-            x<0?"-":"", abs(xd), xm, y<0?"-":"", abs(yd), ym);
-
-    fText->SetText(fText->GetX(), fText->GetY(), txt);
+    sprintf(txt, "Zd=%s%d\xb0%02.1f'", x<0?"-":"", abs(xd), xm);
+    fText1->SetText(fText1->GetX(), fText1->GetY(), txt);
+
+    sprintf(txt, "Az=%s%d\xb0%02.1f'", y<0?"-":"", abs(yd), ym);
+    fText2->SetText(fText2->GetX(), fText2->GetY(), txt);
 
     SetModified();
@@ -379,6 +393,6 @@
             return false;
 
-        x=-100;
-        y= 100;
+        x= 100;
+        y=-100;
     }
 
Index: /trunk/MagicSoft/Cosy/gui/MGSkyPosition.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGSkyPosition.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/MGSkyPosition.h	(revision 2278)
@@ -35,5 +35,5 @@
     TArc       *fSaturn;
 
-    TArc       *fStars[13];
+    TArc       *fStars[14];
 
     TArc       *fSaturnRing;
@@ -47,5 +47,6 @@
     TLine      *fSunL[4];
 
-    TText      *fText;
+    TText      *fText1;
+    TText      *fText2;
 
     SlaStars   *fSlaStar;
Index: unk/MagicSoft/Cosy/gui/MGStarguider.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGStarguider.cc	(revision 2277)
+++ 	(revision )
@@ -1,1221 +1,0 @@
-#include "MGStarguider.h"
-
-#include <fstream.h>    // ifstream
-#include <iostream.h>   // cout
-#include <iomanip.h>   // cout
-
-#include <TTimer.h>
-
-#include <TGMenu.h>
-#include <TGLabel.h>
-#include <TSystem.h>
-#include <TGSplitter.h>    // TGHorizontal3DLine
-#include <TGTextEntry.h>
-
-#include "MGImage.h"
-#include "MGCoordinates.h"
-
-#include "coord.h"
-
-#include "StarCatalog.h"
-
-#include "Filter.h"
-#include "Filter2.h"
-#include "CaosFilter.h"
-#include "Writer.h"
-#include "base/timer.h"
-
-#include "MStarList.h"
-
-#include <TH1.h>
-#include <TH2.h>
-#include <TGraph.h>
-#include <TCanvas.h>
-
-#include <TFile.h>
-#include <TTree.h>
-#include <TBranch.h>
-
-#include "Led.h"
-#include "Leds.h"
-#include "Rings.h"
-
-#include "MGMenu.h"
-
-ClassImp(MGStarguider);
-
-enum {
-    IDM_kFilter,
-    IDM_kCaosFilter,
-    IDM_kCatalog,
-    IDM_kStarguider,
-    IDM_kStart,
-    IDM_kStop,
-    IDM_kFileType,
-    IDM_kPPM,
-    IDM_kPNG,
-    IDM_kOnce,
-    IDM_kStretch,
-    IDM_kUseFileRaDec,
-    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_kInterpol125,
-    IDM_kInterpol25,
-    IDM_kInterpol10,
-    IDM_kInterpol5,
-    IDM_kInterpol2,
-    IDM_kInterpol1,
-    IDM_kPositions,
-    IDM_kRings,
-    IDM_kLeds,
-    IDM_kStartAnalyse,
-    IDM_kStopAnalyse,
-    IDM_kResetHistograms
-};
-
-Bool_t MGStarguider::HandleTimer(TTimer *t)
-{
-    fImage->DoRedraw();
-    fZoomImage->DoRedraw();
-    return kTRUE;
-}
-
-#define kZOOM 96
-
-void MGStarguider::InitHists()
-{
-    Double_t xmin = 480.1-1.0;
-    Double_t xmax = 480.1+1.0;
-
-    Double_t ymin = 217.5-1.0;
-    Double_t ymax = 217.5+1.0;
-
-    Double_t rmin = 117.4-1.0;
-    Double_t rmax = 117.4+1.0;
-
-    Int_t xbin = 60;
-    Int_t ybin = 60;
-    Int_t rbin = 60;
-
-    fHistpr = new TH1F;
-    fHistpr->SetNameTitle("pr","Radius of the ring");
-    fHistpr->SetBins(rbin, rmin, rmax);
-    fHistpr->SetXTitle("r [mm]");
-    fHistpr->SetYTitle("counts");
-
-    fHistprx = new TH1F;
-    fHistprx->SetNameTitle("prx","x-coordinate of the ring-center");
-    fHistprx->SetBins(xbin, xmin, xmax);
-    fHistprx->SetXTitle("x [mm]");
-    fHistprx->SetYTitle("counts");
-
-    fHistpry = new TH1F;
-    fHistpry->SetNameTitle("pry","y-coordniate of the ring-center");
-    fHistpry->SetBins(ybin, ymin, ymax);
-    fHistpry->SetXTitle("y [mm]");
-    fHistpry->SetYTitle("counts");
-
-    for (int i=0; i<6; i++)
-    {
-        TString name  = "Angle";
-        TString title = "Angle of Led ";
-
-        name += i;
-        title += i;
-
-        fHistw[i] = new TH1F;
-        fHistw[i]->SetNameTitle(name, title);
-        fHistw[i]->SetBins(26, -25, 25);
-        fHistw[i]->SetXTitle("\\Phi [arcmin]");
-        fHistw[i]->SetYTitle("counts");
-
-        name = "Angles";
-        title = "Angles of the Leds ";
-
-        name += i;
-        title += i;
-
-        fHistv[i] = new TH1F;
-        fHistv[i]->SetNameTitle(name, title);
-        fHistv[i]->SetBins(721, -180.5, 180.5);
-        fHistv[i]->SetXTitle("\\Phi [deg]");
-        fHistv[i]->SetYTitle("counts");
-    }
-
-    fHistallw = new TH1F;
-    fHistallw->SetNameTitle("allw","Rotation angel");
-    fHistallw->SetBins(26, -25, 25);
-    fHistallw->SetXTitle("\\Phi [arcmin]");
-    fHistallw->SetYTitle("counts");
-
-    fHistprxpry = new TH2F;
-    fHistprxpry->SetNameTitle("prx und pry","x- and y-coordniate of the ring-center");
-    fHistprxpry->SetBins(xbin, xmin, xmax, ybin, ymin, ymax);
-    fHistprxpry->SetXTitle("x [mm]");
-    fHistprxpry->SetYTitle("y [mm]");
-    fHistprxpry->SetZTitle("counts");
-}
-
-void MGStarguider::InitGraphs()
-{
-    fGraphprx = new TGraph;
-    fGraphprx->SetTitle("time-developement of the x-coordinate of the ring-center");
-
-    fGraphpry = new TGraph;
-    fGraphpry->SetTitle("time-developement of the y-coordinate of the ring-center");
-
-    for (int i=0; i<6; i++)
-    {
-        TString title = "Time-developement of the angle ";
-        title += i;
-
-        fGraphw[i] = new TGraph;
-        fGraphw[i]->SetTitle(title);
-    }
-}
-
-void MGStarguider::InitGui()
-{
-    fList = new MGList;
-
-    const TGWindow *p=gClient->GetRoot();
-
-    //
-    // Create Menu for MGStarguider Display
-    //
-    fDisplay = new MGPopupMenu(p);
-    fDisplay->AddEntry("&Filter",      IDM_kFilter);
-    fDisplay->AddEntry("C&aosFilter",  IDM_kCaosFilter);
-    fDisplay->AddEntry("Sao &Catalog", IDM_kCatalog);
-    fDisplay->AddEntry("Starguider",   IDM_kStarguider);
-    fDisplay->AddEntry("Stretch",      IDM_kStretch);
-    fDisplay->DisableEntry(IDM_kStarguider);
-    fDisplay->CheckEntry(IDM_kStretch);
-    fDisplay->Associate(this);
-    fList->Add(fDisplay);
-
-    fFileType = new MGPopupMenu(p);
-    fFileType->AddEntry("PP&M", IDM_kPPM);
-    fFileType->AddEntry("&PNG", IDM_kPNG);
-    fFileType->CheckEntry(IDM_kPNG);
-    fFileType->Associate(this);
-    fList->Add(fFileType);
-
-    fWriteType = new MGPopupMenu(p);
-    fWriteType->AddEntry("&Once",      IDM_kOnce);
-    fWriteType->AddEntry("&Continous", IDM_kContinous);
-    fWriteType->CheckEntry(IDM_kOnce);
-    fWriteType->Associate(this);
-    fList->Add(fWriteType);
-
-    fWriteRate = new MGPopupMenu(p);
-    fWriteRate->AddEntry("25/s", IDM_kRate25ps);
-    fWriteRate->AddEntry("5/s",  IDM_kRate5ps);
-    fWriteRate->AddEntry("1s",   IDM_kRate1s);
-    fWriteRate->AddEntry("5s",   IDM_kRate5s);
-    fWriteRate->AddEntry("30s",  IDM_kRate30s);
-    fWriteRate->AddEntry("1min", IDM_kRate1m);
-    fWriteRate->AddEntry("5min", IDM_kRate5m);
-    fWriteRate->CheckEntry(IDM_kRate1m);
-    fWriteRate->Associate(this);
-    fList->Add(fWriteRate);
-
-    fWrtRate = 25*60;
-
-    fWritePictures = new MGPopupMenu(p);
-    fWritePictures->AddEntry("&Start",      IDM_kStart);
-    fWritePictures->AddEntry("Sto&p",       IDM_kStop);
-    fWritePictures->AddSeparator();
-    fWritePictures->AddPopup("File &Type",  fFileType);
-    fWritePictures->AddPopup("&Write Type", fWriteType);
-    fWritePictures->AddPopup("Write &Rate", fWriteRate);
-    fWritePictures->DisableEntry(IDM_kStop);
-    fWritePictures->Associate(this);
-    fList->Add(fWritePictures);
-
-    fLimMag = new MGPopupMenu(p);
-    fLimMag->AddEntry("3", IDM_kLimMag3);
-    fLimMag->AddEntry("4", IDM_kLimMag4);
-    fLimMag->AddEntry("5", IDM_kLimMag5);
-    fLimMag->AddEntry("6", IDM_kLimMag6);
-    fLimMag->AddEntry("7", IDM_kLimMag7);
-    fLimMag->AddEntry("8", IDM_kLimMag8);
-    fLimMag->AddEntry("9", IDM_kLimMag9);
-    fLimMag->CheckEntry(IDM_kLimMag8);
-    fLimMag->Associate(this);
-    fList->Add(fLimMag);
-
-    fSao->SetLimitMag(7.0);
-
-    fInterpol = new MGPopupMenu(p);
-    fInterpol->AddEntry("125", IDM_kInterpol125);
-    fInterpol->AddEntry("25",  IDM_kInterpol25);
-    fInterpol->AddEntry("10",  IDM_kInterpol10);
-    fInterpol->AddEntry("5",   IDM_kInterpol5);
-    fInterpol->AddEntry("2",   IDM_kInterpol2);
-    fInterpol->AddEntry("Off", IDM_kInterpol1);
-    fInterpol->CheckEntry(IDM_kInterpol1);
-    fInterpol->Associate(this);
-    fList->Add(fInterpol);
-
-    fIntRate = 1;
-
-    fSetup = new MGPopupMenu(p);
-    fSetup->AddPopup("Lim. &Magnitude",      fLimMag);
-    fSetup->AddPopup("Disp. &Interpolation", fInterpol);
-    fSetup->AddEntry("Use Ra/Dec from file", IDM_kUseFileRaDec);
-    fSetup->Associate(this);
-    fList->Add(fSetup);
-
-    fWrite = new MGPopupMenu(p);
-    fWrite->AddEntry("&Positions",  IDM_kPositions);
-    fWrite->AddEntry("&Leds", IDM_kLeds);
-    fWrite->AddEntry("&Rings", IDM_kRings);
-    fWrite->Associate(this);
-    fList->Add(fWrite);
-
-    fAnalyse = new MGPopupMenu(p);
-    fAnalyse->AddEntry("S&tart Analyse", IDM_kStartAnalyse);
-    fAnalyse->AddEntry("St&opp Analyse", IDM_kStopAnalyse);
-    fAnalyse->DisableEntry(IDM_kStopAnalyse);
-    fAnalyse->AddEntry("&Reset Histograms", IDM_kResetHistograms);
-//    fAnalyse->AddEntry("Reset &Graph", IDM_kResetGraph);
-    fAnalyse->Associate(this);
-    fList->Add(fAnalyse);
-
-    fMenu = new MGMenuBar(this, 0, 0, kHorizontalFrame);
-    fMenu->AddPopup("&Display", fDisplay, NULL);
-    fMenu->AddPopup("&WritePics",   fWritePictures,   NULL);
-    fMenu->AddPopup("&Setup",   fSetup,   NULL);
-    fMenu->Resize(fMenu->GetDefaultSize());
-    fMenu->BindKeys(this);
-    AddFrame(fMenu); //, new TGLayoutHints (kLHintsNormal, 0, 4, 0, 0));
-    fList->Add(fMenu);
-
-    fCaOs = new MGPopupMenu(p);
-    fCaOs->AddPopup("&Write", fWrite);
-    fCaOs->AddPopup("&Analyse", fAnalyse);
-    fCaOs->Associate(this);
-    fCaOs->BindKeys(fMenu, this);
-    fList->Add(fCaOs);
-
-    fCRaDec = new MGCoordinates(this, kETypeRaDec);
-    fCRaDec->Move(1, fMenu->GetDefaultHeight()+584);
-    AddFrame(fCRaDec);
-    fList->Add(fCRaDec);
-
-    fCZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fCZdAz->Move(240+12+10, fMenu->GetDefaultHeight()+584);
-    AddFrame(fCZdAz);
-    fList->Add(fCZdAz);
-
-    fPZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
-    fPZdAz->Move(240+12+10, fMenu->GetDefaultHeight()+630);
-    AddFrame(fPZdAz);
-    fList->Add(fPZdAz);
-
-    TGLabel *l = new TGLabel(this, "Arb.-Sky Pos");
-    l->SetTextJustify(kTextLeft);
-    l->Move(480+32, fMenu->GetDefaultHeight()+590);
-    AddFrame(l);
-    fList->Add(l);
-
-    l = new TGLabel(this, "arcsec/pix");
-    l->SetTextJustify(kTextLeft);
-    l->Move(605, fMenu->GetDefaultHeight()+619+13);
-    AddFrame(l);
-    fList->Add(l);
-
-    l = new TGLabel(this, "deg");
-    l->SetTextJustify(kTextLeft);
-    l->Move(605, fMenu->GetDefaultHeight()+619-10);
-    AddFrame(l);
-    fList->Add(l);
-
-    l = new TGLabel(this, "Pointing Pos");
-    l->SetTextJustify(kTextLeft);
-    l->Move(480+32, fMenu->GetDefaultHeight()+655);
-    AddFrame(l);
-    fList->Add(l);
-
-    const Double_t pixsize = 23.4;
-
-    fSao->SetPixSize(pixsize/3600);
-    fSao->SetRotationAngle(0);
-
-    TString txt;
-    txt += pixsize;
-
-    fPixSize = new TGTextEntry(this, txt, IDM_kPixSize);
-    fPixSize->SetAlignment(kTextCenterX);
-    fPixSize->Move(547, fMenu->GetDefaultHeight()+617+13);
-    AddFrame(fPixSize);
-    fList->Add(fPixSize);
-
-    fAngle = new TGTextEntry(this, "         0", IDM_kAngle);
-    fAngle->SetAlignment(kTextCenterX);
-    fAngle->Move(547, fMenu->GetDefaultHeight()+617-10);
-    AddFrame(fAngle);
-    fList->Add(fAngle);
-
-    // TGHorizontal3DLine *fLineSep = new TGHorizontal3DLine(this);
-    // AddFrame(fLineSep, new TGLayoutHints (kLHintsNormal | kLHintsExpandX));
-    // fList->Add(fLineSep);
-
-    //
-    // Create Image Display
-    //
-    fZoomImage = new MGImage(this, kZOOM, kZOOM);
-    fZoomImage->Move(768-kZOOM-2, 700-kZOOM-2);
-    AddFrame(fZoomImage);
-    fList->Add(fZoomImage);
-
-    fImage = new MGImage(this, 768, 576);
-    fImage->Move(0, fMenu->GetDefaultHeight());
-    AddFrame(fImage);
-    fList->Add(fImage);
-
-    //
-    // Make everything visible
-    //
-    SetWindowName("MGStarguider Main Window");
-    SetIconName("MGStarguider");
-
-    MapSubwindows();
-    MapWindow();
-
-    //------------------------------------------------------------
-    //    XY xy(3.819444, 24.05333);
-    //    fCRaDec->SetCoordinates(xy);
-    //    fRaDec->Set(xy.X()*360/24, xy.Y());
-    //------------------------------------------------------------
-}
-
-MGStarguider::MGStarguider(MObservatory::LocationName_t obs)
-: TGMainFrame(gClient->GetRoot(), 768, 740), fFile(NULL), fDx((768-kZOOM)/2), fDy((512-kZOOM)/2)
-{
-    fSao = new StarCatalog(obs);
-    fRaDec = new RaDec(180, 40);
-
-    InitHists();
-    InitGraphs();
-
-    InitGui();
-
-    gVirtualX->GrabButton(fId, kButton2, 0, 0, 0, 0, kTRUE);
-
-    fTimer=new TTimer(this, 100); // 100ms
-    fTimer->TurnOn();
-}
-
-MGStarguider::~MGStarguider()
-{
-    gVirtualX->GrabButton(fId, kButton2, 0, 0, 0, 0, kFALSE);
-
-    fTimer->TurnOff();
-    delete fTimer;
-
-    delete fList;
-
-    delete fSao;
-    delete fRaDec;
-
-    delete fHistpr;
-    delete fHistprx;
-    delete fHistpry;
-    delete fHistprxpry;
-    delete fHistallw;
-    delete fGraphprx;
-    delete fGraphpry;
-
-    for (int i=0; i<6; i++)
-    {
-        delete fHistw[i];
-        delete fHistv[i];
-        delete fGraphw[i];
-    }
-
-    cout << "Camera Display destroyed." << endl;
-}
-
-void MGStarguider::Layout()
-{
-    // Resize(GetDefaultSize());
-}
-
-void MGStarguider::CloseWindow()
-{
-    cout << "EventDisplay::CloseWindow: Exit Application Loop." << endl;
-
-    //fClient.ExitLoop();
-    //    cout << "FIXME: ExitLoop not called!!!!!!" << endl;
-    gSystem->ExitLoop();
-}
-
-void MGStarguider::Toggle(MGPopupMenu *p, UInt_t id)
-{
-    if (p->IsEntryChecked(id))
-        p->UnCheckEntry(id);
-    else
-        p->CheckEntry(id);
-}
-
-void MGStarguider::ResetHists()
-{
-    fHistprx->Reset();
-    fHistpry->Reset();
-    fHistpr->Reset();
-    fHistprxpry->Reset();
-    fHistallw->Reset();
-    for (int i=0; i<6; i++)
-    {
-        fHistw[i]->Reset();
-        fHistv[i]->Reset();
-    }
-}
-
-void MGStarguider::DisplayAnalysis()
-{
-    TCanvas *c = new TCanvas("cring", "Center of the ring", 800, 800);
-    c->Divide(2,2);
-    c->cd(1);
-    fHistprx->Fit("gaus");
-    fHistprx->DrawCopy();
-    c->cd(2);
-    fHistpry->Fit("gaus");
-    fHistpry->DrawCopy();
-    c->cd(3);
-    fHistpr->Fit("gaus");
-    fHistpr->DrawCopy();
-    c->cd(4);
-    fHistprxpry->DrawCopy(/*"surf2"*/);
-    c->Update();
-
-    c = new TCanvas("c3", "Absolute angles of the leds", 800, 800);
-    c->Divide(2,3);
-    for (int i=0; i<6; i++)
-    {
-        c->cd(i+1);
-        TH1 *h = fHistv[i]->DrawCopy();
-        h->SetLineColor(i+1);
-        cout << "Led #" << i << ": MeanPhi=" << h->GetMean() << endl;
-    }
-    c->Update();
-
-    c = new TCanvas("c5", "timedevelopement of prx", 800, 800);
-    fGraphprx->Draw("ALP*");
-    fGraphprx->GetHistogram()->SetXTitle("time [1/25 s]");
-    fGraphprx->GetHistogram()->SetYTitle("x-coordinate");
-    c->Modified();
-    c->Update();
-
-    c = new TCanvas("c6", "timedevelopement of pry", 800, 800);
-    fGraphpry->Draw("ALP*");
-    fGraphpry->GetHistogram()->SetXTitle("time [1/25 s]");
-    fGraphpry->GetHistogram()->SetYTitle("y-coordinate");
-    c->Modified();
-    c->Update();
-
-    c = new TCanvas("c2", "Relative angles of the Leds", 800, 800);
-    c->Divide(2,3);
-    for (int i=0; i<6; i++)
-    {
-        c->cd(i+1);
-        fHistw[i]->DrawCopy();
-    }
-    c->Update();
-
-    c = new TCanvas("c7", "timedevelopement of the relative angles of the Leds", 800, 800);
-    c->Divide(2,3);
-    for (int i=0; i<6; i++)
-    {
-        c->cd(i+1);
-        fGraphw[i]->Draw("ALP*");
-        fGraphw[i]->GetHistogram()->SetXTitle("t [1/25s]");
-        fGraphw[i]->GetHistogram()->SetYTitle("[deg]");
-    }
-    c->Modified();
-    c->Update();
-
-    c = new TCanvas("c4", "rotation angle ", 800, 800);
-    fHistallw->Fit("gaus");
-    fHistallw->DrawCopy();
-}
-
-void MGStarguider::OpenFile()
-{
-    int i=0;
-    char name[100];
-    while (1)
-    {
-        sprintf(name, "data/data%03d.root", i++);
-        if (gSystem->AccessPathName(name, kFileExists))
-            break;
-    }
-
-    fFile = new TFile(name, "RECREATE");
-
-    if (!fFile->IsOpen())
-    {
-        delete fFile;
-        fFile = NULL;
-
-        cout << "Error: Cannot open file '" << name << "'" << endl;
-
-    }
-
-    fTree = new TTree("Data", "Real CaOs Data");
-
-    fLeds = NULL;
-    fRings = NULL;
-    fTime = 0;
-
-    fBranchL = fTree->Branch("Leds.",  "Leds",  &fLeds);
-    fBranchR = fTree->Branch("Rings.", "Rings", &fRings);
-    fBranchT = fTree->Branch("Time.", &fTime, "fTime/D");//, &fTime);
-
-    cout << "Root file '" << name << "' open." << endl;
-}
-
-Bool_t MGStarguider::ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
-{
-    switch (GET_MSG(msg))
-    {
-    case kC_TEXTENTRY:
-        if (GET_SUBMSG(msg)==kTE_ENTER)
-            switch (mp1)
-            {
-            case IDM_kPixSize:
-                {
-                    const Float_t pixsize = atof(fPixSize->GetText());
-                    cout << "Pixel Size changed to " << pixsize << "\"/pix" << endl;
-                    fSao->SetPixSize(pixsize/3600);
-                    return kTRUE;
-                }
-            case IDM_kAngle:
-                {
-                    const Float_t angle = atof(fAngle->GetText());
-                    cout << "Rotation Angle changed to " << angle << "deg" << endl;
-                    fSao->SetRotationAngle(angle);
-                    return kTRUE;
-                }
-            }
-        return kTRUE;
-
-    case kC_COMMAND:
-        //cout << "kC_COMMAND" << endl;
-        switch (GET_SUBMSG(msg))
-        {
-        case kCM_MENU:
-            //cout << "kCM_MENU #" << mp1 << endl;
-            switch (mp1)
-            {
-            case IDM_kCatalog:
-                Toggle(fDisplay, IDM_kCatalog);
-                if (fDisplay->IsEntryChecked(IDM_kCatalog))
-                    fDisplay->EnableEntry(IDM_kStarguider);
-                else
-                {
-                    fDisplay->UnCheckEntry(IDM_kStarguider);
-                    fDisplay->DisableEntry(IDM_kStarguider);
-                }
-                return kTRUE;
-
-            case IDM_kStarguider:
-                Toggle(fDisplay, IDM_kStarguider);
-                gSystem->Unlink("tracking_error.txt");
-                return kTRUE;
-
-            case IDM_kFilter:
-                Toggle(fDisplay, IDM_kFilter);
-                if (fDisplay->IsEntryChecked(IDM_kFilter))
-                    fDisplay->EnableEntry(IDM_kStarguider);
-                else
-                {
-                    fDisplay->UnCheckEntry(IDM_kStarguider);
-                    fDisplay->DisableEntry(IDM_kStarguider);
-                }
-                return kTRUE;
-
-            case IDM_kStretch:
-                Toggle(fDisplay, IDM_kStretch);
-                return kTRUE;
-
-            case IDM_kCaosFilter:
-                if (!fDisplay->IsEntryChecked(IDM_kCaosFilter))
-                    OpenFile();
-                Toggle(fDisplay, IDM_kCaosFilter);
-                if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
-                    fMenu->AddPopup("&CaOs", fCaOs, NULL);
-                else
-                {
-                    fFile->Write();
-                    delete fFile;
-                    fFile = NULL;
-
-                    cout << "Root file closed." << endl;
-
-                    if(fWrite->IsEntryChecked(IDM_kPositions))
-                       fWrite->UnCheckEntry(IDM_kPositions);
-                    if(fWrite->IsEntryChecked(IDM_kLeds))
-                       fWrite->UnCheckEntry(IDM_kLeds);
-                    if(fWrite->IsEntryChecked(IDM_kRings))
-                        fWrite->UnCheckEntry(IDM_kRings);
-                    if(fAnalyse->IsEntryEnabled(IDM_kStopAnalyse))
-                    {
-                        fAnalyse->DisableEntry(IDM_kStopAnalyse);
-                        fAnalyse->EnableEntry(IDM_kStartAnalyse);
-                        ResetHists();
-                        // Graphs are not reset !!!
-                    }
-                    fMenu->RemovePopup("CaOs");
-                }
-                fMenu->Resize(fMenu->GetDefaultSize());
-                MapSubwindows();
-                MapWindow();
-                return kTRUE;
-
-            case IDM_kPositions:
-                Toggle(fWrite, IDM_kPositions);
-                return kTRUE;
-
-            case IDM_kLeds:
-                Toggle(fWrite, IDM_kLeds);
-                return kTRUE;
-
-            case IDM_kRings:
-                Toggle(fWrite, IDM_kRings);
-                return kTRUE;
-
-            case IDM_kStartAnalyse:
-                fAnalyse->DisableEntry(IDM_kStartAnalyse);
-                fAnalyse->EnableEntry(IDM_kStopAnalyse);
-                return kTRUE;
-
-            case IDM_kStopAnalyse:
-                {
-                    fAnalyse->DisableEntry(IDM_kStopAnalyse);
-                    fAnalyse->EnableEntry(IDM_kStartAnalyse);
-
-                    DisplayAnalysis();
-
-                    return kTRUE;
-                }
-
-            case IDM_kResetHistograms:
-                ResetHists();
-                return kTRUE;
-
-/*            case IDM_kResetGraph:
-                {
-                    fGraphx->GetHistogram()->Reset();
-
-                }
-*/
-
-            case IDM_kUseFileRaDec:
-                Toggle(fSetup, IDM_kUseFileRaDec);
-                return kTRUE;
-
-            case IDM_kStart:
-                fWritePictures->DisableEntry(IDM_kStart);
-                fWritePictures->EnableEntry(IDM_kStop);
-                return kTRUE;
-
-            case IDM_kStop:
-                fWritePictures->DisableEntry(IDM_kStop);
-                fWritePictures->EnableEntry(IDM_kStart);
-                return kTRUE;
-
-            case IDM_kPNG:
-                fFileType->CheckEntry(IDM_kPNG);
-                fFileType->UnCheckEntry(IDM_kPPM);
-                return kTRUE;
-
-            case IDM_kPPM:
-                fFileType->CheckEntry(IDM_kPPM);
-                fFileType->UnCheckEntry(IDM_kPNG);
-                return kTRUE;
-
-            case IDM_kOnce:
-                fWriteType->CheckEntry(IDM_kOnce);
-                fWriteType->UnCheckEntry(IDM_kContinous);
-                return kTRUE;
-
-            case IDM_kContinous:
-                fWriteType->CheckEntry(IDM_kContinous);
-                fWriteType->UnCheckEntry(IDM_kOnce);
-                return kTRUE;
-
-            case IDM_kRate25ps:
-            case IDM_kRate5ps:
-            case IDM_kRate1s:
-            case IDM_kRate5s:
-            case IDM_kRate30s:
-            case IDM_kRate1m:
-            case IDM_kRate5m:
-                for (int i=IDM_kRate25ps; i<=IDM_kRate5m; i++)
-                    if (mp1==i)
-                        fWriteRate->CheckEntry(i);
-                    else
-                        fWriteRate->UnCheckEntry(i);
-                switch (mp1)
-                {
-                case IDM_kRate25ps:
-                    fWrtRate = 1;
-                    return kTRUE;
-                case IDM_kRate5ps:
-                    fWrtRate = 5;
-                    return kTRUE;
-                case IDM_kRate1s:
-                    fWrtRate = 25;
-                    return kTRUE;
-                case IDM_kRate5s:
-                    fWrtRate = 5*25;
-                    return kTRUE;
-                case IDM_kRate30s:
-                    fWrtRate = 30*25;
-                    return kTRUE;
-                case IDM_kRate1m:
-                    fWrtRate = 60*25;
-                    return kTRUE;
-                case IDM_kRate5m:
-                    fWrtRate = 5*60*25;
-                    return kTRUE;
-                }
-                return kTRUE;
-
-            case IDM_kInterpol125:
-            case IDM_kInterpol25:
-            case IDM_kInterpol10:
-            case IDM_kInterpol5:
-            case IDM_kInterpol2:
-            case IDM_kInterpol1:
-                for (int i=IDM_kInterpol125; i<=IDM_kInterpol1; i++)
-                    if (mp1==i)
-                        fInterpol->CheckEntry(i);
-                    else
-                        fInterpol->UnCheckEntry(i);
-                switch (mp1)
-                {
-                case IDM_kInterpol1:
-                    fIntRate = 1;
-                    return kTRUE;
-                case IDM_kInterpol2:
-                    fIntRate = 2;
-                    return kTRUE;
-                case IDM_kInterpol5:
-                    fIntRate = 5;
-                    return kTRUE;
-                case IDM_kInterpol10:
-                    fIntRate = 10;
-                    return kTRUE;
-                case IDM_kInterpol25:
-                    fIntRate = 25;
-                    return kTRUE;
-                case IDM_kInterpol125:
-                    fIntRate = 125;
-                    return kTRUE;
-                }
-                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;
-        }
-        break;
-    }
-
-    return kTRUE;
-}
-
-void MGStarguider::GetCoordinates()
-{
-    XY xy = fCRaDec->GetCoordinates();
-
-    if (fSetup->IsEntryChecked(IDM_kUseFileRaDec))
-    {
-        ifstream fin("coordinates.txt");
-        if (!fin)
-            cout << "Error: Cannot open 'coordinates.txt' using fall back solution." << endl;
-        else
-            fin >> xy;
-    }
-
-    fCRaDec->SetCoordinates(xy);
-    fRaDec->Set(xy.X()*360/24, xy.Y());
-}
-
-ZdAz MGStarguider::TrackingError(TArrayF &x, TArrayF &y, TArrayF &mag) const
-{
-    //
-    // Viewable area (FIXME: AZ)
-    //
-    TH2F h("Hist", "dX/dY",  77, -768/2-.5,  768/2+.5, 58, -576/2-.5,  576/2+.5); // 3
-
-    /*
-    TH1F hmag("HistMag", "Mag", 19, 0, 100);
-    for (int i=0; i<mag.GetSize(); i++)
-        hmag.Fill(mag[i]);
-        */
-
-    //
-    // 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]);
-    }
-
-    //
-    // Serach for an excess in the histogram
-    //
-    Int_t mx, my, dummy;
-    h.GetMaximumBin(mx, my, dummy);
-
-    const double xmax = h.GetXaxis()->GetBinCenter(mx);
-    const double dx   = h.GetXaxis()->GetBinWidth(mx);
-
-    const double ymax = h.GetYaxis()->GetBinCenter(my);
-    const double dy   = h.GetYaxis()->GetBinWidth(my);
-
-    cout << setprecision(3);
-    cout << "Cut-XY:       " << xmax << " +- " << dx << " / " << ymax << " +- " << dy << endl;
-
-    TGraph g;
-    for (int i=0; i<mag.GetSize(); i++)
-    {
-        if (!(x[i]>xmax-dx && x[i]<xmax+dx &&
-              y[i]>ymax-dy && y[i]<ymax+dy &&
-              mag[i]>48-15 && mag[i]<48+15))
-            continue;
-
-        g.SetPoint(g.GetN(), x[i], y[i]);
-    }
-
-    cout << "Offset-XY:    " << g.GetMean(1) << " +- " << g.GetRMS(1) << " / ";
-    cout << g.GetMean(2) << " +- " << g.GetRMS(2) << endl;
-
-    AltAz pos0 = fSao->CalcAltAzFromPix(768/2,              576/2)*kRad2Deg;
-    AltAz pos1 = fSao->CalcAltAzFromPix(768/2+g.GetMean(1), 576/2+g.GetMean(2))*kRad2Deg;
-
-    ofstream fout1("pointingpos.txt");
-    fout1 << setprecision(10) << fSao->GetMjd()-52000 << " " << -pos1.Alt() << " " << pos1.Az() << endl;
-
-    pos1 -= pos0;
-
-    ofstream fout2("tracking_error.txt", ios::app);
-    fout2 << setprecision(10) << fSao->GetMjd()-52000 << " " << -pos1.Alt() << " " << pos1.Az() << endl;
-
-    return ZdAz(-pos1.Alt(), pos1.Az());
-}
-
-void MGStarguider::CalcTrackingError(Leds &leds, MStarList &stars)
-{
-    const Int_t max = leds.GetEntries();
-
-    if (stars.GetRealEntries() < 3)
-    {
-        cout << "Sorry, less than 3 stars in FOV!" << endl;
-        return;
-    }
-
-    if (max < 1)
-    {
-        cout << "Sorry, less than 1 detected spot in FOV!" << endl;
-        return;
-    }
-
-    stars.Sort(); // Sort by magnitude
-
-    TString str = "data/tracking_";
-    str += fSao->GetMjd()-52000;
-    str += ".txt";
-
-    ofstream fout(str);
-
-    TArrayF x, y, mag;
-
-    Int_t num = 0;
-
-    // FIXME: Is predifined value 3 a good idea?
-
-    MStar *star;
-    MStarListIter NextStar(&stars);
-    while ((star=NextStar()) && num++<max+3)
-    {
-        TIter NextSp(&leds);
-        Led *spot=NULL;
-        while ((spot=(Led*)NextSp()))
-        {
-            const XY dpos(spot->GetX()-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(spot->GetMag()/star->GetMag(), idx);
-
-            if (fout)
-                fout << x[idx] << " " << y[idx] << " " << mag[idx] << endl;
-        }
-    }
-
-    ZdAz d = TrackingError(x, y, mag);
-
-    //
-    // Calculated offsets
-    //
-
-    // 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;
-
-    //
-    // Current Pointing position
-    //
-    ZdAz cpos = fSao->GetZdAz()-d;
-    fPZdAz->SetCoordinates(cpos);
-}
-
-
-void MGStarguider::ProcessFrame(const unsigned long n, byte *img, struct timeval *tm)
-{
-    static float myimg[768*576];
-
-    for (int i=0; i<768*576; i++)
-        myimg[i] += img[i];
-
-    if (n%fIntRate)
-        return;
-
-    //cout << "Img: " << n << endl;
-
-    byte c[768*576];
-    for (int i=0; i<768*576; i++)
-        c[i] = (byte)(myimg[i]/fIntRate+.5);
-
-    if (fDisplay->IsEntryChecked(IDM_kStretch))
-        Filter::Stretch(c);
-
-    if (!fWritePictures->IsEntryEnabled(IDM_kStart) &&
-        (!(n%fWrtRate) || fWriteType->IsEntryChecked(IDM_kOnce)))
-    {
-        if (fFileType->IsEntryChecked(IDM_kPNG))
-            Writer::Png("pix/file", c, tm);
-
-        if (fFileType->IsEntryChecked(IDM_kPPM))
-            Writer::Ppm("pix/file", c, tm);
-
-        if (fWriteType->IsEntryChecked(IDM_kOnce))
-            ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStop, 0);
-    }
-
-    MStarList spots;
-
-    /*
-    if (fDisplay->IsEntryChecked(IDM_kStarguider))
-        Filter2::Execute(spots, c);
-    else
-     */
-    if (fDisplay->IsEntryChecked(IDM_kFilter))
-        Filter::Execute(c);
-
-    if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
-    {
-        // Double_t kConv = 0.502; // [pix/mm]
-
-        static Timer t0(tm);
-        Timer t(tm);
-
-        fTime = (t.GetMjd()-t0.GetMjd())*24*60*60;
-
-        Leds leds;
-        CaosFilter::Execute(c, leds, 1);
-
-        if (fWrite->IsEntryChecked(IDM_kPositions))
-            leds.Print();
-
-        CaosFilter::FilterLeds(leds);
-        leds.Compress();
-
-        if (fWrite->IsEntryChecked(IDM_kLeds))
-            leds.Print();
-
-        Rings rings;
-        rings.CalcRings(leds);
-
-        leds.Sort();
-
-        fLeds  = &leds;
-        fRings = &rings;
-
-        if (fFile)
-            fTree->Fill();
-
-        if (fWrite->IsEntryChecked(IDM_kRings))
-            rings.Print();
-
-        if (fAnalyse->IsEntryEnabled(IDM_kStopAnalyse))
-        {
-            const Ring &center = rings.GetCenter();
-
-            Double_t phi[6] =
-            {
-                -124.727,
-                 -61.0495,
-                 -16.7907,
-                  49.3119,
-                 139.086
-            };
-
-            fHistpr->Fill(center.GetR());
-            fHistprx->Fill(center.GetX());
-            fHistpry->Fill(center.GetY());
-            fHistprxpry->Fill(center.GetX(), center.GetY());
-
-            Double_t sum = 0;
-            for (int i=0; i<6 && leds.At(i); i++)
-            {
-                const Double_t w = (leds(i).GetPhi()-phi[i])*60;
-
-                sum += w;
-
-                fHistw[i]->Fill(w);
-                fHistv[i]->Fill(leds(i).GetPhi());
-                fGraphw[i]->SetPoint(fGraphw[i]->GetN(), fTime, w);
-            }
-            fHistallw->Fill(sum/5);
-
-            fGraphprx->SetPoint(fGraphprx->GetN(), fTime, center.GetX());
-            fGraphpry->SetPoint(fGraphpry->GetN(), fTime, center.GetY());
-        }
-    }
-
-    byte zimg[kZOOM*kZOOM];
-    for (int y=0; y<kZOOM; y++)
-        for (int x=0; x<kZOOM; x++)
-            zimg[x+y*kZOOM] = c[(fDx+(x-kZOOM/2)/2)+(fDy+(y-kZOOM/2)/2)*768];
-
-    fZoomImage->DrawImg(zimg);
-
-    if (fDisplay->IsEntryChecked(IDM_kCatalog))
-    {
-        Timer time(tm);
-
-        GetCoordinates();
-
-        MStarList stars;
-        fSao->GetStars(stars, time.GetMjd(), *fRaDec);
-
-        if (fDisplay->IsEntryChecked(IDM_kStarguider))
-        {
-            Leds leds;
-            CaosFilter::Execute(c, leds, 1);
-
-            cout << "Found: " << leds.GetEntries() << " leds" << endl;
-
-            CaosFilter::RemoveTwins(leds, 3);
-            leds.Compress();
-
-            cout << "Rest: " << leds.GetEntries() << " leds" << endl;
-
-            CalcTrackingError(leds, stars);
-        }
-
-        byte cimg[768*576];
-        fSao->GetImg(c, cimg, stars);
-
-        DrawCircle(c, 0.5);
-        DrawCircle(c, 1.0);
-        DrawCircle(c, 1.5);
-
-        fCZdAz->SetCoordinates(fSao->GetZdAz());
-
-        fImage->DrawColImg(c, cimg);
-    }
-    else
-        fImage->DrawImg(c);
-
-    memset(myimg, 0, 768*576*sizeof(float));
-}
-
-void MGStarguider::DrawCircle(byte *img, double r)
-{
-    const double rpix = r*60*60/fSao->GetPixSize()+1;
-    const int cx = 768/2;
-    const int cy = 576/2;
-    for (int dx=-(int)(rpix*0.7); dx<(int)(rpix*0.7); dx++)
-    {
-        const int dy = (int)sqrt(rpix*rpix-dx*dx);
-        img[cx+dx + (cy-dy)*768] = 0x40;
-        img[cx+dx + (cy+dy)*768] = 0x40;
-        img[cx-dy + (cy+dx)*768] = 0x40;
-        img[cx+dy + (cy+dx)*768] = 0x40;
-    }
-}
-
-Bool_t MGStarguider::HandleDoubleClick(Event_t *event)
-{
-    const Int_t w = fImage->GetWidth();
-    const Int_t h = fImage->GetHeight();
-    const Int_t x = fImage->GetX();
-    const Int_t y = fImage->GetY();
-
-    if (!(event->fX>x && event->fX<x+w && event->fY>y && event->fY<y+h))
-        return kTRUE;
-
-    Int_t dx = event->fX-x;
-    Int_t dy = event->fY-y;
-
-    if (dx<kZOOM/4) dx=kZOOM/4;
-    if (dy<kZOOM/4) dy=kZOOM/4;
-    if (dx>766-kZOOM/2) dx=766-kZOOM/4;
-    if (dy>510-kZOOM/2) dy=510-kZOOM/4;
-
-    fDx = dx;
-    fDy = dy;
-
-    fSao->Now();
-    AltAz aa = fSao->CalcAltAzFromPix(fDx, fDy)*kRad2Deg;
-
-    cout << "New coordinates for zoom - x, y   : " << fDx << " " << fDy << endl;
-    cout << "New coordinates for zoom - Alt, az: " << aa.Alt() << " " << aa.Az() << endl;
-
-    return kTRUE;
-}
Index: unk/MagicSoft/Cosy/gui/MGStarguider.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGStarguider.h	(revision 2277)
+++ 	(revision )
@@ -1,145 +1,0 @@
-#ifndef COSY_MGStarguider
-#define COSY_MGStarguider
-
-#ifndef ROOT_TGFrame
-#include <TGFrame.h>
-#endif
-#include "PixClient.h"
-#ifndef MARS_MObservatory
-#include "MObservatory.h"
-#endif
-
-#include "MGList.h"
-#include "MGImage.h"
-
-#include "coord.h"
-
-class TArrayF;
-class TH1F;
-class TH2F;
-class TGraph;
-
-class TTimer;
-
-class MGMenuBar;
-class MGPopupMenu;
-class TGTextEntry;
-
-class MGImage;
-class MGCoordinates;
-
-class StarCatalog;
-class MStarList;
-
-class TFile;
-class TTree;
-class TBranch;
-
-class Leds;
-class Rings;
-
-class MGStarguider : public PixClient, public TGMainFrame
-{
-private:
-    MGList        *fList;
-
-    MGMenuBar     *fMenu;
-    MGImage       *fImage;
-    MGImage       *fZoomImage;
-
-    MGPopupMenu   *fDisplay;
-    MGPopupMenu   *fWrite;
-    MGPopupMenu   *fWritePictures;
-    MGPopupMenu   *fFileType;
-    MGPopupMenu   *fWriteType;
-    MGPopupMenu   *fAnalyse;
-    MGPopupMenu   *fCaOs;
-    MGPopupMenu   *fWriteRate;
-    MGPopupMenu   *fInterpol;
-    MGPopupMenu   *fSetup;
-    MGPopupMenu   *fLimMag;
-
-    TFile         *fFile;
-    TTree         *fTree;
-    TBranch       *fBranchL;
-    TBranch       *fBranchT;
-    TBranch       *fBranchR;
-
-    Leds *fLeds;
-    Rings *fRings;
-    Double_t fTime;
-
-    TH1F          *fHistpr;
-    TH1F          *fHistprx;
-    TH1F          *fHistpry;
-    TH1F          *fHistw[6];
-    TH1F          *fHistallw;
-    TH1F          *fHistv[6];
-
-    TH2F          *fHistprxpry;
-
-    TGraph        *fGraphprx;
-    TGraph        *fGraphpry;
-    TGraph        *fGraphw[6];
-
-    MGCoordinates *fCRaDec;
-    MGCoordinates *fCZdAz;
-
-    MGCoordinates *fPZdAz;
-
-    TGTextEntry   *fPixSize;
-    TGTextEntry   *fAngle;
-
-    StarCatalog   *fSao;
-
-    RaDec *fRaDec;
-
-    TTimer *fTimer;
-
-    Int_t fDx;
-    Int_t fDy;
-
-    int fIntRate;
-    int fWrtRate;
-
-    void SetPixSize(const double pixsize);
-    void Toggle(MGPopupMenu *p, UInt_t id);
-    void GetCoordinates();
-    void CalcTrackingError(Leds &, MStarList &);
-    ZdAz TrackingError(TArrayF &alt, TArrayF &az, TArrayF &mag) const;
-
-    void InitHists();
-    void InitGraphs();
-    void InitGui();
-
-    void OpenFile();
-
-    void ResetHists();
-    void DisplayAnalysis();
-
-    Bool_t HandleTimer(TTimer *t);
-    //Bool_t HandleKey(Event_t* event);
-
-    void DrawCircle(byte *img, double r);
-
-public:
-    MGStarguider(MObservatory::LocationName_t obs);
-    virtual ~MGStarguider();
-
-    //void Update();
-
-    void Layout();
-    void CloseWindow();
-
-    Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t parm2);
-    Bool_t HandleDoubleClick(Event_t *event);
-
-    //
-    // Execution of one frame - this function may be overloaded!
-    //
-    void ProcessFrame(const unsigned long n, byte *img, struct timeval *tm);
-
-    ClassDef(MGStarguider, 0)
-};
-
-#endif
Index: /trunk/MagicSoft/Cosy/gui/MGVelocity.cc
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGVelocity.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/MGVelocity.cc	(revision 2278)
@@ -24,79 +24,4 @@
 void MGVelocity::DrawCoordinateSystem()
 {
-    /*
-    TArc arc;
-    arc.SetFillStyle(4000);  // transparent
-    arc.SetFillColor(39);
-    arc.SetLineColor(3);     // green
-    arc.SetLineStyle(2);     // dashed  (s. TAttLine)
-    arc.DrawArc(0, 0,  20);
-
-    arc.SetLineColor(5);     // yellow
-    arc.DrawArc(0, 0,  40);
-
-    arc.SetLineColor(2);     // red
-    arc.DrawArc(0, 0, 60);
-    */
-    //
-    // FIXME? Use TAxis?
-    //
-/*
-    TLine line;
-    line.SetLineColor(13);
-    line.SetLineStyle(3);  // dotted  (s. TAttLine)
-    line.DrawLine(-30., -65., -30.,  65.);
-    line.DrawLine(-65., -30.,  65., -30.);
-    line.DrawLine( 30., -65.,  30.,  65.);
-    line.DrawLine( -6.,  30.,  65.,  30.);
-
-    line.DrawLine(-15., -65., -15.,  65.);
-    line.DrawLine(-65., -15.,  65., -15.);
-    line.DrawLine( 15., -65.,  15.,  65.);
-    line.DrawLine(-65.,  15.,  65.,  15.);
-
-    line.DrawLine(-45., -65., -45.,  65.);
-    line.DrawLine(-65., -45.,  65., -45.);
-    line.DrawLine( 45., -65.,  45.,  65.);
-    line.DrawLine(-65.,  45.,  65.,  45.);
-
-    line.SetLineColor(12);
-    line.SetLineStyle(2);  // dashed  (s. TAttLine)
-    line.DrawLine(-60., -65., -60.,  65.);
-    line.DrawLine(-65., -60.,  65., -60.);
-    line.DrawLine( 60., -65.,  60.,  65.);
-    line.DrawLine(-65.,  60.,  65.,  60.);
-
-    line.SetLineColor(1);  // black
-    line.SetLineStyle(1);  // solid  (s. TAttLine)
-    line.DrawLine(-65.,   0, 65.,  0);
-    line.DrawLine(  0, -65.,  0, 65.);
-
-    line.DrawLine(-1.,  60., 1.,  60.);
-    line.DrawLine(-1., -60., 1., -60.);
-    line.DrawLine(-1.,  30., 1.,  30.);
-    line.DrawLine(-1., -30., 1., -30.);
-
-    line.DrawLine( 60., -1.,  60., 1.);
-    line.DrawLine(-60., -1., -60., 1.);
-    line.DrawLine( 30., -1.,  30., 1.);
-    line.DrawLine(-30., -1., -30., 1.);
-
-    TText text;
-    text.SetTextAlign(22);  // centered, centered (s.TAttText)
-    text.DrawText(60., 5., "vAz['/min]");
-    text.DrawText(0,  70., "vZd['/min]");
-
-    text.SetTextAlign(23);  // centered, centered (s.TAttText)
-    text.DrawText(-60., -2., "-1'");
-    text.DrawText( 60., -2., "1'");
-    text.DrawText(-30., -2., "-.5'");
-    text.DrawText( 30., -2., ".5'");
-
-    text.SetTextAlign(32);  // centered, centered (s.TAttText)
-    text.DrawText(-2., -60., "-1'");
-    text.DrawText(-2.,  60., "1'");
-    text.DrawText(-2., -30., "-.5'");
-    text.DrawText(-2.,  30., ".5'");
-    */
     TWbox box;
     box.DrawWbox(-145, 145, -35, 120,  18,  2, 1);
@@ -130,19 +55,4 @@
 void MGVelocity::InitVelocity()
 {
-    /*
-     fLin1  = new TLine(0, 0, 0, 0);
-    fLin2  = new TLine(0, 0, 0, 0);
-
-    fLin1->SetLineColor(10); // white (s. TAttFill)
-    fLin2->SetLineColor(10); // white
-    fLin1->SetLineStyle(1);  // solid (s. TAttLine)
-    fLin2->SetLineStyle(1); 
-
-    fLin1->Draw();
-    fLin2->Draw();
-
-    fList->Add(fLin1);
-    fList->Add(fLin2);
-    */
     fArrow    = new TArrow(0, 0, 0, 0, 0.01);
     fArrowX   = new TArrow(0, 0, 0, 0, 0.01);
@@ -170,10 +80,4 @@
     fText->Draw();
     fList->Add(fText);
-
-    fTextVel = new TText(2*70, 2*70, "");
-    fTextVel->SetTextAlign(33);  // right, top
-    fTextVel->SetTextColor(10);  // white
-    fTextVel->Draw();
-    fList->Add(fTextVel);
 }
 
@@ -255,6 +159,4 @@
     fArrowAvg->SetX2(avgx);
     fArrowAvg->SetY2(avgy);
-
-    //    cout << avgx << " " << avgy << endl;
 
     if ((fabs(avgx)>/*40.*/110. || fabs(avgy)>/*40.*/110.))
@@ -274,28 +176,4 @@
 }
 
-void MGVelocity::UpdateVelText(Float_t vx, Float_t vy)
-{
-    static int X = ~0;
-    static int Y = ~0;
-
-    vx /= 60.;  //['/min]
-    vy /= 60.;  //['/min]
-
-    int fx = (int)/*floor*/(vx*10.);
-    int fy = (int)/*floor*/(vy*10.);
-
-    if (X==fx && Y==fy)
-        return;
-
-    X = fx;
-    Y = fy;
-
-    char txt[100];
-    sprintf(txt, "Zd=%.1f'\nAz=%.1f'",
-            vy, vx);
-
-    fTextVel->SetText(fTextVel->GetX(), fTextVel->GetY(), txt);
-}
-
 void MGVelocity::Update(ZdAz &zdaz)
 {
@@ -303,12 +181,7 @@
     // calculate actual time for planet positions
     //
-    //    static int X = 0xaffe;
-    //    static int Y = 0xaffe;
-
     float vx = zdaz.Az()*3600.; // ["/min]
     float vy = zdaz.Zd()*3600.; // ["/min]
 
-    UpdateVelText(vx, vy);
-
     int pixx = (int)(vx*fScale/fPix);
     int pixy = (int)(vy*fScale/fPix);
@@ -328,8 +201,4 @@
         return;
 
-    //    cout << fScale << ": " << pixx << " " << pixy;
-    //    cout << fScale << ": " << (int)rc << (int)((int)fOld->X()==pixx) << (int)((int)fOld->Y()==pixy) << "  ";
-    //    cout << zdaz.Az()*3600. << " " << zdaz.Zd()*3600. << endl;
-
     vx *= fScale;
     vy *= fScale;
Index: /trunk/MagicSoft/Cosy/gui/MGVelocity.h
===================================================================
--- /trunk/MagicSoft/Cosy/gui/MGVelocity.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/MGVelocity.h	(revision 2278)
@@ -31,5 +31,5 @@
 
     TText *fText;
-    TText *fTextVel;
+//    TText *fTextVel;
 
     XY *fOld;
@@ -44,5 +44,5 @@
     void DrawCoordinateSystem();
     void UpdateText();
-    void UpdateVelText(Float_t x, Float_t y);
+    //void UpdateVelText(Float_t x, Float_t y);
 
 public:
Index: /trunk/MagicSoft/Cosy/gui/Makefile
===================================================================
--- /trunk/MagicSoft/Cosy/gui/Makefile	(revision 2277)
+++ /trunk/MagicSoft/Cosy/gui/Makefile	(revision 2278)
@@ -32,15 +32,14 @@
 .SUFFIXES: .c .cc .cxx .h .hxx .o 
 
-SRCFILES = MGCoordinate.cc \
+SRCFILES = MGCosy.cc \
+           MGCoordinate.cc \
 	   MGCoordinates.cc \
-	   MGCosy.cc \
 	   MGImage.cc \
 	   MGEmbeddedCanvas.cc \
 	   MGAccuracy.cc \
 	   MGVelocity.cc \
-           MGStarguider.cc \
 	   MGSkyPosition.cc \
-           MGMenu.cc
-
+           MGMenu.cc 
+	   
 SRCS        = $(SRCFILES)
 HEADERS     = $(SRCFILES:.cc=.h)
Index: /trunk/MagicSoft/Cosy/leds.txt
===================================================================
--- /trunk/MagicSoft/Cosy/leds.txt	(revision 2278)
+++ /trunk/MagicSoft/Cosy/leds.txt	(revision 2278)
@@ -0,0 +1,6 @@
+321 535  -1.2 -1.0
+573 460  -2.0  0.9
+633 204   0.0 -1.6
+446  25   0.0  0.0
+193  95   2.0  1.0
+118 347   0.0  9.1
Index: /trunk/MagicSoft/Cosy/main/MCaos.cc
===================================================================
--- /trunk/MagicSoft/Cosy/main/MCaos.cc	(revision 2278)
+++ /trunk/MagicSoft/Cosy/main/MCaos.cc	(revision 2278)
@@ -0,0 +1,410 @@
+#include "MCaos.h"
+
+#include <iostream>
+#include <iomanip>
+
+#include <TSystem.h>
+#include <TFile.h>
+#include <TTree.h>
+#include <TH1.h>
+#include <TH2.h>
+#include <TGraph.h>
+#include <TCanvas.h>
+
+#include "Led.h"
+#include "Ring.h"
+#include "Rings.h"
+#include "FilterLed.h"
+
+#include "coord.h"
+#include "base/timer.h"
+
+void MCaos::ReadResources(const char *name="leds.txt")
+{
+    ifstream fin(name);
+    if (!fin)
+    {
+        cout << "ERROR - Cannot open " << name << endl;
+        return;
+    }
+
+    fPositions.Clear();
+
+    cout << " Reading " << name << ":" << endl;
+    cout << "------------------------------" << endl;
+
+    while (1)
+    {
+        Double_t px, py, ox, oy;
+        fin >> px >> py >> ox >> oy;
+        if (!fin)
+            break;
+
+        cout << " Led #" << fPositions.GetEntriesFast() << ":  ";
+        cout << setw(3) << px << " ";
+        cout << setw(3) << py << "  (";
+        cout << setw(3) << ox << ", ";
+        cout << setw(3) << oy << ")" << endl;
+        AddPosition(px, py, ox, oy);
+    }
+    cout << "Found " << fPositions.GetEntriesFast() << " leds." << endl;
+}
+
+void MCaos::OpenFile()
+{
+    int i=0;
+    char name[100];
+    while (1)
+    {
+        sprintf(name, "data/data%03d.root", i++);
+        if (gSystem->AccessPathName(name, kFileExists))
+            break;
+    }
+
+    if (fFile)
+        delete fFile;
+
+    fFile = new TFile(name, "RECREATE");
+
+    if (!fFile->IsOpen())
+    {
+        delete fFile;
+        fFile = NULL;
+
+        cout << "Error: Cannot open file '" << name << "'" << endl;
+    }
+
+    TTree *tree = new TTree("Data", "Real CaOs Data");
+
+    fLeds = new Leds;
+    fEvtTime = 0;
+
+    tree->Branch("Leds", "TClonesArray", &fLeds);
+    tree->Branch("ZenithDist.", &fZenithDist, "fZenithDist/D");
+    tree->Branch("Azimuth.",    &fAzimuth,    "fAzimuth/D");
+    tree->Branch("EvtTime.",    &fEvtTime,    "fEvtTime/D");
+
+    cout << "Root file '" << name << "' open." << endl;
+}
+
+void MCaos::CloseFile()
+{
+    if (!fFile)
+        return;
+
+    const TString  name = fFile->GetName();
+    const Double_t n    = ((TTree*)fFile->Get("Data"))->GetEntries();
+
+    fFile->Write();
+    delete fFile;
+    fFile = NULL;
+
+    cout << "Root file closed (n=" << n << ")" << endl;
+
+    if (n<1)
+    {
+        gSystem->Unlink(name);
+        cout << "Root file deleted - no entries." << endl;
+    }
+}
+
+void MCaos::InitHistograms()
+{
+    if (fHistpr)
+        return;
+
+    Rings r;
+    r.CalcRings(fPositions);
+
+    const Ring &c = r.GetCenter();
+
+    Double_t xmin = c.GetX()-50;
+    Double_t xmax = c.GetX()+50;
+
+    Double_t ymin = c.GetY()-50;
+    Double_t ymax = c.GetY()+50;
+
+    Double_t rmin = c.GetR()-50;
+    Double_t rmax = c.GetR()+50;
+
+    Int_t xbin = 1001;
+    Int_t ybin = 1001;
+    Int_t rbin = 1001;
+
+    const Int_t sz = 50;
+    fHistled = new TH2F*[fPositions.GetEntriesFast()];
+    fHistw   = new TH1F*[fPositions.GetEntriesFast()];
+    for (int i=0; i<fPositions.GetEntriesFast(); i++)
+    {
+        TString name  = "LED";
+        TString title = "LED #";
+
+        name += i;
+        title += i;
+
+        const Led &p = fPositions(i);
+        fHistled[i] = new TH2F(name, title,
+                               20*sz+1, p.GetX()-sz, p.GetX()+sz,
+                               20*sz+1, p.GetY()-sz, p.GetY()+sz);
+        fHistled[i]->SetXTitle("x [pix]");
+        fHistled[i]->SetYTitle("counts");
+
+        name = "Angle";
+        title = "Angle of the Led #";
+
+        name += i;
+        title += i;
+
+        fHistw[i] = new TH1F;
+        fHistw[i]->SetNameTitle(name, title);
+        fHistw[i]->SetBins(721, -180.5, 180.5);
+        fHistw[i]->SetXTitle("\\Phi [deg]");
+        fHistw[i]->SetYTitle("counts");
+    }
+
+    fHistallw = new TH1F;
+    fHistallw->SetNameTitle("allw","Rotation angel");
+    fHistallw->SetBins(26, -25, 25);
+    fHistallw->SetXTitle("\\Phi [arcmin]");
+    fHistallw->SetYTitle("counts");
+
+    fHistprxpry = new TH2F;
+    fHistprxpry->SetNameTitle("prx und pry","x- and y-coordniate of the ring-center");
+    fHistprxpry->SetBins(xbin, xmin, xmax, ybin, ymin, ymax);
+    fHistprxpry->SetXTitle("x [mm]");
+    fHistprxpry->SetYTitle("y [mm]");
+    fHistprxpry->SetZTitle("counts");
+
+    fGraphprx = new TGraph;
+    fGraphprx->SetTitle("time-developement of the x-coordinate of the ring-center");
+
+    fGraphpry = new TGraph;
+    fGraphpry->SetTitle("time-developement of the y-coordinate of the ring-center");
+
+    fGraphw = new TGraph*[fPositions.GetEntriesFast()];
+    for (int i=0; i<fPositions.GetEntriesFast(); i++)
+    {
+        TString title = "Time-developement of the angle ";
+        title += i;
+
+        fGraphw[i] = new TGraph;
+        fGraphw[i]->SetTitle(title);
+    }
+
+    fHistpr = new TH1F("pr","Radius of the ring", rbin, rmin, rmax);
+    fHistpr->SetXTitle("r [pix]");
+    fHistpr->SetYTitle("counts");
+}
+
+void MCaos::DeleteHistograms()
+{
+    TH1F *dummy = fHistpr;
+    fHistpr=NULL;
+
+    if (!dummy)
+        return;
+
+    delete dummy;
+    delete fHistprxpry;
+    delete fHistallw;
+    delete fGraphprx;
+    delete fGraphpry;
+
+    for (int i=0; i<6; i++)
+    {
+        delete fHistled[i];
+        delete fHistw[i];
+        delete fGraphw[i];
+    }
+    delete fHistled;
+    delete fHistw;
+}
+
+void MCaos::ShowHistograms()
+{
+    if (!fHistpr || fHistpr->GetEntries()<1)
+        return;
+
+    TH1 *h;
+
+    TCanvas *c = new TCanvas("cring", "Center of the ring", 800, 800);
+    c->Divide(2,2);
+    c->cd(1);
+    h = (TH1*)fHistprxpry->ProfileX();
+    h->Fit("gaus");
+    h->Draw();
+    h->SetBit(kCanDelete);
+    c->cd(2);
+    h = (TH1*)fHistprxpry->ProfileY();
+    h->Fit("gaus");
+    h->Draw();
+    h->SetBit(kCanDelete);
+    c->cd(3);
+    fHistpr->Fit("gaus");
+    fHistpr->DrawCopy();
+    c->cd(4);
+    fHistprxpry->DrawCopy(/*"surf2"*/);
+    c->Update();
+
+    const Int_t n1 = (Int_t)(sqrt(fPositions.GetEntriesFast())+1.0);
+    const Int_t n2 = (Int_t)(sqrt(fPositions.GetEntriesFast())+0.5);
+
+    TCanvas *c1 = new TCanvas("cpos", "Led Positions", 800, 600);
+    TCanvas *c2 = new TCanvas("cangle", "Angles of the Leds", 800, 600);
+    c1->Divide(n1, n2);
+    c2->Divide(n1, n2);
+    for (int i=0; i<fPositions.GetEntriesFast(); i++)
+    {
+        c1->cd(i+1);
+        fHistled[i]->DrawCopy();
+        c2->cd(i+1);
+        fHistw[i]->DrawCopy();
+    }
+    c1->Update();
+    c2->Update();
+
+    c = new TCanvas("ctime", "timedevelopement of center", 800, 800);
+    c->Divide(1,2);
+    c->cd(1);
+    h = fGraphprx->GetHistogram();
+    h->SetXTitle("time [s]");
+    h->SetYTitle("x [pix]");
+    h->DrawCopy();
+    //fGraphprx->DrawClone("LP*")->SetBit(kCanDelete);
+    c->cd(2);
+    h = fGraphpry->GetHistogram();
+    h->SetXTitle("time [s]");
+    h->SetYTitle("y [pix]");
+    h->DrawCopy();
+    //fGraphpry->DrawClone("LP*")->SetBit(kCanDelete);
+    c->Modified();
+    c->Update();
+
+    /* --------------------------------------------------------
+     *  CALCULATE OFFSETS!
+     * --------------------------------------------------------
+     Rings r;
+     r.CalcRings(fPositions);
+
+     const Ring &c = r.GetCenter();
+     */
+}
+
+void MCaos::ResetHistograms()
+{
+    if (!fHistpr)
+        return;
+
+    fHistpr->Reset();
+    fHistprxpry->Reset();
+    fHistallw->Reset();
+    for (int i=0; i<6; i++)
+    {
+        fHistled[i]->Reset();
+        fHistw[i]->Reset();
+    }
+}
+
+void MCaos::Run(byte *img, bool printl, bool printr, const ZdAz &pos, const Timer &t)
+{
+    Leds &leds = *fLeds;
+    leds.Clear();
+
+    //          img  width height radius sigma
+    FilterLed f(img, 768, 576, 50, 4.0);
+
+    Int_t first=0;
+    for (int i=0; i<fPositions.GetEntriesFast(); i++)
+    {
+        // Try to find Led in this area
+        const Led &l0 = fPositions(i);
+        f.Execute(leds, l0.GetX(), l0.GetY());
+
+        // Loop over newly found Leds
+        for (int j=first; j<leds.GetEntries(); j++)
+        {
+            Led &l1 = leds(j);
+
+            // Add Offset to Led
+            l1.AddOffset(l0.GetDx(), l0.GetDy());
+
+            // Mark Led in image (FIXME: Move to MStarguider)
+            f.MarkPoint(l1.GetX(), l1.GetY(), l1.GetMag());
+
+            // Fill values into Histogram
+            if (!fHistpr)
+                continue;
+
+            fHistled[i]->Fill(l1.GetX(), l1.GetY());
+            fHistw[i]->Fill(l1.GetPhi());
+        }
+        first = leds.GetEntries();
+    }
+
+    Rings rings;
+    rings.CalcRings(leds, 274-10, 274+10);
+
+    const Ring &center = rings.GetCenter();
+
+    f.DrawCircle(center, 0x80);
+    f.DrawCircle(center,   5.0, 0x80);
+    f.DrawCircle(center, 115.0, 0x80);
+    f.DrawCircle(center, 190.0, 0x80);
+    // f.MarkPoint(center.GetX(), center.GetY(), 0xa0);
+
+    // FIXME!
+    static const Timer t0(t);
+    fEvtTime = (double)t-(double)t0;
+
+    if (fHistpr)
+    {
+        fHistpr->Fill(center.GetR());
+        fHistprxpry->Fill(center.GetX(), center.GetY());
+        fGraphprx->SetPoint(fGraphprx->GetN(), fEvtTime, center.GetX());
+        fGraphpry->SetPoint(fGraphpry->GetN(), fEvtTime, center.GetY());
+    }
+
+    if (printl)
+        leds.Print();
+    if (printr)
+        rings.Print();
+
+    if (fFile && leds.GetEntries()>0)
+    {
+        fZenithDist = pos.Zd(); //fCosy ? fCosy->GetPointingPos().Zd() : 0
+        fAzimuth    = pos.Az(); //fCosy ? fCosy->GetPointingPos().Az() : 0;
+
+        TTree *t = (TTree*)fFile->Get("Data");
+        t->Fill();
+    }
+
+    /*
+        if (fCaosAnalyse->IsEntryEnabled(IDM_kStopAnalyse))
+        {
+            const Ring &center = rings.GetCenter();
+
+            Double_t phi[6] =
+            {
+                -124.727,
+                 -61.0495,
+                 -16.7907,
+                  49.3119,
+                 139.086
+            };
+
+            Double_t sum = 0;
+            for (int i=0; i<6 && leds.At(i); i++)
+            {
+                const Double_t w = (leds(i).GetPhi()-phi[i])*60;
+
+                sum += w;
+
+                fHistw[i]->Fill(w);
+                fHistv[i]->Fill(leds(i).GetPhi());
+                fGraphw[i]->SetPoint(fGraphw[i]->GetN(), fEvtTime, w);
+            }
+            fHistallw->Fill(sum/5);
+            }
+            */
+}
+
Index: /trunk/MagicSoft/Cosy/main/MCaos.h
===================================================================
--- /trunk/MagicSoft/Cosy/main/MCaos.h	(revision 2278)
+++ /trunk/MagicSoft/Cosy/main/MCaos.h	(revision 2278)
@@ -0,0 +1,73 @@
+#ifndef CAOS_MCaos
+#define CAOS_MCaos
+
+#ifndef CAOS_Leds
+#include "Leds.h"
+#endif
+
+#ifndef COSY_PixClient
+#include "PixClient.h" // byte
+#endif
+
+class TFile;
+class TH1F;
+class TH2F;
+class TGraph;
+
+class Timer;
+class ZdAz;
+
+class MCaos
+{
+private:
+    TFile         *fFile;       // File we may write data to
+
+    Leds           fPositions;
+
+    Leds          *fLeds;
+    Double_t       fEvtTime;
+    Double_t       fZenithDist;
+    Double_t       fAzimuth;
+
+    TH1F          *fHistpr;
+    TH2F         **fHistled;
+    TH1F          *fHistallw;
+    TH1F         **fHistw;
+
+    TH2F          *fHistprxpry;
+
+    TGraph        *fGraphprx;
+    TGraph        *fGraphpry;
+    TGraph       **fGraphw;
+
+public:
+    MCaos() : fFile(NULL), fHistpr(NULL)
+    {
+        fLeds = new Leds;
+    }
+    ~MCaos()
+    {
+        CloseFile();
+        DeleteHistograms();
+        delete fLeds;
+    }
+
+    void AddPosition(Float_t x, Float_t y, Float_t dx, Float_t dy)
+    {
+        fPositions.Add(x, y, dx, dy);
+    }
+
+    void ReadResources(const char *name="leds.txt");
+
+    void OpenFile();
+    void CloseFile();
+
+    void InitHistograms();
+    void DeleteHistograms();
+    void ShowHistograms();
+    void ResetHistograms();
+
+    void Run(byte *img, bool printl, bool printr, const ZdAz &pos, const Timer &t);
+};
+
+#endif
Index: /trunk/MagicSoft/Cosy/main/MCosy.cc
===================================================================
--- /trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 2278)
@@ -18,4 +18,5 @@
 
 #include "MGCosy.h"
+#include "MStarguider.h"
 #include "SlaStars.h"
 
@@ -330,4 +331,6 @@
 {
     // FIXME, what when waiting times out (Zombie)
+    if (!fMac1 || !fMac2)
+        return;
 
     while ((fMac1->IsPositioning() || fMac2->IsPositioning()) &&
@@ -428,5 +431,7 @@
     const ZdAz d = dst*kRad2Deg;
 
-    lout << "Target Position: " << d.Zd() << "deg, " << d.Az() << "deg (Zd/Az)" << endl;
+    Timer t;
+    t.Now();
+    lout << t << " - Target Position: " << d.Zd() << "deg, " << d.Az() << "deg (Zd/Az)" << endl;
 
     if (!CheckRange(d))
@@ -504,5 +509,6 @@
         if (!cdzd && !cdaz)
         {
-            lout << "Positioning done in " << i << (i==1?" step.":" steps.") << endl;
+            t.Now();
+            lout << t << " - Positioning done in " << i << (i==1?" step.":" steps.") << endl;
             SetStatus(MCosy::kStopped);
             return TRUE;
@@ -577,7 +583,7 @@
         // repositioning (relative)
         //
-        lout << "Do Relative Positioning Done" << endl;
+
+        lout << "Do Relative Positioning..." << endl;
         DoRelPos(rd, cdzd, cdaz);
-
         lout << "Relative Positioning Done" << endl;
     }
@@ -588,5 +594,6 @@
         SetStatus(MCosy::kStopped);
 
-    lout << "Warning: Requested position not reached (i=" << i << ")" << endl;
+    t.Now();
+    lout << t << " - Warning: Requested position not reached (i=" << i << ")" << endl;
     return FALSE;
 }
@@ -692,5 +699,5 @@
     // is 25% of the |v|
     //
-    const Float_t limit    = 0.25;
+    //const Float_t limit    = 0.25;
 
     //
@@ -721,5 +728,5 @@
     }
     */
-
+   /*
     if (sgn(vt->Az()) != sgn(vcalc.Az())
         && fabs(vt->Az()) < limit*fabs(vcalc.Az())
@@ -729,5 +736,5 @@
             vt->Az(0);
         }
-    else
+    else*/
         if (fabs(vt->Az()) > maxtrack*vraz)
         {
@@ -735,5 +742,5 @@
             vt->Az(maxtrack*vraz*sgn(vcalc.Az()));
         }
-
+/*
     if (sgn(vt->Zd()) != sgn(vcalc.Zd())
         && fabs(vt->Zd()) < limit*fabs(vcalc.Zd())
@@ -744,5 +751,5 @@
         }
     else
-        if (fabs(vt->Zd()) > maxtrack*vrzd)
+ */       if (fabs(vt->Zd()) > maxtrack*vrzd)
         {
             lout << "Warning: Altitude speed limit (" << maxtrack*100 << "%) exceeded... limited." << endl;
@@ -880,12 +887,13 @@
     XY xy(Rad2Deg(dst.Ra())*24/360, Rad2Deg(dst.Dec()));
 
-    lout << "Start tracking:";
+    sla.Now();
+    lout << sla << " - Start tracking:";
     lout << " Ra: " << xy.X() << "h  " << "Dec: " << xy.Y() << "\xb0" << endl;
-#ifdef EXPERT
+/*#ifdef EXPERT
     ofstream fout("coordinates.txt");
     fout << xy;
     fout.close();
 #endif
-    //
+*/    //
     // Initialize Tracker (slalib or starguider)
     //
@@ -1014,7 +1022,10 @@
     }
 
+    sla.Now();
+
     fBackground = kBgdNone;
     StopMovement();
-    lout << "Tracking stopped." << endl;
+
+    lout << sla << " - Tracking stopped." << endl;
 }
 
@@ -1037,13 +1048,16 @@
     //
     cout << "Stopping movement (dec=30%)..." << endl;
+    if (fMac1 && fMac2)
+    {
 #ifdef EXPERT
-    fMac1->SetDeceleration(0.5*fMac1->GetVelRes());
-    fMac2->SetDeceleration(0.5*fMac2->GetVelRes());
+        fMac1->SetDeceleration(0.5*fMac1->GetVelRes());
+        fMac2->SetDeceleration(0.5*fMac2->GetVelRes());
 #else
-    fMac1->SetDeceleration(0.3*fMac1->GetVelRes());
-    fMac2->SetDeceleration(0.3*fMac2->GetVelRes());
+        fMac1->SetDeceleration(0.3*fMac1->GetVelRes());
+        fMac2->SetDeceleration(0.3*fMac2->GetVelRes());
 #endif
-    fMac1->SetRpmMode(FALSE);
-    fMac2->SetRpmMode(FALSE);
+        fMac1->SetRpmMode(FALSE);
+        fMac2->SetRpmMode(FALSE);
+    }
 
 /*
@@ -1201,5 +1215,4 @@
         return (void*)0xaffe;
         */
-#ifdef EXPERT
     case WM_TPOINT:
         {
@@ -1222,4 +1235,11 @@
             cout << "     SE-Pos: " << za.Alt() << "° " << za.Az() << "°" << endl;
             *tpout << fmod(za.Az()+360, 360) << " " << za.Alt() << " ";
+
+            if (fStarguider)
+            {
+                XY tp = fStarguider->GetCoordinates();
+                *tpout << 90-tp.X() << " " << tp.Y() << " ";
+            }
+
             *tpout << rd.Ra()/15 << " " << rd.Dec() << " " << setprecision(11) << sla.GetMjd() << endl;
 
@@ -1227,5 +1247,4 @@
         }
         return (void*)0xca1b;
-#endif
 
     case WM_TRACKPOS:
@@ -1283,8 +1302,9 @@
         cout << "WM_Track: START" << endl;
         {
+            RaDec dest = ((RaDec*)mp)[0];
+            if (fStarguider)
+                fStarguider->SetPointingPosition(((RaDec*)mp)[1]);
             if (!CheckNetwork())
                 return (void*)0xebb0;
-
-            RaDec dest = *((RaDec*)mp);
             TrackPosition(dest*kDeg2Rad);
         }
@@ -1352,17 +1372,18 @@
             RaDec rd(xy.X()*15., xy.Y()); // [deg]
 
-            const ZdAz a0 = sla.CalcZdAz(rd*kDeg2Rad);
-
-            ZdAz a1;
-            AlignTrackingPos(a0, a1);
-            a1 *= 180/TMath::Pi();
-
-            const ZdAz a2 = a1*16384/360;
-            const ZdAz se = a0*16384/360;
+            ZdAz a1 = sla.CalcZdAz(rd*kDeg2Rad); // [rad]
+            const ZdAz a0 = a1*kRad2Deg;               // [deg]
 
             cout << "Ra/Dec source: " << xy.X()  << "h " << xy.Y()  << "°" << endl;
             cout << "Zd/Az target:  " << a0.Zd() << "° " << a0.Az() << "°" << endl;
+
+            if (fZd1 && fZd2 && fAz)
+                AlignTrackingPos(a0, a1);
+
+            a1 = fBending(a1)*kRad2Deg;
+
+            const ZdAz a2 = a1*16384/360;
+
             cout << "Zd/Az bended:  " << a1.Zd() << "° " << a1.Az() << "°" << endl;
-            cout << "SE target:     " << se.Zd() << "  " << se.Az() << endl;
             cout << "SE bended:     " << a2.Zd() << "  " << a2.Az() << endl;
         }
@@ -1781,4 +1802,11 @@
 }
 
+ZdAz MCosy::GetPointingPos() const
+{
+    // GetPointingPos [deg]
+    const ZdAz seist = GetSePos()*2*TMath::Pi()/16384; // [se]
+    return fBending.CorrectBack(seist)*180/TMath::Pi();
+}
+
 Bool_t MCosy::HandleTimer(TTimer *t)
 {
@@ -1793,6 +1821,5 @@
         fAz->DisplayVal();
 
-    ZdAz seist = GetSePos()*2*TMath::Pi()/16384; // [se]
-    ZdAz bendist = fBending.CorrectBack(seist);
+    ZdAz bendist = GetPointingPos();
 
     Byte_t avail = 0;
@@ -1810,6 +1837,6 @@
     lout.UpdateGui();
 
-    fWin->Update(bendist*(360.0/2/TMath::Pi()), fTrackingError,
-                 fVelocity, fOffset, fRaDec, fZdAzSoll, fStatus, avail);
+    fWin->Update(bendist, fTrackingError, fVelocity, /*fOffset,*/
+                 fRaDec, fZdAzSoll, fStatus, avail);
 
     const Bool_t trigger = fTriggerDisplay;
@@ -2076,5 +2103,5 @@
 
 MCosy::MCosy(int mode, const char *dev, const int baud, MLog &out)
-: Network(dev, baud, out), fObservatory(MObservatory::kMagic1), fZd1(0), fZd2(0), fAz(0), fMac1(0), fMac2(0), fMac3(0), fBackground(kBgdNone)
+: Network(dev, baud, out), fObservatory(MObservatory::kMagic1), fStarguider(NULL), fZd1(0), fZd2(0), fAz(0), fMac1(0), fMac2(0), fMac3(0), fBackground(kBgdNone), fStatus(kStopped)
 {
     TEnv env(".cosyrc");
@@ -2109,5 +2136,5 @@
     fZd2->SetDisplay(fWin->GetLabel3());
     fAz->SetDisplay(fWin->GetLabel1());
-#ifdef EXPERT
+
     int i=0;
     char name[100];
@@ -2130,5 +2157,4 @@
     *tpout << time.Year() << " " << time.Month() << " " << time.Day() << " ";
     *tpout << /*"20 1013.25 300 0.5 0.55 0.0065" <<*/ endl;
-#endif
     // temp(°C) pressure(mB) height(m) humidity(1) wavelength(microm) troplapserate(K/m)
 }
@@ -2156,8 +2182,7 @@
 MCosy::~MCosy()
 {
-#ifdef EXPERT
     *tpout << "END" << endl;
+    //streampos size = tpout.tellp();
     delete tpout;
-#endif
 
     cout << "Deleting GUI timer." << endl;
Index: /trunk/MagicSoft/Cosy/main/MCosy.h
===================================================================
--- /trunk/MagicSoft/Cosy/main/MCosy.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/main/MCosy.h	(revision 2278)
@@ -36,4 +36,5 @@
 class MCosy;
 class TH1;
+class MStarguider;
 
 class MTTalk : public MThread
@@ -71,4 +72,6 @@
     const MObservatory::LocationName_t fObservatory;
 
+    MStarguider *fStarguider;
+
     ShaftEncoder *fZd1;
     ShaftEncoder *fZd2;
@@ -187,4 +190,7 @@
     Bool_t HandleTimer(TTimer *t);
 
+    ZdAz GetPointingPos(void) const;
+    void SetStarguider(MStarguider *s) { fStarguider = s; }
+  
     // static ZdAz CorrectTarget(const ZdAz &src, const ZdAz &dst);
     //    static ZdAz RaDec2ZdAz(const double mjd, const RaDec &pos, const RaDec &pm=RaDec(0,0));
Index: /trunk/MagicSoft/Cosy/main/MStarguider.cc
===================================================================
--- /trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 2278)
+++ /trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 2278)
@@ -0,0 +1,958 @@
+#include "MStarguider.h"
+
+#include <fstream.h>    // ifstream
+#include <iostream.h>   // cout
+#include <iomanip.h>   // cout
+
+#include <TH2F.h>
+#include <TGraph.h>
+#include <TTimer.h>
+
+#include <TGMenu.h>
+#include <TGLabel.h>
+#include <TSystem.h>
+#include <TGSplitter.h>    // TGHorizontal3DLine
+#include <TGTextEntry.h>
+
+#include "MCosy.h"
+#include "MCaos.h"
+
+#include "MGImage.h"
+#include "MGCoordinates.h"
+
+#include "coord.h"
+
+#include "Led.h"
+#include "Writer.h"
+#include "FilterLed.h"
+#include "MStarList.h"
+#include "CaosFilter.h"
+#include "StarCatalog.h"
+
+#include "MGMenu.h"
+
+ClassImp(MStarguider);
+
+enum {
+    IDM_kFilter,
+    IDM_kCaosFilter,
+    IDM_kCatalog,
+    IDM_kStarguider,
+    IDM_kStart,
+    IDM_kStop,
+    IDM_kFileType,
+    IDM_kPPM,
+    IDM_kPNG,
+    IDM_kOnce,
+    IDM_kStretch,
+//    IDM_kUseFileRaDec,
+    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_kInterpol125,
+    IDM_kInterpol50,
+    IDM_kInterpol25,
+    IDM_kInterpol10,
+    IDM_kInterpol5,
+    IDM_kInterpol2,
+    IDM_kInterpol1,
+    IDM_kCaosPrintRings,
+    IDM_kCaosPrintLeds,
+    IDM_kCaosAnalStart,
+    IDM_kCaosAnalStop,
+    IDM_kCaosWriteStart,
+    IDM_kCaosWriteStop,
+    IDM_kResetHistograms
+};
+
+Bool_t MStarguider::HandleTimer(TTimer *t)
+{
+    fImage->DoRedraw();
+    fZoomImage->DoRedraw();
+    return kTRUE;
+}
+
+#define kZOOM 96
+
+XY MStarguider::GetCoordinates() const
+{
+    return fPZdAz->GetCoordinates();
+}
+
+void MStarguider::InitGui()
+{
+    fList = new MGList;
+
+    const TGWindow *p=gClient->GetRoot();
+
+    //
+    // Create Menu for MStarguider Display
+    //
+    fDisplay = new MGPopupMenu(p);
+    fDisplay->AddEntry("&Filter",      IDM_kFilter);
+    fDisplay->AddEntry("C&aosFilter",  IDM_kCaosFilter);
+    fDisplay->AddEntry("Sao &Catalog", IDM_kCatalog);
+    fDisplay->AddEntry("Starguider",   IDM_kStarguider);
+    fDisplay->AddEntry("Stretch",      IDM_kStretch);
+    fDisplay->DisableEntry(IDM_kStarguider);
+    fDisplay->CheckEntry(IDM_kStretch);
+    fDisplay->Associate(this);
+    fList->Add(fDisplay);
+
+    fFileType = new MGPopupMenu(p);
+    fFileType->AddEntry("PP&M", IDM_kPPM);
+    fFileType->AddEntry("&PNG", IDM_kPNG);
+    fFileType->CheckEntry(IDM_kPNG);
+    fFileType->Associate(this);
+    fList->Add(fFileType);
+
+    fWriteType = new MGPopupMenu(p);
+    fWriteType->AddEntry("&Once",      IDM_kOnce);
+    fWriteType->AddEntry("&Continous", IDM_kContinous);
+    fWriteType->CheckEntry(IDM_kOnce);
+    fWriteType->Associate(this);
+    fList->Add(fWriteType);
+
+    fWriteRate = new MGPopupMenu(p);
+    fWriteRate->AddEntry("25/s", IDM_kRate25ps);
+    fWriteRate->AddEntry("5/s",  IDM_kRate5ps);
+    fWriteRate->AddEntry("1s",   IDM_kRate1s);
+    fWriteRate->AddEntry("5s",   IDM_kRate5s);
+    fWriteRate->AddEntry("30s",  IDM_kRate30s);
+    fWriteRate->AddEntry("1min", IDM_kRate1m);
+    fWriteRate->AddEntry("5min", IDM_kRate5m);
+    fWriteRate->CheckEntry(IDM_kRate1m);
+    fWriteRate->Associate(this);
+    fList->Add(fWriteRate);
+
+    fWrtRate = 25*60;
+
+    fWritePictures = new MGPopupMenu(p);
+    fWritePictures->AddEntry("&Start",      IDM_kStart);
+    fWritePictures->AddEntry("Sto&p",       IDM_kStop);
+    fWritePictures->AddSeparator();
+    fWritePictures->AddPopup("File &Type",  fFileType);
+    fWritePictures->AddPopup("&Write Type", fWriteType);
+    fWritePictures->AddPopup("Write &Rate", fWriteRate);
+    fWritePictures->DisableEntry(IDM_kStop);
+    fWritePictures->Associate(this);
+    fList->Add(fWritePictures);
+
+    fLimMag = new MGPopupMenu(p);
+    fLimMag->AddEntry("3", IDM_kLimMag3);
+    fLimMag->AddEntry("4", IDM_kLimMag4);
+    fLimMag->AddEntry("5", IDM_kLimMag5);
+    fLimMag->AddEntry("6", IDM_kLimMag6);
+    fLimMag->AddEntry("7", IDM_kLimMag7);
+    fLimMag->AddEntry("8", IDM_kLimMag8);
+    fLimMag->AddEntry("9", IDM_kLimMag9);
+    fLimMag->CheckEntry(IDM_kLimMag8);
+    fLimMag->Associate(this);
+    fList->Add(fLimMag);
+
+    fSao->SetLimitMag(7.0);
+
+    fInterpol = new MGPopupMenu(p);
+    fInterpol->AddEntry("125", IDM_kInterpol125);
+    fInterpol->AddEntry("50",  IDM_kInterpol50);
+    fInterpol->AddEntry("25",  IDM_kInterpol25);
+    fInterpol->AddEntry("10",  IDM_kInterpol10);
+    fInterpol->AddEntry("5",   IDM_kInterpol5);
+    fInterpol->AddEntry("2",   IDM_kInterpol2);
+    fInterpol->AddEntry("Off", IDM_kInterpol1);
+    fInterpol->CheckEntry(IDM_kInterpol1);
+    fInterpol->Associate(this);
+    fList->Add(fInterpol);
+
+    fIntRate = 1;
+
+    fSetup = new MGPopupMenu(p);
+    fSetup->AddPopup("Lim. &Magnitude",      fLimMag);
+    fSetup->AddPopup("Disp. &Interpolation", fInterpol);
+    //fSetup->AddEntry("Use Ra/Dec from file", IDM_kUseFileRaDec);
+    fSetup->Associate(this);
+    fList->Add(fSetup);
+
+    fCaosPrint = new MGPopupMenu(p);
+    fCaosPrint->AddEntry("&Leds",  IDM_kCaosPrintLeds);
+    fCaosPrint->AddEntry("&Rings", IDM_kCaosPrintRings);
+    fCaosPrint->Associate(this);
+    fList->Add(fCaosPrint);
+
+    fCaosWrite = new MGPopupMenu(p);
+    fCaosWrite->AddEntry("&Start", IDM_kCaosWriteStart);
+    fCaosWrite->AddEntry("Sto&p",  IDM_kCaosWriteStop);
+    fCaosWrite->DisableEntry(IDM_kCaosWriteStop);
+    fCaosWrite->Associate(this);
+    fList->Add(fCaosWrite);
+
+    fCaosAnalyse = new MGPopupMenu(p);
+    fCaosAnalyse->AddEntry("S&tart Analyse", IDM_kCaosAnalStart);
+    fCaosAnalyse->AddEntry("St&opp Analyse", IDM_kCaosAnalStop);
+    fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
+    //    fCaosAnalyse->AddEntry("&Reset Histograms", IDM_kResetHistograms);
+    //    fCaosAnalyse->AddEntry("Reset &Graph", IDM_kResetGraph);
+    fCaosAnalyse->Associate(this);
+    fList->Add(fCaosAnalyse);
+
+    fMenu = new MGMenuBar(this, 0, 0, kHorizontalFrame);
+    fMenu->AddPopup("&Display",   fDisplay,         NULL);
+    fMenu->AddPopup("&WritePics", fWritePictures,   NULL);
+    fMenu->AddPopup("&Setup",     fSetup,           NULL);
+    fMenu->Resize(fMenu->GetDefaultSize());
+    fMenu->BindKeys(this);
+    AddFrame(fMenu); 
+    fList->Add(fMenu);
+
+    fCaOs = new MGPopupMenu(p);
+    fCaOs->AddPopup("&Write",   fCaosWrite);
+    fCaOs->AddPopup("&Print",   fCaosPrint);
+    fCaOs->AddPopup("&Analyse", fCaosAnalyse);
+    fCaOs->Associate(this);
+    fCaOs->BindKeys(fMenu, this);
+    fList->Add(fCaOs);
+
+    fCRaDec = new MGCoordinates(this, kETypeRaDec);
+    fCRaDec->Move(1, fMenu->GetDefaultHeight()+584);
+    AddFrame(fCRaDec);
+    fList->Add(fCRaDec);
+
+    fCZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
+    fCZdAz->Move(240+12+10, fMenu->GetDefaultHeight()+584);
+    AddFrame(fCZdAz);
+    fList->Add(fCZdAz);
+
+    fPZdAz = new MGCoordinates(this, kETypeZdAz, kFALSE);
+    fPZdAz->Move(240+12+10, fMenu->GetDefaultHeight()+630);
+    AddFrame(fPZdAz);
+    fList->Add(fPZdAz);
+
+    fFps = new TGLabel(this, "---fps");
+    fFps->SetTextJustify(kTextRight);
+    fFps->Move(633, fMenu->GetDefaultHeight()+578);
+    AddFrame(fFps);
+    fList->Add(fFps);
+
+    fPosZoom = new TGLabel(this, "----.--d/----.--d (----, ----)");
+    fPosZoom->SetTextJustify(kTextRight);
+    fPosZoom->Move(620, fMenu->GetDefaultHeight()+678);
+    AddFrame(fPosZoom);
+    fList->Add(fPosZoom);
+
+    TGLabel *l = new TGLabel(this, "Arb.-Sky Pos");
+    l->SetTextJustify(kTextLeft);
+    l->Move(480+32, fMenu->GetDefaultHeight()+590);
+    AddFrame(l);
+    fList->Add(l);
+
+    l = new TGLabel(this, "arcsec/pix");
+    l->SetTextJustify(kTextLeft);
+    l->Move(605, fMenu->GetDefaultHeight()+619+13);
+    AddFrame(l);
+    fList->Add(l);
+
+    l = new TGLabel(this, "deg");
+    l->SetTextJustify(kTextLeft);
+    l->Move(605, fMenu->GetDefaultHeight()+619-10);
+    AddFrame(l);
+    fList->Add(l);
+
+    l = new TGLabel(this, "Pointing Pos");
+    l->SetTextJustify(kTextLeft);
+    l->Move(480+32, fMenu->GetDefaultHeight()+655);
+    AddFrame(l);
+    fList->Add(l);
+
+    const Double_t pixsize = 23.4;
+
+    fSao->SetPixSize(pixsize/3600);
+    fSao->SetRotationAngle(0);
+
+    TString txt;
+    txt += pixsize;
+
+    fPixSize = new TGTextEntry(this, txt, IDM_kPixSize);
+    fPixSize->SetAlignment(kTextCenterX);
+    fPixSize->Move(547, fMenu->GetDefaultHeight()+617+13);
+    AddFrame(fPixSize);
+    fList->Add(fPixSize);
+
+    fAngle = new TGTextEntry(this, "         0", IDM_kAngle);
+    fAngle->SetAlignment(kTextCenterX);
+    fAngle->Move(547, fMenu->GetDefaultHeight()+617-10);
+    AddFrame(fAngle);
+    fList->Add(fAngle);
+
+    // TGHorizontal3DLine *fLineSep = new TGHorizontal3DLine(this);
+    // AddFrame(fLineSep, new TGLayoutHints (kLHintsNormal | kLHintsExpandX));
+    // fList->Add(fLineSep);
+
+    //
+    // Create Image Display
+    //
+    fZoomImage = new MGImage(this, kZOOM, kZOOM);
+    fZoomImage->Move(768-kZOOM-2, 700-kZOOM-2);
+    AddFrame(fZoomImage);
+    fList->Add(fZoomImage);
+
+    fImage = new MGImage(this, 768, 576);
+    fImage->Move(0, fMenu->GetDefaultHeight());
+    AddFrame(fImage);
+    fList->Add(fImage);
+
+    //
+    // Make everything visible
+    //
+    SetWindowName("MStarguider Main Window");
+    SetIconName("MStarguider");
+
+    MapSubwindows();
+    MapWindow();
+
+    //------------------------------------------------------------
+    //    XY xy(3.819444, 24.05333);
+    //    fCRaDec->SetCoordinates(xy);
+    //    fRaDec->Set(xy.X()*360/24, xy.Y());
+    //------------------------------------------------------------
+}
+
+MStarguider::MStarguider(MObservatory::LocationName_t obs)
+: TGMainFrame(gClient->GetRoot(), 768, 740), fCosy(NULL), fDx((768-kZOOM)/2), fDy((512-kZOOM)/2)
+{
+    cout << " #### FIXME: Make MCaos Thread safe!" << endl;
+
+    fSao = new StarCatalog(obs);
+    fRaDec = new RaDec(180, 40);
+
+    fCaos = new MCaos;
+    fCaos->ReadResources();
+
+    InitGui();
+
+    gVirtualX->GrabButton(fId, kButton2, 0, 0, 0, 0, kTRUE);
+
+    fTimer=new TTimer(this, 1000/25); // 100ms
+    fTimer->TurnOn();
+
+    fTime.Now();
+}
+
+MStarguider::~MStarguider()
+{
+    gVirtualX->GrabButton(fId, kButton2, 0, 0, 0, 0, kFALSE);
+
+    fTimer->TurnOff();
+    delete fTimer;
+
+    delete fList;
+
+    delete fCaos;
+    delete fSao;
+    delete fRaDec;
+
+    cout << "Camera Display destroyed." << endl;
+}
+
+void MStarguider::Layout()
+{
+    // Resize(GetDefaultSize());
+}
+
+void MStarguider::CloseWindow()
+{
+    cout << "EventDisplay::CloseWindow: Exit Application Loop." << endl;
+
+    //fClient.ExitLoop();
+    //    cout << "FIXME: ExitLoop not called!!!!!!" << endl;
+    gSystem->ExitLoop();
+}
+
+void MStarguider::Toggle(MGPopupMenu *p, UInt_t id)
+{
+    if (p->IsEntryChecked(id))
+        p->UnCheckEntry(id);
+    else
+        p->CheckEntry(id);
+}
+
+Bool_t MStarguider::ProcessMessage(Long_t msg, Long_t mp1, Long_t mp2)
+{
+    switch (GET_MSG(msg))
+    {
+    case kC_TEXTENTRY:
+        if (GET_SUBMSG(msg)==kTE_ENTER)
+            switch (mp1)
+            {
+            case IDM_kPixSize:
+                {
+                    const Float_t pixsize = atof(fPixSize->GetText());
+                    cout << "Pixel Size changed to " << pixsize << "\"/pix" << endl;
+                    fSao->SetPixSize(pixsize/3600);
+                    return kTRUE;
+                }
+            case IDM_kAngle:
+                {
+                    const Float_t angle = atof(fAngle->GetText());
+                    cout << "Rotation Angle changed to " << angle << "deg" << endl;
+                    fSao->SetRotationAngle(angle);
+                    return kTRUE;
+                }
+            }
+        return kTRUE;
+
+    case kC_COMMAND:
+        switch (GET_SUBMSG(msg))
+        {
+        case kCM_MENU:
+            switch (mp1)
+            {
+            case IDM_kCatalog:
+                Toggle(fDisplay, IDM_kCatalog);
+                if (fDisplay->IsEntryChecked(IDM_kCatalog))
+                    fDisplay->EnableEntry(IDM_kStarguider);
+                else
+                {
+                    fDisplay->UnCheckEntry(IDM_kStarguider);
+                    fDisplay->DisableEntry(IDM_kStarguider);
+                }
+                return kTRUE;
+
+            case IDM_kStarguider:
+                Toggle(fDisplay, IDM_kStarguider);
+                gSystem->Unlink("tracking_error.txt");
+                return kTRUE;
+
+            case IDM_kFilter:
+                Toggle(fDisplay, IDM_kFilter);
+                if (fDisplay->IsEntryChecked(IDM_kFilter))
+                    fDisplay->EnableEntry(IDM_kStarguider);
+                else
+                {
+                    fDisplay->UnCheckEntry(IDM_kStarguider);
+                    fDisplay->DisableEntry(IDM_kStarguider);
+                }
+                return kTRUE;
+
+            case IDM_kStretch:
+                Toggle(fDisplay, IDM_kStretch);
+                return kTRUE;
+
+            case IDM_kCaosFilter:
+                //if (!fDisplay->IsEntryChecked(IDM_kCaosFilter))
+                //    fCaos->OpenFile();
+                Toggle(fDisplay, IDM_kCaosFilter);
+                if (fDisplay->IsEntryChecked(IDM_kCaosFilter))
+                    fMenu->AddPopup("&CaOs", fCaOs, NULL);
+                else
+                {
+                    if(fCaosWrite->IsEntryChecked(IDM_kCaosPrintLeds))
+                        fCaosWrite->UnCheckEntry(IDM_kCaosPrintLeds);
+                    if(fCaosWrite->IsEntryChecked(IDM_kCaosPrintRings))
+                        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");
+                }
+                fMenu->Resize(fMenu->GetDefaultSize());
+                MapSubwindows();
+                MapWindow();
+                return kTRUE;
+
+            case IDM_kCaosPrintLeds:
+            case IDM_kCaosPrintRings:
+                Toggle(fCaosPrint, mp1);
+                return kTRUE;
+
+            case IDM_kCaosAnalStart:
+                fCaosAnalyse->DisableEntry(IDM_kCaosAnalStart);
+                fCaosAnalyse->EnableEntry(IDM_kCaosAnalStop);
+                fCaos->InitHistograms();
+                return kTRUE;
+
+            case IDM_kCaosAnalStop:
+                fCaosAnalyse->DisableEntry(IDM_kCaosAnalStop);
+                fCaosAnalyse->EnableEntry(IDM_kCaosAnalStart);
+                fCaos->ShowHistograms();
+                fCaos->DeleteHistograms();
+                return kTRUE;
+
+            case IDM_kCaosWriteStart:
+                fCaosWrite->DisableEntry(IDM_kCaosWriteStart);
+                fCaosWrite->EnableEntry(IDM_kCaosWriteStop);
+                fCaos->OpenFile();
+                return kTRUE;
+
+            case IDM_kCaosWriteStop:
+                fCaosWrite->DisableEntry(IDM_kCaosWriteStop);
+                fCaosWrite->EnableEntry(IDM_kCaosWriteStart);
+                fCaos->CloseFile();
+                return kTRUE;
+
+            case IDM_kStart:
+                fWritePictures->DisableEntry(IDM_kStart);
+                fWritePictures->EnableEntry(IDM_kStop);
+                return kTRUE;
+
+            case IDM_kStop:
+                fWritePictures->DisableEntry(IDM_kStop);
+                fWritePictures->EnableEntry(IDM_kStart);
+                return kTRUE;
+
+            case IDM_kPNG:
+                fFileType->CheckEntry(IDM_kPNG);
+                fFileType->UnCheckEntry(IDM_kPPM);
+                return kTRUE;
+
+            case IDM_kPPM:
+                fFileType->CheckEntry(IDM_kPPM);
+                fFileType->UnCheckEntry(IDM_kPNG);
+                return kTRUE;
+
+            case IDM_kOnce:
+                fWriteType->CheckEntry(IDM_kOnce);
+                fWriteType->UnCheckEntry(IDM_kContinous);
+                return kTRUE;
+
+            case IDM_kContinous:
+                fWriteType->CheckEntry(IDM_kContinous);
+                fWriteType->UnCheckEntry(IDM_kOnce);
+                return kTRUE;
+
+            case IDM_kRate25ps:
+            case IDM_kRate5ps:
+            case IDM_kRate1s:
+            case IDM_kRate5s:
+            case IDM_kRate30s:
+            case IDM_kRate1m:
+            case IDM_kRate5m:
+                for (int i=IDM_kRate25ps; i<=IDM_kRate5m; i++)
+                    if (mp1==i)
+                        fWriteRate->CheckEntry(i);
+                    else
+                        fWriteRate->UnCheckEntry(i);
+                switch (mp1)
+                {
+                case IDM_kRate25ps:
+                    fWrtRate = 1;
+                    return kTRUE;
+                case IDM_kRate5ps:
+                    fWrtRate = 5;
+                    return kTRUE;
+                case IDM_kRate1s:
+                    fWrtRate = 25;
+                    return kTRUE;
+                case IDM_kRate5s:
+                    fWrtRate = 5*25;
+                    return kTRUE;
+                case IDM_kRate30s:
+                    fWrtRate = 30*25;
+                    return kTRUE;
+                case IDM_kRate1m:
+                    fWrtRate = 60*25;
+                    return kTRUE;
+                case IDM_kRate5m:
+                    fWrtRate = 5*60*25;
+                    return kTRUE;
+                }
+                return kTRUE;
+
+            case IDM_kInterpol125:
+            case IDM_kInterpol50:
+            case IDM_kInterpol25:
+            case IDM_kInterpol10:
+            case IDM_kInterpol5:
+            case IDM_kInterpol2:
+            case IDM_kInterpol1:
+                for (int i=IDM_kInterpol125; i<=IDM_kInterpol1; i++)
+                    if (mp1==i)
+                        fInterpol->CheckEntry(i);
+                    else
+                        fInterpol->UnCheckEntry(i);
+                switch (mp1)
+                {
+                case IDM_kInterpol1:
+                    fIntRate = 1;
+                    return kTRUE;
+                case IDM_kInterpol2:
+                    fIntRate = 2;
+                    return kTRUE;
+                case IDM_kInterpol5:
+                    fIntRate = 5;
+                    return kTRUE;
+                case IDM_kInterpol10:
+                    fIntRate = 10;
+                    return kTRUE;
+                case IDM_kInterpol25:
+                    fIntRate = 25;
+                    return kTRUE;
+                case IDM_kInterpol50:
+                    fIntRate = 50;
+                    return kTRUE;
+                case IDM_kInterpol125:
+                    fIntRate = 125;
+                    return kTRUE;
+                }
+                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;
+        }
+        break;
+    }
+
+    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) const
+{
+    //
+    // Viewable area (FIXME: AZ)
+    //
+    TH2F h("Hist", "dX/dY", 77, -768/2-.5, 768/2+.5, 58, -576/2-.5, 576/2+.5); // 3
+
+    /*
+    TH1F hmag("HistMag", "Mag", 19, 0, 100);
+    for (int i=0; i<mag.GetSize(); i++)
+        hmag.Fill(mag[i]);
+        */
+
+    //
+    // 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]);
+    }
+
+    //
+    // Serach for an excess in the histogram
+    //
+    Int_t mx, my, dummy;
+    h.GetMaximumBin(mx, my, dummy);
+
+    const double xmax = h.GetXaxis()->GetBinCenter(mx);
+    const double dx   = h.GetXaxis()->GetBinWidth(mx);
+
+    const double ymax = h.GetYaxis()->GetBinCenter(my);
+    const double dy   = h.GetYaxis()->GetBinWidth(my);
+
+    cout << setprecision(3);
+    cout << "Cut-XY:       " << xmax << " +- " << dx << " / " << ymax << " +- " << dy << endl;
+
+    TGraph g;
+    for (int i=0; i<mag.GetSize(); i++)
+    {
+        if (!(x[i]>xmax-dx && x[i]<xmax+dx &&
+              y[i]>ymax-dy && y[i]<ymax+dy &&
+              mag[i]>48-15 && mag[i]<48+15))
+            continue;
+
+        g.SetPoint(g.GetN(), x[i], y[i]);
+    }
+
+    cout << "Offset-XY:    " << g.GetMean(1) << " +- " << g.GetRMS(1) << " / ";
+    cout << g.GetMean(2) << " +- " << g.GetRMS(2) << endl;
+
+    AltAz pos0 = fSao->CalcAltAzFromPix(768/2,              576/2)*kRad2Deg;
+    AltAz pos1 = fSao->CalcAltAzFromPix(768/2+g.GetMean(1), 576/2+g.GetMean(2))*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());
+}
+
+void MStarguider::CalcTrackingError(Leds &leds, MStarList &stars)
+{
+    const Int_t max = leds.GetEntries();
+
+    if (stars.GetRealEntries() < 3)
+    {
+        cout << "Sorry, less than 3 stars in FOV!" << endl;
+        return;
+    }
+
+    if (max < 1)
+    {
+        cout << "Sorry, less than 1 detected spot in FOV!" << endl;
+        return;
+    }
+
+    stars.Sort(); // Sort by magnitude
+
+    TString str = "data/tracking_";
+    str += fSao->GetMjd()-52000;
+    str += ".txt";
+
+    ofstream fout(str);
+
+    TArrayF x, y, mag;
+
+    Int_t num = 0;
+
+    // FIXME: Is predifined value 3 a good idea?
+
+    MStar *star;
+    MStarListIter NextStar(&stars);
+    while ((star=NextStar()) && num++<max+3)
+    {
+        TIter NextSp(&leds);
+        Led *spot=NULL;
+        while ((spot=(Led*)NextSp()))
+        {
+            const XY dpos(spot->GetX()-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(spot->GetMag()/star->GetMag(), idx);
+
+            if (fout)
+                fout << x[idx] << " " << y[idx] << " " << mag[idx] << endl;
+        }
+    }
+
+    ZdAz d = TrackingError(x, y, mag);
+
+    //
+    // Calculated offsets
+    //
+
+    // 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;
+
+    //
+    // Current Pointing position
+    //
+    ZdAz cpos = fSao->GetZdAz()-d;
+    fPZdAz->SetCoordinates(cpos);
+}
+
+bool MStarguider::Interpolate(const unsigned long n, byte *img) const
+{
+    if (fIntRate<=1)
+        return true;
+
+    static unsigned short myimg[768*576];
+
+    unsigned short *f = myimg;
+    byte *i = img;
+    byte *e = img+768*576;
+
+    while (i<e)
+        *f++ += *i++;
+
+    if (n%fIntRate)
+        return false;
+
+    f = myimg;
+    i = img;
+    e = img+768*576;
+
+    while (i<e)
+        *i++ = (byte)(*f++/fIntRate);
+
+    memset(myimg, 0, sizeof(myimg));
+
+    return true;
+}
+
+void MStarguider::ProcessFrame(const unsigned long n, byte *img, struct timeval *tm)
+{
+    static unsigned long n0 = n;
+
+    Timer t(tm);
+
+    const Double_t d = t-fTime;
+    if (d>1)
+    {
+        fFps->SetText(Form("%dfps", (int)((n-n0)/d+.5)));
+        fTime = t;
+        n0 = n;
+    }
+
+    if (!Interpolate(n, img))
+        return;
+
+    FilterLed f(img, 768, 576, 2.5); // 2.5
+
+    if (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);
+
+        if (fFileType->IsEntryChecked(IDM_kPPM))
+            Writer::Ppm("pix/file", img, tm);
+
+        if (fWriteType->IsEntryChecked(IDM_kOnce))
+            ProcessMessage(MK_MSG(kC_COMMAND, kCM_MENU), IDM_kStop, 0);
+    }
+
+    MStarList spots;
+    if (fDisplay->IsEntryChecked(IDM_kFilter))
+        f.Execute();
+
+    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();
+
+        fCaos->Run(img, printl, printr, pos, t);
+    }
+
+    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 (fDisplay->IsEntryChecked(IDM_kCatalog))
+    {
+        Timer time(tm);
+
+        XY xy = fCRaDec->GetCoordinates();
+        fRaDec->Set(xy.X()*360/24, xy.Y());
+
+        UpdatePosZoom();
+
+        MStarList stars;
+        fSao->GetStars(stars, time.GetMjd(), *fRaDec);
+
+        if (fDisplay->IsEntryChecked(IDM_kStarguider))
+        {
+            Leds leds;
+            f.Execute(leds);
+
+            cout << "Found: " << leds.GetEntries() << " stars." << endl;
+
+            CalcTrackingError(leds, stars);
+        }
+
+        byte cimg[768*576];
+        fSao->GetImg(img, cimg, stars);
+
+        const float r = 60*60/fSao->GetPixSize()+1;
+        f.DrawCircle(0.5*r);
+        f.DrawCircle(1.0*r);
+        f.DrawCircle(1.5*r);
+
+        fCZdAz->SetCoordinates(fSao->GetZdAz());
+
+        fImage->DrawColImg(img, cimg);
+    }
+    else
+        fImage->DrawImg(img);
+}
+
+void MStarguider::UpdatePosZoom()
+{
+    if (fDisplay->IsEntryChecked(IDM_kCatalog))
+    {
+        fSao->Now();
+        AltAz aa = fSao->CalcAltAzFromPix(fDx, fDy)*kRad2Deg;
+
+        if (aa.Az()<0)
+            aa.Az(aa.Az()+360);
+        fPosZoom->SetText(Form("%.1fd/%.1fd (%d, %d)",
+                               -aa.Alt(), aa.Az()-180, fDx, fDy));
+    }
+    else
+        fPosZoom->SetText(Form("(%d, %d)", fDx, fDy));
+}
+
+Bool_t MStarguider::HandleDoubleClick(Event_t *event)
+{
+    const Int_t w = fImage->GetWidth();
+    const Int_t h = fImage->GetHeight();
+    const Int_t x = fImage->GetX();
+    const Int_t y = fImage->GetY();
+
+    if (!(event->fX>x && event->fX<x+w && event->fY>y && event->fY<y+h))
+        return kTRUE;
+
+    Int_t dx = event->fX-x;
+    Int_t dy = event->fY-y;
+
+    if (dx<kZOOM/4) dx=kZOOM/4;
+    if (dy<kZOOM/4) dy=kZOOM/4;
+    if (dx>766-kZOOM/4) dx=766-kZOOM/4;
+    if (dy>574-kZOOM/4) dy=574-kZOOM/4;
+
+    fDx = dx;
+    fDy = dy;
+
+    UpdatePosZoom();
+    return kTRUE;
+}
Index: /trunk/MagicSoft/Cosy/main/MStarguider.h
===================================================================
--- /trunk/MagicSoft/Cosy/main/MStarguider.h	(revision 2278)
+++ /trunk/MagicSoft/Cosy/main/MStarguider.h	(revision 2278)
@@ -0,0 +1,131 @@
+#ifndef COSY_MStarguider
+#define COSY_MStarguider
+
+#ifndef ROOT_TGFrame
+#include <TGFrame.h>
+#endif
+#include "PixClient.h"
+#ifndef MARS_MObservatory
+#include "MObservatory.h"
+#endif
+
+#include "MGList.h"
+#include "MGImage.h"
+
+#include "coord.h"
+#include "timer.h"
+
+class TArrayF;
+class TGLabel;
+
+class TTimer;
+
+class MGMenuBar;
+class MGPopupMenu;
+class TGTextEntry;
+
+class MGImage;
+class MGCoordinates;
+
+class StarCatalog;
+class MStarList;
+class MCosy;
+class MCaos;
+
+class Leds;
+
+class MStarguider : public PixClient, public TGMainFrame
+{
+private:
+    MGList        *fList;
+
+    MGMenuBar     *fMenu;
+    MGImage       *fImage;
+    MGImage       *fZoomImage;
+
+    MGPopupMenu   *fDisplay;
+    MGPopupMenu   *fWritePictures;
+    MGPopupMenu   *fFileType;
+    MGPopupMenu   *fWriteType;
+    MGPopupMenu   *fWriteRate;
+    MGPopupMenu   *fInterpol;
+    MGPopupMenu   *fSetup;
+    MGPopupMenu   *fLimMag;
+
+    MGPopupMenu   *fCaosWrite;
+    MGPopupMenu   *fCaosPrint;
+    MGPopupMenu   *fCaosAnalyse;
+    MGPopupMenu   *fCaOs;
+
+    MGCoordinates *fCRaDec;
+    MGCoordinates *fCZdAz;
+
+    MGCoordinates *fPZdAz;
+
+    TGTextEntry   *fPixSize;
+    TGTextEntry   *fAngle;
+
+    TGLabel       *fFps;
+    TGLabel       *fPosZoom;
+
+    StarCatalog   *fSao;
+    MCosy         *fCosy;
+    MCaos         *fCaos;
+
+    RaDec *fRaDec;
+
+    TTimer *fTimer;
+    Timer   fTime;
+
+    Int_t fDx;
+    Int_t fDy;
+
+    byte fIntRate;
+    int  fWrtRate;
+
+    void SetPixSize(const double pixsize);
+    void Toggle(MGPopupMenu *p, UInt_t id);
+    //void GetCoordinates();
+    void CalcTrackingError(Leds &, MStarList &);
+    ZdAz TrackingError(TArrayF &alt, TArrayF &az, TArrayF &mag) const;
+    bool Interpolate(const unsigned long n, byte *img) const;
+
+    //void InitHists();
+    //void InitGraphs();
+    void InitGui();
+
+    //void OpenFile();
+
+    //void ResetHists();
+    //void DisplayAnalysis();
+    void UpdatePosZoom();
+
+    Bool_t HandleTimer(TTimer *t);
+    //Bool_t HandleKey(Event_t* event);
+
+public:
+    MStarguider(MObservatory::LocationName_t obs);
+    virtual ~MStarguider();
+
+    //void Update();
+
+    void Layout();
+    void CloseWindow();
+
+    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;
+
+    //
+    // Execution of one frame - this function may be overloaded!
+    //
+    void ProcessFrame(const unsigned long n, byte *img, struct timeval *tm);
+
+    ClassDef(MStarguider, 0)
+};
+
+#endif
Index: /trunk/MagicSoft/Cosy/main/MainLinkDef.h
===================================================================
--- /trunk/MagicSoft/Cosy/main/MainLinkDef.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/main/MainLinkDef.h	(revision 2278)
@@ -6,4 +6,5 @@
 
 #pragma link C++ class MCosy;
+#pragma link C++ class MStarguider+;
 
 #pragma link C++ class MBending;
Index: /trunk/MagicSoft/Cosy/main/Makefile
===================================================================
--- /trunk/MagicSoft/Cosy/main/Makefile	(revision 2277)
+++ /trunk/MagicSoft/Cosy/main/Makefile	(revision 2278)
@@ -21,5 +21,5 @@
 
 INCLUDES = -I. -I../base -I.. -I../gui -I../catalog -I../devdrv \
-	   -I../candrv -I../incl -I../videodev
+	   -I../candrv -I../incl -I../videodev -I../caos
 
 # @code 
@@ -32,6 +32,8 @@
 .SUFFIXES: .c .cc .cxx .h .hxx .o 
 
-SRCFILES = MCosy.cc \
-           MBending.cc
+SRCFILES = MBending.cc \
+           MStarguider.cc \
+           MCaos.cc \
+	   MCosy.cc
 
 SRCS        = $(SRCFILES)
Index: /trunk/MagicSoft/Cosy/starg.cc
===================================================================
--- /trunk/MagicSoft/Cosy/starg.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/starg.cc	(revision 2278)
@@ -6,5 +6,5 @@
 #include "Camera.h"
 #include "PngReader.h"
-#include "MGStarguider.h"
+#include "MStarguider.h"
 
 /* ---------------------------------------------------------------------- */
@@ -27,29 +27,24 @@
     }
 
-    PixClient *client=new MGStarguider(MObservatory::kMagic1);
+    PixClient *client=new MStarguider(MObservatory::kMagic1);
+    PixGetter *get;
 
     if (dummy)
     {
         cout << " --> Starting in dummy-mode. <--" << endl;
-        PngReader *read=new PngReader(*client);
-        app->Run(kTRUE);
-        read->ExitLoop();
-        delete read;
-        //        delete client;
+        get=new PngReader(*client);
     }
     else
     {
         cout << " --> Starting in real-mode. <--" << endl;
-        //PixClient *client=new MGStarguider(MObservatory::kMagic1);
-        Camera *cam = new Camera(*client);
-        cam->Loop(0);
-        app->Run(kTRUE);
-        cam->ExitLoop();
-        delete cam;
-        //        delete client;
+        get = new Camera(*client);
+        ((Camera*)get)->Loop(0);
     }
 
+    app->Run(kTRUE);
+    get->ExitLoop();
+
+    delete get;
     delete client;
-
 
     cout << "Application end...." << endl;
Index: /trunk/MagicSoft/Cosy/tpoint/tpointfit.C
===================================================================
--- /trunk/MagicSoft/Cosy/tpoint/tpointfit.C	(revision 2277)
+++ /trunk/MagicSoft/Cosy/tpoint/tpointfit.C	(revision 2278)
@@ -105,4 +105,9 @@
     fin >> v[3];
 
+    Double_t dummy;
+    fin>>dummy;
+    fin>>dummy;
+    fin>>dummy;
+
     set.fStarAz = v[0]*TMath::Pi()/180;
     set.fStarEl = v[1]*TMath::Pi()/180;
@@ -311,5 +316,5 @@
     gzd.SetTitle(" \\Delta Zd vs. Zd ");
 
-    ifstream fin("/home/tbretz/pc4/results/tpoint3.txt");
+    ifstream fin("tpoint.txt");
 
     while (fin && fin.get()!='\n');
Index: /trunk/MagicSoft/Cosy/videodev/Camera.cc
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/Camera.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/videodev/Camera.cc	(revision 2278)
@@ -47,4 +47,5 @@
 void Camera::SigInit()
 {
+    return;
     struct sigaction act, old;
 
@@ -215,8 +216,6 @@
     while (img < end)
     {
-        *beg = *img;
-
+        *beg++ = *img;
         img += depth;
-        beg++;
     }
 }
Index: /trunk/MagicSoft/Cosy/videodev/Camera.h
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/Camera.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/videodev/Camera.h	(revision 2278)
@@ -14,7 +14,8 @@
 #endif
 
+#include "PixGetter.h"
 #include "PixClient.h"
 
-class Camera
+class Camera : public PixGetter
 {
 private:
Index: /trunk/MagicSoft/Cosy/videodev/CaosFilter.cc
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/CaosFilter.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/videodev/CaosFilter.cc	(revision 2278)
@@ -28,31 +28,37 @@
 }
 
-float CaosFilter::Mean(const byte *buffer, const int offset, int *min, int *max)
-{
-    double mean = 0.0;
-
-    *min = 0xff;
-    *max = 0x00;
+void CaosFilter::GetStat(const byte *buffer, const int offset, double *mean, double *sdev)
+{
+    double sum = 0;
+    double sq  = 0;
+
+    byte *s  = (byte*)buffer;
+    const byte *e0 = s+768*576;
 
     //
     // calculate mean value
     //
-    for (int x=offset; x<768-offset; x++)
-        for (int y=offset; y<576-offset; y++)
-        {
-            byte val = buffer[y*768+x];
-
-            mean += val;
-
-            if (val>*max)
-                *max = val;
-
-            if (val<*min)
-                *min = val;
-        }
-
-    mean /= (768-2*offset)*(576-2*offset);
-
-    return mean;
+    while (s<e0)
+    {
+        const byte *e = s+576-offset;
+        s += offset;
+
+        while (s<e)
+        {
+            sum += *s;
+            sq  += *s * *s;
+            s++;
+        }
+
+        s+=offset;
+    }
+
+    const Int_t sz = (768-2*offset)*(576-2*offset);
+
+    sum /= sz;
+    sq  /= sz;
+
+    *mean = sum;
+    *sdev = sqrt(sq-sum*sum);
 }
 
@@ -80,25 +86,4 @@
     return (int)my*768 + (int)mx;
 }
-
-float CaosFilter::SDev(const byte *buffer, const int offset, const double mean)
-{
-    //
-    // calculate sigma
-    //
-    double sdev=0.0;
-
-    for (int x=offset; x<768-offset; x++)
-        for (int y=offset; y<576-offset; y++)
-        {
-            const float val = mean - buffer[y*768+x];
-
-            sdev += val*val;
-        }
-
-    sdev /= (768-2*offset)*(576-2*offset)-1;
-
-    return sqrt(sdev);
-}
-
 
 int CaosFilter::GetMeanPosition(const byte *bitmap, const int x, const int y,
@@ -389,27 +374,20 @@
     const int offset = 10;
 
-    int max;
-    int min;
-
-    const float mean = Mean(img, offset, &min, &max);
-    const float sdev = SDev(img, offset, mean);
-
-    const float cut = mean + 2.5*sdev;
+    double mean, sdev;
+    GetStat(img, offset, &mean, &sdev);
+
+    const byte max = mean+2.5*sdev>254 ? 254 : (byte)(mean+2.5*sdev);
 
     //
     // clean image from noise
     //
-    for (int x=0; x<768; x++)
-        for (int y=0; y<576; y++)
-        {
-            if (img[y*768+x]>cut)
-                continue;
-
-            //
-            // FIXME: Create LOOKUP Table!
-            //
-
-            img[y*768+x] = 0;
-        }
+    const byte *e = img+768*576;
+    byte *i = img;
+    while (i<e)
+    {
+        if (*i<=max)
+            *i = 0;
+        i++;
+    }
 
     //
@@ -511,27 +489,20 @@
     const int offset = 10;
 
-    int max;
-    int min;
-
-    const float mean = Mean(img, offset, &min, &max);
-    const float sdev = SDev(img, offset, mean);
-
-    const float cut = mean + 2.5*sdev;
+    double mean, sdev;
+    GetStat(img, offset, &mean, &sdev);
+
+    const byte cut = mean+2.5*sdev>254 ? 254 : (byte)(mean + 2.5*sdev);
 
     //
     // clean image from noise
     //
-    for (int x=0; x<768; x++)
-        for (int y=0; y<576; y++)
-        {
-            if (img[y*768+x]>cut)
-                continue;
-
-            //
-            // FIXME: Create LOOKUP Table!
-            //
-
-            img[y*768+x] = 0;
-        }
+    const byte *e = img+768*576;
+    byte *i = img;
+    while (i<e)
+    {
+        if (*i<=cut)
+            *i = 0;
+        i++;
+    }
 
     //
Index: /trunk/MagicSoft/Cosy/videodev/CaosFilter.h
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/CaosFilter.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/videodev/CaosFilter.h	(revision 2278)
@@ -19,9 +19,6 @@
                            byte *buffer, const int col);
 
-    static float Mean(const byte *buffer, const int offset,
-                      int *min, int *max);
-
-    static float SDev(const byte *buffer, const int offset,
-                      const double mean);
+    static void  GetStat(const byte *buffer, const int offset,
+                         double *mean, double *sdev);
 
     static int   GetMeanPosition(const byte *bitmap,
Index: /trunk/MagicSoft/Cosy/videodev/Filter.cc
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/Filter.cc	(revision 2277)
+++ /trunk/MagicSoft/Cosy/videodev/Filter.cc	(revision 2278)
@@ -24,32 +24,28 @@
 }
 
-float Filter::Mean(const byte *buffer, const int offset, int *min, int *max)
+float Filter::Mean(const byte *buffer, const int offset)
 {
     double mean = 0.0;
 
-    *min = 0xff;
-    *max = 0x00;
+    byte *s  = (byte*)buffer;
+    const byte *e0 = s+768*576;
 
     //
     // calculate mean value
     //
-    for (int x=offset; x<768-offset; x++)
-        for (int y=offset; y<576-offset; y++)
-        {
-            byte val = buffer[y*768+x];
+    while (s<e0)
+    {
+        const byte *e = s+576-offset;
+        s += offset;
 
-            mean += val;
+        while (s<e)
+            mean += *s++;
 
-            if (val>*max)
-                *max = val;
+        s+=offset;
+    }
 
-            if (val<*min)
-                *min = val;
-        }
+    return mean / ((768-2*offset)*(576-2*offset));
+}
 
-    mean /= (768-2*offset)*(576-2*offset);
-
-    return mean;
-}
 
 float Filter::SDev(const byte *buffer, const int offset, const double mean)
@@ -101,27 +97,20 @@
     const int offset = 10;
 
-    int max;
-    int min;
-
-    const float mean = Mean(img, offset, &min, &max);
+    const float mean = Mean(img, offset);
     const float sdev = SDev(img, offset, mean);
 
-    const float cut = mean + 2.5*sdev;
+    const byte cut = mean+2.5*sdev>254 ? 254 : (byte)(mean + 2.5*sdev);
 
     //
     // clean image from noise
     //
-    for (int x=0; x<768; x++)
-        for (int y=0; y<576; y++)
-        {
-            if (img[y*768+x]>cut)
-                continue;
-
-            //
-            // FIXME: Create LOOKUP Table!
-            //
-
-            img[y*768+x] = 0;
-        }
+    const byte *e = img+768*576;
+    byte *i = img;
+    while (i<e)
+    {
+        if (*i<=cut)
+            *i = 0;
+        i++;
+    }
 
     //
@@ -203,32 +192,2 @@
 }
 
-void Filter::Stretch(byte *img)
-{
-    const int offset = 25;
-
-    int max;
-    int min;
-
-    /*const float mean =*/Mean(img, offset, &min, &max);
-
-    const byte diff = max-min;
-
-    for (int x=0; x<768; x++)
-        for (int y=0; y<576; y++)
-        {
-            byte &b = img[y*768+x];
-
-            if (b<min)
-            {
-                b=0;
-                continue;
-            }
-            if (b>max)
-            {
-                b=max;
-                continue;
-            }
-            b -= min;
-            b *= 255/diff;
-        }
-}
Index: /trunk/MagicSoft/Cosy/videodev/Filter.h
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/Filter.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/videodev/Filter.h	(revision 2278)
@@ -17,6 +17,5 @@
                            byte *buffer, const int col);
 
-    static float Mean(const byte *buffer, const int offset,
-                      int *min, int *max);
+    static float Mean(const byte *buffer, const int offset);
 
     static float SDev(const byte *buffer, const int offset,
@@ -29,5 +28,4 @@
 public:
     static void Execute(byte *img);
-    static void Stretch(byte *img);
 
     ClassDef(Filter, 0)
Index: /trunk/MagicSoft/Cosy/videodev/FilterLed.cc
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 2278)
+++ /trunk/MagicSoft/Cosy/videodev/FilterLed.cc	(revision 2278)
@@ -0,0 +1,387 @@
+#include "FilterLed.h"
+
+#include <memory.h>   // memset
+#include <iostream.h> // cout
+
+#include "Led.h"
+#include "Leds.h"
+#include "Ring.h"
+
+ClassImp(FilterLed);
+
+void FilterLed::DrawCircle(float cx, float cy, float rpix, byte col) const
+{
+    if (cx-rpix<1 || cy-rpix<1 || cx+rpix>fW-2 || cy+rpix>fH-2)
+        return;
+
+    for (int dx=-(int)(rpix*0.7); dx<(int)(rpix*0.7); dx++)
+    {
+        const int dy = (int)sqrt(rpix*rpix-dx*dx);
+        fImg[(int)(cx+dx) + (int)(cy-dy)*fW] = col;
+        fImg[(int)(cx+dx) + (int)(cy+dy)*fW] = col;
+        fImg[(int)(cx-dy) + (int)(cy+dx)*fW] = col;
+        fImg[(int)(cx+dy) + (int)(cy+dx)*fW] = col;
+    }
+}
+
+void FilterLed::DrawCircle(const Ring &l, byte col=0x40) const
+{
+    DrawCircle(l.GetX(), l.GetY(), l.GetR(), col);
+}
+
+void FilterLed::DrawCircle(const Ring &l, double r, byte col=0x40) const
+{
+    DrawCircle(l.GetX(), l.GetY(), r, col);
+}
+
+void FilterLed::DrawBox(const int x1, const int y1,
+                        const int x2, const int y2,
+                        const int col) const
+{
+    for (int x=x1; x<x2+1; x++)
+        for (int y=y1; y<y2+1; y++)
+            if (x>=0 && x<fW-1 && y>=0 && y<fH-1)
+                fImg[y*fW+x] = col;
+}
+
+void FilterLed::MarkPoint(const Led &led) const
+{
+    const int x = (int)(led.GetX()+.5);
+    const int y = (int)(led.GetY()+.5);
+    const int m = (int)(led.GetMag());
+
+    MarkPoint(x, y, m);
+}
+
+void FilterLed::MarkPoint(Float_t px, Float_t py, Float_t mag) const
+{
+    const int x = (int)(px+.5);
+    const int y = (int)(py+.5);
+    const int m = (int)(mag);
+
+    DrawBox(x-8, y, x-5, y, m);
+    DrawBox(x, y+5, x, y+8, m);
+    DrawBox(x+5, y, x+8, y, m);
+    DrawBox(x, y-8, x, y-5, m);
+    return;
+}
+
+void FilterLed::GetMinMax(const int offset, byte *min, byte *max) const
+{
+    *min = fImg[0];
+    *max = fImg[0];
+
+    byte *s = (byte*)fImg;
+    const byte *e0 = s+fW*fH;
+
+    //
+    // calculate mean value
+    //
+    while (s<e0)
+    {
+        const byte *e = s+fH-offset;
+        s += offset;
+
+        while (s<e)
+        {
+            if (*s>*max)
+            {
+                *max = *s;
+                if (*max-*min==255)
+                    return;
+            }
+            if (*s<*min)
+            {
+                *min = *s;
+                if (*max-*min==255)
+                    return;
+            }
+            s++;
+        }
+        s+=offset;
+    }
+}
+
+int FilterLed::GetMeanPosition(const int x, const int y,
+                               const int box, float &mx, float &my) const
+{
+    unsigned int sumx=0;
+    unsigned int sumy=0;
+    unsigned int sum =0;
+
+    for (int dx=x-box; dx<x+box+1; dx++)
+        for (int dy=y-box; dy<y+box+1; dy++)
+        {
+            const byte &m = fImg[dy*fW+dx];
+
+            sumx += m*dx;
+            sumy += m*dy;
+            sum  += m;
+        }
+
+    mx = (float)sumx/sum;
+    my = (float)sumy/sum;
+
+    return (int)my*fW + (int)mx;
+}
+
+int FilterLed::GetMeanPosition(const int x, const int y, const int box) const
+{
+    float mx, my;
+    return GetMeanPosition(x, y, box, mx, my);
+}
+/*
+void FilterLed::RemoveTwins(Leds &leds, Double_t radius)
+{
+    for (int i=first; i<leds.GetEntriesFast(); i++)
+    {
+        const Led &led1 = leds(i);
+
+        const Double_t x1 = led1.GetX();
+        const Double_t y1 = led1.GetY();
+
+        for (int j=first; j<leds.GetEntriesFast(); j++)
+        {
+            if (j==i)
+                continuel
+
+            const Led &led2 = leds(j);
+
+            const Double_t x2 = led2.GetX();
+            const Double_t y2 = led2.GetY();
+
+            const Double_t dx = x2-x1;
+            const Double_t dy = y2-y1;
+
+            if (dx*dx+dy*dy<radius*radius)
+            {
+                // FIXME: Interpolation
+                leds.Remove(led2);
+            }
+        }
+    }
+}
+*/
+void FilterLed::RemoveTwinsInterpol(Leds &leds, Int_t first, Double_t radius) const
+{
+    const Int_t num=leds.GetEntriesFast();
+
+    for (int i=first; i<num; i++)
+    {
+        Led *led1 = (Led*)leds.UncheckedAt(i);
+        if (!led1)
+            continue;
+
+        const Double_t x1 = led1->GetX();
+        const Double_t y1 = led1->GetY();
+
+        Double_t mag = led1->GetMag();
+        Double_t x = x1*mag;
+        Double_t y = y1*mag;
+
+        Double_t sqm = mag*mag;
+        Double_t sqx = x*x;
+        Double_t sqy = y*y;
+
+        Int_t n=1;
+
+        for (int j=first; j<num; j++)
+        {
+            if (i==j)
+                continue;
+
+            Led *led2 = (Led*)leds.UncheckedAt(j);
+            if (!led2)
+                continue;
+
+            Double_t x2 = led2->GetX();
+            Double_t y2 = led2->GetY();
+
+            const Double_t dx = x2-x1;
+            const Double_t dy = y2-y1;
+
+            if (dx*dx+dy*dy>radius*radius)
+                continue;
+
+            // Multiply with weihgt
+            const Double_t w = led2->GetMag();
+            x2 *= w;
+            y2 *= w;
+
+            x   += x2;
+            y   += y2;
+            mag += w;
+
+            sqx += x2*x2;
+            sqy += y2*y2;
+            sqm += w*w;
+
+            n++;
+            leds.Remove(led2);
+        }
+
+        x /= mag;
+        y /= mag;
+
+        sqx /= sqm;
+        sqy /= sqm;
+
+        leds.Add(x, y, 0/*sqrt(sqx-x*x)*/, 0/*sqrt(sqy-y*y)*/, mag/n);
+        leds.Remove(led1);
+    }
+    leds.Compress();
+}
+
+void FilterLed::ExecuteAndMark(Leds &leds, int xc, int yc) const
+{
+    const Int_t first = leds.GetEntriesFast();
+
+    Execute(leds, xc, yc);
+
+    // Mark Stars in image
+    for (int i=first; i<leds.GetEntriesFast(); i++)
+        MarkPoint(leds(i));
+}
+
+void FilterLed::Execute(int xc, int yc) const
+{
+    Leds leds;
+    ExecuteAndMark(leds, xc, yc);
+}
+
+void FilterLed::Execute(Leds &leds, int xc, int yc) const
+{
+    int x0 = xc-fBoxW;
+    int x1 = xc+fBoxW;
+    int y0 = yc-fBoxH;
+    int y1 = yc+fBoxH;
+
+    if (x0<0) x0=0;
+    if (y0<0) y0=0;
+    if (x1>fW) x1=fW;
+    if (y1>fH) y1=fH;
+
+    const int wx = x1-x0;
+    const int hy = y1-y0;
+
+    double sum = 0;
+    double sq  = 0;
+
+    for (int x=x0; x<x1; x++)
+        for (int y=y0; y<y1; y++)
+        {
+            byte &b = fImg[y*fW+x];
+
+            sum += b;
+            sq  += b*b;
+        }
+
+    sum /= wx*hy;
+    sq  /= wx*hy;
+
+    const double sdev = sqrt(sq-sum*sum);
+    const byte   max  = sum+fCut*sdev>254 ? 254 : (byte)(sum+fCut*sdev);
+
+    //
+    // clean image from noise
+    // (FIXME: A lookup table could accelerate things...
+    //
+    for (int x=x0; x<x1; x++)
+        for (int y=y0; y<y1; y++)
+        {
+            byte &b = fImg[y*fW+x];
+            if (b<=max)
+                b = 0;
+        }
+
+    //
+    // find mean points
+    //
+    const int maxpnt = wx*hy>0x4000?0x4000:wx*hy;
+
+    int  pos[maxpnt]; // (Use 'new' instead for big numbers!)
+    byte mag[maxpnt]; // (Use 'new' instead for big numbers!)
+
+    int cnt = 0;
+    for (int x=x0; x<x1; x++)
+        for (int y=y0; y<y1; y++)
+        {
+            byte &b = fImg[y*fW+x];
+            if (b==0)
+                continue;
+
+            const int ipos = GetMeanPosition(x, y, 5);
+
+            int j;
+            for (j=0; j<cnt; j++)
+            {
+                if (pos[j]==ipos)
+                {
+                    if (mag[j] < 0xf0)
+                        mag[j] += 0x10;
+                    break;
+                }
+            }
+            if (cnt && j<cnt)
+                continue;
+
+            pos[cnt] = ipos;
+            mag[cnt] = 0x10;
+
+            cnt++;
+            if (cnt==0x4000)
+                return;
+        }
+
+    if (cnt>1000)
+        cout << "FIXME: Cnt>1000." << endl;
+
+    //
+    // Add found positions to array
+    //
+    const int first=leds.GetEntriesFast();
+
+    for (int i=0; i<cnt; i++)
+    {
+        if (mag[i]<=0x80) // 0xa0
+            continue;
+
+        Float_t mx, my;
+        GetMeanPosition(pos[i]%fW, pos[i]/fW, 5, mx, my);
+
+        leds.Add(mx, my, 0, 0, mag[i]);
+    }
+
+    RemoveTwinsInterpol(leds, first, 5);
+}
+
+void FilterLed::Stretch() const
+{
+    byte min, max;
+    GetMinMax(25, &min, &max);
+
+    if (min==max || max-min>230) // 255/230=1.1
+        return;
+
+    const float scale = 255./(max-min);
+
+    byte *b = fImg;
+    const byte *e = fImg+fW*fH;
+
+    while (b<e)
+    {
+        if (*b<min)
+        {
+            *b++=0;
+            continue;
+        }
+        if (*b>max)
+        {
+            *b++=255;
+            continue;
+        }
+        *b = (byte)((*b-min)*scale);
+        b++;
+    }
+}
+
Index: /trunk/MagicSoft/Cosy/videodev/FilterLed.h
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/FilterLed.h	(revision 2278)
+++ /trunk/MagicSoft/Cosy/videodev/FilterLed.h	(revision 2278)
@@ -0,0 +1,64 @@
+#ifndef CAOS_FilterLed
+#define CAOS_FilterLed
+
+#ifndef __CINT__
+#include <TROOT.h>
+#endif
+
+typedef unsigned char byte;
+
+class Led;
+class Leds;
+class Ring;
+
+class FilterLed
+{
+    byte *fImg;
+    int fW;
+    int fH;
+    int fBoxW;
+    int fBoxH;
+    float fCut;
+
+    void GetMinMax(const int offset, byte *min, byte *max) const;
+    int  GetMeanPosition(const int x, const int y, const int box) const;
+    int  GetMeanPosition(const int x, const int y, const int box, float &mx, float &my) const;
+    void RemoveTwinsInterpol(Leds &leds, Int_t first, Double_t radius) const;
+    void DrawBox(const int x1, const int y1,
+                 const int x2, const int y2,
+                 const int col) const;
+
+public:
+    FilterLed(byte *img, int w, int h, double cut=2.5)
+        : fImg(img), fW(w), fH(h), fBoxW(w/2), fBoxH(h/2), fCut(cut)
+    {
+    }
+
+    FilterLed(byte *img, int w, int h, int box, double cut=2.5)
+        : fImg(img), fW(w), fH(h), fBoxW(box), fBoxH(box), fCut(cut)
+    {
+    }
+
+    FilterLed(byte *img, int w, int h, int boxw, int boxh, double cut=2.5)
+        : fImg(img), fW(w), fH(h), fBoxW(boxw), fBoxH(boxh), fCut(cut)
+    {
+    }
+
+    void Execute(Leds &leds, int xc, int yc) const;
+    void Execute(Leds &leds) const { Execute(leds, fW/2, fH/2); }
+    void ExecuteAndMark(Leds &leds, int xc, int yc) const;
+    void ExecuteAndMark(Leds &leds) const { ExecuteAndMark(leds, fW/2, fH/2); }
+    void Execute(int xc, int yc) const;
+    void Execute() const { Execute(fW/2, fH/2); }
+    void MarkPoint(const Led &led) const;
+    void MarkPoint(Float_t x, Float_t y, Float_t mag) const;
+    void Stretch() const;
+    void DrawCircle(float cx, float cy, float r, byte col=0x40) const;
+    void DrawCircle(float r, byte col=0x40) const { DrawCircle(r, fW/2.+.5, fH/2.+.5, col); }
+    void DrawCircle(const Ring &c, byte col=0x40) const;
+    void DrawCircle(const Ring &c, double r, byte col) const;
+
+    ClassDef(FilterLed, 0)
+};
+
+#endif
Index: /trunk/MagicSoft/Cosy/videodev/Makefile
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/Makefile	(revision 2277)
+++ /trunk/MagicSoft/Cosy/videodev/Makefile	(revision 2278)
@@ -34,7 +34,6 @@
            PngReader.cc \
            PixClient.cc \
-	   Filter.cc \
-	   Filter2.cc \
-           CaosFilter.cc \
+           PixGetter.cc \
+	   FilterLed.cc \
            Writer.cc 
 
Index: /trunk/MagicSoft/Cosy/videodev/PixGetter.cc
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/PixGetter.cc	(revision 2278)
+++ /trunk/MagicSoft/Cosy/videodev/PixGetter.cc	(revision 2278)
@@ -0,0 +1,2 @@
+#include "PixGetter.h"
+
Index: /trunk/MagicSoft/Cosy/videodev/PixGetter.h
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/PixGetter.h	(revision 2278)
+++ /trunk/MagicSoft/Cosy/videodev/PixGetter.h	(revision 2278)
@@ -0,0 +1,10 @@
+#ifndef COSY_PixGetter
+#define COSY_PixGetter
+
+class PixGetter
+{
+public:
+    virtual void ExitLoop() {}
+};
+
+#endif
Index: /trunk/MagicSoft/Cosy/videodev/PngReader.h
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/PngReader.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/videodev/PngReader.h	(revision 2278)
@@ -14,9 +14,11 @@
 #endif
 
+#include "PixGetter.h"
+
 class PixClient;
 
 typedef unsigned char byte;
 
-class PngReader
+class PngReader : public PixGetter
 {
 private:
Index: /trunk/MagicSoft/Cosy/videodev/VideodevLinkDef.h
===================================================================
--- /trunk/MagicSoft/Cosy/videodev/VideodevLinkDef.h	(revision 2277)
+++ /trunk/MagicSoft/Cosy/videodev/VideodevLinkDef.h	(revision 2278)
@@ -6,7 +6,8 @@
 
 #pragma link C++ class Writer+;
-#pragma link C++ class Filter+;
-#pragma link C++ class Filter2+;
-#pragma link C++ class CaosFilter+;
+//#pragma link C++ class Filter+;
+//#pragma link C++ class Filter2+;
+#pragma link C++ class FilterLed+;
+//#pragma link C++ class CaosFilter+;
 
 #pragma link C++ class Camera+;
