IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 27859


Ignore:
Timestamp:
May 4, 2010, 4:10:59 PM (16 years ago)
Author:
watersc1
Message:

Fix typos in pstamptool database queries.
Add pstampDependent.outdir entry to changes.txt.

Final tested code for detectability server.
Works on test cases that involve updates.

r

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/dbconfig/changes.txt

    r27856 r27859  
    15921592ALTER TABLE pstampDependent CHANGE COLUMN no_magic need_magic TINYINT;
    15931593ALTER TABLE pstampDependent ADD COLUMN fault SMALLINT after need_magic;
     1594ALTER TABLE pstampDependent ADD COLUMN outdir VARCHAR(255) after imagedb;
    15941595ALTER TABLE pstampDependent ADD KEY (state);
    15951596ALTER TABLE pstampDependent ADD KEY (stage);
  • trunk/ippTools/share/pstamptool_completedreq.sql

    r27856 r27859  
    11SELECT pstampRequest.* ,
    2     IFNULL(labels.priority, 0) AS priority
     2    IFNULL(Label.priority, 0) AS priority
    33FROM pstampRequest
    4     LEFT JOIN labels USING(label)
     4    LEFT JOIN Label USING(label)
    55WHERE state = 'run'
    66    AND (
  • trunk/ippTools/share/pstamptool_pendingdependent.sql

    r27856 r27859  
    44JOIN pstampJob USING(dep_id)
    55JOIN pstampRequest USING(req_id)
    6 JOIN Label ON pstampRequest.label = Label.label
     6LEFT JOIN Label ON pstampRequest.label = Label.label
    77WHERE pstampDependent.state = 'new'
    88    AND pstampDependent.fault = 0
  • trunk/pstamp/scripts/Makefile.am

    r27704 r27859  
    2020        pstamp_checkdependent.pl \
    2121        request_finish.pl \
     22        detectability_respond.pl \
    2223        detect_query_read \
    2324        detect_response_create \
  • trunk/pstamp/scripts/detectability_respond.pl

    r27788 r27859  
    4141my $EXTVER = 1.0;
    4242my $EXTNAME = 'MOPS_DETECTABILITY_RESPONSE';
    43 my ($req_id,$req_name,$product,$need_magic,$missing_tools);
    44 my ($request_file,$output,$dbname,$verbose,$save_temps);
     43my ($req_id,$req_name,$product,$need_magic,$missing_tools,$project);
     44my ($request_file,$output,$workdir,$dbname,$dbserver,$verbose,$save_temps);
    4545GetOptions(
    46     'input=s'          =>     \$request_file,
     46    'input=s'         =>      \$request_file,
    4747    'output=s'        =>      \$output,
     48    'workdir=s'       =>      \$workdir,
    4849    'dbname=s'        =>      \$dbname,
     50    'dbserver=s'      =>      \$dbserver,
    4951    'verbose'         =>      \$verbose,
    5052    'save-temps'      =>      \$save_temps,
     
    5557           -exitval => 3,
    5658    ) unless
    57     defined $request_file and defined $output and defined $dbname;
     59    defined $request_file and defined $output and defined $workdir and defined $dbname;
    5860
    5961my $detect_query_read = can_run('detect_query_read') or (warn "Can't find detect_query_read" and $missing_tools = 1);
     
    7173
    7274my $ipprc = PS::IPP::Config->new();
    73 #my $tmp_dir = "/data/${host}.0/tmp/";
    74 
    75 my $project = resolve_project($ipprc,"project_name",$dbname);
     75
     76if (!$dbserver) {
     77    $dbserver = metadataLookupStr($ipprc->{_siteConfig}, 'PS_DBSERVER');
     78}
     79# This is hardcoded in for the moment.
     80$project = resolve_project($ipprc,"gpc1",$dbname,$dbserver);
     81my $imagedb = $project->{dbname};
     82if (!$imagedb) {
     83    carp("failed to find imagedb for project: $project");
     84}
     85
    7686#
    7787# Parse input request file using detect_query_read (as it's already written).
    7888#
    79 
    8089my $dqr_command = "$detect_query_read --input $request_file";
    8190my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    8291    run(command => $dqr_command, verbose => $verbose);
    8392unless ($success) {
    84     my_die("Unable to perform $dqr_command error code: $error_code",-1,-1,-1,-1,-1,-1);
     93    # This is a problem, because I'm not sure how we handle a failure to read something.
     94    # We need to return a $PSTAMP_INVALID_REQUEST, I think, but if we can't read it,
     95    # we can't send that response back.
     96    die("Unable to perform $dqr_command error code: $error_code");
    8597}
    8698my %query = ();
     
    120132my %image_list_hash;
    121133for (my $i = 1; $i < $Nrows; $i++) {
    122 #    print "$i $Nrows $query{CONTENT}{RA1_DEG}[$i] $query{CONTENT}{DEC1_DEG}[$i]\n";
     134    # This could use the fact that locate_images now accepts position arrays, but
     135    # I'll save that for after I get the majority of things working.
    123136    my $image_set_tmp  = find_image_set($query{HEADER}{FPA_ID}[0],$query{HEADER}{STAGE}[0],
    124137                                        $query{HEADER}{MJD_OBS}[0],$query{HEADER}{FILTER}[0],
     
    126139                                        $query{CONTENT}{ROWNUM}[$i],$verbose);
    127140    unless (%$image_set_tmp) {
    128         my_die("No images were returned.",$query{HEADER}{QUERY_ID}[0],$query{HEADER}{FPA_ID}[0],
    129                $query{HEADER}{MJD_OBS}[0],$query{HEADER}{FILTER}[0],$query{HEADER}{OBSCODE}[0],
    130                $query{HEADER}{STAGE}[0]);
    131     }
    132     print "=== $image_set_tmp->{IMAGE}\n    $image_set_tmp->{PSF}\n    $image_set_tmp->{MASK}\n    $image_set_tmp->{WEIGHT}\n    $image_set_tmp->{SKY_COORDINATES}\n    $image_set_tmp->{ROWNUM}\n";
    133     # This appends, assuming that if we get an image, we also get the identical psf/mask/weight/etc.
     141        # No images were returned, so create a dummy entry that
     142        $image_list_hash{'no_image'}{IMAGE}    = 'no_image';
     143        $image_list_hash{'no_image'}{PSF}      = 'no_psf';
     144        $image_list_hash{'no_image'}{MASK}     = 'no_mask';
     145        $image_list_hash{'no_image'}{WEIGHT}   = 'no_weight';
     146        $image_list_hash{'no_image'}{CATALOG}  = 'no_catalog';
     147        $image_list_hash{'no_image'}{CLASS_ID} = 'no_class';
     148        $image_list_hash{'no_image'}{ERROR}    = $PSTAMP_NO_IMAGE_MATCH;
     149        push @{ $image_list_hash{'no_image'}{SKY_COORDINATES} }, "$query{CONTENT}{RA1_DEG}[$i] $query{CONTENT}{DEC1_DEG}[$i]";
     150        push @{ $image_list_hash{'no_image'}{ROWNUM} }, $query{CONTENT}{ROWNUM}[$i];
     151        next;
     152    }
     153#     print "=== $image_set_tmp->{IMAGE}\n    $image_set_tmp->{PSF}\n";
     154#     print "    $image_set_tmp->{MASK}\n    $image_set_tmp->{WEIGHT}\n";
     155#     print "    $image_set_tmp->{SKY_COORDINATES}\n    $image_set_tmp->{ROWNUM}\n";
     156
     157    # This indexes the results for identical images into the same hash.
    134158    $image_list_hash{$image_set_tmp->{IMAGE}}{IMAGE}    = $image_set_tmp->{IMAGE};
    135159    $image_list_hash{$image_set_tmp->{IMAGE}}{PSF}      = $image_set_tmp->{PSF};
     
    138162    $image_list_hash{$image_set_tmp->{IMAGE}}{CATALOG}  = $image_set_tmp->{CATALOG};
    139163    $image_list_hash{$image_set_tmp->{IMAGE}}{CLASS_ID} = $image_set_tmp->{CLASS_ID};
     164    $image_list_hash{$image_set_tmp->{IMAGE}}{ERROR}    = $image_set_tmp->{ERROR};
    140165    push @{ $image_list_hash{$image_set_tmp->{IMAGE}}{SKY_COORDINATES} }, $image_set_tmp->{SKY_COORDINATES};
    141166    push @{ $image_list_hash{$image_set_tmp->{IMAGE}}{ROWNUM} }, $image_set_tmp->{ROWNUM};
    142167}
     168
    143169my $i = 0;
    144 
    145 
    146170foreach my $k (keys %image_list_hash) {
     171    # If we errored out on finding an image, we need to not try to run psphot here.
     172    if ($image_list_hash{$k}{ERROR} != 0) {
     173        next;
     174    }
    147175    # Write coordinates of the requested targets to a file.
    148     my ($coordfile,$coordname) = tempfile("/tmp/detect.coords.$i.XXXX",
     176    my ($coordfile,$coordname) = tempfile("${workdir}/detect.coords.$i.XXXX",
    149177                                            UNLINK => !$save_temps);
    150     my ($targetfile,$targetname) = tempfile("/tmp/detect.targets.$i.XXXX",
     178    my ($targetfile,$targetname) = tempfile("${workdir}/detect.targets.$i.XXXX",
    151179                                            UNLINK => !$save_temps);
    152180
     
    154182        print $coordfile "$image_list_hash{$k}{SKY_COORDINATES}[$j]\n";
    155183    }
    156     print "$k\n";
     184#    print "$k\n";
    157185    # Convert the sky coordinates to image coordinates with ppCoord.
    158186    my $command = "ppCoord -astrom $image_list_hash{$k}{CATALOG} -radec $coordname";
     
    172200    }
    173201
    174     print "psphot $image_list_hash{$k}{PSF}\n";
     202#     print "psphot $image_list_hash{$k}{PSF}\n";
    175203    # Run psphotForced on the target list.
    176     my $tmpdir  = tempdir("detect.$i.XXXX", DIR => "/tmp/", CLEANUP => !$save_temps);
     204    my $tmpdir  = tempdir("detect.$i.XXXX", DIR => "${workdir}/", CLEANUP => !$save_temps);
    177205    $image_list_hash{$k}{OUTROOT} = "$tmpdir/detectability.$query{HEADER}{STAGE}[0].$query{HEADER}{FPA_ID}[0]";
    178206   
     
    186214        run(command => $psphot_cmd, verbose => $verbose);
    187215    unless ($success) {
    188         my_die("Unable to perform $psphot_cmd. Error_code: $error_code",
    189                $query{HEADER}{QUERY_ID}[0],$query{HEADER}{FPA_ID}[0],
    190                $query{HEADER}{MJD_OBS}[0],$query{HEADER}{FILTER}[0],$query{HEADER}{OBSCODE}[0],
    191                $query{HEADER}{STAGE}[0],$error_code);
     216        $image_list_hash{$k}{ERROR} = $PSTAMP_SYSTEM_ERROR;
    192217    }
    193218}
     
    197222#
    198223my @rownums = ();
     224my @out_errors = ();
    199225my @psphot_Npix = ();
    200226my @psphot_Qfact= ();
    201227my @psphot_flux = ();
     228
    202229foreach my $k (keys %image_list_hash) {
    203     my $cmf = "$image_list_hash{$k}{OUTROOT}.$image_list_hash{$k}{CLASS_ID}.cmf";
    204    
    205     my ($tmp_Npix,$tmp_Qfact,$tmp_flux) = read_cmf_file($cmf,$image_list_hash{$k}{EXTENSION_BASE});
    206 
    207     push @rownums,        @{ $image_list_hash{$k}{ROWNUM} };
    208     push @psphot_Npix,    @{ $tmp_Npix };
    209     push @psphot_Qfact,   @{ $tmp_Qfact };
    210     push @psphot_flux,    @{ $tmp_flux };
     230    if ($image_list_hash{$k}{ERROR} == 0) {
     231        my $cmf = "$image_list_hash{$k}{OUTROOT}.$image_list_hash{$k}{CLASS_ID}.cmf";
     232       
     233        my ($tmp_Npix,$tmp_Qfact,$tmp_flux) = read_cmf_file($cmf,$image_list_hash{$k}{EXTENSION_BASE});
     234       
     235        push @rownums,        @{ $image_list_hash{$k}{ROWNUM} };
     236        push @out_errors,     (map { $image_list_hash{$k}{ERROR} }  @{ $image_list_hash{$k}{ROWNUM} });
     237        push @psphot_Npix,    @{ $tmp_Npix };
     238        push @psphot_Qfact,   @{ $tmp_Qfact };
     239        push @psphot_flux,    @{ $tmp_flux };
     240    }
     241    else {
     242        push @rownums,        @{ $image_list_hash{$k}{ROWNUM} };
     243        push @out_errors,     (map { $image_list_hash{$k}{ERROR} }  @{ $image_list_hash{$k}{ROWNUM} });
     244        push @psphot_Npix,    (map { 0 }  @{ $image_list_hash{$k}{ROWNUM} });
     245        push @psphot_Qfact,   (map { 0.0 }  @{ $image_list_hash{$k}{ROWNUM} });
     246        push @psphot_flux,    (map { 0.0 }  @{ $image_list_hash{$k}{ROWNUM} });
     247    }   
    211248}
    212249
     
    215252                    $query{HEADER}{MJD_OBS}[0],$query{HEADER}{filter}[0],
    216253                    $query{HEADER}{obscode}[0],
    217                     \@rownums, \@psphot_Npix, \@psphot_Qfact, \@psphot_flux);
    218 print "Wrote response file $output\n";
     254                    \@rownums, \@out_errors, \@psphot_Npix, \@psphot_Qfact, \@psphot_flux);
     255# print "Wrote response file $output\n";
    219256#
    220257# Add to datastore
    221258#
    222 # my $finish_command = "dquery_finish.pl --req_id $req_id --req_name $req_name --product $product";
    223 # ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    224 #     run(command => $finish_command, verbose => $verbose);
    225 # unless ($success) {
    226 #     warn ("Unable to perform $finish_command error code: $error_code");
    227 # }
    228 
     259# Files are added to the datastore by dquery_finish.pl
    229260#
    230261# Cleanup
    231262#
    232263# Since everything is written to temporary files, there should be nothing to cleanup.
     264
    233265#
    234266# Utilities
     
    243275    my $index  = shift;
    244276    my $verbose = shift;
    245    
     277
     278    # This is the set of things that we need in order to run psphotForced
    246279    my $option_mask |= 1;
    247280    $option_mask |= $PSTAMP_SELECT_IMAGE;
     
    253286    my $mjd_max = $mjd + 1;
    254287
    255     # Construct a row list:
    256 
     288    # Construct a row list.
    257289    my @rowList;
    258290    $rowList[0]->{CENTER_X} = $ra;
     
    261293    $rowList[0]->{STAGE} = $stage;
    262294    $rowList[0]->{COORD_MASK} = 0;
    263     print "$stage\n";
     295
     296    #    print "$stage\n";
    264297    # Call the PStamp code to find the images that contain the target on the given MJD in the specified filter.
    265     my @images = locate_images($ipprc,$dbname,
     298    my @images = locate_images($ipprc,$imagedb,
    266299                                \@rowList,
    267300                               "bycoord",$stage,
     
    284317            }
    285318            elsif ($stage eq 'stack') {
    286                 # Stacks hide the exposure name very well, so I'm
    287                 # choosing the stack_id as the FPA_ID to match. This
    288                 # probably needs to be approved somehow.
     319                # Stacks hide the exposure name very well, so we can only match against stage_id
    289320                if (${ $j }{stage_id} ne $FPA_ID) {
    290321                    next;
     
    299330            }
    300331            # Debug prints of all the components of this image
    301             foreach my $k (keys %{ $j }) {
    302                 print "$i $j $k ${ $j }{$k}\n";
    303             }
    304            
     332#           foreach my $k (keys %{ $j }) {
     333#               if ($k eq 'row_index') {
     334#                   print "$i $j $k @{${ $j }{$k} }\n";
     335#               }
     336#               print "$i $j $k ${ $j }{$k}\n";
     337#           }
     338
     339            # Check for existance of the images. Drawn mostly from pstampparse.pl
     340            my $run_state = ${ $j }{state};
     341            my $data_state = ${ $j }{data_state};
     342            $data_state = $run_state if $stage eq 'stack';
     343            my $fault = 0;
     344            if (($run_state eq 'goto_purged') or ($data_state eq 'purged') or
     345                ($run_state eq 'drop') or
     346                ($run_state eq 'error_cleaned') or
     347                ($run_state eq 'goto_scrubbed') or ($data_state eq 'scrubbed')) {
     348                # image is gone and it's not coming back
     349                $fault = $PSTAMP_GONE;
     350            }
     351            elsif  (($data_state ne 'full') or ($need_magic and (${ $j }{magicked} < 0))) {
     352                if (($stage eq 'stack')||($stage eq 'diff')) {
     353                    # updating stacks and diffs isn't implemented
     354                    $fault = $PSTAMP_NOT_IMPLEMENTED;
     355                }
     356                if ($stage eq 'chip') {
     357                    my $burntool_state = ${ $j }{burntool_state};
     358                    if ($burntool_state and (abs($burntool_state) < 14)) {
     359                        $fault = $PSTAMP_NOT_AVAILABLE;
     360                    }
     361                }
     362               
     363                if ($fault == 0) {
     364                    # This bombs us out to dqueryparse, which will then flag a job for this run to be updated.
     365                    my_die_for_update($data_state,$query{HEADER}{STAGE}[0],
     366                                      ${ $j }{stage_id},${ $j }{class_id} || ${ $j }{skycell_id},
     367                                      $need_magic,$imagedb,$PSTAMP_NOT_AVAILABLE);
     368                }
     369            }
     370
    305371            # This image matches, so we want to save the information into our output structure
    306372            $image_info{ROWNUM} = $index;
     
    309375            $image_info{MASK}   = ${ $j }{mask};
    310376            $image_info{WEIGHT} = ${ $j }{weight};
     377            $image_info{ERROR}  = $fault;
    311378            $image_info{SKY_COORDINATES} = "$ra $dec";
    312379            # To do sky->image coordinate transformations, we need to use the cmf/smf file. If
     
    419486    my $obscode = shift;
    420487    my $rownum_ref = shift;
     488    my $out_err_ref = shift;
    421489    my $psphot_Npix_ref = shift;
    422490    my $psphot_Qfact_ref = shift;
     
    427495    my $columns = [
    428496        # matching rownum from detectability original request
    429         { name => 'ROWNUM',   type => '20A', writetype => TSTRING },
     497        { name => 'ROWNUM',   type => 'V', writetype => TULONG },
     498        # any errors that occurred during processing
     499        { name => 'ERROR_CODE',   type => 'V', writetype => TULONG },
    430500        # number of pixels used in hypothetical PSF for the query detection
    431501        { name => 'DETECT_N', type => 'V',   writetype => TULONG },
     
    469539    for (my $i = 0; $i < $numRows; $i++) {
    470540        push @{$colData{'ROWNUM'}},      ${ $rownum_ref }[$i];
     541        push @{$colData{'ERROR_CODE'}},  ${ $out_err_ref }[$i];
    471542        push @{$colData{'DETECT_N'}},    ${ $psphot_Npix_ref }[$i];
    472543        push @{$colData{'DETECT_F'}},    ${ $psphot_Qfact_ref }[$i];
     
    543614
    544615}
     616
     617sub my_die_for_update {
     618    my $state = shift;
     619    my $stage = shift;
     620    my $stage_id = shift;
     621    my $component = shift;
     622    my $need_magic = shift;
     623    my $imagedb = shift;
     624    my $exit_code = shift;
     625
     626    print "$state $stage $stage_id $component $need_magic $imagedb\n";
     627    print STDERR "$state $stage $stage_id $component $need_magic $imagedb\n";
     628    exit($exit_code);
     629}
  • trunk/pstamp/scripts/dquery_finish.pl

    r27578 r27859  
    119119# XXX: have the jobs produce the reglist as with postage stamp requests
    120120my ($REGLIST, $reg_list) = tempfile("$out_dir/reqlist.XXXX", UNLINK => !$save_temps);
     121
    121122foreach my $job (@jobs) {
    122123    my $job_id = $job->{job_id};
     
    127128        # the job generated a response file put it into the Data Store
    128129        print $REGLIST "$response_file|||table|\n";
     130
     131        # do the same if we have an error file.  Should the parse data be uploaded as well?
     132        my $err_file = "parse_error.txt";
     133        if (-e "$out_dir/$err_file") {
     134            print $REGLIST "$err_file|||text|\n";
     135        }
    129136    } else  {
    130137        print STDERR "detect_query response file for job $job_id not found\n" if $verbose;
     
    138145    $command .= " --copy --datapath $out_dir";
    139146    $command .= " --type MOPS_DETECTABILITY_RESPONSE";
     147    $command .= " --ps0 $req_id";
    140148    $command .= " --dbname $dbname" if $dbname;
    141149
  • trunk/pstamp/scripts/dqueryparse.pl

    r27578 r27859  
    22#
    33# parse a MOPS_DETCTABILITY_QUERY table and create a results file
    4 #
    5 # Note: this file is currently only a placeholder which creates a fake response file
    6 # and adds a completed job to the database
    74#
    85
    96use strict;
    107use warnings;
    11 
     8use Carp;
    129use Getopt::Long qw( GetOptions );
    1310use Pod::Usage qw( pod2usage );
    1411use IPC::Cmd 0.36 qw( can_run run );
    1512
     13use PS::IPP::PStamp::RequestFile qw( :standard );
     14use PS::IPP::PStamp::Job qw( :standard );
    1615use PS::IPP::Config qw($PS_EXIT_SUCCESS
    1716                       $PS_EXIT_UNKNOWN_ERROR
     
    2625                       );
    2726
     27my ($no_update, $imagedb, $label);
    2828my ($req_file, $req_id, $out_dir, $product, $mode, $dbname, $dbserver, $verbose, $save_temps);
    29 
     29my ($job_id,$rownum); # stuff from the post-update world
    3030#
    3131# parse args
     
    3535        'file=s'          =>      \$req_file,
    3636        'req_id=s'        =>      \$req_id,
     37        'job_id=s'        =>      \$job_id,
     38        'rownum=s'        =>      \$rownum,
    3739        'out_dir=s'       =>      \$out_dir,
     40        'label=s'         =>      \$label,
    3841        'product=s'       =>      \$product,
    3942        'mode=s'          =>      \$mode,
     
    4447) or pod2usage(2);
    4548
    46 my $err = "";
    47 
    48 if (!$req_file) {
    49     $err .= "--file is required\n";
    50 }
    51 if (!$req_id) {
    52     $err .= "--req_id is required\n";
    53 }
    54 if (!$out_dir) {
    55     $err .="--out_dir is required\n";
    56 }
    57 if (!$product) {
    58     $err .="--product is required\n";
    59 }
    60 
    61 
    62 die $err if ($err);
    63 
     49die "invalid mode '$mode'" unless ($mode eq "list_uri") or ($mode eq "queue_job");
     50die "--file or --job_id is required" if !$req_file;
     51
     52if ($mode ne "list_uri") {
     53    die "req_id is required" if !$req_id;
     54    die "out_dir is required" if !$out_dir;
     55    die "product is required" if !$product;
     56}
    6457my $missing_tools;
    6558my $pstamptool = can_run('pstamptool') or (warn "Can't find pstamptool" and $missing_tools =1);
    66 my $fakedresponse = can_run('fakedresponse.pl') or (warn "Can't find fakedresponse.pl" and $missing_tools =1);
     59my $detectresponse = can_run('detectability_respond.pl') or
     60    (warn "Can't find detectability_respond.pl" and $missing_tools = 1);
    6761my $fields = can_run('fields') or (warn "Can't find fields" and $missing_tools =1);
    6862
     
    7771}
    7872
     73# just deal with these arguments once and for all
     74$pstamptool .= " -dbname $dbname" if $dbname;
     75$pstamptool .= " -dbserver $dbserver" if $dbserver;
     76$detectresponse .= " --dbname $dbname" if $dbname;
     77
     78$no_update = 1 if $mode eq "list_job";
     79
     80# Unless we're running as a job, write the parse arguments in case we need to rerun this parsing.
     81if (!$job_id) {
     82    my $argslist = "$out_dir/parse.args";
     83    open ARGSLIST, ">$argslist" or my_die("failed to open argslist file $argslist", $job_id, $PS_EXIT_UNKNOWN_ERROR);
     84    print ARGSLIST "--label $label --mode $mode --req_id $req_id --product $product --out_dir $out_dir --file $req_file\n";
     85    close ARGSLIST;
     86}
     87
    7988# get the query id and check the extname and version from the header
    80 my $fields_output;
     89my $fields_output; 
    8190{
    8291    my $command = "echo $req_file | $fields -x 0 EXTNAME EXTVER QUERY_ID";
     
    8493        run(command => $command, verbose => $verbose);
    8594   
    86 #   fields doesn't return zero when it succeeds
    87 #    unless ($success) {
    88 #        print STDERR @$stderr_buf;
    89 #    }
    9095    $fields_output = join "", @$stdout_buf;
    9196}
    9297my (undef, $extname, $extver, $req_name) = split " ", $fields_output;
    9398
    94 die "$req_file is missing one of EXTNAME EXTVER or QUERY_ID"
     99my_die("$req_file is missing one of EXTNAME EXTVER or QUERY_ID", $PS_EXIT_PROG_ERROR)
    95100    if !(defined($extname) and defined($extver) and defined($req_name));
    96 
    97 die "$req_file has EXTNAME $extname not MOPS_DETECTABILITY_QUERY table"
    98                 if $extname ne         "MOPS_DETECTABILITY_QUERY";
    99 die "$req_file is version $extver expecting 1" if $extver ne 1;
    100 
    101 if (0) {
    102 $out_dir .= "/$req_name";
    103 }
     101my_die("$req_file has EXTNAME $extname not MOPS_DETECTABILITY_QUERY table", $PS_EXIT_PROG_ERROR)
     102    if $extname ne "MOPS_DETECTABILITY_QUERY";
     103my_die("$req_file is version $extver expecting 1", $PS_EXIT_PROG_ERROR)
     104    if $extver ne 1;
     105
     106# Set up the workdir for this query.
    104107if (! -e $out_dir ) {
    105     mkdir $out_dir or die "cannot create output directory $out_dir";
     108    mkdir $out_dir or my_die("cannot create output directory $out_dir", $PS_EXIT_PROG_ERROR);
    106109} elsif (! -d $out_dir ) {
    107     die "output fileset directory $out_dir exists but is not a directory";
    108 }
    109 
    110 #
    111 # XXX Eventually we will parse the file here, to look up the list of input images to be processed
    112 # and queue a job for each image.
    113 # The request file will be one of the arguments. Each job will look at all rows and create entries
    114 # in the response file the coordinates that overlap the image.
    115 #
    116 # In the meantime we don't parse the file here, pass it to the program fakedresponse.
    117 # It creates a response file with a line for each row in the request file
    118 
    119 my $response_file = "$out_dir/response.fits";
     110    my_die ("output fileset directory $out_dir exists but is not a directory", $PS_EXIT_PROG_ERROR);
     111}
     112
     113
     114# Pass along the request file to the response generator.
     115my $response_file = "$out_dir/${req_name}.dresponse.${req_id}.fits";
    120116my $fault;
     117my $data_to_update = '';
    121118{
    122     my $command = "$fakedresponse --input $req_file --output $response_file --workdir $out_dir";
     119    my $command = "$detectresponse --input $req_file --output $response_file --workdir $out_dir";
    123120    $command .= " --save-temps" if $save_temps;
    124121    $command .= " --verbose" if $verbose;
     
    127124        run(command => $command, verbose => $verbose);
    128125    unless ($success) {
    129         warn("Unable to perform $command error code: $error_code");
    130     }
     126        warn("Warn! Unable to perform $command error code: $error_code");
     127    }
     128    # Use the fault code to see if we can regenerate the missing data.
    131129    $fault = $error_code >> 8;
    132 }
    133 
    134 my $job_id;
     130    if ($fault == $PSTAMP_NOT_AVAILABLE) {
     131        $data_to_update = (split /\n/, (join "", @$stdout_buf))[-1];   
     132    }   
     133}
     134
    135135my $result;
    136 {
     136# If we returned correctly with a valid response file, get a job ID
     137# for the completed work, and move the response to a standardized name.
     138if ($fault == 0) {
    137139    my $command = "$pstamptool -addjob -req_id $req_id -outputBase $out_dir";
    138     $command .= " -job_type detect_query -state stop -fault $fault";
     140    $command .= " -job_type detect_query -state stop -fault 0";
    139141    $command .= " -rownum 1";
    140     $command .= " -dbname $dbname" if $dbname;
    141     $command .= " -dbserver $dbserver" if $dbserver;
    142 
    143     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    144         run(command => $command, verbose => $verbose);
    145 
     142
     143    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     144        run(command => $command, verbose => $verbose);
    146145    if ($success) {
    147146        $job_id = join "", @$stdout_buf;
     
    156155    }
    157156}
    158 
    159 
     157else {
     158    # Failed to run correctly, which means that we need to queue a job and flag data for updating.
     159    # Get the dependency id for the data we're requesting be updated.
     160    my $dep_id = queue_update_run($req_id,$job_id,$out_dir,$label,$data_to_update);
     161
     162    # Link this request to a job and link that job to any dependency
     163    my $command = "$pstamptool -addjob -req_id $req_id -outputBase $out_dir";
     164    $command .= " -job_type detect_query -state run -fault 0";
     165    $command .= " -rownum 1";
     166    $command .= " -dep_id $dep_id" if $dep_id;
     167
     168    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     169        run(command => $command, verbose => $verbose);
     170
     171    if ($success) {
     172        $job_id = join "", @$stdout_buf;
     173        chomp $job_id;
     174        if ($job_id && -e $response_file) {
     175            # We shouldn't have a response file at this stage.
     176            rename $response_file, "$out_dir/response${job_id}.fits";
     177        }
     178        $result = 0;
     179    } else {
     180        warn("Unable to perform $command error code: $error_code");
     181        $result = $error_code >> 8;
     182    }
     183}
     184
     185# This does not set the request state to stop.  That will happen with the request_finish.pl script,
     186# which will notice that we've inserted the stopped job and decide we're finished. Easy enough.
    160187{
    161188    my $command = "$pstamptool -updatereq -req_id $req_id -name $req_name -outProduct $product";
    162189    $command .= " -fault $result" if $result;
    163     $command .= " -dbname $dbname" if $dbname;
    164     $command .= " -dbserver $dbserver" if $dbserver;
    165190
    166191    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    167192        run(command => $command, verbose => $verbose);
    168193    unless ($success) {
    169         die "$command failed";
     194        my_die("$command failed",$PS_EXIT_UNKNOWN_ERROR);
    170195    }
    171196}
    172197 
    173198exit 0;
     199
     200
     201# If we have to queue an update run, do so and create a new dependent
     202sub queue_update_run {
     203    my ($req_id, $job_id, $out_dir, $label, $data_to_update) = @_;
     204
     205    my ($state, $stage, $stage_id, $component, $need_magic, $imagedb) = split /\s+/, $data_to_update;
     206   
     207    if (($state ne 'cleaned') and ($state ne 'update') and ($state ne 'goto_cleaned')) {
     208        # We should have received one of these states, so if not, signal that we have a problem.
     209        my_die("$stage $stage_id is in unexpected state $state", $PS_EXIT_PROG_ERROR);
     210    }
     211    my $dep_id;
     212    my $command = "$pstamptool -getdependent -stage $stage -stage_id $stage_id -imagedb $imagedb -component $component ";
     213    $command .= " -outdir $out_dir";
     214    $command .= " -need_magic" if $need_magic;
     215
     216    my $rlabel = "dq_ud_" . $label if $label;
     217    $command .= " -rlabel $rlabel" if $rlabel;
     218
     219    if (!$no_update) {
     220        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     221            run(command => $command, verbose => $verbose);
     222        unless ($success) {
     223            my_die("$command failed", $PS_EXIT_UNKNOWN_ERROR);
     224        }
     225        my $output = join "", @$stdout_buf;
     226        chomp $output;
     227        $dep_id = $output;
     228
     229        my_die("pstamptool -getdependent returned invalid dep_id", $PS_EXIT_PROG_ERROR) if !$dep_id;
     230    }
     231    else {
     232        print STDERR "skipping $command\n";
     233        $dep_id = 42;
     234    }
     235
     236    return($dep_id);
     237}
     238       
     239   
     240
     241sub my_die {
     242    my $msg = shift;
     243    my $fault = shift;
     244
     245    carp $msg;
     246
     247    # we don't fault the request here pstamp_parser_run.pl handles that if necessary
     248    exit $fault;
     249}
  • trunk/pstamp/scripts/pstamp_job_run.pl

    r27751 r27859  
    7474my $ppstamp    = can_run('ppstamp') or (warn "Can't find ppstamp" and $missing_tools = 1);
    7575my $pstamp_get_image_job    = can_run('pstamp_get_image_job.pl') or (warn "Can't find pstamp_get_image_job.pl" and $missing_tools = 1);
     76my $dqueryparse = can_run('dqueryparse.pl') or (warn "Can't find dqueryparse.pl" and $missing_tools = 1);
    7677
    7778if ($missing_tools) {
     
    165166    }
    166167} elsif ($jobType eq "detect_query") {
    167     my_die("detect_query jobs not supported yet", $job_id,$PS_EXIT_CONFIG_ERROR);
     168    # detect_query jobs are basically holders to note that we need a file updated before we can continue.
     169    # Load the argument list that dqueryparse should have created the first time it ran, so we know how to
     170    # run it again the same way.
     171    my $argslist = "$outputBase/parse.args";
     172    open ARGSLIST, "<$argslist" or my_die("failed to open argslist file $argslist", $job_id, $PS_EXIT_UNKNOWN_ERROR);
     173    my $argString = <ARGSLIST>;
     174    close ARGSLIST;
     175    chomp $argString;
     176
     177    # XXX: should we do any other sanity checking?
     178    my_die("arglist file $argslist is empty", $job_id, $PS_EXIT_DATA_ERROR) if !$argString;
     179
     180    my $command = "$dqueryparse --job_id $job_id $argString";
     181    $command .= " --dbname $dbname" if $dbname;
     182    $command .= " --dbserver $dbserver" if $dbserver;
     183    $command .= " --verbose" if $verbose;
     184    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     185        run(command => $command, verbose => $verbose);
     186
     187    # Although dqueryparse can potentially force more updates (and more jobs), it should still return success.
     188    if ($success) {
     189        $jobStatus = $PS_EXIT_SUCCESS;
     190    } else {
     191        $jobStatus = $error_code >> 8;
     192        my_die("dqueryparse.pl failed with error code: $jobStatus", $job_id, $jobStatus);
     193    }
    168194} else {
    169195    my_die("unknown jobType $jobType found", $job_id, $PS_EXIT_PROG_ERROR);
  • trunk/pstamp/scripts/pstamp_parser_run.pl

    r27841 r27859  
    152152        } elsif ($request_type eq "MOPS_DETECTABILITY_QUERY") {
    153153            $reqType = 'dquery';
    154             $parse_cmd = $dqueryparse;
     154            $parse_cmd = "$dqueryparse";
     155            $parse_cmd .= " --label $label" if $label;
    155156        } else {
    156157            print STDERR "Unknown request type $request_type found in $uri";
Note: See TracChangeset for help on using the changeset viewer.