Index: trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 8510)
+++ trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 8614)
@@ -13,6 +13,6 @@
 # This measures how close it is to what's expected
 use constant REJECT_MEAN => {
-    'bias' => 0.01,		# Should be fairly flat; some CRs
-    'dark' => 0.05,		# Lots of CRs
+    'bias' => 10,		# Should be fairly flat; some CRs
+    'dark' => 100,		# Lots of CRs
     'flat' => undef		# Can't define expected value (depends on exposure level)
     };
@@ -21,7 +21,7 @@
 # This measures how much variation there is within the components of an exposure, compared to "typical"
 use constant REJECT_STDEV => {
-    'bias' => 3,		# Components should have the same degree of variation
-    'dark' => 3,		# Components should have the same degree of variation
-    'flat' => 3			# Components should have the same degree of variation
+    'bias' => 10, # Components should have the same degree of variation
+    'dark' => 10, # Components should have the same degree of variation
+    'flat' => 10 # Components should have the same degree of variation
     };
 
@@ -29,7 +29,7 @@
 # This measures how structured the images are, compared to "typical"
 use constant REJECT_MEAN_STDEV => {
-    'bias' => 3,		# All images should be equally structured
-    'dark' => 3,		# All images should be equally structured
-    'flat' => 3 		# All images should be equally structured
+    'bias' => 5,		# All images should be equally structured
+    'dark' => 5,		# All images should be equally structured
+    'flat' => 5 		# All images should be equally structured
     };
 
@@ -51,5 +51,5 @@
 my $exposures;			# Array of exposures
 {
-    my $command = "$dettool -residexp -det_id $detId -iter $iter"; # Command to run
+    my $command = "$dettool -residexp -det_id $detId -iteration $iter"; # Command to run
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -66,5 +66,5 @@
     die "Unable to find exposure id.\n" if not defined $exposure->{exp_id};
     die "Unable to find mean.\n" if not defined $exposure->{bg};
-    die "Unable to find stdev.\n" if not defined $exposure->{stdev};
+    die "Unable to find stdev.\n" if not defined $exposure->{bg_stdev};
     die "Unable to find mean stdev.\n" if not defined $exposure->{bg_mean_stdev};
     push @expIds, $exposure->{exp_id};
@@ -92,16 +92,36 @@
 for (my $i = 0; $i < scalar @means; $i++) {
     my $expId = $expIds[$i];	# Exposure ID
-    my $command = "$dettool -updateresidexp -det_id $detId -iter $iter -exp_id $expId";	# Command to run
-    if ((defined REJECT_MEAN->{$detType} and
-	 ($means[$i] - $meanStats->mean()) / $meanStats->standard_deviation() > REJECT_MEAN->{$detType})
-	or
-	(defined REJECT_STDEV->{$detType} and
-	 ($stdevs[$i] - $stdevStats->mean()) / $stdevStats->standard_deviation() > REJECT_STDEV->{$detType})
-	or
-	(defined REJECT_MEAN_STDEV->{$detType} and
-	 ($meanStdevs[$i] - $meanStdevStats->mean()) / $meanStdevStats->standard_deviation >
-	 REJECT_MEAN_STDEV->{$detType})) {
-	$expId .= ' -reject';
+    my $command = "$dettool -updateresidexp -det_id $detId -iteration $iter -exp_id $expId"; # Command to run
+    my $reject = 0;		# Reject this exposure?
+    if (defined REJECT_MEAN->{$detType} and
+	defined $meanStats->standard_deviation() and
+	$meanStats->standard_deviation() > 0 and
+	($means[$i] - $meanStats->mean()) / $meanStats->standard_deviation() > REJECT_MEAN->{$detType}) {
+	print "Rejecting $expId based on bad mean: " .
+	    (($means[$i] - $meanStats->mean()) / $meanStats->standard_deviation()) .
+	    " vs " . REJECT_MEAN->{$detType} . "\n";
+	$reject = 1;
+    } elsif (defined REJECT_STDEV->{$detType} and
+	     defined $stdevStats->standard_deviation() and
+	     $stdevStats->standard_deviation() > 0 and
+	     ($stdevs[$i] - $stdevStats->mean()) / $stdevStats->standard_deviation() >
+	     REJECT_STDEV->{$detType}) {
+	print "Rejecting $expId based on bad stdev: " .
+	    (($stdevs[$i] - $stdevStats->mean()) / $stdevStats->standard_deviation()) .
+	    " vs " . REJECT_STDEV->{$detType} . "\n";
+	$reject = 1;
+    } elsif (defined REJECT_MEAN_STDEV->{$detType} and
+	     defined $meanStdevStats->standard_deviation() and 
+	     $meanStdevStats->standard_deviation() > 0 and
+	     ($meanStdevs[$i] - $meanStdevStats->mean()) / $meanStdevStats->standard_deviation() >
+	     REJECT_MEAN_STDEV->{$detType}) {
+	print "Rejecting $expId based on bad stdev: " .
+	    (($meanStdevs[$i] - $meanStdevStats->mean()) / $meanStdevStats->standard_deviation()) .
+	    " vs " . REJECT_MEAN_STDEV->{$detType} . "\n";
+	$reject = 1;
+    }
 
+    if ($reject) {
+	$command .= ' -reject';
 	### XXX: Need some way to know whether the exposure has already been rejected.
 	### rejection flag in $exposures?
@@ -126,8 +146,9 @@
 # Put the result into the database
 {
-    my $command = "$dettool -adddetrunsummary -det_id $detId -iter $iter " .
+    my $command = "$dettool -adddetrunsummary -det_id $detId -iteration $iter " .
 	"-bg " . $meanStats->mean() . " -bg_stdev " . $stdevStats->mean() .
 	" -bg_mean_stdev " . $meanStdevStats->mean();
     $command .= " -reject" if not $master;
+
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -136,15 +157,15 @@
 
 # Re-run processing if required
-if (not $stop) {
-    my $command = "$dettool -updatedetrun -det_id $detId -iter $iter";
+{
+    my $command = "$dettool -updatedetrun -det_id $detId";
     if ($stop) {
 	$command .= ' -stop';
     } else {
-	$command .= ' -go';
+	$command .= ' -again';
     }
+    
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
     die "Unable to perform dettool -updatedetrun: $error_code\n" if not $success;
 }
-
 __END__
