Index: /trunk/magic/remove/src/Makefile.simple
===================================================================
--- /trunk/magic/remove/src/Makefile.simple	(revision 20495)
+++ /trunk/magic/remove/src/Makefile.simple	(revision 20496)
@@ -11,4 +11,8 @@
 
 streaksremove:  ${OBJECTS}
+
+install:
+	cp streaksremove $(PSCONFDIR)/$(PSCONFIG)/bin
+	chmod 755  $(PSCONFDIR)/$(PSCONFIG)/bin/streaksremove
 
 clean:
Index: /trunk/magic/remove/src/streaksremove.c
===================================================================
--- /trunk/magic/remove/src/streaksremove.c	(revision 20495)
+++ /trunk/magic/remove/src/streaksremove.c	(revision 20496)
@@ -2,4 +2,5 @@
 #include "streaksextern.h"
 #include "libgen.h"
+#include "unistd.h"
 
 extern bool  sFileLock(sFile * sfile);
@@ -90,5 +91,5 @@
 
 sFile *sFileOpen(pmConfig *config, ippStage stage, psString fileSelect,
-    psString outputExt, bool required)
+    psString outputFilename, bool required)
 {
     bool status;
@@ -96,11 +97,13 @@
     memset(sfile, 0, sizeof(sFile));
 
-    // We use psFits directly to read the image file unless the stage is warp
+    // We use psFits directly to read the image unless the stage is warp
     // or diff. In those cases we use the pmFPAfile functions to read the image file
-    // to make managing the astrometry easy
+    // to make managing the astrometry easy.
+    // The reason we don't use pmFPAfile in all cases is that I was having trouble getting
+    // all of the keywords in the raw image files written to the output destreaked files
 
     if (!CHIP_LEVEL_INPUT(stage) && !strcmp(fileSelect, "INPUT")) {
         // stage is warp or diff AND fileSelect eq "INPUT"
-        // get data from pmFPAfile. We read the mask and weight files using psFits
+        // get data from pmFPAfile.
 
         // we need to know what the nebulous and real filenames are so we steal
@@ -112,13 +115,16 @@
             streaksremoveExit("", PS_EXIT_PROG_ERROR);
         }
-        if (infiles->n < 1) {
-            psError(PS_ERR_IO, false, "Found n == %ld files in %s in arguments\n", infiles->n, "INPUT");
+        if (infiles->n != 1) {
+            psError(PS_ERR_IO, false, "Found n == %ld files in %s in arguments expencted 1\n",
+                infiles->n, "INPUT");
             streaksremoveExit("", PS_EXIT_DATA_ERROR);
         }
+        // end of file name lookup code adapted from pmFPAfileDefineFromArgs
+        //
         sfile->name = psStringCopy(infiles->data[0]);
-        // end of file name lookup code adapted from pmFPAfileDefineFromArgs
-
         sfile->inNebulous = IN_NEBULOUS(sfile->name);
 
+        // XXX: I should probably be using a different file rule for the diff, but I don't
+        // have an input rule that takes a diff image. This should be compatible
         sfile->pmfile = pmFPAfileDefineFromArgs(&status, config, "PPSUB.INPUT", "INPUT");
         if (!sfile->pmfile) {
@@ -141,8 +147,8 @@
     }
 
-    // using psFits for i/o
-
-    sfile->name = psMetadataLookupStr(&status, config->arguments, fileSelect);
-    if (!status || !sfile->name) {
+    // For all other files we use using psFits for i/o
+
+    psString name = psMetadataLookupStr(&status, config->arguments, fileSelect);
+    if (!status || !name) {
         if (required) {
             psError(PS_ERR_IO, false, "Failed to lookup name for %s", fileSelect);
@@ -153,8 +159,9 @@
     }
 
-    // if outputExt is not null it contains the extension to append to name
+    // if outputFilename is not null name it contains the "directory" 
+    // and outputFilename is the basename name of the file (or nebulous key)
     // and the file is to be opened for writing
-    if (outputExt) {
-        psStringAppend(&sfile->name, outputExt);
+    if (outputFilename) {
+        psStringAppend(&sfile->name, "%s%s", name, outputFilename);
         sfile->resolved_name = resolveFilename(config, sfile, true);
         if (!sfile->resolved_name) {
@@ -166,4 +173,5 @@
         sfile->fits->options = psFitsOptionsAlloc();
     } else {
+        sfile->name = psStringCopy(name);
         sfile->resolved_name = resolveFilename(config, sfile, false);
         if (!sfile->resolved_name) {
@@ -180,5 +188,5 @@
     if (!sfile->fits) {
         psError(PS_ERR_IO, false, "failed to open fits file %s for %s",
-                    sfile->resolved_name, outputExt ? "writing" : "reading");
+                    sfile->resolved_name, outputFilename ? "writing" : "reading");
         sFileFree(sfile);
         streaksremoveExit("", 1);
@@ -274,5 +282,6 @@
     sf->nHDU = sf->inImage->nHDU;
 
-    // don't need to free this see basename(3)
+    // don't need to free inputBasename see basename(3)
+    // The names of the temporary and recovery files are taken from the input
     char *inputBasename = basename(sf->inImage->name);
     sf->outImage = sFileOpen(config, stage, "OUTPUT", inputBasename, true);
@@ -362,4 +371,10 @@
     // unless stage is raw or chip when we get here we're done
     if (!CHIP_LEVEL_INPUT(sf->stage)) {
+        sf->view->readout = -1;
+        pmFPAfileIOChecks(sf->config, sf->view, PM_FPA_AFTER);
+        sf->view->cell = -1;
+        pmFPAfileIOChecks(sf->config, sf->view, PM_FPA_AFTER);
+        sf->view->chip = -1;
+        pmFPAfileIOChecks(sf->config, sf->view, PM_FPA_AFTER);
         return false;
     }
@@ -673,5 +688,5 @@
 readImageFrom_pmFile(streakFiles *sf)
 {
-    // XXX: Currently this function assumes that it is only used for a single
+    // XXX: This function assumes that it is only used for a single
     // chip single cell FPA (i.e. a skycell)
     pmFPAview *view = sf->view;
@@ -754,5 +769,4 @@
     }
     sfile->fits->options = psFitsOptionsAlloc();
-    sfile->fits->options->extword = psStringCopy(extname);
     sfile->fits->options->scaling = PS_FITS_SCALE_MANUAL;
     sfile->fits->options->bitpix = bitpix;
@@ -978,4 +992,10 @@
         streaksremoveExit("", PS_EXIT_DATA_ERROR);
     }
+    psFree(sfile->header);
+    psFree(sfile->image);
+    psFree(sfile->imagecube);
+    psFree(sfile->name);
+    psFree(sfile->resolved_name);
+    psFree(sfile);
 }
 
@@ -1119,25 +1139,45 @@
 
 static bool
+deleteFile(sFile *sfile)
+{
+
+    if (sfile->inNebulous) {
+        nebServer *server = getNebServer(NULL);
+        if (!nebDelete(server, sfile->name)) {
+            psError(PM_ERR_SYS, false, "failed to delete %s\n%s.", sfile->name,
+                nebErr(server));
+            return false;
+        }
+    } else {
+        if (unlink(sfile->resolved_name)) {
+            psError(PM_ERR_SYS, false, "failed to delete %s\n%s.", sfile->resolved_name,
+                strerror(errno));
+            return false;
+        }
+    }
+    return true;
+}
+
+static bool
 deleteTemps(streakFiles *sfiles)
 {
-    bool status = false;
-
-    nebServer *server = getNebServer(NULL);
-
-    if (!server) {
-        // TODO: handle this
-        //
-        // no files are in nebulous
-        return true;
-    }
-    if (sfiles->outImage->inNebulous) {
-        if (!nebDelete(server, sfiles->outImage->name)) {
-            psError(PM_ERR_SYS, false, "failed to delete %s\n%s.", sfiles->outImage->name,
-                nebErr(getNebServer(NULL)));
+    if (sfiles->outMask) {
+        if (!deleteFile(sfiles->outMask)) {
+            psError(PM_ERR_SYS, false, "failed to delete Mask.");
             return false;
         }
     }
 
-    // TODO: weight and mask images
+    if (sfiles->outWeight) {
+        if (!deleteFile(sfiles->outWeight)) {
+            psError(PM_ERR_SYS, false, "failed to delete Weight.");
+            return false;
+        }
+    }
+
+    if (!deleteFile(sfiles->outImage)) {
+        psError(PM_ERR_SYS, false, "failed to delete Image.");
+        return false;
+    }
 
     return true;
@@ -1151,4 +1191,7 @@
     StreakPixels *pixels;
     PixelPos *pixelPos;
+
+    psLibInit(NULL);
+
     pmConfig *config = parseArguments(argc, argv);
     if (!config) {
@@ -1286,6 +1329,14 @@
         }
     }
+    psFree(ourNebServer);
+    psFree(config);
+    pmConceptsDone();
+    pmConfigDone();
+    psLibFinalize();
+#ifdef notyet
+#endif
 
     //  PAU
+    fprintf(stderr, "Found %d leaks at %s\n", psMemCheckLeaks (0, NULL, NULL, false), "streaksremove");
 
     return 0;
