Index: /branches/eam_branches/ipp-20140423/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /branches/eam_branches/ipp-20140423/Ohana/src/libdvo/include/dvo.h	(revision 36823)
+++ /branches/eam_branches/ipp-20140423/Ohana/src/libdvo/include/dvo.h	(revision 36824)
@@ -171,4 +171,5 @@
   ID_STAR_USE_PAR   = 0x00200000, // parallax used (not AVE or PM)
   ID_STAR_NO_ASTROM = 0x00400000, // mean astrometry could not be measured
+  ID_STAR_BAD_PM    = 0x00800000, // mean astrometry could not be measured
   ID_OBJ_EXT        = 0x01000000, // extended in our data (eg, PS)
   ID_OBJ_EXT_ALT    = 0x02000000, // extended in external data (eg, 2MASS)
Index: /branches/eam_branches/ipp-20140423/Ohana/src/relastro/src/UpdateObjects.c
===================================================================
--- /branches/eam_branches/ipp-20140423/Ohana/src/relastro/src/UpdateObjects.c	(revision 36823)
+++ /branches/eam_branches/ipp-20140423/Ohana/src/relastro/src/UpdateObjects.c	(revision 36824)
@@ -119,6 +119,13 @@
       fitPAR.chisq = NAN;
 
+      // if we fail to fit the astrometry for some reason, we need to set/reset these
+      catalog[i].average[j].flags |= ID_STAR_NO_ASTROM;
+      catalog[i].average[j].ChiSqAve  = NAN;
+      catalog[i].average[j].ChiSqPM   = NAN;
+      catalog[i].average[j].ChiSqPar  = NAN;
+      catalog[i].average[j].Npos = 0;
+
       if (catalog[i].average[j].Nmeasure == 0) {
-	  continue;
+	continue;
       }
 
@@ -146,4 +153,8 @@
 	}
 
+	// reset the bit to note that a detection was used (or not)
+	measure[k].dbFlags &= ~ID_MEAS_USED_OBJ;
+	if (measureBig) { measureBig[k].dbFlags &= ~ID_MEAS_USED_OBJ; }
+
 	// does the measurement pass the supplied filtering constraints?
 	// MeasFilterTestTiny does not test psfQF
@@ -151,6 +162,4 @@
 	int keepMeasure = measureBig ? MeasFilterTest(&measureBig[k], FALSE) : MeasFilterTestTiny(&measure[k], FALSE);
 	if (!keepMeasure) {
-	  measure[k].dbFlags &= ~ID_MEAS_USED_OBJ;
-	  if (measureBig) { measureBig[k].dbFlags &= ~ID_MEAS_USED_OBJ; }
 	  continue;
 	}
@@ -166,6 +175,4 @@
 	  double radius = hypot(Xi, Yi);
 	  if (radius > ExcludeBogusRadius) {
-	      measure[k].dbFlags &= ~ID_MEAS_USED_OBJ;
-	      if (measureBig) { measureBig[k].dbFlags &= ~ID_MEAS_USED_OBJ; }
 	      measure[k].dbFlags |= ID_MEAS_POOR_ASTROM;
 	      if (measureBig) { measureBig[k].dbFlags |= ID_MEAS_POOR_ASTROM; }
@@ -178,11 +185,6 @@
 	// outlier rejection
 	if (FALSE && FlagOutlier && (measure[k].dbFlags & ID_MEAS_POOR_ASTROM)) {
-	  measure[k].dbFlags &= ~ID_MEAS_USED_OBJ;
-	  if (measureBig) { measureBig[k].dbFlags &= ~ID_MEAS_USED_OBJ; }
 	  continue;
 	}
-
-	measure[k].dbFlags |= ID_MEAS_USED_OBJ;
-	if (measureBig) { measureBig[k].dbFlags |= ID_MEAS_USED_OBJ; }
 
 	R[N] = getMeanR (&measure[k], &catalog[i].average[j], &catalog[i].secfilt[j*Nsecfilt]);
@@ -199,14 +201,6 @@
 	// allow a given photcode or measurement to be
 	// ignored if the error is NAN (for photcode, set astromErrSys to NaN)
-	if (isnan(dX[N])) {
-	  measure[k].dbFlags &= ~ID_MEAS_USED_OBJ;
-	  if (measureBig) { measureBig[k].dbFlags &= ~ID_MEAS_USED_OBJ; }
-	  continue;
-	}
-	if (isnan(dY[N])) {
-	  measure[k].dbFlags &= ~ID_MEAS_USED_OBJ;
-	  if (measureBig) { measureBig[k].dbFlags &= ~ID_MEAS_USED_OBJ; }
-	  continue;
-	}
+	if (isnan(dX[N])) continue;
+	if (isnan(dY[N])) continue;
 
 	// add systematic error in quadrature, if desired
@@ -238,12 +232,13 @@
 	}
 
+	measure[k].dbFlags |= ID_MEAS_USED_OBJ;
+	if (measureBig) { measureBig[k].dbFlags |= ID_MEAS_USED_OBJ; }
+
 	N++;
-      }
+      } // loop over measurements : catalog[i].average[j].Nmeasure 
 
       // if we have too few good detections for the desired fit, or too limited a
       // baseline, use a fit with fewer parameters.  XXX if we have too few measurements
       // for even the average position, consider including the lower-quality detections?
-
-      catalog[i].average[j].flags &= ~ID_STAR_NO_ASTROM;
 
       // find Tmin & Tmax from the list of accepted measurements
@@ -268,10 +263,4 @@
       // too few measurements for average position (require 2 values)
       if (N < SRC_MEAS_TOOFEW) {
-	// XXX need to define PHOTOM and ASTROM object flags
-	// XXX reset the average value fields?
-	catalog[i].average[j].flags |= ID_STAR_NO_ASTROM;
-	catalog[i].average[j].ChiSqAve  = NAN;
-	catalog[i].average[j].ChiSqPM   = NAN;
-	catalog[i].average[j].ChiSqPar  = NAN;
 	if (N < 2) continue;
       }
@@ -305,4 +294,10 @@
 	catalog[i].average[j].flags |= ID_STAR_FIT_PM;
 	Npm ++;
+
+	// XXX a hard-wired hack...
+	if ((fabs(fitPM.uR) > 2.0) || (fabs(fitPM.uD) > 2.0)) {
+	  mode = FIT_AVERAGE;
+	  catalog[i].average[j].flags |= ID_STAR_BAD_PM;
+	}
       }
 
@@ -331,4 +326,10 @@
 	  catalog[i].average[j].flags |= ID_STAR_FIT_PAR;
 	  Npar ++;
+
+	  // XXX a hard-wired hack...
+	  if ((fabs(fitPAR.uR) > 2.0) || (fabs(fitPAR.uD) > 2.0)) {
+	    mode = FIT_AVERAGE;
+	    catalog[i].average[j].flags |= ID_STAR_BAD_PM;
+	  }
 	} else {
 	  // need to set mode = FIT_PM_ONLY if we do not fit for parallax
@@ -452,4 +453,8 @@
       catalog[i].average[j].Trange = (Trange * 86400 * 365.25);
       catalog[i].average[j].Npos = fit.Nfit;
+
+      // unset the NO_ASTROM bit (not(NO_ASTROM) == HAVE_ASTROM)
+      catalog[i].average[j].flags &= ~ID_STAR_NO_ASTROM;
+
       if (XVERB) fprintf (stderr, "%f %f -> %f %f (%f,%f) pm=(%f %f) chisq=(%f, %f, %f)\n",
                           catalog[i].average[j].R,
