Index: /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/burnfix.c
===================================================================
--- /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/burnfix.c	(revision 26766)
+++ /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/burnfix.c	(revision 26767)
@@ -72,5 +72,5 @@
       ymid = (cell->burn[k].y0m + cell->burn[k].y1m +
 	      cell->burn[k].y0p + cell->burn[k].y1p + 2) / 4;
-      i = ABS(cell->burn[k].nfit) / 2;
+      i = cell->burn[k].nfit / 2;
       printf("%3d %5d %3d %5d %5d %3d %5d %3d %5d %5d %2d %1d %1d %6.3f %8d\n", 
 	     k, 
@@ -109,6 +109,9 @@
 /* Restore all the burns */
    for(k=0; k<cell->npersist; k++) {
-      if(!cell->persist[k].fiterr) 
+      if(!cell->persist[k].fiterr && 
+	 (cell->persist[k].func == BURN_PWR || 
+	  cell->persist[k].func == BURN_EXP) ) {
 	 sub_fit(nx, ny, NX, buf, &(cell->persist[k]), -1);
+      }
    }
    return(0);
@@ -124,6 +127,9 @@
 /* Restore all the burns */
    for(k=0; k<cell->npersist; k++) {
-      if(!cell->persist[k].fiterr) 
+      if(!cell->persist[k].fiterr && 
+	 (cell->persist[k].func == BURN_PWR || 
+	  cell->persist[k].func == BURN_EXP) ) {
 	 sub_fit(nx, ny, NX, buf, &(cell->persist[k]), +1);
+      }
    }
    return(0);
Index: /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/burntool.c
===================================================================
--- /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/burntool.c	(revision 26766)
+++ /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/burntool.c	(revision 26767)
@@ -364,7 +364,7 @@
 /* Read the persistence data for this OTA */
    if(persistfile != NULL) {			/* Text data file */
-      if(persist_read(OTA, persistfile)) exit(-317);
+      if(persist_read(OTA, persistfile, apply)) exit(-317);
    } else if(persistfitsfile != NULL) {		/* FITS table */
-      if(persist_fits_read(OTA, persistfitsfile) != FH_SUCCESS)
+      if(persist_fits_read(OTA, persistfitsfile, apply) != FH_SUCCESS)
 	 exit(-318);
    }
@@ -770,5 +770,5 @@
    printf(" restore={t|f}  Restore the input MEF by adding input fits?\n");
    printf(" apply={t|f}    Modify the input MEF by subtracting previously calculated fits?\n");
-   printf(" tableonly={t|f} Calculate fits but do *not* modify the input MEF images, only write tables\n");
+   printf(" tableonly={t|f} Calculate fits but do *not* modify the input MEF pixels, only write tables\n");
    printf(" in=fname       Input file for previous burn persistence streaks\n");
    printf(" infits=fname   Input FITS file for previous burn persistence streaks (stored\n");
Index: /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/burntool.h
===================================================================
--- /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/burntool.h	(revision 26766)
+++ /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/burntool.h	(revision 26767)
@@ -91,4 +91,5 @@
 #define BURN_EXP  2		/* Exponential */
 #define BURN_BLASTED 3		/* Blasted top to bottom: flag only for IPP */
+#define BURN_POSSLOPE 4		/* Positive slope fit (bad) but significant */
 #define PSF_STAR  9		/* Unfitted: good psf star */
 
@@ -175,8 +176,12 @@
 STATIC int burn_restore(int nx, int ny, int NX, IMTYPE *buf, CELL *cell);
 STATIC int burn_apply(int nx, int ny, int NX, IMTYPE *buf, CELL *cell);
-STATIC int persist_read(CELL *cell, const char *infile);
+STATIC int persist_read(CELL *cell, const char *infile, int apply);
 STATIC int persist_write(CELL *cell, const char *outfile);
 STATIC int persist_fix(int nx, int ny, int stride, IMTYPE *buf, CELL *cell);
 STATIC int persist_merge(CELL *cell);
+
+//fh_result persist_fits_read(CELL *cell, const char *filename, int apply);
+//fh_result persist_fits_write(CELL *cell, HeaderUnit phu);
+//fh_result persist_fits_remove_tables(HeaderUnit phu_in, const char *fileout);
 
 STATIC int star_detect(int nx, int ny, int NX, int NY, DTYPE *data,
Index: /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/man/burntool.1
===================================================================
--- /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/man/burntool.1	(revision 26766)
+++ /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/man/burntool.1	(revision 26767)
@@ -61,5 +61,7 @@
 	until they finally achieve a legal fit with negligible amplitude.
 	Note that if burntool decides that a fit had a bad slope but was 
-	non-negligible it negates the "nfit" width parameter of the fit box.
+	non-negligible it writes a function type "BURN_POSSLOPE" = 4 so
+	the box is available for masking if desired, and can be refitted
+	on subsequent images.
 
 	Burntool also identifies really blasted areas which are saturated from
@@ -229,5 +231,5 @@
 
 	tableonly={t|f}
-		Calculate fits but do *not* modify the input MEF images, 
+		Calculate fits but do *not* modify the input MEF pixels, 
 		only write the fit data as a FITS and/or text table (default f)
 
Index: /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persist_fits.c
===================================================================
--- /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persist_fits.c	(revision 26766)
+++ /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persist_fits.c	(revision 26767)
@@ -52,4 +52,5 @@
    AREA_TABLE_COL_SXFIT,
    AREA_TABLE_COL_EXFIT,
+   AREA_TABLE_COL_FITERR,
    
    /* Add new columns above this line. */
@@ -86,4 +87,5 @@
    { "sxfit", "Starting column for fit",              "pixels",  FH_TABLE_FORMAT_INT,    3,     0 },
    { "exfit", "Ending column for fit",                "pixels",  FH_TABLE_FORMAT_INT,    3,     0 },
+   { "fiterr", "Error code of fit",                "",  FH_TABLE_FORMAT_INT,    3,     0 },
 };
 
@@ -237,5 +239,5 @@
 	 }
          num_areas++;
-         num_fits += ABS(cell[j].persist[k].nfit);
+         num_fits += cell[j].persist[k].nfit;
       }
 
@@ -257,5 +259,5 @@
 	 }
          num_areas++;
-         num_fits += ABS(cell[j].burn[k].nfit);
+         num_fits += cell[j].burn[k].nfit;
       }
    }
@@ -310,5 +312,6 @@
       (fh_table_write_value(table, data, row, AREA_TABLE_COL_NFIT,  &(area->nfit))  != FH_SUCCESS) ||
       (fh_table_write_value(table, data, row, AREA_TABLE_COL_SXFIT, &(area->sxfit)) != FH_SUCCESS) ||
-      (fh_table_write_value(table, data, row, AREA_TABLE_COL_EXFIT, &(area->exfit)) != FH_SUCCESS))
+      (fh_table_write_value(table, data, row, AREA_TABLE_COL_EXFIT, &(area->exfit)) != FH_SUCCESS) ||
+      (fh_table_write_value(table, data, row, AREA_TABLE_COL_FITERR, &(area->fiterr)) != FH_SUCCESS))
    {
       fprintf(stderr, "\rerror: Error writing data to row %d of area table.\n", row);
@@ -480,5 +483,5 @@
 	    if(cell[j].persist[k].fiterr) continue;
 	 }
-	 for(i=0; i<ABS(cell[j].persist[k].nfit); i++) 
+	 for(i=0; i<cell[j].persist[k].nfit; i++) 
          {
             result = write_fit_row(hu, data, table, row++,
@@ -505,5 +508,5 @@
 	 }
 
-	 for(i=0; i<ABS(cell[j].burn[k].nfit); i++) 
+	 for(i=0; i<cell[j].burn[k].nfit; i++) 
          {
             result = write_fit_row(hu, data, table, row++,
@@ -538,5 +541,5 @@
  */
 static fh_result
-read_area(fhTable * table, void * data, int row)
+read_area(fhTable * table, void * data, int row, int apply)
 {
    int cell_num;
@@ -585,5 +588,6 @@
       (fh_table_read_value(table, data, row, AREA_TABLE_COL_NFIT,  &(boxbuf[row].nfit)) != FH_SUCCESS) ||
       (fh_table_read_value(table, data, row, AREA_TABLE_COL_SXFIT, &(boxbuf[row].sxfit)) != FH_SUCCESS) ||
-      (fh_table_read_value(table, data, row, AREA_TABLE_COL_EXFIT, &(boxbuf[row].exfit)) != FH_SUCCESS))
+      (fh_table_read_value(table, data, row, AREA_TABLE_COL_EXFIT, &(boxbuf[row].exfit)) != FH_SUCCESS) ||
+      (fh_table_read_value(table, data, row, AREA_TABLE_COL_FITERR, &(boxbuf[row].fiterr)) != FH_SUCCESS))
    {
       fprintf(stderr,
@@ -595,5 +599,5 @@
    
    /* Make space for fits (to be read later). */
-   if(ABS(boxbuf[row].nfit) > 0)
+   if(boxbuf[row].nfit > 0)
    {
       /* This shouldn't happen, but be nice and don't leak. */
@@ -602,7 +606,7 @@
       if(boxbuf[row].yfit) free(boxbuf[row].yfit);
 
-      boxbuf[row].zero = (double *)calloc(ABS(boxbuf[row].nfit), sizeof(double));
-      boxbuf[row].xfit = (int *)calloc(ABS(boxbuf[row].nfit), sizeof(int));
-      boxbuf[row].yfit = (int *)calloc(ABS(boxbuf[row].nfit), sizeof(int));
+      boxbuf[row].zero = (double *)calloc(boxbuf[row].nfit, sizeof(double));
+      boxbuf[row].xfit = (int *)calloc(boxbuf[row].nfit, sizeof(int));
+      boxbuf[row].yfit = (int *)calloc(boxbuf[row].nfit, sizeof(int));
 
       if(boxbuf[row].zero == NULL ||
@@ -612,5 +616,6 @@
 	 exit(-671);
       }
-      boxbuf[row].fiterr = 0;
+// 100203 JT: fiterr now saved and read, refit if not just an "apply"
+      if(!apply) boxbuf[row].fiterr = 0;
    }
 
@@ -635,5 +640,5 @@
  */
 static fh_result
-read_area_table(HeaderUnit hu, fhTable * table)
+read_area_table(HeaderUnit hu, fhTable * table, int apply)
 {
    fh_result result = FH_INVALID;
@@ -688,5 +693,5 @@
    for(i = 0; i < num_rows; i++)
    {
-      if((result = read_area(table, data, i)) != FH_SUCCESS) break;
+      if((result = read_area(table, data, i, apply)) != FH_SUCCESS) break;
    }
 
@@ -840,5 +845,5 @@
    for(area = 0; area < num_areas; area++)
    {
-      for(i = 0; i < ABS(boxbuf[area].nfit); i++)
+      for(i = 0; i < boxbuf[area].nfit; i++)
       {
          if((result = read_fit(hu, table, data, fit_table_row, 
@@ -901,5 +906,5 @@
  */
 fh_result
-persist_fits_read(CELL *cell, const char * filename)
+persist_fits_read(CELL *cell, const char * filename, int apply)
 {
    HeaderUnit phu;
@@ -938,5 +943,5 @@
 
    fh_ehu_by_extname(phu, DEFAULT_EXTNAME_AREA_TABLE);
-   if((result = read_area_table(area_hu, &area_table)) != FH_SUCCESS)
+   if((result = read_area_table(area_hu, &area_table, apply)) != FH_SUCCESS)
    {
       free(area_table.strbuf);
Index: /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persist_fits.h
===================================================================
--- /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persist_fits.h	(revision 26766)
+++ /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persist_fits.h	(revision 26767)
@@ -24,5 +24,5 @@
 
 fh_result
-persist_fits_read(CELL *cell, const char * filename);
+persist_fits_read(CELL *cell, const char * filename, int apply);
 
 fh_result
Index: /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persistfix.c
===================================================================
--- /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persistfix.c	(revision 26766)
+++ /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persistfix.c	(revision 26767)
@@ -34,4 +34,9 @@
 	 }
 	 continue;
+      }
+
+/* This had a significant positive slope, what do we do now? */
+      if( (cell->persist)[k].func == BURN_POSSLOPE) {
+/* try again, so let it slide through... */
       }
 
Index: /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persistio.c
===================================================================
--- /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persistio.c	(revision 26766)
+++ /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/persistio.c	(revision 26767)
@@ -16,5 +16,5 @@
 /****************************************************************/
 /* persist_read(): Read all the persistence trails from a file */
-STATIC int persist_read(CELL *cell, const char *infile)
+STATIC int persist_read(CELL *cell, const char *infile, int apply)
 {
    int i, k, nbox=0;
@@ -38,5 +38,5 @@
    while(fgets(line, 1024, fp) != NULL) {
       if(line[0] == '#') continue;
-      sscanf(line, "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %lf %d %d %d",
+      sscanf(line, "%d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %lf %d %d %d %d",
 	     &boxbuf[nbox].cell, &boxbuf[nbox].time,
 	     &boxbuf[nbox].cx, &boxbuf[nbox].cy,
@@ -50,9 +50,10 @@
 	     &boxbuf[nbox].func, &boxbuf[nbox].up,
 	     &boxbuf[nbox].slope, &boxbuf[nbox].nfit,
-	     &boxbuf[nbox].sxfit, &boxbuf[nbox].exfit);
-      if(ABS(boxbuf[nbox].nfit) > 0) {
-	 boxbuf[nbox].zero = (double *)calloc(ABS(boxbuf[nbox].nfit), sizeof(double));
-	 boxbuf[nbox].xfit = (int *)calloc(ABS(boxbuf[nbox].nfit), sizeof(int));
-	 boxbuf[nbox].yfit = (int *)calloc(ABS(boxbuf[nbox].nfit), sizeof(int));
+	     &boxbuf[nbox].sxfit, &boxbuf[nbox].exfit,
+	     &boxbuf[nbox].fiterr);
+      if(boxbuf[nbox].nfit > 0) {
+	 boxbuf[nbox].zero = (double *)calloc(boxbuf[nbox].nfit, sizeof(double));
+	 boxbuf[nbox].xfit = (int *)calloc(boxbuf[nbox].nfit, sizeof(int));
+	 boxbuf[nbox].yfit = (int *)calloc(boxbuf[nbox].nfit, sizeof(int));
 	 if(boxbuf[nbox].zero == NULL ||
 	    boxbuf[nbox].xfit == NULL ||
@@ -62,5 +63,5 @@
 	 }
 
-	 for(i=0; i<ABS(boxbuf[nbox].nfit); i++) {
+	 for(i=0; i<boxbuf[nbox].nfit; i++) {
 	    if(fgets(line, 1024, fp) == NULL) {
 	       fprintf(stderr, "\rerror: short read of burn lines\n");
@@ -71,5 +72,6 @@
 	 }
       }
-      boxbuf[nbox].fiterr = 0;
+// 100203 JT: fiterr now saved and read, refit if not just an "apply"
+      if(!apply) boxbuf[nbox].fiterr = 0;
 /* Augment counts */
       k = boxbuf[nbox].cell;
@@ -159,9 +161,9 @@
 	 k = boxid[kp];
 	 zk = 0.0;
-	 if(ABS(box[k].nfit) > 0) zk = box[k].zero[ABS(box[k].nfit)/2];
+	 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(ABS(box[j].nfit) > 0) zj = box[j].zero[ABS(box[j].nfit)/2];
+	    if(box[j].nfit > 0) zj = box[j].zero[box[j].nfit/2];
 	    if(ABS(yctr[jp]-yctr[kp]) > DIFFERENT_STREAK) {
 /* Trim back the feebler streak */
@@ -266,5 +268,5 @@
 	    if(cell[j].persist[k].nfit <= 0) continue;
 	 }
-	 fprintf(fp, "%3d %7d  %3d %3d %5d %3d  %3d %3d  %3d %3d  %3d %3d %3d %3d %3d %3d %3d %3d  %1d %1d %9.6f %3d %3d %3d\n",
+	 fprintf(fp, "%3d %7d  %3d %3d %5d %3d  %3d %3d  %3d %3d  %3d %3d %3d %3d %3d %3d %3d %3d  %1d %1d %9.6f %3d %3d %3d %d\n",
 		 j, cell[j].persist[k].time, 
 		 cell[j].persist[k].cx, cell[j].persist[k].cy, 
@@ -278,6 +280,7 @@
 		 cell[j].persist[k].func, cell[j].persist[k].up, 
 		 cell[j].persist[k].slope, cell[j].persist[k].nfit,
-		 cell[j].persist[k].sxfit, cell[j].persist[k].exfit);
-	 for(i=0; i<ABS(cell[j].persist[k].nfit); i++) {
+		 cell[j].persist[k].sxfit, cell[j].persist[k].exfit, 
+		 cell[j].persist[k].fiterr);
+	 for(i=0; i<cell[j].persist[k].nfit; i++) {
 	    fprintf(fp, "%3d %3d %8.4f\n", cell[j].persist[k].xfit[i], 
 		    cell[j].persist[k].yfit[i], cell[j].persist[k].zero[i]);
@@ -302,5 +305,5 @@
 
 	 i = (cell[j].burn[k].ex - cell[j].burn[k].sx + 1) / 2;
-	 fprintf(fp, "%3d %7d  %3d %3d %5d %3d  %3d %3d  %3d %3d  %3d %3d %3d %3d %3d %3d %3d %3d  %1d %1d %9.6f %3d %3d %3d\n", 
+	 fprintf(fp, "%3d %7d  %3d %3d %5d %3d  %3d %3d  %3d %3d  %3d %3d %3d %3d %3d %3d %3d %3d  %1d %1d %9.6f %3d %3d %3d %d\n", 
 		 j, cell[j].burn[k].time, 
 		 cell[j].burn[k].cx, cell[j].burn[k].cy,
@@ -314,6 +317,7 @@
 		 cell[j].burn[k].func, cell[j].burn[k].up, 
 		 cell[j].burn[k].slope, cell[j].burn[k].nfit,
-		 cell[j].burn[k].sxfit, cell[j].burn[k].exfit);
-	 for(i=0; i<ABS(cell[j].burn[k].nfit); i++) {
+		 cell[j].burn[k].sxfit, cell[j].burn[k].exfit, 
+		 cell[j].burn[k].fiterr);
+	 for(i=0; i<cell[j].burn[k].nfit; i++) {
 	    fprintf(fp, "%3d %3d %8.4f\n", cell[j].burn[k].xfit[i], 
 		    cell[j].burn[k].yfit[i], cell[j].burn[k].zero[i]);
Index: /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/trailfit.c
===================================================================
--- /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/trailfit.c	(revision 26766)
+++ /branches/eam_branches/20091201/extsrc/gpcsw/gpcsrc/fits/burntool/trailfit.c	(revision 26767)
@@ -30,5 +30,6 @@
 
    if(box->func != BURN_PWR && box->func != BURN_EXP) {
-      fprintf(stderr, "error: unimplemented fit function %d\n", box->func);
+      fprintf(stderr, "error: unimplemented fit function %d (err %d)\n", 
+	      box->func, box->fiterr);
       return(-1);
    }
@@ -230,12 +231,12 @@
 /* FIXME: sanity check fits */
    if(slope >= FIT_MAX_SLOPE || slope < FIT_MIN_SLOPE) {
-/* Check whether it's a significant trail (but with wrong slope) or just 
- * noise.  In the former case negate box->nfit, 
- * in the latter case set it to zero. */
+      box->slope = slope;
+      box->nfit = 0;
+      box->fiterr = FIT_SLOPE_ERROR;
+/* Check whether it's a significant trail (but with pos slope) or just noise */
       linearrms(nfit, ybuf+y1, zbuf+y1, slope, zero, &trial);
-      box->slope = slope;
-      if(trial > 2*rms) box->nfit = -box->nfit;
-      else box->nfit = 0;
-      box->fiterr = FIT_SLOPE_ERROR;
+/* 100203 JT: bad idea: appears to be a bug in read/writing ABS(nfit) */
+//      if(trial > 2*rms) box->nfit = -box->nfit;
+      if(trial > 2*rms) box->func = BURN_POSSLOPE;
       return(-1);
    }
