Index: /trunk/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm
===================================================================
--- /trunk/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 27952)
+++ /trunk/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 27953)
@@ -248,5 +248,7 @@
         # With the mdc file it has all of the information that it needs.
         $image_name   = "PPIMAGE.CHIP";
-        $mask_name    = "PPIMAGE.CHIP.MASK";
+        # $mask_name    = "PPIMAGE.CHIP.MASK";
+        # XXX: really need to handle this properly!
+        $mask_name    = "PSASTRO.OUTPUT.MASK";
         $weight_name  = "PPIMAGE.CHIP.VARIANCE";
         $cmf_name     = "PSPHOT.OUTPUT";
@@ -430,14 +432,25 @@
         $out->{imagedb} = $imagedb;
 
+        my $mask_base;
+        if ($want_astrom) {
+            if (! defined $out->{astrom}) {
+                if (! find_astrometry($ipprc, $imagedb, $out, $verbose)) {
+                    print STDERR "failed to find astrometry for $stage $stage_id\n";
+                    next;
+                }
+            }
+            # XXX: do this right by looking at the recipe
+            $mask_base = $out->{cam_path_base};
+        } else {
+            $mask_base = $base;
+        }
+
         if ($base) {
             $out->{image}  = $ipprc->filename($image_name,  $base, $class_id) if $image_name;
-            $out->{mask}   = $ipprc->filename($mask_name,   $base, $class_id) if $mask_name;
+            $out->{mask}   = $ipprc->filename($mask_name,   $mask_base, $class_id) if $mask_name;
             $out->{weight} = $ipprc->filename($weight_name, $base, $class_id) if $weight_name;
             $out->{cmf}    = $ipprc->filename($cmf_name, $base, $class_id) if $cmf_name;
             $out->{psf}    = $ipprc->filename($psf_name, $base, $class_id) if $psf_name;
             $out->{backmdl}= $ipprc->filename($backmdl_name, $base, $class_id) if $backmdl_name;
-        }
-        if ($want_astrom and ! defined $out->{astrom}) {
-            $out->{astrom} = find_astrometry($ipprc, $imagedb, $image, $verbose);
         }
 
@@ -842,9 +855,10 @@
 # cache of results of ppConfigDump
 my %astromSources;
-my $last_exp_id = 0;
-my $lastAstromFile;
+# my $last_exp_id = 0;
+my $lastCamRun;
 
 # find the astrometry file for a given exposure
 # return undef if no completed camRun exists
+# XXX Right now this probably only works cameras where the astrometry is done at the camera stage
 sub find_astrometry {
     my $ipprc = shift;
@@ -856,10 +870,13 @@
 
     my $exp_id = $image->{exp_id};
-    if (($exp_id eq $last_exp_id) and $lastAstromFile) {
+    if (defined $lastCamRun) {
         # running camtool 60 times is really expensive when the answer is the same every time
-        return $lastAstromFile;
-    }
-    $last_exp_id = 0;
-    $lastAstromFile = undef;
+        $image->{astrom} = $lastCamRun->{astrom};
+        $image->{cam_path_base} = $lastCamRun->{path_base};
+        return 1;
+    }
+    # $last_exp_id = 0;
+    # $lastAstromFile = undef;
+    $lastCamRun = undef;
 
     my $camRun = getCamRunByExpID($exp_id);
@@ -871,5 +888,5 @@
         unless ($success) {
             print STDERR @$stderr_buf;
-            return undef;
+            return 0;
         }
 
@@ -879,9 +896,9 @@
         if (!$output) {
             print STDERR "no output returned from $command\n" if $verbose;
-            return undef;
+            return 0;
         }
         my $camruns = parse_md_fast($mdcParser, $output);
         if (!$camruns) {
-            return undef;
+            return 0;
         }
 
@@ -897,5 +914,5 @@
         if (!$camRun) {
             # no cam runs for this exposure id therefore best astrometry is whatever is in the header
-            return undef;
+            return 0;
         }
         updateCamRunCache($camRun);
@@ -927,9 +944,15 @@
     my $astromFile = $ipprc->filename($astromSource, $camRoot);
     if ($astromFile) {
-        $lastAstromFile = $astromFile;
-        $last_exp_id = $exp_id;
-    }
-
-    return $astromFile;
+        $lastCamRun = $camRun;
+        $lastCamRun->{astrom} = $astromFile;
+#        $last_exp_id = $exp_id;
+    } else {
+        return 0;
+    }
+
+    $image->{astrom} = $astromFile;
+    $image->{cam_path_base} = $camRoot;
+
+    return 1;
 }
 
Index: /trunk/pstamp/scripts/pstamp_job_run.pl
===================================================================
--- /trunk/pstamp/scripts/pstamp_job_run.pl	(revision 27952)
+++ /trunk/pstamp/scripts/pstamp_job_run.pl	(revision 27953)
@@ -89,4 +89,9 @@
     $argString = $params->{job_args};
 
+    # XXX: should we do any other sanity checking?
+    my_die("argument list is empty", $job_id, $PS_EXIT_DATA_ERROR) if !$argString;
+
+    # XXX: remove -astrom from argString and add it here
+
     $argString .= " -file $params->{image}";
     $argString .= " -mask $params->{mask}";
@@ -94,7 +99,4 @@
         $argString .= " -variance $params->{weight}";
     }
-
-    # XXX: should we do any other sanity checking?
-    my_die("argument list is empty", $job_id, $PS_EXIT_DATA_ERROR) if !$argString;
 
     my $command = "$ppstamp $outputBase $argString";
