Index: trunk/psModules/src/concepts/pmConceptsRead.c
===================================================================
--- trunk/psModules/src/concepts/pmConceptsRead.c	(revision 17874)
+++ trunk/psModules/src/concepts/pmConceptsRead.c	(revision 17911)
@@ -181,4 +181,19 @@
 }
 
+psMetadataItem *p_pmConceptsReadSingleFromDefaults(const char *name, const psMetadata *defaults,
+                                                  const pmFPA *fpa, const pmChip *chip, const pmCell *cell)
+{
+    PS_ASSERT_STRING_NON_EMPTY(name, NULL);
+    PS_ASSERT_METADATA_NON_NULL(defaults, NULL);
+
+    psMetadataItem *item = psMetadataLookup(defaults, name); // The concept, or NULL
+    psTrace("psModules.concepts", 10, "%s: %p\n", name, item);
+    if (item && item->type == PS_DATA_METADATA) {
+        // This is a menu
+        psTrace("psModules.concepts", 5, "%s is of type METADATA.\n", name);
+        item = p_pmConceptsDepend(name, item->data.md, defaults, fpa, chip, cell);
+    }
+    return item;
+}
 
 bool p_pmConceptsReadFromDefaults(psMetadata *target, const psMetadata *specs,
@@ -209,47 +224,5 @@
         pmConceptSpec *spec = specItem->data.V; // The specification
         psString name = specItem->name; // The concept name
-        psMetadataItem *conceptItem = psMetadataLookup(defaults, name); // The concept, or NULL
-        psTrace("psModules.concepts", 10, "%s: %p\n", name, conceptItem);
-        if (conceptItem && conceptItem->type == PS_DATA_METADATA) {
-            psTrace("psModules.concepts", 5, "%s is of type METADATA.\n", name);
-            // Check for DEPEND
-            psMetadata *dependMenu = conceptItem->data.V; // The DEPEND menu
-            psString depend = NULL; // The CONCEPT.DEPEND
-            psStringAppend(&depend, "%s.DEPEND", name);
-            const char *dependConcept = psMetadataLookupStr(&mdok, defaults, depend); // The concept name
-            if (!mdok || !dependConcept || strlen(dependConcept) == 0) {
-                psError(PS_ERR_IO, true, "Unable to parse %s: couldn't find %s in DEFAULTS.\n", name,
-                        depend);
-                psFree(depend);
-                continue;
-            }
-            psFree(depend);
-            // Now look up the depend value
-            psMetadataItem *dependValue = NULL; // The value of the concept we're looking up
-            if (cell) {
-                dependValue = psMetadataLookup(cell->concepts, dependConcept);
-            }
-            if (chip && !dependValue) {
-                dependValue = psMetadataLookup(chip->concepts, dependConcept);
-            }
-            if (fpa && !dependValue) {
-                dependValue = psMetadataLookup(chip->concepts, dependConcept);
-            }
-            if (!dependValue) {
-                psError(PS_ERR_IO, true, "Unable to find %s to resolve %s in DEFAULTS.\n",
-                        dependConcept, name);
-                continue;
-            }
-            if (dependValue->type != PS_DATA_STRING) {
-                psError(PS_ERR_BAD_PARAMETER_TYPE, true, "%s is required to resolve %s in DEFAULTS, "
-                        "but it is not of type STRING.\n", dependConcept, name);
-                continue;
-            }
-            const char *dependKey = dependValue->data.V; // The key to the DEPEND menu
-            psTrace("psModules.concepts", 7, "%s.DEPEND resolves to %s....\n", name, dependKey);
-
-            conceptItem = psMetadataLookup(dependMenu, dependKey);
-            // Now we can parse this as we would ordinarily
-        }
+        psMetadataItem *conceptItem = p_pmConceptsReadSingleFromDefaults(name, defaults, fpa, chip, cell);
         if (conceptItem && !conceptParse(spec, conceptItem, PM_CONCEPT_SOURCE_DEFAULTS,
                                          cameraFormat, target, fpa, chip, cell)) {
@@ -282,5 +255,5 @@
     psMetadata *cameraFormat = hduLow->format; // The camera format
     bool mdok = true;                   // Status of MD lookup
-    psMetadata *transSpec = psMetadataLookupMetadata(&mdok, cameraFormat, "TRANSLATION"); // The TRANSLATION spec
+    psMetadata *transSpec = psMetadataLookupMetadata(&mdok, cameraFormat, "TRANSLATION"); // TRANSLATION spec
     if (!mdok || !transSpec) {
         psError(PS_ERR_IO, true, "Failed to find \"TRANSLATION\"");
@@ -318,42 +291,59 @@
         }
 
-        if (! headerItem) {
-            psString keywords = psMetadataLookupStr(&mdok, transSpec, name); // The FITS keywords
-            if (mdok && strlen(keywords) > 0) {
-                // In case there are multiple headers
-                psList *keys = psStringSplit(keywords, " ,;", true); // List of keywords
-                if (keys->n == 1) {
-                    // Only one key --- proceed as usual
+        if (!headerItem) {
+            psMetadataItem *formatItem = psMetadataLookup(transSpec, name); // Item with keyword
+            if (!formatItem) {
+                continue;
+            }
+            if (formatItem->type == PS_DATA_METADATA) {
+                // This is a menu
+                psTrace("psModules.concepts", 5, "%s is of type METADATA.\n", name);
+                formatItem = p_pmConceptsDepend(name, formatItem->data.md, transSpec, fpa, chip, cell);
+                if (!formatItem) {
+                    continue;
+                }
+            }
+            if (formatItem->type != PS_DATA_STRING) {
+                psError(PS_ERR_BAD_PARAMETER_TYPE, true, "Type for concept %s in TRANSLATION is not STR",
+                        name);
+                psFree(specsIter);
+                return false;
+            }
+            psString keywords = formatItem->data.str; // The FITS keywords
+
+            // In case there are multiple headers
+            psList *keys = psStringSplit(keywords, " ,;", true); // List of keywords
+            if (keys->n == 1) {
+                // Only one key --- proceed as usual
+                if (hduLow->header) {
+                    headerItem = psMetadataLookup(hduLow->header, keywords);
+                }
+                if (!headerItem && hduHigh != hduLow && hduHigh->header) {
+                    headerItem = psMetadataLookup(hduHigh->header, keywords);
+                }
+                psMemIncrRefCounter(headerItem);
+            } else {
+                psListIterator *keysIter = psListIteratorAlloc(keys, PS_LIST_HEAD, false); // Iterator
+                psString key = NULL; // Item from iteration
+                psList *values = psListAlloc(NULL); // List containing the values
+                while ((key = psListGetAndIncrement(keysIter))) {
+                    psMetadataItem *value = NULL;
                     if (hduLow->header) {
-                        headerItem = psMetadataLookup(hduLow->header, keywords);
-                    }
-                    if (!headerItem && hduHigh != hduLow && hduHigh->header) {
-                        headerItem = psMetadataLookup(hduHigh->header, keywords);
-                    }
-                    psMemIncrRefCounter(headerItem);
-                } else {
-                    psListIterator *keysIter = psListIteratorAlloc(keys, PS_LIST_HEAD, false); // Iterator
-                    psString key = NULL; // Item from iteration
-                    psList *values = psListAlloc(NULL); // List containing the values
-                    while ((key = psListGetAndIncrement(keysIter))) {
-                        psMetadataItem *value = NULL;
-                        if (hduLow->header) {
-                            value = psMetadataLookup(hduLow->header, key);
-                        }
-                        if (!value && hduHigh != hduLow && hduHigh->header) {
-                            value = psMetadataLookup(hduHigh->header, key);
-                        }
-                        if (value) {
-                            psListAdd(values, PS_LIST_TAIL, value);
-                        } else {
-                            psWarning("Unable to find header %s --- assuming value is NULL", key);
-                        }
-                    }
-                    psFree(keysIter);
-                    headerItem = psMetadataItemAlloc(name, PS_DATA_LIST, specItem->comment, values);
-                    psFree(values);
-                }
-                psFree(keys);
-            }
+                        value = psMetadataLookup(hduLow->header, key);
+                    }
+                    if (!value && hduHigh != hduLow && hduHigh->header) {
+                        value = psMetadataLookup(hduHigh->header, key);
+                    }
+                    if (value) {
+                        psListAdd(values, PS_LIST_TAIL, value);
+                    } else {
+                        psWarning("Unable to find header %s --- assuming value is NULL", key);
+                    }
+                }
+                psFree(keysIter);
+                headerItem = psMetadataItemAlloc(name, PS_DATA_LIST, specItem->comment, values);
+                psFree(values);
+            }
+            psFree(keys);
         }
 
@@ -371,4 +361,48 @@
     psFree(specsIter);
     return status;
+}
+
+psMetadataItem *p_pmConceptsReadSingleFromDatabase(const char *name, const psMetadata *database, psDB *db,
+                                                   const pmFPA *fpa, const pmChip *chip, const pmCell *cell)
+{
+    PS_ASSERT_STRING_NON_EMPTY(name, NULL);
+    PS_ASSERT_METADATA_NON_NULL(database, NULL);
+
+    psMetadataItem *item = psMetadataLookup(database, name); // Item to return
+    if (item && item->type == PS_DATA_METADATA) {
+        // This is a menu
+        psTrace("psModules.concepts", 5, "%s is of type METADATA.\n", name);
+        item = p_pmConceptsDepend(name, item->data.md, database, fpa, chip, cell);
+    }
+    if (!item) {
+        return NULL;
+    }
+    if (item->type != PS_DATA_STRING) {
+        psWarning("%s in DATABASE in camera format is not of type STR --- ignored.", name);
+        return NULL;
+    }
+
+    psString sql = pmConceptsInterpolate(item->data.str, fpa, chip, cell);
+    if (!p_psDBRunQuery(db, sql)) {
+        psWarning("Unable to query database for concept %s --- ignored.", name);
+        psFree(sql);
+        return NULL;
+    }
+    psFree(sql);
+
+    psArray *rows = p_psDBFetchResult(db); // Rows returned from the query
+    if (rows->n == 0) {
+        psWarning("No rows returned from database query for concept %s --- ignored.", name);
+        return NULL;
+    }
+    if (rows->n > 1) {
+        psWarning("Multiple rows returned from database query for concept %s --- using the first", name);
+    }
+    psMetadata *row = rows->data[0]; // First (and only) row
+    if (row->list->n > 1) {
+        psWarning("Multiple columns returned from database query for concept %s --- using the first", name);
+    }
+
+    return psMetadataGet(row, PS_LIST_HEAD); // Item of interest
 }
 
@@ -400,38 +434,9 @@
         pmConceptSpec *spec = specItem->data.V; // The specification
         psString name = specItem->name; // The concept name
-        const char *dbLookup = psMetadataLookupStr(&mdok, dbSpec, name);
-        if (mdok && dbLookup) {
-            psString sql = pmConceptsInterpolate(dbLookup, fpa, chip, cell);
-
-            if (!p_psDBRunQuery(db, sql)) {
-                psWarning("Unable to query database for concept %s --- ignored.", name);
-                psFree(sql);
-                continue;
-            }
-            psFree(sql);
-
-            psArray *rows = p_psDBFetchResult(db); // Rows returned from the query
-            if (rows->n == 0) {
-                psWarning("No rows returned from database query for concept %s --- ignored.", name);
-                continue;
-            }
-            if (rows->n > 1) {
-                psWarning("Multiple rows returned from database query for concept %s --- using the first",
-                          name);
-            }
-            psMetadata *row = rows->data[0]; // First (and only) row
-            if (row->list->n > 1) {
-                psWarning("Multiple columns returned from database query for concept %s --- using the first",
-                          name);
-            }
-            psMetadataItem *conceptItem = psMetadataGet(row, PS_LIST_HEAD); // Item of interest
-
-            // Now we have the result
-            if (!conceptParse(spec, conceptItem, PM_CONCEPT_SOURCE_DATABASE,
-                              cameraFormat, target, fpa, chip, cell)) {
-                psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to parse concept %s from database.\n",
-                        name);
-                status = false;
-            }
+        psMetadataItem *conceptItem = p_pmConceptsReadSingleFromDatabase(name, dbSpec, db, fpa, chip, cell);
+        if (conceptItem && !conceptParse(spec, conceptItem, PM_CONCEPT_SOURCE_DATABASE,
+                                         cameraFormat, target, fpa, chip, cell)) {
+            psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to parse concept %s from database.\n", name);
+            status = false;
         }
     } // Iterating through the concept specifications
