Index: trunk/ippScripts/scripts/detrend_reject_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 12979)
+++ trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 13275)
@@ -17,16 +17,16 @@
 use PS::IPP::Metadata::List qw( parse_md_list );
 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 File::Temp qw( tempfile );
+
+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 Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
@@ -93,33 +93,14 @@
 }
 
-# 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;
-
-# Generate the file list, and get the statistics
-my $outputFile = "$exp_tag.detresid.$det_id.$iter"; # Root name
-my $outputRoot = File::Spec->catfile( $workdir, $outputFile );
-
-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
-
+
+my ($list1File, $list1Name) = tempfile( "$exp_tag.detresid.$det_id.$iter.b1.list.XXXX", UNLINK => 1 );
+my ($list2File, $list2Name) = tempfile( "$exp_tag.detresid.$det_id.$iter.b2.list.XXXX", UNLINK => 1 );
 my @means;			# Array of means
 my @variances;			# Array of variances
 my @meanStdevs;			# Array of mean stdevs
 my @names;			# Array of names (class_id)
-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 @meanStdevs, $file->{bg_mean_stdev};
@@ -131,5 +112,7 @@
 close $list2File;
 
-# Output products --- need to synch with the camera configuration!
+# Output products
+$workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
+my $outputRoot = $ipprc->file_prepare( "$exp_tag.detresid.$det_id.$iter", $workdir, ${$files}[0]->{path_base} );
 my $jpeg1Name = $ipprc->filename("PPIMAGE.JPEG1", $outputRoot); # Binned JPEG #1
 my $jpeg2Name = $ipprc->filename("PPIMAGE.JPEG2", $outputRoot); # Binned JPEG #2
@@ -146,5 +129,5 @@
 	    &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;
+	&my_die("Unable to find expected output file: $jpeg1Name", $det_id, $iter, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($jpeg1Name);
     }
     
@@ -158,5 +141,5 @@
 	    &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;
+	&my_die("Unable to find expected output file: $jpeg2Name", $det_id, $iter, $exp_tag, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($jpeg2Name);
     }
 }
@@ -173,9 +156,8 @@
 my $reject_exp_sn           = rejection_limit( 'EXP.SN',           $det_type, $filter );
 
-my $logName = "$exp_tag.detreject.$det_id.$iter.log"; # Name for log
-$logName = File::Spec->catfile( $workdir, $logName );
+my $logName = caturi( $workdir, "$exp_tag.detreject.$det_id.$iter.log" ); # Name for log
 my $logFile;
 unless ($no_op) {
-    open $logFile, "> $logName" or &my_die("Unable to open log file $logName", $det_id, $iter, $exp_tag, $PS_EXIT_SYS_ERROR);
+    $logFile = $ipprc->file_create_open( $logName );
 }
 
@@ -311,6 +293,4 @@
 
 # Add the result into the database
-$outputRoot = $ipprc->convert_filename_relative( $outputRoot );
-
 my $bg = $mean;
 my $bg_stdev = $stdev;
@@ -330,7 +310,4 @@
 	exit($error_code);
     }
-
-    unlink $list1Name;
-    unlink $list2Name;
 }
 
