Index: trunk/tools/dlapstacks
===================================================================
--- trunk/tools/dlapstacks	(revision 33438)
+++ trunk/tools/dlapstacks	(revision 33521)
@@ -20,4 +20,8 @@
 use DBI;
 use PS::IPP::Config 1.01 qw( :standard );
+use File::Temp qw( tempfile tempdir );
+use File::Basename;
+use IPC::Cmd 0.36 qw( can_run run );
+
 
 my $ipprc = PS::IPP::Config->new();
@@ -25,14 +29,22 @@
 
 my ($requested_filter, $no_display, $skycell_id, $verbose, $ra, $dec, $convolved);
-my ($singleframe);
+my ($singleframe, $fitToFrame, $deasin, $save_temps);
+
+my $temproot = '/local/ipp/tmp';
+my $outdir;
+
 
 GetOptions(
     'filter|f=s'    => \$requested_filter,  # restrict to a single filter 
-    'skycell_id=s'  => \$skycell_id,        # don't bother with 
-    'convolved'     => \$convolved,         # display convolved stack images instead of unconvolved
-    'label=s'       => \$label,             # use a different label (careful!)
-    'tess_id=s'     => \$tess_id,           # 
+    'skycell_id|s=s'=> \$skycell_id,        # don't bother with 
+    'convolved|c'   => \$convolved,         # display convolved stack images instead of unconvolved
+    'fit'           => \$fitToFrame,
+    'deasin|d'      => \$deasin,
+    'label|l=s'     => \$label,             # use a different label (careful!)
+    'tess_id|t=s'   => \$tess_id,           # 
+    'outdir|o=s'    => \$outdir,
     'no-display|n'  => \$no_display,        # don't run ds9 just print the filenames
     'single'        => \$singleframe,
+    'save-temps'    => \$save_temps,
     'verbose|v'     => \$verbose,
 ) or pod2usage( 2 );
@@ -46,4 +58,14 @@
 pod2usage( -msg => 'if ra is suplied <dec> must be supplied as well', -exitval => 2)
     if (defined $ra and ! defined $dec);
+
+my $missing_tools;
+my $ppstamp;
+if ($deasin) {
+    $ppstamp = can_run('ppstamp') or (warn "Can't find ppstamp") and $missing_tools = 1;
+}
+if ($missing_tools) {
+    # warn("Can't find required tools.");
+    exit($PS_EXIT_CONFIG_ERROR);
+}
 
 my $extension;
@@ -154,4 +176,17 @@
         $imagesize = 680;
     }
+    if ($deasin) {
+        if ($outdir) {
+            print "Saving deasined files to $outdir\n";
+            # user specified output directory save the files there
+            if (!-d $outdir) {
+                mkdir $outdir or die "failed to mkdir $outdir\n";
+            }
+        } else {
+            # otherwise use a temporary directory
+            $outdir = tempdir ("$temproot/dlapstacks.temp.XXXX", CLEANUP => !$save_temps);
+            print "deasined files will be saved to $outdir\n" if $save_temps;
+        }
+    }
     # Build the ds9 argument list
     my @args;
@@ -159,7 +194,27 @@
     foreach my $f (@files) {
         next if ! $f;
-        my $filename = $f->{path_base} . ".$extension";
-        my $resolved = $ipprc->file_resolve($filename);
-        next if !$resolved;
+        my $pathname = $f->{path_base} . ".$extension";
+        my $filename = $ipprc->file_resolve($pathname);
+        next if !$filename;
+        # run ppstamp to undo the asin scaling. The output from ppstamp is linear F32 images
+        if ($deasin) {
+            my $base = basename($pathname);
+            my $destbase = $base;
+
+            # drop the .fits for the output path for ppstamp
+            $destbase =~ s/\.fits//g;
+
+            my $command = "$ppstamp -wholefile -file $filename $outdir/$destbase";
+            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                run(command => $command, verbose => $verbose);
+            unless ($success) {
+                print STDERR join "", @$stderr_buf;
+                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+                die("Unable to perform ppstamp: $error_code\n");
+            }
+
+            # new filename for ds9
+            $filename = "$outdir/$base";
+        }
 
         if ($f->{X} > 0) {
@@ -184,10 +239,12 @@
         # loaded. pan thatone and then matching the wcs for the frames.
         # Also that allows V0 and V3 skycells to have east to the left orientation
-        # push @args, ($resolved, $panargs);
-        push @args, $resolved;
+        # push @args, ($filename, $panargs);
+        push @args, $filename;
     }
     my $ds9cmd = "ds9 -geom 1024x1024 @args $panargs";
     $ds9cmd .= " -single" if $singleframe;
+    $ds9cmd .= " -zoom to fit" if $fitToFrame;
     $ds9cmd .= " -match frames wcs";
+    $ds9cmd .= " -frame first" if $singleframe;
     print "$ds9cmd\n" if $verbose;
     system $ds9cmd;
