Index: /trunk/psModules/src/objects/pmPSFtry.c
===================================================================
--- /trunk/psModules/src/objects/pmPSFtry.c	(revision 9840)
+++ /trunk/psModules/src/objects/pmPSFtry.c	(revision 9841)
@@ -5,6 +5,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.23 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-10-30 03:03:38 $
+ *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-11-03 15:12:24 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -49,4 +49,5 @@
     psFree (test->modelPSF);
     psFree (test->metric);
+    psFree (test->metricErr);
     psFree (test->fitMag);
     psFree (test->mask);
@@ -67,11 +68,12 @@
     pmPSFtry *test = (pmPSFtry *) psAlloc(sizeof(pmPSFtry));
 
-    test->psf      = pmPSFAlloc (type, poissonErrors, psfTrendMask);
-    test->sources  = psMemIncrRefCounter(sources);
-    test->modelEXT = psArrayAlloc (sources->n);
-    test->modelPSF = psArrayAlloc (sources->n);
-    test->metric   = psVectorAlloc (sources->n, PS_TYPE_F64);
-    test->fitMag   = psVectorAlloc (sources->n, PS_TYPE_F64);
-    test->mask     = psVectorAlloc (sources->n, PS_TYPE_U8);
+    test->psf       = pmPSFAlloc (type, poissonErrors, psfTrendMask);
+    test->sources   = psMemIncrRefCounter(sources);
+    test->modelEXT  = psArrayAlloc (sources->n);
+    test->modelPSF  = psArrayAlloc (sources->n);
+    test->metric    = psVectorAlloc (sources->n, PS_TYPE_F64);
+    test->metricErr = psVectorAlloc (sources->n, PS_TYPE_F64);
+    test->fitMag    = psVectorAlloc (sources->n, PS_TYPE_F64);
+    test->mask      = psVectorAlloc (sources->n, PS_TYPE_U8);
 
     for (int i = 0; i < test->modelEXT->n; i++) {
@@ -80,4 +82,5 @@
         test->modelPSF->data[i] = NULL;
         test->metric->data.F64[i] = 0;
+        test->metricErr->data.F64[i] = 0;
         test->fitMag->data.F64[i] = 0;
     }
@@ -175,4 +178,5 @@
         psfTry->modelPSF->data[i] = modelPSF;
 
+        // I'm not using the pmSourceMagnitudes API, why?
         // XXX : use a different aperture radius from the fit radius?
         // XXX : use a different estimator for the local sky?
@@ -188,4 +192,6 @@
 
         psfTry->metric->data.F64[i] = obsMag - fitMag;
+        psfTry->metricErr->data.F64[i] = modelPSF->dparams->data.F32[PM_PAR_I0] / modelPSF->params->data.F32[PM_PAR_I0];
+
         psfTry->fitMag->data.F64[i] = fitMag;
         Npsf ++;
@@ -280,5 +286,9 @@
     // linear clipped fit of ApResid to r2rflux
     psPolynomial1D *poly = psPolynomial1DAlloc (PS_POLYNOMIAL_ORD, 1);
-    poly = psVectorClipFitPolynomial1D (poly, stats, psfTry->mask, PSFTRY_MASK_ALL, psfTry->metric, NULL, r2rflux);
+
+    // XXX test to only fit a constant offset (no SKYBIAS)
+    poly->mask[1] = 1;
+
+    poly = psVectorClipFitPolynomial1D (poly, stats, psfTry->mask, PSFTRY_MASK_ALL, psfTry->metric, psfTry->metricErr, r2rflux);
     if (poly == NULL) {
         psError(PS_ERR_UNKNOWN, false, "Failed to fit clipped poly");
@@ -286,4 +296,28 @@
     }
     psLogMsg ("pmPSFtryMetric", 4, "fit stats: %f +/- %f\n", stats->sampleMedian, stats->sampleStdev);
+    psLogMsg ("pmPSFtryMetric", 4, "apresid: %f +/- %f\n", poly->coeff[0], poly->coeffErr[0]);
+
+    // XXX test dump of fitted model (dump when tracing?)
+    if (0) {
+        FILE *f = fopen ("resid.dat", "w");
+        psVector *apfit = psPolynomial1DEvalVector (poly, r2rflux);
+        for (int i = 0; i < psfTry->sources->n; i++) {
+            int keep = (psfTry->mask->data.U8[i] & PSFTRY_MASK_ALL);
+
+            pmSource *source = psfTry->sources->data[i];
+            float x = source->peak->x;
+            float y = source->peak->y;
+
+            fprintf (f, "%d  %d, %f %f %f  %f %f %f  %f\n",
+                     i, keep, x, y,
+                     psfTry->fitMag->data.F64[i],
+                     r2rflux->data.F64[i],
+                     psfTry->metric->data.F64[i],
+                     psfTry->metricErr->data.F64[i],
+                     apfit->data.F64[i]);
+        }
+        fclose (f);
+        psFree (apfit);
+    }
 
     pmPSFMaskApTrend (psfTry->psf->ApTrend, PM_PSF_APTREND_SKYBIAS);
