Index: trunk/psModules/src/objects/pmPSF_IO.c
===================================================================
--- trunk/psModules/src/objects/pmPSF_IO.c	(revision 18600)
+++ trunk/psModules/src/objects/pmPSF_IO.c	(revision 18601)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.33 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-07-17 20:43:38 $
+ *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-07-17 22:38:15 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -35,4 +35,5 @@
 #include "pmFPAview.h"
 #include "pmFPAfile.h"
+#include "pmFPAfileFitsIO.h"
 
 #include "pmPeaks.h"
@@ -107,5 +108,5 @@
 }
 
-bool pmPSFmodelWriteForView (const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmPSFmodelWriteForView (const pmFPAview *view, pmFPAfile *file, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(view, false);
@@ -142,5 +143,5 @@
 
 // read in all chip-level PSFmodel files for this FPA
-bool pmPSFmodelWriteFPA (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmPSFmodelWriteFPA (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(view, false);
@@ -164,5 +165,5 @@
 
 // read in all cell-level PSFmodel files for this chip
-bool pmPSFmodelWriteChip (pmChip *chip, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmPSFmodelWriteChip (pmChip *chip, const pmFPAview *view, pmFPAfile *file, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(view, false);
@@ -185,5 +186,5 @@
 //   - psf table (+header) : FITS Table
 bool pmPSFmodelWrite (psMetadata *analysis, const pmFPAview *view,
-                      pmFPAfile *file, const pmConfig *config)
+                      pmFPAfile *file, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(view, false);
@@ -208,5 +209,7 @@
 
     // get the current header
-    pmHDU *hdu = pmFPAviewThisHDU (view, file->fpa);
+    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // Suitable FPA for writing
+    pmHDU *hdu = psMemIncrRefCounter(pmFPAviewThisHDU(view, fpa));
+    psFree(fpa);
     if (!hdu) {
         psError(PS_ERR_UNKNOWN, false, "Unable to find HDU");
@@ -229,4 +232,5 @@
         if (!menu) {
             psError(PS_ERR_UNKNOWN, true, "missing EXTNAME.RULES in camera.config");
+            psFree(hdu);
             return false;
         }
@@ -236,4 +240,5 @@
         if (!rule) {
             psError(PS_ERR_UNKNOWN, false, "missing entry for PSF.HEAD in EXTNAME.RULES in camera.config");
+            psFree(hdu);
             return false;
         }
@@ -245,4 +250,5 @@
             psError(PS_ERR_UNKNOWN, false, "missing entry for PSF.TABLE in EXTNAME.RULES in camera.config");
             psFree (headName);
+            psFree(hdu);
             return false;
         }
@@ -255,4 +261,5 @@
             psFree (headName);
             psFree (tableName);
+            psFree(hdu);
             return false;
         }
@@ -278,4 +285,5 @@
         psFree (headName);
     }
+    psFree(hdu);
 
     // select the psf of interest
@@ -481,5 +489,5 @@
 
 // if this file needs to have a PHU written out, write one
-bool pmPSFmodelWritePHU (const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmPSFmodelWritePHU (const pmFPAview *view, pmFPAfile *file, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(view, false);
@@ -496,6 +504,9 @@
     if (file->fpa->chips->n == 1) return true;
 
+
     // find the FPA phu
-    pmHDU *phu = pmFPAviewThisPHU (view, file->fpa);
+    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // Suitable FPA for writing
+    pmHDU *phu = psMemIncrRefCounter(pmFPAviewThisPHU(view, fpa));
+    psFree(fpa);
 
     // if there is no PHU, this is a single header+image (extension-less) file. This could be
@@ -508,4 +519,5 @@
         pmConfigConformHeader (outhead, file->format);
     }
+    psFree(phu);
 
     psMetadataAddBool (outhead, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
