Index: trunk/psphot/src/psphotStackMatchPSFs.c
===================================================================
--- trunk/psphot/src/psphotStackMatchPSFs.c	(revision 31154)
+++ trunk/psphot/src/psphotStackMatchPSFs.c	(revision 32348)
@@ -25,6 +25,7 @@
     }
 
-    // loop over the available readouts (ignore chisq image)
+    // loop over the available readouts
     for (int i = 0; i < num; i++) {
+	// set up the PSF-matching parameters describing the input images
 	if (!psphotStackMatchPSFsPrepare (config, view, options, i)) {
             psError (PSPHOT_ERR_CONFIG, false, "failed to set PSF matching options for entry %d", i);
@@ -33,19 +34,12 @@
     }
 
-    // Generate target PSF
+    // XXX convolve == false might not be valid at the moment
     if (options->convolve) {
-        options->psf = psphotStackPSF(config, options->numCols, options->numRows, options->psfs, options->inputMask);
-        if (!options->psf) {
+	// Determine the 1st target PSF (either AUTO or defined by PSPHOT.STACK.TARGET.PSF.FWHM)
+	// NOTE: this also set the full list of target FWHMs (options->targetSeeing)
+        if (!psphotStackPSF(config, options)) {
             psError(psErrorCodeLast(), false, "Unable to determine output PSF.");
             return false;
         }
-        psMetadataAddPtr(config->arguments, PS_LIST_TAIL, "PSF.TARGET", PS_DATA_UNKNOWN, "Target PSF for stack", options->psf);
-        options->targetSeeing = pmPSFtoFWHM(options->psf, 0.5 * options->numCols, 0.5 * options->numRows); // FWHM for target
-        psLogMsg("psphotStack", PS_LOG_INFO, "Target seeing FWHM: %f\n", options->targetSeeing);
-
-	// XXX is this needed to supply the psf to psphot??
-        // pmChip *outChip = pmFPAfileThisChip(config->files, view, "PPSTACK.TARGET.PSF"); // Output chip
-        // psMetadataAddPtr(outChip->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_UNKNOWN, "Target PSF", options->psf);
-        // outChip->data_exists = true;
     }
 
@@ -63,4 +57,5 @@
 
 // convolve the image to match desired PSF
+// XXX is this code consistent with 'convolve' = false?
 bool psphotStackMatchPSFsReadout (pmConfig *config, const pmFPAview *view, psphotStackOptions *options, int index) {
 
@@ -109,16 +104,22 @@
 	saveMatchData(readoutOut, options, index);
     }
-    rescaleData(readoutOut, config, options, index);
+
+    // renormalize the stack variances to have sigma = 1.0
+    if (!psphotStackRenormaliseVariance(config, readoutOut)) {
+        psError(psErrorCodeLast(), false, "Unable to renormalise variance.");
+        return false;
+    }
 
     // save the output fwhm values in the readout->analysis.  we may have / will have multiple output PSF sizes,
     // so we save this in a vector.  if the vector is not yet defined, create it
-    bool mdok = false;
-    psVector *fwhmValues = psMetadataLookupVector(&mdok, readoutOut->analysis, "STACK.PSF.FWHM.VALUES");
-    if (!fwhmValues) {
-	fwhmValues = psVectorAllocEmpty(10, PS_TYPE_F32);
-	psMetadataAddVector(readoutOut->analysis, PS_LIST_TAIL, "STACK.PSF.FWHM.VALUES", PS_META_REPLACE, "PSF sizes", fwhmValues);
-	psFree(fwhmValues); // drops the extra copy
+    // NOTE: fwhmValues as defined here has 1 + nMatched PSF : 0 == unmatched
+    psVector *fwhmValues = psVectorAllocEmpty(10, PS_TYPE_F32);
+    psVectorAppend(fwhmValues, NAN); // XXX this corresponds to the unmatched image set
+    for (int i = 0; i < options->targetSeeing->n; i++) {
+	psVectorAppend(fwhmValues, options->targetSeeing->data.F32[i]);
     }
-    psVectorAppend(fwhmValues, options->targetSeeing);
+    psMetadataAddVector(readoutSrc->analysis, PS_LIST_TAIL, "STACK.PSF.FWHM.VALUES", PS_META_REPLACE, "PSF sizes", fwhmValues);
+    psMetadataAddVector(readoutOut->analysis, PS_LIST_TAIL, "STACK.PSF.FWHM.VALUES", PS_META_REPLACE, "PSF sizes", fwhmValues);
+    psFree(fwhmValues); // drops the extra copy
 
     return true;
