Index: branches/czw_branch/20110406/ippTools/src/laptool.c
===================================================================
--- branches/czw_branch/20110406/ippTools/src/laptool.c	(revision 31397)
+++ branches/czw_branch/20110406/ippTools/src/laptool.c	(revision 31407)
@@ -121,5 +121,5 @@
   }
 
-  if (lapSequencePrintObject(stdout, run, !simple)) {
+  if (!lapSequencePrintObject(stdout, run, !simple)) {
     psError(PS_ERR_UNKNOWN, false, "failed to print object");
     psFree(run);
@@ -141,5 +141,5 @@
   psMetadata *where = psMetadataAlloc();
   PXOPT_COPY_S64(config->args, where, "-seq_id", "seq_id", "==");
-  PXOPT_COPY_STR(config->args, where, "-name",   "name",   "LIKE");
+  PXOPT_COPY_STR(config->args, where, "-seq_name",   "name",   "LIKE");
 
   psString query = pxDataGet("laptool_listsequence.sql");
@@ -214,4 +214,5 @@
   PXOPT_LOOKUP_STR(label,           config->args, "-label",           false, false);
   PXOPT_LOOKUP_STR(dist_group,      config->args, "-dist_group",      false, false);
+  PXOPT_LOOKUP_BOOL(all_obsmode,    config->args, "-all_obsmode",     false);
   PXOPT_LOOKUP_BOOL(simple,         config->args, "-simple",          false);
 
@@ -226,6 +227,6 @@
 				  NULL, // registered
 				  0,    // fault
-				  0,    // quick_sass_id
-				  0     // final_sass_id
+				  INT64_MAX,    // quick_sass_id
+				  INT64_MAX     // final_sass_id
 				  );
   if (!run) {
@@ -248,11 +249,9 @@
   }
 
-  // point of no return
-  if (!psDBCommit(config->dbh)) {
-    psError(PS_ERR_UNKNOWN, false, "database error");
-    return false;
-  }
-
-  if (lapRunPrintObject(stdout, run, !simple)) {
+
+  if (!lapRunPrintObject(stdout, run, !simple)) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
     psError(PS_ERR_UNKNOWN, false, "failed to print object");
     psFree(run);
@@ -265,16 +264,44 @@
   psString query = pxDataGet("laptool_definerun.sql");
   if (!query) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+
     psError(PXTOOLS_ERR_SYS, false, "failed to retrieve SQL statement");
     return(false);
   }
-  psString where = NULL;
-  if (!pxspaceAddWhere(config, &where, "rawExp")) {
+
+  // Add constraints to the exposure search:
+  psMetadata *where = psMetadataAlloc();
+  PXOPT_COPY_STR(config->args, where, "-filter", "rawExp.filter", "==");
+
+  // This seems unnecessarily clunky.
+  if (!all_obsmode) {
+    //    fprintf(stderr, "Not doing all obsmodes!\n");
+    if (!psMetadataLookupStr(NULL,config->args,"-obsmode")) {
+      //      fprintf(stderr, "Not doing all obsmodes and none specified!\n");
+      psMetadataAddStr(where,PS_LIST_TAIL,"rawExp.obs_mode",0,"==","3PI");
+    }
+    pxAddLabelSearchArgs (config, where, "-obsmode", "rawExp.obsmode", "==");
+  }
+  
+  psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+  if (!pxspaceAddWhere(config, &whereClause, "rawExp")) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+
     psError(psErrorCodeLast(), false, "pxSpaceAddWhere failed");
     return(false);
   }
-  psStringAppend(&query, " %s", where);
+  psStringAppend(&query, " %s", whereClause);
   psFree(where);
 
+  // Fetch exposures
   if (!p_psDBRunQuery(config->dbh, query)) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+
     psError(PS_ERR_UNKNOWN, false, "database error");
     psFree(query);
@@ -285,8 +312,16 @@
   psArray *output = p_psDBFetchResult(config->dbh);
   if (!output) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+
     psError(PS_ERR_UNKNOWN, false, "database error");
     return(false);
   }
   if (!psArrayLength(output)) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+
     psTrace("laptool", PS_LOG_INFO, "no rows found");
     psFree(output);
@@ -294,12 +329,24 @@
   }
 
-
   // Insert the exposure data
   for (long i = 0; i < output->n; i++) {
     psMetadata *row = output->data[i]; // Row from select
+    // Add default values from this run:
+    psMetadataAddS64(row, PS_LIST_TAIL, "lap_id", 0, "", lap_id);
+    psMetadataAddS64(row, PS_LIST_TAIL, "pair_id", 0, "", INT64_MAX);
+    psMetadataAddStr(row, PS_LIST_TAIL, "data_state", 0, "", "new");
     lapExpRow *lapExp = lapExpObjectFromMetadata(row);
     lapExp->lap_id = lap_id;
 
     if (!lapExpInsertObject(config->dbh,lapExp)) {
+      if (!lapExpPrintObject(stdout, lapExp, !simple)) {
+	if (!psDBRollback(config->dbh)) {
+	  psError(PS_ERR_UNKNOWN, false, "database error");
+	}
+	psError(PS_ERR_UNKNOWN, false, "failed to print object");
+	psFree(run);
+	return false;
+      }
+      
       if (!psDBRollback(config->dbh)) {
 	psError(PS_ERR_UNKNOWN, false, "database error");
@@ -312,4 +359,11 @@
   }
 
+  // point of no return
+  if (!psDBCommit(config->dbh)) {
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    return false;
+  }
+
+  
   psFree(output);
   return(true);  
@@ -341,5 +395,4 @@
     psFree(whereClause);
   }
-  psStringAppend(&query, " ORDER BY rawExp.dateobs ");
   
   if (!p_psDBRunQuery(config->dbh, query)) {
@@ -452,4 +505,6 @@
     psFree(limitString);
   }
+  psStringAppend(&query, " ORDER BY rawExp.dateobs ");
+  
   if (!p_psDBRunQuery(config->dbh, query)) {
     psError(PS_ERR_UNKNOWN, false, "database error");
@@ -554,7 +609,9 @@
 {
   PS_ASSERT_PTR_NON_NULL(config, false);
+  PXOPT_LOOKUP_S64(lap_id, config->args, "-lap_id", true, false);
+
   PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
   PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
-  
+
   psMetadata *where = psMetadataAlloc();
   PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
@@ -565,41 +622,52 @@
     return(false);
   }
-  psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-  if (whereClause) {
+  
+  if (psListLength(where->list)) {
+    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     psStringPrepend(&whereClause, "\n AND ");
-  }
-  
-  psString limitString = NULL;
+    psStringSubstitute(&query,whereClause,"@WHERE@");
+    psFree(whereClause);
+  }
+
   if (limit) {
-    limitString = psDBGenerateLimitSQL(limit);
-    psStringPrepend(&limitString, "\n");
-  }
-
-  if (!p_psDBRunQueryF(config->dbh, query, whereClause, limitString ? limitString : "")) {
-    psError(PXTOOLS_ERR_PROG, false, "database error");
+    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);
-    psFree(whereClause);
-    return(false);
-  }
-  psFree(limitString);
+    return false;
+  }
   psFree(query);
-  psFree(whereClause);
-  
+
   psArray *output = p_psDBFetchResult(config->dbh);
   if (!output) {
-    psError(PS_ERR_UNKNOWN, false, "database error");
-    return(false);
+    psErrorCode err = psErrorCodeLast();
+    switch (err) {
+    case PS_ERR_DB_CLIENT:
+      psError(PXTOOLS_ERR_SYS, false, "database error");
+    case PS_ERR_DB_SERVER:
+      psError(PXTOOLS_ERR_PROG, false, "database error");
+    default:
+      psError(PXTOOLS_ERR_PROG, false, "unknown error %d",err);
+    }
+
+    return false;
   }
   if (!psArrayLength(output)) {
     psTrace("laptool", PS_LOG_INFO, "no rows found");
     psFree(output);
-    return(true);
-  }
-  
-  if (!ippdbPrintMetadatas(stdout, output, "lapRunStacks", !simple)) {
-    psError(PS_ERR_UNKNOWN, false, "failed to print array");
-    psFree(output);
-    return(false);
+    return true;
+  }
+
+  if (psArrayLength(output)) {
+    if (!ippdbPrintMetadatas(stdout, output, "lapRunStacks", !simple)) {
+      psError(PS_ERR_UNKNOWN, false, "failed to print array");
+      psFree(output);
+      return false;
+    }
   }
 
Index: branches/czw_branch/20110406/ippTools/src/laptool.h
===================================================================
--- branches/czw_branch/20110406/ippTools/src/laptool.h	(revision 31397)
+++ branches/czw_branch/20110406/ippTools/src/laptool.h	(revision 31407)
@@ -9,5 +9,6 @@
 
 typedef enum {
-  LAPTOOL_MODE_DEFINESEQUENCE     = 0x0,
+  LAPTOOL_MODE_NONE           = 0x0,
+  LAPTOOL_MODE_DEFINESEQUENCE,
   LAPTOOL_MODE_LISTSEQUENCE,
   LAPTOOL_MODE_DEFINERUN,
Index: branches/czw_branch/20110406/ippTools/src/laptoolConfig.c
===================================================================
--- branches/czw_branch/20110406/ippTools/src/laptoolConfig.c	(revision 31397)
+++ branches/czw_branch/20110406/ippTools/src/laptoolConfig.c	(revision 31407)
@@ -57,4 +57,9 @@
   ADD_OPT(Str, definerunArgs, "-label",                       "define the label used", NULL);
   ADD_OPT(Str, definerunArgs, "-dist_group",                  "define the distribution group for this data", NULL);
+
+  psMetadataAddStr(definerunArgs, PS_LIST_TAIL, "-obsmode", PS_META_DUPLICATE_OK, "search by obsmode", NULL);
+  ADD_OPT(Bool,definerunArgs, "-all_obsmode",                 "use all science obsmodes", false);
+  
+  ADD_OPT(Bool,definerunArgs, "-simple",                      "use the simple output format", false);
   
   // -pendingrun
@@ -67,5 +72,5 @@
   ADD_OPT(Str, pendingrunArgs, "-state",                      "search by LAP run state", NULL);
   ADD_OPT(Str, pendingrunArgs, "-fault",                      "search by LAP run fault", NULL);
-
+  ADD_OPT(Bool,pendingrunArgs, "-simple",                     "use the simple output format", false);
   // -updaterun
   psMetadata *updaterunArgs = psMetadataAlloc();
