Index: trunk/psphot/src/psphotReadoutCleanup.c
===================================================================
--- trunk/psphot/src/psphotReadoutCleanup.c	(revision 24188)
+++ trunk/psphot/src/psphotReadoutCleanup.c	(revision 24203)
@@ -9,11 +9,11 @@
     if (psErrorCodeLast() == PSPHOT_ERR_DATA) {
         psErrorStackPrint(stderr, "Error in the psphot readout analysis");
-	psErrorClear();
-    } 
-    if (psErrorCodeLast() != PS_ERR_NONE) { 
-	psFree (psf);
-	psFree (sources);
-	psFree (detections);
-	return false;
+        psErrorClear();
+    }
+    if (psErrorCodeLast() != PS_ERR_NONE) {
+        psFree (psf);
+        psFree (sources);
+        psFree (detections);
+        return false;
     }
 
@@ -22,7 +22,7 @@
         if (!psphotPSFstats (readout, recipe, psf)) {
             psError(PSPHOT_ERR_PROG, false, "Failed to measure PSF shape parameters");
-	    psFree (psf);
-	    psFree (sources);
-	    psFree (detections);
+            psFree (psf);
+            psFree (sources);
+            psFree (detections);
             return false;
         }
@@ -32,10 +32,24 @@
         if (!psphotMomentsStats (readout, recipe, sources)) {
             psError(PSPHOT_ERR_PROG, false, "Failed to measure Moment shape parameters");
-	    psFree (psf);
-	    psFree (sources);
-	    psFree (detections);
+            psFree (psf);
+            psFree (sources);
+            psFree (detections);
             return false;
         }
     }
+
+    // Check to see if the image quality was measured
+    if (!psf) {
+        bool mdok;                      // Status of MD lookup
+        int nIQ = psMetadataLookupS32(&mdok, recipe, "IQ_NSTAR"); // Number of stars for IQ measurement
+        if (!mdok || nIQ <= 0) {
+            psError(PSPHOT_ERR_DATA, false, "Unable to measure image quality");
+            psFree (psf);
+            psFree (sources);
+            psFree (detections);
+            return false;
+        }
+    }
+
 
     // write NSTARS to the image header
@@ -48,19 +62,19 @@
     psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.HEADER",  PS_DATA_METADATA, "header stats", header);
     if (sources) {
-	psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY, "psphot sources", sources);
+        psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY, "psphot sources", sources);
     }
     if (psf) {
-	// save the psf for possible output.  if there was already an entry, it was loaded from external sources
-	// the new one may have been updated or modified, so replace the existing entry.  We
-	// are required to save it on the chip, but this will cause problems if we ever want to
-	// run psphot on an unmosaiced image
-	pmCell *cell = readout->parent;
-	pmChip *chip = cell->parent;
-	psMetadataAdd (chip->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_UNKNOWN | PS_META_REPLACE,  "psphot psf", psf);
+        // save the psf for possible output.  if there was already an entry, it was loaded from external sources
+        // the new one may have been updated or modified, so replace the existing entry.  We
+        // are required to save it on the chip, but this will cause problems if we ever want to
+        // run psphot on an unmosaiced image
+        pmCell *cell = readout->parent;
+        pmChip *chip = cell->parent;
+        psMetadataAdd (chip->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_DATA_UNKNOWN | PS_META_REPLACE,  "psphot psf", psf);
     }
 
     if (psErrorCodeLast() != PS_ERR_NONE) {
         psErrorStackPrint(stderr, "unexpected remaining errors");
-	abort();
+        abort();
     }
 
