Index: branches/eam_branches/ipp-20140610/Ohana/src/addstar/include/addstar.h
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/addstar/include/addstar.h	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/addstar/include/addstar.h	(revision 36921)
@@ -251,10 +251,14 @@
 void       update_coords          PROTO((Average *average, Measure *measure, off_t *next));
 off_t 	  *init_measure_links     PROTO((Average *average, off_t Naverage, Measure *measure, off_t Nmeasure));
+off_t 	  *init_lensing_links     PROTO((Average *average, off_t Naverage, Lensing *lensing, off_t Nlensing));
 off_t 	  *init_missing_links     PROTO((Average *average, off_t Naverage, Missing *missing, off_t Nmissing));
 off_t 	   add_meas_link     	  PROTO((Average *average, off_t *next_meas, off_t Nmeasure, off_t NMEASURE));
 off_t 	   add_miss_link     	  PROTO((Average *average, off_t *next_miss, off_t Nmissing));
+off_t      add_lens_link          PROTO((Average *average, off_t *next_lens, off_t Nlensing, off_t NLENSING));
 off_t 	  *build_measure_links    PROTO((Average *average, off_t Naverage, Measure *measure, off_t Nmeasure));
+off_t     *build_lensing_links    PROTO((Average *average, off_t Naverage, Lensing *lensing, off_t Nlensing));
 Measure   *sort_measure     	  PROTO((Average *average, off_t Naverage, Measure *measure, off_t Nmeasure, off_t *next_meas));
 Missing   *sort_missing     	  PROTO((Average *average, off_t Naverage, Missing *missing, off_t Nmissing, off_t *next_miss));
+Lensing   *sort_lensing           PROTO((Average *average, off_t Naverage, Lensing *lensing, off_t Nlensing, off_t *next_lens));
 int        ImageOptions		  PROTO((AddstarClientOptions *options, Image *images, off_t Nimages));
 int        GetFileMode		  PROTO((Header *header));
Index: branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/ReadStarsFITS.c
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 36921)
@@ -78,5 +78,5 @@
   }
   if (!strcmp (type, "PS1_V5")) {
-    if (table.header[0].Naxis[0] == 296) {
+    if (table.header[0].Naxis[0] == 312) {
       stars = Convert_PS1_V5_Lensing (&table, &Nstars);
     } else {
@@ -901,4 +901,6 @@
 
     ALLOCATE (stars[i].lensing, Lensing, 1);
+    dvo_lensing_init (stars[i].lensing);
+
     stars[i].lensing->X11_sm_obj  = ps1data[i].X11_sm_obj;
     stars[i].lensing->X12_sm_obj  = ps1data[i].X12_sm_obj;
Index: branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/addstar.c
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/addstar.c	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/addstar.c	(revision 36921)
@@ -7,5 +7,5 @@
   int Nmatch, status, loadObjects;
   off_t i, Nimages;
-  off_t Naverage, Nmeasure;
+  off_t Naverage, Nmeasure, Nlensing;
   Stars *stars, **subset;
   Image *images;
@@ -100,5 +100,5 @@
 
   /* match stars to existing catalog data (or otherwise manipulate catalog data) */
-  Nmatch = Naverage = Nmeasure = 0;
+  Nmatch = Naverage = Nmeasure = Nlensing = 0;
   for (i = 0; loadObjects && (i < skylist[0].Nregions); i++) {
 
@@ -107,5 +107,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 | LOAD_MISS | LOAD_SECF;
+    catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF | LOAD_LENSING;
     catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
 
@@ -182,4 +182,5 @@
     Naverage += catalog.Naverage;
     Nmeasure += catalog.Nmeasure;
+    Nlensing += catalog.Nlensing;
 
     // write out catalog, if appropriate
@@ -187,5 +188,5 @@
       SetProtect (TRUE);
       if (options.update) {
-	catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
+	catalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF | LOAD_LENSING;
 	dvo_catalog_update (&catalog, VERBOSE);
       } else {
@@ -231,5 +232,5 @@
   gettimeofday (&stop, NULL);
   dtime = DTIME (stop, start);
-  fprintf (stderr, "SUCCESS: elapsed time %9.4f sec for %5d stars (%5d matches), "OFF_T_FMT" average, "OFF_T_FMT" measure\n", dtime, Nstars, Nmatch,  Naverage,  Nmeasure);
+  fprintf (stderr, "SUCCESS: elapsed time %9.4f sec for %5d stars (%5d matches), "OFF_T_FMT" average, "OFF_T_FMT" measure, "OFF_T_FMT" lensing\n", dtime, Nstars, Nmatch,  Naverage,  Nmeasure, Nlensing);
 
   exit (0);
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 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/build_links.c	(revision 36921)
@@ -225,2 +225,138 @@
 }
 
+/*******************************************************************************************/
+
+/* 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++, N++) {
+    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 ();
+    }
+  }
+  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 */
+off_t 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++) {
+    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-20140610/Ohana/src/addstar/src/find_matches_closest.c
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/find_matches_closest.c	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/find_matches_closest.c	(revision 36921)
@@ -7,6 +7,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 Nsecfilt, Nsec;
   unsigned int objID, catID;
@@ -42,4 +42,5 @@
   Nmatch = 0;
   NMEAS = Nmeas = catalog[0].Nmeasure;
+  NLENS = Nlens = catalog[0].Nlensing;
 
   // current max obj ID for this catalog
@@ -100,6 +101,8 @@
     // is sorted while processed
     next_meas = init_measure_links (catalog[0].average, Nave, catalog[0].measure, Nmeas);
+    next_lens = init_lensing_links (catalog[0].average, Nave, catalog[0].lensing, Nlens);
   } else {
     next_meas = build_measure_links (catalog[0].average, Nave, catalog[0].measure, Nmeas);
+    next_lens = build_lensing_links (catalog[0].average, Nave, catalog[0].lensing, Nlens);
   }    
 
@@ -177,7 +180,13 @@
       REALLOCATE (catalog[0].measure, Measure, NMEAS);
     }
+    if (Nlens >= NLENS) {
+      NLENS = Nlens + 1000;
+      // REALLOCATE (next_lens, off_t, NLENS);
+      REALLOCATE (catalog[0].lensing, Lensing, NLENS);
+    }
 
     /* 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 **/
@@ -186,5 +195,6 @@
     catalog[0].measure[Nmeas]          = stars[N].measure;
 
-    /** dR,dD now represent arcsec **/
+    // measure now carries R,D (not dR,dD) 
+    // note that ReadStarsFITS does not set measure.R,D and average.R,D
     catalog[0].measure[Nmeas].R        = stars[N].average.R;
     catalog[0].measure[Nmeas].D        = stars[N].average.D;
@@ -220,4 +230,15 @@
     }
 
+    // add the lensing values if they exist
+    if (stars[N].lensing) {
+      catalog[0].lensing[Nlens] = stars[N].lensing[0];
+      
+      catalog[0].lensing[Nlens].averef = n;
+      catalog[0].lensing[Nlens].objID = catalog[0].average[n].objID;
+      catalog[0].lensing[Nlens].catID = catalog[0].catID;
+      catalog[0].average[n].Nlensing ++;
+      Nlens ++;
+    }
+
     /* adds the measurement to the calibration if appropriate color terms are found */
     /* we call this before (optionally) setting the average magnitude to avoid auto-correlations */
@@ -255,4 +276,9 @@
       REALLOCATE (catalog[0].measure, Measure, NMEAS);
     }
+    if (Nlens >= NLENS - NSTAR_GROUP) {
+      NLENS = Nlens + 1000;
+      REALLOCATE (next_lens, off_t, NLENS);
+      REALLOCATE (catalog[0].lensing, Lensing, NLENS);
+    }
     if (Nave >= NAVE) {
       NAVE = Nave + 1000;
@@ -284,30 +310,54 @@
 
     for (j = 0; j < NSTAR_GROUP; j++) {
-        // supply the measurments from this detection
-        catalog[0].measure[Nmeas]           = stars[i + j].measure;
-
-        // the following measure elements cannot be set until here:
-        catalog[0].measure[Nmeas].R        = stars[i].average.R;
-        catalog[0].measure[Nmeas].D        = stars[i].average.D;
-        catalog[0].measure[Nmeas].dbFlags  = 0;
-        catalog[0].measure[Nmeas].averef   = Nave;
-        catalog[0].measure[Nmeas].objID    = catalog[0].average[Nave].objID;
-        catalog[0].measure[Nmeas].catID    = catalog[0].catID;
-
-        /* set the average magnitude if not already set and the photcode.equiv is not 0 */
-        /* in UPDATE mode, this value is not saved; use relphot to recalculate */
-        if (Nsec > -1) { 
-            catalog[0].secfilt[Nave*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas], MAG_CLASS_PSF);
-        }
-
-        /* next[Nmeas] should always be -1 in this context (it is always the only
-           measurement for the star) */
-        stars[i+j].found = Nmeas;
-        next_meas[Nmeas] = -1;  // inital value here update below
-        Nmeas ++;
+      // supply the measurments from this detection
+      dvo_measure_init (&catalog[0].measure[Nmeas]);
+      catalog[0].measure[Nmeas]           = stars[i + j].measure;
+
+      // the following measure elements cannot be set until here:
+      catalog[0].measure[Nmeas].R        = stars[i].average.R;
+      catalog[0].measure[Nmeas].D        = stars[i].average.D;
+      catalog[0].measure[Nmeas].dbFlags  = 0;
+      catalog[0].measure[Nmeas].averef   = Nave;
+      catalog[0].measure[Nmeas].objID    = catalog[0].average[Nave].objID;
+      catalog[0].measure[Nmeas].catID    = catalog[0].catID;
+
+      /* set the average magnitude if not already set and the photcode.equiv is not 0 */
+      /* in UPDATE mode, this value is not saved; use relphot to recalculate */
+      if (Nsec > -1) { 
+	catalog[0].secfilt[Nave*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas], MAG_CLASS_PSF);
+      }
+
+      /* next[Nmeas] should always be -1 in this context (it is always the only
+	 measurement for the star) */
+      stars[i+j].found = Nmeas;
+      next_meas[Nmeas] = -1;  // inital value here update below
+      Nmeas ++;
     }
     for (j = 0; j < NSTAR_GROUP - 1; j++) {
-        next_meas[Nmeas - NSTAR_GROUP + j] = Nmeas - NSTAR_GROUP + j + 1;
-    }
+      next_meas[Nmeas - NSTAR_GROUP + j] = Nmeas - NSTAR_GROUP + j + 1;
+    }
+
+    // if we have lensing data, insert that as well
+    if (stars[i].lensing) {
+      catalog[0].average[Nave].Nlensing = NSTAR_GROUP;
+      catalog[0].average[Nave].lensingOffset = Nlens;
+      for (j = 0; j < NSTAR_GROUP; j++) {
+	// add the lensing values if they exist
+	if (stars[i + j].lensing) {
+	  dvo_lensing_init (&catalog[0].lensing[Nlens]);
+	  catalog[0].lensing[Nlens] = stars[i + j].lensing[0];
+	  
+	  catalog[0].lensing[Nlens].averef = Nave;
+	  catalog[0].lensing[Nlens].objID = catalog[0].average[Nave].objID;
+	  catalog[0].lensing[Nlens].catID = catalog[0].catID;
+	  next_lens[Nlens] = -1; 
+	  Nlens ++;
+	}
+      }
+      for (j = 0; j < NSTAR_GROUP - 1; j++) {
+	next_lens[Nlens - NSTAR_GROUP + j] = Nlens - NSTAR_GROUP + j + 1;
+      }
+    }
+
     Nave ++;
   }
@@ -315,4 +365,5 @@
   REALLOCATE (catalog[0].average, Average, Nave);
   REALLOCATE (catalog[0].measure, Measure, Nmeas);
+  REALLOCATE (catalog[0].lensing, Lensing, Nlens);
  
   if (options.nosort) {
@@ -321,4 +372,5 @@
     catalog[0].sorted = TRUE;
     catalog[0].measure = sort_measure (catalog[0].average, Nave, catalog[0].measure, Nmeas, next_meas);
+    catalog[0].lensing = sort_lensing (catalog[0].average, Nave, catalog[0].lensing, Nlens, next_lens);
   }
 
@@ -334,6 +386,7 @@
   catalog[0].Naverage = Nave;
   catalog[0].Nmeasure = Nmeas;
+  catalog[0].Nlensing = Nlens;
   catalog[0].Nsecf_mem = Nave*Nsecfilt;
-  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 (catalog[0].found);
@@ -345,4 +398,5 @@
   free (Y2);
   free (next_meas);
+  free (next_lens);
 
   return (Nmatch);
Index: branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/mkcmf.c
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/mkcmf.c	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/addstar/src/mkcmf.c	(revision 36921)
@@ -15,4 +15,5 @@
 void gauss_init (int Nbin);
 double rnd_gauss (double mean, double sigma);
+void writeStars_PS1_V5_Lensing (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars);
 void writeStars_PS1_V5 (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars);
 void writeStars_PS1_V4 (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars);
@@ -334,4 +335,8 @@
   if (!strcmp(type, "PS1_V5")) {
     writeStars_PS1_V5 (&ftable, X, Y, M, Flag, Nstars); 
+    found = TRUE;
+  }
+  if (!strcmp(type, "PS1_V5_Lensing")) {
+    writeStars_PS1_V5_Lensing (&ftable, X, Y, M, Flag, Nstars); 
     found = TRUE;
   }
@@ -880,2 +885,135 @@
 }
 
+void writeStars_PS1_V5_Lensing (FTable *ftable, double *X, double *Y, double *M, unsigned int *Flag, int Nstars) {
+
+  int i;
+  CMF_PS1_V5_Lensing *stars;
+  float flux, fSN;
+
+  // XXX add gaussian-distributed noise based on counts
+  // this needs to make different output 'stars' entries depending on the desired type
+  ALLOCATE (stars, CMF_PS1_V5_Lensing, Nstars);
+  for (i = 0; i < Nstars; i++) {
+
+    flux = pow (10.0, -0.4*M[i]);
+    fSN = 1.0 / sqrt(flux);
+
+    stars[i].detID = i;
+    stars[i].X = X[i];
+    stars[i].Y = Y[i];
+    stars[i].dX = FX * fSN;
+    stars[i].dY = FY * fSN;
+
+    stars[i].posangle = 10.0;
+    stars[i].pltscale = 0.25;
+
+    stars[i].M = M[i];
+    stars[i].dM = fSN;
+
+    stars[i].Flux = flux;
+    stars[i].dFlux = flux * fSN;
+
+    stars[i].Map = M[i] - 0.05;
+    stars[i].MapRaw = M[i] - 0.10;
+
+    stars[i].apRadius = 8.0;
+
+    stars[i].apFlux = pow(10.0, -0.4*stars[i].Map);
+    stars[i].apFluxErr = stars[i].apFlux * fSN;
+
+    stars[i].Mcalib = M[i] + ZERO_POINT + 2.5*log10(exptime);
+    stars[i].dMcal = 0.05;
+
+    XY_to_RD (&stars[i].RA, &stars[i].DEC, X[i], Y[i], &coords);
+    stars[i].apNpix = 3.14*8.0*8.0;
+
+    stars[i].Mpeak     = M[i] + 1.0;
+    stars[i].sky       = SKY;
+    stars[i].dSky      = DSKY;
+    stars[i].psfChisq  = PSFCHI;
+    stars[i].crNsigma  = CRN;
+    stars[i].extNsigma = EXTN;
+    stars[i].fx        = FX;
+    stars[i].fy        = FY;
+    stars[i].df        = DF;
+
+    stars[i].k         = 1.0;
+    stars[i].fwhmMaj   = FX*2.8;
+    stars[i].fwhmMin   = FY*2.8;
+
+    // randomly give poor PSFQF values
+    if ((BAD_PSFQF_FRAC > 0.0) && (drand48() < BAD_PSFQF_FRAC)) {
+      stars[i].psfQF     = 0.25;
+      stars[i].psfQFperf = 0.24;
+    } else {
+      stars[i].psfQF     = PSFQUAL;
+      stars[i].psfQFperf = MAX(PSFQUAL - 0.01, 0.0);
+    }
+
+    stars[i].psfNdof   = 1;
+    stars[i].psfNpix   = 2;
+
+    stars[i].Mxx       = FX;
+    stars[i].Mxy       = 0.01;
+    stars[i].Myy       = FX;
+    stars[i].M3c       = FX;
+    stars[i].M3s       = FX;
+    stars[i].M4c       = FX;
+    stars[i].M4s       = FX;
+    stars[i].Mr1       = FX;
+    stars[i].Mrh       = FX;
+
+    stars[i].X11_sm_obj = FX;
+    stars[i].X12_sm_obj = FX;
+    stars[i].X22_sm_obj = FY;
+    stars[i].E1_sm_obj  = FX;
+    stars[i].E2_sm_obj  = FX;
+
+    stars[i].X11_sh_obj = FX;
+    stars[i].X12_sh_obj = 0.2;
+    stars[i].X22_sh_obj = FY;
+    stars[i].E1_sh_obj  = FX*0.9;
+    stars[i].E2_sh_obj  = FX*0.8;
+
+    stars[i].X11_sm_psf = FX;
+    stars[i].X12_sm_psf = FX;
+    stars[i].X22_sm_psf = FX;
+    stars[i].E1_sm_psf  = FX;
+    stars[i].E2_sm_psf  = FX;
+
+    stars[i].X11_sh_psf = FX;
+    stars[i].X12_sh_psf = FX;
+    stars[i].X22_sh_psf = FX;
+    stars[i].E1_sh_psf  = FX;
+    stars[i].E2_sh_psf  = FX;
+
+    stars[i].kronFlux  = flux * 1.25;
+    stars[i].kronFluxErr = fSN * flux * 1.25;
+
+    stars[i].kronInner = fSN * flux * 0.9;
+    stars[i].kronOuter = fSN * flux * 1.5;
+
+    stars[i].skyLimitRad = 1;
+    stars[i].skyLimitFlux = 2;
+    stars[i].skyLimitSlope = 0.2;
+
+    stars[i].flags     = Flag[i];
+    stars[i].flags2    = 0x80;
+
+    stars[i].nFrames   = 1;
+
+    if (ADDNOISE) {
+      stars[i].X += FX * fSN * rnd_gauss(0.0, 1.0);
+      stars[i].Y += FY * fSN * rnd_gauss(0.0, 1.0);
+      float Moff = fSN*rnd_gauss(0.0, 1.0);
+      stars[i].M += Moff;
+      stars[i].Map += Moff;
+      stars[i].MapRaw += Moff;
+      stars[i].Mcalib += Moff;
+    }
+  }
+
+  gfits_table_set_CMF_PS1_V5_Lensing (ftable, stars, Nstars);
+  gfits_modify (ftable->header, "EXTTYPE",   "%s", 1, "PS1_V5");
+}
+
Index: branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/average-ps1-v5.d
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/average-ps1-v5.d	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/average-ps1-v5.d	(revision 36921)
@@ -43,9 +43,9 @@
 FIELD Nextend,        NEXTEND,     unsigned short,  number of extended measurements
 
-FIELD measureOffset,  OFF_MEASURE, uint32_t,   	    offset to first psf measurement
-FIELD missingOffset,  OFF_MISSING, uint32_t,   	    offset to first missing obs
-FIELD lensingOffset,  OFF_LENSING, uint32_t,   	    offset to first lensing obs
-FIELD lensobjOffset,  OFF_LENSOBJ, uint32_t,   	    offset to mean lensing data
-FIELD extendOffset,   OFF_EXTEND,  uint32_t,   	    offset to extended object entry
+FIELD measureOffset,  OFF_MEASURE, int,   	    offset to first psf measurement
+FIELD missingOffset,  OFF_MISSING, int,   	    offset to first missing obs
+FIELD lensingOffset,  OFF_LENSING, int,   	    offset to first lensing obs
+FIELD lensobjOffset,  OFF_LENSOBJ, int,   	    offset to mean lensing data
+FIELD extendOffset,   OFF_EXTEND,  int,   	    offset to extended object entry
 
 FIELD refColor,       REF_COLOR,   float,   	    color of reference stars
Index: branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/average.d
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/average.d	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/average.d	(revision 36921)
@@ -45,7 +45,7 @@
 FIELD measureOffset,  OFF_MEASURE, int,             offset to first psf measurement
 FIELD missingOffset,  OFF_MISSING, int,             offset to first missing obs
-FIELD lensingOffset,  OFF_LENSING, uint32_t,   	    offset to first lensing obs
-FIELD lensobjOffset,  OFF_LENSOBJ, uint32_t,   	    offset to mean lensing data
-FIELD extendOffset,   OFF_EXTEND,  uint32_t,   	    offset to extended object entry
+FIELD lensingOffset,  OFF_LENSING, int,   	    offset to first lensing obs
+FIELD lensobjOffset,  OFF_LENSOBJ, int,   	    offset to mean lensing data
+FIELD extendOffset,   OFF_EXTEND,  int,   	    offset to extended object entry
 
 FIELD refColor,       REF_COLOR,   float,   	    color of astrometry ref stars
Index: branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/lensing-ps1-v5.d
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/lensing-ps1-v5.d	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/lensing-ps1-v5.d	(revision 36921)
@@ -42,5 +42,5 @@
 FIELD  objID,          OBJ_ID,       unsigned int,   unique ID for object in table
 FIELD  catID,          CAT_ID,       unsigned int,   unique ID for table in which object was first realized
-FIELD    pad,             PAD,       int
+FIELD  averef,         AVE_REF,      unsigned int,   reference to average entry      
 
 # 28 x float
Index: branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/lensing.d
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/lensing.d	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/libautocode/def/lensing.d	(revision 36921)
@@ -42,5 +42,5 @@
 FIELD   objID,          OBJ_ID,       unsigned int,   unique ID for object in table
 FIELD   catID,          CAT_ID,       unsigned int,   unique ID for table in which object was first realized
-FIELD    pad,             PAD,       int
+FIELD  averef,         AVE_REF,       unsigned int,   reference to average entry      
 
 # 28 x float
Index: branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/libdvo/include/dvo.h	(revision 36921)
@@ -606,7 +606,8 @@
   char catmode;				/* storage mode (raw, mef, split, mysql) */
   char catformat;			/* storage format (elixir, panstarrs, etc) */
-  char catflags;				/* choices to be loaded */
   char sorted;				/* is measure table average-sorted? */
   
+  short catflags; /* choices to be loaded */
+
   /* pointers for data manipulation */
   off_t *found;
Index: branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog.c
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog.c	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_catalog.c	(revision 36921)
@@ -333,62 +333,4 @@
 
 // init all data, or just catalog data
-void dvo_catalog_init (Catalog *catalog, int complete) {
-
-  // the following are used to guide open/create/load
-  if (complete) {
-    catalog[0].f = NULL;
-    catalog[0].filename = NULL;
-
-    catalog[0].lockmode = 0;
-    catalog[0].catmode  = 0;
-    catalog[0].catformat = 0;
-    catalog[0].catflags = 0;
-    catalog[0].Nsecfilt = 0;
-  }
-
-  gfits_init_header (&catalog[0].header);
-
-  // the following describe the catalog files on disk
-  catalog[0].average = NULL;
-  catalog[0].measure = NULL; 
-  catalog[0].missing = NULL; 
-  catalog[0].secfilt = NULL;
-  
-  catalog[0].averageT = NULL;
-  catalog[0].measureT = NULL; 
-
-  catalog[0].objID = 0;
-  catalog[0].catID = 0;
-  catalog[0].sorted = 0;
-
-  catalog[0].Naverage = 0;
-  catalog[0].Nmeasure = 0;
-  catalog[0].Nmissing = 0;
-  catalog[0].Nsecf_mem = 0;
-
-  catalog[0].Naves_disk = 0;
-  catalog[0].Nmeas_disk = 0;
-  catalog[0].Nmiss_disk = 0;
-  catalog[0].Nsecf_disk = 0;
-
-  catalog[0].Naves_off  = 0;
-  catalog[0].Nmeas_off  = 0;
-  catalog[0].Nmiss_off  = 0;
-  catalog[0].Nsecf_off  = 0;
-
-  /* pointers to SPLIT data files */
-  catalog[0].measure_catalog = NULL;
-  catalog[0].missing_catalog = NULL;
-  catalog[0].secfilt_catalog = NULL;
-
-  /* pointers for data manipulation */
-  catalog[0].found = NULL;
-  catalog[0].image = NULL;
-  catalog[0].mosaic = NULL;
-  catalog[0].X = NULL;
-  catalog[0].Y = NULL;
-}
-
-// init all data, or just catalog data
 void dvo_lensing_init (Lensing *lensing) {
   lensing->X11_sm_obj = NAN;
@@ -429,6 +371,5 @@
   lensing->objID = -1;
   lensing->catID = -1;
-
-  lensing->pad = 0;
+  lensing->averef = 0;
 }
 
@@ -477,4 +418,76 @@
 
   lensobj->pad = 0;
+}
+
+// init all data, or just catalog data
+void dvo_catalog_init (Catalog *catalog, int complete) {
+
+  // the following are used to guide open/create/load
+  if (complete) {
+    catalog[0].f = NULL;
+    catalog[0].filename = NULL;
+
+    catalog[0].lockmode = 0;
+    catalog[0].catmode  = 0;
+    catalog[0].catformat = 0;
+    catalog[0].catflags = 0;
+    catalog[0].Nsecfilt = 0;
+  }
+
+  gfits_init_header (&catalog[0].header);
+
+  // the following describe the catalog files on disk
+  catalog[0].average = NULL;
+  catalog[0].measure = NULL; 
+  catalog[0].missing = NULL; 
+  catalog[0].secfilt = NULL;
+  
+  catalog[0].lensing = NULL; 
+  catalog[0].lensobj = NULL; 
+
+  catalog[0].averageT = NULL;
+  catalog[0].measureT = NULL; 
+
+  catalog[0].objID = 0;
+  catalog[0].catID = 0;
+  catalog[0].sorted = 0;
+
+  catalog[0].Naverage = 0;
+  catalog[0].Nmeasure = 0;
+  catalog[0].Nmissing = 0;
+  catalog[0].Nsecf_mem = 0;
+
+  catalog[0].Nlensing = 0;
+  catalog[0].Nlensobj = 0;
+
+  catalog[0].Naves_disk = 0;
+  catalog[0].Nmeas_disk = 0;
+  catalog[0].Nmiss_disk = 0;
+  catalog[0].Nsecf_disk = 0;
+
+  catalog[0].Nlensing_disk = 0;
+  catalog[0].Nlensobj_disk = 0;
+
+  catalog[0].Naves_off  = 0;
+  catalog[0].Nmeas_off  = 0;
+  catalog[0].Nmiss_off  = 0;
+  catalog[0].Nsecf_off  = 0;
+
+  catalog[0].Nlensing_off  = 0;
+  catalog[0].Nlensobj_off  = 0;
+
+  /* pointers to SPLIT data files */
+  catalog[0].measure_catalog = NULL;
+  catalog[0].missing_catalog = NULL;
+  catalog[0].secfilt_catalog = NULL;
+  catalog[0].lensing_catalog = NULL;
+  catalog[0].lensobj_catalog = NULL;
+
+  /* pointers for data manipulation */
+  catalog[0].found = NULL;
+  catalog[0].image = NULL;
+  catalog[0].mosaic = NULL;
+  catalog[0].X = NULL;
+  catalog[0].Y = NULL;
 }
 
Index: branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c
===================================================================
--- branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c	(revision 36920)
+++ branches/eam_branches/ipp-20140610/Ohana/src/libdvo/src/dvo_convert_PS1_V5.c	(revision 36921)
@@ -454,4 +454,6 @@
     out[i].objID       = in[i].objID;
     out[i].catID       = in[i].catID;
+
+    out[i].averef      = in[i].averef;
   }
   return (out);
@@ -504,4 +506,6 @@
     out[i].objID       = in[i].objID;
     out[i].catID       = in[i].catID;
+
+    out[i].averef      = in[i].averef;
   }
   return (out);
