Index: /trunk/extsrc/gpcsw/gpcsrc/fits/burntool/burnfix.c
===================================================================
--- /trunk/extsrc/gpcsw/gpcsrc/fits/burntool/burnfix.c	(revision 42856)
+++ /trunk/extsrc/gpcsw/gpcsrc/fits/burntool/burnfix.c	(revision 42857)
@@ -13,4 +13,136 @@
 #include "burntool.h"
 #include "burnparams.h"
+
+
+#define DIFFERENT_STREAK_BURN 20	/* Proximity for union versus intersection */
+
+# define myMAXSIZE 20000
+
+
+/****************************************************************/
+/* burn_merge(): Disentangle overlapping burns */
+STATIC int burn_merge(CELL *cell)
+{
+   int i, j, k, n, nbox, xs, xe;
+   int jp, kp,xdiff;
+   int lapmax;
+   OBJBOX *box;
+   int xusage[myMAXSIZE], yctr[myMAXSIZE], boxid[myMAXSIZE];
+   double zk, zj;
+
+/* Look at all the boxes -- if not isolated then merge or sever */
+   nbox = cell->nburn;
+   box = cell->burn;
+
+/* Assess the clustering of all the streaks */
+   for(i=0; i<myMAXSIZE; i++) xusage[i] = 0;
+   for(i=0; i<myMAXSIZE; i++) boxid[i] = -1;
+   for(k=0; k<nbox; k++) {
+      if(box[k].ex >= myMAXSIZE-1) continue;
+      for(i=box[k].sx; i<=box[k].ex; i++) xusage[i] += 1;
+   }
+
+/* Identify clusters */
+   for(xs=0; xs<myMAXSIZE-1; xs++) {
+    if(xusage[xs] == 0) continue;
+      for(xe=xs, lapmax=0; xe<myMAXSIZE-1; xe++) {
+	     if(xusage[xe+1] == 0) break;
+	     lapmax = MAX(lapmax, xusage[xe]);
+      }
+      if(lapmax == 1) {	/* No overlap?  No problem. */
+	    xs = xe + 1;	/* Hop to next gap */
+	    continue;
+      }
+
+      /* Which boxes overlap this cluster? */
+      for(k=n=0; k<nbox; k++) {
+        if(box[k].sx > xe || box[k].ex < xs) continue;
+	     boxid[n] = k;
+	     //yctr[n] = (box[k].y0m+box[k].y1m+box[k].y0p+box[k].y1p+2) / 4;
+	     yctr[n] = (box[k].sy+box[k].ey+1) / 2;
+	     n++;
+      }
+
+      for(kp=0; kp<n; kp++) {
+	    k = boxid[kp];
+	    zk = 0.0;
+	    if(box[k].nfit > 0) zk = box[k].zero[box[k].nfit/2];
+	    for(jp=kp+1; jp<n; jp++) {
+	     j = boxid[jp];
+	     zj = 0.0;
+	     if(box[j].nfit > 0) zj = box[j].zero[box[j].nfit/2];
+	     if(box[j].sx > box[k].sx) xdiff = box[j].sx - box[k].ex;
+	     if(box[j].sx <= box[k].sx) xdiff = box[k].sx - box[j].ex;
+
+	     /* CZW: Since I added the initialization statement above, */
+	     /*      any box with boxid == -1 hasn't been set, and */
+	     /*      therefore needs to be dropped, I think. */
+	     if ((k < 0)||(j < 0)) { continue; }
+
+        /*Tdb20220222: In the case of overlapping x-coords but not overlapping y-coords*/
+        /*I fail to see the need to change the x-range of any streaks. That just spells disaster*/
+        // Case 1: different y start => sever 
+	     if(ABS(yctr[jp]-yctr[kp]) > DIFFERENT_STREAK_BURN) {
+         // Trim back the feebler streak 
+	      if(zk > zj) {
+		    /*
+		    if(box[j].sxfit >= box[k].sxfit) 
+	       box[j].sxfit = MAX(box[j].sxfit, box[k].exfit+1);
+		    if(box[j].exfit <= box[k].exfit) 
+		    box[j].exfit = MIN(box[j].exfit, box[k].sxfit-1);
+		    */
+	      } else {
+	       /*	
+          if(box[k].sxfit >= box[j].sxfit) 
+		    box[k].sxfit = MAX(box[k].sxfit, box[j].exfit+1);
+		    if(box[k].exfit <= box[j].exfit) 
+		    box[k].exfit = MIN(box[k].exfit, box[j].sxfit-1);
+		    */
+	      }
+	     }
+
+	     /* Case 2: pretty much the same y start => union */
+	     if((ABS(yctr[jp]-yctr[kp]) <= DIFFERENT_STREAK_BURN) && (xdiff < 0) ) {
+          /* Merge k into j, trash k from further consideration */
+	       box[j].time = MAX(box[j].time, box[k].time);
+	       box[j].sx = MIN(box[j].sx, box[k].sx);
+	       box[j].ex = MAX(box[j].ex, box[k].ex);
+	       box[j].sy = MIN(box[j].sy, box[k].sy);
+	       box[j].ey = MAX(box[j].ey, box[k].ey);
+	       box[j].y0m = MIN(box[j].y0m, box[k].y0m);
+	       box[j].y0p = MAX(box[j].y0p, box[k].y0p);
+	       box[j].x0m = MIN(box[j].x0m, box[k].x0m);
+	       box[j].x0p = MAX(box[j].x0p, box[k].x0p);
+	       box[j].y1m = MIN(box[j].y1m, box[k].y1m);
+	       box[j].y1p = MAX(box[j].y1p, box[k].y1p);
+	       box[j].x1m = MIN(box[j].x1m, box[k].x1m);
+	       box[j].x1p = MAX(box[j].x1p, box[k].x1p);
+	       box[j].sxfit = MIN(box[j].sxfit, box[k].sxfit);
+	       box[j].exfit = MAX(box[j].exfit, box[k].exfit);
+	       box[j].eyfit = MIN(box[j].eyfit, box[k].eyfit);
+
+	       box[k].exfit = -999;
+	       yctr[kp] = -2 * DIFFERENT_STREAK_BURN;
+	    }
+	 }
+      }
+
+      xs = xe + 1;	/* Hop to next gap */
+
+   } /* Cluster loop */
+
+/* Excise the boxes which have been eradicated (sxfit > exfit) */
+   for(k=0; k<nbox; k++) {
+      if(box[k].exfit < 0) {
+	    for(j=k; j<nbox-1; j++) memcpy(box+j, box+j+1, sizeof(OBJBOX));
+	    nbox--;
+	    k--;
+      }
+   }
+   cell->nburn = nbox;
+
+
+   return(0);
+}
 
 /****************************************************************/
@@ -47,4 +179,8 @@
       if(!cell->burn[k].fiterr) sub_fit(nx, ny, NX, buf, cell->burn+k, 1);
    }
+
+
+/* Merge all overlapping streaks */
+   burn_merge(cell);
 
 /* For testing, blow away the data in favor of the mask */
Index: /trunk/extsrc/gpcsw/gpcsrc/fits/burntool/burntool.h
===================================================================
--- /trunk/extsrc/gpcsw/gpcsrc/fits/burntool/burntool.h	(revision 42856)
+++ /trunk/extsrc/gpcsw/gpcsrc/fits/burntool/burntool.h	(revision 42857)
@@ -181,4 +181,5 @@
 STATIC int persist_fix(int nx, int ny, int stride, IMTYPE *buf, CELL *cell,char *camera);
 STATIC int persist_merge(CELL *cell);
+STATIC int burn_merge(CELL *cell);
 
 //fh_result persist_fits_read(CELL *cell, const char *filename, int apply);
