Index: /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.c
===================================================================
--- /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.c	(revision 6725)
+++ /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.c	(revision 6726)
@@ -1,4 +1,5 @@
 #include <stdio.h>
 #include "pslib.h"
+#include "psAdditionals.h"
 #include "pmHDU.h"
 #include "pmFPA.h"
@@ -19,5 +20,7 @@
     psFree (file->names);
 
-    psFitsClose (file->fits);
+    if (file->fits != NULL) {
+        psFitsClose (file->fits);
+    }
 
     psFree (file->filerule);
@@ -26,7 +29,8 @@
     psFree (file->extxtra);
 
-    // these are just views:
-    // psFree (file->filename);
-    // psFree (file->extname);
+    psFree (file->filename);
+    psFree (file->extname);
+
+    // these are just views ??
     // psFree (file->phu);
     // psFree (file->header);
@@ -63,5 +67,4 @@
 }
 
-// XXX is a file allowed to be both READ and WRITE?
 pmFPAfile *pmFPAfileDefine (psMetadata *files, psMetadata *format, pmFPA *fpa, char *name)
 {
@@ -195,6 +198,6 @@
 }
 
-// open file (if not already opened XXX : better tracking of open state?
-bool pmFPAfileOpen (pmFPAfile *file, pmFPAview *view)
+// open file (if not already opened)
+bool pmFPAfileOpen (pmFPAfile *file, const pmFPAview *view)
 {
 
@@ -220,5 +223,4 @@
 
     // determine the file name
-    // XXX file->filename needs to be a copy, and freed in places like this...
     file->filename = pmFPAfileNameFromRule (file->filerule, file, view);
     if (file->filename == NULL)
@@ -227,14 +229,21 @@
     // indirect filenames
     if (!strcasecmp (file->filename, "@FPAIO")) {
+        psFree (file->filename);
         extra = pmFPAfileNameFromRule (file->filextra, file, view);
         file->filename = psMetadataLookupStr (&status, file->names, extra);
+        psFree (extra);
         if (file->filename == NULL)
             return false;
+        // psMetadataLookupStr just returns a view, file->filename must be protected
+        psMemIncrRefCounter (file->filename);
     }
     if (!strcasecmp (file->filename, "@DETDB")) {
+        psFree (file->filename);
         extra = pmFPAfileNameFromRule (file->filextra, file, view);
         // file->filename = pmDetrendSelect (extra);
+        psFree (extra);
         if (file->filename == NULL)
             return false;
+        psMemIncrRefCounter (file->filename);
     }
 
@@ -263,5 +272,5 @@
 }
 
-bool pmFPAfileRead (pmFPAfile *file, pmFPAview *view)
+bool pmFPAfileRead (pmFPAfile *file, const pmFPAview *view)
 {
     if (file->mode != PM_FPA_MODE_READ)
@@ -305,5 +314,5 @@
 }
 
-bool pmFPAfileWrite (pmFPAfile *file, pmFPAview *view)
+bool pmFPAfileWrite (pmFPAfile *file, const pmFPAview *view)
 {
     if (file->mode != PM_FPA_MODE_WRITE)
@@ -324,7 +333,6 @@
     switch (file->type) {
     case PM_FPA_FILE_IMAGE:
-        psLogMsg (__func__, 3, "write image disabled\n");
-        // XXX disabled for now
-        // XXX pmFPAviewWriteFitsImage (view, file->fits);
+        pmFPAviewWriteFitsImage (view, file);
+        psTrace ("pmFPAfile", 5, "wrote image %s (fpa: %p)\n", file->filename, file->fpa);
         break;
 
@@ -335,5 +343,5 @@
     case PM_FPA_FILE_CMF:
         pmFPAviewWriteObjects (view, file);
-        psTrace ("pmFPAfile", 5, "wrote %s (type: %d)\n", file->filename, file->type);
+        psTrace ("pmFPAfile", 5, "wrote object %s (fpa: %p)\n", file->filename, file->fpa);
         break;
 
@@ -345,5 +353,5 @@
 }
 
-bool pmFPAfileClose (pmFPAfile *file, pmFPAview *view)
+bool pmFPAfileClose (pmFPAfile *file, const pmFPAview *view)
 {
     if (file->state == PM_FPA_STATE_CLOSED) {
@@ -385,5 +393,5 @@
 
 // attempt open, read, write, or close pmFPAfiles in files
-bool pmFPAfileIOChecks (psMetadata *files, pmFPAview *view, pmFPAfilePlace place)
+bool pmFPAfileIOChecks (psMetadata *files, const pmFPAview *view, pmFPAfilePlace place)
 {
     // recipe override values (command-line options):
@@ -405,5 +413,5 @@
 
 // select the rule from the camera configuration, perform substitutions as needed
-char *pmFPAfileNameFromRule (char *rule, pmFPAfile *file, pmFPAview *view)
+char *pmFPAfileNameFromRule (char *rule, pmFPAfile *file, const pmFPAview *view)
 {
 
@@ -445,5 +453,5 @@
 }
 
-psImage *pmFPAfileReadoutImage (psMetadata *files, pmFPAview *view, char *name, int Nx, int Ny, int type)
+psImage *pmFPAfileReadoutImage (psMetadata *files, const pmFPAview *view, char *name, int Nx, int Ny, int type)
 {
     bool status;
@@ -476,5 +484,5 @@
 // given an already-opened fits file, read the components corresponding
 // to the specified view
-bool pmFPAviewReadFitsImage (pmFPAview *view, pmFPAfile *file)
+bool pmFPAviewReadFitsImage (const pmFPAview *view, pmFPAfile *file)
 {
     bool status;
@@ -506,8 +514,7 @@
         return status;
     }
+    return false;
 
     // XXX pmReadoutRead, pmReadoutReadSegement disabled for now
-    return false;
-
     # if (0)
 
@@ -526,9 +533,7 @@
 }
 
-// XXX image writes disabled for now
-# if (0)
-    // given an already-opened fits file, write the components corresponding
-    // to the specified view
-    bool pmFPAviewWriteFitsImage (pmFPAview *view, pmFPAfile *file)
+// given an already-opened fits file, write the components corresponding
+// to the specified view
+bool pmFPAviewWriteFitsImage (const pmFPAview *view, pmFPAfile *file)
 {
 
@@ -536,6 +541,7 @@
     psFits *fits = file->fits;
 
+    // pmFPAWrite takes care of all PHUs as needed
     if (view->chip == -1) {
-        pmFPAWrite (fpa, fits, NULL, NULL);
+        pmFPAWrite (fpa, fits, NULL);
         return true;
     }
@@ -546,6 +552,22 @@
     pmChip *chip = fpa->chips->data[view->chip];
 
+    // do we need to write out a PHU for this entry?
+    if (file->phu == NULL) {
+        pmHDU *hdu = pmFPAviewThisHDU (view, file->fpa);
+        pmHDU *phu = pmFPAviewThisPHU (view, file->fpa);
+        // if this hdu is the phu, the write function below will create the phu
+        if (hdu != phu) {
+            // we assume that the PHU is just a header
+            psMetadata *outhead = psMetadataCopy (NULL, phu->header);
+            psMetadataAdd (outhead, PS_LIST_TAIL, "EXTEND", PS_DATA_BOOL | PS_META_REPLACE, "this file has extensions", true);
+            psFitsWriteHeaderNotImage (file->fits, outhead);
+            file->phu = phu->header;
+            psTrace ("pmFPAfile", 5, "wrote phu %s (type: %d)\n", file->filename, file->type);
+            psFree (outhead);
+        }
+    }
+
     if (view->cell == -1) {
-        pmChipWrite (chip, fits, NULL, NULL);
+        pmChipWrite (chip, fits, NULL);
         return true;
     }
@@ -557,11 +579,15 @@
 
     if (view->readout == -1) {
-        pmCellWrite (cell, fits, NULL, NULL);
+        pmCellWrite (cell, fits, NULL);
         return true;
     }
-
-    if (view->readout >= cell->readouts->n) {
-        return false;
-    }
+    return false;
+
+    // XXX disable readout write for now
+    # if (0)
+
+        if (view->readout >= cell->readouts->n) {
+            return false;
+        }
     pmReadout *readout = cell->readouts->data[view->readout];
 
@@ -572,4 +598,4 @@
     }
     return true;
-}
-# endif
+    # endif
+}
Index: /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.h
===================================================================
--- /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.h	(revision 6725)
+++ /branches/rel10_ifa/psModules/src/astrom/pmFPAfile.h	(revision 6726)
@@ -7,6 +7,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.1.2.7 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-03-28 02:16:26 $
+*  @version $Revision: 1.1.2.8 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-03-29 20:55:42 $
 *
 *  Copyright 2004-2005 Institute for Astronomy, University of Hawaii
@@ -74,22 +74,22 @@
 pmFPAfile *pmFPAfileAlloc ();
 pmFPAfile *pmFPAfileDefine (psMetadata *files, psMetadata *camera, pmFPA *fpa, char *name);
-bool pmFPAfileOpen (pmFPAfile *file, pmFPAview *view);
-bool pmFPAfileRead (pmFPAfile *file, pmFPAview *view);
-bool pmFPAfileWrite (pmFPAfile *file, pmFPAview *view);
-bool pmFPAfileClose (pmFPAfile *file, pmFPAview *view);
+bool pmFPAfileOpen (pmFPAfile *file, const pmFPAview *view);
+bool pmFPAfileRead (pmFPAfile *file, const pmFPAview *view);
+bool pmFPAfileWrite (pmFPAfile *file, const pmFPAview *view);
+bool pmFPAfileClose (pmFPAfile *file, const pmFPAview *view);
 
-bool pmFPAfileReadChecks (psMetadata *files, pmFPAview *view);
-bool pmFPAfileIOChecks (psMetadata *files, pmFPAview *view, pmFPAfilePlace place);
+bool pmFPAfileReadChecks (psMetadata *files, const pmFPAview *view);
+bool pmFPAfileIOChecks (psMetadata *files, const pmFPAview *view, pmFPAfilePlace place);
 
-psImage *pmFPAfileReadoutImage (psMetadata *files, pmFPAview *view, char *name, int Nx, int Ny, int type);
+psImage *pmFPAfileReadoutImage (psMetadata *files, const pmFPAview *view, char *name, int Nx, int Ny, int type);
 
 // read an image into the current view
-bool pmFPAviewReadFitsImage (pmFPAview *view, pmFPAfile *file);
+bool pmFPAviewReadFitsImage (const pmFPAview *view, pmFPAfile *file);
 
 // write the components for the specified view
-bool pmFPAviewWriteFitsImage (pmFPAview *view, pmFPAfile *file);
+bool pmFPAviewWriteFitsImage (const pmFPAview *view, pmFPAfile *file);
 
 // convert the rule to a name based on the current view
-char *pmFPAfileNameFromRule (char *rule, pmFPAfile *file, pmFPAview *view);
+char *pmFPAfileNameFromRule (char *rule, pmFPAfile *file, const pmFPAview *view);
 
 pmFPAfile *pmFPAfileConstruct (psMetadata *files, psMetadata *format, psMetadata *camera, char *name);
Index: /branches/rel10_ifa/psModules/src/astrom/pmFPAview.c
===================================================================
--- /branches/rel10_ifa/psModules/src/astrom/pmFPAview.c	(revision 6725)
+++ /branches/rel10_ifa/psModules/src/astrom/pmFPAview.c	(revision 6726)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.1.2.5 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-28 02:16:26 $
+ *  @version $Revision: 1.1.2.6 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-03-29 20:55:42 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -39,5 +39,5 @@
 }
 
-pmFPAdepth pmFPAviewDepth (pmFPAview *view)
+pmFPAdepth pmFPAviewDepth (const pmFPAview *view)
 {
 
@@ -54,5 +54,5 @@
 }
 
-pmChip *pmFPAviewThisChip (pmFPAview *view, pmFPA *fpa)
+pmChip *pmFPAviewThisChip (const pmFPAview *view, pmFPA *fpa)
 {
 
@@ -100,5 +100,5 @@
 }
 
-pmCell *pmFPAviewThisCell (pmFPAview *view, pmFPA *fpa)
+pmCell *pmFPAviewThisCell (const pmFPAview *view, pmFPA *fpa)
 {
 
@@ -156,5 +156,5 @@
 }
 
-pmReadout *pmFPAviewThisReadout (pmFPAview *view, pmFPA *fpa)
+pmReadout *pmFPAviewThisReadout (const pmFPAview *view, pmFPA *fpa)
 {
 
@@ -211,5 +211,5 @@
 }
 
-pmHDU *pmFPAviewThisHDU (pmFPAview *view, pmFPA *fpa)
+pmHDU *pmFPAviewThisHDU (const pmFPAview *view, pmFPA *fpa)
 {
     // the HDU is attached to a cell, chip or fpa
@@ -229,14 +229,14 @@
 }
 
-pmHDU *pmFPAviewThisPHU (pmFPAview *view, pmFPA *fpa)
+pmHDU *pmFPAviewThisPHU (const pmFPAview *view, pmFPA *fpa)
 {
     // select the HDU which corresponds to the PHU containing this view
 
     pmHDU *hdu;
-    pmFPAview *new;
+    pmFPAview new;
     pmChip *chip;
     pmCell *cell;
 
-    *new = *view;
+    new = *view;
 
     if (view->chip < 0) {
@@ -251,6 +251,6 @@
         if (hdu->phu)
             return hdu;
-        new->chip = -1;
-        hdu = pmFPAviewThisPHU (new, fpa);
+        new.chip = -1;
+        hdu = pmFPAviewThisPHU (&new, fpa);
         return hdu;
     }
@@ -260,6 +260,6 @@
         if (hdu->phu)
             return hdu;
-        new->cell = -1;
-        hdu = pmFPAviewThisPHU (new, fpa);
+        new.cell = -1;
+        hdu = pmFPAviewThisPHU (&new, fpa);
         return hdu;
     }
Index: /branches/rel10_ifa/psModules/src/astrom/pmFPAview.h
===================================================================
--- /branches/rel10_ifa/psModules/src/astrom/pmFPAview.h	(revision 6725)
+++ /branches/rel10_ifa/psModules/src/astrom/pmFPAview.h	(revision 6726)
@@ -7,6 +7,6 @@
 *  @author EAM, IfA
 *
-*  @version $Revision: 1.1.2.7 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-03-28 02:16:26 $
+*  @version $Revision: 1.1.2.8 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-03-29 20:55:42 $
 *
 *  Copyright 2004-2005 Institute for Astronomy, University of Hawaii
@@ -44,8 +44,8 @@
 
 // determine the current view depth
-pmFPAdepth pmFPAviewDepth (pmFPAview *view);
+pmFPAdepth pmFPAviewDepth (const pmFPAview *view);
 
 // return the currently selected chip for this view
-pmChip *pmFPAviewThisChip (pmFPAview *view, pmFPA *fpa);
+pmChip *pmFPAviewThisChip (const pmFPAview *view, pmFPA *fpa);
 
 // advance view to the next chip
@@ -53,5 +53,5 @@
 
 // return the currently selected cell for this view
-pmCell *pmFPAviewThisCell (pmFPAview *view, pmFPA *fpa);
+pmCell *pmFPAviewThisCell (const pmFPAview *view, pmFPA *fpa);
 
 // advance view to the next cell
@@ -59,5 +59,5 @@
 
 // return the currently selected readout for this view
-pmReadout *pmFPAviewThisReadout (pmFPAview *view, pmFPA *fpa);
+pmReadout *pmFPAviewThisReadout (const pmFPAview *view, pmFPA *fpa);
 
 // advance view to the next readout
@@ -65,6 +65,6 @@
 
 // return the HDU corresponding to the current view
-pmHDU *pmFPAviewThisHDU (pmFPAview *view, pmFPA *fpa);
-pmHDU *pmFPAviewThisPHU (pmFPAview *view, pmFPA *fpa);
+pmHDU *pmFPAviewThisHDU (const pmFPAview *view, pmFPA *fpa);
+pmHDU *pmFPAviewThisPHU (const pmFPAview *view, pmFPA *fpa);
 
 # endif
Index: /branches/rel10_ifa/psModules/src/astrom/pmHDU.c
===================================================================
--- /branches/rel10_ifa/psModules/src/astrom/pmHDU.c	(revision 6725)
+++ /branches/rel10_ifa/psModules/src/astrom/pmHDU.c	(revision 6726)
@@ -13,4 +13,6 @@
     psFree(hdu->images);
     psFree(hdu->table);
+    psFree(hdu->weights);
+    psFree(hdu->masks);
 }
 
Index: /branches/rel10_ifa/psModules/src/objects/pmFPAviewObjectsIO.h
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmFPAviewObjectsIO.h	(revision 6725)
+++ /branches/rel10_ifa/psModules/src/objects/pmFPAviewObjectsIO.h	(revision 6726)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.1.2.1 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-09 03:14:23 $
+ *  @version $Revision: 1.1.2.2 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-03-29 20:55:42 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -13,15 +13,15 @@
 # define PM_FPA_OBJECTS_IO_H
 
-bool pmFPAviewReadObjects (pmFPAview *view, pmFPAfile *file);
-bool pmFPAReadObjects (pmFPA *fpa, pmFPAview *view, pmFPAfile *file);
-bool pmChipReadObjects (pmChip *chip, pmFPAview *view, pmFPAfile *file);
-bool pmCellReadObjects (pmCell *cell, pmFPAview *view, pmFPAfile *file);
-bool pmReadoutReadObjects (pmReadout *readout, pmFPAview *view, pmFPAfile *file);
+bool pmFPAviewReadObjects (const pmFPAview *view, pmFPAfile *file);
+bool pmFPAReadObjects (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file);
+bool pmChipReadObjects (pmChip *chip, const pmFPAview *view, pmFPAfile *file);
+bool pmCellReadObjects (pmCell *cell, const pmFPAview *view, pmFPAfile *file);
+bool pmReadoutReadObjects (pmReadout *readout, const pmFPAview *view, pmFPAfile *file);
 
-bool pmFPAviewWriteObjects (pmFPAview *view, pmFPAfile *file);
-bool pmFPAWriteObjects (pmFPA *fpa, pmFPAview *view, pmFPAfile *file);
-bool pmChipWriteObjects (pmChip *chip, pmFPAview *view, pmFPAfile *file);
-bool pmCellWriteObjects (pmCell *cell, pmFPAview *view, pmFPAfile *file);
-bool pmReadoutWriteObjects (pmReadout *readout, pmFPAview *view, pmFPAfile *file);
+bool pmFPAviewWriteObjects (const pmFPAview *view, pmFPAfile *file);
+bool pmFPAWriteObjects (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file);
+bool pmChipWriteObjects (pmChip *chip, const pmFPAview *view, pmFPAfile *file);
+bool pmCellWriteObjects (pmCell *cell, const pmFPAview *view, pmFPAfile *file);
+bool pmReadoutWriteObjects (pmReadout *readout, const pmFPAview *view, pmFPAfile *file);
 
 # endif /* PM_FPA_OBJECTS_IO_H */
Index: /branches/rel10_ifa/psModules/src/objects/pmFPAviewReadObjects.c
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmFPAviewReadObjects.c	(revision 6725)
+++ /branches/rel10_ifa/psModules/src/objects/pmFPAviewReadObjects.c	(revision 6726)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA; GLG, MHPCC
  *
- *  @version $Revision: 1.1.2.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-28 02:14:56 $
+ *  @version $Revision: 1.1.2.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-03-29 20:55:42 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -28,5 +28,5 @@
 
 // Given a FITS file pointer, read the table of object data
-bool pmFPAviewReadObjects (pmFPAview *view, pmFPAfile *file)
+bool pmFPAviewReadObjects (const pmFPAview *view, pmFPAfile *file)
 {
     pmFPA *fpa = file->fpa;
@@ -67,5 +67,5 @@
 
 // read in all chip-level Objects files for this FPA
-bool pmFPAReadObjects (pmFPA *fpa, pmFPAview *view, pmFPAfile *file)
+bool pmFPAReadObjects (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file)
 {
 
@@ -79,5 +79,5 @@
 
 // read in all cell-level Objects files for this chip
-bool pmChipReadObjects (pmChip *chip, pmFPAview *view, pmFPAfile *file)
+bool pmChipReadObjects (pmChip *chip, const pmFPAview *view, pmFPAfile *file)
 {
 
@@ -91,5 +91,5 @@
 
 // read in all readout-level Objects files for this cell
-bool pmCellReadObjects (pmCell *cell, pmFPAview *view, pmFPAfile *file)
+bool pmCellReadObjects (pmCell *cell, const pmFPAview *view, pmFPAfile *file)
 {
 
@@ -103,5 +103,5 @@
 
 // read in all readout-level Objects files for this cell
-bool pmReadoutReadObjects (pmReadout *readout, pmFPAview *view, pmFPAfile *file)
+bool pmReadoutReadObjects (pmReadout *readout, const pmFPAview *view, pmFPAfile *file)
 {
 
Index: /branches/rel10_ifa/psModules/src/objects/pmFPAviewWriteObjects.c
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmFPAviewWriteObjects.c	(revision 6725)
+++ /branches/rel10_ifa/psModules/src/objects/pmFPAviewWriteObjects.c	(revision 6726)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA; GLG, MHPCC
  *
- *  @version $Revision: 1.1.2.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-28 02:14:56 $
+ *  @version $Revision: 1.1.2.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-03-29 20:55:42 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -14,4 +14,5 @@
 #include <string.h>
 #include "pslib.h"
+#include "psAdditionals.h"
 #include "pmHDU.h"
 #include "pmFPA.h"
@@ -27,32 +28,6 @@
 #include "pmFPAviewObjectsIO.h"
 
-bool psFitsWriteHeaderNotImage (psFits *fits, psMetadata *header)
-{
-
-    int status = 0;
-    char fitsErr[1024];
-
-    psMetadataItem *mdi = NULL;
-    mdi = psMetadataLookup (header, "NAXIS");
-    if (mdi != NULL) {
-        mdi->data.S32 = 0;
-        mdi->type = PS_DATA_S32;
-    }
-
-    fits_create_img (fits->fd, SHORT_IMG, 0, NULL, &status);
-
-    if (status != 0) {
-        fits_get_errstatus (status, fitsErr);
-        psError (PS_ERR_IO, true, PS_ERRORTEXT_psFits_WRITE_FAILED, fitsErr);
-        exit (2);
-    }
-
-    psFitsWriteHeader (header, fits);
-
-    return true;
-}
-
 // Given a FITS file pointer, read the table of object data
-bool pmFPAviewWriteObjects (pmFPAview *view, pmFPAfile *file)
+bool pmFPAviewWriteObjects (const pmFPAview *view, pmFPAfile *file)
 {
 
@@ -94,5 +69,5 @@
 
 // read in all chip-level Objects files for this FPA
-bool pmFPAWriteObjects (pmFPA *fpa, pmFPAview *view, pmFPAfile *file)
+bool pmFPAWriteObjects (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file)
 {
 
@@ -106,5 +81,5 @@
 
 // read in all cell-level Objects files for this chip
-bool pmChipWriteObjects (pmChip *chip, pmFPAview *view, pmFPAfile *file)
+bool pmChipWriteObjects (pmChip *chip, const pmFPAview *view, pmFPAfile *file)
 {
 
@@ -118,5 +93,5 @@
 
 // read in all readout-level Objects files for this cell
-bool pmCellWriteObjects (pmCell *cell, pmFPAview *view, pmFPAfile *file)
+bool pmCellWriteObjects (pmCell *cell, const pmFPAview *view, pmFPAfile *file)
 {
 
@@ -130,5 +105,5 @@
 
 // read in all readout-level Objects files for this cell
-bool pmReadoutWriteObjects (pmReadout *readout, pmFPAview *view, pmFPAfile *file)
+bool pmReadoutWriteObjects (pmReadout *readout, const pmFPAview *view, pmFPAfile *file)
 {
 
@@ -139,7 +114,8 @@
     pmHDU *hdu;
     pmHDU *phu;
+    psMetadata *updates;
+    psMetadata *outhead;
 
     psArray *sources = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.SOURCES");
-    return true;
 
     switch (file->type) {
@@ -166,5 +142,25 @@
         hdu = pmFPAviewThisHDU (view, file->fpa);
         filename = pmFPAfileNameFromRule (file->filerule, file, view);
-        pmSourcesWriteCMP (sources, filename, hdu->header);
+
+        // copy the header to an output header, add the output header data
+        outhead = psMetadataCopy (NULL, hdu->header);
+
+        // check/fix first line (must be SIMPLE = F)
+        psMetadataItem *item = psMetadataGet (outhead, PS_LIST_HEAD);
+        if (strcmp (item->name, "SIMPLE") && strcmp (item->name, "XTENSION")) {
+            psErrorStackPrint(stderr, "invalid header: first line is neither SIMPLE nor XTENSION\n");
+            exit(EXIT_FAILURE);
+        }
+        if (!strcmp (item->name, "XTENSION")) {
+            psMetadataRemoveIndex (outhead, PS_LIST_HEAD);
+        }
+        psMetadataAddBool (outhead, PS_LIST_HEAD, "SIMPLE", PS_META_REPLACE, "CMP file, not simple", false);
+
+        // copy over the entries saved in the
+        updates = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.HEADER");
+        psMetadataCopy (outhead, updates);
+
+        pmSourcesWriteCMP (sources, filename, outhead);
+        psFree (outhead);
         psFree (filename);
         break;
@@ -184,10 +180,10 @@
 
             // CMF always has extensions
-            psMetadataAdd (phu->header, PS_LIST_TAIL, "EXTEND", PS_DATA_BOOL | PS_META_REPLACE, "this file has extensions", true);
-            psFitsWriteHeaderNotImage (file->fits, phu->header);
+            outhead = psMetadataCopy (NULL, phu->header);
+            psMetadataAdd (outhead, PS_LIST_TAIL, "EXTEND", PS_DATA_BOOL | PS_META_REPLACE, "this file has extensions", true);
+            psFitsWriteHeaderNotImage (file->fits, outhead);
             file->phu = phu->header;
             psTrace ("pmFPAfile", 5, "wrote phu %s (type: %d)\n", file->filename, file->type);
-
-            file->phu = phu->header;
+            psFree (outhead);
         }
 
@@ -196,8 +192,10 @@
             // determine name for header extension
             headname = pmFPAfileNameFromRule (file->extxtra, file, view);
-            psMetadataAddStr (hdu->header, PS_LIST_TAIL, "EXTNAME", PS_META_REPLACE, "extension name", headname);
-            psFitsWriteHeaderNotImage (file->fits, hdu->header);
+            outhead = psMetadataCopy (NULL, hdu->header);
+            psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTNAME", PS_META_REPLACE, "extension name", headname);
+            psFitsWriteHeaderNotImage (file->fits, outhead);
             psTrace ("pmFPAfile", 5, "wrote ext head %s (type: %d)\n", file->filename, file->type);
             file->header = hdu->header;
+            psFree (outhead);
             psFree (headname);
         }
@@ -205,5 +203,7 @@
         // write this table to disk
         dataname = pmFPAfileNameFromRule (file->extrule, file, view);
-        pmSourcesWriteCMF (file->fits, sources, hdu->header, dataname);
+        updates = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.HEADER");
+        pmSourcesWriteCMF (file->fits, sources, updates, dataname);
+        psTrace ("pmFPAfile", 5, "wrote ext data %s (type: %d)\n", file->filename, file->type);
         psFree (dataname);
         break;
Index: /branches/rel10_ifa/psModules/src/objects/pmSourceIO_CMF.c
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmSourceIO_CMF.c	(revision 6725)
+++ /branches/rel10_ifa/psModules/src/objects/pmSourceIO_CMF.c	(revision 6726)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.1.2.4 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-28 02:14:56 $
+ *  @version $Revision: 1.1.2.5 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-03-29 20:55:42 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -34,5 +34,4 @@
     psArray *table;
     psMetadata *row;
-    psMetadata *theader;
     int i, type;
     psF32 *PAR, *dPAR;
@@ -84,10 +83,10 @@
 
     // create the basic table header
-    theader = psMetadataAlloc ();
-    psMetadataAdd (theader, PS_LIST_HEAD, "EXTNAME", PS_DATA_STRING, "extension name", extname);
+    psMetadataAdd (header, PS_LIST_HEAD, "EXTNAME", PS_DATA_STRING | PS_META_REPLACE, "extension name", extname);
 
-    psFitsWriteTable (fits, theader, table);
+    psFitsWriteTable (fits, header, table);
     psTrace ("pmFPAfile", 5, "wrote ext data %s\n", extname);
 
+    psFree (table);
     return true;
 }
Index: /branches/rel10_ifa/psModules/src/objects/pmSourceIO_CMP.c
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmSourceIO_CMP.c	(revision 6725)
+++ /branches/rel10_ifa/psModules/src/objects/pmSourceIO_CMP.c	(revision 6726)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.1.2.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-09 04:25:03 $
+ *  @version $Revision: 1.1.2.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-03-29 20:55:42 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -107,4 +107,5 @@
     }
     fclose (f);
+    psFree (line);
     return true;
 }
Index: /branches/rel10_ifa/psModules/src/objects/pmSourceIO_SX.c
===================================================================
--- /branches/rel10_ifa/psModules/src/objects/pmSourceIO_SX.c	(revision 6725)
+++ /branches/rel10_ifa/psModules/src/objects/pmSourceIO_SX.c	(revision 6726)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.1.2.3 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-03-09 04:25:03 $
+ *  @version $Revision: 1.1.2.4 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-03-29 20:55:42 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -76,4 +76,5 @@
     }
     fclose (f);
+    psFree (line);
     return true;
 }
