Index: /trunk/ppStack/src/ppStack.h
===================================================================
--- /trunk/ppStack/src/ppStack.h	(revision 20710)
+++ /trunk/ppStack/src/ppStack.h	(revision 20711)
@@ -99,4 +99,5 @@
                                const psArray *regions, // Array with array of regions used in each PSF match
                                const psArray *kernels, // Array with array of kernels used in each PSF match
+                               const psVector *weightings, // Weighting factors for each image
                                const psVector *addVariance // Additional variance for rejection
     );
@@ -114,5 +115,6 @@
                          pmReadout *outRO,   // Output readout
                          const psArray *readouts, // Input readouts
-                         const psArray *rejected // Array with pixels rejected in each image
+                         const psArray *rejected, // Array with pixels rejected in each image
+                         const psVector *weightings // Weighting factors for each image
     );
 
@@ -142,4 +144,5 @@
                   psArray **kernels,    // Array of kernels used in each PSF matching, returned
                   float *chi2,          // Chi^2 from the stamps
+                  float *weighting,     // Stack weighting (1/noise^2)
                   const psArray *sources, // Array of sources
                   const pmPSF *psf,     // Target PSF
Index: /trunk/ppStack/src/ppStackLoop.c
===================================================================
--- /trunk/ppStack/src/ppStackLoop.c	(revision 20710)
+++ /trunk/ppStack/src/ppStackLoop.c	(revision 20711)
@@ -444,4 +444,6 @@
     psVector *matchChi2 = psVectorAlloc(num, PS_TYPE_F32); // chi^2 for stamps when matching
     psVectorInit(matchChi2, NAN);
+    psVector *weightings = psVectorAlloc(num, PS_TYPE_F32); // Combination weightings for images (1/noise^2)
+    psVectorInit(weightings, NAN);
     for (int i = 0; i < num; i++) {
         psTrace("ppStack", 2, "Convolving input %d of %d to target PSF....\n", i, num);
@@ -482,5 +484,5 @@
         psArray *sources = haveSources ? globalSources : indSources->data[i]; // Sources for matching
         psTimerStart("PPSTACK_MATCH");
-        if (!ppStackMatch(readout, &regions, &kernels, &matchChi2->data.F32[i],
+        if (!ppStackMatch(readout, &regions, &kernels, &matchChi2->data.F32[i], &weightings->data.F32[i],
                           sources, targetPSF, rng, config)) {
             psErrorStackPrint(stderr, "Unable to match image %d --- ignoring.", i);
@@ -718,4 +720,5 @@
             psArrayAdd(job->args, 1, subRegions);
             psArrayAdd(job->args, 1, subKernels);
+            psArrayAdd(job->args, 1, weightings);
             psArrayAdd(job->args, 1, matchChi2);
             if (!psThreadJobAddPending(job)) {
@@ -725,4 +728,5 @@
                 psFree(stack);
                 psFree(inputMask);
+                psFree(weightings);
                 psFree(matchChi2);
                 psFree(view);
@@ -961,4 +965,5 @@
             psArrayAdd(job->args, 1, outRO);
             psArrayAdd(job->args, 1, rejected);
+            psArrayAdd(job->args, 1, weightings);
             if (!psThreadJobAddPending(job)) {
                 psFree(job);
Index: /trunk/ppStack/src/ppStackMatch.c
===================================================================
--- /trunk/ppStack/src/ppStackMatch.c	(revision 20710)
+++ /trunk/ppStack/src/ppStackMatch.c	(revision 20711)
@@ -47,5 +47,5 @@
 #endif
 
-bool ppStackMatch(pmReadout *readout, psArray **regions, psArray **kernels, float *chi2,
+bool ppStackMatch(pmReadout *readout, psArray **regions, psArray **kernels, float *chi2, float *weighting,
                   const psArray *sources, const pmPSF *psf, psRandom *rng, const pmConfig *config)
 {
@@ -384,4 +384,5 @@
         (void)psBinaryOp(readout->image, readout->image, "+",
                          psScalarAlloc(- psStatsGetValue(bg, PS_STAT_ROBUST_MEDIAN), PS_TYPE_F32));
+        *weighting = 1.0 / PS_SQR(psStatsGetValue(bg, PS_STAT_ROBUST_STDEV));
         psMetadataAddF32(readout->analysis, PS_LIST_TAIL, "PPSTACK.WEIGHTING", 0,
                          "Weighting by 1/noise^2 for stack",
Index: /trunk/ppStack/src/ppStackReadout.c
===================================================================
--- /trunk/ppStack/src/ppStackReadout.c	(revision 20710)
+++ /trunk/ppStack/src/ppStackReadout.c	(revision 20711)
@@ -22,8 +22,9 @@
     psArray *subRegions = args->data[3]; // Regions for PSF-matching
     psArray *subKernels = args->data[4]; // Kernels for PSF-matching
-    psVector *addVariance = args->data[5]; // Additional variance when rejecting
-
-    psArray *inspect = ppStackReadoutInitial(config, outRO, thread->readouts,
-                                             subRegions, subKernels, addVariance);
+    psVector *weightings = args->data[5]; // Weightings (1/noise^2) for each image
+    psVector *addVariance = args->data[6]; // Additional variance when rejecting
+
+    psArray *inspect = ppStackReadoutInitial(config, outRO, thread->readouts, subRegions, subKernels,
+                                             weightings, addVariance);
 
     job->results = inspect;
@@ -42,6 +43,8 @@
     pmReadout *outRO = args->data[2];   // Output readout
     psArray *rejected = args->data[3];  // Rejected pixels
-
-    bool status = ppStackReadoutFinal(config, outRO, thread->readouts, rejected); // Status of operation
+    psVector *weightings = args->data[4];  // Weighting (1/noise^2) for each image
+
+    bool status = ppStackReadoutFinal(config, outRO, thread->readouts, rejected,
+                                      weightings); // Status of operation
 
     thread->busy = false;
@@ -77,5 +80,6 @@
 
 psArray *ppStackReadoutInitial(const pmConfig *config, pmReadout *outRO, const psArray *readouts,
-                               const psArray *regions, const psArray *kernels, const psVector *addVariance)
+                               const psArray *regions, const psArray *kernels, const psVector *weightings,
+                               const psVector *addVariance)
 {
     assert(config);
@@ -86,4 +90,5 @@
     assert(readouts->n == regions->n);
     assert(regions->n == kernels->n);
+    assert(weightings && weightings->n == readouts->n && weightings->type.type == PS_TYPE_F32);
     assert(addVariance && addVariance->n == readouts->n && addVariance->type.type == PS_TYPE_F32);
     static int sectionNum = 0;          // Section number; for debugging outputs
@@ -118,4 +123,6 @@
         }
 
+#if 0
+        // This doesn't seem to work, so getting the weightings directly from a vector
         float weighting = psMetadataLookupF32(&mdok, ro->analysis, "PPSTACK.WEIGHTING"); // Relative weight
         if (!mdok || !isfinite(weighting)) {
@@ -125,4 +132,5 @@
             psLogMsg("ppStack", PS_LOG_INFO, "Weighting for image %d is %f", i, weighting);
         }
+#endif
 
         // Ensure there is a mask, or pmStackCombine will complain
@@ -132,5 +140,5 @@
         }
 
-        stack->data[i] = pmStackDataAlloc(ro, weighting, addVariance->data.F32[i]);
+        stack->data[i] = pmStackDataAlloc(ro, weightings->data.F32[i], addVariance->data.F32[i]);
     }
 
@@ -176,5 +184,5 @@
 
 bool ppStackReadoutFinal(const pmConfig *config, pmReadout *outRO, const psArray *readouts,
-                         const psArray *rejected)
+                         const psArray *rejected, const psVector *weightings)
 {
     assert(config);
@@ -183,4 +191,5 @@
     assert(rejected);
     assert(readouts->n == rejected->n);
+    assert(weightings && weightings->n == readouts->n && weightings->type.type == PS_TYPE_F32);
 
     static int sectionNum = 0;
@@ -212,4 +221,6 @@
         assert(ro);
 
+#if 0
+        // This doesn't seem to work, so getting the weightings directly from a vector
         bool mdok;                      // Status of MD lookup
         float weighting = psMetadataLookupF32(&mdok, ro->analysis, "PPSTACK.WEIGHTING"); // Relative weight
@@ -218,4 +229,5 @@
             weighting = 1.0;
         }
+#endif
 
         // Ensure there is a mask, or pmStackCombine will complain
@@ -225,5 +237,5 @@
         }
 
-        pmStackData *data = pmStackDataAlloc(ro, weighting, NAN);
+        pmStackData *data = pmStackDataAlloc(ro, weightings->data.F32[i], NAN);
         data->reject = psMemIncrRefCounter(rejected->data[i]);
         stack->data[i] = data;
Index: /trunk/ppStack/src/ppStackThread.c
===================================================================
--- /trunk/ppStack/src/ppStackThread.c	(revision 20710)
+++ /trunk/ppStack/src/ppStackThread.c	(revision 20711)
@@ -235,5 +235,5 @@
 
     {
-        psThreadTask *task = psThreadTaskAlloc("PPSTACK_INITIAL_COMBINE", 6);
+        psThreadTask *task = psThreadTaskAlloc("PPSTACK_INITIAL_COMBINE", 7);
         task->function = &ppStackReadoutInitialThread;
         psThreadTaskAdd(task);
@@ -249,5 +249,5 @@
 
     {
-        psThreadTask *task = psThreadTaskAlloc("PPSTACK_FINAL_COMBINE", 4);
+        psThreadTask *task = psThreadTaskAlloc("PPSTACK_FINAL_COMBINE", 5);
         task->function = &ppStackReadoutFinalThread;
         psThreadTaskAdd(task);
