Index: /branches/eam_branches/ipp-20120405/Ohana/src/libautocode/def/image.d
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/libautocode/def/image.d	(revision 33826)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/libautocode/def/image.d	(revision 33827)
@@ -69,6 +69,6 @@
 
 FIELD 	  dMagSys,          MAG_SYS_ERR,          float,      	  systematic photometry error
-FIELD 	  nFitAstrom,       N_FIT_ASTROM,         short,      	  number of stars used for astrometry cal
-FIELD 	  nFitPhotom,       N_FIT_PHOTOM,         short,      	  number of stars used for photometry cal
+FIELD 	  nFitAstrom,       N_FIT_ASTROM,         unsigned short, number of stars used for astrometry cal
+FIELD 	  nFitPhotom,       N_FIT_PHOTOM,         unsigned short, number of stars used for photometry cal
 
 FIELD 	  photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
Index: /branches/eam_branches/ipp-20120405/Ohana/src/opihi/cmd.data/gridify.c
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/opihi/cmd.data/gridify.c	(revision 33826)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/opihi/cmd.data/gridify.c	(revision 33827)
@@ -5,9 +5,11 @@
   int i, Nx, Ny, Xb, Yb, Normalize, N;
   float Xmin, Xmax, dX, Ymin, Ymax, dY, initValue;
-  float *buf, *val;
+  float *buf, *val, *cnt;
   int *Nval;
-  Buffer *bf;
   Vector *vx, *vy, *vz;
   opihi_flt *x, *y, *z;
+
+  Buffer *bf = NULL;
+  Buffer *ct = NULL;
 
   Normalize = TRUE;
@@ -15,4 +17,6 @@
     remove_argument (N, &argc, argv);
     Normalize = FALSE;
+    if ((ct = SelectBuffer (argv[N], ANYBUFFER, TRUE)) == NULL) return (FALSE);
+    remove_argument (N, &argc, argv);
   }
 
@@ -24,6 +28,28 @@
   }
 
-  if (argc != 11) {
-    gprint (GP_ERR, "USAGE: gridify x y z buffer Xmin Xmax dX Ymin Ymax dY\n");
+  Xmin = Xmax = dX = NAN;
+  if ((N = get_argument (argc, argv, "-x"))) {
+    remove_argument (N, &argc, argv);
+    Xmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    Xmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    dX   = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  }    
+
+  Ymin = Ymax = dY = NAN;
+  if ((N = get_argument (argc, argv, "-y"))) {
+    remove_argument (N, &argc, argv);
+    Ymin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    Ymax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    dY   = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  }    
+
+  if (argc != 5) {
+    gprint (GP_ERR, "USAGE: gridify x y z buffer [-x Xmin Xmax dX] [-y Ymin Ymax dY] [-init-value value] [-raw]\n");
     return (FALSE);
   }
@@ -41,19 +67,25 @@
   REQUIRE_VECTOR_FLT (vz, FALSE); 
 
-  Xmin = atof (argv[5]);
-  Xmax = atof (argv[6]);
-  dX   = atof (argv[7]);
+  if (isnan(dX)) {
+    Xmin = 0;
+    Xmax = bf[0].matrix.Naxis[0];
+    dX = 1;
+  }
 
-  Ymin = atof (argv[8]);
-  Ymax = atof (argv[9]);
-  dY   = atof (argv[10]);
+  if (isnan(dY)) {
+    Ymin = 0;
+    Ymax = bf[0].matrix.Naxis[1];
+    dY = 1;
+  }
 
-  Nx = (Xmax - Xmin) / dX + 1;
-  Ny = (Ymax - Ymin) / dY + 1;
+  Nx = (Xmax - Xmin) / dX;
+  Ny = (Ymax - Ymin) / dY;
   
-  gfits_free_matrix (&bf[0].matrix);
-  gfits_free_header (&bf[0].header);
-  CreateBuffer (bf, Nx, Ny, -32, 0.0, 1.0);
-  strcpy (bf[0].file, "(empty)");
+  if ((Nx != bf[0].matrix.Naxis[0]) || (Ny != bf[0].matrix.Naxis[1])) {
+    gfits_free_matrix (&bf[0].matrix);
+    gfits_free_header (&bf[0].header);
+    CreateBuffer (bf, Nx, Ny, -32, 0.0, 1.0);
+    strcpy (bf[0].file, "(empty)");
+  }
 
   ALLOCATE (val, float, Nx*Ny);
@@ -68,4 +100,6 @@
     Xb = (*x - Xmin) / dX;
     Yb = (*y - Ymin) / dY;
+    if (Xb < 0) continue;
+    if (Yb < 0) continue;
     if (Xb >= Nx) continue;
     if (Yb >= Ny) continue;
@@ -74,15 +108,27 @@
   }
 
+  if (!Normalize) {
+    gfits_free_matrix (&ct[0].matrix);
+    gfits_free_header (&ct[0].header);
+    CreateBuffer (ct, Nx, Ny, -32, 0.0, 1.0);
+    strcpy (ct[0].file, "(empty)");
+
+    buf = (float *) bf[0].matrix.buffer;
+    cnt = (float *) ct[0].matrix.buffer;
+    for (i = 0; i < Nx*Ny; i++) {
+      if (Nval[i] == 0) continue;
+      buf[i] = val[i];
+      cnt[i] = Nval[i];
+    }
+    free (val);
+    free (Nval);
+    return TRUE;
+  }
+
   buf = (float *) bf[0].matrix.buffer;
   for (i = 0; i < Nx*Ny; i++) {
     buf[i] = initValue;
-    if (Normalize) {
-      if (Nval[i] == 0) {
-	continue;
-      }
-      buf[i] = val[i] / Nval[i];
-    } else {
-      buf[i] = val[i];
-    }
+    if (Nval[i] == 0) continue;
+    buf[i] = val[i] / Nval[i];
   }
 
Index: /branches/eam_branches/ipp-20120405/Ohana/src/opihi/dvo/avextract.c
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/opihi/dvo/avextract.c	(revision 33826)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/opihi/dvo/avextract.c	(revision 33827)
@@ -35,4 +35,11 @@
     remove_argument (N, &argc, argv);
     VERBOSE = TRUE;
+  }
+
+  int VERBOSE2 = FALSE;
+  if ((N = get_argument (argc, argv, "-vv"))) {
+    remove_argument (N, &argc, argv);
+    VERBOSE = TRUE;
+    VERBOSE2 = TRUE;
   }
 
@@ -173,7 +180,5 @@
     catalog.filename = (HOST_ID || PARALLEL_LOCAL) ? hostfile : skylist[0].filename[i];
     catalog.catflags = LOAD_AVES | LOAD_SECF;
-    if (needMeasures) {
-      catalog.catflags |= LOAD_MEAS;
-    }
+    catalog.catflags |= needMeasures ? LOAD_MEAS : SKIP_MEAS;
     catalog.Nsecfilt = 0;
 
@@ -181,5 +186,5 @@
       
     // an error exit status here is a significant error
-    if (!dvo_catalog_open (&catalog, NULL, FALSE, "r")) {
+    if (!dvo_catalog_open (&catalog, NULL, VERBOSE2, "r")) {
       gprint (GP_ERR, "ERROR: failure to open catalog file %s\n", catalog.filename);
       exit (2);
Index: /branches/eam_branches/ipp-20120405/Ohana/src/opihi/dvo/avmatch.c
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/opihi/dvo/avmatch.c	(revision 33826)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/opihi/dvo/avmatch.c	(revision 33827)
@@ -129,4 +129,13 @@
   }
 
+  // check the requested fields : are all average/secfilt entries, or do we need measures?
+  int needMeasures = FALSE;
+  for (i = 0; !needMeasures && (i < Nfields); i++) {
+    if (fields[i].magMode == MAG_NONE) continue;
+    if (fields[i].photcode == NULL) continue; // assert this?
+    if (fields[i].photcode[0].type == PHOT_REF) needMeasures = TRUE;
+    if (fields[i].photcode[0].type == PHOT_DEP) needMeasures = TRUE;
+  }
+
   /* load regions which contain all supplied RA,DEC coordinates */
   if ((skylist = SelectRegionsByCoordVectors (RAvec, DECvec)) == NULL) goto escape;
@@ -168,5 +177,6 @@
     snprintf (hostfile, 1024, "%s/%s.cpt", HOSTDIR, skylist[0].regions[i]->name);
     catalog.filename = HOST_ID ? hostfile : skylist[0].filename[i];
-    catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_SECF;
+    catalog.catflags = LOAD_AVES | LOAD_SECF;
+    catalog.catflags |= needMeasures ? LOAD_MEAS : SKIP_MEAS;
     catalog.Nsecfilt = 0;
 
Index: /branches/eam_branches/ipp-20120405/Ohana/src/opihi/dvo/gstar.c
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/opihi/dvo/gstar.c	(revision 33826)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/opihi/dvo/gstar.c	(revision 33827)
@@ -116,5 +116,6 @@
 
   /* lock, load, unlock catalog */
-  catalog.catflags = GetMeasures ? LOAD_AVES | LOAD_MEAS | LOAD_SECF : LOAD_AVES | LOAD_SECF;
+  catalog.catflags = LOAD_AVES | LOAD_SECF;
+  catalog.catflags |= GetMeasures ? LOAD_MEAS : SKIP_MEAS;
   catalog.Nsecfilt = 0;
 
Index: /branches/eam_branches/ipp-20120405/Ohana/src/relphot/include/relphot.h
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/relphot/include/relphot.h	(revision 33826)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/relphot/include/relphot.h	(revision 33827)
@@ -27,5 +27,5 @@
   float dMcal;
   float dMsys;
-  short nFitPhotom;
+  unsigned short nFitPhotom;
   short Xm;
   float secz;
Index: /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/MosaicOps.c	(revision 33826)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/MosaicOps.c	(revision 33827)
@@ -228,4 +228,6 @@
     mosaic[i].photcode = 0;
     mosaic[i].skipCal = FALSE;
+    
+    memset (&mosaic[i].coords, 0, sizeof(Coords));
 
     MosaicN_IMAGE[i] = 10;
@@ -709,12 +711,7 @@
 }
 
-# if (0)
-int setMmos_old (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr) {
-
-  off_t i, j, m, c, n, N, Nmax;
-  int mark, bad, Nfew, Nbad, Ncal, Nrel, Ngrid, Nsys, Nbright;
-  float Msys, Mrel, Mcal, Mgrid, Mflat;
-  double *list, *dlist, *Mlist, *dMlist;
-  StatType stats;
+int setMmos (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr) {
+
+  off_t i, N, Nmax;
   Image *image;
 
@@ -722,9 +719,12 @@
   if (FREEZE_MOSAICS) return (FALSE);
 
+  if (NTHREADS) {
+    int status = setMmos_threaded (catalog, PoorImages, flatcorr);
+    return status;
+  }
+
   image = getimages (&N, NULL);
 
   fprintf (stderr, "limiting negative clouds to %f\n", CLOUD_TOLERANCE);
-
-  int Nsecfilt = GetPhotcodeNsecfilt ();
 
   if (PoorImages) {
@@ -738,234 +738,4 @@
     Nmax = MAX (Nmax, N_onMosaic[i]);
   }
-  ALLOCATE (list, double, Nmax);
-  ALLOCATE (dlist, double, Nmax);
-  ALLOCATE (Mlist, double, Nmax);
-  ALLOCATE (dMlist, double, Nmax);
-
-  Nfew = Nbad = Ncal = Nrel = Ngrid = Nsys = 0;
-
-  for (i = 0; i < Nmosaic; i++) {
-    
-    /* on PoorImages run, skip good images */
-    if (PoorImages) {
-      bad = mosaic[i].flags & (ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_SKIP);
-      if (!bad) continue;
-    }      
-
-    // UBERCAL image: if this is an ubercal image, set minUbercalDist to 0:
-    // we optionally do not recalibrate images with UBERCAL zero points 
-    if (mosaic[i].flags & ID_IMAGE_PHOTOM_UBERCAL) {
-      mosaic[i].ubercalDist = 0;
-      // propagate ubercalDist to the images
-      for (j = 0; j < MosaicN_Image[i]; j++) {
-	off_t im = MosaicToImage[i][j];
-	image[im].ubercalDist = mosaic[i].ubercalDist;
-	// fprintf (stderr, "%d %d %d\n", (int) i, (int) im, image[im].ubercalDist);
-      }
-      if (KEEP_UBERCAL) continue;
-    }
-
-    // do not modify the calibration for mosaics with partial images loaded (skipCal TRUE)
-    if (mosaic[i].skipCal) continue;
-
-    int minUbercalDist = 1000;
-
-    int testImage = FALSE;
-    testImage |= (abs(mosaic[i].start - 1323003245) < 10);
-    testImage |= (abs(mosaic[i].start - 1323003069) < 10);
-    testImage |= (abs(mosaic[i].start - 1323003125) < 10);
-    testImage |= (abs(mosaic[i].start - 1323003300) < 10);
-    testImage |= (abs(mosaic[i].start - 1323003365) < 10);
-    testImage |= (abs(mosaic[i].start - 1323003191) < 10);
-    testImage |= (abs(mosaic[i].start - 1323003014) < 10);
-    testImage |= (abs(mosaic[i].start - 1323003484) < 10);
-    testImage |= (abs(mosaic[i].start - 1323003419) < 10);
-    testImage |= (abs(mosaic[i].start - 1323002949) < 10);
-
-    FILE *fout = NULL;
-    if (testImage) {
-      char filename[64];
-      snprintf (filename, 64, "test.%05d.%02d.dat", (int) i, npass_output);
-      fout = fopen (filename, "w");
-    }
-
-    // number of stars to measure the bright-end scatter
-    Nbright = 0;
-
-    N = 0;
-    for (j = 0; j < N_onMosaic[i]; j++) {
-      
-      m = MosaicToMeasure[i][j];
-      c = MosaicToCatalog[i][j];
-      
-      if (fout) {
-	Mcal  = getMcal  (m, c, flatcorr, catalog);
-	Mgrid = getMgrid (m, c);
-	Mrel  = getMrel  (catalog, m, c);
-	Mflat = getMflat (m, c, flatcorr, catalog);
-
-	n = catalog[c].measureT[m].averef;
-	Msys = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt]);
-
-	float delta = Msys - Mrel - Mcal - Mgrid + Mflat;
-
-	int isBad = (catalog[c].measureT[m].dbFlags & MEAS_BAD);
-
-	fprintf (fout, "%f %f : %f %f %f %f %f  : %f %d\n", catalog[c].averageT[n].R, catalog[c].averageT[n].D, Msys, Mrel, Mcal, Mgrid, Mflat, delta, isBad);
-      }
-
-      if (catalog[c].measureT[m].dbFlags & MEAS_BAD) {
-	  Nbad ++;
-	  continue;
-      }
-      Mcal  = getMcal  (m, c, flatcorr, catalog);
-      if (isnan(Mcal)) {
-	  Ncal++;
-	  continue;
-      }
-      Mgrid = getMgrid (m, c);
-      if (isnan(Mgrid)) {
-	  Ngrid ++;
-	  continue;
-      }
-      Mrel  = getMrel  (catalog, m, c);
-      if (isnan(Mrel)) {
-	  Nrel ++;
-	  continue;
-      }
-      
-      // image.Mcal is not supposed to include the flat-field correction, so we need to
-      // apply that offset as well here for this image (in other words, each detection is
-      // being compared to the model, excluding the zero point, Mcal.  The model includes
-      // the flat-correction.  NOTE the sign of Mflat (Image.Mcal = Measure.Mcal - Mflat)
-
-      Mflat = getMflat (m, c, flatcorr, catalog);
-
-      n = catalog[c].measureT[m].averef;
-      Msys = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt]);
-      if (isnan(Msys)) {
-	Nsys++;
-	continue;
-      }
-
-      PhotCode *code = GetPhotcodebyCode (catalog[c].measureT[m].photcode);
-      if (!code) goto skip;
-      if (code->equiv < 1) goto skip;
-      int Nsec = GetPhotcodeNsec (code->equiv);
-      if (Nsec == -1) goto skip;
-      minUbercalDist = MIN (catalog[c].secfilt[n*Nsecfilt + Nsec].ubercalDist, minUbercalDist);
-
-    skip:
-      list[N]  = Msys - Mrel - Mcal - Mgrid + Mflat;
-      dlist[N] = MAX (catalog[c].measureT[m].dM, MIN_ERROR);
-      if (catalog[c].measureT[m].dM < IMFIT_SYS_SIGMA_LIM) {
-	Mlist[Nbright] = list[N];
-	dMlist[Nbright] = dlist[N];
-	Nbright ++;
-      }
-      N++;
-    }
-    /* N_onMosaic[i] is all measurements, N is good measurements */
-
-    if (fout) {
-      fclose (fout);
-    }
-
-    /* too few good measurements or too many bad measurements (skip in PoorImages run) */
-    if (!PoorImages) {
-      mark = (N < IMAGE_TOOFEW) || (N < IMAGE_GOOD_FRACTION*N_onMosaic[i]);
-      if (mark) {
-	if (VERBOSE2) { fprintf (stderr, "marked mosaic %s ("OFF_T_FMT"), ("OFF_T_FMT" < %d) || ("OFF_T_FMT" < %f*"OFF_T_FMT")\n", image[MosaicToImage[i][0]].name,  i,  N, IMAGE_TOOFEW,  N, IMAGE_GOOD_FRACTION,  N_onMosaic[i]); }
-	mosaic[i].flags |= ID_IMAGE_PHOTOM_FEW;
-	Nfew ++;
-	if (testImage) {
-	  fprintf (stderr, "NOTE: *** marked test image poor : %d %d %d***\n", (int) N, (int) IMAGE_TOOFEW, (int) (IMAGE_GOOD_FRACTION*N_onMosaic[i]));
-	}
-      } else {
-	mosaic[i].flags &= ~ID_IMAGE_PHOTOM_FEW;
-      }
-    }
-    liststats (list, dlist, N, &stats);
-    if (VERBOSE2 && PoorImages) fprintf (stderr, "Mmos: %f %f %d "OFF_T_FMT"\n", stats.mean, stats.sigma, stats.Nmeas,  N);
-
-    mosaic[i].Mcal  = stats.mean;
-    mosaic[i].dMcal = stats.error;
-    mosaic[i].nFitPhotom = N;
-    mosaic[i].Xm    = 100.0*log10(stats.chisq);
-
-    if (testImage) {
-      fprintf (stderr, "test image %d (%d) %f %f %d ... ", (int) i, mosaic[i].start, stats.mean, stats.error, mosaic[i].nFitPhotom);
-    }
-
-    plot_setMcal (list, N, &stats, CLOUD_TOLERANCE);
-
-    // bright end scatter
-    liststats (Mlist, dMlist, Nbright, &stats);
-    mosaic[i].dMsys = stats.sigma;
-
-    if (mosaic[i].Mcal < -CLOUD_TOLERANCE) {
-      mosaic[i].Mcal = 0.0;
-    }
-
-    if (testImage) {
-      fprintf (stderr, "%f %f\n", mosaic[i].Mcal, mosaic[i].dMsys);
-    }
-
-    // minUbercalDist calculated here is the min value for any star owned by this image
-    // since this particular image is tied to that star, bump its distance by 1
-    mosaic[i].ubercalDist = minUbercalDist + 1;
-
-    // propagate ubercalDist to the images
-    for (j = 0; j < MosaicN_Image[i]; j++) {
-      off_t im = MosaicToImage[i][j];
-      image[im].ubercalDist = mosaic[i].ubercalDist;
-      // fprintf (stderr, "%d %d %d\n", (int) i, (int) im, image[im].ubercalDist);
-    }
-  }
-  free (list);
-  free (dlist);
-  free (Mlist);
-  free (dMlist);
-
-  npass_output ++;
-
-  fprintf (stderr, "%d mosaics marked having too few measurements (Nbad: %d, Ncal: %d, Ngrid: %d, Nrel: %d, Nsys: %d)\n", Nfew, Nbad, Ncal, Ngrid, Nrel, Nsys);
-
-  if (PoorImages) {
-    IMAGE_BAD = ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_SKIP;
-    STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
-    MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR_PHOTOM | ID_MEAS_SKIP_PHOTOM | ID_MEAS_AREA;
-  }
-  return (TRUE);
-}
-# endif
-  
-int setMmos (Catalog *catalog, int PoorImages, FlatCorrectionTable *flatcorr) {
-
-  off_t i, N, Nmax;
-  Image *image;
-
-  if (!MOSAIC_ZEROPT) return (FALSE);
-  if (FREEZE_MOSAICS) return (FALSE);
-
-  if (NTHREADS) {
-    int status = setMmos_threaded (catalog, PoorImages, flatcorr);
-    return status;
-  }
-
-  image = getimages (&N, NULL);
-
-  fprintf (stderr, "limiting negative clouds to %f\n", CLOUD_TOLERANCE);
-
-  if (PoorImages) {
-    // XXX use bad stars and measurements for PoorImages? or not?
-    // IMAGE_BAD = STAR_BAD = MEAS_BAD = 0;
-    IMAGE_BAD = 0;
-  }
-
-  Nmax = 0;
-  for (i = 0; i < Nmosaic; i++) {
-    Nmax = MAX (Nmax, N_onMosaic[i]);
-  }
 
   SetMmosInfo info;
@@ -973,5 +743,5 @@
 
   for (i = 0; i < Nmosaic; i++) {
-    setMmos_mosaic (mosaic, i, image, catalog, &info, flatcorr);
+    setMmos_mosaic (&mosaic[i], i, image, catalog, &info, flatcorr);
   }
   SetMmosInfoFree (&info);
@@ -1029,4 +799,6 @@
 
   int testImage = FALSE;
+  // testImage |= (abs(mosaic[0].start - 1324104046) < 10);
+  // testImage |= (abs(mosaic[0].start - 1324103823) < 10);
   // testImage |= (abs(mosaic[0].start - 1323003245) < 10);
   // testImage |= (abs(mosaic[0].start - 1323003069) < 10);
@@ -1075,20 +847,20 @@
     if (catalog[c].measureT[m].dbFlags & MEAS_BAD) {
       info->Nbad ++;
-      return TRUE;
+      continue;
     }
     Mcal  = getMcal  (m, c, flatcorr, catalog);
     if (isnan(Mcal)) {
       info->Ncal++;
-      return TRUE;
+      continue;
     }
     Mgrid = getMgrid (m, c);
     if (isnan(Mgrid)) {
       info->Ngrid ++;
-      return TRUE;
+      continue;
     }
     Mrel  = getMrel  (catalog, m, c);
     if (isnan(Mrel)) {
       info->Nrel ++;
-      return TRUE;
+      continue;
     }
       
@@ -1104,5 +876,5 @@
     if (isnan(Msys)) {
       info->Nsys++;
-      return TRUE;
+      continue;
     }
 
@@ -1167,5 +939,5 @@
 
   if (testImage) {
-    fprintf (stderr, "%f %f\n", mosaic[0].Mcal, mosaic[0].dMsys);
+    fprintf (stderr, "%f %f  :  %d %f\n", mosaic[0].Mcal, mosaic[0].dMsys, mosaic[0].Xm, pow(10.0, 0.01*mosaic[0].Xm));
   }
 
@@ -1215,4 +987,8 @@
   ThreadInfo *threadinfo;
   ALLOCATE (threadinfo, ThreadInfo, NTHREADS);
+
+  // each time this function is called, we cycle through the available mosaics
+  // make sure we start at 0
+  nextMosaic = 0;;
 
   // launch N worker threads
@@ -1266,4 +1042,11 @@
   free (threadinfo);
 
+  npass_output ++;
+
+  if (PoorImages) {
+    IMAGE_BAD = ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_SKIP;
+    STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
+    MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR_PHOTOM | ID_MEAS_SKIP_PHOTOM | ID_MEAS_AREA;
+  }
   return TRUE;
 }
@@ -1288,5 +1071,5 @@
     Image *image = threadinfo->image;
 
-    setMmos_mosaic (mosaic, i, image, catalog, &results, flatcorr);
+    setMmos_mosaic (&mosaic[i], i, image, catalog, &results, flatcorr);
     SetMmosInfoAccum (&threadinfo->info, &results);
   }
@@ -1507,4 +1290,5 @@
   for (i = 0; i < Nmosaic; i++) {
     if (mosaic[i].flags & IMAGE_BAD) continue;
+    if (mosaic[i].skipCal) continue;
     list[n] = mosaic[i].Mcal;
     dlist[n] = 1;
@@ -1533,6 +1317,6 @@
   n = 0;
   for (i = 0; i < Nmosaic; i++) {
-
     if (mosaic[i].flags & IMAGE_BAD) continue;
+    if (mosaic[i].skipCal) continue;
     list[n] = mosaic[i].dMcal;
     dlist[n] = 1;
@@ -1563,4 +1347,5 @@
   for (i = 0; i < Nmosaic; i++) {
     if (mosaic[i].flags & IMAGE_BAD)  continue;
+    if (mosaic[i].skipCal) continue;
 
     N = 0;
@@ -1605,6 +1390,6 @@
   n = 0;
   for (i = 0; i < Nmosaic; i++) {
-
     if (mosaic[i].flags & IMAGE_BAD) continue;
+    if (mosaic[i].skipCal) continue;
     list[n] = pow(10.0, 0.01*mosaic[i].Xm);
     dlist[n] = 1;
@@ -1637,4 +1422,5 @@
   for (i = N = 0; i < Nmosaic; i++) {
     if (mosaic[i].flags & IMAGE_BAD) continue;
+    if (mosaic[i].skipCal) continue;
     mlist[N] = mosaic[i].Mcal;
     slist[N] = mosaic[i].dMcal;
@@ -1654,4 +1440,7 @@
     // if we are keeping ubercal sacrosanct, then we should not be allowed to break them...
     if (KEEP_UBERCAL && (mosaic[i].flags & ID_IMAGE_PHOTOM_UBERCAL)) continue;
+
+    if (mosaic[i].flags & (ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_SKIP)) continue;
+    if (mosaic[i].skipCal) continue;
 
     mark = FALSE;
Index: /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/StarOps.c
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/StarOps.c	(revision 33826)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/StarOps.c	(revision 33827)
@@ -144,5 +144,5 @@
     SetMrelInfoAccum (&summary, &results);
   }
-  fprintf (stderr, "%d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", summary.Ncode, summary.Nfew, summary.Nbad, summary.Ncal, summary.Nmos, summary.Ngrid, summary.Nsys);
+  if (VERBOSE2) fprintf (stderr, "%d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", summary.Ncode, summary.Nfew, summary.Nbad, summary.Ncal, summary.Nmos, summary.Ngrid, summary.Nsys);
 
   SetMrelInfoFree (&results);
@@ -167,5 +167,5 @@
     SetMrelInfoAccum (&summary, &results);
   }
-  fprintf (stderr, "%d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", summary.Ncode, summary.Nfew, summary.Nbad, summary.Ncal, summary.Nmos, summary.Ngrid, summary.Nsys);
+  if (VERBOSE2) fprintf (stderr, "%d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", summary.Ncode, summary.Nfew, summary.Nbad, summary.Ncal, summary.Nmos, summary.Ngrid, summary.Nsys);
 
   SetMrelInfoFree (&results);
@@ -191,4 +191,8 @@
   ThreadInfo *threadinfo;
   ALLOCATE (threadinfo, ThreadInfo, NTHREADS);
+
+  // each time this function is called, we cycle through the available catalogs.
+  // make sure we start at 0
+  nextCatalog = 0;;
 
   // launch N worker threads
@@ -221,5 +225,5 @@
   // report stats & summary from the threads
   for (i = 0; i < NTHREADS; i++) {
-    fprintf (stderr, "setMrel thread %d : %d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", 
+    if (VERBOSE2) fprintf (stderr, "setMrel thread %d : %d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", 
 	     i, 
 	     threadinfo[i].summary.Ncode, 
@@ -232,5 +236,5 @@
     SetMrelInfoAccum (&summary, &threadinfo[i].summary);
   }
-  fprintf (stderr, "total : %d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", summary.Ncode, summary.Nfew, summary.Nbad, summary.Ncal, summary.Nmos, summary.Ngrid, summary.Nsys);
+  if (VERBOSE2) fprintf (stderr, "total : %d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", summary.Ncode, summary.Nfew, summary.Nbad, summary.Ncal, summary.Nmos, summary.Ngrid, summary.Nsys);
   free (threadinfo);
 
Index: /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/relphot.c
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/relphot.c	(revision 33826)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/relphot.c	(revision 33827)
@@ -128,4 +128,5 @@
       setMmos  (catalog, FALSE, flatcorr);
       setMgrid (catalog, flatcorr);
+      MARKTIME("-- set Mrel, Mcal, Mmos, Mgrid : %f sec\n", dtime);
     
       if (PLOTSTUFF) {
@@ -139,8 +140,14 @@
       // if (i < NLOOP - 1) rationalize_mosaics (catalog, Ncatalog);
       // if (i % 6 == 1) rationalize_images ();
-      if (i % 6 == 2) clean_measures (catalog, Ncatalog, FALSE, flatcorr); 
-      if (i % 6 == 3) clean_stars (catalog, Ncatalog);
-      if (i % 6 == 5) clean_mosaics ();
-      if (i % 6 == 5) clean_images ();
+
+      // NOTE : in the past, I was not iterating enough before cleaning.  make sure we do
+      // at least 8 loops first -- that should get the systematic errors down to the ~1%
+      // level, even in cases where we have an even split between photometric data and
+      // data with 1 mag of extinction.
+
+      if ((i > 8) && (i % 8 == 2)) clean_measures (catalog, Ncatalog, FALSE, flatcorr); 
+      if ((i > 8) && (i % 8 == 3)) clean_stars (catalog, Ncatalog);
+      if ((i > 8) && (i % 8 == 5)) clean_mosaics ();
+      if ((i > 8) && (i % 8 == 5)) clean_images ();
 
       // if ((i == 1) || (i == 5) || (i ==  9) || (i == 13)) clean_measures (catalog, Ncatalog, FALSE); 
@@ -148,5 +155,5 @@
       // if ((i == 4) || (i == 8) || (i == 12) || (i == 16)) clean_mosaics ();
       // if ((i == 4) || (i == 8) || (i == 12) || (i == 16)) clean_images ();
-      global_stats (catalog, Ncatalog, flatcorr);
+      if (i % 3 == 2) global_stats (catalog, Ncatalog, flatcorr);
       MARKTIME("-- finished loop %d: %f sec\n", i, dtime);
     }
Index: /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/select_images.c
===================================================================
--- /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/select_images.c	(revision 33826)
+++ /branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/select_images.c	(revision 33827)
@@ -250,20 +250,22 @@
 
     // the sky region RA is defined to be 0 - 360.0
-    if (RminImage > RmaxBand[iDecBandMin]) { 
-      fprintf (stderr, "skip image %s (%f,%f) on boundary\n", timage[i].name, 0.5*(RminImage + RmaxImage), 0.5*(DminImage + DmaxImage));
+    if (RminImage < RminBand[iDecBandMin]) { 
+      if (VERBOSE2) fprintf (stderr, "skip image %s (%f,%f) on boundary\n", timage[i].name, 0.5*(RminImage + RmaxImage), 0.5*(DminImage + DmaxImage));
       continue;
     }
-    if (RminImage > RmaxBand[iDecBandMax]) {
-      fprintf (stderr, "skip image %s (%f,%f) on boundary\n", timage[i].name, 0.5*(RminImage + RmaxImage), 0.5*(DminImage + DmaxImage));
+    if (RminImage < RminBand[iDecBandMax]) {
+      if (VERBOSE2) fprintf (stderr, "skip image %s (%f,%f) on boundary\n", timage[i].name, 0.5*(RminImage + RmaxImage), 0.5*(DminImage + DmaxImage));
       continue;
     }
-    if (RmaxImage < RminBand[iDecBandMin]) {
-      fprintf (stderr, "skip image %s (%f,%f) on boundary\n", timage[i].name, 0.5*(RminImage + RmaxImage), 0.5*(DminImage + DmaxImage));
+    if (RmaxImage > RmaxBand[iDecBandMin]) {
+      if (VERBOSE2) fprintf (stderr, "skip image %s (%f,%f) on boundary\n", timage[i].name, 0.5*(RminImage + RmaxImage), 0.5*(DminImage + DmaxImage));
       continue;
     }
-    if (RmaxImage < RminBand[iDecBandMax]) {
-      fprintf (stderr, "skip image %s (%f,%f) on boundary\n", timage[i].name, 0.5*(RminImage + RmaxImage), 0.5*(DminImage + DmaxImage));
+    if (RmaxImage > RmaxBand[iDecBandMax]) {
+      if (VERBOSE2) fprintf (stderr, "skip image %s (%f,%f) on boundary\n", timage[i].name, 0.5*(RminImage + RmaxImage), 0.5*(DminImage + DmaxImage));
       continue;
     }
+
+    // fprintf (ftest, "%s %lf %lf - %lf %lf : %lf %lf\n", timage[i].name, RminImage, DminImage, RmaxImage, DmaxImage, 0.5*(RminImage + RmaxImage), 0.5*(DminImage + DmaxImage));
 
     // image overlaps region, keep it
