Index: trunk/pstamp/scripts/pstamp_job_run.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_job_run.pl	(revision 37259)
+++ trunk/pstamp/scripts/pstamp_job_run.pl	(revision 37318)
@@ -91,4 +91,6 @@
 my $ppBackgroundStack = can_run('ppBackgroundStack') or (warn "Can't find ppBackgroundStack" and $missing_tools = 1);
 my $stack_bkg_mk_mdc = can_run('stack_bkg_mk_mdc.pl') or (warn "Can't find stack_bkg_mk_mdc.pl" and $missing_tools = 1);
+my $fpack = can_run('fpack') or (warn "Can't find fpack" and $missing_tools = 1);
+
 
 if ($missing_tools) {
@@ -146,7 +148,4 @@
     my $mask;
     my $variance;
-
-    # We need to background restore HERE
-    # and change the image file supplied to ppstamp
 
     my $fileArgs = " -file $params->{image}";
@@ -224,5 +223,5 @@
 
     # unless the stage is stack_summary we use ppstamp to make postage stamps (including -wholefile)
-    # otherwise we make copies of the input files to the outputs
+    # if stage is stack_summary we make copies of the input files to the outputs
     my $use_ppstamp = ($stage ne 'stack_summary');
 
@@ -243,5 +242,86 @@
             $exitStatus = $PS_EXIT_SYS_ERROR;
         }
-        # XXX: if stage is stack deal with EXP and NUM images if selected
+
+        # if stage is stack deal with EXP and NUM images if selected by running ppstamp again
+        if (!$exitStatus and $stage eq 'stack' and ($options & ($PSTAMP_SELECT_EXP | $PSTAMP_SELECT_NUM))) {
+            # XXX Here I am assuming that nothing relevant gets added to $argString 
+            # Need to check
+            my $roiArgs = $params->{job_args};
+
+            # XXX: define expnum and exptime input images in the params file so that we don't have to
+            # make assumptions about the file rules here
+            my $pathBase = $params->{stack_path_base};
+            $pathBase = $params->{path_base} unless defined $pathBase;
+            unless (defined $pathBase) {
+                my_die("stack path base undefined found when creating exp or expnum image", $job_id, $PS_EXIT_PROG_ERROR);
+            }
+            $pathBase .= ".unconv";
+            my $tmpBase = "$outputBase.TMP";
+            # nocompress because exp image gets corrupted if we do
+            my $command = "$ppstamp $tmpBase $roiArgs  -nocompress -stage stack";
+            # XXX: use file rules
+            # Image is the EXP (exptime) image
+            $command .= " -file $pathBase.exp.fits";
+            # treat number image as mask
+            $command .= " -mask $pathBase.num.fits" if ($options & $PSTAMP_SELECT_NUM);
+
+            $command .= " -forheader $calibfile" if $calibfile;
+
+            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                run(command => $command, verbose => $verbose);
+
+            if (WIFEXITED($error_code)) {
+                $exitStatus = WEXITSTATUS($error_code);
+            } else {
+                print STDERR "ppstamp failed error_code: $error_code\n";
+                $exitStatus = $PS_EXIT_SYS_ERROR;
+            }
+
+            # command to compress fits files to stdout using gzip
+            my $fpack_gzip = "$fpack -g -S";
+            $fpack_gzip .= " -D" unless $save_temps;
+
+            if (!$exitStatus and ($options & $PSTAMP_SELECT_NUM)) {
+                # rename the num image if selected since it is not a mask
+                # Change .exp.mk to .num
+                my $tmpName = "$tmpBase.mk.fits";
+                my $numName = "$outputBase.num.fits";
+                # optionally fpack it while we're here unless user wants uncompressed images
+                unless ($options & $PSTAMP_SELECT_UNCOMPRESSED) {
+                    my $command = "$fpack_gzip $tmpName > $numName";
+                    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                        run(command => $command, verbose => $verbose);
+
+                    if (WIFEXITED($error_code)) {
+                        $exitStatus = WEXITSTATUS($error_code);
+                    } else {
+                        print STDERR "ppstamp failed error_code: $error_code\n";
+                        print STDERR "@$stderr_buf\n";
+                        $exitStatus = $PS_EXIT_SYS_ERROR;
+                    }
+                } elsif (!$exitStatus) {
+                    rename $tmpName, $numName 
+                        or my_die ("failed to rename $tmpName to $numName", $job_id, $PS_EXIT_UNKNOWN_ERROR);
+                }
+
+            }
+            # fpack the exp image if not -nocompress
+            unless ($exitStatus or ($options & $PSTAMP_SELECT_UNCOMPRESSED)) {
+                my $command = "$fpack_gzip $tmpBase.fits > $outputBase.exp.fits";
+                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                    run(command => $command, verbose => $verbose);
+
+                if (WIFEXITED($error_code)) {
+                    $exitStatus = WEXITSTATUS($error_code);
+                } else {
+                    print STDERR "ppstamp failed error_code: $error_code\n";
+                    print STDERR "@$stderr_buf\n";
+                    $exitStatus = $PS_EXIT_SYS_ERROR;
+                }
+            } elsif (!$exitStatus) {
+                rename "$tmpBase.fits", "$outputBase.exp.fits"
+                    or my_die ("failed to rename $tmpBase.fits to $outputBase.exp.fits", $job_id, $PS_EXIT_UNKNOWN_ERROR);
+            }
+        }
     } else {
         $exitStatus = justCopyFiles($outputBase, \$options, $params);
