Index: /branches/eam_branches/ipp-20140206/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/libdvo/include/dvo.h	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/libdvo/include/dvo.h	(revision 36519)
@@ -409,4 +409,5 @@
   uint32_t       flags;
   int            catID;
+  int            nOwn;
 } AverageTiny;
 
Index: /branches/eam_branches/ipp-20140206/Ohana/src/libdvo/src/RegionHostTable.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/libdvo/src/RegionHostTable.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/libdvo/src/RegionHostTable.c	(revision 36519)
@@ -85,4 +85,9 @@
     if (line[i] == '#') continue;
     if (line[i] == 0) continue;
+
+    UserPatch.Rmin = 360;
+    UserPatch.Rmax =   0;
+    UserPatch.Dmin = +90;
+    UserPatch.Dmax = -90;
 
     double Rmin, Rmax, Dmin, Dmax;
@@ -116,4 +121,9 @@
     hosts[Nhosts].Dmax = Dmax;
     
+    UserPatch.Rmin = MIN(Rmin, UserPatch.Rmin);
+    UserPatch.Rmax = MIN(Rmax, UserPatch.Rmax);
+    UserPatch.Dmin = MIN(Dmin, UserPatch.Dmin);
+    UserPatch.Dmax = MIN(Dmax, UserPatch.Dmax);
+
     // InitIOBuffer (&hosts[Nhosts].stdout, 1000);
     // InitIOBuffer (&hosts[Nhosts].stderr, 1000);
@@ -147,2 +157,78 @@
   return table;
 }
+
+// wait for all children to complete, report output to stdout
+int RegionHostTableWaitJobs (RegionHostTable *regionHosts, char *file, int lineno) {
+
+  int i;
+
+  // we have launched regionHosts->Nhosts jobs; wait for all of them to complete...
+  // if one (N) failed to launch, we will get an ECHILD error from the last (N) calls
+  int done = FALSE;
+  for (i = 0; !done  && (i < regionHosts->Nhosts); i++) {
+    int status = 0;
+
+    // XXX we'll need to pass in WNOHANG and keep retrying if we want to have a timeout...
+    int pid = waitpid (-1, &status, 0);
+    if (!pid) {
+      // this should only occur if we called waitpid with the WNOHANG option
+      fprintf (stderr, "programming error (1)? %s %d", file, lineno);
+      exit (2);
+    }
+    if (pid == -1) {
+      switch (errno) {
+	case ECHILD:
+	  done = TRUE;
+	  break;
+	default:
+	  fprintf (stderr, "programming error (2)? %s %d", file, lineno);
+	  exit (2);
+      }
+    }
+
+    // when the host has finished, close the open sockets
+
+    // find the host which has finished
+    int Nout, j;
+    int found = FALSE;
+    for (j = 0; j < regionHosts->Nhosts; j++) {
+      if (regionHosts->hosts[j].pid != pid) continue;
+      found = TRUE;
+      // check on the status of this and report any output?
+      fprintf (stderr, "job finished for %s (%d)\n", regionHosts->hosts[j].hostname, pid);
+      // read the stderr and stdout
+      IOBuffer buffer;
+      InitIOBuffer (&buffer, 100);
+      EmptyIOBuffer (&buffer, 100, regionHosts->hosts[j].stdio[HOST_STDOUT]);
+      fprintf (stderr, "--- stdout from %s ---\n", regionHosts->hosts[j].hostname);
+      Nout = write (STDOUT_FILENO, buffer.buffer, buffer.Nbuffer);
+      if (Nout != buffer.Nbuffer) { fprintf (stderr, "(error writing log?)\n"); }
+      fprintf (stderr, "\n");
+	  
+      InitIOBuffer (&buffer, 100);
+      EmptyIOBuffer (&buffer, 100, regionHosts->hosts[j].stdio[HOST_STDERR]);
+      fprintf (stderr, "--- stderr from %s ---\n", regionHosts->hosts[j].hostname);
+      Nout = write (STDOUT_FILENO, buffer.buffer, buffer.Nbuffer);
+      if (Nout != buffer.Nbuffer) { fprintf (stderr, "(error writing log?)\n"); }
+      fprintf (stderr, "\n");
+      if (WIFEXITED(status)) {
+	fprintf (stderr, "normal completion, exit status is %d\n", WEXITSTATUS(status));
+	regionHosts->hosts[j].status = WEXITSTATUS(status);
+	if (regionHosts->hosts[j].status) {
+	  fprintf (stderr, "job failed on %s\n", regionHosts->hosts[j].hostname);
+	  continue;
+	}
+      } else {
+	regionHosts->hosts[j].status = -1;
+	fprintf (stderr, "job exited abnormally on %s\n", regionHosts->hosts[j].hostname);
+	continue;
+      }
+    }
+    if (!found) {
+      fprintf (stderr, "Programming error: failed to matched finished job to known host!\n");
+      exit (2);
+    }
+  }
+  return TRUE;
+}
+
Index: /branches/eam_branches/ipp-20140206/Ohana/src/libdvo/src/dvo_catalog.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/libdvo/src/dvo_catalog.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/libdvo/src/dvo_catalog.c	(revision 36519)
@@ -119,4 +119,5 @@
   average->measureOffset   = -1;
   average->catID     	   = 0;
+  average->nOwn     	   = 0;
 }
 
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/include/relphot.h
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/include/relphot.h	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/include/relphot.h	(revision 36519)
@@ -178,4 +178,9 @@
 char   SKY_TABLE[DVO_MAX_PATH];
 int    SKY_DEPTH;  /** XXX EAM : depth of catalog tables, fix usage */
+
+// globals for parallel region operations
+char  *REGION_FILE;
+char  *IMAGE_TABLE;
+int    REGION_HOST_ID;
 
 int          HOST_ID;
@@ -290,5 +295,5 @@
 void          InterpolateGrid     PROTO((float *buffer, int Nx, int Ny, Coords *ccd, Coords *gcoords));
 off_t        *SelectRefMosaic     PROTO((Mosaic **refmosaic, off_t *Nimage));
-int           args                PROTO((int argc, char **argv));
+RelphotMode   args                PROTO((int argc, char **argv));
 int           args_client         PROTO((int argc, char **argv));
 int           bcatalog            PROTO((Catalog *subcatalog, Catalog *catalog, int Ncat));
@@ -339,5 +344,5 @@
 void          initMosaics         PROTO((Image *subset, off_t Nsubset, Image *image, char *inSubset, off_t Nimage));
 void          initMrel            PROTO((Catalog *catalog, int Ncatalog));
-void          initialize          PROTO((int argc, char **argv));
+RelphotMode   initialize          PROTO((int argc, char **argv));
 void          initialize_client   PROTO((int argc, char **argv));
 void          liststats_setmode   PROTO((StatType *stats, char *strmode));
@@ -487,2 +492,7 @@
 ImageMag *ImageMagLoad(char *filename, off_t *nimage_mags);
 int ImageMagSave(char *filename, ImageMag *image_mags, off_t Nimage_mags);
+
+int markObjects (Catalog *catalog, int Ncatalog);
+
+int relphot_parallel_images ();
+int relphot_parallel_regions ();
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/ImageMagIO.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/ImageMagIO.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/ImageMagIO.c	(revision 36519)
@@ -69,12 +69,12 @@
   ALLOCATE (image_mags, ImageMag, Nrow);
   for (i = 0; i < Nrow; i++) {
-    image_mag[i].Mcal                      = Mcal       [i];
-    image_mag[i].dMcal                     = dMcal      [i];
-    image_mag[i].dMagSys                   = dMagSys    [i];
-    image_mag[i].Xm                        = Xm         [i];
-    image_mag[i].nFitPhotom                = nFitPhotom [i];
-    image_mag[i].flags                     = flags      [i];
-    image_mag[i].ubercalDist               = ubercalDist[i];
-    image_mag[i].imageID                   = imageID    [i];
+    image_mags[i].Mcal                      = Mcal       [i];
+    image_mags[i].dMcal                     = dMcal      [i];
+    image_mags[i].dMagSys                   = dMagSys    [i];
+    image_mags[i].Xm                        = Xm         [i];
+    image_mags[i].nFitPhotom                = nFitPhotom [i];
+    image_mags[i].flags                     = flags      [i];
+    image_mags[i].ubercalDist               = ubercalDist[i];
+    image_mags[i].imageID                   = imageID    [i];
   }
   fprintf (stderr, "loaded data for %lld images\n", (long long) Nrow);
@@ -89,6 +89,6 @@
   free (imageID    );
 
-  *nimage_mag = Nrow;
-  return image_mag;
+  *nimage_mags = Nrow;
+  return image_mags;
 }
 
@@ -148,12 +148,12 @@
   // assign the storage arrays
   for (i = 0; i < Nimage_mags; i++) {
-    Mcal       [i]   = image_mag[i].Mcal       ;
-    dMcal      [i]   = image_mag[i].dMcal      ;
-    dMagSys    [i]   = image_mag[i].dMagSys    ;
-    Xm         [i]   = image_mag[i].Xm         ;
-    nFitPhotom [i]   = image_mag[i].nFitPhotom ;
-    flags      [i]   = image_mag[i].flags      ;
-    ubercalDist[i]   = image_mag[i].ubercalDist;
-    imageID    [i]   = image_mag[i].imageID    ;
+    Mcal       [i]   = image_mags[i].Mcal       ;
+    dMcal      [i]   = image_mags[i].dMcal      ;
+    dMagSys    [i]   = image_mags[i].dMagSys    ;
+    Xm         [i]   = image_mags[i].Xm         ;
+    nFitPhotom [i]   = image_mags[i].nFitPhotom ;
+    flags      [i]   = image_mags[i].flags      ;
+    ubercalDist[i]   = image_mags[i].ubercalDist;
+    imageID    [i]   = image_mags[i].imageID    ;
   }
 
@@ -179,5 +179,5 @@
   FILE *f = fopen (filename, "w");
   if (!f) {
-    fprintf (stderr, "ERROR: cannot open image_mag file for output %s\n", filename);
+    fprintf (stderr, "ERROR: cannot open image_mags file for output %s\n", filename);
     return FALSE;
   }
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/ImageTable.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/ImageTable.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/ImageTable.c	(revision 36519)
@@ -3,10 +3,7 @@
 Image *ImageTableLoad(char *filename, off_t *nimage) {
 
-  int i, Ncol;
-  off_t Nrow;
-  Header header;
-  Header theader;
-  Matrix matrix;
-  FTable ftable;
+  int status;
+  off_t Nimage;
+  FITS_DB db;
 
   db.mode   = dvo_catalog_catmode (CATMODE);
@@ -30,5 +27,5 @@
   // convert database table to internal structure (binary to Image)
   // 'image' points to the same memory as db->ftable->buffer
-  Image *image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
+  Image *image = gfits_table_get_Image (&db.ftable, &Nimage, &db.swapped);
   if (!image) {
     Shutdown ("ERROR: failed to read images");
@@ -41,5 +38,5 @@
 int ImageTableSave (char *filename, Image *images, off_t Nimages) {
 
-  int i;
+  int status;
   FITS_DB db;
 
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/StarOps.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/StarOps.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/StarOps.c	(revision 36519)
@@ -898,4 +898,7 @@
 int markObjects (Catalog *catalog, int Ncatalog) {
 
+  int i, n;
+  off_t j;
+
   // How strongly do I own this object?
   for (i = 0; i < Ncatalog; i++) {
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/args.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/args.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/args.c	(revision 36519)
@@ -343,7 +343,8 @@
   }
 
+  REGION_FILE = NULL;
   if ((N = get_argument (argc, argv, "-region-hosts"))) {
     remove_argument (N, &argc, argv);
-    REGION_FILE = strcreate (argv[N])
+    REGION_FILE = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
   }
@@ -352,5 +353,5 @@
   if ((N = get_argument (argc, argv, "-region-hostID"))) {
     remove_argument (N, &argc, argv);
-    REGION_HOST_ID = atoi (argv[N])
+    REGION_HOST_ID = atoi (argv[N]);
     remove_argument (N, &argc, argv);
   }
@@ -365,9 +366,10 @@
     mode = APPLY_OFFSETS;
   }
+  IMAGE_TABLE = NULL;
   if ((N = get_argument (argc, argv, "-parallel-images"))) {
     remove_argument (N, &argc, argv);
     mode = PARALLEL_IMAGES;
     if (N >= argc) relphot_usage();
-    IMAGE_TABLE = strcreate (argv[N])
+    IMAGE_TABLE = strcreate (argv[N]);
     remove_argument (N, &argc, argv);
     if (!REGION_FILE) relphot_usage();
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/assign_images.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/assign_images.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/assign_images.c	(revision 36519)
@@ -34,12 +34,14 @@
 
 // assign images to the region hosts; at the end, each host will have its list of images
-int select_images_hostregion (RegionHostTable *hosts, Image *image, off_t Nimage) {
+int select_images_hostregion (RegionHostTable *regionHosts, Image *image, off_t Nimage) {
 
-  INITTIME;
+  int i, j, ecode, found;
+
+  // INITTIME;
   
   for (i = 0; i < regionHosts->Nhosts; i++) {
-    regionHosts->host[i].Nimage = 0;
-    regionHosts->host[i].NIMAGE = D_NIMAGE;
-    ALLOCATE (regionHosts->host[i].image, Image, regionHosts->host[i].NIMAGE);
+    regionHosts->hosts[i].Nimage = 0;
+    regionHosts->hosts[i].NIMAGE = D_NIMAGE;
+    ALLOCATE (regionHosts->hosts[i].image, Image, regionHosts->hosts[i].NIMAGE);
   }
 
@@ -71,4 +73,5 @@
 
     /* define image center - note the DIS images (mosaic phu) are special */
+    double Xc, Yc, Rc, Dc;
     if (!strcmp(&image[j].coords.ctype[4], "-DIS")) {
       Xc = 0.0; Yc = 0.0;
@@ -82,15 +85,16 @@
 
     i = find_host_for_coords (regionHosts, Rc, Dc);
+    if (i == -1) continue;
 
     // this is a bit memory expensive : I am making a complete copy of the image table here
-    off_t Nsubset = regionHosts->host[i].Nimage;
-    regionHosts->host[i].image[Nsubset] = image[j];
-    // regionHosts->host[i].line_number[Nsubset] = j;
+    off_t Nsubset = regionHosts->hosts[i].Nimage;
+    regionHosts->hosts[i].image[Nsubset] = image[j];
+    // regionHosts->hosts[i].line_number[Nsubset] = j;
 
-    regionHosts->host[i].Nimage ++;
-    if (regionHosts->host[i].Nimage == regionHosts->host[i].NIMAGE) {
-      regionHosts->host[i].NIMAGE += D_NIMAGE;
-      REALLOCATE (regionHosts->host[i].image, Image, regionHosts->host[i].NIMAGE);
-      // REALLOCATE (regionHosts->host[i].line_number, off_t, regionHosts->host[i].NIMAGE);
+    regionHosts->hosts[i].Nimage ++;
+    if (regionHosts->hosts[i].Nimage == regionHosts->hosts[i].NIMAGE) {
+      regionHosts->hosts[i].NIMAGE += D_NIMAGE;
+      REALLOCATE (regionHosts->hosts[i].image, Image, regionHosts->hosts[i].NIMAGE);
+      // REALLOCATE (regionHosts->hosts[i].line_number, off_t, regionHosts->hosts[i].NIMAGE);
     }
   }
@@ -99,9 +103,11 @@
 
 // XXX add a search tree to speed this up?
-int find_host_for_coords (RegionHostTable *regionHosts, double R, double d) {
+int find_host_for_coords (RegionHostTable *regionHosts, double Rc, double Dc) {
+
+  int i;
 
   for (i = 0; i < regionHosts->Nhosts; i++) {
 
-    RegionHostInfo *host = regionHosts->hosts[i];
+    RegionHostInfo *host = &regionHosts->hosts[i];
     if (Rc <  host->Rmin) continue;
     if (Rc >= host->Rmax) continue;
@@ -111,4 +117,5 @@
     return i;
   }
+  return -1;
 }
 
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/launch_region_hosts.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/launch_region_hosts.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/launch_region_hosts.c	(revision 36519)
@@ -1,6 +1,6 @@
 # include "relphot.h"
+# define DEBUG 0
 
-// strextend (command, "-v");
-int strextend (char *input, char *format,..) {
+int strextend (char *input, char *format,...) {
 
   char tmpfmt[1024], tmpline[1024];
@@ -10,5 +10,5 @@
   
   snprintf (tmpfmt, 1024, "%%s %s", format);
-  vsnprintf (tmpline, 1024, tmpfmt, input, argp);
+  vsnprintf (tmpline, 1024, tmpfmt, argp);
   strcpy (input, tmpline);
 
@@ -26,8 +26,9 @@
     // communication files:
     // subset images per host : CATDIR/Image.HOSTNAME.fits
-    sprintf (filename, "%s/Image.%s.fits", CATDIR, host->hostname);
+    char filename[1024];
+    snprintf (filename, 1024, "%s/Image.%s.fits", CATDIR, host->hostname);
 
     // write the image subset for this host
-    ImageTableSave (filename, host->images, host->Nimages);
+    ImageTableSave (filename, host->image, host->Nimage);
 
     char command[1024];
@@ -58,5 +59,6 @@
 
   // XXX probably need an alternate function here...
-  HostTableWaitJobs (table, __FILE__, __LINE__, VERBOSE);
-
+  RegionHostTableWaitJobs (regionHosts, __FILE__, __LINE__);
+ 
+  return TRUE;
 }
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/relphot_parallel_images.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/relphot_parallel_images.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/relphot_parallel_images.c	(revision 36519)
@@ -12,8 +12,6 @@
 int relphot_parallel_images () {
 
-  int i, status, Ncatalog;
+  int i, Ncatalog;
   Catalog *catalog = NULL;
-  FITS_DB db;
-  SkyList *skylist = NULL;
 
   INITTIME;
@@ -37,5 +35,5 @@
   SkyTable *sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
   SkyTableSetFilenames (sky, CATDIR, "cpt");
-  SkyList *skylist = SkyListByBounds (sky, UserPatch.Rmin, UserPatch.Rmax, UserPatch.Dmin, UserPatch.Dmax);
+  SkyList *skylist = SkyListByBounds (sky, -1, UserPatch.Rmin, UserPatch.Rmax, UserPatch.Dmin, UserPatch.Dmax);
 
   /* load catalog data from region files (hostID is 0 since we are not a client */
@@ -111,5 +109,5 @@
   MARKTIME("-- finalize Mcal values: %f sec\n", dtime);
 
-  share_image_mags (regionHosts, i);
+  share_image_mags (regionHosts, -1);
 
   // save_image_subset ();
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/relphot_parallel_regions.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/relphot_parallel_regions.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/relphot_parallel_regions.c	(revision 36519)
@@ -5,10 +5,17 @@
 int relphot_parallel_regions () {
 
-  int i, status, Ncatalog;
-  Catalog *catalog = NULL;
+  int status;
   FITS_DB db;
-  SkyList *skylist = NULL;
 
   INITTIME;
+
+  // load the flat correction table (if defined)
+  char flatcorrfile[256];
+  sprintf (flatcorrfile, "%s/flatcorr.fits", CATDIR);
+  FlatCorrectionTable *flatcorr = FlatCorrectionLoad (flatcorrfile, VERBOSE);
+
+  SkyTable *sky = SkyTableLoadOptimal (CATDIR, SKY_TABLE, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
+  SkyTableSetFilenames (sky, CATDIR, "cpt");
+  SkyList *skylist = SkyListByBounds (sky, -1, UserPatch.Rmin, UserPatch.Rmax, UserPatch.Dmin, UserPatch.Dmax);
 
   // load the RegionTable (UserRegion should not be used at this level)
@@ -45,8 +52,8 @@
 
   // retrieve updated image parameters from the remote hosts (also set Image.mcal)
-  slurp_image_mags (regionHosts, XXX);
+  slurp_image_mags (regionHosts, -1);
 
   /* update catalogs (in parallel) */
-  reload_catalogs ();
+  reload_catalogs (skylist, flatcorr, 0, NULL);
 
   // do not save changes if we did not make changes. 
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/share_image_mags.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/share_image_mags.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/share_image_mags.c	(revision 36519)
@@ -6,4 +6,5 @@
 int share_image_mags (RegionHostTable *regionHosts, int nloop) {
 
+  off_t i, Nimages;
   Image *images = getimages (&Nimages, NULL);
 
@@ -16,5 +17,5 @@
   for (i = 0; i < Nimages; i++) {
     // XXX does this image have missing detections (does someone else need it?)
-    if (imageExtra[i].Nmiss == 0) continue;
+    // XXX : NOTE NEED TO FIX THIS: if (imageExtra[i].Nmiss == 0) continue;
     
     set_image_mags (&image_mags[Nimage_mags], &images[i]);
@@ -30,5 +31,5 @@
   char filename[1024];
   snprintf (filename, 1024, "%s/%s.imagemags.fits", CATDIR, hostname);
-  ImageMagsSave (filename, image_mags, Nimage_mags);
+  ImageMagSave (filename, image_mags, Nimage_mags);
 
   update_imsync_file (hostname, nloop);
@@ -39,5 +40,6 @@
 int slurp_image_mags (RegionHostTable *regionHosts, int nloop) {
 
-  Image *getimages (&Nimage, NULL);
+  off_t Nimage, i;
+  Image *images = getimages (&Nimage, NULL);
 
   int Nimage_mags = 0;
@@ -45,5 +47,5 @@
   ALLOCATE (image_mags, ImageMag, 1);
 
-  for (i = 0; i < Nhost; i++) {
+  for (i = 0; i < regionHosts->Nhosts; i++) {
     if (regionHosts->hosts.hostsID == REGION_HOST_ID) continue;
     // XXX add neighbor check? : if (not_neighbor(host[i])) continue;
@@ -53,5 +55,7 @@
     char filename[1024];
     snprintf (filename, 1024, "%s/%s.imagemags.fits", CATDIR, regionHosts->hosts[i].hostname);
-    ImageMag *image_mags_subset = ImageMagLoad (host[i], &Nsubset);
+
+    off_t Nsubset;
+    ImageMag *image_mags_subset = ImageMagLoad (regionHosts->hosts[i], &Nsubset);
 
     image_mags = merge_image_mags (image_mags, &Nimage_mags, image_mags_subset, Nsubset);
@@ -59,16 +63,16 @@
 
   for (i = 0; i < Nimage_mags; i++) {
-    seq = getImageByID (image_mags[i].ID);
+    off_t seq = getImageByID (image_mags[i].ID);
     if (seq < 0) {
       // XXX is this a problem? (no, other hosts don't know which images I own)
       continue;
     }
-    image[seq].Mcal  	   = image_mags[i].Mcal;
-    image[seq].dMcal  	   = image_mags[i].dMcal;
-    image[seq].dMagSys	   = image_mags[i].dMagSys;
-    image[seq].Xm  	   = image_mags[i].Xm;
-    image[seq].nFitPhotom  = image_mags[i].nFitPhotom;
-    image[seq].flags 	   = image_mags[i].flags;
-    image[seq].ubercalDist = image_mags[i].ubercalDist;
+    images[seq].Mcal  	   = image_mags[i].Mcal;
+    images[seq].dMcal  	   = image_mags[i].dMcal;
+    images[seq].dMagSys	   = image_mags[i].dMagSys;
+    images[seq].Xm  	   = image_mags[i].Xm;
+    images[seq].nFitPhotom  = image_mags[i].nFitPhotom;
+    images[seq].flags 	   = image_mags[i].flags;
+    images[seq].ubercalDist = image_mags[i].ubercalDist;
   }
 }
@@ -78,4 +82,5 @@
 int check_imsync_file (char *hostname, int nloop) {
 
+  char message[MSG_LENGTH];
   char filename[1024];
   sprintf (filename, "%s/%s.image.sync", CATDIR, hostname);
@@ -83,5 +88,5 @@
   FILE *f = NULL; 
 
-  while (retry) {
+  while (TRUE) {
 
     f = fopen (filename);
@@ -100,4 +105,5 @@
 
     // message is of the form: NLOOP: %03d
+    int loop;
     sscanf (message, "%s %d", &loop);
     if (loop != nloop) {
@@ -111,4 +117,5 @@
 int update_imsync_file (char *hostname, int nloop) {
 
+  char message[MSG_LENGTH];
   char filename[1024];
   sprintf (filename, "%s/%s.image.sync", CATDIR, hostname);
@@ -140,5 +147,8 @@
 int set_image_mags (ImageMag *image_mags, Image *image) {
 
-  image_mags->M  = image->Mcal;
+  fprintf (stderr, "ERROR: need to fix this function\n");
+  exit (2);
+
+  image_mags->Mcal  = image->Mcal;
 
   return TRUE;
@@ -147,7 +157,9 @@
 ImageMag *merge_image_mags (ImageMag *target, int *ntarget, ImageMag *source, int Nsource) {
 
+  off_t i;
+
   REALLOCATE (target, ImageMag, *ntarget + Nsource);
   for (i = 0; i < Nsource; i++) {
-    int n = i + *ntarget;
+    off_t n = i + *ntarget;
     target[n] = source[i];
   }
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/share_mean_mags.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/share_mean_mags.c	(revision 36518)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/share_mean_mags.c	(revision 36519)
@@ -5,4 +5,7 @@
 # define D_NMEANMAGS 10000
 int share_mean_mags (Catalog *catalog, int Ncatalog, RegionHostTable *regionHosts, int nloop) {
+
+  int i;
+  off_t j;
 
   off_t Nmeanmags = 0;
@@ -55,11 +58,15 @@
 int slurp_mean_mags (Catalog *catalog, int Ncatalog, RegionHostTable *regionHosts, int nloop) {
 
+  off_t i;
+
   int Nmeanmags = 0;
   MeanMag *meanmags = NULL;
   ALLOCATE (meanmags, MeanMag, 1);
 
-  for (i = 0; i < Nhost; i++) {
-    if (not_neighbor(host[i])) continue;
-    // XXX skip regions->hosts[i].hostID == REGION_HOST_ID
+  int Nsecfilt = GetPhotcodeNsecfilt();
+
+  for (i = 0; i < regionHosts->Nhosts; i++) {
+    // if (not_neighbor(host[i])) continue;
+    if (regionHosts->hosts[i].hostID == REGION_HOST_ID) continue;
 
     check_sync_file (regionHosts->hosts[i].hostname, nloop);
@@ -68,6 +75,6 @@
     snprintf (filename, 1024, "%s/%s.meanmags.fits", CATDIR, regionHosts->hosts[i].hostname);
 
-    int Nsubset = 0;
-    MeanMag *meanmagsSubset = MeanMagLoad (host[i], &Nsubset);
+    off_t Nsubset = 0;
+    MeanMag *meanmagsSubset = MeanMagLoad (regionHosts->hosts[i].hostname, &Nsubset);
 
     // merge_mean_mags reallocs meanmags and frees the input meanmagsSubset
@@ -87,7 +94,8 @@
     }
 
-    Nsecfit = photcode_to_secfilt (meanmags[i].photcode);
-    catalog[catSeq].secfilt[objSeq*Nsec + Nsecfilt].mag = meanmags[i].mag;
-  }
+    int Nsec = photcode_to_secfilt (meanmags[i].photcode);
+    catalog[catSeq].secfilt[objSeq*Nsecfilt + Nsec].mag = meanmags[i].mag;
+  }
+  return TRUE;
 }
 
@@ -96,4 +104,5 @@
 int check_sync_file (char *hostname, int nloop) {
 
+  char message[MSG_LENGTH];
   char filename[1024];
   sprintf (filename, "%s/%s.meanmags.sync", CATDIR, hostname);
@@ -102,7 +111,7 @@
   FILE *f = NULL; 
 
-  while (retry) {
-
-    f = fopen (filename);
+  while (TRUE) {
+
+    f = fopen (filename, "r");
     if (!f) {
       usleep (2000000);
@@ -110,4 +119,5 @@
     }
 
+    // XXX MSG_LENGTH : 0 EOL byte?
     int Nread = fread (message, MSG_LENGTH, 1, f);
     if (Nread < MSG_LENGTH) {
@@ -119,4 +129,5 @@
 
     // message is of the form: NLOOP: %03d
+    int loop;
     sscanf (message, "%s %d", &loop);
     if (loop != nloop) {
@@ -126,4 +137,5 @@
     return TRUE;
   }
+  return FALSE;
 }
 
@@ -141,4 +153,5 @@
 int update_sync_file (char *hostname, int nloop) {
 
+  char message[MSG_LENGTH];
   char filename[1024];
   sprintf (filename, "%s/%s.meanmags.sync", CATDIR, hostname);
@@ -172,7 +185,9 @@
 MeanMag *merge_mean_mags (MeanMag *target, int *ntarget, MeanMag *source, int Nsource) {
 
+  off_t i;
+
   REALLOCATE (target, MeanMag, *ntarget + Nsource);
   for (i = 0; i < Nsource; i++) {
-    int n = i + *ntarget;
+    off_t n = i + *ntarget;
     target[n] = source[i];
   }
