Index: trunk/pstamp/scripts/dqueryparse.pl
===================================================================
--- trunk/pstamp/scripts/dqueryparse.pl	(revision 18237)
+++ trunk/pstamp/scripts/dqueryparse.pl	(revision 18237)
@@ -0,0 +1,117 @@
+#!/usr/bin/env perl
+#
+# parse a MOPS_DETCTABILITY_QUERY table and create a results file
+#
+# Note: this file is currently only a placeholder which creates a fake response file
+# and adds a completed job to the database
+#
+# TODO: split off the actual generation of the response file into a standalone program
+#
+
+use strict;
+use warnings;
+
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
+use IPC::Cmd 0.36 qw( can_run run );
+
+use PS::IPP::Config qw($PS_EXIT_SUCCESS
+		       $PS_EXIT_UNKNOWN_ERROR
+		       $PS_EXIT_SYS_ERROR
+		       $PS_EXIT_CONFIG_ERROR
+		       $PS_EXIT_PROG_ERROR
+		       $PS_EXIT_DATA_ERROR
+		       $PS_EXIT_TIMEOUT_ERROR
+		       metadataLookupStr
+		       metadataLookupBool
+		       caturi
+		       );
+
+my ($uri, $out_dir, $mode, $req_id, $verbose, $save_temps);
+
+#
+# parse args
+#
+
+GetOptions(
+        'uri=s'           =>      \$uri,
+        'out_dir=s'       =>      \$out_dir,
+        'verbose'         =>      \$verbose,
+        'mode=s'          =>      \$mode,
+        'req_id=s'        =>      \$req_id,
+        'save-temps'      =>      \$save_temps,
+        'verbose'         =>      \$verbose,
+) or pod2usage(2);
+
+my $err = "";
+
+if (!$uri) {
+    $err .= "--uri is required\n";
+}
+if (!$out_dir) {
+    $err .="--out_dir is required\n";
+}
+
+if (!$req_id) {
+    $err .= "--req_id is required\n";
+}
+
+die $err if ($err);
+
+my $missing_tools;
+my $pstamptool = can_run('pstamptool') or (warn "Can't find pstamptool" and $missing_tools =1);
+my $fakedresponse = can_run('fakedresponse.pl') or (warn "Can't find fakedresponse.pl" and $missing_tools =1);
+
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit ($PS_EXIT_CONFIG_ERROR);
+}
+
+my $response_file = "$out_dir/response.fits";
+my $result;
+{
+    my $command = "$fakedresponse --input $uri --output $response_file --workdir $out_dir";
+    $command .= " --save-temps" if $save_temps;
+    $command .= " --verbose" if $verbose;
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+    unless ($success) {
+        warn("Unable to perform $command error code: $error_code");
+    }
+    $result = $error_code >> 8;
+}
+
+my $job_id;
+{
+    my $command = "$pstamptool -addjob -req_id $req_id -uri $uri -outputBase $out_dir -job_type detect_query";
+    $command .= " -state stop -result $result";
+
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => $verbose);
+
+    if ($success) {
+        $job_id = join "", @$stdout_buf;
+        chomp $job_id;
+        $result = 0;
+    } else {
+        warn("Unable to perform $command error code: $error_code");
+        $result = $error_code >> 8;
+    }
+}
+
+exit $result;
+
+
+
+
+sub fake_dquery_response {
+    my $line = shift;
+    my ($rownum, $ra1, $dec1, $ra2, $dec2, $mag) = split " ", $line;
+
+    # todo perhaps think more about these values
+    my $n = rand(16);
+    my $f = rand(1);
+
+    return ($rownum, $n, $f);
+}
Index: trunk/pstamp/scripts/fakedresponse.pl
===================================================================
--- trunk/pstamp/scripts/fakedresponse.pl	(revision 18237)
+++ trunk/pstamp/scripts/fakedresponse.pl	(revision 18237)
@@ -0,0 +1,103 @@
+#!/usr/bin/env perl
+#
+# parse a MOPS_DETCTABILITY_QUERY table and create a results file
+#
+# Note: this file is currently only a placeholder which creates a fake response file
+#
+
+use strict;
+use warnings;
+
+use Getopt::Long qw( GetOptions );
+use Pod::Usage qw( pod2usage );
+use DBI;
+use PS::IPP::Metadata::Config;
+use PS::IPP::Metadata::Stats;
+use PS::IPP::Metadata::List qw( parse_md_list );
+use File::Copy;
+use File::Basename;
+
+use PS::IPP::Config qw($PS_EXIT_SUCCESS
+		       $PS_EXIT_UNKNOWN_ERROR
+		       $PS_EXIT_SYS_ERROR
+		       $PS_EXIT_CONFIG_ERROR
+		       $PS_EXIT_PROG_ERROR
+		       $PS_EXIT_DATA_ERROR
+		       $PS_EXIT_TIMEOUT_ERROR
+		       metadataLookupStr
+		       metadataLookupBool
+		       caturi
+		       );
+my ($input, $output, $verbose, $save_temps, $workdir);
+#
+# parse args
+#
+
+
+GetOptions(
+        'input=s'          =>     \$input,
+        'output=s'        =>      \$output,
+        'workdir=s'       =>      \$workdir,
+        'verbose'         =>      \$verbose,
+        'save-temps'      =>      \$save_temps,
+) or pod2usage(2);
+
+my $err = "";
+
+if (!$input) {
+    $err .= "--input is required\n";
+}
+if (!$output) {
+    $err .="--output is required\n";
+}
+
+die $err if ($err);
+
+# spit the contents of the file in text format 
+# XXX Do we lose precision on the floats when we do this?
+
+my $query_text = `detect_query_read --nolabel --input $input`;
+
+my @lines = split "^", $query_text;
+
+# first line is the header keywords
+my $header = shift @lines;
+chomp $header;
+die "failed to parse $input" unless $header;
+
+my ($query_id, $fpa_id, $dateobs, $filter, $obscode) = split " ", $header;
+die "failed to parse $input" unless $query_id && $fpa_id && $dateobs && $filter && $obscode;
+
+if (!$workdir) {
+    $workdir="/tmp";
+}
+my $txt_file = "$workdir/response$$.txt";
+open OUT, ">$txt_file";
+
+print OUT "$query_id $fpa_id $dateobs $filter $obscode\n";
+
+while (my $line = shift @lines) {
+    chomp $line;
+    my ($rownum, $detect_n, $detect_f) = fake_dquery_response($line);
+
+    printf OUT "%-12s %-8d %-8.4f\n", $rownum, $detect_n, $detect_f;
+}
+close OUT;
+
+my $result = system "detect_response_create --input $txt_file --output $output";
+
+unlink $txt_file unless $save_temps;
+
+exit $result;
+
+
+sub fake_dquery_response {
+    my $line = shift;
+    my ($rownum, $ra1, $dec1, $ra2, $dec2, $mag) = split " ", $line;
+
+    # todo perhaps think more about these values
+    my $n = rand(16);
+    my $f = rand(1);
+
+    return ($rownum, $n, $f);
+}
