Index: trunk/psModules/src/camera/pmFPAfileDefine.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 12299)
+++ trunk/psModules/src/camera/pmFPAfileDefine.c	(revision 12300)
@@ -880,4 +880,5 @@
         return NULL;
     }
+    file->detrend = results;
 
     // replace the existing value of filextra with the selected detID
@@ -889,5 +890,4 @@
     // file->fileLevel = results->level;
 
-    psFree (results);
     psFree (options);
 
Index: trunk/psModules/src/camera/pmFPAfileIO.c
===================================================================
--- trunk/psModules/src/camera/pmFPAfileIO.c	(revision 12299)
+++ trunk/psModules/src/camera/pmFPAfileIO.c	(revision 12300)
@@ -225,23 +225,41 @@
     // get name from detrend database
     // extrule expands to yield the class_id
-    // filextra contains the desired -det_id detID -iteration iter string
+    // file->detrend->detID contains the desired -det_id detID -iteration iter string
     if (!strcasecmp (file->filename, "@DETDB")) {
-        char *extra = pmFPAfileNameFromRule (file->extrule, file, view);
-        if (extra == NULL) {
-            psError(PS_ERR_IO, false, "error converting extrule to name %s\n", file->extrule);
-            return false;
-        }
-        psTrace ("pmFPAfile", 6, "looking for detrend (%s, %s)\n", file->filextra, extra);
-
+        if (!file->detrend) {
+            psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find information about selected detrend.");
+            return false;
+        }
+
+        psString classId = NULL;        // The class identifier, to pass to pmDetrendFile
+        psMetadata *menu = psMetadataLookupMetadata(&status, file->camera, "CLASSID"); // Menu of class IDs
+        if (!status || !menu) {
+            psWarning("Unable to find CLASSID metadata in camera configuration --- "
+                      "assuming no class identifier required.");
+        } else {
+            const char *rule = psMetadataLookupStr(&status, menu, file->detrend->level); // Rule for class_id
+            if (!status || !rule || strlen(rule) == 0) {
+                psWarning("Unable to find %s in CLASSID within camera configuration --- "
+                          "attempting to proceed without it.", file->detrend->level);
+            } else {
+                classId = pmFPAfileNameFromRule(rule, file, view);
+                if (!classId) {
+                    psError(PS_ERR_IO, false, "error converting extrule to name: %s\n", rule);
+                    return false;
+                }
+            }
+        }
+        psTrace ("pmFPAfile", 6, "looking for detrend (%s, %s)\n", file->detrend->detID, classId);
         psFree (file->filename);
-        file->filename = pmDetrendFile(file->filextra, extra, config);
+
+        file->filename = pmDetrendFile(file->detrend->detID, classId, config);
         if (file->filename == NULL) {
-            psError(PS_ERR_IO, false, "failed to find a valid detrend image for detID %s : classID %s\n", file->filextra, extra);
-            psFree (extra);
+            psError(PS_ERR_IO, false, "failed to find a valid detrend image for detID %s : classID %s\n", file->detrend->detID, classId);
+            psFree (classId);
             return false;
         }
 
         psTrace ("pmFPAfile", 6, "got detrend file %s\n", file->filename);
-        psFree (extra);
+        psFree (classId);
     }
 
Index: trunk/psModules/src/detrend/pmDetrendDB.c
===================================================================
--- trunk/psModules/src/detrend/pmDetrendDB.c	(revision 12299)
+++ trunk/psModules/src/detrend/pmDetrendDB.c	(revision 12300)
@@ -47,8 +47,8 @@
     options->version  = NULL;
     options->dettype  = NULL;
-    options->exptime  = 0.0; 
-    options->airmass  = 0.0; 
-    options->dettemp  = 0.0; 
-    options->twilight = 0.0; 
+    options->exptime  = 0.0;
+    options->airmass  = 0.0;
+    options->dettemp  = 0.0;
+    options->twilight = 0.0;
 
     options->exptimeSet  = false; // not selected
@@ -120,7 +120,7 @@
     char *type = NULL;
     if (options->dettype) {
-	type = psMemIncrRefCounter (options->dettype);
+        type = psMemIncrRefCounter (options->dettype);
     } else {
-	type = pmDetrendTypeToString (options->type);
+        type = pmDetrendTypeToString (options->type);
     }
     unsigned int nFail;
@@ -189,10 +189,26 @@
 
     bool mdstatus;
-    results->level = 0;
     int detID = psMetadataLookupS32 (&mdstatus, md, "det_id");
+    if (!mdstatus) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find det_id in output from detselect.");
+        psLogMsg ("psModule.detrend", PS_LOG_ERROR, "detselect response:\n %s\n", buffer->data);
+        goto failure;
+    }
     int iteration  = psMetadataLookupS32 (&mdstatus, md, "iteration");
+    if (!mdstatus) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find iteration in output from detselect.");
+        psLogMsg ("psModule.detrend", PS_LOG_ERROR, "detselect response:\n %s\n", buffer->data);
+        goto failure;
+    }
+    psString fileLevel = psMetadataLookupStr(&mdstatus, md, "file_level");
+    if (!mdstatus || !fileLevel || strlen(fileLevel) == 0) {
+        psError(PS_ERR_UNEXPECTED_NULL, true, "Unable to find file_level in output from detselect.");
+        psLogMsg ("psModule.detrend", PS_LOG_ERROR, "detselect response:\n %s\n", buffer->data);
+        goto failure;
+    }
 
     results->detID = NULL; // it should be NULL already from the Alloc above
     psStringAppend (&results->detID, " -det_id %d -iteration %d ", detID, iteration);
+    results->level = psMemIncrRefCounter(fileLevel);
 
     psTrace("psModules.detrend", 5, "generated detID %s\n", results->detID);
@@ -225,5 +241,4 @@
 
     PS_ASSERT_PTR_NON_NULL(detID, NULL);
-    PS_ASSERT_PTR_NON_NULL(classID, NULL);
 
     bool status;
@@ -232,5 +247,8 @@
 
     // generate the detselect command
-    psStringAppend (&line, "detselect -select %s -class_id %s", detID, classID);
+    psStringAppend (&line, "detselect -select %s", detID);
+    if (classID && strlen(classID) > 0) {
+        psStringAppend(&line, " -class_id %s", classID);
+    }
     pmConfigDatabaseCommand(&line, config);
     pmConfigTraceCommand(&line);
Index: trunk/psModules/src/detrend/pmDetrendDB.h
===================================================================
--- trunk/psModules/src/detrend/pmDetrendDB.h	(revision 12299)
+++ trunk/psModules/src/detrend/pmDetrendDB.h	(revision 12300)
@@ -9,6 +9,6 @@
  * @author EAM, IfA
  *
- * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
- * @date $Date: 2007-02-18 21:03:20 $
+ * @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
+ * @date $Date: 2007-03-08 01:22:17 $
  * Copyright 2004-2005 Institute for Astronomy, University of Hawaii
  */
@@ -35,16 +35,15 @@
 } pmDetrendType;
 
-typedef struct
-{
-    char *camera;			// name of camera
-    char *version;			// optional version string
-    char *filter;			// name of filter
-    char *dettype;			// actual detrend type name
-    float exptime;			// exposure time (for dark, maybe flat & fringe)
-    float airmass;			// for fringe
-    float dettemp;			// for fringe
-    float twilight;			// hours (or seconds?) since/before nearest twilight
-    psTime time;			// time of input data
-    pmDetrendType type;			// type of detrend data
+typedef struct {
+    char *camera;                       // name of camera
+    char *version;                      // optional version string
+    char *filter;                       // name of filter
+    char *dettype;                      // actual detrend type name
+    float exptime;                      // exposure time (for dark, maybe flat & fringe)
+    float airmass;                      // for fringe
+    float dettemp;                      // for fringe
+    float twilight;                     // hours (or seconds?) since/before nearest twilight
+    psTime time;                        // time of input data
+    pmDetrendType type;                 // type of detrend data
 
     bool  exptimeSet;
@@ -52,20 +51,10 @@
     bool  dettempSet;
     bool  twilightSet;
-}
-pmDetrendSelectOptions;
+} pmDetrendSelectOptions;
 
-typedef struct
-{
-    char *detID;   // identifier of detrend run
-    pmFPALevel level;   // level in FPA hierarchy of individual file
-}
-pmDetrendSelectResults;
-
-typedef struct
-{
-    char *detID;   // identifier of detrend image
-    char *classID;   // level in FPA hierarchy of individual file
-}
-pmDetrendFileOptions;
+typedef struct {
+    char *detID;                        // identifier of detrend run
+    const char *level;                  // level in FPA hierarchy of individual file
+} pmDetrendSelectResults;
 
 psString pmDetrendTypeToString (pmDetrendType type);
