Index: /branches/eam_branches/20100225/psModules/src/objects/pmPSF_IO.c
===================================================================
--- /branches/eam_branches/20100225/psModules/src/objects/pmPSF_IO.c	(revision 27262)
+++ /branches/eam_branches/20100225/psModules/src/objects/pmPSF_IO.c	(revision 27263)
@@ -470,19 +470,22 @@
         psMetadataAddS32 (header, PS_LIST_TAIL, "YCENTER", 0, "", psf->residuals->yCenter);
 
-        // write the residuals as three planes of the image
-        // this call creates an extension with NAXIS3 = 3
+        // write the residuals as planes of the image
+	psArray *images = psArrayAllocEmpty (1);
+	psArrayAdd (images, 1, psf->residuals->Ro);  // z = 0 is Ro
+
         if (psf->residuals->Rx) {
-            // this call creates an extension with NAXIS3 = 3
-            psArray *images = psArrayAllocEmpty (3);
-            psArrayAdd (images, 1, psf->residuals->Ro);
             psArrayAdd (images, 1, psf->residuals->Rx);
             psArrayAdd (images, 1, psf->residuals->Ry);
-
-            psFitsWriteImageCube (file->fits, header, images, residName);
-            psFree (images);
-        } else {
-            // this call creates an extension with NAXIS3 = 1
-            psFitsWriteImage(file->fits, header, psf->residuals->Ro, 0, residName);
-        }
+	}
+
+	// note that all N plane are implicitly of the same type, so we convert the mask
+	if (psf->residuals->mask) {
+	    psImage *mask = psImageCopy (NULL, psf->residuals->mask, psf->residuals->Ro->type.type);
+	    psArrayAdd (images, 1, mask);
+	    psFree (mask);
+	}
+
+	psFitsWriteImageCube (file->fits, header, images, residName);
+	psFree (images);
         psFree (residName);
         psFree (header);
@@ -937,10 +940,29 @@
         psRegion fullImage = {0, 0, 0, 0};
         psFitsReadImageBuffer(psf->residuals->Ro, file->fits, fullImage, 0); // Desired pixels
-        if (Nz > 1) {
-            assert (Nz == 3);
+
+	// note that all N plane are implicitly of the same type, so we convert the mask
+	psImage *mask = psImageCopy(NULL, psf->residuals->mask, psf->residuals->Ro->type.type);
+	psImageInit (psf->residuals->mask, 0);
+	psImageInit (psf->residuals->Rx, 0.0);
+	psImageInit (psf->residuals->Ry, 0.0);
+	switch (Nz) {
+	  case 1: // Ro only
+	    break;
+	  case 2: // Ro and mask
+            psFitsReadImageBuffer(mask, file->fits, fullImage, 1); // Desired pixels
+	    psImageCopy (psf->residuals->mask, mask, PM_TYPE_RESID_MASK);
+	    break;
+	  case 3: // Ro, Rx and Ry, no mask
             psFitsReadImageBuffer(psf->residuals->Rx, file->fits, fullImage, 1); // Desired pixels
             psFitsReadImageBuffer(psf->residuals->Ry, file->fits, fullImage, 2); // Desired pixels
-        }
-        // XXX notice that we are not saving the resid->mask
+	    break;
+	  case 4: // Ro, Rx, Ry, and mask:
+            psFitsReadImageBuffer(psf->residuals->Rx, file->fits, fullImage, 1); // Desired pixels
+            psFitsReadImageBuffer(psf->residuals->Ry, file->fits, fullImage, 2); // Desired pixels
+            psFitsReadImageBuffer(mask, file->fits, fullImage, 3); // Desired pixels
+	    psImageCopy (psf->residuals->mask, mask, PM_TYPE_RESID_MASK);
+	    break;
+        }
+	psFree (mask);
     }
 
