Index: branches/ipp-magic-v0/tools/collapse_diffinputs.pl
===================================================================
--- branches/ipp-magic-v0/tools/collapse_diffinputs.pl	(revision 21062)
+++ branches/ipp-magic-v0/tools/collapse_diffinputs.pl	(revision 21062)
@@ -0,0 +1,124 @@
+#!/usr/bin/env perl
+#
+# 
+# script to convert existing diffRun and diffInputSkyfiles rows 
+# From version 1.1.46 of the schema to 1.1.47
+#
+# Assumes that the changes up to the diffInputSkyfile columns have been
+# made up to the place where it says that this script should be run
+
+use strict;
+use warnings;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+
+use DBI;
+
+my $dbname = shift;
+my $dbuser = shift;
+my $dbpass = shift;
+my $dbhost = shift;
+die "usage: $0 dbname dbuser dbpassword dbhost\n" unless $dbname and $dbuser and $dbpass and $dbhost;
+
+my $dbh  = getDBHandle($dbname, $dbuser, $dbpass, $dbhost);
+
+# find all exposures in summitExp
+# my $query = "SELECT * FROM diffInputSkyfile where template = 1";
+
+my $query = "
+select
+    diff_id,
+    warp1,
+    stack1,
+    warp2,
+    stack2,
+    skycell_id
+    FROM
+        (select
+            diff_id,
+            warp1 as warp1,
+            stack2 as stack1,
+            skycell_id as skycell_id
+        from diffInputSkyfile
+        WHERE template = 0
+        ) as inputs
+    JOIN
+        (select
+            diff_id,
+            warp1 as warp2,
+            stack2 as stack2,
+            skycell_id as stack_skycell
+        from diffInputSkyfile
+        WHERE template = 1
+        ) as templates
+    USING (diff_id)
+";
+
+my $stmt = $dbh->prepare($query);
+$stmt->execute();
+
+
+my $sub_query .= "(SELECT exp_id FROM diffInputSkyfile JOIN warpRun on diffInputSkyfile.warp1 = warpRun.warp_id join fakeRun using(fake_id) join camRun using(cam_id) join chipRun using(chip_id) where diff_id = ? )";
+
+my $exp_query = "UPDATE diffRun SET exp_id = $sub_query where diff_id = ?";
+
+while (my $ref = $stmt->fetchrow_hashref()) {
+    my $diff_id = $ref->{diff_id};
+    my $warp1 = $ref->{warp1};
+    my $warp2 = $ref->{warp2};
+    my $stack1 = $ref->{stack1};
+    my $stack2 = $ref->{stack2};
+    my $skycell_id = $ref->{skycell_id};
+
+    # reorganizing columns puts warp1 and stack 1 correctly for template.
+    # set all of the stack and warp fields in the template = 0 input
+    # (the template = 1 row will be deleted)
+    if ($warp1 and $stack2) {
+        # usual case warp - stack
+        $dbh->do("UPDATE diffInputSkyfile SET stack2 = $stack2  where diff_id = $diff_id AND template = 0");
+    } elsif ($stack1 and $stack2) {
+        # stack - stack
+        $dbh->do("UPDATE diffInputSkyfile SET stack1 = $stack1, stack2 = $stack2  where diff_id = $diff_id AND template = 0");
+    } elsif ($warp1 and $warp2) {
+        # warp - warp
+        $dbh->do("UPDATE diffInputSkyfile SET warp2 = $warp2  where diff_id = $diff_id AND template = 0");
+    } elsif ($stack1 and $warp2) {
+        # stack - warp
+        $dbh->do("UPDATE diffInputSkyfile SET stack1 = $stack1, warp2 = $warp2  where diff_id = $diff_id AND template = 0");
+    } else {
+        die "unexpected input set for $diff_id";
+    }
+
+    # set skycell_id in any existing diffSkyfile
+    $dbh->do("UPDATE diffSkyfile set skycell_id = ? where diff_id = ?", undef, $skycell_id, $diff_id);
+
+    #
+    # set the exp_id for the diffRun if the input is a warp
+    #
+    if ($warp1) {
+        my $rows = $dbh->do($exp_query, undef, $diff_id, $diff_id);
+        die "failed to update diffRun.exp_id for $diff_id" if ($rows != 1);
+    }
+}
+
+exit 0;
+
+
+sub getDBHandle {
+    my $dbname   = shift;
+    my $dbuser   = shift;
+    my $dbpass   = shift;
+    my $dbserver = shift;
+
+    die "database environment not set up" unless defined($dbserver) and defined($dbuser)
+        and defined($dbpass) and defined($dbname);
+
+    my $dsn = "DBI:mysql:host=$dbserver;database=$dbname";
+
+    my $dbh = DBI->connect($dsn, $dbuser, $dbpass) or die "Cannot connect to server\n";
+
+    return $dbh;
+}
+
Index: branches/ipp-magic-v0/tools/diff_inputs.pl
===================================================================
--- branches/bills_081204/tools/diff_inputs.pl	(revision 20890)
+++ branches/ipp-magic-v0/tools/diff_inputs.pl	(revision 21062)
@@ -26,5 +26,5 @@
 
 my ($db_host, $db_name, $db_user, $db_pw); # Database details
-my ($diff_id);                  # Diff of interest
+my ($diff_id, $skycell_id);     # Diff and skycell of interest
 my ($input);                    # Input list
 
@@ -35,4 +35,5 @@
            'dbpass=s' => \$db_pw, # Database p/w
            'diff_id=s' => \$diff_id, # Diff identifier
+           'skycell_id=s' => \$skycell_id, # Skycell identifier
            'input=s'  => \$input, # Input list to generate
            ) or die "Unable to parse arguments.\n";
@@ -57,15 +58,22 @@
     "     NULL AS stack_path_base" .
     " FROM diffRun" .
-    " JOIN diffInputSkyfile USING(diff_id, skycell_id)" .
-    " JOIN warpSkyfile USING(warp_id, skycell_id)" .
-    " WHERE diff_id = $diff_id" .
-    " UNION" .
+    " JOIN diffInputSkyfile USING(diff_id)" .
+    " JOIN warpSkyfile" .
+    "     ON warpSkyfile.skycell_id = diffInputSkyfile.skycell_id" .
+    "     AND (warpSkyfile.warp_id = diffInputSkyfile.warp1" .
+    "         OR warpSkyfile.warp_id = diffInputSkyfile.warp2)" .
+    " WHERE diff_id = $diff_id";
+$sql .= " AND diffInputSkyfile.skycell_id = $skycell_id" if defined $skycell_id;
+$sql.= " UNION" .
     " SELECT" .
     "     NULL AS warp_path_base," .
     "     stackSumSkyfile.path_base AS stack_path_base" .
     " FROM diffRun" .
-    " JOIN diffInputSkyfile USING(diff_id, skycell_id)" .
-    " JOIN stackSumSkyfile USING(stack_id)" .
+    " JOIN diffInputSkyfile USING(diff_id)" .
+    " JOIN stackSumSkyfile" .
+    "     ON (stackSumSkyfile.stack_id = diffInputSkyfile.stack1" .
+    "         OR stackSumSkyfile.stack_id = diffInputSkyfile.stack2)" .
     " WHERE diff_id = $diff_id";
+$sql .= " AND diffInputSkyfile.skycell_id = $skycell_id" if defined $skycell_id;
 
 
Index: branches/ipp-magic-v0/tools/diff_outputs.pl
===================================================================
--- branches/bills_081204/tools/diff_outputs.pl	(revision 20890)
+++ branches/ipp-magic-v0/tools/diff_outputs.pl	(revision 21062)
@@ -18,8 +18,9 @@
                              'JPEG1' => '.b1.jpg', # Binned JPEG
                              'JPEG2' => '.b2.jpg', # Binned JPEG
+                             'KERNEL' => '.subkernel', # Convolution kernel
                          };
 
 my ($db_host, $db_name, $db_user, $db_pw); # Database details
-my ($diff_id);                  # Diff of interest
+my ($diff_id, $skycell_id);     # Diff and skycell of interest
 my ($input);                    # Input list
 my ($products);                 # Products of interest
@@ -31,4 +32,5 @@
            'dbpass=s' => \$db_pw, # Database p/w
            'diff_id=s' => \$diff_id, # Diff identifier
+           'skycell_id=s' => \$skycell_id, # Skycell identifier
            'products=s' => \$products, # Products of interest
            ) or die "Unable to parse arguments.\n";
@@ -50,4 +52,5 @@
 # Query to run
 my $sql = "SELECT path_base FROM diffSkyfile WHERE diff_id = $diff_id";
+$sql .= " AND skycell_id = $skycell_id" if defined $skycell_id;
 
 # List of diffs
Index: branches/ipp-magic-v0/tools/stack_outputs.pl
===================================================================
--- branches/bills_081204/tools/stack_outputs.pl	(revision 20890)
+++ branches/ipp-magic-v0/tools/stack_outputs.pl	(revision 21062)
@@ -16,7 +16,8 @@
                              'WEIGHT' => '.wt.fits', # Weight
                              'SOURCES' => '.cmf', # Sources
-			     'LOG' => '.log', # Log file
-			     'B1' => '.b1.jpg',	# Binned jpeg
-			     'B2' => '.b2.jpg',	# Binned jpeg
+                             'LOG' => '.log', # Log file
+                             'STATS' => '.stats', # Statistics file
+                             'B1' => '.b1.jpg', # Binned jpeg
+                             'B2' => '.b2.jpg', # Binned jpeg
                          };
 
