Index: /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/build_links.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/build_links.c	(revision 36921)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/build_links.c	(revision 36922)
@@ -237,5 +237,6 @@
 
   ALLOCATE (next_lens, off_t, Nlensing);
-  for (i = 0; i < Naverage; i++, N++) {
+  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;
@@ -253,4 +254,5 @@
       abort ();
     }
+    N++;
   }
   return (next_lens);
@@ -346,4 +348,5 @@
   ALLOCATE (tmplensing, Lensing, Nlensing);
   for (i = 0; i < Naverage; i++) {
+    if (!average[i].Nlensing) continue;
     n = average[i].lensingOffset;
     average[i].lensingOffset = N;
Index: /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/find_matches_closest.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/find_matches_closest.c	(revision 36921)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/find_matches_closest.c	(revision 36922)
@@ -182,5 +182,5 @@
     if (Nlens >= NLENS) {
       NLENS = Nlens + 1000;
-      // REALLOCATE (next_lens, off_t, NLENS);
+      REALLOCATE (next_lens, off_t, NLENS);
       REALLOCATE (catalog[0].lensing, Lensing, NLENS);
     }
@@ -188,5 +188,4 @@
     /* add to end of measurement list */
     add_meas_link (&catalog[0].average[n], next_meas, Nmeas, NMEAS);
-    add_lens_link (&catalog[0].average[n], next_lens, Nlens, NLENS); // ?
 	
     /** add measurements for this star **/
@@ -232,4 +231,5 @@
     // add the lensing values if they exist
     if (stars[N].lensing) {
+      add_lens_link (&catalog[0].average[n], next_lens, Nlens, NLENS); // ?
       catalog[0].lensing[Nlens] = stars[N].lensing[0];
       
Index: /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/resort_catalog.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/resort_catalog.c	(revision 36921)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/resort_catalog.c	(revision 36922)
@@ -1,3 +1,7 @@
 # include "addstar.h"
+
+void SortAveMatch (off_t *MEAS, off_t *AVE, off_t N);
+void resort_catalog_measure (Catalog *catalog);
+void resort_catalog_lensing (Catalog *catalog);
 
 void resort_catalog_old (Catalog *catalog) {
@@ -27,17 +31,4 @@
 
   return;
-}
-
-// sort the measure Sequence based on the average Sequence entries
-void SortAveMeasMatch (off_t *MEAS, off_t *AVE, off_t N) {
-
-# define SWAPFUNC(A,B){ off_t tmp_meas; off_t tmp_ave;		\
-    tmp_meas = MEAS[A]; MEAS[A] = MEAS[B]; MEAS[B] = tmp_meas;	\
-    tmp_ave  = AVE[A];  AVE[A]  = AVE[B];  AVE[B]  = tmp_ave;	\
-  }
-# define COMPARE(A,B)(AVE[A] < AVE[B])
-  OHANA_SORT (N, COMPARE, SWAPFUNC);
-# undef SWAPFUNC
-# undef COMPARE
 }
 
@@ -53,4 +44,12 @@
 void resort_catalog (Catalog *catalog) {
 
+  if (catalog[0].sorted == TRUE) return;
+
+  resort_catalog_measure (catalog);
+  resort_catalog_lensing (catalog);
+}
+
+void resort_catalog_measure (Catalog *catalog) {
+
   off_t Naverage, Nmeasure;
   Measure *measure;
@@ -62,6 +61,4 @@
   Measure *measureTMP = NULL;
 
-  if (catalog[0].sorted == TRUE) return;
-
   // struct timeval start, stop;
   // gettimeofday (&start, NULL);
@@ -70,4 +67,6 @@
   Nmeasure = catalog[0].Nmeasure;
   Naverage = catalog[0].Naverage;
+
+  if (!Nmeasure) return;
 
   measure = catalog[0].measure;
@@ -113,5 +112,5 @@
   // fprintf (stderr, "\n");
 
-  SortAveMeasMatch(measureSeq, averageSeq, Nmeasure);
+  SortAveMatch(measureSeq, averageSeq, Nmeasure);
   // MARKTIME("sort : %f sec\n", dtime);
 
@@ -188,2 +187,152 @@
 }
 
+void resort_catalog_lensing (Catalog *catalog) {
+
+  off_t Naverage, Nlensing;
+  Lensing *lensing;
+  Average *average;
+  off_t i, j, N, currentAve;
+
+  off_t *lensingSeq = NULL;
+  off_t *averageSeq = NULL;
+  Lensing *lensingTMP = NULL;
+
+  // struct timeval start, stop;
+  // gettimeofday (&start, NULL);
+
+  /* internal counters */
+  Nlensing = catalog[0].Nlensing;
+  Naverage = catalog[0].Naverage;
+
+  if (!Nlensing) return;
+
+  lensing = catalog[0].lensing;
+  average = catalog[0].average;
+  
+  // we have a table of average objects and an unsorted table of measurements.  each measurement
+  // has a reference to the average object sequence (as well as an ID)
+  // lensing[i].averef -> average[averef]
+  // lensing[i].objID = average[averef].objID
+  // lensing[i].catID = average[averef].catID
+
+  // we want a sorted lensing array with all averef entries in sequence
+
+  ALLOCATE (lensingSeq, off_t,   Nlensing);
+  ALLOCATE (averageSeq, off_t,   Nlensing);
+
+  for (i = 0; i < Nlensing; i++) {
+    lensingSeq[i] = i;
+    averageSeq[i] = lensing[i].averef;
+    
+    if (catalog[0].catformat >= DVO_FORMAT_PS1_V1) {
+      // earlier formats did not carry the objID or catID, so they are not available (we could assign on load, but we don't)
+      myAssert(average[averageSeq[i]].catID == lensing[lensingSeq[i]].catID, "object / detection mismatch");
+# if (1)
+      myAssert(average[averageSeq[i]].objID == lensing[lensingSeq[i]].objID, "object / detection mismatch");
+# else
+      // for reasons I do not understand, the mini dvodbs generated on stsci1X had a handful of detections with an inconsistency between averef and objID.  
+      // this happened for 28 detections in the dbs on /data/stsci1?.0/eugene/dvo3pi.20130616, but not at all (as far as I know) in the rest of LAP DVO
+      if (average[averageSeq[i]].objID != lensing[lensingSeq[i]].objID) {
+	fprintf (stderr, "R");
+	lensing[lensingSeq[i]].objID = average[averageSeq[i]].objID; // XXX I don't really like this...
+      }
+# endif
+    }
+  }
+  
+  // check that averageSeq is now in order
+  // for (i = 1; i < Nlensing; i++) {
+  //   if (averageSeq[i] < averageSeq[i-1]) {
+  //     fprintf (stderr, "%d ", (int) i);
+  //   }
+  // }
+  // fprintf (stderr, "\n");
+
+  SortAveMatch(lensingSeq, averageSeq, Nlensing);
+  // MARKTIME("sort : %f sec\n", dtime);
+
+  // check that averageSeq is now in order
+  // for (i = 1; i < Nlensing; i++) {
+  //   if (averageSeq[i] < averageSeq[i-1]) {
+  //     fprintf (stderr, "%d ", (int) i);
+  //   }
+  // }
+  // fprintf (stderr, "\n");
+
+  // copy the lensing entries in the sorted order
+  ALLOCATE (lensingTMP, Lensing, Nlensing);
+  for (i = 0; i < Nlensing; i++) {
+    j = lensingSeq[i];
+    lensingTMP[i] = lensing[j];
+  }
+  // MARKTIME("assign lensing : %f sec\n", dtime);
+
+  // update the values of average.lensingOffset and average.Nlensing
+  FREE(lensing);
+  catalog[0].lensing = lensingTMP;
+
+  N = 0;
+  currentAve = averageSeq[0];
+  average[currentAve].lensingOffset = 0;
+  for (i = 0; i < Nlensing; i++) {
+    if (averageSeq[i] != currentAve) {
+      // we have hit the next entry in the list
+      average[currentAve].Nlensing = N;
+      N = 0;
+      currentAve = averageSeq[i];
+      average[currentAve].lensingOffset = i;
+    }
+    N++;
+  }
+  // N++;
+  average[currentAve].Nlensing = N;
+  // MARKTIME("update Nlensing : %f sec\n", dtime);
+
+  int NlensingTotal = 0;
+  int lensingOffsetOK = TRUE;
+  for (i = 0; i < Naverage; i++) {
+    NlensingTotal += catalog[0].average[i].Nlensing;
+    if (VERBOSE && !(NlensingTotal <= catalog[0].Nlensing)) {
+      fprintf (stderr, "too few lensing: %d %d %d\n", (int) i, NlensingTotal, (int) catalog[0].Nlensing);
+    }
+    lensingOffsetOK &= (catalog[0].average[i].lensingOffset < catalog[0].Nlensing);
+    if (VERBOSE && !(catalog[0].average[i].lensingOffset < catalog[0].Nlensing)) {
+      fprintf (stderr, "offset too large: %d %d %d\n", (int) i, catalog[0].average[i].Nlensing, (int) catalog[0].Nlensing);
+    }
+    lensingOffsetOK &= (catalog[0].average[i].lensingOffset + catalog[0].average[i].Nlensing <= catalog[0].Nlensing);
+    if (VERBOSE && !(catalog[0].average[i].lensingOffset + catalog[0].average[i].Nlensing <= catalog[0].Nlensing)) {
+      fprintf (stderr, "orrset + Nlensing too large: %d + %d > %d %d\n", (int) i, catalog[0].average[i].lensingOffset, catalog[0].average[i].Nlensing, (int) catalog[0].Nlensing);
+    }
+  }
+
+  if (!lensingOffsetOK) {
+    fprintf (stderr, "ERROR: catalog %s has an invalid lensingOffset\n", catalog[0].filename);
+  }
+
+  if (NlensingTotal != catalog[0].Nlensing) {
+    fprintf (stderr, "ERROR: catalog %s has an invalid Nlensing\n", catalog[0].filename);
+  }
+
+  // MARKTIME("  match time %9.4f sec for %7lld lensings, %6lld average\n", dtime, (long long) Nlensing, (long long) Naverage);
+
+  catalog[0].sorted = TRUE;
+
+  FREE (lensingSeq);
+  FREE (averageSeq);
+
+  return;
+}
+
+// sort the measure or lensing Sequence based on the average Sequence entries
+void SortAveMatch (off_t *MEAS, off_t *AVE, off_t N) {
+
+# define SWAPFUNC(A,B){ off_t tmp_meas; off_t tmp_ave;		\
+    tmp_meas = MEAS[A]; MEAS[A] = MEAS[B]; MEAS[B] = tmp_meas;	\
+    tmp_ave  = AVE[A];  AVE[A]  = AVE[B];  AVE[B]  = tmp_ave;	\
+  }
+# define COMPARE(A,B)(AVE[A] < AVE[B])
+  OHANA_SORT (N, COMPARE, SWAPFUNC);
+# undef SWAPFUNC
+# undef COMPARE
+}
+
Index: /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/resort_threaded.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/resort_threaded.c	(revision 36921)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/resort_threaded.c	(revision 36922)
@@ -41,5 +41,5 @@
     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;
+    catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_LENSING;
     catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
   
Index: /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/resort_unthreaded_catalogs.c
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/resort_unthreaded_catalogs.c	(revision 36921)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/resort_unthreaded_catalogs.c	(revision 36922)
@@ -30,5 +30,5 @@
     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;
+    catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_LENSING;
     catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
 
Index: /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h	(revision 36921)
+++ /branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h	(revision 36922)
@@ -606,5 +606,5 @@
   char catmode;				/* storage mode (raw, mef, split, mysql) */
   char catformat;			/* storage format (elixir, panstarrs, etc) */
-  char sorted;				/* is measure table average-sorted? */
+  int sorted;				/* is measure table average-sorted? (NOTE this is an int only because gfits_scan %t requires it) */
   
   short catflags; /* choices to be loaded */
