Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h	(revision 37603)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h	(revision 37604)
@@ -284,5 +284,5 @@
 int           args                PROTO((int argc, char **argv));
 int           args_client         PROTO((int argc, char **argv));
-int           bcatalog            PROTO((Catalog *subcatalog, Catalog *catalog));
+int           bcatalog            PROTO((Catalog *subcatalog, Catalog *catalog, int Nsubcatalog));
 void          clean_images        PROTO((void));
 void          clean_measures      PROTO((Catalog *catalog, int Ncatalog, int final));
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FitChip.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FitChip.c	(revision 37603)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FitChip.c	(revision 37604)
@@ -72,5 +72,7 @@
       return FALSE;
     }
-    // fprintf (stderr, "using %d for %s\n", order_use, image[0].name);
+    // XXX WATCH OUT HERE!!
+    if (!CHIPMAP) order_use = MIN(order_use, 1);
+    fprintf (stderr, "using %d for %s\n", order_use, image[0].name);
 
     // when fitting the map, first fit a linear model (below? change Npolyterms to -1)
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FrameCorrection.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FrameCorrection.c	(revision 37603)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FrameCorrection.c	(revision 37604)
@@ -180,5 +180,5 @@
 
     Average *average = &catalog[cat].average[ave];
-    Measure *measure = &catalog[cat].measure[meas]; // MeasureTiny?
+    MeasureTiny *measure = &catalog[cat].measureT[meas]; // MeasureTiny?
 
     // record these in arcsec or degree?
@@ -240,25 +240,44 @@
 int FrameCorrectionFitLocal (Catalog *catalog, int Ncatalog, AstromOffsetMap *map, Coords *coords) {
 
+  int i;
   double Xave, Yave, Xmeas, Ymeas;
   float *X, *Y, *dX, *dY;
 
+  int Nicrf = ICRFmax();
+  if (!Nicrf) {
+    fprintf (stderr, "no matched ICRF quasars for reference correction, skipping\n");
+    return FALSE;
+  }
+
+  double *Rave, *Dave, *Rmeas, *Dmeas;
+  float *dXf, *dYf, *dPos;
+
   int Npts = 0;
-  int NPTS = 100;
-
-  ALLOCATE (X, float, NPTS);
-  ALLOCATE (Y, float, NPTS);
-  ALLOCATE (dX, float, NPTS);
-  ALLOCATE (dY, float, NPTS);
-
-  int Nicrf = ICRFmax();
-
-  int i;
+  ALLOCATE (Rave, double, Nicrf);
+  ALLOCATE (Dave, double, Nicrf);
+  ALLOCATE (Rmeas, double, Nicrf);
+  ALLOCATE (Dmeas, double, Nicrf);
+
+  ALLOCATE (X,  float, Nicrf);
+  ALLOCATE (Y,  float, Nicrf);
+  ALLOCATE (dX, float, Nicrf);
+  ALLOCATE (dY, float, Nicrf);
+  ALLOCATE (dXf, float, Nicrf);
+  ALLOCATE (dYf, float, Nicrf);
+
+  ALLOCATE (dPos, float, Nicrf);
+
+  int *mask = NULL;
+  ALLOCATE (mask, int, Nicrf);
+  memset (mask, 0, Nicrf*sizeof(int));
+
+  // select the ICRF QSOS and save the necessary data
   for (i = 0; i < Nicrf; i++) {
 
-    int cat, meas, ave;
-    ICRFdata (i, &cat, &meas, &ave);
+    int cat, ave, meas;
+    ICRFdata (i, &cat, &ave, &meas);
 
     Average *average = &catalog[cat].average[ave];
-    Measure *measure = &catalog[cat].measure[meas]; // MeasureTiny?
+    MeasureTiny *measure = &catalog[cat].measureT[meas]; // MeasureTiny?
 
     // this local correction is defined for (Rmin < R < Rmax, Dmin < D < Dmax)
@@ -278,16 +297,111 @@
     dX[Npts] = Xave - Xmeas;
     dY[Npts] = Yave - Ymeas;
+    Rave[Npts] = average->R;
+    Dave[Npts] = average->D;
+    Rmeas[Npts] = measure->R;
+    Dmeas[Npts] = measure->D;
     Npts ++;
-    if (Npts == NPTS) {
-      NPTS += 100;
-      REALLOCATE (X, float, NPTS);
-      REALLOCATE (Y, float, NPTS);
-      REALLOCATE (dX, float, NPTS);
-      REALLOCATE (dY, float, NPTS);
-    }
-  }    
-
-  AstromOffsetMapFit (map, X, Y, dX, Npts, TRUE);
-  AstromOffsetMapFit (map, X, Y, dY, Npts, FALSE);
+  }
+
+  float *Xfit, *Yfit, *dXfit, *dYfit;
+  ALLOCATE (Xfit, float, Npts);
+  ALLOCATE (Yfit, float, Npts);
+  ALLOCATE (dXfit, float, Npts);
+  ALLOCATE (dYfit, float, Npts);
+
+  int Niter;
+  for (Niter = 0; Niter < 3; Niter ++) {
+    int Nkeep = 0;
+    for (i = 0; i < Npts; i++) {
+      if (mask[i]) continue;
+      
+      Xfit[Nkeep] = X[i];
+      Yfit[Nkeep] = Y[i];
+      dXfit[Nkeep] = dX[i];
+      dYfit[Nkeep] = dY[i];
+      Nkeep ++;
+    }
+
+    AstromOffsetMapFit (map, Xfit, Yfit, dXfit, Nkeep, TRUE);
+    AstromOffsetMapFit (map, Xfit, Yfit, dYfit, Nkeep, FALSE);
+
+    for (i = 0; i < Npts; i++) {
+      dXf[i] = AstromOffsetMapValue (map, X[i], Y[i], TRUE);
+      dYf[i] = AstromOffsetMapValue (map, X[i], Y[i], FALSE);
+    }
+
+    // generate a histogram of the dPos = sqrt((dXf - dX)^2 + (dYf - dY)^2) values
+    Nkeep = 0;
+    for (i = 0; i < Npts; i++) {
+      if (mask[i]) continue;
+      float dP = hypot((dXf[i] - dX[i]), (dYf[i] - dY[i]));
+      if (isnan(dP)) continue; // not all points are in the map
+      dPos[Nkeep] = dP;
+      Nkeep ++;
+    }
+    fsort (dPos, Nkeep);
+    
+    float dPmax = 1.5*dPos[(int)(0.95*Nkeep)];
+
+    // mask points based on dPmax
+    Nkeep = 0;
+    for (i = 0; i < Npts; i++) {
+      float dP = hypot((dXf[i] - dX[i]), (dYf[i] - dY[i]));
+      if (dP > dPmax) mask[i] = 1;
+      if (isnan(dP)) mask[i] = 1;
+      if (mask[i]) Nkeep++;
+    }
+    fprintf (stderr, "keeping %d of %d points\n", Nkeep, Npts);
+  }
+  free (Xfit);
+  free (Yfit);
+  free (dXfit);
+  free (dYfit);
+
+  // *** save the icrf points and fit residuals
+  { 
+    FILE *f = fopen ("map.dat", "w");
+
+    for (i = 0; i < Npts; i++) {
+      fprintf (f, "%3d %12.8f %12.8f  %12.8f %12.8f  %f %f : %f %f : %f %f : %d\n", i, Rave[i], Dave[i], Rmeas[i], Dmeas[i], X[i], Y[i], dX[i], dY[i], dXf[i], dYf[i], mask[i]);
+    }    
+    fclose (f);
+  }
+
+  // write out the maps as images
+  {
+    Header header;
+    Matrix matrix;
+    
+    gfits_init_header (&header);
+    header.bitpix = -32;
+    header.Naxes = 2;
+    header.Naxis[0] = map->Nx;
+    header.Naxis[1] = map->Ny;
+
+    gfits_create_header (&header);
+    gfits_create_matrix (&header, &matrix);
+    PutCoords (coords, &header);
+
+    float *buffer = (float *)matrix.buffer;
+
+    int ix, iy;
+    for (ix = 0; ix < map->Nx; ix++) {
+      for (iy = 0; iy < map->Ny; iy++) {
+	buffer[ix + map->Nx*iy] = map->dXv[ix][iy];
+      }
+    }
+    
+    gfits_write_header ("map.dX.fits", &header);
+    gfits_write_matrix ("map.dX.fits", &matrix);
+
+    for (ix = 0; ix < map->Nx; ix++) {
+      for (iy = 0; iy < map->Ny; iy++) {
+	buffer[ix + map->Nx*iy] = map->dYv[ix][iy];
+      }
+    }
+    gfits_write_header ("map.dY.fits", &header);
+    gfits_write_matrix ("map.dY.fits", &matrix);
+  }
 
   return TRUE;
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ICRF.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ICRF.c	(revision 37603)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ICRF.c	(revision 37604)
@@ -18,4 +18,6 @@
 int ICRFsave (int cat, int ave, int meas) {
   
+  fprintf (stderr, "ICRF: %d %d %d\n", cat, ave, meas);
+
   ICRFtoCatalog[Nicrf] = cat;
   ICRFtoAverage[Nicrf] = ave;
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ImageOps.c	(revision 37603)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ImageOps.c	(revision 37604)
@@ -810,6 +810,6 @@
     // XXX subtract off dR,dD GAL here
     if (USE_GALAXY_MODEL) {
-      ref[i].R += measure[0].RoffGAL;
-      ref[i].D += measure[0].DoffGAL;
+      ref[i].R += measure[0].RoffGAL / 3600.0;
+      ref[i].D += measure[0].DoffGAL / 3600.0;
     }
 
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateObjects.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateObjects.c	(revision 37603)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateObjects.c	(revision 37604)
@@ -210,6 +210,6 @@
 
     if (USE_GALAXY_MODEL) {
-      Ri -= measure[0].RoffGAL;
-      Di -= measure[0].DoffGAL;
+      Ri -= measure[k].RoffGAL / 3600.0;
+      Di -= measure[k].DoffGAL / 3600.0;
     }
 
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/bcatalog.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/bcatalog.c	(revision 37603)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/bcatalog.c	(revision 37604)
@@ -19,5 +19,5 @@
 int LimitDensityCatalog_ByNmeasureGrid (Catalog *subcatalog, Catalog *oldcatalog);
 
-int bcatalog (Catalog *subcatalog, Catalog *catalog) {
+int bcatalog (Catalog *subcatalog, Catalog *catalog, int Nsubcatalog) {
 
   off_t i, j, offset;
@@ -62,4 +62,6 @@
 
   int Nicrf = 0;
+
+  FILE *f = fopen ("test.icrf.dat", "a");
 
   /* exclude stars not in range or with too few measurements */
@@ -98,5 +100,8 @@
 	// we need to save the location of the ICRF QSOs in the database
 	if (subcatalog[0].measureT[Nmeasure].dbFlags & ID_MEAS_ICRF_QSO) {
-	  ICRFsave (i, Naverage, Nmeasure);
+	  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 ++;
 	}
@@ -224,4 +229,6 @@
   subcatalog[0].catID    = catalog[0].catID;
   assert (Nsecfilt == catalog[0].Nsecfilt);
+
+  fclose (f);
 
 // limit the total number of stars in the catalog
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/initialize.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/initialize.c	(revision 37603)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/initialize.c	(revision 37604)
@@ -63,4 +63,6 @@
   initstats (STATMODE);
 
+  if (USE_ICRF_CORRECT) ICRFinit();
+
   /* XXX drop irrelevant entries */
   if (SHOW_PARAMS) {
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 37603)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/load_catalogs.c	(revision 37604)
@@ -68,5 +68,5 @@
       // results are in Average, Secfilt, and MeasureTiny
       // Ncat tracks the actually used catalogs
-      bcatalog (&catalog[Ncat], &tcatalog);
+      bcatalog (&catalog[Ncat], &tcatalog, Ncat);
       dvo_catalog_unlock (&tcatalog);
       dvo_catalog_free (&tcatalog);
