Index: trunk/pstamp/scripts/pstamp_parser_run.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 25053)
+++ trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 25058)
@@ -200,4 +200,6 @@
     unlink $error_file_name if (-e $error_file_name);
 
+    # Run the parser
+
     my $command = "$parse_cmd";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
Index: trunk/pstamp/scripts/pstampparse.pl
===================================================================
--- trunk/pstamp/scripts/pstampparse.pl	(revision 25053)
+++ trunk/pstamp/scripts/pstampparse.pl	(revision 25058)
@@ -91,9 +91,12 @@
 # make sure the file contains what we are expecting
 
-die "$request_file_name is not a PS1_PS_REQEST" if $extname ne "PS1_PS_REQUEST";
-die "REQ_NAME not found in $request_file_name"  if (!$req_name);
-die "wrong EXTVER $extver found in $request_file_name" if ($extver ne "1");
+# we shouldn't get here if this is the case so just die. No need to notify the client
+my_die("$request_file_name is not a PS1_PS_REQEST", $PS_EXIT_PROG_ERROR) if $extname ne "PS1_PS_REQUEST";
+my_die("REQ_NAME not found in $request_file_name", $PS_EXIT_PROG_ERROR)  if (!$req_name);
+my_die("wrong EXTVER $extver found in $request_file_name", $PS_EXIT_PROG_ERROR) if ($extver ne "1");
 
 if ($req_id and !$no_update) {
+    # update the database with the request name. This will be used as the
+    # the output data store's product name
     my $command = "$pstamptool -updatereq -req_id $req_id  -name $req_name";
     $command .= " -outProduct $product";
@@ -103,5 +106,5 @@
         run(command => $command, verbose => $verbose);
     unless ($success) {
-        die "$command failed";
+        my_die("$command failed", $PS_EXIT_UNKNOWN_ERROR);
     }
 }
@@ -120,5 +123,5 @@
     }
     my $table =  $mdcParser->parse(join "", @$stdout_buf) or
-        die("Unable to parse metdata config doc");
+        my_die("Unable to parse metdata config doc", $PS_EXIT_UNKNOWN_ERROR);
 
     $rows = parse_md_list($table);
@@ -154,16 +157,38 @@
     my $option_mask= $row->{OPTION_MASK};
 
-    die "valid region of interest is required to make postage stamps"
-        if (($job_type eq "stamp") and ! validROI($row));
-
     my $skycenter = $row->{skycenter} = ! ($row->{COORD_MASK} & $PSTAMP_CENTER_IN_PIXELS);
-    $component = "" if (defined($component) and ($component eq "null" or $component eq "all"));
+
+    my $search_component = (!defined($component) or ($component eq "null")) ? "" : $component;
     
-    die "job_type is list_uri but mode is $mode" if ($job_type eq "list_uri") and ($mode ne "list_uri");
+    if (!$skycenter and !$search_component) {
+        print STDERR "COMPONENT must be specified for pixel coordinate ROI\n" if $verbose;
+        insertFakeJobForRow($row, 1, 45);
+        $num_jobs++;
+        next;
+    }
+
+    $search_component = "" if $search_component eq "all";
+
+    if (($job_type eq "stamp") and ! validROI($row)) {
+        print STDERR "invalid ROI for row $rownum\n" if $verbose;
+        insertFakeJobForRow($row, 1, 46);
+        $num_jobs++;
+        next;
+    }
+
+    
+    # $mode list_uri is a debugging mode (it may used by the http interface)
+    # if this happens just croak
+    my_die("job_type is list_uri but mode is $mode", $PS_EXIT_PROG_ERROR) if ($job_type eq "list_uri") and ($mode ne "list_uri");
 
     # note: resolve_project avoids running pstamptool every time by remembering the
     # last project resolved
     my $proj_hash = resolve_project($ipprc, $project, $dbname, $dbserver);
-    die "project $project not found\n" unless $proj_hash;
+    if (!$proj_hash) {
+        print STDERR "project $project not found\n"  if $verbose;
+        insertFakeJobForRow($row, 1, 46);
+        $num_jobs++;
+        next;
+    }
 
     my $image_db   = $proj_hash->{dbname};
@@ -175,6 +200,6 @@
     if (@rowList) {
         my $firstRow = $rowList[0];
-        # XXX: the collecting might work with !$skycenter but I need to think about it
-        if ($skycenter and same_images_of_interest($row, $firstRow)) {
+#        if ($skycenter and same_images_of_interest($row, $firstRow)) {
+        if (same_images_of_interest($row, $firstRow)) {
             push @rowList, $row;
             # On to the next row
@@ -188,37 +213,39 @@
 
     if ($req_type eq "bycoord") {
-        die "center must be specified in sky coordintes for bycoord" if ( ! $skycenter);
-        die "lookup bycoord is not yet implemented";
+        if (!$skycenter) {
+            print STDERR "center must be specified in sky coordintes for bycoord" if $verbose;
+            insertFakeJobForRow($row, 1, 46);
+            $num_jobs++;
+            next;
+        }
+        print STDERR "lookup bycoord is not yet implemented" if $verbose;
+        insertFakeJobForRow($row, 1, 47);
+        $num_jobs++;
+        next;
     } else {
-
-        # Call PS::IPP::PStamp::Job's locate_images routine to get the parameters for this
-        # request specification. An array reference is returned
-        my ($x, $y);
-        $imageList = locate_images($ipprc, $image_db, $req_type, $stage, $id, $component, $skycenter,
-                $x, $y, $mjd_min, $mjd_max, $filter, $verbose);
-    }
-
-    if (!$imageList or !@$imageList) {
-        print STDERR "no matching images found for row $rownum\n";
-    }
-    $row->{need_magic} = $need_magic;
-    push @rowList, $row;
-}
-
-if (@rowList) {
-    $num_jobs += queueJobs($mode, \@rowList, $imageList);
-}
-
-if ($mode eq "queue_jobs") {
-    if ($num_jobs) {
-        exit 0;
-    } else {
-        # no jobs queued return error to get the request stopped
-        # TODO: need to define meaningful error codes
-        exit $PS_EXIT_UNKNOWN_ERROR;
-    }
-} else {
-    exit 0;
-}
+            # Call PS::IPP::PStamp::Job's locate_images subroutine to get the parameters for this
+            # request specification. An array reference is returned.
+            my ($x, $y);
+            $imageList = locate_images($ipprc, $image_db, $req_type, $stage, $id, $search_component,
+                $skycenter, $x, $y, $mjd_min, $mjd_max, $filter, $verbose);
+        }
+
+        if (!$imageList or !@$imageList) {
+            print STDERR "no matching images found for row $rownum\n" if $verbose;
+            # note in this case queueJobs inserts the fake job for these rows
+        }
+        $row->{need_magic} = $need_magic;
+        push @rowList, $row;
+    }
+
+    if (@rowList) {
+        $num_jobs += queueJobs($mode, \@rowList, $imageList);
+}
+
+if (($mode eq "queue_jobs") and ($num_jobs eq 0)) {
+    print STDERR "no jobs created for $req_name\n" if $verbose;
+    insertFakeJobForRow(undef, 1, 41);
+}
+exit 0;
 
 
@@ -230,4 +257,35 @@
     my $have_skycells = shift;
     my $need_magic = shift;
+
+    my $rownum = $row->{ROWNUM};
+
+    my $components = $row->{components};
+    my $numComponents = scalar keys %$components;
+    if ( $numComponents == 0 ) {
+        print STDERR "no jobs for row $rownum\n" if $verbose;
+        insertFakeJobForRow($row, 1, 44);
+        return 1;
+    }
+
+    my $roi_string;
+
+    # note values were insured to be numbers in validROI()
+    my $x = $row->{CENTER_X};
+    my $y = $row->{CENTER_Y};
+    my $w = $row->{WIDTH};
+    my $h = $row->{HEIGHT};
+    my $coord_mask = $row->{COORD_MASK};
+    if ($x && ($x ne "null") && $y && ($y ne "null") && $w && ($w ne "null") && $h && ($h ne "null")) {
+        if ($coord_mask & $PSTAMP_CENTER_IN_PIXELS) {
+            $roi_string = "-pixcenter $x $y";
+        } else {
+            $roi_string = "-skycenter $x $y";
+        }
+        if ($coord_mask & $PSTAMP_RANGE_IN_PIXELS) {
+            $roi_string .= " -pixrange $w $h";
+        } else {
+            $roi_string .= " -arcrange $w $h";
+        }
+    }
 
     # loop over images
@@ -242,28 +300,5 @@
 
         # skip this component if it is not in the list for this row
-        next if !$row->{components}->{$component};
-
-        my $rownum = $row->{ROWNUM};
-
-        my $roi_string;
-
-        # note values were insured to be numbers in validROI()
-        my $x = $row->{CENTER_X};
-        my $y = $row->{CENTER_Y};
-        my $w = $row->{WIDTH};
-        my $h = $row->{HEIGHT};
-        my $coord_mask = $row->{COORD_MASK};
-        if ($x && ($x ne "null") && $y && ($y ne "null") && $w && ($w ne "null") && $h && ($h ne "null")) {
-            if ($coord_mask & $PSTAMP_CENTER_IN_PIXELS) {
-                $roi_string = "-pixcenter $x $y";
-            } else {
-                $roi_string = "-skycenter $x $y";
-            }
-            if ($coord_mask & $PSTAMP_RANGE_IN_PIXELS) {
-                $roi_string .= " -pixrange $w $h";
-            } else {
-                $roi_string .= " -arcrange $w $h";
-            }
-        }
+        next if ! $components->{$component};
 
         $job_num++;
@@ -271,6 +306,8 @@
         my $imagefile = $image->{image};
         if (($stage ne "stack") and ($need_magic and !$image->{magicked})) {
-            # XXX: should we add a faulted job so the client can know what happened?
-            # print STDERR "skippping non-magicked image $imagefile\n" if $verbose;
+            # XXX: should we add a faulted job so the client can know what happened if no images come back?
+            print STDERR "skippping non-magicked image $imagefile\n" if $verbose;
+
+            # for now assume yes.
 
             insertFakeJobForRow($row, $job_num, 43);
@@ -303,16 +340,21 @@
 
         # copy the argument list to a file
-        open ARGSLIST, ">$argslist" or die "failed to open $argslist";
+        open ARGSLIST, ">$argslist" or my_die("failed to open $argslist", $PS_EXIT_UNKNOWN_ERROR);
         print ARGSLIST "$args\n";
-        close ARGSLIST or die "failed to close $argslist";
-
-        # XXX: TODO: here is where we need to check whether or not the source inputs still exist
-        # and if not, queue an update job and set the job state appropriately.
+        close ARGSLIST or my_die("failed to close $argslist", $PS_EXIT_UNKNOWN_ERROR);
 
         my $newState = "run";
+        my $fault = 0;
+        if ($image->{data_state} ne "full") {
+            # XXX here is where we need to queue an update job
+            # for now just say that the image is not available
+            $newState = 'stop';
+            $fault = 49;
+        }
 
         $num_jobs++;
         my $command = "$pstamptool -addjob  -req_id $req_id -job_type $row->{JOB_TYPE}"
                         . " -outputBase $output_base -rownum $rownum -state $newState";
+        $command .= " -fault $fault" if $fault;
         $command .= " -exp_id $exp_id" if $exp_id;
         $command .= " -dbname $dbname" if $dbname;
@@ -332,5 +374,5 @@
                 # should we keep going for other uris? If so how do we report that some
                 # of the work that the request wanted isn't going to get done
-                die "failed to queue job for request $req_id";
+                my_die("failed to queue job for request $req_id", $PS_EXIT_UNKNOWN_ERROR);
             }
         } else {
@@ -341,4 +383,5 @@
 }
 
+# queue jobs for a collection of request specifications that have the same Images of Interest
 sub queueJobs
 {
@@ -359,5 +402,9 @@
         }
     } elsif ($job_type eq "get_image") {
-        die "get_image jobs not implemented yet";
+        print STDERR "get_image jobs not implemented yet" if $verbose;
+        foreach my $row (@$rowList) {
+            insertFakeJobForRow($row, 1, 47);
+            $num_jobs++;
+        }
     } else {
         if (!$imageList or (scalar @$imageList eq 0)) {
@@ -370,12 +417,4 @@
             return $num_jobs;
         }
-        my $thisRun;
-
-        my ($pointsList, $pointsListName) = tempfile ("/tmp/pointsList.XXXX", UNLINK => !$save_temps);
-        foreach my $row (@$rowList) {
-            print $pointsList "$row->{ROWNUM} $row->{CENTER_X} $row->{CENTER_Y}\n";
-            $row->{components} = {};
-        }
-        close $pointsList;
 
         my $have_skycells;
@@ -386,8 +425,30 @@
         }
         
+        my $thisRun;
+
+        my ($pointsList, $pointsListName) = tempfile ("/tmp/pointsList.XXXX", UNLINK => !$save_temps);
+        my $npoints = 0;
+        foreach my $row (@$rowList) {
+            $row->{components} = {};
+            if ($row->{skycenter}) {
+                print $pointsList "$row->{ROWNUM} $row->{CENTER_X} $row->{CENTER_Y}\n";
+                $npoints++;
+            } else {
+                # this row's center is in pixel coordinates add all images to the component list for this row
+                foreach my $i (@$imageList) {
+                    my $component = $have_skycells ? $i->{skycell_id} : $i->{class_id};
+                    my_die( "image found with no value for component", $PS_EXIT_UNKNOWN_ERROR) if !$component;
+                    $row->{components}->{$component} = 1;
+                }
+            }
+        }
+        close $pointsList;
+
         my $tess_dir_abs;
         my $last_tess_id = "";
         while ($thisRun = getOneRun($stage, $imageList)) {
-            {
+            if ($npoints) {
+                # we collected a set of sky coordintates above filter the images so that only
+                # those tat contain the centers are processed
                 my $command = "$dvoImagesAtCoords $pointsListName";
                 if ($have_skycells) {
@@ -401,5 +462,5 @@
                 } else {
                     my $astrom = $thisRun->[0]->{astrom};
-                    die "no astrometry file found" if !$astrom;
+                    my_die( "no astrometry file found", $PS_EXIT_UNKNOWN_ERROR) if !$astrom;
                     my $astrom_resolved = $ipprc->file_resolve($astrom);
                     $command .= " -astrom $astrom_resolved";
@@ -410,5 +471,5 @@
                     print STDERR @$stderr_buf;
                     my $rc = $error_code >> 8;
-                    die "dvoImagesAtCoords failed: $rc";
+                    my_die( "dvoImagesAtCoords failed: $rc", $PS_EXIT_UNKNOWN_ERROR);
                 }
                 # now we have a list of row numbers and components
@@ -457,5 +518,5 @@
             # should we keep going for other uris? If so how do we report that some
             # of the work that the request wanted isn't going to get done
-            die "failed to queue job for request $req_id";
+            my_die("failed to queue job for request $req_id", $PS_EXIT_UNKNOWN_ERROR);
         }
     } else {
@@ -480,5 +541,5 @@
         return $image->{diff_id};
     } else {
-        die "unenexpected stage: $stage found";
+        my_die("unenexpected stage: $stage found", $PS_EXIT_PROG_ERROR);
     }
 }
@@ -554,2 +615,14 @@
     return undef;
 }
+
+sub my_die
+{
+    my $msg = shift;
+    my $fault = shift;
+
+    carp $msg;
+
+    # we don't fault the request here pstamp_parser_run.pl handles that if necessary
+
+    return $fault;
+}
