Index: /branches/eam_branches/ipp-20130509/Ohana/src/relphot/include/relphot.h
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/relphot/include/relphot.h	(revision 35736)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/relphot/include/relphot.h	(revision 35737)
@@ -118,5 +118,4 @@
   int projID;
   int skycellID;
-  unsigned int tzero;
   unsigned int tzero;
   unsigned char trate;
@@ -401,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: /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/BoundaryTreeOps.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/BoundaryTreeOps.c	(revision 35736)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/BoundaryTreeOps.c	(revision 35737)
@@ -53,7 +53,6 @@
   int zone, band;
 
-  if (!primaryCellName) return FALSE;
-
-  primaryCellName[0] = 0;
+  *projID = -1;
+  *skycellID = -1;
 
   if (!tree) return FALSE;
@@ -64,9 +63,4 @@
   }
   
-# define USE_PROJECTION_CELL 0
-# if (USE_PROJECTION_CELL)
-  snprintf (primaryCellName, DVO_MAX_PATH, "RINGS.V3.%s", tree->name[zone][band]);
-# else
-
   // 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.
@@ -80,12 +74,6 @@
   int N = xi + tree->NX_SUB * yi;
   
-  // XXX short-circuit this for now (we should use this code if we make tree have more variable NX,NY values
-  // char format[24], skycellname[128];
-  // int Ndigit = (int)(log10(tree->NX_SUB*tree->NY_SUB)) + 1 ;
-  // snprintf (format, 24, "%s.%%0%dd", tree->name[zone][band], Ndigit);
-  // snprintf (skycellname, 128, format, N);
-
-  snprintf (primaryCellName, DVO_MAX_PATH, "RINGS.V3.%s.%03d", tree->name[zone][band], N);
-# endif
+  *projID = tree->projID[zone][band];
+  *skycellID = N;
 
   return TRUE;
Index: /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/ImageOps.c	(revision 35736)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/ImageOps.c	(revision 35737)
@@ -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;
@@ -436,10 +461,7 @@
 
 // returns image.Mcal - ff(x,y)
-int MatchImageSkycellID (off_t meas, int cat, char *name) {
-
-  off_t i;
-
-  if (!name) return FALSE;
-  if (!name[0]) return FALSE;
+int MatchImageSkycellID (off_t meas, int cat, int myProjectionID, int mySkycellID) {
+
+  off_t i;
 
   if (!MeasureToImage) return FALSE;
@@ -448,12 +470,11 @@
   if (i == -1) return FALSE;
 
-  // this is a bit crude: stack image names are of the form:
-  // RINGS.V3.skycell.1495.027.sky.191211.stk.988232.cmf.
-
-  // the primaryCell has a name of the form RINGS.V3.skycell.1495 or RINGS.V3.skycell.1495.027
-  // (if we use projection or skycell as the primary)
-
-  if (!strncmp(image[i].name, name, strlen(name))) return TRUE;
-  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;
 }
 
Index: /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/ImageSubset.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/ImageSubset.c	(revision 35736)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/ImageSubset.c	(revision 35737)
@@ -58,14 +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 (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);
+  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 
@@ -161,19 +161,14 @@
   // 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;
-
-    projID[i] = -1;
-    skycellID[i] = -1;
-    if (!strncmp (image[i].name, "RINGS.V3.skycell", strlen("RINGS.V3.skycell"))) {
-      projID[i] = atoi(&image[i].name[18]);
-      skycellID[i] = atoi(&image[i].name[23]);
-    }
+    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;
   }
 
Index: /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/reload_catalogs.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/reload_catalogs.c	(revision 35736)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/reload_catalogs.c	(revision 35737)
@@ -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;
Index: /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/setMrelCatalog.c
===================================================================
--- /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/setMrelCatalog.c	(revision 35736)
+++ /branches/eam_branches/ipp-20130509/Ohana/src/relphot/src/setMrelCatalog.c	(revision 35737)
@@ -406,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;
@@ -474,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) {
