Index: /trunk/ippScripts/scripts/detrend_norm_apply.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_norm_apply.pl	(revision 11315)
+++ /trunk/ippScripts/scripts/detrend_norm_apply.pl	(revision 11316)
@@ -8,5 +8,13 @@
 use Data::Dumper;
 
-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;
@@ -60,5 +68,9 @@
 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); 
+}
 
 unless (defined $workdir) {
@@ -85,9 +97,12 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform ppImage: $error_code\n" if not $success;
-    die "Can't find expected output file: $output\n" if not -e $output;
-    die "Can't find expected output file: $b1name\n" if not -e $b1name;
-    die "Can't find expected output file: $b2name\n" if not -e $b2name;
-    die "Can't find expected output file: $statsName\n" if not -e $statsName;
+    unless ($success) {
+	$error_code >> 8;
+	&my_die("Unable to perform ppImage: $error_code", $det_id, $iter, $class_id, $error_code);
+    }
+    &my_die("Can't find expected output file: $output", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -e $output;
+    &my_die("Can't find expected output file: $b1name", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -e $b2name;
+    &my_die("Can't find expected output file: $b2name", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -e $b1name;
+    &my_die("Can't find expected output file: $statsName", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -e $statsName;
 }
 
@@ -96,12 +111,12 @@
 {
     my $statsFile;		# File handle
-    open $statsFile, $statsName or die "Can't open statistics file $statsName: $!\n";
+    open $statsFile, $statsName or &my_die("Can't open statistics file $statsName: $!\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
     my @contents = <$statsFile>; # Contents of file
     close $statsFile;
     my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
     my $metadata = $mdcParser->parse(join "", @contents)
-        or die "unable to parse metadata config";
+        or &my_die("Unable to parse metadata config", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
     $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
-    $stats->parse($metadata) or die "Unable to find all values in statistics output.\n";
+    $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
 }
 
@@ -127,9 +142,30 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -addnormalizedimfile: $error_code\n"
-	if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform dettool -addnormalizedimfile: $error_code\n");
+	exit($error_code);
+    }
 
     unlink $statsName;
 }
+
+sub my_die
+{
+    my $msg = shift;		# Warning message on die
+    my $det_id = shift;		# Detrend identifier
+    my $iter = shift;		# Iteration
+    my $class_id = shift;	# Class identifier
+    my $exit_code = shift;	# Exit code to add
+
+    warn($msg);
+    if ($det_id and $iter and $class_id) {
+	my $command = "$dettool -addnormalizedimfile -det_id $det_id -iteration $iter -class_id $class_id -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
+}
+
 
 END {
@@ -139,2 +175,4 @@
     $? = $status;
 }
+
+__END__
Index: /trunk/ippScripts/scripts/detrend_norm_calc.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_norm_calc.pl	(revision 11315)
+++ /trunk/ippScripts/scripts/detrend_norm_calc.pl	(revision 11316)
@@ -9,4 +9,14 @@
 use PS::IPP::Metadata::List qw( parse_md_list );
 use Data::Dumper;
+
+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 Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
@@ -54,8 +64,13 @@
 my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
 my $ppNormCalc = can_run('ppNormCalc') or (warn "Can't find ppNormCalc" and $missing_tools = 1);
-die "Can't find required tools.\n" if $missing_tools;
-
-die "Unrecognised detrend type: $detType\n" unless exists NORMALIZE()->{lc($detType)};
-
+if ($missing_tools) { 
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR); 
+}
+
+unless (exists NORMALIZE()->{lc($detType)}) {
+    warn("Unrecognised detrend type: $detType");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
@@ -69,6 +84,8 @@
     my ( $stdin, $stdout, $stderr ); # Buffers for running program
     print "Running [$command]...\n";
-    run \@command, \$stdin, \$stdout, \$stderr or
-	die "Unable to perform dettool -processedimfile on detrend $detId/$iter: $?";
+    if (not run(\@command, \$stdin, \$stdout, \$stderr)) {
+	warn("Unable to perform dettool -processedimfile on detrend $detId/$iter: $?");
+	exit($PS_EXIT_SYS_ERROR);
+    }
 #    print $stdout . "\n";
     
@@ -82,6 +99,9 @@
 	    push @single, "\n";
 
-	    my $list = parse_md_list( $mdcParser->parse( join( "\n", @single ) ) ) or
-		die "Unable to parse output from dettool.\n";
+	    my $list = parse_md_list( $mdcParser->parse( join( "\n", @single ) ) );
+	    unless (defined $list) {
+		warn("Unable to parse output from dettool.");
+		exit($PS_EXIT_PROG_ERROR);
+	    }
 	    push @files, @$list;
 	    @single = ();
@@ -119,11 +139,16 @@
 	my @command = split /\s+/, $ppNormCalc;
 	print "Running [$ppNormCalc]...\n";
-	run \@command, \$normData, \$stdout, \$stderr or
-	    die "Unable to perform ppNormCalc: $?";
+	if (not run(\@command, \$normData, \$stdout, \$stderr)) {
+	    warn("Unable to perform ppNormCalc: $?");
+	    exit($PS_EXIT_SYS_ERROR);
+	}
 	print $stdout . "\n";
 	
 	# Parse the output
-	$norms = $mdcParser->parse($stdout)
-	    or die "unable to parse metadata config doc";
+	$norms = $mdcParser->parse($stdout);
+	unless ($norms) {
+	    warn("Unable to parse metadata config doc");
+	    exit($PS_EXIT_PROG_ERROR);
+	}
     }
 
@@ -158,6 +183,8 @@
 	my ( $stdin, $stdout, $stderr ); # Buffers for running program
 	print "Running [$command]...\n";
-	run \@command, \$stdin, \$stdout, \$stderr or
-	    die "Unable to perform dettool -addnormstat for $className: $?";
+	if (not run \@command, \$stdin, \$stdout, \$stderr) {
+	    warn("Unable to perform dettool -addnormstat for $className: $?");
+	    exit($PS_EXIT_SYS_ERROR);
+	}
 	print $stdout . "\n";
     }
@@ -170,2 +197,4 @@
     $? = $status;
 }
+
+__END__
Index: /trunk/ippScripts/scripts/detrend_norm_exp.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_norm_exp.pl	(revision 11315)
+++ /trunk/ippScripts/scripts/detrend_norm_exp.pl	(revision 11316)
@@ -8,5 +8,13 @@
 
 use IPC::Cmd qw( can_run run );
-use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::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::Metadata::List qw( parse_md_list );
 use Statistics::Descriptive;
@@ -49,5 +57,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
@@ -60,8 +71,12 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -normalizedimfile: $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 ppImage: $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);
+    $files = parse_md_list($metadata) or
+	&my_die("Unable to parse metadata list", $det_id, $iter, $PS_EXIT_PROG_ERROR);
 }
 
@@ -72,8 +87,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, $iter, $PS_EXIT_SYS_ERROR) 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, $iter, $PS_EXIT_SYS_ERROR) unless defined $file->{bg};
+	&my_die("Unable to find bg_mean_stdev for class_id=$class_id", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless defined $file->{bg_mean_stdev};
 	push @backgrounds, $file->{bg};
 	push @variances, $file->{bg_stdev}**2;
@@ -127,5 +142,5 @@
     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;
+    &my_die("Unable to find expected output file: $jpeg1Name", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless -f $jpeg1Name;
 }
 
@@ -135,5 +150,5 @@
     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;
+    &my_die("Unable to find expected output file: $jpeg2Name", $det_id, $iter, $PS_EXIT_SYS_ERROR) unless -f $jpeg2Name;
 }
 
@@ -148,8 +163,28 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -addnormalizedexp: $error_code\n" if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	&my_die("Unable to perform dettool -addnormalizedexp: $error_code", $det_id, $iter, $error_code);
+    }
 
     unlink $list1Name;
     unlink $list2Name;
+}
+
+
+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 -addprocessedimfile -det_id $det_id -iter $iter -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
 }
 
Index: /trunk/ippScripts/scripts/detrend_process_exp.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 11315)
+++ /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;
 }
 
Index: /trunk/ippScripts/scripts/detrend_process_imfile.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 11315)
+++ /trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 11316)
@@ -11,5 +11,13 @@
 use PS::IPP::Metadata::Stats;
 
-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;
@@ -60,9 +68,15 @@
 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); 
+}
 
 # Recipe to use in processing
 my $recipe = RECIPES->{lc($det_type)};
-die "Unrecognised detrend type: $det_type\n" if not defined $recipe;
+unless (defined $recipe) {
+    warn("Unrecognised detrend type: $det_type");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 ### Output file name
@@ -88,9 +102,12 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform ppImage on $input_uri: $error_code\n" if not $success;
-    die "Couldn't find expected output file: $outputImage\n" if not -f $outputImage;
-    die "Couldn't find expected output file: $outputStats\n" if not -f $outputStats;
-    die "Couldn't find expected output file: $outputBin1\n" if not -f $outputBin1;
-    die "Couldn't find expected output file: $outputBin2\n" if not -f $outputBin2;
+    unless ($success) {
+	$error_code >> 8;
+	&my_die("Unable to perform ppImage: $error_code", $det_id, $exp_tag, $class_id, $error_code);
+    }
+    &my_die("Couldn't find expected output file: $outputImage", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputImage;
+    &my_die("Couldn't find expected output file: $outputStats", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStats;
+    &my_die("Couldn't find expected output file: $outputBin1", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin1;
+    &my_die("Couldn't find expected output file: $outputBin2", $det_id, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin2;
 }
 
@@ -104,7 +121,7 @@
     my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
     my $metadata = $mdcParser->parse(join "", @contents)
-        or die "unable to parse metadata config";
+        or &my_die("Unable to parse metadata config", $det_id, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
     $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
-    $stats->parse($metadata) or die "Unable to find all values in statistics output.\n";
+    $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
 }
 
@@ -131,8 +148,28 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -addprocessedimfile for $det_id/$exp_tag/$class_id: $error_code\n"
-	if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform dettool -addprocessedimfile: $error_code\n");
+	exit($error_code);
+    }
 
     unlink $outputStats;
+}
+
+sub my_die
+{
+    my $msg = shift; # Warning message on die
+    my $det_id = shift;		# Detrend identifier
+    my $exp_tag = shift; # Exposure tag
+    my $class_id = shift; # Class identifier
+    my $exit_code = shift; # Exit code to add
+
+    warn($msg);
+    if ($det_id and $exp_tag and $class_id) {
+	my $command = "$dettool -addprocessedimfile -det_id $det_id -exp_tag $exp_tag -class_id $class_id -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
 }
 
Index: /trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 11315)
+++ /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";
     }
Index: /trunk/ippScripts/scripts/detrend_reject_imfile.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 11315)
+++ /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;
 }
 
Index: /trunk/ippScripts/scripts/detrend_resid.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_resid.pl	(revision 11315)
+++ /trunk/ippScripts/scripts/detrend_resid.pl	(revision 11316)
@@ -12,5 +12,13 @@
 use Data::Dumper;
 
-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;
@@ -92,10 +100,13 @@
 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); 
+}
 
 # Recipe to use in processing
-die "Unrecognised mode: $mode\n" if not defined RECIPES->{$mode};
+&my_die("Unrecognised mode: $mode", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR) unless defined RECIPES->{$mode};
 my $recipe = RECIPES->{$mode}->{lc($det_type)};
-die "Unrecognised detrend type: $det_type\n" if not defined $recipe;
+&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR) unless defined $recipe;
 
 ### Output file names --- must match camera configuration!
@@ -124,5 +135,5 @@
     if (lc($mode) ne 'verify') {
 	my $detFlag = DETRENDS->{lc($det_type)};
-	die "Unrecognised detrend type: $det_type\n" if not defined $detFlag;
+	&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR) unless defined $detFlag;
 	$command .= "$detFlag $detrend";
     }
@@ -130,9 +141,12 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform ppImage on $input_uri: $error_code\n" if not $success;
-    die "Couldn't find expected output file: $outputName\n" if not -f $outputName;
-    die "Couldn't find expected output file: $outputStats\n" if not -f $outputStats;
-    die "Couldn't find expected output file: $bin1Name\n" if not -f $bin1Name;
-    die "Couldn't find expected output file: $bin2Name\n" if not -f $bin2Name;
+    unless ($success) {
+	$error_code >> 8;
+	&my_die("Unable to perform ppImage: $error_code", $det_id, $iter, $exp_tag, $class_id, $error_code);
+    }
+    &my_die("Couldn't find expected output file: $outputName", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputName;
+    &my_die("Couldn't find expected output file: $outputStats", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStats;
+    &my_die("Couldn't find expected output file: $bin1Name", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $bin1Name;
+    &my_die("Couldn't find expected output file: $bin2Name", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $bin2Name;
 }
 
@@ -141,12 +155,12 @@
 {
     my $statsFile;		# File handle
-    open $statsFile, $outputStats or die "Can't open statistics file $outputStats: $!\n";
+    open $statsFile, $outputStats or &my_die("Can't open statistics file $outputStats: $!", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_SYS_ERROR);
     my @contents = <$statsFile>; # Contents of file
     close $statsFile;
     my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
-    my $metadata = $mdcParser->parse(join "", @contents)
-        or die "unable to parse metadata config doc";
+    my $metadata = $mdcParser->parse(join "", @contents) or
+	&my_die("Unable to parse metadata config doc", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
     $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
-    $stats->parse($metadata) or die "Unable to find all values in statistics output.\n";
+    $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
 }
 
@@ -173,8 +187,31 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -addprocessed for $det_id/$exp_tag/$class_id: $error_code\n"
-	if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform dettool -addresidimfile: $error_code\n");
+	exit($error_code);
+    }
 
     unlink $outputStats;    
+}
+
+
+
+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 $class_id = shift; # Class identifier
+    my $exit_code = shift; # Exit code to add
+
+    warn($msg);
+    if ($det_id and $iter and $exp_tag) {
+	my $command = "$dettool -addresidimfile -det_id $det_id -iteration $iter -exp_tag $exp_tag -class_id $class_id -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
 }
 
Index: /trunk/ippScripts/scripts/detrend_stack.pl
===================================================================
--- /trunk/ippScripts/scripts/detrend_stack.pl	(revision 11315)
+++ /trunk/ippScripts/scripts/detrend_stack.pl	(revision 11316)
@@ -12,5 +12,13 @@
 use PS::IPP::Metadata::Stats;
 
-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,12 +60,15 @@
     };
 
-my $recipe = RECIPES()->{lc($det_type)}; # Recipe to use in stacking
-die "Unrecognised detrend type: $det_type\n" if not defined $recipe;
-
 # Look for programs we need
 my $missing_tools;
 my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
 my $ppMerge = can_run('ppMerge') or (warn "Can't find ppMerge" 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 $recipe = RECIPES()->{lc($det_type)}; # Recipe to use in stacking
+&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR) unless defined $recipe;
 
 my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
@@ -70,8 +81,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, $iter, $class_id, $error_code);
+    }
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata config doc", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
+    $files = parse_md_list($metadata) or 
+	&my_die("Unable to parse metadata list", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR);
 }
 
@@ -101,7 +116,10 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform ppMerge: $error_code\n" if not $success;
-    die "Unable to find expected output file: $outputStack\n" if not -f $outputStack;
-    die "Unable to find expected output file: $outputStats\n" if not -f $outputStats;
+    unless ($success) {
+	$error_code >> 8;
+	&my_die("Unable to perform ppMerge: $error_code", $det_id, $iter, $class_id, $error_code);
+    }
+    &my_die("Unable to find expected output file: $outputStack\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStack;
+    &my_die("Unable to find expected output file: $outputStats\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStats;
 }
 
@@ -109,14 +127,15 @@
 my $stats;			# Statistics from ppImage
 {
-    open(my $statsFile, "$outputStats")
-        or die "Can't open statistics file $outputStats: $!\n";
+    open(my $statsFile, "$outputStats") or
+	&my_die("Can't open statistics file $outputStats: $!", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
     my $contents = do { local $/; <$statsFile> }; # Contents of file
     close($statsFile);
 
     my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
-    my $metadata = $mdcParser->parse($contents)
-        or die "unable to parse metadata config doc";
+    my $metadata = $mdcParser->parse($contents) or
+	&my_die("Unable to parse metadata config doc", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
     $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
-    $stats->parse($metadata) or die "Unable to find all values in statistics output.\n";
+    $stats->parse($metadata)  or
+	&my_die("Unable to find all values in statistics output.", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR);
 }
 
@@ -137,7 +156,29 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform dettool -addstacked: $error_code\n" if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform dettool -addstacked: $error_code\n");
+	exit($error_code);
+    }
 
     unlink $outputStats;
+}
+
+
+sub my_die
+{
+    my $msg = shift; # Warning message on die
+    my $det_id = shift;		# Detrend identifier
+    my $iter = shift;		# Iteration
+    my $class_id = shift; # Class identifier
+    my $exit_code = shift; # Exit code to add
+
+    warn($msg);
+    if ($det_id and $iter and $class_id) {
+	my $command = "$dettool -addstacked -det_id $det_id -iteration $iter -class_id $class_id -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
 }
 
Index: /trunk/ippScripts/scripts/phase2.pl
===================================================================
--- /trunk/ippScripts/scripts/phase2.pl	(revision 11315)
+++ /trunk/ippScripts/scripts/phase2.pl	(revision 11316)
@@ -8,5 +8,13 @@
 use PS::IPP::Metadata::Stats;
 use Data::Dumper;
-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;
@@ -51,5 +59,8 @@
 my $p2tool = can_run('p2tool') or (warn "Can't find p2tool" 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); 
+}
 
 unless (defined $workdir) {
@@ -79,9 +90,12 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform ppImage on $input: $error_code\n" if not $success;
-    die "Couldn't find expected output file: $outputImage\n" if not -f $outputImage;
-    die "Couldn't find expected output file: $outputBin1\n" if not -f $outputBin1;
-    die "Couldn't find expected output file: $outputBin2\n" if not -f $outputBin2;
-    die "Couldn't find expected output file: $outputStats\n" if not -f $outputStats;
+    unless ($success) {
+	$error_code >> 8;
+	&my_die("Unable to perform ppImage: $error_code", $exp_tag, $class_id, $error_code);
+    }
+    &my_die("Couldn't find expected output file: $outputImage\n", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputImage;
+    &my_die("Couldn't find expected output file: $outputBin1\n", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin1;
+    &my_die("Couldn't find expected output file: $outputBin2\n", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin2;
+    &my_die("Couldn't find expected output file: $outputStats\n", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR) unless -f $outputStats;
 }
 
@@ -90,12 +104,13 @@
 {
     my $statsFile;		# File handle
-    open $statsFile, $outputStats or die "Can't open statistics file $outputStats: $!\n";
+    open $statsFile, $outputStats or &my_die("Can't open statistics file $outputStats: $!", $exp_tag, $class_id, $PS_EXIT_SYS_ERROR);
     my @contents = <$statsFile>; # Contents of file
     close $statsFile;
     my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
-    my $metadata = $mdcParser->parse(join "", @contents)
-            or die "unable to parse metadata config doc";
+    my $metadata = $mdcParser->parse(join "", @contents) or
+	&my_die("Unable to parse metadata config doc", $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
     $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
-    $stats->parse($metadata) or die "Unable to find all values in statistics output.\n";
+    $stats->parse($metadata) or
+	&my_die("Unable to find all values in statistics output.\n", $exp_tag, $class_id, $PS_EXIT_PROG_ERROR);
 }
 
@@ -120,8 +135,28 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform p2tool -addprocessedimfile for $expTag/$classId: $error_code\n"
-	if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform p2tool -addprocessedimfile: $error_code\n");
+	exit($error_code);
+    }
 
     unlink $outputStats;
+}
+
+
+sub my_die
+{
+    my $msg = shift; # Warning message on die
+    my $exp_tag = shift; # Exposure tag
+    my $class_id = shift; # Class identifier
+    my $exit_code = shift; # Exit code to add
+
+    warn($msg);
+    if ($exp_tag and $class_id) {
+	my $command = "$dettool -addprocessedimfile -exp_tag $exp_tag -class_id $class_id -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
 }
 
Index: /trunk/ippScripts/scripts/phase3.pl
===================================================================
--- /trunk/ippScripts/scripts/phase3.pl	(revision 11315)
+++ /trunk/ippScripts/scripts/phase3.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;
@@ -45,5 +53,8 @@
 my $p3tool = can_run('p3tool') or (warn "Can't find p3tool" 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
@@ -56,8 +67,12 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform p3tool -pendingimfile: $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 p3tool: $error_code", $exp_tag, $error_code);
+    }
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata config doc", $exp_tag, $PS_EXIT_PROG_ERROR);
+    $files = parse_md_list($metadata) or
+	&my_die("Unable to parse metadata list", $exp_tag, $PS_EXIT_PROG_ERROR);
 }
 
@@ -74,11 +89,11 @@
 #    my @zp;			# Array of photometric zero points
     foreach my $file (@$files) {
-	die "Unable to find class id\n" unless defined $file->{class_id};
+	&my_die("Unable to find class id", $exp_tag, $PS_EXIT_SYS_ERROR) 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};
-#	die "Unable to find sigma_ra for class_id=$class_id\n" unless defined $file->{sigma_ra};
-#	die "Unable to find sigma_dec for class_id=$class_id\n" unless defined $file->{sigma_dec};
-#	die "Unable to find zp for class_id=$class_id\n" unless defined $file->{zp};
+	&my_die("Unable to find bg for class_id=$class_id", $exp_tag, $PS_EXIT_SYS_ERROR) unless defined $file->{bg};
+	&my_die("Unable to find bg_mean_stdev for class_id=$class_id", $exp_tag, $PS_EXIT_SYS_ERROR) unless defined $file->{bg_mean_stdev};
+#	&my_die("Unable to find sigma_ra for class_id=$class_id", $exp_tag, $PS_EXIT_SYS_ERROR) unless defined $file->{sigma_ra};
+#	&my_die("Unable to find sigma_dec for class_id=$class_id", $exp_tag, $PS_EXIT_SYS_ERROR) unless defined $file->{sigma_dec};
+#	&my_die("Unable to find zp for class_id=$class_id", $exp_tag, $PS_EXIT_SYS_ERROR) unless defined $file->{zp};
 	push @backgrounds, $file->{bg};
 	push @variances, $file->{bg_stdev}**2;
@@ -132,5 +147,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", $exp_tag, $error_code);
+    }
+    &my_die("Unable to find expected output file: $jpeg1", $exp_tag, $PS_EXIT_PROG_ERROR) unless -f $jpeg1;
 }
 
@@ -140,5 +159,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", $exp_tag, $error_code);
+    }
+    &my_die("Unable to find expected output file: $jpeg2", $exp_tag, $PS_EXIT_PROG_ERROR) unless -f $jpeg2;
 }
 
@@ -156,9 +179,30 @@
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
-    die "Unable to perform p3tool -addprocessedexp: $error_code\n" if not $success;
+    unless ($success) {
+	$error_code >> 8;
+	warn("Unable to perform dettool -addprocessedexp: $error_code\n");
+	exit($error_code);
+    }
 
     unlink $list1Name;
     unlink $list2Name;
 }
+
+
+sub my_die
+{
+    my $msg = shift; # Warning message on die
+    my $exp_tag = shift; # Exposure tag
+    my $exit_code = shift; # Exit code to add
+
+    warn($msg);
+    if ($exp_tag) {
+	my $command = "$dettool -addprocessedexp -exp_tag $exp_tag -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
+}
+
 
 END {
