Index: trunk/ippScripts/scripts/camera_exp.pl
===================================================================
--- trunk/ippScripts/scripts/camera_exp.pl	(revision 13824)
+++ trunk/ippScripts/scripts/camera_exp.pl	(revision 14115)
@@ -16,6 +16,7 @@
 use IPC::Cmd 0.36 qw( can_run run );
 use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::Stats;
 use PS::IPP::Metadata::List qw( parse_md_list );
-use Statistics::Descriptive;
+
 use File::Temp qw( tempfile );
 
@@ -34,7 +35,7 @@
 use Pod::Usage qw( pod2usage );
 
-my ($exp_id, $cam_id, $recipe, $camera, $dbname, $workdir, $dvodb, $no_update, $no_op);
+my ($exp_tag, $cam_id, $recipe, $camera, $dbname, $workdir, $reduction, $dvodb, $no_update, $no_op);
 GetOptions(
-	   'exp_id=s'          => \$exp_id, # Exposure identifier
+	   'exp_tag=s'          => \$exp_tag, # Exposure identifier
 	   'cam_id=s'          => \$cam_id, # Camtool identifier
 	   'recipe=s'          => \$recipe, # Recipe to use
@@ -42,4 +43,5 @@
 	   'dbname|d=s'        => \$dbname, # Database name
 	   'workdir|w=s'       => \$workdir, # Working directory
+	   'reduction=s'       => \$reduction, # Reduction class		       
 	   'dvodb|w=s'         => \$dvodb,  # output DVO database
 	   'no-update'         => \$no_update, # Update the database?
@@ -49,7 +51,7 @@
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
 pod2usage(
-    -msg => "Required options: --exp_id --cam_id --camera",
+    -msg => "Required options: --exp_tag --cam_id --camera",
     -exitval => 3,
-) unless defined $exp_id
+) unless defined $exp_tag
     and defined $cam_id
     and defined $camera;
@@ -57,7 +59,37 @@
 $ipprc->define_camera($camera);
 
-use constant RECIPE1 => 'PPIMAGE_J1'; # Recipe to use for ppImage to make JPEGs
-use constant RECIPE2 => 'PPIMAGE_J2'; # Recipe to use for ppImage to make JPEGs
-
+# Recipes to use based on reduction class
+$reduction = 'DEFAULT' unless defined $reduction;
+
+my $recipe1 = $ipprc->reduction($reduction, 'JPEG_BIN1'); # Recipe to use
+&my_die("Unrecognised JPEG recipe", $cam_id, $PS_EXIT_PROG_ERROR) unless defined $recipe1;
+
+my $recipe2 = $ipprc->reduction($reduction, 'JPEG_BIN2'); # Recipe to use
+&my_die("Unrecognised JPEG recipe", $cam_id, $PS_EXIT_PROG_ERROR) unless defined $recipe2;
+
+# values to extract from output metadata and the stats to calculate
+my $CHIPSTATS = 
+   [   
+       #          PPSTATS KEYWORD         STATISTIC          CHIPTOOL FLAG
+	{ name => "bg",             type => "mean",  flag => "-bg",            	dtype => "float" },
+	{ name => "bg_mean_stdev",  type => "stdev", flag => "-bg_mean_stdev", 	dtype => "float" },
+	{ name => "bg_stdev",       type => "rms",   flag => "-bg_stdev",      	dtype => "float" },  
+
+# these should be coming from the psastro results for Nimfile > 1, 
+	{ name => "sigma_ra",       type => "rms",   flag => "-sigma_ra",      	dtype => "float" },  
+	{ name => "sigma_dec",      type => "rms",   flag => "-sigma_dec",     	dtype => "float" },  
+	{ name => "n_stars",        type => "sum",   flag => "-n_stars",       	dtype => "int"   },  
+	{ name => "n_astrom",       type => "sum",   flag => "-n_astrom",      	dtype => "int"   },  
+
+	{ name => "fwhm",           type => "mean",  flag => "-fwhm",          	dtype => "float" },  
+
+# these are not defined for the database table camProcessedExp
+#	{ name => "ap_resid",       type => "mean",  flag => "-ap_resid",      	dtype => "float" },  
+#	{ name => "ap_resid_stdev", type => "rms",   flag => "-ap_resid_stdev", dtype => "float" },  
+#       { name => "fringe_0",       type => "mean",  flag => "-fringe_0",      	dtype => "float" },  
+#       { name => "fringe_1",       type => "rms",   flag => "-fringe_1",      	dtype => "float" },  
+#       { name => "fringe_0",       type => "stdev", flag => "-fringe_2",      	dtype => "float" },  
+   ];
+my $chipStats = PS::IPP::Metadata::Stats->new($CHIPSTATS); # Stats parser
 
 # Look for programs we need
@@ -90,47 +122,19 @@
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
 	&my_die("Unable to parse metadata config doc", $cam_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", $cam_id, $PS_EXIT_PROG_ERROR);
-}
-
-# Gather the statistics
-my ($bg, $bg_stdev, $bg_mean_stdev); # The statistics triplet
-{
-    my @backgrounds;		# Array of backgrounds in each component
-    my @variances;		# Array of variances for each component
-#    my @ra;			# Array of ra errors
-#    my @dec;			# Array of dec errors
-#    my @zp;			# Array of photometric zero points
-    foreach my $file (@$files) {
-	&my_die("Unable to find class id", $cam_id, $PS_EXIT_SYS_ERROR) unless defined $file->{class_id};
-	my $class_id = $file->{class_id};
-	&my_die("Unable to find bg for class_id=$class_id", $cam_id, $PS_EXIT_SYS_ERROR) unless defined $file->{bg};
-	&my_die("Unable to find bg_mean_stdev for class_id=$class_id", $cam_id, $PS_EXIT_SYS_ERROR) unless defined $file->{bg_mean_stdev};
-#	&my_die("Unable to find sigma_ra for class_id=$class_id", $cam_id, $PS_EXIT_SYS_ERROR) unless defined $file->{sigma_ra};
-#	&my_die("Unable to find sigma_dec for class_id=$class_id", $cam_id, $PS_EXIT_SYS_ERROR) unless defined $file->{sigma_dec};
-#	&my_die("Unable to find zp for class_id=$class_id", $cam_id, $PS_EXIT_SYS_ERROR) unless defined $file->{zp};
-	push @backgrounds, $file->{bg};
-	push @variances, $file->{bg_stdev}**2;
-    }
-
-    {
-	my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
-	$stats->add_data(@backgrounds);
-	$bg = ($stats->mean() or 'NAN');
-	$bg_mean_stdev = ($stats->standard_deviation() or 'NAN');
-    }
-    {
-	my $stats = Statistics::Descriptive::Sparse->new; # Statistics calculator
-	$stats->add_data(@variances);
-	$bg_stdev = (sqrt( $stats->mean() ) or 'NAN');
-    }
-}
-
-my ($list1File, $list1Name) = tempfile( "$exp_id.cam$cam_id.b1.list.XXXX", UNLINK => 1 ); # For binning 1
-my ($list2File, $list2Name) = tempfile( "$exp_id.cam$cam_id.b2.list.XXXX", UNLINK => 1 ); # For binning 2
-my ($list3File, $list3Name) = tempfile( "$exp_id.cam$cam_id.b3.list.XXXX", UNLINK => 1 ); # For astrometry
-
-my @means;			# Array of means
-my @stdevs;			# Array of stdevs
+
+    # extract the stats from the metadata
+    unless ($chipStats->parse($metadata)) {
+	&my_die("Unable to find all values in statistics output.\n", $cam_id, $PS_EXIT_PROG_ERROR);
+    }
+}
+
+my ($list1File, $list1Name) = tempfile( "$exp_tag.cm.$cam_id.b1.list.XXXX", UNLINK => 1 ); # For binning 1
+my ($list2File, $list2Name) = tempfile( "$exp_tag.cm.$cam_id.b2.list.XXXX", UNLINK => 1 ); # For binning 2
+my ($list3File, $list3Name) = tempfile( "$exp_tag.cm.$cam_id.b3.list.XXXX", UNLINK => 1 ); # For astrometry
+
 my $chipObjects; 
 foreach my $file (@$files) {
@@ -143,6 +147,4 @@
     print $list2File ($ipprc->filename("PPIMAGE.BIN2", $file->{path_base}, $class_id) . "\n");
     print $list3File ($chipObjects . "\n");
-    push @means, $file->{bg};
-    push @stdevs, $file->{bg_stdev};
 }
 close $list1File;
@@ -151,8 +153,9 @@
 
 # Output products
-$workdir = caturi( $workdir, $exp_id ) if defined $workdir;
-my $outputRoot = $ipprc->file_prepare( "$exp_id.cam$cam_id", $workdir, ${$files}[0]->{path_base} );
-my $jpeg1 = $ipprc->filename("PPIMAGE.JPEG1", $outputRoot); # Binned JPEG #1
-my $jpeg2 = $ipprc->filename("PPIMAGE.JPEG2", $outputRoot); # Binned JPEG #2
+$workdir = caturi( $workdir, $exp_tag ) if defined $workdir;
+my $outputRoot = $ipprc->file_prepare( "$exp_tag.cm.$cam_id", $workdir, ${$files}[0]->{path_base} );
+
+my $jpeg1      = $ipprc->filename("PPIMAGE.JPEG1", $outputRoot); # Binned JPEG #1
+my $jpeg2      = $ipprc->filename("PPIMAGE.JPEG2", $outputRoot); # Binned JPEG #2
 my $fpaObjects = $ipprc->filename("PSASTRO.OUTPUT.MEF", $outputRoot); # MEF psastro output
 
@@ -167,4 +170,5 @@
     # run psastro +mosastro on the set of chips
     # XXX note that this is wrong if imfiles are cells
+    # XXX add a ppStats call which will collect the astrometry stats
     if (scalar @$files > 1) {
 	my $command = "$psastro -list $list3Name $outputRoot +mosastro -chipastro " . 
@@ -202,5 +206,5 @@
     # Make the jpeg for binning 1
     {
-	my $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE " . RECIPE1; # Command to run
+	my $command = "$ppImage -list $list1Name $outputRoot -recipe PPIMAGE $recipe1"; # Command to run
 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	    run(command => $command, verbose => 1);
@@ -214,5 +218,5 @@
     # Make the jpeg for binning 2
     {
-	my $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE " . RECIPE2; # Command to run
+	my $command = "$ppImage -list $list2Name $outputRoot -recipe PPIMAGE $recipe2"; # Command to run
 	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	    run(command => $command, verbose => 1);
@@ -224,19 +228,16 @@
     }
 }
+
+my $fpaCommand = "$camtool -addprocessedexp";
+$fpaCommand .= " -cam_id $cam_id";
+$fpaCommand .= " -uri UNKNOWN";
+$fpaCommand .= " -path_base $outputRoot";
+$fpaCommand .= " -dbname $dbname" if defined $dbname;
+$fpaCommand .= $chipStats->cmdflags();
 
 # Add the result into the database
 unless ($no_update) {
-
-    my $command = "$camtool -addprocessedexp";
-    $command .= " -cam_id $cam_id -uri UNKNOWN -path_base $outputRoot";
-    $command .= " -bg $bg -bg_stdev $bg_stdev -bg_mean_stdev $bg_mean_stdev";
-    $command .= " -sigma_ra 0.0 -sigma_dec 0.0";
-    $command .= " -zp_mean 0.0 -zp_stdev 0.0";
-    $command .= " -fwhm 0.0 -fwhm_range 0.0";
-    $command .= " -n_stars 0 -n_extended 0 -n_cr 0 -n_astrom 0";
-    $command .= " -dbname $dbname" if defined $dbname;
-
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => 1);
+	run(command => $fpaCommand, verbose => 1);
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
@@ -244,4 +245,6 @@
 	exit($error_code);
     }
+} else {
+    print "skipping command: $fpaCommand\n";
 }
 
