Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/include/relphot.h
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/include/relphot.h	(revision 31259)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/include/relphot.h	(revision 31260)
@@ -227,4 +227,6 @@
 void relphot_help (int argc, char **argv);
 
+off_t getImageByID (off_t ID);
+
 int rationalize_mosaics ();
 int LimitDensityCatalog (Catalog *subcatalog, Catalog *catalog);
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/MosaicOps.c	(revision 31259)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/MosaicOps.c	(revision 31260)
@@ -9,4 +9,7 @@
 static off_t   **imlist; /* mosaic -> image[] */
 static off_t   **bin;    /* catalog, measure -> mosaic */
+
+// list of mosaics associated with an image
+static off_t   *mosimage;
 
 // list of mosaic associated with each image  
@@ -33,4 +36,6 @@
   ALLOCATE (NIMLIST, off_t,   NMOSAIC);
 
+  ALLOCATE (mosimage, off_t, Nimage); // mosaic to which image belongs
+
   /* a 'mosaic' in relphot is (unlike relastro) a virtual concept: there is no 
    * entry in the image table that represents this mosaic.  Instead, it is an
@@ -40,4 +45,5 @@
   /* generate list of unique mosaics */
   for (i = 0; i < Nimage; i++) {
+    mosimage[i] = -1;
 
     /* select valid mosaic images by photcode */
@@ -56,4 +62,7 @@
       if (start > mosaic[j].stop)  continue;
       found = TRUE;
+
+      // add reference from image to mosaic
+      mosimage[i] = j;
 
       /* add image to mosaic image list */
@@ -84,4 +93,7 @@
     imlist[Nmosaic][0] = i;
 
+    // add reference from image to mosaic
+    mosimage[i] = Nmosaic;
+    
     Nmosaic ++;
     if (Nmosaic == NMOSAIC) {
@@ -269,4 +281,53 @@
 }
 
+# if 1
+// # if USE_IMAGE_ID
+
+void matchMosaics (Catalog *catalog, off_t meas, int cat) {
+
+  off_t idx, ID, mosID;
+  Measure *measure;
+
+  measure = &catalog[cat].measure[meas];
+
+  ID = measure[0].imageID;
+  idx = getImageByID (ID);
+  if (idx == -1) {
+    if (VERBOSE2) fprintf (stderr, "missed measurement "OFF_T_FMT", %d\n", meas, cat);
+  }
+
+  mosID = mosimage[idx];
+  if (mosID < 0) {
+    Image *image = getimage(idx);
+    fprintf (stderr, "unmatched image %s\n", image[0].name);
+    return;
+  }
+
+  // test to check we got the right match:
+  {
+    Image *image = getimage(idx);
+    unsigned int imageStart = image[0].tzero - MAX(0.01*image[0].trate*image[0].NY, 1);
+    if (imageStart != mosaic[mosID].start) {
+      fprintf (stderr, "error in image to mosaic match\n");
+      abort();
+    }
+  }
+
+  bin[cat][meas] = mosID;
+
+  clist[mosID][Nlist[mosID]] = cat;
+  mlist[mosID][Nlist[mosID]] = meas;
+  Nlist[mosID] ++;
+    
+  if (Nlist[mosID] == NLIST[mosID]) {
+    NLIST[mosID] += 100;
+    REALLOCATE (clist[mosID], int,   NLIST[mosID]);
+    REALLOCATE (mlist[mosID], off_t, NLIST[mosID]);
+  }	
+  return;
+}
+
+# else // USE_IMAGE_ID
+
 void matchMosaics (Catalog *catalog, off_t meas, int cat) {
 
@@ -287,5 +348,5 @@
       setGridMeasure (meas, cat, X, Y);
     }
-# endif
+# endif // GRID_V1
 
     bin[cat][meas] = i;
@@ -303,7 +364,8 @@
   }
   // if we miss a measurement, it might be a REF type of value 
-  // fprintf (stderr, "missed measurement\n");
+  if (VERBOSE2) fprintf (stderr, "missed measurement "OFF_T_FMT", %d\n", meas, cat);
   return;
 }
+# endif // USE_IMAGE_ID
 
 float getMmos (off_t meas, int cat) {
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/reload_catalogs.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/reload_catalogs.c	(revision 31259)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/reload_catalogs.c	(revision 31260)
@@ -1,3 +1,9 @@
 # include "relphot.h"
+
+# define TIMESTAMP(TIME) \
+    gettimeofday (&stop, (void *) NULL);	\
+    dtime = DTIME (stop, start);		\
+    TIME += dtime;				\
+    gettimeofday (&start, (void *) NULL);
 
 void reload_catalogs (SkyList *skylist) {
@@ -8,8 +14,12 @@
   Catalog catalog;
 
+  struct timeval start, stop;
+  double dtime, time1, time2, time3, time4, time5, time6, time7; 
+
   if (VERBOSE) fprintf (stderr, "re-loading catalog data\n");
 
   /* load data from each region file */
   for (i = 0; i < skylist[0].Nregions; i++) {
+    gettimeofday (&start, (void *) NULL);
     catalog.filename = skylist[0].filename[i];
 
@@ -20,4 +30,5 @@
       continue;
     }
+    TIMESTAMP(time1);
 
     catalog.catformat = dvo_catalog_catformat (CATFORMAT);    // set the default catformat from config data
@@ -36,20 +47,35 @@
 	continue;
     }
+    TIMESTAMP(time2);
 
     initImageBins  (&catalog, 1);
     initMosaicBins (&catalog, 1);
     initGridBins   (&catalog, 1);
+    TIMESTAMP(time3);
 
     findImages (&catalog, 1);
     findMosaics (&catalog, 1);
+    TIMESTAMP(time4);
 
     setMrelFinal (&catalog);
+    TIMESTAMP(time5);
+
     dvo_catalog_save (&catalog, VERBOSE); 
     dvo_catalog_unlock (&catalog);
     dvo_catalog_free (&catalog);
+    TIMESTAMP(time6);
 
     freeImageBins (1);
     freeMosaicBins (1);
     freeGridBins (1);
+    TIMESTAMP(time7);
   }
+
+  fprintf (stderr, "time1 %f : find catalog\n", time1);
+  fprintf (stderr, "time2 %f : load catalog\n", time2);
+  fprintf (stderr, "time3 %f : init imbins\n",  time3);
+  fprintf (stderr, "time4 %f : find images\n",  time4);
+  fprintf (stderr, "time5 %f : set Mrel\n",     time5);
+  fprintf (stderr, "time6 %f : save catalog\n", time6);
+  fprintf (stderr, "time7 %f : free catalog\n", time7);
 }
Index: /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/relphot.c
===================================================================
--- /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/relphot.c	(revision 31259)
+++ /branches/eam_branches/ipp-20110404/Ohana/src/relphot/src/relphot.c	(revision 31260)
@@ -47,5 +47,5 @@
     if (!dvo_image_load (&db, VERBOSE, FALSE)) Shutdown ("can't read image catalog %s", db.filename);
   }
-  MARKTIME("load image data: %f sec\n", dtime);
+  MARKTIME("-- load image data: %f sec\n", dtime);
 
   /* load regions and images based on specified sky patch */
@@ -54,5 +54,5 @@
   // XXX this is fairly lame: argv[1] is photcode if UserPatchSelect is true
   skylist = load_images (&db, argv[1], &UserPatch, UserPatchSelect);
-  MARKTIME("load images: %f sec\n", dtime);
+  MARKTIME("-- load images: %f sec\n", dtime);
 
   /* unlock, if we can (else, unlocked below) */
@@ -61,5 +61,5 @@
   /* load catalog data from region files */
   catalog = load_catalogs (skylist, &Ncatalog);
-  MARKTIME("load catalog data: %f sec\n", dtime);
+  MARKTIME("-- load catalog data: %f sec\n", dtime);
   
   /* add in a loop over the catalogs calling dvo_catalog_chipcoords */
@@ -67,5 +67,5 @@
   /* match measurements with images, mosaics */
   initImageBins  (catalog, Ncatalog);
-  MARKTIME("make image bins: %f sec\n", dtime);
+  MARKTIME("-- make image bins: %f sec\n", dtime);
 
   initMosaicBins (catalog, Ncatalog);
@@ -74,8 +74,8 @@
 
   findImages (catalog, Ncatalog);
-  MARKTIME("set up image indexes: %f sec\n", dtime);
+  MARKTIME("-- set up image indexes: %f sec\n", dtime);
 
   findMosaics (catalog, Ncatalog);  /* also sets Grid values */
-  MARKTIME("set up mosaic indexes: %f sec\n", dtime);
+  MARKTIME("-- set up mosaic indexes: %f sec\n", dtime);
 
   SAVEPLOT = FALSE;
@@ -152,4 +152,5 @@
     // if ((i == 4) || (i == 8) || (i == 12) || (i == 16)) clean_images ();
     global_stats (catalog, Ncatalog);
+    MARKTIME("-- finished loop %d: %f sec\n", i, dtime);
   }
 
@@ -169,4 +170,5 @@
   setMcal  (catalog, TRUE);
   setMmos  (catalog, TRUE);
+  MARKTIME("-- finalize Mcal values: %f sec\n", dtime);
 
   /* at this point, we have correct cal coeffs in the image/mosaic structures */
@@ -178,6 +180,7 @@
   /* load catalog data from region files, update Mrel include all data */
   reload_catalogs (skylist);
+  MARKTIME("-- updated all catalogs: %f sec\n", dtime);
+
   setMcalFinal ();
-
   reload_images (&db);
   
