Index: trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 11297)
+++ trunk/ippScripts/scripts/detrend_reject_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
 
@@ -42,5 +50,9 @@
 my $missing_tools;
 my $dettool = can_run('dettool') or (warn "Can't find dettool" 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
 
@@ -52,8 +64,13 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -residexp: $error_code\n" if not $success;
-    my $metadata = $mdcParser->parse(join "", @$stdout_buf)
-        or die "unable to parse metadata config doc";
-    $exposures = parse_md_list($metadata);
+    unless ($success) {
+	$error_code >> 8;
+	&my_die("Unable to perform dettool -residexp: $error_code", $det_id, $iter, $error_code);
+    }
+
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata config doc", $det_id, $iter, $PS_EXIT_PROG_ERROR);
+    $exposures = parse_md_list($metadata) or
+	&my_die("Unable to parse metadata list", $det_id, $iter, $PS_EXIT_PROG_ERROR);
 }
 
@@ -66,10 +83,10 @@
 my @include;			# Array of include flags
 foreach my $exposure (@$exposures) {
-    die "Unable to find exposure id.\n" if not defined $exposure->{exp_tag};
-    die "Unable to find mean.\n" if not defined $exposure->{bg};
-    die "Unable to find stdev.\n" if not defined $exposure->{bg_stdev};
-    die "Unable to find mean stdev.\n" if not defined $exposure->{bg_mean_stdev};
-    die "Unable to find accept.\n" if not defined $exposure->{accept};
-    die "Unable to find include.\n" if not defined $exposure->{include};
+    &my_die("Unable to find exposure id.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{exp_tag};
+    &my_die("Unable to find mean.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{bg};
+    &my_die("Unable to find stdev.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{bg_stdev};
+    &my_die("Unable to find mean stdev.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{bg_mean_stdev};
+    &my_die("Unable to find accept.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{accept};
+    &my_die("Unable to find include.\n", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $exposure->{include};
     push @expTags, $exposure->{exp_tag};
     push @means, $exposure->{bg};
@@ -170,5 +187,8 @@
 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	    run(command => $command, verbose => 1);
-	die "Unable to perform dettool -updateresidexp: $error_code\n" if not $success;
+	unless ($success) {
+	    $error_code >> 8;
+	    &my_die("Unable to perform dettool -updateresidexp: $error_code", $det_id, $iter, $error_code);
+	}
     }
 }
@@ -205,5 +225,9 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -adddetrunsummary: $error_code\n" if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform dettool -adddetrunsummary: $error_code");
+	exit($error_code);
+    }
 }
 
@@ -221,5 +245,9 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -updatedetrun: $error_code\n" if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform dettool -updatedetrun: $error_code");
+	exit($error_code);
+    }
 }
 
@@ -231,5 +259,24 @@
 }
 
-# Retrieve the requested rejection limit, dying if not extant
+
+sub my_die
+{
+    my $msg = shift; # Warning message on die
+    my $det_id = shift;		# Detrend identifier
+    my $iter = shift;		# Iteration
+    my $exit_code = shift; # Exit code to add
+
+    warn($msg);
+    if ($det_id and $iter) {
+	my $command = "$dettool -adddetrunsummary -det_id $det_id -iteration $iter -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
+}
+
+
+
+# Retrieve the requested rejection limit, dying unless extant
 sub rejection_limit
 {
@@ -240,5 +287,5 @@
     my $value = $ipprc->rejection( $name, $det_type, $filter );
     if (not defined $value) {
-	$filter = "(no filter)" if not defined $filter;
+	$filter = "(no filter)" unless defined $filter;
 	die "Unable to determine $name rejection limit for $det_type with $filter.\n";
     }
