Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 20836)
+++ trunk/ippTools/share/Makefile.am	(revision 20841)
@@ -120,4 +120,5 @@
      magicdstool_completed_runs.sql \
      magicdstool_getskycells.sql \
+     magicdstool_getrunids.sql \
      pstamptool_addjob_otherjob.sql \
      pstamptool_addjob_stampjob.sql \
Index: trunk/ippTools/share/magicdstool_completed_runs.sql
===================================================================
--- trunk/ippTools/share/magicdstool_completed_runs.sql	(revision 20836)
+++ trunk/ippTools/share/magicdstool_completed_runs.sql	(revision 20841)
@@ -7,17 +7,5 @@
     magicDSRun.magic_ds_id
     FROM magicDSRun
-    JOIN magicRun USING (magic_id)
-    JOIN magicInputSkyfile USING(magic_id)
-    JOIN diffRun USING(diff_id)
-    JOIN diffInputSkyfile
-        ON diffInputSkyfile.diff_id = diffRun.diff_id
-        AND diffInputSkyfile.skycell_id = diffRun.skycell_id
-        -- Want input warps only AND diffInputSkyfile.warp_id IS NOT NULL
-        AND diffInputSkyfile.template = 0
-    JOIN warpRun USING(warp_id)
-    JOIN fakeRun USING(fake_id)
-    JOIN camRun USING(cam_id)
-    JOIN camProcessedExp USING(cam_id)
-    JOIN rawImfile ON magicRun.exp_id = rawImfile.exp_id
+    JOIN rawImfile ON stage_id = rawImfile.exp_id
     LEFT JOIN magicDSFile
         ON magicDSRun.magic_ds_id = magicDSFile.magic_ds_id
@@ -37,20 +25,5 @@
     magicDSRun.magic_ds_id
     FROM magicDSRun
-    JOIN magicRun USING(magic_id)
-    JOIN magicInputSkyfile USING(magic_id)
-    JOIN diffRun USING(diff_id)
-    JOIN diffInputSkyfile
-        ON diffInputSkyfile.diff_id = diffRun.diff_id
-        AND diffInputSkyfile.skycell_id = diffRun.skycell_id
-        -- Want input warps only
-        AND diffInputSkyfile.warp_id IS NOT NULL
-        AND diffInputSkyfile.template = 0
-    JOIN warpRun USING(warp_id)
-    JOIN fakeRun USING(fake_id)
-    JOIN camRun USING(cam_id)
-    JOIN camProcessedExp USING(cam_id)
-    JOIN chipRun USING(chip_id)
-    JOIN chipProcessedImfile USING(chip_id)
-
+    JOIN chipProcessedImfile ON stage_id = chip_id
     LEFT JOIN magicDSFile
         ON magicDSFile.magic_ds_id = magicDSRun.magic_ds_id
@@ -60,6 +33,6 @@
         AND magicDSRun.stage = 'chip'
     GROUP BY
-        magicDSRun.magic_ds_id,
-        chipRun.chip_id
+        magic_ds_id,
+        chip_id
     HAVING
         COUNT(chipProcessedImfile.class_id) = COUNT(magicDSFile.component)
@@ -70,15 +43,5 @@
     magicDSRun.magic_ds_id
     FROM magicDSRun
-    JOIN magicRun USING (magic_id)
-    JOIN magicInputSkyfile USING(magic_id)
-    JOIN diffRun USING(diff_id)
-    JOIN diffInputSkyfile
-        ON diffInputSkyfile.diff_id = diffRun.diff_id
-        AND diffInputSkyfile.skycell_id = diffRun.skycell_id
-        -- Want input warps only
-        AND diffInputSkyfile.warp_id IS NOT NULL
-        AND diffInputSkyfile.template = 0
-    JOIN warpSkyfile using(warp_id)
-    JOIN warpRun USING(warp_id)
+    JOIN warpSkyfile on stage_id = warp_id
     LEFT JOIN magicDSFile
         ON magicDSRun.magic_ds_id = magicDSFile.magic_ds_id
@@ -91,5 +54,5 @@
     GROUP BY
         magicDSRun.magic_ds_id,
-        warpRun.warp_id
+        warp_id
     HAVING
         COUNT(warpSkyfile.skycell_id) = COUNT(magicDSFile.component)
@@ -102,4 +65,5 @@
     JOIN magicRun USING (magic_id)
     JOIN magicInputSkyfile USING(magic_id)
+    -- Do we really need to join back to diffInputSkyfile here?
     JOIN diffRun USING(diff_id)
     JOIN diffSkyfile USING(diff_id)
Index: trunk/ippTools/share/magicdstool_todestreak.sql
===================================================================
--- trunk/ippTools/share/magicdstool_todestreak.sql	(revision 20836)
+++ trunk/ippTools/share/magicdstool_todestreak.sql	(revision 20841)
@@ -8,5 +8,5 @@
     magicMask.uri as streaks_uri,
     stage,
-    rawImfile.exp_id as stage_id,
+    stage_id,
     class_id as component,
     rawImfile.uri as uri,
@@ -20,14 +20,4 @@
 JOIN magicMask USING (magic_id)
 JOIN magicRun USING (magic_id)
-JOIN magicInputSkyfile USING(magic_id)
-JOIN diffRun USING(diff_id)
-JOIN diffInputSkyfile
-    ON diffInputSkyfile.diff_id = diffRun.diff_id
-    AND diffInputSkyfile.skycell_id = diffRun.skycell_id
-    -- Want input warps only AND diffInputSkyfile.warp_id IS NOT NULL
-    AND diffInputSkyfile.template = 0
-JOIN warpRun USING(warp_id)
-JOIN fakeRun USING(fake_id)
-JOIN camRun USING(cam_id)
 JOIN camProcessedExp USING(cam_id)
 JOIN rawImfile ON magicRun.exp_id = rawImfile.exp_id
@@ -43,10 +33,10 @@
 SELECT DISTINCT
     magicDSRun.magic_ds_id,
-    magicRun.magic_id,
-    magicRun.exp_id,
+    magicDSRun.magic_id,
+    chipRun.exp_id,
     camera,
     magicMask.uri as streaks_uri,
     stage,
-    chip_id as stage_id,
+    stage_id,
     class_id as component,
     chipProcessedImfile.uri,
@@ -59,18 +49,6 @@
 FROM magicDSRun
 JOIN magicMask USING (magic_id)
-JOIN magicRun USING (magic_id)
-JOIN magicInputSkyfile USING(magic_id)
-JOIN diffRun USING(diff_id)
-JOIN diffInputSkyfile
-    ON diffInputSkyfile.diff_id = diffRun.diff_id
-    AND diffInputSkyfile.skycell_id = diffRun.skycell_id
-    -- Want input warps only
-    AND diffInputSkyfile.warp_id IS NOT NULL
-    AND diffInputSkyfile.template = 0
-JOIN warpRun USING(warp_id)
-JOIN fakeRun USING(fake_id)
-JOIN camRun USING(cam_id)
 JOIN camProcessedExp USING(cam_id)
-JOIN chipRun USING(chip_id)
+JOIN chipRun ON chip_id = stage_id
 JOIN chipProcessedImfile USING(chip_id)
 JOIN rawExp ON chipRun.exp_id = rawExp.exp_id
@@ -93,5 +71,5 @@
     magicMask.uri as streaks_uri,
     stage,
-    warp_id as stage_id,
+    stage_id,
     warpSkyfile.skycell_id as component,
     warpSkyfile.uri,
@@ -106,12 +84,5 @@
 JOIN magicRun USING (magic_id)
 JOIN magicInputSkyfile USING(magic_id)
-JOIN diffRun USING(diff_id)
-JOIN diffInputSkyfile
-    ON diffInputSkyfile.diff_id = diffRun.diff_id
-    AND diffInputSkyfile.skycell_id = diffRun.skycell_id
-    -- Want input warps only
-    AND diffInputSkyfile.warp_id IS NOT NULL
-    AND diffInputSkyfile.template = 0
-JOIN warpSkyfile using(warp_id)
+JOIN warpSkyfile ON warp_id = stage_id
 JOIN warpRun USING(warp_id)
 JOIN rawExp ON magicRun.exp_id = rawExp.exp_id
Index: trunk/ippTools/share/pxadmin_create_tables.sql
===================================================================
--- trunk/ippTools/share/pxadmin_create_tables.sql	(revision 20836)
+++ trunk/ippTools/share/pxadmin_create_tables.sql	(revision 20841)
@@ -1064,4 +1064,6 @@
         state VARCHAR(64),
         stage VARCHAR(64),
+        stage_id BIGINT,
+        cam_id BIGINT,
         outroot VARCHAR(255),
         recoveryroot VARCHAR(255),
Index: trunk/ippTools/src/magicdstool.c
===================================================================
--- trunk/ippTools/src/magicdstool.c	(revision 20836)
+++ 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)
 {
