Index: trunk/ippScripts/scripts/detrend_reject_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 14009)
+++ trunk/ippScripts/scripts/detrend_reject_imfile.pl	(revision 14048)
@@ -22,4 +22,6 @@
 use IPC::Cmd 0.36 qw( can_run run );             # tools to run UNIX programs with control over I/O
 use PS::IPP::Metadata::Config;                   # tools to parse the psMetadataConfig files
+use PS::IPP::Metadata::Stats;
+
 use PS::IPP::Metadata::List qw( parse_md_list ); # tools to parse a metadata into a hash list
 use Statistics::Descriptive;                     # tools for calculating basic statistical quantities
@@ -41,9 +43,10 @@
 
 # parse the command-line options
-my ($det_id, $iter, $exp_tag, $det_type, $camera, $filter, $reject, $dbname, $workdir, $no_update, $no_op);
+my ($det_id, $iter, $exp_id, $exp_tag, $det_type, $camera, $filter, $reject, $dbname, $workdir, $reduction, $no_update, $no_op);
 GetOptions(
 	   'det_id|d=s'        => \$det_id,
 	   'iteration=s'       => \$iter,
-	   'exp_tag|e=s'       => \$exp_tag,
+	   'exp_id|e=s'        => \$exp_id,
+	   'exp_tag|=s'        => \$exp_tag,
 	   'det_type|t=s'      => \$det_type,
 	   'camera=s'          => \$camera,
@@ -52,4 +55,5 @@
 	   '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,
@@ -57,8 +61,9 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --det_id --iteration --exp_tag --det_type --camera",
+pod2usage( -msg => "Required options: --det_id --iteration --exp_id --exp_tag --det_type --camera",
 	   -exitval => 3)
     unless defined $det_id
     and defined $iter
+    and defined $exp_id
     and defined $exp_tag
     and defined $det_type
@@ -71,8 +76,8 @@
 $reduction = 'DETREND' unless defined $reduction;
 
-my $recipe1 = $ipprc->reduction($reduction, 'JPEG_BIN1_RESID' . uc($det_type); # Recipe to use
+my $recipe1 = $ipprc->reduction($reduction, 'JPEG_BIN1_RESID_' . 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_RESID' . uc($det_type); # Recipe to use
+my $recipe2 = $ipprc->reduction($reduction, 'JPEG_BIN2_RESID_' . uc($det_type)); # Recipe to use
 &my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless defined $recipe2;
 
@@ -82,17 +87,17 @@
 # XXX  (A) if imfile.Ncomp > 1, (B) if imfile.Ncomp == 1
 my $STATS = 
-   [   
-       #          KEYWORD                 STATISTIC          CHIPTOOL FLAG
-       { name => "bg",             type => "mean",  flag => "-bg" },
-       { name => "bg_mean_stdev",  type => "stdev", flag => "-bg_mean_stdev" },
-       { name => "bg_stdev",       type => "rms",   flag => "-bg_stdev" },
-       { name => "bin_stdev",      type => "rms",   flag => "-bin_stdev" },
-       { name => "fringe_0",       type => "mean",  flag => "-fringe_0" },
-       { name => "fringe_1",       type => "rms",   flag => "-fringe_1" },
-       { name => "fringe_0",       type => "stdev", flag => "-fringe_2" },
-       { name => "user_1",         type => "mean",  flag => "-user_1" }, # fringe residual
-       { name => "user_2",         type => "rms",   flag => "-user_2" }, # fringe residual
-       { name => "user_3",         type => "stdev", flag => "-user_1" }, # fringe residual
-   ];
+    [   
+	#          KEYWORD                 STATISTIC          CHIPTOOL FLAG
+	{ name => "bg",             type => "mean",  flag => "-bg" },
+	{ name => "bg_mean_stdev",  type => "stdev", flag => "-bg_mean_stdev" },
+	{ name => "bg_stdev",       type => "rms",   flag => "-bg_stdev" },
+	{ name => "bin_stdev",      type => "rms",   flag => "-bin_stdev" },
+	{ name => "fringe_0",       type => "mean",  flag => "-fringe_0" },
+	{ name => "fringe_1",       type => "rms",   flag => "-fringe_1" },
+	{ name => "fringe_0",       type => "stdev", flag => "-fringe_2" },
+	{ name => "user_1",         type => "mean",  flag => "-user_1" }, # fringe residual
+	{ name => "user_2",         type => "rms",   flag => "-user_2" }, # fringe residual
+	{ name => "user_3",         type => "stdev", flag => "-user_1" }, # fringe residual
+	];
 
 # Look for programs we need
@@ -106,13 +111,13 @@
 
 # Get list of imfile files
-my $files;			# Array of imfile files
+my ($files, $command, $success, $error_code, $full_buf, $stdout_buf, $stderr_buf);
 {
-    # dettool command to select imfile data for this exp_tag
-    my $command = "$dettool -residimfile";
+    # dettool command to select imfile data for this exp_id
+    $command  = "$dettool -residimfile";
     $command .= " -det_id $det_id";
     $command .= " -iteration $iter";
-    $command .= " -exp_tag $exp_tag";
+    $command .= " -exp_id $exp_id";
     $command .= " -dbname $dbname" if defined $dbname;
-    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) {
@@ -126,13 +131,13 @@
     my $mdcParser = PS::IPP::Metadata::Config->new;	
     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);
+	&my_die("Unable to parse metadata config doc", $det_id, $iter, $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, $iter, $exp_tag, $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to parse metadata list", $det_id, $iter, $exp_id, $PS_EXIT_PROG_ERROR);
 
     # Parse the statistics on the residual image
     my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
-    $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);
+    $stats->parse($metadata) or &my_die("Unable to find all values in statistics output.", $det_id, $iter, $exp_id, $PS_EXIT_PROG_ERROR);
 }
 
@@ -162,28 +167,23 @@
 # build the JPEG images
 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 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 $ipprc->file_resolve($jpeg1Name);
-    }
+    $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 run ppImage: $error_code", $det_id, $iter, $exp_id, $error_code);
+    }
+    &my_die("Unable to find expected output file: $jpeg1Name", $det_id, $iter, $exp_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($jpeg1Name);
     
     # 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 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 $ipprc->file_resolve($jpeg2Name);
-    }
+    $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 run ppImage: $error_code", $det_id, $iter, $exp_id, $error_code);
+    }
+    &my_die("Unable to find expected output file: $jpeg2Name", $det_id, $iter, $exp_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($jpeg2Name);
 }
 
@@ -207,24 +207,8 @@
 my $reject_exp_bin_sn       = rejection_limit( 'EXP.BIN.SN',       $det_type, $filter );
 
-# storage variables
-
-# load the arrays from the imfile output table
-foreach my $file (@$files) {
-    if ($file->{exp_time} > 0.0) {
-	push @fluxes,     	$file->{bg} / $file->{exp_time};       # mean background counts / sec
-    } else {
-	push @fluxes,     	$file->{bg};
-    }
-}
-
-# Reject based on the stats of the imfiles
-# it is VALID to reject on more than one criterion
-&my_die("Number of means and number of variances differ!", $det_id, $iter, $exp_tag, $PS_EXIT_PROG_ERROR) unless scalar @means == scalar @variances;
-
 # storage array
 my @fluxes;
 
-for (my $i = 0; $i < scalar @$files; $i++) {
-    my $file      = $files[$i];
+foreach my $file (@$files) {
     my $name      = $file->{class_id};
     my $mean      = $file->{bg};	# Mean for this imfile
@@ -234,7 +218,9 @@
 
     # calculate and save the fluxes
-    my $flux      = $mean / $file->{exp_time};
+    my $flux;
     if ($file->{exp_time} == 0.0) {
-	push @fluxes, $mean;
+	$flux = $mean;
+    } else {
+	$flux = $mean / $file->{exp_time};
     }
     push @fluxes, $flux;
@@ -296,5 +282,5 @@
     # component means is larger than the limit
     if ($reject_imfile_meanstdev > 0) {
-	if ($meanStdevs > $reject_imfile_meanstdev) {
+	if ($meanStdev > $reject_imfile_meanstdev) {
 	    print $logFile "Rejecting exposure based on bad imfile mean stdev for $name: ";
 	    $reject = 1;
@@ -302,5 +288,5 @@
 	    print $logFile "Imfile mean stdev for $name meets requirements: ";
 	}
-	print $logFile "$meanStdevs vs $reject_imfile_meanstdev\n";
+	print $logFile "$meanStdev vs $reject_imfile_meanstdev\n";
     } else {
 	print $logFile "No rejection on imfile mean stdev for $name\n";
@@ -479,5 +465,5 @@
 }
 
-my $command = "$dettool -addresidexp -det_id $det_id -iteration $iter -exp_tag $exp_tag";
+$command  = "$dettool -addresidexp -det_id $det_id -iteration $iter -exp_id $exp_id";
 $command .= " -recip $recipe1,$recipe2 -path_base $outputRoot ";
 $command .= ' -reject' if $reject;
@@ -492,5 +478,5 @@
 
 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) {
@@ -516,13 +502,13 @@
     my $det_id = shift;		# Detrend identifier
     my $iter = shift;		# Iteration
-    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 $iter and $exp_tag and not $no_update) {
+    if ($det_id and $iter and $exp_id and not $no_update) {
 	my $command = "$dettool -addresidexp";
 	$command .= " -det_id $det_id";
 	$command .= " -iteration $iter";
-	$command .= " -exp_tag $exp_tag";
+	$command .= " -exp_id $exp_id";
 	$command .= " -code $exit_code";
 	$command .= " -dbname $dbname" if defined $dbname;
