Index: trunk/psphot/src/psphotApResid.c
===================================================================
--- trunk/psphot/src/psphotApResid.c	(revision 20831)
+++ trunk/psphot/src/psphotApResid.c	(revision 20864)
@@ -22,12 +22,12 @@
     bool measureAptrend = psMetadataLookupBool (&status, recipe, "MEASURE.APTREND");
     if (!measureAptrend) {
-	// save nan values since these were not calculated
-	psMetadataAdd (recipe, PS_LIST_TAIL, "SKYBIAS",  PS_DATA_F32 | PS_META_REPLACE, "aperture sky bias",   NAN);
-	psMetadataAdd (recipe, PS_LIST_TAIL, "SKYSAT",   PS_DATA_F32 | PS_META_REPLACE, "aperture-determined saturation",   NAN);
-	psMetadataAdd (recipe, PS_LIST_TAIL, "APMIFIT",  PS_DATA_F32 | PS_META_REPLACE, "aperture residual",   NAN);
-	psMetadataAdd (recipe, PS_LIST_TAIL, "DAPMIFIT", PS_DATA_F32 | PS_META_REPLACE, "ap residual scatter", NAN);
-	psMetadataAdd (recipe, PS_LIST_TAIL, "APLOSS",   PS_DATA_F32 | PS_META_REPLACE, "aperture loss (mag)", NAN);
-	psMetadataAdd (recipe, PS_LIST_TAIL, "NAPMIFIT", PS_DATA_S32 | PS_META_REPLACE, "number of apresid stars", 0);
-	return true;
+        // save nan values since these were not calculated
+        psMetadataAdd (recipe, PS_LIST_TAIL, "SKYBIAS",  PS_DATA_F32 | PS_META_REPLACE, "aperture sky bias",   NAN);
+        psMetadataAdd (recipe, PS_LIST_TAIL, "SKYSAT",   PS_DATA_F32 | PS_META_REPLACE, "aperture-determined saturation",   NAN);
+        psMetadataAdd (recipe, PS_LIST_TAIL, "APMIFIT",  PS_DATA_F32 | PS_META_REPLACE, "aperture residual",   NAN);
+        psMetadataAdd (recipe, PS_LIST_TAIL, "DAPMIFIT", PS_DATA_F32 | PS_META_REPLACE, "ap residual scatter", NAN);
+        psMetadataAdd (recipe, PS_LIST_TAIL, "APLOSS",   PS_DATA_F32 | PS_META_REPLACE, "aperture loss (mag)", NAN);
+        psMetadataAdd (recipe, PS_LIST_TAIL, "NAPMIFIT", PS_DATA_S32 | PS_META_REPLACE, "number of apresid stars", 0);
+        return true;
     }
 
@@ -93,5 +93,5 @@
         if (!pmSourceMagnitudes (source, psf, photMode, maskVal)) {
             Nskip ++;
-	    psTrace ("psphot", 3, "skip : bad source mag");
+            psTrace ("psphot", 3, "skip : bad source mag");
             continue;
         }
@@ -99,19 +99,19 @@
         if (!isfinite(source->apMag) || !isfinite(source->psfMag)) {
             Nfail ++;
-	    psTrace ("psphot", 3, "fail : nan mags : %f %f", source->apMag, source->psfMag);
+            psTrace ("psphot", 3, "fail : nan mags : %f %f", source->apMag, source->psfMag);
             continue;
         }
 
-	// aperture residual for this source
-	float dap = source->apMag - source->psfMag;
+        // aperture residual for this source
+        float dap = source->apMag - source->psfMag;
 
         // sanity clipping : if the model is very discrepant, put your expectation in the recipe
         if ((MAX_AP_OFFSET > 0) && (fabs(dap) > MAX_AP_OFFSET)) {
             Nfail ++;
-	    psTrace ("psphot", 3, "fail : bad dap %f %f", dap, MAX_AP_OFFSET);
+            psTrace ("psphot", 3, "fail : bad dap %f %f", dap, MAX_AP_OFFSET);
             continue;
         }
 
-	mag->data.F32[Npsf]     = source->psfMag;
+        mag->data.F32[Npsf]     = source->psfMag;
         apResid->data.F32[Npsf] = dap;
         xPos->data.F32[Npsf]    = model->params->data.F32[PM_PAR_XPOS];
@@ -130,5 +130,5 @@
         Npsf ++;
     }
-    
+
     psLogMsg ("psphot.apresid", PS_LOG_DETAIL, "measure aperture residuals for %d objects (%d skipped, %d failed, %ld invalid)\n",
               Npsf, Nskip, Nfail, sources->n - Npsf - Nskip - Nfail);
@@ -137,18 +137,18 @@
     if (Npsf < APTREND_NSTAR_MIN) {
         psWarning("Only %d valid aperture residual sources (need %d), giving up", Npsf, APTREND_NSTAR_MIN);
-	// save nan values since these were not calculated
-	psMetadataAdd (recipe, PS_LIST_TAIL, "SKYBIAS",  PS_DATA_F32 | PS_META_REPLACE, "aperture sky bias",   NAN);
-	psMetadataAdd (recipe, PS_LIST_TAIL, "SKYSAT",   PS_DATA_F32 | PS_META_REPLACE, "aperture-determined saturation",   NAN);
-	psMetadataAdd (recipe, PS_LIST_TAIL, "APMIFIT",  PS_DATA_F32 | PS_META_REPLACE, "aperture residual",   NAN);
-	psMetadataAdd (recipe, PS_LIST_TAIL, "DAPMIFIT", PS_DATA_F32 | PS_META_REPLACE, "ap residual scatter", NAN);
-	psMetadataAdd (recipe, PS_LIST_TAIL, "APLOSS",   PS_DATA_F32 | PS_META_REPLACE, "aperture loss (mag)", NAN);
-	psMetadataAdd (recipe, PS_LIST_TAIL, "NAPMIFIT", PS_DATA_S32 | PS_META_REPLACE, "number of apresid stars", 0);
-
-	psFree (mag);
-	psFree (mask);
-	psFree (xPos);
-	psFree (yPos);
-	psFree (apResid);
-	psFree (dMag);
+        // save nan values since these were not calculated
+        psMetadataAdd (recipe, PS_LIST_TAIL, "SKYBIAS",  PS_DATA_F32 | PS_META_REPLACE, "aperture sky bias",   NAN);
+        psMetadataAdd (recipe, PS_LIST_TAIL, "SKYSAT",   PS_DATA_F32 | PS_META_REPLACE, "aperture-determined saturation",   NAN);
+        psMetadataAdd (recipe, PS_LIST_TAIL, "APMIFIT",  PS_DATA_F32 | PS_META_REPLACE, "aperture residual",   NAN);
+        psMetadataAdd (recipe, PS_LIST_TAIL, "DAPMIFIT", PS_DATA_F32 | PS_META_REPLACE, "ap residual scatter", NAN);
+        psMetadataAdd (recipe, PS_LIST_TAIL, "APLOSS",   PS_DATA_F32 | PS_META_REPLACE, "aperture loss (mag)", NAN);
+        psMetadataAdd (recipe, PS_LIST_TAIL, "NAPMIFIT", PS_DATA_S32 | PS_META_REPLACE, "number of apresid stars", 0);
+
+        psFree (mag);
+        psFree (mask);
+        psFree (xPos);
+        psFree (yPos);
+        psFree (apResid);
+        psFree (dMag);
         return false;
     }
@@ -168,19 +168,19 @@
     for (int i = 1; i <= APTREND_ORDER_MAX; i++) {
 
-	if (!psphotApResidTrend (readout, psf, Npsf, i, &errorScale, &errorFloor, mask, xPos, yPos, apResid, dMag)) {
-	    break;
-	}
-
-	// store the resulting errorFloor values and the scales, redo the best
-	if (errorFloor < errorFloorMin) {
-	    errorFloorMin = errorFloor;
-	    entryMin = i;
-	}
+        if (!psphotApResidTrend (readout, psf, Npsf, i, &errorScale, &errorFloor, mask, xPos, yPos, apResid, dMag)) {
+            break;
+        }
+
+        // store the resulting errorFloor values and the scales, redo the best
+        if (errorFloor < errorFloorMin) {
+            errorFloorMin = errorFloor;
+            entryMin = i;
+        }
     }
     if (entryMin == -1) {
-	psAbort ("failed on ApResid Trend");
-    }
-
-    // XXX catch error condition 
+        psAbort ("failed on ApResid Trend");
+    }
+
+    // XXX catch error condition
     psphotApResidTrend (readout, psf, Npsf, entryMin, &errorScale, &errorFloor, mask, xPos, yPos, apResid, dMag);
 
@@ -191,13 +191,13 @@
 
     if (psTraceGetLevel("psphot") >= 2) {
-	FILE *dumpFile = fopen ("apresid.dat", "w");
-	for (int i = 0; i < xPos->n; i++) {
-	    fprintf (dumpFile, "%f %f  %f %f %f  %f %f  %x\n", 
-		     xPos->data.F32[i], yPos->data.F32[i], 
-		     mag->data.F32[i], dMag->data.F32[i], dMagSys->data.F32[i],
-		     apResid->data.F32[i], apResidRes->data.F32[i],
-		     mask->data.U8[i]);
-	}
-	fclose (dumpFile);
+        FILE *dumpFile = fopen ("apresid.dat", "w");
+        for (int i = 0; i < xPos->n; i++) {
+            fprintf (dumpFile, "%f %f  %f %f %f  %f %f  %x\n",
+                     xPos->data.F32[i], yPos->data.F32[i],
+                     mag->data.F32[i], dMag->data.F32[i], dMagSys->data.F32[i],
+                     apResid->data.F32[i], apResidRes->data.F32[i],
+                     mask->data.U8[i]);
+        }
+        fclose (dumpFile);
     }
 
@@ -252,5 +252,5 @@
     int nBin = PS_MAX (dMag->n / nGroup, 1);
 
-    // output vectors for ApResid trend 
+    // output vectors for ApResid trend
     psVector *dSo = psVectorAlloc (nBin, PS_TYPE_F32);
     psVector *dMo = psVectorAlloc (nBin, PS_TYPE_F32);
@@ -267,47 +267,47 @@
     int n = 0;
     for (int i = 0; i < dMo->n; i++) {
-	int j;
-	for (j = 0; (j < nGroup) && (n < dMag->n); j++, n++) {
-	    int N = index->data.U32[n];
-	    dMSubset->data.F32[j] = dMag->data.F32[N];
-	    dASubset->data.F32[j] = dap->data.F32[N];
-	    mkSubset->data.U8[j]  = mask->data.U8[N];
-	}
-	dMSubset->n = j;
-	dASubset->n = j;
-	mkSubset->n = j;
-
-	psStatsInit (statsS);
-	psStatsInit (statsM);
-
-	if (j > 2) {
-	    bool status = true;
-	    status &= psVectorStats (statsS, dASubset, NULL, mkSubset, 0xff);
-	    status &= psVectorStats (statsM, dMSubset, NULL, mkSubset, 0xff);
-	    if (!status) { psErrorClear (); }
-	    dSo->data.F32[i] = statsS->robustStdev;
-	    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;
-	    dMo->data.F32[i] = NAN;
-	    dRo->data.F32[i] = NAN;
-	}
+        int j;
+        for (j = 0; (j < nGroup) && (n < dMag->n); j++, n++) {
+            int N = index->data.U32[n];
+            dMSubset->data.F32[j] = dMag->data.F32[N];
+            dASubset->data.F32[j] = dap->data.F32[N];
+            mkSubset->data.U8[j]  = mask->data.U8[N];
+        }
+        dMSubset->n = j;
+        dASubset->n = j;
+        mkSubset->n = j;
+
+        psStatsInit (statsS);
+        psStatsInit (statsM);
+
+        if (j > 2) {
+            bool status = true;
+            status &= psVectorStats (statsS, dASubset, NULL, mkSubset, 0xff);
+            status &= psVectorStats (statsM, dMSubset, NULL, mkSubset, 0xff);
+            if (!status) { psErrorClear (); }
+            dSo->data.F32[i] = statsS->robustStdev;
+            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;
+            dMo->data.F32[i] = NAN;
+            dRo->data.F32[i] = NAN;
+        }
     }
     psFree (dMSubset);
     psFree (dASubset);
     psFree (mkSubset);
-    
+
     psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN);
     if (!psVectorStats (stats, dRo, NULL, NULL, 0)) {
-	// XXX better testing of raised error
-	psErrorClear();
+        // XXX better testing of raised error
+        psErrorClear();
     }
 
     *errorScale = stats->sampleMedian;
     for (int i = 0; i < dSo->n; i++) {
-	*errorFloor = dSo->data.F32[i];
-	if (isfinite(*errorFloor)) break;
+        *errorFloor = dSo->data.F32[i];
+        if (isfinite(*errorFloor)) break;
     }
 
@@ -318,5 +318,5 @@
     psFree (dMo);
     psFree (dSo);
-    
+
     psFree (statsS);
     psFree (statsM);
@@ -328,20 +328,20 @@
 
     int Nx, Ny;
-	
+
     if (readout->image->numCols > readout->image->numRows) {
         Nx = scale;
-	float AR = readout->image->numRows / (float) readout->image->numCols;
-	Ny = (int) (Nx * AR + 0.5);
+        float AR = readout->image->numRows / (float) readout->image->numCols;
+        Ny = (int) (Nx * AR + 0.5);
         Ny = PS_MAX (1, Ny);
     } else {
-	Ny = scale;
-	float AR = readout->image->numRows / (float) readout->image->numCols;
-	Nx = (int) (Ny * AR + 0.5);
-	Nx = PS_MAX (1, Nx);
-    }
-    
+        Ny = scale;
+        float AR = readout->image->numRows / (float) readout->image->numCols;
+        Nx = (int) (Ny * AR + 0.5);
+        Nx = PS_MAX (1, Nx);
+    }
+
     // require at least 10 stars per spatial bin
     if (Npsf < 10*Nx*Ny) {
-	return false;
+        return false;
     }
 
@@ -361,10 +361,10 @@
     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);
+        dMagSoft->data.F32[i] = hypot(dMag->data.F32[i], 0.01);
     }
 
     // XXX test for errors here
     pmTrend2DFit (psf->ApTrend, mask, 0xff, xPos, yPos, apResid, dMagSoft);
-    
+
     // construct the fitted values and the residuals
     psVector *apResidFit = pmTrend2DEvalVector (psf->ApTrend, xPos, yPos);
@@ -387,3 +387,3 @@
     return true;
 }
-    
+
