Index: trunk/psModules/src/camera/pmFPAfileDefine.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 17911)
+++ trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 17988)
@@ -429,4 +429,6 @@
     psMetadata *phu = NULL;
     psMetadata *format = NULL;
+    psMetadata *camera = NULL;
+    psString formatName = NULL;
 
     // use success to identify valid exit conditions (as opposed to 'argument not supplied')
@@ -471,22 +473,30 @@
     psFitsClose(fits);
 
-    // determine the current format from the header
-    // determine camera if not specified already
-    format = pmConfigCameraFormatFromHeader(config, phu, true);
+    // Determine the current format from the header; Determine camera if not specified already.
+    // the returned pointers 'camera' and 'formatName' are allocated here
+    format = pmConfigCameraFormatFromHeader(&camera, &formatName, config, phu, true);
     if (!format) {
         psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", realName);
         psFree(phu);
-        psFree (realName);
+	psFree(camera);
+	psFree(formatName);
+        psFree(realName);
         return NULL;
     }
 
     // build the template fpa, set up the basic view
-    fpa = pmFPAConstruct(config->camera, config->cameraName);
+    // XXX do we want this to be the baseCamera name or the metaCamera name?
+    fpa = pmFPAConstruct(camera, config->cameraName);
     if (!fpa) {
         psError(PS_ERR_IO, false, "Failed to construct FPA from %s", realName);
-        psFree (realName);
+        psFree(phu);
+	psFree(camera);
+	psFree(formatName);
+        psFree(realName);
+        psFree(format);
         return NULL;
     }
     psFree (realName);
+    psFree (camera);
 
     // load the given filerule (from config->camera) and bind it to the fpa
@@ -496,14 +506,13 @@
         psError(PS_ERR_IO, false, "file %s not defined\n", filename);
         psFree(phu);
+	psFree(formatName);
+        psFree(format);
         psFree(fpa);
-        psFree(format);
-        return NULL;
-    }
-    psFree (format);
-    file->format = psMemIncrRefCounter(format);
-    file->formatName = psStringCopy(config->formatName);
+        return NULL;
+    }
+    file->format = format;
+    file->formatName = formatName;
 
     // adjust the rules to identify these files in the file->names data
-    psFree (file->filerule);
     file->filerule = psStringCopy("@FILES");
     file->filesrc = psStringCopy("{CHIP.NAME}.{CELL.NAME}");
@@ -514,5 +523,4 @@
         psFree(phu);
         psFree(fpa);
-        psFree(format);
         return NULL;
     }
@@ -527,4 +535,6 @@
             if (!realName) {
                 psError(PS_ERR_IO, false, "Failed to convert file name %s", (char *) infiles->data[i]);
+		psFree(phu);
+		psFree(fpa);
                 return NULL;
             }
@@ -534,4 +544,6 @@
                 psError(PS_ERR_IO, false, "Failed to open file %s\n", realName);
                 psFree(realName);
+		psFree(phu);
+		psFree(fpa);
                 return NULL;
             }
@@ -541,4 +553,6 @@
                 psFree(realName);
                 psFitsClose(fits);
+		psFree(phu);
+		psFree(fpa);
                 return NULL;
             }
@@ -548,4 +562,6 @@
                 psFree(realName);
                 psFitsClose(fits);
+		psFree(phu);
+		psFree(fpa);
                 return NULL;
             }
@@ -554,4 +570,6 @@
                 psFree(realName);
                 psFitsClose(fits);
+		psFree(phu);
+		psFree(fpa);
                 return NULL;
             }
@@ -566,5 +584,4 @@
             psFree(phu);
             psFree(fpa);
-            psFree(format);
             return NULL;
         }
@@ -670,5 +687,5 @@
 
         if (!format) {
-            format = pmConfigCameraFormatFromHeader(config, phu, true);
+            format = pmConfigCameraFormatFromHeader(NULL, NULL, config, phu, true);
             if (!format) {
                 psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", realName);
@@ -716,5 +733,4 @@
         psFree(phu);
     }
-    psFree(file->format);
     file->format = format;
     file->formatName = psStringCopy(config->formatName);
@@ -775,19 +791,26 @@
     // on first call to this function, config->camera is not set.
     // later calls will give an error if the cameras do not match
-    format = pmConfigCameraFormatFromHeader (config, phu, true);
+    psMetadata *camera = NULL;
+    psString formatName = NULL;
+    format = pmConfigCameraFormatFromHeader (&camera, &formatName, config, phu, true);
     if (!format) {
         psError(PS_ERR_IO, false, "Failed to read CCD format from %s\n", (char *)infiles->data[0]);
         psFree(phu);
+	psFree(camera);
+	psFree(formatName);
         return NULL;
     }
 
     // build the template fpa, set up the basic view
-    fpa = pmFPAConstruct(config->camera, config->cameraName);
+    fpa = pmFPAConstruct (camera, config->cameraName);
     if (!fpa) {
         psError(PS_ERR_IO, false, "Failed to construct FPA from %s", (char *)infiles->data[0]);
         psFree(phu);
+	psFree(camera);
+	psFree(formatName);
         psFree(format);
         return NULL;
     }
+    psFree(camera);
 
     // load the given filerule (from config->camera) and bind it to the fpa
@@ -802,11 +825,8 @@
         return NULL;
     }
-    psFree (file->format);
     file->format = format;
-    file->formatName = psStringCopy(config->formatName);
+    file->formatName = formatName;
 
     // adjust the rules to identify these files in the file->names data
-    psFree (file->filerule);
-    psFree (file->filesrc);
     file->filerule = psStringCopy ("@FILES");
     file->filesrc = psStringCopy ("{CHIP.NAME}.{CELL.NAME}");
@@ -817,5 +837,4 @@
         psFree(phu);
         psFree(fpa);
-        psFree(format);
         return NULL;
     }
@@ -827,5 +846,4 @@
         psFree(phu);
         psFree(fpa);
-        psFree(format);
         return NULL;
     }
@@ -841,5 +859,4 @@
     psFree(view);
     psFree(name);
-    psFree(format);
 
     if (success) {
