Index: /trunk/psModules/src/objects/pmSourceIO.c
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO.c	(revision 14207)
+++ /trunk/psModules/src/objects/pmSourceIO.c	(revision 14208)
@@ -3,6 +3,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.44 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-07-11 03:09:23 $
+ *  @version $Revision: 1.45 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-07-14 03:20:44 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -243,5 +243,4 @@
     char *headname;
     pmHDU *hdu;
-    pmHDU *phu;
     psMetadata *updates;
     psMetadata *outhead;
@@ -294,5 +293,4 @@
 
       case PM_FPA_FILE_CMF:
-        // write a PHU? (only if input image is MEF)
         // write a header? (only if this is the first readout for cell)
         //   note that the file->header is set to track the last hdu->header written
@@ -301,103 +299,4 @@
         // get the current header
         hdu = pmFPAviewThisHDU (view, file->fpa);
-
-        // if file does not yet have a PHU, attempt to write one to disk
-        // we only need a PHU if chips->n > 1 and file->fileLevel == FPA
-        // otherwise, the chip header fills the PHU location
-        // XXX this code could be placed in a 'pmSource_CMF_WritePHU' function and called
-        // from pmFPAfileIO.c.
-        if ((file->fileLevel == PM_FPA_LEVEL_FPA) && (file->fpa->chips->n > 1) && !file->phu) {
-
-            // find the FPA phu
-            phu = pmFPAviewThisPHU (view, file->fpa);
-
-            // if there is no PHU, this is a single header+image (extension-less) file
-            // if there is a PHU, write it out as a 'blank'
-            outhead = psMetadataAlloc();
-            if (phu) {
-                psMetadataCopy (outhead, phu->header);
-            } else {
-                pmConfigConformHeader (outhead, file->format);
-
-                psMetadata *fileData = psMetadataLookupMetadata(NULL, file->format, "FILE"); // File information
-                const char *fpaNameHdr = psMetadataLookupStr(NULL, fileData, "FPA.NAME");
-                if (fpaNameHdr && strlen(fpaNameHdr) > 0) {
-                    const char *fpaName = psMetadataLookupStr(NULL, file->fpa->concepts, "FPA.NAME");
-                    psMetadataAddStr(outhead, PS_LIST_TAIL, fpaNameHdr, PS_META_REPLACE, "FPA name", fpaName);
-                }
-
-#if 0
-                // We need to get some FPA-level concepts in there
-                // This is a hack, not very pretty.  But then, so is writing the FPA in this manner without
-                // using the pmFPAMosaic functions....
-
-                psMetadata *headers = psMetadataLookupMetadata(NULL, file->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);
-                    return false;
-                }
-
-                {
-                    const char *mjdName = psMetadataLookupStr(NULL, headers, "FPA.TIME"); // Header name
-                    if (!mjdName || strlen(mjdName) == 0) {
-                        psError(PS_ERR_UNEXPECTED_NULL, false,
-                                "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
-                    double mjd = psTimeToMJD(time); // The MJD of observation
-                    psMetadataAddF64(outhead, PS_LIST_TAIL, mjdName, PS_META_REPLACE,
-                                     "Time of observation", mjd);
-                }
-
-                {
-                    const char *expName = psMetadataLookupStr(NULL, headers, "FPA.EXPOSURE"); // Header name
-                    if (!expName || strlen(expName) == 0) {
-                        psError(PS_ERR_UNEXPECTED_NULL, false,
-                                "Unable to find FPA.EXPOSURE in %s within camera configuration.",
-                                BLANK_HEADERS);
-                        psFree(outhead);
-                        return false;
-                    }
-                    float exptime = psMetadataLookupF32(NULL, file->fpa->concepts,
-                                                        "FPA.EXPOSURE"); // Exposure time
-                    psMetadataAddF32(outhead, PS_LIST_TAIL, expName, PS_META_REPLACE,
-                                     "Exposure time (sec)", exptime);
-                }
-
-                {
-                    const char *amName = psMetadataLookupStr(NULL, headers, "FPA.AIRMASS"); // Header name
-                    if (!amName || strlen(amName) == 0) {
-                        psError(PS_ERR_UNEXPECTED_NULL, false,
-                                "Unable to find FPA.AIRMASS in %s within camera configuration.",
-                                BLANK_HEADERS);
-                        psFree(outhead);
-                        return false;
-                    }
-                    float airmass = psMetadataLookupF32(NULL, file->fpa->concepts, "FPA.AIRMASS"); // Airmass
-                    psMetadataAddF32(outhead, PS_LIST_TAIL, amName, PS_META_REPLACE,
-                                     "Observation airmass", airmass);
-                }
-#endif
-
-            }
-
-            // if we have mosaic-level astrometry information, add it here:
-            updates = psMetadataLookupPtr (&status, file->fpa->analysis, "PSASTRO.HEADER");
-            if (updates) {
-                psMetadataCopy (outhead, updates);
-            }
-
-            psMetadataAddBool (outhead, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
-            psFitsWriteBlank (file->fits, outhead, "");
-            file->phu = true;
-            psTrace ("pmFPAfile", 5, "wrote phu %s (type: %d)\n", file->filename, file->type);
-            psFree (outhead);
-        }
 
         // define the EXTNAME values for the different data segments:
@@ -430,5 +329,5 @@
         }
 
-        // write out the IMAGE header segment
+        // write out the IMAGE header segment (only for the first readout of the cell)
         {
             // this header block is new, write it to disk
@@ -437,7 +336,8 @@
                 psMetadataAddStr (hdu->header, PS_LIST_TAIL, "EXTDATA", PS_META_REPLACE, "name of table extension", dataname);
                 psMetadataAddStr (hdu->header, PS_LIST_TAIL, "EXTTYPE", PS_META_REPLACE, "extension type", "IMAGE");
-                if (!file->phu) {
+                if (!file->wrote_phu) {
                     // this hdu->header acts as the PHU: set EXTEND to be true
                     psMetadataAddBool (hdu->header, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
+		    file->wrote_phu = true;
                 }
 
@@ -512,4 +412,55 @@
 // a MEF CMF file has: PHU, CELL-HEAD, TABLE, CELL-HEAD, TABLE, TABLE, TABLE...
 
+// 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 status;
+
+    // not needed if already written
+    if (file->wrote_phu) return true;
+
+    // not needed if not FPA
+    // XXX this prevents us from defining a SPLIT/MEF CMF file...
+    if (file->fileLevel != PM_FPA_LEVEL_FPA) return true;
+
+    // not needed if only one chip
+    if (file->fpa->chips->n == 1) return true;
+
+    // find the FPA phu
+    pmHDU *phu = pmFPAviewThisPHU (view, file->fpa);
+
+    // if there is no PHU, this is a single header+image (extension-less) file. This could be
+    // the case for an input SPLIT set of files being written out as a MEF.  if there is a PHU,
+    // write it out as a 'blank'
+    psMetadata *outhead = psMetadataAlloc();
+    if (phu) {
+	psMetadataCopy (outhead, phu->header);
+    } else {
+	pmConfigConformHeader (outhead, file->format);
+
+	psMetadata *fileData = psMetadataLookupMetadata(NULL, file->format, "FILE"); // File information
+	const char *fpaNameHdr = psMetadataLookupStr(NULL, fileData, "FPA.NAME");
+	if (fpaNameHdr && strlen(fpaNameHdr) > 0) {
+	    const char *fpaName = psMetadataLookupStr(NULL, file->fpa->concepts, "FPA.NAME");
+	    psMetadataAddStr(outhead, PS_LIST_TAIL, fpaNameHdr, PS_META_REPLACE, "FPA name", fpaName);
+	}
+    }
+
+    // if we have mosaic-level astrometry information, add it here:
+    psMetadata *updates = psMetadataLookupPtr (&status, file->fpa->analysis, "PSASTRO.HEADER");
+    if (updates) {
+	psMetadataCopy (outhead, updates);
+    }
+
+    psMetadataAddBool (outhead, PS_LIST_TAIL, "EXTEND", PS_META_REPLACE, "this file has extensions", true);
+    psFitsWriteBlank (file->fits, outhead, "");
+    file->wrote_phu = true;
+
+    psTrace ("pmFPAfile", 5, "wrote phu %s (type: %d)\n", file->filename, file->type);
+    psFree (outhead);
+
+    return true;
+}
+
 // Given a FITS file pointer, read the table of object data
 // XXX add in error handling
Index: /trunk/psModules/src/objects/pmSourceIO.h
===================================================================
--- /trunk/psModules/src/objects/pmSourceIO.h	(revision 14207)
+++ /trunk/psModules/src/objects/pmSourceIO.h	(revision 14208)
@@ -4,6 +4,6 @@
  * @author EAM, IfA; GLG, MHPCC
  *
- * @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-05-03 20:04:31 $
+ * @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-07-14 03:20:44 $
  * Copyright 2004 Maui High Performance Computing Center, University of Hawaii
  *
@@ -26,4 +26,6 @@
 bool pmSourcesWrite_SMPDATA (psFits *fits, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname);
 bool pmSourcesWrite_PS1_DEV_0 (psFits *fits, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname);
+
+bool pmSource_CMF_WritePHU (const pmFPAview *view, pmFPAfile *file, const pmConfig *config);
 
 psArray *pmSourcesReadCMP (char *filename, psMetadata *header);
