Index: trunk/Ohana/src/relphot/include/relphot.h
===================================================================
--- trunk/Ohana/src/relphot/include/relphot.h	(revision 39516)
+++ trunk/Ohana/src/relphot/include/relphot.h	(revision 39517)
@@ -639,2 +639,5 @@
 int GetStackSeq (Image *image, double Rstk, double Dstk, unsigned short photcode, float X, float Y);
 void sort_by_ra (double *R, double *D, int *I, int *S, int N);
+
+void RepairWarpMeasuresOpenLogfile ();
+void RepairWarpMeasuresCloseLogfile ();
Index: trunk/Ohana/src/relphot/src/RepairWarpMeasures.c
===================================================================
--- trunk/Ohana/src/relphot/src/RepairWarpMeasures.c	(revision 39516)
+++ trunk/Ohana/src/relphot/src/RepairWarpMeasures.c	(revision 39517)
@@ -1,3 +1,18 @@
 # include "relphot.h"
+
+FILE *logfile = NULL;
+
+void RepairWarpMeasuresOpenLogfile () {
+
+  char name[DVO_MAX_PATH];
+  snprintf (name, DVO_MAX_PATH, "%s/repair.warp.log", HOSTDIR);
+  logfile = fopen (name, "w");
+  if (!logfile) fprintf (stderr, "failed to open repair warp logfile %s\n", name);
+}
+
+void RepairWarpMeasuresCloseLogfile () {
+  if (!logfile) return;
+  fclose (logfile);
+}
 
 // XXX need to load_images first and generate some lookup tables
@@ -51,4 +66,5 @@
 	}
 	// the old imageID was wrong.  replace with the new one:
+	if (logfile) fprintf (logfile, "fix stack %10.6f %10.6f %5d %6.1f %6.1f 0x%08x %d -> %d\n", average->R, average->D, measure->photcode, measure->Xccd, measure->Yccd, measure->detID, measure->imageID, image[im].imageID);
 	measure->imageID = image[im].imageID;
       } else {
@@ -74,4 +90,5 @@
 	  }
 	  // the old imageID was wrong.  replace with the new one:
+	  if (logfile) fprintf (logfile, "fix stack %10.6f %10.6f %5d %6.1f %6.1f 0x%08x %d -> %d\n", average->R, average->D, measure->photcode, measure->Xccd, measure->Yccd, measure->detID, measure->imageID, image[im].imageID);
 	  measure->imageID = image[im].imageID;
 	}
Index: trunk/Ohana/src/relphot/src/relphot_objects.c
===================================================================
--- trunk/Ohana/src/relphot/src/relphot_objects.c	(revision 39516)
+++ trunk/Ohana/src/relphot/src/relphot_objects.c	(revision 39517)
@@ -15,4 +15,6 @@
 
   Catalog catalog;
+
+  INITTIME;
 
   // XXX need to decide how to determine PARALLEL mode...
@@ -32,8 +34,10 @@
   if (db.dbstate == LCK_EMPTY) Shutdown ("ERROR: No images in catalog %s (1)", db.filename);
   if (!dvo_image_load (&db, VERBOSE, FALSE)) Shutdown ("can't read image catalog %s", db.filename);
+  MARKTIME("read image table: %f sec\n", dtime);
   // the raw FITS data is freed by dvo_image_load, leaving only the Image structure data
 
   /* load regions and images based on specified sky patch (default depth) */
   load_images (&db, skylist, &UserPatch, TRUE);
+  MARKTIME("loaded images: %f sec\n", dtime);
 
   // load the ZP corrections here
@@ -42,5 +46,8 @@
   if (REPAIR_WARPS) { 
     FindWarpGroups ();
+    MARKTIME("setup warp groups: %f sec\n", dtime);
     MakeStackIndex ();
+    MARKTIME("setup stack index: %f sec\n", dtime);
+    RepairWarpMeasuresOpenLogfile ();
   }
 
@@ -129,4 +136,7 @@
       free_tiny_values(&catalog);
       dvo_catalog_free (&catalog);
+      freeImageBins (1, FALSE);
+      freeMosaicBins (1, FALSE);
+      freeGridBins (1);
       continue;
     }
@@ -159,5 +169,9 @@
   }
 
-  if (REPAIR_WARPS) FreeWarpGroups ();
+  if (REPAIR_WARPS) {
+    FreeWarpGroups ();
+    FreeStackGroups ();
+    RepairWarpMeasuresCloseLogfile ();
+  }
 
   gfits_db_free (&db);
Index: trunk/Ohana/src/relphot/src/select_images.c
===================================================================
--- trunk/Ohana/src/relphot/src/select_images.c	(revision 39516)
+++ trunk/Ohana/src/relphot/src/select_images.c	(revision 39517)
@@ -271,5 +271,5 @@
     }
   }
-  MARKTIME("finish image selection: %f sec\n", dtime);
+  MARKTIME("\nfinish image selection: %f sec\n", dtime);
 
   if (VERBOSE) fprintf (stderr, "found "OFF_T_FMT" images\n", nimage);
Index: trunk/Ohana/src/relphot/src/setMrelCatalog.c
===================================================================
--- trunk/Ohana/src/relphot/src/setMrelCatalog.c	(revision 39516)
+++ trunk/Ohana/src/relphot/src/setMrelCatalog.c	(revision 39517)
@@ -205,5 +205,5 @@
 
     // ** Choose the calibration (depends on the mode : do I have an image reference or not?) 
-    float Mcal = 0, Mmos = 0, Mgrid = 0;
+    float Mcal = 0.0, Mmos = 0.0, Mgrid = 0.0, Mflat = 0.0;
     off_t meas = measureOffset + k;
     if (getImageEntry (meas, cat) < 0) {
@@ -215,4 +215,5 @@
     } else {
       // getMcal_alt returns image[].Mcal modified by flatcorr(image.photom_map_id,x,y)
+      // NOTE: getMcal_alt does not include measure.Mflat
       Mcal  = getMcal_alt (meas, cat, flatcorr, measureT[k].Xccd, measureT[k].Yccd);
       if (isnan(Mcal))  SKIP_THIS_MEAS(Ncal);
@@ -221,4 +222,5 @@
       Mgrid = getMgrid (meas, cat);
       if (isnan(Mgrid)) SKIP_THIS_MEAS(Ngrid);
+      Mflat = isnan (measureT[k].Mflat) ? 0.0 : measureT[k].Mflat;
     }
 
@@ -244,5 +246,5 @@
       if (CHECK_VALID_MAG(Map, dMap)) {
 	int Nap = results->aperData[Nsec].Nlist;
-	results->aperData[Nsec].flxlist[Nap] = Map - Mcal - Mmos - Mgrid;
+	results->aperData[Nsec].flxlist[Nap] = Map + Mflat - Mcal - Mmos - Mgrid;
 	results->aperData[Nsec].errlist[Nap] = dMap;
 	results->aperData[Nsec].wgtlist[Nap] = useUbercalWeight ? UBERCAL_WEIGHT : 1.0;
@@ -259,5 +261,5 @@
       if (CHECK_VALID_MAG(Mkron, dMkron)) {
 	int Nkron = results->kronData[Nsec].Nlist;
-	results->kronData[Nsec].flxlist[Nkron] = Mkron - Mcal - Mmos - Mgrid;
+	results->kronData[Nsec].flxlist[Nkron] = Mkron + Mflat - Mcal - Mmos - Mgrid;
 	results->kronData[Nsec].errlist[Nkron] = dMkron;
 	results->kronData[Nsec].wgtlist[Nkron] = isUbercal ? UBERCAL_WEIGHT : 1.0;
@@ -272,5 +274,5 @@
     if (CHECK_VALID_MAG(Mpsf, dMpsf)) {
       int Npsf = results->psfData[Nsec].Nlist;
-      results->psfData[Nsec].flxlist[Npsf] = Mpsf - Mcal - Mmos - Mgrid;
+      results->psfData[Nsec].flxlist[Npsf] = Mpsf + Mflat - Mcal - Mmos - Mgrid;
       results->psfData[Nsec].errlist[Npsf] = dMpsf;
       results->psfData[Nsec].wgtlist[Npsf] = isUbercal ? UBERCAL_WEIGHT : 1.0;
Index: trunk/Ohana/src/relphot/src/setMrelFinal.c
===================================================================
--- trunk/Ohana/src/relphot/src/setMrelFinal.c	(revision 39516)
+++ trunk/Ohana/src/relphot/src/setMrelFinal.c	(revision 39517)
@@ -104,6 +104,6 @@
   ALLOCATE (catalog[0].measureRank, char, catalog[0].Nmeasure);
   setMeasureRank (catalog);
-  setMrelOutput (catalog, 1, flatcorr);
-  if (!simpleAverage) setMcalOutput (catalog, 1, flatcorr);
+  setMrelOutput (catalog, 1, flatcorr); // sets the values secfilt.M = <measure.M + measure.Mflat - image.Mcal>
+  setMcalOutput (catalog, 1, flatcorr); // sets measure.Mcal = image.Mcal
 
   /* clear ID_OBJ_POOR, ID_OBJ_FEW values before writing ??? */
