Index: trunk/ippScripts/scripts/lossy_compress_imfile.pl
===================================================================
--- trunk/ippScripts/scripts/lossy_compress_imfile.pl	(revision 27168)
+++ trunk/ippScripts/scripts/lossy_compress_imfile.pl	(revision 27169)
@@ -21,4 +21,5 @@
 use PS::IPP::Config 1.01 qw( :standard );
 use PS::IPP::Metadata::Config;
+use File::Temp qw( tempfile );
 use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
 use Pod::Usage qw( pod2usage );
@@ -28,5 +29,7 @@
 my $regtool = can_run( 'regtool' ) or (warn "Can't find regtool" and $missing_tools = 1);
 my $fpack   = can_run( 'fpack' ) or (warn "Can't find fpack" and $missing_tools = 1);
-my ($exp_id, $class_id, $exp_name, $uri, $bytes, $md5sum, $dbname, $state, $verbose, $no_update, $no_op, $logfile);
+my $funpack   = can_run( 'funpack' ) or (warn "Can't find funpack" and $missing_tools = 1);
+my ($exp_id, $class_id, $exp_name, $uri, $bytes, $md5sum, $dbname, $state);
+my ($verbose, $no_update, $no_op, $logfile, $save_temps);
 my ($camera);
 GetOptions(
@@ -44,4 +47,5 @@
     'no-op'               => \$no_op,
     'logfile=s'           => \$logfile,
+    'save-temps'          => \$save_temps,
     ) or pod2usage ( 2 );
 
@@ -91,11 +95,27 @@
 	if ($ipprc->file_exists($compUri));
     my $compReal= $ipprc->file_resolve( $compUri, 'create');
-    my $compress_command = "$fpack -h -s 8 -S $uriReal >  $compReal";
+    # Apparently we need to funpack before fpacking.  Probably should have realized that beforehand.
+    my $tempfile = new File::Temp ( TEMPLATE => "${exp_name}.XXXX",
+				    DIR => '/tmp/',
+				    UNLINK => !$save_temps,
+				    SUFFIX => '.fits');
+    my $tempReal = $tempfile->filename;
+
+    my $uncompress_command = "$funpack -S $uriReal > $tempReal";
+    my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
+	run(command => $uncompress_command, verbose => $verbose);
+    unless ($success) {
+	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+	&my_die("Unable to uncompress file: $uri -> $compUri: $error_code", 
+		$exp_id,$exp_name,$class_id,$uri, $PS_EXIT_SYS_ERROR);
+    }
+    my $compress_command = "$fpack -h -s 8 -S $tempReal >  $compReal";
     print STDERR "$compReal $uriReal $compress_command\n";
-    my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
+    ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
 	run(command => $compress_command, verbose => $verbose);
     unless ($success) {
 	$error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	&my_die("Unable to compress file: $uri -> $compUri: $error_code", $exp_id,$exp_name,$class_id,$uri, $PS_EXIT_SYS_ERROR);
+	&my_die("Unable to compress file: $uri -> $compUri: $error_code", 
+		$exp_id,$exp_name,$class_id,$uri, $PS_EXIT_SYS_ERROR);
     }
     my $database_command = "$regtool -updateprocessedimfile -exp_id $exp_id -class_id $class_id -set_state compressed";
@@ -135,17 +155,19 @@
 
 	# If that die is removed, this will compress things as well.
-	&my_die("Output compressed file already exists: $compUri\n", 
-		$exp_id,$exp_name,$class_id,$uri,$PS_EXIT_SYS_ERROR) 
-	    if ($ipprc->file_exists($compUri));
-	my $compReal= $ipprc->file_resolve( $compUri, 'create');
-	my $compress_command = "$fpack -h -s 8 -S $uriReal >  $compReal";
-	print STDERR "$compReal $uriReal $compress_command\n";
-	my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
-	    run(command => $compress_command, verbose => $verbose);
-	unless ($success) {
-	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
-	    &my_die("Unable to compress file: $uri -> $compUri: $error_code", 
-		    $exp_id,$exp_name,$class_id,$uri, $PS_EXIT_SYS_ERROR);
-	}
+	# 
+# 	&my_die("Output compressed file already exists: $compUri\n", 
+# 		$exp_id,$exp_name,$class_id,$uri,$PS_EXIT_SYS_ERROR) 
+# 	    if ($ipprc->file_exists($compUri));
+# 	my $compReal= $ipprc->file_resolve( $compUri, 'create');
+	
+# 	my $compress_command = "$fpack -h -s 8 -S $uriReal >  $compReal";
+# 	print STDERR "$compReal $uriReal $compress_command\n";
+# 	my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
+# 	    run(command => $compress_command, verbose => $verbose);
+# 	unless ($success) {
+# 	    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+# 	    &my_die("Unable to compress file: $uri -> $compUri: $error_code", 
+# 		    $exp_id,$exp_name,$class_id,$uri, $PS_EXIT_SYS_ERROR);
+# 	}
     }
 
