Index: trunk/ippScripts/scripts/detrend_create_resid.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_create_resid.pl	(revision 8511)
+++ trunk/ippScripts/scripts/detrend_create_resid.pl	(revision 8609)
@@ -24,12 +24,21 @@
 };
 
+# Prefix to add to filenames
+use constant PREFIX => {
+    'bias' => 'ob',		# Overscan only
+    'dark' => 'obd',		# Overscan and bias only
+    'flat' => 'obdf',		# Overscan, bias and dark only
+};
+
+
 use constant DELETE_STATS => 0;	# Delete the statistics file when done?
 
 # Parse the command-line arguments
-if (scalar @ARGV != 7) {
+if (scalar @ARGV != 8) {
     die "Apply a stacked detrend image to an individual detrend.\n\n" .
-	"Usage: $0 DET_ID EXP_ID CLASS_ID DETREND_TYPE DETREND.fits INPUT.fits OUTPUT_ROOT\n\n";
+	"Usage: $0 DET_ID ITERATION EXP_ID CLASS_ID DETREND_TYPE DETREND.fits INPUT.fits OUTPUT_ROOT\n\n";
 }
 my $detId = shift @ARGV;	# Detrend ID
+my $iter = shift @ARGV;		# Iteration
 my $expId = shift @ARGV;	# Exposure ID
 my $classId = shift @ARGV;	# Class ID
@@ -51,14 +60,19 @@
 my $detFlag = DETRENDS->{$detType};
 die "Unrecognised detrend type: $detType\n" if not defined $detFlag;
+# Prefix to use for filename
+my $prefix = PREFIX->{$detType};
+die  "Unrecognised detrend type: $detType\n" if not defined $prefix;
 
 ### Output file names --- must match camera configuration!
-my $outputName = $output . '.' . $classId . '.fit';
-my $bin1Name = $output . '.' . $classId . '.b1.fit';
-my $bin2Name =  $output . '.' . $classId . '.b2.fit';
+my $outputRoot = $prefix . '_' . $output;
+my $outputName = $outputRoot . '.' . $classId . '.fit';
+my $outputStats = $outputRoot . '.' . $classId . '.stats';
+my $bin1Name = $outputRoot . '.' . $classId . '.b1.fit';
+my $bin2Name =  $outputRoot . '.' . $classId . '.b2.fit';
 
 # Run ppImage
 {
-    my $command = "$ppImage -file $input $output -recipe PPIMAGE $recipe" .
-	" -stat $output.stats $detFlag $detrend"; # Command to run ppImage
+    my $command = "$ppImage -file $input $outputRoot -recipe PPIMAGE $recipe" .
+	" -stat $outputStats $detFlag $detrend"; # Command to run ppImage
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -73,5 +87,5 @@
 {
     my $statsFile;		# File handle
-    open $statsFile, "$output.stats" or die "Can't open statistics file $output.stats: $!\n";
+    open $statsFile, "$outputStats" or die "Can't open statistics file $outputStats: $!\n";
     my @contents = <$statsFile>; # Contents of file
     close $statsFile;
@@ -84,7 +98,7 @@
 # Add the processed file to the database
 {
-    my $command = "dettool -addresidimfile -det_id $detId -exp_id $expId " .
-	"-class_id $classId -recipe $recipe -uri $outputName -b1 $bin1Name " .
-	"-b2 $bin2Name"; # Command to run dettool
+    my $command = "$dettool -addresidimfile -det_id $detId -iteration $iter -exp_id $expId " .
+	"-class_id $classId -recip $recipe -uri $outputName -b1_uri $bin1Name " .
+	"-b2_uri $bin2Name"; # Command to run dettool
     $command .= " -bg " . $stats->bg_mean();
     $command .= " -bg_stdev " . $stats->bg_stdev();
