Index: /trunk/Ohana/src/relphot/include/relphot.h
===================================================================
--- /trunk/Ohana/src/relphot/include/relphot.h	(revision 35758)
+++ /trunk/Ohana/src/relphot/include/relphot.h	(revision 35759)
@@ -17,4 +17,6 @@
 # define GRID_V2
 # define NO_IMAGE -100
+
+# define ID_SECF_STACK_PRIMARY 0x00004000
 
 // choose off_t or int depending on full-scale relphot analysis resources
@@ -114,4 +116,6 @@
   unsigned int photom_map_id;
   unsigned int flags;
+  int projID;
+  int skycellID;
   unsigned int tzero;
   unsigned char trate;
@@ -149,4 +153,5 @@
 char        *IMAGES;
 char        *BCATALOG;
+char        *BOUNDARY_TREE;
 ModeType     MODE;
 
@@ -328,4 +333,9 @@
 void          plot_star_coords    PROTO((Catalog *catalog, int Ncatalog));
 void          plot_stars          PROTO((Catalog *catalog, int Ncatalog));
+
+void          plot_list_add       PROTO((Graphdata *graphdata, double *xlist, double *ylist, int N));
+int           get_graph           PROTO((int N));
+int           open_graph          PROTO((int N));
+
 void          reload_catalogs     PROTO((SkyList *skylist, FlatCorrectionTable *flatcorr, int hostID, char *hostpath));
 int           reload_catalogs_parallel PROTO((SkyList *sky));
@@ -390,7 +400,9 @@
 
 int MatchImageName (off_t meas, int cat, char *name);
+int MatchImageSkycellID (off_t meas, int cat, int myProjectionID, int mySkycellID);
 
 int load_tree (char *treefile);
 int BoundaryTreePrimaryCell (char *primaryCellName, double ra, double dec);
+int BoundaryTreePrimaryCellIDs (int *projID, int *skycellID, double ra, double dec);
 
 int print_measure_set_alt (Average *average, SecFilt *secfilt, Measure *measure);
Index: /trunk/Ohana/src/relphot/src/BoundaryTreeOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/BoundaryTreeOps.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/BoundaryTreeOps.c	(revision 35759)
@@ -49,4 +49,35 @@
 }
 
+int BoundaryTreePrimaryCellIDs (int *projID, int *skycellID, double ra, double dec) {
+
+  int zone, band;
+
+  *projID = -1;
+  *skycellID = -1;
+
+  if (!tree) return FALSE;
+
+  if (!BoundaryTreeCellCoords (tree, &zone, &band, ra, dec)) {
+    fprintf (stderr, "mismatch!\n");
+    return FALSE;
+  }
+  
+  // I have ra, dec, and the primary projection cell.  In order to choose the primary skycell,
+  // I just need to project to ra,dec to X,Y based on the center of the cell and then get the subdivision right.
+    
+  double x = 0.0;
+  double y = 0.0;
+  BoundaryTreeProjection (&x, &y, ra, dec, tree, zone, band);
+  
+  int xi = x / tree->dX[zone][band];
+  int yi = y / tree->dY[zone][band];
+  int N = xi + tree->NX_SUB * yi;
+  
+  *projID = tree->projID[zone][band];
+  *skycellID = N;
+
+  return TRUE;
+}
+
 int load_tree (char *treefile) {
 
Index: /trunk/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 35759)
@@ -45,4 +45,10 @@
 static IDX_T       **ImageToMeasure = NULL; // measure for given measure on image : ImageMeasure[ImageIndex][i] = cat (i : 0 < NonImage[ImageIndex])
 
+// Projection Cell / SkyCell ID : for stack primary detection, we need to know the projection cell and skycell ID for each
+// stack image.  for now, we generate these ID arrays based on the image names when we load in the image table (initImages).
+// When we pass data to the remote clients via the ImageSubset, the projID/skycellID values are carried directly in the table.
+int *projectID = NULL;
+int *skycellID = NULL;
+
 // MeasureToImage was 'bin'
 // ImageToCatalog was 'clist'
@@ -74,8 +80,20 @@
   ALLOCATE (imageIdx, off_t, Nimage);
 
+  // for stack images, assign projection cell ID and skycell ID based on filenames
+  ALLOCATE (projectID,   int, Nimage);
+  ALLOCATE (skycellID,   int, Nimage);
+
   for (i = 0; i < Nimage; i++) {
     imageIdx[i] = i;
     imageIDs[i] = image[i].imageID;
-  }
+    projectID[i] = -1;
+    skycellID[i] = -1;
+    if (!strncmp (image[i].name, "RINGS.V3.skycell", strlen("RINGS.V3.skycell"))) {
+      projectID[i] = atoi(&image[i].name[17]);
+      skycellID[i] = atoi(&image[i].name[22]);
+    }
+  }
+
+  // sort the image index by the IDs
   llsortpair (imageIDs, imageIdx, Nimage);
 }
@@ -88,4 +106,7 @@
   // create full a Image array and save the needed values
   ALLOCATE (image, Image, N);
+  ALLOCATE (projectID, int, N);
+  ALLOCATE (skycellID, int, N);
+
   for (i = 0; i < N; i++) {
     image[i].imageID       = input[i].imageID      ;
@@ -97,4 +118,6 @@
     image[i].trate         = input[i].trate        ;
     image[i].ubercalDist   = input[i].ubercalDist  ;
+    projectID[i]           = input[i].projID       ;
+    skycellID[i]           = input[i].skycellID    ;
   }
   LineNumber = line_number;
@@ -130,4 +153,6 @@
     subset[i].trate         = image[i].trate        ;
     subset[i].ubercalDist   = image[i].ubercalDist  ;
+    subset[i].projID        = projectID[i];
+    subset[i].skycellID     = skycellID[i];
   }
   return subset;
@@ -433,4 +458,23 @@
   if (!strncmp(image[i].name, name, strlen(name))) return TRUE;
   return FALSE;
+}
+
+// returns image.Mcal - ff(x,y)
+int MatchImageSkycellID (off_t meas, int cat, int myProjectionID, int mySkycellID) {
+
+  off_t i;
+
+  if (!MeasureToImage) return FALSE;
+
+  i = MeasureToImage[cat][meas];
+  if (i == -1) return FALSE;
+
+  if (projectID[i] == -1) return FALSE;
+  if (skycellID[i] == -1) return FALSE;
+
+  if (projectID[i] != myProjectionID) return FALSE;
+  if (skycellID[i] != mySkycellID) return FALSE;
+  
+  return TRUE;
 }
 
@@ -595,5 +639,8 @@
     image[i].Xm    = 100.0*log10(stats.chisq);
 
-    plot_setMcal (list, N, &stats, CLOUD_TOLERANCE);
+    if (PLOTSTUFF) {
+      fprintf (stderr, "Mcal for : %s\n", image[i].name);
+      plot_setMcal (list, N, &stats, CLOUD_TOLERANCE);
+    }
 
     // bright end scatter
@@ -683,4 +730,5 @@
 }
 
+static int setMcal_init_done = FALSE;
 void plot_setMcal (double *list, int Npts, StatType *stats, float clouds) {
 
@@ -691,24 +739,36 @@
   if (!PLOTSTUFF) return;
 
+  if (Npts == 0) {
+    // fprintf (stderr, "cannot set image Mcal values yet (nan Mave)\n");
+    return;
+  }
+
+  plot_defaults (&graphdata);
+  graphdata.xmin = -0.01;
+  graphdata.xmax = +1.01;
+  graphdata.ymin = -0.21;
+  graphdata.ymax = +0.21;
+
+  if (!setMcal_init_done) {
+    int kapa = get_graph(0);
+    if (kapa < 1) {
+      kapa = open_graph(0);
+      if (!kapa) return;
+    }
+
+    KapaClearSections (kapa);
+    KapaClearPlots (kapa);
+    KapaSetLimits (kapa, &graphdata);
+    KapaSetFont (kapa, "helvetica", 14);
+    KapaBox (kapa, &graphdata);
+    setMcal_init_done = TRUE;
+  }
+
   ALLOCATE (xlist, double, Npts);
-
-  /**** Mcal vs seq ****/
-  float minMcal    = +100.0;
-  float maxMcal    = -100.0;
   for (i = 0; i < Npts; i++) {
-    xlist[i] = i;
-    minMcal = MIN (list[i], minMcal);
-    maxMcal = MAX (list[i], maxMcal);
-  }
-
-  float McalRange  = MAX(1.2*(maxMcal - minMcal), 0.21);
-  float McalCenter = 0.5*(maxMcal + minMcal);
-
-  plot_defaults (&graphdata);
-  graphdata.xmin = -1;
-  graphdata.xmax = Npts + 1;
-  graphdata.ymin = McalCenter - 0.5*McalRange;
-  graphdata.ymax = McalCenter + 0.5*McalRange;
-  plot_list (&graphdata, xlist, list, Npts, "sequence vs Mcal", "%s.seq.png", OUTROOT);
+    xlist[i] = i / (float) Npts;
+  }
+
+  plot_list_add (&graphdata, xlist, list, Npts);
 
   free (xlist);
Index: /trunk/Ohana/src/relphot/src/ImageSubset.c
===================================================================
--- /trunk/Ohana/src/relphot/src/ImageSubset.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/ImageSubset.c	(revision 35759)
@@ -58,12 +58,14 @@
   char type[16];
 
-  GET_COLUMN (Mcal,    "MCAL",       float);
-  GET_COLUMN (dMcal,   "MCAL_ERR",   float);
-  GET_COLUMN (imageID, "IMAGE_ID",   int);
-  GET_COLUMN (map,     "PHOTOM_MAP", int);
-  GET_COLUMN (flags,   "FLAGS",      int);
-  GET_COLUMN (tzero,   "TZERO",      int);
-  GET_COLUMN (trate,   "TRATE",      short);
-  GET_COLUMN (ucdist,  "UBERCAL_DIST", short);
+  GET_COLUMN (Mcal,    	 "MCAL",         float);
+  GET_COLUMN (dMcal,   	 "MCAL_ERR",     float);
+  GET_COLUMN (imageID, 	 "IMAGE_ID",     int);
+  GET_COLUMN (map,     	 "PHOTOM_MAP",   int);
+  GET_COLUMN (flags,   	 "FLAGS",        int);
+  GET_COLUMN (projID,  	 "PROJ_ID",      int);
+  GET_COLUMN (skycellID, "SKYCELL_ID",   int);
+  GET_COLUMN (tzero,     "TZERO",        int);
+  GET_COLUMN (trate,     "TRATE",        short);
+  GET_COLUMN (ucdist,    "UBERCAL_DIST", short);
 
   // XXX free the fits table data here 
@@ -76,4 +78,6 @@
     image[i].photom_map_id = map[i];
     image[i].flags         = flags[i];
+    image[i].projID        = projID[i];
+    image[i].skycellID     = skycellID[i];
     image[i].tzero         = tzero[i];
     image[i].trate         = trate[i];
@@ -87,4 +91,6 @@
   free (map);
   free (flags);
+  free (projID);
+  free (skycellID);
   free (tzero);
   free (trate);
@@ -124,4 +130,7 @@
   gfits_define_bintable_column (&theader, "J", "FLAGS", "flags", NULL, 1.0, 1.0*0x8000);
 
+  gfits_define_bintable_column (&theader, "J", "PROJ_ID", "ID", NULL, 1.0, 0.0);
+  gfits_define_bintable_column (&theader, "J", "SKYCELL_ID", "ID", NULL, 1.0, 0.0);
+
   gfits_define_bintable_column (&theader, "J", "TZERO", "exposure start", NULL, 1.0, 1.0*0x8000);
   gfits_define_bintable_column (&theader, "I", "TRATE", "tti rate", NULL, 1.0, 0.0);
@@ -134,38 +143,45 @@
   float *Mcal, *dMcal;
   unsigned int *imageID, *map, *flags, *tzero;
+  int *projID, *skycellID;
   unsigned short *trate;
   short *ucdist;
 
   // create intermediate storage arrays
-  ALLOCATE (Mcal,    float, 	     Nimage);
-  ALLOCATE (dMcal,   float, 	     Nimage);
-  ALLOCATE (imageID, unsigned int,   Nimage);
-  ALLOCATE (map,     unsigned int,   Nimage);
-  ALLOCATE (flags,   unsigned int,   Nimage);
-  ALLOCATE (tzero,   unsigned int,   Nimage);
-  ALLOCATE (trate,   unsigned short, Nimage);
-  ALLOCATE (ucdist,  short,          Nimage);
+  ALLOCATE (Mcal,      float, 	     Nimage);
+  ALLOCATE (dMcal,     float, 	     Nimage);
+  ALLOCATE (imageID,   unsigned int,   Nimage);
+  ALLOCATE (map,       unsigned int,   Nimage);
+  ALLOCATE (flags,     unsigned int,   Nimage);
+  ALLOCATE (projID,    int,            Nimage);
+  ALLOCATE (skycellID, int,            Nimage);
+  ALLOCATE (tzero,     unsigned int,   Nimage);
+  ALLOCATE (trate,     unsigned short, Nimage);
+  ALLOCATE (ucdist,    short,          Nimage);
 
   // assign the storage arrays
   for (i = 0; i < Nimage; i++) {
-    Mcal[i]    = image[i].Mcal;
-    dMcal[i]   = image[i].dMcal;
-    imageID[i] = image[i].imageID;
-    map[i]     = image[i].photom_map_id;
-    flags[i]   = image[i].flags;
-    tzero[i]   = image[i].tzero;
-    trate[i]   = image[i].trate;
-    ucdist[i]  = image[i].ubercalDist;
+    Mcal[i]      = image[i].Mcal;
+    dMcal[i]     = image[i].dMcal;
+    imageID[i]   = image[i].imageID;
+    map[i]       = image[i].photom_map_id;
+    flags[i]     = image[i].flags;
+    tzero[i]     = image[i].tzero;
+    trate[i]     = image[i].trate;
+    ucdist[i]    = image[i].ubercalDist;
+    projID[i]    = image[i].projID;
+    skycellID[i] = image[i].skycellID;
   }
 
   // add the columns to the output array
-  gfits_set_bintable_column (&theader, &ftable, "MCAL",         Mcal,    Nimage);
-  gfits_set_bintable_column (&theader, &ftable, "MCAL_ERR",     dMcal,   Nimage);
-  gfits_set_bintable_column (&theader, &ftable, "IMAGE_ID",     imageID, Nimage);
-  gfits_set_bintable_column (&theader, &ftable, "PHOTOM_MAP",   map,     Nimage);
-  gfits_set_bintable_column (&theader, &ftable, "FLAGS",        flags,   Nimage);
-  gfits_set_bintable_column (&theader, &ftable, "TZERO",        tzero,   Nimage);
-  gfits_set_bintable_column (&theader, &ftable, "TRATE",        trate,   Nimage);
-  gfits_set_bintable_column (&theader, &ftable, "UBERCAL_DIST", ucdist,  Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "MCAL",         Mcal,      Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "MCAL_ERR",     dMcal,     Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "IMAGE_ID",     imageID,   Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "PHOTOM_MAP",   map,       Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "FLAGS",        flags,     Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "PROJ_ID",      projID,    Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "SKYCELL_ID",   skycellID, Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "TZERO",        tzero,     Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "TRATE",        trate,     Nimage);
+  gfits_set_bintable_column (&theader, &ftable, "UBERCAL_DIST", ucdist,    Nimage);
 
   free (Mcal);
@@ -174,4 +190,6 @@
   free (map);
   free (flags);
+  free (projID);
+  free (skycellID);
   free (tzero);
   free (trate);
Index: /trunk/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 35759)
@@ -711,5 +711,7 @@
   if (FREEZE_MOSAICS) return (FALSE);
 
-  if (NTHREADS) {
+  // plots cannot be done in a threaded context (the plot commands collide)
+  // so do not run setMmos in threaded mode if PLOTSTUFF is set
+  if (NTHREADS && !PLOTSTUFF) {
     int status = setMmos_threaded (catalog, PoorImages, flatcorr);
     return status;
@@ -734,8 +736,16 @@
   SetMmosInfoInit (&info, Nmax, TRUE, PoorImages);
 
+  // int savePlotDelay = PLOTDELAY;
+  // if (PLOTSTUFF) PLOTDELAY = 0.0;
+
   for (i = 0; i < Nmosaic; i++) {
     setMmos_mosaic (&mosaic[i], i, image, catalog, &info, flatcorr);
   }
   SetMmosInfoFree (&info);
+
+  if (PLOTSTUFF) {
+    fprintf (stdout, "press return\n"); 
+    if (fscanf (stdin, "%*c") != 1) fprintf (stderr, "\n");
+  }
 
   npass_output ++;
@@ -757,5 +767,6 @@
 
   StatType stats;
-  liststats_setmode (&stats, STATMODE);
+  // liststats_setmode (&stats, STATMODE);
+  liststats_setmode (&stats, "INNER_WTMEAN");
 
   double *list   = info->list;
@@ -937,5 +948,8 @@
   }
 
-  plot_setMcal (list, N, &stats, CLOUD_TOLERANCE);
+  if (PLOTSTUFF) {
+    fprintf (stderr, "Mmos: %6.3f %6.3f +/- %6.3f %5d %5d | %s\n", stats.mean, stats.median, stats.sigma, stats.Nmeas, N, image[MosaicToImage[Nmos][0]].name);
+    plot_setMcal (list, N, &stats, CLOUD_TOLERANCE);
+  }
 
   // bright end scatter
@@ -961,4 +975,5 @@
     // fprintf (stderr, "%d %d %d\n", (int) i, (int) im, image[im].ubercalDist);
   }
+  
   return TRUE;
 }
Index: /trunk/Ohana/src/relphot/src/StarOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/StarOps.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/StarOps.c	(revision 35759)
@@ -168,12 +168,12 @@
   for (i = 0; i < Ncatalog; i++) {
     switch (SET_MREL_VERSION) {
-      case 0:
-	setMrel_catalog (catalog, i, pass, flatcorr, &results, Nsecfilt);
-	break;
+      // case 0:
+      // setMrel_catalog (catalog, i, pass, flatcorr, &results, Nsecfilt);
+      // break;
       case 1:
 	setMrel_catalog_alt (catalog, i, pass, flatcorr, &results, Nsecfilt);
 	break;
       default:
-	fprintf (stderr, "invalid setMrel version %d (use 0 or 1)\n", SET_MREL_VERSION);
+	fprintf (stderr, "invalid setMrel version %d (use 1)\n", SET_MREL_VERSION);
 	exit (2);
     }
Index: /trunk/Ohana/src/relphot/src/args.c
===================================================================
--- /trunk/Ohana/src/relphot/src/args.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/args.c	(revision 35759)
@@ -177,11 +177,4 @@
   }
 
-  SET_MREL_VERSION = 1;
-  if ((N = get_argument (argc, argv, "-set-mrel-version"))) {
-    remove_argument (N, &argc, argv);
-    SET_MREL_VERSION = atof(argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-
   UPDATE = FALSE;
   if ((N = get_argument (argc, argv, "-update"))) {
@@ -218,7 +211,9 @@
   }
 
+  BOUNDARY_TREE = NULL;
   if ((N = get_argument (argc, argv, "-boundary-tree"))) {
     remove_argument (N, &argc, argv);
-    load_tree (argv[N]);
+    BOUNDARY_TREE = strcreate(argv[N]);
+    load_tree (BOUNDARY_TREE);
     remove_argument (N, &argc, argv);
   }
@@ -419,4 +414,12 @@
   }
 
+  BOUNDARY_TREE = NULL;
+  if ((N = get_argument (argc, argv, "-boundary-tree"))) {
+    remove_argument (N, &argc, argv);
+    BOUNDARY_TREE = strcreate(argv[N]);
+    load_tree (BOUNDARY_TREE);
+    remove_argument (N, &argc, argv);
+  }
+
   /* specify portion of the sky */
   UserPatch.Rmin = 0;
@@ -433,4 +436,11 @@
     remove_argument (N, &argc, argv);
     UserPatch.Dmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
+  SET_MREL_VERSION = 1;
+  if ((N = get_argument (argc, argv, "-set-mrel-version"))) {
+    remove_argument (N, &argc, argv);
+    SET_MREL_VERSION = atof(argv[N]);
     remove_argument (N, &argc, argv);
   }
Index: /trunk/Ohana/src/relphot/src/load_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/load_images.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/load_images.c	(revision 35759)
@@ -95,4 +95,9 @@
 # endif
 
+  if (Nimage == 0) {
+    fprintf (stderr, "no images selected for analysis : problem with region or photcode?\n");
+    exit (4);
+  }
+
   // match chips to mosaics (if applicable)
   initMosaics (subset, Nsubset, image, inSubset, Nimage);
Index: /trunk/Ohana/src/relphot/src/plot_scatter.c
===================================================================
--- /trunk/Ohana/src/relphot/src/plot_scatter.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/plot_scatter.c	(revision 35759)
@@ -48,4 +48,6 @@
 
 		Mrel = catalog[i].secfilt[Nsecfilt*j+Nsec].M;
+		if (isnan(Mrel)) continue;
+
 		xlist[N] = Mrel;
 		ylist[N] = PhotSysTiny (&catalog[i].measureT[m], &catalog[i].averageT[j], &catalog[i].secfilt[j*Nsecfilt]) - Mcal - Mmos - Mgrid - Mrel;
@@ -54,4 +56,9 @@
 	    }
 	}
+    }
+
+    if (N == 0) {
+      fprintf (stderr, "no valid average values yet\n");
+      continue;
     }
 
Index: /trunk/Ohana/src/relphot/src/plotstuff.c
===================================================================
--- /trunk/Ohana/src/relphot/src/plotstuff.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/plotstuff.c	(revision 35759)
@@ -19,9 +19,13 @@
 }
 
+int get_graph (int N) {
+  return Xgraph[N];
+}
+
 int open_graph (int N) {
 
   char name[100];
 
-  sprintf (name, "gastro [%d]", N);
+  sprintf (name, "gastro:%d", N);
 
   // if -plot is supplied, the plots are shown on the screen; 
@@ -156,4 +160,12 @@
 }
 
+// plot the vector pair to a file with name defined by the varargs format
+void plot_list_add (Graphdata *graphdata, double *xlist, double *ylist, int Npts) {
+
+  KapaPrepPlot (Xgraph[0], Npts, graphdata);
+  PlotVector (Npts, xlist, 0, 0, "x");
+  PlotVector (Npts, ylist, 1, 0, "y");
+}
+
 void plot_defaults (Graphdata *graphdata) {
 
Index: /trunk/Ohana/src/relphot/src/reload_catalogs.c
===================================================================
--- /trunk/Ohana/src/relphot/src/reload_catalogs.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/reload_catalogs.c	(revision 35759)
@@ -156,4 +156,11 @@
   // load the list of hosts
   HostTable *table = HostTableLoad (CATDIR, sky->hosts);
+
+  if (BOUNDARY_TREE) {
+    char *tmppath = abspath(BOUNDARY_TREE, DVO_MAX_PATH);
+    free (BOUNDARY_TREE);
+    BOUNDARY_TREE = tmppath;
+    
+  }
 
   int i;
@@ -187,4 +194,5 @@
     if (!KEEP_UBERCAL)    { snprintf (tmpline, 1024, "%s -reset-ubercal",	command); 		                          strcpy (command, tmpline); }
     if (UPDATE_CATFORMAT) { snprintf (tmpline, 1024, "%s -update-catformat %s", command, UPDATE_CATFORMAT); 		          strcpy (command, tmpline); }
+    if (BOUNDARY_TREE)    { snprintf (tmpline, 1024, "%s -boundary-tree %s",    command, BOUNDARY_TREE); 		          strcpy (command, tmpline); }
     if (SET_MREL_VERSION != 1) { snprintf (tmpline, 1024, "%s -set-mrel-version %d", command, SET_MREL_VERSION);                  strcpy (command, tmpline); }
     if (AreaSelect)       { snprintf (tmpline, 1024, "%s -area %f %f %f %f",    command, AreaXmin, AreaXmax, AreaYmin, AreaYmax); strcpy (command, tmpline); }
Index: /trunk/Ohana/src/relphot/src/select_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/select_images.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/select_images.c	(revision 35759)
@@ -407,4 +407,7 @@
   tcoords.pc1_1  = tcoords.pc2_2 = 1.0;
   tcoords.pc1_2  = tcoords.pc2_1 = 0.0;
+
+  tcoords.Npolyterms = 0;
+  memset (tcoords.polyterms, 0, 14*sizeof(float));
   strcpy (tcoords.ctype, "RA---TAN");
 
Index: /trunk/Ohana/src/relphot/src/setMrelCatalog.c
===================================================================
--- /trunk/Ohana/src/relphot/src/setMrelCatalog.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/setMrelCatalog.c	(revision 35759)
@@ -110,4 +110,5 @@
     if (!isSetMrelFinal && (secfilt[Nsec].flags & STAR_BAD)) continue;
 
+    int NexpPS1 = 0;
     int Ncode = 0;
     int Next = 0;
@@ -132,4 +133,9 @@
       if (code->equiv != thisCode) { continue; }
       Ncode ++;
+
+      // are we a PS1 exposure photcode?
+      if ((measure[k].photcode >= 10000) && (measure[k].photcode <= 10500)) {
+	NexpPS1 ++;
+      }
 
       if (measureT[k].dbFlags & MEAS_BAD) SKIP_THIS_MEAS(Nbad); 
@@ -272,4 +278,13 @@
     int Nminmeas = isSetMrelFinal ? 1 : STAR_TOOFEW + 1;
 
+    // XXX : ugh : another hard-wired photcode entry...
+    if (isSetMrelFinal && (pass == 0)) {
+      if ((thisCode < 6) || (thisCode == 9)) {
+	secfilt[Nsec].Ncode = NexpPS1;
+      } else {
+	secfilt[Nsec].Ncode = Ncode; // 2MASS data if it exists
+      }
+    }
+
     // when performing the grid analysis, STAR_TOOFEW should be set to 1;
     if (N < Nminmeas) { /* too few measurements */
@@ -309,5 +324,5 @@
 
       secfilt[Nsec].Mstdev = 1000.0*stats->sigma; // Mstdev is in millimags (not enough space for more precision)
-      secfilt[Nsec].Ncode = Ncode;
+      // secfilt[Nsec].Ncode = Ncode;
       secfilt[Nsec].Nused = stats->Nmeas;
 
@@ -391,5 +406,8 @@
 
   // set the name of the primary skycell (this is used in a strcmp to match the skycells in stack detections)
-  BoundaryTreePrimaryCell(primaryCell, average[0].R, average[0].D);
+  // XXX drop BoundaryTreePrimaryCell(primaryCell, average[0].R, average[0].D);
+  int projectID = -1;
+  int skycellID = -1;
+  BoundaryTreePrimaryCellIDs(&projectID, &skycellID, average[0].R, average[0].D);
 
   int NstackGood = 0;
@@ -459,5 +477,5 @@
       // which stack image should we use for the mean value?
       // if we request the primary (USE_TREE_FOR_PRIMARY), then find the min distances for data from the primary cell
-      if (MatchImageName (meas, cat, primaryCell)) {
+      if (MatchImageSkycellID (meas, cat, projectID, skycellID)) {
 	float stackPrimaryOffset = getCenterOffset (meas, cat, &measure[k], &stackImageID);
 	if (stackPrimaryOffset < stackPrimaryOffsetMin) {
@@ -550,4 +568,5 @@
     if (stackPrimaryMeasureMin >= 0) {
       measure[k].dbFlags |= ID_MEAS_STACK_PRIMARY;
+      secfilt[Nsec].flags |= ID_SECF_STACK_PRIMARY;
     }
 
Index: /trunk/Ohana/src/relphot/src/setMrelFinal.c
===================================================================
--- /trunk/Ohana/src/relphot/src/setMrelFinal.c	(revision 35758)
+++ /trunk/Ohana/src/relphot/src/setMrelFinal.c	(revision 35759)
@@ -16,9 +16,15 @@
     // flags used by the photometry analysis (excluding UBERCAL)
     unsigned int PHOTOM_FLAGS = 
-      ID_MEAS_NOCAL |       // detection ignored for this analysis (photcode, time range)
-      ID_MEAS_POOR_PHOTOM | // detection is photometry outlier 
-      ID_MEAS_SKIP_PHOTOM | // detection was ignored for photometry measurement 
-      ID_MEAS_AREA;	  // detetion was outside acceptable area of device
-    
+      ID_MEAS_NOCAL |          // detection ignored for this analysis (photcode, time range)
+      ID_MEAS_POOR_PHOTOM |    // detection is photometry outlier 
+      ID_MEAS_SKIP_PHOTOM |    // detection was ignored for photometry measurement 
+      ID_MEAS_AREA |	       // detetion was outside acceptable area of device
+      ID_MEAS_SYNTH_MAG |      // magnitude is synthetic
+      ID_MEAS_STACK_PRIMARY |  // this stack measurement is in the primary skycell
+      ID_MEAS_STACK_PHOT_SRC;  // this measurement supplied the stack photometry
+
+    // ID_MEAS_PHOTOM_UBERCAL -- externally-supplied zero point from ubercal analysis
+    // this is set by 'setphot', do not reset here
+
     // flags used by the photometry analysis (excluding UBERCAL)
     // unsigned int secfiltFlags = 
