Index: trunk/ippScripts/scripts/detrend_norm_calc.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_norm_calc.pl	(revision 11830)
+++ trunk/ippScripts/scripts/detrend_norm_calc.pl	(revision 11831)
@@ -77,8 +77,6 @@
 }
 
-unless (exists NORMALIZE()->{lc($detType)}) {
-    warn("Unrecognised detrend type: $detType");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
+&my_die("Unrecognised detrend type: $detType", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless
+    exists NORMALIZE()->{lc($detType)};
 
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
@@ -93,6 +91,6 @@
     print "Running [$command]...\n";
     if (not run(\@command, \$stdin, \$stdout, \$stderr)) {
-	warn("Unable to perform dettool -processedimfile on detrend $detId/$iter: $?");
-	exit($PS_EXIT_SYS_ERROR);
+	&my_die("Unable to perform dettool -processedimfile on detrend $detId/$iter: $?",
+		$det_id, $iter, $PS_EXIT_SYS_ERROR);
     }
 #    print $stdout . "\n";
@@ -108,8 +106,6 @@
 
 	    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);
-	    }
+	    &my_die("Unable to parse output from dettool", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless
+		defined $list;
 	    push @files, @$list;
 	    @single = ();
@@ -148,6 +144,5 @@
 	print "Running [$ppNormCalc]...\n";
 	if (not run(\@command, \$normData, \$stdout, \$stderr)) {
-	    warn("Unable to perform ppNormCalc: $?");
-	    exit($PS_EXIT_SYS_ERROR);
+	    &my_die("Unable to perform ppNormCalc: $?", $det_id, $iter, $PS_EXIT_SYS_ERROR);
 	}
 	print $stdout . "\n";
@@ -155,8 +150,5 @@
 	# Parse the output
 	$norms = $mdcParser->parse($stdout);
-	unless ($norms) {
-	    warn("Unable to parse metadata config doc");
-	    exit($PS_EXIT_PROG_ERROR);
-	}
+	&my_die("Unable to parse metadata config doc", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless $norms;
     }
 
@@ -199,4 +191,21 @@
 }
 
+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 and $class_id and not $no_update) {
+	my $command = "$dettool -addnormalizedstat -det_id $det_id -iteration $iter -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
+}
+
+
 END {
     my $status = $?;
