Index: /trunk/ppImage/src/Makefile.am
===================================================================
--- /trunk/ppImage/src/Makefile.am	(revision 7770)
+++ /trunk/ppImage/src/Makefile.am	(revision 7771)
@@ -21,5 +21,6 @@
 	ppImageMosaic.c \
 	ppImagePhotom.c \
-	ppImageAstrom.c
+	ppImageAstrom.c \
+	ppImageAddstar.c
 
 ppFocus_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(PSPHOT_CFLAGS) $(PSASTRO_CFLAGS) $(ppImage_CFLAGS)
@@ -40,5 +41,6 @@
 	ppImageMosaic.c \
 	ppImagePhotom.c \
-	ppImageAstrom.c
+	ppImageAstrom.c \
+	ppImageAddstar.c
 
 ppTest_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(ppImage_CFLAGS)
Index: /trunk/ppImage/src/ppImage.h
===================================================================
--- /trunk/ppImage/src/ppImage.h	(revision 7770)
+++ /trunk/ppImage/src/ppImage.h	(revision 7771)
@@ -39,4 +39,5 @@
 bool ppImagePhotom (pmConfig *config, pmFPAview *view);
 bool ppImageAstrom (pmConfig *config);
+bool ppImageAddstar (pmConfig *config);
 
 bool ppImageMosaicChip (pmConfig *config, const pmFPAview *view, char *outFile, char *inFile);
Index: /trunk/ppImage/src/ppImageAddstar.c
===================================================================
--- /trunk/ppImage/src/ppImageAddstar.c	(revision 7771)
+++ /trunk/ppImage/src/ppImageAddstar.c	(revision 7771)
@@ -0,0 +1,115 @@
+# include "ppImage.h"
+
+bool setFilename (pmFPAfile *file, pmFPAview *view);
+bool addstarFile (char *addstarCommand, char *filename);
+
+bool ppImageAddstar (pmConfig *config) {
+
+    bool status;
+    pmChip *chip;
+    pmCell *cell;
+    pmReadout *readout;
+
+    // select recipe options supplied on command line
+    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, RECIPE_NAME);
+
+    // find a pmFPAfile PSASTRO.OUTPUT
+    pmFPAfile *file = psMetadataLookupPtr (&status, config->files, "PSASTRO.OUTPUT");
+    if (!status) {
+	psError (PS_ERR_IO, true, "no PSASTRO.OUTPUT file defined");
+	return false;
+    }
+
+    // find the addstar command: %s is replaced with name of output file
+    char *addstarCommand = psMetadataLookupStr (&status, recipe, "ADDSTAR.COMMAND");
+
+    pmFPAview *view = pmFPAviewAlloc (0);
+    if (file->fileLevel == PM_FPA_LEVEL_CHIP) {
+	// call addstar on this file
+	setFilename (file, view);
+	addstarFile (addstarCommand, file->filename);
+	return true;
+    }
+
+    while ((chip = pmFPAviewNextChip (view, file->fpa, 1)) != NULL) {
+        psLogMsg ("ppImageAddstar", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
+        if (!chip->process || !chip->file_exists) { continue; }
+
+	if (file->fileLevel == PM_FPA_LEVEL_CHIP) {
+	    // call addstar on this file
+	    setFilename (file, view);
+	    addstarFile (addstarCommand, file->filename);
+	    continue;
+	}
+
+	while ((cell = pmFPAviewNextCell (view, file->fpa, 1)) != NULL) {
+	    psLogMsg ("ppImageAddstar", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
+	    if (! cell->process || ! cell->file_exists) { continue; }
+
+	    if (file->fileLevel == PM_FPA_LEVEL_CELL) {
+		// call addstar on this file
+		setFilename (file, view);
+		addstarFile (addstarCommand, file->filename);
+		continue;
+	    }
+
+	    // process each of the readouts
+	    while ((readout = pmFPAviewNextReadout (view, file->fpa, 1)) != NULL) {
+		if (! readout->data_exists) { continue; }
+
+		if (file->fileLevel == PM_FPA_LEVEL_READOUT) {
+		    // call addstar on this file
+		    setFilename (file, view);
+		    addstarFile (addstarCommand, file->filename);
+		    continue;
+		} else {
+		    psError (PS_ERR_IO, true, "inconsistent fileLevel for %s: %d\n", file->name, file->fileLevel);
+		    return false;
+		}
+	    }
+	}
+    }
+    return true;
+}
+
+bool setFilename (pmFPAfile *file, pmFPAview *view) {
+
+    // determine the file name
+    // free a name allocated earlier
+    psFree (file->filename);
+    file->filename = pmFPAfileNameFromRule (file->filerule, file, view);
+    if (file->filename == NULL) {
+        psError(PS_ERR_IO, true, "Filename is NULL");
+        return false;
+    }
+
+    // indirect filenames are not allowed for output
+    if (!strcasecmp (file->filename, "@FILES")) {
+	psError(PS_ERR_IO, true, "indirect filenames are not allowed for output : %s\n", file->name);
+	return false;
+    }
+    if (!strcasecmp (file->filename, "@DETDB")) {
+	psError(PS_ERR_IO, true, "detrend db filenames are not allowed for output : %s\n", file->name);
+	return false;
+    }
+    return true;
+}
+
+bool addstarFile (char *addstarCommand, char *filename) {
+
+    bool status;
+    char *addstarLine = psStringSubstitute (addstarCommand, filename, "%s");
+	
+    // catch addstar stderr/stdout and do what?
+    psIOBuffer *buffer = psIOBufferAlloc (512);
+    psPipe *pipe = psPipeOpen (addstarLine);
+    status = psIOBufferReadEmpty (buffer, 100, pipe->stdout);
+    if (!status) {
+	psError (PS_ERR_IO, false, "detselect is not responding");
+    }
+    psFree (addstarLine);
+    psFree (buffer);
+    psPipeClose (pipe);
+    psFree (pipe);
+    return status;
+}
Index: /trunk/ppImage/src/ppImageLoop.c
===================================================================
--- /trunk/ppImage/src/ppImageLoop.c	(revision 7770)
+++ /trunk/ppImage/src/ppImageLoop.c	(revision 7771)
@@ -58,4 +58,5 @@
     // we perform astrometry on all chips after sources have been detected
     if (options->doAstromChip || options->doAstromMosaic) ppImageAstrom (config);
+    if (options->doAddstar) ppImageAddstar (config);
 
     if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) return false;
Index: /trunk/ppImage/src/ppImageOptions.c
===================================================================
--- /trunk/ppImage/src/ppImageOptions.c	(revision 7770)
+++ /trunk/ppImage/src/ppImageOptions.c	(revision 7771)
@@ -188,4 +188,10 @@
     }
 
+    options->doAddstar = psMetadataLookupBool(NULL, recipe, "ADDSTAR");
+    if (options->doAddstar && !(options->doAstromChip || options->doAstromMosaic)) {
+	psLogMsg(__func__, PS_LOG_ERROR, "Invalid Phase2 Options: cannot Addstar without Astrometry");
+	exit(EXIT_FAILURE);
+    }
+
     return options;
 }
Index: /trunk/ppImage/src/ppImageOptions.h
===================================================================
--- /trunk/ppImage/src/ppImageOptions.h	(revision 7770)
+++ /trunk/ppImage/src/ppImageOptions.h	(revision 7771)
@@ -14,4 +14,5 @@
     bool doAstromChip;                  // per-chip Astrometry
     bool doAstromMosaic;                // full-mosaic Astrometry
+    bool doAddstar;			// add results to object database?
 
     bool doOverscan;                    // Overscan subtraction
