Index: trunk/psphot/src/psphotOutput.c
===================================================================
--- trunk/psphot/src/psphotOutput.c	(revision 5058)
+++ trunk/psphot/src/psphotOutput.c	(revision 5068)
@@ -31,5 +31,5 @@
   
     if (!strcasecmp (outputMode, "SX")) {
-	pmSourcesWriteSX (imdata, outputFile, sources, psf, sky);
+	pmSourcesWriteSX (imdata, config, outputFile, sources, psf, sky);
 	return;
     }
@@ -159,6 +159,6 @@
 }
 
-// elixir/sextractor-style output list with fixed line width
-bool pmSourcesWriteSX (eamReadout *imdata, char *filename, psArray *sources, pmPSF *psf, psStats *skyStats) {
+// elixir-mode / sextractor-style output list with fixed line width
+bool pmSourcesWriteSX (eamReadout *imdata, psMetadata *config, char *filename, psArray *sources, pmPSF *psf, psStats *skyStats) {
 
     int i, status, flags;
@@ -166,6 +166,8 @@
     psF32 *PAR, *dPAR;
     float sky, dmag, apMag, fitMag;
-
-    psLine *line = psLineAlloc (110);  // 104 is dophot-defined line length
+    float x, y, rflux;
+    bool result;
+
+    psLine *line = psLineAlloc (110);  // 110 is sextractor line length
 
     FILE *f = fopen (filename, "w");
@@ -174,4 +176,7 @@
 	return false;
     }
+
+    // aperture radius for ap magnitude
+    float RADIUS = psMetadataLookupF32 (&result, config, "PSF_FIT_RADIUS");
 
     // write sources with models first 
@@ -214,4 +219,12 @@
 	dPAR = model->dparams->data.F32;
 
+	x = source->peak->x;
+	y = source->peak->y;
+
+	// we have already (psphotApplyPSF) defined pixels at least OUTER_RADIUS from source
+	// we need to mask pixels to measure the aperture magnitude
+	// set aperture mask circle of PSF_FIT_RADIUS 
+	psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PSPHOT_MASK_MARKED);
+
 	// save local sky for later
 	sky = model->params->data.F32[0];
@@ -224,5 +237,9 @@
 
 	// measure object photometry
-	status = pmSourcePhotometry (&fitMag, &apMag, model, source->pixels, source->mask);
+	status  = pmSourcePhotometry (&fitMag, &apMag, model, source->pixels, source->mask);
+
+	// correct both apMag and fitMag to same system, consistent with infinite flux star in aperture RADIUS
+	rflux   = pow (10.0, 0.4*fitMag);
+	apMag  -= rflux * psf->skyBias * (M_PI * PS_SQR(RADIUS));
 	fitMag += psf->ApResid;
 
@@ -232,4 +249,7 @@
 	pmSourceSubModel (source->pixels, source->mask, model, false);
 	model->params->data.F32[0] = sky;
+
+	// unmask aperture
+	psImageKeepCircle (source->mask, x, y, RADIUS, "AND", ~PSPHOT_MASK_MARKED);
 
 	if (status == FALSE) continue;
