Index: trunk/ippScripts/scripts/detrend_stack.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_stack.pl	(revision 14009)
+++ trunk/ippScripts/scripts/detrend_stack.pl	(revision 14048)
@@ -88,19 +88,13 @@
 }
 
-$workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
-
-my $outputRoot  = $ipprc->file_prepare( "$camera.$det_type.$det_id.$iter.$class_id", $workdir, ${$files}[0]->{uri} );
-my $outputStack = $outputRoot . '.fits'; # Output name
-my $outputStats = $outputRoot . '.stats'; # Statistics name
-
 # Get list of files to stack
-my $files;			# Array of files to be stacked
+my ($files, $command, $success, $error_code, $full_buf, $stdout_buf, $stderr_buf);
 {
-    my $command = "$dettool -processedimfile -included";
+    $command = "$dettool -processedimfile -included";
     $command .= " -det_id $det_id";
     $command .= " -class_id $class_id";
     $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 => $verbose);
     unless ($success) {
@@ -116,5 +110,11 @@
 }
 
-my $command = "$ppMerge $outputStack"; # Command to run
+$workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
+
+my $outputRoot  = $ipprc->file_prepare( "$camera.$det_type.$det_id.$iter.$class_id", $workdir, ${$files}[0]->{uri} );
+my $outputStack = $outputRoot . '.fits'; # Output name
+my $outputStats = $outputRoot . '.stats'; # Statistics name
+
+$command = "$ppMerge $outputStack"; # Command to run
 foreach my $file (@$files) {
     $command .= ' ' . $file->{uri};
@@ -128,5 +128,5 @@
 unless ($no_op) {
     
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => $verbose);
     unless ($success) {
