Index: branches/eam_branches/ipp-20110710/magic/remove/src/streaksio.c
===================================================================
--- branches/eam_branches/ipp-20110710/magic/remove/src/streaksio.c	(revision 31852)
+++ branches/eam_branches/ipp-20110710/magic/remove/src/streaksio.c	(revision 32337)
@@ -752,5 +752,5 @@
         return;
     }
-    psArray *table = psFitsReadTable(in->fits);
+    psFitsTable *table = psFitsReadTableNew(in->fits);
     if (!table) {
         psError(PS_ERR_UNKNOWN, false, "failed to read table in extension %d from in->resolved name", extnum);
@@ -758,8 +758,9 @@
     }
 
-    if (!psFitsWriteTable(out->fits, out->header, table, extname)) {
+    if (!psFitsWriteTableNew(out->fits, out->header, table, extname)) {
         psError(PS_ERR_UNKNOWN, false, "failed to copy table in extension %d", extnum);
         streaksExit("", PS_EXIT_DATA_ERROR);
     }
+    psFree(table);
 }
 
@@ -839,4 +840,5 @@
     psFree(sfile->name);
     psFree(sfile->resolved_name);
+    psFree(sfile->fits);
     psFree(sfile);
 }
@@ -915,4 +917,8 @@
         sFileFree(sf->outWeight);
         sFileFree(sf->recWeight);
+    }
+    if (sf->inSources) {
+        sFileFree(sf->inSources);
+        sFileFree(sf->outSources);
     }
 }
@@ -1301,4 +1307,5 @@
                 streaksExit("", PS_EXIT_UNKNOWN_ERROR);
             }
+            psFree(compress);
         } else {
             copyTable(out, in, extnum);
Index: branches/eam_branches/ipp-20110710/magic/remove/src/streaksremove.c
===================================================================
--- branches/eam_branches/ipp-20110710/magic/remove/src/streaksremove.c	(revision 31852)
+++ branches/eam_branches/ipp-20110710/magic/remove/src/streaksremove.c	(revision 32337)
@@ -278,5 +278,4 @@
 
     if (statsFileName) {
-    // Write out
         psString resolved = pmConfigConvertFilename(statsFileName, config, true, true); // Resolved filename
         if (!resolved) {
@@ -292,5 +291,5 @@
     }
 
-    // all done. Clean up to look for memory leaks.
+    // all done. Clean up and look for memory leaks.
 
     psFree(sfiles);
@@ -302,5 +301,7 @@
     psLibFinalize();
 
-    fprintf(stderr, "Found %d leaks at %s\n", psMemCheckLeaks (0, NULL, NULL, false), "streaksremove");
+    int leaks = psMemCheckLeaks (0, NULL, stdout, false);
+
+    fprintf(stderr, "Found %d leaks at %s\n", leaks, "streaksremove");
 
     return 0;
@@ -917,5 +918,4 @@
     sFile *out = sfiles->outSources;
 
-
     // Primary header, should be "something.hdr"
     {
@@ -960,19 +960,54 @@
         }
 
-        psArray *inTable = psFitsReadTable(in->fits);
-        if (!inTable) {
+#ifdef CHECK_MEM_USE
+        bool printStats = false;
+        size_t allocated = 0;
+        size_t persistent = 0;
+        size_t allocatedBefore = psMemStats(printStats, &allocated, &persistent);
+        printf("Before: %12lld %12lld %12lld\n", (long long) allocated, (long long) persistent, (long long) allocatedBefore);
+#endif
+
+        psFitsTable *table = psFitsReadTableNew(in->fits);
+
+#ifdef CHECK_MEM_USE
+        size_t allocatedAfter = psMemStats(printStats, &allocated, &persistent);
+        printf("After:  %12lld %12lld %12lld\n", (long long) allocated, (long long) persistent, (long long) allocatedAfter);
+        printf("Change: %12lld\n", (long long) allocatedAfter - allocatedBefore);
+#endif
+
+        if (!table) {
             psErrorStackPrint(stderr, "failed to read table in %s", in->resolved_name);
             streaksExit("", PS_EXIT_DATA_ERROR);
         }
 
-        psArray *outTable = psArrayAllocEmpty(inTable->n);
-        int j = 0;
+        // XXX: Handle case where numCols == 0 Can that happen?
+        // Previously we were writing a blank extension
+
+        // For speed we get intimate with the table
+        // avoid 2 * numRows lookups of the column number by looking
+        // it up ahead of time and verifying that the type is what we
+        // expect.
+        int xCol = psFitsTableGetColumnNumber(table, "X_PSF");
+        assert(xCol >= 0 && xCol < table->numCols);
+        assert(table->columns[xCol].type == PS_DATA_F32);
+
+        int yCol = psFitsTableGetColumnNumber(table, "Y_PSF");
+        assert(yCol >= 0 && yCol < table->numCols);
+        assert(table->columns[yCol].type == PS_DATA_F32);
+
+        // We mark censored rows in this array.
+        bool *rowCensored = psAlloc(table->numRows * sizeof(bool));
+        memset(rowCensored, 0, table->numRows * sizeof(bool));
         int numCensored = 0;
-        for (int i = 0 ; i < inTable->n; i++) {
-            psMetadata *row = inTable->data[i];
-
-            psF32 x = psMetadataLookupF32(NULL, row, "X_PSF");
-            psF32 y = psMetadataLookupF32(NULL, row, "Y_PSF");
-
+
+        for (int i = 0 ; i < table->numRows; i++) {
+
+#ifndef GO_SLOWER
+            psF32 x = table->columns[xCol].data.F32[i];
+            psF32 y = table->columns[yCol].data.F32[i];
+#else
+            psF32 x = psFitsTableGetF32(NULL, table, i, "X_PSF");
+            psF32 y = psFitsTableGetF32(NULL, table, i, "Y_PSF");
+#endif
             psImageMaskType mask;
             if ((x >= maskImage->numCols) || (y >= maskImage->numRows) || (x <  0) || (y < 0) || isnan(x) || isnan(y)) {
@@ -983,22 +1018,24 @@
 
             // Key the source if the center pixel is not masked with maskStreak
-            if (!(mask & maskStreak) ) {
-                psArraySet(outTable, j++, row);
-            } else {
+            if (mask & maskStreak) {
+                rowCensored[i] = true;
                 numCensored++;
             }
         }
 
-        // get rid of unused elements (don't know if this is necessary)
-        psArrayRealloc(outTable, j);
+        // remove the censored rows from the table
+        if (!psFitsTableCensor(table, rowCensored)) {
+            streaksExit("", PS_EXIT_PROG_ERROR);
+        }
 
         addDestreakKeyword(header, true);
-        if (psArrayLength(outTable) > 0) {
+
+        if (table->numCols > 0) {
             printf("Censored %d sources\n", numCensored);
-            if (! psFitsWriteTable(out->fits, header, outTable, extname)) {
+            if (! psFitsWriteTableNew(out->fits, header, table, extname)) {
                 psErrorStackPrint(stderr, "failed to write table to %s", out->resolved_name);
                 streaksExit("", PS_EXIT_DATA_ERROR);
             }
-        } else if (psArrayLength(inTable) == 0) {
+        } else {
             printf("No input sources\n");
             // We'd like to write a blank table, but this is as good as it gets without more work to parse the
@@ -1008,14 +1045,8 @@
                 streaksExit("", PS_EXIT_DATA_ERROR);
             }
-        } else {
-            printf("Censored ALL %d sources\n", numCensored);
-            if (! psFitsWriteTableEmpty(out->fits, header, inTable->data[0], extname)) {
-                psErrorStackPrint(stderr, "failed to write empty table to %s", out->resolved_name);
-                streaksExit("", PS_EXIT_DATA_ERROR);
-            }
         }
         psFree(header);
-        psFree(outTable);
-        psFree(inTable);
+        psFree(table);
+        psFree(rowCensored);
     }
 
@@ -1038,15 +1069,16 @@
         }
 
-        psArray *inTable = psFitsReadTable(in->fits);
-        if (!inTable) {
+        psFitsTable *table = psFitsReadTableNew(in->fits);
+        if (!table) {
             psErrorStackPrint(stderr, "failed to read table in %s", in->resolved_name);
             streaksExit("", PS_EXIT_DATA_ERROR);
         }
-        if (! psFitsWriteTable(out->fits, header, inTable, extname)) {
+        if (! psFitsWriteTableNew(out->fits, header, table, extname)) {
             psErrorStackPrint(stderr, "failed to write table to %s", out->resolved_name);
             streaksExit("", PS_EXIT_DATA_ERROR);
         }
-    }
-
+        psFree(header);
+        psFree(table);
+    }
 
     if (!psFitsClose(out->fits)) {
@@ -1054,3 +1086,4 @@
         streaksExit("", PS_EXIT_DATA_ERROR);
     }
-}
+    out->fits = NULL;   // freed by psFitsclose()
+}
