Index: trunk/ippScripts/scripts/warp_overlap.pl
===================================================================
--- trunk/ippScripts/scripts/warp_overlap.pl	(revision 17518)
+++ trunk/ippScripts/scripts/warp_overlap.pl	(revision 17646)
@@ -23,15 +23,15 @@
 
 use PS::IPP::Config qw($PS_EXIT_SUCCESS
-		       $PS_EXIT_UNKNOWN_ERROR
-		       $PS_EXIT_SYS_ERROR
-		       $PS_EXIT_CONFIG_ERROR
-		       $PS_EXIT_PROG_ERROR
-		       $PS_EXIT_DATA_ERROR
-		       $PS_EXIT_TIMEOUT_ERROR
-		       metadataLookupStr
-		       metadataLookupBool
-		       caturi
-		       file_scheme
-		       );
+                       $PS_EXIT_UNKNOWN_ERROR
+                       $PS_EXIT_SYS_ERROR
+                       $PS_EXIT_CONFIG_ERROR
+                       $PS_EXIT_PROG_ERROR
+                       $PS_EXIT_DATA_ERROR
+                       $PS_EXIT_TIMEOUT_ERROR
+                       metadataLookupStr
+                       metadataLookupBool
+                       caturi
+                       file_scheme
+                       );
 
 my $ipprc = PS::IPP::Config->new(); # IPP configuration
@@ -44,5 +44,5 @@
     'dbname|d=s'        => \$dbname, # Database name
     'verbose'           => \$verbose,   # Print to stdout
-    'no-update'         => \$no_update,	# Don't update the database?
+    'no-update'         => \$no_update, # Don't update the database?
     'no-op'             => \$no_op, # Don't do any operations
     'save-temps'        => \$save_temps, # Save temporary files?
@@ -66,8 +66,8 @@
 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
+    exit($PS_EXIT_CONFIG_ERROR);
+}
+
+my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
 
 # Get list of component imfiles for exposure
@@ -77,30 +77,30 @@
     $command .= " -dbname $dbname" if defined $dbname;
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => $verbose);
+        run(command => $command, verbose => $verbose);
     unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform warptool -imfile: $error_code", $warp_id, $error_code);
-    }
-    
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to perform warptool -imfile: $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);
-    $imfiles = parse_md_list($metadata) or 
-	&my_die("Unable to parse metadata list", $warp_id, $PS_EXIT_PROG_ERROR);
+        &my_die("Unable to parse metadata config doc", $warp_id, $PS_EXIT_PROG_ERROR);
+    $imfiles = parse_md_list($metadata) or
+        &my_die("Unable to parse metadata list", $warp_id, $PS_EXIT_PROG_ERROR);
 }
 
 # Where do we get the astrometry source from?
-my $astromSource;		# The astrometry source
-my $astromAccept;		# Accept the astrometry unconditionally?
-my $astromDepth;		# File level of the astrometry source (SPLIT or MEF)?
+my $astromSource;               # The astrometry source
+my $astromAccept;               # Accept the astrometry unconditionally?
+my $astromDepth;                # File level of the astrometry source (SPLIT or MEF)?
 {
     my $command = "$ppConfigDump -camera $camera -dump-recipe PSWARP -";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => $verbose);
+        run(command => $command, verbose => $verbose);
     unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to perform ppConfigDump: $error_code", $warp_id, $error_code);
+        $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);
+        &my_die("Unable to parse metadata config doc", $warp_id, $PS_EXIT_PROG_ERROR);
     $astromSource = metadataLookupStr($metadata, 'ASTROM.SOURCE');
     $astromAccept = metadataLookupBool($metadata, 'ASTROM.ACCEPT');
@@ -116,57 +116,57 @@
     my $tess_dir = $ipprc->tessellation_catdir( $tess_id ); # Tessellation catdir for DVO
     $tess_dir = $ipprc->convert_filename_absolute( $tess_dir );
-	
+
     my %unique_skycells = (); # Identified skycells (all unique by virtue of hash property)
-    
+
     # XXX this is a bit too hard wired: the concept is that astrometry comes from the MOSAIC vs CHIP output
     # XXX we have moved the astrometry analysis to the camera stage only: we should always have the source be MEF
     if ($astromSource eq 'PSASTRO.OUTPUT.MEF') {
-	# We have a MEF 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, $astromAccept); # List of overlaps
-	if (! @matchlist) {
-	    &my_die("Unable to perform dvoImageOverlaps: missing astrometry", $warp_id, $PS_EXIT_DATA_ERROR);
-	}	    
-	# Match each of the imfiles to this list
-	foreach my $imfile (@$imfiles) {
-	    extract_overlaps(\@matchlist, $imfile, $astromFile, $tess_id, \@overlaps, \%unique_skycells);
-	}
+        # We have a MEF 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, $astromAccept); # List of overlaps
+        if (! @matchlist) {
+            &my_die("Unable to perform dvoImageOverlaps: missing astrometry", $warp_id, $PS_EXIT_DATA_ERROR);
+        }
+        # Match each of the imfiles to this list
+        foreach my $imfile (@$imfiles) {
+            extract_overlaps(\@matchlist, $imfile, $astromFile, $tess_id, \@overlaps, \%unique_skycells);
+        }
     } else {
-	# We have per-imfile astrometry
-	foreach my $imfile (@$imfiles) {
-	    my $astromFile;
-	    if ($astromSource eq 'PSASTRO.OUTPUT') {
-		my $chipRoot = $imfile->{chip_path_base};
-		my $classID = $imfile->{class_id};
-		$astromFile = $ipprc->filename($astromSource, $chipRoot, $classID); # Astrometry file
-	    } else {
-		$astromFile = $imfile->{chip_uri}; # Astrometry file
-	    }
-	    my @matchlist = get_overlaps($astromFile, $tess_dir, $astromAccept); # List of overlaps
-	    if (! @matchlist and $verbose) {
-		print "skipping $astromFile\n";
-	    }	    
-	    
-	    extract_overlaps(\@matchlist, $imfile, $astromFile, $tess_id, \@overlaps, \%unique_skycells);
-	}
+        # We have per-imfile astrometry
+        foreach my $imfile (@$imfiles) {
+            my $astromFile;
+            if ($astromSource eq 'PSASTRO.OUTPUT') {
+                my $chipRoot = $imfile->{chip_path_base};
+                my $classID = $imfile->{class_id};
+                $astromFile = $ipprc->filename($astromSource, $chipRoot, $classID); # Astrometry file
+            } else {
+                $astromFile = $imfile->{chip_uri}; # Astrometry file
+            }
+            my @matchlist = get_overlaps($astromFile, $tess_dir, $astromAccept); # List of overlaps
+            if (! @matchlist and $verbose) {
+                print "skipping $astromFile\n";
+            }
+
+            extract_overlaps(\@matchlist, $imfile, $astromFile, $tess_id, \@overlaps, \%unique_skycells);
+        }
     }
 } else {
     # create an overlap with an entry for each skycell:imfile match
     foreach my $imfile (@$imfiles) {
-	my %overlap = ();
-	$overlap{skycell_id} = 'default';
-	$overlap{tess_id}    = 'default';
-	$overlap{cam_id}     = $imfile->{cam_id};
-	$overlap{class_id}   = $imfile->{class_id};
-	$overlap{fault}      = $imfile->{fault};
-	push @overlaps, \%overlap;
+        my %overlap = ();
+        $overlap{skycell_id} = 'default';
+        $overlap{tess_id}    = 'default';
+        $overlap{cam_id}     = $imfile->{cam_id};
+        $overlap{class_id}   = $imfile->{class_id};
+        $overlap{fault}      = $imfile->{fault};
+        push @overlaps, \%overlap;
     }
 }
 
 # XXX this file needs some additional error checking: if no overlaps are found, we
-# keep running this step over and over (a successful warptool -addoverlap prevents 
+# keep running this step over and over (a successful warptool -addoverlap prevents
 # successive warptime -imfile from running.
 
@@ -192,11 +192,11 @@
     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 => $verbose);
+        run(command => $command, verbose => $verbose);
     unless ($success) {
-	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	warn("Unable to perform warptool -addoverlap: $error_code\n");
-	exit($error_code);
+        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+        warn("Unable to perform warptool -addoverlap: $error_code\n");
+        exit($error_code);
     }
 }
@@ -204,7 +204,7 @@
 sub my_die
 {
-    my $msg = shift;		# Warning message on die
-    my $warp_id = shift;	# Warp identifier
-    my $exit_code = shift;	# Exit code to add
+    my $msg = shift;            # Warning message on die
+    my $warp_id = shift;        # Warp identifier
+    my $exit_code = shift;      # Exit code to add
 
     warn($msg);
@@ -217,18 +217,20 @@
 sub get_overlaps
 {
-    my $filename = shift;	# Filename on which to run dvoImageOverlaps
-    my $tess_dir = shift;	# Tessellation directory
-    my $accept = shift;		# Do we use the -accept-astrom flag?
+    my $filename = shift;       # Filename on which to run dvoImageOverlaps
+    my $tess_dir = shift;       # Tessellation directory
+    my $accept = shift;         # Do we use the -accept-astrom flag?
 
     my $command = "$dvoImageOverlaps -D CATDIR $tess_dir " . $ipprc->file_resolve($filename);
     $command .= ' -accept-astrom' if $accept;
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-	run(command => $command, verbose => $verbose);
+        run(command => $command, verbose => $verbose);
     if (!$success) {
-	print "Missing astrometry for $filename --- check ASTROM.SOURCE in PSWARP recipe\n";
-	return 0;
+        print "Unable to calculate overlaps from $filename\n";
+        print "STDOUT:\n$stdout_buf\n";
+        print "STDERR:\n$stderr_buf\n";
+        return 0;
     }
     return split ('\n', (join "", @$stdout_buf));
-}    
+}
 
 # Extract a list of overlaps for an imfile
@@ -240,9 +242,9 @@
 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 $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
 
@@ -253,55 +255,55 @@
     # 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
+    my $entry;  # How to identify this imfile in the dvoImageOverlaps output
     if ((lc($fileLevel) eq "chip") && (lc($astromDepth) eq "mef")) {
-	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
-	
-	
-	$entry = $filename . '\[' . $extname . '\]';
-	print STDERR "entry: $entry, class: $class_id, extname: $extname, chiproot: $chipRoot\n" if $verbose;
+        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
+
+
+        $entry = $filename . '\[' . $extname . '\]';
+        print STDERR "entry: $entry, class: $class_id, extname: $extname, chiproot: $chipRoot\n" if $verbose;
     } else {
-	$entry = $filename;
-	print STDERR "entry: $entry\n" if $verbose;
-    }
-
-    my @skycells = &select_skycells($entry, @$matches);	# Matching skycells
+        $entry = $filename;
+        print STDERR "entry: $entry\n" if $verbose;
+    }
+
+    my @skycells = &select_skycells($entry, @$matches); # Matching skycells
     my $Nskycells = @skycells;
     printf STDERR "Nskycells: $Nskycells\n" if $verbose;
     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} if $verbose;
-	
-	$unique_skycells->{$skycell} = 1;
+        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} if $verbose;
+
+        $unique_skycells->{$skycell} = 1;
     }
 
     return;
 }
-    
+
 # Find skycells in the list that come from a particular entry
-sub select_skycells 
-{
-    my $entry = shift;		# File+Ext to search for
-    my @list = @_;		# List of "File+Ext : skycell"
+sub select_skycells
+{
+    my $entry = shift;          # File+Ext to search for
+    my @list = @_;              # List of "File+Ext : skycell"
 
     my @skycells = ();
-    my %unique = ();		# Ensure we only return unique skycells for this entry
+    my %unique = ();            # Ensure we only return unique skycells for this entry
 
     foreach my $line (@list) {
-	if ($line =~ m|$entry|) {
-	    my ($skycell) = $line =~ m|$entry\S*\s+:\s+(\S+)|;
-	    if (not defined $unique{$skycell}) {
-		push @skycells, $skycell;
-		$unique{$skycell} = 1;
-	    }
-	}
+        if ($line =~ m|$entry|) {
+            my ($skycell) = $line =~ m|$entry\S*\s+:\s+(\S+)|;
+            if (not defined $unique{$skycell}) {
+                push @skycells, $skycell;
+                $unique{$skycell} = 1;
+            }
+        }
     }
     return @skycells;
