Index: trunk/pstamp/scripts/detectability_respond.pl
===================================================================
--- trunk/pstamp/scripts/detectability_respond.pl	(revision 27788)
+++ trunk/pstamp/scripts/detectability_respond.pl	(revision 27859)
@@ -41,10 +41,12 @@
 my $EXTVER = 1.0;
 my $EXTNAME = 'MOPS_DETECTABILITY_RESPONSE';
-my ($req_id,$req_name,$product,$need_magic,$missing_tools);
-my ($request_file,$output,$dbname,$verbose,$save_temps);
+my ($req_id,$req_name,$product,$need_magic,$missing_tools,$project);
+my ($request_file,$output,$workdir,$dbname,$dbserver,$verbose,$save_temps);
 GetOptions(
-    'input=s'          =>     \$request_file,
+    'input=s'         =>      \$request_file,
     'output=s'        =>      \$output,
+    'workdir=s'       =>      \$workdir,
     'dbname=s'        =>      \$dbname,
+    'dbserver=s'      =>      \$dbserver,
     'verbose'         =>      \$verbose,
     'save-temps'      =>      \$save_temps,
@@ -55,5 +57,5 @@
 	   -exitval => 3,
     ) unless
-    defined $request_file and defined $output and defined $dbname;
+    defined $request_file and defined $output and defined $workdir and defined $dbname;
 
 my $detect_query_read = can_run('detect_query_read') or (warn "Can't find detect_query_read" and $missing_tools = 1);
@@ -71,16 +73,26 @@
 
 my $ipprc = PS::IPP::Config->new();
-#my $tmp_dir = "/data/${host}.0/tmp/";
-
-my $project = resolve_project($ipprc,"project_name",$dbname);
+
+if (!$dbserver) {
+    $dbserver = metadataLookupStr($ipprc->{_siteConfig}, 'PS_DBSERVER');
+}
+# This is hardcoded in for the moment.
+$project = resolve_project($ipprc,"gpc1",$dbname,$dbserver);
+my $imagedb = $project->{dbname};
+if (!$imagedb) {
+    carp("failed to find imagedb for project: $project");
+}
+
 #
 # Parse input request file using detect_query_read (as it's already written).
 #
-
 my $dqr_command = "$detect_query_read --input $request_file";
 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     run(command => $dqr_command, verbose => $verbose);
 unless ($success) {
-    my_die("Unable to perform $dqr_command error code: $error_code",-1,-1,-1,-1,-1,-1);
+    # This is a problem, because I'm not sure how we handle a failure to read something.
+    # We need to return a $PSTAMP_INVALID_REQUEST, I think, but if we can't read it, 
+    # we can't send that response back.
+    die("Unable to perform $dqr_command error code: $error_code");
 }
 my %query = ();
@@ -120,5 +132,6 @@
 my %image_list_hash;
 for (my $i = 1; $i < $Nrows; $i++) {
-#    print "$i $Nrows $query{CONTENT}{RA1_DEG}[$i] $query{CONTENT}{DEC1_DEG}[$i]\n";
+    # This could use the fact that locate_images now accepts position arrays, but
+    # I'll save that for after I get the majority of things working.
     my $image_set_tmp  = find_image_set($query{HEADER}{FPA_ID}[0],$query{HEADER}{STAGE}[0],
 					$query{HEADER}{MJD_OBS}[0],$query{HEADER}{FILTER}[0],
@@ -126,10 +139,21 @@
 					$query{CONTENT}{ROWNUM}[$i],$verbose);
     unless (%$image_set_tmp) {
-	my_die("No images were returned.",$query{HEADER}{QUERY_ID}[0],$query{HEADER}{FPA_ID}[0],
-	       $query{HEADER}{MJD_OBS}[0],$query{HEADER}{FILTER}[0],$query{HEADER}{OBSCODE}[0],
-	       $query{HEADER}{STAGE}[0]);
-    }
-    print "=== $image_set_tmp->{IMAGE}\n    $image_set_tmp->{PSF}\n    $image_set_tmp->{MASK}\n    $image_set_tmp->{WEIGHT}\n    $image_set_tmp->{SKY_COORDINATES}\n    $image_set_tmp->{ROWNUM}\n";
-    # This appends, assuming that if we get an image, we also get the identical psf/mask/weight/etc.
+	# No images were returned, so create a dummy entry that 
+	$image_list_hash{'no_image'}{IMAGE}    = 'no_image';
+	$image_list_hash{'no_image'}{PSF}      = 'no_psf';
+	$image_list_hash{'no_image'}{MASK}     = 'no_mask';
+	$image_list_hash{'no_image'}{WEIGHT}   = 'no_weight';
+	$image_list_hash{'no_image'}{CATALOG}  = 'no_catalog';
+	$image_list_hash{'no_image'}{CLASS_ID} = 'no_class';
+	$image_list_hash{'no_image'}{ERROR}    = $PSTAMP_NO_IMAGE_MATCH;
+	push @{ $image_list_hash{'no_image'}{SKY_COORDINATES} }, "$query{CONTENT}{RA1_DEG}[$i] $query{CONTENT}{DEC1_DEG}[$i]";
+	push @{ $image_list_hash{'no_image'}{ROWNUM} }, $query{CONTENT}{ROWNUM}[$i];
+	next;
+    }
+#     print "=== $image_set_tmp->{IMAGE}\n    $image_set_tmp->{PSF}\n";
+#     print "    $image_set_tmp->{MASK}\n    $image_set_tmp->{WEIGHT}\n";
+#     print "    $image_set_tmp->{SKY_COORDINATES}\n    $image_set_tmp->{ROWNUM}\n";
+
+    # This indexes the results for identical images into the same hash.
     $image_list_hash{$image_set_tmp->{IMAGE}}{IMAGE}    = $image_set_tmp->{IMAGE};
     $image_list_hash{$image_set_tmp->{IMAGE}}{PSF}      = $image_set_tmp->{PSF};
@@ -138,15 +162,19 @@
     $image_list_hash{$image_set_tmp->{IMAGE}}{CATALOG}  = $image_set_tmp->{CATALOG};
     $image_list_hash{$image_set_tmp->{IMAGE}}{CLASS_ID} = $image_set_tmp->{CLASS_ID};
+    $image_list_hash{$image_set_tmp->{IMAGE}}{ERROR}    = $image_set_tmp->{ERROR};
     push @{ $image_list_hash{$image_set_tmp->{IMAGE}}{SKY_COORDINATES} }, $image_set_tmp->{SKY_COORDINATES};
     push @{ $image_list_hash{$image_set_tmp->{IMAGE}}{ROWNUM} }, $image_set_tmp->{ROWNUM};
 }
+
 my $i = 0;
-
-
 foreach my $k (keys %image_list_hash) {
+    # If we errored out on finding an image, we need to not try to run psphot here.
+    if ($image_list_hash{$k}{ERROR} != 0) {
+	next;
+    }
     # Write coordinates of the requested targets to a file.
-    my ($coordfile,$coordname) = tempfile("/tmp/detect.coords.$i.XXXX", 
+    my ($coordfile,$coordname) = tempfile("${workdir}/detect.coords.$i.XXXX", 
 					    UNLINK => !$save_temps);
-    my ($targetfile,$targetname) = tempfile("/tmp/detect.targets.$i.XXXX", 
+    my ($targetfile,$targetname) = tempfile("${workdir}/detect.targets.$i.XXXX", 
 					    UNLINK => !$save_temps);
 
@@ -154,5 +182,5 @@
 	print $coordfile "$image_list_hash{$k}{SKY_COORDINATES}[$j]\n";
     }
-    print "$k\n";
+#    print "$k\n";
     # Convert the sky coordinates to image coordinates with ppCoord.
     my $command = "ppCoord -astrom $image_list_hash{$k}{CATALOG} -radec $coordname";
@@ -172,7 +200,7 @@
     }
 
-    print "psphot $image_list_hash{$k}{PSF}\n";
+#     print "psphot $image_list_hash{$k}{PSF}\n";
     # Run psphotForced on the target list.
-    my $tmpdir  = tempdir("detect.$i.XXXX", DIR => "/tmp/", CLEANUP => !$save_temps);
+    my $tmpdir  = tempdir("detect.$i.XXXX", DIR => "${workdir}/", CLEANUP => !$save_temps);
     $image_list_hash{$k}{OUTROOT} = "$tmpdir/detectability.$query{HEADER}{STAGE}[0].$query{HEADER}{FPA_ID}[0]";
     
@@ -186,8 +214,5 @@
 	run(command => $psphot_cmd, verbose => $verbose);
     unless ($success) {
-	my_die("Unable to perform $psphot_cmd. Error_code: $error_code",
-	       $query{HEADER}{QUERY_ID}[0],$query{HEADER}{FPA_ID}[0],
-	       $query{HEADER}{MJD_OBS}[0],$query{HEADER}{FILTER}[0],$query{HEADER}{OBSCODE}[0],
-	       $query{HEADER}{STAGE}[0],$error_code);
+	$image_list_hash{$k}{ERROR} = $PSTAMP_SYSTEM_ERROR;
     }
 }
@@ -197,16 +222,28 @@
 #
 my @rownums = ();
+my @out_errors = ();
 my @psphot_Npix = ();
 my @psphot_Qfact= ();
 my @psphot_flux = ();
+
 foreach my $k (keys %image_list_hash) {
-    my $cmf = "$image_list_hash{$k}{OUTROOT}.$image_list_hash{$k}{CLASS_ID}.cmf";
-    
-    my ($tmp_Npix,$tmp_Qfact,$tmp_flux) = read_cmf_file($cmf,$image_list_hash{$k}{EXTENSION_BASE});
-
-    push @rownums,        @{ $image_list_hash{$k}{ROWNUM} };
-    push @psphot_Npix,    @{ $tmp_Npix };
-    push @psphot_Qfact,   @{ $tmp_Qfact };
-    push @psphot_flux,    @{ $tmp_flux };
+    if ($image_list_hash{$k}{ERROR} == 0) {
+	my $cmf = "$image_list_hash{$k}{OUTROOT}.$image_list_hash{$k}{CLASS_ID}.cmf";
+	
+	my ($tmp_Npix,$tmp_Qfact,$tmp_flux) = read_cmf_file($cmf,$image_list_hash{$k}{EXTENSION_BASE});
+	
+	push @rownums,        @{ $image_list_hash{$k}{ROWNUM} };
+	push @out_errors,     (map { $image_list_hash{$k}{ERROR} }  @{ $image_list_hash{$k}{ROWNUM} });
+	push @psphot_Npix,    @{ $tmp_Npix };
+	push @psphot_Qfact,   @{ $tmp_Qfact };
+	push @psphot_flux,    @{ $tmp_flux };
+    }
+    else {
+	push @rownums,        @{ $image_list_hash{$k}{ROWNUM} };
+	push @out_errors,     (map { $image_list_hash{$k}{ERROR} }  @{ $image_list_hash{$k}{ROWNUM} });
+	push @psphot_Npix,    (map { 0 }  @{ $image_list_hash{$k}{ROWNUM} });
+	push @psphot_Qfact,   (map { 0.0 }  @{ $image_list_hash{$k}{ROWNUM} });
+	push @psphot_flux,    (map { 0.0 }  @{ $image_list_hash{$k}{ROWNUM} });
+    }	
 }
 
@@ -215,20 +252,15 @@
 		    $query{HEADER}{MJD_OBS}[0],$query{HEADER}{filter}[0],
 		    $query{HEADER}{obscode}[0],
-		    \@rownums, \@psphot_Npix, \@psphot_Qfact, \@psphot_flux);
-print "Wrote response file $output\n";
+		    \@rownums, \@out_errors, \@psphot_Npix, \@psphot_Qfact, \@psphot_flux);
+# print "Wrote response file $output\n";
 #
 # Add to datastore
 #
-# my $finish_command = "dquery_finish.pl --req_id $req_id --req_name $req_name --product $product";
-# ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-#     run(command => $finish_command, verbose => $verbose);
-# unless ($success) {
-#     warn ("Unable to perform $finish_command error code: $error_code");
-# }
-
+# Files are added to the datastore by dquery_finish.pl
 #
 # Cleanup
 #
 # Since everything is written to temporary files, there should be nothing to cleanup.
+
 #
 # Utilities
@@ -243,5 +275,6 @@
     my $index  = shift;
     my $verbose = shift;
-    
+
+    # This is the set of things that we need in order to run psphotForced
     my $option_mask |= 1;
     $option_mask |= $PSTAMP_SELECT_IMAGE;
@@ -253,6 +286,5 @@
     my $mjd_max = $mjd + 1;
 
-    # Construct a row list:
-
+    # Construct a row list. 
     my @rowList;
     $rowList[0]->{CENTER_X} = $ra;
@@ -261,7 +293,8 @@
     $rowList[0]->{STAGE} = $stage;
     $rowList[0]->{COORD_MASK} = 0;
-    print "$stage\n";
+
+    #    print "$stage\n";
     # Call the PStamp code to find the images that contain the target on the given MJD in the specified filter.
-    my @images = locate_images($ipprc,$dbname,
+    my @images = locate_images($ipprc,$imagedb,
 			        \@rowList,
 			       "bycoord",$stage,
@@ -284,7 +317,5 @@
 	    }
 	    elsif ($stage eq 'stack') {
-		# Stacks hide the exposure name very well, so I'm
-		# choosing the stack_id as the FPA_ID to match. This
-		# probably needs to be approved somehow.
+		# Stacks hide the exposure name very well, so we can only match against stage_id
 		if (${ $j }{stage_id} ne $FPA_ID) {
 		    next;
@@ -299,8 +330,43 @@
 	    }
 	    # Debug prints of all the components of this image
-  	    foreach my $k (keys %{ $j }) {
- 		print "$i $j $k ${ $j }{$k}\n";
- 	    }
-	    
+#   	    foreach my $k (keys %{ $j }) {
+# 		if ($k eq 'row_index') {
+# 		    print "$i $j $k @{${ $j }{$k} }\n";
+# 		}
+#  		print "$i $j $k ${ $j }{$k}\n";
+#  	    }
+
+	    # Check for existance of the images. Drawn mostly from pstampparse.pl
+	    my $run_state = ${ $j }{state};
+	    my $data_state = ${ $j }{data_state};
+	    $data_state = $run_state if $stage eq 'stack';
+	    my $fault = 0;
+	    if (($run_state eq 'goto_purged') or ($data_state eq 'purged') or
+		($run_state eq 'drop') or 
+		($run_state eq 'error_cleaned') or 
+		($run_state eq 'goto_scrubbed') or ($data_state eq 'scrubbed')) {
+		# image is gone and it's not coming back
+		$fault = $PSTAMP_GONE;
+	    }
+	    elsif  (($data_state ne 'full') or ($need_magic and (${ $j }{magicked} < 0))) {
+		if (($stage eq 'stack')||($stage eq 'diff')) {
+		    # updating stacks and diffs isn't implemented
+		    $fault = $PSTAMP_NOT_IMPLEMENTED;
+		}
+		if ($stage eq 'chip') {
+		    my $burntool_state = ${ $j }{burntool_state};
+		    if ($burntool_state and (abs($burntool_state) < 14)) {
+			$fault = $PSTAMP_NOT_AVAILABLE;
+		    }
+		}
+		
+		if ($fault == 0) {
+		    # This bombs us out to dqueryparse, which will then flag a job for this run to be updated.
+		    my_die_for_update($data_state,$query{HEADER}{STAGE}[0],
+				      ${ $j }{stage_id},${ $j }{class_id} || ${ $j }{skycell_id},
+				      $need_magic,$imagedb,$PSTAMP_NOT_AVAILABLE);
+		}
+	    }
+
 	    # This image matches, so we want to save the information into our output structure
 	    $image_info{ROWNUM} = $index;
@@ -309,4 +375,5 @@
 	    $image_info{MASK}   = ${ $j }{mask};
 	    $image_info{WEIGHT} = ${ $j }{weight};
+	    $image_info{ERROR}  = $fault;
 	    $image_info{SKY_COORDINATES} = "$ra $dec";
 	    # To do sky->image coordinate transformations, we need to use the cmf/smf file. If 
@@ -419,4 +486,5 @@
     my $obscode = shift;
     my $rownum_ref = shift;
+    my $out_err_ref = shift;
     my $psphot_Npix_ref = shift;
     my $psphot_Qfact_ref = shift;
@@ -427,5 +495,7 @@
     my $columns = [
 	# matching rownum from detectability original request
-        { name => 'ROWNUM',   type => '20A', writetype => TSTRING }, 
+        { name => 'ROWNUM',   type => 'V', writetype => TULONG }, 
+	# any errors that occurred during processing
+        { name => 'ERROR_CODE',   type => 'V', writetype => TULONG }, 
         # number of pixels used in hypothetical PSF for the query detection
         { name => 'DETECT_N', type => 'V',   writetype => TULONG },
@@ -469,4 +539,5 @@
     for (my $i = 0; $i < $numRows; $i++) {
 	push @{$colData{'ROWNUM'}},      ${ $rownum_ref }[$i];
+	push @{$colData{'ERROR_CODE'}},  ${ $out_err_ref }[$i];
 	push @{$colData{'DETECT_N'}},    ${ $psphot_Npix_ref }[$i];
 	push @{$colData{'DETECT_F'}},    ${ $psphot_Qfact_ref }[$i];
@@ -543,2 +614,16 @@
 
 }
+
+sub my_die_for_update {
+    my $state = shift;
+    my $stage = shift;
+    my $stage_id = shift;
+    my $component = shift;
+    my $need_magic = shift;
+    my $imagedb = shift;
+    my $exit_code = shift;
+
+    print "$state $stage $stage_id $component $need_magic $imagedb\n";
+    print STDERR "$state $stage $stage_id $component $need_magic $imagedb\n";
+    exit($exit_code);
+}
