Index: branches/simmosaic_branches/ippTools/src/pxchip.c
===================================================================
--- branches/simmosaic_branches/ippTools/src/pxchip.c	(revision 24860)
+++ branches/simmosaic_branches/ippTools/src/pxchip.c	(revision 27839)
@@ -69,6 +69,6 @@
     psMetadataAddF64(md,  PS_LIST_TAIL, "-posang_min",         0, "search by min rotator position angle", NAN);
     psMetadataAddF64(md,  PS_LIST_TAIL, "-posang_max",         0, "search by max rotator position angle", NAN);
-    psMetadataAddF32(md,  PS_LIST_TAIL, "-solang_min",         0, "search by min solar angle", NAN);
-    psMetadataAddF32(md,  PS_LIST_TAIL, "-solang_max",         0, "search by max solar angle", NAN);
+    psMetadataAddF32(md,  PS_LIST_TAIL, "-sun_angle_min",         0, "search by min solar angle", NAN);
+    psMetadataAddF32(md,  PS_LIST_TAIL, "-sun_angle_max",         0, "search by max solar angle", NAN);
     psMetadataAddStr(md,  PS_LIST_TAIL, "-object",             0, "search by exposure object", NULL);
     psMetadataAddStr(md,  PS_LIST_TAIL, "-comment",            0, "search by comment field (LIKE comparison)", NULL);
@@ -96,5 +96,5 @@
     PXOPT_COPY_STR(config->args, where, "-exp_type", "rawExp.exp_type", "==");
     PXOPT_COPY_STR(config->args, where, "-filelevel", "rawExp.filelevel", "==");
-    PXOPT_COPY_STR(config->args, where, "-filter", "rawExp.filter", "==");
+    PXOPT_COPY_STR(config->args, where, "-filter", "rawExp.filter", "LIKE");
     PXOPT_COPY_F64(config->args, where, "-airmass_min", "rawExp.airmass", ">=");
     PXOPT_COPY_F64(config->args, where, "-airmass_max", "rawExp.airmass", "<");
@@ -122,12 +122,12 @@
     PXOPT_COPY_F64(config->args, where, "-posang_max", "rawExp.posang", "<");
     PXOPT_COPY_STR(config->args, where, "-object", "rawExp.object", "==");
-    PXOPT_COPY_F32(config->args, where, "-solang_min", "rawExp.solang", ">=");
-    PXOPT_COPY_F32(config->args, where, "-solang_max", "rawExp.solang", "<");
+    PXOPT_COPY_F32(config->args, where, "-sun_angle_min", "rawExp.sun_angle", ">=");
+    PXOPT_COPY_F32(config->args, where, "-sun_angle_max", "rawExp.sun_angle", "<");
     PXOPT_COPY_STR(config->args, where, "-comment", "rawExp.comment", "LIKE");
-    PXOPT_COPY_STR(config->args, where, "-obs_mode", "rawExp.obs_mode", "==");
-    return true;
-}
-
-bool pxchipRunSetState(pxConfig *config, psS64 chip_id, const char *state, const bool magicked)
+    PXOPT_COPY_STR(config->args, where, "-obs_mode", "rawExp.obs_mode", "LIKE");
+    return true;
+}
+
+bool pxchipRunSetState(pxConfig *config, psS64 chip_id, const char *state, const psS64 magicked)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -140,5 +140,5 @@
     }
 
-    char *query = "UPDATE chipRun SET state = '%s', magicked = %d WHERE chip_id = %" PRId64;
+    char *query = "UPDATE chipRun SET state = '%s', magicked = %" PRId64 " WHERE chip_id = %" PRId64;
     if (!p_psDBRunQueryF(config->dbh, query, state, magicked, chip_id)) {
         psError(PS_ERR_UNKNOWN, false,
@@ -175,4 +175,9 @@
     psString query = psStringCopy("UPDATE chipRun JOIN rawExp USING(exp_id) SET state = '%s'");
 
+    if (!strcmp(state, "cleaned") || !strcmp(state, "purged")) {
+        // if magicked is non-zero set it to -1
+        psStringAppend(&query, ", chipRun.magicked = IF(chipRun.magicked = 0, 0, -1)");
+    }
+
     if (where && psListLength(where->list) > 0) {
         psString whereClause = psDBGenerateWhereSQL(where, NULL);
@@ -192,27 +197,26 @@
 }
 
-
-bool pxchipRunSetLabel(pxConfig *config, psS64 chip_id, const char *label)
+bool pxchipProcessedImfileSetStateByQuery(pxConfig *config, psMetadata *where, const char *state)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
-    // note label == NULL should be explicitly allowed
-
-    char *query = "UPDATE chipRun SET label = '%s' WHERE chip_id = %" PRId64;
-    if (!p_psDBRunQueryF(config->dbh, query, label, chip_id)) {
-        psError(PS_ERR_UNKNOWN, false,
-                "failed to change state for chip_id %" PRId64, chip_id);
-        return false;
-    }
-
-    return true;
-}
-
-
-bool pxchipRunSetLabelByQuery(pxConfig *config, psMetadata *where, const char *label)
-{
-    PS_ASSERT_PTR_NON_NULL(config, false);
-    // note label == NULL should be explicitly allowed
-
-    psString query = psStringCopy("UPDATE chipRun JOIN rawExp USING(exp_id) SET label = '%s'");
+    PS_ASSERT_PTR_NON_NULL(state, false);
+
+    // check that state is a valid string value
+    if (!pxIsValidState(state)) {
+        psError(PS_ERR_UNKNOWN, false, "invalid chipProcessedImfile state: %s", state);
+        return false;
+    }
+
+/*     if (!strcmp(state, "full")) { */
+/*         // There are states that need to be met for a run to be set to full that we don't */
+/*         // check here. */
+/*         // for example all of the run's Imfiles must have chipProcessedImfile.data_state == "full" */
+/*         // chipRun.magicked = (SUM(!chipProcessedImfile.magicked) = 0) */
+/*         // so don't do allow setting the state to full */
+/*         psError(PS_ERR_UNKNOWN, true, "cannot use -updaterun so set chipRun state to full"); */
+/*         return false; */
+/*     } */
+
+    psString query = psStringCopy("UPDATE chipProcessedImfile JOIN chipRun USING(chip_id) JOIN rawExp ON chipRun.exp_id = rawExp.exp_id SET data_state = '%s'");
 
     if (where && psListLength(where->list) > 0) {
@@ -220,7 +224,10 @@
         psStringAppend(&query, " %s", whereClause);
         psFree(whereClause);
-    }
-
-    if (!p_psDBRunQueryF(config->dbh, query, label)) {
+    } else {
+        psError(PS_ERR_UNKNOWN, true, "search parameters are required");
+        return false;
+    }
+
+    if (!p_psDBRunQueryF(config->dbh, query, state)) {
         psFree(query);
         psError(PS_ERR_UNKNOWN, false, "database error");
@@ -232,5 +239,4 @@
     return true;
 }
-
 
 psS64 pxchipQueueByExpTag(pxConfig *config,
@@ -238,9 +244,12 @@
                          const char *workdir,
                          const char *label,
+                         const char *data_group,
+                         const char *dist_group,
                          const char *reduction,
                          const char *expgroup,
                          const char *dvodb,
                          const char *tess_id,
-                         const char *end_stage)
+                         const char *end_stage,
+                         const char *note)
 {
     PS_ASSERT_PTR_NON_NULL(config, false);
@@ -260,4 +269,6 @@
             "dirty",    // workdir_state
             label,
+            data_group,
+            dist_group,
             reduction,
             expgroup,
@@ -265,5 +276,6 @@
             tess_id,
             end_stage,
-            0           // magicked
+            0,          // magicked
+            note
             )
     ) {
