Index: trunk/ippScripts/scripts/detrend_correct_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/detrend_correct_imfile.pl	(revision 20369)
+++ trunk/ippScripts/scripts/detrend_correct_imfile.pl	(revision 20378)
@@ -10,4 +10,5 @@
 print "\n\n";
 print "Starting script $0 on $host\n\n";
+print "command: @ARGV\n\n";
 
 use vars qw( $VERSION );
@@ -23,34 +24,29 @@
 use Pod::Usage qw( pod2usage );
 
-my ( $det_id, $exp_id, $class_id, $det_type, $exp_tag, $input_uri, $camera, $dbname, $workdir, $reduction,
-     $verbose, $no_update, $no_op, $outroot, $redirect, $corr_id, $corr_type, $corr_uri );
+my ( $det_id, $class_id, $det_type, $input_uri, $camera, $dbname,
+     $verbose, $no_update, $no_op, $outroot, $redirect, $corr_uri );
 GetOptions(
     'det_id|d=s'        => \$det_id,
-    'exp_id|e=s'        => \$exp_id,
     'class_id|i=s'      => \$class_id,
     'det_type|t=s'      => \$det_type,
-    'exp_tag|=s'        => \$exp_tag,
     'input_uri|u=s'     => \$input_uri,
-    'corr_id|u=s'       => \$corr_id,
-    'corr_type|u=s'     => \$corr_type,
+    'corr_uri|u=s'      => \$corr_uri,
+    'outroot|o=s'       => \$outroot,
     'camera|c=s'        => \$camera,
     'dbname|d=s'        => \$dbname, # Database name
-    'workdir|w=s'       => \$workdir, # Working directory, for output files
     'verbose'           => \$verbose,   # Print to stdout
     'no-update'         => \$no_update,
     'no-op'             => \$no_op,
-    'outroot'           => \$outroot,
     'redirect-output'   => \$redirect,
 ) or pod2usage( 2 );
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --det_id --exp_id --class_id --det_type --exp_tag --input_uri --camera",
+pod2usage( -msg => "Required options: --det_id --class_id --det_type --input_uri --camera --outroot",
 	   -exitval => 3)
     unless defined $det_id
-    and defined $exp_id
     and defined $class_id
     and defined $det_type
-    and defined $exp_tag
     and defined $input_uri
+    and defined $outroot
     and defined $camera;
 
@@ -59,18 +55,23 @@
 
 if ($redirect) {
-    die "must suplly --outroot with --redirect-output" if !defined ($outroot);
-    my $logDest     = $ipprc->filename("LOG.IMFILE", $outroot, $class_id)
-       or &my_die("Missing entry from camera config", $det_id, $exp_id, $class_id, $PS_EXIT_CONFIG_ERROR);
+    my $logDest = $ipprc->filename("LOG.IMFILE", $outroot, $class_id)
+       or &my_die("Missing entry from camera config", $det_id, $class_id, $PS_EXIT_CONFIG_ERROR);
     $ipprc->redirect_output($logDest);
 }
 
-# Recipes to use as a function of detrend type
-$reduction = "DETREND" unless defined $reduction;
-my $recipe = $ipprc->reduction($reduction, uc($det_type) . '_PROCESS'); # Recipe name to use
+# 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;
+# my $recipe = $ipprc->reduction($reduction, uc($det_type) . '_CORRECT'); # Recipe name to use
+
+# XXX for now, check and warn, but don't exit on unexpected det_types
+# 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 $dvocorr = can_run('dvoApplyCorr') or (warn "Can't find dvoApplyCorr" 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.");
@@ -78,17 +79,14 @@
 }
 
-&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);
+&my_die("Couldn't find input file: $input_uri\n", $det_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($input_uri);
 
-$workdir = caturi( $workdir, "$camera.$det_type.$det_id" ) if defined $workdir;
-
-# detselect -select -det_id $corr_id -class_id $class_id;
-
-my $outputRoot  = $ipprc->file_prepare( "$exp_tag/$exp_tag.detproc.$det_id", $workdir, $input_uri );
-my $outputImage = $ipprc->filename("DVOCORR.OUTPUT", $outputRoot, $class_id) or &my_die("Missing entry from camera config", $det_id, $exp_id, $class_id, $PS_EXIT_PROG_ERROR);
+my $outputImage = $ipprc->filename("DVOFLAT.OUTPUT", $outroot, $class_id) or &my_die("Missing entry from camera config", $det_id, $class_id, $PS_EXIT_PROG_ERROR);
 
 # Run dvoApplyCorr
 unless ($no_op) {
-    my $command = "$dvocorr -file $input_uri $outputRoot";
-    $command .= " -corr $corr_uri";
+    # unless explicitly supplied, the correction image is determined by dvoApplyCorr via look up to the detrend database
+    my $command = "$dvoApplyCorr -file $input_uri $outroot";
+    $command .= " -corr $corr_uri" if defined $corr_uri;
+    $command .= " -dbname $dbname" if defined $dbname;
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -96,17 +94,16 @@
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform ppImage: $error_code", $det_id, $exp_id, $class_id, $error_code);
+	&my_die("Unable to perform ppImage: $error_code", $det_id, $class_id, $error_code);
     }
 
-    &my_die("Couldn't find expected output file: $outputImage", $det_id, $exp_id, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputImage);
+    &my_die("Couldn't find expected output file: $outputImage", $det_id, $class_id, $PS_EXIT_SYS_ERROR) unless -f $ipprc->file_resolve($outputImage);
 }
 
 # command to update database
-my $command = "$dettool -addcorrectedimfile";
+my $command = "$dettool -addcorrectimfile";
 $command .= " -det_id $det_id";
-$command .= " -exp_id $exp_id";
 $command .= " -class_id $class_id";
 $command .= " -uri $outputImage";
-$command .= " -path_base $outputRoot";
+$command .= " -path_base $outroot";
 $command .= " -dbname $dbname" if defined $dbname;
 
@@ -117,5 +114,5 @@
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	warn("Unable to perform dettool -addcorrectedimfile: $error_code\n");
+	warn("Unable to perform dettool -addcorrectimfile: $error_code\n");
 	exit($error_code);
     }
@@ -128,15 +125,13 @@
     my $msg = shift; # Warning message on die
     my $det_id = shift;		# Detrend identifier
-    my $exp_id = shift; # Exposure tag
     my $class_id = shift; # Class identifier
     my $exit_code = shift; # Exit code to add
 
     carp($msg);
-    if (defined $det_id and defined $exp_id and defined $class_id and not $no_update) {
-	my $command = "$dettool -addcorrectedimfile";
+    if (defined $det_id and defined $class_id and not $no_update) {
+	my $command = "$dettool -addcorrectimfile";
 	$command .= " -det_id $det_id";
-	$command .= " -exp_id $exp_id"; 
 	$command .= " -class_id $class_id";
-	$command .= " -path_base $outputRoot";
+	$command .= " -path_base $outroot";
 	$command .= " -code $exit_code";
 	$command .= " -dbname $dbname" if defined $dbname;
