Index: trunk/ppStack/src/ppStackLoop.c
===================================================================
--- trunk/ppStack/src/ppStackLoop.c	(revision 20837)
+++ trunk/ppStack/src/ppStackLoop.c	(revision 20995)
@@ -252,8 +252,6 @@
     // Preparation iteration: Load the sources, and get a target PSF model
     psTrace("ppStack", 1, "Determining target PSF....\n");
-    psArray *globalSources = NULL;      // Global list of sources for matching (haveSources = TRUE)
-    psArray *indSources = psArrayAlloc(num); // Individual lists of sources for matching (haveSources = FALSE)
+    psArray *sourceLists = psArrayAlloc(num); // Individual lists of sources for matching
     pmPSF *targetPSF = NULL;            // Target PSF
-    bool haveSources = psMetadataLookupBool(NULL, config->arguments, "HAVE.SOURCES"); // Global sources?
     if (psMetadataLookupBool(NULL, config->arguments, "HAVE.PSF")) {
         pmFPAfileActivate(config->files, false, NULL);
@@ -269,5 +267,4 @@
         psMetadataItem *fileItem; // Item from iteration
         psArray *psfs = psArrayAlloc(num); // PSFs for PSF envelope
-        psArray *sourceLists = NULL;    // Source lists for merging sources from multiple readouts
         int numCols = 0, numRows = 0;   // Size of image
         int index = 0;                  // Index for file
@@ -280,6 +277,4 @@
                 psError(PS_ERR_UNKNOWN, false, "Unable to find PSF.");
                 psFree(view);
-                psFree(globalSources);
-                psFree(indSources);
                 psFree(sourceLists);
                 psFree(fileIter);
@@ -298,6 +293,4 @@
                 psError(PS_ERR_UNKNOWN, false, "Unable to determine size of image from PSF.");
                 psFree(view);
-                psFree(globalSources);
-                psFree(indSources);
                 psFree(sourceLists);
                 psFree(fileIter);
@@ -310,64 +303,42 @@
             }
 
-            if (!haveSources) {
-                pmReadout *ro = pmFPAviewThisReadout(view, inputFile->fpa); // Readout with sources
-                psArray *sources = psMetadataLookupPtr(NULL, ro->analysis, "PSPHOT.SOURCES"); // Sources
-                if (!sources) {
-                    psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find sources in readout.");
-                    return NULL;
-                }
-                indSources->data[index] = psMemIncrRefCounter(sources);
-
-
-                // Calculate zero points if we don't trust the source lists
-                if (psMetadataLookupBool(NULL, recipe, "ZP")) {
-                    psTrace("ppStack", 2, "Photometering input %d of %d....\n", index, num);
-                    pmFPAfileActivate(config->files, false, NULL);
-                    fileActivationSingle(config, convolveFiles, true, index);
-                    pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PPSTACK.INPUT",
-                                                            index);// File of interest
-                    pmFPAview *view = filesIterateDown(config);
-                    if (!view) {
-                        psFree(globalSources);
-                        psFree(indSources);
-                        psFree(targetPSF);
-                        return false;
-                    }
-
-                    pmReadout *ro = pmFPAviewThisReadout(view, file->fpa); // Readout of interest
-
-                    if (!ppStackInputPhotometry(ro, sources, config)) {
-                        psError(PS_ERR_UNKNOWN, false, "Unable to do photometry on input sources");
-                        psFree(globalSources);
-                        psFree(indSources);
-                        psFree(targetPSF);
-                        return false;
-                    }
-
-                    psFree(view);
-                    if (!filesIterateUp(config)) {
-                        psFree(globalSources);
-                        psFree(indSources);
-                        psFree(targetPSF);
-                        return false;
-                    }
-                    pmFPAfileActivate(config->files, false, NULL);
-                    fileActivation(config, prepareFiles, true);
-                }
-
-
-#ifdef TESTING
-                ppStackSourcesPrint(sources);
-#endif
-                sourceLists = ppStackSourceListAdd(sourceLists, sources, config);
-                if (!sourceLists) {
-                    psError(PS_ERR_UNKNOWN, false, "Unable to add sources to list.");
-                    psFree(view);
-                    psFree(globalSources);
-                    psFree(indSources);
-                    psFree(fileIter);
-                    psFree(psfs);
+            pmReadout *ro = pmFPAviewThisReadout(view, inputFile->fpa); // Readout with sources
+            psArray *sources = psMetadataLookupPtr(NULL, ro->analysis, "PSPHOT.SOURCES"); // Sources
+            if (!sources) {
+                psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find sources in readout.");
+                return NULL;
+            }
+            sourceLists->data[index] = psMemIncrRefCounter(sources);
+
+            // Re-do photometry if we don't trust the source lists
+            if (psMetadataLookupBool(NULL, recipe, "PHOT")) {
+                psTrace("ppStack", 2, "Photometering input %d of %d....\n", index, num);
+                pmFPAfileActivate(config->files, false, NULL);
+                fileActivationSingle(config, convolveFiles, true, index);
+                pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PPSTACK.INPUT", index); // File
+                pmFPAview *view = filesIterateDown(config);
+                if (!view) {
+                    psFree(sourceLists);
+                    psFree(targetPSF);
                     return false;
                 }
+
+                pmReadout *ro = pmFPAviewThisReadout(view, file->fpa); // Readout of interest
+
+                if (!ppStackInputPhotometry(ro, sources, config)) {
+                    psError(PS_ERR_UNKNOWN, false, "Unable to do photometry on input sources");
+                    psFree(sourceLists);
+                    psFree(targetPSF);
+                    return false;
+                }
+
+                psFree(view);
+                if (!filesIterateUp(config)) {
+                    psFree(sourceLists);
+                    psFree(targetPSF);
+                    return false;
+                }
+                pmFPAfileActivate(config->files, false, NULL);
+                fileActivation(config, prepareFiles, true);
             }
 
@@ -375,4 +346,14 @@
         }
         psFree(fileIter);
+
+        // Zero point calibration
+        if (!ppStackSourcesTransparency(sourceLists, view, config)) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to calculate transparency differences");
+            psFree(sourceLists);
+            psFree(targetPSF);
+            return false;
+        }
+
+        exit(1);
 
         targetPSF = ppStackPSF(config, numCols, numRows, psfs);
@@ -380,6 +361,5 @@
         if (!targetPSF) {
             psError(PS_ERR_UNKNOWN, false, "Unable to determine output PSF.");
-            psFree(globalSources);
-            psFree(indSources);
+            psFree(sourceLists);
             psFree(view);
             return false;
@@ -390,28 +370,4 @@
                          "Target PSF", targetPSF);
         outChip->data_exists = true;
-
-        if (haveSources) {
-            // We want to hang on to the 'sources' even when its host FPA is blown away
-            pmReadout *sourcesRO = pmFPAfileThisReadout(config->files, view, "PPSTACK.INPUT.SOURCES");
-            globalSources = psMetadataLookupPtr(NULL, sourcesRO->analysis, "PSPHOT.SOURCES");
-            psMemIncrRefCounter(globalSources);
-            if (!globalSources) {
-                psError(PS_ERR_UNKNOWN, true, "Unable to find sources.");
-                psFree(view);
-                return false;
-            }
-        } else {
-            globalSources = ppStackSourceListCombine(sourceLists, config);
-            psFree(sourceLists);
-            if (!globalSources) {
-                psError(PS_ERR_UNKNOWN, false, "Unable to add sources to list.");
-                psFree(view);
-                psFree(psfs);
-                return false;
-            }
-#ifdef TESTING
-            ppStackSourcesPrint(globalSources);
-#endif
-        }
 
         psFree(view);
@@ -460,6 +416,5 @@
         pmFPAview *view = filesIterateDown(config);
         if (!view) {
-            psFree(globalSources);
-            psFree(indSources);
+            psFree(sourceLists);
             psFree(targetPSF);
             psFree(rng);
@@ -478,6 +433,5 @@
             psError(PS_ERR_UNKNOWN, true, "Sizes of input images don't match: %dx%d vs %dx%d",
                     readout->image->numCols, readout->image->numRows, numCols, numRows);
-            psFree(globalSources);
-            psFree(indSources);
+            psFree(sourceLists);
             psFree(targetPSF);
             psFree(rng);
@@ -489,8 +443,7 @@
         // Background subtraction, scaling and normalisation is performed automatically by the image matching
         psArray *regions = NULL, *kernels = NULL; // Regions and kernels used in subtraction
-        psArray *sources = haveSources ? globalSources : indSources->data[i]; // Sources for matching
         psTimerStart("PPSTACK_MATCH");
         if (!ppStackMatch(readout, &regions, &kernels, &matchChi2->data.F32[i], &weightings->data.F32[i],
-                          sources, targetPSF, rng, config)) {
+                          sourceLists->data[i], targetPSF, rng, config)) {
             psErrorStackPrint(stderr, "Unable to match image %d --- ignoring.", i);
             inputMask->data.U8[i] = PPSTACK_MASK_MATCH;
@@ -532,6 +485,5 @@
         memDump("match");
     }
-    psFree(globalSources);
-    psFree(indSources);
+    psFree(sourceLists);
     psFree(targetPSF);
     psFree(rng);
