Index: branches/czw_branch/20101203/ippTools/src/chiptool.c
===================================================================
--- branches/czw_branch/20101203/ippTools/src/chiptool.c	(revision 29965)
+++ branches/czw_branch/20101203/ippTools/src/chiptool.c	(revision 29982)
@@ -271,7 +271,8 @@
 
     // default
+    PXOPT_LOOKUP_BOOL(unique, config->args, "-unique", false);
     PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-
+    
     // find the exp_id of all the exposures that we want to queue up.
     psString query = pxDataGet("chiptool_find_rawexp.sql");
@@ -288,4 +289,8 @@
     psFree(where);
 
+    if (unique) {
+      psStringAppend(&query, "AND chip_id IS NULL");
+    }
+    
     if (!p_psDBRunQuery(config->dbh, query)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
Index: branches/czw_branch/20101203/ippTools/src/chiptoolConfig.c
===================================================================
--- branches/czw_branch/20101203/ippTools/src/chiptoolConfig.c	(revision 29965)
+++ branches/czw_branch/20101203/ippTools/src/chiptoolConfig.c	(revision 29982)
@@ -61,4 +61,5 @@
     psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-set_dist_group",  0,      "define distribution group", NULL);
     psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-set_note",  0,           "define note", NULL);
+    psMetadataAddBool(definebyqueryArgs, PS_LIST_TAIL, "-unique",   0,           "only queue exposures that have no previous chipRun", 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);
Index: branches/czw_branch/20101203/ippTools/src/pztool.c
===================================================================
--- branches/czw_branch/20101203/ippTools/src/pztool.c	(revision 29965)
+++ branches/czw_branch/20101203/ippTools/src/pztool.c	(revision 29982)
@@ -112,5 +112,5 @@
     PXOPT_LOOKUP_STR(telescope, config->args, "-telescope", true, false);
     PXOPT_LOOKUP_STR(uri, config->args, "-uri", true, false);
-
+    
     if (!pzDataStoreInsert(config->dbh,
             camera,
Index: branches/czw_branch/20101203/ippTools/src/regtool.c
===================================================================
--- branches/czw_branch/20101203/ippTools/src/regtool.c	(revision 29965)
+++ branches/czw_branch/20101203/ippTools/src/regtool.c	(revision 29982)
@@ -331,4 +331,5 @@
 
   psString previous_uri = NULL;
+  psString this_uri = NULL;
   bool ok_to_burn = true;
   bool already_burned = true;
@@ -340,8 +341,14 @@
     psMetadata *row = output->data[i];
 
-    // Use tmp_class_id because class_id may not be populated for all imfiles
+    // Add the information about this row and the previous, if it exists.
+    // Write the class_id stuff for debugging.
     this_class_id = psStringCopy(psMetadataLookupStr(NULL,row,"tmp_class_id"));
     psStringSubstitute(&this_class_id,"XY","ota");
-    // Determine if we've crossed a class_id boundary.
+    psMetadataAddStr(row,PS_LIST_TAIL,"this_class_id",PS_META_REPLACE,"",this_class_id);
+    if (previous_class_id) {
+      psMetadataAddStr(row,PS_LIST_TAIL,"previous_class_id",PS_META_REPLACE,"",previous_class_id);
+    }
+    
+    // Determine if we've crossed a class_id boundary, as this resets the bits.
     if (previous_class_id) {
       if (strcmp(this_class_id,previous_class_id) != 0) {
@@ -352,16 +359,12 @@
     }
 
-    // If the state of this imfile is not "pending_burntool" then skip it, because 
-    if (!ignore_state) {
-      psString imfile_state = psMetadataLookupStr(NULL,row,"imfile_state");
-
-      if (!imfile_state) {
-	continue;
-      }
-      if (strcmp("pending_burntool",imfile_state) != 0) {
-	continue;
-      }
-    }
-    
+    // Write the URIs as well.
+    this_uri = psStringCopy(psMetadataLookupStr(NULL,row,"uri")); // Duplicate, but helpful for my debugging.
+    psMetadataAddStr(row,PS_LIST_TAIL,"this_uri",PS_META_REPLACE,"",this_uri); 
+    if (previous_uri) {
+      psMetadataAddStr(row,PS_LIST_TAIL,"previous_uri",PS_META_REPLACE,"",previous_uri);
+    }
+
+    // Check the two status variables
     // Convert bits of the SQL query into booleans describing the data state
     if ((psMetadataLookupS32(NULL,row,"is_downloaded") != 1)||
@@ -381,29 +384,66 @@
     psMetadataAddBool(row,PS_LIST_TAIL,"already_burned",PS_META_REPLACE,"",already_burned);
 
-    // We'll need to return the previous uri so we know where the input table is:
-    if (previous_uri) {
-      psMetadataAddStr(row,PS_LIST_TAIL,"previous_uri",PS_META_REPLACE,"",previous_uri);
-      psFree(previous_uri);
-    }
-    previous_uri = psStringCopy(psMetadataLookupStr(NULL,row,"uri"));
-
-    // Write the class_id stuff at the end for debugging.
-    psMetadataAddStr(row,PS_LIST_TAIL,"this_class_id",PS_META_REPLACE,"",this_class_id);
-    if (previous_class_id) {
-      psMetadataAddStr(row,PS_LIST_TAIL,"previous_class_id",PS_META_REPLACE,"",previous_class_id);
-      psFree(previous_class_id);
-    }
-    // Use tmp_class_id because class_id may not be populated for all imfiles
-    previous_class_id = psStringCopy(psMetadataLookupStr(NULL,row,"tmp_class_id"));
-    psStringSubstitute(&previous_class_id,"XY","ota");
+    // Check the uri for this exposure
+    if (!this_uri) {
+	ok_to_burn = false;
+	already_burned = false;
+
+	// Save this round for next round.
+	psFree(previous_class_id);
+	psFree(previous_uri);
+	previous_class_id = psStringCopy(psMetadataLookupStr(NULL,row,"tmp_class_id"));
+	psStringSubstitute(&previous_class_id,"XY","ota");
+	previous_uri = psStringCopy(psMetadataLookupStr(NULL,row,"uri")); // Save for next round.	
+	continue;
+    }      
+
+    //    printf("STATUS: %s %s %s %s (%d %d)\n",this_uri,previous_uri,this_class_id,previous_class_id,ok_to_burn,already_burned);
+
+    // If the state of this imfile is not "pending_burntool" then we can't burn it. 
+    if (!ignore_state) {
+      psString imfile_state = psMetadataLookupStr(NULL,row,"imfile_state");
+
+      if (!imfile_state) { // imfile state is NULL, so we probably aren't registered.
+	ok_to_burn = false;
+
+	// Save this round for next round.
+	psFree(previous_class_id);
+	psFree(previous_uri);
+	previous_class_id = psStringCopy(psMetadataLookupStr(NULL,row,"tmp_class_id"));
+	psStringSubstitute(&previous_class_id,"XY","ota");
+	previous_uri = psStringCopy(psMetadataLookupStr(NULL,row,"uri")); // Save for next round.	
+	continue;
+      }
+      if (strcmp("pending_burntool",imfile_state) != 0) { // Probably the state is full, do not twiddle states	
+	// Save this round for next round.
+	psFree(previous_class_id);
+	psFree(previous_uri);
+	previous_class_id = psStringCopy(psMetadataLookupStr(NULL,row,"tmp_class_id"));
+	psStringSubstitute(&previous_class_id,"XY","ota");
+	previous_uri = psStringCopy(psMetadataLookupStr(NULL,row,"uri")); // Save for next round.	
+	continue;
+      }
+    }
+    
     // Determine if we've already suggested an entry for this ota, and if not, copy this
     // suggestion into our output result list.
     if (!ok_to_burn || already_burned) {
+      // Save this round for next round.
+      psFree(previous_class_id);
+      psFree(previous_uri);
+      previous_class_id = psStringCopy(psMetadataLookupStr(NULL,row,"tmp_class_id"));
+      psStringSubstitute(&previous_class_id,"XY","ota");
+      previous_uri = psStringCopy(psMetadataLookupStr(NULL,row,"uri")); // Save for next round.	      
       continue;
     }
-    
+    // If we're here, then we think we could potentially burntool this file.
     psArrayAdd(results,results->n,row);
-    // If we're here, then we think we could potentially burntool this file.
-
+
+    // Save this round for next round.
+    psFree(previous_class_id);
+    psFree(previous_uri);
+    previous_class_id = psStringCopy(psMetadataLookupStr(NULL,row,"tmp_class_id"));
+    psStringSubstitute(&previous_class_id,"XY","ota");
+    previous_uri = psStringCopy(psMetadataLookupStr(NULL,row,"uri")); // Save for next round.	      
   }
   
@@ -499,5 +539,5 @@
     PXOPT_LOOKUP_S32(bytes, config->args,  "-bytes", false, false);
     PXOPT_LOOKUP_STR(md5sum, config->args, "-md5sum", false, false);
-    PXOPT_LOOKUP_Bool(video_cells, config->args, "-video_cells", false, false);
+    PXOPT_LOOKUP_BOOL(video_cells, config->args, "-video_cells", false);
     
     PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
@@ -1060,4 +1100,7 @@
     PXOPT_LOOKUP_F32(moon_phase, config->args, "-moon_phase", false, false);
     PXOPT_LOOKUP_STR(label,  config->args, "-label", false, false);
+    PXOPT_LOOKUP_STR(data_group,  config->args, "-data_group", false, false);
+    PXOPT_LOOKUP_STR(dist_group,  config->args, "-dist_group", false, false);
+    PXOPT_LOOKUP_STR(chip_workdir,config->args, "-chip_workdir", false, false);
     PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
 
@@ -1259,13 +1302,13 @@
     // else continue on...
 
-
+    chip_workdir = chip_workdir ? chip_workdir : workdir;
     // insert an entry into the chipPendingExp table
     // this can only be run as the newExp's state has been set to stop
     if (!pxchipQueueByExpTag(config,
                 exp_id,
-                workdir,
+			     chip_workdir,
                 label,
-                NULL,       // data_group
-                NULL,       // dist_group
+			     data_group,
+			     dist_group,
                 reduction,
                 NULL,       // expgroup
Index: branches/czw_branch/20101203/ippTools/src/regtoolConfig.c
===================================================================
--- branches/czw_branch/20101203/ippTools/src/regtoolConfig.c	(revision 29965)
+++ branches/czw_branch/20101203/ippTools/src/regtoolConfig.c	(revision 29982)
@@ -268,4 +268,7 @@
     ADD_OPT(F32,  addprocessedexpArgs, "-moon_phase",       "define moon phase (0.0 = new)",   NAN);
     ADD_OPT(Str,  addprocessedexpArgs, "-label",            "define label for chip stage (non-detrend data only)", NULL);
+    ADD_OPT(Str,  addprocessedexpArgs, "-data_group",       "define data_group for chip stage (non-detrend data only)", NULL);
+    ADD_OPT(Str,  addprocessedexpArgs, "-dist_group",       "define dist_group for chip stage (non-detrend data only)", NULL);
+    ADD_OPT(Str,  addprocessedexpArgs, "-chip_workdir",     "define workdir for chip stage (non-detrend data only)", NULL);
     ADD_OPT(Str,  addprocessedexpArgs, "-hostname",         "define host name", NULL);
     ADD_OPT(S16,  addprocessedexpArgs, "-fault",             "set fault code", 0);
