Index: branches/eam_branches/ipp-20130509/ippTools/src/chiptool.c
===================================================================
--- branches/eam_branches/ipp-20130509/ippTools/src/chiptool.c	(revision 35565)
+++ branches/eam_branches/ipp-20130509/ippTools/src/chiptool.c	(revision 35594)
@@ -269,5 +269,5 @@
 
     PXOPT_LOOKUP_STR(workdir, config->args, "-set_workdir", true, false);
-    PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
+    PXOPT_LOOKUP_STR(label, config->args, "-set_label", true, false);
     PXOPT_LOOKUP_STR(reduction, config->args, "-set_reduction", false, false);
     PXOPT_LOOKUP_STR(expgroup, config->args, "-set_expgroup", false, false);
@@ -298,9 +298,11 @@
     psFree(where);
 
+    psString labelHook = NULL;
     if (unique) {
-      psStringAppend(&query, "AND chip_id IS NULL");
+      psStringAppend(&labelHook, "\nAND chipRun.label = '%s'", label);
+      psStringAppend(&query, " AND chip_id IS NULL");
     }
     
-    if (!p_psDBRunQuery(config->dbh, query)) {
+    if (!p_psDBRunQueryF(config->dbh, query, labelHook ? labelHook : "")) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(query);
Index: branches/eam_branches/ipp-20130509/ippTools/src/chiptoolConfig.c
===================================================================
--- branches/eam_branches/ipp-20130509/ippTools/src/chiptoolConfig.c	(revision 35565)
+++ branches/eam_branches/ipp-20130509/ippTools/src/chiptoolConfig.c	(revision 35594)
@@ -52,5 +52,5 @@
 
     psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-set_workdir",  0,            "define workdir (required)", NULL);
-    psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-set_label",  0,            "define label", NULL);
+    psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-set_label",  0,            "define label (required)", NULL);
     psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-set_reduction",  0,            "define reduction class", NULL);
     psMetadataAddStr(definebyqueryArgs, PS_LIST_TAIL, "-set_expgroup",  0,            "define exposure group", NULL);
Index: branches/eam_branches/ipp-20130509/ippTools/src/laptool.c
===================================================================
--- branches/eam_branches/ipp-20130509/ippTools/src/laptool.c	(revision 35565)
+++ branches/eam_branches/ipp-20130509/ippTools/src/laptool.c	(revision 35594)
@@ -22,6 +22,8 @@
 // Run level
 static bool definerunMode(pxConfig *config);
+static bool definerunbyreleaseMode(pxConfig *config);
 static bool pendingrunMode(pxConfig *config);
 static bool updaterunMode(pxConfig *config);
+
 // Exposure level
 static bool pendingexpMode(pxConfig *config);
@@ -64,4 +66,5 @@
     
     MODECASE(LAPTOOL_MODE_DEFINERUN,     definerunMode);
+    MODECASE(LAPTOOL_MODE_DEFINERUNBYRELEASE, definerunbyreleaseMode);
     MODECASE(LAPTOOL_MODE_PENDINGRUN,    pendingrunMode);
     MODECASE(LAPTOOL_MODE_UPDATERUN,     updaterunMode);
@@ -401,4 +404,179 @@
 }
 
+static bool definerunbyreleaseMode(pxConfig *config)
+{
+  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);
+  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);
+  PXOPT_LOOKUP_S64(rel_id,          config->args, "-rel_id",          false, false);
+  PXOPT_LOOKUP_STR(rel_name,        config->args, "-release_name",    false, false);
+  PXOPT_LOOKUP_BOOL(simple,         config->args, "-simple",          false);
+
+  // Validate config
+  if ((!rel_name)&&(!rel_id)) {
+    // Die here.
+  }
+  
+  lapRunRow *run = lapRunRowAlloc(0, // lap_id
+				  seq_id,
+				  tess_id,
+				  projection_cell,
+				  filter,
+				  "registered", // state
+				  label,
+				  dist_group,
+				  NULL, // registered
+				  0,    // fault
+				  INT64_MAX,    // quick_sass_id
+				  INT64_MAX     // 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 (!lapRunInsertObject(config->dbh, run)) {
+    if (!psDBRollback(config->dbh)) {
+      psError(PS_ERR_UNKNOWN, false, "database error");
+    }
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    psFree(run);
+    return(false);
+  }
+
+  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);
+    return(false);
+  }
+
+  psS64 lap_id = psDBLastInsertID(config->dbh);
+
+  psString query = pxDataGet("laptool_definerunbyrelease.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);
+  }
+  
+  // Add constraints
+  psMetadata *relWhere = psMetadataAlloc();
+  PXOPT_COPY_STR(config->args, relWhere, "-filter",       "rawExp.filter", "==");
+  PXOPT_COPY_STR(config->args, relWhere, "-release_name", "ippRelease.release_name", "==");
+  PXOPT_COPY_S64(config->args, relWhere, "-rel_id",       "ippRelease.rel_id", "==");
+  PXOPT_COPY_TIME(config->args, relWhere, "-dateobs_begin", "rawExp.dateobs", ">=");
+  PXOPT_COPY_TIME(config->args, relWhere, "-dateobs_end", "rawExp.dateobs", "<=");
+  PXOPT_COPY_RADEC(config->args, relWhere, "-ra_min", "rawExp.ra", ">=");
+  PXOPT_COPY_RADEC(config->args, relWhere, "-ra_max", "rawExp.ra", "<");
+  PXOPT_COPY_RADEC(config->args, relWhere, "-decl_min", "rawExp.decl", ">=");
+  PXOPT_COPY_RADEC(config->args, relWhere, "-decl_max", "rawExp.decl", "<");
+
+  psMetadata *lapWhere = psMetadataAlloc();
+  PXOPT_COPY_S64(config->args, lapWhere, "-seq_id",       "lapRun.seq_id", "==");
+  PXOPT_COPY_STR(config->args, lapWhere, "-filter",       "lapRun.filter", "==");
+
+  psString relWhereClause = psDBGenerateWhereConditionSQL(relWhere,NULL);
+  psString lapWhereClause = psDBGenerateWhereConditionSQL(lapWhere,NULL);
+
+  if (relWhereClause) {
+    psStringSubstitute(&query,relWhereClause,"@RELWHERE@");
+  }
+  if (lapWhereClause) {
+    psStringSubstitute(&query,lapWhereClause,"@LAPWHERE@");
+  }
+  psFree(relWhere);
+  psFree(lapWhere);
+
+  // 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);
+    return(false);
+  }
+  psFree(query);
+
+  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);
+    return(true);
+  }
+
+  // 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");
+      }
+      psError(PS_ERR_UNKNOWN, false, "database error");
+      psFree(output);
+      psFree(lapExp);
+      return(false);
+    }
+  }
+
+  // point of no return
+  if (!psDBCommit(config->dbh)) {
+    psError(PS_ERR_UNKNOWN, false, "database error");
+    return false;
+  }
+
+  
+  psFree(output);
+  return(true);  
+}
+
+
+  
+  
+		   
 static bool pendingrunMode(pxConfig *config)
 {
Index: branches/eam_branches/ipp-20130509/ippTools/src/laptool.h
===================================================================
--- branches/eam_branches/ipp-20130509/ippTools/src/laptool.h	(revision 35565)
+++ branches/eam_branches/ipp-20130509/ippTools/src/laptool.h	(revision 35594)
@@ -13,4 +13,5 @@
   LAPTOOL_MODE_LISTSEQUENCE,
   LAPTOOL_MODE_DEFINERUN,
+  LAPTOOL_MODE_DEFINERUNBYRELEASE,
   LAPTOOL_MODE_PENDINGRUN,
   LAPTOOL_MODE_UPDATERUN,
Index: branches/eam_branches/ipp-20130509/ippTools/src/laptoolConfig.c
===================================================================
--- branches/eam_branches/ipp-20130509/ippTools/src/laptoolConfig.c	(revision 35565)
+++ branches/eam_branches/ipp-20130509/ippTools/src/laptoolConfig.c	(revision 35594)
@@ -57,9 +57,28 @@
   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);
+
+  // -definerunbyrelease
+  psMetadata *definerunbyreleaseArgs = psMetadataAlloc();
+  ADD_OPT(S64, definerunbyreleaseArgs, "-seq_id",             "define the LAP sequence for this run (required)", 0);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-projection_cell",    "define the projection cell for this run (required)", NULL);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-tess_id",            "define the tessellation used (required)", NULL);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-filter",             "define the filter used (required)", NULL);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-label",              "define the label used", NULL);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-dist_group",         "define the distribution group for this data", NULL);
+  ADD_OPT(S64, definerunbyreleaseArgs, "-rel_id",             "define the release to copy", 0);
+  ADD_OPT(Str, definerunbyreleaseArgs, "-release_name",       "define the release to copy", NULL);
+  
+  ADD_OPT(Bool, definerunbyreleaseArgs, "-simple",            "use the simple output format", false);
+  psMetadataAddTime(definerunbyreleaseArgs, PS_LIST_TAIL, "-dateobs_begin",      0, "search for exposures by time (>=)", NULL);
+  psMetadataAddTime(definerunbyreleaseArgs, PS_LIST_TAIL, "-dateobs_end",        0, "search for exposures by time (<=)", NULL);
+  psMetadataAddF64(definerunbyreleaseArgs,  PS_LIST_TAIL, "-ra_min",             0, "search by min RA (degrees) ", NAN);
+  psMetadataAddF64(definerunbyreleaseArgs,  PS_LIST_TAIL, "-ra_max",             0, "search by max RA (degrees) ", NAN);
+  psMetadataAddF64(definerunbyreleaseArgs,  PS_LIST_TAIL, "-decl_min",           0, "search by min DEC (degrees)", NAN);
+  psMetadataAddF64(definerunbyreleaseArgs,  PS_LIST_TAIL, "-decl_max",           0, "search by max DEC (degrees)", NAN);
   
   // -pendingrun
@@ -204,4 +223,5 @@
   PXOPT_ADD_MODE("-listsequence",            "", LAPTOOL_MODE_LISTSEQUENCE,     listsequenceArgs);
   PXOPT_ADD_MODE("-definerun",               "", LAPTOOL_MODE_DEFINERUN,        definerunArgs);
+  PXOPT_ADD_MODE("-definerunbyrelease",      "", LAPTOOL_MODE_DEFINERUNBYRELEASE, definerunbyreleaseArgs);
   PXOPT_ADD_MODE("-pendingrun",              "", LAPTOOL_MODE_PENDINGRUN,       pendingrunArgs);
   PXOPT_ADD_MODE("-listrun",                 "", LAPTOOL_MODE_PENDINGRUN,       listrunArgs);
