Index: /branches/pap_branch_070920/psModules/src/camera/pmFPAfile.c
===================================================================
--- /branches/pap_branch_070920/psModules/src/camera/pmFPAfile.c	(revision 15111)
+++ /branches/pap_branch_070920/psModules/src/camera/pmFPAfile.c	(revision 15112)
@@ -40,4 +40,5 @@
         psFitsClose (file->fits);
     }
+    psFree(file->compression);
 
     psFree (file->filerule);
@@ -72,4 +73,6 @@
     file->fpa = NULL;
     file->fits = NULL;
+    file->compression = NULL;
+    file->bitpix = 0;
     file->names = psMetadataAlloc();
 
Index: /branches/pap_branch_070920/psModules/src/camera/pmFPAfile.h
===================================================================
--- /branches/pap_branch_070920/psModules/src/camera/pmFPAfile.h	(revision 15111)
+++ /branches/pap_branch_070920/psModules/src/camera/pmFPAfile.h	(revision 15112)
@@ -4,6 +4,6 @@
  * @author EAM, IfA
  *
- * @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-09-19 21:37:58 $
+ * @version $Revision: 1.21.2.1 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-09-29 22:01:01 $
  * Copyright 2004-2005 Institute for Astronomy, University of Hawaii
  */
@@ -51,6 +51,5 @@
 } pmFPAfileState;
 
-typedef struct
-{
+typedef struct {
     pmFPAfileMode mode;                 // is this file read, written, or only used internally?
     pmFPAfileType type;                 // what type of data is read from / written to disk?
@@ -63,5 +62,7 @@
 
     pmFPA *fpa;                         // for I/O files, we carry a pointer to the complete fpa
-    psFits *fits;                       // for I/O files of fits type (IMAGE, CMP, CMF), we carry a file handle
+    psFits *fits;                       // for I/O files of fits type (IMAGE, CMP, CMF) we carry a file handle
+    psFitsCompression *compression;     // Compression for FITS images
+    int bitpix;                         // Bits per pixel for output
 
     bool wrote_phu;                     // have we written a PHU for this file?
@@ -93,6 +94,5 @@
     psMetadata *format;                 // Camera format
     psString formatName;                // name of the camera format
-}
-pmFPAfile;
+} pmFPAfile;
 
 // allocate an empty pmFPAfile structure
Index: /branches/pap_branch_070920/psModules/src/camera/pmFPAfileDefine.c
===================================================================
--- /branches/pap_branch_070920/psModules/src/camera/pmFPAfileDefine.c	(revision 15111)
+++ /branches/pap_branch_070920/psModules/src/camera/pmFPAfileDefine.c	(revision 15112)
@@ -78,4 +78,24 @@
 }
 
+// Parse an option from a metadata, returning the appropriate integer value
+static int parseOptionInt(const psMetadata *md, // Metadata containing the option
+                          const char *name, // Option name
+                          const char *source, // Description of source, for warning messages
+                          int defaultValue // Default value
+                          )
+{
+    psMetadataItem *item = psMetadataLookup(md, name); // Item with the value of interest
+    if (!item) {
+        psWarning("Unable to find value for %s in %s --- set to %d.", name, source, defaultValue);
+        return defaultValue;
+    }
+    int value = psMetadataItemParseS32(item); // Value of interst
+    if (value < 0) {
+        psWarning("Bad value for %s in %s (%d) --- set to %d.", name, source, value, defaultValue);
+        return defaultValue;
+    }
+    return value;
+}
+
 
 // define an input-type pmFPAfile, bind to the optional fpa if supplied
@@ -250,4 +270,42 @@
         file->fpa = pmFPAConstruct(file->camera);
     }
+
+    // Get compression scheme
+    const char *compName = psMetadataLookupStr(&status, data, "COMPRESSION"); // Name of compression scheme
+    if (compName && strcasecmp(compName, "NONE") != 0) {
+        psMetadata *compSchemes = psMetadataLookupMetadata(&status, camera, "COMPRESSION"); // Comp. schemes
+        if (!compSchemes) {
+            psWarning("Unable to find COMPRESSION in camera configuration --- compression disabled.");
+            goto COMPRESSION_DONE;
+        }
+        psMetadata *compression = psMetadataLookupMetadata(NULL, compSchemes, compName); // Compression info
+        if (!compression) {
+            psWarning("Unable to find %s in COMPRESSION in camera configuration --- compression disabled.",
+                      compName);
+            goto COMPRESSION_DONE;
+        }
+        const char *typeString = psMetadataLookupStr(NULL, compression, "COMPRESSION"); // Compression type
+        if (!typeString || strlen(typeString)) {
+            psWarning("Can't find COMPRESSION in compression scheme %s --- compression disabled.", compName);
+            goto COMPRESSION_DONE;
+        }
+        psFitsCompressionType type = psFitsCompressionTypeFromString(typeString); // Compression type enum
+
+        psString source = NULL;         // Source of options
+        psStringAppend(&source, "%s in COMPRESSION in camera %s", compName, cameraName);
+        psVector *tile = psVectorAlloc(3, PS_TYPE_S32); // Tile sizes
+        file->bitpix = parseOptionInt(compression, "BITPIX", source, 0); // Bits per pixel
+        tile->data.S32[0] = parseOptionInt(compression, "TILE.X", source, 0); // Tiling in x
+        tile->data.S32[1] = parseOptionInt(compression, "TILE.Y", source, 1); // Tiling in y
+        tile->data.S32[2] = parseOptionInt(compression, "TILE.Z", source, 1); // Tiling in z
+        int noise = parseOptionInt(compression, "NOISE", source, 16); // Noise bits
+        int hscale = parseOptionInt(compression, "HSCALE", source, 0); // Scaling for HCOMPRESS
+        int hsmooth = parseOptionInt(compression, "HSMOOTH", source, 0); // Smoothing for HCOMPRESS
+        psFree(source);
+
+        file->compression = psFitsCompressionAlloc(type, tile, noise, hscale, hsmooth);
+        psFree(tile);
+    }
+COMPRESSION_DONE:
 
     file->fileLevel = pmFPAPHULevel(format);
Index: /branches/pap_branch_070920/psModules/src/camera/pmFPAfileIO.c
===================================================================
--- /branches/pap_branch_070920/psModules/src/camera/pmFPAfileIO.c	(revision 15111)
+++ /branches/pap_branch_070920/psModules/src/camera/pmFPAfileIO.c	(revision 15112)
@@ -688,8 +688,15 @@
         }
 
-        // 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 && !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 +765,5 @@
             return false;
         }
-	file->formatName = psStringCopy(config->formatName);
+        file->formatName = psStringCopy(config->formatName);
 
     } else {
@@ -795,12 +802,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:
