Index: trunk/ppStats/src/ppStatsLoop.c
===================================================================
--- trunk/ppStats/src/ppStatsLoop.c	(revision 13640)
+++ trunk/ppStats/src/ppStatsLoop.c	(revision 13657)
@@ -69,13 +69,12 @@
 
 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
+                        pmCell *cell,     // Cell for which to get statistics
+                        psFits *fits,     // FITS file handle
+                        ppStatsData *data,// The data
+                        const pmConfig *config // Configuration
     )
 {
     assert(chipResults);
     assert(cell);
-    assert(fits);
     assert(data);
     assert(config);
@@ -96,7 +95,8 @@
 
     if (psListLength(data->headers) > 0 && cell->hdu) {
-        if (!pmCellReadHeader(cell, fits)) {
+        if (fits && !pmCellReadHeader(cell, fits)) {
             psError (PS_ERR_IO, false, "trouble reading cell header\n");
-	    return PS_EXIT_DATA_ERROR;
+            psFree(cellResults);
+            return PS_EXIT_DATA_ERROR;
         }
         pmHDU *hdu = cell->hdu;     // HDU for headers
@@ -104,7 +104,8 @@
     }
     if (psListLength(data->concepts) > 0) {
-        if (!pmCellReadHeader(cell, fits)) {
+        if (fits && !pmCellReadHeader(cell, fits)) {
             psError (PS_ERR_IO, false, "trouble reading cell header\n");
-	    return PS_EXIT_DATA_ERROR;
+            psFree(cellResults);
+            return PS_EXIT_DATA_ERROR;
         }
         pmConceptsReadCell(cell, PM_CONCEPT_SOURCE_ALL, false, config->database);
@@ -123,11 +124,13 @@
     pmHDU *hdu = pmHDUFromCell(cell); // HDU for cell
     if (!hdu || hdu->blankPHU) {
-	psError (PS_ERR_UNKNOWN, false, "trouble finding HDU for cell\n");
-	return PS_EXIT_CONFIG_ERROR;
-    }
-
-    if (!pmCellRead(cell, fits, config->database)) {
-	psError (PS_ERR_IO, false, "trouble reading cell data\n");
-	return PS_EXIT_DATA_ERROR;
+        psError (PS_ERR_UNKNOWN, false, "trouble finding HDU for cell\n");
+        psFree(cellResults);
+        return PS_EXIT_CONFIG_ERROR;
+    }
+
+    if (fits && !pmCellRead(cell, fits, config->database)) {
+        psError (PS_ERR_IO, false, "trouble reading cell data\n");
+        psFree(cellResults);
+        return PS_EXIT_DATA_ERROR;
     }
 
@@ -147,5 +150,5 @@
     }
 
-    // Measure basic image statistics (means, stdevs, etc).  
+    // Measure basic image statistics (means, stdevs, etc).
     if (data->sample <= 0.0) {
         if (!psImageStats(data->stats, readout->image, readout->mask, data->maskVal)) {
@@ -205,41 +208,41 @@
 
     // measure other types of statistics tests
-    
+
 statsDone:
     // count saturated pixels
-    if (psListLength(data->summary) > 0)     { 
-	bool get_nSatPixels = false;
-	bool get_fSatPixels = false;
+    if (psListLength(data->summary) > 0)     {
+        bool get_nSatPixels = false;
+        bool get_fSatPixels = false;
 
         psListIterator *iterator = psListIteratorAlloc(data->summary, PS_LIST_HEAD, false);
-        psString choice; 
+        psString choice;
         while ((choice = psListGetAndIncrement(iterator))) {
-	    if (!strcasecmp (choice, "SAT_PIXEL_NUM"))  get_nSatPixels = true;
-	    if (!strcasecmp (choice, "SAT_PIXEL_FRAC")) get_fSatPixels = true;
-        }
-
-	if (!get_nSatPixels && !get_fSatPixels) {
-	    goto cellDone;
-	}
-
-	// Get the "concepts" of interest
-	float saturation = psMetadataLookupF32(&mdok, cell->concepts, "CELL.SATURATION"); // Saturation level
-	if (!mdok || isnan(saturation)) {
-	    psLogMsg(__func__, PS_LOG_WARN, "CELL.SATURATION is not set --- unable to measure N_SAT_PIXELS.\n");
-	    if (get_nSatPixels) psMetadataAddS32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_NUM", 0, NULL, 0);
-	    if (get_fSatPixels) psMetadataAddF32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_FRAC", 0, NULL, NAN);
-	    goto cellDone;
-	}
-
-	int nSatPixels = 0;
-	for (int j = 0; j < readout->image->numRows; j++) {
-	    for (int i = 0; i < readout->image->numCols; i++) {
-		if (readout->image->data.F32[j][i] >= saturation) {
-		    nSatPixels ++;
-		}
-	    }
-	}
-	if (get_nSatPixels) psMetadataAddS32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_NUM", 0, NULL, nSatPixels);
-	if (get_fSatPixels) psMetadataAddF32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_FRAC", 0, NULL, nSatPixels / (double)(readout->image->numRows * readout->image->numCols));
+            if (!strcasecmp (choice, "SAT_PIXEL_NUM"))  get_nSatPixels = true;
+            if (!strcasecmp (choice, "SAT_PIXEL_FRAC")) get_fSatPixels = true;
+        }
+
+        if (!get_nSatPixels && !get_fSatPixels) {
+            goto cellDone;
+        }
+
+        // Get the "concepts" of interest
+        float saturation = psMetadataLookupF32(&mdok, cell->concepts, "CELL.SATURATION"); // Saturation level
+        if (!mdok || isnan(saturation)) {
+            psLogMsg(__func__, PS_LOG_WARN, "CELL.SATURATION is not set --- unable to measure N_SAT_PIXELS.\n");
+            if (get_nSatPixels) psMetadataAddS32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_NUM", 0, NULL, 0);
+            if (get_fSatPixels) psMetadataAddF32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_FRAC", 0, NULL, NAN);
+            goto cellDone;
+        }
+
+        int nSatPixels = 0;
+        for (int j = 0; j < readout->image->numRows; j++) {
+            for (int i = 0; i < readout->image->numCols; i++) {
+                if (readout->image->data.F32[j][i] >= saturation) {
+                    nSatPixels ++;
+                }
+            }
+        }
+        if (get_nSatPixels) psMetadataAddS32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_NUM", 0, NULL, nSatPixels);
+        if (get_fSatPixels) psMetadataAddF32(cellResults, PS_LIST_TAIL, "SAT_PIXEL_FRAC", 0, NULL, nSatPixels / (double)(readout->image->numRows * readout->image->numCols));
     }
 
@@ -247,19 +250,20 @@
     // Add the cell results to the chip
     addToHierarchy(cellResults, chipResults, cellName, "Results for cell");
-    pmCellFreeData(cell);
+    if (fits) {
+        pmCellFreeData(cell);
+    }
     return PS_EXIT_SUCCESS;
 }
 
 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
+                        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
     )
 {
     assert(fpaResults);
     assert(chip);
-    assert(fits);
     assert(view);
     assert(data);
@@ -279,5 +283,5 @@
         psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Desired cell view (%d) doesn't match "
                 "number of cells (%ld)\n", view->cell, cells->n);
-	return PS_EXIT_CONFIG_ERROR;
+        return PS_EXIT_CONFIG_ERROR;
     }
 
@@ -290,7 +294,8 @@
 
     if (psListLength(data->headers) > 0 && chip->hdu) {
-        if (!pmChipReadHeader(chip, fits)) {
+        if (fits && !pmChipReadHeader(chip, fits)) {
             psError (PS_ERR_IO, false, "trouble reading chip header\n");
-	    return PS_EXIT_DATA_ERROR;
+            psFree(chipResults);
+            return PS_EXIT_DATA_ERROR;
         }
         pmHDU *hdu = chip->hdu;     // HDU for headers
@@ -298,7 +303,8 @@
     }
     if (psListLength(data->concepts) > 0) {
-        if (!pmChipReadHeader(chip, fits)) {
+        if (fits && !pmChipReadHeader(chip, fits)) {
             psError (PS_ERR_IO, false, "trouble reading chip header\n");
-	    return PS_EXIT_DATA_ERROR;
+            psFree(chipResults);
+            return PS_EXIT_DATA_ERROR;
         }
         pmConceptsReadChip(chip, PM_CONCEPT_SOURCE_ALL, false, false, config->database);
@@ -309,12 +315,18 @@
         pmCell *cell = cells->data[view->cell]; // Cell of interest
         result = cellStats(chipResults, cell, fits, data, config);
-	if (result != PS_EXIT_SUCCESS) {
-	    psError (PS_ERR_IO, false, "trouble with cell stats for %d\n", view->cell);
-	    pmChipFreeData(chip);
-	    return result;
-	}
-	addToHierarchy(chipResults, fpaResults, chipName, "Results for chip");
-	pmChipFreeData(chip);
-	return PS_EXIT_SUCCESS;
+        if (result != PS_EXIT_SUCCESS) {
+            psError (PS_ERR_IO, false, "trouble with cell stats for %d\n", view->cell);
+            if (fits) {
+                pmChipFreeData(chip);
+            }
+            psFree(chipResults);
+            return result;
+        }
+        addToHierarchy(chipResults, fpaResults, chipName, "Results for chip");
+        if (fits) {
+            pmChipFreeData(chip);
+        }
+        psFree(chipResults);
+        return PS_EXIT_SUCCESS;
     }
 
@@ -323,18 +335,23 @@
         pmCell *cell = cells->data[i];  // Cell of interest
         result = cellStats(chipResults, cell, fits, data, config);
-	if (result != PS_EXIT_SUCCESS) {
-	    psError (PS_ERR_IO, false, "trouble with cell stats for %d\n", i);
-	    pmChipFreeData(chip);
-	    return result;
-	}
+        if (result != PS_EXIT_SUCCESS) {
+            psError (PS_ERR_IO, false, "trouble with cell stats for %d\n", i);
+            if (fits) {
+                pmChipFreeData(chip);
+            }
+            psFree(chipResults);
+            return result;
+        }
     }
 
     addToHierarchy(chipResults, fpaResults, chipName, "Results for chip");
-    pmChipFreeData(chip);
+    if (fits) {
+        pmChipFreeData(chip);
+    }
     return PS_EXIT_SUCCESS;
 }
 
 psMetadata *ppStatsLoop(psExit *result,
-			psMetadata *fpaResults, // Metadata to hold the FPA results
+                        psMetadata *fpaResults, // Metadata to hold the FPA results
                         ppStatsData *data, // The data
                         const pmConfig *config // Configuration
@@ -345,5 +362,4 @@
     psFits *fits = data->fits;          // FITS file handle
     PS_ASSERT_PTR_NON_NULL(fpa, NULL);
-    PS_ASSERT_PTR_NON_NULL(fits, NULL);
 
     *result = PS_EXIT_SUCCESS;
@@ -353,17 +369,27 @@
 
     // allocate a new one if needed
-    psMetadata *newResults = psMemIncrRefCounter(fpaResults);
+    psMetadata *newResults = fpaResults;
     if (!newResults) {
-	newResults = psMetadataAlloc();
+        newResults = psMetadataAlloc();
     }
 
     // Iterate through the FPA
     if (psListLength(data->headers) > 0 && fpa->hdu) {
-	pmFPAReadHeader(fpa, fits);
+        if (fits && !pmFPAReadHeader(fpa, fits)) {
+            psError(PS_ERR_IO, false, "Unable to read header for FPA.");
+            psFree(view);
+            psFree(newResults);
+            return NULL;
+        }
         pmHDU *hdu = fpa->hdu;          // HDU for headers
         getMetadata(newResults, hdu->header, data->headers);
     }
     if (psListLength(data->concepts) > 0) {
-	pmFPAReadHeader(fpa, fits);
+        if (fits && !pmFPAReadHeader(fpa, fits)) {
+            psError(PS_ERR_IO, false, "Unable to read header for FPA.");
+            psFree(view);
+            psFree(newResults);
+            return NULL;
+        }
         pmConceptsReadFPA(fpa, PM_CONCEPT_SOURCE_ALL, false, config->database);
         getMetadata(newResults, fpa->concepts, data->concepts);
@@ -373,14 +399,16 @@
     if (view->chip >= 0) {
         pmChip *chip = chips->data[view->chip]; // Chip of interest
-	*result = chipStats(newResults, chip, fits, view, data, config);
+        *result = chipStats(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);
-	    psFree (view);
-	    psFree (newResults);
-	    return NULL;
-	}
-	pmFPAFreeData(fpa);
-	psFree(view);
-	return newResults;
+            psError(PS_ERR_UNKNOWN, false, "trouble with stats for cell %d\n", view->cell);
+            psFree (view);
+            psFree (newResults);
+            return NULL;
+        }
+        if (fits) {
+            pmFPAFreeData(fpa);
+        }
+        psFree(view);
+        return newResults;
     }
 
@@ -389,14 +417,17 @@
         pmChip *chip = chips->data[i];  // Chip of interest
         *result = chipStats(newResults, chip, fits, view, data, config);
-	if (*result != PS_EXIT_SUCCESS) {
-	    psError(PS_ERR_UNKNOWN, false, "trouble with stats for chip %d\n", i);
-	    psFree (view);
-	    psFree (newResults);
-	    return NULL;
-	}
-    }
-
-    pmFPAFreeData(fpa);
+        if (*result != PS_EXIT_SUCCESS) {
+            psError(PS_ERR_UNKNOWN, false, "trouble with stats for chip %d\n", i);
+            psFree (view);
+            psFree (newResults);
+            return NULL;
+        }
+    }
+
+    if (fits) {
+        pmFPAFreeData(fpa);
+    }
     psFree(view);
+
     return newResults;
 }
