Index: trunk/ippScripts/scripts/warp_overlap.pl
===================================================================
--- trunk/ippScripts/scripts/warp_overlap.pl	(revision 12800)
+++ trunk/ippScripts/scripts/warp_overlap.pl	(revision 12863)
@@ -26,4 +26,5 @@
     $PS_EXIT_DATA_ERROR
     $PS_EXIT_TIMEOUT_ERROR
+    _mdLookupStr
     );
 my $ipprc = PS::IPP::Config->new(); # IPP configuration
@@ -49,4 +50,6 @@
 ) unless defined $warp_id
     and defined $camera;
+
+$ipprc->define_camera($camera);
 
 # Look for programs we need
@@ -88,14 +91,29 @@
 }
 
+# determine the imfile/skycell overlaps
+my @overlaps = ();
+
 unless ($no_op) {
     ## Calculate the overlaps between imfiles and skycells
 
-    # XXX where does the catdir come from (recipe file for pswarp?)
     # the tess_id is the catdir used to define the tessalation
-    $tess_id = $catdir;
+    my $command = "ppConfigDump -camera $camera -dump-recipe PSWARP -";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => 1);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to perform ppConfigDump: $error_code", $error_code);
+    }
+    my $parser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
+    my $recipe = $parser->parse( join '', @$stdout_buf);
+    my $tess_id = _mdLookupStr($recipe, 'DVO.TESSALATION');
+    print STDERR "tessalation: $tess_id\n";
+
     # we have two options for the astrometry file: 
     # - saved by chiptool, in which case there may be a different one for each imfile
     # - saved by camtool, in which case there may only be a single astrom file
     my $psastroFile;
+
+    # XXX select this value from the recipe files (psastro? maybe not...?)
     my $camAstrom = 1;
     if ($camAstrom) {
@@ -103,4 +121,8 @@
 	my $camRoot = $ipprc->convert_filename_absolute( $imfile->{cam_path_base} );
         $psastroFile = $ipprc->filename("PSASTRO.OUTPUT", $camRoot); # MEF psastro output
+
+	my @dirlist = File::Spec->splitdir( $psastroFile );
+	my $psastroRootFile = pop @dirlist;
+	print STDERR "psastroRootFile: $psastroRootFile\n";
 
 	# run the dvoImageOverlaps program to get the overlaps with this image 
@@ -112,5 +134,11 @@
 	    &my_die("Unable to perform dvoImageOverlaps: $error_code", $warp_id, $error_code);
 	}
-	my @matchlist = split ('\n', $stdout_buf);
+	my @matchlist = split ('\n', (join "", @$stdout_buf));
+
+	#my $Nmatch = @matchlist;
+	#print STDERR "Nmatch: $Nmatch\n";
+	#foreach my $line (@matchlist) {
+	#    print STDERR "line: $line\n";
+	#}
 
 	# dvoImageOverlaps -D CATDIR tessalation (megacam) :: returns:
@@ -120,25 +148,51 @@
 
 	# now match the imfiles to this list
-	my @overlaps = ();
-	foreach $imfile (@$imfiles) {
-	    my $class_id = $imfile->{class_id};
-	    my $chipRoot = $ipprc->convert_filename_absolute( $imfile->{chip_path_base} );
-	    my $extname = $ipprc->extname_rule("CMF.HEAD", $class_id); # MEF psastro output
-	    
-	    my $entry = "$psastroFile\[$extname\]";
+	my @unique_skycells = ();
+	my %found_skycells = ();
+	foreach my $imfile (@$imfiles) {
+	    my $fileLevel = $imfile->{filelevel};
+	    my $entry = "";
+	    if ($fileLevel eq "chip") {
+		my $class_id = $imfile->{class_id};
+		my $chipRoot = $ipprc->convert_filename_absolute( $imfile->{chip_path_base} );
+		my $extname = $ipprc->extname_rule("CMF.HEAD", $class_id); # MEF psastro output
+		
+		print STDERR "class: $class_id, chiproot: $chipRoot, extname: $extname\n";
+
+		$entry = "$psastroRootFile\[$extname\]";
+	    } else {
+		$entry = "$psastroRootFile";
+	    }
 	    my @skycells = &select_skycells ($entry, @matchlist);
-	    foreach $skycell (@skycells) {
+	    foreach my $skycell (@skycells) {
 		my %overlap = ();
 		$overlap{skycell_id} = $skycell;
-		$overlap{tess_id}    = $catdir;
+		$overlap{tess_id}    = $tess_id;
 		$overlap{cam_id}     = $imfile->{cam_id};
 		$overlap{class_id}   = $imfile->{class_id};
 		$overlap{fault}      = $imfile->{fault};
-		push @overlaps \$overlap;
+		push @overlaps, \%overlap;
+
+		printf STDERR "overlap: %s : %s\n", $skycell, $imfile->{cam_id};
+
+		# generate a unique list of the skycells
+		unless ($found_skycells{$skycell}) {
+		    push @unique_skycells, $skycell;
+		    $found_skycells{$skycell} = 1;
+		}
 	    }
 	}
+
+	# extract the skycells to names of the form $camRoot.skycell...
+	# with calls to dvoImageExtract 
+	my $skycellDir = "$camRoot/skycells";
+	system "mkdir -p $skycellDir" unless -d $skycellDir;
+
+	foreach my $skycell (@unique_skycells) {
+	    vsystem ("dvoImageExtract -D CATDIR $tess_id $skycell -o $skycellDir/$skycell");
+	}
+
     } else {
-	my @overlaps = ();
-	foreach $imfile (@$imfiles) {
+	foreach my $imfile (@$imfiles) {
 	    my $class_id = $imfile->{class_id};
 	    my $chipRoot = $ipprc->convert_filename_absolute( $imfile->{chip_path_base} );
@@ -163,12 +217,14 @@
 	    my @skycells = &select_skycells ($psastroFile, @matchlist);
 	    # XXX should I check and warn if int(@skycells) != int(@matchlist) ?
-	    foreach $skycell (@skycells) {
+	    foreach my $skycell (@skycells) {
 		my %overlap = ();
 		$overlap{skycell_id} = $skycell;
-		$overlap{tess_id}    = $catdir;
+		$overlap{tess_id}    = $tess_id;
 		$overlap{cam_id}     = $imfile->{cam_id};
 		$overlap{class_id}   = $imfile->{class_id};
 		$overlap{fault}      = $imfile->{fault};
-		push @overlaps \$overlap;
+		push @overlaps, \%overlap;
+
+		print STDERR "overlap: $skycell -> $imfile->{cam_id}\n";
 	    }
 	}
@@ -176,5 +232,4 @@
 } else {
     # create an overlap with an entry for each skycell:imfile match
-    my @overlaps = ();
     foreach my $imfile (@$imfiles) {
 	my %overlap = ();
@@ -184,5 +239,5 @@
 	$overlap{class_id}   = $imfile->{class_id};
 	$overlap{fault}      = $imfile->{fault};
-	push @overlaps \$overlap;
+	push @overlaps, \%overlap;
     }
 }
@@ -247,13 +302,20 @@
     my @list = @_;
 
+    my $skycell;
     my @skycells = ();
 
     foreach my $line (@list) {
 	if ($line =~ m|$entry|) {
-	    ($skycell) = $line =~ m|$entry\s+:\s+(\S+)|;
+	    ($skycell) = $line =~ m|$entry\S*\s+:\s+(\S+)|;
 	    push @skycells, $skycell;
 	}
     }
-    return @skycells;
+    @skycells;
+}
+
+sub vsystem {
+    print STDERR "@_\n";
+    my $status = system ("@_");
+    $status;
 }
 
