Index: /branches/eam_branches/ipp-20150616/Ohana/src/addstar/Makefile
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/addstar/Makefile	(revision 38543)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/addstar/Makefile	(revision 38544)
@@ -76,4 +76,5 @@
 $(SRC)/edge_check.$(ARCH).o \
 $(SRC)/fakeimage.$(ARCH).o \
+$(SRC)/find_matches.$(ARCH).o \
 $(SRC)/find_matches_closest.$(ARCH).o \
 $(SRC)/GetZeroPointExposure.$(ARCH).o \
Index: /branches/eam_branches/ipp-20150616/Ohana/src/addstar/include/addstar.h
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/addstar/include/addstar.h	(revision 38543)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/addstar/include/addstar.h	(revision 38544)
@@ -220,7 +220,7 @@
 
 int        find_matches_closest   PROTO((SkyRegion *region, Catalog *newcat, Catalog *catalog, AddstarClientOptions options));
+int        find_matches        	  PROTO((SkyRegion *region, Catalog *newcat, Catalog *catalog, AddstarClientOptions options));
 
 # if (0)
-int        find_matches           	 PROTO((SkyRegion *region, Stars *stars,  unsigned int Nstars, Catalog *catalog, AddstarClientOptions options));
 int        find_matches_refstars  	 PROTO((SkyRegion *region, Stars **stars, unsigned int Nstars, Catalog *catalog, AddstarClientOptions options));
 int        find_matches_closest_refstars PROTO((SkyRegion *region, Stars **stars, unsigned int Nstars, Catalog *catalog, AddstarClientOptions options));
Index: /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/addstar.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/addstar.c	(revision 38543)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/addstar.c	(revision 38544)
@@ -150,5 +150,5 @@
 	  Nmatch += find_matches_closest (skylist[0].regions[i], newcat, &catalog, options);
 	} else {
-	  // XXX Nmatch += find_matches (skylist[0].regions[i], stars, Nstars, &catalog, options);
+	  Nmatch += find_matches (skylist[0].regions[i], newcat, &catalog, options);
 	}
 	break;
Index: /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/find_matches.c
===================================================================
--- /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/find_matches.c	(revision 38543)
+++ /branches/eam_branches/ipp-20150616/Ohana/src/addstar/src/find_matches.c	(revision 38544)
@@ -1,11 +1,11 @@
 # include "addstar.h"
 
-int find_matches (SkyRegion *region, Stars *stars, unsigned int NstarsIn, Catalog *catalog, AddstarClientOptions options) {
-
-  off_t i, j, n, N, J, Nstars;
+int find_matches (SkyRegion *region, Catalog *srccat, Catalog *tgtcat, AddstarClientOptions options) {
+
+  off_t i, j, n, N, J, Nstars, mSrc;
   double RADIUS, RADIUS2;
   double *X1, *Y1, *X2, *Y2;
   double dX, dY, dR;
-  off_t *N1, *N2, *next_meas;
+  off_t *N1, *N2;
   off_t Nave, NAVE, Nmeas, NMEAS, Nmatch;
   int Nsecfilt, Nsec, status;
@@ -13,9 +13,4 @@
   Coords tcoords;
 
-  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,23 +22,39 @@
 
   /** allocate local arrays (stars) **/
-  ALLOCATE (X1, double, NstarsIn);
-  ALLOCATE (Y1, double, NstarsIn);
-  ALLOCATE (N1, off_t,  NstarsIn);
-
-  /** allocate local arrays (catalog) **/
-  NAVE = Nave = catalog[0].Naverage;
+  ALLOCATE (X1, double, srccat->Naverage);
+  ALLOCATE (Y1, double, srccat->Naverage);
+  ALLOCATE (N1, off_t,  srccat->Naverage);
+
+  myAbort ("figure out if found_t needs to be tracking Naverage or Nmeasure"); 
+  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;
+  NMEAS = Nmeas = tgtcat[0].Nmeasure;
   
-  // current max obj ID for this catalog
-  objID = catalog[0].objID;
-  catID = catalog[0].catID;
+  off_t *next_meas = 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 ZEA projection has the
@@ -51,5 +62,5 @@
    * possible X,Y points map back to R,D and the local plate scale changes substantially far from
    * the projection pole.  a better mapping might be ARC, not yet implemented (see
-   * coordops.update.c).  We use the center of the region (catalog) for crval1,2. 
+   * coordops.update.c).  We use the center of the region (tgtcat) for crval1,2. 
    */
   InitCoords (&tcoords, "DEC--ARC");
@@ -64,6 +75,6 @@
   /* build spatial index (RA sort) referencing input array sequence */
   Nstars = 0;
-  for (i = 0; i < NstarsIn; i++) {
-    status = RD_to_XY (&X1[Nstars], &Y1[Nstars], stars[i].average.R, stars[i].average.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;
@@ -71,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);
@@ -84,15 +95,15 @@
   /* 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)) {
-    next_meas = init_measure_links (catalog[0].average, Nave, catalog[0].measure, Nmeas);
+  if (tgtcat[0].sorted && (tgtcat[0].Nmeasure == tgtcat[0].Nmeasure_disk)) {
+    next_meas = init_measure_links (tgtcat[0].average, Nave, tgtcat[0].measure, Nmeas);
   } else {
-    next_meas = build_measure_links (catalog[0].average, Nave, catalog[0].measure, Nmeas);
+    next_meas = build_measure_links (tgtcat[0].average, Nave, tgtcat[0].measure, Nmeas);
   }    
 
@@ -102,5 +113,4 @@
 
   /** find matched stars **/
-  // XXX could use NSTAR_GROUP to do this match more quicky if NSTAR_GROUP > 1
   for (i = j = 0; (i < Nstars) && (j < Nave); ) {
     if (!finite(X1[i]) || !finite(Y1[i])) { 
@@ -138,91 +148,96 @@
 
       /* 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);
-      }
-
-      /* add to end of measurement list */
-      add_meas_link (&catalog[0].average[n], next_meas, Nmeas, NMEAS);
+	REALLOCATE (tgtcat[0].measure, Measure, NMEAS);
+      }
 
       /** add measurements for this star **/
-
-      // set the new measurements
-      catalog[0].measure[Nmeas]          = stars[N].measure;
-
-      // the following measure elements cannot be set until here:
-      catalog[0].measure[Nmeas].dbFlags  = 0;
-      catalog[0].measure[Nmeas].averef   = n; // this must be an absolute sequence number, if partial average is loaded 
-      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 R:
-      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) {
-	// take declination into account and check again.
-	double cosD = cos(RAD_DEG*catalog[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, 
-		   stars[N].average.R, stars[N].average.D,
-		   X1[i], X2[J], 
-		   Y1[i], Y2[J]);
-	}
-      }
-
-      /* 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 if 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);
-	}
-      }
-
-      /*** flag multiple stars */
-      /* this image star matches more than one catalog star */
-      if (stars[N].found > -1) {
-	catalog[0].measure[stars[N].found].dbFlags |= ID_MEAS_BLEND_MEAS;
-	catalog[0].measure[Nmeas].dbFlags |= ID_MEAS_BLEND_MEAS;
-      } 
-      if (stars[N].found == -2) { /* this image star matches a catalog star on a neighboring catalog */
-	catalog[0].measure[Nmeas].dbFlags |= ID_MEAS_BLEND_MEAS_X;
-      } 
-      if (stars[N].found == -1) { /* this image star matches only this catalog star */
-	stars[N].found = Nmeas;  /* save first match, in case coincidences are found */
-      }
-      /* this catalog star matches more than one image star */
-      if (catalog[0].found_t[n] > -1) {
-	catalog[0].measure[catalog[0].found_t[n]].dbFlags |= ID_MEAS_BLEND_OBJ;
-	catalog[0].measure[Nmeas].dbFlags |= ID_MEAS_BLEND_OBJ;
-      } else {
-	catalog[0].found_t[n] = Nmeas;
-      }
-      /* 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 */
-      catalog[0].average[n].Nmeasure ++;
-      Nmeas ++;
+      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);
+	
+	/** add measurements for this star **/
+	
+	// set the new measurements
+	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   = n; // this must be an absolute sequence number, if partial average is loaded 
+	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 R:
+	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*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", 
+		     tgtcat[0].average[n].R, tgtcat[0].average[n].D, 
+		     srccat->average[N].R, srccat->average[N].D,
+		     X1[i], X2[J], 
+		     Y1[i], Y2[J]);
+	  }
+	}
+
+	/* 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 if photcode.equiv is not 0 */
+	/* in UPDATE mode, this value is not saved; use relphot to recalculate */
+	if (Nsec > -1) { 
+	  if (isnan(tgtcat[0].secfilt[n*Nsecfilt+Nsec].M)) {
+	    tgtcat[0].secfilt[n*Nsecfilt+Nsec].M = PhotCat (&tgtcat[0].measure[Nmeas], MAG_CLASS_PSF);
+	  }
+	}
+
+	myAbort ("fix logic on multiple detections");
+
+	/*** flag multiple stars */
+	/* this image star matches more than one tgtcat star */
+	if (srccat->found_t[N] > -1) {
+	  tgtcat[0].measure[srccat->found_t[N]].dbFlags |= ID_MEAS_BLEND_MEAS;
+	  tgtcat[0].measure[Nmeas].dbFlags |= ID_MEAS_BLEND_MEAS;
+	} 
+	if (srccat->found_t[N] == -2) { /* this image star matches a tgtcat star on a neighboring tgtcat */
+	  tgtcat[0].measure[Nmeas].dbFlags |= ID_MEAS_BLEND_MEAS_X;
+	} 
+	if (srccat->found_t[N] == -1) { /* this image star matches only this tgtcat star */
+	  srccat->found_t[N] = Nmeas;  /* save first match, in case coincidences are found */
+	}
+	/* this tgtcat star matches more than one image star */
+	if (tgtcat[0].found_t[n] > -1) {
+	  tgtcat[0].measure[tgtcat[0].found_t[n]].dbFlags |= ID_MEAS_BLEND_OBJ;
+	  tgtcat[0].measure[Nmeas].dbFlags |= ID_MEAS_BLEND_OBJ;
+	} else {
+	  tgtcat[0].found_t[n] = Nmeas;
+	}
+	/* 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 ++;
+      }
 
       if (!options.update) {
 	/* in UPDATE mode, newly calculated coordinates are not saved */
-	update_coords (&catalog[0].average[n], &catalog[0].measure[0], next_meas);
+	update_coords (&tgtcat[0].average[n], &tgtcat[0].measure[0], next_meas);
       }
     }
@@ -230,91 +245,95 @@
   }
 
-/* incorporate unmatched image stars, if this star is in field of this catalog */
+/* 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 += NSTAR_GROUP) {
+  for (i = 0; (i < Nstars) && !options.only_match; i ++) {
     /* make sure there is space for next entry */
-    if (Nmeas >= NMEAS - NSTAR_GROUP) {
-      NMEAS = Nmeas + 1000;
+    if (Nmeas >= NMEAS - srccat->average[i].Nmeasure) {
+      NMEAS = Nmeas + 1000 + srccat->average[i].Nmeasure;
       REALLOCATE (next_meas, off_t, NMEAS);
-      REALLOCATE (catalog[0].measure, Measure, NMEAS);
+      REALLOCATE (tgtcat[0].measure, Measure, NMEAS);
     }
     if (Nave >= NAVE) {
       NAVE = Nave + 1000;
-      REALLOCATE (catalog[0].average, Average, NAVE);
-      REALLOCATE (catalog[0].secfilt, SecFilt, NAVE*catalog[0].Nsecfilt);
-    }
-
-    if (stars[i].found >= 0) continue;
-    if (!IN_REGION (stars[i].average.R, stars[i].average.D)) continue;
-
-    dvo_average_init (&catalog[0].average[Nave]);
-    catalog[0].average[Nave].R         	   = stars[i].average.R;
-    catalog[0].average[Nave].D         	   = stars[i].average.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;
+      REALLOCATE (tgtcat[0].average, Average, 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] >= 0) 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;
 
     if (PSPS_ID) {
-      catalog[0].average[Nave].extID = CreatePSPSObjectID(catalog[0].average[Nave].R, catalog[0].average[Nave].D);
+      tgtcat[0].average[Nave].extID = CreatePSPSObjectID(tgtcat[0].average[Nave].R, tgtcat[0].average[Nave].D);
     }
 
     objID ++;
 
-    for (j = 0; j < Nsecfilt; j++) {
-      dvo_secfilt_init (&catalog[0].secfilt[Nave*Nsecfilt+j], SECFILT_RESET_ALL);
-    }
-
-    for (j = 0; j < NSTAR_GROUP; j++) {
+    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 measurments from this detection
-      catalog[0].measure[Nmeas]           = stars[i + j].measure;
+      dvo_measure_init (&tgtcat[0].measure[Nmeas]);
+      tgtcat[0].measure[Nmeas] = srccat->measure[mSrc + mOff];
 
       // the following measure elements cannot be set until here:
-      catalog[0].measure[Nmeas].dbFlags   = 0;
-      catalog[0].measure[Nmeas].averef    = Nave; // XXX EAM : must be absolute Nave if partial read
-      catalog[0].measure[Nmeas].objID     = catalog[0].average[Nave].objID;
-      catalog[0].measure[Nmeas].catID     = catalog[0].catID;
+      tgtcat[0].measure[Nmeas].dbFlags   = 0;
+      tgtcat[0].measure[Nmeas].averef    = Nave; // XXX EAM : must be absolute Nave if partial read
+      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) { 
-	catalog[0].secfilt[Nave*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas], MAG_CLASS_PSF);
+	tgtcat[0].secfilt[Nave*Nsecfilt+Nsec].M = PhotCat (&tgtcat[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].found = Nmeas;
-      next_meas[Nmeas] = -1;  // initial value here update below
+      srccat->found_t[i] = Nmeas;
       Nmeas ++;
-    }
-    for (j = 0; j < NSTAR_GROUP - 1; j++) {
-      next_meas[Nmeas - NSTAR_GROUP + j] = Nmeas - NSTAR_GROUP + j + 1;
+
+      // 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;
     }
     Nave ++;
   }
       
-  REALLOCATE (catalog[0].average, Average, Nave);
-  REALLOCATE (catalog[0].measure, Measure, Nmeas);
+  REALLOCATE (tgtcat[0].average, Average, Nave);
+  REALLOCATE (tgtcat[0].measure, Measure, Nmeas);
  
   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);
-  }
-
-/* note stars which have been found in this catalog */
-  for (i = 0; i < NstarsIn; i++) {
-    if (stars[i].found > -1) {
-      stars[i].found = -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].Nsecfilt_mem = Nave*Nsecfilt;
+    tgtcat[0].sorted = TRUE;
+    tgtcat[0].measure = sort_measure (tgtcat[0].average, Nave, tgtcat[0].measure, Nmeas, next_meas);
+  }
+
+/* note stars which have been found in this tgtcat */
+  for (i = 0; i < srccat->Nmeasure; 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].Nsecfilt_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);
 
@@ -333,10 +352,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
    
 */
