Index: /trunk/psastro/src/psastroAstromGuess.c
===================================================================
--- /trunk/psastro/src/psastroAstromGuess.c	(revision 20795)
+++ /trunk/psastro/src/psastroAstromGuess.c	(revision 20796)
@@ -265,4 +265,6 @@
     psVector *cornerDn = psVectorAllocEmpty (100, PS_TYPE_F32);
 
+    psVector *cornerMK = psVectorAllocEmpty (100, PS_TYPE_U8);
+
     if (DEBUG) psastroDumpCorners ("corners.up.guess3.dat", "corners.dn.guess3.dat", fpa);
 
@@ -272,4 +274,26 @@
     while ((chip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
         if (!chip->process || !chip->file_exists || !chip->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 (!chip->cells->n) goto skip_chip;
+	pmCell *cell = chip->cells->data[0];
+	if (!cell) goto skip_chip;
+
+	if (!cell->readouts->n) goto skip_chip;
+	pmReadout *readout = cell->readouts->data[0];
+	if (!readout) goto skip_chip;
+
+	psMetadata *updates = psMetadataLookupMetadata (&status, readout->analysis, "PSASTRO.HEADER");
+	if (!updates) goto skip_chip;
+	
+	int nAstro = psMetadataLookupS32 (&status, updates, "NASTRO");
+	if (!nAstro) goto skip_chip;
+
+	float astError = psMetadataLookupF32 (&status, updates, "CERROR");
+	if (fabs(astError) < 1e-6) goto skip_chip;
 
 	psPlane ptCH, ptFP, ptTP;
@@ -290,4 +314,16 @@
 	psVectorAppend (cornerRn, ptSky.r);
 	psVectorAppend (cornerDn, ptSky.d);
+	psVectorAppend (cornerMK, 0);
+	continue;
+
+    skip_chip:
+	// new corner locations based on the calibrated astrometry
+	psVectorAppend (cornerLn, 0.0);
+	psVectorAppend (cornerMn, 0.0);
+	psVectorAppend (cornerPn, 0.0);
+	psVectorAppend (cornerQn, 0.0);
+	psVectorAppend (cornerRn, 0.0);
+	psVectorAppend (cornerDn, 0.0);
+	psVectorAppend (cornerMK, 1);
     }
 
@@ -314,6 +350,7 @@
     map->y->coeffMask[1][1] = PS_POLY_MASK_SET;
     
-    psVectorFitPolynomial2D (map->x, NULL, 0, cornerPn, NULL, cornerPs, cornerQs);
-    psVectorFitPolynomial2D (map->y, NULL, 0, cornerQn, NULL, cornerPs, cornerQs);
+    // fit the valid chips, mask the invalid chips
+    psVectorFitPolynomial2D (map->x, cornerMK, 1, cornerPn, NULL, cornerPs, cornerQs);
+    psVectorFitPolynomial2D (map->y, cornerMK, 1, cornerQn, NULL, cornerPs, cornerQs);
     
     // apply the linear fit...
@@ -328,6 +365,6 @@
     psStats *statsQ = psStatsAlloc (PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV);
 
-    psVectorStats (statsP, cornerPd, NULL, NULL, 0);
-    psVectorStats (statsQ, cornerQd, NULL, NULL, 0);
+    psVectorStats (statsP, cornerPd, NULL, cornerMK, 1);
+    psVectorStats (statsQ, cornerQd, NULL, cornerMK, 1);
 
     float angle = atan2 (map->y->coeff[1][0], map->x->coeff[1][0]);
@@ -353,5 +390,5 @@
     psMetadataAddF32 (header, PS_LIST_TAIL, "AST_DS", PS_META_REPLACE, "boresite scatter in DEC (TP units)", statsQ->sampleStdev);
 
-    if (0) {
+    if (DEBUG) {
 	FILE *f = fopen ("corners.dat", "w");
 	for (int i = 0; i < cornerRo->n; i++) {
@@ -370,4 +407,6 @@
     psFree (statsP);
     psFree (statsQ);
+
+    psFree (cornerMK);
 
     psFree (cornerLn);
