Index: /trunk/psastro/src/psastroChipAstrom.c
===================================================================
--- /trunk/psastro/src/psastroChipAstrom.c	(revision 18475)
+++ /trunk/psastro/src/psastroChipAstrom.c	(revision 18476)
@@ -11,6 +11,6 @@
     psMetadata *recipe  = psMetadataLookupPtr (NULL, config->recipes, PSASTRO_RECIPE);
     if (!recipe) {
-	psError(PSASTRO_ERR_CONFIG, true, "Can't find PSASTRO recipe");
-	return false;
+        psError(PSASTRO_ERR_CONFIG, true, "Can't find PSASTRO recipe");
+        return false;
     }
 
@@ -18,6 +18,6 @@
     pmFPAfile *input = psMetadataLookupPtr (NULL, config->files, "PSASTRO.INPUT");
     if (!input) {
-	psError(PSASTRO_ERR_CONFIG, true, "Can't find input data");
-	return false;
+        psError(PSASTRO_ERR_CONFIG, true, "Can't find input data");
+        return false;
     }
 
@@ -25,75 +25,92 @@
     pmFPA *fpa = input->fpa;
 
+    int numGoodChips = 0;               // Number of chips for which astrometry succeeds
+
     while ((chip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
         psTrace ("psastro", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
         if (!chip->process || !chip->file_exists) { continue; }
-	
-	while ((cell = pmFPAviewNextCell (view, fpa, 1)) != NULL) {
+
+        int numGoodCells = 0;           // Number of cells for which astrometry succeeds
+        while ((cell = pmFPAviewNextCell (view, fpa, 1)) != NULL) {
             psTrace ("psastro", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
             if (!cell->process || !cell->file_exists) { continue; }
-	    if (!chip->fromFPA) { continue; }
+            if (!chip->fromFPA) { continue; }
 
-	    // process each of the readouts
-	    while ((readout = pmFPAviewNextReadout (view, fpa, 1)) != NULL) {
-		if (! readout->data_exists) { continue; }
+            // process each of the readouts
+            int numGoodRO = 0;          // Number of readouts for which astrometry succeeds
+            while ((readout = pmFPAviewNextReadout (view, fpa, 1)) != NULL) {
+                if (! readout->data_exists) { continue; }
 
-		// select the raw objects for this readout
-		psArray *rawstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.RAWSTARS");
-		if (rawstars == NULL) { continue; }
+                // select the raw objects for this readout
+                psArray *rawstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.RAWSTARS");
+                if (rawstars == NULL) { continue; }
 
-		// select the raw objects for this readout
-		psArray *refstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.REFSTARS");
-		if (refstars == NULL) { continue; }
+                // select the raw objects for this readout
+                psArray *refstars = psMetadataLookupPtr (NULL, readout->analysis, "PSASTRO.REFSTARS");
+                if (refstars == NULL) { continue; }
 
-		// the absolute minimum number of stars is 4 (for order = 1)
-		if ((rawstars->n < 4) || (refstars->n < 4)) {
-		    readout->data_exists = false;
-		    psLogMsg ("psastro", 3, "insufficient rawstars (%ld) or refstars (%ld)", 
-			      rawstars->n, refstars->n);
-		    continue;
-		} 
+                // the absolute minimum number of stars is 4 (for order = 1)
+                if ((rawstars->n < 4) || (refstars->n < 4)) {
+                    readout->data_exists = false;
+                    psLogMsg ("psastro", 3, "insufficient rawstars (%ld) or refstars (%ld)",
+                              rawstars->n, refstars->n);
+                    continue;
+                }
 
-		// save WCS and analysis metadata in update header
-		psMetadata *updates = psMetadataAlloc();
+                // save WCS and analysis metadata in update header
+                psMetadata *updates = psMetadataAlloc();
 
-		// XXX update the header with info to reflect the failure
-		if (!psastroOneChipGrid (fpa, chip, refstars, rawstars, recipe, updates)) {
-		    readout->data_exists = false;
-		    psLogMsg ("psastro", 3, "failed to find a solution\n");
-		    psFree (updates);
-		    continue;
-		}
-		// XXX update the header with info to reflect the failure
-		if (!psastroOneChipFit (fpa, chip, refstars, rawstars, recipe, updates)) {
-		    readout->data_exists = false;
-		    psLogMsg ("psastro", 3, "failed to find a solution\n");
-		    psFree (updates);
-		    continue;
-		}
+                // XXX update the header with info to reflect the failure
+                if (!psastroOneChipGrid (fpa, chip, refstars, rawstars, recipe, updates)) {
+                    readout->data_exists = false;
+                    psLogMsg ("psastro", 3, "failed to find a solution\n");
+                    psFree (updates);
+                    continue;
+                }
+                // XXX update the header with info to reflect the failure
+                if (!psastroOneChipFit (fpa, chip, refstars, rawstars, recipe, updates)) {
+                    readout->data_exists = false;
+                    psLogMsg ("psastro", 3, "failed to find a solution\n");
+                    psFree (updates);
+                    continue;
+                }
 
-		// write the elapsed time here; this will be updated in psastroMosaicAstrometry, if called
-		psMetadataAddF32 (updates, PS_LIST_TAIL, "DT_ASTR", PS_META_REPLACE, "elapsed psastro time", psTimerMark ("psastroAnalysis"));
+                numGoodRO++;
 
-		pmAstromWriteWCS (updates, fpa, chip, NONLIN_TOL);
-		psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSASTRO.HEADER",  PS_DATA_METADATA, "psastro header stats", updates);
-		psFree (updates);
+                // write the elapsed time here; this will be updated in psastroMosaicAstrometry, if called
+                psMetadataAddF32 (updates, PS_LIST_TAIL, "DT_ASTR", PS_META_REPLACE, "elapsed psastro time", psTimerMark ("psastroAnalysis"));
 
-		if (psTraceGetLevel("psastro.dump") > 0) {
+                pmAstromWriteWCS (updates, fpa, chip, NONLIN_TOL);
+                psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSASTRO.HEADER",  PS_DATA_METADATA, "psastro header stats", updates);
+                psFree (updates);
 
-		    char *filename = NULL;
-		    char *chipname = psMetadataLookupStr (NULL, chip->concepts, "CHIP.NAME");
+                if (psTraceGetLevel("psastro.dump") > 0) {
 
-		    psStringAppend (&filename, "rawstars.ch.%s.dat", chipname);
-		    psastroDumpStars (rawstars, filename);
-		    psFree (filename);
-		    filename = NULL;
+                    char *filename = NULL;
+                    char *chipname = psMetadataLookupStr (NULL, chip->concepts, "CHIP.NAME");
 
-		    psStringAppend (&filename, "refstars.ch.%s.dat", chipname);
-		    psastroDumpStars (refstars, filename);
-		    psFree (filename);
-		    filename = NULL;
-		}
-	    }
-	}
+                    psStringAppend (&filename, "rawstars.ch.%s.dat", chipname);
+                    psastroDumpStars (rawstars, filename);
+                    psFree (filename);
+                    filename = NULL;
+
+                    psStringAppend (&filename, "refstars.ch.%s.dat", chipname);
+                    psastroDumpStars (refstars, filename);
+                    psFree (filename);
+                    filename = NULL;
+                }
+            }
+            if (numGoodRO > 0) {
+                numGoodCell++;
+            }
+        }
+        if (numGoodCell > 0) {
+            numGoodChip++;
+        }
+    }
+
+    if (fpa->chips->n == 1 && numGoodChip == 0) {
+        psError(PSASTRO_ERR_UNKNOWN, false, "Failed to fit single chip.");
+        return false;
     }
 
@@ -102,12 +119,12 @@
 # if (0)
     if (!psastroFixChipsTest (config, recipe)) {
-	psError(PSASTRO_ERR_UNKNOWN, false, "failed to align problematic chips");
-	return false;
+        psError(PSASTRO_ERR_UNKNOWN, false, "failed to align problematic chips");
+        return false;
     }
 # endif
 
     if (!psastroFixChips (config, recipe)) {
-	psError(PSASTRO_ERR_UNKNOWN, false, "failed to align problematic chips");
-	return false;
+        psError(PSASTRO_ERR_UNKNOWN, false, "failed to align problematic chips");
+        return false;
     }
 
