Index: /trunk/psModules/src/camera/pmFPAfileIO.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAfileIO.c	(revision 14205)
+++ /trunk/psModules/src/camera/pmFPAfileIO.c	(revision 14206)
@@ -465,7 +465,7 @@
         status = psFitsClose (file->fits);
         file->fits = NULL;
-        file->phu = NULL;
         file->header = NULL;
         file->state = PM_FPA_STATE_CLOSED;
+        file->wrote_phu = false;
         break;
 
@@ -664,7 +664,4 @@
       case PM_FPA_FILE_HEADER:
       case PM_FPA_FILE_FRINGE:
-        // XXX note that for CMF and PSF, we do not know yet if there is actually any data to
-        // write out.  this is because these types of output files have their data stored on the
-        // readout->analysis metadata structure of another (existing) fpa
       case PM_FPA_FILE_CMF:
       case PM_FPA_FILE_PSF:
@@ -691,5 +688,5 @@
 
         // In some cases, we need to read the PHU after we've opened the file.  This happens for
-        // the images supplied by the detrend database, which are only identified here (above).
+        // the images supplied by the detrend database, which are only identified here (pmConfigConvertFilename).
         if (!pmFPAfileReadPHU (file, view, config)) {
             psError (PS_ERR_IO, true, "error reading PHU for %s (%s) (%d:%d:%d)\n", file->filename, file->name, view->chip, view->cell, view->readout);
@@ -787,4 +784,6 @@
     PS_ASSERT_PTR_NON_NULL(view, false);
 
+    if (file->wrote_phu) return true;
+
     bool status = true;
     switch (file->type) {
@@ -793,42 +792,16 @@
       case PM_FPA_FILE_WEIGHT:
       case PM_FPA_FILE_FRINGE:
-        if (file->mode == PM_FPA_MODE_WRITE) {
-            // Want to write out any potential blank
-            // XXX why do we need to construct a new fpa??
-            pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config); // FPA, ensuring it's ready to write a blank
-
-            switch (file->fileLevel) {
-              case PM_FPA_LEVEL_FPA:
-                if (file->fpa->wrote_phu) break;
-                status = pmFPAWrite(fpa, file->fits, NULL, true, false);
-                file->fpa->wrote_phu = true;
-                break;
-              case PM_FPA_LEVEL_CHIP: {
-                  pmChip *chip = pmFPAviewThisChip(view, fpa);
-                  if (chip->wrote_phu) break;
-                  status = pmChipWrite(chip, file->fits, NULL, true, false);
-                  chip->wrote_phu = true;
-                  break;
-              }
-              case PM_FPA_LEVEL_CELL: {
-                  pmCell *cell = pmFPAviewThisCell(view, fpa);
-                  if (cell->wrote_phu) break;
-                  status = pmCellWrite(cell, file->fits, NULL, true);
-                  cell->wrote_phu = true;
-                  break;
-              }
-              default:
-                psAbort("fileLevel not correctly set");
-                break;
-            }
-
-            psFree(fpa);
-        }
+	status = pmFPAviewFitsWritePHU (view, file, config);
+	break;
+      case PM_FPA_FILE_CMF:
+	status = pmSource_CMF_WritePHU (view, file, config);
+	break;
+      case PM_FPA_FILE_PSF:
+	status = pmPSF_WritePHU (view, file, config);
+	break;
       case PM_FPA_FILE_SX:
       case PM_FPA_FILE_RAW:
       case PM_FPA_FILE_OBJ:
       case PM_FPA_FILE_CMP:
-      case PM_FPA_FILE_CMF:
-      case PM_FPA_FILE_PSF:
       case PM_FPA_FILE_JPEG:
       case PM_FPA_FILE_KAPA:
@@ -842,4 +815,6 @@
         return false;
     }
+    // XXX this is also being set in the individual functions.  choose one or the other
+    file->wrote_phu = true;
     return true;
 }
