Index: /branches/eam_branches/relastro.20100326/Makefile
===================================================================
--- /branches/eam_branches/relastro.20100326/Makefile	(revision 27488)
+++ /branches/eam_branches/relastro.20100326/Makefile	(revision 27489)
@@ -56,4 +56,6 @@
 $(SRC)/save_catalogs.$(ARCH).o       \
 $(SRC)/write_coords.$(ARCH).o        \
+$(SRC)/CoordOps.$(ARCH).o        \
+$(SRC)/FixProblemImages.$(ARCH).o        \
 $(SRC)/relastroVisual.$(ARCH).o
 
Index: /branches/eam_branches/relastro.20100326/include/relastro.h
===================================================================
--- /branches/eam_branches/relastro.20100326/include/relastro.h	(revision 27488)
+++ /branches/eam_branches/relastro.20100326/include/relastro.h	(revision 27489)
@@ -151,5 +151,5 @@
 void          dump_grid           PROTO((void));
 int           edge_check          PROTO((double *x1, double *y1, double *x2, double *y2));
-void          findImages          PROTO((Catalog *catalog, int Ncatalog));
+void          findImages          PROTO((Catalog *catalog, int Ncatalog, int MATCHCAT));
 int           findMosaics         PROTO((Catalog *catalog, int Ncatalog));
 Image        *find_images         PROTO((FITS_DB *db, GSCRegion *region, off_t Nregion, off_t *Nimage, off_t **LineNum));
@@ -166,5 +166,5 @@
 void          free_catalogs       PROTO((Catalog *catalog, int Ncatalog));
 int           gcatalog            PROTO((Catalog *catalog, int FINAL));
-Coords       *getCoords           PROTO((off_t meas, int cat));
+// Coords       *getCoords           PROTO((off_t meas, int cat));
 float         getMcal             PROTO((off_t meas, int cat));
 float         getMgrid            PROTO((off_t meas, int cat));
@@ -178,5 +178,5 @@
 void          initGrid            PROTO((int dX, int dY));
 void          initGridBins        PROTO((Catalog *catalog, int Ncatalog));
-void          initImageBins       PROTO((Catalog *catalog, int Ncatalog));
+void          initImageBins       PROTO((Catalog *catalog, int Ncatalog, int FULLINIT));
 void          initImages          PROTO((Image *input, off_t N));
 void          initMosaicBins      PROTO((Catalog *catalog, int Ncatalog));
@@ -199,5 +199,5 @@
 int           main                PROTO((int argc, char **argv));
 void          mark_images         PROTO((Image *image, off_t Nimage, Image *timage, off_t Ntimage));
-void          matchImage          PROTO((Catalog *catalog, off_t meas, int cat));
+void          matchImage          PROTO((Catalog *catalog, off_t meas, int cat, int MATCHCAT));
 void          matchMosaics        PROTO((Catalog *catalog, off_t meas, int cat));
 GSCRegion    *name_region         PROTO((char *name, off_t *Nregions));
@@ -250,5 +250,5 @@
 void fit_free (CoordFit *fit);
 void fit_add (CoordFit *fit, double x1, double y1, double x2, double y2, double wt);
-void fit_eval (CoordFit *fit);
+int fit_eval (CoordFit *fit);
 void fit_apply (CoordFit *fit, double *x2, double *y2, double x1, double y1);
 double **poly2d_dx (double **poly, int Nx, int Ny);
@@ -256,8 +256,8 @@
 double **poly2d_copy (double **poly, int Nx, int Ny);
 double poly2d_eval (double **poly, int Nx, int Ny, double x, double y);
-CoordFit *fit_apply_coords (CoordFit *fit, Coords *coords);
+int fit_apply_coords (CoordFit *fit, Coords *coords);
 int CoordsGetCenter (CoordFit *fit, double tol, double *xo, double *yo);
 CoordFit *CoordsSetCenter (CoordFit *input, double Xo, double Yo);
-void FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
+int FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
 void FitMosaic (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
 void FitSimple (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
@@ -303,2 +303,15 @@
 			       StatType statsR, StatType statsD, double thresh);
 
+
+
+int FixProblemImages (SkyList *skylist);
+int *getCatlist (int *N, off_t im);
+
+void initCoords (void);
+void getOffsets (double *dPos, off_t *nPos, off_t N);
+void saveOffsets (double dPos, off_t nPos, off_t N);
+void setBadCoords (off_t N);
+int badCoords (off_t N);
+Coords *getCoords (off_t N);
+int saveCoords (Coords *coords, off_t N);
+void resetImageRaw (Catalog *catalog, int Ncatalog, off_t im);
Index: /branches/eam_branches/relastro.20100326/src/CoordOps.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/CoordOps.c	(revision 27488)
+++ /branches/eam_branches/relastro.20100326/src/CoordOps.c	(revision 27489)
@@ -10,5 +10,8 @@
 void initCoords (void) {
 
-  images = getImages (&N);
+  off_t N;
+  Image *images;
+
+  images = getimages (&N);
 
   NoldCoords = N;
@@ -32,5 +35,5 @@
 }
 
-Coords getCoords (off_t N) {
+Coords *getCoords (off_t N) {
 
   if (N < 0) return NULL;
@@ -68,2 +71,13 @@
 }
   
+void getOffsets (double *dPos, off_t *nPos, off_t N) {
+
+  if (N < 0) return;
+  if (N >= NoldCoords) return;
+
+  *dPos = dPosSum[N];
+  *nPos = nPosSum[N];
+  
+  return;
+}
+  
Index: /branches/eam_branches/relastro.20100326/src/FitChip.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/FitChip.c	(revision 27488)
+++ /branches/eam_branches/relastro.20100326/src/FitChip.c	(revision 27489)
@@ -17,10 +17,9 @@
 // XXX save measurements of the fit quality (scatter, chisq) in the image table
 
-void FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords) {
+int FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords) {
 
   int i, Nscatter, Niter, skip;
   CoordFit *fit;
   double dL, dM, dR, dRmax, *values;
-  Coords oldCoords;
 
   ALLOCATE (values, double, Nmatch);
@@ -91,5 +90,5 @@
       fit_free (fit);
       free (values);
-      return;
+      return FALSE;
     }
 
Index: /branches/eam_branches/relastro.20100326/src/FixProblemImages.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/FixProblemImages.c	(revision 27488)
+++ /branches/eam_branches/relastro.20100326/src/FixProblemImages.c	(revision 27489)
@@ -4,55 +4,64 @@
 // original coordinates and recalculate the positions
 
-int FixProblemImages () {
+int FixProblemImages (SkyList *skylist) {
 
-  off_t im, Nimage;
+  off_t i, Nimage;
   Image *image;
+  SkyList sublist;
+
+  // allocate so we can reallocate below
+  ALLOCATE (sublist.regions, SkyRegion *, 1);
+  ALLOCATE (sublist.filename, char *, 1);
 
   image = getimages (&Nimage);
 
+  // first check on the dPos reported for each image
   for (i = 0; i < Nimage; i++) {
-    
+    double dPosSum, dPos;
+    off_t nPos;
+    getOffsets (&dPosSum, &nPos, i);
+    dPos = sqrt(dPosSum / nPos);
+    if (dPos > 4.0) {
+      setBadCoords (i);
+    }
+  }
+
+  for (i = 0; i < Nimage; i++) {
+    int j, cat, Ncat, *catlist, Ncatlist;
+    Catalog *catalog;
+
     // check if this image should be fixed
     if (!badCoords(i)) continue;
 
-    // get list of catalogs containing measurements for the bad images:
-    catlist = getCatList(im, &Ncat);
-    measlist = getMeasList(im, &Nmeas);
+    // I need a list of the catalogs for this image
+    catlist = getCatlist(&Ncatlist, i);
 
-    for (i = 0; i < Nlist[im]; i++) {
-      m = mlist[im][i];
-      c = clist[im][i];
-      
+    // allocate Ncatlist skylist regions
+    REALLOCATE (sublist.regions, SkyRegion *, Ncatlist);
+    REALLOCATE (sublist.filename, char *, Ncatlist);
+    sublist.Nregions = Ncatlist;
+    sublist.ownElements = FALSE; // this list is only holding a view to the elements
 
-
-    // set up the basic catalog info
-    catalog.filename  = skylist[0].filename[i];
-    catalog.catformat = dvo_catalog_catformat (CATFORMAT);    // set the default catformat from config data
-    catalog.catmode   = dvo_catalog_catmode (CATMODE);        // set the default catmode from config data
-    catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
-    catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
-
-    if (!dvo_catalog_open (&catalog, skylist[0].regions[i], VERBOSE, "w")) {
-      fprintf (stderr, "ERROR: failure reading catalog %s\n", catalog.filename);
-      exit (1);
-    }
-    if (!catalog.Naves_disk) {
-      if (VERBOSE) fprintf (stderr, "no data in %s, skipping\n", catalog.filename);
-      dvo_catalog_unlock (&catalog);
-      dvo_catalog_free (&catalog);
-      continue;
+    // copy the desired catalogs from skylist to skylistSubset
+    for (j = 0; j < Ncatlist; j++) {
+      cat = catlist[j];
+      sublist.filename[j] = skylist[0].filename[cat];
+      sublist.regions[j] = skylist[0].regions[cat];
     }
 
+    catalog = load_catalogs (&sublist, &Ncat, FALSE);
+    assert (Ncat == Ncatlist);
+
     // match measurements with images
-    initImageBins (&catalog, 1);
-    findImages (&catalog, 1);
+    initImageBins (catalog, Ncat, FALSE);
+    findImages (catalog, Ncat, FALSE);
 
     // update the detection coordinates using the new image parameters
-    UpdateMeasures (&catalog, 1);
+    resetImageRaw (catalog, Ncat, i);
 
-    freeImageBins (1);
+    freeImageBins (Ncat);
 
     // write the updated detections to disk
-    save_catalogs (&catalog, 1);
+    save_catalogs (catalog, Ncat);
   }
   
Index: /branches/eam_branches/relastro.20100326/src/ImageOps.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/ImageOps.c	(revision 27488)
+++ /branches/eam_branches/relastro.20100326/src/ImageOps.c	(revision 27489)
@@ -11,4 +11,8 @@
 static Image        *image;   // list of available images
 static off_t        Nimage;   // number of available images
+
+static int         *Ncatlist;  // catalogs associated with each image
+static int         *NCATLIST;  // catalogs associated with each image
+static int         **catlist;  // catalogs associated with each image
 
 // if we search by image ID, we sort (imageIDs, imageIdx) by imageIDs to get a sorted
@@ -32,4 +36,10 @@
 }
 
+int *getCatlist (int *N, off_t im) {
+
+  *N = Ncatlist[im];
+  return (catlist[im]);
+}
+
 void initImages (Image *input, off_t N) {
 
@@ -86,5 +96,6 @@
 }
 
-void initImageBins (Catalog *catalog, int Ncatalog) {
+// these are really image & catalog indexes
+void initImageBins (Catalog *catalog, int Ncatalog, int FULLINIT) {
 
   off_t i, j;
@@ -102,8 +113,20 @@
 
   for (i = 0; i < Nimage; i++) {
-    Nlist[i] = 0;
+    Nlist[i] =   0;
     NLIST[i] = 100;
     ALLOCATE (clist[i], int, NLIST[i]);
     ALLOCATE (mlist[i], off_t, NLIST[i]);
+  }
+
+  if (FULLINIT) {
+    ALLOCATE (Ncatlist, int,  Nimage);
+    ALLOCATE (NCATLIST, int,  Nimage);
+    ALLOCATE (catlist, int *, Nimage);
+
+    for (i = 0; i < Nimage; i++) {
+      Ncatlist[i] =  0;
+      NCATLIST[i] = 32;
+      ALLOCATE (catlist[i], int, NCATLIST[i]);
+    }
   }
 }
@@ -126,5 +149,5 @@
 
 /* match measurements to images */
-void findImages (Catalog *catalog, int Ncatalog) {
+void findImages (Catalog *catalog, int Ncatalog, int MATCHCAT) {
 
   off_t i, j;
@@ -136,5 +159,5 @@
       // ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[j].photcode);
       // if (photcode[0].code != ecode) continue;
-      matchImage (catalog, j, i);
+      matchImage (catalog, j, i, MATCHCAT);
     }
   }
@@ -149,8 +172,9 @@
 # if USE_IMAGE_ID
 // this is the imageID-based match
-void matchImage (Catalog *catalog, off_t meas, int cat) {
+void matchImage (Catalog *catalog, off_t meas, int cat, int MATCHCAT) {
 
   off_t idx, ID;
   Measure *measure;
+  int i, found;
 
   measure = &catalog[cat].measure[meas];
@@ -178,4 +202,21 @@
     REALLOCATE (mlist[idx], off_t, NLIST[idx]);
   }
+
+  if (MATCHCAT) {
+    // index for image -> catalog list
+    found = FALSE;
+    for (i = 0; !found && (i < Ncatlist[idx]); i++) {
+      if (catlist[idx][i] == cat) found = TRUE;
+    }
+    if (!found) {
+      catlist[idx][Ncatlist[idx]] = cat;
+      Ncatlist[idx] ++;
+      if (Ncatlist[idx] == NCATLIST[idx]) {
+	NCATLIST[idx] += 32;
+	REALLOCATE (catlist[idx], int, NCATLIST[idx]);
+      }
+    }
+  }
+
   return;
 }
@@ -183,5 +224,5 @@
 # else
 // this is the time-based match
-void matchImage (Catalog *catalog, off_t meas, int cat) {
+void matchImage (Catalog *catalog, off_t meas, int cat, int MATCHCAT) {
 
   off_t i;
@@ -221,4 +262,5 @@
 # endif
 
+/*
 Coords *getCoords (off_t meas, int cat) {
 
@@ -229,4 +271,5 @@
   return (&image[i].coords);
 }
+*/
 
 void plot_images () {
@@ -280,6 +323,7 @@
 void fixImageRaw (Catalog *catalog, int Ncatalog, off_t im) {
 
-  off_t i, m, c, n;
+  off_t i, m, c, n, nPos;
   double X, Y, L, M, P, Q, R, D, dR, dD;
+  double dPos;
 
   Mosaic *mosaic;
@@ -325,10 +369,10 @@
     dD = 3600.0*(catalog[c].average[n].D - D);
 
-    if (fabs(catalog[c].measure[m].dR - dR) > 10.0) {
+    if (fabs(catalog[c].measure[m].dR - dR) > 2.0) {
       fprintf (stderr, "!");
       setBadCoords (im); // report a failure for this image
       return;
     }
-    if (fabs(catalog[c].measure[m].dD - dD) > 10.0) {
+    if (fabs(catalog[c].measure[m].dD - dD) > 2.0) {
       fprintf (stderr, "*");
       setBadCoords (im); // report a failure for this image
@@ -355,4 +399,69 @@
 
   saveOffsets (dPos, nPos, im);
+
+  return;
+}
+
+// return StarData values for detections in the specified image, converting coordinates from the
+// chip positions: X,Y -> L,M -> P,Q -> R,D
+void resetImageRaw (Catalog *catalog, int Ncatalog, off_t im) {
+
+  off_t i, m, c, n;
+  double X, Y, L, M, P, Q, R, D, dR, dD;
+
+  Mosaic *mosaic;
+  Coords *moscoords, *imcoords, *oldcoords;
+
+  // check if this image is bad and should be skipped
+  if (!badCoords(im)) {
+    fprintf (stderr, "ERROR: inconsistent result?");
+    exit (1);
+  }
+
+  // replace the current coords with the old coords:
+  oldcoords = getCoords (im);
+  memcpy (&image[im].coords, oldcoords, sizeof(Coords));
+
+  moscoords = NULL;
+  mosaic = getMosaicForImage (im);
+  if (mosaic != NULL) {
+    moscoords = &mosaic[0].coords;
+  }
+  imcoords = &image[im].coords;
+
+  for (i = 0; i < Nlist[im]; i++) {
+    m = mlist[im][i];
+    c = clist[im][i];
+
+    X = catalog[c].measure[m].Xccd;
+    Y = catalog[c].measure[m].Yccd;
+    n = catalog[c].measure[m].averef;
+
+    dR = dD = 0.0;
+    if (moscoords == NULL) {
+      // this is a Simple image (not a mosaic)
+      // note that for a Simple image, L,M = P,Q
+      XY_to_LM (&L, &M, X, Y, imcoords);
+      LM_to_RD (&R, &D, L, M, imcoords);
+    } else {
+      XY_to_LM (&L, &M, X, Y, imcoords);
+      XY_to_LM (&P, &Q, L, M, moscoords);
+      LM_to_RD (&R, &D, P, Q, moscoords);
+    }
+
+    catalog[c].measure[m].dR = dR;
+    catalog[c].measure[m].dD = dD;
+
+    if (catalog[c].measure[m].dR > +180.0*3600.0) {
+      // average on high end of boundary, move star up
+      R += 360.0;
+      catalog[c].measure[m].dR = 3600.0*(catalog[c].average[n].R - R);
+    }
+    if (catalog[c].measure[m].dR < -180.0*3600.0) {
+      // average on low end of boundary, move star down
+      R -= 360.0;
+      catalog[c].measure[m].dR = 3600.0*(catalog[c].average[n].R - R);
+    }
+  }
 
   return;
Index: /branches/eam_branches/relastro.20100326/src/UpdateObjectOffsets.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/UpdateObjectOffsets.c	(revision 27488)
+++ /branches/eam_branches/relastro.20100326/src/UpdateObjectOffsets.c	(revision 27489)
@@ -34,6 +34,6 @@
 
     // match measurements with images
-    initImageBins (&catalog, 1);
-    findImages (&catalog, 1);
+    initImageBins (&catalog, 1, FALSE);
+    findImages (&catalog, 1, FALSE);
 
     // update the detection coordinates using the new image parameters
Index: /branches/eam_branches/relastro.20100326/src/fitpoly.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/fitpoly.c	(revision 27488)
+++ /branches/eam_branches/relastro.20100326/src/fitpoly.c	(revision 27489)
@@ -146,7 +146,6 @@
     // ix, iy, vector[i][0], ix, iy, vector[i][1]);
   }	
-
-  status = dgaussjordan (matrix, vector, fit[0].Nelems, 2); 
-  if (!status) {
+  
+  if (!dgaussjordan (matrix, vector, fit[0].Nelems, 2)) {
     return (FALSE);
   }
Index: /branches/eam_branches/relastro.20100326/src/relastro.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/relastro.c	(revision 27488)
+++ /branches/eam_branches/relastro.20100326/src/relastro.c	(revision 27489)
@@ -47,8 +47,8 @@
 
   /* match measurements with images */
-  initImageBins (catalog, Ncatalog);
+  initImageBins (catalog, Ncatalog, TRUE);
   MARKTIME("make image bins: %f sec\n", dtime);
 
-  findImages (catalog, Ncatalog);
+  findImages (catalog, Ncatalog, TRUE);
   MARKTIME("set up image indexes: %f sec\n", dtime);
 
@@ -86,5 +86,5 @@
 
   // iterate over catalogs to make detection coordinates consistant
-  FixProblemImages ();
+  FixProblemImages (skylist);
 
   // save the updated image parameters
