Index: /trunk/psphot/src/psphotSourceMatch.c
===================================================================
--- /trunk/psphot/src/psphotSourceMatch.c	(revision 38389)
+++ /trunk/psphot/src/psphotSourceMatch.c	(revision 38390)
@@ -700,2 +700,63 @@
 
 
+psArray *psphotLinkSources (pmConfig *config, const pmFPAview *view, const char *filerule) 
+{
+
+    int num = psphotFileruleCount(config, filerule);
+
+    psArray *sourcesArrays = psArrayAlloc(num);
+
+    // loop over inputs find the maximum sequence number and save pointers to the arrays
+    int seqMax = -1;
+    for (int i = 0; i < num; i++) {
+
+	// find the currently selected readout
+	pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, i); // File of interest
+	psAssert (file, "missing file?");
+
+	pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
+	psAssert (readout, "missing readout?");
+
+	pmDetections *detections = psMetadataLookupPtr (NULL, readout->analysis, "PSPHOT.DETECTIONS");
+	psAssert (detections, "missing detections?");
+        psAssert (detections->allSources, "all sources not defined?");
+
+        // allSources value won't change but should be careful
+        detections->allSources = psArraySort(detections->allSources, pmSourceSortBySeq);
+
+        sourcesArrays->data[i] = psMemIncrRefCounter(detections->allSources);
+        pmSource *lastSrc = detections->allSources->data[detections->allSources->n -1 ];
+        int thisSeq = lastSrc->seq;
+        if (thisSeq > seqMax) {
+            seqMax = thisSeq;
+        }
+    }
+
+    if (seqMax < 0) {
+        psError (PSPHOT_ERR_UNKNOWN, true, "failed to find maximum sequence number\n");
+        return NULL;
+    }
+
+    // allocate objects array
+    psArray *objects = psArrayAlloc(seqMax + 1);
+    // loop over inputs and create objects that reference all of the sources
+    for (int i = 0; i < num; i++) {
+        // sources for this input
+        psArray *sources = sourcesArrays->data[i];
+        for (int j = 0; j < sources->n ; j++) {
+            pmSource *src = sources->data[j];
+            //  XXX       This is no longer needed I think. I added it to work around something else. Check
+            src->imageID = i;
+            int seq = src->seq;
+            pmPhotObj *obj = objects->data[seq];
+            if (!obj) {
+                // first source for this object
+                obj = objects->data[seq] = pmPhotObjAlloc();
+            }
+            pmPhotObjAddSource(obj, src);
+        }
+    }
+    psFree(sourcesArrays);
+
+    return objects;
+}
