Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/Makefile
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/Makefile	(revision 37683)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/Makefile	(revision 37684)
@@ -60,5 +60,4 @@
 $(SRC)/CoordOps.$(ARCH).o            \
 $(SRC)/extra.$(ARCH).o            \
-$(SRC)/FixProblemImages.$(ARCH).o    \
 $(SRC)/StarMaps.$(ARCH).o    \
 $(SRC)/high_speed_catalogs.$(ARCH).o  \
@@ -125,5 +124,4 @@
 $(SRC)/relastro_objects.$(ARCH).o    \
 $(SRC)/save_catalogs.$(ARCH).o       \
-$(SRC)/CoordOps.$(ARCH).o            \
 $(SRC)/extra.$(ARCH).o            \
 $(SRC)/high_speed_catalogs.$(ARCH).o  \
@@ -141,13 +139,4 @@
 $(SRC)/BrightCatalog.$(ARCH).o 
 
-#$(SRC)/FitChip.$(ARCH).o             \
-#$(SRC)/FitMosaic.$(ARCH).o           \
-#$(SRC)/UpdateChips.$(ARCH).o         \
-#$(SRC)/UpdateMosaic.$(ARCH).o        \
-#$(SRC)/UpdateSimple.$(ARCH).o        \
-#$(SRC)/write_coords.$(ARCH).o        \
-#$(SRC)/FixProblemImages.$(ARCH).o    \
-#$(SRC)/StarMaps.$(ARCH).o    \
-
 $(RELASTRO_CLIENT): $(INC)/relastro.h
 $(BIN)/relastro_client.$(ARCH): $(RELASTRO_CLIENT)
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h	(revision 37683)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h	(revision 37684)
@@ -489,15 +489,8 @@
 int relastroGetVisual(void);
 
-int FixProblemImages (SkyList *skylist);
 int *getCatlist (int *N, off_t im);
 
-void initCoords (void);
-void getOffsets (double *dPos, off_t *nPos, off_t N);
-void saveOffsets (double dPos, off_t nPos, off_t N);
-void setBadCoords (off_t N);
-int badCoords (off_t N);
-Coords *getCoords (off_t N);
-int saveCoords (Coords *coords, off_t N);
-void resetImageRaw (Catalog *catalog, int Ncatalog, off_t im);
+void SaveCoords (Coords *tgt, Coords *src);
+void RestoreCoords (Coords *tgt, Coords *src, Image *image);
 
 int high_speed_catalogs (SkyTable *sky, SkyList *skylist, int hostID, char *hostpath);
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FitChip.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FitChip.c	(revision 37683)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FitChip.c	(revision 37684)
@@ -14,5 +14,5 @@
 
   for (Niter = 0; Niter < IMFIT_CLIP_NITER; Niter ++) {
-    
+
     // measure the scatter for the unmarked (good) measurements with dM < limit
     // SIGMA_LIM here is redundant with ImageOps.c:915
@@ -119,7 +119,11 @@
 	lockUpdateChips ();
 	AstromOffsetTable *table = get_astrom_table ();
-	AstromOffsetTableNewMap(table, order_use, image);
+	AstromOffsetTableNewMap(table, order_use, order_use, image);
 	unlockUpdateChips ();
       }
+      // XXX Need to check / update the order of the map here.  if we already have a map, it might have been
+      // allocated with too small arrays
+      AstromOffsetMapSetOrder (image[0].coords.offsetMap, order_use, order_use, image);
+      image[0].coords.offsetMap->keep = TRUE; // if we are trying or re-trying this map, we should keep it unless it fails
       fit_map (image[0].coords.offsetMap, raw, ref, Nmatch);
       image[0].coords.Npolyterms = -1;
@@ -262,19 +266,19 @@
    L,M -> X,Y (polynomial transformation : WRP)
 
-Some details about this function:
-
-- the initial value of the clipping radius is set based on the distribution of the dR
-  values for the bright sources.
-
-- NITER clipping passes are performed
-
-- the per-star astrometric errors are included in the fit.  The photcode table controls
-  whether only the reported positional errors are used, or if the magnitudes errors are
-  scaled to determine the error, and if there is a systematic floor for all sources.
-
-- input detections are pre-filtered on the basis of the photFlags, etc, in bcatalog
-
-- outlying detections are clipped in the iterative passes, but the clipped detections are
-  not recorded
+   Some details about this function:
+
+   - the initial value of the clipping radius is set based on the distribution of the dR
+   values for the bright sources.
+
+   - NITER clipping passes are performed
+
+   - the per-star astrometric errors are included in the fit.  The photcode table controls
+   whether only the reported positional errors are used, or if the magnitudes errors are
+   scaled to determine the error, and if there is a systematic floor for all sources.
+
+   - input detections are pre-filtered on the basis of the photFlags, etc, in bcatalog
+
+   - outlying detections are clipped in the iterative passes, but the clipped detections are
+   not recorded
 
 */
@@ -282,13 +286,13 @@
 /* example using fit_apply() :
 
-  f = fopen ("test3.dat", "w");
-
-  // apply new coords to raw (X,Y -> L,M)
-  for (i = 0; i < Nmatch; i++) {
-    fprintf (f, "%f %f  %f %f  ", raw[i].X, raw[i].Y, raw[i].L, raw[i].M);
-    fit_apply (newfit, &L1, &M1, raw[i].X - coords[0].crpix1, raw[i].Y - coords[0].crpix2);
-    fprintf (f, "%f %f\n", L1, M1);
-  }
-  fclose (f);
+   f = fopen ("test3.dat", "w");
+
+   // apply new coords to raw (X,Y -> L,M)
+   for (i = 0; i < Nmatch; i++) {
+   fprintf (f, "%f %f  %f %f  ", raw[i].X, raw[i].Y, raw[i].L, raw[i].M);
+   fit_apply (newfit, &L1, &M1, raw[i].X - coords[0].crpix1, raw[i].Y - coords[0].crpix2);
+   fprintf (f, "%f %f\n", L1, M1);
+   }
+   fclose (f);
 */
 
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ImageOps.c	(revision 37683)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ImageOps.c	(revision 37684)
@@ -336,15 +336,4 @@
 # endif
 
-/*
-Coords *getCoords (off_t meas, int cat) {
-
-  off_t i;
-
-  i = MeasureToImage[cat][meas];
-  if (i == -1) return (NULL);
-  return (&image[i].coords);
-}
-*/
-
 float getColorBlue (off_t meas, int cat) {
 
@@ -434,7 +423,4 @@
   Mosaic *mosaic;
   Coords *moscoords, *imcoords;
-
-  // check if this image is bad and should be skipped
-  if (badCoords(im)) return;
 
   // WRP images need to have an associated mosaic
@@ -598,5 +584,4 @@
   int Noff = NoffRAave + NoffDECave + NoffRAori + NoffDECori;
   if (VERBOSE2 && (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);
 
   return;
@@ -605,86 +590,4 @@
 void printNcatTotal () {
   fprintf (stderr, "NcatTotal: %d\n", NcatTotal);
-}
-
-// return StarData values for detections in the specified image, converting coordinates from the
-// chip positions: X,Y -> L,M -> P,Q -> R,D
-void resetImageRaw (Catalog *catalog, int Ncatalog, off_t im) {
-
-  off_t i, m, c, n;
-  double X, Y, L, M, P, Q, R, D;
-
-  Mosaic *mosaic;
-  Coords *moscoords, *imcoords, *oldcoords;
-
-  fprintf (stderr, "fix resetImageRaw wrt MeasureTiny\n");
-  abort();
-
-  // check if this image is bad and should be skipped
-  if (!badCoords(im)) {
-    fprintf (stderr, "ERROR: inconsistent result?");
-    abort();
-  }
-
-  // replace the current coords with the old coords:
-  oldcoords = getCoords (im);
-  memcpy (&image[im].coords, oldcoords, sizeof(Coords));
-
-  // WRP images need to have an associated mosaic
-  moscoords = NULL;
-  if (!strcmp(&image[im].coords.ctype[4], "-WRP")) {
-    mosaic = getMosaicForImage (im);
-    if (mosaic == NULL) return;  // if we cannot find the associated image, skip it
-    moscoords = &mosaic[0].coords;
-  }
-  imcoords = &image[im].coords;
-
-  for (i = 0; i < N_onImage[im]; i++) {
-    m = ImageToMeasure[im][i];
-    c = ImageToCatalog[im][i];
-
-    // XXX unclear if this should use Measure or MeasureTiny; it is only called by FixProblemImages, which has not been updated
-    Measure *measure = &catalog[c].measure[m];
-
-    X = measure[0].Xccd;
-    Y = measure[0].Yccd;
-    if (USE_FIXED_PIXCOORDS) {
-      if (isfinite(measure[0].Xfix) && isfinite(measure[0].Yfix)) {
-	float dX = measure[0].Xfix - measure[0].Xccd;
-	float dY = measure[0].Yfix - measure[0].Yccd;
-	if (hypot(dX,dY) < 2.0) {
-	  X = measure[0].Xfix;
-	  Y = measure[0].Yfix;
-	} 
-      }
-    }
-    n = measure[0].averef;
-
-    if (moscoords == NULL) {
-      // this is a Simple image (not a mosaic)
-      // note that for a Simple image, L,M = P,Q
-      XY_to_LM (&L, &M, X, Y, imcoords);
-      LM_to_RD (&R, &D, L, M, imcoords);
-    } else {
-      XY_to_LM (&L, &M, X, Y, imcoords);
-      XY_to_LM (&P, &Q, L, M, moscoords);
-      LM_to_RD (&R, &D, P, Q, moscoords);
-    }
-    measure[0].R = R;
-    measure[0].D = D;
-
-    float dRoff = dvoOffsetR(measure, &catalog[c].average[n]);
-
-    if (dRoff > +180.0*3600.0) {
-      // average on high end of boundary, move star up
-      measure[0].R += 360.0;
-      dRoff -= 360.0*3600.0;
-    }
-    if (dRoff < -180.0*3600.0) {
-      measure[0].R -= 360.0;
-      dRoff += 360.0*3600.0;
-    }
-  }
-
-  return;
 }
 
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateChips.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateChips.c	(revision 37683)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateChips.c	(revision 37684)
@@ -36,5 +36,4 @@
   off_t i, Nraw, Nref, nFitAstr;
   StarData *raw, *ref;
-  Coords *oldCoords;
   float dXpixSys, dYpixSys;
   double *Ro, *Do;
@@ -96,5 +95,7 @@
 
     // save these in case of failure
-    saveCoords (&image[i].coords, i);
+    Coords oldCoords;
+    SaveCoords (&oldCoords, &image[i].coords);
+
     dXpixSys = image[i].dXpixSys;
     dYpixSys = image[i].dYpixSys;
@@ -106,7 +107,6 @@
       if (VERBOSE) fprintf (stderr, "reject fit for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT" of %d\n", image[i].name,  i,  Nraw, image[i].nstar);
 
-      // restore status quo ante
-      oldCoords = getCoords (i);
-      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
+      // restore status quo ante (replace truMap with tmpMap)
+      RestoreCoords (&image[i].coords, &oldCoords, &image[i]);
       image[i].dXpixSys = dXpixSys;
       image[i].dYpixSys = dYpixSys; 
@@ -123,7 +123,7 @@
     if (!checkStarMap (i)) {
       if (VERBOSE) fprintf (stderr, "fit diverges too much for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
-      // restore status quo ante
-      oldCoords = getCoords (i);
-      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
+
+      // restore status quo ante (replace truMap with tmpMap)
+      RestoreCoords (&image[i].coords, &oldCoords, &image[i]);
       image[i].dXpixSys = dXpixSys;
       image[i].dYpixSys = dYpixSys; 
@@ -139,5 +139,7 @@
     } 
 
-    // apply the modified R,D back to the measures
+    // Apply the modified coords back to the measure.R,D.  Note that raw.R,D, ref.L,M, etc
+    // are all automatically updated in this block because they are re-generated from
+    // image.coords on each pass.
     setImageRaw (catalog, Ncatalog, i, raw, Nraw, MODE_MOSAIC);
     if (USE_GALAXY_MODEL) {
@@ -247,5 +249,4 @@
   off_t Nraw, Nref, nFitAstr;
   StarData *raw, *ref;
-  Coords *oldCoords;
   float dXpixSys, dYpixSys;
 
@@ -294,5 +295,6 @@
 
     // save these in case of failure
-    saveCoords (&image[i].coords, i);
+    Coords oldCoords;
+    SaveCoords (&oldCoords, &image[i].coords);
     dXpixSys = image[i].dXpixSys;
     dYpixSys = image[i].dYpixSys;
@@ -304,7 +306,6 @@
       if (VERBOSE) fprintf (stderr, "reject fit for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT" of %d\n", image[i].name,  i,  Nraw, image[i].nstar);
 
-      // restore status quo ante
-      oldCoords = getCoords (i);
-      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
+      // restore status quo ante (replace truMap with tmpMap)
+      RestoreCoords (&image[i].coords, &oldCoords, &image[i]);
       image[i].dXpixSys = dXpixSys;
       image[i].dYpixSys = dYpixSys; 
@@ -321,7 +322,7 @@
     if (!checkStarMap (i)) {
       if (VERBOSE) fprintf (stderr, "fit diverges too much for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
-      // restore status quo ante
-      oldCoords = getCoords (i);
-      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
+
+      // restore status quo ante (replace truMap with tmpMap)
+      RestoreCoords (&image[i].coords, &oldCoords, &image[i]);
       image[i].dXpixSys = dXpixSys;
       image[i].dYpixSys = dYpixSys; 
@@ -522,2 +523,46 @@
 }
 
+// save the coords and offset map in src at tgt
+void SaveCoords (Coords *tgt, Coords *src) {
+  myAssert (tgt, "oops");
+  myAssert (src, "oops");
+  CopyCoords (tgt, src); // src retains a pointer to the AstromOffsetMap table, to be saved
+  tgt->offsetMap = AstromOffsetMapCopy(src->offsetMap); // oldCoords now saves the old values in a new structure
+}
+
+// restore the coords and offset map at tgt from src
+void RestoreCoords (Coords *tgt, Coords *src, Image *image) {
+
+  AstromOffsetMap *truMap = tgt->offsetMap;
+  AstromOffsetMap *tmpMap = src->offsetMap;
+
+  // copy the coords structure data first, then fix the offsetMap pointers
+  CopyCoords (tgt, src);
+
+  // we want to keep the old solution, which is (maybe) a pointer to a stand-alone
+  // map.  the image has a bad solution, but it is a pointer to the map in the I/O
+  // table.  those values will be saved on exit.
+
+  if (!tmpMap && !truMap) return;
+
+  if (tmpMap && truMap) {
+    AstromOffsetMapSetOrder (truMap, tmpMap->Nx, tmpMap->Ny, image);
+    AstromOffsetMapCopyData (truMap, tmpMap);
+    return;
+  }
+
+  if (tmpMap && !truMap) {
+    // is this even possible?
+    lockUpdateChips ();
+    AstromOffsetTable *table = get_astrom_table ();
+    AstromOffsetTableNewMap(table, tmpMap->Nx, tmpMap->Ny, image);  // registers the map with the image
+    unlockUpdateChips ();
+    AstromOffsetMapCopyData (truMap, tmpMap);
+    return;
+  }
+  if (!tmpMap && truMap) {
+    truMap->keep = FALSE;
+    return;
+  }
+  myAbort ("oops");
+}
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_images.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_images.c	(revision 37683)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_images.c	(revision 37684)
@@ -23,9 +23,5 @@
   MARKTIME("load images: %f sec\n", dtime);
 
-  initCoords();
-
   /* load catalog data from region files : subselect high-quality measurements */
-  // XXX pass in the image table
-  // XXX who carries the image grid?
 
   // photcodesKeep is used here to allow measurements from the images being calibrated
@@ -119,7 +115,4 @@
   UpdateObjectOffsets (skylist, 0, NULL);
 
-  // iterate over catalogs to make detection coordinates consistant
-  // FixProblemImages (skylist);
-
   if (!PARALLEL) {
     // save the updated image parameters
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_parallel_images.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_parallel_images.c	(revision 37683)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_parallel_images.c	(revision 37684)
@@ -56,6 +56,4 @@
   SkyTableSetFilenames (sky, CATDIR, "cpt");
   SkyList *skylist = SkyListByBounds (sky, -1, UserPatch.Rmin, UserPatch.Rmax, UserPatch.Dmin, UserPatch.Dmax);
-
-  initCoords();
 
   /* load catalog data from region files (hostID is 0 since we are not a client */
