Index: /trunk/DataChallenge/copy_essence.pl
===================================================================
--- /trunk/DataChallenge/copy_essence.pl	(revision 10596)
+++ /trunk/DataChallenge/copy_essence.pl	(revision 10596)
@@ -0,0 +1,62 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use File::Spec;
+use File::Find::Rule;
+
+die "Usage: $0 SOURCE_DIR TARGET_DIR\n" if scalar @ARGV != 2;
+
+my $source = File::Spec->canonpath( $ARGV[0] );
+my $target = File::Spec->canonpath( $ARGV[1] );
+
+my @dirs = File::Find::Rule->directory->in( $source );
+
+# Create the directory structure
+foreach my $dir ( @dirs ) {
+    my $rel = File::Spec->abs2rel( $dir, $source );
+    my @subs = File::Spec->splitpath( $rel );
+    
+    # Create all the underlying directories
+    my $source_path = $source;	# Path we've traversed for source
+    my $path = $target;	# Path we've traversed for target
+    foreach my $subdir ( @subs ) {
+	my $newdir = File::Spec->catdir( $path, $subdir );
+	if (defined $subdir and length $subdir > 0) {
+	    if (not -d $newdir) {
+		print "mkdir $newdir\n";
+#		mkdir $newdir;
+	    }
+	    $path = $newdir;
+	}
+    }
+}
+
+# Find the files
+my @files = File::Find::Rule->file->name( '*.fits*' )->in( $source );
+
+foreach my $file ( @files ) {
+    my $rel_name = File::Spec->abs2rel( $file, $source );
+
+    my $unzipper;
+    if ($rel_name =~ /\.fits\.gz$/) {
+	$unzipper = 'gunzip -c';
+	$rel_name =~ s/\.gz$//;
+    } elsif ($rel_name =~ /\.fits\.bz2$/) {
+	$unzipper = 'bunzip2 -c';
+	$rel_name =~ s/\.bz2$//;
+    } elsif ($rel_name =~ /\.fits$/) {
+	$unzipper = 'cat';
+    } else {
+	die "Unrecognised file type for $rel_name\n";
+    }
+
+    my $out_file = File::Spec->rel2abs( $rel_name, $target );
+
+    print "$unzipper $file > $out_file\n";
+#    system "$unzipper $file > $out_file";
+}
+
+__END__;
+
Index: /trunk/DataChallenge/inject_essence.pl
===================================================================
--- /trunk/DataChallenge/inject_essence.pl	(revision 10596)
+++ /trunk/DataChallenge/inject_essence.pl	(revision 10596)
@@ -0,0 +1,64 @@
+#!/usr/bin/env perl
+
+use warnings;
+use strict;
+
+use IPC::Cmd qw( can_run run );
+use File::Spec;
+use File::Find::Rule;
+use PS::IPP::Config;
+my $ipprc = PS::IPP::Config->new();
+
+die "Usage: $0 DIR1 DIR2 DIR3...\n" if scalar @ARGV == 0;
+
+my $pxinject = can_run('pxinject') or die "Can't find pxinject\n";
+
+my $num = 0;
+foreach my $dir ( @ARGV ) {
+    my $absdir = File::Spec->rel2abs( $dir );
+
+    $num += inject('bias', 'zero*.fits', $absdir);
+    $num += inject('flat', '?flat*.fits', $absdir);
+    $num += inject('sm', 'sm*.fits', $absdir);
+    $num += inject('w', 'w*.fits', $absdir);
+}
+
+print "$num files injected.\n";
+
+
+sub inject
+{
+    my $type = shift;		# Type of image
+    my $glob = shift;		# Glob for this type
+    my $dir  = shift;		# Directory to search (absolute path)
+
+    my @files = File::Find::Rule->file->name( $glob )->in( $dir );
+    foreach my $file ( @files ) {
+	my ( $vol, $path, $name ) = File::Spec->splitpath( $file );
+	my ( $exp ) = $name =~ /(.*)\.fits/;
+
+	my $relpath = $ipprc->convert_filename_relative( $file );
+
+	{
+	    my $command = "$pxinject -newExp -exp_id $exp -inst CTIO_MOSAIC2 -telescope CTIO4m " .
+		"-exp_type $type -imfiles 1" ; # Command to run
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run( command => $command, verbose => 1 );
+	    die "Unable to inject $exp: $error_code\n" if not $success;
+	}
+
+	{
+	    my $command = "$pxinject -newImfile -exp_tag $exp -class fpa -class_id fpa " .
+		"-uri $relpath"; # Command to run
+	    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+		run( command => $command, verbose => 1 );
+	    die "Unable to inject $exp imfile: $error_code\n" if not $success;
+	}
+	
+    }
+
+    return scalar @files;
+}
+
+
+__END__
