Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/include/relastro.h
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/include/relastro.h	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/include/relastro.h	(revision 28720)
@@ -3,4 +3,5 @@
 # include <kapa.h>
 # include <signal.h>
+# include <assert.h>
 
 typedef enum {
@@ -76,4 +77,10 @@
   int    Nmeas;
 } StatType;
+
+# define MARKTIME(MSG,...) { \
+  float dtime; \
+  gettimeofday (&stop, (void *) NULL); \
+  dtime = DTIME (stop, start); \
+  fprintf (stderr, MSG, __VA_ARGS__); }
 
 /* global variables set in parameter file */
@@ -150,4 +157,5 @@
 int NY_MAP;
 double DPOS_MAX;
+double ADDSTAR_RADIUS;
 
 /*** relphot prototypes ***/
@@ -212,4 +220,6 @@
 void create_image_db (FITS_DB *db);
 void save_catalogs (Catalog *catalog, int Ncatalog);
+
+int reload_images (FITS_DB *db);
 
 int           main                PROTO((int argc, char **argv));
@@ -272,8 +282,8 @@
 double **poly2d_copy (double **poly, int Nx, int Ny);
 double poly2d_eval (double **poly, int Nx, int Ny, double x, double y);
-int fit_apply_coords (CoordFit *fit, Coords *coords);
+int fit_apply_coords (CoordFit *fit, Coords *coords, int keepRef);
 int CoordsGetCenter (CoordFit *fit, double tol, double *xo, double *yo);
 CoordFit *CoordsSetCenter (CoordFit *input, double Xo, double Yo);
-int FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
+int FitChip (StarData *raw, StarData *ref, int Nmatch, Image *image);
 void FitMosaic (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
 void FitSimple (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/ConfigInit.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/ConfigInit.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/ConfigInit.c	(revision 28720)
@@ -36,4 +36,5 @@
   GetConfig (config, "RELASTRO_MAP_NY",        "%d",  0, &NY_MAP);
   GetConfig (config, "RELASTRO_DPOS_MAX",      "%lf", 0, &DPOS_MAX);
+  GetConfig (config, "ADDSTAR_RADIUS",         "%lf", 0, &ADDSTAR_RADIUS);
 
   GetConfig (config, "GSCFILE",                "%s",  0, GSCFILE);
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/FitChip.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/FitChip.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/FitChip.c	(revision 28720)
@@ -3,13 +3,6 @@
 
 // XXX make these user parameters
-# define FIT_CHIP_MAX_ERROR 0.05
 # define FIT_CHIP_NITER     3
 # define FIT_CHIP_NSIGMA    3.0
-
-
-// XXX we should test if the fit is sufficiently constrained across the chip, or if the
-// new positions deviate too much from the old positions
-
-// XXX add visualization tools: per chip residual plots
 
 // XXX save the fit[0].Npts value in the image table?
@@ -17,5 +10,5 @@
 // XXX save measurements of the fit quality (scatter, chisq) in the image table
 
-int FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords) {
+int FitChip (StarData *raw, StarData *ref, int Nmatch, Image *image) {
 
   int i, Nscatter, Niter, skip;
@@ -29,5 +22,5 @@
     for (i = Nscatter = 0; i < Nmatch; i++) {
       if (raw[i].mask) continue;
-      if (isnan(raw[i].dMag) || raw[i].dMag > FIT_CHIP_MAX_ERROR) continue;
+      if (isnan(raw[i].dMag) || raw[i].dMag > SIGMA_LIM) continue;
 
       dL = raw[i].L - ref[i].L;
@@ -39,15 +32,20 @@
     }
 
-    // for a 2D Gaussian, 40% of the points are within 1 sigma; dRmax is ~ 3 sigma
-    dsort (values, Nscatter);
-    dRmax = FIT_CHIP_NSIGMA*values[(int)(0.40*Nscatter)];
-    relastroVisualPlotScatter(values, dRmax, Nscatter);
-    relastroVisualPlotRawRef(raw, ref, dRmax, Nmatch);
+    if (Nscatter > 5) {
+      // for a 2D Gaussian, 40% of the points are within 1 sigma; dRmax is ~ 3 sigma
+      // XXX this test is not sensible for Nscatter < XXX (5?)
+      dsort (values, Nscatter);
+      dRmax = FIT_CHIP_NSIGMA*values[(int)(0.40*Nscatter)];
+      relastroVisualPlotScatter(values, dRmax, Nscatter);
+      relastroVisualPlotRawRef(raw, ref, dRmax, Nmatch);
+    } else {
+      dRmax = 0;
+    }
 
     // fit the requested order polynomial
     if (CHIPORDER > 0) {
-      coords[0].Npolyterms = CHIPORDER;
+      image[0].coords.Npolyterms = CHIPORDER;
     }
-    fit = fit_init (coords[0].Npolyterms);
+    fit = fit_init (image[0].coords.Npolyterms);
 
     // generate the fit matches
@@ -56,5 +54,5 @@
 	continue;
       }
-      if (isnan(raw[i].dMag) || raw[i].dMag > FIT_CHIP_MAX_ERROR) {
+      if (isnan(raw[i].dMag) || raw[i].dMag > SIGMA_LIM) {
 	continue;
       }
@@ -64,5 +62,8 @@
       dM = raw[i].M - ref[i].M;
       dR = hypot (dL, dM);
-      if (dR > dRmax) continue;
+
+      // fprintf (stderr, "fit %f %f -> %f %f : %f %f (%f vs %f) wt: %f\n", raw[i].X, raw[i].Y, ref[i].L, ref[i].M, raw[i].L, raw[i].M, dR, dRmax, raw[i].dPos);
+
+      if ((dRmax > 0.0) && (dR > dRmax)) continue;
 
       fit_add (fit, raw[i].X, raw[i].Y, ref[i].L, ref[i].M, raw[i].dPos);
@@ -71,5 +72,5 @@
     // check if the fit has enough data points for the polynomial order
     skip = FALSE;
-    switch (coords[0].Npolyterms) {
+    switch (image[0].coords.Npolyterms) {
       case 0:
       case 1:
@@ -83,11 +84,12 @@
         break;
       default:
-        fprintf (stderr, "invalid chip order %d\n", coords[0].Npolyterms);
+        fprintf (stderr, "invalid chip order %d\n", image[0].coords.Npolyterms);
 	skip = TRUE;
     }
     if (skip) {
-      fprintf (stderr, "insufficient measurements (%d) for requested order (%d)\n", fit[0].Npts, coords[0].Npolyterms);
+      if (VERBOSE) fprintf (stderr, "insufficient measurements (%d) for requested order (%d)\n", fit[0].Npts, image[0].coords.Npolyterms);
       fit_free (fit);
       free (values);
+      image[0].flags |= ID_IMAGE_ASTROM_FEW;
       return FALSE;
     }
@@ -98,9 +100,11 @@
     if (!fit_eval (fit)) {
       fprintf (stderr, "failed to fit new model\n");
+      image[0].flags |= ID_IMAGE_ASTROM_FAIL;
       return FALSE;
     }
 
-    if (!fit_apply_coords (fit, coords)) {
+    if (!fit_apply_coords (fit, &image[0].coords, FALSE)) {
       fprintf (stderr, "failed to fit new model\n");
+      image[0].flags |= ID_IMAGE_ASTROM_FAIL;
       return FALSE;
     }
@@ -109,5 +113,5 @@
 
     for (i = 0; i < Nmatch; i++) {
-      XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, coords);
+      XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, &image[0].coords);
     }
 
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/FitMosaic.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/FitMosaic.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/FitMosaic.c	(revision 28720)
@@ -26,5 +26,5 @@
   }
   fit_eval (fit);
-  fit_apply_coords (fit, coords);
+  fit_apply_coords (fit, coords, TRUE);
   fit_free (fit);
 
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/FitSimple.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/FitSimple.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/FitSimple.c	(revision 28720)
@@ -26,5 +26,5 @@
   }
   fit_eval (fit);
-  fit_apply_coords (fit, coords);
+  fit_apply_coords (fit, coords, TRUE);
   fit_free (fit);
 
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/ImageOps.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/ImageOps.c	(revision 28720)
@@ -319,4 +319,15 @@
 }
 
+void dump_measures(Average *average, Measure *measure) {
+
+  off_t j, off;
+
+  for (j = 0; j < average[0].Nmeasure; j++) {
+    off = average[0].measureOffset + j;
+    fprintf (stderr, "%f, %f\n", measure[off].dR, measure[off].dD);
+  }
+  return;
+}
+
 // return StarData values for detections in the specified image, converting coordinates from the
 // chip positions: X,Y -> L,M -> P,Q -> R,D
@@ -325,5 +336,5 @@
   off_t i, m, c, n, nPos;
   double X, Y, L, M, P, Q, R, D, dR, dD;
-  double dPos;
+  double dPos, DPOS_MAX_ASEC;
 
   Mosaic *mosaic;
@@ -341,4 +352,10 @@
   }
   imcoords = &image[im].coords;
+
+  if (moscoords) {
+    DPOS_MAX_ASEC = 3600.0*DPOS_MAX*hypot(moscoords[0].cdelt1, moscoords[0].cdelt2);
+  } else {
+    DPOS_MAX_ASEC = 3600.0*DPOS_MAX*hypot(imcoords[0].cdelt1, imcoords[0].cdelt2);
+  }
 
   // accumulate the rms position offsets.  if this value, or any specific entry, is too
@@ -372,26 +389,26 @@
 
     // complain if the new location is far from the average location
-    // XXX warning or error here??
-    if (fabs(dR) > 2.0) {
-      fprintf (stderr, "!");
-      setBadCoords (im); // report a failure for this image
-      return;
-    }
-    if (fabs(dD) > 2.0) {
-      fprintf (stderr, "*");
-      setBadCoords (im); // report a failure for this image
-      return;
+    // NOTE: This should never happen, or our StarMap tests are not working
+    if (fabs(dR) > 1.5*ADDSTAR_RADIUS) {
+      fprintf (stderr, "measurement is far from average location (R): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
+      dump_measures (&catalog[c].average[n], catalog[c].measure);
+      abort ();
+    }
+    if (fabs(dD) > 1.5*ADDSTAR_RADIUS) {
+      fprintf (stderr, "measurement is far from average location (D): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
+      dump_measures (&catalog[c].average[n], catalog[c].measure);
+      abort ();
     }
 
     // complain if the new location is far from the old location
-    if (fabs(catalog[c].measure[m].dR - dR) > 2.0) {
-      fprintf (stderr, "@");
-      setBadCoords (im); // report a failure for this image
-      return;
-    }
-    if (fabs(catalog[c].measure[m].dD - dD) > 2.0) {
-      fprintf (stderr, "#");
-      setBadCoords (im); // report a failure for this image
-      return;
+    if (fabs(catalog[c].measure[m].dR - dR) > DPOS_MAX_ASEC) {
+      fprintf (stderr, "measurement is far from original location (R): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
+      dump_measures (&catalog[c].average[n], catalog[c].measure);
+      abort();
+    }
+    if (fabs(catalog[c].measure[m].dD - dD) > DPOS_MAX_ASEC) {
+      fprintf (stderr, "measurement is far from original location (D): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
+      dump_measures (&catalog[c].average[n], catalog[c].measure);
+      abort();
     }
 
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/StarMaps.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/StarMaps.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/StarMaps.c	(revision 28720)
@@ -20,6 +20,6 @@
   int  Nx;
   int  Ny;
-  int *stars;
-  StarMapPoint *points;
+  int *stars; // arrays to count the number of stars in each map bin
+  StarMapPoint *points; // test points generated based on map
   int Npoints;
 } StarMap;
@@ -45,4 +45,6 @@
 
   for (i = 0; i < Nimages; i++) {
+    starmap[i].Npoints = 0;
+    starmap[i].points = NULL;
     starmap[i].Nx = images[i].NX / NX_MAP;
     starmap[i].Ny = images[i].NY / NY_MAP;
@@ -58,4 +60,7 @@
   off_t i, N, Nimages;
   int xbin, ybin;
+  struct timeval start, stop;
+
+  gettimeofday (&start, (void *) NULL);
 
   images = getimages(&Nimages);
@@ -74,4 +79,5 @@
     starmap[N].stars[ybin*NX_MAP + xbin] ++;
   }
+  MARKTIME("assign stars to starmap bins: %f sec\n", dtime);
 
   return (TRUE);
@@ -88,4 +94,6 @@
   for (i = 0; i < Nimages; i++) {
     
+    assert (!starmap[i].points);
+
     ALLOCATE (starmap[i].points, StarMapPoint, NX_MAP*NY_MAP);
     starmap[i].Npoints = 0;
@@ -98,6 +106,6 @@
 
 	// set the pixel coordinates
-	point[0].X = ix * NX_MAP; // XXX fix 0.5 pixel offset
-	point[0].Y = iy * NY_MAP; // XXX fix 0.5 pixel offset
+	point[0].X = ix * starmap[i].Nx; // XXX fix 0.5 pixel offset
+	point[0].Y = iy * starmap[i].Nx; // XXX fix 0.5 pixel offset
 
 	// set the transformed coordinates
@@ -108,4 +116,6 @@
       }
     }
+
+    if (VERBOSE) fprintf (stderr, "starmap: %d points for image %s\n", starmap[i].Npoints, images[i].name);
   }
 
@@ -131,9 +141,10 @@
     starmap[N].points[i].dM = starmap[N].points[i].M - M;
 
-    dLmax = MAX(starmap[N].points[i].dL, dLmax);
-    dMmax = MAX(starmap[N].points[i].dM, dMmax);
+    dLmax = MAX(fabs(starmap[N].points[i].dL), dLmax);
+    dMmax = MAX(fabs(starmap[N].points[i].dM), dMmax);
   }
 
-  // XXX these need to be set somehow
+  if (VERBOSE) fprintf (stderr, "max deviations for %s using %d pts : %f, %f\n", images[N].name, starmap[N].Npoints, dLmax, dMmax);
+
   if (dLmax > DPOS_MAX) return (FALSE);
   if (dMmax > DPOS_MAX) return (FALSE);
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/UpdateChips.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/UpdateChips.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/UpdateChips.c	(revision 28720)
@@ -31,9 +31,8 @@
     // FitChip does iterative, clipped fitting
     // fprintf (stderr, "image "OFF_T_FMT" : Nstars: "OFF_T_FMT"\n",  i,  Nraw);
-    if (!FitChip (raw, ref, Nraw, &image[i].coords)) {
-      fprintf (stderr, "reject fit for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
+    if (!FitChip (raw, ref, Nraw, &image[i])) {
+      if (VERBOSE) fprintf (stderr, "reject fit for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
       oldCoords = getCoords (i);
       memcpy (&image[i].coords, oldCoords, sizeof(Coords));
-      // XXX need to set some flag to note failure here
       free (raw);
       free (ref);
@@ -42,8 +41,8 @@
 
     if (!checkStarMap (i)) {
-      fprintf (stderr, "fit diverges too much for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
+      if (VERBOSE) fprintf (stderr, "fit diverges too much for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
       oldCoords = getCoords (i);
       memcpy (&image[i].coords, oldCoords, sizeof(Coords));
-      // XXX need to set some flag to note failure here
+      image[i].flags |= ID_IMAGE_ASTROM_POOR;
     }
 
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/UpdateMeasures.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/UpdateMeasures.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/UpdateMeasures.c	(revision 28720)
@@ -5,4 +5,11 @@
   off_t i, Nimage;
   Image *image;
+
+  int badImage = 
+    ID_IMAGE_ASTROM_NOCAL | 
+    ID_IMAGE_ASTROM_POOR | 
+    ID_IMAGE_ASTROM_FAIL | 
+    ID_IMAGE_ASTROM_SKIP | 
+    ID_IMAGE_ASTROM_FEW;
 
   image = getimages (&Nimage);
@@ -14,4 +21,5 @@
 
     // skip images that have failed solutions (divergent or otherwise)
+    if (image[i].flags & badImage) continue;
 
     /* convert measure coordinates to raw entries */
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/bcatalog.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/bcatalog.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/bcatalog.c	(revision 28720)
@@ -45,5 +45,5 @@
       offset = catalog[0].average[i].measureOffset + j;
       
-      //filter objects based on user supplied criteria
+      // filter objects based on user supplied criteria
       if (!MeasFilterTest(&catalog[0].measure[offset])) {
 	catalog[0].measure[offset].dbFlags &= ~ID_MEAS_USED_CHIP;
@@ -51,5 +51,5 @@
       }
 
-      //filter out outliers
+      // filter out outliers
       if (FlagOutlier && (catalog[0].measure[offset].dbFlags & ID_MEAS_POOR_ASTROM)) {
 	catalog[0].measure[offset].dbFlags &= ~ID_MEAS_USED_CHIP;
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/fitpoly.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/fitpoly.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/fitpoly.c	(revision 28720)
@@ -110,4 +110,5 @@
   if (fit[0].Npts == 0) {
     fprintf (stderr, "warning: no valid pts\n");
+    return (FALSE);
   }
 
@@ -140,9 +141,9 @@
   }
 
-  for (i = 0; i < fit[0].Nelems; i++) {
+  for (i = 0; FALSE && (i < fit[0].Nelems); i++) {
     ix = i % fit[0].Nterms;
     iy = i / fit[0].Nterms;
-    // fprintf (stderr, "x2 : x^%dy^%d: %10.4g    y2 : x^%dy^%d: %10.4g \n", 
-    // ix, iy, vector[i][0], ix, iy, vector[i][1]);
+    fprintf (stderr, "x2 : x^%dy^%d: %10.4g    y2 : x^%dy^%d: %10.4g \n", 
+    ix, iy, vector[i][0], ix, iy, vector[i][1]);
   }	
   
@@ -151,9 +152,9 @@
   }
 
-  for (i = 0; i < fit[0].Nelems; i++) {
+  for (i = 0; FALSE && i < fit[0].Nelems; i++) {
     ix = i % fit[0].Nterms;
     iy = i / fit[0].Nterms;
-    // fprintf (stderr, "x2 : x^%dy^%d: %10.4g    y2 : x^%dy^%d: %10.4g \n", 
-    // ix, iy, vector[i][0], ix, iy, vector[i][1]);
+    fprintf (stderr, "x2 : x^%dy^%d: %10.4g    y2 : x^%dy^%d: %10.4g \n", 
+    ix, iy, vector[i][0], ix, iy, vector[i][1]);
   }	
 
@@ -274,25 +275,35 @@
 /* this should only apply to the polynomial, not the projection terms */
 /* compare with psastro supporting code */
-int fit_apply_coords (CoordFit *fit, Coords *coords) {
+int fit_apply_coords (CoordFit *fit, Coords *coords, int keepRef) {
 
   double Xo, Yo, R1, R2;
   CoordFit *modfit;
 
-  /* I have L,M = fit(X,Y). set corresponding terms for coords */
-
-  // L = a[0][0] + a[1][0]x^1 y^0 + a[0][1] x^0 y^1 + ...
-  // L = pc1_1*cd1*(x - cp1) + pc1_2*cd2*(y - cp2) + ...
-
-  if (!CoordsGetCenter (fit, 0.001, &Xo, &Yo)) {
-    fprintf (stderr, "failed to modify model\n");
-    return (FALSE);
-  }
-  coords[0].crpix1 = Xo;
-  coords[0].crpix2 = Yo;
-  
-  // resulting fit should have zero constant terms
-  modfit = CoordsSetCenter (fit, Xo, Yo);
-
-  /* we do not modify crval1,2: these are kept at the default values */
+  if (keepRef) {
+    // adjust crpix1,2 as needed:
+    // L = a[0][0] + a[1][0]x^1 y^0 + a[0][1] x^0 y^1 + ...
+    // L = pc1_1*cd1*(x - cp1) + pc1_2*cd2*(y - cp2) + ...
+
+    if (!CoordsGetCenter (fit, 0.001, &Xo, &Yo)) {
+      fprintf (stderr, "failed to modify model\n");
+      return (FALSE);
+    }
+    coords[0].crpix1 = Xo;
+    coords[0].crpix2 = Yo;
+    /* we do not modify crval1,2: these are kept at the default values */
+  
+    // resulting fit should have zero constant terms
+    modfit = CoordsSetCenter (fit, Xo, Yo);
+  } else {
+    // L = a[0][0] + a[1][0]x^1 y^0 + a[0][1] x^0 y^1 + ...
+    // P = cd1*x, Q = cd2*y
+    // L = pc1_1*P + pc1_2*Q + ...
+
+    /* modify crval1,2: these are kept at the default values */
+
+    coords[0].crpix1 = 0.0;
+    coords[0].crpix2 = 0.0;
+    modfit = fit;
+  }
 
   // set cdelt1, cdelt2
@@ -331,6 +342,10 @@
     coords[0].polyterms[6][1] = modfit[0].yfit[0][3]*R2*R2*R2;
   }
-
-  fit_free (modfit);
+  
+  if (keepRef) {
+    fit_free (modfit);
+  } else {
+    fit_apply (fit, &coords[0].crval1, &coords[0].crval2, coords[0].crpix1, coords[0].crpix2); 
+  }
   /* keep the order and type from initial values */
   
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/initialize.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/initialize.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/initialize.c	(revision 28720)
@@ -100,5 +100,5 @@
   initstats (STATMODE);
 
-  // IMAGE_BAD = ID_IMAGE_POOR | ID_IMAGE_FEW | ID_IMAGE_SKIP;
+  // IMAGE_BAD = ID_IMAGE_ASTROM_POOR | ID_IMAGE_ASTROM_FEW | ID_IMAGE_ASTROM_SKIP;
   // STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
   // MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR_ASTROM | ID_MEAS_SKIP_ASTROM | ID_MEAS_AREA;
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/load_images.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/load_images.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/load_images.c	(revision 28720)
@@ -33,4 +33,9 @@
   // convert database table to internal structure
   image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
+  if (!image) {
+      fprintf (stderr, "ERROR: failed to read images\n");
+      exit (2);
+  }
+
   MARKTIME("  convert image table: %f sec\n", dtime);
 
@@ -52,2 +57,19 @@
   return (skylist);
 }
+
+int reload_images (FITS_DB *db) {
+
+  Image     *image;
+  off_t     Nimage, Nx, i;
+  VTable    *vtable;
+
+  image = getimages (&Nimage);
+
+  vtable = &db[0].vtable;
+
+  gfits_scan (vtable[0].header, "NAXIS1", OFF_T_FMT, 1,  &Nx);
+  for (i = 0; i < Nimage; i++) {
+    memcpy (vtable[0].buffer[i], &image[i], Nx);
+  }
+  return (TRUE);
+}
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/mkpolyterm.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/mkpolyterm.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/mkpolyterm.c	(revision 28720)
@@ -66,4 +66,5 @@
       Xo -= beta[0][0];
       Yo -= beta[1][0];
+
       dPos = hypot(beta[0][0], beta[1][0]);
       if (i == 0) {
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/relastro.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/relastro.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/relastro.c	(revision 28720)
@@ -1,9 +1,3 @@
 # include "relastro.h"
-
-# define MARKTIME(MSG,...) { \
-  float dtime; \
-  gettimeofday (&stop, (void *) NULL); \
-  dtime = DTIME (stop, start); \
-  fprintf (stderr, MSG, __VA_ARGS__); }
 
 int main (int argc, char **argv) {
@@ -103,4 +97,6 @@
   freeImageBins (1);
 
+  reload_images (&db);
+    
   // iterate over catalogs to make detection coordinates consistant
   UpdateObjectOffsets (skylist);
Index: /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/select_images.c
===================================================================
--- /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/select_images.c	(revision 28719)
+++ /branches/eam_branches/ipp-20100621/Ohana/src/relastro/src/select_images.c	(revision 28720)
@@ -28,4 +28,9 @@
   double *RmaxSky;
   off_t *index;
+
+  int badImage = 
+    ID_IMAGE_ASTROM_POOR | 
+    ID_IMAGE_ASTROM_FAIL | 
+    ID_IMAGE_ASTROM_FEW;
 
   if (skylist[0].Nregions < 1) {
@@ -171,6 +176,6 @@
       image[nimage] = timage[i]; 
       /* always allow 'few' images to succeed, if possible */
-      if (image[nimage].flags & ID_IMAGE_FEW) { 
-	image[nimage].flags &= ~(ID_IMAGE_FEW | ID_IMAGE_POOR);
+      if (image[nimage].flags & ID_IMAGE_ASTROM_FEW) { 
+	image[nimage].flags &= ~ID_IMAGE_ASTROM_FEW;
       }
       if (RESET) {
@@ -178,5 +183,5 @@
 	assignMcal (&image[nimage], (double *) NULL, -1);
 	image[nimage].dMcal = NAN;
-	image[nimage].flags &= ~ID_IMAGE_POOR;
+	image[nimage].flags &= ~badImage;
       }
       line_number[nimage] = i;
