Index: trunk/psModules/src/concepts/pmConceptsWrite.c
===================================================================
--- trunk/psModules/src/concepts/pmConceptsWrite.c	(revision 22699)
+++ trunk/psModules/src/concepts/pmConceptsWrite.c	(revision 23428)
@@ -226,4 +226,35 @@
 }
 
+// Return the camera format appropriate for a focal plane hierarchy
+static psMetadata *conceptsCameraFormat(const pmFPA *fpa, const pmChip *chip, const pmCell *cell)
+{
+    pmHDU *hdu = pmHDUGetLowest(fpa, chip, cell); // The HDU at the lowest level
+    if (!hdu) {
+        return NULL;
+    }
+    return hdu->format;
+}
+
+// Return the DATABASE metadata from the format
+static psMetadata *conceptsDatabase(const psMetadata *format)
+{
+    bool mdok;                          // Status of MD lookup
+    return psMetadataLookupMetadata(&mdok, format, "DEFAULTS");
+}
+
+// Return the TRANSLATION metadata from the format
+static psMetadata *conceptsTranslation(const psMetadata *format)
+{
+    bool mdok;                          // Status of MD lookup
+    return psMetadataLookupMetadata(&mdok, format, "TRANSLATION");
+}
+
+// Return the DEFAULTS metadata from the format
+static psMetadata *conceptsDefaults(const psMetadata *format)
+{
+    bool mdok;                          // Status of MD lookup
+    return psMetadataLookupMetadata(&mdok, format, "DEFAULTS");
+}
+
 
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -231,8 +262,7 @@
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
-bool p_pmConceptsWriteToCells(const psMetadata *specs, const pmCell *cell, const psMetadata *concepts)
-{
-    PS_ASSERT_PTR_NON_NULL(specs, false);
-    PS_ASSERT_PTR_NON_NULL(concepts, false);
+bool p_pmConceptWriteToCells(const pmCell *cell, const pmConceptSpec *spec,
+                             const psMetadataItem *conceptItem, const psMetadata *format)
+{
     if (!cell) {
         return false;
@@ -242,253 +272,280 @@
     }
 
-    pmHDU *hdu = pmHDUGetLowest(NULL, NULL, cell); // The HDU at the lowest level
+    if (!format) {
+        format = conceptsCameraFormat(NULL, NULL, cell);
+        if (!format) {
+            return false;
+        }
+    }
+
+    psMetadataItem *cameraItem = psMetadataLookup(cell->config, conceptItem->name); // Version in the config
+    if (!cameraItem) {
+        return false;
+    }
+
+    psString nameSource = NULL; // String with the concept name and ".SOURCE" added
+    psStringAppend(&nameSource, "%s.SOURCE", conceptItem->name);
+    bool mdok = true;       // Status of MD lookup
+    psString source = psMetadataLookupStr(&mdok, cell->config, nameSource); // The source
+    if (mdok && strlen(source) > 0) {
+        psTrace("psModules.concepts", 8, "%s is %s\n", nameSource, source);
+        if (strcasecmp(source, "HEADER") == 0) {
+            if (cameraItem->type != PS_DATA_STRING) {
+                psWarning("Concept %s is specified by header, but is not of type STR --- ignored.",
+                          conceptItem->name);
+                psFree(nameSource);
+                return false;
+            }
+
+            // Formatted version
+            psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_HEADER,
+                                                      format, NULL, NULL, cell);
+            if (!formatted) {
+                psFree(nameSource);
+                return true;
+            }
+
+            psTrace("psModules.concepts", 8, "Writing %s to header %s\n",
+                    conceptItem->name, cameraItem->data.str);
+            pmHDU *hdu = pmHDUGetLowest(NULL, NULL, cell); // Header data unit
+            if (!hdu) {
+                psError(PS_ERR_UNEXPECTED_NULL, false,
+                        "Unable to find HDU to write concept %s", conceptItem->name);
+                return false;
+            }
+            writeHeader(hdu, cameraItem->data.V, formatted);
+            psFree(formatted);
+        } else if (strcasecmp(source, "VALUE") == 0) {
+            // Formatted version
+            psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_CELLS,
+                                                      format, NULL, NULL, cell);
+            if (!formatted) {
+                psFree(nameSource);
+                return true;
+            }
+
+            psTrace("psModules.concepts", 8, "Checking %s against camera format.\n", conceptItem->name);
+            if (!compareConcepts(formatted, cameraItem)) {
+                psWarning("Concept %s is specified by value in the camera format, but the values don't match",
+                          conceptItem->name);
+            }
+            psFree(formatted);
+        } else {
+            psWarning("Concept source %s isn't HEADER or VALUE --- can't write", nameSource);
+        }
+    } else {
+        // Assume it's specified by value
+        psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_CELLS,
+                                                  format, NULL, NULL, cell);
+        if (!formatted) {
+            psFree(nameSource);
+            return true;
+        }
+
+        if (!compareConcepts(formatted, cameraItem)) {
+            psWarning("Concept %s is specified by value in the camera format, but the values don't match.",
+                      conceptItem->name);
+        }
+        psFree(formatted);
+    }
+    psFree(nameSource);
+
+    return true;
+}
+
+bool p_pmConceptWriteToDefaults(const pmFPA *fpa, const pmChip *chip, const pmCell *cell,
+                                const pmConceptSpec *spec, const psMetadataItem *conceptItem,
+                                const psMetadata *format, const psMetadata *defaults)
+{
+    if (!format) {
+        format = conceptsCameraFormat(fpa, chip, cell);
+        if (!format) {
+            return false;
+        }
+    }
+    if (!defaults) {
+        defaults = conceptsDefaults(format);
+        if (!defaults) {
+            return false;
+        }
+    }
+
+    psMetadataItem *defaultItem = p_pmConceptsReadSingleFromDefaults(conceptItem->name, defaults,
+                                                                     fpa, chip, cell);
+    if (!defaultItem) {
+        return false;
+    }
+    psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_DEFAULTS,
+                                              format, fpa, chip, cell);
+    if (!formatted) {
+        return true;
+    }
+
+    if (strcmp(defaultItem->name, conceptItem->name) != 0) {
+        // Correct the name to match the concept name
+        defaultItem = psMetadataItemCopy(defaultItem);
+        psFree(defaultItem->name);
+        defaultItem->name = psStringCopy(conceptItem->name);
+    } else {
+        psMemIncrRefCounter(defaultItem);
+    }
+
+    if (!compareConcepts(formatted, defaultItem)) {
+        psWarning("Concept %s is specified by the DEFAULTS in the camera format, but the values don't match.",
+                  conceptItem->name);
+    }
+    psFree(defaultItem);
+    psFree(formatted);
+
+    return true;
+}
+
+
+bool p_pmConceptWriteToHeader(const pmFPA *fpa, const pmChip *chip, const pmCell *cell,
+                              const pmConceptSpec *spec, const psMetadataItem *conceptItem,
+                              const psMetadata *format, const psMetadata *translation)
+{
+    if (!format) {
+        format = conceptsCameraFormat(fpa, chip, cell);
+        if (!format) {
+            return false;
+        }
+    }
+    if (!translation) {
+        translation = conceptsTranslation(format);
+        if (!translation) {
+            return false;
+        }
+    }
+
+    psMetadataItem *headerItem = psMetadataLookup(translation, conceptItem->name); // How to format for header
+    if (!headerItem) {
+        return false;
+    }
+    if (headerItem->type == PS_DATA_METADATA) {
+        // This is a menu
+        psTrace("psModules.concepts", 5, "%s is of type METADATA.\n", conceptItem->name);
+        headerItem = p_pmConceptsDepend(conceptItem->name, headerItem->data.md, translation, fpa, chip, cell);
+        if (!headerItem) {
+            return false;
+        }
+    }
+    if (headerItem->type != PS_DATA_STRING) {
+        psWarning("TRANSLATION keyword for concept %s isn't of type STR --- ignored.", conceptItem->name);
+        return false;
+    }
+    psTrace("psModules.concepts", 3, "Writing %s to header %s\n", conceptItem->name, headerItem->data.str);
+    psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_HEADER,
+                                              format, fpa, chip, cell);
+    if (!formatted) {
+        // Found it, but it doesn't need to be written
+        return true;
+    }
+
+    pmHDU *hdu = pmHDUGetLowest(fpa, chip, cell); // HDU to which to write
     if (!hdu) {
-        return false;
-    }
-    psMetadata *cameraFormat = hdu->format; // The camera format
-    psMetadataIterator *specsIter = psMetadataIteratorAlloc(specs, PS_LIST_HEAD, NULL); // Iterator
-    psMetadataItem *specItem = NULL;    // Item from the specs metadata
-    while ((specItem = psMetadataGetAndIncrement(specsIter))) {
-        pmConceptSpec *spec = specItem->data.V; // The specification
-        psString name = specItem->name; // The concept name
-        psMetadataItem *cameraItem = psMetadataLookup(cell->config, name); // The concept from the camera,
-        // or NULL
-        if (cameraItem) {
-            // Grab the concept
-            psMetadataItem *conceptItem = psMetadataLookup(concepts, name); // The concept
-
-            psString nameSource = NULL; // String with the concept name and ".SOURCE" added
-            psStringAppend(&nameSource, "%s.SOURCE", name);
-            bool mdok = true;       // Status of MD lookup
-            psString source = psMetadataLookupStr(&mdok, cell->config, nameSource); // The source
-            if (mdok && strlen(source) > 0) {
-                psTrace("psModules.concepts", 8, "%s is %s\n", nameSource, source);
-                if (strcasecmp(source, "HEADER") == 0) {
-                    if (cameraItem->type != PS_DATA_STRING) {
-                        psWarning("Concept %s is specified by header, but is not "
-                                 "of type STR --- ignored.\n", conceptItem->name);
-                        continue;
-                    }
-
-                    // Formatted version
-                    psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_HEADER,
-                                                              cameraFormat, NULL, NULL, cell);
-                    if (!formatted) {
-                        continue;
-                    }
-
-                    psTrace("psModules.concepts", 8, "Writing %s to header %s\n", name, cameraItem->data.str);
-                    writeHeader(hdu, cameraItem->data.V, formatted);
-                    psFree(formatted);
-                } else if (strcasecmp(source, "VALUE") == 0) {
-                    // Formatted version
-                    psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_CELLS,
-                                                              cameraFormat, NULL, NULL, cell);
-                    if (!formatted) {
-                        continue;
-                    }
-
-                    psTrace("psModules.concepts", 8, "Checking %s against camera format.\n", name);
-                    if (! compareConcepts(formatted, cameraItem)) {
-                        psWarning("Concept %s is specified by value in the camera "
-                                 "format, but the values don't match.\n", name);
-                    }
-                    psFree(formatted);
-                } else {
-                    psWarning("Concept source %s isn't HEADER or VALUE --- can't "
-                             "write\n", nameSource);
-                }
-            } else {
-                // Assume it's specified by value
-                psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_CELLS,
-                                                          cameraFormat, NULL, NULL, cell);
-                if (!formatted) {
-                    continue;
-                }
-
-                if (! compareConcepts(formatted, cameraItem)) {
-                    psWarning("Concept %s is specified by value in the camera "
-                             "format, but the values don't match.\n", name);
-                }
-                psFree(formatted);
-            }
-            psFree(nameSource);
-        }
-
-    }
-    psFree(specsIter);
+        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find HDU to write concept %s", conceptItem->name);
+        return false;
+    }
+    writeHeader(hdu, headerItem->data.V, formatted);
+    psFree(formatted);
+
     return true;
 }
 
-bool p_pmConceptsWriteToDefaults(const psMetadata *specs, const pmFPA *fpa, const pmChip *chip,
-                                 const pmCell *cell, const psMetadata *concepts)
-{
-    PS_ASSERT_PTR_NON_NULL(specs, false);
-    PS_ASSERT_PTR_NON_NULL(concepts, false);
-
-    pmHDU *hdu = pmHDUGetLowest(fpa, chip, cell); // The HDU at the lowest level
-    if (!hdu) {
-        return false;
-    }
-    psMetadata *cameraFormat = hdu->format; // The camera format
-    bool mdok = true;                   // Status of MD lookup
-    psMetadata *defaults = psMetadataLookupMetadata(&mdok, cameraFormat, "DEFAULTS"); // The DEFAULTS spec
-    if (!mdok || !defaults) {
-        return false;
-    }
-
-    psMetadataIterator *specsIter = psMetadataIteratorAlloc(specs, PS_LIST_HEAD, NULL); // Iterator
-    psMetadataItem *specItem = NULL;    // Item from the specs metadata
-    while ((specItem = psMetadataGetAndIncrement(specsIter))) {
-        pmConceptSpec *spec = specItem->data.V; // The specification
-        psString name = specItem->name; // The concept name
-
-        psMetadataItem *defaultItem = p_pmConceptsReadSingleFromDefaults(name, defaults, fpa, chip, cell);
-        if (!defaultItem) {
-            continue;
-        }
-        psMetadataItem *conceptItem = psMetadataLookup(concepts, name); // The item from the concepts
-        psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_DEFAULTS,
-                                                  cameraFormat, fpa, chip, cell);
-        if (!formatted) {
-            continue;
-        }
-
-        if (strcmp(defaultItem->name, name) != 0) {
-            // Correct the name to match the concept name
-            defaultItem = psMetadataItemCopy(defaultItem);
-            psFree(defaultItem->name);
-            defaultItem->name = psStringCopy(name);
-        } else {
-            psMemIncrRefCounter(defaultItem);
-        }
-
-        if (!compareConcepts(formatted, defaultItem)) {
-            psWarning("Concept %s is specified by the DEFAULTS in the camera "
-                      "format, but the values don't match.\n", name);
-        }
-        psFree(defaultItem);
-        psFree(formatted);
-    }
-    psFree(specsIter);
+bool p_pmConceptWriteToDatabase(const pmFPA *fpa, const pmChip *chip, const pmCell *cell,
+                                pmConfig *config, const pmConceptSpec *spec,
+                                const psMetadataItem *conceptItem, const psMetadata *format,
+                                const psMetadata *database)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+#ifndef HAVE_PSDB
+    return false;
+#else
+
+    if (!format) {
+        format = conceptsCameraFormat(fpa, chip, cell);
+        if (!format) {
+            return false;
+        }
+    }
+    if (!database) {
+        database = conceptsDatabase(format);
+        if (!database) {
+            return false;
+        }
+    }
+
+    psMetadataItem *dbItem = p_pmConceptsReadSingleFromDatabase(conceptItem->name, database, config,
+                                                                fpa, chip, cell); // Database version
+    if (!dbItem) {
+        return false;
+    }
+
+    psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_DATABASE,
+                                              format, fpa, chip, cell);
+    if (!formatted) {
+        return false;
+    }
+
+    if (strcmp(dbItem->name, conceptItem->name) != 0) {
+        // Correct the name to match the concept name
+        dbItem = psMetadataItemCopy(dbItem);
+        psFree(dbItem->name);
+        dbItem->name = psStringCopy(conceptItem->name);
+    } else {
+        psMemIncrRefCounter(dbItem);
+    }
+
+    if (!compareConcepts(formatted, dbItem)) {
+        psWarning("Concept %s is specified by the DATABASE in the camera "
+                  "format, but the values don't match.\n", conceptItem->name);
+    }
+    psFree(dbItem);
+    psFree(formatted);
+
     return true;
-}
-
-
-bool p_pmConceptsWriteToHeader(const psMetadata *specs, const pmFPA *fpa, const pmChip *chip,
-                               const pmCell *cell, const psMetadata *concepts)
-{
-    PS_ASSERT_PTR_NON_NULL(specs, false);
-    PS_ASSERT_PTR_NON_NULL(concepts, false);
-
-    pmHDU *hdu = pmHDUGetLowest(fpa, chip, cell); // The HDU at the lowest level
-    if (!hdu) {
-        return false;
-    }
-    psMetadata *cameraFormat = hdu->format; // The camera format
-    bool mdok = true;                   // Status of MD lookup
-    psMetadata *translation = psMetadataLookupMetadata(&mdok, cameraFormat, "TRANSLATION"); // The TRANSLATION spec
-    if (!mdok || !translation) {
-        return false;
-    }
-
-    psMetadataIterator *specsIter = psMetadataIteratorAlloc(specs, PS_LIST_HEAD, NULL); // Iterator
-    psMetadataItem *specItem = NULL;    // Item from the specs metadata
-    while ((specItem = psMetadataGetAndIncrement(specsIter))) {
-        pmConceptSpec *spec = specItem->data.V; // The specification
-        psString name = specItem->name; // The concept name
-        psMetadataItem *headerItem = psMetadataLookup(translation, name); // The item from the TRANSLATION
-        if (!headerItem) {
-            continue;
-        }
-        if (headerItem->type == PS_DATA_METADATA) {
-            // This is a menu
-            psTrace("psModules.concepts", 5, "%s is of type METADATA.\n", name);
-            headerItem = p_pmConceptsDepend(name, headerItem->data.md, translation, fpa, chip, cell);
-            if (!headerItem) {
-                continue;
-            }
-        }
-        if (headerItem->type != PS_DATA_STRING) {
-            psWarning("TRANSLATION keyword for concept %s isn't of type STR --- ignored.", name);
-            continue;
-        }
-        psTrace("psModules.concepts", 3, "Writing %s to header %s\n", name, headerItem->data.str);
-        psMetadataItem *conceptItem = psMetadataLookup(concepts, name); // The item from the concepts
-        psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_HEADER,
-                                                  cameraFormat, fpa, chip, cell);
-        if (!formatted) {
-            continue;
-        }
-        writeHeader(hdu, headerItem->data.V, formatted);
-        psFree(formatted);
-    }
-    psFree(specsIter);
+#endif
+}
+
+
+bool pmConceptWriteSingle(const pmFPA *fpa, const pmChip *chip, const pmCell *cell,
+                          pmConfig *config, const psMetadataItem *conceptItem)
+{
+    pmConceptsInit();
+
+    psMetadata *format = conceptsCameraFormat(fpa, chip, cell); // Camera format
+    if (!format) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to retrieve camera format.");
+        return false;
+    }
+
+    const char *name = conceptItem->name; // Name of concept
+
+    psMetadata *conceptsFPA = pmConceptsSpecs(PM_FPA_LEVEL_FPA); // Concept specifications for FPA
+    bool mdok;                          // Status of MD lookup
+    pmConceptSpec *spec = psMetadataLookupPtr(&mdok, conceptsFPA, name); // Concept specification of interest
+    if (!spec) {
+        psMetadata *conceptsChip = pmConceptsSpecs(PM_FPA_LEVEL_CHIP); // Concept specifications for Chip
+        spec = psMetadataLookupPtr(&mdok, conceptsChip, name);
+        if (!spec) {
+            psMetadata *conceptsCell = pmConceptsSpecs(PM_FPA_LEVEL_CELL); // Concept specifications for Cell
+            spec = psMetadataLookupPtr(&mdok, conceptsCell, name);
+            if (!spec) {
+                psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find specification for concept %s", name);
+                return false;
+            }
+        }
+    }
+
+    if (!p_pmConceptWriteToCells(cell, spec, conceptItem, format) &&
+        !p_pmConceptWriteToDefaults(fpa, chip, cell, spec, conceptItem, format, NULL) &&
+        !p_pmConceptWriteToHeader(fpa, chip, cell, spec, conceptItem, format, NULL) &&
+        !p_pmConceptWriteToDatabase(fpa, chip, cell, config, spec, conceptItem, format, NULL)) {
+        return false;
+    }
     return true;
 }
-
-// XXX Warning: This code has not been tested at all
-bool p_pmConceptsWriteToDatabase(const psMetadata *specs, const pmFPA *fpa, const pmChip *chip,
-                                 const pmCell *cell, pmConfig *config, const psMetadata *concepts)
-{
-    PS_ASSERT_PTR_NON_NULL(specs, false);
-    PS_ASSERT_PTR_NON_NULL(concepts, false);
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    #ifndef HAVE_PSDB
-    return false;
-    #else
-
-    pmHDU *hdu = pmHDUGetLowest(fpa, chip, cell); // The HDU at the lowest level
-    if (!hdu) {
-        return false;
-    }
-    psMetadata *cameraFormat = hdu->format; // The camera format
-    bool mdok = true;                   // Status of MD lookup
-    psMetadata *database = psMetadataLookupMetadata(&mdok, cameraFormat, "DATABASE"); // The DATABASE spec
-    if (!mdok || !database) {
-        return false;
-    }
-    psMetadataIterator *specsIter = psMetadataIteratorAlloc(specs, PS_LIST_HEAD, NULL); // Iterator
-    psMetadataItem *specItem = NULL;    // Item from the specs metadata
-    while ((specItem = psMetadataGetAndIncrement(specsIter))) {
-        pmConceptSpec *spec = specItem->data.V; // The specification
-        psString name = specItem->name; // The concept name
-
-        psMetadataItem *dbItem = p_pmConceptsReadSingleFromDatabase(name, database, config, fpa, chip, cell);
-        if (!dbItem) {
-            continue;
-        }
-
-        psMetadataItem *conceptItem = psMetadataLookup(concepts, name); // The item from the concepts
-        psMetadataItem *formatted = conceptFormat(spec, conceptItem, PM_CONCEPT_SOURCE_DATABASE,
-                                                  cameraFormat, fpa, chip, cell);
-        if (!formatted) {
-            continue;
-        }
-
-        if (strcmp(dbItem->name, name) != 0) {
-            // Correct the name to match the concept name
-            dbItem = psMetadataItemCopy(dbItem);
-            psFree(dbItem->name);
-            dbItem->name = psStringCopy(name);
-        } else {
-            psMemIncrRefCounter(dbItem);
-        }
-
-        if (!compareConcepts(formatted, dbItem)) {
-            psWarning("Concept %s is specified by the DATABASE in the camera "
-                      "format, but the values don't match.\n", name);
-        }
-        psFree(dbItem);
-        psFree(formatted);
-    }
-    psFree(specsIter);
-    return true;
-    #endif
-}
-
-
-
 
 
@@ -498,5 +555,4 @@
                           const pmChip *chip, // The chip
                           const pmCell *cell, // The cell
-                          pmConceptSource source, // The source of the concepts to write
                           pmConfig *config, // Configuration
                           psMetadata *concepts // The concepts to write out
@@ -508,18 +564,32 @@
     pmConceptsInit();
 
-    psTrace("psModules.concepts", 3, "Writing concepts (%p %p %p): %d\n", fpa, chip, cell, source);
-
-    if (source & PM_CONCEPT_SOURCE_CELLS) {
-        p_pmConceptsWriteToCells(*specs, cell, concepts);
-    }
-    if (source & PM_CONCEPT_SOURCE_DEFAULTS) {
-        p_pmConceptsWriteToDefaults(*specs, fpa, chip, cell, concepts);
-    }
-    if (source & (PM_CONCEPT_SOURCE_PHU | PM_CONCEPT_SOURCE_HEADER)) {
-        p_pmConceptsWriteToHeader(*specs, fpa, chip, cell, concepts);
-    }
-    if (source & PM_CONCEPT_SOURCE_DATABASE) {
-        p_pmConceptsWriteToDatabase(*specs, fpa, chip, cell, config, concepts);
-    }
+    psTrace("psModules.concepts", 3, "Writing concepts (%p %p %p)\n", fpa, chip, cell);
+
+    psMetadata *format = conceptsCameraFormat(fpa, chip, cell); // Camera format
+    if (!format) {
+        psError(PS_ERR_UNKNOWN, false, "Unable to retrieve camera format.");
+        return false;
+    }
+
+    psMetadata *defaults = conceptsDefaults(format); // DEFAULTS configuration
+    psMetadata *translation = conceptsTranslation(format); // TRANSLATION configuration
+    psMetadata *database = conceptsDatabase(format); // DATABASE configuration
+
+    psMetadataIterator *iter = psMetadataIteratorAlloc(*specs, PS_LIST_HEAD, NULL); // Iterator
+    psMetadataItem *item = NULL;    // Item from the specs metadata
+    while ((item = psMetadataGetAndIncrement(iter))) {
+        pmConceptSpec *spec = item->data.V; // The specification
+        psString name = item->name; // The concept name
+
+        psMetadataItem *concept = psMetadataLookup(concepts, name); // Concept to write
+
+        if (!p_pmConceptWriteToCells(cell, spec, concept, format) &&
+            !p_pmConceptWriteToDefaults(fpa, chip, cell, spec, concept, format, defaults) &&
+            !p_pmConceptWriteToHeader(fpa, chip, cell, spec, concept, format, translation) &&
+            !p_pmConceptWriteToDatabase(fpa, chip, cell, config, spec, concept, format, database)) {
+            psTrace("psModules.concepts", 1, "Unable to write concept %s to any output", name);
+        }
+    }
+    psFree(iter);
 
     return true;
@@ -527,10 +597,10 @@
 
 
-bool pmConceptsWriteFPA(const pmFPA *fpa, pmConceptSource source, bool propagateDown, pmConfig *config)
+bool pmConceptsWriteFPA(const pmFPA *fpa, bool propagateDown, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(fpa, false);
     psMetadata *conceptsFPA = pmConceptsSpecs(PM_FPA_LEVEL_FPA); // Concept specifications
     psTrace("psModules.concepts", 5, "Writing FPA concepts: %p %p\n", conceptsFPA, fpa->concepts);
-    bool success = conceptsWrite(&conceptsFPA, fpa, NULL, NULL, source, config, fpa->concepts);
+    bool success = conceptsWrite(&conceptsFPA, fpa, NULL, NULL, config, fpa->concepts);
     if (propagateDown) {
         psArray *chips = fpa->chips;        // Array of chips
@@ -538,5 +608,5 @@
             pmChip *chip = chips->data[i];  // Chip of interest
             if (chip && !chip->hdu) {
-                success &= pmConceptsWriteChip(chip, source, false, true, config);
+                success &= pmConceptsWriteChip(chip, false, true, config);
             }
         }
@@ -546,6 +616,5 @@
 
 
-bool pmConceptsWriteChip(const pmChip *chip, pmConceptSource source, bool propagateUp,
-                         bool propagateDown, pmConfig *config)
+bool pmConceptsWriteChip(const pmChip *chip, bool propagateUp, bool propagateDown, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(chip, false);
@@ -553,8 +622,8 @@
     psTrace("psModules.concepts", 5, "Writing chip concepts: %p %p\n", conceptsChip, chip->concepts);
     pmFPA *fpa = chip->parent;          // FPA to which the chip belongs
-    bool success = conceptsWrite(&conceptsChip, fpa, chip, NULL, source, config, chip->concepts);
+    bool success = conceptsWrite(&conceptsChip, fpa, chip, NULL, config, chip->concepts);
     if (propagateUp && !fpa->hdu) {
         psMetadata *conceptsFPA = pmConceptsSpecs(PM_FPA_LEVEL_FPA); // Concept specifications
-        success &= conceptsWrite(&conceptsFPA, fpa, chip, NULL, source, config, fpa->concepts);
+        success &= conceptsWrite(&conceptsFPA, fpa, chip, NULL, config, fpa->concepts);
     }
     if (propagateDown) {
@@ -563,5 +632,5 @@
             pmCell *cell = cells->data[i];  // Cell of interest
             if (cell && !cell->hdu) {
-                success &= pmConceptsWriteCell(cell, source, false, config);
+                success &= pmConceptsWriteCell(cell, false, config);
             }
         }
@@ -571,5 +640,5 @@
 
 
-bool pmConceptsWriteCell(const pmCell *cell, pmConceptSource source, bool propagateUp, pmConfig *config)
+bool pmConceptsWriteCell(const pmCell *cell, bool propagateUp, pmConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(cell, false);
@@ -579,12 +648,12 @@
     pmFPA *fpa = chip->parent;          // FPA to which the chip belongs
 
-    bool success = conceptsWrite(&conceptsCell, fpa, chip, cell, source, config, cell->concepts);
+    bool success = conceptsWrite(&conceptsCell, fpa, chip, cell, config, cell->concepts);
     if (propagateUp) {
         if (!chip->hdu) {
             psMetadata *conceptsChip = pmConceptsSpecs(PM_FPA_LEVEL_CHIP); // Concept specifications
-            success &= conceptsWrite(&conceptsChip, fpa, chip, cell, source, config, chip->concepts);
+            success &= conceptsWrite(&conceptsChip, fpa, chip, cell, config, chip->concepts);
             if (!fpa->hdu) {
                 psMetadata *conceptsFPA = pmConceptsSpecs(PM_FPA_LEVEL_FPA); // Concept specifications
-                success &= conceptsWrite(&conceptsFPA, fpa, chip, cell, source, config, fpa->concepts);
+                success &= conceptsWrite(&conceptsFPA, fpa, chip, cell, config, fpa->concepts);
             }
         }
