Index: /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/LoadData.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/LoadData.c	(revision 38535)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/LoadData.c	(revision 38536)
@@ -13,5 +13,4 @@
   dvo_catalog_init (catalog, TRUE);
   ALLOCATE (catalog->average, Average, 1);
-  ALLOCATE (catalog->secfilt, SecFilt, 1);
   ALLOCATE (catalog->measure, Measure, 1);
   ALLOCATE (catalog->lensing, Lensing, 1);
@@ -117,8 +116,9 @@
     
     AddstarClientOptions matchOptions = *options;
-    matchOptions.radius = 0.1; // tight radius at this stage
+    matchOptions.radius = 0.4; // tight radius at this stage
     matchOptions.calibrate = FALSE;
     matchOptions.only_match = FALSE;
     matchOptions.nosort = FALSE;
+    matchOptions.photcode = 0; // use an invalid photcode to avoid touching secfilt
 
     find_matches_closest (&region, newcat, catalog, matchOptions);
Index: /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/LoadStars.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/LoadStars.c	(revision 38535)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/LoadStars.c	(revision 38536)
@@ -12,6 +12,9 @@
 
   Catalog *catalog = NULL;
-  // ALLOCATE (catalog, Catalog, 1);
-  // dvo_catalog_init (catalog, TRUE);
+  ALLOCATE (catalog, Catalog, 1);
+  dvo_catalog_init (catalog, TRUE);
+  ALLOCATE (catalog->average, Average, 1);
+  ALLOCATE (catalog->measure, Measure, 1);
+  ALLOCATE (catalog->lensing, Lensing, 1);
 
   for (i = 0; i < Nfile; i++) {
@@ -69,11 +72,28 @@
     }
 
+    off_t NimagesStart = *Nimages;
     Catalog *newcat = LoadData (f, &file[i], images, Nimages, headers, extsize, headerSets, NheaderSets, options);
-    catalog = newcat;
 
-    // XXX I need to use find_matches_closest here, but for testing:
-    // find_matches_closest (region, catalog, newcat, options);
-    // dvo_catalog_free (newcat);
-    // free (newcat);
+    // if we added data from an image, then we can merge it in
+    if (*Nimages > NimagesStart) {
+      double Rref, Dref;
+      XY_to_RD (&Rref, &Dref, 0.0, 0.0, &images[0][*Nimages-1].coords);
+      SkyRegion region;
+      region.Rmin = Rref - 180.0;
+      region.Rmax = Rref + 180.0;
+      region.Dmin = Dref -  90.0;
+      region.Dmax = Dref +  90.0;
+      
+      AddstarClientOptions matchOptions = *options;
+      matchOptions.radius = 0.4; // tight radius at this stage
+      matchOptions.calibrate = FALSE;
+      matchOptions.only_match = FALSE;
+      matchOptions.nosort = FALSE;
+      matchOptions.photcode = 0; // use an invalid photcode to avoid touching secfilt
+      
+      find_matches_closest (&region, newcat, catalog, matchOptions);
+    }
+    dvo_catalog_free (newcat);
+    free (newcat);
 
   next_file:
@@ -95,5 +115,4 @@
     exit (0);
   }
-  ohana_memcheck (TRUE);
 
   AddstarFileFree (file, Nfile);
Index: /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/addstar.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/addstar.c	(revision 38535)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/addstar.c	(revision 38536)
@@ -196,9 +196,4 @@
   SkyListFree (skylist);
 
-  if (options.mode != ADDSTAR_MODE_REFCAT) {
-    dvo_catalog_free (newcat);
-    free (newcat);
-  }
-
   // We only measure a single value for the entire mosaic (add all images to this function)
   if (options.calibrate) { FindCalibration (&images[0]); }
@@ -236,4 +231,9 @@
   float dtime = DTIME (stopAddstar, startAddstar);
   fprintf (stderr, "SUCCESS: elapsed time %9.4f sec for "OFF_T_FMT" stars (%5d matches), "OFF_T_FMT" average, "OFF_T_FMT" measure, "OFF_T_FMT" lensing\n", dtime, newcat->Naverage, Nmatch,  Naverage,  Nmeasure, Nlensing);
+
+  if (options.mode != ADDSTAR_MODE_REFCAT) {
+    dvo_catalog_free (newcat);
+    free (newcat);
+  }
 
   // XXX test
Index: /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/find_matches_closest.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/find_matches_closest.c	(revision 38535)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/find_matches_closest.c	(revision 38536)
@@ -1,11 +1,11 @@
 # include "addstar.h"
 
-int find_matches_closest (SkyRegion *region, Catalog *newcat, Catalog *catalog, AddstarClientOptions options) {
-  
-  off_t i, j, n, N, J, Jmin, status, Nstars;
+int find_matches_closest (SkyRegion *region, Catalog *srccat, Catalog *tgtcat, AddstarClientOptions options) {
+
+  off_t i, j, n, N, J, Jmin, status, Nstars, mSrc;
   double RADIUS, RADIUS2, Rmin;
   double *X1, *Y1, *X2, *Y2;
   double dX, dY, dR;
-  off_t *N1, *N2, *next_meas, *next_lens;
+  off_t *N1, *N2;
   off_t Nave, NAVE, Nmeas, NMEAS, Nmatch, Nlens, NLENS;
   int Nsecfilt, Nsec;
@@ -13,10 +13,4 @@
   Coords tcoords;
 
-  // XXX this probably is no longer needed (absorbed by catalog->average[j].Nmeasure
-  if (NSTAR_GROUP <= 0) {
-      fprintf (stderr, "ERROR: NSTAR_GROUP NOT SET!\n");
-      exit (1);
-  }
-
   /* photcode data - must by of type DEP; options.photcode is equiv photcode for all input
      images this function requires incoming stars to have the same photcode.equiv value.  if
@@ -27,37 +21,46 @@
   Nsec     = GetPhotcodeNsec (options.photcode);
 
-  /** allocate local arrays (newcat) **/
-  ALLOCATE (X1, double, newcat->Naverage);
-  ALLOCATE (Y1, double, newcat->Naverage);
-  ALLOCATE (N1, off_t,  newcat->Naverage);
-
-  if (!newcat->found_t) {
-    ALLOCATE (newcat->found_t, off_t, newcat->Naverage);
-    for (i = 0; i < newcat->Naverage; i++) {
-      newcat->found_t[i] = -1;
-    }
-  }
-
-  /** allocate local arrays (catalog) **/
-  NAVE = Nave = catalog[0].Naverage;
+  /** allocate local arrays (srccat) **/
+  ALLOCATE (X1, double, srccat->Naverage);
+  ALLOCATE (Y1, double, srccat->Naverage);
+  ALLOCATE (N1, off_t,  srccat->Naverage);
+
+  if (!srccat->found_t) {
+    ALLOCATE (srccat->found_t, off_t, srccat->Naverage);
+    for (i = 0; i < srccat->Naverage; i++) {
+      srccat->found_t[i] = -1;
+    }
+  }
+
+  /** allocate local arrays (tgtcat) **/
+  NAVE = Nave = tgtcat[0].Naverage;
   ALLOCATE (X2, double, NAVE);
   ALLOCATE (Y2, double, NAVE);
   ALLOCATE (N2, off_t,  NAVE);
-  ALLOCATE (catalog[0].found_t, off_t, NAVE);
+
+  if (!tgtcat->found_t) {
+    ALLOCATE (tgtcat[0].found_t, off_t, NAVE);
+    for (i = 0; i < NAVE; i++) {
+      tgtcat->found_t[i] = -1;
+    }
+  }
   /* for secfilt j and star i, secfilt[i*Nsecfilt+j] */
 
   /* internal counters */
   Nmatch = 0;
-  NMEAS = Nmeas = catalog[0].Nmeasure;
-  NLENS = Nlens = catalog[0].Nlensing;
-
-  // current max obj ID for this catalog
-  objID = catalog[0].objID;
-  catID = catalog[0].catID;
+  NMEAS = Nmeas = tgtcat[0].Nmeasure;
+  NLENS = Nlens = tgtcat[0].Nlensing;
+
+  off_t *next_meas = NULL;
+  off_t *next_lens = NULL;
+
+  // current max obj ID for this tgtcat
+  objID = tgtcat[0].objID;
+  catID = tgtcat[0].catID;
 
   /* project onto rectilinear grid with 1 arcsec pixels. the choice of ARC projection has
    * the advantage that every point in R,D has a mapping to a unique X,Y.  However, note
    * that not all possible X,Y points map back to R,D and the local plate scale changes
-   * far from the projection pole. We use the center of the region (catalog) for crval1,2.
+   * far from the projection pole. We use the center of the region (tgtcat) for crval1,2.
    */
   InitCoords (&tcoords, "DEC--ARC");
@@ -72,6 +75,6 @@
   /* build spatial index (RA sort) referencing input array sequence */
   Nstars = 0;
-  for (i = 0; i < newcat->Naverage; i++) {
-    status = RD_to_XY (&X1[Nstars], &Y1[Nstars], newcat->average[i].R, newcat->average[i].D, &tcoords);
+  for (i = 0; i < srccat->Naverage; i++) {
+    status = RD_to_XY (&X1[Nstars], &Y1[Nstars], srccat->average[i].R, srccat->average[i].D, &tcoords);
     if (!status) continue;
     N1[Nstars] = i;
@@ -79,5 +82,5 @@
   }
   if (Nstars < 1) {
-    if (VERBOSE) fprintf (stderr, "skipping %s, no overlapping stars\n", catalog[0].filename);
+    if (VERBOSE) fprintf (stderr, "skipping %s, no overlapping stars\n", tgtcat[0].filename);
     free (X1);
     free (Y1);
@@ -87,4 +90,5 @@
     free (N2);
     return (0);
+    // XXX check on this
   }
   if (Nstars > 1) sort_coords_index (X1, Y1, N1, Nstars);
@@ -92,19 +96,19 @@
   /* build spatial index (RA sort) */
   for (i = 0; i < Nave; i++) {
-    RD_to_XY (&X2[i], &Y2[i], catalog[0].average[i].R, catalog[0].average[i].D, &tcoords);
+    RD_to_XY (&X2[i], &Y2[i], tgtcat[0].average[i].R, tgtcat[0].average[i].D, &tcoords);
     N2[i] = i;
-    catalog[0].found_t[N2[i]] = -1;
+    tgtcat[0].found_t[N2[i]] = -1;
   }
   if (Nave > 1) sort_coords_index (X2, Y2, N2, Nave);
 
   /* set up pointers for linked list of measure */
-  if (catalog[0].sorted && (catalog[0].Nmeasure == catalog[0].Nmeasure_disk)) {
-    // this version is only valid if we have done a full catalog load, and if the catalog
+  if (tgtcat[0].sorted && (tgtcat[0].Nmeasure == tgtcat[0].Nmeasure_disk)) {
+    // this version is only valid if we have done a full tgtcat load, and if the tgtcat
     // 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);
+    next_meas = init_measure_links (tgtcat[0].average, Nave, tgtcat[0].measure, Nmeas);
+    next_lens = init_lensing_links (tgtcat[0].average, Nave, tgtcat[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);
+    next_meas = build_measure_links (tgtcat[0].average, Nave, tgtcat[0].measure, Nmeas);
+    next_lens = build_lensing_links (tgtcat[0].average, Nave, tgtcat[0].lensing, Nlens);
   }    
 
@@ -113,6 +117,6 @@
   RADIUS2 = RADIUS*RADIUS;
 
-  /** find matched stars **/
-  // XXX could use NSTAR_GROUP to do this match more quicky if NSTAR_GROUP > 1
+  /****************** find matched stars ********************/
+
   for (i = j = 0; (i < Nstars) && (j < Nave); ) {
     if (!finite(X1[i]) || !finite(Y1[i])) { 
@@ -138,6 +142,6 @@
 
     // XXX check that this is allocated
-    if (newcat->found_t[N1[i]] != -1) {
-        /* this star has already been assigned to an object in this or another catalog */
+    if (srccat->found_t[N1[i]] != -1) {
+        /* this star has already been assigned to an object in this or another tgtcat */
         i++;
         continue;
@@ -178,221 +182,224 @@
 
     /* make sure there is space for next entry */
-    if (Nmeas >= NMEAS) {
-      NMEAS = Nmeas + 1000;
+    if (Nmeas >= NMEAS - srccat->average[N].Nmeasure) {
+      NMEAS = Nmeas + 1000 + srccat->average[N].Nmeasure;
       REALLOCATE (next_meas, off_t, NMEAS);
-      REALLOCATE (catalog[0].measure, Measure, NMEAS);
-    }
-    if (Nlens >= NLENS) {
-      NLENS = Nlens + 1000;
+      REALLOCATE (tgtcat[0].measure, Measure, NMEAS);
+    }
+    if (Nlens >= NLENS - srccat->average[N].Nlensing) {
+      NLENS = Nlens + 1000 + srccat->average[N].Nlensing;
       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);
-	
+      REALLOCATE (tgtcat[0].lensing, Lensing, NLENS);
+    }
+
     /** add measurements for this star **/
-
-    // set the new measurements
-    catalog[0].measure[Nmeas]          = newcat->measure[N];
-
-    // measure now carries R,D (not dR,dD) 
-    catalog[0].measure[Nmeas].dbFlags  = 0;
-    catalog[0].measure[Nmeas].averef   = n;
-    catalog[0].measure[Nmeas].objID    = catalog[0].average[n].objID;
-    catalog[0].measure[Nmeas].catID    = catalog[0].catID;
-
-    float dRoff = dvoOffsetR(&catalog[0].measure[Nmeas], &catalog[0].average[n]);
-
-    // rationalize dR
-    if (dRoff > +180.0*3600.0) {
-      // average on high end of boundary, move star up
-      catalog[0].measure[Nmeas].R += 360.0;
-      dRoff -= 360.0*3600.0;
-    }
-    if (dRoff < -180.0*3600.0) {
-      // average on low end of boundary, move star down
-      catalog[0].measure[Nmeas].R -= 360.0;
-      dRoff += 360.0*3600.0;
-    }
-    if (fabs(dRoff) > 10*RADIUS) {
+    off_t mOff = srccat->average[N].measureOffset;
+    for (mSrc = 0; mSrc < srccat->average[N].Nmeasure; mSrc++) {
+      /* add to end of measurement list */
+      add_meas_link (&tgtcat[0].average[n], next_meas, Nmeas, NMEAS);
+
+      // set the new measurements
+      tgtcat[0].measure[Nmeas]          = srccat->measure[mSrc + mOff];
+
+      // measure now carries R,D (not dR,dD) 
+      tgtcat[0].measure[Nmeas].dbFlags  = 0;
+      tgtcat[0].measure[Nmeas].averef   = n;
+      tgtcat[0].measure[Nmeas].objID    = tgtcat[0].average[n].objID;
+      tgtcat[0].measure[Nmeas].catID    = tgtcat[0].catID;
+
+      float dRoff = dvoOffsetR(&tgtcat[0].measure[Nmeas], &tgtcat[0].average[n]);
+
+      // rationalize dR
+      if (dRoff > +180.0*3600.0) {
+	// average on high end of boundary, move star up
+	tgtcat[0].measure[Nmeas].R += 360.0;
+	dRoff -= 360.0*3600.0;
+      }
+      if (dRoff < -180.0*3600.0) {
+	// average on low end of boundary, move star down
+	tgtcat[0].measure[Nmeas].R -= 360.0;
+	dRoff += 360.0*3600.0;
+      }
+      if (fabs(dRoff) > 10*RADIUS) {
 	// take declination into account and check again.
-	double cosD = cos(RAD_DEG*catalog[0].average[n].D);
+	double cosD = cos(RAD_DEG*tgtcat[0].average[n].D);
 	if (fabs(dRoff*cosD) > 10*RADIUS) {
-	    fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
-		     catalog[0].average[n].R, catalog[0].average[n].D, 
-		     newcat->average[N].R, newcat->average[N].D,
-		     X1[i], X2[Jmin], 
-		     Y1[i], Y2[Jmin]);
-	    // XXX abort on this? -- this is a bad failure...
+	  fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
+		   tgtcat[0].average[n].R, tgtcat[0].average[n].D, 
+		   srccat->average[N].R, srccat->average[N].D,
+		   X1[i], X2[Jmin], 
+		   Y1[i], Y2[Jmin]);
+	  // XXX abort on this? -- this is a bad failure...
 	}
-    }
-
-    // add the lensing values if they exist
-    if (newcat->lensing) {
-      add_lens_link (&catalog[0].average[n], next_lens, Nlens, NLENS); // ?
-      catalog[0].lensing[Nlens] = newcat->lensing[N];
-      
-      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 */
-    if (options.calibrate) {
-      AddToCalibration (&catalog[0].average[n], &catalog[0].secfilt[n*Nsecfilt], catalog[0].measure, &catalog[0].measure[Nmeas], next_meas, N);
-    }
-
-    /* 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) { 
-      if (isnan(catalog[0].secfilt[n*Nsecfilt+Nsec].M)) {
-	catalog[0].secfilt[n*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas], MAG_CLASS_PSF);
-      }
-    }
-
-    /* if we choose to flag close encounters, see find_matches.c */
-    /* if we choose to calculate RA,DEC averages, see update_coords.c */
-
-    /* Nm is updated, but not written out in -update mode (for existing entries)
-       Nm is recalculated in build_meas_links if loaded table is not sorted */
-    newcat->found_t[N] = Nmeas;
-    catalog[0].found_t[n] = Nmeas;
-    catalog[0].average[n].Nmeasure ++;
-    Nmeas ++;
-    i++;
-  }
-
-  /** incorporate unmatched image stars, if this star is in field of this catalog **/
-  /* these new entries are all written out in UPDATE mode */ 
-  for (i = 0; (i < Nstars) && !options.only_match; i += NSTAR_GROUP) {
-    fprintf (stderr, "loop %d\n", (int) i);
-    /* make sure there is space for next entry */
-    if (Nmeas >= NMEAS - NSTAR_GROUP) {
-      NMEAS = Nmeas + 1000;
-      REALLOCATE (next_meas, off_t, NMEAS);
-      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;
-      REALLOCATE (catalog[0].average, Average, NAVE);
-      REALLOCATE (catalog[0].secfilt, SecFilt, NAVE*catalog[0].Nsecfilt);
-    }
-
-    if (newcat->found_t[i] != -1) continue;
-    if (!IN_REGION (newcat->average[i].R, newcat->average[i].D)) continue;
-
-    dvo_average_init (&catalog[0].average[Nave]);
-    catalog[0].average[Nave].R         	   = newcat->average[i].R;
-    catalog[0].average[Nave].D         	   = newcat->average[i].D;
-
-    catalog[0].average[Nave].Nmeasure  	   = NSTAR_GROUP;
-    catalog[0].average[Nave].measureOffset = Nmeas;
-    catalog[0].average[Nave].objID     	   = objID;
-    catalog[0].average[Nave].catID     	   = catID;
-
-    ohana_memcheck(1);
-
-    if (PSPS_ID) {
-        catalog[0].average[Nave].extID = CreatePSPSObjectID(catalog[0].average[Nave].R, catalog[0].average[Nave].D);
-    }
-
-    objID ++;
-
-    for (j = 0; j < Nsecfilt; j++) {
-      // XXXX inconsistent with my alloc above
-      dvo_secfilt_init (&catalog[0].secfilt[Nave*Nsecfilt+j], SECFILT_RESET_ALL);
-    }
-
-    ohana_memcheck(1);
-
-    for (j = 0; j < NSTAR_GROUP; j++) {
-      // supply the measurments from this detection
-      dvo_measure_init (&catalog[0].measure[Nmeas]);
-      catalog[0].measure[Nmeas] = newcat->measure[i + j];
-
-      // the following measure elements cannot be set until here:
-      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;
+      }
+
+      /* 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 */
+      if (options.calibrate) {
+	AddToCalibration (&tgtcat[0].average[n], &tgtcat[0].secfilt[n*Nsecfilt], tgtcat[0].measure, &tgtcat[0].measure[Nmeas], next_meas, N);
+      }
 
       /* 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) */
-      newcat->found_t[i+j] = Nmeas;
-      next_meas[Nmeas] = -1;  // inital value here update below
+	if (isnan(tgtcat[0].secfilt[n*Nsecfilt+Nsec].M)) {
+	  tgtcat[0].secfilt[n*Nsecfilt+Nsec].M = PhotCat (&tgtcat[0].measure[Nmeas], MAG_CLASS_PSF);
+	}
+      }
+
+      /* Nm is updated, but not written out in -update mode (for existing entries)
+	 Nm is recalculated in build_meas_links if loaded table is not sorted */
+      tgtcat[0].average[n].Nmeasure ++;
       Nmeas ++;
     }
-    for (j = 0; j < NSTAR_GROUP - 1; j++) {
-      next_meas[Nmeas - NSTAR_GROUP + j] = Nmeas - NSTAR_GROUP + j + 1;
-    }
-
-    ohana_memcheck(1);
+
+    // add the lensing values if they exist
+    if (srccat->lensing) {
+      /** add measurements for this star **/
+      off_t mOff = srccat->average[N].lensingOffset;
+      for (mSrc = 0; mSrc < srccat->average[N].Nlensing; mSrc++) {
+	/* add to end of lensing list */
+
+	add_lens_link (&tgtcat[0].average[n], next_lens, Nlens, NLENS); // ?
+	tgtcat[0].lensing[Nlens] = srccat->lensing[mSrc + mOff];
+      
+	tgtcat[0].lensing[Nlens].averef = n;
+	tgtcat[0].lensing[Nlens].objID = tgtcat[0].average[n].objID;
+	tgtcat[0].lensing[Nlens].catID = tgtcat[0].catID;
+	tgtcat[0].average[n].Nlensing ++;
+	Nlens ++;
+      }
+    }
+
+    /* if we choose to flag close encounters, see find_matches.c */
+    /* if we choose to calculate RA,DEC averages, see update_coords.c */
+
+    srccat->found_t[N] = n;
+    tgtcat[0].found_t[n] = -1;
+    i++;
+  }
+
+  /*************** add unmatched stars *************************/
+
+  /** incorporate unmatched image stars, if this star is in field of this tgtcat **/
+  /* these new entries are all written out in UPDATE mode */ 
+  for (i = 0; (i < Nstars) && !options.only_match; i++) {
+    /* make sure there is space for next entry */
+    if (Nmeas >= NMEAS - srccat->average[i].Nmeasure) {
+      NMEAS = Nmeas + 1000 + srccat->average[i].Nmeasure;
+      REALLOCATE (next_meas, off_t, NMEAS);
+      REALLOCATE (tgtcat[0].measure, Measure, NMEAS);
+    }
+    if (Nlens >= NLENS - srccat->average[i].Nlensing) {
+      NLENS = Nlens + 1000 + srccat->average[i].Nlensing;
+      REALLOCATE (next_lens, off_t, NLENS);
+      REALLOCATE (tgtcat[0].lensing, Lensing, NLENS);
+    }
+    if (Nave >= NAVE) {
+      NAVE = Nave + 1000;
+      REALLOCATE (tgtcat[0].average, Average, NAVE);
+      REALLOCATE (tgtcat[0].found_t, off_t, NAVE);
+      if (tgtcat[0].secfilt) {
+	// we only update the secfilt table if it has been allocated for output
+	REALLOCATE (tgtcat[0].secfilt, SecFilt, NAVE*tgtcat[0].Nsecfilt);
+      }
+    }
+
+    if (srccat->found_t[i] != -1) continue;
+    if (!IN_REGION (srccat->average[i].R, srccat->average[i].D)) continue;
+
+    dvo_average_init (&tgtcat[0].average[Nave]);
+    tgtcat[0].average[Nave].R         	   = srccat->average[i].R;
+    tgtcat[0].average[Nave].D         	   = srccat->average[i].D;
+
+    tgtcat[0].average[Nave].Nmeasure  	   = srccat->average[i].Nmeasure;
+    tgtcat[0].average[Nave].measureOffset  = Nmeas;
+    tgtcat[0].average[Nave].objID     	   = objID;
+    tgtcat[0].average[Nave].catID     	   = catID;
+
+    // this star has not yet been merged into another tgtcat
+    tgtcat[0].found_t[Nave]     	   = -1;
+
+    if (PSPS_ID) {
+        tgtcat[0].average[Nave].extID = CreatePSPSObjectID(tgtcat[0].average[Nave].R, tgtcat[0].average[Nave].D);
+    }
+
+    objID ++;
+
+    // we only update the secfilt table if it has been allocated for output
+    for (j = 0; tgtcat[0].secfilt && (j < Nsecfilt); j++) {
+      dvo_secfilt_init (&tgtcat[0].secfilt[Nave*Nsecfilt+j], SECFILT_RESET_ALL);
+    }
+
+    off_t mOff = srccat->average[i].measureOffset;
+    for (mSrc = 0; mSrc < srccat->average[i].Nmeasure; mSrc++) {
+      // supply the measurements from this detection
+      dvo_measure_init (&tgtcat[0].measure[Nmeas]);
+      tgtcat[0].measure[Nmeas] = srccat->measure[mSrc + mOff];
+
+      // the following measure elements cannot be set until here:
+      tgtcat[0].measure[Nmeas].dbFlags  = 0;
+      tgtcat[0].measure[Nmeas].averef   = Nave;
+      tgtcat[0].measure[Nmeas].objID    = tgtcat[0].average[Nave].objID;
+      tgtcat[0].measure[Nmeas].catID    = tgtcat[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) { 
+	tgtcat[0].secfilt[Nave*Nsecfilt+Nsec].M = PhotCat (&tgtcat[0].measure[Nmeas], MAG_CLASS_PSF);
+      }
+
+      Nmeas ++;
+
+      // update the next_meas pointer for this entry (last one for this star is -1)
+      next_meas[Nmeas-1] = (mSrc < srccat->average[i].Nmeasure - 1) ? Nmeas : -1;
+    }
+    srccat->found_t[i] = Nave;
 
     // if we have lensing data, insert that as well
-    if (newcat->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 (newcat->lensing) {
-	  dvo_lensing_init (&catalog[0].lensing[Nlens]);
-	  catalog[0].lensing[Nlens] = newcat->lensing[i + j];
+    if (srccat->lensing) {
+      tgtcat[0].average[Nave].Nlensing = srccat->average[i].Nlensing;
+      tgtcat[0].average[Nave].lensingOffset = Nlens;
+
+      off_t mOff = srccat->average[i].lensingOffset;
+      for (mSrc = 0; mSrc < srccat->average[i].Nlensing; mSrc++) {
+	dvo_lensing_init (&tgtcat[0].lensing[Nlens]);
+	tgtcat[0].lensing[Nlens] = srccat->lensing[mSrc + mOff];
 	  
-	  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;
-      }
-    }
-    ohana_memcheck(1);
-
+	tgtcat[0].lensing[Nlens].averef = Nave;
+	tgtcat[0].lensing[Nlens].objID = tgtcat[0].average[Nave].objID;
+	tgtcat[0].lensing[Nlens].catID = tgtcat[0].catID;
+	Nlens ++;
+	next_lens[Nlens-1] = (mSrc < srccat->average[i].Nlensing - 1) ? Nlens : -1; 
+      }
+    }
     Nave ++;
   }
-      
-  REALLOCATE (catalog[0].average, Average, Nave);
-  REALLOCATE (catalog[0].measure, Measure, Nmeas);
-  REALLOCATE (catalog[0].lensing, Lensing, Nlens);
+
+  REALLOCATE (tgtcat[0].average, Average, Nave);
+  REALLOCATE (tgtcat[0].measure, Measure, Nmeas);
+  REALLOCATE (tgtcat[0].lensing, Lensing, Nlens);
  
   if (options.nosort) {
-    catalog[0].sorted = FALSE;
+    tgtcat[0].sorted = FALSE;
   } else {
-    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);
-  }
-
-  /* note stars which have been found in this catalog */
-  for (i = 0; i < newcat->Naverage; i++) {
-    if (newcat->found_t[i] > -1) {
-      newcat->found_t[i] = -2;
-    }
-  }
-
-  /* check if the catalog has changed?  if no change, no need to write */
-  catalog[0].objID    = objID; // new max value, save on catalog close
-  catalog[0].Naverage = Nave;
-  catalog[0].Nmeasure = Nmeas;
-  catalog[0].Nlensing = Nlens;
-  catalog[0].Nsecfilt_mem = Nave*Nsecfilt;
+    tgtcat[0].sorted = TRUE;
+    tgtcat[0].measure = sort_measure (tgtcat[0].average, Nave, tgtcat[0].measure, Nmeas, next_meas);
+    tgtcat[0].lensing = sort_lensing (tgtcat[0].average, Nave, tgtcat[0].lensing, Nlens, next_lens);
+  }
+
+  /* note stars which have been found in this tgtcat */
+  for (i = 0; i < srccat->Naverage; i++) {
+    if (srccat->found_t[i] > -1) {
+      srccat->found_t[i] = -2;
+    }
+  }
+
+  /* check if the tgtcat has changed?  if no change, no need to write */
+  tgtcat[0].objID    = objID; // new max value, save on tgtcat close
+  tgtcat[0].Naverage = Nave;
+  tgtcat[0].Nmeasure = Nmeas;
+  tgtcat[0].Nlensing = Nlens;
+  tgtcat[0].Nsecfilt_mem = tgtcat[0].secfilt ? Nave*Nsecfilt : 0;
   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);
 
@@ -412,10 +419,10 @@
    notes:
    
-   for finding if a catalog star is in an image or an image star is in the catalog:
+   for finding if a tgtcat star is in an image or an image star is in the tgtcat:
    
-   catalogs have boundaries defined by RA and DEC, but they may curve in projection
+   tgtcats have boundaries defined by RA and DEC, but they may curve in projection
    images have boundaries which are lines in pixels coords, but curve in RA and DEC
    
-   catalog[0].found[Ncat] but stars[Nstar].found
+   tgtcat[0].found[Ncat] but stars[Nstar].found
    
 */
Index: /branches/eam_branches/ipp-20150616/Ohana/src/addstar/test/simple.dvo
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/addstar/test/simple.dvo	(revision 38535)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/addstar/test/simple.dvo	(revision 38536)
@@ -8,5 +8,5 @@
 
 macro test.one
-  test.fields PS1_V5    PS1_V4
+  test.fields PS1_V5 PS1_V5
 end
 
