Index: trunk/psphot/src/output.c
===================================================================
--- trunk/psphot/src/output.c	(revision 4641)
+++ trunk/psphot/src/output.c	(revision 4901)
@@ -3,49 +3,89 @@
 // output functions: we have several fixed modes (sx, obj, cmp)
 
+static int GetDophotType (psSource *source) {
+
+    switch (source->type) {
+
+      case PS_SOURCE_DEFECT:
+      case PS_SOURCE_SATURATED:
+	return (8);
+
+      case PS_SOURCE_SATSTAR:
+	return (10);
+
+      case PS_SOURCE_PSFSTAR:
+      case PS_SOURCE_GOODSTAR:
+	return (1);
+
+      case PS_SOURCE_POOR_FIT_PSF:
+	return (7);
+
+      case PS_SOURCE_FAIL_FIT_PSF:
+      case PS_SOURCE_FAINTSTAR:
+	return (4);
+
+      case PS_SOURCE_GALAXY:
+      case PS_SOURCE_FAINT_GALAXY:
+      case PS_SOURCE_DROP_GALAXY:
+      case PS_SOURCE_FAIL_FIT_GAL:
+      case PS_SOURCE_POOR_FIT_GAL:
+	return (2);
+
+      case PS_SOURCE_OTHER:
+      default:
+	return (0);
+    }
+    return (0);
+}
+
 void output (psImageData *imdata, psMetadata *config, psArray *sources) {
 
-  char *outputMode = psMetadataLookupPtr (&status, config, "OUTPUT_MODE");
-
-  if (outputMode == NULL) {
-    WriteSourcesText (imdata, config, sources);
+    bool status;
+
+    char *outputMode = psMetadataLookupPtr (&status, config, "OUTPUT_MODE");
+
+    if (outputMode == NULL) {
+	WriteSourcesText (imdata, config, sources);
+	exit (0);
+    }
+
+    if (!strcasecmp (outputMode, "OBJ")) {
+	WriteSourcesOBJ (imdata, config, sources);
+	exit (0);
+    }
+  
+    if (!strcasecmp (outputMode, "SX")) {
+	WriteSourcesSX (imdata, config, sources);
+	exit (0);
+    }
+  
+    if (!strcasecmp (outputMode, "CMP")) {
+	WriteSourcesCMP (imdata, config, sources);
+	exit (0);
+    }
+  
+    if (!strcasecmp (outputMode, "CMF")) {
+	WriteSourcesCMF (imdata, config, sources);
+	exit (0);
+    }
+  
+    psAbort ("psphot", "unknown output mode %s", outputMode);
+}
+
+bool WriteSourcesText (psImageData *imdata, psMetadata *config, psArray *sources) {
+
+    bool status;
+
+    char *filename = psMetadataLookupPtr (&status, config, "OUTPUT");
+
+    DumpImage (imdata->image, filename);
+
+    // get these names from config?
+    DumpModelPSF (sources, "psfsources.dat");
+    DumpModelFLT (sources, "fltsources.dat");
+    DumpModelNULL (sources, "nullsources.dat");
+    DumpMoments (sources, "moments.dat");
+
     exit (0);
-  }
-
-  if (!strcasecmp (outputMode, "OBJ")) {
-    WriteSourcesOBJ (imdata, config, sources);
-    exit (0);
-  }
-  
-  if (!strcasecmp (outputMode, "SX")) {
-    WriteSourcesSX (imdata, config, sources);
-    exit (0);
-  }
-  
-  if (!strcasecmp (outputMode, "CMP")) {
-    WriteSourcesCMP (imdata, config, sources);
-    exit (0);
-  }
-  
-  if (!strcasecmp (outputMode, "CMF")) {
-    WriteSourcesCMF (imdata, config, sources);
-    exit (0);
-  }
-  
-  psAbort ("psphot", "unknown output mode %s", outputMode);
-}
-
-bool WriteSourceText (psImageData *imdata, psMetadata *config, psArray *sources) {
-
-  char *filename = psMetadataLookupPtr (&status, config, "OUTPUT");
-
-  DumpImage (imdata->image, filename);
-
-  // get these names from config?
-  DumpModelPSF (sources, "psfsources.dat");
-  DumpModelFLT (sources, "fltsources.dat");
-  DumpModelNULL (sources, "nullsources.dat");
-  DumpMoments (sources, "moments.dat");
-
-  exit (0);
 }
 
@@ -53,9 +93,62 @@
 bool WriteSourcesOBJ (psImageData *imdata, psMetadata *config, psArray *sources) {
 
-# define NCHAR 104
-
-    char line[NCHAR];
-
-    char *filename = psMetadataLookupPtr (&status, config, "OUTPUT");
+    int i, type;
+    bool status;
+    psModel *model;
+    psF32 *PAR, *dPAR;
+    float dmag, apMag, fitMag;
+
+    char *filename = psMetadataLookupPtr (&status, config, "OUTPUT");
+    psLine *line = psLineAlloc (104);  // 104 is dophot-defined line length
+
+    FILE *f = fopen (filename, "w");
+    if (f == NULL) {
+	psLogMsg ("WriteSourceOBJ", 3, "can't open output file for output %s\n", filename);
+	return false;
+    }
+
+    // write sources with models first 
+    for (i = 0; i < sources->n; i++) {
+	psSource *source = (psSource *) sources->data[i];
+	model = source->modelPSF;
+	if (model == NULL) continue;
+	PAR = model->params->data.F32;
+	dPAR = model->dparams->data.F32;
+
+	pmSourcePhotometry (&fitMag, &apMag, model, source->pixels, source->mask);
+	dmag = dPAR[1] / PAR[1];
+
+	type = GetDophotType (source);
+	psLineInit (line);
+	psLineAdd (line, "%3d", type);
+	psLineAdd (line, "%8.2f", PAR[2]);
+	psLineAdd (line, "%8.2f", PAR[3]);
+	psLineAdd (line, "%8.3f", fitMag);
+	psLineAdd (line, "%6.3f", dmag);
+	psLineAdd (line, "%9.2f", PAR[0]);
+	psLineAdd (line, "%9.3f", PAR[4]);
+	psLineAdd (line, "%9.3f", PAR[5]);
+	psLineAdd (line, "%7.2f", PAR[6]);
+	psLineAdd (line, "%8.3f", 32.0);
+	psLineAdd (line, "%8.3f", apMag);
+	psLineAdd (line, "%8.2f", apMag - fitMag);
+	fwrite (line->line, 1, line->Nline, f);
+    }
+    fclose (f);
+    return true;
+
+}
+
+// elixir/sextractor-style output list with fixed line width
+bool WriteSourcesSX (psImageData *imdata, psMetadata *config, psArray *sources) {
+
+    int i, type;
+    bool status;
+    psModel *model;
+    psF32 *PAR, *dPAR;
+    float dmag, apMag, fitMag;
+
+    char *filename = psMetadataLookupPtr (&status, config, "OUTPUT");
+    psLine *line = psLineAlloc (104);  // 104 is dophot-defined line length
 
     FILE *f = fopen (filename, "w");
@@ -70,38 +163,42 @@
 	model = (psModel  *) source->modelPSF;
 	if (model == NULL) continue;
-	params = model->params;
-	dparams = model->dparams;
+	PAR = model->params->data.F32;
+	dPAR = model->dparams->data.F32;
 
 	pmSourcePhotometry (&fitMag, &apMag, model, source->pixels, source->mask);
-	dmag = dparams[0].data.F32[1] / params[0].data.F32[1];
-
+	dmag = dPAR[1] / PAR[1];
+
+	// XXX EAM : fix this to match sextractor output
 	type = GetDophotType (source);
-	init_line (line, &Nline);
-	add_to_line (line, &Nline, "%3d", type);
-	add_to_line (line, &Nline, "%8.2f", params[0].data.F32[2]);
-	add_to_line (line, &Nline, "%8.2f", params[0].data.F32[3]);
-	add_to_line (line, &Nline, "%8.3f", fitMag);
-	add_to_line (line, &Nline, "%6.3f", dmag);
-	add_to_line (line, &Nline, "%9.2f", params[0].data.F32[0]);
-	add_to_line (line, &Nline, "%9.3f", params[0].data.F32[4]);
-	add_to_line (line, &Nline, "%9.3f", params[0].data.F32[5]);
-	add_to_line (line, &Nline, "%7.2f", params[0].data.F32[6]);
-	add_to_line (line, &Nline, "%8.3f", 32.0);
-	add_to_line (line, &Nline, "%8.3f", apMag);
-	add_to_line (line, &Nline, "%8.2f", apMag - fitMag);
+	psLineInit (line);
+	psLineAdd (line, "%3d", type);
+	psLineAdd (line, "%8.2f", PAR[2]);
+	psLineAdd (line, "%8.2f", PAR[3]);
+	psLineAdd (line, "%8.3f", fitMag);
+	psLineAdd (line, "%6.3f", dmag);
+	psLineAdd (line, "%9.2f", PAR[0]);
+	psLineAdd (line, "%9.3f", PAR[4]);
+	psLineAdd (line, "%9.3f", PAR[5]);
+	psLineAdd (line, "%7.2f", PAR[6]);
+	psLineAdd (line, "%8.3f", 32.0);
+	psLineAdd (line, "%8.3f", apMag);
+	psLineAdd (line, "%8.2f", apMag - fitMag);
+	fwrite (line->line, 1, line->Nline, f);
     }
     fclose (f);
     return true;
-
-}
-
-// elixir/sextractor-style output list with fixed line width
-bool WriteSourcesSX (psImageData *imdata, psMetadata *config, psArray *sources) {
-
-# define NCHAR 104
-
-    char line[NCHAR];
-
-    char *filename = psMetadataLookupPtr (&status, config, "OUTPUT");
+}
+
+// elixir-style pseudo FITS table (header + ascii list)
+bool WriteSourcesCMP (psImageData *imdata, psMetadata *config, psArray *sources) {
+
+    int i, type;
+    bool status;
+    psModel *model;
+    psF32 *PAR, *dPAR;
+    float dmag, apMag, fitMag;
+
+    char *filename = psMetadataLookupPtr (&status, config, "OUTPUT");
+    psLine *line = psLineAlloc (104);  // 104 is dophot-defined line length
 
     FILE *f = fopen (filename, "w");
@@ -110,4 +207,6 @@
 	return false;
     }
+
+    // write imdata->header to file
 
     // write sources with models first 
@@ -116,75 +215,26 @@
 	model = (psModel  *) source->modelPSF;
 	if (model == NULL) continue;
-	params = model->params;
-	dparams = model->dparams;
+	PAR = model->params->data.F32;
+	dPAR = model->dparams->data.F32;
 
 	pmSourcePhotometry (&fitMag, &apMag, model, source->pixels, source->mask);
-	dmag = dparams[0].data.F32[1] / params[0].data.F32[1];
+	dmag = dPAR[1] / PAR[1];
 
 	// XXX EAM : fix this to match sextractor output
 	type = GetDophotType (source);
-	init_line (line, &Nline);
-	add_to_line (line, &Nline, "%3d", type);
-	add_to_line (line, &Nline, "%8.2f", params[0].data.F32[2]);
-	add_to_line (line, &Nline, "%8.2f", params[0].data.F32[3]);
-	add_to_line (line, &Nline, "%8.3f", fitMag);
-	add_to_line (line, &Nline, "%6.3f", dmag);
-	add_to_line (line, &Nline, "%9.2f", params[0].data.F32[0]);
-	add_to_line (line, &Nline, "%9.3f", params[0].data.F32[4]);
-	add_to_line (line, &Nline, "%9.3f", params[0].data.F32[5]);
-	add_to_line (line, &Nline, "%7.2f", params[0].data.F32[6]);
-	add_to_line (line, &Nline, "%8.3f", 32.0);
-	add_to_line (line, &Nline, "%8.3f", apMag);
-	add_to_line (line, &Nline, "%8.2f", apMag - fitMag);
-    }
-    fclose (f);
-    return true;
-
-}
-
-// elixir-style pseudo FITS table (header + ascii list)
-bool WriteSourcesCMP (psImageData *imdata, psMetadata *config, psArray *sources) {
-
-
-# define NCHAR 104
-
-    char line[NCHAR];
-
-    char *filename = psMetadataLookupPtr (&status, config, "OUTPUT");
-
-    FILE *f = fopen (filename, "w");
-    if (f == NULL) {
-	psLogMsg ("WriteSourceOBJ", 3, "can't open output file for output %s\n", filename);
-	return false;
-    }
-
-    // write imdata->header to file
-
-    // write sources with models first 
-    for (i = 0; i < sources->n; i++) {
-	psSource *source = (psSource *) sources->data[i];
-	model = (psModel  *) source->modelPSF;
-	if (model == NULL) continue;
-	params = model->params;
-	dparams = model->dparams;
-
-	pmSourcePhotometry (&fitMag, &apMag, model, source->pixels, source->mask);
-	dmag = dparams[0].data.F32[1] / params[0].data.F32[1];
-
-	// XXX EAM : fix this to match sextractor output
-	type = GetDophotType (source);
-	init_line (line, &Nline);
-	add_to_line (line, &Nline, "%3d", type);
-	add_to_line (line, &Nline, "%8.2f", params[0].data.F32[2]);
-	add_to_line (line, &Nline, "%8.2f", params[0].data.F32[3]);
-	add_to_line (line, &Nline, "%8.3f", fitMag);
-	add_to_line (line, &Nline, "%6.3f", dmag);
-	add_to_line (line, &Nline, "%9.2f", params[0].data.F32[0]);
-	add_to_line (line, &Nline, "%9.3f", params[0].data.F32[4]);
-	add_to_line (line, &Nline, "%9.3f", params[0].data.F32[5]);
-	add_to_line (line, &Nline, "%7.2f", params[0].data.F32[6]);
-	add_to_line (line, &Nline, "%8.3f", 32.0);
-	add_to_line (line, &Nline, "%8.3f", apMag);
-	add_to_line (line, &Nline, "%8.2f", apMag - fitMag);
+	psLineInit (line);
+	psLineAdd (line, "%3d", type);
+	psLineAdd (line, "%8.2f", PAR[2]);
+	psLineAdd (line, "%8.2f", PAR[3]);
+	psLineAdd (line, "%8.3f", fitMag);
+	psLineAdd (line, "%6.3f", dmag);
+	psLineAdd (line, "%9.2f", PAR[0]);
+	psLineAdd (line, "%9.3f", PAR[4]);
+	psLineAdd (line, "%9.3f", PAR[5]);
+	psLineAdd (line, "%7.2f", PAR[6]);
+	psLineAdd (line, "%8.3f", 32.0);
+	psLineAdd (line, "%8.3f", apMag);
+	psLineAdd (line, "%8.2f", apMag - fitMag);
+	fwrite (line->line, 1, line->Nline, f);
     }
     fclose (f);
@@ -195,4 +245,6 @@
 bool WriteSourcesCMF (psImageData *imdata, psMetadata *config, psArray *sources) {
 
+    bool status;
+
     char *filename = psMetadataLookupPtr (&status, config, "OUTPUT");
 
@@ -200,18 +252,5 @@
 
     // write FITS table to file (use autocode tools)
-}
-
-// we require a fixed number of chars already allocated
-init_line (char *line, int *Nline) {
-  *Nline = 0;
-}
-
-add_to_line (char *line, int *Nline, char *format, ...) {
-
-    va_list ap;
-
-    va_start (ap, format);
-    Nchar = vsprintf (&line[*Nline], format, ap);
-    *Nline += Nchar;
-}
-
+
+    return true;
+}
