Index: trunk/psModules/src/objects/pmSourceFitSet.c
===================================================================
--- trunk/psModules/src/objects/pmSourceFitSet.c	(revision 29004)
+++ trunk/psModules/src/objects/pmSourceFitSet.c	(revision 31153)
@@ -39,4 +39,5 @@
 #include "pmSourceDiffStats.h"
 #include "pmSource.h"
+#include "pmSourcePhotometry.h"
 
 #include "pmSourceFitModel.h"
@@ -296,7 +297,8 @@
 
 // set the model parameters for this fit set
-bool pmSourceFitSetValues (pmSourceFitSetData *set, const psVector *dparam,
-                           const psVector *param, pmSource *source,
-                           psMinimization *myMin, int nPix, bool fitStatus)
+bool pmSourceFitSetValues (pmSourceFitSetData *set, 
+			   const psVector *dparam, const psVector *param, const psImage *covar, 
+			   pmSource *source, psMinimization *myMin, int nPix, 
+			   bool fitStatus, pmSourceFitOptions *options, psImageMaskType maskVal)
 {
     PS_ASSERT_PTR_NON_NULL(set, false);
@@ -311,4 +313,5 @@
 
     int n = 0;
+    int nStart = 0;
     for (int i = 0; i < set->paramSet->n; i++) {
 
@@ -320,11 +323,29 @@
             psTrace ("psModules.objects", 4, "%f +/- %f", param->data.F32[n], dparam->data.F32[n]);
         }
+	if (options->saveCovariance) {
+	    // we only save the covar matrix for this object with itself (ignore cross terms between objects)
+	    model->covar = psImageAlloc(model->params->n, model->params->n, PS_TYPE_F32);
+	    for (int ix = 0; ix < model->params->n; ix++) {
+		for (int iy = 0; iy < model->params->n; iy++) {
+		    model->covar->data.F32[iy][ix] = covar->data.F32[nStart+iy][nStart+ix];
+		}
+	    }
+	}
+	nStart += model->params->n;
         psTrace ("psModules.objects", 4, " src %d", i);
+
+	model->nIter = myMin->iter;
+	// model->nPar is set by pmSourceFitSetMasks
 
         // save the resulting chisq, nDOF, nIter
         // these are not unique for any one source
-        model->chisq = myMin->value;
-        model->nIter = myMin->iter;
-        model->nDOF  = nPix - model->params->n;
+	if (options->poissonErrors) {
+	    model->chisq = myMin->value;
+	    model->nPix  = nPix;
+	    model->nDOF  = nPix - model->nPar;
+	    model->chisqNorm = model->chisq / model->nDOF;
+	} else {
+	    pmSourceChisqUnsubtracted (source, model, maskVal);
+	}
 
         // set the model success or failure status
@@ -380,4 +401,5 @@
     for (int i = 0; i < set->paramSet->n; i++) {
         psVector *paramOne = set->paramSet->data[i];
+        pmModel  *modelOne = set->modelSet->data[i];
 
         switch (mode) {
@@ -387,4 +409,5 @@
                 if (j == PM_PAR_I0) continue;
                 constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n + j] = 1;
+		modelOne->nPar = 1;
             }
             break;
@@ -396,4 +419,5 @@
                 if (j == PM_PAR_I0) continue;
                 constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n + j] = 1;
+		modelOne->nPar = 3;
             }
             break;
@@ -401,4 +425,5 @@
             // EXT model fits all params (except sky)
             constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n + PM_PAR_SKY] = 1;
+	    modelOne->nPar = paramOne->n - 1;
             break;
           default:
@@ -550,5 +575,5 @@
     }
 
-// parameter errors from the covariance matrix
+    // parameter errors from the covariance matrix
     psVector *dparams = psVectorAlloc (thisSet->nParamSet, PS_TYPE_F32);
     for (int i = 0; i < dparams->n; i++) {
@@ -558,5 +583,5 @@
     }
 
-// get the Gauss-Newton distance for fixed model parameters
+    // get the Gauss-Newton distance for fixed model parameters
     if (constraint->paramMask != NULL) {
 	psVector *delta = psVectorAlloc (params->n, PS_TYPE_F32);
@@ -580,5 +605,5 @@
     }
 
-    pmSourceFitSetValues (thisSet, dparams, params, source, myMin, y->n, fitStatus);
+    pmSourceFitSetValues (thisSet, dparams, params, covar, source, myMin, y->n, fitStatus, options, maskVal);
     psTrace ("psModules.objects", 5, "onPic: %d, fitStatus: %d, nIter: %d, chisq: %f, nPix: %ld\n", onPic, fitStatus, myMin->iter, myMin->value, y->n);
 
