Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/assign_images.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/assign_images.c	(revision 36610)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/assign_images.c	(revision 36611)
@@ -95,4 +95,22 @@
     }
     
+    // Exclude images with crazy astrometry
+    // XXX NOTE : this is gpc1-specific
+    { 
+      double dP1 = hypot(image[j].coords.pc1_1, image[j].coords.pc1_2);
+      double dP2 = hypot(image[j].coords.pc2_1, image[j].coords.pc2_2);
+      if (fabs(dP1 - 1.0) > 0.02) continue;
+      if (fabs(dP2 - 1.0) > 0.02) continue;
+
+      double X00, Y00, X10, Y10, X01, Y01;
+      XY_to_LM (&X00, &Y00, 0.0, 0.0, &image[j].coords);
+      XY_to_LM (&X10, &Y10, image[j].NX, 0.0, &image[j].coords);
+      XY_to_LM (&X01, &Y01, 0.0, image[j].NY, &image[j].coords);
+      double dS0 = hypot ((X00 - X10), (Y00 - Y10));
+      double dS1 = hypot ((X00 - X01), (Y00 - Y01));
+      if (dS0 > 6000) continue;
+      if (dS1 > 6500) continue;
+    }	
+
     // use a reference coordinate for each image to assign to hosts
     // define image center - note the DIS images (mosaic phu) are special
@@ -104,4 +122,6 @@
     XY_to_RD (&Rc, &Dc, Xc, Yc, &image[j].coords);
     Rc = ohana_normalize_angle_to_midpoint (Rc, 180.0);
+    image[j].RAo  = Rc;
+    image[j].DECo = Dc;
 
     i = find_host_for_coords (regionHosts, Rc, Dc);
@@ -241,4 +261,7 @@
   int i;
 
+  if (isnan(Rc)) return -1;
+  if (isnan(Dc)) return -1;
+
   for (i = 0; i < regionHosts->Nhosts; i++) {
 
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/launch_region_hosts.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/launch_region_hosts.c	(revision 36610)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relastro/src/launch_region_hosts.c	(revision 36611)
@@ -55,6 +55,13 @@
 
     char command[1024];
-    snprintf (command, 1024, "relastro -parallel-images %s -region-hosts %s -region-hostID %d -D CATDIR %s -region %f %f %f %f -statmode %s -minerror %f", 
-	      filename, REGION_FILE, host->hostID, CATDIR, host->RminCat, host->RmaxCat, host->DminCat, host->DmaxCat, STATMODE, MIN_ERROR);
+    snprintf (command, 1024, "relastro -parallel-images %s", filename);
+    strextend (command, "-region-hosts %s", REGION_FILE);
+    strextend (command, "-region-hostID %d", host->hostID);
+    strextend (command, "-D CATDIR %s", CATDIR);
+    strextend (command, "-region %f %f %f %f", host->RminCat, host->RmaxCat, host->DminCat, host->DmaxCat);
+    strextend (command, "-statmode %s", STATMODE);
+    strextend (command, "-minerror %f", MIN_ERROR);
+    strextend (command, "-nloop %d", NLOOP);
+    strextend (command, "-threads %d", NTHREADS);
 
     switch (FIT_TARGET) {
@@ -82,4 +89,21 @@
     if (PHOTCODE_SKIP_LIST) strextend (command, "-photcode %s", PHOTCODE_SKIP_LIST);
 
+    if (MaxDensityUse) 	    strextend (command, "-max-density %f", MaxDensityValue);
+    if (ImagSelect)         strextend (command, "-instmag %f %f", ImagMin, ImagMax);
+    if (PhotFlagSelect)     strextend (command, "+photflags"); 
+    if (PhotFlagBad)        strextend (command, "+photflagbad %d", PhotFlagBad); 
+    if (PhotFlagPoor)       strextend (command, "+photflagpoor %d", PhotFlagPoor); 
+
+    if (MinBadQF > 0.0)        strextend (command, "-min-bad-psfqf %f", MinBadQF);
+    if (MaxMeanOffset != 10.0) strextend (command, "-max-mean-offset  %f", MaxMeanOffset);
+
+    if (TimeSelect) { 
+      char *tstart = ohana_sec_to_date (TSTART);
+      char *tstop  = ohana_sec_to_date (TSTOP);
+      strextend (command, "-time %s %s", tstart, tstop); 
+      free (tstart);
+      free (tstop);
+    }
+
     fprintf (stderr, "command: %s\n", command);
     
@@ -102,5 +126,4 @@
     // remove client is done, go ahead with next client
     check_sync_file (syncfile, 1);
-    clear_sync_file (syncfile);
     free (syncfile);
   }
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/MosaicOps.c	(revision 36610)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/MosaicOps.c	(revision 36611)
@@ -511,10 +511,16 @@
   /* send results to initGridBins */
 
-  off_t i, j, m, NX, NY;
+  off_t i, j, m, NX, NY, NC, Nc;
   double R, D, Rmid, Dmid;
   double Mcal, dMcal, Xm;
-
-  for (i = 0; i < Nmosaic; i++) {
-    Rmid = Dmid = 0.0;
+  double *Rc, *Dc;
+
+  NC = 100;
+  ALLOCATE (Rc, double, NC);
+  ALLOCATE (Dc, double, NC);
+
+  for (i = 0; i < Nmosaic; i++) {
+    Nc = 0;
+    Rmid = Dmid = NAN;
     Mcal = dMcal = Xm = 0;
     for (j = 0; j < MosaicN_Image[i]; j++) {
@@ -529,7 +535,32 @@
       NY = image[m].NY;
       XY_to_RD (&R, &D, 0.5*NX, 0.5*NY, &image[m].coords);
-
-      Rmid += R;
-      Dmid += D;
+      R = ohana_normalize_angle_to_midpoint (R, 180.0);
+
+      // Exclude images with crazy astrometry
+      // XXX NOTE : this is gpc1-specific
+      { 
+	double dP1 = hypot(image[m].coords.pc1_1, image[m].coords.pc1_2);
+	double dP2 = hypot(image[m].coords.pc2_1, image[m].coords.pc2_2);
+	if (fabs(dP1 - 1.0) > 0.02) continue;
+	if (fabs(dP2 - 1.0) > 0.02) continue;
+
+	double X00, Y00, X10, Y10, X01, Y01;
+	XY_to_LM (&X00, &Y00, 0.0, 0.0, &image[m].coords);
+	XY_to_LM (&X10, &Y10, image[m].NX, 0.0, &image[m].coords);
+	XY_to_LM (&X01, &Y01, 0.0, image[m].NY, &image[m].coords);
+	double dS0 = hypot ((X00 - X10), (Y00 - Y10));
+	double dS1 = hypot ((X00 - X01), (Y00 - Y01));
+	if (dS0 > 6000) continue;
+	if (dS1 > 6500) continue;
+      }	
+
+      Rc[Nc] = R;
+      Dc[Nc] = D;
+      Nc ++;
+      if (Nc >= NC) {
+	NC += 100;
+	REALLOCATE (Rc, double, NC);
+	REALLOCATE (Dc, double, NC);
+      }
 
       Mcal  += image[m].Mcal;
@@ -544,6 +575,10 @@
       image[m].Xm    = NAN_S_SHORT;
     }
-    Rmid = Rmid / MosaicN_Image[i];
-    Dmid = Dmid / MosaicN_Image[i];
+    if (Nc > 0) {
+      dsort (Rc, Nc);
+      dsort (Dc, Nc);
+      Rmid = Rc[(int)(0.5*Nc)];
+      Dmid = Dc[(int)(0.5*Nc)];
+    }
 
     strcpy (mosaic[i].coords.ctype, "DEC--TAN");
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 36610)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/assign_images.c	(revision 36611)
@@ -88,6 +88,36 @@
     }
     
+    // Exclude images with crazy astrometry
+    // XXX NOTE : this is gpc1-specific
+    { 
+      double dP1 = hypot(image[j].coords.pc1_1, image[j].coords.pc1_2);
+      double dP2 = hypot(image[j].coords.pc2_1, image[j].coords.pc2_2);
+      if (fabs(dP1 - 1.0) > 0.02) continue;
+      if (fabs(dP2 - 1.0) > 0.02) continue;
+
+      double X00, Y00, X10, Y10, X01, Y01;
+      XY_to_LM (&X00, &Y00, 0.0, 0.0, &image[j].coords);
+      XY_to_LM (&X10, &Y10, image[j].NX, 0.0, &image[j].coords);
+      XY_to_LM (&X01, &Y01, 0.0, image[j].NY, &image[j].coords);
+      double dS0 = hypot ((X00 - X10), (Y00 - Y10));
+      double dS1 = hypot ((X00 - X01), (Y00 - Y01));
+      if (dS0 > 6000) continue;
+      if (dS1 > 6500) continue;
+    }	
+
     // use a reference coordinate for each image to assign to hosts
+    // define image center - note the DIS images (mosaic phu) are special, but we have 
+    // already excluded them above.  we also save the image centers for reference
+    double Xc, Yc;
     double Rc, Dc;
+
+    Xc = 0.5*image[j].NX; 
+    Yc = 0.5*image[j].NY;
+
+    XY_to_RD (&Rc, &Dc, Xc, Yc, &image[j].coords);
+    Rc = ohana_normalize_angle_to_midpoint (Rc, 180.0);
+    image[j].RAo  = Rc;
+    image[j].DECo = Dc;
+
     if (MOSAIC_ZEROPT) {
       // use the coords of the associated mosaic to select
@@ -95,12 +125,4 @@
       Rc = mosaic->coords.crval1;
       Dc = mosaic->coords.crval2;
-    } else {
-
-      // define image center - note the DIS images (mosaic phu) are special
-      double Xc, Yc;
-      Xc = 0.5*image[j].NX; 
-      Yc = 0.5*image[j].NY;
-
-      XY_to_RD (&Rc, &Dc, Xc, Yc, &image[j].coords);
       Rc = ohana_normalize_angle_to_midpoint (Rc, 180.0);
     }
@@ -242,4 +264,7 @@
   int i;
 
+  if (isnan(Rc)) return -1;
+  if (isnan(Dc)) return -1;
+
   for (i = 0; i < regionHosts->Nhosts; i++) {
 
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/client_logger.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/client_logger.c	(revision 36610)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/client_logger.c	(revision 36611)
@@ -5,9 +5,9 @@
 
 static FILE *logfile = NULL;
-int client_logger_init () {
+int client_logger_init (char *dirname) {
 
   char filename[DVO_MAX_PATH];
 
-  snprintf (filename, DVO_MAX_PATH, "%s/log.rlpc.XXXXXX", HOSTDIR);
+  snprintf (filename, DVO_MAX_PATH, "%s/log.rlpc.XXXXXX", dirname);
     
   int fd = mkstemp (filename);
@@ -32,4 +32,6 @@
   vfprintf (logfile, format, argp);
   va_end (argp);
+
+  fflush (logfile);
   return TRUE;
 }
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 36610)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/launch_region_hosts.c	(revision 36611)
@@ -58,16 +58,21 @@
 	      PhotcodeList, filename, REGION_FILE, host->hostID, CATDIR, host->RminCat, host->RmaxCat, host->DminCat, host->DmaxCat, STATMODE, CAMERA, STAR_TOOFEW, MIN_ERROR);
 
-    if (VERBOSE)       	 strextend (command, "-v");
-    if (VERBOSE2)      	 strextend (command, "-vv");
-    if (RESET)         	 strextend (command, "-reset");
-    if (RESET_ZEROPTS) 	 strextend (command, "-reset-zpts");
-    if (UPDATE)        	 strextend (command, "-update");
-    if (MOSAIC_ZEROPT) 	 strextend (command, "-mosaic");
-    if (FREEZE_IMAGES) 	 strextend (command, "-imfreeze");
-    if (FREEZE_MOSAICS)	 strextend (command, "-mosfreeze");
-    if (!KEEP_UBERCAL) 	 strextend (command, "-reset-ubercal");
-    if (PARALLEL)      	 strextend (command, "-parallel");
-    if (PARALLEL_MANUAL) strextend (command, "-parallel-manual");
-    if (PARALLEL_SERIAL) strextend (command, "-parallel-serial");
+    if (VERBOSE)       	     strextend (command, "-v");
+    if (VERBOSE2)      	     strextend (command, "-vv");
+    if (RESET)         	     strextend (command, "-reset");
+    if (RESET_ZEROPTS) 	     strextend (command, "-reset-zpts");
+    if (!KEEP_UBERCAL) 	     strextend (command, "-reset-ubercal");
+    if (DophotSelect)  	     strextend (command, "-dophot %d", DophotValue);
+    if (ImagSelect)    	     strextend (command, "-instmag %f %f", ImagMin, ImagMax);
+    if (MaxDensityUse) 	     strextend (command, "-max-density %f", MaxDensityValue);
+    if (SyntheticPhotometry) strextend (command, "-synthphot");
+
+    if (UPDATE)        	     strextend (command, "-update");
+    if (MOSAIC_ZEROPT) 	     strextend (command, "-mosaic");
+    if (FREEZE_IMAGES) 	     strextend (command, "-imfreeze");
+    if (FREEZE_MOSAICS)	     strextend (command, "-mosfreeze");
+    if (PARALLEL)      	     strextend (command, "-parallel");
+    if (PARALLEL_MANUAL)     strextend (command, "-parallel-manual");
+    if (PARALLEL_SERIAL)     strextend (command, "-parallel-serial");
 
     // XXX deprecate this if we are happy with the new version
@@ -94,5 +99,4 @@
     // remote client is done, go ahead with next client
     check_sync_file (syncfile, 1);
-    clear_sync_file (syncfile);
     free (syncfile);
   }
Index: /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/relphot_client.c
===================================================================
--- /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/relphot_client.c	(revision 36610)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/relphot_client.c	(revision 36611)
@@ -19,5 +19,5 @@
   // get configuration info, args, lockfile (set CATDIR, HOST_ID, HOSTDIR, etc) 
   initialize_client (argc, argv);
-  client_logger_init ();
+  client_logger_init (HOSTDIR);
 
   // load the current sky table (layout of all SkyRegions) 
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 36610)
+++ /branches/eam_branches/ipp-20140206/Ohana/src/relphot/src/relphot_parallel_images.c	(revision 36611)
@@ -19,8 +19,11 @@
   INITTIME;
 
+  client_logger_init (CATDIR);
+
   // load the RegionTable (UserRegion should not be used at this level)
   RegionHostTable *regionHosts = RegionHostTableLoad (CATDIR, REGION_FILE);
   int myHost = regionHosts->index[REGION_HOST_ID];
   RegionHostFindNeighbors (regionHosts, myHost);
+  client_logger_message ("started parallel images on %s\n", regionHosts->hosts[myHost].hostname);
 
   // load the subset images belonging to this host
@@ -52,4 +55,5 @@
   catalog = load_catalogs (skylist, &Ncatalog, 0, NULL);
   MARKTIME("-- load catalog data: %f sec\n", dtime);
+  client_logger_message ("loaded catalog data\n");
 
   // NOTE: if I let all hosts load blindly, I saturate the data clients with too many
@@ -61,4 +65,5 @@
   // generate tables go from catID,objID -> catSeq,objSeq
   indexCatalogs (catalog, Ncatalog);
+  client_logger_message ("indexed Catalogs\n");
 
   /* match measurements with images, mosaics */
@@ -75,6 +80,7 @@
   MARKTIME("-- set up mosaic indexes: %f sec\n", dtime);
 
+  client_logger_message ("done setting up indexes\n");
+
   // dumpObjects ("test.obj.dat", catalog, Ncatalog);
-
   markObjects (catalog, Ncatalog);
 
@@ -94,4 +100,5 @@
 
   /* determine fit values */
+  client_logger_message ("starting the loops : %d \n", NLOOP);
   for (i = 0; i < NLOOP; i++) {
 
@@ -101,7 +108,9 @@
     // share mean mags for objects at the boundary (number of unowned meas > 0)
     share_mean_mags (catalog, Ncatalog, regionHosts, i);
+    client_logger_message ("shared mean mag data : loop %d \n", i);
 
     // load mean mags from other region hosts
     slurp_mean_mags (catalog, Ncatalog, regionHosts, i);
+    client_logger_message ("slurped mean mag data : loop %d \n", i);
 
     // set the image (Mcal) and mosaic (Mmos) zero point offsets given the mean mags and measurements
@@ -112,6 +121,8 @@
     // share image mags for images with non-zero unowned detections
     share_image_mags (regionHosts, i);
+    client_logger_message ("shared image data : loop %d \n", i);
 
     slurp_image_mags (regionHosts, i);
+    client_logger_message ("slurped image data : loop %d \n", i);
 
     if ((i > 8) && (i % 8 == 2)) clean_measures (catalog, Ncatalog, FALSE, flatcorr); 
@@ -123,4 +134,5 @@
     MARKTIME("-- finished loop %d: %f sec\n", i, dtime);
   }
+  client_logger_message ("done with loops\n");
 
   // this is a checkpoint to make sure all hosts have finished the loop above
@@ -135,4 +147,5 @@
     free (loopsync);
   }    
+  client_logger_message ("all hosts are done the loops\n");
 
   /* set Mcal & Mmos for bad images */
@@ -144,4 +157,5 @@
 
   share_image_mags (regionHosts, -1);
+  client_logger_message ("done with parallel images\n");
 
   exit (0);
