Index: /branches/eam_branches/relastro.20100326/include/relastro.h
===================================================================
--- /branches/eam_branches/relastro.20100326/include/relastro.h	(revision 27496)
+++ /branches/eam_branches/relastro.20100326/include/relastro.h	(revision 27497)
@@ -94,4 +94,5 @@
 
 int    RESET;
+int    NLOOP;
 int    UPDATE;
 int    PLOTSTUFF;
Index: /branches/eam_branches/relastro.20100326/src/ImageOps.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/ImageOps.c	(revision 27496)
+++ /branches/eam_branches/relastro.20100326/src/ImageOps.c	(revision 27497)
@@ -333,7 +333,9 @@
   if (badCoords(im)) return;
 
+  // WRP images need to have an associated mosaic
   moscoords = NULL;
-  mosaic = getMosaicForImage (im);
-  if (mosaic != NULL) {
+  if (!strcmp(&image[im].coords.ctype[4], "-WRP")) {
+    mosaic = getMosaicForImage (im);
+    if (mosaic == NULL) return;  // if we cannot find the associated image, skip it
     moscoords = &mosaic[0].coords;
   }
@@ -383,10 +385,10 @@
     // complain if the new location is far from the old location
     if (fabs(catalog[c].measure[m].dR - dR) > 2.0) {
-      fprintf (stderr, "!");
+      fprintf (stderr, "@");
       setBadCoords (im); // report a failure for this image
       return;
     }
     if (fabs(catalog[c].measure[m].dD - dD) > 2.0) {
-      fprintf (stderr, "*");
+      fprintf (stderr, "#");
       setBadCoords (im); // report a failure for this image
       return;
@@ -436,7 +438,9 @@
   memcpy (&image[im].coords, oldcoords, sizeof(Coords));
 
+  // WRP images need to have an associated mosaic
   moscoords = NULL;
-  mosaic = getMosaicForImage (im);
-  if (mosaic != NULL) {
+  if (!strcmp(&image[im].coords.ctype[4], "-WRP")) {
+    mosaic = getMosaicForImage (im);
+    if (mosaic == NULL) return;  // if we cannot find the associated image, skip it
     moscoords = &mosaic[0].coords;
   }
@@ -492,6 +496,4 @@
   StarData *raw;
 
-  ALLOCATE (raw, StarData, Nlist[im]);
-
   mosaic = NULL;
   moscoords = NULL;
@@ -500,8 +502,10 @@
     if (mosaic == NULL) {
       fprintf (stderr, "mosaic not found for image %s\n", image[im].name);
-      abort();
+      return NULL;
     }
     moscoords = &mosaic[0].coords;
   }
+
+  ALLOCATE (raw, StarData, Nlist[im]);
 
   for (i = 0; i < Nlist[im]; i++) {
@@ -567,6 +571,4 @@
   StarData *ref;
 
-  ALLOCATE (ref, StarData, Nlist[im]);
-
   mosaic = NULL;
   moscoords = NULL;
@@ -575,8 +577,10 @@
     if (mosaic == NULL) {
       fprintf (stderr, "mosaic not found for image %s\n", image[im].name);
-      abort ();
+      return NULL;
     }
     moscoords = &mosaic[0].coords;
   }
+
+  ALLOCATE (ref, StarData, Nlist[im]);
 
   for (i = 0; i < Nlist[im]; i++) {
Index: /branches/eam_branches/relastro.20100326/src/MosaicOps.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/MosaicOps.c	(revision 27496)
+++ /branches/eam_branches/relastro.20100326/src/MosaicOps.c	(revision 27497)
@@ -146,5 +146,8 @@
 
     Nmos = getMosaicByTimes (start, stop, startMos, stopMos, indexMos);
-    if (Nmos == -1) continue;
+    if (Nmos == -1) {
+      fprintf (stderr, "cannot match mosaic for %s\n", image[i].name);
+      continue;
+    }
 
     // mosaic corresponding to this image
@@ -185,4 +188,8 @@
     // this function does the reverse-lookup for the mosaic corresponding to this image
     new = getImageRaw (catalog, Ncatalog, im, &Nnew, MODE_MOSAIC);
+    if (!new) {
+      fprintf (stderr, "inconsistent: missing mosaic for image already associated with a mosaic? (1)\n");
+      abort();
+    }
     
     // merge new and raw
@@ -217,4 +224,8 @@
     // this function does the reverse-lookup for the mosaic corresponding to this image
     new = getImageRef (catalog, Ncatalog, im, &Nnew, MODE_MOSAIC);
+    if (!new) {
+      fprintf (stderr, "inconsistent: missing mosaic for image already associated with a mosaic? (2)\n");
+      abort();
+    }
     
     // merge new and ref
Index: /branches/eam_branches/relastro.20100326/src/UpdateChips.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/UpdateChips.c	(revision 27496)
+++ /branches/eam_branches/relastro.20100326/src/UpdateChips.c	(revision 27497)
@@ -18,7 +18,9 @@
     /* convert measure coordinates to raw entries */
     raw = getImageRaw (catalog, Ncatalog, i, &Nraw, MODE_MOSAIC);
+    if (!raw) continue;
 
     /* convert average coordinates to ref entries */
     ref = getImageRef (catalog, Ncatalog, i, &Nref, MODE_MOSAIC);
+    if (!ref) continue;
 
     // note that Nraw & Nref must be equal: if not, we made a programming error in one of these two functions.
Index: /branches/eam_branches/relastro.20100326/src/UpdateObjects.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/UpdateObjects.c	(revision 27496)
+++ /branches/eam_branches/relastro.20100326/src/UpdateObjects.c	(revision 27497)
@@ -40,10 +40,12 @@
 int UpdateObjects (Catalog *catalog, int Ncatalog) {
 
-  int i, j, k, m, N, Nsecfilt;
+  off_t j, k, m;
+  int i, N, Nsecfilt, mode;
   StatType statsR, statsD;
   Coords coords;
   PMFit fit;
   time_t To;
-  int mode, Nave, Npm, Npar, Nskip;
+  off_t Nave, Npm, Npar, Nskip;
+  off_t NaveSum, NpmSum, NparSum, NskipSum;
   double Tmin, Tmax;
 
@@ -63,5 +65,4 @@
   // use J2000 as a reference time
   To = ohana_date_to_sec ("2000/01/01");
-  Nave = Npar = Npm = 0;
 
   // XXX in the future, use catalog[0].Nsecfilt only?  allow catalogs to have variable Nsecfilt?
@@ -69,9 +70,10 @@
   assert (catalog[0].Nsecfilt == Nsecfilt);
 
+  NaveSum = NparSum = NpmSum = NskipSum = 0;
   for (i = 0; i < Ncatalog; i++) {
 
     if (VERBOSE) fprintf (stderr, "astrometrize catalog %d : %lld ave, %lld meas\n", i, (long long) catalog[i].Naverage, (long long) catalog[i].Nmeasure);
 
-    Nskip = 0;
+    Nave = Npar = Npm = Nskip = 0;
     for (j = 0; j < catalog[i].Naverage; j++) {
       /* calculate the average value of R,D for a single star */
@@ -270,8 +272,12 @@
     }
 
-    if (VERBOSE) fprintf (stderr, "catalog %d : %d ave, %d pm, %d par : Nskip % d\n", i, Nave, Npm, Npar, Nskip);
+    NaveSum += Nave;
+    NpmSum += Npm;
+    NparSum += Npar;
+    NskipSum += Nskip;
+    if (VERBOSE) fprintf (stderr, "catalog %lld : %lld ave, %lld pm, %lld par : Nskip %lld\n", (long long) i, (long long) Nave, (long long) Npm, (long long) Npar, (long long) Nskip);
   }
 
-  if (VERBOSE) fprintf (stderr, "fitted %d objects (%d ave, %d pm, %d par)\n", Nave + Npm + Npar, Nave, Npm, Npar);
+  fprintf (stderr, "fitted %lld objects (%lld ave, %lld pm, %lld par), skipped %lld\n", (long long) (NaveSum + NpmSum + NparSum), (long long) NaveSum, (long long) NpmSum, (long long) NparSum, (long long) NskipSum);
   return (TRUE);
 }
Index: /branches/eam_branches/relastro.20100326/src/UpdateSimple.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/UpdateSimple.c	(revision 27496)
+++ /branches/eam_branches/relastro.20100326/src/UpdateSimple.c	(revision 27497)
@@ -18,7 +18,9 @@
     /* convert measure coordinates to raw entries */
     raw = getImageRaw (catalog, Ncatalog, i, &Nstars, MODE_SIMPLE);
+    if (!raw) continue;
 
     /* convert average coordinates to ref entries */
     ref = getImageRef (catalog, Ncatalog, i, &Nstars, MODE_SIMPLE);
+    if (!ref) continue;
 
     FitSimple (raw, ref, Nstars, &image[i].coords);
Index: /branches/eam_branches/relastro.20100326/src/args.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/args.c	(revision 27496)
+++ /branches/eam_branches/relastro.20100326/src/args.c	(revision 27497)
@@ -244,4 +244,11 @@
     remove_argument (N, &argc, argv);
     DophotSelect = TRUE;
+  }
+
+  NLOOP = 4;
+  if ((N = get_argument (argc, argv, "-nloop"))) {
+    remove_argument (N, &argc, argv);
+    NLOOP = atof (argv[N]);
+    remove_argument (N, &argc, argv);
   }
 
@@ -267,4 +274,5 @@
   fprintf (stderr, "  -statmode (mode)\n");
   fprintf (stderr, "  -reset");
+  fprintf (stderr, "  -nloop (N) : number of image-fit iterations");
   fprintf (stderr, "  -update : apply new fit to database\n");
   fprintf (stderr, "  -params\n");
Index: /branches/eam_branches/relastro.20100326/src/relastro.c
===================================================================
--- /branches/eam_branches/relastro.20100326/src/relastro.c	(revision 27496)
+++ /branches/eam_branches/relastro.20100326/src/relastro.c	(revision 27497)
@@ -9,5 +9,5 @@
 int main (int argc, char **argv) {
 
-  int status, Ncatalog;
+  int i, status, Ncatalog;
   Catalog *catalog;
   FITS_DB db;
@@ -61,13 +61,23 @@
   switch (FIT_TARGET) {
     case TARGET_SIMPLE:
-      UpdateSimple (catalog, Ncatalog);
+      for (i = 0; i < NLOOP; i++) {
+	UpdateObjects (catalog, Ncatalog);
+	UpdateSimple (catalog, Ncatalog);
+      }
       break;
 
     case TARGET_CHIPS:
-      UpdateChips (catalog, Ncatalog);
+      for (i = 0; i < NLOOP; i++) {
+	UpdateObjects (catalog, Ncatalog);
+	UpdateChips (catalog, Ncatalog);
+	MARKTIME("update chips: %f sec\n", dtime);
+      }
       break;
 
     case TARGET_MOSAICS:
-      UpdateMosaic (catalog, Ncatalog);
+      for (i = 0; i < NLOOP; i++) {
+	UpdateObjects (catalog, Ncatalog);
+	UpdateMosaic (catalog, Ncatalog);
+      }
       break;
 
