Index: trunk/Ohana/src/opihi/dvo/Makefile
===================================================================
--- trunk/Ohana/src/opihi/dvo/Makefile	(revision 39568)
+++ trunk/Ohana/src/opihi/dvo/Makefile	(revision 39569)
@@ -25,4 +25,5 @@
 $(SRC)/dvo_host_utils.$(ARCH).o		\
 $(SRC)/region_list.$(ARCH).o		\
+$(SRC)/mySequence.$(ARCH).o		\
 $(SRC)/photcode_ops.$(ARCH).o	  	\
 $(SRC)/find_matches.$(ARCH).o           
Index: trunk/Ohana/src/opihi/dvo/gstar.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/gstar.c	(revision 39568)
+++ trunk/Ohana/src/opihi/dvo/gstar.c	(revision 39569)
@@ -92,5 +92,5 @@
   off_t j, k, m, N, Nlo, Nhi;
   int Nsecfilt, NPTS, QUIET, INST, SHOW_MASKS;
-  int found, GetMeasures;
+  int found, GetMeasure, GetLensing;
   int SaveVectors;
   Vector *vec1, *vec2, *vec3, *vec4, *vec5, *vec6;
@@ -196,11 +196,18 @@
   }
 
-  GetMeasures = FALSE;
+  GetMeasure = FALSE;
   if ((N = get_argument (argc, argv, "-m"))) {
-    GetMeasures = TRUE;
+    GetMeasure = TRUE;
     remove_argument (N, &argc, argv);
   }
   if ((N = get_argument (argc, argv, "-meas"))) {
-    GetMeasures = TRUE;
+    GetMeasure = TRUE;
+    remove_argument (N, &argc, argv);
+  }
+  if (SaveVectors) GetMeasure = TRUE;
+
+  GetLensing = FALSE;
+  if ((N = get_argument (argc, argv, "-lens"))) {
+    GetLensing = TRUE;
     remove_argument (N, &argc, argv);
   }
@@ -258,5 +265,6 @@
   /* lock, load, unlock catalog */
   catalog.catflags = DVO_LOAD_AVERAGE | DVO_LOAD_SECFILT;
-  catalog.catflags |= GetMeasures ? DVO_LOAD_MEASURE : DVO_SKIP_MEASURE;
+  catalog.catflags |= GetMeasure ? DVO_LOAD_MEASURE : DVO_SKIP_MEASURE;
+  catalog.catflags |= GetLensing ? DVO_LOAD_LENSING : DVO_SKIP_LENSING;
   catalog.Nsecfilt = 0;
 
@@ -676,5 +684,5 @@
       gprint (GP_LOG, "\n");
       
-      if (GetMeasures || SaveVectors) {
+      if (GetMeasure) {
 
 	if (catalog.average[k].Nmeasure == 0) continue;
@@ -717,5 +725,5 @@
 	  float dDoff = dvoOffsetD(&catalog.measure[Nv], &catalog.average[k]);
 
-	  if (GetMeasures && !QUIET) {
+	  if (GetMeasure && !QUIET) {
 	    date = ohana_sec_to_date (catalog.measure[Nv].t);
 	    gprint (GP_LOG, "%6.3f ",  Mcat);
@@ -744,4 +752,7 @@
  	    gprint (GP_LOG, "%5.2f ",  FromShortPixels(catalog.measure[Nv].FWx));
 	    gprint (GP_LOG, "%5.2f ",  FromShortPixels(catalog.measure[Nv].FWy));
+ 	    gprint (GP_LOG, "%5.2f ",  FromShortPixels(catalog.measure[Nv].Mxx));
+	    gprint (GP_LOG, "%5.2f ",  FromShortPixels(catalog.measure[Nv].Mxy));
+	    gprint (GP_LOG, "%5.2f ",  FromShortPixels(catalog.measure[Nv].Myy));
 
 	    if (FULL_OUTPUT) {
@@ -777,6 +788,39 @@
 	      gprint (GP_LOG, "%10.3e ", catalog.measure[Nv].dFluxAp);
 	    }
-	    gprint (GP_LOG, "\n");
-
+
+	    if (GetLensing && catalog.average[k].Nlensing) {
+	      int mLens = catalog.average[k].lensingOffset;
+	      int foundLens = FALSE;
+	      for (int Nlens = 0; !foundLens && (Nlens < catalog.average[k].Nlensing); Nlens ++) {
+		Lensing *lensing = &catalog.lensing[mLens + Nlens];
+		if (lensing->imageID != catalog.measure[Nv].imageID) continue;
+
+		gprint (GP_LOG, "%10.3e ",  lensing->X11_sm_obj);
+		gprint (GP_LOG, "%10.3e ",  lensing->X12_sm_obj);
+		gprint (GP_LOG, "%10.3e ",  lensing->X22_sm_obj);
+		gprint (GP_LOG, "%10.3e ",  lensing-> E1_sm_obj);
+		gprint (GP_LOG, "%10.3e ",  lensing-> E2_sm_obj);
+		gprint (GP_LOG, "%10.3e ",  lensing->X11_sh_obj);
+		gprint (GP_LOG, "%10.3e ",  lensing->X12_sh_obj);
+		gprint (GP_LOG, "%10.3e ",  lensing->X22_sh_obj);
+		gprint (GP_LOG, "%10.3e ",  lensing-> E1_sh_obj);
+		gprint (GP_LOG, "%10.3e ",  lensing-> E2_sh_obj);
+		gprint (GP_LOG, "%10.3e ",  lensing->X11_sm_psf);
+		gprint (GP_LOG, "%10.3e ",  lensing->X12_sm_psf);
+		gprint (GP_LOG, "%10.3e ",  lensing->X22_sm_psf);
+		gprint (GP_LOG, "%10.3e ",  lensing-> E1_sm_psf);
+		gprint (GP_LOG, "%10.3e ",  lensing-> E2_sm_psf);
+		gprint (GP_LOG, "%10.3e ",  lensing->X11_sh_psf);
+		gprint (GP_LOG, "%10.3e ",  lensing->X12_sh_psf);
+		gprint (GP_LOG, "%10.3e ",  lensing->X22_sh_psf);
+		gprint (GP_LOG, "%10.3e ",  lensing-> E1_sh_psf);
+		gprint (GP_LOG, "%10.3e ",  lensing-> E2_sh_psf);
+		gprint (GP_LOG, " %10.3e ", lensing-> E1_psf);
+		gprint (GP_LOG,  "%10.3e ", lensing-> E2_psf);
+		foundLens = TRUE;
+	      }	      
+	    }
+
+	    gprint (GP_LOG, "\n");
 	    free (date);
 	  }
Index: trunk/Ohana/src/opihi/dvo/mextract.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/mextract.c	(revision 39568)
+++ trunk/Ohana/src/opihi/dvo/mextract.c	(revision 39569)
@@ -216,4 +216,11 @@
   int needStarpar = dbFieldNeedStarpar (fields, Nfields, FALSE);
 
+  // the lensing table does not have a good index to/from the measure table.  if we need lensing
+  // parameters, we need to generate a lookup table to avoid excess iterations
+  mySequenceType *lensingSeq = NULL;
+  if (needLensing) {
+    lensingSeq = mySequenceAlloc();
+  }
+
   // grab data from all selected sky regions
   struct sigaction *old_sigaction = SetInterrupt();
@@ -249,5 +256,7 @@
 
     for (j = 0; (j < catalog.Naverage) && !interrupt; j++) {
-      m = catalog.average[j].measureOffset;
+      Average *average = &catalog.average[j];
+
+      m = average->measureOffset;
       if (m > catalog.Nmeasure) {
 	gprint (GP_ERR, "ERROR: inconsistent average->measure offset.  Unsorted database?\n");
@@ -257,5 +266,19 @@
       dbExtractMeasuresInitAve (); // reset counters for saved fields (costs very little)
 
-      for (k = 0; (k < catalog.average[j].Nmeasure); k++, m++) {
+      off_t Lj;
+      off_t Loff = average->lensingOffset;
+
+      if (needLensing) {
+	mySequenceSetSize (lensingSeq, average->Nlensing);
+	
+	// generate an index for these lensing entries (based on Lj and imageID)
+	for (Lj = 0; Lj < average->Nlensing; Lj++) {
+	  mySequenceSetValue (lensingSeq, catalog.lensing[Loff + Lj].imageID, Lj);
+	}
+	
+	mySequenceSort (lensingSeq);
+      }
+
+      for (k = 0; (k < average->Nmeasure); k++, m++) {
 	if (catalog.measure[m].averef != j) {
 	  gprint (GP_ERR, "ERROR: inconsistent measure->average link.  Unsorted database?\n");
@@ -266,11 +289,14 @@
 	dbExtractMeasuresInitMeas (); // reset counters for saved fields  (costs very little
 
-	Average *average = &catalog.average[j];
-
 	int Nstarpar = average->starparOffset;
-	StarPar *starpar = needStarpar ? &catalog.starpar[Nstarpar] : NULL;
-
-	int Nlensing = average->lensingOffset;
-	Lensing *lensing = needLensing ? &catalog.lensing[Nlensing] : NULL;
+	StarPar *starpar = needStarpar && average->Nstarpar ? &catalog.starpar[Nstarpar] : NULL;
+
+	Lensing *lensing = NULL;
+
+	// a gpc1 analysis specific choice: only look for lensing if photcode is a warp one
+	if (needLensing && average->Nlensing && (catalog.measure[m].photcode >= 12000) && (catalog.measure[m].photcode <= 12500)) {
+	  Lj = mySequenceGetEntry (lensingSeq, catalog.measure[m].imageID);
+	  if (Lj >= 0) lensing = &catalog.lensing[Loff + Lj];
+	}
 
 	int Nsec = j*Nsecfilt;
Index: trunk/Ohana/src/opihi/dvo/mySequence.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/mySequence.c	(revision 39569)
+++ trunk/Ohana/src/opihi/dvo/mySequence.c	(revision 39569)
@@ -0,0 +1,78 @@
+# include "dvoshell.h"
+
+mySequenceType *mySequenceAlloc () {
+
+  mySequenceType *mySequence;
+  ALLOCATE (mySequence, mySequenceType, 1);
+  ALLOCATE (mySequence->value,    int, 1);
+  ALLOCATE (mySequence->sequence, int, 1);
+
+  // an uninit'ed index is invalid
+  mySequence->Nsequence = 0;
+  mySequence->NSEQUENCE = 1;
+
+  return mySequence;
+}
+
+int mySequenceFree (mySequenceType *mySequence) {
+
+  free (mySequence->value);
+  free (mySequence->sequence);
+  free (mySequence);
+  return TRUE;
+}
+
+// set minID and maxID externally
+int mySequenceSetSize (mySequenceType *mySequence, int Nmax) {
+
+  mySequence->Nsequence = Nmax;
+  if (mySequence->Nsequence > mySequence->NSEQUENCE) {
+    mySequence->NSEQUENCE = mySequence->Nsequence;
+    REALLOCATE (mySequence->value,    int, mySequence->NSEQUENCE);
+    REALLOCATE (mySequence->sequence, int, mySequence->NSEQUENCE);
+  }
+  return TRUE;
+}
+
+int mySequenceSetValue (mySequenceType *mySequence, int value, int entry) {
+
+  mySequence->value[entry] = value;
+  mySequence->sequence[entry] = entry;
+  return TRUE;
+}
+
+int mySequenceSort (mySequenceType *mySequence) {
+
+  isortpair (mySequence->value, mySequence->sequence, mySequence->Nsequence);
+  return TRUE;
+}
+
+// find the entry by bisection:
+int mySequenceGetEntry (mySequenceType *mySequence, int value) {
+
+  int Nlo = 0; 
+  int Nhi = mySequence->Nsequence - 1;
+
+  if (value < mySequence->value[Nlo]) return -1;
+  if (value > mySequence->value[Nhi]) return -1;
+
+  int N;
+  while (Nhi - Nlo > 4) {
+    N = 0.5*(Nlo + Nhi);
+    if (mySequence->value[N] < value) {
+      Nlo = MAX(N, 0);
+    } else {
+      Nhi = MIN(N + 1, mySequence->Nsequence - 1);
+    }
+  }
+  // mySequence->value[Nlo] <  value 
+  // mySequence->value[Nhi] >= value
+
+  for (N = Nlo; N <= Nhi; N++) {
+    if (mySequence->value[N] == value) {
+      return mySequence->sequence[N];
+    }
+  }
+  return -1;
+}
+
Index: trunk/Ohana/src/opihi/include/dvoshell.h
===================================================================
--- trunk/Ohana/src/opihi/include/dvoshell.h	(revision 39568)
+++ trunk/Ohana/src/opihi/include/dvoshell.h	(revision 39569)
@@ -26,4 +26,14 @@
   e_time t;
 } CMPstars;
+
+// a structure to define a sequence lookup
+typedef struct {
+  int minID;
+  int maxID;
+  int *value;
+  int *sequence;
+  int Nsequence;
+  int NSEQUENCE;
+} mySequenceType;
 
 /** some globals used particularly by DVO_CLIENT **/
@@ -93,3 +103,10 @@
 int          HostTableGetResults    PROTO((char *uniquer, int VERBOSE));
 
+mySequenceType *mySequenceAlloc ();
+int mySequenceFree (mySequenceType *mySequence);
+int mySequenceSetSize (mySequenceType *mySequence, int Nmax);
+int mySequenceSetValue (mySequenceType *mySequence, int value, int entry);
+int mySequenceSort (mySequenceType *mySequence);
+int mySequenceGetEntry (mySequenceType *mySequence, int value);
+
 # endif // DVOSHELL_H
