Index: /trunk/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm
===================================================================
--- /trunk/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 28014)
+++ /trunk/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm	(revision 28015)
@@ -632,21 +632,23 @@
     if (($img_type eq "raw") or ($img_type eq "chip")) {
 
-        my $runs = lookup_runs_by_cam_id_and_coords($ipprc, $imagedb, $img_type,
+        my $chips = lookup_by_cam_id_and_coords($ipprc, $imagedb, $img_type,
             $ra, $dec, $need_magic, $dateobs_begin, $dateobs_end, $filter, $data_group, $verbose);
 
-        # lookup is going to filter these we don't need to do it here
-#        $runs = filterRuns($img_type, $need_magic, $runs, $verbose);
-        foreach my $run (@$runs) {
-            next if $component and ($run->{component} ne $component);
-            my $these_results = lookup($ipprc, $rowList, $imagedb, "byid", $img_type, $run->{id},
-                $tess_id, $run->{component}, $need_magic, 
-                $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
-
-            next if !$these_results;
-            push @$results, @$these_results;
+        if (!$chips or scalar @$chips == 0) {
+            setErrorCodes($rowList, $PSTAMP_NO_IMAGE_MATCH);
+        } else {
+            foreach my $chip (@$chips) {
+                next if $component and ($chip->{component} ne $component);
+                my $these_results = lookup($ipprc, $rowList, $imagedb, "byid", $img_type, $chip->{id},
+                    $tess_id, $chip->{component}, $need_magic, 
+                    $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
+
+                next if !$these_results;
+                push @$results, @$these_results;
+            }
         }
         
     } else {
-        # this should be checked elsewhere
+        # this should have been checked elsewhere
         die "unexpected image type $img_type" if ($img_type ne "warp") 
                                               and ($img_type ne "stack") and ($img_type ne "diff");
@@ -654,11 +656,15 @@
         my $skycells = lookup_skycell_by_coords($ipprc, $tess_id, $component, $ra, $dec, $verbose);
 
-        foreach my $skycell (@$skycells) {
-            my $these_results = lookup($ipprc, $rowList, $imagedb, "byskycell", $img_type, undef,
-                $skycell->{tess_id}, $skycell->{component}, $need_magic, 
-                $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
-
-            next if !$these_results;
-            push @$results, @$these_results;
+        if (!$skycells or scalar @$skycells == 0) {
+            setErrorCodes($rowList, $PSTAMP_NO_IMAGE_MATCH);
+        } else {
+            foreach my $skycell (@$skycells) {
+                my $these_results = lookup($ipprc, $rowList, $imagedb, "byskycell", $img_type, undef,
+                    $skycell->{tess_id}, $skycell->{component}, $need_magic, 
+                    $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
+
+                next if !$these_results;
+                push @$results, @$these_results;
+            }
         }
     }
@@ -667,8 +673,9 @@
 }
 
-# lookup_runs_by_cam_id_and_coords
-# given an ra, dec, and optionally other paramters, find camera runs for exposures
+# lookup__by_cam_id_and_coords
+# given an ra, dec, and optionally other parameters, find camera runs for exposures
 # that are within some distance of the coordinates
-sub lookup_runs_by_cam_id_and_coords {
+
+sub lookup_by_cam_id_and_coords {
     my $ipprc      = shift;
     my $imagedb   = shift;
@@ -686,6 +693,8 @@
 
     {
+        my $search_radius = 1.6; # XXX: this should be camera specific
+
         my $command = "$camtool -dbname $imagedb -processedexp -pstamp_order";
-           $command .= " -ra $ra -decl $dec -radius 1.6";
+           $command .= " -ra $ra -decl $dec -radius $search_radius";
            $command .= " -dateobs_begin $dateobs_begin" if $dateobs_begin;
            $command .= " -dateobs_end   $dateobs_end"   if $dateobs_end  ;
@@ -693,5 +702,5 @@
            # NOTE: we are applying the data_group to the camera run.
            # If we're looking for chip stage images there is no guarentee that 
-           # chipRun.data_group = camRun.data_group. In practice this is almost
+           # chipRun.data_group eq camRun.data_group. In practice this is almost
            # always the case. If this turns out to be a problem we can defer
            # the data_group test to when we look up the chipProcessedImfiles
@@ -705,5 +714,5 @@
         return undef;
     }
-    my $runs;
+    my $components;
     my $last_exp_id = 0;
     foreach my $camRun (@$camruns) {
@@ -773,26 +782,29 @@
 
         # build the hash to return
-        my $run = {
+        # XXX: very few of the entries in this hash are currently used.
+        # Used are: id, component
+        # I had plans to use it for the camRun lookup optimization, but the cache solves the
+        # problem
+        my $comp = {
             exp_id    => $camRun->{exp_id},
             exp_name  => $camRun->{exp_name},
             chip_id   => $camRun->{chip_id},
             cam_id    => $camRun->{cam_id},
-            astrom    => $astrom_resolved,  # XXX: is astrom used?
             class_id  => $class_id,
             component => $class_id
         };
         if ($img_type eq "chip") {
-            $run->{id} = $camRun->{chip_id};
-            $run->{state} = $camRun->{chip_state};
-            $run->{magicked} = $camRun->{chip_magicked};
-        } else {
-            $run->{id} = $camRun->{exp_id};
-            $run->{state} = 'full';
-            $run->{magicked} = $camRun->{raw_magicked};
-        }
-        push @$runs, $run;
-    }
-
-    return $runs;
+            $comp->{id} = $camRun->{chip_id};
+            $comp->{state} = $camRun->{chip_state};
+            $comp->{magicked} = $camRun->{chip_magicked};
+        } else {
+            $comp->{id} = $camRun->{exp_id};
+            $comp->{state} = 'full';
+            $comp->{magicked} = $camRun->{raw_magicked};
+        }
+        push @$components, $comp;
+    }
+
+    return $components;
 }
 
Index: /trunk/pstamp/scripts/pstampparse.pl
===================================================================
--- /trunk/pstamp/scripts/pstampparse.pl	(revision 28014)
+++ /trunk/pstamp/scripts/pstampparse.pl	(revision 28015)
@@ -528,15 +528,17 @@
         my $data_state = $image->{data_state};
         $data_state = $run_state if $stage eq 'stack';
-        if (($run_state eq 'goto_purged') or ($data_state eq 'purged') or
-            ($run_state eq 'drop') or 
-            ($run_state eq 'error_cleaned') or ($data_state eq 'error_cleaned') or
-            ($run_state eq 'goto_scrubbed') or ($data_state eq 'scrubbed')) {
+        if (($run_state =~ /purged/) or ($run_state =~ /scrubbed/)) {
             # image is gone and it's not coming back
             $newState = 'stop';
             $fault = $PSTAMP_GONE;
+        } elsif (($run_state eq "error_cleaned") or ($data_state eq 'error_cleaned')) {
+            # if cleanup had an error don't get the user's hopes up
+            $newState = 'stop';
+            $fault = $PSTAMP_NOT_AVAILABLE;
         } elsif (($data_state ne 'full') or ($need_magic and ($image->{magicked} < 0))) {
             if ($stage eq 'chip') {
                 my $burntool_state = $image->{burntool_state};
-                if ($burntool_state and (abs($burntool_state) < 14)) {
+                # XXX: get value 13 from ppImage recipe 
+                if ($burntool_state and (abs($burntool_state) < 13)) {
                     $newState = 'stop';
                     $fault = $PSTAMP_NOT_AVAILABLE;
@@ -708,9 +710,14 @@
             my $data_state = $image->{data_state};
             $data_state = $run_state if $stage eq "stack";
-            if (($run_state eq 'goto_purged') or ($data_state eq 'purged') or
-                ($run_state eq 'goto_scrubbed') or ($data_state eq 'scrubbed')) {
+#            if (($run_state eq 'goto_purged') or ($data_state eq 'purged') or
+#                ($run_state eq 'goto_scrubbed') or ($data_state eq 'scrubbed')) {
+            if (($run_state =~ /purged/) or ($run_state =~ /scrubbed/)) {
                 # image is gone and it's not coming back
                 $newState = 'stop';
                 $fault = $PSTAMP_GONE;
+            } elsif ($run_state eq "error_cleaned") {
+                # if cleanup had an error don't get the user's hopes up
+                $newState = 'stop';
+                $fault = $PSTAMP_NOT_AVAILABLE;
             } elsif (($data_state ne 'full') or ($need_magic and ($image->{magicked} < 0))) {
                 # wait for update unless the customer asks us to not to
