Index: trunk/psModules/src/camera/pmFPA.h
===================================================================
--- trunk/psModules/src/camera/pmFPA.h	(revision 14886)
+++ trunk/psModules/src/camera/pmFPA.h	(revision 14889)
@@ -6,6 +6,6 @@
  * @author Eugene Magnier, IfA
  *
- * @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-08-23 23:42:41 $
+ * @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-09-19 21:37:58 $
  * Copyright 2005-2006 Institute for Astronomy, University of Hawaii
  */
@@ -55,4 +55,5 @@
     psMetadata *analysis;               ///< FPA-level analysis metadata
     const psMetadata *camera;           ///< Camera configuration
+    const char *formatName;             ///< Name of camera format
     psArray *chips;                     ///< The component chips
     pmHDU *hdu;                         ///< FITS header data unit of interest, or NULL
Index: trunk/psModules/src/camera/pmFPAfile.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfile.c	(revision 14886)
+++ trunk/psModules/src/camera/pmFPAfile.c	(revision 14889)
@@ -27,8 +27,10 @@
     psTrace ("pmFPAfileFree", 5, "freeing %s\n", file->name);
     psFree (file->fpa);
+    psFree (file->src);
     psFree (file->readout);
     psFree (file->names);
 
     psFree (file->camera);
+    psFree (file->cameraName);
     psFree (file->format);
     psFree (file->formatName);
@@ -73,4 +75,5 @@
 
     file->camera = NULL;
+    file->cameraName = NULL;
     file->format = NULL;
     file->formatName = NULL;
@@ -175,8 +178,8 @@
 
     if (strstr (newName, "{FPA.NAME}") != NULL) {
-	char *name = psMetadataLookupStr (NULL, fpa->concepts, "FPA.NAME");
-	if (name != NULL) {
-	    psStringSubstitute(&newName, "fpa", "{FPA.NAME}");
-	}
+        char *name = psMetadataLookupStr (NULL, fpa->concepts, "FPA.NAME");
+        if (name != NULL) {
+            psStringSubstitute(&newName, "fpa", "{FPA.NAME}");
+        }
     }
     if (strstr (newName, "{CHIP.NAME}") != NULL) {
Index: trunk/psModules/src/camera/pmFPAfile.h
===================================================================
--- trunk/psModules/src/camera/pmFPAfile.h	(revision 14886)
+++ trunk/psModules/src/camera/pmFPAfile.h	(revision 14889)
@@ -4,6 +4,6 @@
  * @author EAM, IfA
  *
- * @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-08-23 23:42:41 $
+ * @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-09-19 21:37:58 $
  * Copyright 2004-2005 Institute for Astronomy, University of Hawaii
  */
@@ -65,5 +65,5 @@
     psFits *fits;                       // for I/O files of fits type (IMAGE, CMP, CMF), we carry a file handle
 
-    bool wrote_phu;			// have we written a PHU for this file?
+    bool wrote_phu;                     // have we written a PHU for this file?
     psMetadata *header;                 // pointer (view) to the current hdu header
 
@@ -90,6 +90,7 @@
 
     psMetadata *camera;                 // Camera configuration
+    psString cameraName;                // Name of the camera
     psMetadata *format;                 // Camera format
-    char *formatName;                   // name of the camera format
+    psString formatName;                // name of the camera format
 }
 pmFPAfile;
Index: trunk/psModules/src/camera/pmFPAfileDefine.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 14886)
+++ trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 14889)
@@ -79,4 +79,5 @@
         file->fpa = psMemIncrRefCounter(fpa);
         file->camera = psMemIncrRefCounter((psMetadata *)fpa->camera);
+        file->cameraName = psMemIncrRefCounter(config->cameraName);
     }
 
@@ -97,6 +98,6 @@
                                           pmFPA *fpa, // Optional FPA to bind
                                           const char *name, // Name of file rule
-                                          const char *cameraName, // Name of camera configuration to use
-                                          const char *formatName // Name of camera format to use
+                                          psString cameraName, // Name of camera configuration to use
+                                          psString formatName // Name of camera format to use
     )
 {
@@ -164,4 +165,5 @@
         } else {
             camera = config->camera;
+            cameraName = config->cameraName;
         }
     } else {
@@ -184,12 +186,17 @@
     }
     file->camera = psMemIncrRefCounter(camera);
+    file->cameraName = psMemIncrRefCounter(cameraName);
 
     // Use the format we were told to, the format specified in the file rule, or default to the default format
     if (!formatName || strlen(formatName) == 0) {
-        // select the format list from the selected camera
-        formatName = psMetadataLookupStr(&status, data, "FILE.FORMAT");
-        if (!formatName || strcmp(formatName, "NONE") == 0) {
-            // Try to get by with the default
-            formatName = config->formatName;
+        if (fpa && fpa->formatName && strlen(fpa->formatName) > 0) {
+            formatName = (psString)fpa->formatName; // Casting away "const" --- it will be copied, so no prob.
+        } else {
+            // select the format list from the selected camera
+            formatName = psMetadataLookupStr(&status, data, "FILE.FORMAT");
+            if (!formatName || strcmp(formatName, "NONE") == 0) {
+                // Try to get by with the default
+                formatName = config->formatName;
+            }
         }
     }
@@ -263,4 +270,23 @@
 
     return pmFPAfileDefineOutputForFormat(config, fpa, name, NULL, NULL);
+}
+
+// define a pmFPAfile, bind to the optional file if supplied
+pmFPAfile *pmFPAfileDefineOutputFromFile(const pmConfig *config, pmFPAfile *file, const char *name)
+{
+    PS_ASSERT_PTR_NON_NULL(config, NULL);
+    PS_ASSERT_PTR_NON_NULL(config->files, NULL);
+    PS_ASSERT_PTR_NON_NULL(config->camera, NULL);
+    PS_ASSERT_STRING_NON_EMPTY(name, NULL);
+
+    char *cameraName = NULL, *formatName = NULL; // Name of camera and format
+    pmFPA *fpa = NULL;                  // FPA for file
+    if (file) {
+        cameraName = file->cameraName;
+        formatName = file->formatName;
+        fpa = file->fpa;
+    }
+
+    return pmFPAfileDefineOutputForFormat(config, fpa, name, cameraName, formatName);
 }
 
@@ -911,19 +937,4 @@
     PS_ASSERT_STRING_NON_EMPTY(filename, NULL);
 
-    // Need to look up the format
-    bool mdok;                          // Status of MD lookup
-    psMetadata *formats = psMetadataLookupMetadata(&mdok, src->camera, "FORMATS"); // The FORMATS
-    if (!mdok || !formats) {
-        psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find FORMATS in camera configuration %s.\n",
-                config->cameraName);
-        return NULL;
-    }
-    psMetadata *format = psMetadataLookupMetadata(&mdok, formats, config->formatName); // The format
-    if (!mdok || !format) {
-        psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find camera format %s within camera %s.\n",
-                config->formatName, config->cameraName);
-        return NULL;
-    }
-
     pmFPA *fpa = pmFPAConstruct(src->camera);
     pmFPAfile *file = pmFPAfileDefineOutput (config, fpa, filename);
@@ -932,8 +943,23 @@
         return NULL;
     }
-    file->src = src; // inherit output elements from this source pmFPA
+    file->src = psMemIncrRefCounter(src); // inherit output elements from this source pmFPA
     file->xBin = xBin;
     file->yBin = yBin;
     psFree (fpa);
+    return file;
+}
+
+pmFPAfile *pmFPAfileDefineFromFile(const pmConfig *config, pmFPAfile *src, int xBin, int yBin,
+                                   const char *filename)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+    PS_ASSERT_PTR_NON_NULL(src, false);
+    PS_ASSERT_STRING_NON_EMPTY(filename, NULL);
+
+    pmFPAfile *file = pmFPAfileDefineOutputForFormat(config, NULL, filename, src->cameraName,
+                                                     src->formatName);
+    file->src = psMemIncrRefCounter(src->fpa); // inherit output elements from this source pmFPA
+    file->xBin = xBin;
+    file->yBin = yBin;
     return file;
 }
@@ -1038,5 +1064,5 @@
     }
 
-    file->src = src; // inherit output elements from this source pmFPA
+    file->src = psMemIncrRefCounter(src); // inherit output elements from this source pmFPA
 
     file->mosaicLevel = PM_FPA_LEVEL_CHIP; // don't do any I/O on this at a lower level
@@ -1083,5 +1109,5 @@
     }
 
-    file->src = src; // inherit output elements from this source pmFPA
+    file->src = psMemIncrRefCounter(src); // inherit output elements from this source pmFPA
 
     file->mosaicLevel = PM_FPA_LEVEL_FPA; // don't do any I/O on this at a lower level
Index: trunk/psModules/src/camera/pmFPAfileDefine.h
===================================================================
--- trunk/psModules/src/camera/pmFPAfileDefine.h	(revision 14886)
+++ trunk/psModules/src/camera/pmFPAfileDefine.h	(revision 14889)
@@ -4,6 +4,6 @@
  * @author EAM, IfA
  *
- * @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-08-23 23:42:41 $
+ * @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-09-19 21:37:58 $
  * Copyright 2004-2005 Institute for Astronomy, University of Hawaii
  */
@@ -28,13 +28,20 @@
 // Define an output pmFPAfile
 pmFPAfile *pmFPAfileDefineOutput(const pmConfig *config, // Configuration
-				 pmFPA *fpa, // Optional FPA to bind
-				 const char *name // Name of camera format to use
+                                 pmFPA *fpa, // Optional FPA to bind
+                                 const char *name // Name of file rule
     );
 
+/// Same as pmFPAfileDefineOutput, but binds to the fpa in the provided file
+pmFPAfile *pmFPAfileDefineOutputFromFile(const pmConfig *config, // Configuration
+                                         pmFPAfile *file, // File to bind FPAs, or NULL
+                                         const char *name // Name of file rule
+    );
+
+/// Define the FPA file using the provided camera and format names.
 pmFPAfile *pmFPAfileDefineOutputForFormat(const pmConfig *config, // Configuration
-					  pmFPA *fpa, // Optional FPA to bind
-					  const char *name, // Name of file rule
-					  const char *cameraName, // Name of camera configuration to use
-					  const char *formatName // Name of camera format to use
+                                          pmFPA *fpa, // Optional FPA to bind
+                                          const char *name, // Name of file rule
+                                          psString cameraName, // Name of camera configuration to use
+                                          psString formatName // Name of camera format to use
     );
 
@@ -69,4 +76,12 @@
 // reference count is held by the config->files metadata.
 pmFPAfile *pmFPAfileDefineFromFPA (const pmConfig *config, pmFPA *src, int xBin, int yBin, const char *filename);
+
+/// Same as pmFPAfileDefineFromFPA, except it uses an FPA file instead of an FPA
+pmFPAfile *pmFPAfileDefineFromFile(const pmConfig *config, // Configuration
+                                   pmFPAfile *src, // Source file for this file
+                                   int xBin, int yBin, // Binning for this file
+                                   const char *filename // Name of file rule
+    );
+
 
 // create a new output pmFPAfile based on an existing FPA
