Index: trunk/ippScripts/scripts/background_chip.pl
===================================================================
--- trunk/ippScripts/scripts/background_chip.pl	(revision 34422)
+++ 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;
