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 27489)
@@ -4,55 +4,64 @@
 // original coordinates and recalculate the positions
 
-int FixProblemImages () {
+int FixProblemImages (SkyList *skylist) {
 
-  off_t im, Nimage;
+  off_t i, Nimage;
   Image *image;
+  SkyList sublist;
+
+  // allocate so we can reallocate below
+  ALLOCATE (sublist.regions, SkyRegion *, 1);
+  ALLOCATE (sublist.filename, char *, 1);
 
   image = getimages (&Nimage);
 
+  // first check on the dPos reported for each image
   for (i = 0; i < Nimage; i++) {
-    
+    double dPosSum, dPos;
+    off_t nPos;
+    getOffsets (&dPosSum, &nPos, i);
+    dPos = sqrt(dPosSum / nPos);
+    if (dPos > 4.0) {
+      setBadCoords (i);
+    }
+  }
+
+  for (i = 0; i < Nimage; i++) {
+    int j, cat, Ncat, *catlist, Ncatlist;
+    Catalog *catalog;
+
     // 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);
+    // I need a list of the catalogs for this image
+    catlist = getCatlist(&Ncatlist, i);
 
-    for (i = 0; i < Nlist[im]; i++) {
-      m = mlist[im][i];
-      c = clist[im][i];
-      
+    // allocate Ncatlist skylist regions
+    REALLOCATE (sublist.regions, SkyRegion *, Ncatlist);
+    REALLOCATE (sublist.filename, char *, Ncatlist);
+    sublist.Nregions = Ncatlist;
+    sublist.ownElements = FALSE; // this list is only holding a view to the elements
 
-
-    // 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;
+    // copy the desired catalogs from skylist to skylistSubset
+    for (j = 0; j < Ncatlist; j++) {
+      cat = catlist[j];
+      sublist.filename[j] = skylist[0].filename[cat];
+      sublist.regions[j] = skylist[0].regions[cat];
     }
 
+    catalog = load_catalogs (&sublist, &Ncat, FALSE);
+    assert (Ncat == Ncatlist);
+
     // match measurements with images
-    initImageBins (&catalog, 1);
-    findImages (&catalog, 1);
+    initImageBins (catalog, Ncat, FALSE);
+    findImages (catalog, Ncat, FALSE);
 
     // update the detection coordinates using the new image parameters
-    UpdateMeasures (&catalog, 1);
+    resetImageRaw (catalog, Ncat, i);
 
-    freeImageBins (1);
+    freeImageBins (Ncat);
 
     // write the updated detections to disk
-    save_catalogs (&catalog, 1);
+    save_catalogs (catalog, Ncat);
   }
   
