Index: /branches/czw_branch/cleanup/ippScripts/scripts/ipp_cleanup.pl
===================================================================
--- /branches/czw_branch/cleanup/ippScripts/scripts/ipp_cleanup.pl	(revision 24971)
+++ /branches/czw_branch/cleanup/ippScripts/scripts/ipp_cleanup.pl	(revision 24972)
@@ -767,16 +767,20 @@
     }
 
-    # if there are no detProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
-    if (@$stdout_buf == 0)  {
-	my $command = "$dettool -det_id $stage_id -updateprocessedimfile -data_state new";
-	$command .= " -dbname $dbname" if defined $dbname;
-
-	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	    run(command => $command, verbose => $verbose);
-	unless ($success) {
-	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
-	}
-	exit 0;
+#     # if there are no detProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
+#     if (@$stdout_buf == 0)  {
+# 	my $command = "$dettool -det_id $stage_id -updateprocessedimfile -data_state new";
+# 	$command .= " -dbname $dbname" if defined $dbname;
+
+# 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+# 	    run(command => $command, verbose => $verbose);
+# 	unless ($success) {
+# 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+# 	    &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
+# 	}
+# 	exit 0;
+#     }
+    
+    if (@$stdout_buf == 0) {
+	exit 0; #silently abort. I need to fix this for propers
     }
 
@@ -891,28 +895,30 @@
 	&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     }
-    $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-    $exps = parse_md_list($metadata) or
-	&my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-
-    foreach my $exp (@$exps) {
-	my $iteration = $exp->{iteration};
-	my $command;
-	if ($mode eq "goto_cleaned") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
-	}
-	if ($mode eq "goto_scrubbed") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
-	}
-	if ($mode eq "goto_purged") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
-	}
-	$command .= " -dbname $dbname" if defined $dbname;
-        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-            run(command => $command, verbose => $verbose);
-        unless ($success) {
-            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
-        }
+    if (@$stdout_buf != 0) {
+	$metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	my $exps = parse_md_list($metadata) or
+	    &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	
+	foreach my $exp (@$exps) {
+	    my $iteration = $exp->{iteration};
+	    my $command;
+	    if ($mode eq "goto_cleaned") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
+	    }
+	    if ($mode eq "goto_scrubbed") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
+	    }
+	    if ($mode eq "goto_purged") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
+	    }
+	    $command .= " -dbname $dbname" if defined $dbname;
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
+	    }
+	}
     }
     
@@ -921,6 +927,6 @@
 if ($stage eq "detrend.process.exp") {
     die "--stage_id required for stage $stage\n" if !$stage_id;
-    # this stage uses 'camtool'
-    my $camtool = can_run('dettool') or die "Can't find dettool";
+    # this stage uses 'dettool'
+    my $dettool = can_run('dettool') or die "Can't find dettool";
 
     # Get list of component imfiles
@@ -934,4 +940,9 @@
         &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
     }
+    
+    if (@$stdout_buf == 0) {
+	exit 0; #silently abort. I need to fix this for propers
+    }
+
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
         &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
@@ -1019,28 +1030,30 @@
 	&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     }
-    $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-    $exps = parse_md_list($metadata) or
-	&my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-
-    foreach my $exp (@$exps) {
-	my $iteration = $exp->{iteration};
-	my $command;
-	if ($mode eq "goto_cleaned") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
-	}
-	if ($mode eq "goto_scrubbed") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
-	}
-	if ($mode eq "goto_purged") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
-	}
-	$command .= " -dbname $dbname" if defined $dbname;
-        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-            run(command => $command, verbose => $verbose);
-        unless ($success) {
-            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
-        }
+    if (@$stdout_buf != 0) {
+	$metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	$exps = parse_md_list($metadata) or
+	    &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	
+	foreach my $exp (@$exps) {
+	    my $iteration = $exp->{iteration};
+	    my $command;
+	    if ($mode eq "goto_cleaned") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
+	    }
+	    if ($mode eq "goto_scrubbed") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
+	    }
+	    if ($mode eq "goto_purged") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
+	    }
+	    $command .= " -dbname $dbname" if defined $dbname;
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
+	    }
+	}
     }
 
@@ -1051,10 +1064,10 @@
     die "--stage_id required for stage $stage\n" if !$stage_id;
 
-    # this stage uses 'stacktool'
-    my $stacktool = can_run('dettool') or die "Can't find dettool";
+    # this stage uses 'dettool'
+    my $dettool = can_run('dettool') or die "Can't find dettool";
 
     # Get list of component imfiles
-    my $skyfiles;                  # Array reference of component files
-    my $command = "dettool -pendingcleanup_stacked -det_id $stage_id"; # Command to run
+    my $stacks;                  # Array reference of component files
+    my $command = "$dettool -pendingcleanup_stacked -det_id $stage_id"; # Command to run
     $command .= " -dbname $dbname" if defined $dbname;
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
@@ -1066,5 +1079,5 @@
 	&my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
 
-    $skyfiles = parse_md_list($metadata) or 
+    $stacks = parse_md_list($metadata) or 
 	&my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
     
@@ -1079,29 +1092,29 @@
 
 	my $status = 1;
-	if ($mode eq "goto_cleaned") {
-	    my $config_file = $ipprc->filename("PPMERGE.CONFIG", $path_base, $stage_id);
-
-	    $config_file =~ s%^file://%%;
-	    if (!$config_file or ! -e $config_file) {
-		print STDERR "skipping cleanup for $stage $stage_id $path_base" .
-		    " because config file is missing\n";
-		$status = 0;
-	    }
-	    $config_file = 'file://' . $config_file;
-	}
-	elsif ($mode eq "goto_scrubbed") {
-	    my $config_file = $ipprc->filename("PPMERGE.CONFIG", $path_base, $stage_id);
-	    $config_file =~ s%^file://%%;
-	    if ($config_file and -e $config_file) {
-		print STDERR "skipping scrubbed for $stage $stage_id $path_base" .
-		    " because config file is present\n";
-		$status = 0;
-	    }
-	}
+# 	if ($mode eq "goto_cleaned") {
+# 	    my $config_file = $ipprc->filename("PPMERGE.CONFIG", $path_base, $stage_id);
+
+# 	    $config_file =~ s%^file://%%;
+# 	    if (!$config_file or ! -e $config_file) {
+# 		print STDERR "skipping cleanup for $stage $stage_id $path_base" .
+# 		    " because config file is missing\n";
+# 		$status = 0;
+# 	    }
+# 	    $config_file = 'file://' . $config_file;
+# 	}
+# 	elsif ($mode eq "goto_scrubbed") {
+# 	    my $config_file = $ipprc->filename("PPMERGE.CONFIG", $path_base, $stage_id);
+# 	    $config_file =~ s%^file://%%;
+# 	    if ($config_file and -e $config_file) {
+# 		print STDERR "skipping scrubbed for $stage $stage_id $path_base" .
+# 		    " because config file is present\n";
+# 		$status = 0;
+# 	    }
+# 	}
 	if ($status) {
 	    # delete the temporary image datafiles
-	    addFilename(\@files, "PPMERGE.OUTPUT", $path_base, $skycell_id);
-	    addFilename(\@files, "PPMERGE.OUTPUT.SIGMA", $path_base, $skycell_id);
-	    addFilename(\@files, "PPMERGE.OUTPUT.COUNT", $path_base, $skycell_id);
+#	    addFilename(\@files, "PPMERGE.OUTPUT", $path_base, $stage_id);
+	    addFilename(\@files, "PPMERGE.OUTPUT.SIGMA", $path_base, $stage_id);
+	    addFilename(\@files, "PPMERGE.OUTPUT.COUNT", $path_base, $stage_id);
 
 	    if ($mode eq "goto_purged") {
@@ -1154,30 +1167,31 @@
 	&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     }
-    $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-    $exps = parse_md_list($metadata) or
-	&my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-
-    foreach my $exp (@$exps) {
-	my $iteration = $exp->{iteration};
-	my $command;
-	if ($mode eq "goto_cleaned") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
-	}
-	if ($mode eq "goto_scrubbed") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
-	}
-	if ($mode eq "goto_purged") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
-	}
-	$command .= " -dbname $dbname" if defined $dbname;
-        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-            run(command => $command, verbose => $verbose);
-        unless ($success) {
-            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
-        }
-    }
-
+    if (@$stdout_buf != 0) {
+	$metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	my $exps = parse_md_list($metadata) or
+	    &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	
+	foreach my $exp (@$exps) {
+	    my $iteration = $exp->{iteration};
+	    my $command;
+	    if ($mode eq "goto_cleaned") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
+	    }
+	    if ($mode eq "goto_scrubbed") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
+	    }
+	    if ($mode eq "goto_purged") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
+	    }
+	    $command .= " -dbname $dbname" if defined $dbname;
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
+	    }
+	}
+    }
     exit 0;
 }
@@ -1186,5 +1200,5 @@
     die "--stage_id required for stage $stage\n" if !$stage_id;
     # this stage uses 'camtool'
-    my $camtool = can_run('dettool') or die "Can't find dettool";
+    my $dettool = can_run('dettool') or die "Can't find dettool";
 
     my $command = "$dettool -pendingcleanup_normalizedstat -det_id $stage_id"; # Command to run
@@ -1198,9 +1212,9 @@
         &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
 
-    $exps = parse_md_list($metadata) or
+    my $exps = parse_md_list($metadata) or
         &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
 
     foreach my $exp (@$exps) {
-	my $path_base = $exp->{path_base};
+#	my $path_base = $exp->{path_base};
 	my $iteration = $exp->{iteration};
 	my $class_id  = $exp->{class_id};
@@ -1222,4 +1236,5 @@
 		run(command => $command, verbose => $verbose);
 	    unless ($success) {
+		print STDERR "WTF: $success TTT $error_code QQQ\n";
 		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
 		&my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
@@ -1248,28 +1263,30 @@
 	&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     }
-    $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-    $exps = parse_md_list($metadata) or
-	&my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-
-    foreach my $exp (@$exps) {
-	my $iteration = $exp->{iteration};
-	my $command;
-	if ($mode eq "goto_cleaned") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
-	}
-	if ($mode eq "goto_scrubbed") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
-	}
-	if ($mode eq "goto_purged") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
-	}
-	$command .= " -dbname $dbname" if defined $dbname;
-        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-            run(command => $command, verbose => $verbose);
-        unless ($success) {
-            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
-        }
+    if (@$stdout_buf != 0) {
+	$metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	$exps = parse_md_list($metadata) or
+	    &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	
+	foreach my $exp (@$exps) {
+	    my $iteration = $exp->{iteration};
+	    my $command;
+	    if ($mode eq "goto_cleaned") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
+	    }
+	    if ($mode eq "goto_scrubbed") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
+	    }
+	    if ($mode eq "goto_purged") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
+	    }
+	    $command .= " -dbname $dbname" if defined $dbname;
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
+	    }
+	}
     }
 
@@ -1280,5 +1297,5 @@
     die "--stage_id required for stage $stage\n" if !$stage_id;
     # this stage uses 'camtool'
-    my $camtool = can_run('dettool') or die "Can't find dettool";
+    my $dettool = can_run('dettool') or die "Can't find dettool";
 
     # Get list of component imfiles
@@ -1377,30 +1394,165 @@
 	&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     }
-    $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+    if (@$stdout_buf != 0) {
+	$metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	$exps = parse_md_list($metadata) or
+	    &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	
+	foreach my $exp (@$exps) {
+	    my $iteration = $exp->{iteration};
+	    my $command;
+	    if ($mode eq "goto_cleaned") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
+	    }
+	    if ($mode eq "goto_scrubbed") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
+	    }
+	    if ($mode eq "goto_purged") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
+	    }
+	    $command .= " -dbname $dbname" if defined $dbname;
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
+	    }
+	}
+    }
+
+    exit 0;
+}
+if ($stage eq "detrend.norm.exp") {
+    die "--stage_id required for stage $stage\n" if !$stage_id;
+    # this stage uses 'camtool'
+    my $dettool = can_run('dettool') or die "Can't find dettool";
+
+    # Get list of component imfiles
+    # XXX may need a different my_die for each stage
+    my $exps;                      # Array of component files
+    my $command = "$dettool -pendingcleanup_normalizedexp -det_id $stage_id"; # Command to run
+    $command .= " -dbname $dbname" if defined $dbname;
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
+    unless ($success) {
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
+    }
+    # This is a hack to bomb out until I can diagnose why pantasks wants to keep running this
+    if (@$stdout_buf == 0) {
+	exit 0;
+    }
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+        &my_die("Unable to parse metadata config doc", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
+
     $exps = parse_md_list($metadata) or
-	&my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to parse metadata list", "$stage", $stage_id, $PS_EXIT_PROG_ERROR);
 
     foreach my $exp (@$exps) {
+	my $exp_id = $exp->{exp_id};
 	my $iteration = $exp->{iteration};
-	my $command;
+	my $path_base = $exp->{path_base};
+
+	my $status = 1;
+	# don't clean up unless the data needed to update is available
+	# goto_scrubbed now requires the config file to not be present
 	if ($mode eq "goto_cleaned") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
-	}
-	if ($mode eq "goto_scrubbed") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
-	}
-	if ($mode eq "goto_purged") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
-	}
-	$command .= " -dbname $dbname" if defined $dbname;
-        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-            run(command => $command, verbose => $verbose);
-        unless ($success) {
-            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
-        }
-    }
-
+	    my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
+	    
+	    if (!$config_file or ! -e $config_file) {
+		print STDERR "skipping cleanup for $stage $stage_id because config file is missing\n";
+		$status = 0;
+	    }
+	}
+	elsif ($mode eq "goto_scrubbed") {
+	    my $config_file = $ipprc->filename("PSASTRO.CONFIG", $path_base);
+	    
+	    if ($config_file and -e $config_file) {
+		print STDERR "skipping cleanup for $stage $stage_id because config file ($config_file) is present\n";
+		$status = 0;
+	    }
+	}
+	if ($status) {
+	    my @files = ();
+	    # delete the temporary image datafiles
+#	    addFilename (\@files, "PSASTRO.OUTPUT", $path_base);
+	    if ($mode eq "goto_purged") {
+		# additional files to remove for 'purge' mode
+		addFilename (\@files, "PPIMAGE.JPEG1", $path_base);
+		addFilename (\@files, "PPIMAGE.JPEG2", $path_base);
+#		addFilename (\@files, "PSASTRO.STATS", $path_base);
+	    }
+	    # actual command to delete the files
+	    $status = &delete_files (\@files);
+	}
+	
+	if ($status)  {
+	    my $command = "$dettool -updatenormalizedexp -det_id $stage_id -iteration $iteration";
+	    if ($mode eq "goto_cleaned") {
+		$command .= " -data_state cleaned";
+	    }
+	    if ($mode eq "goto_scrubbed") {
+		$command .= " -data_state scrubbed";
+	    }
+	    if ($mode eq "goto_purged") {
+		$command .= " -data_state purged";
+	    }
+	    $command .= " -dbname $dbname" if defined $dbname;
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		print STDERR " residexp had an issue setting the state:? $success $error_code\n";
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
+	    }
+	} else {
+	    my $command = "$dettool -updatenormalizedexp -det_id $stage_id -exp_id $exp_id -iteration $iteration -data_state $error_state";
+	    $command .= " -dbname $dbname" if defined $dbname;
+	    
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
+	    }
+	    exit $PS_EXIT_UNKNOWN_ERROR;
+	}
+    }
+    # Check to see if we can mark the whole detRunSummary object as cleaned.
+
+    $command = "$dettool -pendingcleanup_detrunsummary -det_id $stage_id"; 
+    $command .= " -dbname $dbname" if defined $dbname;
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = run(command => $command, verbose => $verbose);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
+    }
+    if (@$stdout_buf != 0) {
+	$metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	$exps = parse_md_list($metadata) or
+	    &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	
+	foreach my $exp (@$exps) {
+	    my $iteration = $exp->{iteration};
+	    my $command;
+	    if ($mode eq "goto_cleaned") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
+	    }
+	    if ($mode eq "goto_scrubbed") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
+	    }
+	    if ($mode eq "goto_purged") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
+	    }
+	    $command .= " -dbname $dbname" if defined $dbname;
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
+	    }
+	}
+    }
     exit 0;
 }
@@ -1425,15 +1577,19 @@
 
     # if there are no detProcessedImfiles (@$stdout_buf == 0), the reset the state to 'new'
-    if (@$stdout_buf == 0)  {
-	my $command = "$dettool -det_id $stage_id -updateresidimfile -data_state new";
-	$command .= " -dbname $dbname" if defined $dbname;
-
-	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	    run(command => $command, verbose => $verbose);
-	unless ($success) {
-	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
-	}
-	exit 0;
+#     if (@$stdout_buf == 0)  {
+# 	my $command = "$dettool -det_id $stage_id -updateresidimfile -data_state new";
+# 	$command .= " -dbname $dbname" if defined $dbname;
+
+# 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+# 	    run(command => $command, verbose => $verbose);
+# 	unless ($success) {
+# 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+# 	    &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
+# 	}
+# 	exit 0;
+#     }
+    
+    if (@$stdout_buf == 0) {
+	exit 0; #silently abort. I need to fix this for propers
     }
 
@@ -1543,28 +1699,30 @@
 	&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     }
-    $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-    $exps = parse_md_list($metadata) or
-	&my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-
-    foreach my $exp (@$exps) {
-	my $iteration = $exp->{iteration};
-	my $command;
-	if ($mode eq "goto_cleaned") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
-	}
-	if ($mode eq "goto_scrubbed") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
-	}
-	if ($mode eq "goto_purged") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
-	}
-	$command .= " -dbname $dbname" if defined $dbname;
-        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-            run(command => $command, verbose => $verbose);
-        unless ($success) {
-            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
-        }
+    if (@$stdout_buf != 0) {
+	$metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	my $exps = parse_md_list($metadata) or
+	    &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	
+	foreach my $exp (@$exps) {
+	    my $iteration = $exp->{iteration};
+	    my $command;
+	    if ($mode eq "goto_cleaned") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
+	    }
+	    if ($mode eq "goto_scrubbed") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
+	    }
+	    if ($mode eq "goto_purged") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
+	    }
+	    $command .= " -dbname $dbname" if defined $dbname;
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
+	    }
+	}
     }
 
@@ -1574,5 +1732,5 @@
     die "--stage_id required for stage $stage\n" if !$stage_id;
     # this stage uses 'camtool'
-    my $camtool = can_run('dettool') or die "Can't find dettool";
+    my $dettool = can_run('dettool') or die "Can't find dettool";
 
     # Get list of component imfiles
@@ -1585,4 +1743,8 @@
         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
         &my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
+    }
+    # This is a hack to bomb out until I can diagnose why pantasks wants to keep running this
+    if (@$stdout_buf == 0) {
+	exit 0;
     }
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
@@ -1631,5 +1793,5 @@
 	
 	if ($status)  {
-	    my $command = "$dettool -updateresiddexp -det_id $stage_id -exp_id $exp_id -iteration $iteration";
+	    my $command = "$dettool -updateresidexp -det_id $stage_id -exp_id $exp_id -iteration $iteration";
 	    if ($mode eq "goto_cleaned") {
 		$command .= " -data_state cleaned";
@@ -1645,4 +1807,5 @@
 		run(command => $command, verbose => $verbose);
 	    unless ($success) {
+		print STDERR " residexp had an issue setting the state:? $success $error_code\n";
 		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
 		&my_die("Unable to perform dettool: $error_code", "$stage", $stage_id, $error_code);
@@ -1670,30 +1833,31 @@
 	&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
     }
-    $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-    $exps = parse_md_list($metadata) or
-	&my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
-
-    foreach my $exp (@$exps) {
-	my $iteration = $exp->{iteration};
-	my $command;
-	if ($mode eq "goto_cleaned") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
-	}
-	if ($mode eq "goto_scrubbed") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
-	}
-	if ($mode eq "goto_purged") {
-	    $command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
-	}
-	$command .= " -dbname $dbname" if defined $dbname;
-        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-            run(command => $command, verbose => $verbose);
-        unless ($success) {
-            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
-        }
-    }
-
+    if (@$stdout_buf != 0) {
+	$metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	    &my_die("Unable to parse metadata config doc", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	$exps = parse_md_list($metadata) or
+	    &my_die("Unable to parse metadata list", "$stage (detRunSummary)", $stage_id, $PS_EXIT_PROG_ERROR);
+	
+	foreach my $exp (@$exps) {
+	    my $iteration = $exp->{iteration};
+	    my $command;
+	    if ($mode eq "goto_cleaned") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state cleaned";
+	    }
+	    if ($mode eq "goto_scrubbed") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state scrubbed";
+	    }
+	    if ($mode eq "goto_purged") {
+		$command = "$dettool -updatedetrunsummary -det_id $stage_id -iteration $iteration -data_state purged";
+	    }
+	    $command .= " -dbname $dbname" if defined $dbname;
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run(command => $command, verbose => $verbose);
+	    unless ($success) {
+		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+		&my_die("Unable to perform dettool: $error_code", "$stage (detRunSummary)", $stage_id, $error_code);
+	    }
+	}
+    }
     exit 0;
 }
@@ -1706,9 +1870,17 @@
     my $files = shift; # reference to a list of files to unlink
 
+    open(TMPLOG,">>/tmp/czw.cleanup.log");
+    flock(TMPLOG,2);
+
     # this script is, of course, very dangerous.
     foreach my $file (@$files) {
         print STDERR "unlinking $file\n";
+	print TMPLOG "$file\n";
         $ipprc->file_delete($file);
     }
+
+    flock(TMPLOG,8);
+    close(TMPLOG);
+
     return 1;
 }
Index: /branches/czw_branch/cleanup/ippTasks/detrend.cleanup.pro
===================================================================
--- /branches/czw_branch/cleanup/ippTasks/detrend.cleanup.pro	(revision 24971)
+++ /branches/czw_branch/cleanup/ippTasks/detrend.cleanup.pro	(revision 24972)
@@ -234,5 +234,5 @@
     book getword detCleanupProcessedImfile $pageName class_id -var CLASS_ID 
     book getword detCleanupProcessedImfile $pageName camera   -var CAMERA
-    book getword detCleanupProcessedImfile $pageName state    -var CLEANUP_MODE
+    book getword detCleanupProcessedImfile $pageName data_state    -var CLEANUP_MODE
     book getword detCleanupProcessedImfile $pageName dbname   -var DBNAME
 
@@ -244,5 +244,5 @@
 
     # XXX is everything listed here needed?
-    $run = ipp_cleanup.pl --stage detrend.process.imfile --stage_id $DET_ID --exp_id $EXP_ID --class_id $CLASS_ID --camera $CAMERA --mode $CLEANUP_MODE
+    $run = ipp_cleanup.pl --stage detrend.process.imfile --stage_id $DET_ID --camera $CAMERA --mode $CLEANUP_MODE
     add_standard_args run
 
@@ -352,5 +352,5 @@
     book getword detCleanupProcessedExp $pageName exp_id   -var EXP_ID   
     book getword detCleanupProcessedExp $pageName camera -var CAMERA
-    book getword detCleanupProcessedExp $pageName state -var CLEANUP_MODE
+    book getword detCleanupProcessedExp $pageName data_state -var CLEANUP_MODE
     book getword detCleanupProcessedExp $pageName dbname -var DBNAME
 
@@ -362,5 +362,5 @@
 
     # XXX is everything listed here needed?
-    $run = ipp_cleanup.pl --stage detrend.process.exp --stage_id $DET_ID --exp_id $EXP_ID --camera $CAMERA --mode $CLEANUP_MODE
+    $run = ipp_cleanup.pl --stage detrend.process.exp --stage_id $DET_ID --camera $CAMERA --mode $CLEANUP_MODE
     add_standard_args run
 
@@ -470,5 +470,5 @@
     book getword detCleanupStackedImfile $pageName class_id -var CLASS_ID 
     book getword detCleanupStackedImfile $pageName camera -var CAMERA
-    book getword detCleanupStackedImfile $pageName state -var CLEANUP_MODE
+    book getword detCleanupStackedImfile $pageName data_state -var CLEANUP_MODE
     book getword detCleanupStackedImfile $pageName dbname -var DBNAME
 
@@ -480,5 +480,5 @@
 
     # XXX is everything listed here needed?
-    $run = ipp_cleanup.pl --stage detrend.stack.imfile --stage_id $DET_ID --iteration $ITERATION --class_id $CLASS_ID --camera $CAMERA --mode $CLEANUP_MODE
+    $run = ipp_cleanup.pl --stage detrend.stack.imfile --stage_id $DET_ID --camera $CAMERA --mode $CLEANUP_MODE
     add_standard_args run
 
@@ -588,5 +588,5 @@
     book getword detCleanupNormStatImfile $pageName iteration -var ITERATION
     book getword detCleanupNormStatImfile $pageName camera -var CAMERA
-    book getword detCleanupNormStatImfile $pageName state -var CLEANUP_MODE
+    book getword detCleanupNormStatImfile $pageName data_state -var CLEANUP_MODE
     book getword detCleanupNormStatImfile $pageName dbname -var DBNAME
 
@@ -598,5 +598,5 @@
 
     # XXX is everything listed here needed?
-    $run = ipp_cleanup.pl --stage detrend.normstat.imfile --stage_id $DET_ID --iteration $ITERATION --camera $CAMERA --mode $CLEANUP_MODE
+    $run = ipp_cleanup.pl --stage detrend.normstat.imfile --stage_id $DET_ID --camera $CAMERA --mode $CLEANUP_MODE
     add_standard_args run
 
@@ -704,5 +704,5 @@
     book getword detCleanupNormImfile $pageName class_id -var CLASS_ID 
     book getword detCleanupNormImfile $pageName camera -var CAMERA
-    book getword detCleanupNormImfile $pageName state -var CLEANUP_MODE
+    book getword detCleanupNormImfile $pageName data_state -var CLEANUP_MODE
     book getword detCleanupNormImfile $pageName dbname -var DBNAME
 
@@ -714,5 +714,5 @@
 
     # XXX is everything listed here needed?
-    $run = ipp_cleanup.pl --stage detrend.norm.imfile --stage_id $DET_ID --iteration $ITERATION --class_id $CLASS_ID --camera $CAMERA --mode $CLEANUP_MODE
+    $run = ipp_cleanup.pl --stage detrend.norm.imfile --stage_id $DET_ID --camera $CAMERA --mode $CLEANUP_MODE
     add_standard_args run
 
@@ -819,5 +819,5 @@
     book getword detCleanupNormExp $pageName iteration -var ITERATION
     book getword detCleanupNormExp $pageName camera -var CAMERA
-    book getword detCleanupNormExp $pageName state -var CLEANUP_MODE
+    book getword detCleanupNormExp $pageName data_state -var CLEANUP_MODE
     book getword detCleanupNormExp $pageName dbname -var DBNAME
 
@@ -829,5 +829,5 @@
 
     # XXX is everything listed here needed?
-    $run = ipp_cleanup.pl --stage detrend.norm.exp --stage_id $DET_ID --iteration $ITERATION --camera $CAMERA --mode $CLEANUP_MODE
+    $run = ipp_cleanup.pl --stage detrend.norm.exp --stage_id $DET_ID --camera $CAMERA --mode $CLEANUP_MODE
     add_standard_args run
 
@@ -937,5 +937,5 @@
     book getword detCleanupResidImfile $pageName iteration -var ITERATION     
     book getword detCleanupResidImfile $pageName camera -var CAMERA
-    book getword detCleanupResidImfile $pageName state -var CLEANUP_MODE
+    book getword detCleanupResidImfile $pageName data_state -var CLEANUP_MODE
     book getword detCleanupResidImfile $pageName dbname -var DBNAME
 
@@ -947,5 +947,5 @@
 
     # XXX is everything listed here needed?
-    $run = ipp_cleanup.pl --stage detrend.resid.imfile --stage_id $DET_ID --exp_id $EXP_ID --class_id $CLASS_ID --iteration $ITERATION --camera $CAMERA --mode $CLEANUP_MODE
+    $run = ipp_cleanup.pl --stage detrend.resid.imfile --stage_id $DET_ID  --camera $CAMERA --mode $CLEANUP_MODE
     add_standard_args run
 
@@ -1056,5 +1056,5 @@
     book getword detCleanupResidExp $pageName iteration -var ITERATION
     book getword detCleanupResidExp $pageName camera 	-var CAMERA
-    book getword detCleanupResidExp $pageName state  	-var CLEANUP_MODE
+    book getword detCleanupResidExp $pageName data_state  	-var CLEANUP_MODE
     book getword detCleanupResidExp $pageName dbname 	-var DBNAME
 
@@ -1066,5 +1066,5 @@
 
     # XXX is everything listed here needed?
-    $run = ipp_cleanup.pl --stage detrend.resid.exp --stage_id $DET_ID --exp_id $EXP_ID --class_id $CLASS_ID --camera $CAMERA --mode $CLEANUP_MODE
+    $run = ipp_cleanup.pl --stage detrend.resid.exp --stage_id $DET_ID --camera $CAMERA --mode $CLEANUP_MODE
     add_standard_args run
 
Index: /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedexp.sql
===================================================================
--- /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedexp.sql	(revision 24971)
+++ /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedexp.sql	(revision 24972)
@@ -1,7 +1,12 @@
 SELECT
-    detNormalizedExp.*
+    detNormalizedExp.*,
+    rawExp.camera    
 FROM detRunSummary
 JOIN detNormalizedExp
     USING(det_id,iteration)
+JOIN detInputExp
+     USING(det_id,iteration)
+JOIN rawExp
+     USING(exp_id)
 WHERE
     ((detRunSummary.data_state = 'goto_cleaned'
Index: /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedimfile.sql
===================================================================
--- /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedimfile.sql	(revision 24971)
+++ /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedimfile.sql	(revision 24972)
@@ -1,7 +1,12 @@
 SELECT
-    detNormalizedImfile.*
+    detNormalizedImfile.*,
+    rawExp.camera
 FROM detRunSummary
 JOIN detNormalizedImfile
     USING(det_id,iteration)
+JOIN detInputExp
+     USING(det_id,iteration)
+JOIN rawExp
+     USING(exp_id)
 WHERE
     ((detRunSummary.data_state = 'goto_cleaned'
Index: /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedstat.sql
===================================================================
--- /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedstat.sql	(revision 24971)
+++ /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_normalizedstat.sql	(revision 24972)
@@ -1,7 +1,12 @@
-SELECT
-    detNormalizedStatImfile.*
+SELECT DISTINCT
+    detNormalizedStatImfile.*,
+    rawExp.camera
 FROM detRunSummary
 JOIN detNormalizedStatImfile
     USING(det_id,iteration)
+JOIN detInputExp
+     USING(det_id,iteration)
+JOIN rawExp
+     USING(exp_id)
 WHERE
     ((detRunSummary.data_state = 'goto_cleaned'
Index: /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_processedexp.sql
===================================================================
--- /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_processedexp.sql	(revision 24971)
+++ /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_processedexp.sql	(revision 24972)
@@ -1,8 +1,11 @@
 -- need to restrict to a single detRunSummary (require all to say 'cleaned'?)
 SELECT
-    detProcessedExp.*
+    detProcessedExp.*,
+    rawExp.camera
 FROM detRunSummary
 JOIN detProcessedExp
     USING(det_id)
+JOIN rawExp
+     USING(exp_id)
 WHERE
     ((detRunSummary.data_state = 'goto_cleaned'
Index: /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_processedimfile.sql
===================================================================
--- /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_processedimfile.sql	(revision 24971)
+++ /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_processedimfile.sql	(revision 24972)
@@ -1,16 +1,15 @@
--- need to restrict to a single detRunSummary (require all to say 'cleaned'?)
 SELECT
-    detProcessedImfile.*
+    detProcessedImfile.*,
+    rawExp.camera
 FROM detRunSummary
 JOIN detProcessedImfile
     USING(det_id)
+JOIN detInputExp
+     USING(det_id,iteration,exp_id)
+JOIN rawExp
+     USING(exp_id)
 WHERE
-    ((detRunSummary.data_state = 'goto_cleaned'
-      AND detProcessedImfile.data_state = 'full')
-     OR (detRunSummary.data_state = 'goto_scrubbed'
-         AND detProcessedImfile.data_state = 'full')
-     OR (detRunSummary.data_state = 'goto_purged'
-         AND detProcessedImfile.data_state = 'full')
-     OR detProcessedImfile.data_state = 'goto_cleaned'
+     (detProcessedImfile.data_state = 'goto_cleaned'
      OR detProcessedImfile.data_state = 'goto_scrubbed'
      OR detProcessedImfile.data_state = 'goto_purged')
+
Index: /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_residexp.sql
===================================================================
--- /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_residexp.sql	(revision 24971)
+++ /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_residexp.sql	(revision 24972)
@@ -1,7 +1,12 @@
 SELECT
-    detResidExp.*
+    detResidExp.*,
+    rawExp.camera
 FROM detRunSummary
 JOIN detResidExp
     USING(det_id,iteration)
+JOIN detInputExp
+     USING(det_id,iteration,exp_id)
+JOIN rawExp
+    USING(exp_id)
 WHERE
     ((detRunSummary.data_state = 'goto_cleaned'
Index: /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_residimfile.sql
===================================================================
--- /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_residimfile.sql	(revision 24971)
+++ /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_residimfile.sql	(revision 24972)
@@ -1,7 +1,12 @@
 SELECT
-    detResidImfile.*
+    detResidImfile.*,
+    rawExp.camera
 FROM detRunSummary
 JOIN detResidImfile
     USING(det_id,iteration)
+JOIN detInputExp
+    USING(det_id,iteration,exp_id)
+JOIN rawExp
+    USING(exp_id)
 WHERE
     ((detRunSummary.data_state = 'goto_cleaned'
Index: /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_stacked.sql
===================================================================
--- /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_stacked.sql	(revision 24971)
+++ /branches/czw_branch/cleanup/ippTools/share/dettool_pendingcleanup_stacked.sql	(revision 24972)
@@ -1,7 +1,12 @@
 SELECT
-    detStackedImfile.*
+    detStackedImfile.*,
+    rawExp.camera
 FROM detRunSummary
 JOIN detStackedImfile
-    USING(det_id,iteration)
+     USING(det_id,iteration)
+JOIN detInputExp
+     USING(det_id,iteration)
+JOIN rawExp
+     USING(exp_id)
 WHERE
     ((detRunSummary.data_state = 'goto_cleaned'
Index: /branches/czw_branch/cleanup/ippTools/src/dettool.c
===================================================================
--- /branches/czw_branch/cleanup/ippTools/src/dettool.c	(revision 24971)
+++ /branches/czw_branch/cleanup/ippTools/src/dettool.c	(revision 24972)
@@ -1912,5 +1912,5 @@
 	" WHERE det_id = %" PRId64
 	" AND iteration = %" PRId32
-	" AND class_id = %s";
+	" AND class_id = '%s'";
     if (!p_psDBRunQueryF(config->dbh, query, data_state, det_id, iteration, class_id)) {
         psError(PS_ERR_UNKNOWN, false,
@@ -1934,6 +1934,7 @@
 	" WHERE det_id = %" PRId64
 	" AND iteration = %" PRId32
-	" AND class_id = %s";
-    if (!p_psDBRunQueryF(config->dbh, query, data_state, det_id, iteration)) {
+	" AND class_id = '%s'";
+/*     fprintf(stderr,"DETTOOL SAYS: %s\n",query); */
+    if (!p_psDBRunQueryF(config->dbh, query, data_state, det_id, iteration,class_id)) {
         psError(PS_ERR_UNKNOWN, false,
                 "failed to change state for det_id %" PRId64 ", iteration %" PRId32, 
@@ -1956,5 +1957,5 @@
 	" WHERE det_id = %" PRId64
 	" AND iteration = %" PRId32
-	" AND class_id = %s";
+	" AND class_id = '%s'";
     if (!p_psDBRunQueryF(config->dbh, query, data_state, det_id, iteration, class_id)) {
         psError(PS_ERR_UNKNOWN, false,
Index: /branches/czw_branch/cleanup/ippTools/src/dettool_processedimfile.c
===================================================================
--- /branches/czw_branch/cleanup/ippTools/src/dettool_processedimfile.c	(revision 24971)
+++ /branches/czw_branch/cleanup/ippTools/src/dettool_processedimfile.c	(revision 24972)
@@ -314,5 +314,5 @@
     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
-
+/*     int i; */
     psMetadata *where = psMetadataAlloc();
     PXOPT_COPY_S64(config->args, where, "-det_id", "det_id", "==");
@@ -345,4 +345,5 @@
         return false;
     }
+/*     fprintf(stderr,"DETTOOL:procimfile: %s\n",query); */
     psFree(query);
 
@@ -350,8 +351,10 @@
     if (!output) {
         psError(PS_ERR_UNKNOWN, false, "database error");
+/* 	fprintf(stderr,"WTF !output?\n"); */
         return false;
     }
     if (!psArrayLength(output)) {
         psTrace("dettool", PS_LOG_INFO, "no rows found");
+/* 	fprintf(stderr,"WTF no rows??\n"); */
         psFree(output);
         return true;
@@ -359,4 +362,6 @@
 
     // negative simple so the default is true
+/*     i = (int) ippdbPrintMetadatas(stdout, output, "detCleanupProcessedImfile", !simple); */
+/*     fprintf(stderr,">>%d<<\n",i); */
     if (!ippdbPrintMetadatas(stdout, output, "detCleanupProcessedImfile", !simple)) {
         psError(PS_ERR_UNKNOWN, false, "failed to print array");
@@ -364,4 +369,6 @@
         return false;
     }
+/*     fprintf(stderr,"DETTOOL:procimfile: %s\n",output); */
+/*     psFree(output); */
 
     psFree(output);
