Index: trunk/psphot/src/psphotOutput.c
===================================================================
--- trunk/psphot/src/psphotOutput.c	(revision 5828)
+++ trunk/psphot/src/psphotOutput.c	(revision 5837)
@@ -24,4 +24,20 @@
 	return;
     }
+    if (!strcasecmp (outputMode, "OBJ")) {
+	pmSourcesWriteOBJ (imdata, config, outputFile, sources, psf, sky);
+	return;
+    }
+    if (!strcasecmp (outputMode, "SX")) {
+	pmSourcesWriteSX (imdata, config, outputFile, sources, psf, sky);
+	return;
+    }
+    if (!strcasecmp (outputMode, "CMP")) {
+	pmSourcesWriteCMP (imdata, config, outputFile, sources, psf, sky);
+	return;
+    }
+    if (!strcasecmp (outputMode, "CMF")) {
+	pmSourcesWriteCMF (imdata, config, outputFile, sources, psf, sky);
+	return;
+    }
     if (!strcasecmp (outputMode, "TEXT")) {
 	pmSourcesWriteText (imdata, config, outputFile, sources, psf, sky);
@@ -29,45 +45,5 @@
     }
 
-    if (!strcasecmp (outputMode, "OBJ")) {
-	pmSourcesWriteOBJ (imdata, config, outputFile, sources, psf, sky);
-	return;
-    }
-  
-    if (!strcasecmp (outputMode, "SX")) {
-	pmSourcesWriteSX (imdata, config, outputFile, sources, psf, sky);
-	return;
-    }
-  
-    if (!strcasecmp (outputMode, "CMP")) {
-	pmSourcesWriteCMP (imdata, config, outputFile, sources, psf, sky);
-	return;
-    }
-  
-    if (!strcasecmp (outputMode, "CMF")) {
-	pmSourcesWriteCMF (imdata, config, outputFile, sources, psf, sky);
-	return;
-    }
-
     psAbort ("psphot", "unknown output mode %s", outputMode);
-}
-
-bool pmSourcesWriteText (eamReadout *imdata, psMetadata *config, char *filename, psArray *sources, pmPSF *psf, psStats *sky) {
-
-    char *name = (char *) psAlloc (strlen(filename) + 10);
-
-    sprintf (name, "%s.psf.dat", filename);
-    pmModelWritePSFs (sources, config, name, psf);
-
-    sprintf (name, "%s.flt.dat", filename);
-    pmModelWriteFLTs (sources, name);
-
-    sprintf (name, "%s.nul.dat", filename);
-    pmModelWriteNULLs (sources, name);
-
-    sprintf (name, "%s.mnt.dat", filename);
-    pmMomentsWriteText (sources, name);
-
-    psFree (name);
-    return true;
 }
 
@@ -115,5 +91,5 @@
 	psLineAdd (line, "%9.3f", PAR[5]);
 	psLineAdd (line, "%7.2f", PAR[6]);
-	psLineAdd (line, "%8.3f", 32.0);
+	psLineAdd (line, "%8.3f", 99.999);
 	psLineAdd (line, "%8.3f", source->apMag);
 	psLineAdd (line, "%8.2f\n", apResid);
@@ -166,5 +142,5 @@
 	psLineAdd (line, "%9.2f", 0.0); // should be FWHMy
 	psLineAdd (line, "%6.1f", 0.0); // should be Theta
-	psLineAdd (line, "%9.4f", 32.0); // should be MAG_ISO
+	psLineAdd (line, "%9.4f", 99.999); // should be MAG_ISO
 	psLineAdd (line, "%9.4f", source->apMag);
 	psLineAdd (line, "%4d\n", 0); // should be flags
@@ -182,5 +158,5 @@
     psMetadataItem *mdi;
     psF32 *PAR, *dPAR;
-    float dmag, apResid, lsky;
+    float dmag, lsky;
     bool status;
 
@@ -188,4 +164,5 @@
     float RADIUS = psMetadataLookupF32 (&status, config, "PSF_FIT_RADIUS");
     float ZERO_POINT = psMetadataLookupF32 (&status, config, "ZERO_POINT");
+    if (!status) ZERO_POINT = 25.0;
     char *PHOTCODE = psMetadataLookupPtr (&status, config, "PHOTCODE");
 
@@ -237,5 +214,4 @@
 	dmag = dPAR[1] / PAR[1];
 	type = pmSourceDophotType (source);
-	apResid = source->apMag - source->fitMag;
 	lsky = (PAR[0] < 1.0) ? 0.0 : log10(PAR[0]);
 
@@ -243,10 +219,10 @@
 	psLineAdd (line, "%6.1f ", PAR[2]);
 	psLineAdd (line, "%6.1f ", PAR[3]);
-	psLineAdd (line, "%6.3f ", PS_MIN (32.0, source->fitMag + ZERO_POINT));
-	psLineAdd (line, "%03d ", (int)(1000*dmag));
+	psLineAdd (line, "%6.3f ", PS_MIN (99.999, source->fitMag + ZERO_POINT));
+	psLineAdd (line, "%03d ",  PS_MIN (999, (int)(1000*dmag)));
 	psLineAdd (line, "%2d ",   type);
 	psLineAdd (line, "%3.1f ", lsky);
-	psLineAdd (line, "%6.3f ", 32.0);    // should be 'Mgal
-	psLineAdd (line, "%6.3f ", PS_MIN (32.0, source->apMag + ZERO_POINT)); 
+	psLineAdd (line, "%6.3f ", 99.999);    // should be 'Mgal
+	psLineAdd (line, "%6.3f ", PS_MIN (99.999, source->apMag + ZERO_POINT)); 
 	psLineAdd (line, "%6.2f ", PAR[4]);  // should be 'FHWM x'
 	psLineAdd (line, "%6.2f ", PAR[5]);  // should be 'FHWM y'
@@ -297,5 +273,5 @@
 
     table = psArrayAlloc (sources->n);
-    sources->n = 0;
+    table->n = 0;
 
     for (i = 0; i < sources->n; i++) {
@@ -313,15 +289,15 @@
 
 	row = psMetadataAlloc ();
-	psMetadataAdd (row, PS_LIST_HEAD, "X_PIX",   PS_DATA_F32, "", PAR[2]);
-	psMetadataAdd (row, PS_LIST_HEAD, "Y_PIX",   PS_DATA_F32, "", PAR[3]);
-	psMetadataAdd (row, PS_LIST_HEAD, "MAG_RAW", PS_DATA_F32, "", source->fitMag + ZERO_POINT);
-	psMetadataAdd (row, PS_LIST_HEAD, "MAG_ERR", PS_DATA_F32, "", (int)(1000*dmag));
-	psMetadataAdd (row, PS_LIST_HEAD, "MAG_GAL", PS_DATA_F32, "", type);
-	psMetadataAdd (row, PS_LIST_HEAD, "MAG_AP",  PS_DATA_F32, "", lsky);
-	psMetadataAdd (row, PS_LIST_HEAD, "LOG_SKY", PS_DATA_F32, "", 32.0);    // should be 'Mgal'
-	psMetadataAdd (row, PS_LIST_HEAD, "FWHM_X",  PS_DATA_F32, "", source->apMag + ZERO_POINT); 
-	psMetadataAdd (row, PS_LIST_HEAD, "FWHM_Y",  PS_DATA_F32, "", PAR[4]);  // should be 'FHWM x'
-	psMetadataAdd (row, PS_LIST_HEAD, "THETA",   PS_DATA_F32, "", PAR[5]);  // should be 'FHWM y'
-	psMetadataAdd (row, PS_LIST_HEAD, "DOPHOT",  PS_TYPE_S8,  "", 0);   // should be theta
+	psMetadataAdd (row, PS_LIST_TAIL, "X_PIX",   PS_DATA_F32, "", PAR[2]);
+	psMetadataAdd (row, PS_LIST_TAIL, "Y_PIX",   PS_DATA_F32, "", PAR[3]);
+	psMetadataAdd (row, PS_LIST_TAIL, "MAG_RAW", PS_DATA_F32, "", source->fitMag + ZERO_POINT);
+	psMetadataAdd (row, PS_LIST_TAIL, "MAG_ERR", PS_DATA_F32, "", (int)(1000*dmag));
+	psMetadataAdd (row, PS_LIST_TAIL, "MAG_GAL", PS_DATA_F32, "", type);
+	psMetadataAdd (row, PS_LIST_TAIL, "MAG_AP",  PS_DATA_F32, "", lsky);
+	psMetadataAdd (row, PS_LIST_TAIL, "LOG_SKY", PS_DATA_F32, "", 32.0);    // should be 'Mgal'
+	psMetadataAdd (row, PS_LIST_TAIL, "FWHM_X",  PS_DATA_F32, "", source->apMag + ZERO_POINT); 
+	psMetadataAdd (row, PS_LIST_TAIL, "FWHM_Y",  PS_DATA_F32, "", PAR[4]);  // should be 'FHWM x'
+	psMetadataAdd (row, PS_LIST_TAIL, "THETA",   PS_DATA_F32, "", PAR[5]);  // should be 'FHWM y'
+	psMetadataAdd (row, PS_LIST_TAIL, "DOPHOT",  PS_TYPE_S32, "", 0);   // should be theta
 	// psMetadataAdd (header, PS_LIST_HEAD, "DUMMY",   PS_DATA_STRING, "", NULL);
     
@@ -339,7 +315,19 @@
     psMetadataAdd (theader, PS_LIST_HEAD, "EXTNAME", PS_DATA_STRING, "extension name", "SMPFILE");
 
+    psImage *image;
+
+    image = psImageAlloc (512, 512, PS_DATA_F32);
+    for (int j = 0; j < 512; j++) {
+	for (int i = 0; i < 512; i++) {
+	    image->data.F32[j][i] = i;
+	}
+    }
+
     psFits *fits = psFitsOpen (filename, "w");
-    psFitsWriteHeader (imdata->header, fits);
-    psFitsWriteTable (fits, theader, table);
+    // psFitsWriteHeader (imdata->header, fits);
+    // psFitsWriteTable (fits, theader, table);
+
+    psFitsWriteImage (fits, NULL, image, 0);
+    psFitsWriteTable (fits, NULL, table);
     psFitsClose (fits);
 
@@ -349,23 +337,21 @@
 /***** Text Output Methods *****/
 
-// write the peaks to an output file
-bool pmPeaksWriteText (psArray *peaks, char *filename) {
-
-    int i;
-    FILE *f;
-	
-    f = fopen (filename, "w");
-    if (f == NULL) {
-	psLogMsg ("pmPeaksWriteText", 3, "can't open output file for peaks%s\n", filename);
-	return false;
-    }
-
-    for (i = 0; i < peaks->n; i++) {
-	pmPeak *peak = peaks->data[i];
-	if (peak == NULL) continue;
-	fprintf (f, "%5d %5d  %7.1f\n", 
-		 peak->x, peak->y, peak->counts); 
-    }
-    fclose (f);
+bool pmSourcesWriteText (eamReadout *imdata, psMetadata *config, char *filename, psArray *sources, pmPSF *psf, psStats *sky) {
+
+    char *name = (char *) psAlloc (strlen(filename) + 10);
+
+    sprintf (name, "%s.psf.dat", filename);
+    pmModelWritePSFs (sources, config, name, psf);
+
+    sprintf (name, "%s.flt.dat", filename);
+    pmModelWriteFLTs (sources, name);
+
+    sprintf (name, "%s.nul.dat", filename);
+    pmModelWriteNULLs (sources, name);
+
+    sprintf (name, "%s.mnt.dat", filename);
+    pmMomentsWriteText (sources, name);
+
+    psFree (name);
     return true;
 }
@@ -381,4 +367,6 @@
     bool status;
 
+    float RADIUS = psMetadataLookupF32 (&status, config, "PSF_FIT_RADIUS");
+
     f = fopen (filename, "w");
     if (f == NULL) {
@@ -387,6 +375,4 @@
     }
 
-    float RADIUS = psMetadataLookupF32 (&status, config, "PSF_FIT_RADIUS");
-
    // write sources with models first 
     for (i = 0; i < sources->n; i++) {
@@ -394,7 +380,4 @@
 
 	if (source->type != PM_SOURCE_STAR) continue;
-	// if (source->mode & PM_SOURCE_FAIL) continue;
-	// if (source->mode & PM_SOURCE_POOR) continue;
-
 	model = pmSourceMagnitudes (source, psf, RADIUS);
 	if (model == NULL) continue;
@@ -407,5 +390,5 @@
 	dMag = dPAR[1] / PAR[1];
 
-	fprintf (f, "%7.1f %7.1f  %5.1f %8.4f  %7.4f %7.4f  ", 
+	fprintf (f, "%7.1f %7.1f  %7.1f %8.4f  %7.4f %7.4f  ", 
 		 PAR[2], PAR[3], PAR[0], source->fitMag, dMag, dPos);
 
@@ -449,7 +432,6 @@
 
 	if (source->type != PM_SOURCE_GALAXY) continue;
-	
-	if (source->modelFLT == NULL) continue;
-	model = pmSourceMagnitudes (source, NULL, source->modelFLT->radius);
+	model = pmSourceMagnitudes (source, NULL, 0.0);
+	if (model == NULL) continue;
 
 	PAR  = model->params->data.F32;
@@ -458,12 +440,8 @@
 	// dPos is shape error
 	// XXX these are hardwired for SGAUSS
-	dPos = 0;
-	dPos += PS_SQR(dPAR[4] / PAR[4]);
-	dPos += PS_SQR(dPAR[5] / PAR[5]);
-	// dPos += PS_SQR(dPAR[7] / PAR[7]);
-	dPos = sqrt (dPos);
+	dPos = hypot ((dPAR[4] / PAR[4]), (dPAR[5] / PAR[5]));
 	dMag = dPAR[1] / PAR[1];
 
-	fprintf (f, "%7.1f %7.1f  %7.1f %7.3f  %7.4f %7.4f  ", 
+	fprintf (f, "%7.1f %7.1f  %7.1f %8.4f  %7.4f %7.4f  ", 
 		 PAR[2], PAR[3], PAR[0], source->fitMag, dMag, dPos);
 
@@ -560,4 +538,26 @@
 }
 
+// write the peaks to an output file
+bool pmPeaksWriteText (psArray *peaks, char *filename) {
+
+    int i;
+    FILE *f;
+	
+    f = fopen (filename, "w");
+    if (f == NULL) {
+	psLogMsg ("pmPeaksWriteText", 3, "can't open output file for peaks%s\n", filename);
+	return false;
+    }
+
+    for (i = 0; i < peaks->n; i++) {
+	pmPeak *peak = peaks->data[i];
+	if (peak == NULL) continue;
+	fprintf (f, "%5d %5d  %7.1f\n", 
+		 peak->x, peak->y, peak->counts); 
+    }
+    fclose (f);
+    return true;
+}
+
 // translations between psphot object types and dophot object types
 int pmSourceDophotType (pmSource *source) {
