Index: trunk/PStamp/lib/PStamp/Job.pm
===================================================================
--- trunk/PStamp/lib/PStamp/Job.pm	(revision 19761)
+++ trunk/PStamp/lib/PStamp/Job.pm	(revision 20074)
@@ -44,6 +44,40 @@
     # we die in response to bad data in request files
     die "Unknown req_type: $req_type" if ($req_type ne "byid") and ($req_type ne "byexp")
-                                        and ($req_type ne "bycoord");
-    if ($req_type eq "bycoord") {
+                                        and ($req_type ne "bycoord") and ($req_type ne "bydiff");
+    if (($req_type eq "byid") and ($img_type eq "diff")) {
+        # lookups of all of the information for diff images requires a two level lookup to
+        # get the exposure information. Switching the req_type allows us to keep that code
+        # in one place
+        $req_type = "bydiff";
+    }
+    if ($req_type eq "bydiff") {
+        my $results = lookup_bydiff($ipprc, $image_db, $id, $img_type, $verbose);
+        if (!$results) {
+            return undef;
+        }
+        if ($img_type eq "diff") {
+            return [$results];
+        } elsif (($img_type eq "raw") or ($img_type eq "chip")) {
+            $req_type = "byexp";
+            $id = $results->{exp_name};
+            return undef if !$id;
+            # fall through and lookup byexp
+        } elsif ($img_type eq "warp") {
+            $req_type = "byid";
+            $id = $results->{warp_id};
+            return undef if !$id;
+            # fall through and lookup by warp_id 
+        } elsif ($img_type eq "stack") {
+            $req_type = "byid";
+            $id = $results->{stack_id};
+            return undef if !$id;
+            # fall though and lookup by stack id
+        } else {
+            # shouldn't I check this elsewhere?
+            print STDERR "Error: $img_type is an unknown image type\n";
+            return undef;
+        }
+
+    } elsif ($req_type eq "bycoord") {
 
         # run
@@ -52,4 +86,7 @@
         my $results = lookup_bycoord($ipprc, $image_db, $x, $y, $mjd_min, $mjd_max, $filter, $verbose);
 
+        # now take the results and lookup byexp
+        # XXX: This needs work. What stops results from returning multiple images????
+        # We should loop over the set of images returned building up the full results array
         $req_type = "byexp";
         $id = $results->{exp_name};
@@ -97,5 +134,5 @@
         $id_opt = "-exp_id";
         $command .= " -class_id $class_id" if $class_id;
-        $want_astrom = 0;
+        $want_astrom = 1;
         $use_class_id = 1;
     } elsif ($img_type eq "chip") {
@@ -162,4 +199,7 @@
     foreach my $image (@$images) {
         my $base;
+
+        next if ($img_type eq "warp" and $image->{ignored});
+
         if ($base_name) {
             $base = $image->{$base_name};
@@ -170,13 +210,13 @@
         if (!$camera) {
             # This assumes that all images have the same camera
-            $ipprc->define_camera($image->{camera});
             $camera = $image->{camera};
+            $ipprc->define_camera($camera);
         }
         my $out = {};
         $out->{exp_id} = $image->{exp_id};
+        $out->{exp_name} = $image->{exp_name};
         $out->{image}  = $image->{uri};
         $out->{state}  = $image->{state}; # state is undef for rawExp, but that's ok
         $class_id = $image->{class_id} if $use_class_id;
-
 
         # find the mask and weight images
@@ -199,4 +239,103 @@
 
     return $output;
+}
+sub lookup_bydiff {
+    my $ipprc    = shift;
+    my $image_db = shift;
+    my $id       = shift;
+    my $img_type = shift;
+    my $verbose = shift;
+
+    my $missing_tools;
+    my $difftool = can_run('difftool') or (warn "Can't find difftool" and $missing_tools = 1);
+    if ($missing_tools) {
+        warn("Can't find required tools.");
+        exit ($PS_EXIT_CONFIG_ERROR);
+    }
+
+    my $command = "$difftool -diffskyfile -diff_id $id -dbname $image_db";
+
+    # run the tool and parse the output
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                run(command => $command, verbose => $verbose);
+    unless ($success) {
+        # not sure if we should die here
+        print STDERR @$stderr_buf;
+        return undef;
+    }
+
+    my $buf = join "", @$stdout_buf;
+    if (!$buf) {
+        return undef;
+    }
+
+    my $mdcParser = PS::IPP::Metadata::Config->new;
+    my $images = parse_md_fast($mdcParser, $buf)
+        or die ("Unable to parse metadata config doc");
+
+    my $n = @$images;
+    if ($n > 1) {
+        die ("difftool returned an unexpected number of diffskyfiles: $n");
+    } elsif ($n == 0) {
+        return undef;
+    }
+
+    my $image = $images->[0];
+
+    # The standard way to do a diff is warp - stack 
+    # so we interpret the requested image in that way
+
+    # XXX the query currently returns max long long for null
+    # this line is ready if we switch the code to return zero for null
+    my $stack_id = $image->{stack_id_temp_1};
+    if ($stack_id and ($stack_id != 9223372036854775807)) {
+        $image->{stack_id} = $stack_id;
+    }
+
+    # XXX the query currently returns max long long for null
+    # this line is ready if we switch the code to return zero for null
+    my $warp_id =  $image->{warp_id_temp_0};
+    if ($warp_id and ($warp_id != 9223372036854775807)) {
+        $image->{warp_id} = $warp_id;
+
+        ## now use the warp and go get the exposure information
+        $command = "warptool -warped -warp_id $warp_id -dbname $image_db -limit 1";
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                    run(command => $command, verbose => $verbose);
+        unless ($success) {
+            # not sure if we should die here
+            print STDERR @$stderr_buf;
+            return undef;
+        }
+
+        my $buf = join "", @$stdout_buf;
+        if (!$buf) {
+            return undef;
+        }
+
+        my $mdcParser = PS::IPP::Metadata::Config->new;
+        my $warps = parse_md_fast($mdcParser, $buf)
+            or die ("Unable to parse metadata config doc");
+        my $warp = $warps->[0];
+
+        $image->{exp_id} = $warp->{exp_id};
+        $image->{exp_name} = $warp->{exp_name};
+        $image->{camera} = $warp->{camera};
+    }
+
+    if ($img_type eq "diff") {
+        # the $image is going to be returned directly so we need to do some more processing
+        $image->{image} = $image->{uri};
+        if ($image->{camera}) {
+            $ipprc->define_camera($image->{camera});
+            $image->{mask}   = $ipprc->filename("PPSUB.OUTPUT.MASK", $image->{path_base});
+            $image->{weight} = $ipprc->filename("PPSUB.OUTPUT.WEIGHT", $image->{path_base});
+        } else {
+            # XXX this will only happen if the minuend is not a warp
+            print STDERR "WARNING: cannot resolve camera so cannot get weight and mask images\n";
+        }
+    }
+
+    return $image;
 }
 
