Index: /trunk/psphot/src/find_defects.c
===================================================================
--- /trunk/psphot/src/find_defects.c	(revision 4187)
+++ /trunk/psphot/src/find_defects.c	(revision 4188)
@@ -10,5 +10,7 @@
   float gradx, grady, gradlim;
   float N0, N1, peak, sky;
-  int x, y;
+  int x, y, Nzap;
+
+  psTimerStart ("psphot");
 
   // measure the x and y max gradients
@@ -56,4 +58,5 @@
       
   // search for pixels with (dfdy > grady) or (dfdx > gradx) allowing for Nsigma outliers
+  Nzap = 0;
   for (int i = 0; i < sources->n; i++) {
     psSource *source = sources->data[i];
@@ -66,4 +69,5 @@
     sky  = source->moments->Sky;
     peak = source->moments->Peak - sky;
+    if (peak < 0) continue;
     if (peak < peak*PS_MIN(gradx, grady) + NSIGMA*sqrt(2*sky + peak)) continue;
     psTrace (".psphot.defects", 4, "peak: %f, SN: %f\n", peak, source->moments->SN);
@@ -74,13 +78,14 @@
 	N0 = image->data.F32[iy][ix];
 	N1 = image->data.F32[iy][ix + 1];
-	gradlim = fabs(N1 - N0 - NSIGMA*(sqrt(N1) + sqrt(N0))) / peak;
-	// XXX this line is wrong: we should use the appropriate noise from the noise map
+	gradlim = fabs(N1 - N0) - NSIGMA*(sqrt(N1 + N0));
+	// gradient - N*sigma
+	gradlim = gradlim / peak;
 	
 	// if gradient is too large, zap the high pixel
 	if (gradlim < gradx) continue;
-	if (N1 > N0) {
+	if (N0 > N1) {
+	  zap->data.U8[iy][ix] = 1;
+	} else {
 	  zap->data.U8[iy][ix + 1] = 1;
-	} else {
-	  zap->data.U8[iy][ix] = 1;
 	}
       }
@@ -92,18 +97,17 @@
 	N0 = image->data.F32[iy][ix];
 	N1 = image->data.F32[iy + 1][ix];
-	gradlim = fabs(N1 - N0 - NSIGMA*(sqrt(N1) + sqrt(N0))) / peak;
-	// XXX this line is wrong: we should use the appropriate noise from the noise map
+	gradlim = fabs(N1 - N0) - NSIGMA*(sqrt(N1 + N0));
+	gradlim = gradlim / peak;
 	
 	// if gradient is too large, zap the high pixel
 	// note the inefficiency: we are re-testing known zapped entries
-	if (gradlim > grady) continue;
-	if (N1 > N0) {
+	if (gradlim < grady) continue;
+	if (N0 > N1) {
+	  zap->data.U8[iy][ix] = 1;
+	} else {
 	  zap->data.U8[iy + 1][ix] = 1;
-	} else {
-	  zap->data.U8[iy][ix] = 1;
 	}	    
       }
     }
-    psFree (zap);
 
     // mask & zero the zapped pixels
@@ -114,8 +118,12 @@
 	y = iy + image->row0;
 	zapmask->data.F32[y][x] = 1;
+	Nzap ++;
       }
     }
+    psFree (zap);
 
   }
+  psLogMsg ("psphot", 3, "zap: %f sec, %d pixels\n", psTimerMark ("psphot"), Nzap);
+
   return (true);
 }
Index: /trunk/psphot/src/psUtils.c
===================================================================
--- /trunk/psphot/src/psUtils.c	(revision 4187)
+++ /trunk/psphot/src/psUtils.c	(revision 4188)
@@ -1,5 +1,3 @@
 # include "psphot.h"
-
-static psHash *timers = NULL;
 
 // set actual region based on image parameters
@@ -21,4 +19,5 @@
 }
 
+static psHash *timers = NULL;
 
 // start/restart a named timer
@@ -110,4 +109,32 @@
     }
     return (TRUE);
+}
+
+// find the location of the specified argument
+int psArgumentGet (int argc, char **argv, char *arg) {
+
+    int i;
+
+    for (i = 0; i < argc; i++) {
+	if (!strcmp(argv[i], arg))
+	    return (i);
+    }
+  
+    return ((int)NULL);
+}
+
+// remove the specified argument (by location)
+int psArgumentRemove (int N, int *argc, char **argv) {
+
+    int i;
+
+    if ((N != (int)NULL) && (N != 0)) {
+	(*argc)--;
+	for (i = N; i < *argc; i++) {
+	    argv[i] = argv[i+1];
+	}
+    }
+
+    return (N);
 }
 
Index: /trunk/psphot/src/psphot.c
===================================================================
--- /trunk/psphot/src/psphot.c	(revision 4187)
+++ /trunk/psphot/src/psphot.c	(revision 4188)
@@ -34,9 +34,11 @@
     psf = choose_psf_model (image, config, sources);
 
-    psImage *zap = psImageAlloc (image->numCols, image->numRows, PS_TYPE_F32);
-    psImageInit (zap, 0);
-    find_defects (zap, sources, config, psf);
-    dump_image (zap, argv[2]);
-    exit (0);
+    if (0) { 
+      psImage *zap = psImageAlloc (image->numCols, image->numRows, PS_TYPE_F32);
+      psImageInit (zap, 0);
+      find_defects (zap, sources, config, psf);
+      dump_image (zap, argv[2]);
+      exit (0);
+    }
 
     // test PSF on all except SATURATE and DEFECT (artifacts)
