Index: trunk/pstamp/scripts/detectability_respond.pl
===================================================================
--- trunk/pstamp/scripts/detectability_respond.pl	(revision 33142)
+++ trunk/pstamp/scripts/detectability_respond.pl	(revision 33248)
@@ -20,15 +20,7 @@
 use PS::IPP::PStamp::RequestFile qw( :standard );
 use PS::IPP::PStamp::Job qw( :standard );
-use PS::IPP::Config qw($PS_EXIT_SUCCESS
-                       $PS_EXIT_UNKNOWN_ERROR
-                       $PS_EXIT_SYS_ERROR
-                       $PS_EXIT_CONFIG_ERROR
-                       $PS_EXIT_PROG_ERROR
-                       $PS_EXIT_DATA_ERROR
-                       $PS_EXIT_TIMEOUT_ERROR
-                       metadataLookupStr
-                       metadataLookupBool
-                       caturi
-                       );
+use PS::IPP::Config qw( :standard );
+use PS::IPP::Metadata::List qw( parse_md_list );
+
 use Astro::FITS::CFITSIO qw( :constants );
 Astro::FITS::CFITSIO::PerlyUnpacking(1);
@@ -75,4 +67,6 @@
 
 my $ipprc = PS::IPP::Config->new();
+my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
+
 
 if (!$dbserver) {
@@ -231,4 +225,8 @@
 	my $mjd_max = $mjd + 1;
 	
+        my $req_filter;
+        if ($filter ne 'Not_Set') {
+            $req_filter = $filter . '%';
+        }
 	
 	# Call the PStamp code to find the images that contain the target on the given MJD in the specified filter.
@@ -238,6 +236,5 @@
 					  $fpa_id,undef,undef,
 					  $option_mask,$need_magic,
-					  # $ra,$dec,
-					  $mjd_min,$mjd_max,$filter . ".00000",undef,$verbose);  
+					  $mjd_min,$mjd_max,$req_filter,undef,$verbose);  
 	
 	foreach my $this_image_ref (@{ $pstamp_images_ref }) {
@@ -395,7 +392,12 @@
 	my $weight= $query{$fpa_id}{WEIGHT}[$index];
 	my $stage = $query{$fpa_id}{STAGE}[$index];
-	# if there's a fault, then we can't process this image.
-	if (($fault != 0)||($query{$fpa_id}{BAD_COMPONENT}[$index] == 1)) {
-	    $query{$fpa_id}{PROC_ERROR}[$index] = 23;
+	my $stage_id = $query{$fpa_id}{STAGE_ID}[$index];
+	my $component = $query{$fpa_id}{COMPONENT_ID}[$index];
+        # print "Input is from $stage $stage_id $component\n";
+
+	# if there's a fault or quality problem, then we can't process this image.
+	# if (($fault != 0)||($query{$fpa_id}{BAD_COMPONENT}[$index] == 1)) {
+	if ($fault = check_component($stage, $stage_id, $component, $imagedb)) {
+	    $query{$fpa_id}{PROC_ERROR}[$index] = $fault;
 	    
 	    $query{$fpa_id}{NPIX}[$index] = 0;
@@ -789,4 +791,62 @@
 }
 
+sub check_component {
+    my ($stage, $stage_id, $component, $imagedb) = @_;
+
+    print "Checking status of component $stage $stage_id $component\n";
+
+    my $command;
+    if ($stage eq 'diff') {
+        $command = "difftool -diffskyfile -diff_id $stage_id -skycell_id $component";
+    } elsif ($stage eq 'stack') {
+        $command = "stacktool -sumskyfile -stack_id $stage_id";
+    } elsif ($stage eq 'warp') {
+        $command = "warptool -warped -warp_id $stage_id -skycell_id $component";
+    } elsif ($stage eq 'chip') {
+        $command = "chiptool -processedimfile -chip_id $stage_id -class_id $component";
+    } else {
+        die("check_component not implemented for stage $stage yet");
+    }
+
+    $command .= " -dbname $imagedb";
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+	run(command => $command, verbose => $verbose);
+    unless ($success) {
+        my $rc = $error_code >> 8;
+        carp "$command failed $error_code $rc";
+        exit $PS_EXIT_UNKNOWN_ERROR;
+    }
+    my $output = join "", @$stdout_buf;
+    my $metadata = $mdcParser->parse($output);
+    my $results = parse_md_list($metadata);
+    if (scalar @$results != 1) {
+        carp "$command returned too many components: " . scalar @$results;
+        exit $PS_EXIT_UNKNOWN_ERROR;
+    }
+    my $it = $results->[0];
+
+    if ($stage eq 'stack') {
+        $it->{data_state} = $it->{state};
+    }
+
+    my $return_status = 0;
+    if ($it->{quality}) {
+        print "  Bad quality: $it->{quality}\n";
+        $return_status = $PSTAMP_GONE;
+    } elsif ($it->{fault}) {
+        print "  Faulted: $it->{fault}\n";
+        $return_status = $PSTAMP_GONE;
+    } elsif ($it->{data_state} ne 'full') {
+        # XXX does this work for stack?
+        carp "  Faulted: $it->{data_state}\n";
+        $return_status = $PSTAMP_GONE;
+    } else {
+        print "  Component ok.\n";
+    }
+
+    return $return_status;
+}
+
 sub my_die {
     my $message = shift;
