Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/include/dvomerge.h
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/include/dvomerge.h	(revision 37083)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/include/dvomerge.h	(revision 37084)
@@ -129,10 +129,16 @@
 int        merge_catalogs_old     PROTO((SkyRegion *region, Catalog *output, Catalog *input, double RADIUS, int *secflitMap));
 
+off_t 	  *build_measure_links    PROTO((Average *average, off_t Naverage, Measure *measure, off_t Nmeasure));
 off_t 	  *init_measure_links     PROTO((Average *average, off_t Naverage, Measure *measure, off_t Nmeasure));
+int   	   add_meas_link     	  PROTO((Average *average, off_t *next, off_t Nmeasure, off_t NMEASURE));
+Measure   *sort_measure     	  PROTO((Average *average, off_t Naverage, Measure *measure, off_t Nmeasure, off_t *next));
+
+off_t 	  *build_lensing_links    PROTO((Average *average, off_t Naverage, Lensing *lensing, off_t Nlensing));
+off_t 	  *init_lensing_links     PROTO((Average *average, off_t Naverage, Lensing *lensing, off_t Nlensing));
+int   	   add_lens_link     	  PROTO((Average *average, off_t *next, off_t Nlensing, off_t NLENSING));
+Lensing   *sort_lensing     	  PROTO((Average *average, off_t Naverage, Lensing *lensing, off_t Nlensing, off_t *next));
+
 off_t 	  *init_missing_links     PROTO((Average *average, off_t Naverage, Missing *missing, off_t Nmissing));
-off_t 	  *build_measure_links    PROTO((Average *average, off_t Naverage, Measure *measure, off_t Nmeasure));
-int   	   add_meas_link     	  PROTO((Average *average, off_t *next, off_t Nmeasure, off_t NMEASURE));
 int   	   add_miss_link     	  PROTO((Average *average, off_t *next, off_t Nmissing));
-Measure   *sort_measure     	  PROTO((Average *average, off_t Naverage, Measure *measure, off_t Nmeasure, off_t *next));
 Missing   *sort_missing     	  PROTO((Average *average, off_t Naverage, Missing *missing, off_t Nmissing, off_t *next_miss));
 
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/LoadCatalog.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/LoadCatalog.c	(revision 37083)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/LoadCatalog.c	(revision 37084)
@@ -8,5 +8,5 @@
 
     // always load all of the data (if any exists)
-    catalog[0].catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
+    catalog[0].catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF | LOAD_LENSING | LOAD_LENSOBJ;
 
     catalog[0].catformat = dvo_catalog_catformat (CATFORMAT);  // set the default catformat from config data
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/build_links.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/build_links.c	(revision 37083)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/build_links.c	(revision 37084)
@@ -1,3 +1,26 @@
 # include "dvomerge.h"
+
+/* 
+
+There are two modes for the measure table: sorted and unsorted. 
+
+In sorted mode, all measures associated with a given average are in a single block.  The block
+is pointed to by average->measureOffset and the range is average->Nmeasure
+
+In unsorted mode, it is not possible to go directly from average to measure without scanning.
+In this case, it is necessary to use the value measure->averef to find the corresponding
+average entry.  
+
+Note that average->measureOffset and measure->averef are only valid for a given load of the
+data: they refer to the sequence number in the data blocks.
+
+next_meas is a list of the equivalent sequence of the measure block as if it were sorted.
+
+to find the sequence of measurements for a given average:
+n_0 = average->measureOffset
+n_1 = next_meas[n_0]
+n_i = next_meas[n_i-1]
+
+*/
 
 /* build the initial links assuming the table is sorted, 
@@ -6,17 +29,17 @@
 
   off_t i, j, N;
-  off_t *next;
+  off_t *next_meas;
 
   N = 0;
 
-  ALLOCATE (next, off_t, Nmeasure);
+  ALLOCATE (next_meas, off_t, Nmeasure);
   for (i = 0; i < Naverage; i++, N++) {
     for (j = 0; j < average[i].Nmeasure - 1; j++, N++) {
-      next[N] = N + 1;
+      next_meas[N] = N + 1;
       if (N >= Nmeasure) {
 	fprintf (stderr, "WARNING: N out of bounds (1)\n");
       }
     }
-    next[N] = -1;
+    next_meas[N] = -1;
     if (N >= Nmeasure) {
       fprintf (stderr, "WARNING: N out of bounds (2)\n");
@@ -28,59 +51,5 @@
     }
   }
-  return (next);
-}
-
-/* average[].measureOffset, average[].Nmeasure are valid within an addstar run */
-int add_meas_link (Average *average, off_t *next, off_t Nmeasure, off_t NMEASURE) {
-
-  off_t k, m;
-
-  /* if we have trouble, check validity of next[m] : m < Nmeasure */
-  m = average[0].measureOffset;  
-
-  for (k = 0; k < average[0].Nmeasure - 1; k++)  {
-    m = next[m];
-    if (m >= NMEASURE) {
-      fprintf (stderr, "WARNING: m out of bounds (3)\n");
-    }
-  }
-
-  /* set up references */
-  next[Nmeasure] = -1;
-  if (Nmeasure >= NMEASURE) {
-    fprintf (stderr, "WARNING: Nmeasure out of bounds (1)\n");
-  }
-
-  if (m == -1) {
-    average[0].measureOffset = Nmeasure;
-  } else {
-    next[m] = Nmeasure;
-    if (m >= NMEASURE) {
-      fprintf (stderr, "WARNING: m out of bounds (4)\n");
-    }
-  }
-
-  return (TRUE);
-}
-
-Measure *sort_measure (Average *average, off_t Naverage, Measure *measure, off_t Nmeasure, off_t *next) {
-
-  off_t i, k, n, N;
-  Measure *tmpmeasure;
-
-  /* fix order of Measure (memory intensive, but fast) */
-  N = 0; 
-  ALLOCATE (tmpmeasure, Measure, Nmeasure);
-  for (i = 0; i < Naverage; i++) {
-    n = average[i].measureOffset;
-    average[i].measureOffset = N;
-    for (k = 0; k < average[i].Nmeasure; k++, N++) {
-      tmpmeasure[N] = measure[n]; 
-      tmpmeasure[N].averef = i;
-      n = next[n];
-    }
-  }
-  free (measure);
-  return (tmpmeasure);
+  return (next_meas);
 }
 
@@ -95,7 +64,7 @@
 
   off_t i, m, k, Nm, averef;
-  off_t *next;
-
-  ALLOCATE (next, off_t, Nmeasure);
+  off_t *next_meas;
+
+  ALLOCATE (next_meas, off_t, Nmeasure);
 
   /* reset the Nm, offset values for average */
@@ -108,5 +77,5 @@
     averef = measure[Nm].averef;
     m = average[averef].measureOffset;  
-    next[Nm] = -1;
+    next_meas[Nm] = -1;
 
     if (m == -1) { /* no links yet for source */
@@ -116,6 +85,6 @@
     }
 
-    for (k = 0; next[m] != -1; k++) {
-      m = next[m];
+    for (k = 0; next_meas[m] != -1; k++) {
+      m = next_meas[m];
       if (m >= Nmeasure) {
 	fprintf (stderr, "WARNING: m out of bounds (1)\n");
@@ -124,11 +93,69 @@
 
     average[averef].Nmeasure = k + 2;
-    next[m] = Nm;
+    next_meas[m] = Nm;
     if (m >= Nmeasure) {
       fprintf (stderr, "WARNING: m out of bounds (2)\n");
     }
   }
-  return (next);
-}
+  return (next_meas);
+}
+
+/* average[].measureOffset, average[].Nmeasure are valid within an addstar run */
+int add_meas_link (Average *average, off_t *next_meas, off_t Nmeasure, off_t NMEASURE) {
+
+  off_t k, m;
+
+  /* if we have trouble, check validity of next_meas[m] : m < Nmeasure */
+  m = average[0].measureOffset;  
+
+  for (k = 0; k < average[0].Nmeasure - 1; k++)  {
+    m = next_meas[m];
+    if (m >= NMEASURE) {
+      fprintf (stderr, "WARNING: m out of bounds (3)\n");
+    }
+  }
+
+  /* set up references */
+  next_meas[Nmeasure] = -1;
+  if (Nmeasure >= NMEASURE) {
+    fprintf (stderr, "WARNING: Nmeasure out of bounds (1)\n");
+  }
+
+  if (m == -1) {
+    average[0].measureOffset = Nmeasure;
+  } else {
+    next_meas[m] = Nmeasure;
+    if (m >= NMEASURE) {
+      fprintf (stderr, "WARNING: m out of bounds (4)\n");
+    }
+  }
+
+  return (TRUE);
+}
+
+Measure *sort_measure (Average *average, off_t Naverage, Measure *measure, off_t Nmeasure, off_t *next_meas) {
+
+  off_t i, k, n, N;
+  Measure *tmpmeasure;
+
+  /* fix order of Measure (memory intensive, but fast) */
+  N = 0; 
+  ALLOCATE (tmpmeasure, Measure, Nmeasure);
+  for (i = 0; i < Naverage; i++) {
+    n = average[i].measureOffset;
+    average[i].measureOffset = N;
+    for (k = 0; k < average[i].Nmeasure; k++, N++) {
+      if (n == -1) abort();
+      tmpmeasure[N] = measure[n]; 
+      if (measure[n].averef != i) abort();
+      tmpmeasure[N].averef = i;
+      n = next_meas[n];
+    }
+  }
+  free (measure);
+  return (tmpmeasure);
+}
+
+/*******************************************************************************************/
 
 /* build the initial links assuming the table is sorted */
@@ -136,15 +163,15 @@
 
   off_t i, j, N;
-  off_t *next;
+  off_t *next_miss;
 
   N = 0;
 
-  ALLOCATE (next, off_t, Nmissing);
+  ALLOCATE (next_miss, off_t, Nmissing);
   for (i = 0; i < Naverage; i++) {
     for (j = 0; j < average[i].Nmissing - 1; j++, N++) {
-      next[N] = N + 1;
+      next_miss[N] = N + 1;
     }
     if (average[i].Nmissing > 0) {
-      next[N] = -1;
+      next_miss[N] = -1;
       if (N >= Nmissing) {
 	fprintf (stderr, "overflow in init_missing_links");
@@ -155,8 +182,8 @@
 
   }
-  return (next);
-}
-
-int add_miss_link (Average *average, off_t *next, off_t Nmissing) {
+  return (next_miss);
+}
+
+int add_miss_link (Average *average, off_t *next_miss, off_t Nmissing) {
 
   off_t k, m;
@@ -165,13 +192,13 @@
   if (average[0].Nmissing < 1) {
     average[0].missingOffset = Nmissing;
-    next[Nmissing] = -1;
+    next_miss[Nmissing] = -1;
     return (TRUE);
   }
 
   m = average[0].missingOffset;  
-  for (k = 0; k < average[0].Nmissing - 1; k++) m = next[m];
+  for (k = 0; k < average[0].Nmissing - 1; k++) m = next_miss[m];
   /* set up references */
-  next[Nmissing] = -1;
-  next[m] = Nmissing;
+  next_miss[Nmissing] = -1;
+  next_miss[m] = Nmissing;
   return (TRUE);
 }
@@ -180,5 +207,5 @@
    we must always save the missing table, if it exists */
 
-Missing *sort_missing (Average *average, off_t Naverage, Missing *missing, off_t Nmissing, off_t *next) {
+Missing *sort_missing (Average *average, off_t Naverage, Missing *missing, off_t Nmissing, off_t *next_miss) {
 
   off_t i, k, n, N;
@@ -193,5 +220,5 @@
     for (k = 0; k < average[i].Nmissing; k++, N++) {
       tmpmissing[N] = missing[n]; 
-      n = next[n];
+      n = next_miss[n];
     }
   }
@@ -199,2 +226,142 @@
   return (tmpmissing);
 }
+
+/*******************************************************************************************/
+
+/* build the initial links assuming the table is sorted, 
+   not partial, and has a correct set of average[].lensingOffset,Nlensing values */
+off_t *init_lensing_links (Average *average, off_t Naverage, Lensing *lensing, off_t Nlensing) {
+
+  off_t i, j, N;
+  off_t *next_lens;
+
+  N = 0;
+
+  ALLOCATE (next_lens, off_t, Nlensing);
+  for (i = 0; i < Naverage; i++) {
+    if (!average[i].Nlensing) continue;
+    for (j = 0; j < average[i].Nlensing - 1; j++, N++) {
+      next_lens[N] = N + 1;
+      if (N >= Nlensing) {
+	fprintf (stderr, "WARNING: N out of bounds (1)\n");
+      }
+    }
+    next_lens[N] = -1;
+    if (N >= Nlensing) {
+      fprintf (stderr, "WARNING: N out of bounds (2)\n");
+    }
+
+    if (N >= Nlensing) {
+      fprintf (stderr, "overflow in init_lensing_links\n");
+      abort ();
+    }
+    N++;
+  }
+  return (next_lens);
+}
+
+/* construct lensing links which are valid FOR THIS LOAD
+ * - if we have a full load, we will get links which can
+ *   be used by other programs (eg, relphot, etc)
+ * - if we have a partial load, the links are only valid
+ *   for that partial load
+ */ 
+
+off_t *build_lensing_links (Average *average, off_t Naverage, Lensing *lensing, off_t Nlensing) {
+
+  off_t i, m, k, Nm, averef;
+  off_t *next_lens;
+
+  ALLOCATE (next_lens, off_t, Nlensing);
+
+  /* reset the Nm, offset values for average */
+  for (i = 0; i < Naverage; i++) {
+    average[i].lensingOffset = -1;
+    average[i].Nlensing     =  0;
+  }
+
+  for (Nm = 0; Nm < Nlensing; Nm++) {
+    averef = lensing[Nm].averef;
+    m = average[averef].lensingOffset;  
+    next_lens[Nm] = -1;
+
+    if (m == -1) { /* no links yet for source */
+      average[averef].lensingOffset = Nm;
+      average[averef].Nlensing     = 1;
+      continue;
+    }
+
+    for (k = 0; next_lens[m] != -1; k++) {
+      m = next_lens[m];
+      if (m >= Nlensing) {
+	fprintf (stderr, "WARNING: m out of bounds (1)\n");
+      }
+    }
+
+    average[averef].Nlensing = k + 2;
+    next_lens[m] = Nm;
+    if (m >= Nlensing) {
+      fprintf (stderr, "WARNING: m out of bounds (2)\n");
+    }
+  }
+  return (next_lens);
+}
+
+/* average[].lensingOffset, average[].Nlensing are valid within an addstar run */
+int add_lens_link (Average *average, off_t *next_lens, off_t Nlensing, off_t NLENSING) {
+
+  off_t k, m;
+
+  /* if we have trouble, check validity of next_lens[m] : m < Nlensing */
+  m = average[0].lensingOffset;  
+
+  for (k = 0; k < average[0].Nlensing - 1; k++)  {
+    m = next_lens[m];
+    if (m >= NLENSING) {
+      fprintf (stderr, "WARNING: m out of bounds (3)\n");
+    }
+  }
+
+  /* set up references */
+  next_lens[Nlensing] = -1;
+  if (Nlensing >= NLENSING) {
+    fprintf (stderr, "WARNING: Nlensing out of bounds (1)\n");
+  }
+
+  if (m == -1) {
+    average[0].lensingOffset = Nlensing;
+  } else {
+    next_lens[m] = Nlensing;
+    if (m >= NLENSING) {
+      fprintf (stderr, "WARNING: m out of bounds (4)\n");
+    }
+  }
+
+  return (TRUE);
+}
+
+Lensing *sort_lensing (Average *average, off_t Naverage, Lensing *lensing, off_t Nlensing, off_t *next_lens) {
+
+  off_t i, k, n, N;
+  Lensing *tmplensing;
+
+  /* fix order of Lensing (memory intensive, but fast) */
+  N = 0; 
+  ALLOCATE (tmplensing, Lensing, Nlensing);
+  for (i = 0; i < Naverage; i++) {
+    if (!average[i].Nlensing) continue;
+    n = average[i].lensingOffset;
+    average[i].lensingOffset = N;
+    for (k = 0; k < average[i].Nlensing; k++, N++) {
+      if (n == -1) abort();
+      tmplensing[N] = lensing[n]; 
+      if (lensing[n].averef != i) abort();
+      tmplensing[N].averef = i;
+      n = next_lens[n];
+    }
+  }
+  free (lensing);
+  return (tmplensing);
+}
+
+
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeContinue.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeContinue.c	(revision 37083)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeContinue.c	(revision 37084)
@@ -131,6 +131,4 @@
       merge_catalogs_old (&outsky[0].regions[j], &outcatalog, &incatalog, RADIUS, secfiltMap);
 
-      outcatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
-
       // if we receive a signal which would cause us to exit, wait until the full catalog is written
       SetProtect (TRUE);
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeContinue_threaded.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeContinue_threaded.c	(revision 37083)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeContinue_threaded.c	(revision 37084)
@@ -74,6 +74,4 @@
       merge_catalogs_old (&threadData->outsky->regions[j], &outcatalog, &incatalog, RADIUS, threadData->secfiltMap);
 
-      outcatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
-
       // if we receive a signal which would cause us to exit, wait until the full catalog is written
       SetProtect (TRUE);
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeCreate.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeCreate.c	(revision 37083)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeCreate.c	(revision 37084)
@@ -178,5 +178,4 @@
     SkyListFree (inlist);
 
-    outcatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
     dvo_catalog_save (&outcatalog, VERBOSE);
     dvo_catalog_unlock (&outcatalog);
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeFromList.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeFromList.c	(revision 37083)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeFromList.c	(revision 37084)
@@ -143,6 +143,4 @@
     merge_catalogs_old (&skyregion, &outcatalog, &incatalog, RADIUS, secfiltMap);
     
-    outcatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
-
     dmhObjectAdd (outstat[0].history, &outcatalog.header, inStats);
 
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeUpdate_catalogs.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeUpdate_catalogs.c	(revision 37083)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeUpdate_catalogs.c	(revision 37084)
@@ -165,6 +165,4 @@
       merge_catalogs_old (outlist[0].regions[j], &outcatalog, &incatalog, RADIUS, secfiltMap);
 
-      outcatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
-
       if (outstat[j].missed) {
 	dmhObjectAdd (outstat[j].history, &outcatalog.header, inStats);
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeUpdate_threaded.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeUpdate_threaded.c	(revision 37083)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/dvomergeUpdate_threaded.c	(revision 37084)
@@ -72,6 +72,4 @@
       merge_catalogs_old (&threadData->outsky->regions[j], &outcatalog, &incatalog, RADIUS, threadData->secfiltMap);
 
-      outcatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
-
       // if we receive a signal which would cause us to exit, wait until the full catalog is written
       SetProtect (TRUE);
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/merge_catalogs_new.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/merge_catalogs_new.c	(revision 37083)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/merge_catalogs_new.c	(revision 37084)
@@ -8,10 +8,8 @@
 // input entries always define new objects
 
-#define notyet 1
-
 int merge_catalogs_new (SkyRegion *region, Catalog *output, Catalog *input, int *secfiltMap) {
   
   off_t i, j, offset;
-  off_t NAVERAGE, NMEASURE, Naverage, Nmeasure, NsecfiltIn, NsecfiltOut, Nm;
+  off_t NAVERAGE, NMEASURE, NLENSING, Naverage, Nmeasure, Nlensing, NsecfiltIn, NsecfiltOut, Nm;
 
   Naverage = output[0].Naverage;
@@ -63,4 +61,21 @@
       }
       output[0].average[Naverage].Nmeasure = Nm;
+
+      Nm = 0;
+      for (j = 0; j < input[0].average[i].Nlensing; j++) {
+	  offset = input[0].average[i].lensingOffset + j;
+
+	  output[0].lensing[Nlensing] = input[0].lensing[offset];
+	  output[0].lensing[Nlensing].averef = Naverage;
+
+	  Nlensing ++;
+	  Nm ++;
+	  if (Nlensing == NLENSING) {
+	      NLENSING += 1000;
+	      REALLOCATE (output[0].lensing, Lensing, NLENSING);
+	  }
+      }
+      output[0].average[Naverage].Nlensing = Nm;
+
       Naverage ++;
       if (Naverage == NAVERAGE) {
@@ -72,7 +87,8 @@
   REALLOCATE (output[0].average, Average, MAX (Naverage, 1));
   REALLOCATE (output[0].measure, Measure, MAX (Nmeasure, 1));
+  REALLOCATE (output[0].lensing, Lensing, MAX (Nlensing, 1));
   REALLOCATE (output[0].secfilt, SecFilt, NsecfiltOut*MAX (Naverage, 1));
   output[0].Naverage = Naverage;
-  output[0].Nmeasure = Nmeasure;
+  output[0].Nlensing = Nlensing;
   output[0].Nsecf_mem = Naverage * NsecfiltOut;
 
@@ -84,8 +100,9 @@
   
   if (VERBOSE) {
-      fprintf (stderr, OFF_T_FMT": using "OFF_T_FMT" stars ("OFF_T_FMT" measures) for catalog\n", 
+      fprintf (stderr, OFF_T_FMT": using "OFF_T_FMT" stars ("OFF_T_FMT" measures, "OFF_T_FMT" lensing) for catalog\n", 
 	        i, 
 	        output[0].Naverage, 
-	        output[0].Nmeasure);
+	        output[0].Nmeasure, 
+	        output[0].Nlensing);
   }
   return (TRUE);
Index: /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/merge_catalogs_old.c
===================================================================
--- /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/merge_catalogs_old.c	(revision 37083)
+++ /branches/eam_branches/ipp-20140717/Ohana/src/dvomerge/src/merge_catalogs_old.c	(revision 37084)
@@ -14,6 +14,6 @@
   double *X1, *Y1, *X2, *Y2;
   double dX, dY, dR;
-  off_t *N1, *N2,  *next_meas;
-  off_t Nave, NAVE, Nmeas, NMEAS, Nmatch;
+  off_t *N1, *N2, *next_meas, *next_lens;
+  off_t Nave, NAVE, Nmeas, NMEAS, Nmatch, Nlens, NLENS;
   int NsecfiltIn;
   int NsecfiltOut;
@@ -52,4 +52,5 @@
   Nmatch = 0;
   NMEAS = Nmeas = output[0].Nmeasure;
+  NLENS = Nlens = output[0].Nlensing;
 
   // current max obj ID for this catalog
@@ -113,5 +114,4 @@
   }
 
-
   /* set up pointers for linked list of measure */
   if (output[0].sorted && (output[0].Nmeasure >= output[0].Nmeas_disk)) {
@@ -119,6 +119,8 @@
     // is sorted while processed
     next_meas = init_measure_links (output[0].average, Nave, output[0].measure, Nmeas);
+    next_lens = init_lensing_links (output[0].average, Nave, output[0].lensing, Nlens);
   } else {
     next_meas = build_measure_links (output[0].average, Nave, output[0].measure, Nmeas);
+    next_lens = build_lensing_links (output[0].average, Nave, output[0].lensing, Nlens);
   }    
 
@@ -182,4 +184,9 @@
       REALLOCATE (output[0].measure, Measure, NMEAS);
     }
+    if (Nlens + input[0].average[N].Nmeasure >= NLENS) {
+      NLENS = Nlens + input[0].average[N].Nmeasure + 1000;
+      REALLOCATE (next_lens, off_t, NLENS);
+      REALLOCATE (output[0].lensing, Lensing, NLENS);
+    }
 
     // 4) average properties from the input and the output db need to be properly merged.
@@ -191,4 +198,5 @@
       if (REPLACE_BY_PHOTCODE) {
 	// index to first measure for this object
+	// XXX this does not support lensing measurements
 	int Mout = output[0].average[n].measureOffset;  
 	if (replace_match(&output[0].average[n], &output[0].measure[Mout], &input[0].average[N], &input[0].measure[offset])) {
@@ -248,4 +256,21 @@
     }
 
+    // if lensing measurements exist, add them too
+    if (output[0].lensing) {
+      for (Nin = 0; Nin < input[0].average[N].Nlensing; Nin++) {
+	/* add to end of lensing list */
+	add_lens_link (&output[0].average[n], next_lens, Nlens, NLENS);
+	
+	// set the new lensing
+	off_t lensoff = input[0].average[N].lensingOffset + Nin;
+	output[0].lensing[Nlens] = input[0].lensing[lensoff];
+
+	output[0].lensing[Nlens].averef   = n;
+	output[0].lensing[Nlens].objID    = output[0].average[n].objID;
+	output[0].lensing[Nlens].catID    = output[0].catID;
+	Nlens ++;
+      }
+    }
+
     // update the average properties to reflect the incoming entries:
     // if the original value is NAN but the input value is not, accept the input:
@@ -288,4 +313,9 @@
       REALLOCATE (output[0].measure, Measure, NMEAS);
     }
+    if (Nlens + input[0].average[N].Nlensing >= NLENS) {
+      NLENS = Nlens + input[0].average[N].Nlensing + 1000;
+      REALLOCATE (next_lens, off_t, NLENS);
+      REALLOCATE (output[0].lensing, Lensing, NLENS);
+    }
     if (Nave >= NAVE) {
       NAVE = Nave + 1000;
@@ -355,4 +385,30 @@
       next_meas[Nmeas - Ngroup + j] = Nmeas - Ngroup + j + 1;
     }
+
+    /** add lensing for this input average object **/
+    if (output[0].lensing) {
+      for (Nin = 0; Nin < input[0].average[N].Nlensing; Nin ++) {
+	// supply the lensing values from this detection
+	off_t lensoff = input[0].average[N].lensingOffset + Nin;
+	output[0].lensing[Nlens]           = input[0].lensing[lensoff];
+
+	// the following lensing elements cannot be set until here:
+	output[0].lensing[Nlens].averef   = Nave;
+	output[0].lensing[Nlens].objID    = output[0].average[Nave].objID;
+	output[0].lensing[Nlens].catID    = output[0].catID;
+
+	// as we add lensing, update Nlensing to match
+	output[0].average[Nave].Nlensing ++;
+
+	/* we set next[Nlens] to -1 here, and update correctly below */
+	next_lens[Nlens] = -1;
+	Nlens ++;
+      }
+      int Ngroup = input[0].average[N].Nlensing;
+      for (j = 0; j < Ngroup - 1; j++) {
+	next_lens[Nlens - Ngroup + j] = Nlens - Ngroup + j + 1;
+      }
+    }
+
     Nave ++;
   }
@@ -362,4 +418,5 @@
   REALLOCATE (output[0].average, Average, Nave);
   REALLOCATE (output[0].measure, Measure, Nmeas);
+  REALLOCATE (output[0].lensing, Lensing, Nlens);
  
 # define NOSORT 0
@@ -369,4 +426,5 @@
     output[0].sorted = TRUE;
     output[0].measure = sort_measure (output[0].average, Nave, output[0].measure, Nmeas, next_meas);
+    output[0].lensing = sort_lensing (output[0].average, Nave, output[0].lensing, Nlens, next_lens);
   }
 
@@ -384,8 +442,10 @@
   output[0].Naverage = Nave;
   output[0].Nmeasure = Nmeas;
+  output[0].Nlensing = Nlens;
   output[0].Nsecf_mem = Nave*NsecfiltOut;
-  if (VERBOSE) fprintf (stderr, "Nstars, Nave, Nmeas: "OFF_T_FMT" "OFF_T_FMT" "OFF_T_FMT", ("OFF_T_FMT" matches)\n",  Nstars,  Nave,  Nmeas,  Nmatch);
+  if (VERBOSE) fprintf (stderr, "Nstars, Nave, Nmeas, Nlens: "OFF_T_FMT" "OFF_T_FMT" "OFF_T_FMT" "OFF_T_FMT", ("OFF_T_FMT" matches)\n",  Nstars,  Nave,  Nmeas,  Nlens, Nmatch);
 
   free (next_meas);
+  free (next_lens);
 
   free (X2);
