Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h	(revision 37654)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h	(revision 37655)
@@ -200,4 +200,7 @@
 int    USE_GALAXY_MODEL;
 int    USE_ICRF_CORRECT;
+
+int    USE_ICRF_LOCAL;
+int    USE_ICRF_SHFIT;
 
 int    RESET;
@@ -285,5 +288,5 @@
 int           args                PROTO((int argc, char **argv));
 int           args_client         PROTO((int argc, char **argv));
-int           bcatalog            PROTO((Catalog *subcatalog, Catalog *catalog, int Nsubcatalog));
+int           bcatalog            PROTO((Catalog *subcatalog, Catalog *catalog));
 void          clean_images        PROTO((void));
 void          clean_measures      PROTO((Catalog *catalog, int Ncatalog, int final));
@@ -573,4 +576,5 @@
 int ICRFdata (int n, int *cat, int *ave, int *meas);
 int ICRFmax ();
+int select_catalog_ICRF (Catalog *catalog, int Ncatalog);
 
 FrameCorrectionType *FrameCorrectionInit (double scale);
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/BrightCatalog.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/BrightCatalog.c	(revision 37654)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/BrightCatalog.c	(revision 37655)
@@ -70,4 +70,6 @@
     GET_COLUMN(R,         "RA",   	double);
     GET_COLUMN(D,         "DEC",  	double);
+    GET_COLUMN(RoffGAL,   "ROFF_GAL",  	float);
+    GET_COLUMN(DoffGAL,   "DOFF_GAL",  	float);
     GET_COLUMN(M,         "MAG_SYS",  	float);
     GET_COLUMN(Mcal,      "MAG_CAL",  	float);
@@ -94,4 +96,6 @@
       measure[i].R         = R[i];
       measure[i].D         = D[i];
+      measure[i].RoffGAL   = RoffGAL[i];
+      measure[i].DoffGAL   = DoffGAL[i];
       measure[i].M         = M[i];
       measure[i].Mcal      = Mcal[i];
@@ -115,4 +119,6 @@
     free (R       );
     free (D       );
+    free (RoffGAL );
+    free (DoffGAL );
     free (M       );
     free (Mcal    );
@@ -343,6 +349,8 @@
     gfits_create_table_header (&theader, "BINTABLE", "MEASURE_TINY");
 
-    gfits_define_bintable_column (&theader, "E", "RA_OFF",   "ra offset",                  "arcsec", 1.0, 0.0);
-    gfits_define_bintable_column (&theader, "E", "DEC_OFF",  "dec offset",                 "arcsec", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "D", "RA",       "ra",                         "degrees", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "D", "DEC",      "dec",                        "degrees", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "ROFF_GAL", "ra offset",                  "arcsec", 1.0, 0.0);
+    gfits_define_bintable_column (&theader, "E", "DOFF_GAL", "dec offset",                 "arcsec", 1.0, 0.0);
     gfits_define_bintable_column (&theader, "E", "MAG_SYS",  "magnitude (sys)",             NULL,    1.0, 0.0);
     gfits_define_bintable_column (&theader, "E", "MAG_CAL",  "magnitude (cal)",             NULL,    1.0, 0.0);
@@ -368,4 +376,6 @@
     double *R         ; ALLOCATE (R        ,  double, catalog->Nmeasure);
     double *D         ; ALLOCATE (D        ,  double, catalog->Nmeasure);
+    float  *RoffGAL   ; ALLOCATE (RoffGAL  ,  float,  catalog->Nmeasure);
+    float  *DoffGAL   ; ALLOCATE (DoffGAL  ,  float,  catalog->Nmeasure);
     float  *M         ; ALLOCATE (M        ,  float,  catalog->Nmeasure);
     float  *Mcal      ; ALLOCATE (Mcal     ,  float,  catalog->Nmeasure);
@@ -390,4 +400,6 @@
       R[i]        = measure[i].R        ;
       D[i]        = measure[i].D        ;
+      RoffGAL[i]  = measure[i].RoffGAL  ;
+      DoffGAL[i]  = measure[i].DoffGAL  ;
       M[i]  	  = measure[i].M        ;
       Mcal[i]     = measure[i].Mcal     ;
@@ -411,4 +423,6 @@
     gfits_set_bintable_column (&theader, &ftable, "RA",   	R,         catalog->Nmeasure);
     gfits_set_bintable_column (&theader, &ftable, "DEC",  	D,         catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "ROFF_GAL",   RoffGAL,   catalog->Nmeasure);
+    gfits_set_bintable_column (&theader, &ftable, "DOFF_GAL",   DoffGAL,   catalog->Nmeasure);
     gfits_set_bintable_column (&theader, &ftable, "MAG_SYS",  	M,         catalog->Nmeasure);
     gfits_set_bintable_column (&theader, &ftable, "MAG_CAL",  	Mcal,      catalog->Nmeasure);
@@ -430,4 +444,6 @@
     free (R       );
     free (D       );
+    free (RoffGAL );
+    free (DoffGAL );
     free (M       );
     free (Mcal    );
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ConfigInit.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ConfigInit.c	(revision 37654)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ConfigInit.c	(revision 37655)
@@ -39,4 +39,6 @@
   if (!ScanConfig (config, "USE_GALAXY_MODEL",    "%d", 0, &USE_GALAXY_MODEL))     USE_GALAXY_MODEL = FALSE;
   if (!ScanConfig (config, "USE_ICRF_CORRECT",    "%d", 0, &USE_ICRF_CORRECT))     USE_ICRF_CORRECT = FALSE;
+  if (!ScanConfig (config, "USE_ICRF_LOCAL",      "%d", 0, &USE_ICRF_LOCAL))       USE_ICRF_LOCAL = FALSE;
+  if (!ScanConfig (config, "USE_ICRF_SHFIT",      "%d", 0, &USE_ICRF_SHFIT))       USE_ICRF_SHFIT = FALSE;
 
   // force CATDIR to be absolute (so parallel mode will work)
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FrameCorrection.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FrameCorrection.c	(revision 37654)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FrameCorrection.c	(revision 37655)
@@ -98,10 +98,7 @@
   FrameCorrectionType *frame = NULL;
 
-# define LOCAL_FRAME 0
-# define SH_FRAME 1
-
   int POLE = FALSE;
 
-  if (LOCAL_FRAME) {
+  if (USE_ICRF_LOCAL) {
     // for the local frame correction, we are going to convert R,D into 
     // X,Y linear coordinates in an 'image' centered on the field center. 
@@ -140,5 +137,5 @@
   } 
 
-  if (SH_FRAME) {
+  if (USE_ICRF_SHFIT) {
     // Lmax in recipe
     int Lmax = 20;
@@ -611,5 +608,5 @@
       if (dP > dPmax) mask[i] = 1;
       if (isnan(dP)) mask[i] = 1;
-      if (mask[i]) Nkeep++;
+      if (!mask[i]) Nkeep++;
     }
     fprintf (stderr, "keeping %d of %d points\n", Nkeep, Npts);
@@ -703,7 +700,9 @@
 	double dX = AstromOffsetMapValue (map, Xave, Yave, TRUE);
 	double dY = AstromOffsetMapValue (map, Xave, Yave, FALSE);
-	double Xmeas = Xave - dX;
-	double Ymeas = Yave - dY;
-	XY_to_RD (&average->R, &average->D, Xmeas, Ymeas, coords);
+	if (!isnan(dX) && !isnan(dY)) {
+	  double Xmeas = Xave - dX;
+	  double Ymeas = Yave - dY;
+	  XY_to_RD (&average->R, &average->D, Xmeas, Ymeas, coords);
+	}
       } else {
 	myAssert (frame, "no frame correction defined");
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ICRF.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ICRF.c	(revision 37654)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ICRF.c	(revision 37655)
@@ -48,2 +48,45 @@
   return TRUE;
 }
+
+int select_catalog_ICRF (Catalog *catalog, int Ncatalog) {
+
+  if (!USE_ICRF_CORRECT) return TRUE;
+
+  int N = 0;
+
+  char filename[1024];
+  snprintf (filename, 1024, "%s/test.icrf.dat", CATDIR);
+  FILE *f = fopen (filename, "w");
+
+  int c, i, j;
+  for (c = 0; c < Ncatalog; c++) {
+
+    for (i = 0; i < catalog[c].Naverage; i++) {
+      if (!(catalog[c].average[i].flags & ID_ICRF_QSO)) continue;
+
+      // only save a single value
+      int savedICRF = FALSE;
+      for (j = 0; j < catalog[c].average[i].Nmeasure; j++) {
+	  
+	int offset = catalog[c].average[i].measureOffset + j;
+	  
+	if (!(catalog[c].measureT[offset].dbFlags & ID_MEAS_ICRF_QSO)) continue;
+
+	fprintf (f, "%d %d %d : %f %f : %f %f\n", c, i, offset,
+		 catalog[c].average[i].R, catalog[c].average[i].D,
+		 catalog[c].measureT[offset].R, catalog[c].measureT[offset].D);
+
+	if (savedICRF) continue;
+
+	ICRFsave (c, i, offset);
+	N ++;
+	savedICRF = TRUE;
+      }
+    }
+  }
+  fclose (f);
+
+  fprintf (stderr, "added %d ICRF QSO\n", N);
+
+  return TRUE;
+}
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/bcatalog.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/bcatalog.c	(revision 37654)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/bcatalog.c	(revision 37655)
@@ -19,5 +19,5 @@
 int LimitDensityCatalog_ByNmeasureGrid (Catalog *subcatalog, Catalog *oldcatalog);
 
-int bcatalog (Catalog *subcatalog, Catalog *catalog, int Nsubcatalog) {
+int bcatalog (Catalog *subcatalog, Catalog *catalog) {
 
   off_t i, j, offset;
@@ -61,10 +61,4 @@
   }
 
-  int Nicrf = 0;
-
-  char filename[1024];
-  snprintf (filename, 1024, "%s/test.icrf.dat", CATDIR);
-  FILE *f = fopen (filename, "a");
-
   /* exclude stars not in range or with too few measurements */
   for (i = 0; i < catalog[0].Naverage; i++) {
@@ -84,8 +78,7 @@
     }
 
-    // if we want to do static correction to ICRF, keep the ICRF QSOs 
+    // if we want to do static correction to ICRF, keep the ICRF QSOs (regardless of other data quality)
     if (USE_ICRF_CORRECT && (subcatalog[0].average[Naverage].flags & ID_ICRF_QSO)) {
       Nm = 0;
-      int savedICRF = FALSE;
       for (j = 0; j < catalog[0].average[i].Nmeasure; j++) {
 	  
@@ -101,16 +94,4 @@
 	}
 	  
-	// we need to save the location of the ICRF QSOs in the database
-	if (subcatalog[0].measureT[Nmeasure].dbFlags & ID_MEAS_ICRF_QSO) {
-	  if (!savedICRF) {
-	    ICRFsave (Nsubcatalog, Naverage, Nmeasure);
-	    fprintf (f, "%d %d %d : %f %f : %f %f\n", (int) Nsubcatalog, (int) Naverage, (int) Nmeasure,
-		     catalog[0].average[i].R, catalog[0].average[i].D,
-		     catalog[0].measure[offset].R, catalog[0].measure[offset].D);
-	    Nicrf ++;
-	    savedICRF = TRUE;
-	  }
-	}
-
 	Nmeasure ++;
 	Nm ++;
@@ -236,6 +217,4 @@
   assert (Nsecfilt == catalog[0].Nsecfilt);
 
-  fclose (f);
-
 // limit the total number of stars in the catalog
   if (MaxDensityUse) {
@@ -253,6 +232,4 @@
   }
 
-  // fprintf (stderr, "added %d ICRF QSO\n", Nicrf);
-  
   return (TRUE);
 }
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/load_catalogs.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/load_catalogs.c	(revision 37654)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/load_catalogs.c	(revision 37655)
@@ -19,10 +19,4 @@
 
   ALLOCATE (catalog, Catalog, skylist[0].Nregions);
-
-  if (subselect && USE_GALAXY_MODEL) {
-    char filename[1024];
-    snprintf (filename, 1024, "%s/test.icrf.dat", CATDIR);
-    unlink (filename);
-  }
 
   // load data from each region file, only use bright stars
@@ -74,5 +68,5 @@
       // results are in Average, Secfilt, and MeasureTiny
       // Ncat tracks the actually used catalogs
-      bcatalog (&catalog[Ncat], &tcatalog, Ncat);
+      bcatalog (&catalog[Ncat], &tcatalog);
       dvo_catalog_unlock (&tcatalog);
       dvo_catalog_free (&tcatalog);
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_images.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_images.c	(revision 37654)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_images.c	(revision 37655)
@@ -33,4 +33,7 @@
   catalog = load_catalogs (skylist, &Ncatalog, TRUE, 0, NULL, NULL);
   MARKTIME("load catalog data: %f sec\n", dtime);
+
+  // find ICRF QSOs for reference downstream (only if USE_ICRF_CORRECT)
+  select_catalog_ICRF (catalog, Ncatalog);
 
   if (photcodesReset) {
@@ -72,5 +75,4 @@
     case TARGET_CHIPS:
       for (i = 0; i < NLOOP; i++) {
-	// XXX is is really possible that I do not update the measure.R,D on each loop?? 
 	UpdateObjects (catalog, Ncatalog, (i > 0)); // calculate <R>,<D>; if (i > 0), apply Galaxy Motion Model (if desired)
 	if ((i > 1) || !USE_GALAXY_MODEL) {
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_parallel_images.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_parallel_images.c	(revision 37654)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_parallel_images.c	(revision 37655)
@@ -87,6 +87,7 @@
     case TARGET_CHIPS:
       for (i = 0; i < NLOOP; i++) {
-	UpdateObjects (catalog, Ncatalog, i);
+	UpdateObjects (catalog, Ncatalog, (i > 0));
 	share_mean_pos (catalog, Ncatalog, regionHosts, i);
+	// XXX FrameCorrection (catalog, Ncatalog); -- this needs to happen on the master machine?
 	slurp_mean_pos (catalog, Ncatalog, regionHosts, i);
 	UpdateChips (catalog, Ncatalog);
