Index: trunk/ippScripts/scripts/detrend_norm_apply.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_norm_apply.pl	(revision 11831)
+++ trunk/ippScripts/scripts/detrend_norm_apply.pl	(revision 11837)
@@ -39,5 +39,6 @@
     $dbname,			# Database name
     $workdir,			# Working directory, for output files
-    $no_update			# Don't update the database
+    $no_update,			# Don't update the database
+    $no_op,			# Don't do any operations
     );
 GetOptions(
@@ -51,5 +52,6 @@
     'dbname|d=s'        => \$dbname,
     'workdir|w=s'       => \$workdir, # Working directory, for output files
-    'no-update'         => \$no_update
+    'no-update'         => \$no_update,
+    'no-op'             => \$no_op,
     ) or pod2usage( 2 );
     
@@ -100,5 +102,6 @@
 
 # Run normalisation
-{
+my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+unless ($no_op) {
     my $command = "$ppImage -file $input $outputRoot -norm $value -stat $statsName -recipe PPIMAGE " . RECIPE(); # Command to run
     $command .= ' -isfringe' if lc($det_type) eq 'fringe';
@@ -113,9 +116,6 @@
     &my_die("Can't find expected output file: $b2name", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -e $b2name;
     &my_die("Can't find expected output file: $statsName", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -e $statsName;
-}
-
-# Get the statistics on the normalised image
-my $stats;			# Statistics from ppImage
-{
+    
+    # Get the statistics on the normalised image
     my $statsFile;		# File handle
     open $statsFile, $statsName or &my_die("Can't open statistics file $statsName: $!\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
@@ -124,8 +124,7 @@
     my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
     my $metadata = $mdcParser->parse(join "", @contents)
-        or &my_die("Unable to parse metadata config", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
-    $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+	or &my_die("Unable to parse metadata config", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
     $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
-}
+}    
 
 # Update the database
@@ -133,17 +132,13 @@
 $b1name = $ipprc->convert_filename_relative( $b1name );
 $b2name = $ipprc->convert_filename_relative( $b2name );
+
+my $bg = ($stats->bg_mean() or 'NAN');
+my $bg_stdev = ($stats->bg_stdev() or 'NAN');
+my $bg_mean_stdev = ($stats->bg_mean_stdev() or 'NAN');
+
 unless ($no_update) {
     my $command = "$dettool -addnormalizedimfile -det_id $det_id -iteration $iter -class_id $class_id ".
 	"-uri $output -b1_uri $outputRoot"; # Command to run
-    # Add the statistics triplet
-    $command .= " -bg " . $stats->bg_mean();
-    if (defined($stats->bg_mean_stdev())) {
-	$command .= " -bg_mean_stdev " . $stats->bg_mean_stdev();
-    } else {
-	# May be undefined if there's only a single imfile
-	$command .= " -bg_mean_stdev 0";
-    }
-    $command .= " -bg_stdev " . $stats->bg_stdev();
-
+    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
     $command .= " -dbname $dbname" if defined $dbname;
 
Index: trunk/ippScripts/scripts/detrend_norm_calc.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_norm_calc.pl	(revision 11831)
+++ trunk/ippScripts/scripts/detrend_norm_calc.pl	(revision 11837)
@@ -31,18 +31,20 @@
 
 # Parse command-line arguments
-my ($detId,			# Detrend id
+my ($det_id,			# Detrend id
     $iter,			# Iteration
     $detType,			# Detrend type
     $workdir,			# Working directory for output files
     $dbname,			# Database name
-    $no_update			# Don't update the database?
+    $no_update,			# Don't update the database?
+    $no_op,			# Don't do operations
     );
 GetOptions(
-	'det_id|d=s'	=> \$detId,
+	'det_id|d=s'	=> \$det_id,
 	'iteration|i=s'	=> \$iter,
 	'det_type|t=s'  => \$detType,
 	'workdir|w=s'   => \$workdir,
  	'dbname|d=s'    => \$dbname,# Database name
-        'no-update'     => \$no_update
+        'no-update'     => \$no_update,
+	'no-op'         => \$no_op,
 	) or pod2usage( 2 );
 
@@ -50,5 +52,5 @@
 pod2usage( -msg => "Required options --det_id --iteration --det_type",
 	   -exitval => 3,
-	   ) unless defined $detId
+	   ) unless defined $det_id
     and defined $iter
     and defined $detType;
@@ -85,5 +87,5 @@
 my @files;			# The input files
 {
-    my $command = "$dettool -processedimfile -det_id $detId"; # Command to run
+    my $command = "$dettool -processedimfile -det_id $det_id"; # Command to run
     $command .= " -dbname $dbname" if defined $dbname;
     my @command = split /\s+/, $command;
@@ -91,5 +93,5 @@
     print "Running [$command]...\n";
     if (not run(\@command, \$stdin, \$stdout, \$stderr)) {
-	&my_die("Unable to perform dettool -processedimfile on detrend $detId/$iter: $?",
+	&my_die("Unable to perform dettool -processedimfile on detrend $det_id/$iter: $?",
 		$det_id, $iter, $PS_EXIT_SYS_ERROR);
     }
@@ -115,23 +117,23 @@
 
 my $norms;			# MDC with normalisations
-if (NORMALIZE()->{lc($detType)}) {
+if (NORMALIZE()->{lc($detType)} and not $no_op) {
 
     my %matrix; # Matrix of statistics as a function of exposures and classes
     foreach my $file (@files) {
-	my $expTag = $file->{'exp_tag'}; # Exposure ID
-	my $classId = $file->{'class_id'}; # Class ID
+	my $exp_tag = $file->{'exp_tag'}; # Exposure ID
+	my $class_id = $file->{'class_id'}; # Class ID
 	my $stat = $file->{STATISTIC()}; # Statistic of interest
 	
 	# Create matrix elements
-	$matrix{$expTag} = {} if not defined $matrix{$expTag};
-	$matrix{$expTag}->{$classId} = $stat;
+	$matrix{$exp_tag} = {} if not defined $matrix{$exp_tag};
+	$matrix{$exp_tag}->{$class_id} = $stat;
     }
     
     # Generate the input for ppNormCalc
     my $normData;			# Normalisation data
-    foreach my $expTag (keys %matrix) {
-	$normData .= "$expTag\tMETADATA\n";
-	foreach my $classId (keys %{$matrix{$expTag}}) {
-	    $normData .= "\t" . $classId . "\tF32\t" . $matrix{$expTag}->{$classId} . "\n";
+    foreach my $exp_tag (keys %matrix) {
+	$normData .= "$exp_tag\tMETADATA\n";
+	foreach my $class_id (keys %{$matrix{$exp_tag}}) {
+	    $normData .= "\t" . $class_id . "\tF32\t" . $matrix{$exp_tag}->{$class_id} . "\n";
 	}
 	$normData .= "END\n\n";
@@ -157,11 +159,11 @@
     my %classes;		# List of unique classes
     foreach my $file (@files) {
-	my $classId = $file->{'class_id'}; # Class Id
-	$classes{$classId} = 1;
+	my $class_id = $file->{'class_id'}; # Class Id
+	$classes{$class_id} = 1;
     }
     
-    foreach my $classId (keys %classes) {
+    foreach my $class_id (keys %classes) {
 	my %mdValue;	# Metadata value for this class id
-	$mdValue{name} = $classId;
+	$mdValue{name} = $class_id;
 	$mdValue{value} = 1.0;
 	push @$norms, \%mdValue;
@@ -176,5 +178,5 @@
 	my $normalisation = $normItem->{value}; # Normalisation for component
 
-	my $command = "$dettool -addnormalizedstat -det_id $detId -iteration $iter -class_id $className ".
+	my $command = "$dettool -addnormalizedstat -det_id $det_id -iteration $iter -class_id $className ".
 	    "-norm $normalisation"; # Command to run
 	$command .= " -dbname $dbname" if defined $dbname;
@@ -199,5 +201,5 @@
 
     warn($msg);
-    if ($det_id and $iter and $class_id and not $no_update) {
+    if ($det_id and $iter and not $no_update) {
 	my $command = "$dettool -addnormalizedstat -det_id $det_id -iteration $iter -code $exit_code";
 	$command .= " -dbname $dbname" if defined $dbname;
Index: trunk/ippScripts/scripts/detrend_norm_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_norm_exp.pl	(revision 11831)
+++ trunk/ippScripts/scripts/detrend_norm_exp.pl	(revision 11837)
@@ -33,5 +33,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($det_id, $iter, $det_type, $camera, $dbname, $workdir, $no_update);
+my ($det_id, $iter, $det_type, $camera, $dbname, $workdir, $no_update, $no_op);
 GetOptions(
     'det_id|d=s'        => \$det_id,
@@ -41,5 +41,6 @@
     'dbname|d=s'        => \$dbname, # Database name
     'workdir|w=s'       => \$workdir,	# Working directory, for output files
-    'no-update'         => \$no_update
+    'no-update'         => \$no_update,
+    'no-op'             => \$no_op,
 ) or pod2usage( 2 );
 
@@ -104,11 +105,11 @@
 	my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
 	$stats->add_data(@backgrounds);
-	$bg = $stats->mean();
-	$bg_mean_stdev = $stats->standard_deviation() || 0.0;
+	$bg = ($stats->mean() or 'NAN');
+	$bg_mean_stdev = ($stats->standard_deviation() or 'NAN');
     }
     {
 	my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
 	$stats->add_data(@variances);
-	$bg_stdev = sqrt( $stats->mean() );
+	$bg_stdev = (sqrt( $stats->mean() ) or 'NAN');
     }
 }
@@ -147,18 +148,20 @@
 my $jpeg2Name = $ipprc->filename("PPIMAGE.JPEG2", $outputRoot); # Binned JPEG #2
 
-# Make the jpeg for binning 1
-{
-    my $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE " . RECIPE1; # Command to run
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    &my_die("Unable to find expected output file: $jpeg1Name", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless -f $jpeg1Name;
-}
-
-# Make the jpeg for binning 2
-{
-    my $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE " . RECIPE2; # Command to run
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    &my_die("Unable to find expected output file: $jpeg2Name", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless -f $jpeg2Name;
+unless ($no_op) {
+    # Make the jpeg for binning 1
+    {
+	my $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE " . RECIPE1; # Command to run
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	&my_die("Unable to find expected output file: $jpeg1Name", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless -f $jpeg1Name;
+    }
+    
+    # Make the jpeg for binning 2
+    {
+	my $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE " . RECIPE2; # Command to run
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	&my_die("Unable to find expected output file: $jpeg2Name", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless -f $jpeg2Name;
+    }
 }
 
Index: trunk/ippScripts/scripts/detrend_process_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 11831)
+++ trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 11837)
@@ -33,5 +33,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($det_id, $exp_tag, $camera, $dbname, $workdir, $no_update);
+my ($det_id, $exp_tag, $camera, $dbname, $workdir, $no_update, $no_op);
 GetOptions(
     'det_id|d=s'        => \$det_id,
@@ -40,5 +40,6 @@
     'dbname|d=s'        => \$dbname, # Database name
     'workdir|w=s'       => \$workdir,	# Working directory, for output files
-    'no-update'         => \$no_update
+    'no-update'         => \$no_update,
+    'no-op'             => \$no_op,
 ) or pod2usage( 2 );
 
@@ -102,11 +103,11 @@
 	my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
 	$stats->add_data(@backgrounds);
-	$bg = $stats->mean();
-	$bg_mean_stdev = $stats->standard_deviation() || 0.0;
+	$bg = ($stats->mean() or 'NAN');
+	$bg_mean_stdev = ($stats->standard_deviation() or 'NAN');
     }
     {
 	my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
 	$stats->add_data(@variances);
-	$bg_stdev = sqrt( $stats->mean() );
+	$bg_stdev = (sqrt( $stats->mean() ) or 'NAN');
     }
 }
@@ -143,28 +144,29 @@
 my $jpeg2 = $ipprc->filename("PPIMAGE.JPEG2", $outputRoot); # Binned JPEG #2
 
-# Make the jpeg for binning 1
-{
-    my $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE " . RECIPE1; # Command to run
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform ppImage: $error_code", $det_id, $exp_tag, $error_code);
-    }
-    &my_die("Unable to find expected output file: $jpeg1", $det_id, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $jpeg1;
-}
-
-# Make the jpeg for binning 2
-{
-    my $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE " . RECIPE2; # Command to run
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform ppImage: $error_code", $det_id, $exp_tag, $error_code);
-    }
-    &my_die("Unable to find expected output file: $jpeg2", $det_id, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $jpeg2;
-}
-
+unless ($no_op) {
+    # Make the jpeg for binning 1
+    {
+	my $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE " . RECIPE1; # Command to run
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform ppImage: $error_code", $det_id, $exp_tag, $error_code);
+	}
+	&my_die("Unable to find expected output file: $jpeg1", $det_id, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $jpeg1;
+    }
+    
+    # Make the jpeg for binning 2
+    {
+	my $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE " . RECIPE2; # Command to run
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform ppImage: $error_code", $det_id, $exp_tag, $error_code);
+	}
+	&my_die("Unable to find expected output file: $jpeg2", $det_id, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $jpeg2;
+    }
+}
 
 # Add the result into the database
Index: trunk/ippScripts/scripts/detrend_process_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 11831)
+++ trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 11837)
@@ -32,5 +32,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($det_id, $exp_tag, $class_id, $det_type, $input_uri, $camera, $dbname, $workdir, $no_update);
+my ($det_id, $exp_tag, $class_id, $det_type, $input_uri, $camera, $dbname, $workdir, $no_update, $no_op);
 GetOptions(
     'det_id|d=s'        => \$det_id,
@@ -42,5 +42,6 @@
     'dbname|d=s'        => \$dbname, # Database name
     'workdir|w=s'       => \$workdir, # Working directory, for output files
-    'no-update'         => \$no_update
+    'no-update'         => \$no_update,
+    'no-op'             => \$no_op,
 ) or pod2usage( 2 );
 
@@ -105,5 +106,6 @@
 
 # Run ppImage
-{
+my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+unless ($no_op) {
     my $command = "$ppImage -file $input_uri $outputRoot -recipe PPIMAGE $recipe" .
 	" -stat $outputStats"; # Command to run ppImage
@@ -118,9 +120,6 @@
     &my_die("Couldn't find expected output file: $outputBin1", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin1;
     &my_die("Couldn't find expected output file: $outputBin2", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin2;
-}
 
-# Get the statistics on the processed image
-my $stats;			# Statistics from ppImage
-{
+    # Get the statistics on the processed image
     my $statsFile;		# File handle
     open $statsFile, "$outputStats" or die "Can't open statistics file $outputStats: $!\n";
@@ -130,5 +129,4 @@
     my $metadata = $mdcParser->parse(join "", @contents)
         or &my_die("Unable to parse metadata config", $det_id, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
-    $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
     $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
 }
@@ -139,16 +137,13 @@
 $outputBin2  = $ipprc->convert_filename_relative( $outputBin2  );
 
+my $bg = ($stats->bg_mean() or 'NAN');
+my $bg_stdev = ($stats->bg_stdev() or 'NAN');
+my $bg_mean_stdev = ($stats->bg_mean_stdev() or 'NAN');
+
 # Add the processed file to the database
 unless ($no_update) {
     my $command = "$dettool -addprocessedimfile -det_id $det_id -exp_tag $exp_tag " .
 	"-class_id $class_id -recip $recipe -uri $outputImage -b1_uri $outputRoot";  # Command to run dettool
-    $command .= " -bg " . $stats->bg_mean();
-    if (defined($stats->bg_mean_stdev())) {
-	$command .= " -bg_mean_stdev " . $stats->bg_mean_stdev();
-    } else {
-	# May be undefined if there's only a single imfile
-	$command .= " -bg_mean_stdev 0";
-    }
-    $command .= " -bg_stdev " . $stats->bg_stdev();
+    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
     $command .= " -dbname $dbname" if defined $dbname;
 
Index: trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 11831)
+++ trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 11837)
@@ -32,5 +32,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($det_id, $iter, $det_type, $camera, $filter, $workdir, $dbname, $no_update);
+my ($det_id, $iter, $det_type, $camera, $filter, $workdir, $dbname, $no_update, $no_op);
 GetOptions(
 	   'det_id|d=s'        => \$det_id,
@@ -41,5 +41,6 @@
 	   'workdir|w=s'       => \$workdir, # Working directory for output files
 	   'dbname|d=s'        => \$dbname, # Database name
-	   'no-update'         => \$no_update
+	   'no-update'         => \$no_update,
+	   'no-op'             => \$no_op,
 ) or pod2usage( 2 );
 
@@ -133,5 +134,5 @@
     $command = "$dettool -updateresidexp -det_id $det_id -iteration $iter -exp_tag $expTag"; # Command to run
     $command .= " -dbname $dbname" if defined $dbname;
-
+    
     $reject = 0;		# Reject this exposure?
 
@@ -143,4 +144,14 @@
     }
 
+    # 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 ($i == 0 and $iter == 0) {
+	    $reject = 1;
+	}
+	goto UPDATE;
+    }
+    
     if ($reject_mean and defined $meanStats->standard_deviation() ) {
 	my $dMean = abs($means[$i] - $meanStats->mean()) ;
@@ -201,5 +212,5 @@
     }
 }
-    
+
 # 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
@@ -220,41 +231,48 @@
 print "Stop: $stop\n";
 
-# Put the result into the database
+# Put results into the database
 unless ($no_update) {
-    my $varianceStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
-    $varianceStats->add_data(@variances);
-
-    my $command = "$dettool -adddetrunsummary -det_id $det_id -iteration $iter" .
-	" -bg " . $meanStats->mean() . " -bg_stdev " . sqrt($varianceStats->mean()) .
-	" -bg_mean_stdev " . $meanStats->standard_deviation();
-    $command .= " -accept" if $master;
-    $command .= " -dbname $dbname" if defined $dbname;
-
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	warn("Unable to perform dettool -adddetrunsummary: $error_code");
-	exit($error_code);
-    }
-}
-
-# Re-run processing if required
-unless ($no_update) {
-    my $command = "$dettool -updatedetrun -det_id $det_id";
-    if ($stop) {
-	$command .= ' -state stop';
-    } else {
-	$command .= ' -again';
-    }
-    
-    $command .= " -dbname $dbname" if defined $dbname;
-    
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	warn("Unable to perform dettool -updatedetrun: $error_code");
-	exit($error_code);
+
+    {
+	# Add the summary
+	my $varianceStats = Statistics::Descriptive::Sparse->new(); # Statistics calculator
+	$varianceStats->add_data(@variances);
+	
+	my $bg = ($meanStats->mean() or 'NAN');
+	my $bg_stdev = (sqrt( $varianceStats->mean() ) or 'NAN');
+	my $bg_mean_stdev = ($meanStats->standard_deviation() or 'NAN');
+	
+	my $command = "$dettool -adddetrunsummary -det_id $det_id -iteration $iter";
+	$command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
+	$command .= " -accept" if $master;
+	$command .= " -dbname $dbname" if defined $dbname;
+	
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    warn("Unable to perform dettool -adddetrunsummary: $error_code");
+	    exit($error_code);
+	}
+    }
+
+    # Re-run processing if required
+    {
+	my $command = "$dettool -updatedetrun -det_id $det_id";
+	if ($stop) {
+	    $command .= ' -state stop';
+	} else {
+	    $command .= ' -again';
+	}
+	
+	$command .= " -dbname $dbname" if defined $dbname;
+	
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    warn("Unable to perform dettool -updatedetrun: $error_code");
+	    exit($error_code);
+	}
     }
 }
Index: trunk/ippScripts/scripts/detrend_reject_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 11831)
+++ trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 11837)
@@ -33,5 +33,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($det_id, $iter, $exp_tag, $det_type, $camera, $filter, $reject, $dbname, $workdir, $no_update);
+my ($det_id, $iter, $exp_tag, $det_type, $camera, $filter, $reject, $dbname, $workdir, $no_update, $no_op);
 GetOptions(
 	   'det_id|d=s'        => \$det_id,
@@ -45,4 +45,5 @@
 	   'workdir|w=s'       => \$workdir, # Working directory, for output files
 	   'no-update'         => \$no_update,
+	   'no-op'             => \$no_op,
 	   ) or pod2usage( 2 );
 
@@ -129,26 +130,29 @@
 my $jpeg2Name = $ipprc->filename("PPIMAGE.JPEG2", $outputRoot); # Binned JPEG #2
 
-# Make the jpeg for binning 1
-{
-    my $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE " . RECIPE1; # Command to run
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to run ppImage: $error_code", $det_id, $iter, $exp_tag, $error_code);
-    }
-    &my_die("Unable to find expected output file: $jpeg1Name", $det_id, $iter, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $jpeg1Name;
-}
-
-# Make the jpeg for binning 2
-{
-    my $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE " . RECIPE2; # Command to run
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to run ppImage: $error_code", $det_id, $iter, $exp_tag, $error_code);
-    }
-    &my_die("Unable to find expected output file: $jpeg2Name", $det_id, $iter, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $jpeg2Name;
+unless ($no_op) {
+
+    # Make the jpeg for binning 1
+    {
+	my $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE " . RECIPE1; # Command to run
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to run ppImage: $error_code", $det_id, $iter, $exp_tag, $error_code);
+	}
+	&my_die("Unable to find expected output file: $jpeg1Name", $det_id, $iter, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $jpeg1Name;
+    }
+    
+    # Make the jpeg for binning 2
+    {
+	my $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE " . RECIPE2; # Command to run
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to run ppImage: $error_code", $det_id, $iter, $exp_tag, $error_code);
+	}
+	&my_die("Unable to find expected output file: $jpeg2Name", $det_id, $iter, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $jpeg2Name;
+    }
 }
 
@@ -172,4 +176,6 @@
     my $stdev = sqrt($variances[$i]);	# Stdev for this imfile
 
+    last if $no_op;
+
     if ($reject_imfile_mean) {
 	if (abs($mean) > $reject_imfile_mean * $stdev) {
@@ -212,5 +218,4 @@
 	print "no rejection for imfile S/N\n";
     }
-    
 }
 
@@ -235,42 +240,44 @@
 ## Reject based on the exposure ensemble stats
 # reject if the exposure ensemble mean is deviant
-if ($reject_exp_mean) {
-    if (abs($mean) > $reject_exp_mean * $stdev) {
-	print "Rejecting exposure based on bad mean: \n";
-	print "mean: $mean, stdev: $stdev (limit is: % $reject_exp_mean\n";
-	$reject = 1;
-    } 
-} else {
-    print "no rejection for exp mean\n";
-}
-# reject if the exposure ensemble stdev is deviant
-if ($reject_exp_stdev) {
-    if ($stdev > $reject_exp_stdev) {
-	print "Rejecting exposure based on bad mean stdev: " . $stdev . " vs " .
-	    $reject_exp_stdev . "\n";
-	$reject = 1;
-    }
-} else {
-    print "no rejection for exp stdev\n";
-}
-# reject if the exposure ensemble mean stdev is deviant
-if ($reject_exp_meanstdev) {
-    if ($meanStdev > $reject_exp_meanstdev) {
-	print "Rejecting exposure based on bad mean stdev: " . $meanStdev . " vs " .
-	    $reject_exp_meanstdev . "\n";
-	$reject = 1;
-    }
-} else {
-    print "no rejection for exp mean stdev\n";
-}
-# reject if the signal-to-noise is insufficient
-if ($reject_exp_sn) {
-    if (abs($mean) < abs($stdev * $reject_exp_sn)) {
-	print "Rejecting exposure based on poor S/N: \n";
-	print "signal: $mean vs noise: $stdev (s/n limit is: $reject_exp_sn)\n";
-	$reject = 1;
-    }
-} else {
-    print "no rejection for exp S/N\n";
+unless ($no_op) {
+    if ($reject_exp_mean) {
+	if (abs($mean) > $reject_exp_mean * $stdev) {
+	    print "Rejecting exposure based on bad mean: \n";
+	    print "mean: $mean, stdev: $stdev (limit is: % $reject_exp_mean\n";
+	    $reject = 1;
+	} 
+    } else {
+	print "no rejection for exp mean\n";
+    }
+    # reject if the exposure ensemble stdev is deviant
+    if ($reject_exp_stdev) {
+	if ($stdev > $reject_exp_stdev) {
+	    print "Rejecting exposure based on bad mean stdev: " . $stdev . " vs " .
+		$reject_exp_stdev . "\n";
+	    $reject = 1;
+	}
+    } else {
+	print "no rejection for exp stdev\n";
+    }
+    # reject if the exposure ensemble mean stdev is deviant
+    if ($reject_exp_meanstdev) {
+	if ($meanStdev > $reject_exp_meanstdev) {
+	    print "Rejecting exposure based on bad mean stdev: " . $meanStdev . " vs " .
+		$reject_exp_meanstdev . "\n";
+	    $reject = 1;
+	}
+    } else {
+	print "no rejection for exp mean stdev\n";
+    }
+    # reject if the signal-to-noise is insufficient
+    if ($reject_exp_sn) {
+	if (abs($mean) < abs($stdev * $reject_exp_sn)) {
+	    print "Rejecting exposure based on poor S/N: \n";
+	    print "signal: $mean vs noise: $stdev (s/n limit is: $reject_exp_sn)\n";
+	    $reject = 1;
+	}
+    } else {
+	print "no rejection for exp S/N\n";
+    }
 }
 
@@ -278,8 +285,13 @@
 $jpeg1Name = $ipprc->convert_filename_relative( $jpeg1Name );
 $jpeg2Name = $ipprc->convert_filename_relative( $jpeg2Name );
+
+my $bg = $mean;
+my $bg_stdev = $stdev;
+my $bg_mean_stdev = $meanStdev;
+
 unless ($no_update) {
     my $command = "$dettool -addresidexp -det_id $det_id -iteration $iter -exp_tag $exp_tag " .
-	"-recip " . RECIPE1() . "," . RECIPE2() . " -b1_uri $outputRoot " .
-	"-bg $mean -bg_stdev $stdev -bg_mean_stdev $meanStdev";	# Command to run
+	"-recip " . RECIPE1() . "," . RECIPE2() . " -b1_uri $outputRoot "; # Command to run
+    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
     $command .= ' -reject' if $reject;
     $command .= " -dbname $dbname" if defined $dbname;
Index: trunk/ippScripts/scripts/detrend_resid.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_resid.pl	(revision 11831)
+++ trunk/ippScripts/scripts/detrend_resid.pl	(revision 11837)
@@ -34,5 +34,5 @@
 
 my ($det_id, $iter, $exp_tag, $class_id, $det_type, $detrend,
-        $input_uri, $camera, $mode, $dbname, $workdir, $no_update);
+        $input_uri, $camera, $mode, $dbname, $workdir, $no_update, $no_op);
 GetOptions(
     'det_id|d=s'        => \$det_id,
@@ -48,4 +48,5 @@
     'workdir|w=s'       => \$workdir,	# Working directory, for output files
     'no-update'         => \$no_update,
+    'no-op'             => \$no_op,
 ) or pod2usage( 2 );
 
@@ -138,5 +139,6 @@
 
 # Run ppImage
-{
+my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+unless ($no_op) {
     my $command = "$ppImage -file $input_uri $outputRoot -recipe PPIMAGE $recipe" .
 	" -stat $outputStats "; # Command to run ppImage
@@ -159,9 +161,6 @@
     &my_die("Couldn't find expected output file: $bin1Name", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $bin1Name;
     &my_die("Couldn't find expected output file: $bin2Name", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $bin2Name;
-}
-
-# Get the statistics on the residual image
-my $stats;			# Statistics from ppImage
-{
+
+    # Get the statistics on the residual image
     my $statsFile;		# File handle
     open $statsFile, $outputStats or &my_die("Can't open statistics file $outputStats: $!", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR);
@@ -179,18 +178,14 @@
 $bin1Name = $ipprc->convert_filename_relative( $bin1Name );
 $bin2Name = $ipprc->convert_filename_relative( $bin2Name );
+
+my $bg = ($stats->bg_mean() or 'NAN');
+my $bg_stdev = ($stats->bg_stdev() or 'NAN');
+my $bg_mean_stdev = ($stats->bg_mean_stdev() or 'NAN');
+
 unless ($no_update) {
     my $command = "$dettool -addresidimfile -det_id $det_id -iteration $iter -exp_tag $exp_tag " .
 	"-class_id $class_id -recip $recipe -uri $outputName -b1_uri $outputRoot"; # Command to run dettool
-    $command .= " -bg " . $stats->bg_mean();
+    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
     $command .= " -dbname $dbname" if defined $dbname;
-
-    if (defined($stats->bg_mean_stdev())) {
-	$command .= " -bg_mean_stdev " . $stats->bg_mean_stdev();
-    } else {
-	# May be undefined if there is only a single imfile
-	$command .= ' -bg_mean_stdev 0';
-    }
-
-    $command .= " -bg_stdev " . $stats->bg_stdev();
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
Index: trunk/ippScripts/scripts/detrend_stack.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_stack.pl	(revision 11831)
+++ trunk/ippScripts/scripts/detrend_stack.pl	(revision 11837)
@@ -33,5 +33,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($det_id, $iter, $class_id, $det_type, $camera, $dbname, $workdir, $no_update);
+my ($det_id, $iter, $class_id, $det_type, $camera, $dbname, $workdir, $no_update, $no_op);
 GetOptions(
     'det_id|d=s'        => \$det_id,
@@ -42,5 +42,6 @@
     'dbname|d=s'        => \$dbname, # Database name
     'workdir|w=s'       => \$workdir,	# Working directory, for output files
-    'no-update'         => \$no_update
+    'no-update'         => \$no_update,
+    'no-op'             => \$no_op,
 ) or pod2usage( 2 );
 
@@ -112,5 +113,6 @@
 my $outputStats = $outputRoot . '.stats'; # Statistics name
 
-{
+my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+unless ($no_op) {
     my $command = "$ppMerge $outputStack"; # Command to run
     foreach my $file (@$files) {
@@ -122,5 +124,5 @@
     $command .= ' -type ' . uc($det_type); # Type of stacking to perform
     $command .= " -stats $outputStats";	# Statistics output filename
-
+    
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -131,14 +133,11 @@
     &my_die("Unable to find expected output file: $outputStack\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStack;
     &my_die("Unable to find expected output file: $outputStats\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStats;
-}
 
-# Get the statistics on the stacked image
-my $stats;			# Statistics from ppMerge
-{
+    # Get the statistics on the stacked image
     open(my $statsFile, "$outputStats") or
 	&my_die("Can't open statistics file $outputStats: $!", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
     my $contents = do { local $/; <$statsFile> }; # Contents of file
     close($statsFile);
-
+    
     my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
     my $metadata = $mdcParser->parse($contents) or
@@ -149,4 +148,8 @@
 }
 
+my $bg = ($stats->bg_mean() or 'NAN');
+my $bg_stdev = ($stats->bg_stdev() or 'NAN');
+my $bg_mean_stdev = ($stats->bg_mean_stdev() or 'NAN');
+
 # Add the resultant into the database
 $outputStack = $ipprc->convert_filename_relative($outputStack);
@@ -154,13 +157,7 @@
     my $command = "$dettool -addstacked -det_id $det_id -iteration $iter -class_id $class_id" .
 	" -uri $outputStack -recip $recipe"; # Command to run
-    $command .= ' -bg ' . $stats->bg_mean();
-    if (defined($stats->bg_mean_stdev())) {
-    $command .= ' -bg_mean_stdev ' . $stats->bg_mean_stdev();
-    } else {
-	# May be undefined if there is only a single imfile
-	$command .= ' -bg_mean_stdev 0';
-    }
-    $command .= ' -bg_stdev ' . $stats->bg_stdev();
+    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
     $command .= " -dbname $dbname" if defined $dbname;
+
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
Index: trunk/ippScripts/scripts/phase2.pl
===================================================================
--- trunk/ippScripts/scripts/phase2.pl	(revision 11831)
+++ trunk/ippScripts/scripts/phase2.pl	(revision 11837)
@@ -38,5 +38,6 @@
     $dbname,			# Database name
     $workdir,			# Working directory, for output files
-    $no_update			# Don't update the database?
+    $no_update,			# Don't update the database?
+    $no_op,			# Don't do any operations?
     );
 GetOptions(
@@ -47,5 +48,6 @@
     'dbname|d=s'    => \$dbname, # Database name
     'workdir|w=s'   => \$workdir,
-    'no-update'     => \$no_update
+    'no-update'     => \$no_update,
+    'no-op'         => \$no_op,
 ) or pod2usage( 2 );
 
@@ -88,10 +90,11 @@
 $input = $ipprc->convert_filename_absolute( $input );
 
-# Run ppImage
-{
+my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+unless ($no_op) {
+    # Run ppImage
     print "outputImage: $outputImage\n";
     print "outputBin1: $outputBin1\n";
     print "outputStats: $outputStats\n";
-
+    
     my $command = "$ppImage -file $input $outputRoot -recipe PPIMAGE " . RECIPE .
 	" -stat $outputStats"; # Command to run ppImage
@@ -106,9 +109,6 @@
     &my_die("Couldn't find expected output file: $outputBin2\n", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin2;
     &my_die("Couldn't find expected output file: $outputStats\n", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStats;
-}
 
-# Get the statistics on the processed image
-my $stats;			# Statistics from ppImage
-{
+    # Get the statistics on the processed image
     my $statsFile;		# File handle
     open $statsFile, $outputStats or &my_die("Can't open statistics file $outputStats: $!", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR);
@@ -118,5 +118,4 @@
     my $metadata = $mdcParser->parse(join "", @contents) or
 	&my_die("Unable to parse metadata config doc", $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
-    $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
     $stats->parse($metadata) or
 	&my_die("Unable to find all values in statistics output.\n", $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
@@ -127,4 +126,9 @@
 $outputBin1  = $ipprc->convert_filename_relative( $outputBin1 );
 $outputBin2  = $ipprc->convert_filename_relative( $outputBin2 );
+
+my $bg = ($stats->bg_mean() or 'NAN');
+my $bg_stdev = ($stats->bg_stdev() or 'NAN');
+my $bg_mean_stdev = ($stats->bg_mean_stdev() or 'NAN');
+
 unless ($no_update) {
     # Command to run p2tool
@@ -135,11 +139,5 @@
     $command .= " -uri $outputImage";
     $command .= " -b1_uri $outputRoot";
-    $command .= " -bg " . $stats->bg_mean();
-    $command .= " -bg_stdev " . $stats->bg_stdev();
-    if ( defined $stats->bg_mean_stdev() ) {
-	$command .= " -bg_mean_stdev " . $stats->bg_mean_stdev();
-    } else {
-	$command .= " -bg_mean_stdev 0.0";
-    }
+    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
     $command .= " -dbname $dbname" if defined $dbname;
 
Index: trunk/ippScripts/scripts/phase3.pl
===================================================================
--- trunk/ippScripts/scripts/phase3.pl	(revision 11831)
+++ trunk/ippScripts/scripts/phase3.pl	(revision 11837)
@@ -33,5 +33,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($exp_tag, $camera, $dbname, $workdir, $no_update);
+my ($exp_tag, $camera, $dbname, $workdir, $no_update, $no_op);
 GetOptions(
     'exp_tag|e=s'       => \$exp_tag,
@@ -39,5 +39,6 @@
     'dbname|d=s'        => \$dbname, # Database name
     'workdir|w=s'       => \$workdir,
-    'no-update'         => \$no_update
+    'no-update'         => \$no_update,
+    'no-op'             => \$no_op,
 ) or pod2usage( 2 );
 
@@ -111,11 +112,11 @@
 	my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
 	$stats->add_data(@backgrounds);
-	$bg = $stats->mean();
-	$bg_mean_stdev = $stats->standard_deviation() || 0.0;
+	$bg = ($stats->mean() or 'NAN');
+	$bg_mean_stdev = ($stats->standard_deviation() or 'NAN');
     }
     {
 	my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
 	$stats->add_data(@variances);
-	$bg_stdev = sqrt( $stats->mean() );
+	$bg_stdev = (sqrt( $stats->mean() ) or 'NAN');
     }
 }
@@ -169,54 +170,57 @@
 my $fpaObjects = $ipprc->filename("PSASTRO.OUTPUT", $outputRoot); # MEF psastro output
 
-# run psastro +mosastro on the set of chips
-# XXX note that this is wrong if imfiles are cells
-if (@$files > 1) {
-    my $command = "$psastro -list $list3Name $outputRoot +mosastro -chipastro";
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform psastro: $error_code", $exp_tag, $error_code);
-    }
-    &my_die("Unable to find expected output file: $fpaObjects", $exp_tag, $PS_EXIT_PROG_ERROR) unless -f $fpaObjects;
-} else {
-    $fpaObjects = $chipObjects;
-}
-
-# run addstar on either the single chip output or the single fpa output
-# XXX this construct requires the user to have a valid .ptolemyrc 
-# XXX which in turn points at ippconfig/dvo.site
-{
-    my $command = "addstar -D CAMERA $camera $fpaObjects";
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform addstar: $error_code", $exp_tag, $error_code);
-    }
-}
-
-# Make the jpeg for binning 1
-{
-    my $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE " . RECIPE1; # Command to run
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform ppImage: $error_code", $exp_tag, $error_code);
-    }
-    &my_die("Unable to find expected output file: $jpeg1", $exp_tag, $PS_EXIT_PROG_ERROR) unless -f $jpeg1;
-}
-
-# Make the jpeg for binning 2
-{
-    my $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE " . RECIPE2; # Command to run
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform ppImage: $error_code", $exp_tag, $error_code);
-    }
-    &my_die("Unable to find expected output file: $jpeg2", $exp_tag, $PS_EXIT_PROG_ERROR) unless -f $jpeg2;
+unless ($no_op) {
+
+    # run psastro +mosastro on the set of chips
+    # XXX note that this is wrong if imfiles are cells
+    if (@$files > 1) {
+	my $command = "$psastro -list $list3Name $outputRoot +mosastro -chipastro";
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform psastro: $error_code", $exp_tag, $error_code);
+	}
+	&my_die("Unable to find expected output file: $fpaObjects", $exp_tag, $PS_EXIT_PROG_ERROR) unless -f $fpaObjects;
+    } else {
+	$fpaObjects = $chipObjects;
+    }
+
+    # run addstar on either the single chip output or the single fpa output
+    # XXX this construct requires the user to have a valid .ptolemyrc 
+    # XXX which in turn points at ippconfig/dvo.site
+    {
+	my $command = "addstar -D CAMERA $camera $fpaObjects";
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform addstar: $error_code", $exp_tag, $error_code);
+	}
+    }
+    
+    # Make the jpeg for binning 1
+    {
+	my $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE " . RECIPE1; # Command to run
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform ppImage: $error_code", $exp_tag, $error_code);
+	}
+	&my_die("Unable to find expected output file: $jpeg1", $exp_tag, $PS_EXIT_PROG_ERROR) unless -f $jpeg1;
+    }
+
+    # Make the jpeg for binning 2
+    {
+	my $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE " . RECIPE2; # Command to run
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run(command => $command, verbose => 1);
+	unless ($success) {
+	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to perform ppImage: $error_code", $exp_tag, $error_code);
+	}
+	&my_die("Unable to find expected output file: $jpeg2", $exp_tag, $PS_EXIT_PROG_ERROR) unless -f $jpeg2;
+    }
 }
 
@@ -226,4 +230,5 @@
 $jpeg1  = $ipprc->convert_filename_relative($jpeg1);
 $jpeg2  = $ipprc->convert_filename_relative($jpeg2);
+
 unless ($no_update) {
     my $command = "$p3tool -addprocessedexp -exp_tag $exp_tag -uri UNKNOWN " .
Index: trunk/ippScripts/scripts/phase4_overlap.pl
===================================================================
--- trunk/ippScripts/scripts/phase4_overlap.pl	(revision 11831)
+++ trunk/ippScripts/scripts/phase4_overlap.pl	(revision 11837)
@@ -32,10 +32,11 @@
 use Pod::Usage qw( pod2usage );
 
-my ($det_id, $dbname, $workdir, $no_update);
+my ($p4_id, $dbname, $workdir, $no_update, $no_op);
 GetOptions(
     'p4_id|i=s'         => \$p4_id, # Phase 4 identifier
     'dbname|d=s'        => \$dbname, # Database name
     'workdir|w=s'       => \$workdir, # Working directory, for output files
-    'no-update'         => \$no_update
+    'no-update'         => \$no_update,	# Don't update the database?
+    'no-op'             => \$no_op, # Don't do any operations
 ) or pod2usage( 2 );
 
@@ -67,4 +68,5 @@
     }
 
+    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", $p4_id, $PS_EXIT_PROG_ERROR);
@@ -73,17 +75,21 @@
 }
 
+unless ($no_op) {
+
 ### Calculates, in some as-yet unknown way the overlaps between imfiles and skycells
 
-# Add in the skycell and tesselation for each imfile
-foreach my $imfile (@$imfiles) {
-    $imfile->{skycell_id} = 'default';
-    $imfile->{tess_id} = 'default';
+} else {
+    # Add in the skycell and tesselation for each imfile
+    foreach my $imfile (@$imfiles) {
+	$imfile->{skycell_id} = 'default';
+	$imfile->{tess_id} = 'default';
+    }
 }
-
+    
 
 # Generate a MDC file with the overlaps
 $workdir = $ipprc->convert_filename_absolute( $workdir );
-my $overlapName = File::Spec->catfile( $workdir, $p4_id . ".overlap.mdc" ); 
-open my $overlapFile "> $overlapName" or
+my $overlapName = File::Spec->catfile( $workdir, $p4_id . '.overlap.mdc' ); 
+open my $overlapFile, "> $overlapName" or
     &my_die("Unable to open mdc file $overlapName", $p4_id, $PS_EXIT_DATA_ERROR);
 print $overlapFile "p4SkyCellMap MULTI\n\n";
Index: trunk/ippScripts/scripts/phase4_warp.pl
===================================================================
--- trunk/ippScripts/scripts/phase4_warp.pl	(revision 11831)
+++ trunk/ippScripts/scripts/phase4_warp.pl	(revision 11837)
@@ -32,5 +32,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($p4_id, $skycell_id, $camera, $dbname, $workdir, $no_update);
+my ($p4_id, $skycell_id, $camera, $dbname, $workdir, $no_update, $no_op);
 GetOptions(
     'p4_id|i=s'         => \$p4_id, # Phase 4 identifier
@@ -39,5 +39,6 @@
     'dbname|d=s'        => \$dbname, # Database name
     'workdir|w=s'       => \$workdir, # Working directory, for output files
-    'no-update'         => \$no_update
+    'no-update'         => \$no_update,	# Don't update the database?
+    'no-op'             => \$no_op, # Don't do any operations?
 ) or pod2usage( 2 );
 
@@ -62,4 +63,5 @@
 
 # Get list of component imfiles for exposure
+my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
 my $imfiles;
 {
@@ -97,13 +99,14 @@
 open my $listFile, '>' . $listName;
 foreach my $imfile (@$imfiles) {
-    my $uri = $ipprc->convert_filename_absolute( $file->{uri} );
+    my $uri = $ipprc->convert_filename_absolute( $imfile->{uri} );
 
-    print $listFile $uri . "\n";
+    print $listFile "$uri\n";
 }
 close $listFile;
 
 # Run pswarp
-{
-    my $command = "$pswarp -file $uri $outputRoot -stat $outputStats"; # Command to run pswarp
+my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+unless ($no_op) {
+    my $command = "$pswarp -list $listName $outputRoot -stat $outputStats"; # Command to run pswarp
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -116,17 +119,12 @@
     &my_die("Couldn't find expected output file: $outputBin1", $p4_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin1;
     &my_die("Couldn't find expected output file: $outputBin2", $p4_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin2;
-}
 
-# Get the statistics on the warped image
-my $stats;			# Statistics from pswarp
-{
+    # Get the statistics on the warped image
     my $statsFile;		# File handle
     open $statsFile, "$outputStats" or die "Can't open statistics file $outputStats: $!\n";
     my @contents = <$statsFile>; # Contents of file
     close $statsFile;
-    my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
     my $metadata = $mdcParser->parse(join "", @contents)
         or &my_die("Unable to parse metadata config", $p4_id, $skycell_id, $PS_EXIT_PROG_ERROR);
-    $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
     $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $p4_id, $skycell_id, $PS_EXIT_PROG_ERROR);
 }
@@ -137,10 +135,12 @@
 $outputBin2  = $ipprc->convert_filename_relative( $outputBin2  );
 
+my $bg = ($stats->bg_mean() or 'NAN');
+my $bg_stdev = ($stats->bg_stdev() or 'NAN');
+
 # Add the processed file to the database
 unless ($no_update) {
     my $command = "$p4tool -warped -p4_id $p4_id -skycell_id $skycell_id" .
 	" -uri $outputImage -b1_uri $outputRoot";  # Command to run dettool
-    $command .= " -bg " . $stats->bg_mean();
-    $command .= " -bg_stdev " . $stats->bg_stdev();
+    $command .= " -bg $bg -bg_stdev $bg_stdev";
     $command .= " -dbname $dbname" if defined $dbname;
 
Index: trunk/ippScripts/scripts/phase5_subtract.pl
===================================================================
--- trunk/ippScripts/scripts/phase5_subtract.pl	(revision 11831)
+++ trunk/ippScripts/scripts/phase5_subtract.pl	(revision 11837)
@@ -33,5 +33,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($p5_id, $camera, $dbname, $workdir, $no_update);
+my ($p5_id, $camera, $dbname, $workdir, $no_update, $no_op);
 GetOptions(
     'p5_id|d=s'         => \$p5_id, # Phase 5 identifier
@@ -39,5 +39,6 @@
     'dbname|d=s'        => \$dbname, # Database name
     'workdir|w=s'       => \$workdir,	# Working directory, for output files
-    'no-update'         => \$no_update,
+    'no-update'         => \$no_update,	# Don't update the database?
+    'no-op'             => \$no_op, # Don't do any operations?
 ) or pod2usage( 2 );
 
@@ -61,7 +62,8 @@
 
 # Get list of components for subtraction
+my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
 my $files;
 {
-    my $command = "$p5tool -inputscfile -p5_id $p4_id";
+    my $command = "$p5tool -inputscfile -p5_id $p5_id";
     $command .= " -dbname $dbname" if defined $dbname;
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -116,10 +118,11 @@
 
 my $outputName = $outputRoot . ".fits";
-# my $bin1Name =  $ipprc->filename("PPIMAGE.BIN1", $outputRoot, $class_id);
-# my $bin2Name =  $ipprc->filename("PPIMAGE.BIN2", $outputRoot, $class_id);
-# my $outputStats = $outputRoot . '.stats';
+my $bin1Name =  $ipprc->filename("PPIMAGE.BIN1", $outputRoot);
+my $bin2Name =  $ipprc->filename("PPIMAGE.BIN2", $outputRoot);
+my $outputStats = $outputRoot . '.stats';
 
 # Perform subtraction
-{
+my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
+unless ($no_op) {
     my $command = "$pois $template $input $outputName"; # Command to run pois
 
@@ -134,19 +137,14 @@
 #    &my_die("Couldn't find expected output file: $bin2Name",    $p5_id, $PS_EXIT_SYS_ERROR) unless -f $bin2Name;
 #    &my_die("Couldn't find expected output file: $outputStats", $p5_id, $PS_EXIT_SYS_ERROR) unless -f $outputStats;
-}
-
-# Get the statistics on the residual image
-if (0) { ### Disabled because pois doesn't output stats yet
-    my $stats;			# Statistics from ppImage
-    {
+
+    # Get the statistics on the residual image
+    if (0) { ### Disabled because pois doesn't output stats yet
 	my $statsFile;		# File handle
-	open $statsFile, $outputStats or &my_die("Can't open statistics file $outputStats: $!", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR);
+	open $statsFile, $outputStats or &my_die("Can't open statistics file $outputStats: $!", $p5_id, $PS_EXIT_SYS_ERROR);
 	my @contents = <$statsFile>; # Contents of file
-    close $statsFile;
-	my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
+	close $statsFile;
 	my $metadata = $mdcParser->parse(join "", @contents) or
-	    &my_die("Unable to parse metadata config doc", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
-	$stats = PS::IPP::Metadata::Stats->new(); # Stats parser
-	$stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
+	    &my_die("Unable to parse metadata config doc", $p5_id, $PS_EXIT_PROG_ERROR);
+	$stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $p5_id, $PS_EXIT_PROG_ERROR);
     }
 }
@@ -154,15 +152,15 @@
 # Add the processed file to the database
 $outputName = $ipprc->convert_filename_relative( $outputName );
+
+my $bg = ($stats->bg_mean() or 'NAN');
+my $bg_stdev = ($stats->bg_stdev() or 'NAN');
+
 unless ($no_update) {
 
     # Add the subtraction result
     {
-	my $command = "$p5tool -adddiffscfile -p5_id $p5_id -uri $outputName -b1_uri $outputRoot"; # Command to run p5tool
+	my $command = "$p5tool -adddiffscfile -p5_id $p5_id -uri $outputName -b1_uri $outputRoot";
+	$command .= " -bg $bg -bg_stdev $bg_stdev";
 	$command .= " -dbname $dbname" if defined $dbname;
-	
-	if (0) { ### Disabled because no statistics yet
-	    $command .= " -bg " . $stats->bg_mean();
-	    $command .= " -bg_stdev " . $stats->bg_stdev();
-	}
 	
 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -170,5 +168,5 @@
 	unless ($success) {
 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &my_die("Unable to perform p5tool -adddiffscfile: $error_code", $p5_id, $iter, $exp_tag, $class_id, $error_code);
+	    &my_die("Unable to perform p5tool -adddiffscfile: $error_code", $p5_id, $error_code);
 	}
 	
@@ -199,5 +197,5 @@
 
     warn($msg);
-    if ($det_id and $iter and $exp_tag and not $no_update) {
+    if ($p5_id and not $no_update) {
 	my $command = "$p5tool -updaterun -p5_id $p5_id -state stop -code $exit_code";
 	$command .= " -dbname $dbname" if defined $dbname;
