Index: trunk/psModules/src/objects/pmPSF_IO.c
===================================================================
--- trunk/psModules/src/objects/pmPSF_IO.c	(revision 13526)
+++ trunk/psModules/src/objects/pmPSF_IO.c	(revision 14207)
@@ -6,6 +6,6 @@
  *  @author EAM, IfA
  *
- *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2007-05-26 02:52:27 $
+ *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2007-07-14 03:20:22 $
  *
  *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
@@ -224,5 +224,4 @@
     bool status;
     pmHDU *hdu;
-    pmHDU *phu;
     char *headName, *tableName, *residName;
 
@@ -240,31 +239,4 @@
     // XXX this code could be placed in a 'pmPSF_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'
-	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);
-	    }
-	}
-
-	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 used for image header, table data, and residual image segments
@@ -315,7 +287,8 @@
 	psMetadataAddStr (hdu->header, PS_LIST_TAIL, "EXTRESID", PS_META_REPLACE, "name of resid extension", residName);
 	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;
 	}
 
@@ -471,4 +444,47 @@
 }
 
+// if this file needs to have a PHU written out, write one
+bool pmPSF_WritePHU (const pmFPAview *view, pmFPAfile *file, const pmConfig *config) {
+
+    // 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);
+	}
+    }
+
+    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;
+}
+
 bool pmFPAviewReadPSFmodel (const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
 {
