Index: trunk/ippTools/src/magicdstool.c
===================================================================
--- trunk/ippTools/src/magicdstool.c	(revision 20788)
+++ trunk/ippTools/src/magicdstool.c	(revision 20841)
@@ -41,4 +41,5 @@
 static bool setmagicDSRunState(pxConfig *config, psS64 magic_id, const char *state);
 static bool magicDSRunComplete(pxConfig *config);
+static bool magicDSGetIDs(pxConfig *config, psString stage, psS64 magic_id, psS64 *stage_id, psS64 *cam_id);
 
 #ifdef notdef
@@ -344,4 +345,11 @@
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
 
+    psS64 stage_id, cam_id;
+
+    if (!magicDSGetIDs(config, stage, magic_id, &stage_id, &cam_id)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to get ids");
+        return false;
+    }
+
     magicDSRunRow *run = magicDSRunRowAlloc(
             0,          // ID
@@ -349,5 +357,7 @@
             "run",      // state
             stage,
-            outroot,    
+            stage_id,
+            cam_id,
+            outroot,
             recoveryroot,
             re_place,
@@ -517,4 +527,73 @@
 }
 
+static bool magicDSGetIDs(pxConfig *config, psString stage, psS64 magic_id, psS64 *stage_id, psS64 *cam_id)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+    PS_ASSERT_PTR_NON_NULL(stage, false);
+    PS_ASSERT_PTR_NON_NULL(stage_id, false);
+    PS_ASSERT_PTR_NON_NULL(cam_id, false);
+
+    if (!strcmp(stage, "diff")) {
+        // don't need these ids for diff stage
+        *stage_id = 0;
+        *cam_id = 0;
+        return true;
+    } 
+    
+    int stageNum;
+    if (!strcmp(stage, "raw")) {
+        stageNum = 0;
+    } else if (!strcmp(stage, "chip")) {
+        stageNum = 1;
+    } else if (!strcmp(stage, "warp")) {
+        stageNum = 2;
+    } else {
+        psError(PXTOOLS_ERR_DATA, true, "%s is not a valid value for stage", stage);
+        return false;
+    }
+
+
+    psString query = pxDataGet("magicdstool_getrunids.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query, magic_id)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    }
+    psFree(query);
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psTrace("magicdstool", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
+    }
+    if (psArrayLength(output) > 1) {
+        psError(PS_ERR_UNKNOWN, true, "unexpected number of rows found %ld for magic_id %ld", 
+            psArrayLength(output), magic_id);
+        return false;
+    }
+    psMetadata *row = output->data[0];
+
+    *cam_id = psMetadataLookupS64(NULL, row, "cam_id");
+    if (stageNum == 0) {
+        *stage_id = psMetadataLookupS64(NULL, row, "exp_id");
+    } else if (stageNum == 1) {
+        *stage_id = psMetadataLookupS64(NULL, row, "chip_id");
+    } else if (stageNum == 2) {
+        *stage_id = psMetadataLookupS64(NULL, row, "warp_id");
+    }
+
+    return true;
+}
+
 static bool magicDSRunComplete(pxConfig *config)
 {
