Index: trunk/psModules/src/camera/pmFPAfileIO.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileIO.c	(revision 15140)
+++ trunk/psModules/src/camera/pmFPAfileIO.c	(revision 15180)
@@ -359,11 +359,19 @@
             return false;
         }
+
+        // do we need to write out a PHU?
+        if (!pmFPAfileWritePHU(file, fileView, config)) {
+            psError(PS_ERR_IO, false, "failed to write phu for %s (%s)", file->filename, file->name);
+            return false;
+        }
+
         psFree (fileView);
     }
 
-    // do we need to write out a PHU?
-    if (level >= file->fileLevel) {
-        if (!pmFPAfileWritePHU (file, view, config)) {
-            psError(PS_ERR_IO, false, "failed to write phu for %s (%s)", file->filename, file->name);
+    if (file->compression) {
+        psTrace("psModules.camera", 7, "Setting compression for %s (%s)\n", file->filename, file->name);
+        if (!psFitsCompressionApply(file->fits, file->compression)) {
+            psError(PS_ERR_UNKNOWN, false, "Unable to set compression options for %s (%s) (%d:%d:%d)\n",
+                    file->filename, file->name, view->chip, view->cell, view->readout);
             return false;
         }
@@ -667,5 +675,6 @@
       case PM_FPA_FILE_CMF:
       case PM_FPA_FILE_PSF:
-        psTrace ("psModules.camera", 5, "opening %s (%s) (%d:%d:%d)\n", file->filename, file->name, view->chip, view->cell, view->readout);
+        psTrace ("psModules.camera", 5, "opening %s (%s) (%d:%d:%d)\n",
+                 file->filename, file->name, view->chip, view->cell, view->readout);
         file->fits = psFitsOpen (file->filename, mode);
         if (file->fits == NULL) {
@@ -679,5 +688,6 @@
             psMetadata *fileMenu = psMetadataLookupMetadata (NULL, file->format, "FILE");
             if (!fileMenu) {
-                psError (PS_ERR_IO, true, "FILE METADATA missing from camera format %s\n", file->formatName);
+                psError (PS_ERR_IO, true, "FILE METADATA missing from camera format %s\n",
+                         config->formatName);
                 return false;
             }
@@ -688,8 +698,18 @@
         }
 
-        // 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 (pmConfigConvertFilename).
+        if (file->compression) {
+            psTrace("psModules.camera", 7, "Setting compression for %s (%s)\n", file->filename, file->name);
+            if (!psFitsCompressionApply(file->fits, file->compression)) {
+                psError(PS_ERR_UNKNOWN, false, "Unable to set compression options for %s (%s) (%d:%d:%d)\n",
+                        file->filename, file->name, view->chip, view->cell, view->readout);
+                return false;
+            }
+        }
+
+        // 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 (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);
+            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);
             return false;
         }
@@ -758,5 +778,5 @@
             return false;
         }
-	file->formatName = psStringCopy(config->formatName);
+        file->formatName = psStringCopy(config->formatName);
 
     } else {
@@ -796,12 +816,12 @@
       case PM_FPA_FILE_WEIGHT:
       case PM_FPA_FILE_FRINGE:
-	status = pmFPAviewFitsWritePHU (view, file, config);
-	break;
+        status = pmFPAviewFitsWritePHU (view, file, config);
+        break;
       case PM_FPA_FILE_CMF:
-	status = pmSource_CMF_WritePHU (view, file, config);
-	break;
+        status = pmSource_CMF_WritePHU (view, file, config);
+        break;
       case PM_FPA_FILE_PSF:
-	status = pmPSFmodelWritePHU (view, file, config);
-	break;
+        status = pmPSFmodelWritePHU (view, file, config);
+        break;
       case PM_FPA_FILE_SX:
       case PM_FPA_FILE_RAW:
