Index: trunk/ippScripts/scripts/background_chip.pl
===================================================================
--- trunk/ippScripts/scripts/background_chip.pl	(revision 35117)
+++ trunk/ippScripts/scripts/background_chip.pl	(revision 35118)
@@ -14,4 +14,5 @@
 use DateTime;
 my $mjd_start = DateTime->now->mjd;   # MJD of starting script
+use File::Basename;
 
 use vars qw( $VERSION );
@@ -123,4 +124,5 @@
 my $in_image = $ipprc->filename("PPIMAGE.CHIP", $in_path, $class_id);
 my $in_mask = $ipprc->filename("PPIMAGE.CHIP.MASK", $in_path, $class_id);
+my $in_wt = $ipprc->filename("PPIMAGE.CHIP.VARIANCE", $in_path, $class_id);
 my $in_bg = $ipprc->filename("PSPHOT.BACKMDL", $in_path, $class_id);
 my $in_pattern = $ipprc->filename("PPIMAGE.PATTERN", $in_path, $class_id);
@@ -148,7 +150,11 @@
 }
 
+# XXX: M31 TEST turn off restoring pattern correction
+$apply_pattern = 0;
+
 # Set up files
 &my_die("Couldn't find input file: $in_image\n", $chip_bg_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($in_image);
 &my_die("Couldn't find input file: $in_mask\n", $chip_bg_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($in_mask);
+&my_die("Couldn't find input file: $in_wt\n", $chip_bg_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($in_wt);
 &my_die("Couldn't find input file: $in_bg\n", $chip_bg_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($in_bg) or !$apply_bg;
 &my_die("Couldn't find input file: $in_pattern\n", $chip_bg_id, $class_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($in_pattern) or !$apply_pattern;
@@ -163,7 +169,76 @@
 my $out_image = prepare_output("PPBACKGROUND.OUTPUT", $outroot, $class_id, 1);
 my $out_mask = prepare_output("PPBACKGROUND.OUTPUT.MASK", $outroot, $class_id, 1);
+my $out_wt = prepare_output("PPBACKGROUND.OUTPUT.VARIANCE", $outroot, $class_id, 1);
 my $out_stats = prepare_output("PPBACKGROUND.STATS", $outroot, $class_id, 1);
 my $out_config = prepare_output("PPBACKGROUND.CONFIG", $outroot, $class_id, 1);
 my $traceDest = prepare_output("TRACE.IMFILE", $outroot, $class_id, 1);
+
+my $auxmask;
+
+my $apply_auxiliary_mask = 1;
+if ($apply_auxiliary_mask) {
+    my @auxmask_start_date = qw(
+    0000
+    5178
+    5218
+    5403
+    5424
+    5455
+    5485
+    5512
+    5545
+    5770
+    5797
+    5825
+    5854
+    5881
+    6138
+    6153
+    6197
+    6214
+    );
+    my @auxmask_end_date = qw(
+    5177
+    5217
+    5402
+    5423
+    5454
+    5484
+    5511
+    5544
+    5769
+    5796
+    5824
+    5853
+    5880
+    6137
+    6152
+    6196
+    6213
+    9999
+    );
+
+    my $outroot_base = basename($outroot);
+
+    # Assume that outroot base begins with exp_name: fix by passing in exp_name
+    my $mjd = substr($outroot_base, 1, 4);
+    if ($mjd =~ /\D/) {
+            my $error_code = 1;
+            &my_die("unexpected outroot base: $outroot_base: $error_code", $chip_bg_id, $class_id, $error_code);
+    }
+
+    my $i = 0;
+    while ($auxmask_end_date[$i] < $mjd) {
+        $i++;
+    }
+
+    my $auxmask_base = "/data/ipp064.0/bills/m31-masks/www.usm.uni-muenchen.de/people/arri/PAndromeda/masks_fits/mask";
+
+    $auxmask = sprintf "%s_%04d-%04d_%s.fits", $auxmask_base, $auxmask_start_date[$i], $auxmask_end_date[$i], $class_id;
+    print "Auxililary mask file is $auxmask\n";
+}
+    
+
+
 
 # Run ppImage
@@ -172,4 +247,6 @@
     $command .= " -image $in_image";
     $command .= " -mask $in_mask";
+    $command .= " -variance $in_wt";
+    $command .= " -auxmask $auxmask" if $auxmask;
     $command .= " -stats $out_stats" if $do_stats;
     $command .= " -background $in_bg" if $apply_bg;
Index: trunk/ippScripts/scripts/background_warp.pl
===================================================================
--- trunk/ippScripts/scripts/background_warp.pl	(revision 35117)
+++ trunk/ippScripts/scripts/background_warp.pl	(revision 35118)
@@ -29,4 +29,5 @@
 my $missing_tools;
 my $bgtool = can_run('bgtool') or (warn "Can't find bgtool" and $missing_tools = 1);
+my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
 my $pswarp = can_run('pswarp') or (warn "Can't find pswarp" and $missing_tools = 1);
 my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
@@ -38,5 +39,5 @@
 
 my ($warp_bg_id, $skycell_id, $tess_dir, $reduction, $camera, $dbname, $outroot, $threads, $verbose, $no_update, $no_op, $redirect, $save_temps);
-my ($astrometry, $imageName, $maskName, $magicked);
+my ($astrometry, $imageName, $maskName, $weightName, $magicked);
 GetOptions(
     'warp_bg_id|i=s'      => \$warp_bg_id, # Warp identifier
@@ -117,9 +118,32 @@
 # Get list of filenames
 my $tempOutRoot = "/tmp/background.warp.$warp_bg_id.$skycell_id";
+
+# XXX: get this from recipe
+my $alternate_astrometry_label = 'M31.test.20130206.bgsub';
+# XXX: get exp_id from command line
+my $exp_id = 196758;
+
 if (!defined $imageName) {
     # go find our inputs
-    my ($imageFile, $maskFile);
+    my $astrom_path_base;
+    if (defined $alternate_astrometry_label) {
+        my $command = "$camtool -processedexp -exp_id $exp_id -label $alternate_astrometry_label";
+        $command .= " -dbname $dbname" if $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 -processedexp: $error_code", $warp_bg_id, $error_code);
+        }
+        my $entries = $mdcParser->parse_list(join "", @$stdout_buf) or
+            &my_die("Unable to parse metadata config doc", $warp_bg_id, $PS_EXIT_PROG_ERROR);
+        my $entry = $entries->[0];
+        $astrom_path_base = $entry->{path_base};
+    }
+
+    my ($imageFile, $maskFile, $weightFile);
     ($imageFile, $imageName) = tempfile( "$tempOutRoot.image.list.XXXX",  UNLINK => !$save_temps);
     ($maskFile, $maskName) = tempfile( "$tempOutRoot.mask.list.XXXX",   UNLINK => !$save_temps);
+    ($weightFile, $weightName) = tempfile( "$tempOutRoot.wt.list.XXXX",   UNLINK => !$save_temps);
     my $command = "$bgtool -warpinputs";
     $command .= " -warp_bg_id $warp_bg_id";
@@ -139,19 +163,28 @@
         my $image = $ipprc->filename("PPBACKGROUND.OUTPUT", $chip_path, $class_id);
         my $mask = $ipprc->filename("PPBACKGROUND.OUTPUT.MASK", $chip_path, $class_id );
+        my $wt = $ipprc->filename("PPBACKGROUND.OUTPUT.VARIANCE", $chip_path, $class_id );
         print $imageFile "$image\n";
         print $maskFile "$mask\n";
+        print $weightFile "$wt\n";
         &my_die("Can't find input image: $image", $warp_bg_id, $skycell_id, $PS_EXIT_PROG_ERROR) unless $ipprc->file_exists($image);
         &my_die("Can't find input mask: $mask", $warp_bg_id, $skycell_id, $PS_EXIT_PROG_ERROR) unless $ipprc->file_exists($mask);
+        &my_die("Can't find input mask: $wt", $warp_bg_id, $skycell_id, $PS_EXIT_PROG_ERROR) unless $ipprc->file_exists($wt);
 
         &my_die("Magic status don't match: $magicked vs $file->{magicked}", $warp_bg_id, $skycell_id, $PS_EXIT_PROG_ERROR) if defined $magicked and $magicked != $file->{magicked};
         $magicked = $file->{magicked};
 
-        my $cam_path = $file->{cam_path_base};
-        my $astrom = $ipprc->filename($astromSource, $cam_path);
-        &my_die("Astrometry files don't match: $astrom vs $astrometry", $warp_bg_id, $skycell_id, $PS_EXIT_PROG_ERROR) if defined $astrometry and $astrom ne $astrometry;
-        $astrometry = $astrom;
+        if (!$astrom_path_base) {
+            my $cam_path = $file->{cam_path_base};
+            my $astrom = $ipprc->filename($astromSource, $cam_path);
+            &my_die("Astrometry files don't match: $astrom vs $astrometry", $warp_bg_id, $skycell_id, $PS_EXIT_PROG_ERROR) if defined $astrometry and $astrom ne $astrometry;
+            $astrometry = $astrom;
+        } elsif (!$astrometry) {
+            # using alternate astrometry
+            $astrometry = $ipprc->filename($astromSource, $astrom_path_base);
+        }
     }
     close $imageFile;
     close $maskFile;
+    close $weightFile;
 }
 
@@ -166,4 +199,5 @@
 my $out_image = prepare_output("PSWARP.OUTPUT", $outroot, $skycell_id,  1);
 my $out_mask = prepare_output("PSWARP.OUTPUT.MASK", $outroot, $skycell_id, 1);
+my $out_wt = prepare_output("PSWARP.OUTPUT.VARIANCE", $outroot, $skycell_id, 1);
 my $out_stats = prepare_output("SKYCELL.STATS", $outroot, $skycell_id, 1);
 my $out_config = prepare_output("PSWARP.CONFIG", $outroot, $skycell_id, 1);
@@ -179,4 +213,5 @@
     $command .= " -list $imageName";
     $command .= " -masklist $maskName";
+    $command .= " -variancelist $weightName";
     $command .= " -astrom $astrometry";
     $command .= " $outroot $skyFile";
@@ -220,4 +255,5 @@
         &my_die("Couldn't find expected output file: $out_image", $warp_bg_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($out_image);
         &my_die("Couldn't find expected output file: $out_mask", $warp_bg_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($out_mask);
+        &my_die("Couldn't find expected output file: $out_wt", $warp_bg_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($out_wt);
         &my_die("Couldn't find expected output file: $out_config", $warp_bg_id, $skycell_id, $PS_EXIT_SYS_ERROR) unless $ipprc->file_exists($out_config);
     }
