Index: trunk/ippScripts/scripts/detrend_process_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 11298)
+++ trunk/ippScripts/scripts/detrend_process_exp.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;
@@ -47,5 +55,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
@@ -58,8 +69,12 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -processedimfile: $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;
+	&my_die("Unable to perform dettool -processedimfile: $error_code", $det_id, $exp_tag, $error_code);
+    }
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata config doc", $det_id, $exp_tag, $PS_EXIT_PROG_ERROR);
+    $files = parse_md_list($metadata) or
+	&my_die("Unable to parse metadata list", $det_id, $exp_tag, $PS_EXIT_PROG_ERROR);
 }
 
@@ -70,8 +85,8 @@
     my @variances;    # Array of variances for each component
     foreach my $file (@$files) {
-	die "Unable to find class id\n" unless defined $file->{class_id};
+	&my_die("Unable to find class id", $det_id, $exp_tag, $PS_EXIT_SYS_ERRROR) unless defined $file->{class_id};
 	my $class_id = $file->{class_id};
-	die "Unable to find bg for class_id=$class_id\n" unless defined $file->{bg};
-	die "Unable to find bg_mean_stdev for class_id=$class_id\n" unless defined $file->{bg_mean_stdev};
+	&my_die("Unable to find bg for class_id=$class_id", $det_id, $exp_tag, $PS_EXIT_SYS_ERROR) unless defined $file->{bg};
+	&my_die("Unable to find bg_mean_stdev for class_id=$class_id", $det_id, $exp_tag, $PS_EXIT_SYS_ERROR) unless defined $file->{bg_mean_stdev};
 	push @backgrounds, $file->{bg};
 	push @variances, $file->{bg_stdev}**2;
@@ -125,5 +140,9 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to find expected output file: $jpeg1\n" if not -f $jpeg1;
+    unless ($success) {
+	$error_code >> 8;
+	&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;
 }
 
@@ -133,5 +152,9 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to find expected output file: $jpeg2\n" if not -f $jpeg2;
+    unless ($success) {
+	$error_code >> 8;
+	&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;
 }
 
@@ -148,8 +171,29 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -addprocessedexp: $error_code\n" if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform dettool -addprocessedexp: $error_code");
+	exit($error_code);
+    }
 
     unlink $list1Name;
     unlink $list2Name;
+}
+
+
+sub my_die
+{
+    my $msg = shift; # Warning message on die
+    my $det_id = shift;		# Detrend identifier
+    my $exp_tag = shift; # Exposure tag
+    my $exit_code = shift; # Exit code to add
+
+    warn($msg);
+    if ($det_id && $exp_tag) {
+	my $command = "$dettool -addprocessedexp -det_id $det_id -exp_tag $exp_tag -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
 }
 
