Index: trunk/ippScripts/scripts/detrend_process_exp.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 14009)
+++ trunk/ippScripts/scripts/detrend_process_exp.pl	(revision 14048)
@@ -16,4 +16,6 @@
 use IPC::Cmd 0.36 qw( can_run run );
 use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::Stats;
+
 use PS::IPP::Metadata::List qw( parse_md_list );
 use File::Temp qw( tempfile );
@@ -33,12 +35,14 @@
 use Pod::Usage qw( pod2usage );
 
-my ($det_id, $det_type, $exp_tag, $camera, $dbname, $workdir, $no_update, $no_op);
+my ($det_id, $exp_id, $det_type, $exp_tag, $camera, $dbname, $workdir, $reduction, $no_update, $no_op);
 GetOptions(
     'det_id|d=s'        => \$det_id,
     'det_type|t=s'      => \$det_type,
-    'exp_tag|e=s'       => \$exp_tag,
+    'exp_id|d=s'        => \$exp_id,
+    'exp_tag|=s'        => \$exp_tag,
     'camera|c=s'        => \$camera,
     'dbname|d=s'        => \$dbname, # Database name
     'workdir|w=s'       => \$workdir,	# Working directory, for output files
+    'reduction|=s'      => \$reduction,
     'no-update'         => \$no_update,
     'no-op'             => \$no_op,
@@ -46,8 +50,9 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --det_id --det_type --exp_tag --camera",
+pod2usage( -msg => "Required options: --det_id --det_type --exp_id --exp_tag --camera",
 	   -exitval => 3) 
     unless defined $det_id
     and defined $det_type
+    and defined $exp_id
     and defined $exp_tag
     and defined $camera;
@@ -58,9 +63,9 @@
 $reduction = 'DETREND' unless defined $reduction;
 
-my $recipe1 = $ipprc->reduction($reduction, 'JPEG_BIN1_IMAGE' . uc($det_type); # Recipe to use
-&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless defined $recipe1;
-
-my $recipe2 = $ipprc->reduction($reduction, 'JPEG_BIN2_IMAGE' . uc($det_type); # Recipe to use
-&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless defined $recipe2;
+my $recipe1 = $ipprc->reduction($reduction, 'JPEG_BIN1_IMAGE_' . uc($det_type)); # Recipe to use
+&my_die("Unrecognised detrend type: $det_type", $det_id, $exp_id, $PS_EXIT_PROG_ERROR) unless defined $recipe1;
+
+my $recipe2 = $ipprc->reduction($reduction, 'JPEG_BIN2_IMAGE_' . uc($det_type)); # Recipe to use
+&my_die("Unrecognised detrend type: $det_type", $det_id, $exp_id, $PS_EXIT_PROG_ERROR) unless defined $recipe2;
 
 # values to extract from output metadata and the stats to calculate
@@ -87,13 +92,13 @@
 
 # Get list of component files
-my $files;			# Array of component files
+my ($files, $command, $success, $error_code, $full_buf, $stdout_buf, $stderr_buf);
 {
-    my $command = "$dettool -processedimfile -det_id $det_id -exp_tag $exp_tag"; # Command to run
+    $command = "$dettool -processedimfile -det_id $det_id -exp_id $exp_id"; # Command to run
     $command .= " -dbname $dbname" if defined $dbname;
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
-    unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform dettool -processedimfile: $error_code", $det_id, $exp_tag, $error_code);
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform dettool -processedimfile: $error_code", $det_id, $exp_id, $error_code);
     }
 
@@ -101,14 +106,14 @@
     my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", $det_id, $exp_tag, $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to parse metadata config doc", $det_id, $exp_id, $PS_EXIT_PROG_ERROR);
 
     # parse the file info in the metadata
     $files = parse_md_list($metadata) or
-	&my_die("Unable to parse metadata list", $det_id, $exp_tag, $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to parse metadata list", $det_id, $exp_id, $PS_EXIT_PROG_ERROR);
 
     # parse the stats in the metadata
     my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
     unless ($stats->parse($metadata)) {
-	&my_die("Unable to find all values in statistics output.\n", $chip_id, $class_id, $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to find all values in statistics output.\n", $det_id, $exp_id, $PS_EXIT_PROG_ERROR);
     }
 }
@@ -132,32 +137,28 @@
 unless ($no_op) {
     # Make the jpeg for binning 1
-    {
-	my $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE $recipe1"; # Command to run
-	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	    run(command => $command, verbose => 1);
-	unless ($success) {
-	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &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 $ipprc->file_resolve($jpeg1);
-    }
+    $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE $recipe1"; # Command to run
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform ppImage: $error_code", $det_id, $exp_id, $error_code);
+    }
+    &my_die("Unable to find expected output file: $jpeg1", $det_id, $exp_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($jpeg1);
     
     # Make the jpeg for binning 2
-    {
-	my $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE $recipe2"; # Command to run
-	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	    run(command => $command, verbose => 1);
-	unless ($success) {
-	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &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 $ipprc->file_resolve($jpeg2);
-    }
+    $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE $recipe2"; # Command to run
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform ppImage: $error_code", $det_id, $exp_id, $error_code);
+    }
+    &my_die("Unable to find expected output file: $jpeg2", $det_id, $exp_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($jpeg2);
 }
 
 # Command to update the database
-my $command = "$dettool -addprocessedexp";
+$command  = "$dettool -addprocessedexp";
 $command .= " -det_id $det_id";
-$command .= " -exp_tag $exp_tag";
+$command .= " -exp_id $exp_id";
 $command .= " -recip $recipe1,$recipe2 -path_base $outputRoot";
 $command .= " -dbname $dbname" if defined $dbname;
@@ -172,5 +173,5 @@
 # Add the processed file to the database
 unless ($no_update) {
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
     unless ($success) {
@@ -187,12 +188,12 @@
     my $msg = shift; # Warning message on die
     my $det_id = shift;		# Detrend identifier
-    my $exp_tag = shift; # Exposure tag
+    my $exp_id = shift; # Exposure tag
     my $exit_code = shift; # Exit code to add
 
     carp($msg);
-    if ($det_id and $exp_tag and not $no_update) {
+    if ($det_id and $exp_id and not $no_update) {
 	my $command = "$dettool -addprocessedexp";
 	$command .= " -det_id $det_id";
-	$command .= " -exp_tag $exp_tag";
+	$command .= " -exp_id $exp_id";
 	$command .= " -code $exit_code";
 	$command .= " -dbname $dbname" if defined $dbname;
