Index: trunk/ippScripts/scripts/flatcorr_proc.pl
===================================================================
--- trunk/ippScripts/scripts/flatcorr_proc.pl	(revision 19352)
+++ trunk/ippScripts/scripts/flatcorr_proc.pl	(revision 19373)
@@ -34,4 +34,5 @@
 use PS::IPP::Metadata::Config;
 use PS::IPP::Metadata::Stats;
+use PS::IPP::Metadata::List qw( parse_md_list );
 use PS::IPP::Config 1.01 qw( :standard );
 
@@ -42,5 +43,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($corr_id, $dvodb, $region, $filter, $dbname, $workdir, $no_update, $no_op);
+my ($corr_id, $dvodb, $region, $filter, $dbname, $workdir, $verbose, $no_update, $no_op);
 GetOptions(
     'corr_id|i=s'      => \$corr_id,
@@ -50,4 +51,5 @@
     '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,
@@ -66,4 +68,5 @@
 my $missing_tools;
 my $relphot     = can_run('relphot')      or (warn "Can't find relphot"      and $missing_tools = 1);
+my $addstar     = can_run('addstar')      or (warn "Can't find addstar"      and $missing_tools = 1);
 my $dvoMakeCorr = can_run('dvoMakeCorr')  or (warn "Can't find dvoMakeCorr"  and $missing_tools = 1);
 my $detselect   = can_run('detselect') 	  or (warn "Can't find detselect"    and $missing_tools = 1);
@@ -76,8 +79,8 @@
 }
 
-my($outgrid, $outcorr);
-
-$outgrid = "$workdir/grid.$corr_id.fits";
-$outcorr = "$workdir/corr.$corr_id";
+my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
+
+my $outgrid = "$workdir/grid.$corr_id.fits";
+my $outcorr = "$workdir/corr.$corr_id";
 
 # parse the region (RAs,RAe:DECs,DECe) : item = +/-NNN.NNNN
@@ -86,18 +89,33 @@
 my ($DECs, $DECe) = split (",", $coords[1]);
 
+# Run addstar -resort to ensure the db is indexed
+{
+    my $command = "echo $addstar -resort";
+    $command .= " -D CATDIR $dvodb";
+    $command .= " -region $RAs $RAe $DECs $DECe";
+
+    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 addstar -resort for dvodb $dvodb on region $region: $error_code", $corr_id, $error_code);
+    }
+}
+
 # Run relphot (filter) for the specified region (need to clarify the options like imfreeze)
 {
-    my $command = "$relphot $filter";
-    $command .= "-D CATDIR $dvodb";
-    $command .= "-region $RAs $RAe $DECs $DECe";
-    $command .= "-grid $outgrid";
-    $command .= "-imfreeze";
-
-    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 relphot -grid on region $region: $error_code", $dvo_id, $region, "RELPHOT", $status, $dbname);
+    my $command = "echo $relphot $filter";
+    $command .= " -D CATDIR $dvodb";
+    $command .= " -region $RAs $RAe $DECs $DECe";
+    $command .= " -grid $outgrid";
+    $command .= " -imfreeze";
+
+    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 relphot -grid for dvodb $dvodb on region $region: $error_code", $corr_id, $error_code);
     }
 }
@@ -107,6 +125,9 @@
 my $chip_id;
 {
-    my $command = "$flatcorr -inputexp -corr_id $corr_id -limit 1"; # Command to run
-    $command .= " -dbname $dbname" if defined $dbname;
+    my $command = "$flatcorr -inputexp";
+    $command .= " -corr_id $corr_id";
+    $command .= " -limit 1";
+    $command .= " -dbname $dbname" if defined $dbname;
+
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         run(command => $command, verbose => $verbose);
@@ -119,8 +140,8 @@
 
     # extract the metadata for the files into a hash list
-    $files = parse_md_list($metadata) or
+    my $files = parse_md_list($metadata) or
         &my_die("Unable to parse metadata list", $corr_id, $PS_EXIT_PROG_ERROR);
 
-    # check for existence
+    # check for existence 
     my $file = $$files[0];
     $chip_id = $file->{chip_id};
@@ -131,6 +152,8 @@
 my $files;
 {
-    my $command = "$flatcorr -inputimfile -chip_id $chip_id"; # Command to run
-    $command .= " -dbname $dbname" if defined $dbname;
+    my $command = "$flatcorr -inputimfile";
+    $command .= " -chip_id $chip_id";
+    $command .= " -dbname $dbname" if defined $dbname;
+
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         run(command => $command, verbose => $verbose);
@@ -150,14 +173,33 @@
 my $det_id;
 {
+    # get the filelevel from one of these chips
+    my $file = $$files[0];
+
+    my $filelevel = $file->{filelevel};
+    my $camera    = $file->{camera};
+    my $telescope = $file->{telescope};
+    my $filter    = $file->{filter};
+
+    ## XXX the flat-field correction is only defined for a single
+    ## camera.  we are grabbing the camera here, but we are assuming
+    ## the flatcorrRun was defined for only a single camera.
+    $ipprc->define_camera($camera);
+
     my $command = "$dettool -register_detrend";
-    $command .= "-det_type FLATCORR";
-    $command .= "-file_level $fileLevel";
-    $command .= "-workdir $workdir";
-    $command .= "-inst $inst";
-    $command .= " -dbname $dbname" if defined $dbname;
-    ## XXX what else do we need, and where do we get it?
-
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	cache_run(command => $command, verbose => 1);
+    $command .= " -det_type FLATCORR";
+    $command .= " -filelevel $filelevel";
+    $command .= " -workdir $workdir";
+    $command .= " -inst $camera";
+    $command .= " -telescope $telescope";
+    $command .= " -filter $filter";
+    $command .= " -dbname $dbname" if defined $dbname;
+
+    ## the flat-field correction is valid for any airmass, exptime, solangle
+    ## for now, we assume the posangle and ccd_temp are not important
+    ## XXX someone needs to set the use_begin, use_end values??
+    ## XXX inherit a label from the flatcorrRun?
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
 
     unless ($success) { 
@@ -173,7 +215,6 @@
         &my_die("Unable to parse metadata list", $corr_id, $PS_EXIT_PROG_ERROR);
 
-    my $file = $$output[0];
-
-    $det_id = $file->{det_id};
+    # $file = $$output[0];
+    $det_id = $$output[0]->{det_id};
 }
 
@@ -184,10 +225,15 @@
     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);
+    my $uri = $ipprc->filename("DVOCORR.OUTPUT", $outcorr, $class_id);
+    unless ($uri) {
+	&my_die ("Unable to find DVOCORR.OUTPUT in filerules", $corr_id, $PS_EXIT_PROG_ERROR);
+    }
+
+    my $command = "echo $dvoMakeCorr $outcorr";
+    $command .= " -file $outgrid";
+    $command .= " -ref $reffile";
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
 
     unless ($success) { 
@@ -197,5 +243,5 @@
 
     # register the detrend correction imfile
-    my $command = "$dettool -register_detrend_imfile";
+    $command = "$dettool -register_detrend_imfile";
     $command .= " -det_id $det_id";
     $command .= " -class_id $class_id";
@@ -203,6 +249,6 @@
     $command .= " -dbname $dbname" if defined $dbname;
 
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	cache_run(command => $command, verbose => 1);
+    ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
 
     unless ($success) { 
@@ -212,13 +258,14 @@
 }
 
-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) {
+# set the detrun state to 'stop'
+{
+    my $command = "$dettool -updatedetrun";
+    $command .= " -det_id $det_id";
+    $command .= " -state stop";
+    $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);
@@ -226,23 +273,25 @@
         exit($error_code);
     }
-} else {
-    print "skipping command: $command\n";
-}
-
-my $command = "$flatcorr -addprocess";
-$command .= " -corr_id $corr_id";
-$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 regtool -addprocessedimfile: $error_code");
-        exit($error_code);
-    }
-} else {
-    print "skipping command: $command\n";
+{ 
+    my $command = "$flatcorr -addprocess";
+    $command .= " -corr_id $corr_id";
+    $command .= " -hostname $host" if defined $host;
+    $command .= " -dbname $dbname" if defined $dbname;
+
+    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 regtool -addprocessedimfile: $error_code");
+	    exit($error_code);
+	}
+    } else {
+	print "skipping command: $command\n";
+    }
 }
 
@@ -250,17 +299,13 @@
 {
     my $msg = shift; # Warning message on die
-    my $dvo_id    = shift;
-    my $region    = shift;
-    my $last_step = shift;
-    my $status 	  = shift;
-    my $dbname 	  = shift;
+    my $corr_id    = shift;
+    my $exit_code  = shift;
 
     carp($msg);
-    if (defined $dvo_id && defined $region && defined $last_step && defined $status and not $no_update) {
-        my $command = "$caltool -addcalrun";
-	$command .= " -dvo_id $dvo_id";
-        $command .= " -region $region";
-	$command .= " -last_step $last_step";
-	$command .= " -status $status";
+    if (not $no_update) {
+        my $command = "$flatcorr -addprocess";
+	$command .= " -corr_id $corr_id";
+        $command .= " -code $exit_code";
+        $command .= " -hostname $host" if defined $host;
         $command .= " -dbname $dbname" if defined $dbname;
         system ($command);
