Index: trunk/ippScripts/scripts/detrend_reject_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 11298)
+++ trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 11316)
@@ -12,5 +12,13 @@
 use Statistics::Descriptive;
 
-use PS::IPP::Config;
+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
+    );
 my $ipprc = PS::IPP::Config->new(); # IPP configuration
 use File::Spec;
@@ -52,5 +60,8 @@
 my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
 my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
-die "Can't find required tools.\n" if $missing_tools;
+if ($missing_tools) { 
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR); 
+}
 
 my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
@@ -63,8 +74,14 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -residimfile: $error_code\n" if not $success;
-    my $metadata = $mdcParser->parse(join "", @$stdout_buf) 
-        or die "unable to parse metadata config doc";
-    $files = parse_md_list($metadata);
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform dettool -residimfile: $error_code\n");
+	exit($error_code);
+    }
+
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata config doc", $det_id, $iter, $exp_tag, $PS_EXIT_PROG_ERROR);
+    $files = parse_md_list($metadata) or
+	&my_die("Unable to parse metadata list", $det_id, $iter, $exp_tag, $PS_EXIT_PROG_ERROR);
 }
 
@@ -107,5 +124,9 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to find expected output file: $jpeg1Name\n" if not -f $jpeg1Name;
+    unless ($success) {
+	$error_code >> 8;
+	&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;
 }
 
@@ -115,5 +136,9 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to find expected output file: $jpeg2Name\n" if not -f $jpeg2Name;
+    unless ($success) {
+	$error_code >> 8;
+	&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;
 }
 
@@ -131,5 +156,5 @@
 # Reject based on the stats of the imfiles
 # it is VALID to reject on more than one criterion
-die "Number of means and number of variances differ!\n" if scalar @means != scalar @variances;
+&my_die("Number of means and number of variances differ!", $det_id, $iter, $exp_tag, $PS_EXIT_PROG_ERROR) unless scalar @means == scalar @variances;
 for (my $i = 0; $i < scalar @means; $i++) {
     my $mean = $means[$i];	# Mean for this imfile
@@ -248,5 +273,9 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -addresidexp: $error_code\n" if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform dettool -addresidexp: $error_code\n");
+	exit($error_code);
+    }
 
     unlink $list1Name;
@@ -259,4 +288,22 @@
         or die "failed to execute sync: $!" ;
     $? = $status;
+}
+
+
+sub my_die
+{
+    my $msg = shift; # Warning message on die
+    my $det_id = shift;		# Detrend identifier
+    my $iter = shift;		# Iteration
+    my $exp_tag = shift; # Exposure tag
+    my $exit_code = shift; # Exit code to add
+
+    warn($msg);
+    if ($det_id and $iteration and $exp_tag) {
+	my $command = "$dettool -addresidexp -det_id $det_id -iteration $iter -exp_tag $exp_tag -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
 }
 
