Index: /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/find_matches.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/find_matches.c	(revision 36903)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/find_matches.c	(revision 36904)
@@ -14,6 +14,6 @@
 
   if (NSTAR_GROUP <= 0) {
-      fprintf (stderr, "ERROR: NSTAR_GROUP NOT SET!\n");
-      exit (1);
+    fprintf (stderr, "ERROR: NSTAR_GROUP NOT SET!\n");
+    exit (1);
   }
 
@@ -169,16 +169,19 @@
       float dRoff = dvoOffsetR(&catalog[0].measure[Nmeas], &catalog[0].average[n]);
 
-      // rationalize dR:
+      // rationalize R:
       if (dRoff > +180.0*3600.0) {
-	  // average on high end of boundary, move star up
-	  catalog[0].measure[Nmeas].R += 360.0;
-	  dRoff -= 360.0*3600.0;
+	// average on high end of boundary, move star up
+	catalog[0].measure[Nmeas].R += 360.0;
+	dRoff -= 360.0*3600.0;
       }
       if (dRoff < -180.0*3600.0) {
-	  // average on low end of boundary, move star down
-	  catalog[0].measure[Nmeas].R -= 360.0;
-	  dRoff += 360.0*3600.0;
-      }
-      if (dRoff > 10*RADIUS) {
+	// average on low end of boundary, move star down
+	catalog[0].measure[Nmeas].R -= 360.0;
+	dRoff += 360.0*3600.0;
+      }
+      if (fabs(dRoff) > 10*RADIUS) {
+	// take declination into account and check again.
+	double cosD = cos(RAD_DEG*catalog[0].average[n].D);
+	if (fabs(dRoff*cosD) > 10*RADIUS) {
 	  fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
 		   catalog[0].average[n].R, catalog[0].average[n].D, 
@@ -186,4 +189,5 @@
 		   X1[i], X2[J], 
 		   Y1[i], Y2[J]);
+	}
       }
 
@@ -234,6 +238,6 @@
   }
 
-  /* incorporate unmatched image stars, if this star is in field of this catalog */
-  /* these new entries are all written out in UPDATE mode */ 
+/* incorporate unmatched image stars, if this star is in field of this catalog */
+/* these new entries are all written out in UPDATE mode */ 
   for (i = 0; (i < Nstars) && !options.only_match; i += NSTAR_GROUP) {
     /* make sure there is space for next entry */
@@ -262,5 +266,5 @@
 
     if (PSPS_ID) {
-        catalog[0].average[Nave].extID = CreatePSPSObjectID(catalog[0].average[Nave].R, catalog[0].average[Nave].D);
+      catalog[0].average[Nave].extID = CreatePSPSObjectID(catalog[0].average[Nave].R, catalog[0].average[Nave].D);
     }
 
@@ -286,9 +290,9 @@
       /* in UPDATE mode, this value is not saved; use relphot to recalculate */
       if (Nsec > -1) { 
-          catalog[0].secfilt[Nave*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas]);
+	catalog[0].secfilt[Nave*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas]);
       }
 
       /* next[Nmeas] should always be -1 in this context (it is always the only
-         measurement for the star) */
+	 measurement for the star) */
       stars[i].found = Nmeas;
       next_meas[Nmeas] = -1;  // initial value here update below
@@ -311,5 +315,5 @@
   }
 
-  /* note stars which have been found in this catalog */
+/* note stars which have been found in this catalog */
   for (i = 0; i < NstarsIn; i++) {
     if (stars[i].found > -1) {
@@ -318,5 +322,5 @@
   }
 
-  /* check if the catalog has changed?  if no change, no need to write */
+/* check if the catalog has changed?  if no change, no need to write */
   catalog[0].objID    = objID; // new max value, save on catalog close
   catalog[0].Naverage = Nave;
Index: /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/find_matches_closest.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/find_matches_closest.c	(revision 36903)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/find_matches_closest.c	(revision 36904)
@@ -207,11 +207,15 @@
       dRoff += 360.0*3600.0;
     }
-    if (dRoff > 10*RADIUS) {
-      fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
-	       catalog[0].average[n].R, catalog[0].average[n].D, 
-	       stars[N].average.R, stars[N].average.D,
-	       X1[i], X2[Jmin], 
-	       Y1[i], Y2[Jmin]);
-	// XXX abort on this? -- this is a bad failure...
+    if (fabs(dRoff) > 10*RADIUS) {
+	// take declination into account and check again.
+	double cosD = cos(RAD_DEG*catalog[0].average[n].D);
+	if (fabs(dRoff*cosD) > 10*RADIUS) {
+	    fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
+		     catalog[0].average[n].R, catalog[0].average[n].D, 
+		     stars[N].average.R, stars[N].average.D,
+		     X1[i], X2[Jmin], 
+		     Y1[i], Y2[Jmin]);
+	    // XXX abort on this? -- this is a bad failure...
+	}
     }
 
Index: /branches/eam_branches/ipp-20140610/Ohana/src/dvomerge/src/merge_catalogs_old.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/dvomerge/src/merge_catalogs_old.c	(revision 36903)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/dvomerge/src/merge_catalogs_old.c	(revision 36904)
@@ -19,5 +19,5 @@
 
   off_t i, j, k, Nin, offset, J, Jmin, status, Nstars;
-  double RADIUS2, Rmin, Rin, Din;
+  double RADIUS2, Rmin;
   double *X1, *Y1, *X2, *Y2;
   double dX, dY, dR;
@@ -206,10 +206,11 @@
       output[0].measure[Nmeas] = input[0].measure[offset];
 
-      Rin = input[0].average[N].R - input[0].measure[offset].dR / 3600.0;
-      Din = input[0].average[N].D - input[0].measure[offset].dD / 3600.0;
-
-      /** dR,dD now represent arcsec **/
-      output[0].measure[Nmeas].dR       = 3600.0*(output[0].average[n].R - Rin);  // XXX update these based on choice of astromety
-      output[0].measure[Nmeas].dD       = 3600.0*(output[0].average[n].D - Din);  // XXX update these based on choice of astromety
+      // old code: find R,D using average_in[0], the get offset relative to average_out[0].  no longer
+      // needed since we carry around R,D
+      // Rin = input[0].average[N].R - input[0].measure[offset].dR / 3600.0;
+      // Din = input[0].average[N].D - input[0].measure[offset].dD / 3600.0;
+      // output[0].measure[Nmeas].dR = 3600.0*(output[0].average[n].R - Rin);
+      // output[0].measure[Nmeas].dD = 3600.0*(output[0].average[n].D - Din);
+
       output[0].measure[Nmeas].dbFlags  = 0;  // XXX why reset these?
       output[0].measure[Nmeas].averef   = n;
@@ -221,25 +222,26 @@
       // fprintf (stderr, "Nave : "OFF_T_FMT", Nmeas : "OFF_T_FMT", dR: %f, dD: %f, catID: %d\n",  n,  Nmeas, output[0].measure[Nmeas].dR, output[0].measure[Nmeas].dD, output[0].measure[i].catID);
 
-      // rationalize dR
-      if (output[0].measure[Nmeas].dR > +180.0*3600.0) {
+      float dRoff = dvoOffsetR(&output[0].measure[Nmeas], &output[0].average[n]);
+
+      // rationalize R
+      if (dRoff > +180.0*3600.0) {
 	// average on high end of boundary, move star up
-	Rin += 360.0;
-	output[0].measure[Nmeas].dR = 3600.0*(output[0].average[n].R - Rin);
-      }
-      if (output[0].measure[Nmeas].dR < -180.0*3600.0) {
+	output[0].measure[Nmeas].R += 360.0;
+	dRoff -= 360.0*3600.0;
+      }
+      if (dRoff < -180.0*3600.0) {
 	// average on low end of boundary, move star down
-	Rin -= 360.0;
-	output[0].measure[Nmeas].dR = 3600.0*(output[0].average[n].R - Rin);
-      }
-      if (fabs(output[0].measure[Nmeas].dR) > 10*RADIUS) {
-        // ok take declination into account and check again.
-        double cosD = cos(RAD_DEG*Din);
-        if (fabs(output[0].measure[Nmeas].dR*cosD) > 10*RADIUS) {
-
-            fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
-             output[0].average[n].R, output[0].average[n].D, Rin, Din,
-             X1[i], X2[Jmin], Y1[i], Y2[Jmin]);
-
-            // XXX abort on this? -- this is a bad failure...
+	output[0].measure[Nmeas].R -= 360.0;
+	dRoff += 360.0*3600.0;
+      }
+      if (fabs(dRoff) > 10*RADIUS) {
+        // take declination into account and check again.
+        double cosD = cos(RAD_DEG*output[0].average[n].D);
+        if (fabs(dRoff*cosD) > 10*RADIUS) {
+	  fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
+		   output[0].average[n].R, output[0].average[n].D, 
+		   output[0].measure[Nmeas].R, output[0].measure[Nmeas].D,
+		   X1[i], X2[Jmin], Y1[i], Y2[Jmin]);
+	  // XXX abort on this? -- this is a bad failure...
         }
       }
Index: /branches/eam_branches/ipp-20140610/Ohana/src/dvomerge/src/replace_match.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/dvomerge/src/replace_match.c	(revision 36903)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/dvomerge/src/replace_match.c	(revision 36904)
@@ -7,5 +7,4 @@
   unsigned int averef;
   unsigned int catID;
-  double Rin, Din;
 
   // find the matching photcode in the object's list of measurements
@@ -18,10 +17,11 @@
     measure_out[Nout] = measure_in[0];
 
-    Rin = average_in[0].R - measure_in[0].dR / 3600.0;
-    Din = average_in[0].D - measure_in[0].dD / 3600.0;
+    // old code: find R,D using average_in[0], the get offset relative to average_out[0].  no longer
+    // needed since we carry around R,D
+    // double Rin = average_in[0].R - measure_in[0].dR / 3600.0;
+    // double Din = average_in[0].D - measure_in[0].dD / 3600.0;
+    // measure_out[Nout].dR = 3600.0*(average_out[0].R - Rin);
+    // measure_out[Nout].dD = 3600.0*(average_out[0].D - Din);
 
-    /** dR,dD now represent arcsec **/
-    measure_out[Nout].dR       = 3600.0*(average_out[0].R - Rin);
-    measure_out[Nout].dD       = 3600.0*(average_out[0].D - Din);
     measure_out[Nout].dbFlags  = 0;  // XXX why reset these?
     measure_out[Nout].averef   = averef;
@@ -29,24 +29,25 @@
     measure_out[Nout].catID    = catID;
 
+    float dRoff = dvoOffsetR(&measure_out[Nout], average_out);
+
     // rationalize dR
-    if (measure_out[Nout].dR > +180.0*3600.0) {
+    if (dRoff > +180.0*3600.0) {
       // average on high end of boundary, move star up
-      Rin += 360.0;
-      measure_out[Nout].dR = 3600.0*(average_out[0].R - Rin);
+      measure_out[Nout].R += 360.0;
+      dRoff -= 360.0*3600.0;
     }
-    if (measure_out[Nout].dR < -180.0*3600.0) {
+    if (dRoff < -180.0*3600.0) {
       // average on low end of boundary, move star down
-      Rin -= 360.0;
-      measure_out[Nout].dR = 3600.0*(average_out[0].R - Rin);
+      measure_out[Nout].R -= 360.0;
+      dRoff += 360.0*3600.0;
     }
 
     // warn on surprisingly distant detections
-    if (fabs(measure_out[Nout].dR) > 10*RADIUS) {
+    if (fabs(dRoff) > 10*RADIUS) {
       // ok take declination into account and check again.
-      double cosD = cos(RAD_DEG*Din);
-      if (fabs(measure_out[Nout].dR*cosD) > 10*RADIUS) {
-
+      double cosD = cos(RAD_DEG*average_out[0].D);
+      if (fabs(dRoff*cosD) > 10*RADIUS) {
 	fprintf (stderr, "surprisingly distant detection: %10.6f,%10.6f vs %10.6f,%10.6f\n", 
-		 average_out[0].R, average_out[0].D, Rin, Din);
+		 average_out[0].R, average_out[0].D, measure_out[Nout].R, measure_out[Nout].D);
       }
     }
