Index: /trunk/ippScripts/scripts/phase4_overlap.pl
===================================================================
--- /trunk/ippScripts/scripts/phase4_overlap.pl	(revision 11806)
+++ /trunk/ippScripts/scripts/phase4_overlap.pl	(revision 11806)
@@ -0,0 +1,140 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+## report the program and machine
+use Sys::Hostname;
+my $host = hostname();
+print "\n\n";
+print "Starting script $0 on $host\n\n";
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use IPC::Cmd qw( can_run run );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::Stats;
+
+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
+    );
+my $ipprc = PS::IPP::Config->new(); # IPP configuration
+use File::Spec;
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+
+my ($det_id, $dbname, $workdir, $no_update);
+GetOptions(
+    'p4_id|i=s'         => \$p4_id, # Phase 4 identifier
+    'dbname|d=s'        => \$dbname, # Database name
+    'workdir|w=s'       => \$workdir, # Working directory, for output files
+    'no-update'         => \$no_update
+) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+pod2usage(
+    -msg => "Required options: --p4_id",
+    -exitval => 3,
+) unless defined $p4_id;
+
+# Look for programs we need
+my $missing_tools;
+my $p4tool = can_run('p4tool') or (warn "Can't find p4tool" and $missing_tools = 1);
+#my $overlap = can_run('overlap') or (warn "Can't find overlap" and $missing_tools = 1);
+if ($missing_tools) { 
+    warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR); 
+}
+
+# Get list of component imfiles for exposure
+my $imfiles;
+{
+    my $command = "$p4tool -imfile -p4_id $p4_id";
+    $command .= " -dbname $dbname" if defined $dbname;
+    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 p4tool -imfile: $error_code", $p4_id, $error_code);
+    }
+
+    my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+	&my_die("Unable to parse metadata config doc", $p4_id, $PS_EXIT_PROG_ERROR);
+    $imfiles = parse_md_list($metadata) or 
+	&my_die("Unable to parse metadata list", $p4_id, $PS_EXIT_PROG_ERROR);
+}
+
+### Calculates, in some as-yet unknown way the overlaps between imfiles and skycells
+
+# Add in the skycell and tesselation for each imfile
+foreach my $imfile (@$imfiles) {
+    $imfile->{skycell_id} = 'default';
+    $imfile->{tess_id} = 'default';
+}
+
+
+# Generate a MDC file with the overlaps
+$workdir = $ipprc->convert_filename_absolute( $workdir );
+my $overlapName = File::Spec->catfile( $workdir, $p4_id . ".overlap.mdc" ); 
+open my $overlapFile "> $overlapName" or
+    &my_die("Unable to open mdc file $overlapName", $p4_id, $PS_EXIT_DATA_ERROR);
+print $overlapFile "p4SkyCellMap MULTI\n\n";
+foreach my $imfile (@$imfiles) {
+    print $overlapFile "p4_id\t\tS32\t" . $imfile->{p4_id} . "\n";
+    print $overlapFile "skycell_id\tSTR\t" . $imfile->{skycell_id} . "\n";
+    print $overlapFile "tess_id\t\tSTR\t" . $imfile->{tess_id} . "\n";
+    print $overlapFile "exp_tag\t\tSTR\t" . $imfile->{exp_tag} . "\n";
+    print $overlapFile "p3_version\tS32\t" . $imfile->{p3_version} . "\n";
+    print $overlapFile "class_id\t\tSTR\t" . $imfile->{class_id} . "\n";
+}
+close $overlapFile;
+
+# Add the processed file to the database
+unless ($no_update) {
+    my $command = "$p4tool -addoverlap -p4_id $p4_id -info $overlapName"; # Command to run p4tool
+    $command .= " -dbname $dbname" if defined $dbname;
+
+    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);
+	warn("Unable to perform p4tool -addoverlap: $error_code\n");
+	exit($error_code);
+    }
+
+    unlink $overlapName;
+}
+
+
+
+sub my_die
+{
+    my $msg = shift;		# Warning message on die
+    my $p4_id = shift;		# Phase 4 identifier
+    my $exit_code = shift;	# Exit code to add
+
+    warn($msg);
+    if ($p4_id) {
+	my $command = "$p4tool -addoverlap -p4_id $p4_id -code $exit_code";
+	$command .= " -dbname $dbname" if defined $dbname;
+        system ($command);
+    }
+    exit $exit_code;
+}
+
+END {
+    my $status = $?;
+    system("sync") == 0
+        or die "failed to execute sync: $!" ;
+    $? = $status;
+}
+
+__END__
