Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 25800)
+++ trunk/ippTools/share/Makefile.am	(revision 25816)
@@ -152,4 +152,5 @@
      faketool_unmasked.sql \
      flatcorr_chiprundone.sql \
+     flatcorr_camerarundone.sql \
      flatcorr_pendingprocess.sql \
      flatcorr_inputimfile.sql \
Index: trunk/ippTools/share/addtool_find_pendingexp.sql
===================================================================
--- trunk/ippTools/share/addtool_find_pendingexp.sql	(revision 25800)
+++ trunk/ippTools/share/addtool_find_pendingexp.sql	(revision 25816)
@@ -1,4 +1,5 @@
 SELECT
     addRun.*,
+    camProcessedExp.path_base as camroot,
     rawExp.exp_tag,
     rawExp.exp_id,
@@ -9,4 +10,6 @@
 FROM addRun
 JOIN camRun
+    USING(cam_id)
+JOIN camProcessedExp
     USING(cam_id)
 JOIN chipRun
@@ -21,4 +24,6 @@
     camRun.state = 'full'
     AND ((addRun.state = 'new' AND addProcessedExp.add_id IS NULL) OR addRun.state = 'update')
+    AND addRun.dvodb IS NOT NULL
+    AND addRun.workdir IS NOT NULL
     AND addMask.label IS NULL
 
Index: trunk/ippTools/share/addtool_queue_cam_id.sql
===================================================================
--- trunk/ippTools/share/addtool_queue_cam_id.sql	(revision 25800)
+++ trunk/ippTools/share/addtool_queue_cam_id.sql	(revision 25816)
@@ -6,7 +6,8 @@
         '%s',           -- workdir
 	'%s',           -- workdir_state
+        '%s',           -- reduction
         '%s',           -- label
         '%s',           -- dvodb 
-	0               -- magicked
+	%d		-- image_only
     FROM camRun
     WHERE
Index: trunk/ippTools/share/flatcorr_camerarundone.sql
===================================================================
--- trunk/ippTools/share/flatcorr_camerarundone.sql	(revision 25816)
+++ trunk/ippTools/share/flatcorr_camerarundone.sql	(revision 25816)
@@ -0,0 +1,15 @@
+SELECT
+    corr_id,
+    camRun.*
+FROM flatcorrRun
+JOIN flatcorrCamLink
+    USING(corr_id)
+JOIN camRun
+    USING(cam_id)
+LEFT JOIN addRun
+    ON camRun.cam_id = addRun.cam_id
+WHERE
+    flatcorrRun.state = 'new'
+    AND flatcorrCamLink.include = 1
+    AND camRun.state = 'full'
+    AND addRun.cam_id is NULL
Index: trunk/ippTools/share/flatcorr_chiprundone.sql
===================================================================
--- trunk/ippTools/share/flatcorr_chiprundone.sql	(revision 25800)
+++ trunk/ippTools/share/flatcorr_chiprundone.sql	(revision 25816)
@@ -1,5 +1,4 @@
 SELECT
     corr_id,
-    cam_id,
     chipRun.*
 FROM flatcorrRun
@@ -14,3 +13,3 @@
     AND flatcorrChipLink.include = 1
     AND chipRun.state = 'full'
-    AND camRun.cam_id is NULL;
+    AND camRun.cam_id is NULL
Index: trunk/ippTools/share/flatcorr_pendingprocess.sql
===================================================================
--- trunk/ippTools/share/flatcorr_pendingprocess.sql	(revision 25800)
+++ trunk/ippTools/share/flatcorr_pendingprocess.sql	(revision 25816)
@@ -10,29 +10,29 @@
   reduction,
   region,
-  chip_count,
-  cam_count
+  cam_count,
+  add_count
 FROM 
   (SELECT 
      flatcorrRun.*,
-     flatcorrChipLink.corr_id as chip_corr_id,
-     count(flatcorrChipLink.chip_id) as chip_count
+     flatcorrCamLink.corr_id as cam_corr_id,
+     count(flatcorrCamLink.cam_id) as cam_count
    FROM flatcorrRun
-   JOIN flatcorrChipLink
-     ON flatcorrChipLink.corr_id = flatcorrRun.corr_id
-  WHERE flatcorrChipLink.include = 1
+   JOIN flatcorrCamLink
+  USING (corr_id)
+  WHERE flatcorrCamLink.include = 1
    GROUP BY
-     flatcorrChipLink.corr_id) AS t1
+     flatcorrCamLink.corr_id) AS t1
 LEFT JOIN
   (SELECT 
-     flatcorrCamLink.corr_id as cam_corr_id,
-     count(flatcorrCamLink.cam_id) as cam_count
-   FROM flatcorrCamLink
-   JOIN camRun
-       ON flatcorrCamLink.cam_id = camRun.cam_id
-   WHERE camRun.state = 'full'
-     AND flatcorrCamLink.include = 1
+     flatcorrAddstarLink.corr_id as add_corr_id,
+     count(flatcorrAddstarLink.add_id) as add_count
+   FROM flatcorrAddstarLink
+   JOIN addRun
+   USING (add_id)
+   WHERE addRun.state = 'full'
+     AND flatcorrAddstarLink.include = 1
    GROUP BY
-       flatcorrCamLink.corr_id) AS t2
-ON t1.chip_corr_id = t2.cam_corr_id
-WHERE chip_count = cam_count
+       flatcorrAddstarLink.corr_id) AS t2
+ON t1.cam_corr_id = t2.add_corr_id
+WHERE cam_count = add_count
 AND state = 'new'
Index: trunk/ippTools/share/pxadmin_create_tables.sql
===================================================================
--- trunk/ippTools/share/pxadmin_create_tables.sql	(revision 25800)
+++ trunk/ippTools/share/pxadmin_create_tables.sql	(revision 25816)
@@ -464,7 +464,8 @@
     workdir VARCHAR(255),
     workdir_state VARCHAR(64),
+    reduction VARCHAR(64),
     label VARCHAR(64),
     dvodb VARCHAR(255),
-    magicked BIGINT,
+    image_only TINYINT,
     PRIMARY KEY(add_id),
     KEY(add_id),
@@ -480,5 +481,4 @@
     add_id BIGINT AUTO_INCREMENT,
     dtime_addstar FLOAT,
-    n_stars INT,
     path_base VARCHAR(255),
     fault SMALLINT NOT NULL,
@@ -1182,5 +1182,5 @@
 ) ENGINE=innodb DEFAULT CHARSET=latin1;
 
--- these two tables link the flatcorrRun to the associated chip and camera runs
+-- these three tables link the flatcorrRun to the associated chip, camera, and addstar runs
 CREATE TABLE flatcorrChipLink (
         corr_id BIGINT,
@@ -1201,4 +1201,15 @@
         FOREIGN KEY(chip_id) REFERENCES chipRun(chip_id),
         FOREIGN KEY(cam_id) REFERENCES camRun(cam_id)
+) ENGINE=innodb DEFAULT CHARSET=latin1;
+
+CREATE TABLE flatcorrAddstarLink (
+        corr_id BIGINT,
+        cam_id BIGINT,
+        add_id BIGINT,
+        include TINYINT,
+        PRIMARY KEY(corr_id, cam_id, add_id),
+        FOREIGN KEY (corr_id)  REFERENCES  flatcorrRun(corr_id),
+        FOREIGN KEY (cam_id)  REFERENCES  camRun(cam_id),
+        FOREIGN KEY (add_id)  REFERENCES  addRun(add_id)
 ) ENGINE=innodb DEFAULT CHARSET=latin1;
 
Index: trunk/ippTools/share/pxadmin_drop_tables.sql
===================================================================
--- trunk/ippTools/share/pxadmin_drop_tables.sql	(revision 25800)
+++ trunk/ippTools/share/pxadmin_drop_tables.sql	(revision 25816)
@@ -59,4 +59,5 @@
 DROP TABLE IF EXISTS flatcorrChipLink;
 DROP TABLE IF EXISTS flatcorrCamLink;
+DROP TABLE IF EXISTS flatcorrAddstarLink;
 DROP TABLE IF EXISTS pstampDataStore;
 DROP TABLE IF EXISTS pstampProject;
Index: trunk/ippTools/src/addtool.c
===================================================================
--- trunk/ippTools/src/addtool.c	(revision 25800)
+++ trunk/ippTools/src/addtool.c	(revision 25816)
@@ -40,9 +40,4 @@
 static bool maskedMode(pxConfig *config);
 static bool unblockMode(pxConfig *config);
-static bool pendingcleanuprunMode(pxConfig *config);
-static bool pendingcleanupexpMode(pxConfig *config);
-static bool donecleanupMode(pxConfig *config);
-static bool exportrunMode(pxConfig *config);
-static bool importrunMode(pxConfig *config);
 
 # define MODECASE(caseName, func) \
@@ -74,9 +69,4 @@
         MODECASE(ADDTOOL_MODE_MASKED,               maskedMode);
         MODECASE(ADDTOOL_MODE_UNBLOCK,              unblockMode);
-        MODECASE(ADDTOOL_MODE_PENDINGCLEANUPRUN,    pendingcleanuprunMode);
-        MODECASE(ADDTOOL_MODE_PENDINGCLEANUPEXP,    pendingcleanupexpMode);
-        MODECASE(ADDTOOL_MODE_DONECLEANUP,          donecleanupMode);
-        MODECASE(ADDTOOL_MODE_EXPORTRUN,            exportrunMode);
-        MODECASE(ADDTOOL_MODE_IMPORTRUN,            importrunMode);
         default:
             psAbort("invalid option (this should not happen)");
@@ -118,10 +108,11 @@
     }
 
-    PXOPT_LOOKUP_STR(workdir, config->args, "-set_workdir", false, false);
-    PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
-    PXOPT_LOOKUP_STR(reduction, config->args, "-set_reduction", false, false);
-    PXOPT_LOOKUP_STR(dvodb, config->args, "-set_dvodb", false, false);
-    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
-    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+    PXOPT_LOOKUP_STR(workdir,     config->args, "-set_workdir", false, false);
+    PXOPT_LOOKUP_STR(dvodb,       config->args, "-set_dvodb", false, false);
+    PXOPT_LOOKUP_STR(label,       config->args, "-set_label", false, false);
+    PXOPT_LOOKUP_STR(reduction,   config->args, "-set_reduction", false, false);
+    PXOPT_LOOKUP_BOOL(image_only, config->args, "-image_only", false);
+    PXOPT_LOOKUP_BOOL(pretend,    config->args, "-pretend", false);
+    PXOPT_LOOKUP_BOOL(simple,     config->args, "-simple", false);
 
     // find the cam_id of all the exposures that we want to queue up.
@@ -170,4 +161,29 @@
     }
 
+    // loop over our list of camRun rows to check the supplied and selected dvodb and workdir values:
+    for (long i = 0; i < psArrayLength(output); i++) {
+        psMetadata *md = output->data[i];
+
+        camRunRow *row = camRunObjectFromMetadata(md);
+        if (!row) {
+            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
+            psFree(output);
+            return false;
+        }
+
+        if (!dvodb && !row->dvodb) {
+            psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, cam_id %" PRId64, row->label, row->cam_id);
+            psFree(output);
+            return false;
+        }
+        if (!workdir && !row->workdir) {
+            psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined workdir: label: %s, cam_id %" PRId64, row->label, row->cam_id);
+            psFree(output);
+            return false;
+        }
+
+        psFree(row);
+    }
+
     // start a transaction so we don't end up with an exp without any associted
     // imfiles
@@ -183,6 +199,5 @@
     // old values in place (i.e., passing the values through).
 
-
-    // loop over our list of addRun rows
+    // loop over our list of camRun rows
     for (long i = 0; i < psArrayLength(output); i++) {
         psMetadata *md = output->data[i];
@@ -190,5 +205,5 @@
         camRunRow *row = camRunObjectFromMetadata(md);
         if (!row) {
-            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into addRun");
+            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
             psFree(output);
             return false;
@@ -199,8 +214,8 @@
 			       row->cam_id,
 			       workdir     ? workdir   : row->workdir,
+			       reduction   ? reduction : row->reduction,
 			       label       ? label     : row->label,
-			       "RECIPE",
-			       dvodb       ? dvodb     : row->dvodb
-			       
+			       dvodb       ? dvodb     : row->dvodb,
+			       image_only
         )) {
             if (!psDBRollback(config->dbh)) {
@@ -234,5 +249,4 @@
     PXOPT_COPY_S64(config->args, where, "-cam_id",    "camRun.cam_id", "==");
     pxcamGetSearchArgs (config, where); // most search arguments based on camera
-
     PXOPT_COPY_STR(config->args, where, "-label",     "addRun.label", "==");
     PXOPT_COPY_STR(config->args, where, "-state",     "addRun.state", "==");
@@ -287,5 +301,4 @@
     pxAddLabelSearchArgs (config, where, "-label", "addRun.label", "==");
     PXOPT_COPY_STR(config->args, where, "-reduction", "addRun.reduction", "==");
-
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
@@ -349,8 +362,7 @@
 
     // optional
-    PXOPT_LOOKUP_F32(dtime_addstar, config->args,  "-dtime_addstar", false, false);
-    PXOPT_LOOKUP_S32(n_stars, config->args,        "-n_stars", false, false);
-    PXOPT_LOOKUP_STR(path_base, config->args, "-path_base", false, false);
-    PXOPT_LOOKUP_S64(magicked, config->args, "-magicked", false, false);
+    PXOPT_LOOKUP_STR(path_base,     config->args, "-path_base", false, false);
+    PXOPT_LOOKUP_F32(dtime_addstar, config->args, "-dtime_addstar", false, false);
+    PXOPT_LOOKUP_S16(fault,         config->args, "-fault", false, false);
 
     // generate restrictions
@@ -401,7 +413,6 @@
         pendingRow->add_id,
         dtime_addstar,
-        n_stars,
         path_base,
-	0
+	fault
         );
 
@@ -418,5 +429,5 @@
 
     // since there is only one exp per 'new' set addRun.state = 'full'
-    if (!pxaddRunSetState(config, row->add_id, "full", magicked)) {
+    if (!pxaddRunSetState(config, row->add_id, "full")) {
         psError(PS_ERR_UNKNOWN, false, "failed to change addRun.state for add_id: %" PRId64, row->add_id);
         psFree(row);
@@ -439,8 +450,4 @@
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-
-    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
-    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-    PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
 
     // generate restrictions
@@ -451,4 +458,8 @@
     pxAddLabelSearchArgs (config, where, "-label",    "addRun.label",     "==");
     PXOPT_COPY_STR(config->args, where, "-reduction", "addRun.reduction", "==");
+
+    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
+    PXOPT_LOOKUP_BOOL(faulted, config->args, "-faulted", false);
 
     if (!psListLength(where->list) &&
@@ -628,9 +639,8 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
-
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_S64(config->args, where, "-add_id",   "add_id",   "==");
     PXOPT_COPY_S64(config->args, where, "-cam_id",  "cam_id",  "==");
+    PXOPT_LOOKUP_S16(fault, config->args, "-fault", true, false);
 
     if (!pxSetFaultCode(config->dbh, "addProcessedExp", where, fault)) {
@@ -713,324 +723,2 @@
     return true;
 }
-
-static bool pendingcleanuprunMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
-    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-
-    psMetadata *where = psMetadataAlloc();
-    pxAddLabelSearchArgs (config, where, "-label", "addRun.label", "==");
-
-    psString query = pxDataGet("addtool_pendingcleanuprun.sql");
-    if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        return false;
-    }
-
-    if (where && psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-    psFree(where);
-
-    // treat limit == 0 as "no limit"
-    if (limit) {
-        psString limitString = psDBGenerateLimitSQL(limit);
-        psStringAppend(&query, " %s", limitString);
-        psFree(limitString);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        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("addtool", PS_LOG_INFO, "no rows found");
-        psFree(output);
-        return true;
-    }
-
-    // negative simple so the default is true
-    if (!ippdbPrintMetadatas(stdout, output, "addPendingCleanupRun", !simple)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
-        psFree(output);
-        return false;
-    }
-
-    psFree(output);
-
-    return true;
-}
-
-
-static bool pendingcleanupexpMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-    PXOPT_LOOKUP_S64(add_id, config->args, "-add_id", false, false);
-    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
-    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-
-    psMetadata *where = psMetadataAlloc();
-    if (add_id) {
-        PXOPT_COPY_S64(config->args, where, "-add_id", "add_id", "==");
-    }
-    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
-
-    psString query = pxDataGet("addtool_pendingcleanupexp.sql");
-    if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        return false;
-    }
-
-    if (where && psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-    psFree(where);
-
-    // treat limit == 0 as "no limit"
-    if (limit) {
-        psString limitString = psDBGenerateLimitSQL(limit);
-        psStringAppend(&query, " %s", limitString);
-        psFree(limitString);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        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("chiptool", PS_LOG_INFO, "no rows found");
-        psFree(output);
-        return true;
-    }
-
-    // negative simple so the default is true
-    if (!ippdbPrintMetadatas(stdout, output, "addPendingCleanupExp", !simple)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
-        psFree(output);
-        return false;
-    }
-
-    psFree(output);
-
-    return true;
-}
-
-
-static bool donecleanupMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-    PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
-    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-
-    psMetadata *where = psMetadataAlloc();
-    PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
-
-    psString query = pxDataGet("addtool_donecleanup.sql");
-    if (!query) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        return false;
-    }
-
-    if (where && psListLength(where->list)) {
-        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-        psStringAppend(&query, " AND %s", whereClause);
-        psFree(whereClause);
-    }
-    psFree(where);
-
-    // treat limit == 0 as "no limit"
-    if (limit) {
-        psString limitString = psDBGenerateLimitSQL(limit);
-        psStringAppend(&query, " %s", limitString);
-        psFree(limitString);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-        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("addtool", PS_LOG_INFO, "no rows found");
-        psFree(output);
-        return true;
-    }
-
-    // negative simple so the default is true
-    if (!ippdbPrintMetadatas(stdout, output, "addDoneCleanup", !simple)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to print array");
-        psFree(output);
-        return false;
-    }
-
-    psFree(output);
-
-    return true;
-}
-
-bool exportrunMode(pxConfig *config)
-{
-  typedef struct ExportTable {
-    char tableName[80];
-    char sqlFilename[80];
-  } ExportTable;
-
-  int numExportTables = 2;
-
-  PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-  PXOPT_LOOKUP_S64(det_id, config->args, "-add_id", true,  false);
-  PXOPT_LOOKUP_STR(outfile, config->args, "-outfile", true,  false);
-  PXOPT_LOOKUP_U64(limit,   config->args, "-limit",   false, false);
-  PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
-
-  FILE *f = fopen (outfile, "w");
-  if (f == NULL) {
-    psError(PS_ERR_UNKNOWN, false, "failed to open output file");
-    return false;
-  }
-
-  psMetadata *where = psMetadataAlloc();
-  PXOPT_COPY_S64(config->args, where, "-add_id", "add_id", "==");
-
-  ExportTable tables [] = {
-    {"addRun", "addtool_export_run.sql"},
-    {"addProcessedExp", "addtool_export_processed_exp.sql"},
-  };
-
-  for (int i=0; i < numExportTables; i++) {
-    psString query = pxDataGet(tables[i].sqlFilename);
-    if (!query) {
-      psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-      return false;
-    }
-
-    if (where && psListLength(where->list)) {
-      psString whereClause = psDBGenerateWhereSQL(where, NULL);
-      psStringAppend(&query, " %s", whereClause);
-      psFree(whereClause);
-    }
-
-    // treat limit == 0 as "no limit"
-    if (limit) {
-      psString limitString = psDBGenerateLimitSQL(limit);
-      psStringAppend(&query, " %s", limitString);
-      psFree(limitString);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-      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)) {
-      psError(PS_ERR_UNKNOWN, true, "no rows found");
-      psFree(output);
-      return false;
-    }
-
-    if (clean) {
-        if (!strcmp(tables[i].tableName, "addRun")) {
-            if (!pxSetStateCleaned("addRun", "state", output)) {
-                psFree(output);
-                psError(PS_ERR_UNKNOWN, false, "pxSetStateClean failed for table %s",  tables[i].tableName);
-                return false;
-            }
-        }
-    }
-
-    // we must write the export table in non-simple (true) format
-    if (!ippdbPrintMetadatas(f, output, tables[i].tableName, true)) {
-      psError(PS_ERR_UNKNOWN, false, "failed to print array");
-      psFree(output);
-      return false;
-    }
-    psFree(output);
-  }
-
-  fclose (f);
-
-  return true;
-}
-
-bool importrunMode(pxConfig *config)
-{
-  unsigned int nFail;
-
-  PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-  PXOPT_LOOKUP_STR(infile, config->args, "-infile", true,  false);
-
-  psMetadata *input = psMetadataConfigRead (NULL, &nFail, infile, false);
-
-  fprintf (stdout, "---- input ----\n");
-  psMetadataPrint (stderr, input, 1);
-
-  psMetadataItem *item = psMetadataLookup (input, "addRun");
-  psAssert (item, "entry not in input?");
-  psAssert (item->type == PS_DATA_METADATA_MULTI, "entry not multi?");
-
-  psMetadataItem *entry = psListGet (item->data.list, 0);
-  assert (entry);
-  assert (entry->type == PS_DATA_METADATA);
-  addRunRow *addRun = addRunObjectFromMetadata (entry->data.md);
-  addRunInsertObject (config->dbh, addRun);
-
-  // fprintf (stdout, "---- add run ----\n");
-  // psMetadataPrint (stderr, entry->data.md, 1);
-
-  item = psMetadataLookup (input, "addProcessedExp");
-  psAssert (item, "entry not in input?");
-  psAssert (item->type == PS_DATA_METADATA_MULTI, "entry not multi?");
-
-  for (int i = 0; i < item->data.list->n; i++) {
-    psMetadataItem *entry = psListGet (item->data.list, i);
-    assert (entry);
-    assert (entry->type == PS_DATA_METADATA);
-    addProcessedExpRow *addProcessedExp = addProcessedExpObjectFromMetadata (entry->data.md);
-    addProcessedExpInsertObject (config->dbh, addProcessedExp);
-
-    // fprintf (stdout, "---- row %d ----\n", i);
-    // psMetadataPrint (stderr, entry->data.md, 1);
-  }
-
-  return true;
-}
Index: trunk/ippTools/src/addtoolConfig.c
===================================================================
--- trunk/ippTools/src/addtoolConfig.c	(revision 25800)
+++ trunk/ippTools/src/addtoolConfig.c	(revision 25816)
@@ -58,7 +58,8 @@
     psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-set_reduction",      0, "define reduction class", NULL);
     psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-set_dvodb",          0, "define DVO db", NULL);
+    psMetadataAddBool(definebyqueryArgs, PS_LIST_TAIL, "-image_only",        0, "addstar image metadata but not detections", false);
     psMetadataAddBool(definebyqueryArgs, PS_LIST_TAIL, "-pretend",           0, "do not actually modify the database", false);
+    psMetadataAddBool(definebyqueryArgs, PS_LIST_TAIL, "-simple",            0, "use the simple output format", false);
     psMetadataAddBool(definebyqueryArgs, PS_LIST_TAIL, "-all",               0, "allow everything to be queued without search terms", false);
-    psMetadataAddBool(definebyqueryArgs, PS_LIST_TAIL, "-simple",            0, "use the simple output format", false);
 
     // -updaterun
@@ -87,8 +88,7 @@
     psMetadata *addprocessedexpArgs = psMetadataAlloc();
     psMetadataAddS64(addprocessedexpArgs, PS_LIST_TAIL, "-add_id", 0,            "define addtool ID (required)", 0);
+    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-path_base", 0,            "define base output location", NULL);
     psMetadataAddF32(addprocessedexpArgs, PS_LIST_TAIL, "-dtime_addstar", 0, "define elapsed time for DVO insertion (seconds)", NAN);
-    psMetadataAddS32(addprocessedexpArgs, PS_LIST_TAIL, "-n_stars", 0,            "define number of stars", 0);
-    psMetadataAddStr(addprocessedexpArgs, PS_LIST_TAIL, "-path_base", 0,            "define base output location", NULL);
-    psMetadataAddS64(addprocessedexpArgs, PS_LIST_TAIL, "-magicked", 0,             "set magicked", 0);
+    psMetadataAddS16(addprocessedexpArgs, PS_LIST_TAIL, "-fault",  0,            "set fault code", 0);
 
     // -processedexp
@@ -101,6 +101,7 @@
 
     psMetadataAddU64(processedexpArgs, PS_LIST_TAIL, "-limit",    0,            "limit result set to N items", 0);
+    psMetadataAddBool(processedexpArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
+    psMetadataAddBool(processedexpArgs, PS_LIST_TAIL, "-faulted", 0,            "only return imfiles with a fault status set", false);
     psMetadataAddBool(processedexpArgs, PS_LIST_TAIL, "-all",     0,            "list everything without restriction", false);
-    psMetadataAddBool(processedexpArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
 
     // -revertprocessedexp
@@ -118,4 +119,5 @@
     psMetadataAddS64(updateprocessedexpArgs, PS_LIST_TAIL, "-add_id", 0,            "search by addtool ID", 0);
     psMetadataAddS64(updateprocessedexpArgs, PS_LIST_TAIL, "-cam_id",  0,            "search by camtool ID", 0);
+    psMetadataAddS16(updateprocessedexpArgs, PS_LIST_TAIL, "-fault",  0,            "set fault code", 0);
 
     // -block
@@ -130,35 +132,4 @@
     psMetadata *unblockArgs = psMetadataAlloc();
     psMetadataAddStr(unblockArgs, PS_LIST_TAIL, "-label",  0,            "name of a label to unmask (required)", NULL);
-
-    // -pendingcleanuprun
-    psMetadata *pendingcleanuprunArgs = psMetadataAlloc();
-    psMetadataAddStr(pendingcleanuprunArgs, PS_LIST_TAIL, "-label", PS_META_DUPLICATE_OK, "list blocks for specified label", NULL);
-    psMetadataAddBool(pendingcleanuprunArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
-    psMetadataAddU64(pendingcleanuprunArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
-
-    // -pendingcleanupexp
-    psMetadata *pendingcleanupexpArgs = psMetadataAlloc();
-    psMetadataAddStr(pendingcleanupexpArgs, PS_LIST_TAIL, "-label", PS_META_DUPLICATE_OK, "list blocks for specified label", NULL);
-    psMetadataAddS64(pendingcleanupexpArgs, PS_LIST_TAIL, "-add_id", 0,            "search by addstar ID", 0);
-    psMetadataAddBool(pendingcleanupexpArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
-    psMetadataAddU64(pendingcleanupexpArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
-
-    // -donecleanup
-    psMetadata *donecleanupArgs = psMetadataAlloc();
-    psMetadataAddStr(donecleanupArgs, PS_LIST_TAIL, "-label",  0,            "list blocks for specified label", NULL);
-    psMetadataAddBool(donecleanupArgs, PS_LIST_TAIL, "-simple",  0,            "use the simple output format", false);
-    psMetadataAddU64(donecleanupArgs, PS_LIST_TAIL, "-limit",  0,            "limit result set to N items", 0);
-
-    // -exportrun
-    psMetadata *exportrunArgs = psMetadataAlloc();
-    psMetadataAddS64(exportrunArgs, PS_LIST_TAIL, "-add_id", 0,          "export this addstar ID (required)", 0);
-    psMetadataAddStr(exportrunArgs, PS_LIST_TAIL, "-outfile", 0,          "export to this file (required)", NULL);
-    psMetadataAddU64(exportrunArgs, PS_LIST_TAIL, "-limit",   0,          "limit result set to N items", 0);
-    psMetadataAddBool(exportrunArgs, PS_LIST_TAIL, "-clean",  0,          "export tables as cleaned", false);
-
-    // -importrun
-    psMetadata *importrunArgs = psMetadataAlloc();
-    psMetadataAddStr(importrunArgs, PS_LIST_TAIL, "-infile",  0,          "import from this file (required)", NULL);
-
 
     psMetadata *argSets = psMetadataAlloc();
@@ -175,9 +146,4 @@
     PXOPT_ADD_MODE("-masked",               "show blocked labels",                  ADDTOOL_MODE_MASKED,        maskedArgs);
     PXOPT_ADD_MODE("-unblock",              "remove a label block",                 ADDTOOL_MODE_UNBLOCK,       unblockArgs);
-    PXOPT_ADD_MODE("-pendingcleanuprun",    "show runs that need to be cleaned up", ADDTOOL_MODE_PENDINGCLEANUPRUN, pendingcleanuprunArgs);
-    PXOPT_ADD_MODE("-pendingcleanupexp",    "show exps for cleanup runs",           ADDTOOL_MODE_PENDINGCLEANUPEXP, pendingcleanupexpArgs);
-    PXOPT_ADD_MODE("-donecleanup",          "show runs that have been cleaned",     ADDTOOL_MODE_DONECLEANUP,       donecleanupArgs);
-    PXOPT_ADD_MODE("-exportrun",            "export run for import on other database", ADDTOOL_MODE_EXPORTRUN, exportrunArgs);
-    PXOPT_ADD_MODE("-importrun",            "import run from metadata file",           ADDTOOL_MODE_IMPORTRUN, importrunArgs);
 
     if (!pxGetOptions(stderr, argc, argv, config, modes, argSets)) {
Index: trunk/ippTools/src/flatcorr.c
===================================================================
--- trunk/ippTools/src/flatcorr.c	(revision 25800)
+++ trunk/ippTools/src/flatcorr.c	(revision 25816)
@@ -33,4 +33,6 @@
 #include "pxtools.h"
 #include "pxchip.h"
+#include "pxcam.h"
+#include "pxadd.h"
 #include "flatcorr.h"
 
@@ -38,8 +40,9 @@
 static bool definerunMode(pxConfig *config);
 static bool addchipMode(pxConfig *config);
+static bool dropchipMode(pxConfig *config);
 static bool addcameraMode(pxConfig *config);
+static bool dropcameraMode(pxConfig *config);
 static bool advancecameraMode(pxConfig *config);
-static bool dropchipMode(pxConfig *config);
-static bool dropcameraMode(pxConfig *config);
+static bool advanceaddstarMode(pxConfig *config);
 static bool pendingprocessMode(pxConfig *config);
 static bool addprocessMode(pxConfig *config);
@@ -47,6 +50,4 @@
 static bool inputexpMode(pxConfig *config);
 static bool inputimfileMode(pxConfig *config);
-static bool exportrunMode(pxConfig *config);
-static bool importrunMode(pxConfig *config);
 
 static bool setflatcorrRunState(pxConfig *config, psS64 corr_id, const char *state);
@@ -73,8 +74,9 @@
         MODECASE(FLATCORR_MODE_DEFINERUN,      definerunMode);
         MODECASE(FLATCORR_MODE_ADDCHIP,        addchipMode);
+        MODECASE(FLATCORR_MODE_DROPCHIP,       dropchipMode);
         MODECASE(FLATCORR_MODE_ADDCAMERA,      addcameraMode);
+        MODECASE(FLATCORR_MODE_DROPCAMERA,     dropcameraMode);
         MODECASE(FLATCORR_MODE_ADVANCECAMERA,  advancecameraMode);
-        MODECASE(FLATCORR_MODE_DROPCHIP,       dropchipMode);
-        MODECASE(FLATCORR_MODE_DROPCAMERA,     dropcameraMode);
+        MODECASE(FLATCORR_MODE_ADVANCEADDSTAR, advanceaddstarMode);
         MODECASE(FLATCORR_MODE_PENDINGPROCESS, pendingprocessMode);
         MODECASE(FLATCORR_MODE_ADDPROCESS,     addprocessMode);
@@ -82,6 +84,4 @@
         MODECASE(FLATCORR_MODE_INPUTEXP,       inputexpMode);
         MODECASE(FLATCORR_MODE_INPUTIMFILE,    inputimfileMode);
-        MODECASE(FLATCORR_MODE_EXPORTRUN,      exportrunMode);
-        MODECASE(FLATCORR_MODE_IMPORTRUN,      importrunMode);
         default:
             psAbort("invalid option (this should not happen)");
@@ -404,4 +404,28 @@
 }
 
+static bool dropcameraMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    // required
+    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
+    PXOPT_LOOKUP_S64(cam_id, config->args, "-cam_id", true, false);
+
+    // UPDATE flatcorrCamLink set include = 0 where corr_id = %lld AND cam_id = %lld
+    psString query = pxDataGet("flatcorr_dropcamera.sql");
+    if (!query) {
+	psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+	return false;
+    }
+
+    if (!p_psDBRunQueryF(config->dbh, query, (long long) corr_id, (long long) cam_id)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    }
+
+    return true;
+}
+
 // select the flatcorr chip runs that have completed and for which there is no camera entry
 // queue a new camera run for them
@@ -413,4 +437,7 @@
     PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
     PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
+
+    psMetadata *where = psMetadataAlloc();
+    pxAddLabelSearchArgs (config, where, "-label", "flatcorrRun.label", "==");
 
     psString query = pxDataGet("flatcorr_chiprundone.sql");
@@ -419,4 +446,11 @@
 	return false;
     }
+
+    if (where && psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
 
     // treat limit == 0 as "no limit"
@@ -528,31 +562,7 @@
 }
 
-static bool dropcameraMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    // required
-    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
-    PXOPT_LOOKUP_S64(cam_id, config->args, "-cam_id", true, false);
-
-    // UPDATE flatcorrCamLink set include = 0 where corr_id = %lld AND cam_id = %lld
-    psString query = pxDataGet("flatcorr_dropcamera.sql");
-    if (!query) {
-	psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-	return false;
-    }
-
-    if (!p_psDBRunQueryF(config->dbh, query, (long long) corr_id, (long long) cam_id)) {
-        psError(PS_ERR_UNKNOWN, false, "database error");
-        psFree(query);
-        return false;
-    }
-
-    return true;
-}
-
-// select the flatcorr chip runs that have completed and for which there is no camera entry
-// queue a new camera run for them
-static bool pendingprocessMode(pxConfig *config)
+// Select the flatcorr camera runs that have completed and for which there is no addstar
+// entry.  Queue a new addstar run for them
+static bool advanceaddstarMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -560,9 +570,10 @@
     PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
     PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
+    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
 
     psMetadata *where = psMetadataAlloc();
-    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
-
-    psString query = pxDataGet("flatcorr_pendingprocess.sql");
+    pxAddLabelSearchArgs (config, where, "-label", "flatcorrRun.label", "==");
+
+    psString query = pxDataGet("flatcorr_camerarundone.sql");
     if (!query) {
 	psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
@@ -601,8 +612,175 @@
     }
 
+    if (pretend) {
+        // negative simple so the default is true
+        if (!ippdbPrintMetadatas(stdout, output, "flatcorr_addcamera", !simple)) {
+            psError(PS_ERR_UNKNOWN, false, "failed to print array");
+            psFree(output);
+            return false;
+        }
+    }
+
+    // start a transaction so we don't end up with an exp without any associted
+    // imfiles
+    if (!psDBTransaction(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(output);
+        return false;
+    }
+
+    // loop over our list of chipRun rows
+    for (long i = 0; i < psArrayLength(output); i++) {
+        psMetadata *md = output->data[i];
+
+        bool status;
+        psS64 corr_id = psMetadataLookupS64(&status, md, "corr_id");
+        if (!status) {
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for corr_id");
+            psFree(output);
+            return false;
+        }
+
+        camRunRow *row = camRunObjectFromMetadata(md);
+        if (!row) {
+            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipRun");
+            psFree(output);
+            return false;
+        }
+
+        // queue the exp : force image_only to be false (flatcorr is meaningless with just image info)
+        if (!pxaddQueueByCamID(
+		config,
+		row->cam_id,
+		row->workdir,
+		row->reduction,
+		row->label,
+		row->dvodb,
+		0)) {
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false,
+                    "failed to trying to queue cam_id: %" PRId64, row->cam_id);
+            psFree(row);
+            psFree(output);
+            return false;
+        }
+
+	// figure out the ID of the flatcorrRun we just created
+	psS64 add_id = psDBLastInsertID(config->dbh);
+
+	// add the addRun entry to the flatcorrAddstarLink table (include is TRUE)
+        if (!flatcorrAddstarLinkInsert(config->dbh, corr_id, row->cam_id, add_id, 1)) {
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+        psFree(row);
+    }
+    psFree(output);
+
+    if (!psDBCommit(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    return false;
+}
+
+// select the flatcorr chip runs that have completed and for which there is no camera entry
+// queue a new camera run for them
+static bool pendingprocessMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
+    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
+
+    psMetadata *where = psMetadataAlloc();
+    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
+
+    psString query = pxDataGet("flatcorr_pendingprocess.sql");
+    if (!query) {
+	psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+	return false;
+    }
+
+    if (where && psListLength(where->list)) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+    psFree(where);
+
+    // treat limit == 0 as "no limit"
+    if (limit) {
+	psString limitString = psDBGenerateLimitSQL(limit);
+	psStringAppend(&query, " %s", limitString);
+	psFree(limitString);
+    }
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    }
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        psTrace("flatcorr", PS_LOG_INFO, "no rows found");
+        psFree(output);
+        return true;
+    }
+
     if (!ippdbPrintMetadatas(stdout, output, "flatcorrPending", !simple)) {
 	psError(PS_ERR_UNKNOWN, false, "failed to print array");
 	psFree(output);
 	return false;
+    }
+
+    return true;
+}
+
+// XXX need a fault state
+static bool addprocessMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
+    PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
+    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
+
+    char *query = "UPDATE flatcorrRun SET state = 'full', hostname = '%s', fault = '%hd' WHERE corr_id = %" PRId64;
+
+    if (!p_psDBRunQueryF(config->dbh, query, hostname, code, corr_id)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to change state for corr_id %" PRId64, corr_id);
+        return false;
+    }
+
+    return true;
+}
+
+static bool updaterunMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
+    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
+    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
+
+    if (!setflatcorrRunState(config, corr_id, state)) {
+        if (!psDBRollback(config->dbh)) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+        }
+        psError(PS_ERR_UNKNOWN, false, "failed to set run state");
+        return false;
     }
 
@@ -714,41 +892,4 @@
 }
 
-// XXX need a fault state
-static bool addprocessMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
-    PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
-    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
-
-    char *query = "UPDATE flatcorrRun SET state = 'full', hostname = '%s', fault = '%hd' WHERE corr_id = %" PRId64;
-
-    if (!p_psDBRunQueryF(config->dbh, query, hostname, code, corr_id)) {
-        psError(PS_ERR_UNKNOWN, false, "failed to change state for corr_id %" PRId64, corr_id);
-        return false;
-    }
-
-    return true;
-}
-
-static bool updaterunMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
-    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
-
-    if (!setflatcorrRunState(config, corr_id, state)) {
-        if (!psDBRollback(config->dbh)) {
-            psError(PS_ERR_UNKNOWN, false, "database error");
-        }
-        psError(PS_ERR_UNKNOWN, false, "failed to set run state");
-        return false;
-    }
-
-    return true;
-}
-
 static bool setflatcorrRunState(pxConfig *config, psS64 corr_id, const char *state)
 {
@@ -774,144 +915,2 @@
     return true;
 }
-
-bool exportrunMode(pxConfig *config)
-{
-  typedef struct ExportTable {
-    char tableName[80];
-    char sqlFilename[80];
-  } ExportTable;
-  
-  int numExportTables = 3;
-
-  PS_ASSERT_PTR_NON_NULL(config, NULL);
-
-  PXOPT_LOOKUP_S64(det_id, config->args, "-corr_id", true,  false);
-  PXOPT_LOOKUP_STR(outfile, config->args, "-outfile", true,  false);
-  PXOPT_LOOKUP_U64(limit,   config->args, "-limit",   false, false);
-
-  FILE *f = fopen (outfile, "w");
-  if (f == NULL) {
-    psError(PS_ERR_UNKNOWN, false, "failed to open output file");
-    return false;
-  }
-
-  psMetadata *where = psMetadataAlloc();
-  PXOPT_COPY_S64(config->args, where, "-corr_id", "corr_id", "==");
-
-  ExportTable tables [] = {
-    {"flatcorrRun", "flatcorr_export_run.sql"},
-    {"flatcorrCamLink", "flatcorr_export_cam_link.sql"},
-    {"flatcorrChipLink", "flatcorr_export_chip_link.sql"},
-  };
-
-  for (int i=0; i < numExportTables; i++) {
-    psString query = pxDataGet(tables[i].sqlFilename);
-    if (!query) {
-      psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-      return false;
-    }
-
-    if (where && psListLength(where->list)) {
-      psString whereClause = psDBGenerateWhereSQL(where, NULL);
-      psStringAppend(&query, " %s", whereClause);
-      psFree(whereClause);
-    }
-
-    // treat limit == 0 as "no limit"
-    if (limit) {
-      psString limitString = psDBGenerateLimitSQL(limit);
-      psStringAppend(&query, " %s", limitString);
-      psFree(limitString);
-    }
-
-    if (!p_psDBRunQuery(config->dbh, query)) {
-      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("regtool", PS_LOG_INFO, "no rows found");
-      psFree(output);
-      return true;
-    }
-
-    // we must write the export table in non-simple (true) format
-    if (!ippdbPrintMetadatas(f, output, tables[i].tableName, true)) {
-      psError(PS_ERR_UNKNOWN, false, "failed to print array");
-      psFree(output);
-      return false;
-    }
-    psFree(output);
-  }
-
-    fclose (f);
-
-    return true;
-}
-
-bool importrunMode(pxConfig *config)
-{
-  unsigned int nFail;
-  psMetadataItem *item, *entry;
-  
-  int numImportTables = 3;
-  
-  char tables[3] [80] = {"flatcorrRun", "flatcorrCamLink", "flatcorrChipLink"};
-
-  PS_ASSERT_PTR_NON_NULL(config, NULL);
-  
-  PXOPT_LOOKUP_STR(infile, config->args, "-infile", true,  false);
-
-  psMetadata *input = psMetadataConfigRead (NULL, &nFail, infile, false);
-
-  fprintf (stdout, "---- input ----\n");
-  psMetadataPrint (stderr, input, 1);
-
-  for (int i = 0; i < numImportTables; i++) {
-    item = psMetadataLookup (input, tables[i]);
-    psAssert (item, "entry not in input?");
-    psAssert (item->type == PS_DATA_METADATA_MULTI, "entry not multi?");
-    
-    entry = psListGet (item->data.list, 0);
-    assert (entry);
-    assert (entry->type == PS_DATA_METADATA);
-  
-    switch (i) {
-      case 0:
-      {
-        flatcorrRunRow *flatcorrRun = flatcorrRunObjectFromMetadata (entry->data.md);
-        flatcorrRunInsertObject (config->dbh, flatcorrRun);
-
-        // fprintf (stdout, "---- flatcorr run ----\n");
-        // psMetadataPrint (stderr, entry->data.md, 1);
-        break;
-      } 
-      case 1:
-      {
-        flatcorrCamLinkRow *flatcorrCamLink = flatcorrCamLinkObjectFromMetadata (entry->data.md);
-        flatcorrCamLinkInsertObject (config->dbh, flatcorrCamLink);
-
-        // fprintf (stdout, "---- flatcorr cam link ----\n");
-        // psMetadataPrint (stderr, entry->data.md, 1);
-        break;
-      }
-      case 2:
-      {
-        flatcorrChipLinkRow *flatcorrChipLink = flatcorrChipLinkObjectFromMetadata (entry->data.md);
-        flatcorrChipLinkInsertObject (config->dbh, flatcorrChipLink);
-
-        // fprintf (stdout, "---- flatcorr chip link ----\n");
-        // psMetadataPrint (stderr, entry->data.md, 1);
-        break;
-      }
-    }
-  }
-  return true;
-}
Index: trunk/ippTools/src/flatcorr.h
===================================================================
--- trunk/ippTools/src/flatcorr.h	(revision 25800)
+++ trunk/ippTools/src/flatcorr.h	(revision 25816)
@@ -28,8 +28,9 @@
     FLATCORR_MODE_DEFINERUN,
     FLATCORR_MODE_ADDCHIP,
+    FLATCORR_MODE_DROPCHIP,
     FLATCORR_MODE_ADDCAMERA,
+    FLATCORR_MODE_DROPCAMERA,
     FLATCORR_MODE_ADVANCECAMERA,
-    FLATCORR_MODE_DROPCHIP,
-    FLATCORR_MODE_DROPCAMERA,
+    FLATCORR_MODE_ADVANCEADDSTAR,
     FLATCORR_MODE_PENDINGPROCESS,
     FLATCORR_MODE_ADDPROCESS,
@@ -37,6 +38,4 @@
     FLATCORR_MODE_INPUTEXP,
     FLATCORR_MODE_INPUTIMFILE,
-    FLATCORR_MODE_EXPORTRUN,
-    FLATCORR_MODE_IMPORTRUN
 } flatcorrMode;
 
Index: trunk/ippTools/src/flatcorrConfig.c
===================================================================
--- trunk/ippTools/src/flatcorrConfig.c	(revision 25800)
+++ trunk/ippTools/src/flatcorrConfig.c	(revision 25816)
@@ -83,4 +83,9 @@
     psMetadataAddS64(addchipArgs, PS_LIST_TAIL, "-chip_id", 0,            "define Chip ID (required)", 0);
 
+    // -dropchip
+    psMetadata *dropchipArgs = psMetadataAlloc();
+    psMetadataAddS64(dropchipArgs, PS_LIST_TAIL, "-corr_id", 0,            "define Flat Correction ID (required)", 0);
+    psMetadataAddS64(dropchipArgs, PS_LIST_TAIL, "-chip_id", 0,            "define Chip ID (required)", 0);
+
     // -addcamera
     psMetadata *addcameraArgs = psMetadataAlloc();
@@ -89,8 +94,8 @@
     psMetadataAddS64(addcameraArgs, PS_LIST_TAIL, "-cam_id", 0,             "define Camera ID (required)", 0);
 
-    // -dropchip
-    psMetadata *dropchipArgs = psMetadataAlloc();
-    psMetadataAddS64(dropchipArgs, PS_LIST_TAIL, "-corr_id", 0,            "define Flat Correction ID (required)", 0);
-    psMetadataAddS64(dropchipArgs, PS_LIST_TAIL, "-chip_id", 0,            "define Chip ID (required)", 0);
+    // -dropcamera
+    psMetadata *dropcameraArgs = psMetadataAlloc();
+    psMetadataAddS64(dropcameraArgs, PS_LIST_TAIL, "-corr_id", 0,      "define Flat Correction ID (required)", 0);
+    psMetadataAddS64(dropcameraArgs, PS_LIST_TAIL, "-cam_id", 0,       "define Camera ID (required)", 0);
 
     // -advancecamera
@@ -99,9 +104,12 @@
     psMetadataAddBool(advancecameraArgs, PS_LIST_TAIL, "-simple",  0, "use the simple output format", false);
     psMetadataAddBool(advancecameraArgs, PS_LIST_TAIL, "-pretend", 0, "use the simple output format", false);
+    psMetadataAddStr(advancecameraArgs,  PS_LIST_TAIL, "-label",  PS_META_DUPLICATE_OK, "search by label", NULL);
 
-    // -dropcamera
-    psMetadata *dropcameraArgs = psMetadataAlloc();
-    psMetadataAddS64(dropcameraArgs, PS_LIST_TAIL, "-corr_id", 0,      "define Flat Correction ID (required)", 0);
-    psMetadataAddS64(dropcameraArgs, PS_LIST_TAIL, "-cam_id", 0,       "define Camera ID (required)", 0);
+    // -advanceaddstar
+    psMetadata *advanceaddstarArgs = psMetadataAlloc();
+    psMetadataAddU64 (advanceaddstarArgs, PS_LIST_TAIL, "-limit",   0, "limit result set to N items", 0);
+    psMetadataAddBool(advanceaddstarArgs, PS_LIST_TAIL, "-simple",  0, "use the simple output format", false);
+    psMetadataAddBool(advanceaddstarArgs, PS_LIST_TAIL, "-pretend", 0, "use the simple output format", false);
+    psMetadataAddStr(advanceaddstarArgs,  PS_LIST_TAIL, "-label",  PS_META_DUPLICATE_OK, "search by label", NULL);
 
     // -pendingprocess
@@ -155,4 +163,5 @@
     PXOPT_ADD_MODE("-addcamera",      "add an existing camera run to a flat correction run", FLATCORR_MODE_ADDCAMERA,      addcameraArgs);
     PXOPT_ADD_MODE("-advancecamera",  "migrate completed chips to camera stage analysis",    FLATCORR_MODE_ADVANCECAMERA,  advancecameraArgs);
+    PXOPT_ADD_MODE("-advanceaddstar", "migrate completed exposures to addstar processing",   FLATCORR_MODE_ADVANCEADDSTAR,  advanceaddstarArgs);
     PXOPT_ADD_MODE("-dropchip",       "drop a chip from a flat correction run",              FLATCORR_MODE_DROPCHIP,       dropchipArgs);
     PXOPT_ADD_MODE("-dropcamera",     "drop an exposure (camera stage analysis)",            FLATCORR_MODE_DROPCAMERA,     dropcameraArgs);
@@ -162,6 +171,4 @@
     PXOPT_ADD_MODE("-inputexp",       "list exposures for a correction run",                 FLATCORR_MODE_INPUTEXP,       inputexpArgs);
     PXOPT_ADD_MODE("-inputimfile",    "list imfiles for a chip run",                         FLATCORR_MODE_INPUTIMFILE,    inputimfileArgs);
-    PXOPT_ADD_MODE("-exportrun",            "export run for import on other database",       FLATCORR_MODE_EXPORTRUN,      exportrunArgs);
-    PXOPT_ADD_MODE("-importrun",            "import run from metadata file",                 FLATCORR_MODE_IMPORTRUN,      importrunArgs);
 
     if (!pxGetOptions(stderr, argc, argv, config, modes, argSets)) {
Index: trunk/ippTools/src/pxadd.c
===================================================================
--- trunk/ippTools/src/pxadd.c	(revision 25800)
+++ trunk/ippTools/src/pxadd.c	(revision 25816)
@@ -29,5 +29,5 @@
 #include "pxadd.h"
 
-bool pxaddRunSetState(pxConfig *config, psS64 add_id, const char *state, psS64 magicked)
+bool pxaddRunSetState(pxConfig *config, psS64 add_id, const char *state)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -41,8 +41,7 @@
     }
 
-    char *query = "UPDATE addRun SET state = '%s', magicked = %" PRId64 " WHERE add_id = %" PRId64;
-    if (!p_psDBRunQueryF(config->dbh, query, state, magicked, add_id)) {
-        psError(PS_ERR_UNKNOWN, false,
-                "failed to change state for add_id %" PRId64, add_id);
+    char *query = "UPDATE addRun SET state = '%s' WHERE add_id = %" PRId64;
+    if (!p_psDBRunQueryF(config->dbh, query, state, add_id)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to change state for add_id %" PRId64, add_id);
         return false;
     }
@@ -123,11 +122,11 @@
 }
 
-// Need to think more about this to see what we want it to do. BROKEN
 bool pxaddQueueByCamID(pxConfig *config,
                        psS64 cam_id,
 		       char *workdir,
+		       char *reduction,
 		       char *label,
-		       char *recipe,
-		       char *dvodb)
+		       char *dvodb,
+		       bool image_only)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -149,9 +148,11 @@
     if (!p_psDBRunQueryF(config->dbh, query,
 			 "new", // state
-			 workdir  ? workdir  : "NULL",
+			 workdir  ? workdir   : "NULL",
 			 "dirty", //workdir_state
-			 label    ? label    : "NULL",
-			 dvodb    ? dvodb    : "NULL",
-			 (long long)cam_id
+			 reduction? reduction : "NULL",
+			 label    ? label     : "NULL",
+			 dvodb    ? dvodb     : "NULL",
+			 image_only,
+			 (long long) cam_id
     )) {
         psError(PS_ERR_UNKNOWN, false, "database error");
Index: trunk/ippTools/src/pxadd.h
===================================================================
--- trunk/ippTools/src/pxadd.h	(revision 25800)
+++ trunk/ippTools/src/pxadd.h	(revision 25816)
@@ -25,17 +25,16 @@
 #include "pxtools.h"
 
-bool pxaddRunSetState(pxConfig *config, psS64 add_id, const char *state, psS64 magicked);
+bool pxaddRunSetState(pxConfig *config, psS64 add_id, const char *state);
 bool pxaddRunSetStateByQuery(pxConfig *config, psMetadata *where, const char *state);
 bool pxaddRunSetLabel(pxConfig *config, psS64 add_id, const char *label);
 bool pxaddRunSetLabelByQuery(pxConfig *config, psMetadata *where, const char *label);
 
-// Likely BROKEN
 bool pxaddQueueByCamID(pxConfig *config,
-                        psS64 cam_id,
-                        char *workdir,
-                        char *label,
-                        char *recipe,
-       		        char *dvodb);
-
+		       psS64 cam_id,
+		       char *workdir,
+		       char *reduction,
+		       char *label,
+		       char *dvodb,
+		       bool image_only);
 
 #endif // PXADD_H
