Index: trunk/ippScripts/scripts/detrend_process_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 12800)
+++ trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 13275)
@@ -18,15 +18,15 @@
 use Statistics::Descriptive;
 
-use PS::IPP::Config qw(
-    $PS_EXIT_SUCCESS
-    $PS_EXIT_UNKNOWN_ERROR
-    $PS_EXIT_SYS_ERROR
-    $PS_EXIT_CONFIG_ERROR
-    $PS_EXIT_PROG_ERROR
-    $PS_EXIT_DATA_ERROR
-    $PS_EXIT_TIMEOUT_ERROR
-    );
+use PS::IPP::Config qw($PS_EXIT_SUCCESS
+		       $PS_EXIT_UNKNOWN_ERROR
+		       $PS_EXIT_SYS_ERROR
+		       $PS_EXIT_CONFIG_ERROR
+		       $PS_EXIT_PROG_ERROR
+		       $PS_EXIT_DATA_ERROR
+		       $PS_EXIT_TIMEOUT_ERROR
+		       caturi
+		       );
 my $ipprc = PS::IPP::Config->new(); # IPP configuration
-use File::Spec;
+use File::Temp qw( tempfile );
 
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
@@ -115,30 +115,12 @@
 }
 
-# Set output directory
-if (defined $workdir) {
-    $workdir = $ipprc->convert_filename_absolute( $workdir );
-    my $subdir = "$camera.$det_type.$det_id";
-    $workdir = File::Spec->catdir( $workdir, $subdir, $exp_tag );
-} else {
-    my $example = $ipprc->convert_filename_absolute( ${$files}[0]->{path_base} ); # Example original name
-    my ($vol, $dir, $file) = File::Spec->splitpath( $example );
-    $workdir = $dir;
-}
-system "mkdir -p $workdir" unless -d $workdir;
-
-my $outputFile = $exp_tag . '.detproc.' . $det_id; # Root name
-my $outputRoot = File::Spec->catfile( $workdir, $outputFile );
-
-# output files:
-my $list1Name = $outputRoot . '.b1.list'; # Name for the input file list for binning 1
-my $list2Name = $outputRoot . '.b2.list'; # Name for the input file list for binning 2
+# File lists for the two binnings
+my ($list1File, $list1Name) = tempfile( "$exp_tag.detproc.$det_id.b1.list.XXXX", UNLINK => 1 );
+my ($list2File, $list2Name) = tempfile( "$exp_tag.detproc.$det_id.b2.list.XXXX", UNLINK => 1 );
 my @means;			# Array of means
 my @stdevs;			# Array of stdevs
-open my $list1File, '>' . $list1Name;
-open my $list2File, '>' . $list2Name;
 foreach my $file (@$files) {
-    my $origRoot = $ipprc->convert_filename_absolute( $file->{path_base} ); # Original root name
-    print $list1File ($ipprc->filename( "PPIMAGE.BIN1", $origRoot, $file->{class_id} ) . "\n");
-    print $list2File ($ipprc->filename( "PPIMAGE.BIN2", $origRoot, $file->{class_id} ) . "\n");
+    print $list1File ($ipprc->filename( "PPIMAGE.BIN1", $file->{path_base}, $file->{class_id} ) . "\n");
+    print $list2File ($ipprc->filename( "PPIMAGE.BIN2", $file->{path_base}, $file->{class_id} ) . "\n");
     push @means, $file->{bg};
     push @stdevs, $file->{bg_stdev};
@@ -147,5 +129,7 @@
 close $list2File;
 
-# Output products
+# Output files
+$workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
+my $outputRoot = $ipprc->file_prepare( "$exp_tag.detproc.$det_id", $workdir, ${$files}[0]->{path_base} );
 my $jpeg1 = $ipprc->filename("PPIMAGE.JPEG1", $outputRoot); # Binned JPEG #1
 my $jpeg2 = $ipprc->filename("PPIMAGE.JPEG2", $outputRoot); # Binned JPEG #2
@@ -161,5 +145,5 @@
 	    &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;
+	&my_die("Unable to find expected output file: $jpeg1", $det_id, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($jpeg1);
     }
     
@@ -173,10 +157,7 @@
 	    &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
-$outputRoot = $ipprc->convert_filename_relative( $outputRoot );
+	&my_die("Unable to find expected output file: $jpeg2", $det_id, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($jpeg2);
+    }
+}
 
 unless ($no_update) {
@@ -192,7 +173,4 @@
 	exit($error_code);
     }
-
-    unlink $list1Name;
-    unlink $list2Name;
 }
 
