Index: /trunk/Ohana/src/addstar/src/ReadStarsFITS.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 30724)
+++ /trunk/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 30725)
@@ -45,4 +45,8 @@
   }
   if (!strcmp (type, "PS1_V2")) {
+    if (table.header[0].Naxis[0] == 20) {
+      // skip the invalid DETEFF tables which were mistakenly labeled as PS1_V2
+      return (NULL);
+    }
     stars = Convert_PS1_V2 (&table, &Nstars);
   }
@@ -51,5 +55,5 @@
   }
   if (stars == NULL) {
-    fprintf (stderr, "ERROR: invalid table type %s\n", type);
+    fprintf (stderr, "invalid table type %s\n", type);
     return (NULL);
   }
@@ -212,4 +216,85 @@
   Stars *stars;
   CMF_PS1_V1 *ps1data;
+
+  // CMF_PS1_V1 was modified 2009.05.26 (r24251) to use doubles for ra & dec.  this was a
+  // mistake in two ways: a new format should have been defined (eg, CMF_PS1_V2), and the
+  // layout used did not have clean byte-boundaries for the corresponding structure.  The
+  // former means we have two varieties of CMF_PS1_V1 out there; the latter means that the
+  // autocode tools do not work to read in the new version, even if we recognize it.  Here we
+  // test for the existence of the broken version (table[0].headers[0].Naxis[0] == 136), and
+  // call a special conversion function if it is found.
+
+  if (table[0].header[0].Naxis[0] == 136) {
+      stars = Convert_PS1_V1_Alt (table, nstars);
+      return (stars);
+  }
+
+  ps1data = gfits_table_get_CMF_PS1_V1 (table, &Nstars, NULL);
+  if (!ps1data) {
+    fprintf (stderr, "skipping inconsistent entry\n");
+    return (NULL);
+  }
+  ZeroPt = GetZeroPoint();
+
+  ALLOCATE (stars, Stars, Nstars);
+  for (i = 0; i < Nstars; i++) {
+    InitStar (&stars[i]);
+    stars[i].measure.Xccd       = ps1data[i].X;
+    stars[i].measure.Yccd       = ps1data[i].Y;
+    stars[i].measure.dXccd      = ToShortPixels(ps1data[i].dX);
+    stars[i].measure.dYccd      = ToShortPixels(ps1data[i].dY);
+
+    stars[i].measure.posangle   = ToShortDegrees(ps1data[i].posangle);
+    stars[i].measure.pltscale   = ps1data[i].pltscale;
+
+    if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
+	stars[i].measure.M      = NAN;
+    } else {
+	stars[i].measure.M      = ps1data[i].M + ZeroPt;
+    }
+    stars[i].measure.dM         = ps1data[i].dM;
+    stars[i].measure.dMcal      = ps1data[i].dMcal;
+    stars[i].measure.Map        = ps1data[i].Map + ZeroPt;
+		        
+    stars[i].measure.Sky        = ps1data[i].sky;
+    stars[i].measure.dSky       = ps1data[i].dSky;
+		        
+    stars[i].measure.psfChisq   = ps1data[i].psfChisq;
+    stars[i].measure.psfQual    = ps1data[i].psfQual;
+    stars[i].measure.psfNdof    = ps1data[i].psfNdof;
+    stars[i].measure.psfNpix    = ps1data[i].psfNpix;
+    stars[i].measure.crNsigma   = ps1data[i].crNsigma;
+    stars[i].measure.extNsigma  = ps1data[i].extNsigma;
+
+    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
+    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
+    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);
+
+    stars[i].measure.Mxx        = ToShortPixels(ps1data[i].Mxx);
+    stars[i].measure.Mxy        = ToShortPixels(ps1data[i].Mxy);
+    stars[i].measure.Myy        = ToShortPixels(ps1data[i].Myy);
+		        
+    stars[i].measure.photFlags  = ps1data[i].flags;
+
+    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
+    stars[i].measure.detID      = ps1data[i].detID; 
+
+    // the Average fields and the following Measure fields are set in FilterStars after
+    // the image metadata is in hand:  dR, dD, Mcal, dt, airmass, az, t, imageID, extID, 
+    // averef is set in find_matches, dbFlags is zero on ingest.
+
+    // the following fields are currently not being set anywhere: t_msec
+  }    
+  *nstars = Nstars;
+  return (stars);
+}
+
+Stars *Convert_PS1_V1_Alt (FTable *table, unsigned int *nstars) {
+
+  off_t Nstars; 
+  unsigned int i;
+  double ZeroPt;
+  Stars *stars;
+  CMF_PS1_V2 *ps1data;
 
   // CMF_PS1_V1 was modified 2009.05.26 (r24251) to use doubles for ra & dec.  this was a
@@ -221,10 +306,5 @@
   // call a special conversion function if it is found.
 
-  if (table[0].header[0].Naxis[0] == 136) {
-      stars = Convert_PS1_V1_Alt (table, nstars);
-      return (stars);
-  }
-
-  ps1data = gfits_table_get_CMF_PS1_V1 (table, &Nstars, NULL);
+  ps1data = gfits_table_get_CMF_PS1_V1_Alt (table, &Nstars, NULL);
   if (!ps1data) {
     fprintf (stderr, "skipping inconsistent entry\n");
@@ -286,5 +366,5 @@
 }
 
-Stars *Convert_PS1_V1_Alt (FTable *table, unsigned int *nstars) {
+Stars *Convert_PS1_V2 (FTable *table, unsigned int *nstars) {
 
   off_t Nstars; 
@@ -294,13 +374,5 @@
   CMF_PS1_V2 *ps1data;
 
-  // CMF_PS1_V1 was modified 2009.05.26 (r24251) to use doubles for ra & dec.  this was a
-  // mistake in two ways: a few format should have been defined (eg, CMF_PS1_V2), and the
-  // layout used did not have clean byte-boundaries for the corresponding structure.  The
-  // former means we have two varieties of CMF_PS1_V1 out there; the latter means that the
-  // autocode tools do not work to read in the new version, even if we recognize it.  Here we
-  // test for the existence of the broken version (table[0].headers[0].Naxis[0] == 136), and
-  // call a special conversion function if it is found.
-
-  ps1data = gfits_table_get_CMF_PS1_V1_Alt (table, &Nstars, NULL);
+  ps1data = gfits_table_get_CMF_PS1_V2 (table, &Nstars, NULL);
   if (!ps1data) {
     fprintf (stderr, "skipping inconsistent entry\n");
@@ -362,5 +434,5 @@
 }
 
-Stars *Convert_PS1_V2 (FTable *table, unsigned int *nstars) {
+Stars *Convert_PS1_V3 (FTable *table, unsigned int *nstars) {
 
   off_t Nstars; 
@@ -368,7 +440,7 @@
   double ZeroPt;
   Stars *stars;
-  CMF_PS1_V2 *ps1data;
-
-  ps1data = gfits_table_get_CMF_PS1_V2 (table, &Nstars, NULL);
+  CMF_PS1_V3 *ps1data;
+
+  ps1data = gfits_table_get_CMF_PS1_V3 (table, &Nstars, NULL);
   if (!ps1data) {
     fprintf (stderr, "skipping inconsistent entry\n");
@@ -429,70 +501,2 @@
   return (stars);
 }
-
-Stars *Convert_PS1_V3 (FTable *table, unsigned int *nstars) {
-
-  off_t Nstars; 
-  unsigned int i;
-  double ZeroPt;
-  Stars *stars;
-  CMF_PS1_V3 *ps1data;
-
-  ps1data = gfits_table_get_CMF_PS1_V3 (table, &Nstars, NULL);
-  if (!ps1data) {
-    fprintf (stderr, "skipping inconsistent entry\n");
-    return (NULL);
-  }
-  ZeroPt = GetZeroPoint();
-
-  ALLOCATE (stars, Stars, Nstars);
-  for (i = 0; i < Nstars; i++) {
-    InitStar (&stars[i]);
-    stars[i].measure.Xccd       = ps1data[i].X;
-    stars[i].measure.Yccd       = ps1data[i].Y;
-    stars[i].measure.dXccd      = ToShortPixels(ps1data[i].dX);
-    stars[i].measure.dYccd      = ToShortPixels(ps1data[i].dY);
-
-    stars[i].measure.posangle   = ToShortDegrees(ps1data[i].posangle);
-    stars[i].measure.pltscale   = ps1data[i].pltscale;
-
-    if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
-	stars[i].measure.M      = NAN;
-    } else {
-	stars[i].measure.M      = ps1data[i].M + ZeroPt;
-    }
-    stars[i].measure.dM         = ps1data[i].dM;
-    stars[i].measure.dMcal      = ps1data[i].dMcal;
-    stars[i].measure.Map        = ps1data[i].Map + ZeroPt;
-		        
-    stars[i].measure.Sky        = ps1data[i].sky;
-    stars[i].measure.dSky       = ps1data[i].dSky;
-		        
-    stars[i].measure.psfChisq   = ps1data[i].psfChisq;
-    stars[i].measure.psfQual    = ps1data[i].psfQual;
-    stars[i].measure.psfNdof    = ps1data[i].psfNdof;
-    stars[i].measure.psfNpix    = ps1data[i].psfNpix;
-    stars[i].measure.crNsigma   = ps1data[i].crNsigma;
-    stars[i].measure.extNsigma  = ps1data[i].extNsigma;
-
-    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
-    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
-    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);
-
-    stars[i].measure.Mxx        = ToShortPixels(ps1data[i].Mxx);
-    stars[i].measure.Mxy        = ToShortPixels(ps1data[i].Mxy);
-    stars[i].measure.Myy        = ToShortPixels(ps1data[i].Myy);
-		        
-    stars[i].measure.photFlags  = ps1data[i].flags;
-
-    // this is may optionally be replaced by the internal sequence (see FilterStars.c)
-    stars[i].measure.detID      = ps1data[i].detID; 
-
-    // the Average fields and the following Measure fields are set in FilterStars after
-    // the image metadata is in hand:  dR, dD, Mcal, dt, airmass, az, t, imageID, extID, 
-    // averef is set in find_matches, dbFlags is zero on ingest.
-
-    // the following fields are currently not being set anywhere: t_msec
-  }    
-  *nstars = Nstars;
-  return (stars);
-}
