Index: /trunk/ippScripts/Build.PL
===================================================================
--- /trunk/ippScripts/Build.PL	(revision 13111)
+++ /trunk/ippScripts/Build.PL	(revision 13112)
@@ -47,4 +47,5 @@
 			       scripts/ipp_serial_chip.pl
 			       scripts/ipp_serial_camera.pl
+			       scripts/ipp_serial_warp.pl
 			       scripts/ipp_simulation_data.pl
 			       )],
Index: /trunk/ippScripts/MANIFEST
===================================================================
--- /trunk/ippScripts/MANIFEST	(revision 13111)
+++ /trunk/ippScripts/MANIFEST	(revision 13112)
@@ -30,4 +30,5 @@
 scripts/ipp_serial_chip.pl
 scripts/ipp_serial_camera.pl
+scripts/ipp_serial_warp.pl
 scripts/ipp_simulation_data.pl
 t/00_distribution.t
Index: /trunk/ippScripts/scripts/ipp_serial_warp.pl
===================================================================
--- /trunk/ippScripts/scripts/ipp_serial_warp.pl	(revision 13112)
+++ /trunk/ippScripts/scripts/ipp_serial_warp.pl	(revision 13112)
@@ -0,0 +1,125 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use vars qw( $VERSION );
+$VERSION = '0.01';
+
+use IPC::Cmd 0.36 qw( can_run run );
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::List qw( parse_md_list );
+
+use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+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
+
+my ($dbname,			# Database name to use
+    $no_op,			# No operations?
+    $no_update,			# No updating?
+    );
+GetOptions(
+	   'dbname|d=s' => \$dbname,
+	   'no-op' => \$no_op,
+	   'no-update' => \$no_update,
+) or pod2usage( 2 );
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+
+pod2usage(
+	  -msg => "Required options: --dbname",
+	  -exitval => 3,
+	  ) unless defined $dbname;
+
+my $mdcParser = PS::IPP::Metadata::Config->new;	# Metadata config parser
+
+# Look for programs we need
+my $missing_tools;
+my $warptool = can_run('warptool') or
+    (warn "Can't find warptool" and $missing_tools = 1);
+my $warp_skycell = can_run('warp_skycell.pl') or
+    (warn "Can't find warp_skycell.pl" and $missing_tools = 1);
+my $warp_overlap = can_run('warp_overlap.pl') or
+    (warn "Can't find warp_overlap.pl" and $missing_tools = 1);
+
+if ($missing_tools) {
+    warn ("Can't find required tools");
+    exit($PS_EXIT_CONFIG_ERROR); 
+}
+
+
+# Calculate overlaps
+{
+    my $list;
+    my $command = "$warptool -tooverlap -dbname $dbname"; # Command to run
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run( command => $command, verbose => 1 );
+    die "Unable to get warps for which to calculate overlaps: $error_code\n" if not $success;
+    $list = parse_md_list( $mdcParser->parse( join( '', @$stdout_buf ) ) ) or
+	die "Unable to parse output from warptool.\n";
+
+    foreach my $item (@$list) {
+	my $warp_id = $item->{warp_id};
+	my $cam_id = $item->{cam_id};
+	my $workdir = $item->{workdir};
+	my $camera = $item->{camera};
+	
+	my $command = "$warp_overlap --warp_id $warp_id --camera $camera --dbname $dbname";
+	$command .= " --no-op" if defined $no_op;
+	$command .= " --no-update" if defined $no_update;
+	$command .= " --workdir $workdir" if defined $workdir;
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run( command => $command, verbose => 1 );
+	die "Unable to get warp overlaps on $warp_id: $error_code\n" if not $success;
+    }
+}
+
+
+# Warping proper
+{
+    my $list;
+    my $command = "$warptool -towarped -dbname $dbname"; # Command to run
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run( command => $command, verbose => 1 );
+    die "Unable to get warps for warping: $error_code\n" if not $success;
+    $list = parse_md_list( $mdcParser->parse( join( '', @$stdout_buf ) ) ) or
+	die "Unable to parse output from warptool.\n";
+
+    foreach my $item (@$list) {
+	my $warp_id = $item->{warp_id};
+	my $skycell_id = $item->{skycell_id};
+	my $tess_id = $item->{tess_id};
+	my $cam_id = $item->{cam_id};
+	my $workdir = $item->{workdir};
+	my $camera = $item->{camera};
+	
+	my $command = "$warp_skycell --warp_id $warp_id --skycell_id $skycell_id --tess_id $tess_id --camera $camera --dbname $dbname";
+	$command .= " --no-op" if defined $no_op;
+	$command .= " --no-update" if defined $no_update;
+	$command .= " --workdir $workdir" if defined $workdir and $workdir ne "NULL";
+	my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	    run( command => $command, verbose => 1 );
+	die "Unable to do warp processing on $warp_id,$skycell_id: $error_code\n" if not $success;
+    }
+}
+
+
+
+END {
+    my $status = $?;
+    system("sync") == 0
+        or die "failed to execute sync: $!" ;
+    $? = $status;
+}
+
+__END__
Index: /trunk/ippScripts/scripts/ipp_simulation_data.pl
===================================================================
--- /trunk/ippScripts/scripts/ipp_simulation_data.pl	(revision 13111)
+++ /trunk/ippScripts/scripts/ipp_simulation_data.pl	(revision 13112)
@@ -121,9 +121,9 @@
     ( $basename, $counter ) = filename( $name, $counter );
     my $filename = File::Spec->catfile( $workdir, $basename ) if defined $workdir; # File name
-    run( command => "$ppSim -camera $camera -type BIAS $filename",
-	 verbose => 1 ) or die "Unable to run ppSim";
-    run( command => "$inject --camera $camera --telescope $telescope --workdir $workdir " .
-	 "--dbname $dbname $basename",
-	  verbose => 1 ) or die "Unable to inject file.";
+#    run( command => "$ppSim -camera $camera -type BIAS $filename",
+#	 verbose => 1 ) or die "Unable to run ppSim";
+#    run( command => "$inject --camera $camera --telescope $telescope --workdir $workdir " .
+#	 "--dbname $dbname $basename",
+#	  verbose => 1 ) or die "Unable to inject file.";
 }
 
@@ -133,9 +133,9 @@
     ( $basename, $counter ) = filename( $name, $counter );
     my $filename = File::Spec->catfile( $workdir, $basename ) if defined $workdir; # File name
-    run ( command => "$ppSim -camera $camera -type DARK -exptime $exptime $filename",
-	  verbose => 1 ) or die "Unable to run ppSim";
-    run( command => "$inject --camera $camera --telescope $telescope --workdir $workdir " .
-	 "--dbname $dbname $basename",
-	 verbose => 1 ) or die "Unable to inject file.";
+#    run ( command => "$ppSim -camera $camera -type DARK -exptime $exptime $filename",
+#	  verbose => 1 ) or die "Unable to run ppSim";
+#    run( command => "$inject --camera $camera --telescope $telescope --workdir $workdir " .
+#	 "--dbname $dbname $basename",
+#	 verbose => 1 ) or die "Unable to inject file.";
 }
 
@@ -147,12 +147,11 @@
 	( $basename, $counter ) = filename( $name, $counter );
 	my $filename = File::Spec->catfile( $workdir, $basename ) if defined $workdir; # File name
-	run( command => "$ppSim -camera $camera -type FLAT -filter $filter -exptime $exptime $filename",
-	     verbose => 1 ) or die "Unable to run ppSim";
-        run( command => "$inject --camera $camera --telescope $telescope --workdir $workdir " .
-	     "--dbname $dbname $basename",
-	     verbose => 1 ) or die "Unable to inject file.";
+#	run( command => "$ppSim -camera $camera -type FLAT -filter $filter -exptime $exptime $filename",
+#	     verbose => 1 ) or die "Unable to run ppSim";
+#        run( command => "$inject --camera $camera --telescope $telescope --workdir $workdir " .
+#	     "--dbname $dbname $basename",
+#	     verbose => 1 ) or die "Unable to inject file.";
     }
 }
-
 
 # Generate object images
Index: /trunk/ippScripts/scripts/warp_overlap.pl
===================================================================
--- /trunk/ippScripts/scripts/warp_overlap.pl	(revision 13111)
+++ /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 = $?;
Index: /trunk/ippScripts/scripts/warp_skycell.pl
===================================================================
--- /trunk/ippScripts/scripts/warp_skycell.pl	(revision 13111)
+++ /trunk/ippScripts/scripts/warp_skycell.pl	(revision 13112)
@@ -101,4 +101,5 @@
 my $outputBin2 = $ipprc->filename("PSWARP.BIN2", $outputRoot, $skycell_id );
 my $outputStats = $outputRoot . '.stats';
+my $skyFile = File::Spec->catfile( $workdir, $skycell_id ); # Target sky cell file
 my $listName = $outputRoot . '.list';
 
@@ -115,5 +116,5 @@
 my $stats = PS::IPP::Metadata::Stats->new(); # Stats parser
 unless ($no_op) {
-    my $command = "$pswarp -list $listName $outputRoot -stat $outputStats"; # Command to run pswarp
+    my $command = "$pswarp -list $listName $outputRoot $skyFile -stat $outputStats"; # Command to run pswarp
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => 1);
@@ -124,6 +125,6 @@
     &my_die("Couldn't find expected output file: $outputImage", $warp_id, $skycell_id, $tess_id, $PS_EXIT_SYS_ERROR) unless -f $outputImage;
     &my_die("Couldn't find expected output file: $outputStats", $warp_id, $skycell_id, $tess_id, $PS_EXIT_SYS_ERROR) unless -f $outputStats;
-    &my_die("Couldn't find expected output file: $outputBin1", $warp_id, $skycell_id, $tess_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin1;
-    &my_die("Couldn't find expected output file: $outputBin2", $warp_id, $skycell_id, $tess_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin2;
+#    &my_die("Couldn't find expected output file: $outputBin1", $warp_id, $skycell_id, $tess_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin1;
+#    &my_die("Couldn't find expected output file: $outputBin2", $warp_id, $skycell_id, $tess_id, $PS_EXIT_SYS_ERROR) unless -f $outputBin2;
 
     # Get the statistics on the warped image
