Index: /trunk/ppImage/configure.ac
===================================================================
--- /trunk/ppImage/configure.ac	(revision 7507)
+++ /trunk/ppImage/configure.ac	(revision 7508)
@@ -18,11 +18,12 @@
   [AS_HELP_STRING(--enable-optimize,enable compiler optimization)],
   [AC_MSG_RESULT(compile optimization enabled)
-   CFLAGS="${CFLAGS=} -O2"],
+   CFLAGS="-O2"],
   [AC_MSG_RESULT([compile optimization disabled])
-   CFLAGS="${CFLAGS=} -O0 -g"]
+   CFLAGS="-O0 -g"]
 )
 
 PKG_CHECK_MODULES([PSLIB], [pslib >= 0.9.0]) 
 PKG_CHECK_MODULES([PSMODULE], [psmodule >= 0.0.0]) 
+PKG_CHECK_MODULES([PSPHOT], [psphot >= 0.0.0]) 
 
 CFLAGS="${CFLAGS} -Wall -Werror -std=c99"
Index: /trunk/ppImage/src/Makefile.am
===================================================================
--- /trunk/ppImage/src/Makefile.am	(revision 7507)
+++ /trunk/ppImage/src/Makefile.am	(revision 7508)
@@ -6,6 +6,6 @@
 	ppMem.h
 
-ppImage_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(ppImage_CFLAGS)
-ppImage_LDFLAGS = $(PSLIB_LIBS) $(PSMODULE_LIBS)
+ppImage_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(PSPHOT_CFLAGS) $(ppImage_CFLAGS)
+ppImage_LDFLAGS = $(PSLIB_LIBS) $(PSMODULE_LIBS) $(PSPHOT_LIBS)
 ppImage_SOURCES = \
 	ppImage.c \
@@ -15,9 +15,10 @@
 	ppImageLoop.c \
 	ppImageDetrendReadout.c \
-	ppImageDetrendMask.c \
 	ppImageDetrendBias.c \
 	ppImageDetrendNonLinear.c \
 	ppImageRebinReadout.c \
+	ppImagePhot.c \
 	ppMem.c
+
 #	ppImageParseCamera.c \
 #	ppDetrendFlat.c \
Index: /trunk/ppImage/src/ppImage.h
===================================================================
--- /trunk/ppImage/src/ppImage.h	(revision 7507)
+++ /trunk/ppImage/src/ppImage.h	(revision 7508)
@@ -5,4 +5,5 @@
 #include "pslib.h"
 #include "psmodules.h"
+#include "psphot.h"
 #include "ppImageOptions.h"
 #include "ppMem.h"
@@ -32,3 +33,5 @@
 bool ppImageRebinReadout (pmConfig *config, pmFPAview *view, char *filename);
 
+bool ppImagePhotom (pmConfig *config, pmFPAview *view);
+
 #endif // Pau.
Index: /trunk/ppImage/src/ppImageDetrendMask.c
===================================================================
--- /trunk/ppImage/src/ppImageDetrendMask.c	(revision 7507)
+++ /trunk/ppImage/src/ppImageDetrendMask.c	(revision 7508)
@@ -32,2 +32,8 @@
 // by identifying the image type as a mask (in pmFPAfile).
 // currently, pmFPAfileReadImage converts the image to F32
+
+
+// XXX this function needs to use pmReadoutSetMask to construct the initial mask
+// then the pixels will correspond and pmMaskBadPixels can be correctly applied.
+// this function should not have to lookup the value of CELL.SATURATION, nor should it have to 
+// apply it.  
Index: /trunk/ppImage/src/ppImageDetrendReadout.c
===================================================================
--- /trunk/ppImage/src/ppImageDetrendReadout.c	(revision 7507)
+++ /trunk/ppImage/src/ppImageDetrendReadout.c	(revision 7508)
@@ -10,10 +10,13 @@
     // find the currently selected readout
     pmReadout *input = pmFPAfileThisReadout (config->files, view, "PPIMAGE.INPUT");
-    pmReadoutSetWeight (input);
 
-    // Mask bad pixels
+    // create the target mask and weight images
+    pmReadoutSetMaskWeight (input);
+
+    // apply the externally supplied mask to the input->mask pixels
     if (options->doMask) {
         pmReadout *mask = pmFPAfileThisReadout (config->files, detview, "PPIMAGE.MASK");
-        ppImageDetrendMask(input, mask);
+	pmMaskBadPixels (input, mask, options->maskValue);
+        // ppImageDetrendMask(input, mask);
     }
 
Index: /trunk/ppImage/src/ppImageLoop.c
===================================================================
--- /trunk/ppImage/src/ppImageLoop.c	(revision 7507)
+++ /trunk/ppImage/src/ppImageLoop.c	(revision 7508)
@@ -37,11 +37,6 @@
 		ppImageDetrendReadout (options, config, view);
 
-		if (options->doBin1) {
-		    ppImageRebinReadout (config, view, "PPIMAGE.BIN1");
-		}
-
-		if (options->doBin2) {
-		    ppImageRebinReadout (config, view, "PPIMAGE.BIN2");
-		}
+		if (options->doBin1) ppImageRebinReadout (config, view, "PPIMAGE.BIN1");
+		if (options->doBin2) ppImageRebinReadout (config, view, "PPIMAGE.BIN2");
 
 		pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
@@ -51,6 +46,7 @@
 
 	// ppImageChipMosaic (config, view);
-	// ppImagePSPhot (config, view);
-	// ppImagePSAstro (config, view);
+
+	// we perform photometry on the readouts of this chip in the output
+	ppImagePhotom (config, view);
 
 	pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
@@ -66,4 +62,6 @@
     }
 
+    // ppImageAstrom (config);
+    
     // ppImageFPAMosaic (config, view);
     // ppImageJpegFPA (config, view);
@@ -75,2 +73,4 @@
 }
 
+// input image is: PPIMAGE.INPUT
+// output image is: PPIMAGE.OUTPUT
Index: /trunk/ppImage/src/ppImageOptions.c
===================================================================
--- /trunk/ppImage/src/ppImageOptions.c	(revision 7507)
+++ /trunk/ppImage/src/ppImageOptions.c	(revision 7508)
@@ -28,4 +28,6 @@
     // default flags for various activities
     options->doMask   	= false;	// Mask bad pixels
+    options->maskValue  = 0xff;         // Default mask value
+
     options->doNonLin 	= false;	// Non-linearity correction
     options->doBias   	= false;	// Bias subtraction
@@ -138,4 +140,9 @@
     // Mask recipe options
     options->doMask = psMetadataLookupBool(NULL, recipe, "MASK");
+    psMaskType maskValue = psMetadataLookupU8(&status, recipe, "MASK.VALUE");
+    if (status) {
+	options->maskValue = maskValue;
+    }
+
     options->doBias = psMetadataLookupBool(NULL, recipe, "BIAS");
     options->doDark = psMetadataLookupBool(NULL, recipe, "DARK");
Index: /trunk/ppImage/src/ppImageOptions.h
===================================================================
--- /trunk/ppImage/src/ppImageOptions.h	(revision 7507)
+++ /trunk/ppImage/src/ppImageOptions.h	(revision 7508)
@@ -5,4 +5,6 @@
 typedef struct {
     bool doMask;                        // Mask bad pixels
+    psMaskType maskValue;               // apply this bit-mask to choose masked bits
+
     bool doBias;                        // Bias subtraction
     bool doDark;                        // Dark subtraction
Index: /trunk/ppImage/src/ppImagePhot.c
===================================================================
--- /trunk/ppImage/src/ppImagePhot.c	(revision 7507)
+++ /trunk/ppImage/src/ppImagePhot.c	(revision 7508)
@@ -1,34 +1,93 @@
-#include <stdio.h>
-#include "pslib.h"
-#include "psmodules.h"
-#include "ppImage.h"
+# include "ppImage.h"
 
-bool ppImagePhot(ppData *data, ppOptions *options, pmConfig *config)
-{
-    ppFile *input = data->input;        // The input file information
-    pmFPA *fpa = input->fpa;       // The input FPA
+bool ppImagePhotom (pmConfig *config, pmFPAview *view) {
 
-    int numMosaicked = pmFPAMosaicCells(fpa, 1, 1); // Number of chips mosaicked together
-    psLogMsg(__func__, PS_LOG_INFO, "%d chips mosaicked.\n", numMosaicked);
+    bool status;
+    pmCell *cell;
+    pmReadout *readout;
 
-#if 1
-    // Write out the mosaicked chip, just to see; this wouldn't normally happen
-    psFits *mosaicFile = psFitsOpen("mosaic.fits", "w");
-    psArray *chips = fpa->chips;
-    for (int i = 0; i < chips->n; i++) {
-        pmChip *chip = chips->data[i];
-        if (! chip || ! chip->exists || ! chip->process) {
-            continue;
-        }
-        psArray *cells = chip->cells;
-        pmCell *cell = cells->data[0];
-        psArray *readouts = cell->readouts;
-        pmReadout *readout = readouts->data[0];
-        psImage *image = readout->image;
-        psFitsWriteImage(mosaicFile, NULL, image, 0);
+    psphotModelGroupInit ();
+
+    // select recipe options supplied on command line
+    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes,   "PSPHOT");
+
+    // set default recipe values here
+    // XXX place this in a psphot library function?
+    psMetadataAddStr (recipe, PS_LIST_TAIL, "FITMODE",         PS_META_NO_REPLACE, "default fitting mode", "NONE");
+    psMetadataAddStr (recipe, PS_LIST_TAIL, "PHOTCODE",        PS_META_NO_REPLACE, "default photcode",     "NONE");
+    psMetadataAddStr (recipe, PS_LIST_TAIL, "BREAK_POINT",     PS_META_NO_REPLACE, "default break point",  "NONE");
+    psMetadataAddF32 (recipe, PS_LIST_TAIL, "ZERO_PT",         PS_META_NO_REPLACE, "default zero point",    25.00);
+    psMetadataAddS32 (recipe, PS_LIST_TAIL, "BACKGROUND.XBIN", PS_META_NO_REPLACE, "default binning",          64);
+    psMetadataAddS32 (recipe, PS_LIST_TAIL, "BACKGROUND.YBIN", PS_META_NO_REPLACE, "default binning",          64);
+
+    // find or define a pmFPAfile PSPHOT.INPUT
+    pmFPAfile *input = psMetadataLookupPtr (&status, config->files, "PSPHOT.INPUT");
+    if (!status) {
+
+	// psphotReadout requires a pmFPAfile supplied with the name PSPHOT.INPUT
+	// create a pmFPAfile which points at PPIMAGE.OUTPUT
+	// mode is 'REFERENCE' to prevent double frees of the fpa
+	pmFPAfile *output = psMetadataLookupPtr (&status, config->files, "PPIMAGE.OUTPUT");
+	input = pmFPAfileDefine (config->files, config->camera, output->fpa, "PSPHOT.INPUT");
+	input->mode = PM_FPA_MODE_REFERENCE;
+
+	pmFPAfileDefine (config->files, config->camera, input->fpa, "PSPHOT.OUTPUT");
+
+	// supply the output name (from cmd-line) to all output (WRITE) files
+	// XXX does this cause trouble with existing files?
+	char *outname = psMetadataLookupPtr(&status, config->arguments, "OUTPUT");
+	pmFPAfileAddFileNames (config->files, "OUTPUT", outname, PM_FPA_MODE_WRITE);
     }
-#endif
 
-    // XXX EAM: Insert psphot stuff here
+    // XXX add the option output files here
+
+    // int DX = psMetadataLookupS32 (&status, recipe, "BACKGROUND.XBIN");
+    // int DY = psMetadataLookupS32 (&status, recipe, "BACKGROUND.YBIN");
+
+    // we only was to operate on PSPHOT pmFPAfiles here:
+    pmFPAfileActivate (config->files, false, NULL);
+    pmFPAfileActivate (config->files, true, "PSPHOT.INPUT");
+    pmFPAfileActivate (config->files, true, "PSPHOT.RESID");
+    pmFPAfileActivate (config->files, true, "PSPHOT.OUTPUT");
+
+    pmFPAfileActivate (config->files, true, "PSPHOT.BACKSUB");
+    pmFPAfileActivate (config->files, true, "PSPHOT.BACKGND");
+    pmFPAfileActivate (config->files, true, "PSPHOT.BACKMDL");
+
+
+    // iterate over the cells in the current chip
+    // view->cell = -1;
+
+    while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
+	psLogMsg ("ppImagePhot", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
+	if (! cell->process || ! cell->file_exists) { continue; }
+	pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
+
+	// process each of the readouts
+	while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) {
+	    pmFPAfileIOChecks (config->files, view, PM_FPA_BEFORE);
+	    if (! readout->data_exists) { continue; }
+
+	    // run the actual photometry analysis
+	    psphotReadout (config, view);
+
+	    pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+	}
+	pmFPAfileIOChecks (config->files, view, PM_FPA_AFTER);
+    }
+
+    // de-activate the PSPHOT image files, activate the PPIMAGE ones
+    pmFPAfileActivate (config->files, false, NULL);
+    pmFPAfileActivate (config->files, true, "PPIMAGE.INPUT");
+    pmFPAfileActivate (config->files, true, "PPIMAGE.BIAS");
+    pmFPAfileActivate (config->files, true, "PPIMAGE.DARK");
+    pmFPAfileActivate (config->files, true, "PPIMAGE.MASK");
+    pmFPAfileActivate (config->files, true, "PPIMAGE.FLAT");
+    pmFPAfileActivate (config->files, true, "PPIMAGE.OUTPUT");
+
+    pmFPAfileActivate (config->files, true, "PPIMAGE.BIN1");
+    pmFPAfileActivate (config->files, true, "PPIMAGE.JPEG1");
+    pmFPAfileActivate (config->files, true, "PPIMAGE.BIN2");
+    pmFPAfileActivate (config->files, true, "PPIMAGE.JPEG2");
 
     return true;
