Index: /tags/ipp-20100823/ippTasks/pstamp.pro
===================================================================
--- /tags/ipp-20100823/ippTasks/pstamp.pro	(revision 29332)
+++ /tags/ipp-20100823/ippTasks/pstamp.pro	(revision 29333)
@@ -16,4 +16,5 @@
 $pstampRevDep_DB = 0
 $pstampCleanup_DB = 0
+$pstampStopFaulted_DB = 0
 
 # set PS_DBSERVER if postage stamp database host is not the same as the value for DBSERVER in site.config
@@ -90,7 +91,4 @@
         active false
     end
-    task pstamp.job.revert
-        active false
-    end
     task pstamp.dependent.load
         active false
@@ -108,4 +106,7 @@
         active true
     end
+    task pstamp.stopfaulted
+        active true
+    end
 end
 macro pstamp.revert.off
@@ -114,4 +115,7 @@
     end
     task pstamp.dependent.revert
+        active false
+    end
+    task pstamp.stopfaulted
         active false
     end
@@ -845,2 +849,43 @@
     end
 end
+
+task pstamp.stopfaulted
+    host        local
+
+    periods     -poll $LOADPOLL
+    periods     -exec 30
+    periods     -timeout 300
+    npending    1
+
+    task.exec
+        stderr $LOGSUBDIR/pstamp.stopfaulted.log
+        stderr $LOGSUBDIR/pstamp.stopfaulted.log
+        $run = pstampstopfaulted
+        if ($DB:n == 0)
+            option DEFAULT
+        else 
+            $run = $run $PS_DBSERVER -dbname $DB:$pstampStopFaulted_DB
+            $pstampStopFaulted_DB ++
+            if ($pstampStopFaulted_DB >= $DB:n) set pstampStopFaulted_DB = 0
+        end
+        add_poll_args run
+        add_poll_labels run
+        command $run
+    end
+
+    task.exit $EXIT_SUCCESS
+        # nothing to do
+    end
+
+    task.exit   default
+        showcommand failure
+    end
+
+    task.exit   crash
+        showcommand crash
+    end
+
+    task.exit   timeout
+        showcommand timeout
+    end
+end
Index: /tags/ipp-20100823/ippTools/share/pstamptool_updatejob.sql
===================================================================
--- /tags/ipp-20100823/ippTools/share/pstamptool_updatejob.sql	(revision 29332)
+++ /tags/ipp-20100823/ippTools/share/pstamptool_updatejob.sql	(revision 29333)
@@ -1,2 +1,4 @@
-UPDATE pstampJob LEFT JOIN pstampDependent USING(dep_id)
+UPDATE pstampJob 
+    JOIN pstampRequest USING(req_id) 
+    LEFT JOIN pstampDependent USING(dep_id)
 SET 
Index: /tags/ipp-20100823/ippTools/share/pxadmin_create_tables.sql
===================================================================
--- /tags/ipp-20100823/ippTools/share/pxadmin_create_tables.sql	(revision 29332)
+++ /tags/ipp-20100823/ippTools/share/pxadmin_create_tables.sql	(revision 29333)
@@ -1441,10 +1441,13 @@
         imagedb    VARCHAR(64),
         rlabel     VARCHAR(64),
+        need_magic TINYINT,
         outdir     VARCHAR(255),
-        need_magic TINYINT,
+        fault      SMALLINT,
+        fault_count INT,
         PRIMARY KEY(dep_id),
         KEY(state),
         KEY(stage),
-        KEY(stage_id)
+        KEY(stage_id),
+        KEY(fault)
 ) ENGINE=innodb DEFAULT CHARSET=latin1;
 
Index: /tags/ipp-20100823/ippTools/src/pstamptool.c
===================================================================
--- /tags/ipp-20100823/ippTools/src/pstamptool.c	(revision 29332)
+++ /tags/ipp-20100823/ippTools/src/pstamptool.c	(revision 29333)
@@ -883,7 +883,8 @@
     PXOPT_LOOKUP_S64(req_id,    config->args, "-req_id", false, false);
     PXOPT_LOOKUP_S64(dep_id,    config->args, "-dep_id", false, false);
-
-    if (!job_id && !req_id && !dep_id) {
-        psError(PS_ERR_UNKNOWN, true, "at least one of -job_id -req_id or -dep_id is required");
+    PXOPT_LOOKUP_S32(fault_count, config->args, "-fault_count",  false, false);
+
+    if (!job_id && !req_id && !dep_id && !fault_count) {
+        psError(PS_ERR_UNKNOWN, true, "at least one of -job_id -req_id -dep_id or -fault_count is required");
         return false;
     }
@@ -904,4 +905,6 @@
     PXOPT_COPY_S32(config->args, where, "-fault",  "pstampJob.fault", "==");
     PXOPT_COPY_STR(config->args, where, "-state",  "pstampJob.state", "==");
+    PXOPT_COPY_S32(config->args, where, "-fault_count", "pstampJob.fault_count", ">=");
+    pxAddLabelSearchArgs(config, where, "-label", "pstampRequest.label", "LIKE");
 
     psString query = pxDataGet("pstamptool_updatejob.sql");
@@ -948,4 +951,8 @@
     PXOPT_COPY_S64(config->args, where, "-dep_id", "dep_id", "==");
     PXOPT_COPY_S32(config->args, where, "-fault",  "pstampDependent.fault", "==");
+    pxAddLabelSearchArgs(config, where, "-label", "pstampRequest.label", "LIKE");
+    // if (fault_count) {
+        PXOPT_COPY_S32(config->args, where, "-fault_count", "pstampDependent.fault_count", ">=");
+    // }
 
     // XXX: How about selecting by pstampRequest.label? No. That is too dangerous by itself.
Index: /tags/ipp-20100823/ippTools/src/pstamptoolConfig.c
===================================================================
--- /tags/ipp-20100823/ippTools/src/pstamptoolConfig.c	(revision 29332)
+++ /tags/ipp-20100823/ippTools/src/pstamptoolConfig.c	(revision 29333)
@@ -167,6 +167,9 @@
     psMetadataAddStr(updatejobArgs, PS_LIST_TAIL, "-state", 0,             "current state of jobs to update", 0);
     psMetadataAddS16(updatejobArgs, PS_LIST_TAIL, "-fault", 0,             "current value for job fault", 0);
+    psMetadataAddStr(updatejobArgs, PS_LIST_TAIL, "-label", PS_META_DUPLICATE_OK, "search by pstampJob label (LIKE comparision)", NULL);
+    psMetadataAddS32(updatejobArgs, PS_LIST_TAIL, "-fault_count", 0,       "select by fault_count (>=)", 0);
     psMetadataAddStr(updatejobArgs, PS_LIST_TAIL, "-set_state", 0,            "new state", NULL);
     psMetadataAddS16(updatejobArgs, PS_LIST_TAIL, "-set_fault", 0,            "new result", 0);
+    psMetadataAddU64(updatejobArgs, PS_LIST_TAIL, "-limit", 0,      "not used", 0);
 
     // -stopdependentjob
@@ -176,5 +179,8 @@
     psMetadataAddS64(stopdependentjobArgs, PS_LIST_TAIL,  "-dep_id", 0,       "dep_id of jobs to update", 0);
     psMetadataAddS16(stopdependentjobArgs, PS_LIST_TAIL,  "-fault", 0,        "current value for dependent fault", 0);
+    psMetadataAddS32(stopdependentjobArgs, PS_LIST_TAIL,  "-fault_count", 0,   "select by fault_count (>=)", 0);
     psMetadataAddS16(stopdependentjobArgs, PS_LIST_TAIL,  "-set_fault", 0,    "new fault value for job and dependent (required)", 0);
+    psMetadataAddStr(stopdependentjobArgs, PS_LIST_TAIL, "-label", PS_META_DUPLICATE_OK, "search by pstampJob label (LIKE comparision)", NULL);
+    psMetadataAddU64(stopdependentjobArgs, PS_LIST_TAIL, "-limit", 0,      "not used", 0);
 
     // -revertjob
@@ -187,5 +193,5 @@
     psMetadataAddBool(revertjobArgs, PS_LIST_TAIL, "-clear_fault_count", 0,       "clear job fault count", false);
     psMetadataAddBool(revertjobArgs, PS_LIST_TAIL, "-all", 0,       "revert all faulted jobs", false);
-    psMetadataAddU64(revertjobArgs, PS_LIST_TAIL, "-limit", 0,      "limit result set to N items", 0);
+    psMetadataAddU64(revertjobArgs, PS_LIST_TAIL, "-limit", 0,      "not used", 0);
 
     // -getdependent
Index: /tags/ipp-20100823/pstamp/scripts/Makefile.am
===================================================================
--- /tags/ipp-20100823/pstamp/scripts/Makefile.am	(revision 29332)
+++ /tags/ipp-20100823/pstamp/scripts/Makefile.am	(revision 29333)
@@ -19,4 +19,6 @@
 	pstamp_webrequest.pl \
         pstamp_get_image_job.pl \
+	psmkreq \
+	pstampstopfaulted \
 	pstamp_checkdependent.pl \
 	request_finish.pl \
Index: /tags/ipp-20100823/pstamp/scripts/detectability_respond.pl
===================================================================
--- /tags/ipp-20100823/pstamp/scripts/detectability_respond.pl	(revision 29332)
+++ /tags/ipp-20100823/pstamp/scripts/detectability_respond.pl	(revision 29333)
@@ -644,5 +644,5 @@
 	$columns = [
 	    # matching rownum from detectability original request
-	    { name => 'ROWNUM',   type => 'V', writetype => TULONG }, 
+	    { name => 'ROWNUM',   type => '20A', writetype => TULONG }, 
 	    # any errors that occurred during processing
 	    { name => 'ERROR_CODE',   type => 'V', writetype => TULONG }, 
Index: /tags/ipp-20100823/pstamp/scripts/psmkreq
===================================================================
--- /tags/ipp-20100823/pstamp/scripts/psmkreq	(revision 29333)
+++ /tags/ipp-20100823/pstamp/scripts/psmkreq	(revision 29333)
@@ -0,0 +1,374 @@
+#!/bin/env perl
+###
+### pstamprequest
+###
+###     Program to make a postage stamp request table for a set of coordinates
+###
+
+use warnings;
+use strict;
+
+use Getopt::Long qw( GetOptions ) ; # :config auto_help auto_version gnu_getopt );
+use Pod::Usage qw( pod2usage );
+use PS::IPP::Config qw( :standard );
+use PS::IPP::PStamp::RequestFile qw( :standard );
+use PS::IPP::PStamp::Job qw( :standard );
+use File::Temp qw(tempfile);
+use File::Basename qw(basename);
+use IPC::Cmd 0.36 qw( can_run run );
+use Carp;
+use POSIX;
+
+my $verbose;
+my $save_temps;
+
+# list file columns
+# RA DEC FILTER MJD_MIN MJD_MAX | COMMENT
+
+my ($ra, $dec, $x, $y, $list, $output, $req_name, $req_name_base);
+
+my ($image, $mask, $variance, $cmf, $psf, $backmdl, $inverse);
+my ($unconvolved, $use_imfile_id, $no_wait);
+
+my $default_size = 100;
+my $job_type     = 'stamp';
+my $req_type     = 'bycoord';
+my $stage        = 'chip';
+my $option_mask;
+my $width        = $default_size;
+my $height       = $default_size;
+my $project      = 'gpc1';
+my $coord_mask;
+my $pixcenter;
+my $arcseconds;
+
+my $id;
+my $tess_id = 'null';
+my $component = 'null';
+my $data_group = 'null';
+my $filter;
+my $mjd_min = 0;
+my $mjd_max = 0;
+my $comment;
+
+my $missing_tools;
+my $pstamp_request_file  = can_run('pstamp_request_file')  or (warn "Can't find required program pstamp_request_file"  and $missing_tools = 1);
+
+if ($missing_tools) {
+    warn("Can't find required tools.");
+    exit ($PS_EXIT_CONFIG_ERROR);
+}
+
+GetOptions(
+    'list=s'            => \$list,          # list of coordinates if undef ra and dec or x and y are required
+    'ra=s'              => \$ra,            # 
+    'dec=s'             => \$dec,
+    'x=s'               => \$x,
+    'y=s'               => \$y,
+    'width=i'           => \$width,
+    'height=i'          => \$height,
+    'pixcenter'         => \$pixcenter,
+    'arcseconds'        => \$arcseconds,
+    'coord_mask=i'      => \$coord_mask,
+    'job_type=s'        => \$job_type,
+    'output=s'          => \$output,
+    'req_name=s'        => \$req_name,
+    'req_name_base=s'   => \$req_name_base,
+    'stage=s'           => \$stage,
+    'project=s'         => \$project,
+    'req_type=s'        => \$req_type,
+    'id=s'              => \$id,
+    'tess_id=s'         => \$tess_id,
+    'component=s'       => \$component,
+    'data_group=s'      => \$data_group,
+    'filter=s'          => \$filter,
+    'mjd_min=s'         => \$mjd_min,
+    'mjd_max=s'         => \$mjd_max,
+    'comment=s'         => \$comment,
+
+    'option_mask=i'     => \$option_mask,
+    'image'             => \$image,
+    'mask'              => \$mask,
+    'variance'          => \$variance,
+    'cmf'               => \$cmf,
+    'psf'               => \$psf,
+    'backmdl'           => \$backmdl,
+    'inverse'           => \$inverse,
+    'unconvolved'       => \$unconvolved,
+    'use_imfile_id'     => \$use_imfile_id,
+    'do_not_wait'       => \$no_wait,
+
+    'verbose'           => \$verbose,
+    'save-temps'        => \$save_temps,
+) or pod2usage(2);
+
+pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
+
+pod2usage( -msg => "Required options: --req_name | --req_name_base --stage ") 
+    unless (defined $req_name or defined $req_name_base)
+       and defined $stage;
+
+pod2usage( -msg => "Invalid job_type: $job_type", -exitval =>1 )
+        unless ($job_type eq 'stamp' or $job_type eq 'get_image');
+
+if ($job_type eq 'stamp') {
+    if (defined $list) {
+        pod2usage( -msg => "--ra --dec --x --y are not used with --list", -exitval =>1 )
+            if defined $x or defined $y;
+    } elsif (!$pixcenter) {
+        pod2usage( -msg => "Required options for stamp requests: --ra and --dec or --list", -exitval =>1 )
+            unless (defined $ra and defined $dec);
+        # to simplify code we just use $x and $y from here
+        $x = $ra;
+        $y = $dec;
+    } else {
+        pod2usage( -msg => "Required options for stamp requests: --x and --y or --list", -exitval =>1 )
+                unless (defined $x and defined $y);
+    }
+} else {
+    pod2usage( -msg => "req_type must be byid or byexp for get_image requests",
+        -exitval =>1 ) unless $req_type eq 'byid' or $req_type eq 'byexp';
+    $x  = 0;
+    $y = 0;
+}
+
+if ($req_type eq 'byid') {
+    pod2usage( -msg => "ID required for get_image requests", -exitval =>1 )
+            if !defined $id;
+    die("ID must be number for byid requests") if !validID($id);
+} elsif ($req_type eq 'byexp') {
+    pod2usage( -msg => "ID (exp_name) required for byexp requests", -exitval =>1 )
+            if !defined $id;
+} elsif ($req_type eq 'bydiff') {
+    pod2usage( -msg => "ID (exp_name) required for bydiff requests", -exitval =>1 )
+            if !defined $id;
+} elsif ($req_type eq 'byskycell') {
+    pod2usage( -msg => "tess_id and skycell_id required for byskycell requests", -exitval =>1 )
+            if !(defined $tess_id and defined $component);
+} elsif ($req_type ne 'bycoord') {
+    pod2usage( -msg => "$req_type is not a valid req_type", -exitval =>1 );
+}
+
+$id = 0 if !$id;
+
+unless ($stage eq 'raw' or $stage eq 'chip' or $stage eq 'warp' or $stage eq 'diff' or $stage eq 'stack') {
+    die "$stage is not a valid value for stage\n";
+}
+
+checkFilter($filter, 'null', $filter)  if $filter;
+checkMJD($mjd_min, 0, "") if $mjd_min;
+checkMJD($mjd_max, 0, "" ) if $mjd_min;
+
+# user supplied option mask takes precedence
+if (!defined $option_mask) {
+    $option_mask = 0;
+    if ($job_type eq 'stamp') {
+        $option_mask |= $PSTAMP_SELECT_IMAGE    if $image;
+        $option_mask |= $PSTAMP_SELECT_MASK     if $mask;
+        $option_mask |= $PSTAMP_SELECT_VARIANCE if $variance;
+        $option_mask = $PSTAMP_SELECT_IMAGE    if $option_mask == 0;
+
+        # if no image was requested make a stamp of the image
+
+        $option_mask |= $PSTAMP_SELECT_CMF      if $cmf;
+        $option_mask |= $PSTAMP_SELECT_PSF      if $psf;
+        $option_mask |= $PSTAMP_SELECT_BACKMDL  if $backmdl;
+        $option_mask |= $PSTAMP_SELECT_UNCONV   if $unconvolved;
+        $option_mask |= $PSTAMP_USE_IMFILE_ID   if $use_imfile_id;
+        $option_mask |= $PSTAMP_SELECT_INVERSE  if $inverse;
+    }
+    $option_mask |= $PSTAMP_NO_WAIT_FOR_UPDATE if $no_wait;
+}
+
+
+# user supplied coord_mask takes precedence
+if (!defined $coord_mask) {
+    $coord_mask = 0;
+    if ($pixcenter) {
+        $coord_mask |= $PSTAMP_CENTER_IN_PIXELS;
+    }
+    if (!$arcseconds) {
+        $coord_mask |= $PSTAMP_RANGE_IN_PIXELS;
+    }
+}
+
+
+# if req_name is not supplied use the current date and time to build one off of the base
+if (!$req_name) {
+    my $datestr = strftime "%Y%m%dT%H%M%S", gmtime;
+    $req_name .= $req_name_base . $datestr;
+}
+
+# ok ready to go
+
+my $rows;
+if ($list) {
+    $rows = readList($list);
+} else {
+    $rows = [];
+    push @$rows, buildRow("", $comment, $x, $y, $filter, $mjd_min, $mjd_max);
+}
+
+my ($tdf, $table_def_name) = tempfile ("/tmp/tabledef.XXXX", UNLINK => !$save_temps);
+print $tdf "$req_name 1\n";
+my $rownum = 0;
+foreach my $row (@$rows) {
+    $rownum++;
+    my $line = "$rownum $row->{ra}\t$row->{dec}\t$width $height"
+        . " $coord_mask $job_type $option_mask $project $req_type"
+        . " $stage $id $tess_id $component $data_group"
+        . " $row->{filter} $row->{mjd_min} $row->{mjd_max}";
+
+    if ($row->{comment} and $row->{comment} ne '') {
+        $line .= " |$row->{comment}" ;
+    } else {
+        $line .= " |$comment" if $comment;
+    }
+
+    print "$line\n" if $verbose;
+    print $tdf "$line\n";
+}
+close $tdf;
+
+{
+    my $command = "$pstamp_request_file --input $table_def_name --req_name $req_name";
+    $command .= " --output $output" if $output;
+    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
+        run(command => $command, verbose => 0);
+    unless ($success) {
+        print STDERR @$stderr_buf;
+        exit $error_code >> 8;
+    }
+}
+
+exit 0;
+
+sub readList {
+    my $file = shift;
+    open IN, "<$file" or die "failed to open $file for input\n";
+
+    my @rows;
+
+    my $linenumber = 0;
+    foreach my $line (<IN>) {
+        $linenumber++;
+        chomp $line;
+        next if !$line;
+        next if ($line =~ /^#/);
+
+        my ($spec, $comment) = split /\|/, $line;
+        $comment = '' if !defined $comment;
+
+        my @vals = split " ", $spec;
+        push @rows, buildRow("at line number $linenumber", $comment, @vals);
+    }
+    close IN;
+
+    return \@rows;
+}
+
+sub buildRow {
+    my $linenumber = shift;
+    my $comment = shift;
+    my @vals = @_;
+
+    my $row = {};
+    $row->{ra}      = checkRA($vals[0], $linenumber);
+    $row->{dec}     = checkDEC($vals[1], $linenumber);
+    $row->{filter}  = checkFilter($vals[2], $filter, $linenumber);
+    $row->{mjd_min} = checkMJD($vals[3], $mjd_min, $linenumber);;
+    $row->{mjd_max} = checkMJD($vals[4], $mjd_max, $linenumber);;
+    $row->{comment} = $comment;
+
+    return $row;
+}
+
+sub validNumber {
+    my $val = shift;
+
+    return 0 if !defined $val;
+
+    return ($val =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/);
+}
+
+sub validID
+{
+    my $val = shift;
+
+    return 0 if !$val;
+
+    return  ! ($val =~ /\D/);
+}
+
+sub checkCoord {
+    my $c          = shift;
+    my $linenumber = shift;
+
+    my $result;
+    if ($c =~ /\:/) {
+        # sexagesmial format not valid for pixel coordinates
+        die "invalid coordinate value $c found $linenumber\n" if $pixcenter;
+
+        my ($d, $m, $s) = split '\:', $c;
+        die "invalid coordinate value $c found $linenumber\n" 
+            unless validNumber($d) and validNumber($m) and validNumber($s);
+        my $sign;
+        if ($c =~ /^-/) {   # checking $d < 0 doesn't work for $d = -0
+            $sign = -1;
+            $d = -$d;
+        } else {
+            $sign = 1;
+        }
+        $result = $sign * ($d + ($m + $s/60.) /60.);
+    } else {
+        die "$c is not  valid coordinate value $linenumber\n" 
+            unless validNumber($c);
+        $result = $c;
+    }
+    return $result;
+}
+
+sub checkRA {
+    my $ra = shift;
+    my $linenumber = shift;
+    my $checked = checkCoord($ra, $linenumber);
+
+    if ($ra =~ /\:/) {
+        return $checked * 360. / 24.;
+    } else {
+        return $checked;
+    }
+}
+
+sub checkDEC {
+    return checkCoord(@_);
+}
+
+sub checkFilter {
+    my $f = shift;
+    my $default = shift;
+    my $linenumber = shift;
+
+    $default = 'null' if !defined $default;
+
+    $f = $default if (!defined$f or $f eq 'null');
+
+    return $f;
+}
+
+sub checkMJD {
+    my $mjd = shift;
+    my $default = shift;
+    my $linenumber = shift;;
+
+    return $default if (!$mjd) ;
+
+    die "invalid mjd value '$mjd' found $linenumber\n" if !validNumber($mjd);
+
+    return $mjd;
+}
+
+sub checkStage {
+    my $stage = shift;
+
+}
Index: /tags/ipp-20100823/pstamp/scripts/pstamp_finish.pl
===================================================================
--- /tags/ipp-20100823/pstamp/scripts/pstamp_finish.pl	(revision 29332)
+++ /tags/ipp-20100823/pstamp/scripts/pstamp_finish.pl	(revision 29333)
@@ -191,5 +191,7 @@
             my $filter = $job_params->{filter};
             $filter = "0" if !$filter;
-            $exp_info = "0|0|0|$filter|0|0";
+            my $mjd_obs = $job_params->{mjd_obs};
+            $mjd_obs = "0" if !$mjd_obs;
+            $exp_info = "$mjd_obs|0|0|$filter|0|0";
         }
 
@@ -477,4 +479,12 @@
     my $ticks = timegm($sec, $min, $hr, $day, $mon-1, $year-1900);
 
+    # dateobs is UTC convert to TAI
+    # XXX: Do this properly
+    if ($year >= 2009) {
+        $ticks += 34;
+    } else {
+        $ticks += 33;
+    }
+
     return 40587.0 + ($ticks/86400.);
 }
Index: /tags/ipp-20100823/pstamp/scripts/pstamp_insert_request.pl
===================================================================
--- /tags/ipp-20100823/pstamp/scripts/pstamp_insert_request.pl	(revision 29332)
+++ /tags/ipp-20100823/pstamp/scripts/pstamp_insert_request.pl	(revision 29333)
@@ -44,5 +44,5 @@
 
 my $pstamptool = can_run('pstamptool')  or (warn "Can't find pstamptool"  and $missing_tools = 1);
-my $pstampdump = can_run('pstampdump')  or (warn "Can't find pstampdump"  and $missing_tools = 1);
+my $fields = can_run('fields')  or (warn "Can't find fields"  and $missing_tools = 1);
 
 if ($missing_tools) {
@@ -53,19 +53,25 @@
 my ($extname, $extver, $req_name);
 {
-    my $command = "$pstampdump -headeronly $tmp_req_file";
+    # get the header keywords of interest.
+    # Note that if it's a pstamp request then REQ_NAME should be defined.
+    # if it's a detectability query it will not have a REQ_NAME but will have a QUERY_ID
+    my $command = "echo $tmp_req_file | $fields -x 0 EXTNAME EXTVER REQ_NAME QUERY_ID";
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
         run(command => $command, verbose => $verbose);
+if (0) {
+    # stoopid fields doesn't set exit status to zero when it works
     unless ($success) {
         print STDERR @$stderr_buf;
         exit $error_code >> 8;
     }
+}
     my $output = join "", @$stdout_buf;
-    ($extname, $extver, $req_name) = split " ", $output;
-    if ($extname and ($extname ne "PS1_PS_REQUEST")) {
+    (undef, $extname, $extver, $req_name) = split " ", $output;
+    if (!$extname or ! (($extname eq "PS1_PS_REQUEST") or ($extname eq "MOPS_DETECTABILITY_QUERY"))) {
         print STDERR "invalid request file\n";
         exit $PS_EXIT_DATA_ERROR;
     }
     if (!defined $req_name) {
-        print STDERR "invalid request file no REQ_NAME\n";
+        print STDERR "invalid request file no REQ_NAME or QUERY_ID\n";
         exit $PS_EXIT_DATA_ERROR;
     }
@@ -91,6 +97,5 @@
 my $req_id = 0;
 {
-
-    my $command = "$pstamptool -addreq -name $req_name -uri $req_file -ds_id 0";
+    my $command = "$pstamptool -addreq -uri $req_file -ds_id 0 -name $req_name";
     $command .= " -dbname $dbname" if $dbname;
     $command .= " -dbserver $dbserver" if $dbserver;
Index: /tags/ipp-20100823/pstamp/scripts/pstamp_server_status
===================================================================
--- /tags/ipp-20100823/pstamp/scripts/pstamp_server_status	(revision 29332)
+++ /tags/ipp-20100823/pstamp/scripts/pstamp_server_status	(revision 29333)
@@ -16,4 +16,5 @@
 
 my ($rundir, $verbose, $save_temps);
+my $br = '<br />';
 
 GetOptions(
@@ -99,10 +100,10 @@
     print "Pantasks Scheduler $scheduler_state.\n";
     if ($controller_state) {
-        print "Pantasks Controller $controller_state.\n";
+        print $br . "Pantasks Controller $controller_state.\n";
     } else {
         print STDERR "Controller state not found";
         exit 1;
     }
-    print "\n";
+    print "$br$br\n";
 } else {
     print STDERR "Scheduler state not found";
@@ -116,5 +117,5 @@
 
 if ($request_run) {
-    print "<b>Parser Status:</b> $request_run->{nrun} running. $request_run->{ngood} parsed successfully.&nbsp; $request_run->{nfail} failed to parse.";
+    print "<br /><b>Parser Status:</b> $request_run->{nrun} running. $request_run->{ngood} parsed successfully.&nbsp; $request_run->{nfail} failed to parse.";
     if ($request_fin) {
         print "  $request_fin->{ngood} Requests finished.";
@@ -125,9 +126,18 @@
     print "\n";
     if ($job_run) {
-        print "<b>Job Status:</b> &nbsp;&nbsp; $job_run->{nrun} running. &nbsp; $job_run->{ngood} completed successfully. &nbsp; $job_run->{nfail} failed";
-        print "\n";
+        print "<br /><b>Job Status:</b> &nbsp;&nbsp; $job_run->{nrun} running. &nbsp; $job_run->{ngood} completed successfully. &nbsp; $job_run->{nfail} failed";
+        print "<br />\n";
     } else {
-        print "Task pstamp.job.run not found.\n";
+        print "Task pstamp.job.run not found.<br />\n";
     }
+    print "<br /><b>Unfinished Requests</b><br />\n";
+    print "<pre>\n";
+    system "/home/panstarrs/bills/ipp/tools/psstatus -r ";
+    print "</pre>\n";
+    print "<br /><b>Requests completed in last 24 hours (most recently completed first)</b><br />\n";
+    print "<pre>\n";
+    # finished requests
+    system "/home/panstarrs/bills/ipp/tools/psstatus -f";
+    print "</pre>\n";
 } else {
     print "Task pstamp.request.run not found.\n";
@@ -135,3 +145,5 @@
 
 
+
+
 exit 0;
Index: /tags/ipp-20100823/pstamp/scripts/pstamp_webrequest.pl
===================================================================
--- /tags/ipp-20100823/pstamp/scripts/pstamp_webrequest.pl	(revision 29332)
+++ /tags/ipp-20100823/pstamp/scripts/pstamp_webrequest.pl	(revision 29333)
@@ -4,5 +4,5 @@
 # pstampwebrequest.pl: take a postage stamp request command line and process it
 #
-# The arguments are the command line parameters for the program pstamprequest
+# The arguments are the command line parameters for the program psmkreq
 #
 # Unless the argument -list is provided the output is the request id for the resulting request
@@ -33,5 +33,5 @@
     'dbname=s'      =>  \$dbname,
     'dbserver=s'    =>  \$dbserver,
-    'project=s'     => \$project,
+#    'project=s'     => \$project,
     'verbose'       => \$verbose,
 );
@@ -70,8 +70,7 @@
 my $missing_tools;
 
-my $pstamprequest = can_run('pstamprequest')  or (warn "Can't find pstamprequest"  and $missing_tools = 1);
+my $psmkreq = can_run('psmkreq')  or (warn "Can't find psmkreq"  and $missing_tools = 1);
 my $pstamptool = can_run('pstamptool')  or (warn "Can't find pstamptool"  and $missing_tools = 1);
 my $pstampparse = can_run('pstampparse.pl')  or (warn "Can't find pstampparse.pl"  and $missing_tools = 1);
-my $pstampparser_run = can_run('pstamp_parser_run.pl')  or (warn "Can't find pstamp_parser_run.pl"  and $missing_tools = 1);
 
 if ($missing_tools) {
@@ -96,6 +95,5 @@
 my $request_file = "$datedir/$request_name.fits";
 {
-    my $command = "$pstamprequest -req_name $request_name -project $project $request_file @ARGV";
-    $command .= " -$job_type" if $job_type;     # default job_type is pstamp
+    my $command = "$psmkreq --req_name $request_name  --output $request_file @ARGV";
 
     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
@@ -103,5 +101,5 @@
     unless ($success) {
         print STDERR @$stderr_buf;
-        die("Unable to perform pstamprequest: $error_code");
+        die("Unable to perform psmkreq: $error_code");
     }
 }
Index: /tags/ipp-20100823/pstamp/scripts/pstampparse.pl
===================================================================
--- /tags/ipp-20100823/pstamp/scripts/pstampparse.pl	(revision 29332)
+++ /tags/ipp-20100823/pstamp/scripts/pstampparse.pl	(revision 29333)
@@ -806,8 +806,15 @@
     my ($r_jobState, $r_fault, $r_dep_id, $imagedb, $state, $stage, $stage_id, $component, $need_magic) = @_;
 
-    # XXX: The update process for warp and subsequent stages requires # destreaking to be performed
-    # because the -pending queries require the inputs to have magicked >= 0
-    # The case of stack-stack diffs not needing to be destreaked is taken care of in pstamp_checkdependent
-    $need_magic = 1 if $imagedb eq 'gpc1';
+    if (($stage ne 'chip') and ($stage ne 'raw')) {
+        # XXX: The update process for warp and subsequent stages requires that destreaking to be performed
+        # because the -pending queries require the inputs to have magicked >= 0
+        # This is too conservative. For example stage is warp and the warp has never been destreaked we don't
+        # need magic either
+        # The case of stack-stack diffs not needing to be destreaked is taken care of in pstamp_checkdependent
+        # maybe we should put all of the smarts there and interpret the need_magic flag in the dependent as
+        # "The client needs to have magic performed" and let
+        # XXX: note the very nasty hard coding of gpc1
+        $need_magic = 1 if $imagedb eq 'gpc1';
+    }
 
     # chipRun's can be in full state if destreaking is necessary
@@ -900,5 +907,5 @@
                     $$r_newState = 'stop';
                     $$r_fault = $PSTAMP_NOT_AVAILABLE;
-                } elsif (!$image->{magicked}) {
+                } elsif ($need_magic and !$image->{magicked}) {
                     $$r_newState = 'stop';
                     $$r_fault = $PSTAMP_NOT_DESTREAKED;
Index: gs/ipp-20100823/pstamp/scripts/pstamprequest
===================================================================
--- /tags/ipp-20100823/pstamp/scripts/pstamprequest	(revision 29332)
+++ 	(revision )
@@ -1,374 +1,0 @@
-#!/bin/env perl
-###
-### pstamprequest
-###
-###     Program to make a postage stamp request table for a set of coordinates
-###
-
-use warnings;
-use strict;
-
-use Getopt::Long qw( GetOptions ) ; # :config auto_help auto_version gnu_getopt );
-use Pod::Usage qw( pod2usage );
-use PS::IPP::Config qw( :standard );
-use PS::IPP::PStamp::RequestFile qw( :standard );
-use PS::IPP::PStamp::Job qw( :standard );
-use File::Temp qw(tempfile);
-use File::Basename qw(basename);
-use IPC::Cmd 0.36 qw( can_run run );
-use Carp;
-use POSIX;
-
-my $verbose;
-my $save_temps;
-
-# list file columns
-# RA DEC FILTER MJD_MIN MJD_MAX
-
-my ($ra, $dec, $x, $y, $list, $output, $req_name, $req_name_base);
-
-my ($image, $mask, $variance, $cmf, $psf, $backmdl, $inverse);
-my ($unconvolved, $use_imfile_id, $no_wait);
-
-my $default_size = 100;
-my $job_type     = 'stamp';
-my $req_type     = 'bycoord';
-my $stage        = 'chip';
-my $option_mask;
-my $width        = $default_size;
-my $height       = $default_size;
-my $project      = 'gpc1';
-my $coord_mask;
-my $pixcenter;
-my $arcseconds;
-
-my $id;
-my $tess_id = 'null';
-my $component = 'null';
-my $data_group = 'null';
-my $filter;
-my $mjd_min = 0;
-my $mjd_max = 0;
-my $comment;
-
-my $missing_tools;
-my $pstamp_request_file  = can_run('pstamp_request_file')  or (warn "Can't find required program pstamp_request_file"  and $missing_tools = 1);
-
-if ($missing_tools) {
-    warn("Can't find required tools.");
-    exit ($PS_EXIT_CONFIG_ERROR);
-}
-
-GetOptions(
-    'list=s'            => \$list,          # list of coordinates if undef ra and dec or x and y are required
-    'ra=s'              => \$ra,             # 
-    'dec=s'             => \$dec,
-    'x=s'               => \$x,
-    'y=s'               => \$y,
-    'width=i'           => \$width,
-    'height=i'          => \$height,
-    'pixcenter'         => \$pixcenter,
-    'arcseconds'        => \$arcseconds,
-    'coord_mask=i'      => \$coord_mask,
-    'job_type=s'        => \$job_type,
-    'output=s'          => \$output,
-    'req_name=s'        => \$req_name,
-    'req_name_base=s'   => \$req_name_base,
-    'stage=s'           => \$stage,
-    'project=s'         => \$project,
-    'req_type=s'        => \$req_type,
-    'id=s'              => \$id,
-    'tess_id=s'         => \$tess_id,
-    'component=s'       => \$component,
-    'data_group=s'      => \$data_group,
-    'filter=s'          => \$filter,
-    'mjd_min=s'         => \$mjd_min,
-    'mjd_max=s'         => \$mjd_max,
-    'comment=s'         => \$comment,
-
-    'option_mask=i'     => \$option_mask,
-    'image'             => \$image,
-    'mask'              => \$mask,
-    'variance'          => \$variance,
-    'cmf'               => \$cmf,
-    'psf'               => \$psf,
-    'backmdl'           => \$backmdl,
-    'inverse'           => \$inverse,
-    'unconvolved'       => \$unconvolved,
-    'use_imfile_id'     => \$use_imfile_id,
-    'do_not_wait'       => \$no_wait,
-
-    'verbose'           => \$verbose,
-    'save-temps'        => \$save_temps,
-) or pod2usage(2);
-
-pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
-
-pod2usage( -msg => "Required options: --req_name | --req_name_base --stage ") 
-    unless (defined $req_name or defined $req_name_base)
-       and defined $stage;
-
-pod2usage( -msg => "Invalid job_type: $job_type", -exitval =>1 )
-        unless ($job_type eq 'stamp' or $job_type eq 'get_image');
-
-if ($job_type eq 'stamp') {
-    if (defined $list) {
-        pod2usage( -msg => "--ra --dec --x --y are not used with --list", -exitval =>1 )
-            if defined $x or defined $y;
-    } elsif (!$pixcenter) {
-        pod2usage( -msg => "Required options for stamp requests: --ra and --dec or --list", -exitval =>1 )
-            unless (defined $ra and defined $dec);
-        # to simplify code we just use $x and $y from here
-        $x = $ra;
-        $y = $dec;
-    } else {
-        pod2usage( -msg => "Required options for stamp requests: --x and --y or --list", -exitval =>1 )
-                unless (defined $x and defined $y);
-    }
-} else {
-    pod2usage( -msg => "req_type must be byid or byexp for get_image requests",
-        -exitval =>1 ) unless $req_type eq 'byid' or $req_type eq 'byexp';
-    $x  = 0;
-    $y = 0;
-}
-
-if ($req_type eq 'byid') {
-    pod2usage( -msg => "ID required for get_image requests", -exitval =>1 )
-            if !defined $id;
-    die("ID must be number for byid requests") if !validID($id);
-} elsif ($req_type eq 'byexp') {
-    pod2usage( -msg => "ID (exp_name) required for byexp requests", -exitval =>1 )
-            if !defined $id;
-} elsif ($req_type eq 'bydiff') {
-    pod2usage( -msg => "ID (exp_name) required for bydiff requests", -exitval =>1 )
-            if !defined $id;
-} elsif ($req_type eq 'byskycell') {
-    pod2usage( -msg => "tess_id and skycell_id required for byskycell requests", -exitval =>1 )
-            if !(defined $tess_id and defined $component);
-} elsif ($req_type ne 'bycoord') {
-    pod2usage( -msg => "$req_type is not a valid req_type", -exitval =>1 );
-}
-
-$id = 0 if !$id;
-
-unless ($stage eq 'raw' or $stage eq 'chip' or $stage eq 'warp' or $stage eq 'diff' or $stage eq 'stack') {
-    die "$stage is not a valid value for stage\n";
-}
-
-checkFilter($filter, 'null', $filter)  if $filter;
-checkMJD($mjd_min, 0, "") if $mjd_min;
-checkMJD($mjd_max, 0, "" ) if $mjd_min;
-
-# user supplied option mask takes precedence
-if (!defined $option_mask) {
-    $option_mask = 0;
-    if ($job_type eq 'stamp') {
-        $option_mask |= $PSTAMP_SELECT_IMAGE    if $image;
-        $option_mask |= $PSTAMP_SELECT_MASK     if $mask;
-        $option_mask |= $PSTAMP_SELECT_VARIANCE if $variance;
-        $option_mask = $PSTAMP_SELECT_IMAGE    if $option_mask == 0;
-
-        # if no image was requested make a stamp of the image
-
-        $option_mask |= $PSTAMP_SELECT_CMF      if $cmf;
-        $option_mask |= $PSTAMP_SELECT_PSF      if $psf;
-        $option_mask |= $PSTAMP_SELECT_BACKMDL  if $backmdl;
-        $option_mask |= $PSTAMP_SELECT_UNCONV   if $unconvolved;
-        $option_mask |= $PSTAMP_USE_IMFILE_ID   if $use_imfile_id;
-        $option_mask |= $PSTAMP_SELECT_INVERSE  if $inverse;
-    }
-    $option_mask |= $PSTAMP_NO_WAIT_FOR_UPDATE if $no_wait;
-}
-
-
-# user supplied coord_mask takes precedence
-if (!defined $coord_mask) {
-    $coord_mask = 0;
-    if ($pixcenter) {
-        $coord_mask |= $PSTAMP_CENTER_IN_PIXELS;
-    }
-    if (!$arcseconds) {
-        $coord_mask |= $PSTAMP_RANGE_IN_PIXELS;
-    }
-}
-
-
-# if req_name is not supplied use the current date and time to build one off of the base
-if (!$req_name) {
-    my $datestr = strftime "%Y%m%dT%H%M%S", gmtime;
-    $req_name .= $req_name_base . $datestr;
-}
-
-# ok ready to go
-
-my $rows;
-if ($list) {
-    $rows = readList($list);
-} else {
-    $rows = [];
-    push @$rows, buildRow("", $comment, $x, $y, $filter, $mjd_min, $mjd_max);
-}
-
-my ($tdf, $table_def_name) = tempfile ("/tmp/tabledef.XXXX", UNLINK => !$save_temps);
-print $tdf "$req_name 1\n";
-my $rownum = 0;
-foreach my $row (@$rows) {
-    $rownum++;
-    my $line = "$rownum $row->{ra}\t$row->{dec}\t$width $height"
-        . " $coord_mask $job_type $option_mask $project $req_type"
-        . " $stage $id $tess_id $component $data_group"
-        . " $row->{filter} $row->{mjd_min} $row->{mjd_max}";
-
-    if ($row->{comment} and $row->{comment} ne '') {
-        $line .= " |$row->{comment}" ;
-    } else {
-        $line .= " |$comment" if $comment;
-    }
-
-    print "$line\n" if $verbose;
-    print $tdf "$line\n";
-}
-close $tdf;
-
-{
-    my $command = "$pstamp_request_file --input $table_def_name --req_name $req_name";
-    $command .= " --output $output" if $output;
-    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
-        run(command => $command, verbose => 0);
-    unless ($success) {
-        print STDERR @$stderr_buf;
-        exit $error_code >> 8;
-    }
-}
-
-exit 0;
-
-sub readList {
-    my $file = shift;
-    open IN, "<$file" or die "failed to open $file for input\n";
-
-    my @rows;
-
-    my $linenumber = 0;
-    foreach my $line (<IN>) {
-        $linenumber++;
-        chomp $line;
-        next if !$line;
-        next if ($line =~ /^#/);
-
-        my ($spec, $comment) = split /\|/, $line;
-        $comment = '' if !defined $comment;
-
-        my @vals = split " ", $spec;
-        push @rows, buildRow("at line number $linenumber", $comment, @vals);
-    }
-    close IN;
-
-    return \@rows;
-}
-
-sub buildRow {
-    my $linenumber = shift;
-    my $comment = shift;
-    my @vals = @_;
-
-    my $row = {};
-    $row->{ra}      = checkRA($vals[0], $linenumber);
-    $row->{dec}     = checkDEC($vals[1], $linenumber);
-    $row->{filter}  = checkFilter($vals[2], $filter, $linenumber);
-    $row->{mjd_min} = checkMJD($vals[3], $mjd_min, $linenumber);;
-    $row->{mjd_max} = checkMJD($vals[4], $mjd_max, $linenumber);;
-    $row->{comment} = $comment;
-
-    return $row;
-}
-
-sub validNumber {
-    my $val = shift;
-
-    return 0 if !defined $val;
-
-    return ($val =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/);
-}
-
-sub validID
-{
-    my $val = shift;
-
-    return 0 if !$val;
-
-    return  ! ($val =~ /\D/);
-}
-
-sub checkCoord {
-    my $c          = shift;
-    my $linenumber = shift;
-
-    my $result;
-    if ($c =~ /\:/) {
-        # sexagesmial format not valid for pixel coordinates
-        die "invalid coordinate value $c found $linenumber\n" if $pixcenter;
-
-        my ($d, $m, $s) = split '\:', $c;
-        die "invalid coordinate value $c found $linenumber\n" 
-            unless validNumber($d) and validNumber($m) and validNumber($s);
-        my $sign;
-        if ($c =~ /^-/) {   # checking $d < 0 doesn't work for $d = -0
-            $sign = -1;
-            $d = -$d;
-        } else {
-            $sign = 1;
-        }
-        $result = $sign * ($d + ($m + $s/60.) /60.);
-    } else {
-        die "$c is not  valid coordinate value $linenumber\n" 
-            unless validNumber($c);
-        $result = $c;
-    }
-    return $result;
-}
-
-sub checkRA {
-    my $ra = shift;
-    my $linenumber = shift;
-    my $checked = checkCoord($ra, $linenumber);
-
-    if ($ra =~ /\:/) {
-        return $checked * 360. / 24.;
-    } else {
-        return $checked;
-    }
-}
-
-sub checkDEC {
-    return checkCoord(@_);
-}
-
-sub checkFilter {
-    my $f = shift;
-    my $default = shift;
-    my $linenumber = shift;
-
-    $default = 'null' if !defined $default;
-
-    $f = $default if (!defined$f or $f eq 'null');
-
-    return $f;
-}
-
-sub checkMJD {
-    my $mjd = shift;
-    my $default = shift;
-    my $linenumber = shift;;
-
-    return $default if (!$mjd) ;
-
-    die "invalid mjd value '$mjd' found $linenumber\n" if !validNumber($mjd);
-
-    return $mjd;
-}
-
-sub checkStage {
-    my $stage = shift;
-
-}
Index: /tags/ipp-20100823/pstamp/scripts/pstampstopfaulted
===================================================================
--- /tags/ipp-20100823/pstamp/scripts/pstampstopfaulted	(revision 29333)
+++ /tags/ipp-20100823/pstamp/scripts/pstampstopfaulted	(revision 29333)
@@ -0,0 +1,15 @@
+
+# script executed by the task pstamp.stopfaulted to stop proceessing of jobs
+# and dependents that have faulted 5 or more times
+# all arguments to this script are passed to pstamptool
+
+# 25 is PSTAMP_NOT_AVAILABLE
+fault_code=25
+fault_count=5
+
+date
+echo stopping faulted dependent jobs
+pstamptool -stopdependentjob -set_fault $fault_code -fault_count $fault_count $*
+
+echo stopping faulted jobs
+pstamptool -updatejob -state run -set_state stop -set_fault $fault_code -fault_count $fault_count $*
Index: /tags/ipp-20100823/pstamp/web/request.php
===================================================================
--- /tags/ipp-20100823/pstamp/web/request.php	(revision 29332)
+++ /tags/ipp-20100823/pstamp/web/request.php	(revision 29333)
@@ -16,19 +16,6 @@
 // XXX This is just a prototype for testing purposes. 
 
-
-// XXX: change to include pstampconfig.php
-
-// BEGIN Local configuration
-
-$WORKDIR = "/data/ipp049.0/pstamp/work";
-$dsroot = "/data/ipp049.0/datastore/dsroot";
-$dbname = "ippRequestServer";
-$dbserver = "ipp049";
-
-$PSCONFDIR = "/data/ipp053.0/home/bills/psconfig";
-$PSCONFIG  = "debug";
-$PSBINDIR  = "$PSCONFDIR/$PSCONFIG.lin64/bin";
-
-// END Local configuration 
+require "pstamp.php";
+require "submitted.php";
 
 # this script sets up the environment to run IPP commands with current directory
@@ -55,4 +42,5 @@
 $diff_checked = "";
 $list_checked = "";
+$getstatus_checked = "";
 $pstamp_checked = "";
 $get_checked = "";
@@ -102,5 +90,5 @@
 if ($rvar_project == "gpc1") {
     $gpc1_selected = "selected";
-    $require_class_id = 1;
+//    $require_class_id = 1;
 } else if ($rvar_project == "megacam-mops") {
     $mops_selected = "selected";
@@ -122,6 +110,6 @@
     $diff_checked = "CHECKED";
 } else {
-    // nothing checked default to By Exposure
-    $exp_checked = "CHECKED";
+    // nothing checked default to By ID
+    $file_checked = "CHECKED";
 }
 
@@ -140,10 +128,10 @@
 
 // is the center is specified in Pixels or sky coordinates
-if ($rvar_center_type == "Sky") {
+if ($rvar_center_type == "Pixels") {
+    $sky_checked="";
+    $pix_checked="checked";
+} else {
     $sky_checked="checked";
     $pix_checked="";
-} else {
-    $sky_checked="";
-    $pix_checked="checked";
 }
 
@@ -168,16 +156,17 @@
 }
 
+$getstatus_checked = "";
+$pstamp_checked = "";
+$list_checked = "";
+$get_checked = "";
 if ($rvar_cmd_mode == "Make Stamps") {
     $pstamp_checked = "checked";
-    $list_checked = "";
-    $get_checked = "";
 } else if ($rvar_cmd_mode == "Get Images") {
     $get_checked = "checked";
-    $list_checked = "";
-    $pstamp_checked = "";
+} else if ($rvar_cmd_mode == "Get Status") {
+    $getstatus_checked = "checked";
 } else {
-    $list_checked = "checked";
-    $get_checked = "";
-    $pstamp_checked = "";
+    // default
+    $pstamp_checked = "checked";
 }
 
@@ -188,11 +177,10 @@
 
 // How do we know whether or not this is the intial page load or not?
-// Well, in that case rvar_img_type is not set so key off of that
+// Well, the first time rvar_img_type is not set. So we key off of that.
 // TODO: find a better way to decide whether or not to run commands
 
 if ($rvar_img_type) {
-
     $jobFinished = 0;
-    if ($request_id == 0) {
+    if (! $getstatus_checked) {
         try {
             $command_line = build_request_cmd();
@@ -201,19 +189,19 @@
             if (! $list_checked) {
                 // The only output from a successful run is the request_id
+                $request_name = trim(Array_pop($output_array));
                 $request_id = Array_pop($output_array);
                 $last_request_id = $request_id;
-                setcookie("our_request_id", $request_id);
-                // echo "The request id is $request_id\n";
-                if (count($output_array) != 0) {
-                    throw new Exception("unexpected output returned by pstampwebrequest.");
+                if ($request_id && $request_name) {
+                    addRequest($request_id, $request_name);
+                    // setcookie("our_request_id", $request_id);
+                    // echo "The request id is $request_id\n";
+                    $getstatus_checked = "checked";
+                    $pstamp_checked = "";
+                } else {
+                    // XXX: TODO print out the error
+                    if (count($output_array) != 0) {
+                        throw new Exception("unexpected output returned by pstampwebrequest.");
+                    }
                 }
-
-                $jobRunning = getRequestStatus();
-                if (!$jobRunning) {
-                  //   echo "1  getRequestStatus reuturned 0\n";
-                    $jobFinished = 1;
-                    $request_id = 0;
-                }
-
             } else {
                 $last_request_id = 0;
@@ -222,26 +210,8 @@
             $error_line = $e->getMessage();
         }
-    } else {
-        try {
-            // get the list of jobs for the request
-            // echo "calling getRequestStatus\n";
-            $jobRunning = getRequestStatus();
-
-            if (!$jobRunning) {
-               //  echo "2  getRequestStatus reuturned 0\n";
-                $jobFinished = 1;
-                $request_id = 0;
-            }
-
-        } catch (Exception $e) {
-            echo "Got Exception $request_id $e\n";
-            $error_line = $e->getMessage();
-        }
-    }
-    if ($last_request_id) {
-        // This doesn't work for get_image
-        listJobs($last_request_id, $jobFinished);
-    }
-}
+    }
+}
+
+// This is the end of the Logic
 
 function build_request_cmd()
@@ -296,10 +266,10 @@
                 throw new Exception('RA and DEC must be specified.');
             }
-            $cmd .= " -skycenter $rvar_RA $rvar_DEC";
+            $cmd .= " --ra $rvar_RA --dec $rvar_DEC";
         } else {
             if (! $rvar_X || ! $rvar_Y) {
                 throw new Exception('X and Y must be specified.');
             }
-            $cmd .= " -pixcenter $rvar_X $rvar_Y";
+            $cmd .= " -pixcenter --x $rvar_X --y $rvar_Y";
         }
 
@@ -308,10 +278,10 @@
                 throw new Exception('dRA and dDEC must be specified.');
             }
-            $cmd .= " -arcrange $rvar_dRA $rvar_dDEC";
+            $cmd .= " --arcseconds --width $rvar_dRA --height $rvar_dDEC";
         } else {
             if (! $rvar_W || ! $rvar_H) {
                 throw new Exception('width and height must be specified.');
             }
-            $cmd .= " -pixrange $rvar_W $rvar_H";
+            $cmd .= " --width $rvar_W --height $rvar_H";
         }
     }
@@ -324,35 +294,37 @@
 
     if ($exp_checked) {
+        if ($rvar_img_type == "stack") {
+            throw new Exception('Lookup by exposure name not supported for stack images.');
+        }
         if (! $rvar_id ) {
             throw new Exception('Must set ID to the Exposure ID.');
         }
-        $cmd .= " -byexp $rvar_img_type $rvar_id";
+        $cmd .= " --req_type byexp --stage $rvar_img_type --id $rvar_id";
     } else if ($file_checked) {
         if (! $rvar_id ) {
             throw new Exception('Must set ID to the exposure name.');
         }
-        $cmd .= " -byid $rvar_img_type $rvar_id";
+        $cmd .= " --req_type byid --stage $rvar_img_type --id $rvar_id";
     } else if ($coord_checked) {
-        // $cmd .= " -bycoord $rvar_img_type";
-        $coord_checked = "";
-        $exp_checked = "checked";
-        throw new Exception("Image selection by coordinate not implemented yet.");
+        $cmd .= " --req_type bycoord --stage $rvar_img_type";
+        $coord_checked = "checked";
+//        throw new Exception("Image selection by coordinate not implemented yet.");
     } else if ($diff_checked) {
         if (! $rvar_id ) {
             throw new Exception('Must set ID to Diff Image ID.');
         }
-        $cmd .= " -bydiff $rvar_img_type $rvar_id";
-    }
-
-    if (($rvar_img_type == "raw") || ($rvar_img_type == "chip")) {
-        if ($require_class_id && ! $rvar_class_id ) {
-            throw new Exception("must specify Class ID with Image Type $rvar_img_type.");
-        }
-        if ((!$rvar_class_id) || ($rvar_class_id == "all")) {
-            $cmd .= " null";
-        } else {
-            $cmd .= " $rvar_class_id";
-        }
-    }
+        $cmd .= " --req_type bydiff --stage $rvar_img_type --id $rvar_id";
+    }
+
+// XXX: don't need to require class_id anymore
+//    if (($rvar_img_type == "raw") || ($rvar_img_type == "chip")) {
+//        if (!$sky_checked && ($require_class_id && ! $rvar_class_id )) {
+//            throw new Exception("must specify Class ID with Image Type $rvar_img_type.");
+//        }
+        // leave off compoennt if we're looking up by coordinates. It breaks it
+        if (!$coord_checked && (($rvar_class_id) && ($rvar_class_id != "all"))) {
+            $cmd .= " --component $rvar_class_id";
+        }
+//    }
 
     return escapeshellcmd($cmd);
@@ -390,4 +362,5 @@
 }
 
+// This is no longer used
 function printURL($line)
 {
@@ -442,119 +415,25 @@
     echo "</td></tr>";
 }
-
-function countRunningJobs()
-{
-    global $output_array;
-
-    $runningJobs = 0;
-    $size = sizeof($output_array);
-    for ($i = 0; $i < $size; $i++) {
-        $elements = explode(" ", $output_array[$i]);
-        if (count($elements) == 3) {
-            $state    = $elements[1];
-            if ($state != "stop") {
-                $runningJobs++;
-            }
-        } else {
-            throw new Exception ("incorrect data in job status: $output_array[$i]");
-        }
-    }
-    return $runningJobs;
-}
-
-function listJobs($request_id, $jobFinished)
-{
-    global $WORKDIR;
-    global $SCRIPT;
-
-    $command_line = "$SCRIPT pstamp_listjobs.pl $request_id";
-    global $dbname;
-    global $dbserver;
-    if ($dbname) {
-        $command_line .= " --dbname $dbname --dbserver $dbserver";
-    }
-
-    run_command($command_line);
-    if ($jobFinished) {
-        global $outFileset;
-        global $dsroot;
-        $parse_error = "$WORKDIR/$request_id/parse_error.txt";
-        #echo "reading $parse_error\n";
-        // readfile( $parse_error );
-        if (file_exists($parse_error)) {
-            $fhandle = fopen($parse_error, "r");
-            if ($fhandle) {
-                $contents = fread($fhandle, 1024);
-                if ($contents) {
-                    global $last_request_id;
-                    global $error_line;
-                    $error_line = "Request $last_request_id: $contents\n";
-                }
-            }
-        }
-    }
-}
-
-function getRequestStatus()
-{
-    global $request_id;
-    global $command_status;
-    global $output_array;
-    global $outFileset;
-    global $dbname;
-    global $dbserver;
-    global $SCRIPT;
-
-    $command_line = "$SCRIPT pstamptool -listreq -req_id $request_id -simple";
-    if ($dbname) {
-        $command_line .= " -dbname $dbname -dbserver $dbserver";
-    }
-    // echo "Running $command_line\n";
-
-    run_command($command_line);
-    if ($command_status == 0) {
-        $size = sizeof($output_array);
-        $runningReq = 0;
-        for ($i = 0; $i < $size; $i++) {
-            $elements = explode(" ", $output_array[$i]);
-            if (count($elements) >= 4) {
-                $state = $elements[2];
-                $outFileset = $elements[4];
-                if ($state != "stop") {
-                    $runningReq++;
-                }
-            } else {
-                throw new Exception ("incorrect data in job status: $output_array[$i]");
-            }
-        }
-        return $runningReq;
-    } else {
-        return 0;
-    }
-}
-
 ?>
 
-<!-- Beginning of the HTML --------------------------------------------- -->
+<!----------------------Beginning of the HTML --------------------------------------------- -->
+
 <html>
- <head>
-  <title>Postage Stamp Request Form</title>
-    <?php
-        if ($request_id != 0) {
-            // This doesn't do what I want. It does a get not a post
-
-            // echo '<META HTTP-EQUIV="refresh" CONTENT="5">';
-
-        }
-    ?>
- </head>
+<head>
+  <title>
+    Postage Stamp Request Form (prototype)
+  </title>
+</head>
+<body>
+
+<H1 align=center>
+Postage Stamp Request Form
+</h1>
+
+<?php
+    welcomeHeader($auth_user, "pstamp_links.php", "Postage Stamp Home");
+?>
+
 <form method="post">
-
-<body>
-
-<H1 align=center>
-Postage Stamp Request
-</h1>
-
 <!-- Whole page is a single column table -->
 
@@ -586,8 +465,9 @@
 <td>
 &nbsp;<b>Select Images By:</b>&nbsp;&nbsp;&nbsp;
+<input type=radio name="select_by" value="db_id" <?php echo $file_checked; ?> >Database ID
+&nbsp;
 <input type=radio name="select_by" value="exposure_id" <?php echo $exp_checked; ?> >Exposure Name
 &nbsp;
-<input type=radio name="select_by" value="db_id" <?php echo $file_checked; ?> >Database ID
-&nbsp;
+
 <input type=radio name="select_by" value="coord" <?php echo $coord_checked; ?> >Coordinates
 &nbsp;
@@ -625,5 +505,5 @@
         echo "Chip ID:";
       } else {
-        echo "Class ID:";
+        echo "Component:";
       }
 ?>
@@ -685,4 +565,5 @@
             &nbsp;
             <input type="text" name="dRA" size=10  value= <?php echo $rvar_dRA; ?> >
+            &nbsp; "
         </td>
         <td>
@@ -691,4 +572,5 @@
             &nbsp;
             <input type="text" name="dDEC" size=10 value="<?php echo $rvar_dDEC;?>" >
+            &nbsp; "
         </td>
     </tr>
@@ -742,19 +624,16 @@
   <tr>
 
-<?php
-  if ($request_id == 0): ?>
     <td><input type=submit value="Submit"></td>
     <td><b>Mode:</b>&nbsp;&nbsp;
+    <input type=radio name="cmd_mode" value="Get Status"<?php echo $getstatus_checked; ?> >Get Status
     <input type=radio name="cmd_mode" value="Make Stamps"<?php echo $pstamp_checked; ?> >Make Stamps
-    <input type=radio name="cmd_mode" value="Get Images" <?php echo $get_checked; ?> >Get Images
+    <input type=radio name="cmd_mode" value="Get Images" <?php echo $get_checked; ?> >Get Bundles
+<!--
     <input type=radio name="cmd_mode" value="List Images" <?php echo $list_checked; ?> >List Images
+-->
     </td>
-<?php 
-  else: ?>
-    <td><input type=submit value="Check Status"></td>
-<!---    <td><input type=submit value="Cancel"></td> --->
 <?php
-  echo "<td><b>Request Id: $request_id";
-  endif; ?>
+  // echo "<td><b>Request Id: $request_id";
+?>
 
   </tr>
@@ -766,6 +645,14 @@
 
 <tr>
+<!--- Don't show the command
     <td>
     <b>Command:</b>&nbsp;&nbsp; <?php echo "$command_line\n";?>
+    </td>
+--->
+
+</tr>
+<tr>
+    <td>
+    <b>Last Command</b>
     </td>
 </tr>
@@ -794,8 +681,11 @@
 <td>
 <table align=center width=100% rules=none>
-<caption height=10 valign=center><b>Results</b></caption>
-
+<caption height=10 valign=center><b>Request Results</b></caption>
 
 <?php 
+if (0) {
+    // This is the old way of listing the status of the current request.
+    // now we save the submitted requests in the session see listRequests() below
+
     $size = sizeof($output_array);
     // echo "<pre>size of output array is $size\n</pre>";
@@ -818,4 +708,5 @@
         }
     }
+} // end if if(0)
 ?>
 </table>
@@ -845,4 +736,8 @@
 </table>
 
+<?php
+    listRequests("http://datastore.ipp.ifa.hawaii.edu/pstampresults", "pstamp_results_fileset");
+?>
+
 <!-- The end -->
 
@@ -861,6 +756,5 @@
 ?>
 
+</form>
 </body>
-</form>
-
 </html>
