Index: trunk/psastro/src/psastroFixChips.c
===================================================================
--- trunk/psastro/src/psastroFixChips.c	(revision 20794)
+++ trunk/psastro/src/psastroFixChips.c	(revision 20795)
@@ -1,4 +1,5 @@
 # include "psastroInternal.h"
 # define NONLIN_TOL 0.001 /* tolerance in pixels */
+# define DEBUG 0
 
 // XXX I think the 'badAstrom' tests may need to be adjusted: see eg the nominal rotation of
@@ -7,4 +8,6 @@
 
     bool status;
+    FILE *f;
+    char *chipName;
 
     bool fixChips = psMetadataLookupBool (&status, config->arguments, "PSASTRO.FIX.CHIPS");
@@ -60,7 +63,31 @@
     int nPts = 0;
 
+    if (DEBUG) {
+	f = fopen ("corners.raw.dat", "w");
+	chipName = NULL;
+    }
+
     pmChip *obsChip = NULL;
     while ((obsChip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
 	if (!obsChip->process || !obsChip->file_exists || !obsChip->data_exists) { continue; }
+
+	// XXX we are currently inconsistent with marking the good vs the bad data
+	// psastroChipAstrom sets data_exists to false if the fit is bad.  this is
+	// probably wrong since it implies there is no data!
+
+	// skip chips for which the astrometry failed (NASTRO == 0)
+	if (!obsChip->cells->n) continue;
+	pmCell *cell = obsChip->cells->data[0];
+	if (!cell) continue;
+
+	if (!cell->readouts->n) continue;
+	pmReadout *readout = cell->readouts->data[0];
+	if (!readout) continue;
+
+	psMetadata *updates = psMetadataLookupMetadata (&status, readout->analysis, "PSASTRO.HEADER");
+	if (!updates) continue;
+	
+	int nAstro = psMetadataLookupS32 (&status, updates, "NASTRO");
+	if (!nAstro) continue;
 
 	// set the chip astrometry using the astrom file
@@ -77,4 +104,9 @@
 	xRef->data.F32[nPts] = ptFP.x;
 	yRef->data.F32[nPts] = ptFP.y;
+
+	if (DEBUG) {
+	    chipName = psMetadataLookupStr(NULL, obsChip->concepts, "CHIP.NAME");
+	    fprintf (f, "%s  %f %f  %f %f\n", chipName, xObs->data.F32[nPts], yObs->data.F32[nPts], xRef->data.F32[nPts], yRef->data.F32[nPts]);
+	}
 	nPts ++;
 
@@ -86,4 +118,9 @@
 	xRef->data.F32[nPts] = ptFP.x;
 	yRef->data.F32[nPts] = ptFP.y;
+
+	if (DEBUG) {
+	    chipName = psMetadataLookupStr(NULL, obsChip->concepts, "CHIP.NAME");
+	    fprintf (f, "%s  %f %f  %f %f\n", chipName, xObs->data.F32[nPts], yObs->data.F32[nPts], xRef->data.F32[nPts], yRef->data.F32[nPts]);
+	}
 	nPts ++;
 
@@ -95,4 +132,9 @@
 	xRef->data.F32[nPts] = ptFP.x;
 	yRef->data.F32[nPts] = ptFP.y;
+
+	if (DEBUG) {
+	    chipName = psMetadataLookupStr(NULL, obsChip->concepts, "CHIP.NAME");
+	    fprintf (f, "%s  %f %f  %f %f\n", chipName, xObs->data.F32[nPts], yObs->data.F32[nPts], xRef->data.F32[nPts], yRef->data.F32[nPts]);
+	}
 	nPts ++;
 
@@ -104,4 +146,9 @@
 	xRef->data.F32[nPts] = ptFP.x;
 	yRef->data.F32[nPts] = ptFP.y;
+
+	if (DEBUG) {
+	    chipName = psMetadataLookupStr(NULL, obsChip->concepts, "CHIP.NAME");
+	    fprintf (f, "%s  %f %f  %f %f\n", chipName, xObs->data.F32[nPts], yObs->data.F32[nPts], xRef->data.F32[nPts], yRef->data.F32[nPts]);
+	}
 	nPts ++;
 
@@ -109,4 +156,5 @@
     }
     xObs->n = yObs->n = xRef->n = yRef->n = nPts;
+    if (DEBUG) fclose (f);
 	
     psPlaneTransform *map = psPlaneTransformAlloc (1, 1);
@@ -129,4 +177,16 @@
     // the measured transformation above must be applied to make the comparison, and also then applied to the 
     // model transformation
+
+    if (DEBUG) {
+	f = fopen ("corners.fit.dat", "w");
+	for (int i = 0; i < xObs->n; i++) {
+	    psPlane obsCoord, refCoord;
+	    refCoord.x = xRef->data.F32[i];
+	    refCoord.y = yRef->data.F32[i];
+	    psPlaneTransformApply (&obsCoord, map, &refCoord);
+	    fprintf (f, "%f %f  %f %f  %f %f\n", xObs->data.F32[i], yObs->data.F32[i], xRef->data.F32[i], yRef->data.F32[i], obsCoord.x, obsCoord.y);
+	}
+	fclose (f);
+    }
 
     psFree (xObs);
@@ -198,4 +258,9 @@
 	// for successful chips, save the measured offsets in the header
 	if (!badAstrom) continue;
+
+	// XXX for now, let's just fail on the bad chips.  In the future, let's try to recover, but we still need to 
+	// catch the failures relative to the model
+	psMetadataAddS32 (updates, PS_LIST_TAIL, "NASTRO", PS_META_REPLACE, "number of astrometry stars", 0);
+	continue;
 
 	psLogMsg ("psastro", PS_LOG_INFO, "fixing chip %d, angle: %f, pixel: %f,%f\n",
