Index: trunk/psLib/src/fits/psFitsImage.c
===================================================================
--- trunk/psLib/src/fits/psFitsImage.c	(revision 19383)
+++ trunk/psLib/src/fits/psFitsImage.c	(revision 19384)
@@ -7,6 +7,6 @@
  *  @author Robert DeSonia, MHPCC
  *
- *  @version $Revision: 1.39 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2008-09-05 07:51:09 $
+ *  @version $Revision: 1.40 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2008-09-05 08:08:33 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -892,7 +892,17 @@
 bool psFitsWriteImageCube(psFits *fits, psMetadata *header, const psArray *input, const char *extname)
 {
+    return psFitsWriteImageCubeWithMask(fits, header, input, NULL, 0, extname);
+}
+
+bool psFitsWriteImageCubeWithMask(psFits *fits, psMetadata *header, const psArray *input,
+                                  const psArray *masks, psMaskType maskVal, const char *extname)
+{
     PS_ASSERT_FITS_NON_NULL(fits, false);
     PS_ASSERT_FITS_WRITABLE(fits, false);
     PS_ASSERT_ARRAY_NON_NULL(input, false);
+    if (masks) {
+        PS_ASSERT_ARRAY_NON_NULL(masks, false);
+        PS_ASSERT_ARRAYS_SIZE_EQUAL(masks, input, false);
+    }
 
     if (input->n == 0) {
@@ -903,5 +913,6 @@
     if (input->n == 1) {
         // The problem reduces to one already solved
-        return psFitsWriteImage(fits, header, input->data[0], 1, extname);
+        return psFitsWriteImageWithMask(fits, header, input->data[0],
+                                        masks ? masks->data[0] : NULL, maskVal, 1, extname);
     }
 
@@ -930,5 +941,5 @@
         psMetadataAddS32(headerCopy, PS_LIST_HEAD, "NAXIS3", PS_META_REPLACE, "Number of image planes",
                          input->n);
-    if (! update) {
+    if (!update) {
         psError(PS_ERR_UNKNOWN, false, _("Failed to add metadata item, %s."),
                 "NAXIS, NAXIS1, NAXIS2, NAXIS3");
@@ -940,5 +951,6 @@
     // The first is an psFitsImageWrite to create the extension.
     // The next are psFitsImageUpdate to write into the extension.
-    if (! psFitsWriteImage(fits, headerCopy, input->data[0], input->n, extname)) {
+    if (!psFitsWriteImageWithMask(fits, headerCopy, input->data[0],
+                                  masks ? masks->data[0] : NULL, maskVal, input->n, extname)) {
         psError(PS_ERR_UNKNOWN, false, _("Could not write image plane %d."), 0);
         psFree(headerCopy);
@@ -948,5 +960,6 @@
 
     for (int i = 1; i < input->n; i++) {
-        if (! psFitsUpdateImage(fits, input->data[i], 0, 0, i)) {
+        if (!psFitsUpdateImageWithMask(fits, input->data[i],
+                                       masks ? masks->data[i] : NULL, maskVal, 0, 0, i)) {
             psError(PS_ERR_UNKNOWN, false, _("Could not write image plane %d."), i);
             return false;
@@ -958,4 +971,10 @@
 
 bool psFitsUpdateImageCube(psFits *fits, const psArray *input, int x0, int y0)
+{
+    return psFitsUpdateImageCubeWithMask(fits, input, NULL, 0, x0, y0);
+}
+
+bool psFitsUpdateImageCubeWithMask(psFits *fits, const psArray *input,
+                                   const psArray *masks, psMaskType maskVal, int x0, int y0)
 {
     PS_ASSERT_FITS_NON_NULL(fits, false);
@@ -969,5 +988,6 @@
 
     for (int i = 0; i < input->n; i++) {
-        if (! psFitsUpdateImage(fits, input->data[i], x0, y0, i)) {
+        if (!psFitsUpdateImageWithMask(fits, input->data[i],
+                                       masks ? masks->data[i] : NULL, maskVal, x0, y0, i)) {
             psError(PS_ERR_UNKNOWN, false, _("Could not update image plane %d."), i);
             return false;
