Index: trunk/MagicSoft/Cosy/main/CosyMainLinkDef.h
===================================================================
--- trunk/MagicSoft/Cosy/main/CosyMainLinkDef.h	(revision 9523)
+++ trunk/MagicSoft/Cosy/main/CosyMainLinkDef.h	(revision 9550)
@@ -11,4 +11,5 @@
 #pragma link C++ class MSlewing+;
 #pragma link C++ class MTracking+;
+#pragma link C++ class MMoonPointing+;
 
 #endif
Index: trunk/MagicSoft/Cosy/main/MCosy.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 9523)
+++ trunk/MagicSoft/Cosy/main/MCosy.cc	(revision 9550)
@@ -351,5 +351,5 @@
 }
 
-void MCosy::TrackPlanet(const Int_t &p) // ra, dec [rad]
+void MCosy::TrackPlanet(Int_t id, Double_t offset, Double_t angle)
 {
     MTracking track(this);
@@ -360,8 +360,9 @@
     track.SetTrackAcc(0.01, 0.01);
 
-    track.TrackPlanet((ePlanets_t)p);
-}
-
-void MCosy::TrackPosition(const RaDec &dst) // ra, dec [rad]
+    track.SetWobble(offset, angle);
+    track.TrackPlanet((ePlanets_t)id);
+}
+
+void MCosy::TrackMoon(Double_t wobble, Double_t offset)
 {
     MTracking track(this);
@@ -372,4 +373,17 @@
     track.SetTrackAcc(0.01, 0.01);
 
+    track.TrackMoon(wobble, offset);
+}
+
+void MCosy::TrackPosition(const RaDec &dst) // ra, dec [rad]
+{
+    MTracking track(this);
+    track.SetOut(fOutRep);
+
+    track.SetPointAcc(0.03, 0.01);
+    track.SetPointVelocity(0.3);
+    track.SetTrackAcc(0.01, 0.01);
+
+    //track.SetWobbleOffset(offset, angle);
     track.TrackPosition(dst);
 }
@@ -386,4 +400,5 @@
     track.SetTrackAcc(0.01, 0.01);
 
+    //track.SetWobbleOffset(offset, angle);
     track.TrackPosition(dst);
 }
@@ -530,15 +545,15 @@
             //cout << "WM_TPoint: done. (return 0xaffe)" << endl;
         }
-        return 0xca1b;
+        break;
 
     case WM_STARGTPOINT:
         if (fStarguider)
             fStarguider->StartTPoint((char*)mp);
-        return 0xca1c;
+        break;
 
     case WM_STARGMODE:
         if (fStarguider)
             fStarguider->StartStarguider(*((bool*)mp));
-        return 0xca1c;
+        break;
 
     case WM_TRACKPOS:
@@ -559,5 +574,5 @@
         }
         //cout << "WM_TrackPosition: done. (return 0xabcd)" << endl;
-        return 0xabcd;
+        break;
 
     case WM_ARM:
@@ -580,5 +595,5 @@
         }
         //cout << "WM_Position: done. (return 0x7777)" << endl;
-        return 0x9999;
+        break;
 
     case WM_POSITION:
@@ -592,5 +607,5 @@
         }
         //cout << "WM_Position: done. (return 0x7777)" << endl;
-        return 0x7777;
+        break;
 
     case WM_POSITION1:
@@ -604,5 +619,5 @@
         }
         //cout << "WM_Position: done. (return 0x7777)" << endl;
-        return 0x7777;
+        break;
 
     case WM_PREPS:
@@ -644,5 +659,5 @@
         }
         //cout << "WM_Track: done. (return 0x8888)" << endl;
-        return 0x7878;
+        break;
 /*
     case WM_TESTSE:
@@ -678,17 +693,38 @@
         }
         //cout << "WM_Track/GRB: done. (return 0x8888)" << endl;
-        return 0x8888;
-
-    case WM_PLANET:
+        break;
+
+    case WM_CELEST:
         //cout << "WM_PLANET: START" << endl;
         {
-            const Int_t p = ((Int_t*)mp)[0];
             if (!CheckNetwork())
                 return 0xebb1;
 
-            TrackPlanet(p);
+            const Double_t *d = (Double_t*)mp;
+
+            const Int_t    id     = TMath::Nint(d[0]);
+            const Double_t offset = d[1];
+            const Double_t angle  = d[2];
+
+            TrackPlanet(id, offset, angle);
         }
         //cout << "WM_PLANET: done. (return 0x8889)" << endl;
-        return 0x8889;
+        break;
+
+    case WM_MOON:
+        //cout << "WM_PLANET: START" << endl;
+        {
+            if (!CheckNetwork())
+                return 0xebb1;
+
+            const Double_t *d = (Double_t*)mp;
+
+            const Double_t wobble = d[0];
+            const Double_t offset = d[1];
+
+            TrackMoon(wobble, offset);
+        }
+        //cout << "WM_PLANET: done. (return 0x8889)" << endl;
+        break;
 /*
     case WM_NEWTRACK:
@@ -702,5 +738,5 @@
         fBending.Load("bending.txt");
         //cout << "WM_LoadBending: done. (return 0xbe0d)" << endl;
-        return 0xbe0d;
+        break;
 
     case WM_RESETBENDING:
@@ -708,5 +744,5 @@
         fBending.Reset();
         //cout << "WM_ResetBending: done. (return 0xbe0e)" << endl;
-        return 0xbe0e;
+        break;
 
     case WM_CALCALTAZ:
@@ -737,5 +773,5 @@
             cout << "SE bended:     " << a2.Zd() << "  " << a2.Az() << endl;
         }
-        return 0xa17a;
+        break;
 
     case WM_ENDSWITCH:
@@ -747,6 +783,5 @@
             cout << pos.Az() << "°" << endl;
         }
-
-        return 0x1010;
+        break;
 
     case WM_QUIT:
@@ -759,8 +794,12 @@
         TerminateApp();
         cout << "WM_Quit: done." << endl;
-        return 0xaaaa;
-    }
-    cout << "MCosy::Proc: Unknown message 0x" << msg << endl;
-    return 0xffffffff;
+        break;
+
+    default:
+        cout << "MCosy::Proc: Unknown message 0x" << msg << endl;
+        return 0xffffffff;
+    }
+
+    return msg;
 }
 
Index: trunk/MagicSoft/Cosy/main/MCosy.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MCosy.h	(revision 9523)
+++ trunk/MagicSoft/Cosy/main/MCosy.h	(revision 9550)
@@ -46,5 +46,6 @@
 #define WM_STARGTPOINT  0x1014
 #define WM_STARGMODE    0x1015
-#define WM_PLANET       0x1016
+#define WM_CELEST       0x1016
+#define WM_MOON         0x1017
 
 class Dkc;
@@ -103,5 +104,6 @@
 
     int  SetPosition(const ZdAz &dst, Bool_t track=kFALSE);
-    void TrackPlanet(const Int_t &p);
+    void TrackPlanet(Int_t id, Double_t offset, Double_t angle);
+    void TrackMoon(Double_t wobble, Double_t offset);
     void TrackPosition(const RaDec &dst);    // ra, dec [rad]
     void TrackPositionGRB(const RaDec &dst); // ra, dec [rad]
Index: trunk/MagicSoft/Cosy/main/MMoonPointing.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MMoonPointing.cc	(revision 9550)
+++ trunk/MagicSoft/Cosy/main/MMoonPointing.cc	(revision 9550)
@@ -0,0 +1,143 @@
+#include "MMoonPointing.h"
+
+#include <TFile.h>
+
+ClassImp(MMoonPointing);
+
+using namespace std;
+
+class MPositionOffsetCalc
+{
+private:
+    Double_t fCosOffset;
+    Double_t fSinOffset;
+
+    Double_t fCosAngle;
+    Double_t fSinAngle;
+
+public:
+    MPositionOffsetCalc(Double_t offset) :
+        fCosOffset(offset<=0?0:TMath::Cos(offset*TMath::DegToRad())),
+        fSinOffset(offset<=0?0:TMath::Sin(offset*TMath::DegToRad())),
+        fCosAngle(0), fSinAngle(0)
+    {
+    }
+
+    MPositionOffsetCalc(Double_t offset, Double_t angle) :
+        fCosOffset(offset<=0?0:TMath::Cos(offset*TMath::DegToRad())),
+        fSinOffset(offset<=0?0:TMath::Sin(offset*TMath::DegToRad())),
+        fCosAngle(TMath::Cos(angle*TMath::DegToRad())),
+        fSinAngle(TMath::Sin(angle*TMath::DegToRad()))
+    {
+    }
+
+    MPositionOffsetCalc(Double_t offset, Double_t cos, Double_t sin) :
+        fCosOffset(offset<=0?0:TMath::Cos(offset*TMath::DegToRad())),
+        fSinOffset(offset<=0?0:TMath::Sin(offset*TMath::DegToRad())),
+        fCosAngle(cos), fSinAngle(sin)
+    {
+    }
+
+    void SetAngle(Double_t c, Double_t s) { fCosAngle=c; fSinAngle=s; }
+    void SetAngle(Double_t a) { fCosAngle=TMath::Cos(a*TMath::DegToRad()); fSinAngle=TMath::Sin(a*TMath::DegToRad()); }
+
+    ZdAz Calc(const ZdAz &pos, Double_t *sn=0, Double_t *cs=0) const
+    {
+        if (fCosOffset==0 && fSinOffset==0)
+        {
+            if (sn)
+                *sn = fSinAngle;
+            if (cs)
+                *cs = fCosAngle;
+            return pos;
+        }
+
+        const Double_t coszd = TMath::Cos(pos.Zd());
+        const Double_t sinzd = TMath::Sin(pos.Zd());
+
+        if (sinzd<=0)
+            return ZdAz();
+
+        const Double_t costheta = coszd*fCosOffset + sinzd*fSinOffset*fCosAngle;
+        if (costheta*costheta >= 1)
+            return ZdAz();
+
+        const Double_t sintheta = TMath::Sqrt(1 - costheta*costheta);
+
+        const Double_t cosdeltaaz = (fCosOffset - coszd*costheta)/(sinzd*sintheta);
+        const Double_t sindeltaaz = fSinAngle*fSinOffset/sintheta;
+
+        if (sn)
+            *sn = fSinAngle*coszd*sindeltaaz - cosdeltaaz*fCosAngle;
+        if (cs)
+            *cs = fSinAngle*sinzd/sintheta;
+
+        return ZdAz(TMath::ACos(costheta), TMath::ATan2(sindeltaaz, cosdeltaaz) + pos.Az());
+    }
+
+    ZdAz GetOffset(const ZdAz &pos, Double_t *sn=0, Double_t *cs=0) const
+    {
+        return Calc(pos, sn, cs) - pos;
+    }
+};
+
+MMoonPointing::MMoonPointing(const char *filename) : fOffsetShadow(-1), fOffsetWobble(-1)
+{
+    if (TString(filename).IsNull())
+    {
+        MakeZombie();
+        return;
+    }
+
+    TFile f(filename);
+    if (f.IsZombie())
+    {
+        MakeZombie();
+        return;
+    }
+
+    if (fCos.Read("cosAngle")<=0)
+    {
+        MakeZombie();
+        return;
+    }
+
+    if (fSin.Read("sinAngle")<=0)
+    {
+        MakeZombie();
+        return;
+    }
+
+    fCos.SetDirectory(0);
+    fSin.SetDirectory(0);
+}
+
+// position of the moon [rad]
+// position of the Moon Shadow [rad]
+// position of telescope pointing [rad]
+Bool_t MMoonPointing::CalcPosition(const ZdAz &moon, ZdAz &srcpos, ZdAz &pointpos) const
+{
+    const Double_t fcos = const_cast<TGraph2D&>(fCos).Interpolate(moon.Az(), moon.Zd());
+    const Double_t fsin = const_cast<TGraph2D&>(fSin).Interpolate(moon.Az(), moon.Zd());
+    if (fcos*fcos + fsin*fsin<0.98)
+        return kFALSE;
+
+    // Calculate Moon Shadow Position:
+    Double_t sn=fsin;
+    Double_t cs=fcos;
+
+    const MPositionOffsetCalc calc1(fOffsetShadow, fcos, fsin);
+    srcpos = calc1.Calc(moon, &sn, &cs);
+    if (srcpos.Zd()==0)
+        return kFALSE;
+
+    // Calaculate Telescope pointing position:
+    const MPositionOffsetCalc calc2(fOffsetWobble, cs, sn);
+    pointpos = calc2.Calc(srcpos);
+    if (pointpos.Zd()==0)
+        return kFALSE;
+
+    return kTRUE;
+}
+
+
Index: trunk/MagicSoft/Cosy/main/MMoonPointing.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MMoonPointing.h	(revision 9550)
+++ trunk/MagicSoft/Cosy/main/MMoonPointing.h	(revision 9550)
@@ -0,0 +1,31 @@
+#ifndef COSY_MMoonPointing
+#define COSY_MMoonPointing
+
+#ifndef ROOT_TGraph2D
+#include <TGraph2D.h>
+#endif
+
+#ifndef MARS_MPointing
+#include "MPointing.h"
+#endif
+
+class MMoonPointing : public TObject
+{
+    TGraph2D fCos;
+    TGraph2D fSin;
+
+    Double_t fOffsetShadow; // [deg] distance of the Moon shadow from the Moon
+    Double_t fOffsetWobble; // [deg] wobble offset
+
+public:
+    MMoonPointing(const char *filename);
+
+    void SetOffsetShadow(Double_t off=-1) { fOffsetShadow=off; }
+    void SetOffsetWobble(Double_t off=-1) { fOffsetWobble=off; }
+
+    Bool_t CalcPosition(const ZdAz &moon, ZdAz &srcpos, ZdAz &pointpos) const;
+
+    ClassDef(MMoonPointing, 0)
+};
+
+#endif
Index: trunk/MagicSoft/Cosy/main/MTracking.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MTracking.cc	(revision 9523)
+++ trunk/MagicSoft/Cosy/main/MTracking.cc	(revision 9550)
@@ -12,4 +12,6 @@
 #include "MDriveCom.h"
 
+#include "MMoonPointing.h"
+
 ClassImp(MTracking);
 
@@ -21,5 +23,5 @@
 MTracking::MTracking(MCosy *cosy)
     : MSlewing(cosy), MThread("MTracking"), fSlalib(fCosy->fObservatory),
-    fTrackAcc(0, 0), fOut(0)
+    fTrackAcc(0, 0), fWobbleOffset(-1), fWobbleAngle(0), fOut(0)
 {
 }
@@ -221,8 +223,40 @@
         sla.Set(fTrackPos/TMath::DegToRad());
     else
-        sla.SetPlanet((ePlanets_t)fTrackType);
-
-//    if (fCosy->fStarguider)
-//        fCosy->fStarguider->SetPointingPosition(sla.GetRaDec());
+        sla.SetPlanet((ePlanets_t)(fTrackType&0xff));
+
+    if (fTrackType==(kEMoon|0x100))
+    {
+        MMoonPointing moon("MoonShadowOffsets.root");
+        if (moon.IsZombie())
+        {
+            gLog << warn << "WARNING - Calculation of moon shadow failed." << endl;
+            return;
+        }
+
+        moon.SetOffsetShadow(fWobbleAngle);
+        moon.SetOffsetWobble(fWobbleOffset);
+
+        ZdAz srcpos, pointpos;
+
+        const ZdAz za     = sla.GetZdAzRad();
+        const ZdAz offset = moon.CalcPosition(za, srcpos, pointpos);
+        sla.ApplyOffsetZdAz(pointpos-za);
+
+        // Ra/Dec, Zd/Az from pointpos
+        // NEW: Source pos from srcpos
+    }
+        /*
+    else
+        if (fWobbleOffset>0)
+        {
+            MPositionOffsetCalc calc(fWobbleOffset, fWobbleAngle);
+            const ZdAz offset = calc.GetOffset(sla.GetZdAzRad());
+            //if (srcpos.Zd()==0)
+            //    return kFALSE;
+            sla.ApplyOffsetAltAz(offset);
+        }
+        */
+    //    if (fCosy->fStarguider)
+    //        fCosy->fStarguider->SetPointingPosition(sla.GetRaDec());
 }
 
@@ -288,4 +322,16 @@
     fTrackPos  = RaDec();
     fTrackType = planet;
+
+    // Start tracking
+    Track();
+}
+
+void MTracking::TrackMoon(Double_t wobble, Double_t offset)
+{
+    fTrackPos  = RaDec();
+    fTrackType = kEMoon|0x100;
+
+    fWobbleOffset = wobble;
+    fWobbleAngle  = offset;
 
     // Start tracking
Index: trunk/MagicSoft/Cosy/main/MTracking.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MTracking.h	(revision 9523)
+++ trunk/MagicSoft/Cosy/main/MTracking.h	(revision 9550)
@@ -28,4 +28,7 @@
     RaDec fTrackPos;
 
+    Double_t fWobbleOffset;
+    Double_t fWobbleAngle;
+
     MLog   *fOut;
 
@@ -42,4 +45,6 @@
     Int_t Thread();
 
+    ZdAz GetZdAzRad(const Slalib &sla);
+
 public:
     MTracking(MCosy *cosy);
@@ -47,5 +52,8 @@
     void TrackPosition(const RaDec &dst); // ra, dec [rad]
     void TrackPlanet(ePlanets_t planet);  // ra, dec [rad]
+    void TrackMoon(Double_t wobble, Double_t offset);
     void SetTrackAcc(Float_t zd, Float_t az) { fTrackAcc.Zd(zd); fTrackAcc.Az(az); }
+
+    void SetWobble(Double_t offset, Double_t angle=0) { fWobbleOffset=offset; fWobbleAngle=angle; }
 
     void SetOut(MLog *fout) { fOut = fout; }
Index: trunk/MagicSoft/Cosy/main/Makefile
===================================================================
--- trunk/MagicSoft/Cosy/main/Makefile	(revision 9523)
+++ trunk/MagicSoft/Cosy/main/Makefile	(revision 9550)
@@ -18,9 +18,10 @@
 #------------------------------------------------------------------------------
 
-SRCFILES = MStarguider.cc \
-           MStargHistograms.cc \
-           MSlewing.cc \
+SRCFILES = MSlewing.cc \
            MTracking.cc \
+	   MMoonPointing.cc \
            MCaos.cc \
+	   MStarguider.cc \
+	   MStargHistograms.cc \
 	   MCosy.cc
 
