Index: trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 18446)
+++ trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 18460)
@@ -30,22 +30,22 @@
 my ($det_id, $iter, $det_type, $camera, $outroot, $filter, $dbname, $verbose, $no_update, $no_op);
 GetOptions(
-	   'det_id|d=s'        => \$det_id,
-	   'iteration=s'       => \$iter,
-	   'det_type|t=s'      => \$det_type,
-	   'camera=s'          => \$camera,
-	   'outroot|w=s'       => \$outroot,   # output file base name
-	   'filter=s'          => \$filter,
-	   'dbname|d=s'        => \$dbname, # Database name
-	   'verbose'           => \$verbose,   # Print to stdout
-	   'no-update'         => \$no_update,
-	   'no-op'             => \$no_op,
+           'det_id|d=s'        => \$det_id,
+           'iteration=s'       => \$iter,
+           'det_type|t=s'      => \$det_type,
+           'camera=s'          => \$camera,
+           'outroot|w=s'       => \$outroot,   # output file base name
+           'filter=s'          => \$filter,
+           'dbname|d=s'        => \$dbname, # Database name
+           'verbose'           => \$verbose,   # Print to stdout
+           'no-update'         => \$no_update,
+           'no-op'             => \$no_op,
 ) or pod2usage( 2 );
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage( -msg => "Required options: --det_id --iteration --det_type --camera --outroot",
-	   -exitval => 3) unless 
-    defined $det_id   and 
-    defined $iter     and 
-    defined $det_type and 
+           -exitval => 3) unless
+    defined $det_id   and
+    defined $iter     and
+    defined $det_type and
     defined $camera   and
     defined $outroot;
@@ -55,6 +55,6 @@
 # XXX -bg_mean_stdev should take stdev of bg_mean if bg_mean_stdev == 0     (B)
 # XXX  (A) if imfile.Ncomp > 1, (B) if imfile.Ncomp == 1
-my $STATS = 
-   [   
+my $STATS =
+   [
        #          KEYWORD                 STATISTIC          CHIPTOOL FLAG
        { name => "bg",             type => "mean",  flag => "-bg",            dtype => "float" },
@@ -66,6 +66,6 @@
 # these stats are used it the rejections but not passed to the database
 # there is some duplication with the above, but the calculation time is minimal
-my $REJSTATS = 
-   [   
+my $REJSTATS =
+   [
        #          KEYWORD                 STATISTIC          CHIPTOOL FLAG
        { name => "bg",             type => "clipmean",  flag => "ensMeanMean",       dtype => "float" },
@@ -84,7 +84,7 @@
 my $missing_tools;
 my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
-if ($missing_tools) { 
+if ($missing_tools) {
     warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR); 
+    exit($PS_EXIT_CONFIG_ERROR);
 }
 
@@ -98,18 +98,18 @@
     $command .= " -dbname $dbname" if defined $dbname;
     ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => $verbose);
+        run(command => $command, verbose => $verbose);
     unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform dettool -residexp: $error_code", $det_id, $iter, $error_code);
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform dettool -residexp: $error_code", $det_id, $iter, $error_code);
     }
 
     # Parse the stdout buffer into a metadata
-    my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
+    my $mdcParser = PS::IPP::Metadata::Config->new;     # Parser for metadata config files
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", $det_id, $iter, $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to parse metadata config doc", $det_id, $iter, $PS_EXIT_PROG_ERROR);
 
     # parse the file info in the metadata
     $exposures = parse_md_list($metadata) or
-	&my_die("Unable to parse metadata list", $det_id, $iter, $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to parse metadata list", $det_id, $iter, $PS_EXIT_PROG_ERROR);
 
     # Parse the statistics on the residual image
@@ -122,5 +122,5 @@
 # each image has a mean mean (average of means over all chips)
 # a standard deviation mean (average of standard deviations over all chips)
-# and a 
+# and a
 
 
@@ -128,9 +128,9 @@
 # we use the statistics of the ensemble to accept/reject exposurs
 my $ensMeanMean       = $rejstats->value_for_flag ("ensMeanMean");       # average of all exposure means (in turn averaged over all chips)
-my $ensMeanStdev      = $rejstats->value_for_flag ("ensMeanStdev");      # standard deviation of all exposure means 
+my $ensMeanStdev      = $rejstats->value_for_flag ("ensMeanStdev");      # standard deviation of all exposure means
 my $ensMeanStdevMean  = $rejstats->value_for_flag ("ensMeanStdevMean");  # average over all exposures of the stdev of the means for each chip
 my $ensMeanStdevStdev = $rejstats->value_for_flag ("ensMeanStdevStdev"); # standard deviation over all exposures of the stdev of the means for each chip
-my $ensStdevMean      = $rejstats->value_for_flag ("ensStdevMean");      # average over all exposures of the sum of the squares of the stdevs for each chip 
-my $ensStdevStdev     = $rejstats->value_for_flag ("ensStdevStdev");     # standard deviation over all exposures of the sum of the squares of the stdevs for each chip 
+my $ensStdevMean      = $rejstats->value_for_flag ("ensStdevMean");      # average over all exposures of the sum of the squares of the stdevs for each chip
+my $ensStdevStdev     = $rejstats->value_for_flag ("ensStdevStdev");     # standard deviation over all exposures of the sum of the squares of the stdevs for each chip
 
 $ipprc->define_camera($camera);
@@ -155,14 +155,14 @@
     print $logFile "Ensemble mean $ensMeanMean +/- $ensMeanStdev\n";
     print $logFile "Ensemble stdev $ensStdevMean +/- $ensStdevStdev\n";
-    print $logFile "Ensemble mean rms (over imfiles) $ensMeanStdevMean +/- $ensMeanStdevStdev\n\n"; 
+    print $logFile "Ensemble mean rms (over imfiles) $ensMeanStdevMean +/- $ensMeanStdevStdev\n\n";
 }
 
 # Go through again to do rejection, and update the database for each exposure
-my $numChanges = 0;		# Number of exposures with changed status
-my $numReject = 0;		# Number of exposures rejected
+my $numChanges = 0;             # Number of exposures with changed status
+my $numReject = 0;              # Number of exposures rejected
 my $firstElement = 1;
 
 foreach my $exposure (@$exposures) {
-    my $mean      = $exposure->{bg};	# Mean for this exposure
+    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
@@ -175,5 +175,5 @@
     &my_die("Unable to find include.\n",     $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $include;
 
-    my $reject = 0;		# Reject this exposure?
+    my $reject = 0;             # Reject this exposure?
 
     $command  = "$dettool -updateresidexp";
@@ -182,94 +182,94 @@
     $command .= " -exp_id $expID";
     $command .= " -dbname $dbname" if defined $dbname;
-    
+
     if (not $accept) {
-	# Rejected this at an earlier stage
-	unless ($no_op) {
-	    print $logFile "Rejecting $expID based on earlier determination.\n";
-	}
-	$reject = 1;
-	goto UPDATE;
+        # Rejected this at an earlier stage
+        unless ($no_op) {
+            print $logFile "Rejecting $expID based on earlier determination.\n";
+        }
+        $reject = 1;
+        goto UPDATE;
     }
 
     # Cop-out if we're not doing any operations
     if ($no_op) {
-	# Make sure something gets rejected (just once!), just so that
-	# we can trace the full range of the workflow
-	if ($firstElement and $iter == 0) {
-	    $reject = 1;
-	}
-	goto UPDATE;
-    }
-    
+        # Make sure something gets rejected (just once!), just so that
+        # we can trace the full range of the workflow
+        if ($firstElement and $iter == 0) {
+            $reject = 1;
+        }
+        goto UPDATE;
+    }
+
     if ($reject_mean > 0 and $ensMeanStdev > 0) {
-	my $delta = abs($mean - $ensMeanMean);
-	if ($delta > ($reject_mean * $ensMeanStdev)) {
-	    print $logFile "Rejecting $expID based on ensemble mean value: ";
-	    $reject = 1;
-	    #goto UPDATE;
-	} else {
-	    print $logFile "$expID OK against ensemble mean: ";
-	}
-	print $logFile "$mean --> $delta vs " . $reject_mean * $ensMeanStdev . "\n";
+        my $delta = abs($mean - $ensMeanMean);
+        if ($delta > ($reject_mean * $ensMeanStdev)) {
+            print $logFile "Rejecting $expID based on ensemble mean value: ";
+            $reject = 1;
+            #goto UPDATE;
+        } else {
+            print $logFile "$expID OK against ensemble mean: ";
+        }
+        print $logFile "$mean --> $delta vs " . $reject_mean * $ensMeanStdev . "\n";
     } else {
-	print $logFile "No rejection of $expID for ensemble mean\n";
+        print $logFile "No rejection of $expID for ensemble mean\n";
     }
 
     if ($reject_stdev > 0 and $ensStdevStdev > 0) {
-	my $delta = abs($stdev - $ensStdevMean);
-	if ($delta > ($reject_stdev * $ensStdevStdev)) {
-	    print $logFile "Rejecting $expID based on ensemble stdev: ";
-	    $reject = 1;
-	    #goto UPDATE;
-	} else {
-	    print $logFile "$expID OK against ensemble stdev: ";
-	}
-	print $logFile "$stdev --> $delta sigma vs " . $reject_stdev * $ensStdevStdev . "\n";
+        my $delta = abs($stdev - $ensStdevMean);
+        if ($delta > ($reject_stdev * $ensStdevStdev)) {
+            print $logFile "Rejecting $expID based on ensemble stdev: ";
+            $reject = 1;
+            #goto UPDATE;
+        } else {
+            print $logFile "$expID OK against ensemble stdev: ";
+        }
+        print $logFile "$stdev --> $delta sigma vs " . $reject_stdev * $ensStdevStdev . "\n";
     } else {
-	print $logFile "No rejection of $expID for ensemble stdev\n";
-    }
-    
+        print $logFile "No rejection of $expID for ensemble stdev\n";
+    }
+
     if ($reject_meanstdev > 0 and $ensMeanStdevStdev > 0) {
-	my $delta = abs($meanStdev - $ensMeanStdevMean);
-	if ($delta > ($reject_meanstdev * $ensMeanStdevStdev)) {
-	    print $logFile "Rejecting $expID based on ensemble mean stdev: ";
-	    $reject = 1;
-	    #goto UPDATE;
-	} else {
-	    print $logFile "$expID OK against ensemble mean stdev: ";
-	}
-	print $logFile "$meanStdev --> $delta sigma vs " . $reject_meanstdev * $ensMeanStdevStdev. "\n";
+        my $delta = abs($meanStdev - $ensMeanStdevMean);
+        if ($delta > ($reject_meanstdev * $ensMeanStdevStdev)) {
+            print $logFile "Rejecting $expID based on ensemble mean stdev: ";
+            $reject = 1;
+            #goto UPDATE;
+        } else {
+            print $logFile "$expID OK against ensemble mean stdev: ";
+        }
+        print $logFile "$meanStdev --> $delta sigma vs " . $reject_meanstdev * $ensMeanStdevStdev. "\n";
     } else {
-	print $logFile "No rejection of $expID for ensemble mean stdev\n";
+        print $logFile "No rejection of $expID for ensemble mean stdev\n";
     }
 
   UPDATE:
     if ($reject) {
-	$command .= ' -reject';
-	$numReject++;
-    }
-    
+        $command .= ' -reject';
+        $numReject++;
+    }
+
     # Check for status changes
     if ((not $include and not $reject) or ($include and $reject)) {
-	unless ($no_op) {
-	    print $logFile "Status of $expID has changed.\n";
-	}
-	$numChanges++;
+        unless ($no_op) {
+            print $logFile "Status of $expID has changed.\n";
+        }
+        $numChanges++;
     }
 
     unless ($no_update) {
-	# Update 
-	( $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 -updateresidexp: $error_code", $det_id, $iter, $error_code);
-	}
+        # Update
+        ( $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 -updateresidexp: $error_code", $det_id, $iter, $error_code);
+        }
     }
 }
 
 # 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
+my $master = 1;                 # This is good enough for a master
+my $stop = 1;                   # Stop iterating
 
 if ($numChanges > 0) {
@@ -282,4 +282,5 @@
     $master = 0;
     $stop = 1;
+    carp "All inputs rejected!\n";
 }
 
@@ -290,4 +291,10 @@
 }
 
+# Allow iteration to be turned off
+my $allow_iter = metadataLookupBool($ipprc->{rejection}, "ITERATION"); # Allow iteration?
+my $force_master = metadataLookupBool($ipprc->{rejection}, "MASTER"); # Force the stack to be accepted
+$stop = 1 unless $allow_iter;
+$master = 1 if $force_master and $stop;
+
 # attempt to prevent endless, pathological iterations
 if ($iter >= $ITER_LIMIT) {
@@ -295,11 +302,4 @@
     exit($PS_EXIT_PROG_ERROR);
 }
-
-# Allow iteration to be turned off
-my $allow_iter = metadataLookupBool($ipprc->{rejection}, "ITERATION"); # Allow iteration?
-my $force_master = metadataLookupBool($ipprc->{rejection}, "MASTER"); # Force the stack to be accepted
-$stop = 1 unless $allow_iter;
-$master = 1 if $force_master;
-
 
 ## add the summary statistics, and request a new iteration if needed
@@ -310,5 +310,5 @@
 $command .= " -dbname $dbname" if defined $dbname;
 ### XXX WE NEED to make this a recipe-driven option
-$command .= " -again" unless $stop; 
+$command .= " -again" unless $stop;
 $command .= $stats->cmdflags();
 
@@ -316,9 +316,9 @@
 unless ($no_update) {
     ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => $verbose);
+        run(command => $command, verbose => $verbose);
     unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	warn("Unable to perform dettool -adddetrunsummary: $error_code");
-	exit($error_code);
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        warn("Unable to perform dettool -adddetrunsummary: $error_code");
+        exit($error_code);
     }
 } else {
@@ -337,15 +337,15 @@
 {
     my $msg = shift; # Warning message on die
-    my $det_id = shift;		# Detrend identifier
-    my $iter = shift;		# Iteration
+    my $det_id = shift;         # Detrend identifier
+    my $iter = shift;           # Iteration
     my $exit_code = shift; # Exit code to add
 
     carp($msg);
     if (defined $det_id and defined $iter and not $no_update) {
-	my $command = "$dettool -adddetrunsummary";
-	$command .= " -det_id $det_id";
-	$command .= " -iteration $iter";
-	$command .= " -code $exit_code";
-	$command .= " -dbname $dbname" if defined $dbname;
+        my $command = "$dettool -adddetrunsummary";
+        $command .= " -det_id $det_id";
+        $command .= " -iteration $iter";
+        $command .= " -code $exit_code";
+        $command .= " -dbname $dbname" if defined $dbname;
         system ($command);
     }
@@ -356,12 +356,12 @@
 sub rejection_limit
 {
-    my $name = shift;		# Rejection limit to 
-    my $type = shift;		# Type of exposure
-    my $filter = shift;		# Filter
+    my $name = shift;           # Rejection limit to
+    my $type = shift;           # Type of exposure
+    my $filter = shift;         # Filter
 
     my $value = $ipprc->rejection( $name, $det_type, $filter );
     if (not defined $value) {
-	$filter = "(no filter)" unless defined $filter;
-	die "Unable to determine $name rejection limit for $det_type with $filter.\n";
+        $filter = "(no filter)" unless defined $filter;
+        die "Unable to determine $name rejection limit for $det_type with $filter.\n";
     }
 
