Index: trunk/psphot/src/psphotApResid.c
===================================================================
--- trunk/psphot/src/psphotApResid.c	(revision 20471)
+++ trunk/psphot/src/psphotApResid.c	(revision 20581)
@@ -190,5 +190,5 @@
     psVector *dMagSys = (psVector *) psBinaryOp (NULL, (void *) dMag, "*", (void *) psScalarAlloc(errorScale, PS_TYPE_F32));
 
-    if (psTraceGetLevel("psphot") >= 5) {
+    if (psTraceGetLevel("psphot") >= 2) {
 	FILE *dumpFile = fopen ("apresid.dat", "w");
 	for (int i = 0; i < xPos->n; i++) {
@@ -289,4 +289,5 @@
 	    dMo->data.F32[i] = statsM->sampleMean;
 	    dRo->data.F32[i] = statsS->robustStdev / statsM->sampleMean;
+	    fprintf (stderr, "%d (%d) : sys: %f, phot: %f, rat: %f\n", i, j, dSo->data.F32[i], dMo->data.F32[i], dRo->data.F32[i]);
 	} else {
 	    dSo->data.F32[i] = NAN;
@@ -339,6 +340,7 @@
 	Nx = PS_MAX (1, Nx);
     }
-
-    if (Npsf < 3*Nx*Ny) {
+    
+    // require at least 10 stars per spatial bin
+    if (Npsf < 10*Nx*Ny) {
 	return false;
     }
@@ -356,6 +358,12 @@
     psf->ApTrend = pmTrend2DAlloc (PM_TREND_MAP, readout->image, Nx, Ny, stats);
 
+    // XXX somewhat arbitrary: soften the errors so the few bright stars do not totally dominate:
+    psVector *dMagSoft = psVectorAlloc (dMag->n, PS_TYPE_F32);
+    for (int i = 0; i < dMag->n; i++) {
+	dMagSoft->data.F32[i] = hypot(dMag->data.F32[i], 0.01);
+    }
+
     // XXX test for errors here
-    pmTrend2DFit (psf->ApTrend, mask, 0xff, xPos, yPos, apResid, dMag);
+    pmTrend2DFit (psf->ApTrend, mask, 0xff, xPos, yPos, apResid, dMagSoft);
     
     // construct the fitted values and the residuals
@@ -373,4 +381,5 @@
 
     psFree (stats);
+    psFree (dMagSoft);
     psFree (apResidFit);
     psFree (apResidRes);
