Index: trunk/psphot/src/psphotApResid.c
===================================================================
--- trunk/psphot/src/psphotApResid.c	(revision 9568)
+++ trunk/psphot/src/psphotApResid.c	(revision 9734)
@@ -4,5 +4,5 @@
 
 // measure the aperture residual statistics
-bool psphotApResid (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf) { 
+bool psphotApResid (pmReadout *readout, psArray *sources, psMetadata *recipe, pmPSF *psf) {
 
     int Nfail = 0;
@@ -18,5 +18,5 @@
     float MAX_AP_OFFSET = psMetadataLookupF32 (&status, recipe, "MAX_AP_OFFSET");
 
-    // set limits on the aperture magnitudes 
+    // set limits on the aperture magnitudes
     pmSourceMagnitudesInit (recipe);
 
@@ -25,74 +25,73 @@
     psf->growth = pmGrowthCurveAlloc (3.0, 100.0, REF_RADIUS);
     psphotGrowthCurve (readout, psf);
-    
-    psVector *mask    = psVectorAlloc (300, PS_TYPE_U8);
-    psVector *xPos    = psVectorAlloc (300, PS_TYPE_F64);
-    psVector *yPos    = psVectorAlloc (300, PS_TYPE_F64);
-    psVector *flux    = psVectorAlloc (300, PS_TYPE_F64);
-    psVector *r2rflux = psVectorAlloc (300, PS_TYPE_F64);
-    psVector *apResid = psVectorAlloc (300, PS_TYPE_F64);
-    psVector *dMag    = psVectorAlloc (300, PS_TYPE_F64);
-    mask->n = xPos->n = yPos->n = flux->n = r2rflux->n = apResid->n = dMag->n = 0;
+
+    psVector *mask    = psVectorAllocEmpty (300, PS_TYPE_U8);
+    psVector *xPos    = psVectorAllocEmpty (300, PS_TYPE_F64);
+    psVector *yPos    = psVectorAllocEmpty (300, PS_TYPE_F64);
+    psVector *flux    = psVectorAllocEmpty (300, PS_TYPE_F64);
+    psVector *r2rflux = psVectorAllocEmpty (300, PS_TYPE_F64);
+    psVector *apResid = psVectorAllocEmpty (300, PS_TYPE_F64);
+    psVector *dMag    = psVectorAllocEmpty (300, PS_TYPE_F64);
     Npsf = 0;
 
     // select all good PM_SOURCE_TYPE_STAR entries
     for (int i = 0; i < sources->n; i++) {
-	source = sources->data[i];
-	model = source->modelPSF;
-
-	if (source->type != PM_SOURCE_TYPE_STAR) continue; 
-	if (source->mode &  PM_SOURCE_MODE_SATSTAR) continue;
-	if (source->mode &  PM_SOURCE_MODE_BLEND) continue;
-	if (source->mode &  PM_SOURCE_MODE_FAIL) continue;
-	if (source->mode &  PM_SOURCE_MODE_POOR) continue;
-
-	// get growth-corrected, apTrend-uncorrected magnitudes in scaled apertures
-	// will fail if below S/N threshold or model is missing
-	if (!pmSourceMagnitudes (source, psf, PM_SOURCE_PHOT_GROWTH)) {
-	    Nskip ++;
-	    continue;
-	}
-
-	apResid->data.F64[Npsf] = source->apMag - source->psfMag;
-
-	xPos->data.F64[Npsf]    = model->params->data.F32[PM_PAR_XPOS];
-	yPos->data.F64[Npsf]    = model->params->data.F32[PM_PAR_YPOS];
-
-	flux->data.F64[Npsf]    = pow(10.0, -0.4*source->psfMag);
-	r2rflux->data.F64[Npsf] = PS_SQR(model->radiusTMP) / flux->data.F64[Npsf];
-
-	mask->data.U8[Npsf] = 0;
-
-	// XXX sanity clip?
-	// XXX need to see if all data were tossed?
-	// XXX need to subtract median?
-	// XXX need to put this in the config data...
-	// if (fabs(apResid->data.F64[Npsf]) > 0.2) continue;
-	if ((MAX_AP_OFFSET > 0) && (fabs(apResid->data.F64[Npsf]) > MAX_AP_OFFSET)) {
-	    Nfail ++;
-	    continue;
-	}
-
-	dMag->data.F64[Npsf] = model->dparams->data.F32[PM_PAR_I0] / model->params->data.F32[PM_PAR_I0];
-
-	psVectorExtend (mask, 	 100, 1);
-	psVectorExtend (xPos, 	 100, 1);
-	psVectorExtend (yPos, 	 100, 1);
-	psVectorExtend (flux,    100, 1);
-	psVectorExtend (r2rflux, 100, 1);
-	psVectorExtend (dMag,    100, 1);
-	psVectorExtend (apResid, 100, 1);
-	Npsf ++;
-    }
-    psLogMsg ("psphot.apresid", 4, "measure aperture residuals : %f sec for %d objects (%d skipped, %d failed, %ld invalid)\n", 
-	      psTimerMark ("psphot"), Npsf, Nskip, Nfail, sources->n - Npsf - Nskip - Nfail);
+        source = sources->data[i];
+        model = source->modelPSF;
+
+        if (source->type != PM_SOURCE_TYPE_STAR) continue;
+        if (source->mode &  PM_SOURCE_MODE_SATSTAR) continue;
+        if (source->mode &  PM_SOURCE_MODE_BLEND) continue;
+        if (source->mode &  PM_SOURCE_MODE_FAIL) continue;
+        if (source->mode &  PM_SOURCE_MODE_POOR) continue;
+
+        // get growth-corrected, apTrend-uncorrected magnitudes in scaled apertures
+        // will fail if below S/N threshold or model is missing
+        if (!pmSourceMagnitudes (source, psf, PM_SOURCE_PHOT_GROWTH)) {
+            Nskip ++;
+            continue;
+        }
+
+        apResid->data.F64[Npsf] = source->apMag - source->psfMag;
+
+        xPos->data.F64[Npsf]    = model->params->data.F32[PM_PAR_XPOS];
+        yPos->data.F64[Npsf]    = model->params->data.F32[PM_PAR_YPOS];
+
+        flux->data.F64[Npsf]    = pow(10.0, -0.4*source->psfMag);
+        r2rflux->data.F64[Npsf] = PS_SQR(model->radiusTMP) / flux->data.F64[Npsf];
+
+        mask->data.U8[Npsf] = 0;
+
+        // XXX sanity clip?
+        // XXX need to see if all data were tossed?
+        // XXX need to subtract median?
+        // XXX need to put this in the config data...
+        // if (fabs(apResid->data.F64[Npsf]) > 0.2) continue;
+        if ((MAX_AP_OFFSET > 0) && (fabs(apResid->data.F64[Npsf]) > MAX_AP_OFFSET)) {
+            Nfail ++;
+            continue;
+        }
+
+        dMag->data.F64[Npsf] = model->dparams->data.F32[PM_PAR_I0] / model->params->data.F32[PM_PAR_I0];
+
+        psVectorExtend (mask,    100, 1);
+        psVectorExtend (xPos,    100, 1);
+        psVectorExtend (yPos,    100, 1);
+        psVectorExtend (flux,    100, 1);
+        psVectorExtend (r2rflux, 100, 1);
+        psVectorExtend (dMag,    100, 1);
+        psVectorExtend (apResid, 100, 1);
+        Npsf ++;
+    }
+    psLogMsg ("psphot.apresid", 4, "measure aperture residuals : %f sec for %d objects (%d skipped, %d failed, %ld invalid)\n",
+              psTimerMark ("psphot"), Npsf, Nskip, Nfail, sources->n - Npsf - Nskip - Nfail);
 
     // XXX choose a better value here?
     if (Npsf < 5) {
-	psError(PSPHOT_ERR_APERTURE, true, "no valid aperture residual sources, giving up");
-	return false;
-    }
-
-    // APTREND options : NONE SKYBIAS XY_LIN XY_QUAD SKY_XY_LIN FULL 
+        psError(PSPHOT_ERR_APERTURE, true, "no valid aperture residual sources, giving up");
+        return false;
+    }
+
+    // APTREND options : NONE SKYBIAS XY_LIN XY_QUAD SKY_XY_LIN FULL
     // APTREND options are used in the switch block below
     pmPSFApTrendOptions ApTrendOption = DEFAULT_OPTION;
@@ -100,6 +99,6 @@
     if (status) ApTrendOption = pmPSFApTrendOptionFromName (optionName);
     if (ApTrendOption == PM_PSF_APTREND_ERROR) {
-	psError(PSPHOT_ERR_APERTURE, true, "invalid aperture residual trend %s", optionName);
-	return false;
+        psError(PSPHOT_ERR_APERTURE, true, "invalid aperture residual trend %s", optionName);
+        return false;
     }
 
@@ -112,177 +111,177 @@
     switch (ApTrendOption) {
       case PM_PSF_APTREND_NONE:
-	// remove ApTrend fit from pmPSFtry
-	psf->ApTrend->coeff[0][0][0][0] = 0;
-	break;
+        // remove ApTrend fit from pmPSFtry
+        psf->ApTrend->coeff[0][0][0][0] = 0;
+        break;
       case PM_PSF_APTREND_CONSTANT:
-	stats->clipIter = 2;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
-	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
-	    return false;
-	}
-	// apply the fit
-	stats->clipIter = 3;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting aperture correction");
-	    return false;
-	}
-	break;
+        stats->clipIter = 2;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
+        psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+            return false;
+        }
+        // apply the fit
+        stats->clipIter = 3;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
+        psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "Fitting aperture correction");
+            return false;
+        }
+        break;
       case PM_PSF_APTREND_SKYBIAS:
-	// first clip out objects which are too far from the median 
-	stats->clipIter = 2;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
-	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
-	    return false;
-	}
-	// apply the fit
-	stats->clipIter = 3;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYBIAS);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
-	    return false;
-	}
-	break;
+        // first clip out objects which are too far from the median
+        stats->clipIter = 2;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
+        psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+            return false;
+        }
+        // apply the fit
+        stats->clipIter = 3;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYBIAS);
+        psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
+            return false;
+        }
+        break;
       case PM_PSF_APTREND_SKYSAT:
-	// first clip out objects which are too far from the median 
-	stats->clipIter = 2;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
-	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
-	    return false;
-	}
-	// apply the fit
-	stats->clipIter = 2;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYBIAS);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
-	    return false;
-	}
-	// apply the fit
-	stats->clipIter = 3;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYSAT);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting skysat");
-	    return false;
-	}
-	break;
+        // first clip out objects which are too far from the median
+        stats->clipIter = 2;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
+        psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+            return false;
+        }
+        // apply the fit
+        stats->clipIter = 2;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYBIAS);
+        psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
+            return false;
+        }
+        // apply the fit
+        stats->clipIter = 3;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYSAT);
+        psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting skysat");
+            return false;
+        }
+        break;
       case PM_PSF_APTREND_XY_LIN:
-	// first clip out objects which are too far from the median 
-	stats->clipIter = 2;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
-	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
-	    return false;
-	}
-	// apply the fit
-	stats->clipIter = 3;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_XY_LIN);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "fitting, XY_LIN");
-	    return false;
-	}
-	break;
+        // first clip out objects which are too far from the median
+        stats->clipIter = 2;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
+        psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+            return false;
+        }
+        // apply the fit
+        stats->clipIter = 3;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_XY_LIN);
+        psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "fitting, XY_LIN");
+            return false;
+        }
+        break;
       case PM_PSF_APTREND_XY_QUAD:
-	// first clip out objects which are too far from the median 
-	stats->clipIter = 2;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
-	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
-	    return false;
-	}
-	// apply the fit
-	stats->clipIter = 3;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_XY_QUAD);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting XY_QUAD");
-	    return false;
-	}
-	break;
+        // first clip out objects which are too far from the median
+        stats->clipIter = 2;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
+        psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+            return false;
+        }
+        // apply the fit
+        stats->clipIter = 3;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_XY_QUAD);
+        psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "Fitting XY_QUAD");
+            return false;
+        }
+        break;
       case PM_PSF_APTREND_SKY_XY_LIN:
-	// first clip out objects which are too far from the median 
-	stats->clipIter = 2;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
-	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
-	    return false;
-	}
-	// apply the fit
-	stats->clipIter = 3;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKY_XY_LIN);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting sky xy_lin");
-	    return false;
-	}
-	break;
+        // first clip out objects which are too far from the median
+        stats->clipIter = 2;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
+        psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+            return false;
+        }
+        // apply the fit
+        stats->clipIter = 3;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKY_XY_LIN);
+        psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "Fitting sky xy_lin");
+            return false;
+        }
+        break;
       case PM_PSF_APTREND_SKYSAT_XY_LIN:
-	// first clip out objects which are too far from the median 
-	stats->clipIter = 2;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
-	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
-	    return false;
-	}
-	// apply the fit
-	stats->clipIter = 3;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYBIAS);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
-	    return false;
-	}
-	// apply the fit
-	stats->clipIter = 3;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYSAT_XY_LIN);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "Fitting skyset xy_lin");
-	    return false;
-	}
-	break;
+        // first clip out objects which are too far from the median
+        stats->clipIter = 2;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
+        psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting nothing");
+            return false;
+        }
+        // apply the fit
+        stats->clipIter = 3;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYBIAS);
+        psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "clipping, fitting sky bias");
+            return false;
+        }
+        // apply the fit
+        stats->clipIter = 3;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYSAT_XY_LIN);
+        psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "Fitting skyset xy_lin");
+            return false;
+        }
+        break;
       case PM_PSF_APTREND_ALL:
-	// first clip out objects which are too far from the median 
-	stats->clipIter = 2;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
-	psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "Failed to measure apTrend");
-	    return false;
-	}
-	// fit just SkyBias and clip out objects which are too far from the median 
-	stats->clipIter = 2;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYBIAS);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "fitting skyBias");
-	    return false;
-	}
-	// finally, fit x, y, SkyBias and clip out objects which are too far from the median 
-	stats->clipIter = 3;
-	pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_ALL);
-	psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
-	if (psf->ApTrend == NULL) {
-	    psError(PSPHOT_ERR_PHOTOM, false, "fitting all");
-	    return false;
-	}
-	break;
+        // first clip out objects which are too far from the median
+        stats->clipIter = 2;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_CONSTANT);
+        psf->ApTrend  = psVectorClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "Failed to measure apTrend");
+            return false;
+        }
+        // fit just SkyBias and clip out objects which are too far from the median
+        stats->clipIter = 2;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_SKYBIAS);
+        psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "fitting skyBias");
+            return false;
+        }
+        // finally, fit x, y, SkyBias and clip out objects which are too far from the median
+        stats->clipIter = 3;
+        pmPSFMaskApTrend (psf->ApTrend, PM_PSF_APTREND_ALL);
+        psf->ApTrend  = psVectorChiClipFitPolynomial4D (psf->ApTrend, stats, mask, PSFTRY_MASK_ALL, apResid, dMag, xPos, yPos, r2rflux, flux);
+        if (psf->ApTrend == NULL) {
+            psError(PSPHOT_ERR_PHOTOM, false, "fitting all");
+            return false;
+        }
+        break;
       default:
-	psError(PSPHOT_ERR_PHOTOM, true, "Unknown APTREND value: %s", optionName);
-	return false;
+        psError(PSPHOT_ERR_PHOTOM, true, "Unknown APTREND value: %s", optionName);
+        return false;
     }
 
@@ -295,8 +294,8 @@
     psStats *residStats = psStatsAlloc (PS_STAT_SAMPLE_STDEV);
     for (int i = 0; i < dMag->n; i++) {
-	if (dMag->data.F64[i] > 0.01) {
-	    mask->data.U8[i] |= 0x02;
-	} 
-	if (! mask->data.U8[i]) Nkeep ++;
+        if (dMag->data.F64[i] > 0.01) {
+            mask->data.U8[i] |= 0x02;
+        }
+        if (! mask->data.U8[i]) Nkeep ++;
     }
     residStats  = psVectorStats (residStats, resid, NULL, mask, 0x03);
@@ -320,12 +319,12 @@
 
     psLogMsg ("psphot.apresid", 3, "measure full-frame aperture residual: %f sec\n", psTimerMark ("psphot"));
-    psLogMsg ("psphot.apresid", 4, "aperture residual: %f +/- %f : %f bias, %f skysat (%d of %d used)\n", 
-	      psf->ApResid, psf->dApResid, psf->skyBias, psf->skySat, Nkeep, Npsf);
-    psLogMsg ("psphot.apresid", 4, "apresid trends: %f %f %f %f %f\n", 
-	      1e3*psf->ApTrend->coeff[1][0][0][0],
-	      1e6*psf->ApTrend->coeff[2][0][0][0],
-	      1e6*psf->ApTrend->coeff[1][1][0][0],
-	      1e3*psf->ApTrend->coeff[0][1][0][0],
-	      1e6*psf->ApTrend->coeff[0][2][0][0]);
+    psLogMsg ("psphot.apresid", 4, "aperture residual: %f +/- %f : %f bias, %f skysat (%d of %d used)\n",
+              psf->ApResid, psf->dApResid, psf->skyBias, psf->skySat, Nkeep, Npsf);
+    psLogMsg ("psphot.apresid", 4, "apresid trends: %f %f %f %f %f\n",
+              1e3*psf->ApTrend->coeff[1][0][0][0],
+              1e6*psf->ApTrend->coeff[2][0][0][0],
+              1e6*psf->ApTrend->coeff[1][1][0][0],
+              1e3*psf->ApTrend->coeff[0][1][0][0],
+              1e6*psf->ApTrend->coeff[0][2][0][0]);
 
     psFree (mask);
