Index: /branches/eam_branches/relastro.20100326/src/CoordOps.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/CoordOps.c	(revision 27488)
+++ /branches/eam_branches/relastro.20100326/src/CoordOps.c	(revision 27488)
@@ -0,0 +1,69 @@
+# include "relastro.h"
+
+static double *dPosSum    = NULL; // sum of dPos^2 for all measures on each image
+static off_t  *nPosSum    = NULL; // sum of measures on each image (used for dPosSum)
+static int    *isBadCoord = NULL; // keep or reject each image?
+
+static Coords *oldCoords;   // list of available images
+static off_t  NoldCoords;   // number of available images
+
+void initCoords (void) {
+
+  images = getImages (&N);
+
+  NoldCoords = N;
+  ALLOCATE (oldCoords, Coords, NoldCoords);
+  ALLOCATE (dPosSum, double, NoldCoords);
+  ALLOCATE (nPosSum, off_t,  NoldCoords);
+  ALLOCATE (isBadCoord, int, NoldCoords);
+  memset (oldCoords,  0, N*sizeof(Coords));
+  memset (dPosSum,    0, N*sizeof(double));
+  memset (nPosSum,    0, N*sizeof(off_t));
+  memset (isBadCoord, 0, N*sizeof(int));
+}
+
+int saveCoords (Coords *coords, off_t N) {
+
+  if (N < 0) return FALSE;
+  if (N >= NoldCoords) return FALSE;
+
+  memcpy (&oldCoords[N], coords, sizeof(Coords));
+  return TRUE;
+}
+
+Coords getCoords (off_t N) {
+
+  if (N < 0) return NULL;
+  if (N >= NoldCoords) return NULL;
+
+  return (&oldCoords[N]);
+}
+
+int badCoords (off_t N) {
+
+  if (N < 0) return FALSE;
+  if (N >= NoldCoords) return FALSE;
+
+  return (isBadCoord[N]);
+}
+  
+void setBadCoords (off_t N) {
+
+  if (N < 0) return;
+  if (N >= NoldCoords) return;
+
+  isBadCoord[N] = TRUE;
+  return;
+}
+  
+void saveOffsets (double dPos, off_t nPos, off_t N) {
+
+  if (N < 0) return;
+  if (N >= NoldCoords) return;
+
+  dPosSum[N] += dPos;
+  nPosSum[N] += nPos;
+  
+  return;
+}
+  
Index: /branches/eam_branches/relastro.20100326/src/FitChip.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/FitChip.c	(revision 27487)
+++ /branches/eam_branches/relastro.20100326/src/FitChip.c	(revision 27488)
@@ -22,4 +22,5 @@
   CoordFit *fit;
   double dL, dM, dR, dRmax, *values;
+  Coords oldCoords;
 
   ALLOCATE (values, double, Nmatch);
@@ -84,5 +85,5 @@
       default:
         fprintf (stderr, "invalid chip order %d\n", coords[0].Npolyterms);
-        abort ();
+	skip = TRUE;
     }
     if (skip) {
@@ -96,6 +97,14 @@
 
     // measure the fit, update the coords & object coordinates
-    fit_eval (fit);
-    fit_apply_coords (fit, coords);
+    if (!fit_eval (fit)) {
+      fprintf (stderr, "failed to fit new model\n");
+      return FALSE;
+    }
+
+    if (!fit_apply_coords (fit, coords)) {
+      fprintf (stderr, "failed to fit new model\n");
+      return FALSE;
+    }
+
     fit_free (fit);
 
@@ -107,5 +116,5 @@
 
   free (values);
-  return;
+  return TRUE;
 }
 
Index: /branches/eam_branches/relastro.20100326/src/FixProblemImages.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/FixProblemImages.c	(revision 27488)
+++ /branches/eam_branches/relastro.20100326/src/FixProblemImages.c	(revision 27488)
@@ -0,0 +1,60 @@
+# include "relastro.h"
+
+// loop over all images.  for any images which have a bad coordinate solution, replace the
+// original coordinates and recalculate the positions
+
+int FixProblemImages () {
+
+  off_t im, Nimage;
+  Image *image;
+
+  image = getimages (&Nimage);
+
+  for (i = 0; i < Nimage; i++) {
+    
+    // check if this image should be fixed
+    if (!badCoords(i)) continue;
+
+    // get list of catalogs containing measurements for the bad images:
+    catlist = getCatList(im, &Ncat);
+    measlist = getMeasList(im, &Nmeas);
+
+    for (i = 0; i < Nlist[im]; i++) {
+      m = mlist[im][i];
+      c = clist[im][i];
+      
+
+
+    // set up the basic catalog info
+    catalog.filename  = skylist[0].filename[i];
+    catalog.catformat = dvo_catalog_catformat (CATFORMAT);    // set the default catformat from config data
+    catalog.catmode   = dvo_catalog_catmode (CATMODE);        // set the default catmode from config data
+    catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
+    catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
+
+    if (!dvo_catalog_open (&catalog, skylist[0].regions[i], VERBOSE, "w")) {
+      fprintf (stderr, "ERROR: failure reading catalog %s\n", catalog.filename);
+      exit (1);
+    }
+    if (!catalog.Naves_disk) {
+      if (VERBOSE) fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
+      dvo_catalog_unlock (&catalog);
+      dvo_catalog_free (&catalog);
+      continue;
+    }
+
+    // match measurements with images
+    initImageBins (&catalog, 1);
+    findImages (&catalog, 1);
+
+    // update the detection coordinates using the new image parameters
+    UpdateMeasures (&catalog, 1);
+
+    freeImageBins (1);
+
+    // write the updated detections to disk
+    save_catalogs (&catalog, 1);
+  }
+  
+  return (TRUE);
+}
Index: /branches/eam_branches/relastro.20100326/src/ImageOps.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/ImageOps.c	(revision 27487)
+++ /branches/eam_branches/relastro.20100326/src/ImageOps.c	(revision 27488)
@@ -286,4 +286,7 @@
   Coords *moscoords, *imcoords;
 
+  // check if this image is bad and should be skipped
+  if (badCoords(im)) return;
+
   moscoords = NULL;
   mosaic = getMosaicForImage (im);
@@ -292,4 +295,10 @@
   }
   imcoords = &image[im].coords;
+
+  // accumulate the rms position offsets.  if this value, or any specific entry, is too
+  // large, we will reset the image to the original coords at the end of the analysis
+
+  dPos = 0.0;
+  nPos = 0;
 
   for (i = 0; i < Nlist[im]; i++) {
@@ -317,13 +326,16 @@
 
     if (fabs(catalog[c].measure[m].dR - dR) > 10.0) {
-      // XXXXX running into this still for last megacam exposure: wrong mosaic?
-      // ???? inconsistently hitting this????
       fprintf (stderr, "!");
-      // abort ();
+      setBadCoords (im); // report a failure for this image
+      return;
     }
     if (fabs(catalog[c].measure[m].dD - dD) > 10.0) {
       fprintf (stderr, "*");
-      // abort ();
-    }
+      setBadCoords (im); // report a failure for this image
+      return;
+    }
+
+    dPos += SQ(catalog[c].measure[m].dR - dR) + SQ(catalog[c].measure[m].dD - dD);
+    nPos ++;
 
     catalog[c].measure[m].dR = dR;
@@ -341,4 +353,7 @@
     }
   }
+
+  saveOffsets (dPos, nPos, im);
+
   return;
 }
Index: /branches/eam_branches/relastro.20100326/src/UpdateChips.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/UpdateChips.c	(revision 27487)
+++ /branches/eam_branches/relastro.20100326/src/UpdateChips.c	(revision 27488)
@@ -7,4 +7,5 @@
   Image *image;
   StarData *raw, *ref;
+  Coords *oldCoords;
 
   image = getimages (&Nimage);
@@ -24,7 +25,12 @@
     assert (Nraw == Nref);
 
+    saveCoords (&image[i].coords, i);
+
     // FitChip does iterative, clipped fitting
     fprintf (stderr, "image %lld : Nstars: %lld\n", (long long) i, (long long) Nraw);
-    FitChip (raw, ref, Nraw, &image[i].coords);
+    if (!FitChip (raw, ref, Nraw, &image[i].coords)) {
+      oldCoords = getCoords (i);
+      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
+    }
 
     free (raw);
Index: /branches/eam_branches/relastro.20100326/src/UpdateObjectOffsets.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/UpdateObjectOffsets.c	(revision 27487)
+++ /branches/eam_branches/relastro.20100326/src/UpdateObjectOffsets.c	(revision 27488)
@@ -1,3 +1,9 @@
 # include "relastro.h"
+
+// We run through each DVO catalog, updating the measures that come from the modified images
+// We need to watch for failures:
+// * in UpdateMeasures, in fixImageRaw, we track the cumulative offset for each image
+// * after all updates are done, we can check for any bad images and reset them to the
+//   original coordinates
 
 int UpdateObjectOffsets (SkyList *skylist) {
Index: /branches/eam_branches/relastro.20100326/src/fitpoly.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/fitpoly.c	(revision 27487)
+++ /branches/eam_branches/relastro.20100326/src/fitpoly.c	(revision 27488)
@@ -103,5 +103,5 @@
 
 /* convert the xsum,ysum,sum terms into vector,matrix and solve */
-void fit_eval (CoordFit *fit) {
+int fit_eval (CoordFit *fit) {
 
   int i, j, ix, iy, jx, jy;
@@ -147,5 +147,8 @@
   }	
 
-  dgaussjordan (matrix, vector, fit[0].Nelems, 2); 
+  status = dgaussjordan (matrix, vector, fit[0].Nelems, 2); 
+  if (!status) {
+    return (FALSE);
+  }
 
   for (i = 0; i < fit[0].Nelems; i++) {
@@ -166,4 +169,5 @@
   array_free (matrix, fit[0].Nelems);
   array_free (vector, fit[0].Nelems);
+  return (TRUE);
 }
 
@@ -271,5 +275,5 @@
 /* this should only apply to the polynomial, not the projection terms */
 /* compare with psastro supporting code */
-CoordFit *fit_apply_coords (CoordFit *fit, Coords *coords) {
+int fit_apply_coords (CoordFit *fit, Coords *coords) {
 
   double Xo, Yo, R1, R2;
@@ -281,5 +285,8 @@
   // L = pc1_1*cd1*(x - cp1) + pc1_2*cd2*(y - cp2) + ...
 
-  CoordsGetCenter (fit, 0.001, &Xo, &Yo);
+  if (!CoordsGetCenter (fit, 0.001, &Xo, &Yo)) {
+    fprintf (stderr, "failed to modify model\n");
+    return (FALSE);
+  }
   coords[0].crpix1 = Xo;
   coords[0].crpix2 = Yo;
@@ -329,6 +336,5 @@
   /* keep the order and type from initial values */
   
-  // XXX if desired in the future, return modfit (and free above)
-  return (NULL);
-}
-
+  return (TRUE);
+}
+
Index: /branches/eam_branches/relastro.20100326/src/mkpolyterm.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/mkpolyterm.c	(revision 27487)
+++ /branches/eam_branches/relastro.20100326/src/mkpolyterm.c	(revision 27488)
@@ -73,4 +73,5 @@
     if (dPos > dPosRef) {
       fprintf (stderr, "*** warning : non-convergence in model conversion (mkpolyterm.c;73) *** \n");
+      return FALSE;
     }
     array_free (alpha, 2);
Index: /branches/eam_branches/relastro.20100326/src/relastro.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/relastro.c	(revision 27487)
+++ /branches/eam_branches/relastro.20100326/src/relastro.c	(revision 27488)
@@ -39,4 +39,6 @@
   skylist = load_images (&db, &UserPatch);
   MARKTIME("load images: %f sec\n", dtime);
+
+  initCoords();
 
   /* load catalog data from region files : subselect high-quality measurements */
@@ -83,4 +85,7 @@
   UpdateObjectOffsets (skylist);
 
+  // iterate over catalogs to make detection coordinates consistant
+  FixProblemImages ();
+
   // save the updated image parameters
   dvo_image_update (&db, VERBOSE);
