Index: /branches/eam_branches/ipp-20130509/Ohana/src/relastro/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/relastro/src/ImageOps.c	(revision 35664)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/relastro/src/ImageOps.c	(revision 35665)
@@ -454,4 +454,5 @@
     }
     n = measure[0].averef;
+    Average *average = &catalog[c].average[n];
 
     if (moscoords == NULL) {
@@ -467,14 +468,29 @@
 
     // new dR, dD : test
-    dR = 3600.0*(catalog[c].average[n].R - R);
-    dD = 3600.0*(catalog[c].average[n].D - D);
+    dR = 3600.0*(average[0].R - R);
+    dD = 3600.0*(average[0].D - D);
+
+    // make sure detection is on the same side of the 0,360 boundary as the average
+    // this will give some funny results withing ~1 arcsec of the pol
+    if (dR > +180.0*3600.0) {
+      // average on high end of boundary, move star up
+      R += 360.0;
+      dR = 3600.0*(average[0].R - R);
+    }
+    if (dR < -180.0*3600.0) {
+      // average on low end of boundary, move star down
+      R -= 360.0;
+      dR = 3600.0*(average[0].R - R);
+    }
+
+    float csdec = cos(average[0].D * RAD_DEG);
 
     // complain if the new location is far from the average location
     // NOTE: This should never happen, or our StarMap tests are not working
-    if (fabs(dR) > 3.0*ADDSTAR_RADIUS) {
+    if (fabs(dR*csdec) > 3.0*ADDSTAR_RADIUS) {
       NoffRAave ++;
       if (VERBOSE2) {
-	fprintf (stderr, "measurement is far from average location (R): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
-	dump_measures (&catalog[c].average[n], catalog[c].measure);
+	fprintf (stderr, "measurement is far from average location (R): %f %f (%f %f %f)\n", average[0].R, average[0].D, dR, dR*csdec, dD);
+	dump_measures (&average[0], catalog[c].measure);
       }
       // abort ();
@@ -483,6 +499,6 @@
       NoffDECave ++;
       if (VERBOSE2) {
-	fprintf (stderr, "measurement is far from average location (D): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
-	dump_measures (&catalog[c].average[n], catalog[c].measure);
+	fprintf (stderr, "measurement is far from average location (D): %f %f (%f %f)\n", average[0].R, average[0].D, dR, dD);
+	dump_measures (&average[0], catalog[c].measure);
       }
       // abort ();
@@ -490,9 +506,9 @@
 
     // complain if the new location is far from the old location
-    if (fabs(catalog[c].measureT[m].dR - dR) > DPOS_MAX_ASEC) {
+    if (fabs(csdec*(measure[0].dR - dR)) > DPOS_MAX_ASEC) {
       NoffRAori ++;
       if (VERBOSE2) {
-	fprintf (stderr, "measurement is far from original location (R): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
-	dump_measures (&catalog[c].average[n], catalog[c].measure);
+	fprintf (stderr, "measurement moves far from original location (R): %f %f (%f %f %f %f)\n", average[0].R, average[0].D, measure[0].dR, dR, csdec*(measure[0].dR - dR), dD);
+	dump_measures (&average[0], catalog[c].measure);
       }
       // abort();
@@ -501,6 +517,6 @@
       NoffDECori ++;
       if (VERBOSE2) {
-	fprintf (stderr, "measurement is far from original location (D): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
-	dump_measures (&catalog[c].average[n], catalog[c].measure);
+	fprintf (stderr, "measurement moves far from original location (D): %f %f (%f %f)\n", average[0].R, average[0].D, dR, measure[0].dD, dD);
+	dump_measures (&average[0], catalog[c].measure);
       }      // abort();
     }
@@ -511,18 +527,10 @@
     measure[0].dR = dR;
     measure[0].dD = dD;
+    measureT[0].dR = dR;
+    measureT[0].dD = dD;
     
-    if (measure[0].dR > +180.0*3600.0) {
-      // average on high end of boundary, move star up
-      R += 360.0;
-      measure[0].dR = 3600.0*(catalog[c].average[n].R - R);
-    }
-    if (measure[0].dR < -180.0*3600.0) {
-      // average on low end of boundary, move star down
-      R -= 360.0;
-      measure[0].dR = 3600.0*(catalog[c].average[n].R - R);
-    }
-
     // set the systematic error for this image:
     measure[0].dRsys = ToShortPixels(dPosSys);
+    measureT[0].dRsys = ToShortPixels(dPosSys);
   }
 
@@ -530,6 +538,4 @@
   if (VERBOSE && (Noff > 0)) fprintf (stderr, "Noff ave RA %d, Noff ave DEC %d, Noff ori RA %d, Noff ori DEC %d\n", NoffRAave, NoffDECave, NoffRAori, NoffDECori);
   saveOffsets (dPos, nPos, im);
-
-  // XXX this function should probably update the average positions as well.
 
   return;
