Index: trunk/Ohana/src/relphot/include/relphot.h
===================================================================
--- trunk/Ohana/src/relphot/include/relphot.h	(revision 39642)
+++ trunk/Ohana/src/relphot/include/relphot.h	(revision 39643)
@@ -618,5 +618,5 @@
 
 void relphot_free (SkyTable *sky, SkyList *skylist);
-void freeImages (int freeImageData);
+void freeImages (char *dbImagePtr);
 void relphot_client_free (SkyTable *sky, SkyList *skylist);
 void BrightCatalogFree (BrightCatalog *bcatalog);
Index: trunk/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- trunk/Ohana/src/relphot/src/ImageOps.c	(revision 39642)
+++ trunk/Ohana/src/relphot/src/ImageOps.c	(revision 39643)
@@ -269,7 +269,6 @@
 }
 
-void freeImages (int freeImageData) {
-
-  if (freeImageData) FREE (image);
+void freeImages (char *dbImagePtr) {
+
   FREE (LineNumber);
 
@@ -280,4 +279,8 @@
   FREE (projectID);
   FREE (skycellID);
+
+  // we call gfits_db_free as well as this function.  sometimes those point at the same
+  // memory location, in which case we should only do the free once.
+  if ((void *) dbImagePtr != (void *) image) free (image);
 
   free_astrom_table();
@@ -662,4 +665,5 @@
     // only freeze the gpc1 chips
     if (FREEZE_IMAGES && isGPC1chip(image[i].photcode)) continue;
+    if (image[i].photcode == 0) continue; // skip the PHU images
 
     // UBERCAL image: if this is an ubercal image, set minUbercalDist to 0:
@@ -674,4 +678,9 @@
     off_t Nref = 0;  // total number of reference stars on the image
     int Nbright = 0; // number of stars to measure the bright-end scatter
+
+    if (N_onImage[i] == 0) {
+      fprintf (stderr, "image missing detections? %s %d "OFF_T_FMT"\n", image[i].name, image[i].nstar, N_onImage[i]);
+      continue;
+    }
 
     for (j = 0; j < N_onImage[i]; j++) {
Index: trunk/Ohana/src/relphot/src/relphot_client.c
===================================================================
--- trunk/Ohana/src/relphot/src/relphot_client.c	(revision 39642)
+++ trunk/Ohana/src/relphot/src/relphot_client.c	(revision 39643)
@@ -83,5 +83,5 @@
       FlatCorrectionTable *flatcorr = NULL;
       reload_catalogs (skylist, flatcorr, HOST_ID, HOSTDIR);
-      freeImages (TRUE);
+      freeImages ((char *)image);
       free (image);
       client_logger_message ("updated catalogs\n");
Index: trunk/Ohana/src/relphot/src/relphot_images.c
===================================================================
--- trunk/Ohana/src/relphot/src/relphot_images.c	(revision 39642)
+++ trunk/Ohana/src/relphot/src/relphot_images.c	(revision 39643)
@@ -263,6 +263,7 @@
   }
   dvo_image_unlock (&db); 
+
+  freeImages (db.ftable.buffer);
   gfits_db_free (&db);
-  freeImages (TRUE);
 
   return TRUE;
Index: trunk/Ohana/src/relphot/src/relphot_objects.c
===================================================================
--- trunk/Ohana/src/relphot/src/relphot_objects.c	(revision 39642)
+++ trunk/Ohana/src/relphot/src/relphot_objects.c	(revision 39643)
@@ -182,6 +182,6 @@
   }
 
+  freeImages(db.ftable.buffer);
   gfits_db_free (&db);
-  freeImages(TRUE);
 
   return (TRUE);
Index: trunk/Ohana/src/relphot/src/relphot_parallel_images.c
===================================================================
--- trunk/Ohana/src/relphot/src/relphot_parallel_images.c	(revision 39642)
+++ trunk/Ohana/src/relphot/src/relphot_parallel_images.c	(revision 39643)
@@ -170,5 +170,5 @@
   freeMosaicBins (Ncatalog, TRUE);
   freeGridBins (Ncatalog);
-  freeImages(TRUE);
+  freeImages((char *)image);
 
   SkyListFree(skylist);
Index: trunk/Ohana/src/relphot/src/relphot_parallel_regions.c
===================================================================
--- trunk/Ohana/src/relphot/src/relphot_parallel_regions.c	(revision 39642)
+++ trunk/Ohana/src/relphot/src/relphot_parallel_regions.c	(revision 39643)
@@ -88,6 +88,6 @@
   MARKTIME ("finished relphot -parallel-regions: %f sec total\n", dtime);
 
+  freeImages(db.ftable.buffer);
   gfits_db_free (&db);
-  freeImages(FALSE);
 
   SkyListFree(skylist);
Index: trunk/Ohana/src/relphot/src/select_images.c
===================================================================
--- trunk/Ohana/src/relphot/src/select_images.c	(revision 39642)
+++ trunk/Ohana/src/relphot/src/select_images.c	(revision 39643)
@@ -46,5 +46,5 @@
   double DmaxSkyRegion = region[0].Dmax;
 
-  double dD = 5.0;
+  double dD = CALIBRATE_STACKS_AND_WARPS ? 0.5 : 5.0;
   double dR = dD / cos(RAD_DEG*DminSkyRegion);
   RminSkyRegion -= dR;
@@ -80,13 +80,4 @@
     }
   }
-
-# if (0)
-  // XXX quick test of the dec bands:
-  FILE *fout = fopen ("dec.bands.dat", "w");
-  for (i = 0; i < NDecBands; i++) {
-    fprintf (fout, "%d %f : %f - %f\n", (int) i, dDecBand * i - 90.0, RminBand[i], RmaxBand[i]);
-  }
-  fclose (fout);
-# endif
 
   SkyRegionCoords *skycoords = NULL;
@@ -168,14 +159,22 @@
     }
 
-    // fprintf (ftest, "%f %f : %f %f : %d %d\n", RminImage, RmaxImage, DminImage, DmaxImage, timage[i].tzero, timage[i].photcode);
-
     // check that this image is even in range of the searched region
-    if (DminImage > DmaxSkyRegion) continue;
-    if (DmaxImage < DminSkyRegion) continue;
-    
-    // the sky region RA is defined to be 0 - 360.0
-    if (RminImage > RmaxSkyRegion) continue;
-    if (RmaxImage < RminSkyRegion) continue;
-
+    if (USE_FULL_OVERLAP) {
+      // for full overlap, the image must be completely inside the region of interest
+      if (DmaxImage > DmaxSkyRegion) continue;
+      if (DminImage < DminSkyRegion) continue;
+      
+      // the sky region RA is defined to be 0 - 360.0
+      if (RmaxImage > RmaxSkyRegion) continue;
+      if (RminImage < RminSkyRegion) continue;
+    } else {
+      if (DminImage > DmaxSkyRegion) continue;
+      if (DmaxImage < DminSkyRegion) continue;
+      
+      // the sky region RA is defined to be 0 - 360.0
+      if (RminImage > RmaxSkyRegion) continue;
+      if (RmaxImage < RminSkyRegion) continue;
+    }
+      
     // the above checks are only valid for the outermost region.  however, at a given
     // declination, the range of RA is limited by the actual catalog boundaries
@@ -204,4 +203,6 @@
       }
     }
+
+    // fprintf (ftest, "%f %f : %f %f : %d %d\n", RminImage, RmaxImage, DminImage, DmaxImage, timage[i].tzero, timage[i].photcode);
 
     // image overlaps region, keep it
