Index: trunk/ippScripts/scripts/camera_exp.pl
===================================================================
--- trunk/ippScripts/scripts/camera_exp.pl	(revision 22429)
+++ trunk/ippScripts/scripts/camera_exp.pl	(revision 22430)
@@ -27,4 +27,17 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
+my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
+my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+my $psastro = can_run('psastro') or (warn "Can't find psastro" and $missing_tools = 1);
+my $addstar = can_run('addstar') or (warn "Can't find addstar" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my ( $exp_tag, $cam_id, $camera, $outroot, $recipe, $dbname, $reduction, $dvodb, $verbose, $no_update,
@@ -57,4 +70,8 @@
     defined $camera;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $cam_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->define_camera($camera);
 
@@ -84,20 +101,4 @@
 my $recipe_addstar = $ipprc->reduction($reduction, 'ADDSTAR'); # Recipe to use
 &my_die("Unrecognised ADDSTAR recipe", $cam_id, $PS_EXIT_CONFIG_ERROR) unless defined $recipe1;
-
-# Look for programs we need
-my $missing_tools;
-my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
-my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
-my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-
-# test for addstar and psastro:
-my $psastro = can_run('psastro') or (warn "Can't find psastro" and $missing_tools = 1);
-my $addstar = can_run('addstar') or (warn "Can't find addstar" and $missing_tools = 1);
-
-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
@@ -272,19 +273,19 @@
 
         if ($do_stats) {
-	    my $fpaStatsReal = $ipprc->file_resolve($fpaStats);
-	    &my_die("Couldn't find expected output file: $fpaStats", $cam_id, $PS_EXIT_SYS_ERROR) unless -f $fpaStatsReal;
-
-	    # parse stats from metadata
-	    $command = "$ppStatsFromMetadata $fpaStatsReal - CAMERA_EXP_FPA";
-	    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-		run(command => $command, verbose => $verbose);
-	    unless ($success) {
-		$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-		&my_die("Unable to perform ppStatsFromMetadata: $error_code", $cam_id, $error_code);
-	    }
-	    foreach my $line (@$stdout_buf) {
-		$cmdflags .= " $line";
-	    }
-	    chomp $cmdflags;
+            my $fpaStatsReal = $ipprc->file_resolve($fpaStats);
+            &my_die("Couldn't find expected output file: $fpaStats", $cam_id, $PS_EXIT_SYS_ERROR) unless -f $fpaStatsReal;
+
+            # parse stats from metadata
+            $command = "$ppStatsFromMetadata $fpaStatsReal - CAMERA_EXP_FPA";
+            ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                run(command => $command, verbose => $verbose);
+            unless ($success) {
+                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+                &my_die("Unable to perform ppStatsFromMetadata: $error_code", $cam_id, $error_code);
+            }
+            foreach my $line (@$stdout_buf) {
+                $cmdflags .= " $line";
+            }
+            chomp $cmdflags;
         }
 
@@ -323,5 +324,5 @@
             $command .= " $realFile";
 
-	    my $mjd_addstar_start = DateTime->now->mjd;   # MJD of starting script
+            my $mjd_addstar_start = DateTime->now->mjd;   # MJD of starting script
 
             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -331,5 +332,5 @@
                 &my_die("Unable to perform addstar: $error_code", $cam_id, $error_code);
             }
-	    $dtime_addstar = 86400.0*(DateTime->now->mjd - $mjd_addstar_start);   # MJD of starting script
+            $dtime_addstar = 86400.0*(DateTime->now->mjd - $mjd_addstar_start);   # MJD of starting script
         }
     }
Index: trunk/ippScripts/scripts/chip_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/chip_imfile.pl	(revision 22429)
+++ trunk/ippScripts/scripts/chip_imfile.pl	(revision 22430)
@@ -25,4 +25,15 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $chiptool = can_run('chiptool') or (warn "Can't find chiptool" and $missing_tools = 1);
+my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
+my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 # Parse the command-line arguments
@@ -53,9 +64,13 @@
     defined $chip_id and
     defined $class_id and
-    defined $chip_imfile_id and 
+    defined $chip_imfile_id and
     defined $uri and
     defined $camera and
     defined $outroot and
     defined $run_state;
+
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $exp_id, $chip_id, $class_id, $PS_EXIT_UNKNOWN_ERROR ); };
 
 $ipprc->define_camera($camera);
@@ -69,15 +84,4 @@
     print STDOUT "Starting script $0 on $host\n\n";
     print STDOUT "COMMAND IS: @ARGV\n\n";
-}
-
-# Look for programs we need
-my $missing_tools;
-my $chiptool = can_run('chiptool') or (warn "Can't find chiptool" and $missing_tools = 1);
-my $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
-my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
 }
 
@@ -132,6 +136,6 @@
         $command .= " -threads $threads" if defined $threads;
         $command .= " -dbname $dbname" if defined $dbname;
-	$command .= " -image_id $chip_imfile_id" if defined $chip_imfile_id;
-	$command .= " -source_id $source_id" if defined $source_id;
+        $command .= " -image_id $chip_imfile_id" if defined $chip_imfile_id;
+        $command .= " -source_id $source_id" if defined $source_id;
         $command .= " -dumpconfig $configuration";
         $command .= " -tracedest $traceDest -log $logDest";
@@ -142,6 +146,6 @@
         $command .= " -threads $threads" if defined $threads;
         $command .= " -dbname $dbname" if defined $dbname;
-	$command .= " -image_id $chip_imfile_id" if defined $chip_imfile_id;
-	$command .= " -source_id $source_id" if defined $source_id;
+        $command .= " -image_id $chip_imfile_id" if defined $chip_imfile_id;
+        $command .= " -source_id $source_id" if defined $source_id;
         $command .= " -tracedest $traceDest -log $logDest";
         $command .= " -Db PPIMAGE:PHOTOM FALSE";
Index: trunk/ippScripts/scripts/detrend_correct_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_correct_imfile.pl	(revision 22429)
+++ trunk/ippScripts/scripts/detrend_correct_imfile.pl	(revision 22430)
@@ -24,4 +24,13 @@
 use Pod::Usage qw( pod2usage );
 
+# Look for programs we need
+my $missing_tools;
+my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
+my $dvoApplyCorr = can_run('dvoApplyCorr') or (warn "Can't find dvoApplyCorr" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
 my ( $det_id, $class_id, $det_type, $input_uri, $camera, $dbname,
      $verbose, $no_update, $no_op, $outroot, $redirect, $corr_uri );
@@ -43,5 +52,5 @@
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage( -msg => "Required options: --det_id --class_id --det_type --input_uri --camera --outroot",
-	   -exitval => 3)
+           -exitval => 3)
     unless defined $det_id
     and defined $class_id
@@ -50,4 +59,8 @@
     and defined $outroot
     and defined $camera;
+
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $det_id, $class_id, $PS_EXIT_UNKNOWN_ERROR ); };
 
 # XXX this exits with status = 0 on failure
@@ -60,6 +73,6 @@
 }
 
-# Recipes to use as a function of detrend type 
-# XXX there are no recipe options for dvoApplyCorr... 
+# Recipes to use as a function of detrend type
+# XXX there are no recipe options for dvoApplyCorr...
 # XXX in the future, we may define corrections other than the flat-field correction
 # $reduction = "DETREND" unless defined $reduction;
@@ -69,13 +82,4 @@
 # det_type is type of the CORRECTED (output) file
 if ($det_type ne "FLAT") { warn ("unexpected input detrend type: correcting data of type $det_type"); }
-
-# Look for programs we need
-my $missing_tools;
-my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
-my $dvoApplyCorr = can_run('dvoApplyCorr') or (warn "Can't find dvoApplyCorr" and $missing_tools = 1);
-if ($missing_tools) { 
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR); 
-}
 
 &my_die("Couldn't find input file: $input_uri\n", $det_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input_uri);
@@ -91,8 +95,8 @@
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => $verbose);
+        run(command => $command, verbose => $verbose);
     unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform ppImage: $error_code", $det_id, $class_id, $error_code);
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform ppImage: $error_code", $det_id, $class_id, $error_code);
     }
 
@@ -111,9 +115,9 @@
 unless ($no_update) {
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => $verbose);
+        run(command => $command, verbose => $verbose);
     unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	warn("Unable to perform dettool -addcorrectimfile: $error_code\n");
-	exit($error_code);
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        warn("Unable to perform dettool -addcorrectimfile: $error_code\n");
+        exit($error_code);
     }
 } else {
@@ -124,5 +128,5 @@
 {
     my $msg = shift; # Warning message on die
-    my $det_id = shift;		# Detrend identifier
+    my $det_id = shift;         # Detrend identifier
     my $class_id = shift; # Class identifier
     my $exit_code = shift; # Exit code to add
@@ -130,10 +134,10 @@
     carp($msg);
     if (defined $det_id and defined $class_id and not $no_update) {
-	my $command = "$dettool -addcorrectimfile";
-	$command .= " -det_id $det_id";
-	$command .= " -class_id $class_id";
-	$command .= " -path_base $outroot";
-	$command .= " -code $exit_code";
-	$command .= " -dbname $dbname" if defined $dbname;
+        my $command = "$dettool -addcorrectimfile";
+        $command .= " -det_id $det_id";
+        $command .= " -class_id $class_id";
+        $command .= " -path_base $outroot";
+        $command .= " -code $exit_code";
+        $command .= " -dbname $dbname" if defined $dbname;
         system ($command);
     }
Index: trunk/ippScripts/scripts/detrend_norm_apply.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_norm_apply.pl	(revision 22429)
+++ trunk/ippScripts/scripts/detrend_norm_apply.pl	(revision 22430)
@@ -22,4 +22,14 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+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);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 # Parse the command-line
@@ -54,7 +64,11 @@
     defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $det_id, $iter, $class_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->define_camera($camera);
 
-my $logDest = $ipprc->filename("LOG.IMFILE", $outroot, $class_id) 
+my $logDest = $ipprc->filename("LOG.IMFILE", $outroot, $class_id)
         or &my_die("Missing entry from camera config", $det_id, $iter, $class_id, $PS_EXIT_CONFIG_ERROR);
 $ipprc->redirect_output($logDest) if $redirect;
@@ -64,8 +78,8 @@
 # Define which detrend types we normalise
 use constant DETTYPE => {
-    'bias'     	       => 'bias',
-    'dark'     	       => 'dark',   
+    'bias'             => 'bias',
+    'dark'             => 'dark',
     'dark_premask'     => 'dark',
-    'shutter'  	       => 'shutter',
+    'shutter'          => 'shutter',
     'flat_premask'     => 'flat',
     'domeflat_premask' => 'flat',
@@ -73,27 +87,17 @@
     'flat_raw'         => 'flat',
     'domeflat_raw'     => 'flat',
-    'skyflat_raw'      => 'flat',   
-    'flat'     	       => 'flat',
-    'domeflat' 	       => 'flat',
-    'skyflat'  	       => 'flat',   
-    'fringe'   	       => 'fringe',   
-    'mask'     	       => 'mask',
-    'darkmask' 	       => 'mask',
-    'flatmask' 	       => 'mask',
-    }; 
+    'skyflat_raw'      => 'flat',
+    'flat'             => 'flat',
+    'domeflat'         => 'flat',
+    'skyflat'          => 'flat',
+    'fringe'           => 'fringe',
+    'mask'             => 'mask',
+    'darkmask'         => 'mask',
+    'flatmask'         => 'mask',
+    };
 
 # convert supplied detrend type to a controlled namespace
 &my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $class_id, $PS_EXIT_PROG_ERROR) unless exists DETTYPE()->{lc($det_type)};
 my $det_type_real = DETTYPE()->{lc($det_type)};
-
-# Look for programs we need
-my $missing_tools;
-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);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 &my_die("Couldn't find input file: $input_uri\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input_uri);
@@ -124,8 +128,8 @@
     # we cannot use ppImage to load a normalized mask : just copy it and build the jpeg images
     if ($det_type_real eq 'mask') {
-	$RECIPE_PPIMAGE = 'PPIMAGE_BIN';
-	my $input_real = $ipprc->file_resolve($input_uri, 0);
-	my $output_real = $ipprc->file_resolve($output, 1);
-	system ("cp $input_real $output_real");
+        $RECIPE_PPIMAGE = 'PPIMAGE_BIN';
+        my $input_real = $ipprc->file_resolve($input_uri, 0);
+        my $output_real = $ipprc->file_resolve($output, 1);
+        system ("cp $input_real $output_real");
     }
 
@@ -162,5 +166,5 @@
     }
     foreach my $line (@$stdout_buf) {
-	$cmdflags .= " $line";
+        $cmdflags .= " $line";
     }
     chomp $cmdflags;
@@ -204,5 +208,5 @@
         $command .= " -iteration $iter";
         $command .= " -class_id $class_id";
-	$command .= " -path_base $outroot";
+        $command .= " -path_base $outroot";
         $command .= " -code $exit_code";
         $command .= " -dbname $dbname" if defined $dbname;
Index: trunk/ippScripts/scripts/detrend_norm_calc.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_norm_calc.pl	(revision 22429)
+++ trunk/ippScripts/scripts/detrend_norm_calc.pl	(revision 22430)
@@ -25,15 +25,24 @@
 use Pod::Usage qw( pod2usage );
 
+# Look for programs we need
+my $missing_tools;
+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);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
 # Parse command-line arguments
 my ($det_id, $iter, $detType, $outroot, $dbname, $verbose, $no_update, $no_op, $redirect );
 GetOptions(
-    'det_id|d=s'	=> \$det_id,    # Detrend id			     
-    'iteration|i=s'	=> \$iter,	# Iteration			     
-    'det_type|t=s'  	=> \$detType,	# Detrend type			     
-    'outroot|w=s'   	=> \$outroot,   # output file base name
-    'dbname|d=s'    	=> \$dbname,	# Database name			     
-    'verbose'       	=> \$verbose,   # Print to stdout
-    'no-update'     	=> \$no_update,	# Don't update the database?	     
-    'no-op'         	=> \$no_op,	# Don't do operations                
+    'det_id|d=s'        => \$det_id,    # Detrend id
+    'iteration|i=s'     => \$iter,      # Iteration
+    'det_type|t=s'      => \$detType,   # Detrend type
+    'outroot|w=s'       => \$outroot,   # output file base name
+    'dbname|d=s'        => \$dbname,    # Database name
+    'verbose'           => \$verbose,   # Print to stdout
+    'no-update'         => \$no_update, # Don't update the database?
+    'no-op'             => \$no_op,     # Don't do operations
     'redirect-output'   => \$redirect,
     ) or pod2usage( 2 );
@@ -41,11 +50,15 @@
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage( -msg => "Required options --det_id --iteration --det_type --outroot",
-	   -exitval => 3,
-	   ) unless 
-    defined $det_id  and 
-    defined $iter    and 
+           -exitval => 3,
+           ) unless
+    defined $det_id  and
+    defined $iter    and
     defined $detType and
     defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $det_id, $iter, $PS_EXIT_UNKNOWN_ERROR ); };
+
 my $logfile = $outroot . ".log";
 
@@ -56,8 +69,8 @@
 # Define which detrend types we normalise
 use constant NORMALIZE => {
-    'bias'     	       => 0,
-    'dark'     	       => 0,   
+    'bias'             => 0,
+    'dark'             => 0,
     'dark_premask'     => 0,
-    'shutter'  	       => 0,
+    'shutter'          => 0,
     'flat_premask'     => 1,
     'domeflat_premask' => 1,
@@ -66,21 +79,12 @@
     'domeflat_raw'     => 1,
     'skyflat_raw'      => 1,
-    'flat'     	       => 1,
-    'domeflat' 	       => 1,
-    'skyflat'  	       => 1,   
-    'fringe'   	       => 0,   
-    'mask'     	       => 0,
-    'darkmask' 	       => 0,
-    'flatmask' 	       => 0,
-    }; 
-
-# Look for programs we need
-my $missing_tools;
-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);
-if ($missing_tools) { 
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR); 
-}
+    'flat'             => 1,
+    'domeflat'         => 1,
+    'skyflat'          => 1,
+    'fringe'           => 0,
+    'mask'             => 0,
+    'darkmask'         => 0,
+    'flatmask'         => 0,
+    };
 
 &my_die("Unrecognised detrend type: $detType", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless exists NORMALIZE()->{lc($detType)};
@@ -89,5 +93,5 @@
 
 # Get the list of inputs
-my @files;			# The input files
+my @files;                      # The input files
 {
     my $command = "$dettool -processedimfile";
@@ -99,79 +103,79 @@
     print "Running [$command]...\n" if $verbose;
     if (not run(\@command, \$stdin, \$stdout, \$stderr)) {
-	&my_die("Unable to perform dettool -processedimfile on detrend $det_id/$iter: $?",
-		$det_id, $iter, $PS_EXIT_SYS_ERROR);
+        &my_die("Unable to perform dettool -processedimfile on detrend $det_id/$iter: $?",
+                $det_id, $iter, $PS_EXIT_SYS_ERROR);
     }
     print $stdout . "\n" if $verbose;
-    
+
     # Because of the length, need to split into individual metadatas --- it parses SO much quicker!
     my @whole = split /\n/, $stdout;
     my @single = ();
     while ( scalar @whole > 0 ) {
-	my $value = shift @whole;
-	push @single, $value;
-	if ($value =~ /^\s*END\s*$/) {
-	    push @single, "\n";
-
-	    my $list = parse_md_list( $mdcParser->parse( join( "\n", @single ) ) );
-	    &my_die("Unable to parse output from dettool", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless
-		defined $list;
-	    push @files, @$list;
-	    @single = ();
-	}
-    }
-}
-
-my $norms;			# MDC with normalisations
+        my $value = shift @whole;
+        push @single, $value;
+        if ($value =~ /^\s*END\s*$/) {
+            push @single, "\n";
+
+            my $list = parse_md_list( $mdcParser->parse( join( "\n", @single ) ) );
+            &my_die("Unable to parse output from dettool", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless
+                defined $list;
+            push @files, @$list;
+            @single = ();
+        }
+    }
+}
+
+my $norms;                      # MDC with normalisations
 if (NORMALIZE()->{lc($detType)} and not $no_op) {
 
     my %matrix; # Matrix of statistics as a function of exposures and classes
     foreach my $file (@files) {
-	my $exp_id = $file->{'exp_id'}; # Exposure ID
-	my $class_id = $file->{'class_id'}; # Class ID
-	my $stat = $file->{STATISTIC()}; # Statistic of interest
-	
-	# Create matrix elements
-	$matrix{$exp_id} = {} if not defined $matrix{$exp_id};
-	$matrix{$exp_id}->{$class_id} = $stat;
-    }
-    
+        my $exp_id = $file->{'exp_id'}; # Exposure ID
+        my $class_id = $file->{'class_id'}; # Class ID
+        my $stat = $file->{STATISTIC()}; # Statistic of interest
+
+        # Create matrix elements
+        $matrix{$exp_id} = {} if not defined $matrix{$exp_id};
+        $matrix{$exp_id}->{$class_id} = $stat;
+    }
+
     # Generate the input for ppNormCalc
-    my $normData;			# Normalisation data
+    my $normData;                       # Normalisation data
     foreach my $exp_id (keys %matrix) {
-	$normData .= "$exp_id\tMETADATA\n";
-	foreach my $class_id (keys %{$matrix{$exp_id}}) {
-	    $normData .= "\t" . $class_id . "\tF32\t" . $matrix{$exp_id}->{$class_id} . "\n";
-	}
-	$normData .= "END\n\n";
+        $normData .= "$exp_id\tMETADATA\n";
+        foreach my $class_id (keys %{$matrix{$exp_id}}) {
+            $normData .= "\t" . $class_id . "\tF32\t" . $matrix{$exp_id}->{$class_id} . "\n";
+        }
+        $normData .= "END\n\n";
     }
 
     # Run ppNormCalc
     {
-	my ( $stdout, $stderr ); # Buffers for running program
-	my @command = split /\s+/, $ppNormCalc;
-	print "Running [$ppNormCalc]...\n" if $verbose;
-	if (not run(\@command, \$normData, \$stdout, \$stderr)) {
-	    &my_die("Unable to perform ppNormCalc: $?", $det_id, $iter, $PS_EXIT_SYS_ERROR);
-	}
-	print $stdout . "\n" if $verbose;
-	
-	# Parse the output
-	$norms = $mdcParser->parse($stdout);
-	&my_die("Unable to parse metadata config doc", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless $norms;
+        my ( $stdout, $stderr ); # Buffers for running program
+        my @command = split /\s+/, $ppNormCalc;
+        print "Running [$ppNormCalc]...\n" if $verbose;
+        if (not run(\@command, \$normData, \$stdout, \$stderr)) {
+            &my_die("Unable to perform ppNormCalc: $?", $det_id, $iter, $PS_EXIT_SYS_ERROR);
+        }
+        print $stdout . "\n" if $verbose;
+
+        # Parse the output
+        $norms = $mdcParser->parse($stdout);
+        &my_die("Unable to parse metadata config doc", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless $norms;
     }
 
 } else {
     # It's something that doesn't need normalisation --- just push in a normalisation of 1
-    my %classes;		# List of unique classes
+    my %classes;                # List of unique classes
     foreach my $file (@files) {
-	my $class_id = $file->{'class_id'}; # Class Id
-	$classes{$class_id} = 1;
-    }
-    
+        my $class_id = $file->{'class_id'}; # Class Id
+        $classes{$class_id} = 1;
+    }
+
     foreach my $class_id (keys %classes) {
-	my %mdValue;	# Metadata value for this class id
-	$mdValue{name} = $class_id;
-	$mdValue{value} = 1.0;
-	push @$norms, \%mdValue;
+        my %mdValue;    # Metadata value for this class id
+        $mdValue{name} = $class_id;
+        $mdValue{value} = 1.0;
+        push @$norms, \%mdValue;
     }
 }
@@ -186,25 +190,25 @@
     foreach my $normItem (@$norms) {
 
-	my $className = $normItem->{name}; # Name of component
-	my $normalisation = $normItem->{value}; # Normalisation for component
-
-	if ($normalisation == 0.0 or lc($normalisation) eq 'nan') {
-	    warn("Class $className has bad normalisation: $normalisation");
-	    exit($PS_EXIT_SYS_ERROR);
-	}
-
-	my $command = $commandBase;
-	$command .= " -class_id $className";
-	$command .= " -norm $normalisation";
-
-	my @command = split /\s+/, $command;
-
-	my ( $stdin, $stdout, $stderr ); # Buffers for running program
-	print "Running [$command]...\n" if $verbose;
-	if (not run \@command, \$stdin, \$stdout, \$stderr) {
-	    warn("Unable to perform dettool -addnormstat for $className: $?");
-	    exit($PS_EXIT_SYS_ERROR);
-	}
-	print $stdout . "\n" if $verbose;
+        my $className = $normItem->{name}; # Name of component
+        my $normalisation = $normItem->{value}; # Normalisation for component
+
+        if ($normalisation == 0.0 or lc($normalisation) eq 'nan') {
+            warn("Class $className has bad normalisation: $normalisation");
+            exit($PS_EXIT_SYS_ERROR);
+        }
+
+        my $command = $commandBase;
+        $command .= " -class_id $className";
+        $command .= " -norm $normalisation";
+
+        my @command = split /\s+/, $command;
+
+        my ( $stdin, $stdout, $stderr ); # Buffers for running program
+        print "Running [$command]...\n" if $verbose;
+        if (not run \@command, \$stdin, \$stdout, \$stderr) {
+            warn("Unable to perform dettool -addnormstat for $className: $?");
+            exit($PS_EXIT_SYS_ERROR);
+        }
+        print $stdout . "\n" if $verbose;
     }
 } else {
@@ -212,12 +216,12 @@
     foreach my $normItem (@$norms) {
 
-	my $className = $normItem->{name}; # Name of component
-	my $normalisation = $normItem->{value}; # Normalisation for component
-
-	if ($normalisation == 0.0 or lc($normalisation) eq 'nan') {
-	    warn("Class $className has bad normalisation: $normalisation");
-	    exit($PS_EXIT_SYS_ERROR);
-	}
-	print "$className : $normalisation\n";
+        my $className = $normItem->{name}; # Name of component
+        my $normalisation = $normItem->{value}; # Normalisation for component
+
+        if ($normalisation == 0.0 or lc($normalisation) eq 'nan') {
+            warn("Class $className has bad normalisation: $normalisation");
+            exit($PS_EXIT_SYS_ERROR);
+        }
+        print "$className : $normalisation\n";
     }
 }
@@ -225,17 +229,17 @@
 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
+    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
 
     carp($msg);
     if (defined $det_id and defined $iter and not $no_update) {
-	my $command = "$dettool -addnormalizedstat";
-	$command .= " -det_id $det_id";
-	$command .= " -iteration $iter";
-	# XXX EAM : we should add this to the db : $command .= " -path_base $outroot";
-	$command .= " -code $exit_code";
-	$command .= " -dbname $dbname" if defined $dbname;
+        my $command = "$dettool -addnormalizedstat";
+        $command .= " -det_id $det_id";
+        $command .= " -iteration $iter";
+        # XXX EAM : we should add this to the db : $command .= " -path_base $outroot";
+        $command .= " -code $exit_code";
+        $command .= " -dbname $dbname" if defined $dbname;
         system ($command);
     }
Index: trunk/ippScripts/scripts/detrend_norm_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_norm_exp.pl	(revision 22429)
+++ trunk/ippScripts/scripts/detrend_norm_exp.pl	(revision 22430)
@@ -24,4 +24,14 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+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);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my ($det_id, $iter, $det_type, $camera, $outroot, $dbname, $reduction, $verbose, $no_update, $no_op, $redirect, $save_temps);
@@ -50,4 +60,8 @@
     defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $det_id, $iter, $PS_EXIT_UNKNOWN_ERROR ); };
+
 my $logfile = $outroot . ".log";
 
@@ -60,14 +74,4 @@
 my $recipe = $ipprc->reduction($reduction, uc($det_type) . '_JPEG_IMAGE');
 &my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless 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 $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 # Get list of component files
Index: trunk/ippScripts/scripts/detrend_process_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 22429)
+++ trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 22430)
@@ -24,4 +24,14 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+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);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my ( $det_id, $exp_id, $det_type, $exp_tag, $camera, $outroot, $dbname, $reduction, $verbose, $no_update,
@@ -53,4 +63,8 @@
     defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $det_id, $exp_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->define_camera($camera);
 
@@ -66,14 +80,4 @@
 my $recipe = $ipprc->reduction($reduction, uc($det_type) . '_JPEG_IMAGE'); # Recipe to use
 &my_die("Unrecognised detrend type: $det_type", $det_id, $exp_id, $PS_EXIT_PROG_ERROR) unless 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 $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 my $cmdflags;
Index: trunk/ippScripts/scripts/detrend_process_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 22429)
+++ trunk/ippScripts/scripts/detrend_process_imfile.pl	(revision 22430)
@@ -22,4 +22,14 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+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);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my ( $det_id, $exp_id, $class_id, $det_type, $exp_tag, $input_uri, $camera, $outroot, $dbname, $reduction,
@@ -55,4 +65,8 @@
     defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $det_id, $exp_id, $class_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 # XXX this exits with status = 0 on failure
 $ipprc->define_camera($camera);
@@ -66,14 +80,4 @@
 my $ppimage_recipe = $ipprc->reduction($reduction, uc($det_type) . '_PROCESS'); # Recipe name for ppImage
 my $jpeg_recipe = $ipprc->reduction($reduction, uc($det_type) . '_JPEG_IMAGE'); # Recipe name for JPEG
-
-# Look for programs we need
-my $missing_tools;
-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);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 &my_die("Couldn't find input file: $input_uri\n", $det_id, $exp_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input_uri);
@@ -128,5 +132,5 @@
     }
     foreach my $line (@$stdout_buf) {
-	$cmdflags .= " $line";
+        $cmdflags .= " $line";
     }
     chomp $cmdflags;
Index: trunk/ippScripts/scripts/detrend_reject_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 22429)
+++ trunk/ippScripts/scripts/detrend_reject_exp.pl	(revision 22430)
@@ -27,4 +27,12 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my ($det_id, $iter, $det_type, $camera, $outroot, $filter, $dbname, $verbose, $no_update, $no_op, $redirect);
@@ -52,4 +60,8 @@
     defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $det_id, $iter, $PS_EXIT_UNKNOWN_ERROR ); };
+
 # check for existing directory, generate if needed
 $ipprc->outroot_prepare($outroot);
@@ -89,12 +101,4 @@
 my $rejstats = PS::IPP::Metadata::Stats->new($REJSTATS); # Stats parser
 
-# Look for programs we need
-my $missing_tools;
-my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
-
 # Get list of component files
 my ($exposures, $command, $success, $error_code, $full_buf, $stdout_buf, $stderr_buf);
@@ -345,5 +349,5 @@
         $command .= " -det_id $det_id";
         $command .= " -iteration $iter";
-	# XXX EAM : we should add this to the db : $command .= " -path_base $outroot";
+        # XXX EAM : we should add this to the db : $command .= " -path_base $outroot";
         $command .= " -code $exit_code";
         $command .= " -dbname $dbname" if defined $dbname;
Index: trunk/ippScripts/scripts/detrend_resid_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_resid_exp.pl	(revision 22429)
+++ trunk/ippScripts/scripts/detrend_resid_exp.pl	(revision 22430)
@@ -35,4 +35,14 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt ); # option parsing
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+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);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 # parse the command-line options
@@ -69,4 +79,8 @@
     defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $det_id, $iter, $exp_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 # load IPP config information for the specified camera
 $ipprc->define_camera($camera);
@@ -81,14 +95,4 @@
 my $recipe = $ipprc->reduction($reduction, uc($det_type) . '_JPEG_RESID'); # Recipe to use
 &my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless 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 $ppImage = can_run('ppImage') or (warn "Can't find ppImage" and $missing_tools = 1);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 # Get list of imfile files
Index: trunk/ippScripts/scripts/detrend_resid_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_resid_imfile.pl	(revision 22429)
+++ trunk/ippScripts/scripts/detrend_resid_imfile.pl	(revision 22430)
@@ -22,4 +22,15 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+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);
+my $ppStats = can_run('ppStats') or (warn "Can't find ppStats" and $missing_tools = 1);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my ( $det_id, $iter, $ref_det_id, $ref_iter, $exp_id, $exp_tag, $class_id, $det_type, $detrend, $input_uri, $camera, $mode, $outroot,
@@ -65,4 +76,8 @@
     defined $detrend;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $det_id, $iter, $exp_id, $class_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->define_camera($camera);
 my $logDest     = $ipprc->filename("LOG.IMFILE", $outroot, $class_id);
@@ -109,15 +124,4 @@
 };
 
-# Look for programs we need
-my $missing_tools;
-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);
-my $ppStats = can_run('ppStats') or (warn "Can't find ppStats" and $missing_tools = 1);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
-
 # outroot examples (HOST components must be set)
 # file://data/ipp004.0/gpc1/20080130
@@ -179,5 +183,5 @@
     }
     foreach my $line (@$stdout_buf) {
-	$cmdflags .= " $line";
+        $cmdflags .= " $line";
     }
     chomp $cmdflags;
@@ -192,5 +196,5 @@
     }
     foreach my $line (@$stdout_buf) {
-	$cmdflags .= " $line";
+        $cmdflags .= " $line";
     }
     chomp $cmdflags;
Index: trunk/ippScripts/scripts/detrend_stack.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_stack.pl	(revision 22429)
+++ trunk/ippScripts/scripts/detrend_stack.pl	(revision 22430)
@@ -23,4 +23,14 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# 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);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my ( $det_id, $iter, $class_id, $det_type, $camera, $outroot, $dbname, $reduction, $threads, $verbose, $save_temps,
@@ -59,4 +69,8 @@
     or &my_die("Missing entry in file rules", $det_id, $iter, $class_id, $PS_EXIT_CONFIG_ERROR);
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $det_id, $iter, $class_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 # optionally redirect the outputs from this script to LOG.IMFILE
 $ipprc->redirect_output($logDest) if $redirect;
@@ -65,14 +79,4 @@
 $reduction = "DETREND" unless defined $reduction;
 my $recipe = $ipprc->reduction($reduction, $det_type . '_STACK'); # Recipe name to use
-
-# 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);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 # The output file rule name depends on the detrend type
@@ -219,5 +223,5 @@
     }
     foreach my $line (@$stdout_buf) {
-	$cmdflags .= " $line";
+        $cmdflags .= " $line";
     }
     chomp $cmdflags;
@@ -261,5 +265,5 @@
         $command .= " -iteration $iter";
         $command .= " -class_id $class_id";
-	# XXX EAM : we should add this to the db : $command .= " -path_base $outroot";
+        # XXX EAM : we should add this to the db : $command .= " -path_base $outroot";
         $command .= " -code $exit_code";
         $command .= " -dbname $dbname" if defined $dbname;
Index: trunk/ippScripts/scripts/diff_skycell.pl
===================================================================
--- trunk/ippScripts/scripts/diff_skycell.pl	(revision 22429)
+++ trunk/ippScripts/scripts/diff_skycell.pl	(revision 22430)
@@ -27,9 +27,19 @@
 use Pod::Usage qw( pod2usage );
 
+# Look for programs we need
+my $missing_tools;
+my $difftool = can_run('difftool') or (warn "Can't find difftool" and $missing_tools = 1);
+my $ppSub = can_run('ppSub') or (warn "Can't find ppSub" and $missing_tools = 1);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
 my ($diff_id, $dbname, $threads, $outroot, $reduction, $verbose, $no_update, $no_op, $redirect);
 my ($skycell_id, $diff_skyfile_id);
 GetOptions(
     'diff_id=s'         => \$diff_id, # Diff identifier
-    'skycell_id=s'      => \$skycell_id, # Diff identifier
+    'skycell_id=s'      => \$skycell_id, # Skycell identifier
     'diff_skyfile_id=s' => \$diff_skyfile_id, # Diff identifier
     'dbname|d=s'        => \$dbname, # Database name
@@ -52,21 +62,14 @@
     and defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $diff_id, $skycell_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 # XXX camera is not known here; cannot use filerules...
 # my $logDest = $ipprc->filename("LOG.EXP", $outroot);
-
 my $logDest = "$outroot.log";
 $ipprc->redirect_output($logDest) if $redirect;
 
 my $source_id = $ipprc->source_id($dbname, $PS_TABLE_ID_DIFF);
-
-# Look for programs we need
-my $missing_tools;
-my $difftool = can_run('difftool') or (warn "Can't find difftool" and $missing_tools = 1);
-my $ppSub = can_run('ppSub') or (warn "Can't find ppSub" and $missing_tools = 1);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 # Get list of components for subtraction
@@ -80,15 +83,14 @@
     unless ($success) {
         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-        &my_die("Unable to perform difftool -inputskyfile: $error_code", $diff_id, $error_code);
+        &my_die("Unable to perform difftool -inputskyfile: $error_code", $diff_id, $skycell_id, $error_code);
     }
 
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-        &my_die("Unable to parse metadata config doc", $diff_id, $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to parse metadata config doc", $diff_id, $skycell_id, $PS_EXIT_PROG_ERROR);
     $files = parse_md_list($metadata) or
-        &my_die("Unable to parse metadata list", $diff_id, $PS_EXIT_PROG_ERROR);
-}
-
-&my_die("Subtraction list does not contain exactly two elements", $diff_id, $PS_EXIT_SYS_ERROR) unless
-    scalar @$files == 2;
+        &my_die("Unable to parse metadata list", $diff_id, $skycell_id, $PS_EXIT_PROG_ERROR);
+}
+
+&my_die("Subtraction list does not contain exactly two elements", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless scalar @$files == 2;
 
 # Identify the input and the template
@@ -123,5 +125,5 @@
     }
     if (defined $tess_id) {
-        &my_die("Tesselation identifiers don't match", $diff_id, $PS_EXIT_SYS_ERROR) unless
+        &my_die("Tesselation identifiers don't match", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless
             $file->{tess_id} eq $tess_id;
     } else {
@@ -129,5 +131,5 @@
     }
     if (defined $skycell_id) {
-        &my_die("Skycell identifiers don't match", $diff_id, $PS_EXIT_SYS_ERROR) unless
+        &my_die("Skycell identifiers don't match", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless
             $file->{skycell_id} eq $skycell_id;
     } else {
@@ -135,5 +137,5 @@
     }
     if (defined $camera) {
-        &my_die("Cameras don't match", $diff_id, $PS_EXIT_SYS_ERROR) unless $file->{camera} eq $camera;
+        &my_die("Cameras don't match", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $file->{camera} eq $camera;
     } else {
         $camera = $file->{camera};
@@ -142,7 +144,7 @@
 }
 
-&my_die("Unable to identify template", $diff_id, $PS_EXIT_SYS_ERROR) unless defined $template;
-&my_die("Unable to identify input", $diff_id, $PS_EXIT_SYS_ERROR) unless defined $input;
-&my_die("Unable to identify camera", $diff_id, $PS_EXIT_SYS_ERROR) unless defined $camera;
+&my_die("Unable to identify template", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless defined $template;
+&my_die("Unable to identify input", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless defined $input;
+&my_die("Unable to identify camera", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless defined $camera;
 $ipprc->define_camera($camera);
 
@@ -152,5 +154,5 @@
 my $recipe_psphot  = $ipprc->reduction($reduction, 'DIFF_PSPHOT'); # Recipe to use for psphot
 unless ($recipe_ppSub and $recipe_psphot) {
-    &my_die("Couldn't find selected reduction for DIFF_PPSUB and DIFF_PSPHOT: $reduction\n", $diff_id, $PS_EXIT_CONFIG_ERROR);
+    &my_die("Couldn't find selected reduction for DIFF_PPSUB and DIFF_PSPHOT: $reduction\n", $diff_id, $skycell_id, $PS_EXIT_CONFIG_ERROR);
 }
 
@@ -177,11 +179,11 @@
 print "templateSources: $templateSources\n";
 
-&my_die("Couldn't find input: $template", $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($template);
-&my_die("Couldn't find input: $templateMask", $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($templateMask);
-&my_die("Couldn't find input: $templateVariance", $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($templateVariance);
-&my_die("Couldn't find input: $input", $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input);
-&my_die("Couldn't find input: $inputMask", $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inputMask);
-&my_die("Couldn't find input: $inputVariance", $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inputVariance);
-&my_die("Couldn't find input: $templateSources", $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($templateSources);
+&my_die("Couldn't find input: $template", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($template);
+&my_die("Couldn't find input: $templateMask", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($templateMask);
+&my_die("Couldn't find input: $templateVariance", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($templateVariance);
+&my_die("Couldn't find input: $input", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input);
+&my_die("Couldn't find input: $inputMask", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inputMask);
+&my_die("Couldn't find input: $inputVariance", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($inputVariance);
+&my_die("Couldn't find input: $templateSources", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($templateSources);
 
 # Get the output filenames
@@ -223,15 +225,15 @@
     unless ($success) {
         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-        &my_die("Unable to perform ppSub: $error_code", $diff_id, $error_code);
-    }
-    &my_die("Couldn't find expected output file: $outputName", $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputName);
-    &my_die("Couldn't find expected output file: $outputMask", $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputMask);
-    &my_die("Couldn't find expected output file: $outputVariance", $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputVariance);
-#    &my_die("Couldn't find expected output file: $outputSources", $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputSources);
-#    &my_die("Couldn't find expected output file: $bin1Name",    $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($bin1Name);
-#    &my_die("Couldn't find expected output file: $bin2Name",    $diff_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($bin2Name);
+        &my_die("Unable to perform ppSub: $error_code", $diff_id, $skycell_id, $error_code);
+    }
+    &my_die("Couldn't find expected output file: $outputName", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputName);
+    &my_die("Couldn't find expected output file: $outputMask", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputMask);
+    &my_die("Couldn't find expected output file: $outputVariance", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputVariance);
+#    &my_die("Couldn't find expected output file: $outputSources", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($outputSources);
+#    &my_die("Couldn't find expected output file: $bin1Name",    $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($bin1Name);
+#    &my_die("Couldn't find expected output file: $bin2Name",    $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($bin2Name);
 
     my $outputStatsReal = $ipprc->file_resolve($outputStats);
-    &my_die("Couldn't find expected output file: $outputStats", $diff_id, $PS_EXIT_SYS_ERROR) unless -f $outputStatsReal;
+    &my_die("Couldn't find expected output file: $outputStats", $diff_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless -f $outputStatsReal;
 
     # measure chip stats
@@ -241,5 +243,5 @@
     unless ($success) {
         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-        &my_die("Unable to perform ppStatsFromMetadata: $error_code", $diff_id, $error_code);
+        &my_die("Unable to perform ppStatsFromMetadata: $error_code", $diff_id, $skycell_id, $error_code);
     }
     foreach my $line (@$stdout_buf) {
@@ -263,5 +265,5 @@
         unless ($success) {
             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-            &my_die("Unable to perform difftool -adddiffskyfile: $error_code", $diff_id, $error_code);
+            &my_die("Unable to perform difftool -adddiffskyfile: $error_code", $diff_id, $skycell_id, $error_code);
         }
     }
@@ -273,8 +275,9 @@
     my $msg = shift;            # Warning message on die
     my $diff_id = shift;        # Diff identifier
+    my $skycell_id = shift;     # Skycell identifier
     my $exit_code = shift;      # Exit code to add
 
     warn($msg);
-    if (defined $diff_id and not $no_update) {
+    if (defined $diff_id and defined $skycell_id and not $no_update) {
         my $command = "$difftool -adddiffskyfile -diff_id $diff_id -skycell_id $skycell_id -code $exit_code";
         $command .= (" -dtime_script " . ((DateTime->now->mjd - $mjd_start) * 86400));
Index: trunk/ippScripts/scripts/fake_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/fake_imfile.pl	(revision 22429)
+++ trunk/ippScripts/scripts/fake_imfile.pl	(revision 22430)
@@ -41,4 +41,14 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $faketool = can_run('faketool') or (warn "Can't find faketool" and $missing_tools = 1);
+my $ppSim = can_run('ppSim') or (warn "Can't find ppSim" and $missing_tools = 1);
+my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 # Parse the command-line arguments
@@ -72,4 +82,8 @@
     defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $exp_id, $fake_id, $class_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->define_camera($camera);
 
@@ -83,14 +97,4 @@
 unless ($recipe) {
     &my_die("Couldn't find selected reduction for FAKE: $reduction\n", $exp_id, $fake_id, $class_id, $PS_EXIT_CONFIG_ERROR);
-}
-
-# Look for programs we need
-my $missing_tools;
-my $faketool = can_run('faketool') or (warn "Can't find faketool" and $missing_tools = 1);
-my $ppSim = can_run('ppSim') or (warn "Can't find ppSim" and $missing_tools = 1);
-my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
 }
 
Index: trunk/ippScripts/scripts/magic_destreak.pl
===================================================================
--- trunk/ippScripts/scripts/magic_destreak.pl	(revision 22429)
+++ trunk/ippScripts/scripts/magic_destreak.pl	(revision 22430)
@@ -26,4 +26,13 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $magicdstool   = can_run('magicdstool') or (warn "Can't find magicdstool" and $missing_tools = 1);
+my $streaksremove = can_run('streaksremove') or (warn "Can't find streaksremove" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 # Parse the command-line arguments
@@ -66,5 +75,9 @@
     defined $component and
     defined $outroot;
-    
+
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $magic_ds_id, $component, $PS_EXIT_UNKNOWN_ERROR ); };
+
 
 my ($skycell_args, $class_id, $skycell_id);
@@ -87,13 +100,4 @@
 
 
-# Look for programs we need
-my $missing_tools;
-my $magicdstool   = can_run('magicdstool') or (warn "Can't find magicdstool" and $missing_tools = 1);
-my $streaksremove = can_run('streaksremove') or (warn "Can't find streaksremove" and $missing_tools = 1);
-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
 
@@ -128,5 +132,5 @@
     if (! -e $outroot ) {
         my $code = system "mkdir -p $outroot";
-        &my_die("cannot create output directory $outroot", $magic_ds_id, $component, 
+        &my_die("cannot create output directory $outroot", $magic_ds_id, $component,
                 $code >> 8) if $code;
     }
@@ -143,5 +147,5 @@
         if (! -e $recoveryroot ) {
             my $code = system "mkdir -p $recoveryroot";
-            &my_die("cannot create output directory $outroot", $magic_ds_id, $component, 
+            &my_die("cannot create output directory $outroot", $magic_ds_id, $component,
                     $code >> 8) if $code;
         }
@@ -151,5 +155,5 @@
 
 # get skycell list if needed
-my ($sfh, $skycell_list);          
+my ($sfh, $skycell_list);
 if ($skycell_args) {
     my $command = "$magicdstool -magic_ds_id $magic_ds_id -getskycells $skycell_args";
Index: trunk/ippScripts/scripts/magic_mask.pl
===================================================================
--- trunk/ippScripts/scripts/magic_mask.pl	(revision 22429)
+++ trunk/ippScripts/scripts/magic_mask.pl	(revision 22430)
@@ -28,4 +28,13 @@
 use Pod::Usage qw( pod2usage );
 
+# Look for programs we need
+my $missing_tools;
+my $magictool = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);
+my $streaksremove = can_run('streaksremove') or (warn "Can't find streaksremove" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
 # Parse the command-line arguments
 my ($magic_id, $camera, $dbname, $outroot, $save_temps, $verbose, $no_update, $no_op, $logfile);
@@ -49,16 +58,11 @@
     defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $magic_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->define_camera($camera);
 
 $ipprc->redirect_output($logfile) if $logfile;
-
-# Look for programs we need
-my $missing_tools;
-my $magictool = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);
-my $streaksremove = can_run('streaksremove') or (warn "Can't find streaksremove" and $missing_tools = 1);
-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
@@ -85,5 +89,5 @@
 ### Do the heavy lifting
 {
-    my $command = "$streaks --streaks"; # Command to execute
+    my $command = "$streaksremove --streaks"; # Command to execute
     my @basenames;              # List of base names
 
Index: trunk/ippScripts/scripts/magic_process.pl
===================================================================
--- trunk/ippScripts/scripts/magic_process.pl	(revision 22429)
+++ trunk/ippScripts/scripts/magic_process.pl	(revision 22430)
@@ -28,4 +28,13 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $magictool      = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);
+my $removestreaks = can_run('RemoveStreaks') or (warn "Can't find RemoveStreaks" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 # Parse the command-line arguments
@@ -53,4 +62,8 @@
     defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $magic_id, $node, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->define_camera($camera);
 
@@ -66,15 +79,6 @@
 # resolve any path:// or file:// in outroot
 $outroot = $ipprc->file_resolve($outroot);
-    
+
 $ipprc->redirect_output($logfile) if $logfile;
-
-# Look for programs we need
-my $missing_tools;
-my $magictool      = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);
-my $removestreaks = can_run('RemoveStreaks') or (warn "Can't find RemoveStreaks" and $missing_tools = 1);
-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
@@ -145,5 +149,5 @@
         # this causes major file pollution, but avoids multi-level queries
         # at higher level nodes.
-        
+
         my ($in_fh, $input_list)  = open_list_file($outroot, "input.list");
         print $in_fh "$outroot\n";
@@ -252,5 +256,5 @@
 
     my $fh;
-    open $fh, "<$resolved" or 
+    open $fh, "<$resolved" or
         &my_die("failed to open streaks file $streaks_file", $magic_id, $node, $PS_EXIT_UNKNOWN_ERROR);
     # the first line in the streaks file contains the number of streaks found
@@ -259,5 +263,5 @@
     close $fh;
     print "$num_streaks streaks found on magicRun $magic_id\n" if $verbose;
-    
+
     my $command = "$magictool -addmask";
     $command   .= " -magic_id $magic_id";
Index: trunk/ippScripts/scripts/magic_tree.pl
===================================================================
--- trunk/ippScripts/scripts/magic_tree.pl	(revision 22429)
+++ trunk/ippScripts/scripts/magic_tree.pl	(revision 22430)
@@ -31,4 +31,13 @@
 
 use constant MAX_FIELDS => 4;   # Maximum number of fields to be in a node
+
+# Look for programs we need
+my $missing_tools;
+my $magictool = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);
+my $warptool = can_run('warptool') or (warn "Can't find warptool" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 # Parse the command-line arguments
@@ -61,16 +70,11 @@
     defined $outroot;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $magic_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->define_camera($camera);
 
 $ipprc->redirect_output($logfile) if $logfile;
-
-# Look for programs we need
-my $missing_tools;
-my $magictool = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);
-my $warptool = can_run('warptool') or (warn "Can't find warptool" and $missing_tools = 1);
-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
@@ -96,5 +100,5 @@
         &my_die("Unable to parse metadata list", $magic_id, $PS_EXIT_PROG_ERROR);
 
-    # make a hash indexed by skycell_id 
+    # make a hash indexed by skycell_id
     foreach my $warp ( @$warps ) {
         my $skycell_id = $warp->{skycell_id};
@@ -156,9 +160,9 @@
 
     my ($header, $status) = (undef, 0);
-    my $fits =  Astro::FITS::CFITSIO::open_file( $skyfileResolved, READONLY, $status ); 
+    my $fits =  Astro::FITS::CFITSIO::open_file( $skyfileResolved, READONLY, $status );
     &my_die("failed to open skycell file: $skyfileResolved: $status", $magic_id, $PS_EXIT_SYS_ERROR) if $status;
 
     ($header, $status) = Astro::FITS::CFITSIO::fits_read_header( $fits );
-    
+
     &my_die("Unable to read skycell header: $status", $magic_id, $PS_EXIT_SYS_ERROR) if $status;
 
@@ -171,5 +175,5 @@
         $naxis2 = $$header{'NAXIS2'} or &my_die("Can't find NAXIS2", $magic_id, $PS_EXIT_SYS_ERROR);
     } else {
-        # if the skyfile is compressed then the WCS won't be in the primary header, move to the 
+        # if the skyfile is compressed then the WCS won't be in the primary header, move to the
         # extension
         my $hdutype;
@@ -257,5 +261,5 @@
     my $node = shift @tasks;
     divide_node($node, \@tasks);
-} 
+}
 
 ### Format tree for magictool
Index: trunk/ippScripts/scripts/register_exp.pl
===================================================================
--- trunk/ippScripts/scripts/register_exp.pl	(revision 22429)
+++ trunk/ippScripts/scripts/register_exp.pl	(revision 22430)
@@ -27,4 +27,13 @@
 my $ipprc = PS::IPP::Config->new();
 
+# Look for commands we need
+my $missing_tools;
+my $regtool = can_run('regtool') or (warn "can't find regtool" and $missing_tools = 1);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn ("Can't find required tools");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
 my ($cache, $exp_id, $exp_tag, $label, $dvodb, $end_stage, $tess_id, $dbname, $verbose, $no_update, $no_op, $save_temps, $logfile);
 GetOptions(
@@ -33,8 +42,8 @@
     'exp_tag|t=s'   => \$exp_tag,
     'dbname|d=s'    => \$dbname, # Database name
-    'label=s'       => \$label,   
-    'dvodb=s'       => \$dvodb,   
-    'end_stage=s'   => \$end_stage, 
-    'tess_id=s'     => \$tess_id,   
+    'label=s'       => \$label,
+    'dvodb=s'       => \$dvodb,
+    'end_stage=s'   => \$end_stage,
+    'tess_id=s'     => \$tess_id,
     'verbose'       => \$verbose,   # Print to stdout
     'no-update'     => \$no_update,
@@ -44,4 +53,8 @@
 ) or pod2usage( 2 );
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $exp_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->redirect_output($logfile) if $logfile;
 
@@ -55,14 +68,4 @@
 my @SCIENCE = ( "object", "science", "light" ); # Observation types to NOT mark as detrend
 my $DETREND_FLAG = "-end_stage reg"; # Flag to use to mark detrend exposure
-
-# Look for commands we need
-my $missing_tools;
-my $regtool = can_run('regtool') or (warn "can't find regtool" and $missing_tools = 1);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-
-if ($missing_tools) {
-    warn ("Can't find required tools");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 # setup cache interface
@@ -123,7 +126,7 @@
 $command .= " -exp_tag $exp_tag";
 $command .= " -hostname  $host"      if defined $host;
-$command .= " -dbname 	 $dbname"    if defined $dbname;
-$command .= " -label  	 $label"     if defined $label;
-$command .= " -dvodb  	 $dvodb"     if defined $dvodb;
+$command .= " -dbname    $dbname"    if defined $dbname;
+$command .= " -label     $label"     if defined $label;
+$command .= " -dvodb     $dvodb"     if defined $dvodb;
 $command .= " -end_stage $end_stage" if defined $end_stage;
 $command .= " -tess_id   $tess_id"   if defined $tess_id;
Index: trunk/ippScripts/scripts/stack_skycell.pl
===================================================================
--- trunk/ippScripts/scripts/stack_skycell.pl	(revision 22429)
+++ trunk/ippScripts/scripts/stack_skycell.pl	(revision 22430)
@@ -29,4 +29,15 @@
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
+
+# Look for programs we need
+my $missing_tools;
+my $stacktool = can_run('stacktool') or (warn "Can't find stacktool" and $missing_tools = 1);
+my $ppStack = can_run('ppStack') or (warn "Can't find ppStack" and $missing_tools = 1);
+my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my ($stack_id, $dbname, $outroot, $debug, $run_state, $threads, $reduction, $verbose, $no_update, $no_op, $redirect, $save_temps);
@@ -55,7 +66,10 @@
     and defined $run_state;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $stack_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 # XXX camera is not known here; cannot use filerules...
 # my $logDest = $ipprc->filename("LOG.EXP", $outroot);
-
 my $logDest = "$outroot.log";
 
@@ -72,15 +86,4 @@
 my $image_id = $stack_id;
 my $source_id = $ipprc->source_id($dbname, $PS_TABLE_ID_STACK);
-
-# Look for programs we need
-my $missing_tools;
-my $stacktool = can_run('stacktool') or (warn "Can't find stacktool" and $missing_tools = 1);
-my $ppStack = can_run('ppStack') or (warn "Can't find ppStack" and $missing_tools = 1);
-my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 # Get list of components for stacking
@@ -300,6 +303,6 @@
 
     unless ($my_die_called) {
-	$my_die_called = 1;
-	delete_temps() unless ($save_temps);
+        $my_die_called = 1;
+        delete_temps() unless ($save_temps);
     }
 
@@ -324,6 +327,6 @@
 {
     unless ($temp_images_exist) {
-	print "No temporary images yet generated\n";
-	return 1;
+        print "No temporary images yet generated\n";
+        return 1;
     }
 
@@ -334,8 +337,8 @@
     unless ($success) {
         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform ppConfigDump: $error_code", $stack_id, $error_code);
+        &my_die("Unable to perform ppConfigDump: $error_code", $stack_id, $error_code);
     }
     my $md = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", $stack_id, $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to parse metadata config doc", $stack_id, $PS_EXIT_PROG_ERROR);
 
     my $temp_delete = metadataLookupBool($md, 'TEMP.DELETE'); # Delete temporary files?
Index: trunk/ippScripts/scripts/warp_overlap.pl
===================================================================
--- trunk/ippScripts/scripts/warp_overlap.pl	(revision 22429)
+++ trunk/ippScripts/scripts/warp_overlap.pl	(revision 22430)
@@ -25,4 +25,14 @@
 
 my $ipprc = PS::IPP::Config->new(); # IPP configuration
+
+# Look for programs we need
+my $missing_tools;
+my $warptool = can_run('warptool') or (warn "Can't find warptool" and $missing_tools = 1);
+my $dvoImageOverlaps = can_run('dvoImageOverlaps') or (warn "Can't find dvoImageOverlaps" and $missing_tools = 1);
+my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my ($warp_id, $camera, $tess_dir, $dbname, $verbose, $no_update, $no_op, $logfile, $save_temps);
@@ -49,15 +59,9 @@
     and defined $tess_dir;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $warp_id, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->define_camera($camera);
-
-# Look for programs we need
-my $missing_tools;
-my $warptool = can_run('warptool') or (warn "Can't find warptool" and $missing_tools = 1);
-my $dvoImageOverlaps = can_run('dvoImageOverlaps') or (warn "Can't find dvoImageOverlaps" and $missing_tools = 1);
-my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 &my_die("Tessellation identifier not provided: $tess_dir", $warp_id, $PS_EXIT_SYS_ERROR) unless $tess_dir ne "NULL";
Index: trunk/ippScripts/scripts/warp_skycell.pl
===================================================================
--- trunk/ippScripts/scripts/warp_skycell.pl	(revision 22429)
+++ trunk/ippScripts/scripts/warp_skycell.pl	(revision 22430)
@@ -26,4 +26,15 @@
 
 my $ipprc = PS::IPP::Config->new(); # IPP configuration
+
+# Look for programs we need
+my $missing_tools;
+my $warptool = can_run('warptool') or (warn "Can't find warptool" and $missing_tools = 1);
+my $pswarp = can_run('pswarp') or (warn "Can't find pswarp" and $missing_tools = 1);
+my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
+my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my ($warp_id, $skycell_id, $warp_skyfile_id, $tess_dir, $camera, $dbname, $outroot, $threads, $run_state, $verbose, $no_update, $no_op, $redirect, $save_temps);
@@ -57,4 +68,8 @@
     and defined $run_state;
 
+# Unhandled exceptions should be passed on to my_die so they get pushed into the database
+$SIG{__DIE__} = sub { die @_ if $^S;
+                      my_die( $_[0], $warp_id, $skycell_id, $tess_dir, $PS_EXIT_UNKNOWN_ERROR ); };
+
 $ipprc->define_camera($camera);
 
@@ -65,15 +80,4 @@
 
 my $source_id = $ipprc->source_id($dbname, $PS_TABLE_ID_WARP);
-
-# Look for programs we need
-my $missing_tools;
-my $warptool = can_run('warptool') or (warn "Can't find warptool" and $missing_tools = 1);
-my $pswarp = can_run('pswarp') or (warn "Can't find pswarp" and $missing_tools = 1);
-my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
-my $ppStatsFromMetadata = can_run('ppStatsFromMetadata') or (warn "Can't find ppStatsFromMetadata" and $missing_tools = 1);
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit($PS_EXIT_CONFIG_ERROR);
-}
 
 # Get list of component imfiles for exposure
