Index: branches/pap/pstamp/scripts/pstamp_finish.pl
===================================================================
--- branches/pap/pstamp/scripts/pstamp_finish.pl	(revision 23948)
+++ branches/pap/pstamp/scripts/pstamp_finish.pl	(revision 25027)
@@ -24,5 +24,5 @@
 use PS::IPP::PStamp::Job qw( :standard );
 
-my ( $req_id, $req_name, $req_file, $out_dir, $product, $dbname, $verbose, $save_temps, $redirect_output);
+my ( $req_id, $req_name, $req_file, $out_dir, $product, $dbname, $dbserver, $verbose, $save_temps, $redirect_output);
 
 # the char to the right of the bar may be used as a single - alias for the longer name
@@ -35,4 +35,5 @@
            'out_dir=s'      => \$out_dir,
 	   'dbname=s'       => \$dbname,
+	   'dbserver=s'     => \$dbserver,
 	   'verbose'        => \$verbose,
 	   'save-temps'     => \$save_temps,
@@ -50,4 +51,8 @@
     my $logDest = "$out_dir/psfinish.$req_id.log";
     $ipprc->redirect_output($logDest);
+}
+
+if (!$dbserver) {
+    $dbserver =  metadataLookupStr($ipprc->{_siteConfig}, 'PS_DBSERVER');
 }
 
@@ -89,5 +94,5 @@
         if (!mkdir $out_dir) {
             print STDERR "cannot create output directory $out_dir";
-            stop_request($req_id, $PS_EXIT_UNKNOWN_ERROR, $dbname);
+            stop_request($req_id, $PS_EXIT_UNKNOWN_ERROR);
         }
 
@@ -97,10 +102,10 @@
         # request
         print STDERR "output directory $out_dir exists but is not a directory";
-        stop_request($req_id, $PS_EXIT_UNKNOWN_ERROR, $dbname);
+        stop_request($req_id, $PS_EXIT_UNKNOWN_ERROR);
     }
 
     if (! -e $req_file ) {
         print STDERR "request file $req_file is missing\n";
-        stop_request($req_id, $PS_EXIT_CONFIG_ERROR, $dbname);
+        stop_request($req_id, $PS_EXIT_CONFIG_ERROR);
     }
 
@@ -112,5 +117,5 @@
         # point 
         print STDERR "failed to read request_file $req_file" ;
-        stop_request($req_id, $PS_EXIT_CONFIG_ERROR, $dbname);
+        stop_request($req_id, $PS_EXIT_CONFIG_ERROR);
     }
 
@@ -138,4 +143,5 @@
         my $command = "$pstamptool -listjob -req_id $req_id";
         $command   .= " -dbname $dbname" if $dbname;
+        $command   .= " -dbserver $dbserver" if $dbserver;
         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
             run(command => $command, verbose => $verbose);
@@ -167,4 +173,6 @@
     }
 
+    my $exp_info;
+    my $last_exp_id = -1;
     foreach my $job (@jobs) {
         my $job_id = $job->{job_id};
@@ -175,10 +183,18 @@
 
         my ($row, $req_info, $project) = get_request_info($rows, $rownum);
-        my $proj_hash = resolve_project($ipprc, $project, $dbname);
+
+        my $proj_hash = resolve_project($ipprc, $project, $dbname, $dbserver);
         my $image_db = $proj_hash->{dbname};
-
-        # get the metadata for the exposure (if any i.e. stack) 
-        # returns an appropriate string if !$exp_id
-        my $exp_info = get_exposure_info($image_db, $exp_id);
+        if (!$image_db) {
+            carp("failed to find imagedb for project: $project");
+            stop_request($req_id, $PS_EXIT_CONFIG_ERROR);
+        }
+
+        if ($exp_id ne $last_exp_id) {
+            # get the metadata for the exposure (if any i.e. stack) 
+            # returns an appropriate string if !$exp_id
+            $exp_info = get_exposure_info($image_db, $exp_id);
+            $last_exp_id = $exp_id;
+        }
 
         if (($job_type eq "stamp") || ($job_type eq "get_image")) {
@@ -251,4 +267,5 @@
         my $command = "$pstamptool -updatereq -req_id $req_id -state stop -fault $request_fault";
         $command   .= " -dbname $dbname" if $dbname;
+        $command   .= " -dbserver $dbserver" if $dbserver;
         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
             run(command => $command, verbose => $verbose);
@@ -263,8 +280,8 @@
     my $req_id = shift;
     my $fault  = shift;
-    my $dbname = shift;
 
     my $command = "$pstamptool -updatereq -req_id $req_id -state stop -fault $fault";
     $command   .= " -dbname $dbname" if $dbname;
+    $command   .= " -dbserver $dbserver" if $dbserver;
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -285,5 +302,5 @@
     # Create a results file module and provide a list of the names (we have the data in the columns)
     my $rowinfo = "$row->{PROJECT}|$row->{JOB_TYPE}|$row->{REQ_TYPE}|$row->{IMG_TYPE}|";
-    $rowinfo   .= "$row->{ID}|$row->{CLASS_ID}|$row->{OPTION_MASK}|$row->{MJD_MIN}|$row->{MJD_MAX}|";
+    $rowinfo   .= "$row->{ID}|$row->{TESS_ID}|$row->{COMPONENT}|$row->{OPTION_MASK}|$row->{MJD_MIN}|$row->{MJD_MAX}|";
     $rowinfo   .= "$row->{REQFILT}|$row->{COORD_MASK}|$row->{CENTER_X}|$row->{CENTER_Y}|";
     $rowinfo   .= "$row->{WIDTH}|$row->{HEIGHT}";
