Index: trunk/ippScripts/scripts/detrend_stack.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_stack.pl	(revision 16563)
+++ trunk/ippScripts/scripts/detrend_stack.pl	(revision 17231)
@@ -18,4 +18,5 @@
 use PS::IPP::Metadata::Stats;
 use PS::IPP::Metadata::List qw( parse_md_list );
+use File::Temp qw( tempfile );
 
 use PS::IPP::Config qw($PS_EXIT_SUCCESS
@@ -33,6 +34,6 @@
 use Pod::Usage qw( pod2usage );
 
-my ( $det_id, $iter, $class_id, $det_type, $camera, $outroot, $dbname, $reduction, $verbose, $no_update,
-     $no_op );
+my ( $det_id, $iter, $class_id, $det_type, $camera, $outroot, $dbname, $reduction, $verbose, $save_temps,
+     $no_update, $no_op );
 GetOptions(
     'det_id|d=s'        => \$det_id,
@@ -45,4 +46,5 @@
     'reduction=s'       => \$reduction,	# Reduction class for processing
     'verbose'           => \$verbose,   # Print to stdout
+    'save-temps'        => \$save_temps, # Save temporary files?
     'no-update'         => \$no_update,
     'no-op'             => \$no_op,
@@ -72,10 +74,24 @@
    [   
        #          KEYWORD                 STATISTIC          CHIPTOOL FLAG
-       { name => "bg",             type => "mean",  flag => "-bg",             dtype => "float" },
-       { name => "bg",             type => "stdev", flag => "-bg_mean_stdev",  dtype => "float" },
-       { name => "bg_stdev",       type => "rms",   flag => "-bg_stdev",       dtype => "float" },
-       # { name => "bg_mean_stdev",  type => "rms",   flag => "-bg_mean_stdev" },
+       { name => "ROBUST_MEDIAN",  type => "mean",  flag => "-bg",             dtype => "float" },
+       { name => "ROBUST_MEDIAN",  type => "stdev", flag => "-bg_mean_stdev",  dtype => "float" },
+       { name => "ROBUST_STDEV",   type => "rms",   flag => "-bg_stdev",       dtype => "float" },
    ];
 my $stats = PS::IPP::Metadata::Stats->new($STATS); # Stats parser
+
+# The output file rule name depends on the detrend type
+my $FILERULES = { 'FLATMASK' => 'PPMERGE.OUTPUT.MASK',
+		  'DARKMASK' => 'PPMERGE.OUTPUT.MASK',
+		  'MASK'     => 'PPMERGE.OUTPUT.MASK',
+		  'BIAS'     => 'PPMERGE.OUTPUT.BIAS',
+		  'DARK'     => 'PPMERGE.OUTPUT.DARK',
+		  'SHUTTER'  => 'PPMERGE.OUTPUT.SHUTTER',
+		  'FLAT'     => 'PPMERGE.OUTPUT.FLAT',
+		  'DOMEFLAT' => 'PPMERGE.OUTPUT.FLAT',
+		  'SKYFLAT'  => 'PPMERGE.OUTPUT.FLAT',
+		  'FRINGE'   => 'PPMERGE.OUTPUT.FRINGE',
+	      };
+my $output_filerule = $FILERULES->{$det_type}; # File rule for output
+&my_die("Unrecognised detrend type: $det_type", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless defined $output_filerule;
 
 # Look for programs we need
@@ -110,4 +126,32 @@
 }
 
+# Generate MDC file with the inputs
+my ($listFile, $listName) = tempfile( $ipprc->file_resolve("$outroot.$class_id.list.XXXX"), UNLINK => !$save_temps );
+my $num = 0;
+foreach my $file (@$files) {
+    if ($file->{ignored}) { next; }
+
+    print $listFile "INPUT$num\tMETADATA\n";
+    $num++;
+
+    my $image = $file->{uri};	# Image name
+    my $mask = $ipprc->filename( "PPIMAGE.OUTPUT.MASK", $file->{path_base} ); # Mask name
+    my $weight = $ipprc->filename( "PPIMAGE.OUTPUT.WEIGHT", $file->{path_base} ); # Weight name
+
+    &my_die("Image $image does not exist", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists( $image );
+    print $listFile "\tIMAGE\tSTR\t" . $image . "\n";
+
+    if ($ipprc->file_exists( $mask )) {
+	print $listFile "\tMASK\tSTR\t" . $mask . "\n";
+    }
+    if ($ipprc->file_exists( $weight )) {
+	print $listFile "\tWEIGHT\tSTR\t" . $weight . "\n";
+    }
+
+    print $listFile "END\n\n";
+}
+close $listFile;
+
+
 # outroot examples (HOST components must be set)
 # file://data/ipp004.0/gpc1/20080130
@@ -118,14 +162,12 @@
 $ipprc->outroot_prepare($outroot);
 
-my $outputStack = $ipprc->filename("PPMERGE.OUTPUT", $outroot, $class_id) or &my_die("Missing entry in file rules", $det_id, $iter, $class_id, $PS_EXIT_CONFIG_ERROR); # Output name
+my $outputStack = $ipprc->filename($output_filerule, $outroot, $class_id) or &my_die("Missing entry in file rules", $det_id, $iter, $class_id, $PS_EXIT_CONFIG_ERROR); # Output name
+my $outputCount = $ipprc->filename("PPMERGE.OUTPUT.COUNT", $outroot, $class_id) or &my_die("Missing entry in file rules", $det_id, $iter, $class_id, $PS_EXIT_CONFIG_ERROR); # Count image
+my $outputSigma = $ipprc->filename("PPMERGE.OUTPUT.SIGMA", $outroot, $class_id) or &my_die("Missing entry in file rules", $det_id, $iter, $class_id, $PS_EXIT_CONFIG_ERROR); # Stdev image
 my $outputStats = $ipprc->filename("PPIMAGE.STATS",  $outroot, $class_id) or &my_die("Missing entry in file rules", $det_id, $iter, $class_id, $PS_EXIT_CONFIG_ERROR); # Statistics name
 my $traceDest   = $ipprc->filename("TRACE.IMFILE",   $outroot, $class_id) or &my_die("Missing entry in file rules", $det_id, $iter, $class_id, $PS_EXIT_CONFIG_ERROR); # Trace messages
 my $logDest     = $ipprc->filename("LOG.IMFILE",     $outroot, $class_id) or &my_die("Missing entry in file rules", $det_id, $iter, $class_id, $PS_EXIT_CONFIG_ERROR); # Log messages
 
-
-$command = "$ppMerge $outputStack"; # Command to run
-foreach my $file (@$files) {
-    $command .= ' ' . $file->{uri};
-}
+$command = "$ppMerge $listName $outroot"; # Command to run
 $command .= " -recipe PPMERGE $recipe";
 $command .= ' -type ' . uc($det_type); # Type of stacking to perform
@@ -136,6 +178,5 @@
 # Stack the files
 unless ($no_op) {
-    
-    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => $verbose);
     unless ($success) {
@@ -144,4 +185,6 @@
     }
     &my_die("Unable to find expected output file: $outputStack\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputStack);
+    &my_die("Unable to find expected output file: $outputCount\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputCount);
+    &my_die("Unable to find expected output file: $outputSigma\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputSigma);
     &my_die("Unable to find expected output file: $outputStats\n", $det_id, $iter, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputStats);
 
