Index: branches/tap_branches/ppStack/src/ppStackCamera.c
===================================================================
--- branches/tap_branches/ppStack/src/ppStackCamera.c	(revision 25900)
+++ branches/tap_branches/ppStack/src/ppStackCamera.c	(revision 27838)
@@ -30,9 +30,9 @@
         pmFPAfileDefineFromArgs(&found, config, name, "FILENAMES");
     if (!file || !found) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to define file %s from %s", name, filename);
+        psError(psErrorCodeLast(), false, "Unable to define file %s from %s", name, filename);
         return NULL;
     }
     if (file->type != type) {
-        psError(PS_ERR_IO, true, "%s is not of type %s", name, pmFPAfileStringFromType(type));
+        psError(PS_ERR_IO, PPSTACK_ERR_CONFIG, "%s is not of type %s", name, pmFPAfileStringFromType(type));
         return NULL;
     }
@@ -53,5 +53,5 @@
     psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSTACK_RECIPE); // Recipe for ppSim
     if (!recipe) {
-        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find recipe %s", PPSTACK_RECIPE);
+        psError(PPSTACK_ERR_CONFIG, false, "Unable to find recipe %s", PPSTACK_RECIPE);
         return false;
     }
@@ -67,5 +67,5 @@
                                                            "PPSTACK.INPUT.MASK"); // Input masks
         if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to define input masks from RUN metadata.");
+            psError(psErrorCodeLast(), false, "Unable to define input masks from RUN metadata.");
             psFree(runImages);
             return false;
@@ -76,5 +76,5 @@
                                                           "PPSTACK.INPUT.VARIANCE"); // Input variances
         if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to define input variances from RUN metadata.");
+            psError(psErrorCodeLast(), false, "Unable to define input variances from RUN metadata.");
             psFree(runImages);
             return false;
@@ -88,10 +88,10 @@
                                                          "PPSTACK.INPUT.SOURCES"); // Input sources
         if (!status) {
-            psError(PS_ERR_UNKNOWN, false, "Unable to define input sources from RUN metadata.");
+            psError(psErrorCodeLast(), false, "Unable to define input sources from RUN metadata.");
             psFree(runImages);
             return false;
         }
         if (!runSrc) {
-            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to define input sources from RUN metadata.");
+            psError(PPSTACK_ERR_CONFIG, true, "Unable to define input sources from RUN metadata.");
             psFree(runImages);
             return false;
@@ -104,5 +104,5 @@
                                                          "PPSTACK.INPUT.PSF"); // Input PSFs
                 if (!status) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to define input PSFs from RUN metadata.");
+                    psError(psErrorCodeLast(), false, "Unable to define input PSFs from RUN metadata.");
                     psFree(runImages);
                     return false;
@@ -118,10 +118,11 @@
                                                                     "PPSTACK.CONV.KERNEL"); // Conv'n kernels
                 if (!status) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to define convolution kernels from RUN metadata.");
+                    psError(psErrorCodeLast(), false,
+                            "Unable to define convolution kernels from RUN metadata.");
                     psFree(runImages);
                     return false;
                 }
                 if (!runKernel) {
-                    psError(PS_ERR_UNEXPECTED_NULL, true,
+                    psError(PPSTACK_ERR_CONFIG, true,
                             "Unable to define convolution kernels from RUN metadata.");
                     psFree(runImages);
@@ -137,5 +138,5 @@
         psMetadata *inputs = psMetadataLookupMetadata(NULL, config->arguments, "INPUTS"); // The inputs info
         if (!inputs) {
-            psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find inputs.");
+            psError(PPSTACK_ERR_ARGUMENTS, false, "Unable to find inputs.");
             return false;
         }
@@ -145,5 +146,5 @@
         while ((item = psMetadataGetAndIncrement(iter))) {
             if (item->type != PS_DATA_METADATA) {
-                psError(PS_ERR_BAD_PARAMETER_TYPE, true,
+                psError(PPSTACK_ERR_ARGUMENTS, true,
                         "Component %s of the input metadata is not of type METADATA", item->name);
                 psFree(iter);
@@ -155,5 +156,5 @@
             psString image = psMetadataLookupStr(NULL, input, "IMAGE"); // Name of image
             if (!image || strlen(image) == 0) {
-                psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Component %s lacks IMAGE of type STR", item->name);
+                psError(PPSTACK_ERR_ARGUMENTS, true, "Component %s lacks IMAGE of type STR", item->name);
                 psFree(iter);
                 return false;
@@ -169,5 +170,5 @@
                                               image, PM_FPA_FILE_IMAGE); // File for image
             if (!imageFile) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to define file from image %d (%s)", i, image);
+                psError(psErrorCodeLast(), false, "Unable to define file from image %d (%s)", i, image);
                 return false;
             }
@@ -175,5 +176,5 @@
             if (mask && strlen(mask) > 0 &&
                 !defineFile(config, imageFile, "PPSTACK.INPUT.MASK", mask, PM_FPA_FILE_MASK)) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to define file from mask %d (%s)", i, mask);
+                psError(psErrorCodeLast(), false, "Unable to define file from mask %d (%s)", i, mask);
                 return false;
             }
@@ -183,5 +184,5 @@
                 if (!defineFile(config, imageFile, "PPSTACK.INPUT.VARIANCE", variance,
                                 PM_FPA_FILE_VARIANCE)) {
-                    psError(PS_ERR_UNKNOWN, false,
+                    psError(psErrorCodeLast(), false,
                             "Unable to define file from variance %d (%s)", i, variance);
                     return false;
@@ -195,19 +196,19 @@
                     havePSFs = true;
                     if (!defineFile(config, imageFile, "PPSTACK.INPUT.PSF", psf, PM_FPA_FILE_PSF)) {
-                        psError(PS_ERR_UNKNOWN, false, "Unable to define file from psf %d (%s)", i, psf);
+                        psError(psErrorCodeLast(), false, "Unable to define file from psf %d (%s)", i, psf);
                         return false;
                     }
                 }
             } else if (havePSFs) {
-                psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find PSF %d", i);
+                psError(PPSTACK_ERR_CONFIG, true, "Unable to find PSF %d", i);
                 return false;
             }
 
             if (!sources || strlen(sources) == 0) {
-                psError(PS_ERR_UNEXPECTED_NULL, true, "SOURCES not provided for file %d", i);
+                psError(PPSTACK_ERR_CONFIG, true, "SOURCES not provided for file %d", i);
                 return false;
             }
             if (!defineFile(config, imageFile, "PPSTACK.INPUT.SOURCES", sources, PM_FPA_FILE_CMF)) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to define file from sources %d (%s)",
+                psError(psErrorCodeLast(), false, "Unable to define file from sources %d (%s)",
                         i, sources);
                 return false;
@@ -217,5 +218,6 @@
                 pmFPAfile *kernel = pmFPAfileDefineOutput(config, imageFile->fpa, "PPSTACK.CONV.KERNEL");
                 if (!kernel) {
-                    psError(PS_ERR_IO, false, _("Unable to generate output file from PPSTACK.CONV.KERNEL"));
+                    psError(psErrorCodeLast(), false,
+                            "Unable to generate output file from PPSTACK.CONV.KERNEL");
                     return false;
                 }
@@ -233,24 +235,23 @@
 
     // Output image
-    pmFPA *fpa = pmFPAConstruct(config->camera, config->cameraName); // FPA to contain the output
-    if (!fpa) {
-        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to construct an FPA from camera configuration.");
-        return false;
-    }
-    pmFPAfile *output = pmFPAfileDefineOutput(config, fpa, "PPSTACK.OUTPUT");
-    psFree(fpa);                        // Drop reference
+    pmFPA *outFPA = pmFPAConstruct(config->camera, config->cameraName); // FPA to contain the output
+    if (!outFPA) {
+        psError(psErrorCodeLast(), false, "Unable to construct an FPA from camera configuration.");
+        return false;
+    }
+    pmFPAfile *output = pmFPAfileDefineOutput(config, outFPA, "PPSTACK.OUTPUT");
+    psFree(outFPA);                        // Drop reference
     if (!output) {
-        psError(PS_ERR_IO, false, _("Unable to generate output file from PPSTACK.OUTPUT"));
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.OUTPUT"));
         return false;
     }
     if (output->type != PM_FPA_FILE_IMAGE) {
-        psError(PS_ERR_IO, true, "PPSTACK.OUTPUT is not of type IMAGE");
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.OUTPUT is not of type IMAGE");
         return false;
     }
     output->save = true;
 
-    if (!pmFPAAddSourceFromFormat(fpa, "Stack", output->format)) {
-        psError(PS_ERR_UNKNOWN, false, "Unable to generate output FPA.");
-        psFree(fpa);
+    if (!pmFPAAddSourceFromFormat(outFPA, "Stack", output->format)) {
+        psError(psErrorCodeLast(), false, "Unable to generate output FPA.");
         return false;
     }
@@ -259,9 +260,9 @@
     pmFPAfile *outMask = pmFPAfileDefineOutput(config, output->fpa, "PPSTACK.OUTPUT.MASK");
     if (!outMask) {
-        psError(PS_ERR_IO, false, _("Unable to generate output file from PPSTACK.OUTPUT.MASK"));
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.OUTPUT.MASK"));
         return false;
     }
     if (outMask->type != PM_FPA_FILE_MASK) {
-        psError(PS_ERR_IO, true, "PPSTACK.OUTPUT.MASK is not of type MASK");
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.OUTPUT.MASK is not of type MASK");
         return false;
     }
@@ -272,9 +273,9 @@
         pmFPAfile *outVariance = pmFPAfileDefineOutput(config, output->fpa, "PPSTACK.OUTPUT.VARIANCE");
         if (!outVariance) {
-            psError(PS_ERR_IO, false, _("Unable to generate output file from PPSTACK.OUTPUT.VARIANCE"));
+            psError(psErrorCodeLast(), false, "Unable to generate output file from PPSTACK.OUTPUT.VARIANCE");
             return false;
         }
         if (outVariance->type != PM_FPA_FILE_VARIANCE) {
-            psError(PS_ERR_IO, true, "PPSTACK.OUTPUT.VARIANCE is not of type VARIANCE");
+            psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.OUTPUT.VARIANCE is not of type VARIANCE");
             return false;
         }
@@ -282,25 +283,177 @@
     }
 
+
+    // Exposure image
+    pmFPA *expFPA = pmFPAConstruct(config->camera, config->cameraName); // FPA to contain the output
+    if (!expFPA) {
+        psError(psErrorCodeLast(), false, "Unable to construct an FPA from camera configuration.");
+        return false;
+    }
+    pmFPAfile *exp = pmFPAfileDefineOutput(config, expFPA, "PPSTACK.OUTPUT.EXP");
+    psFree(expFPA);                        // Drop reference
+    if (!exp) {
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.OUTPUT.EXP"));
+        return false;
+    }
+    if (exp->type != PM_FPA_FILE_IMAGE) {
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.OUTPUT.EXP is not of type IMAGE");
+        return false;
+    }
+    exp->save = true;
+
+    if (!pmFPAAddSourceFromFormat(expFPA, "Stack", exp->format)) {
+        psError(psErrorCodeLast(), false, "Unable to generate output FPA.");
+        return false;
+    }
+
+    // Exposure numbers
+    pmFPAfile *expNum = pmFPAfileDefineOutput(config, exp->fpa, "PPSTACK.OUTPUT.EXPNUM");
+    if (!expNum) {
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.OUTPUT.EXPNUM"));
+        return false;
+    }
+    if (expNum->type != PM_FPA_FILE_MASK) {
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.OUTPUT.EXPNUM is not of type MASK");
+        return false;
+    }
+    expNum->save = true;
+
+    // Weighted exposure
+    pmFPAfile *expWt = pmFPAfileDefineOutput(config, exp->fpa, "PPSTACK.OUTPUT.EXPWT");
+    if (!expWt) {
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.OUTPUT.EXPWT"));
+        return false;
+    }
+    if (expWt->type != PM_FPA_FILE_VARIANCE) {
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.OUTPUT.EXPWT is not of type VARIANCE");
+        return false;
+    }
+    expWt->save = true;
+
+
     if (havePSFs) {
-        pmFPAfile *targetPSF = pmFPAfileDefineOutput(config, output->fpa, "PPSTACK.TARGET.PSF");
+        pmFPA *psfFPA = pmFPAConstruct(config->camera, config->cameraName); // FPA to contain PSF
+        if (!psfFPA) {
+            psError(psErrorCodeLast(), false, "Unable to construct an FPA from camera configuration.");
+            return false;
+        }
+        pmFPAfile *targetPSF = pmFPAfileDefineOutput(config, psfFPA, "PPSTACK.TARGET.PSF");
         if (!targetPSF) {
-            psError(PS_ERR_IO, false, _("Unable to generate output file from PPSTACK.TARGET.PSF"));
-            return false;
-        }
+            psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.TARGET.PSF"));
+            return false;
+        }
+        psFree(psfFPA);
         if (targetPSF->type != PM_FPA_FILE_PSF) {
-            psError(PS_ERR_IO, true, "PPSTACK.TARGET.PSF is not of type PSF");
+            psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.TARGET.PSF is not of type PSF");
             return false;
         }
         targetPSF->save = true;
     }
+
+    // Unconvolved stack
+    pmFPA *unconvFPA = pmFPAConstruct(config->camera, config->cameraName); // FPA to contain unconvolved output
+    if (!unconvFPA) {
+        psError(psErrorCodeLast(), false, "Unable to construct an FPA from camera configuration.");
+        return false;
+    }
+    pmFPAfile *unConv = pmFPAfileDefineOutput(config, unconvFPA, "PPSTACK.UNCONV");
+    psFree(unconvFPA);                  // Drop reference
+    if (!unConv) {
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.UNCONV"));
+        return false;
+    }
+    if (unConv->type != PM_FPA_FILE_IMAGE) {
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.UNCONV is not of type IMAGE");
+        return false;
+    }
+    unConv->save = true;
+
+    if (!pmFPAAddSourceFromFormat(unconvFPA, "Stack", unConv->format)) {
+        psError(psErrorCodeLast(), false, "Unable to generate output FPA.");
+        return false;
+    }
+
+    // Unconvolved mask
+    pmFPAfile *unconvMask = pmFPAfileDefineOutput(config, unconvFPA, "PPSTACK.UNCONV.MASK");
+    if (!unconvMask) {
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.UNCONV.MASK"));
+        return false;
+    }
+    if (unconvMask->type != PM_FPA_FILE_MASK) {
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.UNCONV.MASK is not of type MASK");
+        return false;
+    }
+    unconvMask->save = true;
+
+    // Unconvolved variance
+    if (haveVariances) {
+        pmFPAfile *unconvVariance = pmFPAfileDefineOutput(config, unconvFPA, "PPSTACK.UNCONV.VARIANCE");
+        if (!unconvVariance) {
+            psError(psErrorCodeLast(), false, "Unable to generate output file from PPSTACK.UNCONV.VARIANCE");
+            return false;
+        }
+        if (unconvVariance->type != PM_FPA_FILE_VARIANCE) {
+            psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.UNCONV.VARIANCE is not of type VARIANCE");
+            return false;
+        }
+        unconvVariance->save = true;
+    }
+
+
+    // Exposure image
+    pmFPA *unconvExpFPA = pmFPAConstruct(config->camera, config->cameraName); // FPA to contain the output
+    if (!unconvExpFPA) {
+        psError(psErrorCodeLast(), false, "Unable to construct an FPA from camera configuration.");
+        return false;
+    }
+    pmFPAfile *unconvExp = pmFPAfileDefineOutput(config, unconvExpFPA, "PPSTACK.UNCONV.EXP");
+    psFree(unconvExpFPA);               // Drop reference
+    if (!unconvExp) {
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.UNCONV.EXP"));
+        return false;
+    }
+    if (unconvExp->type != PM_FPA_FILE_IMAGE) {
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.UNCONV.EXP is not of type IMAGE");
+        return false;
+    }
+    unconvExp->save = true;
+
+    if (!pmFPAAddSourceFromFormat(unconvExpFPA, "Stack", unconvExp->format)) {
+        psError(psErrorCodeLast(), false, "Unable to generate output FPA.");
+        return false;
+    }
+
+    // Exposure numbers
+    pmFPAfile *unconvExpNum = pmFPAfileDefineOutput(config, unconvExp->fpa, "PPSTACK.UNCONV.EXPNUM");
+    if (!unconvExpNum) {
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.UNCONV.MASK"));
+        return false;
+    }
+    if (unconvExpNum->type != PM_FPA_FILE_MASK) {
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.UNCONV.EXPNUM is not of type MASK");
+        return false;
+    }
+    unconvExpNum->save = true;
+
+    // Weighted exposure
+    pmFPAfile *unconvExpWt = pmFPAfileDefineOutput(config, unconvExp->fpa, "PPSTACK.UNCONV.EXPWT");
+    if (!unconvExpWt) {
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.UNCONV.EXPWT"));
+        return false;
+    }
+    if (unconvExpWt->type != PM_FPA_FILE_VARIANCE) {
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.UNCONV.EXPWT is not of type VARIANCE");
+        return false;
+    }
+    unconvExpWt->save = true;
 
     // Output JPEGs
     pmFPAfile *jpeg1 = pmFPAfileDefineOutput(config, NULL, "PPSTACK.OUTPUT.JPEG1");
     if (!jpeg1) {
-        psError(PS_ERR_IO, false, _("Unable to generate output file from PPSTACK.OUTPUT.JPEG1"));
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.OUTPUT.JPEG1"));
         return false;
     }
     if (jpeg1->type != PM_FPA_FILE_JPEG) {
-        psError(PS_ERR_IO, true, "PPSTACK.OUTPUT.JPEG1 is not of type JPEG");
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.OUTPUT.JPEG1 is not of type JPEG");
         return false;
     }
@@ -308,9 +461,9 @@
     pmFPAfile *jpeg2 = pmFPAfileDefineOutput(config, NULL, "PPSTACK.OUTPUT.JPEG2");
     if (!jpeg2) {
-        psError(PS_ERR_IO, false, _("Unable to generate output file from PPSTACK.OUTPUT.JPEG2"));
+        psError(psErrorCodeLast(), false, _("Unable to generate output file from PPSTACK.OUTPUT.JPEG2"));
         return false;
     }
     if (jpeg2->type != PM_FPA_FILE_JPEG) {
-        psError(PS_ERR_IO, true, "PPSTACK.OUTPUT.JPEG2 is not of type JPEG");
+        psError(PPSTACK_ERR_CONFIG, true, "PPSTACK.OUTPUT.JPEG2 is not of type JPEG");
         return false;
     }
@@ -323,15 +476,17 @@
         psMetadataLookupBool(&mdok, config->arguments, "-photometry"); // perform photometry
     if (doPhotom) {
-        // This file, PSPHOT.INPUT, is just used as a carrier; output files (eg, PSPHOT.RESID) are defined by
-        // psphotDefineFiles
+        // This pmFPAfile, PSPHOT.INPUT, is just used as a carrier; output files (eg,
+        // PSPHOT.RESID) are defined by psphotDefineFiles
         pmFPAfile *psphotInput = pmFPAfileDefineFromFPA(config, output->fpa, 1, 1, "PSPHOT.INPUT");
         if (!psphotInput) {
-            psError(PS_ERR_IO, false, _("Unable to generate output file from PSPHOT.INPUT"));
-            return false;
-        }
+            psError(psErrorCodeLast(), false, _("Unable to generate output file from PSPHOT.INPUT"));
+            return false;
+        }
+        // specify the number of psphot input images
+        psMetadataAddS32 (config->arguments, PS_LIST_TAIL, "PSPHOT.INPUT.NUM", PS_META_REPLACE, "number of inputs", 1);
 
         // Define associated psphot input/output files
         if (!psphotDefineFiles(config, psphotInput)) {
-            psError(PSPHOT_ERR_CONFIG, false,
+            psError(psErrorCodeLast(), false,
                     "Trouble defining the additional input/output files for psphot");
             return false;
@@ -341,9 +496,9 @@
         pmFPAfile *outPSF = pmFPAfileDefineOutputFromFile(config, output, "PSPHOT.PSF.SAVE");
         if (!outPSF) {
-            psError(PS_ERR_IO, false, _("Unable to generate output file from PSPHOT.PSF.SAVE"));
+            psError(psErrorCodeLast(), false, _("Unable to generate output file from PSPHOT.PSF.SAVE"));
             return false;
         }
         if (outPSF->type != PM_FPA_FILE_PSF) {
-            psError(PS_ERR_IO, true, "PSPHOT.PSF.SAVE is not of type PSF");
+            psError(PPSTACK_ERR_CONFIG, true, "PSPHOT.PSF.SAVE is not of type PSF");
             return false;
         }
