Index: /trunk/ippScripts/scripts/warp_overlap.pl
===================================================================
--- /trunk/ippScripts/scripts/warp_overlap.pl	(revision 12797)
+++ /trunk/ippScripts/scripts/warp_overlap.pl	(revision 12798)
@@ -89,13 +89,100 @@
 
 unless ($no_op) {
-    ## dvoImageOverlaps -D CATDIR tessalation (astrom.cmf)
-    # dvoImageOverlaps uses the astrometry output file, 
-    ## Calculates the overlaps between imfiles and skycells
+    ## 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;
+    # 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;
+    my $camAstrom = 1;
+    if ($camAstrom) {
+	my $imfile = $imfiles->[0];
+	my $camRoot = $ipprc->convert_filename_absolute( $imfile->{cam_path_base} );
+        $psastroFile = $ipprc->filename("PSASTRO.OUTPUT", $camRoot); # MEF psastro output
+
+	# run the dvoImageOverlaps program to get the overlaps with this image 
+	my $command = "dvoImageOverlaps -D CATDIR $tess_id $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', $stdout_buf);
+
+	# 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 @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 @skycells = &select_skycells ($entry, @matchlist);
+	    foreach $skycell (@skycells) {
+		my %overlap = ();
+		$overlap{skycell_id} = $skycell;
+		$overlap{tess_id}    = $catdir;
+		$overlap{cam_id}     = $imfile->{cam_id};
+		$overlap{class_id}   = $imfile->{class_id};
+		$overlap{fault}      = $imfile->{fault};
+		push @overlaps \$overlap;
+	    }
+	}
+    } else {
+	my @overlaps = ();
+	foreach $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";
+	    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 $skycell (@skycells) {
+		my %overlap = ();
+		$overlap{skycell_id} = $skycell;
+		$overlap{tess_id}    = $catdir;
+		$overlap{cam_id}     = $imfile->{cam_id};
+		$overlap{class_id}   = $imfile->{class_id};
+		$overlap{fault}      = $imfile->{fault};
+		push @overlaps \$overlap;
+	    }
+	}
+    }
 } else {
-    # Add in the skycell and tesselation for each imfile
+    # create an overlap with an entry for each skycell:imfile match
+    my @overlaps = ();
     foreach my $imfile (@$imfiles) {
-	$imfile->{skycell_id} = 'default';
-	$imfile->{tess_id} = 'default';
-	$imfile->{fault} = 0;
+	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;
     }
 }
@@ -115,12 +202,12 @@
 open my $overlapFile, "> $overlapName" or die "Unable to open mdc file $overlapName";
 print $overlapFile "warpSkyCellMap MULTI\n\n";
-foreach my $imfile (@$imfiles) {
-    print $overlapFile "warpSkyCellMap\tMETADATA\n";
-    print $overlapFile "\twarp_id\t\tS32\t" . $warp_id . "\n";
-    print $overlapFile "\tskycell_id\tSTR\t" . $imfile->{skycell_id} . "\n";
-    print $overlapFile "\ttess_id\t\tSTR\t" . $imfile->{tess_id} . "\n";
-    print $overlapFile "\tcam_id\t\tS32\t" . int($imfile->{cam_id}) . "\n";
-    print $overlapFile "\tclass_id\tSTR\t" . $imfile->{class_id} . "\n";
-    print $overlapFile "\tfault\t\tS16\t" . $imfile->{fault} . "\n";
+foreach my $overlap (@overlaps) {
+    print $overlapFile "warpSkyCellMap   METADATA\n";
+    print $overlapFile "  warp_id        S32    $warp_id\n";
+    print $overlapFile "  skycell_id     STR    $overlap->{skycell_id}\n";
+    print $overlapFile "  tess_id        STR    $overlap->{tess_id}\n";
+    print $overlapFile "  cam_id         S32    $overlap->{cam_id}\n";
+    print $overlapFile "  class_id       STR    $overlap->{class_id}\n";
+    print $overlapFile "  fault          S16    $overlap->{fault}\n";
     print $overlapFile "END\n\n";
 }
@@ -143,6 +230,4 @@
 }
 
-
-
 sub my_die
 {
@@ -157,4 +242,20 @@
 }
 
+sub select_skycells 
+{
+    my $entry = shift;
+    my @list = @_;
+
+    my @skycells = ();
+
+    foreach my $line (@list) {
+	if ($line =~ m|$entry|) {
+	    ($skycell) = $line =~ m|$entry\s+:\s+(\S+)|;
+	    push @skycells, $skycell;
+	}
+    }
+    return @skycells;
+}
+
 END {
     my $status = $?;
