Index: trunk/ippTools/src/flatcorr.c
===================================================================
--- trunk/ippTools/src/flatcorr.c	(revision 15580)
+++ trunk/ippTools/src/flatcorr.c	(revision 15583)
@@ -79,12 +79,246 @@
 }
 
+
+#define ADDRENAMEPARAMSTR(from, to, oldname, newname, comment) \
+{ \
+    bool status = false; \
+    psString str = psMetadataLookupStr(&status, from, "-" oldname); \
+    if (!status) { \
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -" oldname); \
+        return false; \
+    } \
+    if (str) { \
+        if (!psMetadataAddStr(to, PS_LIST_TAIL, newname, PS_META_DUPLICATE_OK, comment, str)) { \
+            psError(PS_ERR_UNKNOWN, false, "failed to add item " newname); \
+            psFree(to); \
+            return false; \
+        } \
+    } \
+}
+
+#define ADDPARAMSTR(from, to, name) \
+  ADDRENAMEPARAMSTR(from, to, name, name, "==")
+
+#define ADDRENAMEPARAMF(from, to, type, oldname, newname, comment) \
+{ \
+    bool status = false; \
+    ps##type var = psMetadataLookup##type(&status, from, "-" oldname); \
+    if (!status) { \
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -" oldname); \
+        return false; \
+    } \
+    if (!isnan(var)) { \
+        if (!psMetadataAdd##type(to, PS_LIST_TAIL, newname, PS_META_DUPLICATE_OK, comment, var)) { \
+            psError(PS_ERR_UNKNOWN, false, "failed to add item " newname); \
+            psFree(to); \
+            return false; \
+        } \
+    } \
+}
+
+#define ADDPARAMF(from, to, type, name) \
+  ADDRENAMEPARAMF(from, to, type, name, name, "==")
+
 static bool newrunMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    psMetadata *where = psMetadataAlloc();
+    // convert exp_id into a psS64
+{
+    bool status = false;
+    psString var = psMetadataLookupStr(&status, config->args, "-exp_id");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_id");
+        return false;
+    }
+    if (var) {
+        if (!psMetadataAddS64(where, PS_LIST_TAIL, "exp_id", 0, "==", (psS64)atoll(var))) {
+            psError(PS_ERR_UNKNOWN, false, "failed to add item exp_id");
+            psFree(where);
+            return false; 
+        }
+    }
+}
+    ADDPARAMSTR(config->args, where, "exp_name");
+    ADDRENAMEPARAMSTR(config->args, where, "inst", "camera", "==");
+    ADDPARAMSTR(config->args, where, "telescope");
+    ADDPARAMSTR(config->args, where, "dateobs_begin");
+    ADDPARAMSTR(config->args, where, "dateobs_end");
+    ADDPARAMSTR(config->args, where, "exp_tag");
+    ADDPARAMSTR(config->args, where, "exp_type");
+    ADDPARAMSTR(config->args, where, "filelevel");
+    ADDPARAMSTR(config->args, where, "reduction");
+    ADDPARAMSTR(config->args, where, "filter");
+    ADDRENAMEPARAMF(config->args, where, F64, "airmass_min", "airmass", ">=");
+    ADDRENAMEPARAMF(config->args, where, F64, "airmass_max", "airmass", "<");
+    ADDRENAMEPARAMF(config->args, where, F64, "ra_min", "ra", ">=");
+    ADDRENAMEPARAMF(config->args, where, F64, "ra_max", "ra", "<");
+    ADDRENAMEPARAMF(config->args, where, F64, "decl_min", "decl", ">=");
+    ADDRENAMEPARAMF(config->args, where, F64, "decl_max", "decl", "<");
+    ADDRENAMEPARAMF(config->args, where, F32, "exp_time_min", "exp_time", ">=");
+    ADDRENAMEPARAMF(config->args, where, F32, "exp_time_max", "exp_time", "<");
+    ADDRENAMEPARAMF(config->args, where, F32, "sat_pixel_frac_min", "sat_pixel_frac", ">=");
+    ADDRENAMEPARAMF(config->args, where, F32, "sat_pixel_frac_max", "sat_pixel_frac", "<");
+    ADDRENAMEPARAMF(config->args, where, F64, "bg_min", "bt", ">=");
+    ADDRENAMEPARAMF(config->args, where, F64, "bg_max", "bt", "<");
+    ADDRENAMEPARAMF(config->args, where, F64, "bg_stdev_min", "bg_stdev", ">=");
+    ADDRENAMEPARAMF(config->args, where, F64, "bg_stdev_max", "bg_stdev", "<");
+    ADDRENAMEPARAMF(config->args, where, F64, "bg_mean_stdev_min", "bg_mean_stdev", ">=");
+    ADDRENAMEPARAMF(config->args, where, F64, "bg_mean_stdev_max", "bg_mean_stdev", "<");
+    ADDRENAMEPARAMF(config->args, where, F64, "alt_min", "alt", ">=");
+    ADDRENAMEPARAMF(config->args, where, F64, "alt_max", "alt", "<");
+    ADDRENAMEPARAMF(config->args, where, F64, "az_min", "az", ">=");
+    ADDRENAMEPARAMF(config->args, where, F64, "az_max", "az", "<");
+    ADDRENAMEPARAMF(config->args, where, F32, "ccd_temp_min", "ccd_temp", ">=");
+    ADDRENAMEPARAMF(config->args, where, F32, "ccd_temp_max", "ccd_temp", "<");
+    ADDRENAMEPARAMF(config->args, where, F64, "posang_min", "posang", ">=");
+    ADDRENAMEPARAMF(config->args, where, F64, "posang_max", "posang", "<");
+    ADDPARAMSTR(config->args, where, "object");
+    ADDRENAMEPARAMF(config->args, where, F32, "solang_min", "solang", ">=");
+    ADDRENAMEPARAMF(config->args, where, F32, "solang_max", "solang", "<");
+
+    if (where->list->n < 1) {
+        psFree(where);
+        where = NULL;
+    }
+
+    bool status = false;
+    psString workdir = psMetadataLookupStr(&status, config->args, "-set_workdir");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_workdir");
+        return false;
+    }
+
+    psString label = psMetadataLookupStr(&status, config->args, "-set_label");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_label");
+        return false;
+    }
+
+    psString reduction = psMetadataLookupStr(&status, config->args, "-set_reduction");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_reduction");
+        return false;
+    }
+
+    psString expgroup = psMetadataLookupStr(&status, config->args, "-set_expgroup");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_expgroup");
+        return false;
+    }
+
+    psString dvodb = psMetadataLookupStr(&status, config->args, "-set_dvodb");
+    if (!status) {
+        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -set_dvodb");
+        return false;
+    }
+
+    // find the exp_id of all the exposures that we want to queue up.
+    psString query = pxDataGet("chiptool_find_rawexp.sql");
+    if (!query) {
+        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
+        return false;
+    }
+
+    if (where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(where, "rawExp");
+        psFree(where);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+
+    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;
+    }
+
+    // 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;
+    }
+
+    // create a new flatcorrRun
+    if (!flatcorrRunInsert(config->dbh,
+            0,      // corr_id
+            dvodb,
+            "reg",  // state
+            workdir,
+            label
+        )) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    // figure out the ID of the flatcorrRun we just created
+    psS64 corr_id = psDBLastInsertID(config->dbh);
+
+    // loop over our list of exp_ids
+    for (long i = 0; i < psArrayLength(output); i++) {
+        psMetadata *md = output->data[i];
+
+        psS64 exp_id = psMetadataLookupS64(&status, md, "exp_id");
+        if (!status) {
+            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for exp_id");
+            psFree(output);
+            return false;
+        }
+
+        // queue the exp
+        psS64 chip_id = pxchipQueueByExpTag(config, exp_id, workdir, label, reduction, expgroup, dvodb);
+        if (!chip_id) {
+            if (!psDBRollback(config->dbh)) {
+                psError(PS_ERR_UNKNOWN, false, "database error");
+            }
+            psError(PS_ERR_UNKNOWN, false,
+                    "failed to trying to queue exp_id: %" PRId64, exp_id);
+            psFree(output);
+            return false;
+        }
+
+        // add a flatcorrExp to the flatcorr Run we just created
+        if (!flatcorrExpInsert(config->dbh,
+                corr_id,
+                chip_id
+            )) {
+            psError(PS_ERR_UNKNOWN, false, "database error");
+            return false;
+        }
+
+    }
+    psFree(output);
+
+    if (!psDBCommit(config->dbh)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+
+    return true;
+}
+
+static bool pendingMode(pxConfig *config)
+{
+    PS_ASSERT_PTR_NON_NULL(config, false);
+
     return false;
 }
 
-static bool pendingMode(pxConfig *config)
+static bool updateMode(pxConfig *config)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -92,9 +326,2 @@
     return false;
 }
-
-static bool updateMode(pxConfig *config)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-
-    return false;
-}
