Index: trunk/ppImage/src/ppImagePhot.c
===================================================================
--- trunk/ppImage/src/ppImagePhot.c	(revision 6747)
+++ 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;
