Index: trunk/pstamp/lib/PStamp/Job.pm
===================================================================
--- trunk/pstamp/lib/PStamp/Job.pm	(revision 19001)
+++ trunk/pstamp/lib/PStamp/Job.pm	(revision 19161)
@@ -51,8 +51,4 @@
         my $results = lookup_bycoord($ipprc, $image_db, $x, $y, $mjd_min, $mjd_max, $filter, $verbose);
 
-        # If the type is raw we're done, otherwise we have more work to do
-        if ($img_type eq "raw") {
-            return $results;
-        }
         $req_type = "byexp";
         $id = $results->{exp_name};
@@ -90,4 +86,9 @@
     my $want_astrom;
     my $use_class_id;
+
+    # special class_id value "null" means all 
+    if ($class_id eq "null") {
+        $class_id = undef;
+    }
 
     if ($img_type eq "raw") {
@@ -112,6 +113,4 @@
         $weight_name  = "PSWARP.OUTPUT.WEIGHT";
         $base_name    = "path_base"; # name of the field for the warptool output
-        $want_astrom  = 1;
-        $use_class_id = 1;
     } elsif ($img_type eq "diff") {
         $command = "$difftool -diffskyfile -dbname $image_db";
@@ -179,6 +178,6 @@
             $out->{weight} = $ipprc->filename($weight_name, $base, $class_id) if $weight_name;
         }
-        $out->{astrom} = find_astrometry($ipprc, $image) if $want_astrom;
         $out->{camera} = $camera;
+        $out->{astrom} = find_astrometry($ipprc, $image_db, $image, $verbose) if $want_astrom;
 
         push @$output, $out;
@@ -255,11 +254,67 @@
 }
 
+# find the astrometry file for a given exposure
+# return undef if no completed camRun exists
 sub find_astrometry {
-        # 
-        #XXX need to get this from the config as in warp_overlap.pl
-#        $astrom_name = "PSASTRO.OUTPUT.MEF";
-#        $astrom_name = "PSASTRO.OUTPUT";
-#       $out->{astrom} = $ipprc->filename($astrom_name, $base, $class_id) if $astrom_name;
-    return undef;
+    my $ipprc = shift;
+    my $image_db = shift;
+    my $image = shift;      # hashref to output of the lookup tool
+    my $verbose = shift;
+
+    my $missing_tools;
+    my $camtool = can_run("camtool") or (warn "Can't find camtool" and $missing_tools = 1);
+    my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
+    if ($missing_tools) {
+        warn("Can't find required tools.");
+        exit ($PS_EXIT_CONFIG_ERROR);
+    }
+
+    my $command = "$camtool -dbname $image_db -processedexp -exp_id $image->{exp_id}";
+    # run the tool and parse the output
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                run(command => $command, verbose => $verbose);
+    unless ($success) {
+        print STDERR @$stderr_buf;
+        return undef;
+    }
+
+    my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
+
+    my $output = join "", @$stdout_buf;
+    if (!$output) {
+        print STDERR "no output returned from $command\n" if $verbose;
+        return undef;
+    }
+    my $camruns = parse_md_fast($mdcParser, $output);
+    if (!$camruns) {
+        return undef;
+    }
+
+    # If there are multiple cam runs for this exposure, take the last one
+    # assuming that it has the best astrometry 
+    my $camdata = pop @$camruns;
+    if (!$camdata) {
+        # no cam runs for this exposure id therefore best astrometry is whatever is in the header
+        return undef;
+    }
+    my $camRoot = $camdata->{path_base};
+    my $camera = $image->{camera};
+    my $astromSource;
+    {
+       my $command = "$ppConfigDump -camera $camera -dump-recipe PSWARP -";
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        unless ($success) {
+            $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
+            die("Unable to perform ppConfigDump: $error_code");
+        }
+        my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
+            die("Unable to parse metadata config doc");
+        $astromSource = metadataLookupStr($metadata, 'ASTROM.SOURCE');
+    }
+
+    my $astromFile = $ipprc->filename($astromSource, $camRoot);
+
+    return $astromFile;
 }
 
