Index: trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 14009)
+++ trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 14048)
@@ -16,4 +16,6 @@
 use IPC::Cmd 0.36 qw( can_run run );
 use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::Stats;
+
 use PS::IPP::Metadata::List qw( parse_md_list );
 use Statistics::Descriptive;
@@ -86,12 +88,12 @@
 
 # Get list of component files
-my $exposures;			# Array of exposures
+my ($exposures, $command, $success, $error_code, $full_buf, $stdout_buf, $stderr_buf);
 {
     # dettool command to select exp data for this det_run
-    my $command = "$dettool -residexp";
+    $command = "$dettool -residexp";
     $command .= " -det_id $det_id";
     $command .= " -iteration $iter";
     $command .= " -dbname $dbname" if defined $dbname;
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
     unless ($success) {
@@ -118,13 +120,5 @@
 }
 
-my @expTags;			# Array of exposure IDs
-my @accept;			# Array of accept flags
-my @include;			# Array of include flags
-foreach my $exposure (@$exposures) {
-    &my_die("Unable to find exposure id.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{exp_tag};
-    &my_die("Unable to find accept.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{accept};
-    &my_die("Unable to find include.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{include};
-}
-
+# we use the statistics of the ensemble to accept/reject exposurs
 my $ensMeanMean       = &STATS_value_for_flag ($REJSTATS, "ensMeanMean");      
 my $ensMeanStdev      = &STATS_value_for_flag ($REJSTATS, "ensMeanStdev");     
@@ -147,5 +141,5 @@
 unless ($no_op) {
     $logFile = $ipprc->file_create_append( $logName );
-    print $logFile "Ensemble mean $meanEnsemble +/- $stdevEnsemble, stdev $meanStdevEnsemble\n\n";
+    print $logFile "Ensemble mean $ensMeanMean +/- $ensMeanStdevMean, stdev $ensStdevMean\n\n";
 }
 
@@ -153,21 +147,24 @@
 my $numChanges = 0;		# Number of exposures with changed status
 my $numReject = 0;		# Number of exposures rejected
-
-for (my $i = 0; $i < scalar @$exposures; $i++) {
-    my $file      = $files[$i];
-    my $mean      = $file->{bg};	# Mean for this exposure
-    my $stdev     = $file->{bg_stdev}; # Stdev for this exposure
-    my $meanStdev = $file->{bg_mean_stdev}; # Stdev of Means for this exposure
-
-    my $expTag  = $exposure->{exp_tag};
-    my $accept  = $exposure->{accept};
-    my $include = $exposure->{include};
+my $firstElement = 1;
+
+foreach my $exposure (@$exposures) {
+    my $mean      = $exposure->{bg};	# Mean for this exposure
+    my $stdev     = $exposure->{bg_stdev}; # Stdev for this exposure
+    my $meanStdev = $exposure->{bg_mean_stdev}; # Stdev of Means for this exposure
+    my $expID     = $exposure->{exp_id};
+    my $accept    = $exposure->{accept};
+    my $include   = $exposure->{include};
+
+    &my_die("Unable to find exposure id.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $expID;
+    &my_die("Unable to find accept.\n",      $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $accept;
+    &my_die("Unable to find include.\n",     $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $include;
 
     my $reject = 0;		# Reject this exposure?
 
-    my $command = "$dettool -updateresidexp";
+    $command  = "$dettool -updateresidexp";
     $command .= " -det_id $det_id";
     $command .= " -iteration $iter";
-    $command .= " -exp_tag $expTag";
+    $command .= " -exp_id $expID";
     $command .= " -dbname $dbname" if defined $dbname;
     
@@ -175,5 +172,5 @@
 	# Rejected this at an earlier stage
 	unless ($no_op) {
-	    print $logFile "Rejecting $expTag based on earlier determination.\n";
+	    print $logFile "Rejecting $expID based on earlier determination.\n";
 	}
 	$reject = 1;
@@ -185,5 +182,5 @@
 	# Make sure something gets rejected (just once!), just so that
 	# we can trace the full range of the workflow
-	if ($i == 0 and $iter == 0) {
+	if ($firstElement and $iter == 0) {
 	    $reject = 1;
 	}
@@ -194,13 +191,13 @@
 	my $delta = abs($mean - $ensMeanMean);
 	if ($delta > ($reject_mean * $ensMeanStdev)) {
-	    print $logFile "Rejecting $expTag based on ensemble mean value: ";
+	    print $logFile "Rejecting $expID based on ensemble mean value: ";
 	    $reject = 1;
 	    #goto UPDATE;
 	} else {
-	    print $logFile "$expTag OK against ensemble mean: ";
+	    print $logFile "$expID OK against ensemble mean: ";
 	}
 	print $logFile "$mean --> $delta vs " . $reject_mean * $ensMeanStdev . "\n";
     } else {
-	print $logFile "No rejection of $expTag for ensemble mean\n";
+	print $logFile "No rejection of $expID for ensemble mean\n";
     }
 
@@ -208,13 +205,13 @@
 	my $delta = abs($stdev - $ensStdevMean);
 	if ($delta > ($reject_stdev * $ensStdevStdev)) {
-	    print $logFile "Rejecting $expTag based on ensemble stdev: ";
+	    print $logFile "Rejecting $expID based on ensemble stdev: ";
 	    $reject = 1;
 	    #goto UPDATE;
 	} else {
-	    print $logFile "$expTag OK against ensemble stdev: ";
+	    print $logFile "$expID OK against ensemble stdev: ";
 	}
 	print $logFile "$stdev --> $delta sigma vs " . $reject_stdev * $ensStdevStdev . "\n";
     } else {
-	print $logFile "No rejection of $expTag for ensemble stdev\n";
+	print $logFile "No rejection of $expID for ensemble stdev\n";
     }
     
@@ -222,13 +219,13 @@
 	my $delta = abs($meanStdev - $ensMeanStdevMean);
 	if ($delta > ($reject_meanstdev * $ensMeanStdevStdev)) {
-	    print $logFile "Rejecting $expTag based on ensemble mean stdev: ";
+	    print $logFile "Rejecting $expID based on ensemble mean stdev: ";
 	    $reject = 1;
 	    #goto UPDATE;
 	} else {
-	    print $logFile "$expTag OK against ensemble mean stdev: ";
+	    print $logFile "$expID OK against ensemble mean stdev: ";
 	}
 	print $logFile "$meanStdev --> $delta sigma vs " . $reject_meanstdev * $ensMeanStdevStdev. "\n";
     } else {
-	print $logFile "No rejection of $expTag for ensemble mean stdev\n";
+	print $logFile "No rejection of $expID for ensemble mean stdev\n";
     }
 
@@ -242,5 +239,5 @@
     if ((not $include and not $reject) or ($include and $reject)) {
 	unless ($no_op) {
-	    print $logFile "Status of $expTag has changed.\n";
+	    print $logFile "Status of $expID has changed.\n";
 	}
 	$numChanges++;
@@ -249,5 +246,5 @@
     unless ($no_update) {
 	# Update 
-	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	    run(command => $command, verbose => 1);
 	unless ($success) {
@@ -268,5 +265,5 @@
 
 # Rejecting everything --- stop before something bad happens!
-if ($numReject == scalar @means) {
+if ($numReject == scalar @$exposures) {
     $master = 0;
     $stop = 1;
@@ -279,5 +276,5 @@
 }
 
-my $command = "$dettool -adddetrunsummary";
+$command = "$dettool -adddetrunsummary";
 $command .= " -det_id $det_id";
 $command .= " -iteration $iter";
@@ -294,5 +291,5 @@
 # Put results into the database
 unless ($no_update) {
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
     unless ($success) {
@@ -310,5 +307,5 @@
     $command .= " -dbname $dbname" if defined $dbname;
 
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
     unless ($success) {
