Index: /trunk/ippScripts/scripts/flatcorr_proc.pl
===================================================================
--- /trunk/ippScripts/scripts/flatcorr_proc.pl	(revision 19326)
+++ /trunk/ippScripts/scripts/flatcorr_proc.pl	(revision 19327)
@@ -12,4 +12,5 @@
 
 # dettool -register -det_type FLATCORR -filelevel (level) -workdir -inst, etc 
+
 # foreach $imfile () 
 #   dettool -register_imfile -uri, etc, etc
@@ -54,9 +55,10 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --corr_id --dvodb --region --filter",
+pod2usage( -msg => "Required options: --corr_id --dvodb --region --filter --workdir",
            -exitval => 3) unless
     defined $corr_id and
     defined $dvodb and
     defined $region and
+    defined $workdir and
     defined $filter;
 
@@ -77,5 +79,5 @@
 
 $outgrid = "$workdir/grid.$corr_id.fits";
-$outcorr = "$workdir/corr.$corr_id.fits";
+$outcorr = "$workdir/corr.$corr_id";
 
 # parse the region (RAs,RAe:DECs,DECe) : item = +/-NNN.NNNN
@@ -90,5 +92,5 @@
     $command .= "-region $RAs $RAe $DECs $DECe";
     $command .= "-grid $outgrid";
-    $command .= "-imfreeze?";
+    $command .= "-imfreeze";
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -101,42 +103,52 @@
 }
 
-# use one of the input raw images as a reference image
-# XXX I'm not sure how this works: do I run this once per imfile for a given exp?
-my ($reffile)
-{
-    my $command = "$flatcoor -flatcorrimfile -limit 1";
-
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	cache_run(command => $command, verbose => 1);
-
-    unless ($success) { 
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die ("Unable to perform flatcorr -flatcorrimfile: $error_code", $dvo_id, $region, "RELPHOT", $status, $dbname);
-    }
-
+# get a single input exposure
+# flatcorr -inputexp -corr_id $corr_id -limit 1
+my $chip_id;
+{
+    my $command = "$flatcorr -inputexp -corr_id $corr_id -limit 1"; # Command to run
+    $command .= " -dbname $dbname" if defined $dbname;
+    my ( $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 camtool: $error_code", $corr_id, $error_code);
+    }
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
-	&my_die("Unable to parse metadata config doc", $dvo_id, $region, "RELPHOT", $status, $dbname, $PS_EXIT_PROG_ERROR););
-
-    my $imfiles = parse_md_list($metadata) or 
-	&my_die("Unable to parse metadata list", $dvo_id, $region, "RELPHOT", $status, $dbname, $PS_EXIT_PROG_ERROR););
-
-    my $imfile = $imfiles->[0];
-    $reffile = $imfile->{uri};
-}
-
-{
-    my $command = "$dvoMakeCorr -file $outgrid -ref $reffile $outcorr";
-
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	cache_run(command => $command, verbose => 1);
-
-    unless ($success) { 
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die ("Unable to perform dvoMakeCorr: $error_code", $dvo_id, $region, "RELASTRO.OBJECTS", $status, $dbname);
-    }
-}
-
-{
-    my $command = "$dettool -register";
+        &my_die("Unable to parse metadata config doc", $corr_id, $PS_EXIT_PROG_ERROR);
+
+    # extract the metadata for the files into a hash list
+    $files = parse_md_list($metadata) or
+        &my_die("Unable to parse metadata list", $corr_id, $PS_EXIT_PROG_ERROR);
+
+    # check for existence
+    my $file = $$files[0];
+    $chip_id = $file->{chip_id};
+}
+
+# get the list of imfiles for the single input exposure
+# flatcorr -inputimfile -chip_id $chip_id
+my $files;
+{
+    my $command = "$flatcorr -inputimfile -chip_id $chip_id"; # Command to run
+    $command .= " -dbname $dbname" if defined $dbname;
+    my ( $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 camtool: $error_code", $corr_id, $error_code);
+    }
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+        &my_die("Unable to parse metadata config doc", $corr_id, $PS_EXIT_PROG_ERROR);
+
+    # extract the metadata for the files into a hash list
+    $files = parse_md_list($metadata) or
+        &my_die("Unable to parse metadata list", $corr_id, $PS_EXIT_PROG_ERROR);
+}
+
+# set up the detrend run to store the corrected imfiles
+my $det_id;
+{
+    my $command = "$dettool -register_detrend";
     $command .= "-det_type FLATCORR";
     $command .= "-file_level $fileLevel";
@@ -151,11 +163,73 @@
     unless ($success) { 
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die ("Unable to perform addstar -resort on region $region: $error_code", $dvo_id, $region, "RELASTRO.IMAGES", $status, $dbname);
-    }
-}
-
-my $command = "$flatcorr -done";
+	&my_die ("Unable to register new detrend: $error_code", $corr_id, $PS_EXIT_PROG_ERROR);
+    }
+
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+        &my_die("Unable to parse metadata config doc", $corr_id, $PS_EXIT_PROG_ERROR);
+
+    # extract the metadata for the files into a hash list
+    my $output = parse_md_list($metadata) or
+        &my_die("Unable to parse metadata list", $corr_id, $PS_EXIT_PROG_ERROR);
+
+    my $file = $$output[0];
+
+    $det_id = $file->{det_id};
+}
+
+# use input chip image as a reference image
+foreach my $file (@$files) {
+    # create the detrend correction for the imfiles based on the input imfiles
+    my $reffile = $file->{uri};
+    my $class_id = $file->{class_id};
+
+    my $uri = ipprc_filename ($outcorr, "DVO.CORR", class_id);
+
+    my $command = "$dvoMakeCorr -file $outgrid -ref $reffile $outcorr";
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	cache_run(command => $command, verbose => 1);
+
+    unless ($success) { 
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die ("Unable to perform dvoMakeCorr: $error_code", $corr_id, $PS_EXIT_PROG_ERROR);
+    }
+
+    # register the detrend correction imfile
+    my $command = "$dettool -register_detrend_imfile";
+    $command .= " -det_id $det_id";
+    $command .= " -class_id $class_id";
+    $command .= " -uri $uri";
+    $command .= " -dbname $dbname" if defined $dbname;
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	cache_run(command => $command, verbose => 1);
+
+    unless ($success) { 
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die ("Unable to register new detrend: $error_code", $corr_id, $PS_EXIT_PROG_ERROR);
+    }
+}
+
+my $command = "$dettool -updatedetrun";
+$command .= " -det_id $det_id";
+$command .= " -state XXX";
+$command .= " -dbname $dbname" if defined $dbname;
+
+# Push the results into the database
+unless ($no_update) {
+    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);
+        warn ("Unable to perform dettool -updatedetrun: $error_code");
+        exit($error_code);
+    }
+} else {
+    print "skipping command: $command\n";
+}
+
+my $command = "$flatcorr -addprocess";
 $command .= " -corr_id $corr_id";
-$command .= " -stats UNKNOWN";
 $command .= " -dbname $dbname" if defined $dbname;
 
