Index: trunk/ippScripts/scripts/warp_overlap.pl
===================================================================
--- trunk/ippScripts/scripts/warp_overlap.pl	(revision 13085)
+++ trunk/ippScripts/scripts/warp_overlap.pl	(revision 13112)
@@ -91,11 +91,22 @@
 }
 
-# determine the imfile/skycell overlaps
+# Set output directory
+if (defined $workdir) {
+    $workdir = $ipprc->convert_filename_absolute( $workdir );
+} else {
+    my $example = $ipprc->convert_filename_absolute( $$imfiles[0]->{uri} );
+    my ($vol, $dir, $file) = File::Spec->splitpath( $example );
+    $workdir = $dir;
+}
+system "mkdir -p $workdir" unless -d $workdir;
+
+
+# Determine the imfile/skycell overlaps
 my @overlaps = ();
 
 unless ($no_op) {
-    ## Calculate the overlaps between imfiles and skycells
-
-    # the tess_id is the catdir used to define the tessalation
+    # Calculate the overlaps between imfiles and skycells
+
+    # The tess_id is the catdir used to define the tessalation
     my $command = "ppConfigDump -camera $camera -dump-recipe PSWARP -";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -107,23 +118,16 @@
     my $parser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
     my $recipe = $parser->parse( join '', @$stdout_buf);
-    my $tess_id = metadataLookupStr($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) {
+    my @tesselations = split(/\s+/, metadataLookupStr($recipe, 'DVO.TESSALATION')); # List of tesselations
+    foreach my $tess_id ( @tesselations ) {
+	print STDERR "tessalation: $tess_id\n";
+
 	my $imfile = $imfiles->[0];
 	my $camRoot = $ipprc->convert_filename_absolute( $imfile->{cam_path_base} );
-        $psastroFile = $ipprc->filename("PSASTRO.OUTPUT", $camRoot); # MEF psastro output
-
+	my $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 
 	my $command = "dvoImageOverlaps -D CATDIR $tess_id $psastroFile";
@@ -135,16 +139,10 @@
 	}
 	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:
+	
+	# The command "dvoImageOverlaps -D CATDIR tessalation (megacam)" returns:
 	# 729534pa.cmf[ccd00.hdr]  :  skycell.051.fits
 	# PSASTRO.OUTPUT[CMF.HEAD] : SKYCELL
 	# [CMF.HEAD] is optionally used for MEF files (SIMPLE or MOSAIC)
-
+	
 	# now match the imfiles to this list
 	my @unique_skycells = ();
@@ -159,5 +157,5 @@
 		
 		print STDERR "class: $class_id, chiproot: $chipRoot, extname: $extname\n";
-
+		
 		$entry = "$psastroRootFile\[$extname\]";
 	    } else {
@@ -173,7 +171,7 @@
 		$overlap{fault}      = $imfile->{fault};
 		push @overlaps, \%overlap;
-
+		
 		printf STDERR "overlap: %s : %s\n", $skycell, $imfile->{cam_id};
-
+		
 		# generate a unique list of the skycells
 		unless ($found_skycells{$skycell}) {
@@ -184,47 +182,15 @@
 	}
 
-	# 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 {
-	foreach my $imfile (@$imfiles) {
-	    my $class_id = $imfile->{class_id};
-	    my $chipRoot = $ipprc->convert_filename_absolute( $imfile->{chip_path_base} );
-	    my $psastroFile = $ipprc->filename("PSASTRO.OUTPUT", $chipRoot, $class_id); # SPLIT psastro output
-
-	    # run the dvoImageOverlaps program to get the overlaps with this image 
-	    my $command = "dvoImageOverlaps -D CATDIR $tess_id $psastroFile";
+	# Extract the skycells to images, used as warp templates.
+	foreach my $skycell_id (@unique_skycells) {
+	    my $skyDir = File::Spec->catdir($workdir, "tess_" . $tess_id, "sky_" . $skycell_id );
+	    system "mkdir -p $skyDir" unless -d $skyDir;
+	    my $skyFile = File::Spec->catfile( $skyDir , $skycell_id );
+	    my $command = "dvoImageExtract -D CATDIR $tess_id $skycell_id -o $skyFile";
 	    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 dvoImageOverlaps: $error_code", $warp_id, $error_code);
-	    }
-	    my @matchlist = split ('\n', $stdout_buf);
-
-	    # dvoImageOverlaps -D CATDIR tessalation (megacam) :: returns:
-	    # 729534pa.ccd00.cmf  :  skycell.051.fits
-	    # 729534pa.ccd00.cmf  :  skycell.052.fits
-	    # PSASTRO.OUTPUT : SKYCELL
-
-	    # now match the imfiles to this list
-	    my @skycells = &select_skycells ($psastroFile, @matchlist);
-	    # XXX should I check and warn if int(@skycells) != int(@matchlist) ?
-	    foreach my $skycell (@skycells) {
-		my %overlap = ();
-		$overlap{skycell_id} = $skycell;
-		$overlap{tess_id}    = $tess_id;
-		$overlap{cam_id}     = $imfile->{cam_id};
-		$overlap{class_id}   = $imfile->{class_id};
-		$overlap{fault}      = $imfile->{fault};
-		push @overlaps, \%overlap;
-
-		print STDERR "overlap: $skycell -> $imfile->{cam_id}\n";
+		&my_die("Unable to perform dvoImageExtract: $error_code", $warp_id, $error_code);
 	    }
 	}
@@ -243,16 +209,6 @@
 }
 
-if (defined $workdir) {
-    $workdir = $ipprc->convert_filename_absolute( $workdir );
-    $workdir = File::Spec->catdir( $workdir, $exp_tag );
-} else {
-    my $example = $ipprc->convert_filename_absolute( $$imfiles[0]->{uri} );
-    my ($vol, $dir, $file) = File::Spec->splitpath( $example );
-    $workdir = $dir;
-}
-system "mkdir -p $workdir" unless -d $workdir;
-
 # Generate a MDC file with the overlaps
-my $overlapName = File::Spec->catfile( $workdir, 'overlap.mdc' );
+my $overlapName = File::Spec->catfile( $workdir, 'overlaps.wrp' . $warp_id . '.mdc' );
 open my $overlapFile, "> $overlapName" or die "Unable to open mdc file $overlapName";
 print $overlapFile "warpSkyCellMap MULTI\n\n";
@@ -314,10 +270,4 @@
 }
 
-sub vsystem {
-    print STDERR "@_\n";
-    my $status = system ("@_");
-    $status;
-}
-
 END {
     my $status = $?;
