Index: trunk/ppStats/src/ppStatsLoop.c
===================================================================
--- trunk/ppStats/src/ppStatsLoop.c	(revision 13993)
+++ trunk/ppStats/src/ppStatsLoop.c	(revision 13999)
@@ -1,3 +1,3 @@
-#include "ppStatsInternal.h"
+# include "ppStatsInternal.h"
 
 static void getMetadata(psMetadata *target, // Target for metadata
@@ -12,4 +12,24 @@
         if (item) {
             psMetadataAddItem(target, item, PS_LIST_TAIL, 0);
+        }
+    }
+    psFree(iterator);
+    return;
+}
+
+static void getAnalysis(psMetadata *target, // Output Target for metadata
+                        psList *headers,    // List containing desired keywords
+                        psMetadata *source, // Input Source for metadata
+                        psList *list        // List containing analysis blocks
+    )
+{
+    bool status;
+
+    psListIterator *iterator = psListIteratorAlloc(list, PS_LIST_HEAD, false); // Iterator
+    psString name;                      // Name from iteration
+    while ((name = psListGetAndIncrement(iterator))) {
+        psMetadata *folder = psMetadataLookupMetadata(&status, source, name); // Item of interest, or NULL
+        if (folder) {
+            getMetadata (target, folder, headers); 
         }
     }
@@ -55,9 +75,9 @@
 
 
-static psExit cellStats(psMetadata *chipResults, // Metadata holding the chip results
-                        pmCell *cell,     // Cell for which to get statistics
-                        psFits *fits,     // FITS file handle
-                        ppStatsData *data,// The data
-                        const pmConfig *config // Configuration
+psExit ppStatsCell(psMetadata *chipResults, // Metadata holding the chip results
+		   pmCell *cell,	// Cell for which to get statistics
+		   psFits *fits,	// FITS file handle
+		   ppStatsData *data,	// The data
+		   const pmConfig *config // Configuration
     )
 {
@@ -73,4 +93,6 @@
         return PS_EXIT_SUCCESS;
     }
+
+    // select the header unit for this cell
     pmHDU *hdu = pmHDUFromCell(cell); // HDU for cell
     if (!hdu || hdu->blankPHU) {
@@ -84,5 +106,21 @@
     }
 
-    // Cell-level results
+    /*** psphot and psastro put their results on the readout->analysis metadata (PSPHOT.HEADER,
+	 PSASTRO.HEADER).  we need to pull quantities of interest from those locations. to do
+	 this, we need to select the appropriate readout.  ***/
+
+    // Select the readout
+    psArray *readouts = cell->readouts; // Array of component readouts
+    if (readouts->n == 0) {
+        psLogMsg(__func__, PS_LOG_WARN, "No readouts present in cell %s --- skipping\n", cellName);
+        goto cellDone;
+    }
+    if (readouts->n > 1) {
+        psLogMsg(__func__, PS_LOG_WARN, "Multiple readouts (%ld) present in cell %s --- "
+                 "using only the first.\n", readouts->n, cellName);
+    }
+    pmReadout *readout = readouts->data[0]; // The readout of interest
+
+    // Extract Header and Concept values from the Cell and Readout->analysis level
     bool mdok;                          // Status of MD lookup
     psMetadata *cellResults = psMemIncrRefCounter(psMetadataLookupMetadata(&mdok, chipResults, cellName));
@@ -91,4 +129,5 @@
     }
 
+    // Extract Header values
     if (psListLength(data->headers) > 0 && cell->hdu) {
         if (fits && !pmCellReadHeader(cell, fits)) {
@@ -99,5 +138,12 @@
         pmHDU *hdu = cell->hdu;     // HDU for headers
         getMetadata(cellResults, hdu->header, data->headers);
-    }
+
+	// search in the readout->analysis metadata blocks listed in data->analysis
+	if (psListLength(data->analysis) > 0) {
+	    getAnalysis (cellResults, data->headers, readout->analysis, data->analysis);
+	}
+    }
+
+    // Extract Concept values
     if (psListLength(data->concepts) > 0) {
         if (fits && cell->hdu && !pmCellReadHeader(cell, fits)) {
@@ -110,4 +156,5 @@
     }
 
+    // Do we want to measure pixel statistics?
     if (!data->doStats && psListLength(data->summary)) {
         // Nothing further to do --- don't want to waste our time reading the data
@@ -115,4 +162,5 @@
     }
 
+    // Read the image pixel data
     if (fits && !pmCellRead(cell, fits, config->database)) {
         psError (PS_ERR_IO, false, "trouble reading cell data\n");
@@ -121,14 +169,4 @@
     }
 
-    psArray *readouts = cell->readouts; // Array of component readouts
-    if (readouts->n == 0) {
-        psLogMsg(__func__, PS_LOG_WARN, "No readouts present in cell %s --- skipping\n", cellName);
-        goto cellDone;
-    }
-    if (readouts->n > 1) {
-        psLogMsg(__func__, PS_LOG_WARN, "Multiple readouts (%ld) present in cell %s --- "
-                 "using only the first.\n", readouts->n, cellName);
-    }
-    pmReadout *readout = readouts->data[0]; // The readout of interest
     if (!readout->image) {
         psLogMsg(__func__, PS_LOG_WARN, "No image associated with readout in cell %s --- ignored.\n", cellName);
@@ -243,10 +281,10 @@
 }
 
-static psExit chipStats(psMetadata *fpaResults, // Metadata holding the fpa results
-                        pmChip *chip,     // Chip for which to get statistics
-                        psFits *fits,     // FITS file handle
-                        pmFPAview *view,  // View for analysis
-                        ppStatsData *data,// The data
-                        const pmConfig *config // Configuration
+psExit ppStatsChip(psMetadata *fpaResults, // Metadata holding the fpa results
+		   pmChip *chip,     // Chip for which to get statistics
+		   psFits *fits,     // FITS file handle
+		   pmFPAview *view,  // View for analysis
+		   ppStatsData *data,// The data
+		   const pmConfig *config // Configuration
     )
 {
@@ -301,5 +339,5 @@
     if (view->cell >= 0) {
         pmCell *cell = cells->data[view->cell]; // Cell of interest
-        result = cellStats(chipResults, cell, fits, data, config);
+        result = ppStatsCell(chipResults, cell, fits, data, config);
         if (result != PS_EXIT_SUCCESS) {
             psError(PS_ERR_UNKNOWN, false, "trouble with cell stats for %d\n", view->cell);
@@ -316,5 +354,5 @@
     for (int i = 0; i < cells->n; i++) {
         pmCell *cell = cells->data[i];  // Cell of interest
-        result = cellStats(chipResults, cell, fits, data, config);
+        result = ppStatsCell(chipResults, cell, fits, data, config);
         if (result != PS_EXIT_SUCCESS) {
             psError(PS_ERR_UNKNOWN, false, "trouble with cell stats for %d\n", i);
@@ -380,5 +418,5 @@
     if (view->chip >= 0) {
         pmChip *chip = chips->data[view->chip]; // Chip of interest
-        *result = chipStats(newResults, chip, fits, view, data, config);
+        *result = ppStatsChip(newResults, chip, fits, view, data, config);
         if (*result != PS_EXIT_SUCCESS) {
             psError(PS_ERR_UNKNOWN, false, "trouble with stats for cell %d\n", view->cell);
@@ -394,8 +432,8 @@
     }
 
-    // Iterate over cells
+    // Iterate over chips
     for (int i = 0; i < chips->n; i++) {
         pmChip *chip = chips->data[i];  // Chip of interest
-        *result = chipStats(newResults, chip, fits, view, data, config);
+        *result = ppStatsChip(newResults, chip, fits, view, data, config);
         if (*result != PS_EXIT_SUCCESS) {
             psError(PS_ERR_UNKNOWN, false, "trouble with stats for chip %d\n", i);
