Index: trunk/ippScripts/scripts/warp_overlap.pl
===================================================================
--- trunk/ippScripts/scripts/warp_overlap.pl	(revision 14511)
+++ trunk/ippScripts/scripts/warp_overlap.pl	(revision 14567)
@@ -59,8 +59,11 @@
 my $dvoImageOverlaps = can_run('dvoImageOverlaps') or (warn "Can't find dvoImageOverlaps" and $missing_tools = 1);
 my $dvoImageExtract = can_run('dvoImageExtract') or (warn "Can't find dvoImageExtract" and $missing_tools = 1);
-if ($missing_tools) { 
+my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
+if ($missing_tools) {
     warn("Can't find required tools.");
     exit($PS_EXIT_CONFIG_ERROR); 
 }
+
+my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
 
 # Get list of component imfiles for exposure
@@ -75,6 +78,5 @@
 	&my_die("Unable to perform warptool -imfile: $error_code", $warp_id, $error_code);
     }
-
-    my $mdcParser = PS::IPP::Metadata::Config->new;	# Parser for metadata config files
+    
     my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
 	&my_die("Unable to parse metadata config doc", $warp_id, $PS_EXIT_PROG_ERROR);
@@ -83,4 +85,19 @@
 }
 
+# Where do we get the astrometry source from?
+my $astromSource;		# The astrometry source
+{
+    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", $warp_id, $error_code);
+    }
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata config doc", $warp_id, $PS_EXIT_PROG_ERROR);
+    $astromSource = metadataLookupStr('ASTROM.SOURCE');
+}
+
 # Determine the imfile/skycell overlaps
 my @overlaps = ();
@@ -88,5 +105,5 @@
 unless ($no_op) {
     # Calculate the overlaps between imfiles and skycells
-
+    
     my $tessellations = $ipprc->tessellations(); # Hash of defined tessellations
     foreach my $tess_id ( keys %$tessellations ) {
@@ -99,68 +116,30 @@
 	my $tess_dir = $ipprc->convert_filename_absolute( $$tessellations{$tess_id} ); # Catdir for DVO
 	print STDERR "tessellation: $tess_id, $tess_dir\n";
-
-	my $imfile = $imfiles->[0];
-	my $camRoot = $imfile->{cam_path_base};
-	my $psastroFile = $ipprc->filename("PSASTRO.OUTPUT.MEF", $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 -accept-astrom -D CATDIR $tess_dir " . $ipprc->file_resolve($psastroFile);
-	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', (join "", @$stdout_buf));
-	
-	# The command "dvoImageOverlaps -accept-astrom -D CATDIR tessellation (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 = ();
-	my %found_skycells = ();
-	foreach my $imfile (@$imfiles) {
-	    my $fileLevel = $imfile->{filelevel};
-	    my $entry = "";
-	    if (lc($fileLevel) eq "chip") {
-		my $class_id = $imfile->{class_id};
-		my $chipRoot = $ipprc->file_resolve( $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 %unique_skycells = (); # Identified skycells (all unique by virtue of hash property)
+	
+	if ($astromSource and $astromSource ne 'NULL') {
+	    # We have an astrometry file from psastro
+	    my $imfile = $imfiles->[0];
+	    my $camRoot = $imfile->{cam_path_base};
+	    my $astromFile = $ipprc->filename($astromSource, $camRoot); # Astrometry file
+	    
+	    my @matchlist = get_overlaps($astromFile, $tess_dir); # List of overlaps
+	    
+	    # Match each of the imfiles to this list
+	    foreach my $imfile (@$imfiles) {
+		extract_overlaps(\@matchlist, $imfile, $astromFile, $tess_id, \@overlaps, \%unique_skycells);
 	    }
-
-	    my @skycells = &select_skycells ($entry, @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;
-		
-		printf STDERR "overlap: %s : %s , %s\n", $skycell, $imfile->{cam_id}, $imfile->{class_id};
-		
-		# generate a unique list of the skycells
-		unless ($found_skycells{$skycell}) {
-		    push @unique_skycells, $skycell;
-		    $found_skycells{$skycell} = 1;
-		}
+	} else {
+	    # We have the imfiles themselves for the WCS
+	    foreach my $imfile (@$imfiles) {
+		my $astromFile = $imfile->{chip_uri}; # Astrometry file
+		my @matchlist = get_overlaps($astromFile, $tess_dir); # List of overlaps
+		extract_overlaps(\@matchlist, $imfile, $astromFile, $tess_id, \@overlaps, \%unique_skycells);
 	    }
 	}
-
+	
 	# Extract the skycells to images, used as warp templates.
-	foreach my $skycell_id (@unique_skycells) {
+	foreach my $skycell_id (keys %unique_skycells) {
 	    my $skyDir = caturi($workdir, "tess_" . $tess_id, "sky_" . $skycell_id );
 	    my $skyFile = $ipprc->file_prepare( $skycell_id, $skyDir );
@@ -211,5 +190,5 @@
     my $command = "$warptool -addoverlap -mapfile $overlapName"; # Command to run warptool
     $command .= " -dbname $dbname" if defined $dbname;
-
+    
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -233,4 +212,73 @@
 }
 
+# Run dvoImageOverlaps to get the overlaps; return the output
+sub get_overlaps
+{
+    my $filename = shift;	# Filename on which to run dvoImageOverlaps
+    my $tess_dir = shift;	# Tessellation directory
+
+    my $command = "$dvoImageOverlaps -D CATDIR $tess_dir " . $ipprc->file_resolve($filename);
+    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);
+    }
+    return split ('\n', (join "", @$stdout_buf));
+}    
+
+# Extract a list of overlaps for an imfile
+#
+# The command "dvoImageOverlaps -accept-astrom -D CATDIR tessellation (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)
+sub extract_overlaps
+{
+    my $matches = shift;	# Reference to list of skycells from dvoImageOverlaps
+    my $imfile = shift;		# Imfile information
+    my $filename = shift;	# Filename used with dvoImageOverlaps
+    my $tess_id = shift;	# Tessellation identifier
+    my $overlaps = shift;	# Reference to list of overlaps
+    my $unique_skycells = shift; # Reference to hash of found skycells
+
+    # Get rid of the path
+    my @dirlist = File::Spec->splitdir( $filename ); # The elements of the full path
+    $filename = pop @dirlist;
+
+    # Work out how to identify this imfile in the output
+    my $fileLevel = $imfile->{filelevel};
+    my $entry;	# How to identify this imfile in the dvoImageOverlaps output
+    if (lc($fileLevel) eq "chip") {
+	my $class_id = $imfile->{class_id};
+	my $chipRoot = $ipprc->file_resolve( $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 = $filename . '\[' . $extname . '\]';
+    } else {
+	$entry = $filename;
+    }
+
+    my @skycells = &select_skycells($entry, @$matches);	# Matching skycells
+    foreach my $skycell (@skycells) {
+	my %overlap = ();	# Overlap information for warptool
+	$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;
+	
+	printf STDERR "overlap: %s : %s , %s\n", $skycell, $imfile->{cam_id}, $imfile->{class_id};
+	
+	$unique_skycells->{$skycell} = 1;
+    }
+
+    return;
+}
+    
+# Find skycells in the list that come from a particular entry
 sub select_skycells 
 {
@@ -250,5 +298,5 @@
 	}
     }
-    @skycells;
+    return @skycells;
 }
 
