Index: trunk/pstamp/scripts/pstamp_parser_run.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 16976)
+++ trunk/pstamp/scripts/pstamp_parser_run.pl	(revision 17893)
@@ -5,11 +5,18 @@
 ###
 
+#XXX TODO: accept dbname save-temps and verbose as command line parameters
+#XXX see notes about error handling
+
 use warnings;
 use strict;
 
 use Sys::Hostname;
-my $host = hostname();
-print "\n\n";
-print "Starting script $0 on $host\n\n";
+
+my $verbose = 1;
+if ($verbose) {
+    my $host = hostname();
+    print "\n\n";
+    print "Starting script $0 on $host\n\n";
+}
 
 if (@ARGV != 1) {
@@ -18,5 +25,4 @@
 
 my $request_id = $ARGV[0];
-
 
 use IPC::Cmd 0.36 qw( can_run run );
@@ -57,4 +63,5 @@
 my $pstamptool  = can_run('pstamptool')  or (warn "Can't find pstamptool"  and $missing_tools = 1);
 my $pstampparse = can_run('pstampparse') or (warn "Can't find pstampparse" and $missing_tools = 1);
+my $dqueryparse = can_run('dqueryparse.pl') or (warn "Can't find dqueryparse.pl" and $missing_tools = 1);
 my $dsget = can_run('dsget') or (warn "Can't find dsget" and $missing_tools = 1);
 
@@ -71,5 +78,5 @@
     my $command = "$pstamptool -pendingreq -req_id $request_id";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => 1);
+        run(command => $command, verbose => $verbose);
     unless ($success) {
         die("Unable to perform $command error code: $error_code");
@@ -85,5 +92,8 @@
     my $requests = parse_md_list($metadata);
 
-    $psrequest = ${@$requests}[0];
+    # TODO: We are assuming that we get an array of length 1 (or zero)
+    # should we fail if we get more? 
+    #$psrequest = ${@$requests}[0];
+    $psrequest = $requests->[0];
 }
 
@@ -112,5 +122,5 @@
         my $command = "$dsget --uri $uri --filename $new_uri";
         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-            run(command => $command, verbose => 1);
+            run(command => $command, verbose => $verbose);
         unless ($success) {
             die("Unable to perform $command error code: $error_code");
@@ -121,10 +131,10 @@
 }
 
-#  create the output diredtory
+#  find the name of the output directory
 my $outdir;
 if ($ds_id) {
     my $command = "$pstamptool -datastore -ds_id $ds_id";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => 1);
+        run(command => $command, verbose => $verbose);
     unless ($success) {
         die("Unable to perform $command error code: $error_code");
@@ -140,5 +150,5 @@
     my $dataStores = parse_md_list($metadata);
 
-    my $dataStore = ${@$dataStores}[0];
+    my $dataStore = $dataStores->[0];
 
     $outdir = "$outputDataStoreRoot/$psrequest->{outFileset}";
@@ -147,4 +157,5 @@
 }
 
+#  create the output directory
 if (! -e $outdir ) {
     mkdir $outdir or die("unable to create output directory $outdir");
@@ -153,11 +164,28 @@
 }
 
-# run pstampparse to parse the queue the jobs for this request
+# run the appropriate parse command to parse the queue the jobs for this request
+# first check the extension header to find the EXTNAME
+my $request_type = find_request_type($uri);
+
+print STDERR "request_type is $request_type\n" if $verbose;
+
+my $parse_cmd;
+if ($request_type eq "PS1_PS_REQUEST") {
+    $parse_cmd = $pstampparse . " -mode queue_job -req_id $request_id -out_dir $outdir $uri";
+} elsif ($request_type eq "MOPS_DETECTABILITY_QUERY") {
+    $parse_cmd = $dqueryparse  . " --mode queue_job --req_id $request_id --out_dir $outdir --uri $uri";
+}
+
+if (!$parse_cmd) {
+    # XXX TODO mark the error state for the job and set it to stop
+    die "null request type found in $uri" if !$request_type;
+    die "unknown request type $request_type found in $uri";
+}
+
 {
-    my $command = "$pstampparse -mode queue_job -req_id $request_id -out_dir $outdir $uri";
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => 1);
-    unless ($success) {
-#        die("Unable to perform $command error code: $error_code");
+    my $command = "$parse_cmd";
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    unless ($success) {
         if (!open OUT, ">$outdir/parse_error") {
             die("unable to open parse_error file $outdir/parse_error");
@@ -167,5 +195,4 @@
     }
 }
-
 # XXX make sure that a job got actually got queued. If not set the request result and set state to
 # run so that the "request finisher" can build the results file.
@@ -177,5 +204,5 @@
     my $command = "$pstamptool -processedreq -req_id $request_id -state run";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => 1);
+        run(command => $command, verbose => $verbose);
     unless ($success) {
         die("Unable to perform $command error code: $error_code");
@@ -184,2 +211,14 @@
 
 exit 0;
+
+sub find_request_type {
+    # find the EXTNAME in the input fits table
+    # TODO: do this right handling errors etc.
+    my $file_name = shift;
+    my $out = `echo $file_name | fields -x 0 EXTNAME`;
+
+    # output from fields is filename value
+    my ($dummy, $extname) = split " ", $out;
+
+    return $extname;
+}
Index: trunk/pstamp/scripts/pstamp_queue_requests.pl
===================================================================
--- trunk/pstamp/scripts/pstamp_queue_requests.pl	(revision 16976)
+++ trunk/pstamp/scripts/pstamp_queue_requests.pl	(revision 17893)
@@ -5,4 +5,6 @@
 # the database of pending requests.
 #
+
+# XXX TODO: need to take dbname as a paramter
 
 use warnings;
@@ -89,7 +91,8 @@
     my $outProduct = $ds->{outProduct};
     my $ds_id = $ds->{ds_id};
-    my @raw_output;
+    my @lines;
     {
-        my $command = "$dsproductls --uri $ds->{uri}/index.txt --last_fileset $ds->{lastFileset}";
+        my $command = "$dsproductls --uri $ds->{uri}/index.txt";
+        $command .= " --last_fileset $ds->{lastFileset}" if $ds->{lastFileset};
         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
             run(command => $command, verbose => $verbosity);
@@ -100,75 +103,60 @@
         if (@$stdout_buf == 0) {
             print STDERR "no new request files in data store $ds_id\n";
-            next;
+            next; # next data store
         }
-        @raw_output = @$stdout_buf;
+        my $out_buf = join("", @$stdout_buf);
+        # split raw output into lines
+        # XXX: why do I think that i need the map?
+        #my @lines = map {split "\n"} $raw_output;
+        @lines = split /^/, $out_buf;
     }
 
-    foreach my $fs (@raw_output) {
-        # split into lines
-        my @lines = map {split "\n"} $fs;
+    #
+    # each line contains a fileset
+    #
+    foreach my $line (@lines) {
+        # parse the line into fields split by whitespace
+        my ($uri, $fs_name, $date, $type) = split " ", $line;
+
+        next if ( $uri =~ /^#.*/);
+
+        my @files;
+        {
+            my $command = "$dsfilesetls --uri $uri";
+            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                    run(command => $command, verbose => $verbosity);
+            unless ($success) {
+                die("Unable to perform $command: $error_code");
+            }
+
+            if (@$stdout_buf == 0) {
+                print STDERR "no file sets found\n";
+                next;
+            }
+            my $out_buf = join "", @$stdout_buf;
+            @files = split /^/, $out_buf;
+        }
 
         #
-        # each line contains a fileset
+        # For each file in the fileset add a request
         #
-        foreach my $line (@lines) {
-            # parse the line into fields split by whitespace
-            my ($uri, $fs_name, $date, $type) = split " ", $line;
-            $_ = $uri;
+        foreach my $file (@files) {
+            my ($req_uri, $fn, $size, $md5sum, $type, $chipname) = split " ", $file;
+
             # skip comment lines
-            if (/^#.*/) {
-                next;
+            next if $req_uri =~ (/^#.*/);
+            {
+                my $command = "$pstamptool -addreq -uri $req_uri -ds_id $ds_id -out_fileset $outProduct/$fs_name";
+                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+                        run(command => $command, verbose => $verbosity);
+                    unless ($success) {
+                            # XXX: what do we do now?
+                            die("Unable to perform $command: $error_code");
+                    }
             }
-
-            my @out_buf;
-            {
-                my $command = "$dsfilesetls --uri $uri";
-                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-                    run(command => $command, verbose => $verbosity);
-                unless ($success) {
-                    die("Unable to perform $command: $error_code");
-                }
-
-                if (@$stdout_buf == 0) {
-                    print STDERR "no file sets found\n";
-                    next;
-                }
-                @out_buf = @$stdout_buf;
-            }
-
-            #
-            # This is getting indented pretty far
-            # Looks like we're going to need to learn about functions/subroutines :)
-            #
-
-            ## TODO: fail if there is more than one request file in the fileset
-            foreach my $bit (@out_buf) {
-                my @files = map {split "\n"} $bit;
-                #
-                # For each file in the fileset add a request
-                #
-                foreach my $file (@files) {
-                    my ($req_uri, $fn, $size, $md5sum, $type, $chipname) = split " ", $file;
-
-                    # skip comment lines
-                    $_ = $req_uri;
-                    if (/^#.*/) {
-                        next;
-                    }
-
-                    {
-                        my $command = "$pstamptool -addreq -uri $req_uri -ds_id $ds_id -out_fileset $outProduct/$fs_name";
-                        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-                            run(command => $command, verbose => $verbosity);
-                        unless ($success) {
-                                # XXX: what do we do now?
-                                die("Unable to perform $command: $error_code");
-                        }
-                    }
-                    $lastFileset = $fs_name;
-                    # we only allow one request file per fileset
-                    last;
-                }
-            }
+            $lastFileset = $fs_name;
+            # we only allow one request file per fileset
+            # XXX TODO: maybe Relax this restriction
+            last;
         }
     }
