Index: /branches/czw_branch/20110406/dbconfig/changes.txt
===================================================================
--- /branches/czw_branch/20110406/dbconfig/changes.txt	(revision 31395)
+++ /branches/czw_branch/20110406/dbconfig/changes.txt	(revision 31396)
@@ -2058,4 +2058,58 @@
 UPDATE dbversion set schema_version = '1.1.69', updated= CURRENT_TIMESTAMP();
 
-
-
+-- Version 1.1.70
+
+CREATE TABLE lapSequence (
+    seq_id BIGINT AUTO_INCREMENT, -- Identifier for the processing sequence
+    name VARCHAR(64) NOT NULL,    -- short name of the sequence
+    description VARCHAR(255) NOT NULL, -- longer description of the sequence
+    PRIMARY KEY(seq_id),
+    KEY(name)
+) ENGINE=innodb DEFAULT CHARSET=latin1;
+
+CREATE TABLE lapRun (
+    lap_id BIGINT AUTO_INCREMENT, -- Identifier for the processing run
+    seq_id BIGINT NOT NULL,       -- Identifier to match to the sequence
+    tess_id VARCHAR(64) NOT NULL, -- tessellation id to use
+    projection_cell VARCHAR(64) NOT NULL, -- projection cell from the tessellation to consider
+    filter VARCHAR(64) NOT NULL,  -- filter
+    state VARCHAR(64) NOT NULL,   -- state of run
+    label VARCHAR(64) NOT NULL,   -- processing label
+    dist_group VARCHAR(64) NOT NULL, -- distribution group for products of this run
+    registered TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- time run was registered
+    fault SMALLINT NOT NULL,      -- fault code
+    quick_sass_id,                -- stackAssociation id for quick stack
+    final_sass_id,                -- stackAssociation id for final stack
+    PRIMARY KEY(lap_id),
+    KEY(seq_id),
+    KEY(projection_cell),
+    KEY(filter),
+    KEY(state),
+    KEY(label),
+    KEY(fault),
+    FOREIGN KEY(seq_id) REFERENCES lapSequence(seq_id),
+    FOREIGN KEY(quick_sass_id) REFERENCES stackAssociation(sass_id),
+    FOREIGN KEY(final_sass_id) REFERENCES stackAssociation(sass_id)
+) ENGINE=innodb DEFAULT CHARSET=latin1;
+
+CREATE TABLE lapExp (
+    lap_id BIGINT NOT NULL, -- Link back to processing run
+    exp_id BIGINT NOT NULL, -- exposure definition
+    chip_id BIGINT,         -- processing id from chipRun
+    pair_id BIGINT,         -- companion chip_id
+    private TINYINT DEFAULT 0, -- denotes this exposure is private
+    pairwise TINYINT DEFAULT 0, -- denotes if this exposure should be pairwise diffed
+    active TINYINT DEFAULT 0, -- denotes if this exposure is currently in use
+    data_state VARCHAR(64) NOT NULL, -- state of exposure
+    PRIMARY KEY (lap_id),
+    KEY (exp_id),
+    KEY (chip_id),
+    KEY (pair_id),
+    KEY (data_state),
+    FOREIGN KEY (lap_id) REFERENCES lapRun(lap_id),
+    FOREIGN KEY (exp_id) REFERENCES rawExp(exp_id),
+    FOREIGN KEY (chip_id,exp_id) REFERENCES chipRun(chip_id,exp_id),
+    FOREIGN KEY (pair_id) REFERENCES chipRun(chip_id),
+) ENGINE=innodb DEFAULT CHARSET=latin1;
+
+UPDATE dbversion set schema_version = '1.1.70', updated= CURRENT_TIMESTAMP();
Index: /branches/czw_branch/20110406/dbconfig/lap.md
===================================================================
--- /branches/czw_branch/20110406/dbconfig/lap.md	(revision 31396)
+++ /branches/czw_branch/20110406/dbconfig/lap.md	(revision 31396)
@@ -0,0 +1,31 @@
+lapSequence METADATA
+    seq_id         S64         0    # Primary Key AUTO_INCREMENT
+    name           STR         64   # Key
+    description    STR         255
+END
+
+lapRun METADATA
+    lap_id         S64         0    # Primary Key AUTO_INCREMENT
+    seq_id         S64         0    # Key fkey (seq_id) ref lapSequence(seq_id)
+    tess_id        STR         64
+    projection_cell STR        64   # Key
+    filter         STR         64   # Key
+    state          STR         64   # Key
+    label          STR         64   # Key
+    dist_group     STR         64 
+    registered     TAI	       NULL 
+    fault          S16	       0    # Key
+    quick_sass_id  S64         0    # fkey(quick_sass_id) ref stackAssociation(sass_id)
+    final_sass_id  S64         0    # fkey(final_sass_id) ref stackAssociation(sass_id)
+END
+
+lapExp METADATA
+    lap_id         S64         0    # Primary Key fkey (lap_id) ref lapRun(lap_id)
+    exp_id         S64         0    # Key fkey (exp_id) ref rawExp(exp_id)
+    chip_id        S64         0    # Key fkey (exp_id, chip_id) ref chipRun(exp_id, chip_id)
+    pair_id        S64         0    # Key fkey (pair_id) ref chipRun(chip_id)
+    private        BOOL        f    
+    pairwise       BOOL        f    
+    active         BOOL        f
+    data_state     STR         64   # Key
+END
Index: /branches/czw_branch/20110406/ippScripts/scripts/lap_science.pl
===================================================================
--- /branches/czw_branch/20110406/ippScripts/scripts/lap_science.pl	(revision 31395)
+++ /branches/czw_branch/20110406/ippScripts/scripts/lap_science.pl	(revision 31396)
@@ -19,6 +19,8 @@
 my $regtool  = can_run('regtool') or (warn "Can't find regtool" and $missing_tools = 1);
 my $chiptool = can_run('chiptool') or (warn "Can't find chiptool" and $missing_tools = 1);
+my $warptool = can_run('warptool') or (warn "Can't find warptool" and $missing_tools = 1);
 my $difftool = can_run('difftool') or (warn "Can't find difftool" and $missing_tools = 1);
 my $stacktool = can_run('stacktool') or (warn "Can't find stacktool" and $missing_tools = 1);
+my $magicdstool = can_run('magicdstool') or (warn "Can't find magicdstool" and $missing_tools = 1);
 my $laptool  = can_run('laptool') or (warn "Can't find laptool" and $missing_tools = 1);
 
@@ -108,5 +110,5 @@
     my $lap_id = get_lap_id($proj_cell);
     
-    my $command = "$laptool -pendingrun -lap_id $lap_id";
+    my $command = "$laptool -pendingexp -lap_id $lap_id";
     $command .= " -dbname $dbname" if defined $dbname;
     my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
Index: /branches/czw_branch/20110406/ippTools/share/Makefile.am
===================================================================
--- /branches/czw_branch/20110406/ippTools/share/Makefile.am	(revision 31395)
+++ /branches/czw_branch/20110406/ippTools/share/Makefile.am	(revision 31396)
@@ -401,3 +401,11 @@
 	diffphottool_advance.sql \
 	diffphottool_revert.sql \
-	diffphottool_data.sql
+	diffphottool_data.sql \
+	laptool_definerun.sql \
+	laptool_exposures.sql \
+	laptool_inactiveexp.sql \ 
+	laptool_pendingchipexp.sql \
+	laptool_pendingexp.sql \
+	laptool_pendingrun.sql \
+	laptool_stacks.sql
+
Index: /branches/czw_branch/20110406/ippTools/share/laptool_definerun.sql
===================================================================
--- /branches/czw_branch/20110406/ippTools/share/laptool_definerun.sql	(revision 31395)
+++ /branches/czw_branch/20110406/ippTools/share/laptool_definerun.sql	(revision 31396)
@@ -3,5 +3,5 @@
   LEFT JOIN 
   (SELECT exp_id, chip_id
-     FROM reprocExp 
+     FROM lapExp 
      where private IS FALSE 
      AND data_state = 'full') AS old USING(exp_id)
Index: /branches/czw_branch/20110406/ippTools/share/laptool_exposures.sql
===================================================================
--- /branches/czw_branch/20110406/ippTools/share/laptool_exposures.sql	(revision 31395)
+++ /branches/czw_branch/20110406/ippTools/share/laptool_exposures.sql	(revision 31396)
Index: /branches/czw_branch/20110406/ippTools/share/laptool_inactiveexp.sql
===================================================================
--- /branches/czw_branch/20110406/ippTools/share/laptool_inactiveexp.sql	(revision 31396)
+++ /branches/czw_branch/20110406/ippTools/share/laptool_inactiveexp.sql	(revision 31396)
@@ -0,0 +1,28 @@
+SELECT DISTINCT 
+    D.*,diffRun.state,sum(others.active) AS is_in_use FROM (
+  SELECT DISTINCT 
+      W.*,IFNULL(diff1.diff_id,diff2.diff_id) AS diff_id FROM (
+    SELECT DISTINCT
+        lap_id,tess_id,projection_cell,filter,lapRun.state as lapRun_state, lapRun.registered, lapRun.fault, labRun.label,
+        lapExp.exp_id,lapExp.chip_id,lapExp.pair_id,private,pairwise,active,lapExp.data_state,
+        chipRun.state as chipRun_state, sum(chipProcessedImfile.fault) as chip_faults, sum(chipProcessedImfile.quality) as chip_quality,
+        camRun.state as camRun_state,   sum(camProcessedExp.fault) AS cam_faults, sum(camProcessedExp.quality) AS cam_quality,
+        fakeRun.state as fakeRun_state, sum(fakeProcessedImfile.fault) as fake_faults,
+        warpRun.state as warpRun_state, sum(warpSkyfile.fault) as warp_faults, sum(warpSkyfile.quality) as warp_quality,
+        warpRun.magicked
+    FROM lapRun JOIN lapExp USING(lap_id)
+    JOIN chipRun USING(chip_id,exp_id)
+    FROM chipRun JOIN chipProcessedImfile USING(chip_id)
+    LEFT JOIN camRun USING(chip_id) LEFT JOIN camProcessedExp USING(cam_id)
+    LEFT JOIN fakeRun USING(cam_id) LEFT JOIN fakeProcessedImfile USING(fake_id)
+    LEFT JOIN warpRun USING(fake_id) LEFT JOIN warpSkyfile USING(warp_id)
+    WHERE lapExp.active = FALSE
+    AND @WHERE@
+    ) AS W
+-- This was unreasonably slow in testing, so that's why I'm using a subquery here.
+  LEFT JOIN diffInputSkyfile AS diff1 ON (W.warp_id = diff1.warp1)
+  LEFT JOIN diffInputSkyfile AS diff2 ON (W.warp_id = diff2.warp2)
+) AS D
+LEFT JOIN diffRun USING(diff_id)
+JOIN lapExp AS others ON (D.chip_id = lapExp.chip_id AND D.lap_id != lapExp.lap_id)
+GROUP BY chip_id
Index: /branches/czw_branch/20110406/ippTools/share/laptool_pendingexp.sql
===================================================================
--- /branches/czw_branch/20110406/ippTools/share/laptool_pendingexp.sql	(revision 31396)
+++ /branches/czw_branch/20110406/ippTools/share/laptool_pendingexp.sql	(revision 31396)
@@ -0,0 +1,8 @@
+select lap_id, projection_cell, tess_id, registered, state, fault,
+       exp_id, chip_id, pair_id, private, pairwise, active, data_state,
+       dateobs, object, comment
+  FROM lapRun JOIN lapExp USING(lap_id)
+  JOIN rawExp USING(exp_id)
+WHERE active IS TRUE AND fault = 0
+-- lap_id restriction here.
+-- This probably needs to be sorted by dateobs.
Index: /branches/czw_branch/20110406/ippTools/share/laptool_pendingrun.sql
===================================================================
--- /branches/czw_branch/20110406/ippTools/share/laptool_pendingrun.sql	(revision 31395)
+++ /branches/czw_branch/20110406/ippTools/share/laptool_pendingrun.sql	(revision 31396)
@@ -1,8 +1,1 @@
-select lap_id, projection_cell, tess_id, registered, state, fault,
-       exp_id, chip_id, pair_id, private, pairwise, active, data_state,
-       dateobs, object, comment
-  FROM lapRun JOIN lapExp USING(lap_id)
-  JOIN rawExp USING(exp_id)
-WHERE active IS TRUE AND fault = 0
--- lap_id restriction here.
--- This probably needs to be sorted by dateobs.
+SELECT * from lapRun
Index: /branches/czw_branch/20110406/ippTools/share/pxadmin_create_tables.sql
===================================================================
--- /branches/czw_branch/20110406/ippTools/share/pxadmin_create_tables.sql	(revision 31395)
+++ /branches/czw_branch/20110406/ippTools/share/pxadmin_create_tables.sql	(revision 31396)
@@ -1891,4 +1891,59 @@
 ) ENGINE=innodb DEFAULT CHARSET=latin1;
 
+-- Tables for large area processing
+
+CREATE TABLE lapSequence (
+    seq_id BIGINT AUTO_INCREMENT, -- Identifier for the processing sequence
+    name VARCHAR(64) NOT NULL,    -- short name of the sequence
+    description VARCHAR(255) NOT NULL, -- longer description of the sequence
+    PRIMARY KEY(seq_id),
+    KEY(name)
+) ENGINE=innodb DEFAULT CHARSET=latin1;
+
+CREATE TABLE lapRun (
+    lap_id BIGINT AUTO_INCREMENT, -- Identifier for the processing run
+    seq_id BIGINT NOT NULL,       -- Identifier to match to the sequence
+    tess_id VARCHAR(64) NOT NULL, -- tessellation id to use
+    projection_cell VARCHAR(64) NOT NULL, -- projection cell from the tessellation to consider
+    filter VARCHAR(64) NOT NULL,  -- filter
+    state VARCHAR(64) NOT NULL,   -- state of run
+    label VARCHAR(64) NOT NULL,   -- processing label
+    dist_group VARCHAR(64) NOT NULL, -- distribution group for products of this run
+    registered TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- time run was registered
+    fault SMALLINT NOT NULL,      -- fault code
+    quick_sass_id,                -- stackAssociation id for quick stack
+    final_sass_id,                -- stackAssociation id for final stack
+    PRIMARY KEY(lap_id),
+    KEY(seq_id),
+    KEY(projection_cell),
+    KEY(filter),
+    KEY(state),
+    KEY(label),
+    KEY(fault),
+    FOREIGN KEY(seq_id) REFERENCES lapSequence(seq_id),
+    FOREIGN KEY(quick_sass_id) REFERENCES stackAssociation(sass_id),
+    FOREIGN KEY(final_sass_id) REFERENCES stackAssociation(sass_id)
+) ENGINE=innodb DEFAULT CHARSET=latin1;
+
+CREATE TABLE lapExp (
+    lap_id BIGINT NOT NULL, -- Link back to processing run
+    exp_id BIGINT NOT NULL, -- exposure definition
+    chip_id BIGINT,         -- processing id from chipRun
+    pair_id BIGINT,         -- companion chip_id
+    private TINYINT DEFAULT 0, -- denotes this exposure is private
+    pairwise TINYINT DEFAULT 0, -- denotes if this exposure should be pairwise diffed
+    active TINYINT DEFAULT 0, -- denotes if this exposure is currently in use
+    data_state VARCHAR(64) NOT NULL, -- state of exposure
+    PRIMARY KEY (lap_id),
+    KEY (exp_id),
+    KEY (chip_id),
+    KEY (pair_id),
+    KEY (data_state),
+    FOREIGN KEY (lap_id) REFERENCES lapRun(lap_id),
+    FOREIGN KEY (exp_id) REFERENCES rawExp(exp_id),
+    FOREIGN KEY (chip_id,exp_id) REFERENCES chipRun(chip_id,exp_id),
+    FOREIGN KEY (pair_id) REFERENCES chipRun(chip_id),
+) ENGINE=innodb DEFAULT CHARSET=latin1;    
+
 -- This comment line is here to avoid empty query error.
 -- Another way to avoid that problem is to omit the semicolon above but I think that is untidy.
Index: /branches/czw_branch/20110406/ippTools/src/Makefile.am
===================================================================
--- /branches/czw_branch/20110406/ippTools/src/Makefile.am	(revision 31395)
+++ /branches/czw_branch/20110406/ippTools/src/Makefile.am	(revision 31396)
@@ -28,5 +28,6 @@
 	pubtool \
 	diffphottool \
-	minidvodbtool
+	minidvodbtool \
+	laptool
 
 pkginclude_HEADERS = \
@@ -76,5 +77,6 @@
 	pubtool.h \
 	diffphottool.h \
-	minidvodbtool.h
+	minidvodbtool.h \
+	laptool.h
 
 lib_LTLIBRARIES = libpxtools.la
@@ -290,4 +292,10 @@
     minidvodbtoolConfig.c
 
+laptool_CFLAGS = $(PSLIB_CFLAGS) $(PSMODULES_CFLAGS) $(IPPDB_CFLAGS)
+laptool_LDADD = $(PSLIB_LIBS) $(PSMODULES_LIBS) $(IPPDB_LIBS) libpxtools.la
+laptool_SOURCES = \
+    laptool.c \
+    laptoolConfig.c
+
 clean-local:
 	-rm -f TAGS
Index: /branches/czw_branch/20110406/ippTools/src/laptool.c
===================================================================
--- /branches/czw_branch/20110406/ippTools/src/laptool.c	(revision 31395)
+++ /branches/czw_branch/20110406/ippTools/src/laptool.c	(revision 31396)
@@ -45,13 +45,17 @@
 
   switch (config->mode) {
+    MODECASE(LAPTOOL_MODE_DEFINESEQUENCE, definesequenceMode);
+    MODECASE(LAPTOOL_MODE_LISTSEQUENCE,   listsequenceMode);
+    
     MODECASE(LAPTOOL_MODE_DEFINERUN,     definerunMode);
     MODECASE(LAPTOOL_MODE_PENDINGRUN,    pendingrunMode);
     MODECASE(LAPTOOL_MODE_UPDATERUN,     updaterunMode);
 
-    MODECASE(LAPTOOL_MODE_PENDINGCHIPEXP,   pendingchipexpMode);
-    MODECASE(LAPTOOL_MODE_PENDINGQUICKSTACK,pendingquickstackMode);
-    MODECASE(LAPTOOL_MODE_PENDINGDIFF,      pendingdiffMode);
-    MODECASE(LAPTOOL_MODE_PENDINGFINALSTACK,pendingfinalstackMode);
-    MODECASE(LAPTOOL_MODE_UPDATEEXP,        updateexpMode);
+    MODECASE(LAPTOOL_MODE_PENDINGEXP,    pendingexpMode);
+    MODECASE(LAPTOOL_MODE_EXPOSURES,     exposuresMode);
+    MODECASE(LAPTOOL_MODE_STACKS,        stacksMode);
+    MODECASE(LAPTOOL_MODE_UPDATEEXP,     updateexpMode);
+
+    MODECASE(LAPTOOL_MODE_INACTIVEEXP,   inactiveexpMode);
   default:
     psAbort("invalid option (this should not happen)");
@@ -74,4 +78,121 @@
 }
 
+// Sequence level
+
+static bool definesequenceMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config, false);
+
+  PXOPT_LOOKUP_STR(name,        config->args, "-name",        true, false);
+  PXOPT_LOOKUP_STR(description, config->args, "-description", true, false);
+  PXOPT_LOOKUP_BOOL(simple,     config->args, "-simple",     false);
+
+  lapSequenceRow *run = lapSequenceRowAlloc(0, // seq_id
+					    name,
+					    description);
+  if (!run) {
+    psError(PS_ERR_UNKNOWN, false, "failed to alloc lapSequence object");
+    return(true);
+  }
+
+  if (!psDBTransaction(config->dbh)) {
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    return false;
+  }
+
+  if (!lapSequenceInsertObject(config->dbh, run)) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+    psError(PS_ERR_UNKNOWN, false "database error");
+    psFree(run);
+    return(true);
+  }
+
+  // point of no return
+  if (!psDBCommit(config->dbh)) {
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    return false;
+  }
+
+  if (lapSequencePrintObject(stdout, run, !simple)) {
+    psError(PS_ERR_UNKNOWN, false, "failed to print object");
+    psFree(run);
+    return false;
+  }
+
+  psFree(run);
+
+  return true;  
+}
+
+static bool listsequenceMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config, 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, "-seq_id", "seq_id", "==");
+  PXOPT_COPY_STR(config->args, where, "-name",   "name",   "LIKE");
+
+  psString query = pxDataGet("laptool_listsequence.sql");
+  if (!query) {
+    psError(PXTOOL_ERR_SYS, false, "failed to retrieve SQL statement");
+    return false;
+  }
+  if (psListLength(where->list)) {
+    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+    psStringAppend(&query, " WHERE %s", whereClause);
+    psFree(whereClause);
+  }
+
+  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) {
+    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");
+    }
+
+    return false;
+  }
+  if (!psArrayLength(output)) {
+    psTrace("laptool", PS_LOG_INFO, "no rows found");
+    psFree(output);
+    return true;
+  }
+
+  if (psArrayLength(output)) {
+    if (!ippdbPrintMetadatas(stdout, output, "lapSequence", !simple)) {
+      psError(PS_ERR_UNKNOWN, false, "failed to print array");
+      psFree(output);
+      return false;
+    }
+  }
+
+  psFree(output);
+
+  return true;
+}
+  
+
 // Run level
 static bool definerunMode(pxConfig *config)
@@ -79,4 +200,5 @@
   PS_ASSERT_PTR_NON_NULL(config, false);
 
+  PXOPT_LOOKUP_S64(seq_id,          config->args, "-seq_id",          true, false);
   PXOPT_LOOKUP_STR(projection_cell, config->args, "-projection_cell", true, false);
   PXOPT_LOOKUP_STR(tess_id,         config->args, "-tess_id",         true, false);
@@ -84,27 +206,53 @@
   PXOPT_LOOKUP_F64(decl,            config->args, "-decl",            true, false);
   PXOPT_LOOKUP_F32(radius,          config->args, "-radius",          true, false);
-
-  // Insert the run
-  if (!lapRunInsert(config->dbh,
-		       NULL,  // lap_id
-		       projection_cell,
-		       tess_id,
-		       ra,
-		       decl,
-		       radius,
-		       NULL,  // registered
-		       "new", // state
-		       0      // fault
-		       )) {
-    // rollback
+  PXOPT_LOOKUP_STR(filter,          config->args, "-filter",          true, false);
+  PXOPT_LOOKUP_STR(label,           config->args, "-label",           false, false);
+  PXOPT_LOOKUP_STR(dist_group,      config->args, "-dist_group",      false, false);
+
+  lapRunRow *run = lapRunRowAlloc(0, // lap_id
+				  seq_id,
+				  tess_id,
+				  projection_cell,
+				  filter,
+				  "new", // state
+				  label,
+				  dist_group,
+				  NULL, // registered
+				  0,    // fault
+				  NULL, // quick_sass_id
+				  NULL, // final_sass_id
+				  );
+  if (!run) {
+    psError(PS_ERR_UNKNOWN, false, "failed to alloc lapRun object");
+    return(true);
+  }
+
+  if (!psDBTransaction(config->dbh)) {
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    return false;
+  }
+
+  if (!lapSequenceInsertObject(config->dbh, run)) {
     if (!psDBRollback(config->dbh)) {
       psError(PS_ERR_UNKNOWN, false, "database error");
     }
-    psError(PS_ERR_UNKNOWN, false, "database error");
-    
-    return false;
-  }
+    psError(PS_ERR_UNKNOWN, false "database error");
+    psFree(run);
+    return(true);
+  }
+
+  // point of no return
+  if (!psDBCommit(config->dbh)) {
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    return false;
+  }
+
+  if (lapSequencePrintObject(stdout, run, !simple)) {
+    psError(PS_ERR_UNKNOWN, false, "failed to print object");
+    psFree(run);
+    return false;
+  }
+
   psS64 lap_id = psDBLastInsertID(config->dbh);
-
 
   // Find the input exposures
@@ -166,18 +314,196 @@
 {
   PS_ASSERT_PTR_NON_NULL(config, false);
+
   PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
   
-  psmetadata *where = psMetadataAlloc();
+  psMetadata *where = psMetadataAlloc();
+  PXOPT_COPY_S64(config->args, where, "-seq_id", "seq_id", "==");
   PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
   PXOPT_COPY_STR(config->args, where, "-projection_cell", "projection_cell", "==");
+  PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
+  PXOPT_COPY_STR(config->args, where, "-label", "label", "==");
+  PXOPT_COPY_STR(config->args, where, "-state", "state", "==");
+  PXOPT_COPY_STR(config->args, where, "-fault", "fault", "==");
 
   psString query = pxDataGet("laptool_pendingrun.sql");
   if (!query) {
     psError(PXTOOL_ERR_SYS, false, "failed to retrieve SQL statement");
+    return false;
+  }
+  if (psListLength(where->list)) {
+    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+    psStringAppend(&query, " WHERE %s", whereClause);
+    psFree(whereClause);
+  }
+  psStringAppend(&query, " ORDER BY rawExp.dateobs ");
+  
+  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) {
+    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");
+    }
+
+    return false;
+  }
+  if (!psArrayLength(output)) {
+    psTrace("laptool", PS_LOG_INFO, "no rows found");
+    psFree(output);
+    return true;
+  }
+
+  if (psArrayLength(output)) {
+    if (!ippdbPrintMetadatas(stdout, output, "lapRun", !simple)) {
+      psError(PS_ERR_UNKNOWN, false, "failed to print array");
+      psFree(output);
+      return false;
+    }
+  }
+
+  psFree(output);
+
+  return true;
+}
+
+static bool updaterunMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config, false);
+
+  psMetadata *where = psMetadataAlloc();
+  PXOPT_LOOKUP_S64(lap_id, config->args, "-lap_id", true, false);
+  PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
+
+  psMetadata *values = psMetadataAlloc();
+  PXOPT_LOOKUP_STR(state, config->args, "-set_state", false, false);
+  PXOPT_COPY_STR(config->args, values, "-set_state", "state", "==");
+  PXOPT_COPY_S16(config->args, values, "-fault",     "fault", "==");
+  PXOPT_COPY_STR(config->args, values, "-set_label", "label", "==");
+  PXOPT_COPY_S64(config->args, values, "-set_quick_sass_id", "quick_sass_id", "==");
+  PXOPT_COPY_S64(config->args, values, "-set_final_sass_id", "final_sass_id", "==");
+
+  long rows = psDBUpdateRows(config->dbh, "lapRun", where, values);
+  psFree(values);
+  
+  if (rows) {
+    // We're done with these exposures now, so mark them as inactive.
+    if ((strcmp(state,"drop") == 0)||
+	(strcmp(state,"full") == 0)) {
+      values = psMetadataAlloc();
+      psMetadataAddBool(values, PS_LIST_TAIL, "active", 0, "", false);
+      long exps = psDBUpdateRows(config->dbh, "lapExp", where, values);
+
+      if (exps) {
+	return(true);
+      }
+      else {
+	return(false);
+      }
+    }
+    return(true);
+  }
+  else {
+    return(false);
+  }
+}
+
+// Exposure level
+
+static bool pendingexpMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config, 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", "==");
+
+  psString query = pxDataGet("laptool_pendingexp.sql");
+  if (!query) {
+    psError(PXTOOL_ERR_SYS, false, "failed to retrieve SQL statement");
+    return false;
+  }
+  if (psListLength(where->list)) {
+    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
+    psStringAppend(&query, " WHERE %s", whereClause);
+    psFree(whereClause);
+  }
+
+  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) {
+    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");
+    }
+
+    return false;
+  }
+  if (!psArrayLength(output)) {
+    psTrace("laptool", PS_LOG_INFO, "no rows found");
+    psFree(output);
+    return true;
+  }
+
+  if (psArrayLength(output)) {
+    if (!ippdbPrintMetadatas(stdout, output, "lapExp", !simple)) {
+      psError(PS_ERR_UNKNOWN, false, "failed to print array");
+      psFree(output);
+      return false;
+    }
+  }
+
+  psFree(output);
+
+  return true;
+}
+
+
+static bool exposuresMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config, 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", "==");
+  PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
+  
+  psString query = pxDataGet("laptool_exposures.sql");
+  if (!query) {
+    psError(PXTOOL_ERR_SYS, false, "failed to retrieve SQL statement");
     return(false);
   }
   psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
   if (whereClause) {
-    psStringPrepend(&whereClause, "\n AND ");
+    psStringSubstitute(&query,whereClause,"@WHERE@");
   }
   
@@ -218,76 +544,16 @@
   psFree(output);
   return(true);
-  
-}
-static bool updaterunMode(pxConfig *config)
-{
-  PS_ASSERT_PTR_NON_NULL(config, false);
-
-  psMetadata *where = psMetadataAlloc();
-  PXOPT_LOOKUP_S64(lap_id, config->args, "-lap_id", true, false);
-  PXOPT_LOOKUP_S16(fault, config->args, "-fault", false, false);
-  PXOPT_LOOKUP_STR(set_state, config->args, "-set_state", false, false);
-
-  if ((fault == INT16_MAX)&&(!set_state)) {
-    psError(PS_ERR_UNKNOWN, false, "one of -fault or -set_state must be selected");
-    return(false);
-  }
-
+}
+
+static bool stacksMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config, 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", "==");
-
-  // Set a fault
-  if (fault != INT16_MAX) {
-    // this is fairly dangerous : can set all if the where is not set...
-    if (!pxSetFaultCode(config->dbh, "lapRun", where, fault, 0)) {
-      psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
-      psFree (where);
-      return false;
-    }
-    psFree (where);
-    return(true);
-  }
-
-  // Set the state, and deactivate the exposures if needed.
-
-  if ((strcmp(set_state,"drop") == 0)||
-      (strcmp(set_state,"full") == 0)) {
-    // Deactivate exposures
-
-    psString query = pxDataGet("laptool_updaterun_deactivate.sql");
-    if (!query) {
-      psError(PXTOOL_ERR_SYS, false, "failed to retrieve SQL statement");
-      return(false);
-    }
-    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-    psStringAppend(&query, whereClause, " \n ");
-    if (!p_psDBRunQuery(config->dbh, query)) {
-      psError(PS_ERR_UNKNOWN, false, "database error");
-      psFree(query);
-      return(false);
-    }
-    psFree(query);
-  }
-
-  char *query = "UPDATE lapRun SET state = '%s' WHERE lap_id = %"PRId64;
-  if (!p_psDBRunQueryF(config->dbh, query, state, lap_id)) {
-    psError(PS_ERR_UNKNOWN, false,
-	    "failed to change state for lap_id %"PRId64, lap_id);
-    return(false);
-  }
-
-  return(true);  
-}
-// Exposure level
-
-static bool exposuresMode(pxConfig *config)
-{
-  PS_ASSERT_PTR_NON_NULL(config, false);
-  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-  
-  psmetadata *where = psMetadataAlloc();
-  PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
-
-  psString query = pxDataGet("laptool_exposures.sql");
+  
+  psString query = pxDataGet("laptool_stacks.sql");
   if (!query) {
     psError(PXTOOL_ERR_SYS, false, "failed to retrieve SQL statement");
@@ -327,5 +593,5 @@
   }
   
-  if (!ippdbPrintMetadatas(stdout, output, "lapExp", !simple)) {
+  if (!ippdbPrintMetadatas(stdout, output, "lapRunStacks", !simple)) {
     psError(PS_ERR_UNKNOWN, false, "failed to print array");
     psFree(output);
@@ -335,19 +601,89 @@
   psFree(output);
   return(true);
-  
-}
-
-
-static bool pendingchipexpMode(pxConfig *config)
-{
-  PS_ASSERT_PTR_NON_NULL(config, false);
+}
+
+static bool updateexpMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config, false);
+
+  psMetadata *where = psMetadataAlloc();
+  PXOPT_LOOKUP_S64(lap_id,  config->args, "-lap_id",  true, false);
+  PXOPT_LOOKUP_S64(exp_id,  config->args, "-exp_id",  true, false);
+  PXOPT_LOOKUP_S64(chip_id, config->args, "-chip_id", false, false);
+
+  PXOPT_LOOKUP_S64(set_chip_id, config->args, "-set_chip_id", false, false);
+  PXOPT_LOOKUP_S64(set_pair_id, config->args, "-set_pair_id", false, false);
+  PXOPT_LOOKUP_BOOL(private,    config->args, "-private",     false, false);
+  PXOPT_LOOKUP_BOOL(public,     config->args, "-public",      false, false);
+  PXOPT_LOOKUP_BOOL(pairwise,   config->args, "-pairwise",    false, false);
+  PXOPT_LOOKUP_BOOL(nopairwise, config->args, "-nopairwise",  false, false);
+  PXOPT_LOOKUP_BOOL(active,     config->args, "-active",      false, false);
+  PXOPT_LOOKUP_BOOL(inactive,   config->args, "-inactive",    false, false);
+
+
+  if (private && public) {
+    psError(PS_ERR_UNKNOWN, false, "only one of -private and -public may be selected");
+  }
+  if (active && inactive) {
+    psError(PS_ERR_UNKNOWN, false, "only one of -active and -inactive may be selected");
+  }
+  if (pairwise && nopairwise) {
+    psError(PS_ERR_UNKNOWN, false, "only one of -pairwise and -nopairwise may be selected");
+  }
+
+  psMetadata *where = psMetadataAlloc();
+  PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
+  PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
+  PXOPT_COPY_S64(config->args, where, "-chip_id", "chip_id", "==");
+
+  psMetadata *values = psMetadataAlloc();
+  if (set_chip_id) {
+    PXOPT_COPY_S64(config->args, values, "-set_chip_id", "chip_id", "==");
+  }
+  if (set_pair_id) {
+    PXOPT_COPY_S64(config->args, values, "-set_pair_id", "pair_id", "==");
+  }
+  if (private) {
+    psMetadataAddBool(values, PS_LIST_TAIL, "private", 1, "", false);
+  }
+  else if (public) {
+    psMetadataAddBool(values, PS_LIST_TAIL, "private", 0, "", false);
+  }
+  if (active) {
+    psMetadataAddBool(values, PS_LIST_TAIL, "active", 1, "", false);
+  }
+  else if (inactive) {
+    psMetadataAddBool(values, PS_LIST_TAIL, "active", 0, "", false);
+  }
+  if (pairwise) {
+    psMetadataAddBool(values, PS_LIST_TAIL, "pairwise", 1, "", false);
+  }
+  else if (nopairwise) {
+    psMetadataAddBool(values, PS_LIST_TAIL, "pairwise", 0, "", false);
+  }
+  
+  long rows = psDBUpdateRows(config->dbh,"lapExp",where,values);
+  if (rows) {
+    return(true);
+  }
+  else {
+    return(false);
+  }  
+}
+
+
+    
+
+static bool inactiveexpMode(pxConfig *config)
+{
+  PS_ASSERT_PTR_NON_NULL(config, false);
+
+  psMetadata *where = psMetadataAlloc();
+  PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
   
   PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-  
-  psmetadata *where = psMetadataAlloc();
-  PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
-  PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
-
-  psString query = pxDataGet("laptool_pendingchipexp.sql");
+  PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
+  
+  psString query = pxDataGet("laptool_inactiveexp.sql");
   if (!query) {
     psError(PXTOOL_ERR_SYS, false, "failed to retrieve SQL statement");
@@ -356,5 +692,5 @@
   psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
   if (whereClause) {
-    psStringPrepend(&whereClause, "\n AND ");
+    psStringSubstitute(&query,whereClause,"@WHERE@");
   }
   
@@ -395,228 +731,4 @@
   psFree(output);
   return(true);
-  
-
-}
-static bool pendingquickstackMode(pxConfig *config)
-{
-  PS_ASSERT_PTR_NON_NULL(config, false);
-  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-  
-  psmetadata *where = psMetadataAlloc();
-  PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
-  PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
-
-  psString query = pxDataGet("laptool_pendingquickstack.sql");
-  if (!query) {
-    psError(PXTOOL_ERR_SYS, false, "failed to retrieve SQL statement");
-    return(false);
-  }
-  psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-  if (whereClause) {
-    psStringPrepend(&whereClause, "\n AND ");
-  }
-  
-  psString limitString = NULL;
-  if (limit) {
-    limitString = psDBGenerateLimitSQL(limit);
-    psStringPrepend(&limitString, "\n");
-  }
-
-  if (!p_psDBRunQueryF(config->dbh, query, whereClause, limitString ? limitString : "")) {
-    psError(PXTOOLS_ERR_PROG, false, "database error");
-    psFree(limitString);
-    psFree(query);
-    psFree(whereClause);
-    return(false);
-  }
-  psFree(limitString);
-  psFree(query);
-  psFree(whereClause);
-  
-  psArray *output = p_psDBFetchResult(config->dbh);
-  if (!output) {
-    psError(PS_ERR_UNKNOWN, false, "database error");
-    return(false);
-  }
-  if (!psArrayLength(output)) {
-    psTrace("laptool", PS_LOG_INFO, "no rows found");
-    psFree(output);
-    return(true);
-  }
-  
-  if (!ippdbPrintMetadatas(stdout, output, "lapExp", !simple)) {
-    psError(PS_ERR_UNKNOWN, false, "failed to print array");
-    psFree(output);
-    return(false);
-  }
-
-  psFree(output);
-  return(true);
-  
-
-}
-static bool pendingdiffMode(pxConfig *config)
-{
-  PS_ASSERT_PTR_NON_NULL(config, false);
-  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-  
-  psmetadata *where = psMetadataAlloc();
-  PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
-  PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
-
-  psString query = pxDataGet("laptool_pendingdiff.sql");
-  if (!query) {
-    psError(PXTOOL_ERR_SYS, false, "failed to retrieve SQL statement");
-    return(false);
-  }
-  psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-  if (whereClause) {
-    psStringPrepend(&whereClause, "\n AND ");
-  }
-  
-  psString limitString = NULL;
-  if (limit) {
-    limitString = psDBGenerateLimitSQL(limit);
-    psStringPrepend(&limitString, "\n");
-  }
-
-  if (!p_psDBRunQueryF(config->dbh, query, whereClause, limitString ? limitString : "")) {
-    psError(PXTOOLS_ERR_PROG, false, "database error");
-    psFree(limitString);
-    psFree(query);
-    psFree(whereClause);
-    return(false);
-  }
-  psFree(limitString);
-  psFree(query);
-  psFree(whereClause);
-  
-  psArray *output = p_psDBFetchResult(config->dbh);
-  if (!output) {
-    psError(PS_ERR_UNKNOWN, false, "database error");
-    return(false);
-  }
-  if (!psArrayLength(output)) {
-    psTrace("laptool", PS_LOG_INFO, "no rows found");
-    psFree(output);
-    return(true);
-  }
-  
-  if (!ippdbPrintMetadatas(stdout, output, "lapExp", !simple)) {
-    psError(PS_ERR_UNKNOWN, false, "failed to print array");
-    psFree(output);
-    return(false);
-  }
-
-  psFree(output);
-  return(true);
-  
-
-}
-static bool pendingfinalstackMode(pxConfig *config)
-{
-  PS_ASSERT_PTR_NON_NULL(config, false);
-  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-  
-  psmetadata *where = psMetadataAlloc();
-  PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
-  PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
-
-  psString query = pxDataGet("laptool_pendingfinalstack.sql");
-  if (!query) {
-    psError(PXTOOL_ERR_SYS, false, "failed to retrieve SQL statement");
-    return(false);
-  }
-  psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-  if (whereClause) {
-    psStringPrepend(&whereClause, "\n AND ");
-  }
-  
-  psString limitString = NULL;
-  if (limit) {
-    limitString = psDBGenerateLimitSQL(limit);
-    psStringPrepend(&limitString, "\n");
-  }
-
-  if (!p_psDBRunQueryF(config->dbh, query, whereClause, limitString ? limitString : "")) {
-    psError(PXTOOLS_ERR_PROG, false, "database error");
-    psFree(limitString);
-    psFree(query);
-    psFree(whereClause);
-    return(false);
-  }
-  psFree(limitString);
-  psFree(query);
-  psFree(whereClause);
-  
-  psArray *output = p_psDBFetchResult(config->dbh);
-  if (!output) {
-    psError(PS_ERR_UNKNOWN, false, "database error");
-    return(false);
-  }
-  if (!psArrayLength(output)) {
-    psTrace("laptool", PS_LOG_INFO, "no rows found");
-    psFree(output);
-    return(true);
-  }
-  
-  if (!ippdbPrintMetadatas(stdout, output, "lapExp", !simple)) {
-    psError(PS_ERR_UNKNOWN, false, "failed to print array");
-    psFree(output);
-    return(false);
-  }
-
-  psFree(output);
-  return(true);
-  
-
-}
-static bool updateexpMode(pxConfig *config)
-{
-  PS_ASSERT_PTR_NON_NULL(config, false);
-
-  psMetadata *where = psMetadataAlloc();
-  PXOPT_LOOKUP_S64(lap_id, config->args, "-lap_id", true, false);
-  PXOPT_LOOKUP_S64(exp_id, config->args, "-exp_id", true, false);
-  PXOPT_LOOKUP_S64(chip_id, config->args, "-chip_id", false, false);
-
-  PXOPT_LOOKUP_S64(set_chip_id, config->args, "-set_chip_id", false, false);
-  PXOPT_LOOKUP_BOOL(private, config->args, "-private", false, false);
-  PXOPT_LOOKUP_BOOL(active,  config->args, "-active", false, false);
-  PXOPT_LOOKUP_BOOL(inactive, config->args, "-inactive", false, false);
-
-  if (active == inactive) {
-    psError(PS_ERR_UNKNOWN, false, "only one of -active and -inactive may be selected");
-  }
-
-  psMetadata *where = psMetadataAlloc();
-  PXOPT_COPY_S64(config->args, where, "-lap_id", "lap_id", "==");
-  PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
-  PXOPT_COPY_S64(config->args, where, "-chip_id", "chip_id", "==");
-
-  psMetadata *values = psMetadataAlloc();
-  if (set_chip_id) {
-    PXOPT_COPY_S64(config->args, values, "-set_chip_id", "chip_id", "==");
-  }
-  if (private) {
-    PXOPT_COPY_BOOL(config->args, values, "-private", "private", "==");
-  }
-  if (active) {
-    PXOPT_COPY_BOOL(config->args, values, "-active", "active", "==");
-  }
-  if (inactive) {
-    psMetadataAddBool(values, PS_LIST_TAIL, "active", 0, "", false);
-  }
-
-  long rows = psDBUpdateRows(config->dbh,"lapExp",where,values);
-  if (rows) {
-    return(true);
-  }
-  else {
-    return(false);
-  }  
-}
-
-
-    
-	     
+}
+
Index: /branches/czw_branch/20110406/ippTools/src/laptool.h
===================================================================
--- /branches/czw_branch/20110406/ippTools/src/laptool.h	(revision 31395)
+++ /branches/czw_branch/20110406/ippTools/src/laptool.h	(revision 31396)
@@ -9,13 +9,14 @@
 
 typedef enum {
-  LAPTOOL_MODE_DEFINERUN          = 0x0,
+  LAPTOOL_MODE_DEFINESEQUENCE     = 0x0,
+  LAPTOOL_MODE_LISTSEQUENCE,
+  LAPTOOL_MODE_DEFINERUN,
   LAPTOOL_MODE_PENDINGRUN,
+  LAPTOOL_MODE_UPDATERUN,
+  LAPTOOL_MODE_PENDINGEXP,
   LAPTOOL_MODE_EXPOSURES,
-  LAPTOOL_MODE_PENDINGCHIPEXP,   
-  LAPTOOL_MODE_PENDINGQUICKSTACK,
-  LAPTOOL_MODE_PENDINGDIFF,      
-  LAPTOOL_MODE_PENDINGFINALSTACK,
-  LAPTOOL_MODE_UPDATERUN,        
-  LAPTOOL_MODE_UPDATEEXP
+  LAPTOOL_MODE_STACKS,
+  LAPTOOL_MODE_UPDATEEXP,
+  LAPTOOL_MODE_INACTIVEEXP
 } laptoolMode;
 
Index: /branches/czw_branch/20110406/ippTools/src/laptoolConfig.c
===================================================================
--- /branches/czw_branch/20110406/ippTools/src/laptoolConfig.c	(revision 31395)
+++ /branches/czw_branch/20110406/ippTools/src/laptoolConfig.c	(revision 31396)
@@ -32,6 +32,21 @@
   }
 
+  // -definesequence
+  psMetadata *definesequenceArgs = psMetadataAlloc();
+  ADD_OPT(Str, definesequenceArgs, "-name",                   "short name for this LAP sequence (required)", NULL);
+  ADD_OPT(Str, definesequenceArgs, "-description",            "define the description for this LAP sequence (required)", NULL);
+  ADD_OPT(Bool,definesequenceArgs, "-simple",                 "use the simple output format", false);
+  
+  // -listsequence
+  psMetadata *listsequenceArgs = psMetadataAlloc();
+  ADD_OPT(S64, listsequenceArgs, "-seq_id",                   "search by LAP sequence ID", 0);
+  ADD_OPT(Str, listsequenceArgs, "-seq_name",                 "search by LAP sequence name", 0);
+  ADD_OPT(Bool,listsequenceArgs, "-simple",                   "use the simple output format", false);
+  ADD_OPT(U64, listsequenceArgs, "-limit",                    "limit result set to N items", 0);
+
+  
   // -definerun
   psMetadata *definerunArgs = psMetadataAlloc();
+  ADD_OPT(S64, definerunArgs, "-seq_id",                      "define the LAP sequence for this run (required)", 0);
   ADD_OPT(Str, definerunArgs, "-projection_cell",             "define the projection cell for this run (required)", NULL);
   ADD_OPT(Str, definerunArgs, "-tess_id",                     "define the tessellation used (required)", NULL);
@@ -39,73 +54,83 @@
   ADD_OPT(F64, definerunArgs, "-decl",                        "define DEC center (required)", NAN);
   ADD_OPT(F32, definerunArgs, "-radius",                      "define radius from center to consider (required)", NAN);
-
+  ADD_OPT(Str, definerunArgs, "-filter",                      "define the filter used (required)", NULL);
+  ADD_OPT(Str, definerunArgs, "-label",                       "define the label used", NULL);
+  ADD_OPT(Str, definerunArgs, "-dist_group",                  "define the distribution group for this data", NULL);
+  
   // -pendingrun
   psMetadata *pendingrunArgs = psMetadataAlloc();
-  ADD_OPT(Str, pendingrunArgs, "-projection_cell",            "projection cell to consider", NULL);
-  ADD_OPT(S64, pendingrunArgs, "-lap_id",                      "lap run ID", 0);
-  ADD_OPT(U64, pendingrunArgs, "-limit",                      "limit result set to N items", 0);
-  ADD_OPT(Bool,pendingrunArgs, "-simple",                     "use the simple output format", false);
+  ADD_OPT(S64, pendingrunArgs, "-seq_id",                     "search by LAP sequence ID", 0);
+  ADD_OPT(S64, pendingrunArgs, "-lap_id",                     "search by LAP run ID", 0);
+  ADD_OPT(Str, pendingrunArgs, "-projection_cell",            "search by projection cell", NULL);
+  ADD_OPT(Str, pendingrunArgs, "-filter",                     "search by filter", NULL);
+  ADD_OPT(Str, pendingrunArgs, "-label",                      "search by LAP run label", NULL);
+  ADD_OPT(Str, pendingrunArgs, "-state",                      "search by LAP run state", NULL);
+  ADD_OPT(Str, pendingrunArgs, "-fault",                      "search by LAP run fault", NULL);
+
+  // -updaterun
+  psMetadata *updaterunArgs = psMetadataAlloc();
+  ADD_OPT(S64, updaterunArgs, "-lap_id",                      "search by lap run ID", 0);
+  ADD_OPT(Str, updaterunArgs, "-set_state",                   "set state", NULL);
+  ADD_OPT(S16, updaterunArgs, "-fault",                       "set fault code", INT16_MAX);
+  ADD_OPT(Str, updaterunArgs, "-set_label",                   "set label", NULL);
+  ADD_OPT(S64, updaterunArgs, "-set_quick_sass_id",           "set quick stack sass_id", NAN);
+  ADD_OPT(S64, updaterunArgs, "-set_final_sass_id",           "set final stack sass_id", NAN);
+  
+  
+  // -pendingexp
+  psMetadata *pendingexpArgs = psMetadataAlloc();
+  ADD_OPT(S64, pendingexpArgs, "-lap_id",                     "lap run ID", 0);
+  ADD_OPT(Str, pendingexpArgs, "-projection_cell",            "projection cell to consider", NULL);
+  ADD_OPT(U64, pendingexpArgs, "-limit",                      "limit result set to N items", 0);
+  ADD_OPT(Bool,pendingexpArgs, "-simple",                     "use the simple output format", false);
 
   // -exposures
   psMetadata *exposuresArgs = psMetadataAlloc();
-  ADD_OPT(S64, exposuresArgs, "-lap_id",                       "search by lap run ID", 0);
-  ADD_OPT(Bool,exposuresArgs, "-simple",                       "use the simple output format", false);
+  ADD_OPT(S64, exposuresArgs, "-lap_id",                      "search by lap run ID", 0);
+  ADD_OPT(S64, exposuresArgs, "-exp_id",                      "search by exp_id", 0);
+  ADD_OPT(Bool,exposuresArgs, "-simple",                      "use the simple output format", false);
+  ADD_OPT(U64, exposuresArgs, "-limit",                       "limit result set to N items", 0);
 
-  // -pendingexp
-  psMetadata *pendingchipexpArgs = psMetadataAlloc();
-  ADD_OPT(S64, pendingchipexpArgs, "-lap_id",                  "search by lap run ID", 0);
-  ADD_OPT(Str, pendingchipexpArgs, "-filter",                 "search by filter", 0);
-  ADD_OPT(Bool,pendingchipexpArgs, "-simple",                 "use the simple output format", false);
-
+  // -stacks
+  psMetadata *stacksArgs = psMetadataAlloc();
+  ADD_OPT(S64, stacksArgs, "-lap_id",                         "search by lap run ID", 0);
+  ADD_OPT(Bool,stacksArgs, "-simple",                         "use the simple output format", false);
+  ADD_OPT(U64, stacksArgs, "-limit",                          "limit result set to N items", 0);
   
-  // -pendingquickstack
-  psMetadata *pendingquickstackArgs = psMetadataAlloc();
-  ADD_OPT(S64, pendingquickstackArgs, "-lap_id",               "search by lap run ID", 0);
-  ADD_OPT(Str, pendingquickstackArgs, "-filter",              "search by filter", 0);
-  ADD_OPT(Bool,pendingquickstackArgs, "-simple",              "use the simple output format", false);
-  
-  // -pendingdiff
-  psMetadata *pendingdiffArgs = psMetadataAlloc();
-  ADD_OPT(S64, pendingdiffArgs, "-lap_id",                    "search by lap run ID", 0);
-  ADD_OPT(Str, pendingdiffArgs, "-filter",                   "search by filter", 0);
-  ADD_OPT(Bool,pendingdiffArgs, "-simple",                   "use the simple output format", false);
-  
-  // -pendingfinalstack
-  psMetadata *pendingfinalstackArgs = psMetadataAlloc();
-  ADD_OPT(S64, pendingfinalstackArgs, "-lap_id",               "search by lap run ID", 0);
-  ADD_OPT(Str, pendingfinalstackArgs, "-filter",              "search by filter", 0);
-  ADD_OPT(Bool,pendingfinalstackArgs, "-simple",              "use the simple output format", false);
-
-  // -updaterun
-  psMetadata *updaterunArgs = psMetadataAlloc();
-  ADD_OPT(S64, updaterunArgs, "-lap_id",                       "search by lap run ID", 0);
-  ADD_OPT(Str, updaterunArgs, "-set_state",                   "set state", NULL);
-  ADD_OPT(S16, updaterunArgs, "-fault",                       "set fault code", INT16_MAX);
-
   // -updateexp
   psMetadata *updateexpArgs = psMetadataAlloc();
-  ADD_OPT(S64, updateexpArgs, "-lap_id",                       "search by lap run ID", 0);
+  ADD_OPT(S64, updateexpArgs, "-lap_id",                      "search by lap run ID", 0);
   ADD_OPT(S64, updateexpArgs, "-exp_id",                      "search by exposure ID", 0);
   ADD_OPT(S64, updateexpArgs, "-chip_id",                     "search by chip ID", 0);
   ADD_OPT(S64, updateexpArgs, "-set_chip_id",                 "set the chip ID", 0);
+  ADD_OPT(S64, updateexpArgs, "-set_pair_id",                 "set the pair ID", 0);
   ADD_OPT(Bool,updateexpArgs, "-private",                     "set this exposure as private", 0);
+  ADD_OPT(Bool,updateexpArgs, "-public",                      "set this exposure as public", 0);
+  ADD_OPT(Bool,updateexpArgs, "-pairwise",                    "set this exposure to be pairwise", 0);
+  ADD_OPT(Bool,updateexpArgs, "-nopairwise",                  "set this exposure to not be pairwise", 0);
   ADD_OPT(Bool,updateexpArgs, "-active",                      "set this exposure to active", 0);
   ADD_OPT(Bool,updateexpArgs, "-inactive",                    "set this exposure to active", 0);
 
-
+  // -inactiveexp
+  psMetadata *inactiveexpArgs = psMetadataAlloc();
+  ADD_OPT(S64, inactiveexpArgs, "-lap_id",                    "search by lap run ID", 0);
+  ADD_OPT(Bool,inactiveexpArgs, "-simple",                    "use the simple output format", false);
+  ADD_OPT(U64, inactiveexpArgs, "-limit",                     "limit result set to N items", 0);
+  
   
   psMetadata *argSets = psMetadataAlloc();
   psMetadata *modes = psMetadataAlloc();
 
+  PXOPT_ADD_MODE("-definesequence",          "", LAPTOOL_MODE_DEFINESEQUENCE,   definesequenceArgs);
+  PXOPT_ADD_MODE("-listsequence",            "", LAPTOOL_MODE_LISTSEQUENCE,     listsequenceArgs);
   PXOPT_ADD_MODE("-definerun",               "", LAPTOOL_MODE_DEFINERUN,        definerunArgs);
   PXOPT_ADD_MODE("-pendingrun",              "", LAPTOOL_MODE_PENDINGRUN,       pendingrunArgs);
+  PXOPT_ADD_MODE("-updaterun",               "", LAPTOOL_MODE_UPDATERUN,        updaterunArgs);
+  PXOPT_ADD_MODE("-pendingexp",              "", LAPTOOL_MODE_PENDINGEXP,       pendingexpArgs);
   PXOPT_ADD_MODE("-exposures",               "", LAPTOOL_MODE_EXPOSURES,        exposuresArgs);
-  PXOPT_ADD_MODE("-pendingchipexp",          "", LAPTOOL_MODE_PENDINGCHIPEXP,   pendingchipexpArgs);
-  PXOPT_ADD_MODE("-pendingquickstack",       "", LAPTOOL_MODE_PENDINGQUICKSTACK,pendingquickstackArgs);
-  PXOPT_ADD_MODE("-pendingdiff",             "", LAPTOOL_MODE_PENDINGDIFF,      pendingdiffArgs);
-  PXOPT_ADD_MODE("-pendingfinalstack",       "", LAPTOOL_MODE_PENDINGFINALSTACK,pendingfinalstackArgs);
-  PXOPT_ADD_MODE("-updaterun",               "", LAPTOOL_MODE_UPDATERUN,        updaterunArgs);
+  PXOPT_ADD_MODE("-stacks",                  "", LAPTOOL_MODE_STACKS,           stacksArgs);
   PXOPT_ADD_MODE("-updateexp",               "", LAPTOOL_MODE_UPDATEEXP,        updateexpArgs);
-
+  PXOPT_ADD_MODE("-inactiveexp",             "", LAPTOOL_MODE_INACTIVEEXP,      inactiveexpArgs);
+  
   if (!pxGetOptions(stderr, argc, argv, config, modes, argSets)) {
     psError(PS_ERR_UNKNOWN, true, "option parsing failed");
