Index: /trunk/Ohana/src/addstar/src/gstars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/gstars.c	(revision 12487)
+++ /trunk/Ohana/src/addstar/src/gstars.c	(revision 12488)
@@ -5,5 +5,5 @@
 Stars *gstars (char *filename, int *Nstars, Image **images, int *Nimages, int photcode) {
 
-  int i, j, Nfile, Nheader, NHEADER, Nimage, NIMAGE;
+  int i, j, N, Nfile, Nheader, NHEADER, Nimage, NIMAGE;
   int Nskip, Nhead, Ndata, done, status, mode, NinStars;
   char **file, *name;
@@ -52,19 +52,23 @@
     Nimage = Nfile;
     ALLOCATE (image, Image, Nimage);
-    for (i = 0; i < Nfile; i++) {
+    for (i = N = 0; i < Nfile; i++) {
+      // XXX we should not drop all input images if one fails
       if (i > 0) {
 	f = fopen (file[i], "r");
 	if (f == NULL) {
-	  fprintf (stderr, "ERROR: can't read header for %s\n", file[i]);
-	  exit (1);
+	  fprintf (stderr, "can't read header for %s, skipping\n", file[i]);
+	  continue;
 	}
 	gfits_fread_header (f, header);
       }
 
-      ReadImageHeader (header, &image[i], photcode);
+      if (!ReadImageHeader (header, &image[N], photcode)) {
+	fprintf (stderr, "skipping %s\n", file[i]);
+	continue;
+      }
       
       /* find image rootname */
       name = filebasename (file[i]);
-      snprintf (image[i].name, 32, name);
+      snprintf (image[N].name, 32, name);
       free (name);
     
@@ -72,14 +76,14 @@
 	case SIMPLE_CMP:
 	case MOSAIC_CMP:
-	  inStars = ReadStarsTEXT (f, &image[i].nstar);
-	  inStars = FilterStars (inStars, &image[i]);
-	  stars = MergeStars (stars, Nstars, inStars, image[i].nstar);
+	  inStars = ReadStarsTEXT (f, &image[N].nstar);
+	  inStars = FilterStars (inStars, &image[N]);
+	  stars = MergeStars (stars, Nstars, inStars, image[N].nstar);
 	  break;
 
 	case SIMPLE_CMF:
 	case MOSAIC_CMF:
-	  inStars = ReadStarsFITS (f, header, NULL, &image[i].nstar);
-	  inStars = FilterStars (inStars, &image[i]);
-	  stars = MergeStars (stars, Nstars, inStars, image[i].nstar);
+	  inStars = ReadStarsFITS (f, header, NULL, &image[N].nstar);
+	  inStars = FilterStars (inStars, &image[N]);
+	  stars = MergeStars (stars, Nstars, inStars, image[N].nstar);
 	  break;
 
@@ -91,8 +95,14 @@
       fclose (f);
       gfits_free_header (header);
+      N++;
     }
-    *Nimages = Nimage;
+    *Nimages = N;
     *images = image;
     
+    if (N == 0) {
+      fprintf (stderr, "ERROR: no valid image data in %s, giving up\n", filename);
+      exit (1);
+    }
+
     return stars;
   }
@@ -206,19 +216,25 @@
 
   // now run through the images, interpret the headers and read the stars
-  for (i = 0; i < Nimage; i++) {
+  for (i = N = 0; i < Nimage; i++) {
       Nhead = extnum_head[i];
 
       if (VERBOSE) fprintf (stderr, "reading header for %s (%s)\n", exthead[i], extdata[i]);
-      ReadImageHeader (headers[Nhead], &image[i], 0);
+      if (!ReadImageHeader (headers[Nhead], &image[N], 0)) {
+	  fprintf (stderr, "skipping %s\n", exthead[i]);
+	  continue;
+      }
 
       // XXX use something to set the chip name? EXTNAME?
       if (!strcmp(exthead[i], "PHU") && (Nimage == 1)) {
-	snprintf (image[i].name, 32, "%s", name);
+	snprintf (image[N].name, 32, "%s", name);
       } else {
-	snprintf (image[i].name, 32, "%s.%s", name, exthead[i]);
-      }
-
-      // this is an error we should not encounter
-      if (!strcmp(extdata[i], "PHU")) Shutdown ("error in data segment: PHU cannot be table");
+	snprintf (image[N].name, 32, "%s.%s", name, exthead[i]);
+      }
+
+      // skip the table if there is not data segment (eg, mosaic WRP image)
+      if (!strcmp(extdata[i], "NONE")) {
+	  N++;
+	  continue;
+      }
 
       // advance the pointer to the start of the corresponding table block
@@ -230,11 +246,17 @@
       fseek (f, Nskip, SEEK_SET); 
 	 
-      inStars = ReadStarsFITS (f, headers[Nhead], headers[Ndata], &image[i].nstar);
-      inStars = FilterStars (inStars, &image[i]);
-      stars = MergeStars (stars, Nstars, inStars, image[i].nstar);
+      inStars = ReadStarsFITS (f, headers[Nhead], headers[Ndata], &image[N].nstar);
+      inStars = FilterStars (inStars, &image[N]);
+      stars = MergeStars (stars, Nstars, inStars, image[N].nstar);
+      N++;
   }
   free (name);
-  *Nimages = Nimage;
+  *Nimages = N;
   *images = image;
+
+  if (N == 0) {
+    fprintf (stderr, "ERROR: no valid image data in %s, giving up\n", filename);
+    exit (1);
+  }
 
   return stars;
