Index: trunk/ippScripts/scripts/magic_tree.pl
===================================================================
--- trunk/ippScripts/scripts/magic_tree.pl	(revision 18784)
+++ trunk/ippScripts/scripts/magic_tree.pl	(revision 20483)
@@ -33,8 +33,9 @@
 
 # Parse the command-line arguments
-my ($magic_id, $tess_id, $camera, $ra0, $dec0, $dbname, $outroot,
+my ($magic_id, $warp_id, $tess_id, $camera, $ra0, $dec0, $dbname, $outroot,
     $save_temps, $verbose, $no_update, $no_op, $redirect);
 GetOptions(
            'magic_id=s'      => \$magic_id,   # Magic identifier
+           'warp_id=s'       => \$warp_id,    # id for corresponding warps
            'tess_id=s'       => \$tess_id,    # Tessellation identifier
            'camera=s'        => \$camera,     # Camera name
@@ -51,7 +52,8 @@
 
 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-pod2usage( -msg => "Required options: --magic_id --camera --tess_id --ra --dec --outroot",
+pod2usage( -msg => "Required options: --magic_id --warp_id --camera --tess_id --ra --dec --outroot",
            -exitval => 3) unless
     defined $magic_id and
+    defined $warp_id and
     defined $tess_id and
     defined $ra0 and
@@ -69,4 +71,5 @@
 my $missing_tools;
 my $magictool = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);
+my $warptool = can_run('warptool') or (warn "Can't find warptool" and $missing_tools = 1);
 if ($missing_tools) {
     warn("Can't find required tools.");
@@ -76,4 +79,29 @@
 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
 
+### Get a list of warpSkyfiles
+my %warpSkyfiles;                   # hash of warps
+{
+    my $command = "$warptool -warped -warp_id $warp_id"; # Command to run
+    $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 warptool -warped: $error_code", $magic_id, $error_code);
+    }
+
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+        &my_die("Unable to parse metadata config doc", $magic_id, $PS_EXIT_PROG_ERROR);
+
+    my $warps = parse_md_list($metadata) or
+        &my_die("Unable to parse metadata list", $magic_id, $PS_EXIT_PROG_ERROR);
+
+    # make a hash indexed by skycell_id 
+    foreach my $warp ( @$warps ) {
+        my $skycell_id = $warp->{skycell_id};
+        die "failed to lookup skycell_id from warp" if !$skycell_id;
+        $warpSkyfiles{$skycell_id} = $warp;
+    }
+}
 ### Get a list of skycells
 my @skycells;                   # List of skycells
@@ -102,6 +130,17 @@
 my @fields;
 foreach my $skycell_id ( @skycells ) {
-    my $skyfile = $ipprc->filename("SKYCELL.TEMPLATE", $outroot, $skycell_id );
-    $ipprc->skycell_file($tess_id, $skycell_id, $skyfile, $verbose) or &my_die("Unable to generate skycells $skycell_id", $magic_id, $PS_EXIT_PROG_ERROR);
+    # the filename method doesn't add the $skycell_id
+
+    # my $skyfile = $ipprc->filename("SKYCELL.TEMPLATE", $outroot, $skycell_id );
+    #my $skyfile = $ipprc->filename("SKYCELL.TEMPLATE", $outroot, $skycell_id );
+
+#    my $skycellBase = "$outroot/$skycell_id";
+#    my $skyfile = $ipprc->filename("SKYCELL.TEMPLATE", $skycellBase);
+#    $ipprc->skycell_file($tess_id, $skycell_id, $skyfile, $verbose) or &my_die("Unable to generate skycells $skycell_id", $magic_id, $PS_EXIT_PROG_ERROR);
+
+    my $warp = $warpSkyfiles{$skycell_id};
+    die "warpSkyfile for $skycell_id not found" if !$warp;
+    my $skyfileBase = $warp->{path_base};
+    my $skyfile = $ipprc->filename("SKYCELL.TEMPLATE", $skyfileBase);
     my $skyfileResolved = $ipprc->file_resolve( $skyfile );
     my ($header, $status) = Astro::FITS::CFITSIO::fits_read_header( $skyfileResolved );
@@ -181,8 +220,8 @@
 };
 my @tasks = ( $root );
-while (scalar @tasks > 1) {
+while (scalar @tasks) {
     my $node = shift @tasks;
     divide_node($node, \@tasks);
-}
+} 
 
 ### Format tree for magictool
@@ -193,5 +232,5 @@
 
 ### Input tree into database
-{
+if (!$no_update) {
     my $command = "$magictool -inputtree";
     $command   .= " -magic_id $magic_id";
@@ -241,4 +280,6 @@
     my @upper = splice(@sorted, $median); # Upper part of the sorted list
 
+#    print " median $median\n";
+
     return (\@sorted, \@upper);
 }
@@ -257,4 +298,7 @@
         children => {},
     };
+
+#    my $n = scalar @$contents;
+#    print "new node: $node->{position}: $n\n";
 
     $parent->{children}->{$position} = $node;
