Index: /trunk/psModules/src/camera/pmFPAConstruct.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAConstruct.c	(revision 11800)
+++ /trunk/psModules/src/camera/pmFPAConstruct.c	(revision 11801)
@@ -469,4 +469,5 @@
 // It returns a view corresponding to the PHU
 static pmFPAview *addSource(pmFPA *fpa,       // The FPA
+                            const char *fpaname, // The desired FPA name
                             const pmFPAview *phuView, // The view corresponding to the PHU, or NULL
                             const psMetadata *header, // The PHU header, or NULL
@@ -479,5 +480,20 @@
     assert(format);
 
-    bool mdok = true;                   // Status from metadata lookups
+    bool mdok;                          // Status of MD lookup
+
+    // If FPA.NAME is already defined, new name must match it; otherwise, warn the user that something
+    // potentially bad is happening.
+    if (fpaname && install) {
+        const char *currentName = psMetadataLookupStr(&mdok, fpa->concepts, "FPA.NAME"); // Current name
+        if (mdok && currentName && strlen(currentName) > 0 && strcmp(currentName, fpaname) != 0) {
+            psLogMsg(__func__, PS_LOG_WARN, "FPA.NAME for new source (%s) doesn't match FPA.NAME for current "
+                     "fpa (%s).\n", fpaname, currentName);
+        }
+        psMetadataAddStr(fpa->concepts, PS_LIST_HEAD, "FPA.NAME", PS_META_REPLACE, "Name of FPA", fpaname);
+    } else if (!psMetadataLookup(fpa->concepts, "FPA.NAME")) {
+        // Make sure there is an FPA.NAME
+        psMetadataAddStr(fpa->concepts, PS_LIST_HEAD, "FPA.NAME", 0, "Name of FPA", "UNKNOWN");
+    }
+
     psMetadata *fileInfo = psMetadataLookupMetadata(&mdok, format, "FILE"); // The file information
     if (!mdok || !fileInfo) {
@@ -759,5 +775,6 @@
 
 
-bool pmFPAAddSourceFromView(pmFPA *fpa, const pmFPAview *phuView, const psMetadata *format)
+bool pmFPAAddSourceFromView(pmFPA *fpa, const char *fpaname, const pmFPAview *phuView,
+                            const psMetadata *format)
 {
     PS_ASSERT_PTR_NON_NULL(fpa, false);
@@ -765,5 +782,5 @@
     PS_ASSERT_PTR_NON_NULL(format, false);
 
-    pmFPAview *view = addSource(fpa, phuView, NULL, format, true);
+    pmFPAview *view = addSource(fpa, fpaname, phuView, NULL, format, true);
     bool status = (view != NULL);
     psFree(view);
@@ -785,21 +802,14 @@
 
     // Check the name of the FPA
-    psString newFPAname = phuNameFromHeader("FPA.NAME", fileInfo, phu); // New name for the FPA
-    if (!newFPAname) {
-        psError(PS_ERR_IO, true, "Unable to determine FPA.NAME : check for FPA.NAME in FILE in FORMAT");
-        return NULL;
-    }
-
-    // If FPAname already defined, new name must match it; otherwise, warn the user that something potentially
-    // bad is happening.
-    const char *currentFPAname = psMetadataLookupStr(&mdok, fpa->concepts, "FPA.NAME"); // Current name
-    if (mdok && currentFPAname && strlen(currentFPAname) > 0 && strcmp(currentFPAname, newFPAname) != 0) {
-        psLogMsg(__func__, PS_LOG_WARN, "FPA.NAME for new source (%s) doesn't match FPA.NAME for current "
-                 "fpa (%s).\n", newFPAname, currentFPAname);
-    }
-    psMetadataAddStr(fpa->concepts, PS_LIST_HEAD, "FPA.NAME", PS_META_REPLACE, "Name of FPA", newFPAname);
-    psFree(newFPAname);                 // Drop reference
-
-    return addSource(fpa, NULL, phu, format, true);
+    psString fpaname = phuNameFromHeader("FPA.NAME", fileInfo, phu); // New name for the FPA
+    if (!fpaname || strlen(fpaname) == 0) {
+        psError(PS_ERR_IO, true, "Unable to determine FPA.NAME: check for FPA.NAME in FILE in camera format");
+        return NULL;
+    }
+
+    pmFPAview *view = addSource(fpa, fpaname, NULL, phu, format, true); // View of PHU, to return
+    psFree(fpaname);
+
+    return view;
 }
 
@@ -811,5 +821,5 @@
     PS_ASSERT_PTR_NON_NULL(format, NULL);
 
-    return addSource(fpa, NULL, phu, format, false);
+    return addSource(fpa, NULL, NULL, phu, format, false);
 }
 
Index: /trunk/psModules/src/camera/pmFPAConstruct.h
===================================================================
--- /trunk/psModules/src/camera/pmFPAConstruct.h	(revision 11800)
+++ /trunk/psModules/src/camera/pmFPAConstruct.h	(revision 11801)
@@ -4,6 +4,6 @@
  * @author Paul Price, IfA
  *
- * @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-01-24 03:11:19 $
+ * @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-02-15 00:51:20 $
  * Copyright 2005-2006 Institute for Astronomy, University of Hawaii
  */
@@ -33,4 +33,5 @@
 /// configuration is required in order to describe how the FPA is laid out in terms of disk files.
 bool pmFPAAddSourceFromView(pmFPA *fpa,   ///< The FPA
+                            const char *fpaname, ///< FPA.NAME for the source
                             const pmFPAview *phuView, ///< The view, corresponding to the PHU
                             const psMetadata *format ///< Format of file
Index: /trunk/psModules/src/camera/pmFPAfileDefine.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 11800)
+++ /trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 11801)
@@ -180,37 +180,26 @@
     }
 
-    file->camera = psMemIncrRefCounter(config->camera);
+    psMetadata *camera;                 // Camera to use
+    if (fpa && fpa->camera) {
+        camera = (psMetadata*)fpa->camera; // Casting away const, so I can put it in the file
+    } else {
+        camera = config->camera;
+    }
+    file->camera = psMemIncrRefCounter(camera);
+
+    // select the format list from the selected camera
     const char *formatName = psMetadataLookupStr (&status, data, "FILE.FORMAT");
-    psMetadata *format = NULL;          // Camera format to use
-    if (formatName && strcasecmp(formatName, "NONE") != 0) {
-        // select the format list from the selected camera
-        psMetadata *formats = psMetadataLookupMetadata(&status, file->camera, "FORMATS"); // List of formats
-
-        // select the desired format
-        format = psMetadataLookupMetadata(&status, formats, formatName);
-        if (!format) {
-            psError(PS_ERR_IO, false, "format %s for %s not defined", formatName, name);
-            psFree(file);
-            return NULL;
-        }
-        file->format = psMemIncrRefCounter(format);
-        #if 0
-
-        file->src = psMemIncrRefCounter(fpa);
-        file->fpa = pmFPAConstruct(file->camera);
-        #endif
-
-    } else {
+    if (!formatName) {
+        formatName = config->formatName;
+    }
+    psMetadata *formats = psMetadataLookupMetadata(&status, file->camera, "FORMATS"); // List of formats
+    psMetadata *format = psMetadataLookupMetadata(&status, formats, formatName); // Camera format to use
+    if (!format) {
+        // Try to get by with the default
         format = config->format;
-        #if 0
-
-        file->fpa = psMemIncrRefCounter(fpa);
-        #endif
-
-    }
-    #if 1
+    }
+    file->format = psMemIncrRefCounter(format);
+
     file->fpa = psMemIncrRefCounter(fpa);
-    #endif
-
 
     file->fileLevel = pmFPAPHULevel(format);
@@ -925,7 +914,4 @@
     file->xBin = xBin;
     file->yBin = yBin;
-    psFree (file->format);
-    file->format = psMemIncrRefCounter(format);
-
     psFree (fpa);
     return file;
Index: /trunk/psModules/src/camera/pmFPAfileFitsIO.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAfileFitsIO.c	(revision 11800)
+++ /trunk/psModules/src/camera/pmFPAfileFitsIO.c	(revision 11801)
@@ -23,5 +23,5 @@
     PS_ASSERT_PTR_NON_NULL(view, NULL);
 
-    if (file->camera == config->camera || // Need to mosaic, not copy
+    if (//file->camera == config->camera || // Need to mosaic, not copy
             !file->format) {                // Working with the same output format as input format
         return psMemIncrRefCounter(file->fpa);
@@ -50,5 +50,12 @@
     }
 
-    if (!pmFPAAddSourceFromView(fpa, phuView, file->format)) {
+    pmFPA *nameSource = file->src; // Source of FPA.NAME
+    if (!nameSource) {
+        nameSource = file->fpa;
+    }
+    bool mdok;                  // Status of MD lookup
+    const char *fpaname = psMetadataLookupStr(&mdok, nameSource->concepts, "FPA.NAME"); // Name of FPA
+
+    if (!pmFPAAddSourceFromView(fpa, fpaname, phuView, file->format)) {
         psError(PS_ERR_UNKNOWN, false, "Unable to insert HDU into FPA for writing.\n");
         psFree(fpa);
Index: /trunk/psModules/src/camera/pmFPAfileIO.c
===================================================================
--- /trunk/psModules/src/camera/pmFPAfileIO.c	(revision 11800)
+++ /trunk/psModules/src/camera/pmFPAfileIO.c	(revision 11801)
@@ -107,4 +107,6 @@
         }
     }
+#if 0
+    // This should be done by pmFPAfileBlank
     if (file->mode == PM_FPA_MODE_WRITE) {
         // Want to write out any potential blank
@@ -132,5 +134,5 @@
         psFree(fpa);
     }
-
+#endif
     return true;
 
@@ -475,4 +477,93 @@
 }
 
+bool pmFPAfileBlank(pmFPAfile *file, const pmFPAview *view, pmConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(file, false);
+    PS_ASSERT_PTR_NON_NULL(view, false);
+
+    if (file->state & PM_FPA_STATE_INACTIVE) {
+        psTrace("psModules.camera", 6, "skip blank for %s, files is inactive", file->name);
+        return true;
+    }
+
+    if (file->mode != PM_FPA_MODE_WRITE) {
+        psTrace("psModules.camera", 6, "skip blank for %s, mode is not WRITE", file->name);
+        return true;
+    }
+
+    // an internal file should not be returned to here
+    if (file->mode == PM_FPA_MODE_INTERNAL) {
+        psError(PS_ERR_IO, true, "File is mode PM_FPA_MODE_INTERNAL");
+        return false;
+    }
+
+    if (!file->save) {
+        psTrace("psModules.camera", 6, "skip blank for %s, save is FALSE", file->name);
+        return true;
+    }
+
+    // get the current level
+    pmFPALevel level = pmFPAviewLevel (view);
+
+    if (level != file->fileLevel) {
+        psTrace("psModules.camera", 6, "skip blank for %s, level is %s", file->name,
+                pmFPALevelToName(file->freeLevel));
+        return true;
+    }
+
+    // do we need to open this file?
+    if (!pmFPAfileOpen (file, view, config)) {
+        psError(PS_ERR_IO, false, "failed to open %s", file->filename);
+        return false;
+    }
+
+    switch (file->type) {
+    case PM_FPA_FILE_IMAGE:
+    case PM_FPA_FILE_MASK:
+    case PM_FPA_FILE_WEIGHT:
+    case PM_FPA_FILE_FRINGE:
+    if (file->mode == PM_FPA_MODE_WRITE) {
+        // Want to write out any potential blank
+        pmFPA *fpa = pmFPAfileSuitableFPA(file, view, config); // FPA, ensuring it's ready to write a blank
+
+        switch (file->fileLevel) {
+        case PM_FPA_LEVEL_FPA:
+            pmFPAWrite(fpa, file->fits, NULL, true, false);
+            break;
+        case PM_FPA_LEVEL_CHIP: {
+                pmChip *chip = pmFPAviewThisChip(view, fpa);
+                pmChipWrite(chip, file->fits, NULL, true, false);
+                break;
+            }
+        case PM_FPA_LEVEL_CELL: {
+                pmCell *cell = pmFPAviewThisCell(view, fpa);
+                pmCellWrite(cell, file->fits, NULL, true);
+                break;
+            }
+        default:
+            psAbort("fileLevel not correctly set");
+            break;
+        }
+
+        psFree(fpa);
+    }
+    case PM_FPA_FILE_SX:
+    case PM_FPA_FILE_RAW:
+    case PM_FPA_FILE_OBJ:
+    case PM_FPA_FILE_CMP:
+    case PM_FPA_FILE_CMF:
+    case PM_FPA_FILE_PSF:
+    case PM_FPA_FILE_JPEG:
+    case PM_FPA_FILE_MANAPLOT:
+    case PM_FPA_FILE_KAPA:
+      break;
+    default:
+        fprintf (stderr, "warning: type mismatch\n");
+        return false;
+    }
+    return true;
+}
+
+
 bool pmFPAfileWrite(pmFPAfile *file, const pmFPAview *view, pmConfig *config)
 {
@@ -630,28 +721,20 @@
     case PM_FPA_FILE_WEIGHT:
     case PM_FPA_FILE_FRINGE: {
-	// create FPA structure component based on view
-	// XXX drop this ifdef'ed out code??
-#if 0
-	psMetadata *format = file->format; // Camera format configuration
-	if (!format) {
-	    // It's likely a mosaic, for which we don't yet know the appropriate format
-	    const psMetadata *camera = file->fpa->camera; // Camera configuration
-	    psMetadata *formats = psMetadataLookupMetadata(NULL, camera, "FORMATS"); // The FORMATS
-	    if (!formats) {
-		psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find FORMATS in camera configuration.\n");
-		return false;
-	    }
-	    format = psMetadataLookupMetadata(NULL, formats, config->formatName);
-	    if (!format) {
-		psError(PS_ERR_UNEXPECTED_NULL, false,
-			"Unable to find format %s in camera configuration.\n", config->formatName);
-		return false;
-	    }
-	    file->format = psMemIncrRefCounter(format);
-	}
-#endif
-	pmFPAAddSourceFromView (file->fpa, view, file->format);
-	psTrace ("pmFPAfile", 5, "created fpa data elements for %s (fpa: %p)\n", file->name, file->fpa);
-	break;
+            // create FPA structure component based on view
+            psMetadata *format = file->format; // Camera format configuration
+            if (!format) {
+                format = config->format;
+            }
+
+            pmFPA *nameSource = file->src; // Source of FPA.NAME
+            if (!nameSource) {
+                nameSource = file->fpa;
+            }
+            bool mdok;                  // Status of MD lookup
+            const char *fpaname = psMetadataLookupStr(&mdok, nameSource->concepts, "FPA.NAME"); // Name of FPA
+
+            pmFPAAddSourceFromView(file->fpa, fpaname, view, format);
+            psTrace ("pmFPAfile", 5, "created fpa data elements for %s (fpa: %p)\n", file->name, file->fpa);
+            break;
     }
     case PM_FPA_FILE_HEADER:
@@ -807,4 +890,8 @@
                 goto failure;
             }
+            if (!pmFPAfileBlank(file, view, config)) {
+                psError(PS_ERR_IO, false, "failed BLANK in FPA_BEFORE block for %s", file->name);
+                goto failure;
+            }
             break;
         case PM_FPA_AFTER:
Index: /trunk/psModules/src/camera/pmFPAfileIO.h
===================================================================
--- /trunk/psModules/src/camera/pmFPAfileIO.h	(revision 11800)
+++ /trunk/psModules/src/camera/pmFPAfileIO.h	(revision 11801)
@@ -4,6 +4,6 @@
  * @author EAM, IfA
  *
- * @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-01-24 02:54:14 $
+ * @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-02-15 00:51:20 $
  * Copyright 2004-2005 Institute for Astronomy, University of Hawaii
  */
@@ -21,5 +21,5 @@
 bool pmFPAfileRead (pmFPAfile *file, const pmFPAview *view, pmConfig *config);
 
-bool pmFPAfileCreate (pmFPAfile *file, const pmFPAview *view, pmConfig *config);
+bool pmFPAfileCreate (pmFPAfile *file, const pmFPAview *view, const pmConfig *config);
 
 // write to the real file corresponding to the given pmFPAfile for the current view
