Index: /trunk/ippScripts/scripts/flatcorr_proc.pl
===================================================================
--- /trunk/ippScripts/scripts/flatcorr_proc.pl	(revision 25915)
+++ /trunk/ippScripts/scripts/flatcorr_proc.pl	(revision 25916)
@@ -42,5 +42,5 @@
 use Pod::Usage qw( pod2usage );
 
-my ($corr_id, $det_type, $dvodb, $camera, $region, $filter, $dbname, $workdir, $verbose, $no_update, $no_op);
+my ($corr_id, $det_type, $dvodb, $camera, $region, $filter, $dbname, $workdir, $make_correction, $verbose, $no_update, $no_op);
 GetOptions(
     'corr_id|i=s'      => \$corr_id,
@@ -52,5 +52,6 @@
     'dbname|d=s'       => \$dbname,# Database name
     'workdir|w=s'      => \$workdir, # Working directory for output files
-    'verbose'       	=> \$verbose,   # Print to stdout
+    'make_correction'  => \$make_correction,   # Generate the correction image and save to the detrend database
+    'verbose'          => \$verbose,   # Print to stdout
     'no-update'        => \$no_update,
     'no-op'            => \$no_op,
@@ -86,6 +87,5 @@
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
 
-# XXX either workdir needs to be non-nebulous here, or addstar / relphot need to handle 
-# nebulous interactions...
+# dvodb must be non-nebulous; workdir may be in nebulous
 my $outgrid = "$dvodb/flatcorr/$camera.$filter.$corr_id";
 my $outcorr = "$workdir/$camera.$filter.$corr_id";
@@ -143,136 +143,21 @@
 }
 
-# get a single input exposure
-# flatcorr -inputexp -corr_id $corr_id -limit 1
-my $chip_id;
-{
-    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);
-    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
-    my $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";
-    $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);
-    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;
-{
-    # get the filelevel from one of these chips
-    my $file = $$files[0];
-
-    my $filelevel = $file->{filelevel};
-    my $telescope = $file->{telescope};
-
-    my $command = "$dettool -register_detrend";
-    $command .= " -det_type $det_type";
-    $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 => $verbose);
-
-    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 $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);
-
-    # $file = $$output[0];
-    $det_id = $$output[0]->{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("DVOCORR.OUTPUT", $outcorr, $class_id);
-    unless ($uri) {
-	&my_die ("Unable to find DVOCORR.OUTPUT in filerules", $corr_id, $PS_EXIT_PROG_ERROR);
-    }
-
-    my $command = "$dvoMakeCorr $outcorr";
-    $command .= " -file $outgrid.fits";
-    $command .= " -ref $reffile";
-
-    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 dvoMakeCorr: $error_code", $corr_id, $PS_EXIT_PROG_ERROR);
-    }
-
-    # register the detrend correction imfile
-    $command = "$dettool -register_detrend_imfile";
-    $command .= " -det_id $det_id";
-    $command .= " -class_id $class_id";
-    $command .= " -uri $uri";
-    $command .= " -dbname $dbname" if defined $dbname;
-
-    ( $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 register new detrend: $error_code", $corr_id, $PS_EXIT_PROG_ERROR);
-    }
-}
-
-# set the detrun state to 'stop'
-{
+if ($make_correction) {
+
+    # get a single input exposure
+    # flatcorr -inputexp -corr_id $corr_id -limit 1
+    my $chip_id = &get_chip_id();
+
+    # get the list of imfiles for the single input exposure
+    # flatcorr -inputimfile -chip_id $chip_id
+    my $files = &get_imfiles($chip_id);
+
+    # set up the detrend run to store the corrected imfiles
+    my $det_id = &get_det_id($$files[0]);
+
+    # make the (full-sized) detrend correction images
+    &make_detrend_imfiles($det_id, $files);
+
+    # set the detrun state to 'stop'
     my $command = "$dettool -updatedetrun";
     $command .= " -det_id $det_id";
@@ -311,4 +196,144 @@
 }
 
+# get a single input exposure
+# flatcorr -inputexp -corr_id $corr_id -limit 1
+sub get_chip_id {
+
+    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);
+    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
+    my $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];
+    my $chip_id = $file->{chip_id};
+
+    return $chip_id;
+}
+
+# get the list of imfiles for the single input exposure
+# flatcorr -inputimfile -chip_id $chip_id
+sub get_imfiles {
+    my $chip_id = shift; 
+
+    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);
+    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
+    my $files = parse_md_list($metadata) or
+        &my_die("Unable to parse metadata list", $corr_id, $PS_EXIT_PROG_ERROR);
+
+    return $files;
+}
+
+# set up the detrend run to store the corrected imfiles
+sub get_det_id {
+    # get the filelevel from the supplied chip
+    my $file = shift;
+
+    my $filelevel = $file->{filelevel};
+    my $telescope = $file->{telescope};
+
+    my $command = "$dettool -register_detrend";
+    $command .= " -det_type $det_type";
+    $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 => $verbose);
+
+    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 $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);
+
+    # $file = $$output[0];
+    my $det_id = $$output[0]->{det_id};
+    return $det_id;
+}
+
+# use input chip images as reference images to make the detrend correction images
+sub make_detrend_imfiles {
+    my $det_id = shift;
+    my $files = shift;
+
+    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("DVOCORR.OUTPUT", $outcorr, $class_id);
+	unless ($uri) {
+	    &my_die ("Unable to find DVOCORR.OUTPUT in filerules", $corr_id, $PS_EXIT_PROG_ERROR);
+	}
+
+	my $command = "$dvoMakeCorr $outcorr";
+	$command .= " -file $outgrid.fits";
+	$command .= " -ref $reffile";
+
+	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 dvoMakeCorr: $error_code", $corr_id, $PS_EXIT_PROG_ERROR);
+	}
+
+	# register the detrend correction imfile
+	$command = "$dettool -register_detrend_imfile";
+	$command .= " -det_id $det_id";
+	$command .= " -class_id $class_id";
+	$command .= " -uri $uri";
+	$command .= " -dbname $dbname" if defined $dbname;
+
+	( $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 register new detrend: $error_code", $corr_id, $PS_EXIT_PROG_ERROR);
+	}
+    }
+}
+
 sub my_die
 {
