Index: /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSource.c
===================================================================
--- /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSource.c	(revision 30774)
+++ /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSource.c	(revision 30775)
@@ -1166,4 +1166,23 @@
 }
 
+// sort by SN (descending)
+int pmSourceSortByParentSN (const void **a, const void **b)
+{
+    pmSource *Ao = *(pmSource **)a;
+    pmSource *Bo = *(pmSource **)b;
+    pmSource *A  = Ao->parent;
+    pmSource *B  = Bo->parent;
+
+    psF32 fA = (A->peak == NULL) ? 0 : A->peak->SN;
+    psF32 fB = (B->peak == NULL) ? 0 : B->peak->SN;
+    if (isnan (fA)) fA = 0;
+    if (isnan (fB)) fB = 0;
+
+    psF32 diff = fA - fB;
+    if (diff > FLT_EPSILON) return (-1);
+    if (diff < FLT_EPSILON) return (+1);
+    return (0);
+}
+
 // sort by Y (ascending)
 int pmSourceSortByY (const void **a, const void **b)
@@ -1201,4 +1220,21 @@
     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);
+}
+
+// sort by Seq (ascending)
+int pmSourceSortByParentSeq (const void **a, const void **b)
+{
+    pmSource *Ao = *(pmSource **)a;
+    pmSource *Bo = *(pmSource **)b;
+    pmSource *A  = Ao->parent;
+    pmSource *B  = Bo->parent;
 
     int iA = A->seq;
Index: /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSource.h
===================================================================
--- /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSource.h	(revision 30774)
+++ /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSource.h	(revision 30775)
@@ -286,8 +286,10 @@
 bool pmSourceCachePSF (pmSource *source, psImageMaskType maskVal);
 
-int  pmSourceSortBySN (const void **a, const void **b);
 int  pmSourceSortByY (const void **a, const void **b);
 int  pmSourceSortByX (const void **a, const void **b);
 int  pmSourceSortBySeq (const void **a, const void **b);
+int  pmSourceSortByParentSeq (const void **a, const void **b);
+int  pmSourceSortBySN (const void **a, const void **b);
+int  pmSourceSortByParentSN (const void **a, const void **b);
 
 pmSourceMode pmSourceModeFromString (const char *name);
Index: /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSourceIO_CMF_PS1_SV1.c
===================================================================
--- /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSourceIO_CMF_PS1_SV1.c	(revision 30774)
+++ /branches/eam_branches/ipp-20110213/psModules/src/objects/pmSourceIO_CMF_PS1_SV1.c	(revision 30775)
@@ -70,10 +70,10 @@
     pmChip *chip = readout->parent->parent;
 
-    // if the sequence is defined, write these in seq order; otherwise
-    // write them in S/N order:
+    // if the sequence is defined, write these in seq order; otherwise write them in S/N order.
+    // Careful: if we are working with child sources, then we need to sort by the parent info,
+    // not our info
     if (sources->n > 0) {
-        pmSource *source = (pmSource *) sources->data[0];
+        pmSource *source = sources->data[0];
         if (source->seq == -1) {
-	    // let's write these out in S/N order
 	    sources = psArraySort (sources, pmSourceSortBySN);
         } else {
@@ -94,10 +94,15 @@
     // by the time we call this function, all values should be assigned.  let's use asserts to be sure in some cases.
     for (int i = 0; i < sources->n; i++) {
-        pmSource *source = (pmSource *) sources->data[i];
-
-        // If source->seq is -1, source was generated in this analysis.  If source->seq is
-        // not -1, source was read from elsewhere: in the latter case, preserve the source
-        // ID.  source.seq is used instead of source.id since the latter is a const
-        // generated on Alloc, and would thus be wrong for read in sources.
+	// this is the source associated with this image
+        pmSource *thisSource = sources->data[i];
+
+	// this is the "real" version of this source 
+	pmSource *source = thisSource->parent ? thisSource->parent : thisSource;
+
+        // If source->seq is -1, source is unique and generated in this analysis.  If
+        // source->seq is not -1, source was read from elsewhere or tied to other source (eg
+        // from another image): in the latter case, preserve the source ID.  source.seq is used
+        // instead of source.id since the latter is a const generated on Alloc, and would thus
+        // be wrong for read in sources.
         if (source->seq == -1) {
 	    source->seq = i;
@@ -398,5 +403,9 @@
     // we write out all sources, regardless of quality.  the source flags tell us the state
     for (int i = 0; i < sources->n; i++) {
-        pmSource *source = sources->data[i];
+	// this is the source associated with this image
+        pmSource *thisSource = sources->data[i];
+
+	// this is the "real" version of this source 
+	pmSource *source = thisSource->parent ? thisSource->parent : thisSource;
 
         // skip sources without measurements
@@ -555,5 +564,10 @@
     int nParamMax = 0;
     for (int i = 0; i < sources->n; i++) {
-        pmSource *source = sources->data[i];
+	// this is the source associated with this image
+        pmSource *thisSource = sources->data[i];
+
+	// this is the "real" version of this source 
+	pmSource *source = thisSource->parent ? thisSource->parent : thisSource;
+
         if (source->modelFits == NULL) continue;
         for (int j = 0; j < source->modelFits->n; j++) {
@@ -569,5 +583,8 @@
     for (int i = 0; i < sources->n; i++) {
 
-        pmSource *source = sources->data[i];
+        pmSource *thisSource = sources->data[i];
+
+	// this is the "real" version of this source 
+	pmSource *source = thisSource->parent ? thisSource->parent : thisSource;
 
         // XXX if no model fits are saved, write out modelEXT?
@@ -719,8 +736,13 @@
     for (int i = 0; i < sources->n; i++) {
 
-        pmSource *source = sources->data[i];
+	// this is the source associated with this image
+        pmSource *thisSource = sources->data[i];
+
+	// this is the "real" version of this source 
+	pmSource *source = thisSource->parent ? thisSource->parent : thisSource;
 
         // skip sources without radial aper measurements (or insufficient)
-        if (source->radialAper == NULL) continue;
+	if (source->radialAper == NULL) continue;
+
         // psAssert (source->radialAper->n == fwhmValues->n, "inconsistent radial aperture set");
 
