Index: trunk/psModules/src/camera/pmFPAfileDefine.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 14647)
+++ 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
