Index: trunk/pstamp/scripts/detectability_respond.pl
===================================================================
--- trunk/pstamp/scripts/detectability_respond.pl	(revision 33140)
+++ trunk/pstamp/scripts/detectability_respond.pl	(revision 33141)
@@ -41,5 +41,5 @@
 my $EXTVER = 1.0;
 my $EXTNAME = 'MOPS_DETECTABILITY_RESPONSE';
-my ($req_id,$req_name,$product,$need_magic,$missing_tools,$project);
+my ($req_id,$job_id,$req_name,$product,$need_magic,$missing_tools,$project);
 my ($request_file,$output,$workdir,$dbname,$dbserver,$verbose,$save_temps,$ignore_wisdom);
 GetOptions(
@@ -47,4 +47,5 @@
     'output=s'        =>      \$output,
     'workdir=s'       =>      \$workdir,
+    'job_id=s'        =>      \$job_id,
     'dbname=s'        =>      \$dbname,
     'dbserver=s'      =>      \$dbserver,
@@ -298,5 +299,5 @@
 			$query{$fpa_id}{FAULT}[$valid_index] = $PSTAMP_GONE;
 		    }
-		    elsif ($need_magic and ($query{$fpa_id}{MAGICKED}[$valid_index] = 0)) {
+		    elsif ($need_magic and ($query{$fpa_id}{MAGICKED}[$valid_index] eq 0)) {
 			$query{$fpa_id}{FAULT}[$valid_index] = $PSTAMP_NOT_DESTREAKED;
 		    }
@@ -324,45 +325,47 @@
 my %processing_request;
 
-open(WISDOM,">$wisdom_file") or my_die("failed to open wisdom file $wisdom_file");
-foreach my $fpa_id (keys %query) {
-    for (my $i = 0; $i <= $#{ $query{$fpa_id}{ROWNUM} }; $i++) {
-	print WISDOM "$fpa_id\t";
-	foreach my $key (keys %{ $query{$fpa_id} }) {
-	    print WISDOM "$key $query{$fpa_id}{$key}[$i]\t";
-	}
-	print WISDOM "\n";
-        my $data_state = $query{$fpa_id}{DATA_STATE}[$i];
-	if ($query{$fpa_id}{BAD_COMPONENT}[$i] == 0) {
-            if ($data_state ne 'full') {
-                
-                @{ $update_request{$query{$fpa_id}{IMAGE}[$i]}{$query{$fpa_id}{FAULT}[$i]} } = 
-                    ($query{$fpa_id}{STATE}[$i],$query{$fpa_id}{STAGE}[$i],$query{$fpa_id}{STAGE_ID}[$i],
-                     $query{$fpa_id}{COMPONENT_ID}[$i],$query{$fpa_id}{NEED_MAGIC}[$i],$query{$fpa_id}{IMAGE_DB}[$i]);
+if (!$job_id) {
+    open(WISDOM,">$wisdom_file") or my_die("failed to open wisdom file $wisdom_file");
+    foreach my $fpa_id (keys %query) {
+        for (my $i = 0; $i <= $#{ $query{$fpa_id}{ROWNUM} }; $i++) {
+            print WISDOM "$fpa_id\t";
+            foreach my $key (keys %{ $query{$fpa_id} }) {
+                print WISDOM "$key $query{$fpa_id}{$key}[$i]\t";
             }
-	    push @{ $processing_request{$fpa_id}{$query{$fpa_id}{IMAGE}[$i]} }, $i;
-	}
-    }
-}
-close(WISDOM);
-
-# If there is anything that needs to be updated, create the update request list, and then exit the program.
-my $exit_code = 0;
-my $update_request_file = "${workdir}/update_request.dat";
-open(UPDATE_REQUEST,">$update_request_file") or my_die("failed to open update request_file $update_request_file");
-foreach my $images (keys %update_request) {
-    foreach my $fault (keys %{ $update_request{$images} }) {
-	if ($fault == 25) {
-	    $exit_code = 25;
-	}
-	elsif ($fault != 0) {
-	    $exit_code = 21;
-	}
-	my $update_request = join ' ', @{ $update_request{$images}{$fault} };
-	print UPDATE_REQUEST "$update_request\n";
-    }
-}
-close(UPDATE_REQUEST);
-if ($exit_code != 0) {
-    exit($exit_code);
+            print WISDOM "\n";
+            my $data_state = $query{$fpa_id}{DATA_STATE}[$i];
+            if ($query{$fpa_id}{BAD_COMPONENT}[$i] == 0) {
+                if ($data_state ne 'full') {
+                    
+                    @{ $update_request{$query{$fpa_id}{IMAGE}[$i]}{$query{$fpa_id}{FAULT}[$i]} } = 
+                        ($query{$fpa_id}{STATE}[$i],$query{$fpa_id}{STAGE}[$i],$query{$fpa_id}{STAGE_ID}[$i],
+                         $query{$fpa_id}{COMPONENT_ID}[$i],$query{$fpa_id}{NEED_MAGIC}[$i],$query{$fpa_id}{IMAGE_DB}[$i]);
+                }
+                push @{ $processing_request{$fpa_id}{$query{$fpa_id}{IMAGE}[$i]} }, $i;
+            }
+        }
+    }
+    close(WISDOM);
+
+    # If there is anything that needs to be updated, create the update request list, and then exit the program.
+    my $exit_code = 0;
+    my $update_request_file = "${workdir}/update_request.dat";
+    open(UPDATE_REQUEST,">$update_request_file") or my_die("failed to open update request_file $update_request_file");
+    foreach my $images (keys %update_request) {
+        foreach my $fault (keys %{ $update_request{$images} }) {
+            if ($fault == 25) {
+                $exit_code = 25;
+            }
+            elsif ($fault != 0) {
+                $exit_code = 21;
+            }
+            my $update_request = join ' ', @{ $update_request{$images}{$fault} };
+            print UPDATE_REQUEST "$update_request\n";
+        }
+    }
+    close(UPDATE_REQUEST);
+    if ($exit_code != 0) {
+        exit($exit_code);
+    }
 }
 
@@ -714,4 +717,7 @@
     $status = 0;
 #    print "$output\n";
+    if (-e $output) {
+        unlink $output or die "failed to unlink existing response file $output\n";
+    }
     my $outFits = Astro::FITS::CFITSIO::create_file( $output, $status );
     check_fitsio( $status );
Index: trunk/pstamp/scripts/dquery_finish.pl
===================================================================
--- trunk/pstamp/scripts/dquery_finish.pl	(revision 33140)
+++ trunk/pstamp/scripts/dquery_finish.pl	(revision 33141)
@@ -66,5 +66,5 @@
 if ($product eq "NULL") {
     # parsing failed just with fault = 0 (this leaves previously set fault in place
-    stop_request($req_id, 0, $verbose);
+    update_request($req_id, 0, $verbose);
     exit 0;
 }
@@ -73,5 +73,5 @@
 if (! -e $prod_dir ) {
     # something must have gone wrong at the parse stage
-    stop_request($req_id, $PS_EXIT_SYS_ERROR, $verbose);
+    update_request($req_id, $PS_EXIT_SYS_ERROR, $verbose);
     die "product directory does not exist $prod_dir";
 }
@@ -82,9 +82,9 @@
     print STDERR "output fileset directory $outdir does not exist\n" if $verbose;
     if (! mkdir $outdir ) {
-        stop_request($req_id, $PS_EXIT_SYS_ERROR, $verbose);
+        update_request($req_id, $PS_EXIT_SYS_ERROR, $verbose);
         die "cannot create output directory $outdir";
     }
 } elsif (! -d $outdir) {
-    stop_request($req_id, $PS_EXIT_SYS_ERROR, $verbose);
+    update_request($req_id, $PS_EXIT_SYS_ERROR, $verbose);
     die "output fileset directory $outdir exists but is not a directory";
 }
@@ -121,6 +121,7 @@
 
 foreach my $job (@jobs) {
+    next if $job->{parent_id};
     my $job_id = $job->{job_id};
-    my $response_file = "response${job_id}.fits";
+    my $response_file = "response.${job_id}.fits";
     my $response_path = "$outdir/$response_file";
 
@@ -157,15 +158,19 @@
 }
 
-stop_request($req_id, $request_fault, $verbose);
+update_request($req_id, $request_fault, $verbose);
 
 exit 0;
 
-sub stop_request {
+sub update_request {
     my $req_id = shift;
     my $fault = shift;
     my $verbose = shift;
     
-    my $command = "$pstamptool -updatereq -req_id $req_id -set_state stop";
-    $command   .= " -set_fault $fault" if $fault;
+    my $command = "$pstamptool -updatereq -req_id $req_id";
+    if ($fault) {
+        $command .= " -set_fault $fault";
+    } else {
+        $command .= " -set_state stop";
+    }
     $command   .= " -dbname $dbname" if $dbname;
     $command   .= " -dbserver $dbserver" if $dbserver;
Index: trunk/pstamp/scripts/dqueryparse.pl
===================================================================
--- trunk/pstamp/scripts/dqueryparse.pl	(revision 33140)
+++ trunk/pstamp/scripts/dqueryparse.pl	(revision 33141)
@@ -118,7 +118,11 @@
 {
     my $command = "$detectresponse --input $req_file --output $response_file --workdir $outdir";
+    if ($job_id) {
+        $command .= " --job_id $job_id";
+    } else {
+        $command .= " --ignore-wisdom";
+    }
     $command .= " --save-temps" if $save_temps;
     $command .= " --verbose" if $verbose;
-    $command .= " --ignore-wisdom" if !$job_id;
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -133,4 +137,5 @@
 	    my_die ("Update request indicated, but unable to find actual request!", $PS_EXIT_PROG_ERROR);
 	}
+        warn("Some inputs are not available must be updated.";
     }	
     
@@ -139,4 +144,5 @@
 my $result;
 unless ($job_id) {
+    # We are running as a parse job
     # If we returned correctly with a valid response file, get a job ID
     # for the completed work, and move the response to a standardized name.
@@ -152,5 +158,5 @@
             chomp $job_id;
             if ($job_id && -e $response_file) {
-                rename $response_file, "$outdir/response${job_id}.fits";
+                rename $response_file, "$outdir/response.${job_id}.fits";
             }
             $result = 0;
@@ -222,21 +228,28 @@
         my_die ("Parse fault!!", $fault);
     }
+    # This does not set the request state to stop.  That will happen with the request_finish.pl script, 
+    # which will notice that we've inserted the stopped job and decide we're finished. Easy enough.
+    {
+        my $command = "$pstamptool -updatereq -req_id $req_id -set_name $req_name -set_outProduct $product";
+        $command .= " -set_fault $result" if $result;
+
+        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+            run(command => $command, verbose => $verbose);
+        unless ($success) {
+            my_die("$command failed",$PS_EXIT_UNKNOWN_ERROR);
+        }
+    }
 } else {
+    # We are running as a job presumably because an input needed
+    # to be regenerated
+    if ($fault) {
         # in some cases we will need to finish off the request
         my_die ("Run fault!!", $fault);
-}
-
-# This does not set the request state to stop.  That will happen with the request_finish.pl script, 
-# which will notice that we've inserted the stopped job and decide we're finished. Easy enough.
-{
-    my $command = "$pstamptool -updatereq -req_id $req_id -set_name $req_name -set_outProduct $product";
-    $command .= " -set_fault $result" if $result;
-
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => $verbose);
-    unless ($success) {
-        my_die("$command failed",$PS_EXIT_UNKNOWN_ERROR);
-    }
-}
+    }
+    if (-e $response_file) {
+        rename $response_file, "$outdir/response.${job_id}.fits";
+    }
+}
+
  
 exit 0;
Index: trunk/pstamp/scripts/pstamp_job_run.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_job_run.pl	(revision 33140)
+++ trunk/pstamp/scripts/pstamp_job_run.pl	(revision 33141)
@@ -306,5 +306,6 @@
     # Load the argument list that dqueryparse should have created the first time it ran, so we know how to
     # run it again the same way.
-    my $argslist = "$outputBase/parse.args";
+    my $outdir = dirname($outputBase);
+    my $argslist = "$outdir/parse.args";
     open ARGSLIST, "<$argslist" or my_die("failed to open argslist file $argslist", $job_id, $PS_EXIT_UNKNOWN_ERROR);
     my $argString = <ARGSLIST>;
@@ -319,4 +320,5 @@
     $command .= " --dbserver $dbserver" if $dbserver;
     $command .= " --verbose" if $verbose;
+    $command .= " --save-temps" if $save_temps;
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
 	run(command => $command, verbose => $verbose);
@@ -616,4 +618,8 @@
 
     $exit_code = $PS_EXIT_PROG_ERROR unless $exit_code;
+    if ($exit_code > 100) {
+        carp ("invalid exit code: $exit_code changing to $PS_EXIT_UNKNOWN_ERROR");
+        $exit_code = $PS_EXIT_UNKNOWN_ERROR;
+    }
     $job_state = 'run' unless $job_state;
 
