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);
Index: trunk/psModules/src/objects/pmPSF_IO.h
===================================================================
--- trunk/psModules/src/objects/pmPSF_IO.h	(revision 18600)
+++ trunk/psModules/src/objects/pmPSF_IO.h	(revision 18601)
@@ -6,6 +6,6 @@
  * @author EAM, IfA
  *
- * @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-09-24 21:27:58 $
+ * @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2008-07-17 22:38:15 $
  * Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  */
@@ -17,10 +17,10 @@
 /// @{
 
-bool pmPSFmodelWriteForView (const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
-bool pmPSFmodelWriteFPA (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
-bool pmPSFmodelWriteChip (pmChip *chip, const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
-bool pmPSFmodelWrite (psMetadata *analysis, const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
+bool pmPSFmodelWriteForView (const pmFPAview *view, pmFPAfile *file, pmConfig *config);
+bool pmPSFmodelWriteFPA (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file, pmConfig *config);
+bool pmPSFmodelWriteChip (pmChip *chip, const pmFPAview *view, pmFPAfile *file, pmConfig *config);
+bool pmPSFmodelWrite (psMetadata *analysis, const pmFPAview *view, pmFPAfile *file, pmConfig *config);
 
-bool pmPSFmodelWritePHU (const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
+bool pmPSFmodelWritePHU (const pmFPAview *view, pmFPAfile *file, pmConfig *config);
 
 bool pmPSFmodelReadForView (const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
Index: trunk/psModules/src/objects/pmSourceIO.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO.c	(revision 18600)
+++ trunk/psModules/src/objects/pmSourceIO.c	(revision 18601)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.62 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-07-17 20:43:23 $
+ *  @version $Revision: 1.63 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-07-17 22:38:15 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -28,4 +28,5 @@
 #include "pmFPAview.h"
 #include "pmFPAfile.h"
+#include "pmFPAfileFitsIO.h"
 #include "pmConcepts.h"
 
@@ -108,5 +109,5 @@
 
 // Given a FITS file pointer, write the table of object data
-bool pmFPAviewWriteObjects (const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmFPAviewWriteObjects (const pmFPAview *view, pmFPAfile *file, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(view, false);
@@ -114,11 +115,13 @@
     PS_ASSERT_PTR_NON_NULL(file->fpa, false);
 
-    pmFPA *fpa = file->fpa;
+    pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config, false); // Suitable FPA for writing
 
     if (view->chip == -1) {
         if (!pmFPAWriteObjects (fpa, view, file, config)) {
             psError(PS_ERR_IO, false, "Failed to write objects from fpa");
-            return false;
-        }
+            psFree(fpa);
+            return false;
+        }
+        psFree(fpa);
         return true;
     }
@@ -126,4 +129,5 @@
     if (view->chip >= fpa->chips->n) {
         psError(PS_ERR_UNKNOWN, false, "Writing chip == %d (>= chips->n == %ld)", view->chip, fpa->chips->n);
+        psFree(fpa);
         return false;
     }
@@ -133,6 +137,8 @@
         if (!pmChipWriteObjects (chip, view, file, config)) {
             psError(PS_ERR_IO, false, "Failed to write objects from chip");
-            return false;
-        }
+            psFree(fpa);
+            return false;
+        }
+        psFree(fpa);
         return true;
     }
@@ -141,4 +147,5 @@
         psError(PS_ERR_UNKNOWN, false, "Writing cell == %d (>= cells->n == %ld)",
                 view->cell, chip->cells->n);
+        psFree(fpa);
         return false;
     }
@@ -148,7 +155,8 @@
         if (!pmCellWriteObjects (cell, view, file, config)) {
             psError(PS_ERR_IO, false, "Failed to write objects from cell");
-            return false;
-        }
-
+            psFree(fpa);
+            return false;
+        }
+        psFree(fpa);
         return true;
     }
@@ -157,4 +165,5 @@
         psError(PS_ERR_UNKNOWN, false, "Writing readout == %d (>= readouts->n == %ld)",
                 view->readout, cell->readouts->n);
+        psFree(fpa);
         return false;
     }
@@ -163,7 +172,9 @@
     if (!pmReadoutWriteObjects (readout, view, file, config)) {
         psError(PS_ERR_IO, false, "Failed to write objects from readout %d", view->readout);
-        return false;
-    }
-
+        psFree(fpa);
+        return false;
+    }
+
+    psFree(fpa);
     return true;
 }
@@ -244,5 +255,11 @@
     PS_ASSERT_PTR_NON_NULL(view, false);
     PS_ASSERT_PTR_NON_NULL(file, false);
-    PS_ASSERT_PTR_NON_NULL(file->fpa, false);
+
+    pmCell *cell = readout->parent;
+    PS_ASSERT_PTR_NON_NULL(cell, false);
+    pmChip *chip = cell->parent;
+    PS_ASSERT_PTR_NON_NULL(chip, false);
+    pmFPA *fpa = chip->parent;
+    PS_ASSERT_PTR_NON_NULL(fpa, false);
 
     bool status;
@@ -281,5 +298,5 @@
       case PM_FPA_FILE_CMP:
         // a SPLIT format : only one header and object table per file
-        hdu = pmFPAviewThisHDU (view, file->fpa);
+        hdu = pmFPAviewThisHDU (view, fpa);
         if (!hdu) {
             psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find HDU to write sources.");
@@ -317,5 +334,5 @@
 
         // get the current header
-        hdu = pmFPAviewThisHDU (view, file->fpa);
+        hdu = pmFPAviewThisHDU (view, fpa);
         if (!hdu) {
             psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find HDU to write sources.");
@@ -486,5 +503,5 @@
 
 // if this file needs to have a PHU written out, write one
-bool pmSource_CMF_WritePHU (const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
+bool pmSource_CMF_WritePHU (const pmFPAview *view, pmFPAfile *file, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(view, false);
@@ -503,6 +520,8 @@
     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));
 
     // if there is no PHU, this is a single header+image (extension-less) file. This could be
@@ -513,4 +532,5 @@
         psMetadataCopy (outhead, phu->header);
     }
+    psFree(phu);
 
     pmConfigConformHeader (outhead, file->format);
@@ -521,9 +541,16 @@
     // XXX why are these not correctly inserted by pmConfigConformHeader??
 
-    psMetadata *headers = psMetadataLookupMetadata(NULL, file->fpa->camera, BLANK_HEADERS); // Header names
+    // Because these concepts are not part of the "RULE" in the camera format, pmConfigConformHeader only adds
+    // what is required by the "RULE".
+    // Though we can configure Ohana to look at particular header keywords, it doesn't like having the
+    // important values in "HIERARCH FPA.TIME", etc, as is done for skycells, so we stoop to its level,
+    // putting in additional header keywords that it can understand.
+
+    psMetadata *headers = psMetadataLookupMetadata(NULL, fpa->camera, BLANK_HEADERS); // Header names
     if (!headers) {
         psError(PS_ERR_UNEXPECTED_NULL, false,
                 "Unable to find %s metadata within camera configuration", BLANK_HEADERS);
         psFree(outhead);
+        psFree(fpa);
         return false;
     }
@@ -535,7 +562,8 @@
                     "Unable to find FPA.TIME in %s within camera configuration.", BLANK_HEADERS);
             psFree(outhead);
-            return false;
-        }
-        psTime *time = psMetadataLookupTime(NULL, file->fpa->concepts, "FPA.TIME"); // Time of observation
+            psFree(fpa);
+            return false;
+        }
+        psTime *time = psMetadataLookupTime(NULL, fpa->concepts, "FPA.TIME"); // Time of observation
         double mjd = psTimeToMJD(time); // The MJD of observation
         psMetadataAddF64(outhead, PS_LIST_TAIL, mjdName, PS_META_REPLACE,
@@ -550,8 +578,8 @@
                     BLANK_HEADERS);
             psFree(outhead);
-            return false;
-        }
-        float exptime = psMetadataLookupF32(NULL, file->fpa->concepts,
-                                            "FPA.EXPOSURE"); // Exposure time
+            psFree(fpa);
+            return false;
+        }
+        float exptime = psMetadataLookupF32(NULL, fpa->concepts, "FPA.EXPOSURE"); // Exposure time
         psMetadataAddF32(outhead, PS_LIST_TAIL, expName, PS_META_REPLACE,
                          "Exposure time (sec)", exptime);
@@ -565,7 +593,8 @@
                     BLANK_HEADERS);
             psFree(outhead);
-            return false;
-        }
-        float airmass = psMetadataLookupF32(NULL, file->fpa->concepts, "FPA.AIRMASS"); // Airmass
+            psFree(fpa);
+            return false;
+        }
+        float airmass = psMetadataLookupF32(NULL, fpa->concepts, "FPA.AIRMASS"); // Airmass
         psMetadataAddF32(outhead, PS_LIST_TAIL, amName, PS_META_REPLACE,
                          "Observation airmass", airmass);
@@ -575,5 +604,5 @@
     const char *fpaNameHdr = psMetadataLookupStr(NULL, fileData, "FPA.OBS");
     if (fpaNameHdr && strlen(fpaNameHdr) > 0) {
-        const char *fpaName = psMetadataLookupStr(NULL, file->fpa->concepts, "FPA.OBS");
+        const char *fpaName = psMetadataLookupStr(NULL, fpa->concepts, "FPA.OBS");
         psMetadataAddStr(outhead, PS_LIST_TAIL, fpaNameHdr, PS_META_REPLACE,
                          "FPA observation identifier", fpaName);
@@ -581,8 +610,9 @@
 
     // if we have mosaic-level astrometry information, add it here:
-    psMetadata *updates = psMetadataLookupPtr (&status, file->fpa->analysis, "PSASTRO.HEADER");
+    psMetadata *updates = psMetadataLookupPtr (&status, fpa->analysis, "PSASTRO.HEADER");
     if (updates) {
         psMetadataCopy (outhead, updates);
     }
+    psFree(fpa);
 
     psMetadataAddBool (outhead, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
Index: trunk/psModules/src/objects/pmSourceIO.h
===================================================================
--- trunk/psModules/src/objects/pmSourceIO.h	(revision 18600)
+++ trunk/psModules/src/objects/pmSourceIO.h	(revision 18601)
@@ -4,6 +4,6 @@
  * @author EAM, IfA; GLG, MHPCC
  *
- * @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
- * @date $Date: 2008-05-29 13:25:38 $
+ * @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2008-07-17 22:38:15 $
  * Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  *
@@ -30,5 +30,5 @@
 bool pmSourcesWrite_PS1_DEV_1_XFIT (psFits *fits, psArray *sources, char *extname);
 
-bool pmSource_CMF_WritePHU (const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
+bool pmSource_CMF_WritePHU (const pmFPAview *view, pmFPAfile *file, pmConfig *config);
 
 psArray *pmSourcesReadCMP (char *filename, psMetadata *header);
@@ -50,5 +50,5 @@
 bool pmReadoutReadObjects (pmReadout *readout, const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
 
-bool pmFPAviewWriteObjects (const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
+bool pmFPAviewWriteObjects (const pmFPAview *view, pmFPAfile *file, pmConfig *config);
 bool pmFPAWriteObjects (pmFPA *fpa, const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
 bool pmChipWriteObjects (pmChip *chip, const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
