Index: /trunk/psModules/src/camera/pmFPAConstruct.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAConstruct.c	(revision 7617)
+++ /trunk/psModules/src/camera/pmFPAConstruct.c	(revision 7618)
@@ -739,8 +739,9 @@
     PS_ASSERT_PTR_NON_NULL(format, false);
 
-    return addSource(fpa, phuView, NULL, format) ? true : false;
-}
-
-
+    pmFPAview *view = addSource(fpa, phuView, NULL, format);
+    bool status = (view == NULL);
+    psFree (view);
+    return status;
+}
 
 // Add an input file to the FPA
Index: /trunk/psModules/src/camera/pmFPACopy.c
===================================================================
--- /trunk/psModules/src/camera/pmFPACopy.c	(revision 7617)
+++ /trunk/psModules/src/camera/pmFPACopy.c	(revision 7618)
@@ -181,4 +181,5 @@
         psFree(biasIter);
 
+        targetReadout->data_exists = true;
         psFree(targetReadout);          // Drop reference
     }
@@ -251,5 +252,4 @@
     binItem->data.S32 *= yBin;
 
-
     // Copy any headers
     pmHDU *targetHDU = pmHDUFromCell(target); // The target HDU
@@ -265,4 +265,5 @@
     }
 
+    target->data_exists = true;
     return true;
 }
@@ -301,4 +302,5 @@
     psMetadataCopy(target->concepts, source->concepts);
 
+    target->data_exists = true;
     return status;
 }
Index: /trunk/psModules/src/camera/pmFPAMosaic.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAMosaic.c	(revision 7617)
+++ /trunk/psModules/src/camera/pmFPAMosaic.c	(revision 7618)
@@ -458,5 +458,5 @@
     psTrace(__func__, 3, "Spliced image will be %dx%d\n", (int)xSize, (int)ySize);
     psImage *mosaic = psImageAlloc((int)xSize, (int)ySize, type); // The mosaic image
-    psImageInit(mosaic, 0.0);
+    psImageInit(mosaic, 0);
 
     // Next pass through the images to do the mosaicking
Index: /trunk/psModules/src/camera/pmFPAWrite.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAWrite.c	(revision 7617)
+++ /trunk/psModules/src/camera/pmFPAWrite.c	(revision 7618)
@@ -101,5 +101,4 @@
     if ((!pixels && hdu->phu && !hdu->images) || // Data-less PHU
             (pixels && (hdu->images || (!hdu->images && pmHDUGenerateForCell(cell) && hdu->images)))) { // Data
-        psMetadataPrint (stdout, cell->concepts, 0);
         success &= pmConceptsWriteCell(cell, PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CAMERA |
                                        PM_CONCEPT_SOURCE_DEFAULTS, false, NULL);
@@ -152,8 +151,4 @@
         if (blankSegment || imageSegment) {
             pmConceptSource source = PM_CONCEPT_SOURCE_HEADER | PM_CONCEPT_SOURCE_CAMERA | PM_CONCEPT_SOURCE_DEFAULTS;
-            for (int i = 0; i < chip->cells->n; i++) {
-                pmCell *cell = chip->cells->data[i];
-                psMetadataPrint (stdout, cell->concepts, 0);
-            }
             if (!pmConceptsWriteChip(chip, source, false, true, NULL)) {
                 psError(PS_ERR_IO, false, "Unable to write Concepts for Chip.\n");
Index: /trunk/psModules/src/camera/pmFPAfile.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAfile.c	(revision 7617)
+++ /trunk/psModules/src/camera/pmFPAfile.c	(revision 7618)
@@ -81,4 +81,10 @@
     file->state = PM_FPA_STATE_CLOSED;
 
+    file->xBin = 1;
+    file->yBin = 1;
+    file->src = NULL;
+
+    file->save = false;
+
     return file;
 }
@@ -109,4 +115,5 @@
 
 // XXX reconsider this function name / concept
+# if 0
 bool pmFPAfileAddFileNames (psMetadata *files, char *name, char *value, int mode)
 {
@@ -130,4 +137,5 @@
     return true;
 }
+# endif
 
 // select the rule from the camera configuration, perform substitutions as needed
@@ -241,5 +249,7 @@
     if (view->cell == -1) {
         pmFPAAddSourceFromView (out, view, format);
-        return pmChipCopyStructure (outChip, inChip, xBin, yBin);
+        // XXX this is a hack: can i defer this to ppImageRebinChip?
+        // bool status = pmChipCopyStructure (outChip, inChip, xBin, yBin);
+        return true;
     }
     if (view->cell >= inChip->cells->n) {
Index: /trunk/psModules/src/camera/pmFPAfile.h
===================================================================
--- /trunk/psModules/src/camera/pmFPAfile.h	(revision 7617)
+++ /trunk/psModules/src/camera/pmFPAfile.h	(revision 7618)
@@ -7,6 +7,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-06-17 01:50:43 $
+*  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-06-21 22:44:16 $
 *
 *  Copyright 2004-2005 Institute for Astronomy, University of Hawaii
@@ -60,4 +60,5 @@
     pmFPALevel fileLevel;  // what level in the FPA hierarchy represents a unique file?
     pmFPALevel dataLevel;  // at what level do we read/write the data segment?
+    pmFPALevel freeLevel;  // at what level do we read/write the data segment?
 
     pmFPA *fpa;    // for I/O files, we carry a pointer to the complete fpa
Index: /trunk/psModules/src/camera/pmFPAfileDefine.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 7617)
+++ /trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 7618)
@@ -109,4 +109,7 @@
         return NULL;
     }
+    // default is to free the data after use (after written out)
+    // this can be overridden for pmFPAfiles used as carriers as well
+    file->freeLevel = file->dataLevel;
 
     if (fpa != NULL) {
@@ -199,8 +202,8 @@
     char *save = psMetadataLookupStr (&status, data, "FILE.SAVE");
     if (save != NULL) {
-        if (!strcasecmp (type, "TRUE"))     {
+        if (!strcasecmp (save, "TRUE"))     {
             file->save = true;
         }
-        if (!strcasecmp (type, "FALSE"))     {
+        if (!strcasecmp (save, "FALSE"))     {
             file->save = false;
         }
@@ -218,4 +221,7 @@
         return NULL;
     }
+    // default is to free the data after use (after written out)
+    // this can be overridden for pmFPAfiles used as carriers as well
+    file->freeLevel = file->dataLevel;
 
     if (fpa != NULL) {
@@ -266,4 +272,9 @@
     }
 
+    // add argument-supplied OUTPUT name to this file
+    char *outname = psMetadataLookupStr(&status, config->arguments, "OUTPUT");
+    psMetadataAddStr (file->names, PS_LIST_TAIL, "OUTPUT", PS_META_NO_REPLACE, "", outname);
+
+    // place the resulting file in the config system
     psMetadataAddPtr (config->files, PS_LIST_TAIL, name, PS_DATA_UNKNOWN, "", file);
     psFree (file); // we free this copy of file, but 'files' still has a copy
@@ -616,5 +627,5 @@
     PS_ASSERT_INT_POSITIVE(strlen(filename), false);
 
-    pmFPA *fpa = pmFPAConstruct (config->camera);
+    pmFPA *fpa = pmFPAConstruct (src->camera);
     pmFPAfile *file = pmFPAfileDefineOutput (config, fpa, filename);
     if (!file) {
@@ -648,4 +659,14 @@
     }
     file->fpa = pmFPAConstruct(file->camera);
+
+    for (int i = 0; i < file->fpa->chips->n; i++) {
+        pmChip *chip = file->fpa->chips->data[i];
+        for (int j = 0; j < chip->cells->n; j++) {
+            pmCell *cell = chip->cells->data[j];
+            char *name = psMetadataLookupStr (NULL, cell->concepts, "CELL.NAME");
+            fprintf (stderr, "cell %d,%d : %s\n", i, j, name);
+        }
+    }
+
     return file;
 }
Index: /trunk/psModules/src/camera/pmFPAfileFitsIO.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAfileFitsIO.c	(revision 7617)
+++ /trunk/psModules/src/camera/pmFPAfileFitsIO.c	(revision 7618)
@@ -101,5 +101,11 @@
         if (hdu != phu) {
             // we assume that the PHU is just a header
-            psMetadata *outhead = psMetadataCopy (NULL, phu->header);
+            // header may not be defined for constructed images; make a dummy one
+            psMetadata *outhead = NULL;
+            if (phu->header) {
+                outhead = psMetadataCopy (NULL, phu->header);
+            } else {
+                outhead = psMetadataAlloc ();
+            }
             psMetadataAdd (outhead, PS_LIST_TAIL, "EXTEND", PS_DATA_BOOL | PS_META_REPLACE, "this file has extensions", true);
             psFitsWriteBlank (file->fits, outhead);
Index: /trunk/psModules/src/camera/pmFPAfileIO.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAfileIO.c	(revision 7617)
+++ /trunk/psModules/src/camera/pmFPAfileIO.c	(revision 7618)
@@ -261,9 +261,24 @@
 
     // do we need to read this file?
-    if (level != file->dataLevel) {
-        psTrace("pmFPAfile", 6, "skip free of %s at this level %s: dataLevel is %s",
+    if (level != file->freeLevel) {
+        psTrace("pmFPAfile", 6, "skip free of %s at this level %s: freeLevel is %s",
+                file->name, pmFPALevelToName(level), pmFPALevelToName(file->freeLevel));
+        return true;
+    }
+
+    // XXX totally lame hack: how do I handle this?
+    # if 0
+    if (!strcasecmp(file->name, "PPIMAGE.BIN1") && (level != PM_FPA_LEVEL_FPA))
+        {
+            psTrace("pmFPAfile", 5, "skip free of %s at this level %s: dataLevel is %s",
+                    file->name, pmFPALevelToName(level), pmFPALevelToName(file->dataLevel));
+            return true;
+        }
+    if (!strcasecmp(file->name, "PPIMAGE.BIN2") && (level != PM_FPA_LEVEL_FPA)) {
+        psTrace("pmFPAfile", 5, "skip free of %s at this level %s: dataLevel is %s",
                 file->name, pmFPALevelToName(level), pmFPALevelToName(file->dataLevel));
         return true;
     }
+    # endif
 
     switch (file->type) {
@@ -271,4 +286,7 @@
         if (pmFPAviewFreeFitsImage (view, file)) {
             psTrace ("pmFPAfile", 5, "freed %s (type: %d)\n", file->filename, file->type);
+            if (file->filename == NULL) {
+                psTrace ("pmFPAfile", 5, "filename is not defined for %s\n", file->name);
+            }
         } else {
             psError(PS_ERR_UNKNOWN, false, "skipping %s (type: %d)\n", file->filename, file->type);
@@ -310,4 +328,9 @@
     if (file->mode != PM_FPA_MODE_WRITE) {
         psTrace("pmFPAfile", 6, "skip write for %s, mode is not WRITE", file->name);
+        return true;
+    }
+
+    if (!file->save) {
+        psTrace("pmFPAfile", 6, "skip write for %s, save is FALSE", file->name);
         return true;
     }
@@ -393,7 +416,7 @@
 
     // do we need to write this file?
-    if (level != file->dataLevel) {
-        psTrace("pmFPAfile", 6, "skip creation of %s at this level %s: dataLevel is %s",
-                file->name, pmFPALevelToName(level), pmFPALevelToName(file->dataLevel));
+    if (level != file->fileLevel) {
+        psTrace("pmFPAfile", 6, "skip creation of %s at this level %s: fileLevel is %s",
+                file->name, pmFPALevelToName(level), pmFPALevelToName(file->fileLevel));
         return true;
     }
@@ -404,5 +427,5 @@
         /* create a PHU for thie file, if it does not exist */
         pmFPAfileCopyStructureView (file->fpa, file->src, file->format, file->xBin, file->yBin, view);
-        psTrace ("pmFPAfile", 5, "created fpa data elements for %s (fpa: %p)\n", file->name, file->fpa);
+        // psTrace ("pmFPAfile", 5, "created fpa data elements for %s (fpa: %p)\n", file->name, file->fpa);
         break;
 
