Index: trunk/psphot/src/psphotSourcePlots.c
===================================================================
--- trunk/psphot/src/psphotSourcePlots.c	(revision 12626)
+++ trunk/psphot/src/psphotSourcePlots.c	(revision 12627)
@@ -10,11 +10,12 @@
     PS_ASSERT (status);
 
-    int NX = 10*(2*OUTER+1);
-    int NY = 0;
-
     // examine PSF sources in S/N order (brightest first)
     sources = psArraySort (sources, psphotSortBySN);
 
-    bool newRow = true;
+    // counters to track the size of the image and area used in a row
+    int dX = 0;				// starting corner of next box
+    int dY = 0;				// height of row so far
+    int NX = 10*(2*OUTER+1);		// full width of output image
+    int NY = 0;				// total height of output image
 
     // first, examine the PSF and SAT stars:
@@ -30,25 +31,33 @@
 	if (!keep) continue;
 
-	if (newRow) {
-	    dX = 0;
-	    dY = 0;
-	    newRow = false;
+	// how does this subimage get placed into the output image?
+	if (dX + source->pixels->numCols >= NX) {
+	    // too wide for the rest of this row
+	    if (dX == 0) {
+		// alone on this row
+		NY += source->pixels->numRows;
+		dX = 0;
+		dY = 0;
+	    } else {
+		// start the next row
+		NY += dY;
+		dX = source->pixels->numCols;
+		dY = source->pixels->numRows;
+	    }
+	} else {
+	    // extend this row
+	    dX += source->pixels->numCols;
+	    dY = PS_MAX (dY, source->pixels->numRows);
 	}
-
-	if (dX + source->pixels->numCols > NX) {
-	    // go to the next row
-	    // but, if we have only this one entry, we will truncate it.
-	}
-	dX += source->pixels->numCols;
-	dY = PS_MAX (dY, source->pixels->numRows);
-
-	// for a new row:
-	NY += dY;
-
-	RadialPlot (source);
+	psphotRadialPlot ("radial.plots.ps", source);
     }
 
     // allocate output image
-    psImage *output = psImageAlloc (NX, NY, PS_TYPE_F32);
+    psImage *outpos = psImageAlloc (NX, NY, PS_TYPE_F32);
+    psImage *outsub = psImageAlloc (NX, NY, PS_TYPE_F32);
+
+    int Xo = 0;				// starting corner of next box
+    int Yo = 0;				// starting corner of next box
+    int dY = 0;				// height of row so far
 
     // first, examine the PSF and SAT stars:
@@ -64,11 +73,31 @@
 	if (!keep) continue;
 
-	// add to image
+	// how does this subimage get placed into the output image?
+	if (Xo + source->pixels->numCols >= NX) {
+	    // too wide for the rest of this row
+	    if (Xo == 0) {
+		// alone on this row
+		PlaceImage (source, Xo, Yo);
+		Yo += source->pixels->numRows;
+		Xo = 0;
+		dY = 0;
+	    } else {
+		// start the next row
+		Yo += dY;
+		Xo = 0;
+		PlaceImage (source, Xo, Yo);
+		Xo = source->pixels->numCols;
+		dY = source->pixels->numRows;
+	    }
+	} else {
+	    // extend this row
+	    PlaceImage (source, Xo, Yo);
+	    Xo += source->pixels->numCols;
+	    dY = PS_MAX (dY, source->pixels->numRows);
+	}
     }
 
     psLogMsg ("psphot", PS_LOG_INFO, "%ld sources, %d moments, %d failed: %f sec\n", sources->n, Nmoments, Nfail, psTimerMark ("psphot"));
-
     return (sources);
 }
 
-// XXX EAM : filter out bad peaks (eg, no valid pixels available for sky)
