Index: /trunk/pstamp/scripts/pstampparse.pl
===================================================================
--- /trunk/pstamp/scripts/pstampparse.pl	(revision 25066)
+++ /trunk/pstamp/scripts/pstampparse.pl	(revision 25067)
@@ -146,39 +146,4 @@
     # parameters that select the images of interest
     my $project  = $row->{PROJECT};
-    my $req_type = $row->{REQ_TYPE};
-    $stage = $row->{IMG_TYPE};
-    my $id       = $row->{ID};
-    my $component = $row->{COMPONENT};
-
-    my $filter   = $row->{REQFILT};
-    my $mjd_min = $row->{MJD_MIN};
-    my $mjd_max = $row->{MJD_MAX};
-
-    my $option_mask= $row->{OPTION_MASK};
-
-    my $skycenter = $row->{skycenter} = ! ($row->{COORD_MASK} & $PSTAMP_CENTER_IN_PIXELS);
-
-    my $search_component = (!defined($component) or ($component eq "null")) ? "" : $component;
-    
-    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
@@ -191,4 +156,40 @@
         next;
     }
+    my $req_type = $row->{REQ_TYPE};
+    $stage = $row->{IMG_TYPE};
+    my $id       = $row->{ID};
+    my $component = $row->{COMPONENT};
+
+    my $filter   = $row->{REQFILT};
+    my $mjd_min = $row->{MJD_MIN};
+    my $mjd_max = $row->{MJD_MAX};
+
+    my $option_mask= $row->{OPTION_MASK};
+
+    my $skycenter = $row->{skycenter} = ! ($row->{COORD_MASK} & $PSTAMP_CENTER_IN_PIXELS);
+
+    my $search_component = (!defined($component) or ($component eq "null")) ? "" : $component;
+    
+    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");
+
 
     my $image_db   = $proj_hash->{dbname};
@@ -197,14 +198,18 @@
 
     # collect rows with the same images of interest in a list so that they
-    # can be processed optimially
+    # can be looked up together
     if (@rowList) {
         my $firstRow = $rowList[0];
-#        if ($skycenter and same_images_of_interest($row, $firstRow)) {
-        if (same_images_of_interest($row, $firstRow)) {
+        # note order of these parameters matters
+        if (same_images_of_interest($firstRow, $row)) {
+
+            # add this row to the list and move on
             push @rowList, $row;
-            # On to the next row
+
             next;
+
         } else {
-            # queue the jobs for this set of rows
+            # this row has different selectors
+            # queue the jobs for the ones we've collected
             $num_jobs += queueJobs($mode, \@rowList, $imageList);
             @rowList = ();
@@ -212,4 +217,5 @@
     }
 
+    # look up images for the current row
     if ($req_type eq "bycoord") {
         if (!$skycenter) {
@@ -224,11 +230,9 @@
         next;
     } else {
-            # 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,
+        # 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;
@@ -241,4 +245,5 @@
     if (@rowList) {
         $num_jobs += queueJobs($mode, \@rowList, $imageList);
+    }
 }
 
@@ -247,4 +252,7 @@
     insertFakeJobForRow(undef, 1, 41);
 }
+
+# PAU
+
 exit 0;
 
@@ -500,6 +508,15 @@
     my $fault = shift;
 
-    my $command = "$pstamptool -addjob  -req_id $req_id -job_type $row->{JOB_TYPE}"
-                        . " -rownum $row->{ROWNUM} -state stop -fault $fault";
+    my ($job_type, $rownum);
+    if ($row) {
+        $job_type = $row->{JOB_TYPE};
+        $rownum = $row->{ROWNUM};
+    } else {
+        $job_type = "none";
+        $rownum = 0;
+    }
+
+    my $command = "$pstamptool -addjob  -req_id $req_id -job_type $job_type"
+                        . " -rownum $rownum -state stop -fault $fault";
     $command .= " -dbname $dbname" if $dbname;
     $command .= " -dbserver $dbserver" if $dbserver;
@@ -581,5 +598,7 @@
         return 0 if !defined $r2->{COMPONENT} or ($r1->{COMPONENT} ne $r2->{COMPONENT});
     } elsif (defined($r2->{COMPONENT})) {
-        return 0;
+        # if first row has no component all of the images will be retrieved, so
+        # the fact that this row has a component is ok. Fall through to return 1
+        # return 0;
     }
 
