Index: trunk/psModules/src/camera/pmFPAfileFitsIO.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileFitsIO.c	(revision 7723)
+++ trunk/psModules/src/camera/pmFPAfileFitsIO.c	(revision 7726)
@@ -73,5 +73,9 @@
 
 // given an already-opened fits file, write the components corresponding
-// to the specified view
+// to the specified view. when the file was opened, pmFPA/Chip/CellWrite was
+// called on it with blank=true to write the (possible) blank PHU
+// do NOT call the functions below with blank=true or they will write
+// out data in an inconsistent fashion
+// the calls below should recurse down the element to write out all components.
 bool pmFPAviewWriteFitsImage (const pmFPAview *view, pmFPAfile *file)
 {
@@ -84,5 +88,5 @@
     // pmFPAWrite takes care of all PHUs as needed
     if (view->chip == -1) {
-        pmFPAWrite(fpa, fits, NULL, true, true);
+        pmFPAWrite(fpa, fits, NULL, false, true);
         return true;
     }
@@ -94,10 +98,46 @@
     pmChip *chip = fpa->chips->data[view->chip];
 
-
+    if (view->cell == -1) {
+        pmChipWrite (chip, fits, NULL, false, true);
+        return true;
+    }
+
+    if (view->cell >= chip->cells->n) {
+        psError(PS_ERR_IO, true, "Requested cell == %d >= chip->cells->n == %d", view->cell, chip->cells->n);
+        return false;
+    }
+    pmCell *cell = chip->cells->data[view->cell];
+
+    if (view->readout == -1) {
+        return pmCellWrite (cell, fits, NULL, false);
+    }
+    psError(PS_ERR_UNKNOWN, true, "Returning false");
+    return false;
+
+    // XXX disable readout write for now
     #if 0
-    // XXX This no longer required since we write the PHU at open.
-
-    // do we need to write out a PHU for this entry?
-    if (file->phu == NULL) {
+
+    if (view->readout >= cell->readouts->n) {
+        psError(PS_ERR_IO, true, "Requested readout == %d >= cell->readouts->n == %d",
+                view->readout, cell->readouts->n);
+        return false;
+    }
+    pmReadout *readout = cell->readouts->data[view->readout];
+
+    if (view->nRows == 0) {
+        pmReadoutWrite (readout, fits, NULL, NULL);
+    } else {
+        pmReadoutWriteSegment (readout, fits, view->nRows, view->iRows, NULL, NULL);
+    }
+    return true;
+    #endif
+}
+
+// this old code was used to write the blank by hand.
+// pmFPAWrite now takes care of this choice.
+# if 0
+// do we need to write out a PHU for this entry?
+if (file->phu == NULL)
+    {
         pmHDU *hdu = pmFPAviewThisHDU (view, file->fpa);
         pmHDU *phu = pmFPAviewThisPHU (view, file->fpa);
@@ -119,41 +159,5 @@
         }
     }
-    #endif
-
-    if (view->cell == -1) {
-        pmChipWrite (chip, fits, NULL, true, true);
-        return true;
-    }
-
-    if (view->cell >= chip->cells->n) {
-        psError(PS_ERR_IO, true, "Requested cell == %d >= chip->cells->n == %d", view->cell, chip->cells->n);
-        return false;
-    }
-    pmCell *cell = chip->cells->data[view->cell];
-
-    if (view->readout == -1) {
-        return pmCellWrite (cell, fits, NULL, true);
-    }
-    psError(PS_ERR_UNKNOWN, true, "Returning false");
-    return false;
-
-    // XXX disable readout write for now
-    #if 0
-
-    if (view->readout >= cell->readouts->n) {
-        psError(PS_ERR_IO, true, "Requested readout == %d >= cell->readouts->n == %d",
-                view->readout, cell->readouts->n);
-        return false;
-    }
-    pmReadout *readout = cell->readouts->data[view->readout];
-
-    if (view->nRows == 0) {
-        pmReadoutWrite (readout, fits, NULL, NULL);
-    } else {
-        pmReadoutWriteSegment (readout, fits, view->nRows, view->iRows, NULL, NULL);
-    }
-    return true;
-    #endif
-}
+# endif
 
 // given an already-opened fits file, read the components corresponding
Index: trunk/psModules/src/camera/pmFPAfileIO.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileIO.c	(revision 7723)
+++ trunk/psModules/src/camera/pmFPAfileIO.c	(revision 7726)
@@ -98,5 +98,4 @@
         // open the FITS types:
     case PM_FPA_FILE_IMAGE:
-    case PM_FPA_FILE_CMF:
         psTrace ("pmFPAfile", 5, "opening %s (type: %d)\n", file->filename, file->type);
         file->fits = psFitsOpen (file->filename, mode);
@@ -107,5 +106,5 @@
         file->state = PM_FPA_STATE_OPEN;
 
-        // In some cases, I need to call pmFPAAddSourceFromHeader after I've opened the file
+        // In some cases, I need to call pmFPAAddSourceFromHeader after I've opened the file,
         // specifically if I have not called this function on startup.  This happens for the
         // images supplied by the detrend database, which are only identified here (above).
@@ -160,18 +159,18 @@
         }
         if (file->mode == PM_FPA_MODE_WRITE) {
-            pmChip *chip;
-            pmCell *cell;
             switch (file->fileLevel) {
             case PM_FPA_LEVEL_FPA:
-                pmFPAWrite (file->fpa, file->fits, NULL, false, false);
+                pmFPAWrite (file->fpa, file->fits, NULL, true, false);
                 break;
-            case PM_FPA_LEVEL_CHIP:
-                chip = pmFPAviewThisChip(view, file->fpa);
-                pmChipWrite (chip, file->fits, NULL, false, false);
-                break;
-            case PM_FPA_LEVEL_CELL:
-                cell = pmFPAviewThisCell(view, file->fpa);
-                pmCellWrite(cell, file->fits, NULL, false);
-                break;
+            case PM_FPA_LEVEL_CHIP: {
+                    pmChip *chip = pmFPAviewThisChip(view, file->fpa);
+                    pmChipWrite (chip, file->fits, NULL, true, false);
+                    break;
+                }
+            case PM_FPA_LEVEL_CELL: {
+                    pmCell *cell = pmFPAviewThisCell(view, file->fpa);
+                    pmCellWrite(cell, file->fits, NULL, true);
+                    break;
+                }
             default:
                 psAbort ("pmFPAfileIO", "fileLevel not correctly set");
@@ -179,4 +178,15 @@
             }
         }
+        break;
+
+        // open the FITS object files
+    case PM_FPA_FILE_CMF:
+        psTrace ("pmFPAfile", 5, "opening %s (type: %d)\n", file->filename, file->type);
+        file->fits = psFitsOpen (file->filename, mode);
+        if (file->fits == NULL) {
+            psError(PS_ERR_IO, false, "error opening file %s\n", file->filename);
+            return false;
+        }
+        file->state = PM_FPA_STATE_OPEN;
         break;
 
