Index: trunk/psModules/src/objects/pmPSF_IO.c
===================================================================
--- trunk/psModules/src/objects/pmPSF_IO.c	(revision 27178)
+++ trunk/psModules/src/objects/pmPSF_IO.c	(revision 27531)
@@ -491,30 +491,31 @@
         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);
-
-            if (!psFitsWriteImageCube (file->fits, header, images, residName)) {
-                psError(psErrorCodeLast(), false, "Unable to write PSF residuals.");
-                psFree(images);
-                psFree(residName);
-                psFree(header);
-                return false;
-            }
-            psFree (images);
-        } else {
-            // this call creates an extension with NAXIS3 = 1
-            if (!psFitsWriteImage(file->fits, header, psf->residuals->Ro, 0, residName)) {
-                psError(psErrorCodeLast(), false, "Unable to write PSF residuals.");
-                psFree(residName);
-                psFree(header);
-                return false;
-            }
-        }
+	}
+
+	// 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);
+
+	if (!psFitsWriteImageCube (file->fits, header, images, residName)) {
+            psError(psErrorCodeLast(), false, "Unable to write PSF residuals.");
+            psFree(images);
+            psFree(residName);
+            psFree(header);
+            return false;
+        }
+        psFree (images);
         psFree (residName);
         psFree (header);
@@ -1017,6 +1018,21 @@
             return false;
         }
-        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
+            if (!psFitsReadImageBuffer(mask, file->fits, fullImage, 1)) {
+                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
+                return false;
+            }
+	    psImageCopy (psf->residuals->mask, mask, PM_TYPE_RESID_MASK);
+	    break;
+	  case 3: // Ro, Rx and Ry, no mask
             if (!psFitsReadImageBuffer(psf->residuals->Rx, file->fits, fullImage, 1)) {
                 psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
@@ -1027,6 +1043,22 @@
                 return false;
             }
-        }
-        // XXX notice that we are not saving the resid->mask
+	    break;
+	  case 4: // Ro, Rx, Ry, and mask:
+            if (!psFitsReadImageBuffer(psf->residuals->Rx, file->fits, fullImage, 1)) {
+                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
+                return false;
+            }
+            if (!psFitsReadImageBuffer(psf->residuals->Ry, file->fits, fullImage, 2)) {
+                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
+                return false;
+            }
+            if (!psFitsReadImageBuffer(mask, file->fits, fullImage, 3)) {
+                psError(psErrorCodeLast(), false, "Unable to read PSF residual image.");
+                return false;
+            }
+	    psImageCopy (psf->residuals->mask, mask, PM_TYPE_RESID_MASK);
+	    break;
+        }
+	psFree (mask);
     }
 
