Index: /trunk/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 20186)
+++ /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 20187)
@@ -187,8 +187,17 @@
       if (strncmp (pname, base, strlen (base))) continue;
       p = pname + strlen(base);
+
+      /* p is pointing at the DETECTOR ID part of the photcode: CAMERA.FILTER.DETECTOR 
+	 for now, we will let this be of the form SSSSnn where SSS is an arbitrary string
+	 and nn is an integer -> ccdnum */
+      
+      while (!isdigit(*p) && *p) p++;
       if (*p == 0) continue;
       ccdnum = atoi (p);
-      /* some ambiguity here between seq number and id number */
-
+
+      /* ccdnum is an integer, but not necessarily a sequence number.  when the camera layout is
+	 constructed, there will be null values for undefined ccdnums */
+
+# if (0)
       /* add this measurement to the grid cell for this chip */
       ave = measure[0].averef;
@@ -198,4 +207,9 @@
       /* X,Y always positive-definite in range 0,0 - dX, dY */
       RD_to_XY (&X, &Y, ra, dec, &image[i].coords);
+# endif
+
+      // XXX we can now use these values (but need to be careful about old formats)
+      X = measure[0].Xccd;
+      Y = measure[0].Yccd;
       setGridMeasure (meas, cat, X, Y, ccdnum);
     }
@@ -251,5 +265,5 @@
 void setMcal (Catalog *catalog, int PoorImages) {
 
-  int i, j, m, c, n, N, Nmax, mark, bad;
+  int i, j, m, c, n, N, Nmax, mark, bad, Nfew, Nbad, Nmos, Ngrid, Nrel, Nsys;
   float Msys, Mrel, Mmos, Mgrid;
   double *list, *dlist;
@@ -268,4 +282,6 @@
   ALLOCATE (list, double, Nmax);
   ALLOCATE (dlist, double, Nmax);
+
+  Nfew = Nbad = Nmos = Ngrid = Nrel = Nsys = 0;
 
   for (i = 0; i < Nimage; i++) {
@@ -283,14 +299,30 @@
       c = clist[i][j];
       
-      if (catalog[c].measure[m].dbFlags & MEAS_BAD) continue;
+      if (catalog[c].measure[m].dbFlags & MEAS_BAD) {
+	  Nbad++;
+	  continue;
+      }
       Mmos  = getMmos  (m, c);
-      if (isnan(Mmos)) continue;
+      if (isnan(Mmos)) {
+	  Nmos ++;
+	  continue;
+      }
       Mgrid = getMgrid (m, c);
-      if (isnan(Mgrid)) continue;
+      if (isnan(Mgrid)) {
+	  Ngrid++;
+	  continue;
+      }
       Mrel  = getMrel  (catalog, m, c);
-      if (isnan(Mrel)) continue;
+      if (isnan(Mrel)) {
+	  Nrel ++;
+	  continue;
+      }
       
       n = catalog[c].measure[m].averef;
       Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
+      if (isnan(Msys)) {
+	Nsys++;
+	continue;
+      }
       list[N] = Msys - Mrel - Mmos - Mgrid;
       dlist[N] = MAX (catalog[c].measure[m].dM, MIN_ERROR);
@@ -304,4 +336,5 @@
       if (mark) {
 	image[i].code |= ID_IMAGE_FEW;
+	Nfew ++;
       } else {
 	image[i].code &= ~ID_IMAGE_FEW;
@@ -316,4 +349,7 @@
   free (list);
   free (dlist);
+
+  fprintf (stderr, "%d images marked having too few measurements (Nbad: %d, Nmos: %d, Ngrid: %d, Nrel: %d, Nsys: %d)\n", Nfew, Nbad, Nmos, Ngrid, Nrel, Nsys);
+
   if (PoorImages) {
     IMAGE_BAD = ID_IMAGE_POOR | ID_IMAGE_FEW | ID_IMAGE_SKIP;
@@ -397,7 +433,7 @@
   graphdata.ymin = PlotdMmin; 
   graphdata.ymax = PlotdMmax;
-  plot_list (&graphdata, xlist, Mlist, Nimage, "airmass vs Mcal", "airmass.png");
+  plot_list (&graphdata, xlist, Mlist, Nimage, "airmass vs Mcal", "%s.airmass.png", OUTROOT);
   plot_defaults (&graphdata);
-  plot_list (&graphdata, Mlist, dlist, Nimage, "Mcal vs dMcal", NULL);
+  plot_list (&graphdata, Mlist, dlist, Nimage, "Mcal vs dMcal", "%s.Mcal.dMcal.png", OUTROOT);
 
 # define NBIN 200
@@ -416,5 +452,5 @@
   plot_defaults (&graphdata);
   graphdata.style = 1;
-  plot_list (&graphdata, xlist, Mlist, NBIN, "dMcal hist", "dMcalhist.png");
+  plot_list (&graphdata, xlist, Mlist, NBIN, "dMcal hist", "%s.dMcalhist.png", OUTROOT);
 
   free (dlist);
