Index: trunk/psModules/src/objects/pmSourceIO.c
===================================================================
--- trunk/psModules/src/objects/pmSourceIO.c	(revision 18599)
+++ 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);
