Index: trunk/ppSim/src/ppSimInsertGalaxies.c
===================================================================
--- trunk/ppSim/src/ppSimInsertGalaxies.c	(revision 27657)
+++ trunk/ppSim/src/ppSimInsertGalaxies.c	(revision 29002)
@@ -1,4 +1,14 @@
 # include "ppSim.h"
 static char *defaultModel = "PS_MODEL_SERSIC";
+
+float imageSum (psImage *image) {
+    float sum = 0.0;
+    for (int iy = 0; iy < image->numRows; iy++) {
+	for (int ix = 0; ix < image->numCols; ix++) {
+	    sum += image->data.F32[iy][ix];
+	}
+    }
+    return sum;
+}
 
 bool ppSimInsertGalaxies (pmReadout *readout, psImage *expCorr, psArray *galaxies, pmConfig *config) {
@@ -84,4 +94,11 @@
     psArray *sources = sources = detections->allSources;
 
+    // output filename
+    char outname[1024];
+    char *outroot = psMetadataLookupStr(&mdok, config->arguments, "OUTPUT");
+    sprintf (outname, "%s.dat", outroot);
+
+    FILE *outfile = fopen (outname, "a");
+
     // add sources to the readout image & weight
     for (long i = 0; i < galaxies->n; i++) {
@@ -131,11 +148,13 @@
 
 	// XXX let the flux limit be a user-defined number of sky sigmas (not just 1.0)
-	float radius = model->modelRadius (model->params, roughNoise);
+	float radius = model->modelRadius (model->params, 0.1*roughNoise);
 	radius = PS_MAX (radius, 1.0);
 	// XXX the exp(-r^0.25) models can go way out if allowed...
-	radius = PS_MIN (radius, 150.0); 
+	radius = PS_MIN (radius, 300.0); 
 
 	// construct a source, with model flux pixels set, based on the model
 	pmSource *source = pmSourceFromModel (model, readout, radius, PM_SOURCE_TYPE_EXTENDED);
+
+	galaxy->flux = model->modelFlux (model->params);
 
 	// XXX set the mag & err values (should this be done in pmSourceFromModel?)
@@ -145,44 +164,18 @@
 	source->errMag = sqrt(Area*PS_SQR(roughNoise) + galaxy->flux) / galaxy->flux;	
 	
-	// XXX add the sources to a source array
+	// insert the source flux in the image
+	float sum1 = imageSum(source->pixels);
+	pmSourceAddWithOffset (source, PM_MODEL_OP_FULL, 0xff, dX, dY);
+	float sum2 = imageSum(source->pixels);
+	float flux = sum2 - sum1;
 
-# if (0)	
-	if (CONVOLVED_FIT) {
+	// fprintf (stderr, "flux: %f, sum1: %f, sum2: %f, flux2: %f, dflux: %f\n", galaxy->flux, sum1, sum2, sum2 - sum1, galaxy->flux - sum2 + sum1);
 
-	    // select the PSF (XXX : move out of loop)
-	    pmPSF *psfModel = psMetadataLookupPtr (&mdok, chip->analysis, "PSPHOT.PSF");
-	    assert (psf);
-	    
-	    // supply the psf to the source (normalized?)
-	    source->modelPSF = pmModelFromPSFforXY (psfModel, xChip, yChip, 1.0);
+	float par8 = (model->params->n == 8) ? model->params->data.F32[7] : 0.0;
+	fprintf (outfile, "%8.3f %8.3f %10.2f  %2d  %7.3f %5.3f  %5.3f %5.3f %5.3f %5.3f\n", galaxy->x, galaxy->y, flux, 1, source->psfMag, source->errMag, axes.major, axes.minor, axes.theta, par8);
 
-	    // instantiate the psf flux
-	    pmSourceCachePSF (source, maskVal);
-
-	    // convert the cached cached psf model for this source to a psKernel
-	    // XXX for the moment, hard-wire the kernel to be 5x5 (2 pix radius)
-	    // XXX for the moment, hard-wire the kernel to be 9x9 (4 pix radius)
-	    psKernel *psf = psphotKernelFromPSF (source, psfSize);
-
-	    // instantiate the source model flux
-	    // XXX this may need to handle the offset?
-	    pmSourceCacheModel (source, maskVal);
-
-	    // make a storage buffer for the output image
-	    psImage *buffer = psImageCopy (source->modelFlux, PS_TYPE_F32);
-
-	    // convolve the psf image with the model image
-	    psImageConvolveDirect (buffer, source->modelFlux, psf);
-
-	    // save the result back in the source
-	    psFree (source->modelFlux);
-	    source->modelFlux = buffer;
-	}
-# endif
-
-	// insert the source flux in the image
-	pmSourceAddWithOffset (source, PM_MODEL_OP_FULL, 0xff, dX, dY);
 	psArrayAdd (sources, 100,source);
     }
+    fclose (outfile);
 
     // XXX many leaks in here, i think 
@@ -191,3 +184,2 @@
     return true;
 }
-
