Index: trunk/MagicSoft/Cosy/Changelog
===================================================================
--- trunk/MagicSoft/Cosy/Changelog	(revision 7763)
+++ trunk/MagicSoft/Cosy/Changelog	(revision 7764)
@@ -13,6 +13,5 @@
      - do not write pointingpos.txt and tracking_error.txt anymore
 
-
-   * caos/Ring.h: 
+   * caos/Ring.h:
      - added Setter SetXY
 
Index: trunk/MagicSoft/Cosy/catalog/StarCatalog.cc
===================================================================
--- trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 7763)
+++ trunk/MagicSoft/Cosy/catalog/StarCatalog.cc	(revision 7764)
@@ -142,25 +142,40 @@
 }
 
+/*
 void StarCatalog::CalcStars(MStarList &list)
 {
     // full FOV
-    fBox=768;
-    CalcStars(list, 0, 576, 0, 0);
-}
+    fBox=768/2;
+    CalcStars(list, 768/2, 576/2, 0, 0);
+}
+*/
 
 void StarCatalog::CalcStars(MStarList &list, int xc, int yc, 
 			    int xo, int yo) const
 {
+    // For an apropriate unit conversion to pixels [pix/rad]
+    const Double_t scale = TMath::RadToDeg()*sqrt(768*768 + 576*576)/(fAstro->GetRadiusFOV()*2);
+
+    // Offsets to shift [-n/2;n/2] to [0;n] and to
+    // move the stars in the counterdirection of the LEDs
+    const Float_t offx = 768/2 + xo;
+    const Float_t offy = 576/2 + yo;
+
+    // Allow catalog stars to be a bit outside [0.2deg] of the
+    // monitored window. To get the std behaviour set offset=0
+    const Int_t offset = TMath::Nint(0.2*TMath::DegToRad()*scale);
+    const Int_t box    = fBox+offset;
 
     // CalcStars flips the picture in X defaultwise now
-    int x0 = (768-xc)-fBox;
-    int x1 = (768-xc)+fBox;
-    int y0 = yc-fBox;
-    int y1 = yc+fBox;
-
-    if (x0<0) x0=0;
-    if (y0<0) y0=0;
-    if (x1>fW) x1=fW;
-    if (y1>fH) y1=fH;
+    // This defined the box in which stars are really returned
+    int x0 = (768-xc)-box;
+    int x1 = (768-xc)+box;
+    int y0 = yc-box;
+    int y1 = yc+box;
+
+    if (x0<  -offset) x0=  -offset;
+    if (y0<  -offset) y0=  -offset;
+    if (x1>fW+offset) x1=fW+offset;
+    if (y1>fH+offset) y1=fH+offset;
 
     // Align stars into telescope system
@@ -171,7 +186,4 @@
     align.RotateZ(TMath::Pi()/2);
 
-    // For an apropriate unit conversion to pixels
-    const Double_t scale = TMath::RadToDeg()*sqrt(768*768 + 576*576)/(fAstro->GetRadiusFOV()*2);
-
     // Get List of stars from catalog
     TIter Next(fAstro->GetList());
@@ -213,14 +225,15 @@
 
         // Rotate by the rotation angle of the video camera
-        Float_t xx = loc.X()*fCosAngle - loc.Y()*fSinAngle;
-        Float_t yy = loc.X()*fSinAngle + loc.Y()*fCosAngle;
-
-        if (xx<(x0-768/2) || xx >=(x1-768/2) 
-	    || yy<(y0-(576/2+yo)) || yy>=(y1-(576/2+yo)))
+        // and add the offsets on both axis
+        Float_t xx = loc.X()*fCosAngle - loc.Y()*fSinAngle  +  offx;
+        Float_t yy = loc.X()*fSinAngle + loc.Y()*fCosAngle  +  offy;
+
+        // Check if the resulting star is in the
+        // search box for the real stars
+        if (rx<x0 || rx>=x1 || ry<y0 || ry>=y1)
             continue;
 
         // Store pixel coordinates of star in list
-        list.Add(xx+768/2+xo, yy+576/2+yo, -2.5*log10(mag));
-
+        list.Add(rx, ry, -2.5*log10(mag));
     }
 }
Index: trunk/MagicSoft/Cosy/catalog/StarCatalog.h
===================================================================
--- trunk/MagicSoft/Cosy/catalog/StarCatalog.h	(revision 7763)
+++ trunk/MagicSoft/Cosy/catalog/StarCatalog.h	(revision 7764)
@@ -76,6 +76,6 @@
     }
 
+    //void   CalcStars(MStarList &list);
     void   CalcStars(MStarList &list, int xc, int yc, int xo, int yo) const;
-    void   CalcStars(MStarList &list);
     static void DrawStars(MStarList &list, byte *img);
 
Index: trunk/MagicSoft/Cosy/main/MStargHistograms.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MStargHistograms.cc	(revision 7763)
+++ trunk/MagicSoft/Cosy/main/MStargHistograms.cc	(revision 7764)
@@ -127,5 +127,5 @@
 }
 
-void MStargHistograms::Fill(Leds &spots, MStarList &stars, ZdAz &d, ZdAz sao, Ring &center,  ZdAz &centerzdaz, ZdAz &star, Double_t bright, const ZdAz &pos, const MTime &t)
+void MStargHistograms::Fill(Leds &spots, MStarList &stars, ZdAz &d, ZdAz sao, Ring &center,  ZdAz &star, Double_t bright, const ZdAz &pos, const MTime &t)
 {
       // FIXME!
@@ -146,6 +146,6 @@
 	  fCenterX    = center.GetX();
 	  fCenterY    = center.GetY();
-	  fCenterZd   = centerzdaz.Zd();
-	  fCenterAz   = centerzdaz.Az();
+	  //fCenterZd   = centerzdaz.Zd();
+	  //fCenterAz   = centerzdaz.Az();
 	  fStarZd     = star.Zd();
 	  fStarAz     = star.Az();
Index: trunk/MagicSoft/Cosy/main/MStargHistograms.h
===================================================================
--- trunk/MagicSoft/Cosy/main/MStargHistograms.h	(revision 7763)
+++ trunk/MagicSoft/Cosy/main/MStargHistograms.h	(revision 7764)
@@ -31,6 +31,6 @@
     Double_t       fSpots;
     Double_t       fBright;
-    Double_t       fCenterZd;
-    Double_t       fCenterAz;
+//    Double_t       fCenterZd;
+//    Double_t       fCenterAz;
     Double_t       fStarZd;
     Double_t       fStarAz;
@@ -49,5 +49,5 @@
 
     void Fill(Leds &spots, MStarList &stars, ZdAz &d, ZdAz sao, 
-	      Ring &center, ZdAz &centerzdaz, ZdAz &star, Double_t bright,
+	      Ring &center, /*ZdAz &centerzdaz,*/ ZdAz &star, Double_t bright,
 	      const ZdAz &pos, const MTime &t);
     
Index: trunk/MagicSoft/Cosy/main/MStarguider.cc
===================================================================
--- trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 7763)
+++ trunk/MagicSoft/Cosy/main/MStarguider.cc	(revision 7764)
@@ -665,5 +665,4 @@
     else
     {
-
         fStatus = MDriveCom::kStandby;
 
@@ -684,5 +683,5 @@
             fOperations->DisableEntry(IDM_kStargAnalysis);
     }
-    gSystem->Unlink("tracking_error.txt");
+    //gSystem->Unlink("tracking_error.txt");
 }
 
@@ -1371,5 +1370,5 @@
     AltAz pos0 = fSao->CalcAltAzFromPix(768/2,    576/2)*kRad2Deg;
     AltAz pos1 = fSao->CalcAltAzFromPix(768/2+mx, 576/2+my)*kRad2Deg;
-
+/*
     ofstream fout1("pointingpos.txt");
     fout1 << setprecision(10) << fSao->GetMjd()-52000 << " ";
@@ -1385,5 +1384,5 @@
         fout2 << fCosy->GetPointingPos() << " ";
     fout2 << -pos1.Alt() << " " << pos1.Az() << endl;
-
+  */
     return ZdAz(-pos1.Alt(), pos1.Az());
 }
@@ -1853,6 +1852,6 @@
 
     Ring sgcenter(53.2, 293.6); // Center of camera in SG picture [px]
-    ZdAz sgcenterzdaz(0, 0);    // Center of camera in SG picture [deg] 
-                                // (0,0)_deg is at (53.2, 293.6)_px
+    //ZdAz sgcenterzdaz(0, 0);    // Center of camera in SG picture [deg]
+    //                            // (0,0)_deg is at (53.2, 293.6)_px
     ZdAz star(0, 0);            // Star on curtain in [deg]
 
@@ -1862,6 +1861,7 @@
         if (fCosy)
             pos = fCosy->GetPointingPos();
-        sgcenter =
-            fStargCaos->Run(img, kFALSE, kFALSE, pos, t, 30, 3.0); // [px]
+
+        sgcenter = fStargCaos->Run(img, kFALSE, kFALSE, pos, t, 30, 3.0); // [px]
+
         const Float_t pixsize = atof(fPixSize->GetText()); // [arcsec/px]
 
@@ -1871,6 +1871,6 @@
         // wrong exspecially near the zenith were az-lines are highly
         // curved around the zenith!
-        sgcenterzdaz.Zd((sgcenter.GetY()-293.6) * pixsize /3600 );
-	sgcenterzdaz.Az((sgcenter.GetX()-53.2) * pixsize /3600 ); 
+        //sgcenterzdaz.Zd((sgcenter.GetY()-293.6) * pixsize /3600 );
+	//sgcenterzdaz.Az((sgcenter.GetX()-53.2)  * pixsize /3600 );
 #ifdef EXPERT
 	cout << "- LEDs imply offset of Zd=" 
@@ -1918,5 +1918,5 @@
     }
 
-// Find Spot on Camera Center in Starguider camera
+    // Find Spot on Camera Center in Starguider camera
 
     if (fOperations->IsEntryChecked(IDM_kRoqueLampAna))
@@ -1947,6 +1947,4 @@
         f2.DrawCircle(sgcenter, 115.0, 0x0fa);
     }
-
-
 
     // we calculate the offset given by the three ETH Leds visible to
@@ -1958,5 +1956,6 @@
 
     // Position corresponding to the camera center (53.2, 293.6)
-    Ring skycenter(392, 318);
+    //Ring skycenter(392, 318);
+    Ring skycenter(768/2, 576/2);
     // MStarList spots;
 
@@ -1980,12 +1979,14 @@
         // very careful: If center of camera cannot be determined
 	// sgcenter jumps to (0,0)  
-	double sgcenteroffsetX = 0.0;
-	double sgcenteroffsetY = 0.0;
-        if ((double)sgcenter.GetX() > 0.0 && (double)sgcenter.GetY() > 0.0)
-        {
-            //Please never change this offsets!!!
-            sgcenteroffsetX = sgcenter.GetX()-53.2;
-            sgcenteroffsetY = sgcenter.GetY()-293.6;
-        }
+
+        //Please never change this offsets!!!
+        // 53.2 and 293.6 are the "preliminary" camera center
+        // -9 and 28.5 are the offsets of the pointing position in the sky
+        const XY offset(53.2-9, 293.6+28.5);
+
+        const Bool_t centerisvalid = sgcenter.GetX()>0 && sgcenter.GetY()>0;
+        if (centerisvalid)
+            skycenter.SetXY(sgcenter.X() - offset.X(),
+                            sgcenter.Y() - offset.Y());
 
     	// we obtain stars in the effective star FOV and draw them.
@@ -1993,11 +1994,5 @@
 	// We determine the ideal starfield using camera sagging info
 	// from the LEDs
-
-	//cout << sgcenteroffsetX << " " << sgcenteroffsetY << endl;
-
-        fSao->CalcStars(stars, 530, 292, 
-    			-8+2-3+sgcenteroffsetX,  //x: star<spot =>subs
-			31.5-3+sgcenteroffsetY);   //y: star<spot =>subs?
-
+        fSao->CalcStars(stars, 530, 292, skycenter.GetX(), skycenter.GetY());
 	fSao->DrawStars(stars, cimg);
 
@@ -2008,5 +2003,4 @@
 	// Next we evaluate the offset given by the LEDs. This we obtain
 	// in Zd/Az and add it to the tracking error.
-       
 	if (fDisplay->IsEntryChecked(IDM_kStarguider))
         {
@@ -2020,4 +2014,9 @@
  	    fSkyBright->SetText(txt);
 
+            const Bool_t brightnessisvalid = bright<1.75*fLastBright &&
+                bright>30 && bright<110;
+
+            fLastBright = bright;
+
             ULong_t color;
 	    gClient->GetColorByName("Green", color);    
@@ -2033,8 +2032,7 @@
             const Int_t rc = CalcTrackingError(spots, stars, fD, t, bright, numstars);
 
-            if ((bright <= 1.75* fLastBright) && (bright < 110))
-                fStatus = MDriveCom::kMonitoring;
-            else
-                fStatus = MDriveCom::kError;
+            const Bool_t monitoring = brightnessisvalid && centerisvalid;
+
+            fStatus = monitoring ? MDriveCom::kMonitoring : MDriveCom::kError;
 
             if (fCosy)
@@ -2043,8 +2041,6 @@
             if (fOperations->IsEntryChecked(IDM_kStargAnalysis))
                 fStargHistograms->Fill(spots, stars, fD,
-                                       fSao->GetZdAz(), sgcenter, sgcenterzdaz,
+                                       fSao->GetZdAz(), sgcenter, /*sgcenterzdaz,*/
                                        star, bright, fPos, t);
-
-            fLastBright = bright;
 
             if (fCosy)
@@ -2055,14 +2051,19 @@
                                     time.GetMjd(), 0, 0);    // Report
             }
+
         } //kStarguider
 
-	f2.DrawCircle(skycenter, 2.0, 0x0a);
-	
-	f2.DrawCircle(skycenter, 7.4, 0x0a); //0.1deg
-
-	f2.DrawCircle(skycenter, 2.06*.5*74.0, 0x0a);
-	f2.DrawCircle(skycenter, 2.32*.5*74.0, 0x0a);
-        f2.DrawCircle(skycenter, 3.50*.5*74.0, 0x0a);
-        f2.DrawCircle(skycenter, 3.84*.5*74.0, 0x0a);
+        if (centerisvalid)
+        {
+            f2.DrawCircle(skycenter, 2.0, 0x0a);
+
+            f2.DrawCircle(skycenter, 7.4, 0x0a); //0.1deg
+
+            f2.DrawCircle(skycenter, 2.06*.5*74.0, 0x0a);
+            f2.DrawCircle(skycenter, 2.32*.5*74.0, 0x0a);
+            f2.DrawCircle(skycenter, 3.50*.5*74.0, 0x0a);
+            f2.DrawCircle(skycenter, 3.84*.5*74.0, 0x0a);
+        }
+
     } //CalcStars
 
Index: trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 7763)
+++ trunk/MagicSoft/Cosy/tcpip/MDriveCom.cc	(revision 7764)
@@ -231,6 +231,4 @@
     if (stat&kError)
         SetStatus(0);
-    if (stat&kStopped)
-        SetStatus(1);
     if (stat&kStandby)
         SetStatus(2);
Index: trunk/MagicSoft/Cosy/tcpip/MDriveCom.h
===================================================================
--- trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 7763)
+++ trunk/MagicSoft/Cosy/tcpip/MDriveCom.h	(revision 7764)
@@ -29,10 +29,10 @@
     enum
     {
-        kError    = 0x01,
-        kMoving   = 0x02,
-        kTracking = 0x04,
-        kStopping = 0x08,
-        kStopped  = 0x10,
-	kStandby  = 0x20,
+        kError      = 0x01,
+        kMoving     = 0x02,
+        kTracking   = 0x04,
+        kStopping   = 0x08,
+        kStopped    = 0x10,
+	kStandby    = 0x20,
 	kMonitoring = 0x40
     };
