Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h	(revision 37621)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/include/relastro.h	(revision 37622)
@@ -583,2 +583,6 @@
 int FrameCorrectionFitLocal (Catalog *catalog, int Ncatalog, AstromOffsetMap *map, Coords *coords);
 int FrameCorrectionApply (Catalog *catalog, int Ncatalog, FrameCorrectionType *frame, AstromOffsetMap *map, Coords *coords, int POLE);
+
+int dump_stardata_pts (StarData *raw, int Npts, char *filename);
+void printNcatTotal ();
+
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FitChip.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FitChip.c	(revision 37621)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FitChip.c	(revision 37622)
@@ -323,2 +323,20 @@
   return TRUE;
 }
+
+int dump_stardata_pts (StarData *raw, int Npts, char *filename) {
+
+  FILE *f = fopen (filename, "w");
+  if (!f) { 
+    fprintf (stderr, "failed to open file %s\n", filename);
+    return FALSE;
+  }
+
+  int i;
+  for (i = 0; i < Npts; i++) {
+    fprintf (f, "%4d %10.6f %10.6f  %10.6f %10.6f  %9.2f %9.2f  %9.2f %9.2f\n", 
+	     i, raw[i].R, raw[i].D, raw[i].P, raw[i].Q, raw[i].L, raw[i].M, raw[i].X, raw[i].Y);
+  }
+  fclose (f);
+
+  return TRUE;
+}
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FrameCorrection.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FrameCorrection.c	(revision 37621)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/FrameCorrection.c	(revision 37622)
@@ -1,3 +1,4 @@
 # include "relastro.h"
+int FrameCorrectionWriteImageSH (FrameCorrectionType *frame, char *filename, int raDirection);
 
 /* The sequence of calibration is something like this:
@@ -27,5 +28,6 @@
 
   // binD = (DEC + 89.0) / scale
-  ALLOCATE (frame->Nra, int, frame->Ndec);
+  ALLOCATE (frame->Nra,  int, frame->Ndec);
+  ALLOCATE (frame->dR,   double, frame->Ndec);
   ALLOCATE (frame->Roff, double *, frame->Ndec);
   ALLOCATE (frame->Doff, double *, frame->Ndec);
@@ -86,6 +88,6 @@
   FrameCorrectionType *frame = NULL;
 
-# define LOCAL_FRAME 1
-# define SH_FRAME 0
+# define LOCAL_FRAME 0
+# define SH_FRAME 1
 
   int POLE = FALSE;
@@ -144,4 +146,6 @@
 
     // XXX write out an image to represent the correction
+    FrameCorrectionWriteImageSH (frame, "map.dR.fits", TRUE);
+    FrameCorrectionWriteImageSH (frame, "map.dD.fits", FALSE);
 
     SHtermsFree (dRc);
@@ -166,4 +170,5 @@
 
   int Nicrf = ICRFmax();
+  fprintf (stderr, "start Fit %d SH l-modes for %d QSOs\n", dRc->lmax, Nicrf);
 
   int i, j;
@@ -186,4 +191,7 @@
     double dR = average->R - measure->R;
     double dD = average->D - measure->D;
+
+    if (isnan(dR)) continue;
+    if (isnan(dD)) continue;
 
     // get the value of Ylm at this coordinate
@@ -238,4 +246,58 @@
 }
 
+// write out the maps as images
+int FrameCorrectionWriteImageSH (FrameCorrectionType *frame, char *filename, int raDirection) {
+
+  Header header;
+  Matrix matrix;
+  Coords coords;
+    
+  gfits_init_header (&header);
+  header.bitpix = -32;
+  header.Naxes = 2;
+  header.Naxis[0] = 370.0 / frame->scale;
+  header.Naxis[1] = 190.0 / frame->scale;
+
+  int Nx = header.Naxis[0];
+  int Ny = header.Naxis[1];
+
+  gfits_create_header (&header);
+  gfits_create_matrix (&header, &matrix);
+
+  InitCoords (&coords, "DEC--AIT");
+  coords.cdelt1 = coords.cdelt2 = frame->scale;
+  coords.crval1 = 0.0; // SAS center
+  coords.crval2 = 0.0;
+  coords.crpix1 = 0.5*Nx; // middle of projection is middle of map
+  coords.crpix2 = 0.5*Ny; // middle of projection is middle of map
+    
+  PutCoords (&coords, &header);
+
+  float *buffer = (float *)matrix.buffer;
+
+  int ix, iy;
+  for (ix = 0; ix < Nx; ix++) {
+    for (iy = 0; iy < Ny; iy++) {
+      double R, D;
+      int status = XY_to_RD (&R, &D, ix, iy, &coords);
+      if (!status) continue;
+
+      int iD = (D + 89.0) / frame->scale;
+      int iR = R / frame->dR[iD];
+
+      float value = NAN;
+      if (raDirection) {
+	value = frame->Roff[iD][iR];
+      } else {
+	value = frame->Doff[iD][iR];
+      }
+      buffer[ix + Nx*iy] = value;
+    }
+  }
+  gfits_write_header (filename, &header);
+  gfits_write_matrix (filename, &matrix);
+  return TRUE;
+}
+
 int FrameCorrectionFitLocal (Catalog *catalog, int Ncatalog, AstromOffsetMap *map, Coords *coords) {
 
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ICRF.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ICRF.c	(revision 37621)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ICRF.c	(revision 37622)
@@ -18,5 +18,5 @@
 int ICRFsave (int cat, int ave, int meas) {
   
-  fprintf (stderr, "ICRF: %d %d %d\n", cat, ave, meas);
+  // fprintf (stderr, "ICRF: %d %d %d\n", cat, ave, meas);
 
   ICRFtoCatalog[Nicrf] = cat;
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ImageOps.c	(revision 37621)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/ImageOps.c	(revision 37622)
@@ -816,4 +816,6 @@
     // XXX subtract off dR,dD GAL here
     if (USE_GALAXY_MODEL) {
+      myAssert (!isnan(measure[0].RoffGAL), "oops");
+      myAssert (!isnan(measure[0].DoffGAL), "oops");
       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/UpdateMeasures.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateMeasures.c	(revision 37621)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateMeasures.c	(revision 37622)
@@ -1,5 +1,3 @@
 # include "relastro.h"
-
-void printNcatTotal ();
 
 // this function operates on Measure, not MeasureTiny
@@ -32,5 +30,5 @@
   }
 
-  printNcatTotal ();
+  // printNcatTotal ();
 
   return (TRUE);
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateObjectOffsets.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateObjectOffsets.c	(revision 37621)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateObjectOffsets.c	(revision 37622)
@@ -74,5 +74,5 @@
     save_catalogs (&catalog, 1);
   }
-  
+  printNcatTotal();
   return (TRUE);
 }
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateObjects.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateObjects.c	(revision 37621)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/UpdateObjects.c	(revision 37622)
@@ -132,6 +132,6 @@
       UpdateObjects_Chips(average, secfilt, measure, measureBig, Nsecfilt, &fitStatsChips, i, m, Nloop);
     }
-    if (VERBOSE) fprintf (stderr, "catalog %d : chips "OFF_T_FMT" ave, "OFF_T_FMT" pm, "OFF_T_FMT" par : Nskip "OFF_T_FMT", Noffset "OFF_T_FMT"\n",  i,  fitStatsChips.Nave,  fitStatsChips.Npm,  fitStatsChips.Npar,  fitStatsChips.Nskip, fitStatsChips.Noffset);
-    if (VERBOSE) fprintf (stderr, "catalog %d : stack "OFF_T_FMT" ave, "OFF_T_FMT" pm, "OFF_T_FMT" par : Nskip "OFF_T_FMT", Noffset "OFF_T_FMT"\n",  i,  fitStatsStack.Nave,  fitStatsStack.Npm,  fitStatsStack.Npar,  fitStatsStack.Nskip, fitStatsStack.Noffset);
+    if (VERBOSE2) fprintf (stderr, "catalog %d : chips "OFF_T_FMT" ave, "OFF_T_FMT" pm, "OFF_T_FMT" par : Nskip "OFF_T_FMT", Noffset "OFF_T_FMT"\n",  i,  fitStatsChips.Nave,  fitStatsChips.Npm,  fitStatsChips.Npar,  fitStatsChips.Nskip, fitStatsChips.Noffset);
+    if (VERBOSE2) fprintf (stderr, "catalog %d : stack "OFF_T_FMT" ave, "OFF_T_FMT" pm, "OFF_T_FMT" par : Nskip "OFF_T_FMT", Noffset "OFF_T_FMT"\n",  i,  fitStatsStack.Nave,  fitStatsStack.Npm,  fitStatsStack.Npar,  fitStatsStack.Nskip, fitStatsStack.Noffset);
     sumFitStats (&fitStatsChips, &sumStatsChips);
     sumFitStats (&fitStatsStack, &sumStatsStack);
@@ -139,6 +139,6 @@
   freeObjectData ();
 
-  if (VERBOSE) fprintf (stderr, "fitted "OFF_T_FMT" objects ("OFF_T_FMT" ave, "OFF_T_FMT" pm, "OFF_T_FMT" par), skipped "OFF_T_FMT", "OFF_T_FMT" have too large an offset\n",  (sumStatsChips.Nave + sumStatsChips.Npm + sumStatsChips.Npar),  sumStatsChips.Nave,  sumStatsChips.Npm,  sumStatsChips.Npar,  sumStatsChips.Nskip, sumStatsChips.Noffset);
-  if (VERBOSE) fprintf (stderr, "fitted "OFF_T_FMT" objects ("OFF_T_FMT" ave, "OFF_T_FMT" pm, "OFF_T_FMT" par), skipped "OFF_T_FMT", "OFF_T_FMT" have too large an offset\n",  (sumStatsStack.Nave + sumStatsStack.Npm + sumStatsStack.Npar),  sumStatsStack.Nave,  sumStatsStack.Npm,  sumStatsStack.Npar,  sumStatsStack.Nskip, sumStatsStack.Noffset);
+  if (VERBOSE && (Ncatalog > 1)) fprintf (stderr, "fitted "OFF_T_FMT" objects ("OFF_T_FMT" ave, "OFF_T_FMT" pm, "OFF_T_FMT" par), skipped "OFF_T_FMT", "OFF_T_FMT" have too large an offset\n",  (sumStatsChips.Nave + sumStatsChips.Npm + sumStatsChips.Npar),  sumStatsChips.Nave,  sumStatsChips.Npm,  sumStatsChips.Npar,  sumStatsChips.Nskip, sumStatsChips.Noffset);
+  if (VERBOSE && (Ncatalog > 1)) fprintf (stderr, "fitted "OFF_T_FMT" objects ("OFF_T_FMT" ave, "OFF_T_FMT" pm, "OFF_T_FMT" par), skipped "OFF_T_FMT", "OFF_T_FMT" have too large an offset\n",  (sumStatsStack.Nave + sumStatsStack.Npm + sumStatsStack.Npar),  sumStatsStack.Nave,  sumStatsStack.Npm,  sumStatsStack.Npar,  sumStatsStack.Nskip, sumStatsStack.Noffset);
   return (TRUE);
 }
@@ -501,4 +501,10 @@
   coords.crval1 = average[0].R;
   coords.crval2 = average[0].D;
+  if (isnan(coords.crval1)) {
+    return (FALSE);
+  }
+  if (isnan(coords.crval2)) {
+    return (FALSE);
+  }
 
   double dXoff, dYoff;
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/bcatalog.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/bcatalog.c	(revision 37621)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/bcatalog.c	(revision 37622)
@@ -247,5 +247,5 @@
   }
 
-  fprintf (stderr, "added %d ICRF QSO\n", Nicrf);
+  // fprintf (stderr, "added %d ICRF QSO\n", Nicrf);
   
   return (TRUE);
Index: /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/fitpoly.c
===================================================================
--- /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/fitpoly.c	(revision 37621)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/fitpoly.c	(revision 37622)
@@ -353,4 +353,17 @@
   /* keep the order and type from initial values */
   
+  if (isnan(coords[0].crval1)) {
+    return FALSE;
+  }
+  if (isnan(coords[0].crval2)) {
+    return FALSE;
+  }
+  if (isnan(coords[0].crpix1)) {
+    return FALSE;
+  }
+  if (isnan(coords[0].crpix2)) {
+    return FALSE;
+  }
+
   return (TRUE);
 }
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 37621)
+++ /branches/eam_branches/ipp-20140904/Ohana/src/relastro/src/relastro_images.c	(revision 37622)
@@ -74,5 +74,6 @@
 	// XXX is is really possible that I do not update the measure.R,D on each loop?? 
 	UpdateObjects (catalog, Ncatalog, i); // calculate <R>,<D>
-	if (i > 2) FrameCorrection (catalog, Ncatalog);
+	FrameCorrection (catalog, Ncatalog);
+	// if (i > 2) FrameCorrection (catalog, Ncatalog);
 	UpdateChips (catalog, Ncatalog);   // measure.X,Y -> R,D, fit image.coords
 	MARKTIME("update chips: %f sec\n", dtime);
