Index: trunk/psModules/src/objects/pmSource.c
===================================================================
--- trunk/psModules/src/objects/pmSource.c	(revision 21363)
+++ trunk/psModules/src/objects/pmSource.c	(revision 21513)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA: significant modifications.
  *
- *  @version $Revision: 1.69 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2009-02-06 02:31:25 $
+ *  @version $Revision: 1.70 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2009-02-16 22:29:59 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -113,4 +113,5 @@
     source->type = PM_SOURCE_TYPE_UNKNOWN;
     source->mode = PM_SOURCE_MODE_DEFAULT;
+    source->tmpFlags = 0;
     source->extpars = NULL;
     source->region = psRegionSet(NAN, NAN, NAN, NAN);
@@ -534,4 +535,6 @@
     float INNER_RADIUS = psMetadataLookupF32 (&status, recipe, "SKY_INNER_RADIUS");
 
+    pmSourceMode noMoments = PM_SOURCE_MODE_MOMENTS_FAILURE | PM_SOURCE_MODE_SKYVAR_FAILURE | PM_SOURCE_MODE_SKY_FAILURE | PM_SOURCE_MODE_BELOW_MOMENTS_SN;
+
     // XXX allow clump size to be scaled relative to sigmas?
     // make rough IDs based on clumpX,Y,DX,DY
@@ -540,14 +543,18 @@
         pmSource *source = (pmSource *) sources->data[i];
 
-        if (source->peak->x < region->x0) continue;
-        if (source->peak->x > region->x1) continue;
-        if (source->peak->y < region->y0) continue;
-        if (source->peak->y > region->y1) continue;
-
-        source->peak->type = 0;
+	// psf clumps are found for image subregions:
+	// skip sources not in this region 
+        if (source->peak->x <  region->x0) continue;
+        if (source->peak->x >= region->x1) continue;
+        if (source->peak->y <  region->y0) continue;
+	if (source->peak->y >= region->y1) continue;
+
+	// should be set by pmSourceAlloc
+        psAssert (source->type == PM_SOURCE_TYPE_UNKNOWN, "source type was not init-ed?");
 
         // we are basically classifying by moments; use the default if not found
         if (!source->moments) {
             source->type = PM_SOURCE_TYPE_STAR;
+	    psAssert (source->mode & noMoments, "why is this source missing moments?");
             Nstar++;
             continue;
@@ -1049,4 +1056,19 @@
 }
 
+// sort by Seq (ascending)
+int pmSourceSortBySeq (const void **a, const void **b)
+{
+    pmSource *A = *(pmSource **)a;
+    pmSource *B = *(pmSource **)b;
+
+    int iA = A->seq;
+    int iB = B->seq;
+
+    int diff = iA - iB;
+    if (diff > 0) return (+1);
+    if (diff < 0) return (-1);
+    return (0);
+}
+
 pmSourceMode pmSourceModeFromString (const char *name) {
   if (!strcasecmp (name, "DEFAULT"   )) return PM_SOURCE_MODE_DEFAULT;
@@ -1066,5 +1088,4 @@
   if (!strcasecmp (name, "CRLIMIT"   )) return PM_SOURCE_MODE_CR_LIMIT;
   if (!strcasecmp (name, "EXTLIMIT"  )) return PM_SOURCE_MODE_EXT_LIMIT;
-  if (!strcasecmp (name, "SUBTRACTED")) return PM_SOURCE_MODE_SUBTRACTED;
   return PM_SOURCE_MODE_DEFAULT;
 }
@@ -1088,5 +1109,4 @@
     case PM_SOURCE_MODE_CR_LIMIT   : return psStringCopy ("CRLIMIT"   );
     case PM_SOURCE_MODE_EXT_LIMIT  : return psStringCopy ("EXTLIMIT"  );
-    case PM_SOURCE_MODE_SUBTRACTED : return psStringCopy ("SUBTRACTED");
     default:
       return NULL;
