Index: trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 9259)
+++ trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 9288)
@@ -77,4 +77,6 @@
 my @stdevs;			# Array of stdevs
 my @meanStdevs;			# Array of mean stdevs
+my @accept;			# Array of accept flags
+my @include;			# Array of include flags
 foreach my $exposure (@$exposures) {
     die "Unable to find exposure id.\n" if not defined $exposure->{exp_tag};
@@ -82,8 +84,12 @@
     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};
+    die "Unable to find accept.\n" if not defined $exposure->{accept};
+    die "Unable to find include.\n" if not defined $exposure->{include};
     push @expTags, $exposure->{exp_tag};
     push @means, $exposure->{bg};
     push @stdevs, $exposure->{bg_stdev};
     push @meanStdevs, $exposure->{bg_mean_stdev};
+    push @accept, $exposure->{accept};
+    push @include, $exposure->{include};
 }
 my $meanStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
@@ -103,45 +109,52 @@
 
 # Go through again to do rejection, and update the database for each exposure
-unless ($no_update) {
-    my $numRejected = 0;		# Number of exposures rejected
-    for (my $i = 0; $i < scalar @means; $i++) {
-	my $expTag = $expTags[$i];	# Exposure ID
-	my $command = "$dettool -updateresidexp -det_id $det_id -iteration $iter -exp_tag $expTag"; # Command to run
-	my $reject = 0;		# Reject this exposure?
-	if (defined REJECT_MEAN->{$det_type} and
-	    defined $meanStats->standard_deviation() and
-	    $meanStats->standard_deviation() > 0 and
-	    ($means[$i] - $meanStats->mean()) / $meanStats->standard_deviation() > REJECT_MEAN->{$det_type}) {
-	    print "Rejecting $expTag based on bad mean: " .
-		(($means[$i] - $meanStats->mean()) / $meanStats->standard_deviation()) .
-		" vs " . REJECT_MEAN->{$det_type} . "\n";
-	    $reject = 1;
-	} elsif (defined REJECT_STDEV->{$det_type} and
-		 defined $stdevStats->standard_deviation() and
-		 $stdevStats->standard_deviation() > 0 and
-		 ($stdevs[$i] - $stdevStats->mean()) / $stdevStats->standard_deviation() >
-		 REJECT_STDEV->{$det_type}) {
-	    print "Rejecting $expTag based on bad stdev: " .
-		(($stdevs[$i] - $stdevStats->mean()) / $stdevStats->standard_deviation()) .
-		" vs " . REJECT_STDEV->{$det_type} . "\n";
-	    $reject = 1;
-	} elsif (defined REJECT_MEAN_STDEV->{$det_type} and
-		 defined $meanStdevStats->standard_deviation() and 
-		 $meanStdevStats->standard_deviation() > 0 and
-		 ($meanStdevs[$i] - $meanStdevStats->mean()) / $meanStdevStats->standard_deviation() >
-		 REJECT_MEAN_STDEV->{$det_type}) {
-	    print "Rejecting $expTag based on bad stdev: " .
-		(($meanStdevs[$i] - $meanStdevStats->mean()) / $meanStdevStats->standard_deviation()) .
-		" vs " . REJECT_MEAN_STDEV->{$det_type} . "\n";
-	    $reject = 1;
-	}
-	
-	if ($reject) {
-	    $command .= ' -reject';
-	    ### XXX: Need some way to know whether the exposure has already been rejected.
-	    ### rejection flag in $exposures?
-	    $numRejected++;
-	}
-	
+my $numChanges = 0;		# Number of exposures with changed status
+for (my $i = 0; $i < scalar @means; $i++) {
+    my $expTag = $expTags[$i];	# Exposure ID
+    my $command = "$dettool -updateresidexp -det_id $det_id -iteration $iter -exp_tag $expTag"; # Command to run
+    my $reject = 0;		# Reject this exposure?
+    if (not $accept[$i]) {
+	# Rejected this at an earlier stage
+	print "Rejecting $expTag based on earlier determination.\n";
+	$reject = 1;
+    } elsif (defined REJECT_MEAN->{$det_type} and
+	     defined $meanStats->standard_deviation() and
+	     $meanStats->standard_deviation() > 0 and
+	     ($means[$i] - $meanStats->mean()) / $meanStats->standard_deviation() > REJECT_MEAN->{$det_type}) {
+	print "Rejecting $expTag based on bad mean: " .
+	    (($means[$i] - $meanStats->mean()) / $meanStats->standard_deviation()) .
+	    " vs " . REJECT_MEAN->{$det_type} . "\n";
+	$reject = 1;
+    } elsif (defined REJECT_STDEV->{$det_type} and
+	     defined $stdevStats->standard_deviation() and
+	     $stdevStats->standard_deviation() > 0 and
+	     ($stdevs[$i] - $stdevStats->mean()) / $stdevStats->standard_deviation() >
+	     REJECT_STDEV->{$det_type}) {
+	print "Rejecting $expTag based on bad stdev: " .
+	    (($stdevs[$i] - $stdevStats->mean()) / $stdevStats->standard_deviation()) .
+	    " vs " . REJECT_STDEV->{$det_type} . "\n";
+	$reject = 1;
+    } elsif (defined REJECT_MEAN_STDEV->{$det_type} and
+	     defined $meanStdevStats->standard_deviation() and 
+	     $meanStdevStats->standard_deviation() > 0 and
+	     ($meanStdevs[$i] - $meanStdevStats->mean()) / $meanStdevStats->standard_deviation() >
+	     REJECT_MEAN_STDEV->{$det_type}) {
+	print "Rejecting $expTag based on bad stdev: " .
+	    (($meanStdevs[$i] - $meanStdevStats->mean()) / $meanStdevStats->standard_deviation()) .
+	    " vs " . REJECT_MEAN_STDEV->{$det_type} . "\n";
+	$reject = 1;
+    }
+    
+    if ($reject) {
+	$command .= ' -reject';
+    }
+    
+    # Check for status changes
+    if ((not $include[$i] and not $reject) or ($include[$i] and $reject)) {
+	print "Status of $expTag has changed.\n";
+	$numChanges++;
+    }
+    
+    unless ($no_update) {
 	# Update 
 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -149,39 +162,43 @@
 	die "Unable to perform dettool -updateresidexp: $error_code\n" if not $success;
     }
-
-    # Decide if the current is sufficient to use as a master, and if we can stop iterating
-    my $master = 1;			# This is good enough for a master
-    my $stop = 1;			# Stop iterating
-    # XXX: This probably isn't sufficient, but will do for now:
-    if ($numRejected > 0) {
-	$master = 0;
-	$stop = 0;
-    }
-
-    # Put the result into the database
-    {
-	my $command = "$dettool -adddetrunsummary -det_id $det_id -iteration $iter " .
-	    "-bg " . $meanStats->mean() . " -bg_stdev " . $stdevStats->mean() .
-	    " -bg_mean_stdev " . $meanStdevStats->mean();
-	$command .= " -accept" if $master;
-	
-	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	    run(command => $command, verbose => 1);
-	die "Unable to perform dettool -adddetrunsummary: $error_code\n" if not $success;
-    }
-    
-    # Re-run processing if required
-    {
-	my $command = "$dettool -updatedetrun -det_id $det_id";
-	if ($stop) {
-	    $command .= ' -stop';
-	} else {
-	    $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;
-    }
-}
+}
+    
+# Decide if the current is sufficient to use as a master, and if we can stop iterating
+my $master = 1;			# This is good enough for a master
+my $stop = 1;			# Stop iterating
+
+if ($numChanges > 0) {
+    $master = 0;
+    $stop = 0;
+}
+
+print "Master: $master\n";
+print "Stop: $stop\n";
+
+# Put the result into the database
+unless ($no_update) {
+    my $command = "$dettool -adddetrunsummary -det_id $det_id -iteration $iter " .
+	"-bg " . $meanStats->mean() . " -bg_stdev " . $stdevStats->mean() .
+	" -bg_mean_stdev " . $meanStdevStats->mean();
+    $command .= " -accept" if $master;
+    
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    die "Unable to perform dettool -adddetrunsummary: $error_code\n" if not $success;
+}
+
+# Re-run processing if required
+unless ($no_update) {
+    my $command = "$dettool -updatedetrun -det_id $det_id";
+    if ($stop) {
+	$command .= ' -stop';
+    } else {
+	$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__
