IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 5, 2012, 5:19:48 PM (14 years ago)
Author:
mhuber
Message:

merging latest r33407 trunk changes to meh_branches/ppstack_test

Location:
branches/meh_branches/ppstack_test
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/meh_branches/ppstack_test

  • branches/meh_branches/ppstack_test/pstamp/scripts

  • branches/meh_branches/ppstack_test/pstamp/scripts/dqueryparse.pl

    r28818 r33415  
    1 #!/usr/bin/env perl
    2 #
    3 # parse a MOPS_DETCTABILITY_QUERY table and create a results file
     1#! /usr/bin/env perl
     2#
     3#
     4# Create a response to a MOPS_DETECTABILITY_QUERY
     5#
    46#
    57
    68use strict;
    79use warnings;
     10
     11use Sys::Hostname;
    812use Carp;
     13use File::Basename;
     14use File::Copy;
     15use IPC::Cmd 0.36 qw( can_run run );
    916use Getopt::Long qw( GetOptions );
    1017use Pod::Usage qw( pod2usage );
    11 use IPC::Cmd 0.36 qw( can_run run );
     18use File::Temp qw( tempfile tempdir);
     19use POSIX;  # for strftime
    1220
    1321use PS::IPP::PStamp::RequestFile qw( :standard );
    1422use PS::IPP::PStamp::Job qw( :standard );
    15 use PS::IPP::Config qw($PS_EXIT_SUCCESS
    16                        $PS_EXIT_UNKNOWN_ERROR
    17                        $PS_EXIT_SYS_ERROR
    18                        $PS_EXIT_CONFIG_ERROR
    19                        $PS_EXIT_PROG_ERROR
    20                        $PS_EXIT_DATA_ERROR
    21                        $PS_EXIT_TIMEOUT_ERROR
    22                        metadataLookupStr
    23                        metadataLookupBool
    24                        caturi
    25                        );
    26 
    27 my ($no_update, $imagedb, $label);
    28 my ($req_file, $req_id, $outdir, $product, $mode, $dbname, $dbserver, $verbose, $save_temps);
    29 my ($job_id,$rownum); # stuff from the post-update world
    30 #
    31 # parse args
    32 #
    33 
     23use PS::IPP::Config qw( :standard );
     24use PS::IPP::Metadata::List qw( parse_md_list );
     25
     26# use Astro::FITS::CFITSIO qw( :constants );
     27# Astro::FITS::CFITSIO::PerlyUnpacking(1);
     28
     29my $host = hostname();
     30
     31my $mode = 'queue_job';
     32
     33my ($req_id, $product, $need_magic, $missing_tools, $project, $label);
     34my ($request_file, $outdir, $dbname, $dbserver, $verbose, $save_temps, $no_update);
    3435GetOptions(
    35         'file=s'          =>      \$req_file,
    36         'req_id=s'        =>      \$req_id,
    37         'job_id=s'        =>      \$job_id,
    38         'rownum=s'        =>      \$rownum,
    39         'outdir=s'       =>      \$outdir,
    40         'label=s'         =>      \$label,
    41         'product=s'       =>      \$product,
    42         'mode=s'          =>      \$mode,
    43         'dbname=s'        =>      \$dbname,
    44         'dbserver=s'      =>      \$dbserver,
    45         'verbose'         =>      \$verbose,
    46         'save-temps'      =>      \$save_temps,
    47 ) or pod2usage(2);
    48 
    49 die "invalid mode '$mode'" unless ($mode eq "list_uri") or ($mode eq "queue_job");
    50 die "--file or --job_id is required" if !$req_file;
    51 
    52 if ($mode ne "list_uri") {
    53     die "req_id is required" if !$req_id;
    54     die "outdir is required" if !$outdir;
    55     die "product is required" if !$product;
    56 }
    57 my $missing_tools;
    58 my $pstamptool = can_run('pstamptool') or (warn "Can't find pstamptool" and $missing_tools =1);
    59 my $detectresponse = can_run('detectability_respond.pl') or
    60     (warn "Can't find detectability_respond.pl" and $missing_tools = 1);
    61 my $fields = can_run('fields') or (warn "Can't find fields" and $missing_tools =1);
    62 
     36    'file=s'          =>      \$request_file,
     37    'req_id=s'        =>      \$req_id,
     38    'outdir=s'        =>      \$outdir,
     39    'label=s'         =>      \$label,
     40    'product=s'       =>      \$product,
     41    'mode=s'          =>      \$mode,
     42    'dbname=s'        =>      \$dbname,
     43    'dbserver=s'      =>      \$dbserver,
     44    'verbose'         =>      \$verbose,
     45    'save-temps'      =>      \$save_temps,
     46    'no-update'       =>      \$no_update,
     47    ) or pod2usage(2);
     48
     49pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
     50pod2usage( -msg => "Required options: --file --req_id --outdir --dbname",
     51           -exitval => 3,
     52    ) unless
     53    defined $request_file and defined $outdir and defined $dbname and defined $req_id;
     54
     55my $detect_query_read = can_run('detect_query_read') or (warn "Can't find detect_query_read" and $missing_tools = 1);
     56my $pstamptool = can_run('pstamptool') or (warn "Can't find pstamptool" and $missing_tools = 1);
     57my $fields = can_run('fields') or (warn "Can't find fields" and $missing_tools = 1);
    6358if ($missing_tools) {
    6459    warn("Can't find required tools.");
    65     exit ($PS_EXIT_CONFIG_ERROR);
    66 }
     60    exit($PS_EXIT_CONFIG_ERROR);
     61}
     62
     63unless (defined $verbose) {
     64    $verbose = 0;
     65}
     66
     67my $ipprc = PS::IPP::Config->new();
     68my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
     69
    6770
    6871if (!$dbserver) {
    69     my $ipprc = PS::IPP::Config->new();
    70     $dbserver =  metadataLookupStr($ipprc->{_siteConfig}, 'PS_DBSERVER');
    71 }
    72 
    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.
    81 if (!$job_id) {
    82     my $argslist = "$outdir/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 --outdir $outdir --file $req_file\n";
    85     close ARGSLIST;
     72    $dbserver = metadataLookupStr($ipprc->{_siteConfig}, 'PS_DBSERVER');
     73}
     74
     75$pstamptool .= " -dbname $dbname -dbserver $dbserver";
     76
     77# Project name is hardcoded to gpc1 for the moment.
     78$project = resolve_project($ipprc,"gpc1",$dbname,$dbserver);
     79my $imagedb = $project->{dbname};
     80if (!$imagedb) {
     81    my_die("failed to find imagedb for project: $project", $PS_EXIT_CONFIG_ERROR);
    8682}
    8783
     
    8985my $fields_output;
    9086{
    91     my $command = "echo $req_file | $fields -x 0 EXTNAME EXTVER QUERY_ID";
     87    my $command = "echo $request_file | $fields -x 0 EXTNAME EXTVER QUERY_ID";
    9288    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    9389        run(command => $command, verbose => $verbose);
     
    9793my (undef, $extname, $extver, $req_name) = split " ", $fields_output;
    9894
    99 my_die("$req_file is missing one of EXTNAME EXTVER or QUERY_ID", $PS_EXIT_PROG_ERROR)
     95my_die("$request_file is missing one of EXTNAME EXTVER or QUERY_ID", $PS_EXIT_PROG_ERROR)
    10096    if !(defined($extname) and defined($extver) and defined($req_name));
    101 my_die("$req_file has EXTNAME $extname not MOPS_DETECTABILITY_QUERY table", $PS_EXIT_PROG_ERROR)
     97my_die("$request_file has EXTNAME $extname not MOPS_DETECTABILITY_QUERY table", $PS_EXIT_PROG_ERROR)
    10298    if $extname ne "MOPS_DETECTABILITY_QUERY";
    103 my_die("$req_file is version $extver expecting 1", $PS_EXIT_PROG_ERROR)
     99my_die("$request_file is version $extver expecting 1 or 2", $PS_EXIT_PROG_ERROR)
    104100    if ($extver ne 1) and ($extver ne 2);
    105101
    106 # Set up the workdir for this query.
    107 if (! -e $outdir ) {
    108     mkdir $outdir or my_die("cannot create output directory $outdir", $PS_EXIT_PROG_ERROR);
    109 } elsif (! -d $outdir ) {
    110     my_die ("output fileset directory $outdir exists but is not a directory", $PS_EXIT_PROG_ERROR);
    111 }
    112 
    113 
    114 # Pass along the request file to the response generator.
    115 my $response_file = "$outdir/${req_name}.dresponse.${req_id}.fits";
    116 my $fault;
    117 my $data_to_update = '';
     102if ($req_id) {
     103    my $command = "$pstamptool -listreq  -name $req_name -not_req_id $req_id";
     104    # set verbose to false so that error message about request not found doesn't appear in parse_error.txt
     105    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     106        run(command => $command, verbose => 0);
     107    if ($success) {
     108        # -listreq succeeded duplicate request name
     109        print STDERR "QUERY_ID $req_name has already been used\n";
     110        $command = "$pstamptool -addjob -job_type none -rownum 0 -state stop -fault $PSTAMP_DUP_REQUEST -req_id $req_id";
     111        unless ($no_update) {
     112            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     113                run(command => $command, verbose => $verbose);
     114            if (!$success) {
     115                my_die("$command failed", $PS_EXIT_UNKNOWN_ERROR);
     116            }
     117        } else {
     118            print STDERR "skipping $command\n";
     119        }
     120       
     121        my $datestr = strftime "%Y%m%d%H%M%S.$req_id", gmtime;
     122        $req_name = "ERROR.$datestr";
     123    }
     124    $command = "$pstamptool -updatereq -req_id $req_id  -set_name $req_name -set_outProduct $product";
     125    unless ($no_update) {
     126        ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     127            run(command => $command, verbose => $verbose);
     128        unless ($success) {
     129            my_die("$command failed", $PS_EXIT_UNKNOWN_ERROR);
     130        }
     131    } else {
     132        print STDERR "skipping $command\n";
     133    }
     134    exit (0);
     135}
     136
     137
     138
     139#
     140# query is a hash which uses $fpa_id as the key
     141# The values are hash references which have the various parameter name as the key
     142#   The values of the parameter hashes are arrays which contain the values for individual rows
     143#   in the detectabilty query request
     144my %query = ();
     145
    118146{
    119     my $command = "$detectresponse --input $req_file --output $response_file --workdir $outdir";
    120     $command .= " --save-temps" if $save_temps;
    121     $command .= " --verbose" if $verbose;
    122 
     147    #
     148    # Parse input request file using detect_query_read
     149    # result is a simple text file
     150    #
     151    my $dqr_command = "$detect_query_read --dbname $imagedb --input $request_file";
    123152    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    124         run(command => $command, verbose => $verbose);
     153        run(command => $dqr_command, verbose => $verbose);
    125154    unless ($success) {
    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.
    129     $fault = $error_code >> 8;
    130     if ($fault == $PSTAMP_NOT_AVAILABLE) {
    131         unless (-e "$outdir/update_request.dat") {
    132             my_die ("Update request indicated, but unable to find actual request!", $PS_EXIT_PROG_ERROR);
    133         }
    134     }   
    135    
    136 }
    137 
    138 my $result;
    139 # If we returned correctly with a valid response file, get a job ID
    140 # for the completed work, and move the response to a standardized name.
    141 if ($fault == 0) {
    142     my $command = "$pstamptool -addjob -req_id $req_id -outputBase $outdir";
    143     $command .= " -job_type detect_query -state stop -fault 0";
    144     $command .= " -rownum 1";
    145 
    146     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    147         run(command => $command, verbose => $verbose);
    148     if ($success) {
    149         $job_id = join "", @$stdout_buf;
    150         chomp $job_id;
    151         if ($job_id && -e $response_file) {
    152             rename $response_file, "$outdir/response${job_id}.fits";
    153         }
    154         $result = 0;
     155        # This is a problem, because I'm not sure how we handle a failure to read something.
     156        # We need to return a $PSTAMP_INVALID_REQUEST, I think, but if we can't read it,
     157        # we can't send that response back.
     158        die("Unable to perform $dqr_command error code: $error_code");
     159    }
     160    my $Nrows = 0;
     161    {
     162        my @column_names = ();
     163        # split output into lines skip until the line which lists the column names is found
     164        # Parse subsequent lines
     165        foreach my $entry (split /\n/, (join "", @$stdout_buf)) {
     166            if ($entry =~ /^#/) {
     167                @column_names = split /\s+/, $entry;
     168                shift(@column_names);  # Dump the hash sign.
     169            }
     170            elsif (scalar @column_names) {
     171                # ROWNUM RA1_DEG DEC1_DEG RA2_DEG DEC2_DEG MAG QUERY_ID FPA_ID MJD-OBS FILTER OBSCODE STAGE
     172                my %row_data;
     173                @row_data{@column_names} = (split /\s+/, $entry);
     174                for (my $i = 0; $i <= $#column_names; $i++) {
     175                    push @{ $query{$row_data{"FPA_ID"}}{$column_names[$i]} }, $row_data{$column_names[$i]};
     176                    $Nrows = scalar(keys(%query));
     177                    # print "$row_data{'FPA_ID'} $Nrows $i $column_names[$i] $row_data{$column_names[$i]}\n";
     178                }
     179            }
     180        }
     181    }
     182    #
     183    # Identify target components.  This should properly collate targets on a single imfile.
     184    #
     185    my $query_id;
     186    foreach my $fpa_id (keys %query) {
     187        my %temp_hash;
     188        my $query_style = 'byexp';
     189        my $stage;
     190        my $filter;
     191        my $mjd;
     192        # Confirm that we only have one stage/filter/mjd
     193        if ($fpa_id ne 'Not_Set') {   # We only need to check things that aren't the known odd case.
     194            for (my $i = 0; $i <= $#{ $query{$fpa_id}{STAGE} }; $i++) {
     195                $temp_hash{STAGE}{$query{$fpa_id}{STAGE}[$i]} = 1;
     196                $temp_hash{FILTER}{$query{$fpa_id}{FILTER}[$i]} = 1;
     197                $temp_hash{'MJD-OBS'}{$query{$fpa_id}{'MJD-OBS'}[$i]} = 1;
     198            }
     199            if (scalar(keys(%{ $temp_hash{STAGE} })) == 1) {
     200                $stage = (keys(%{ $temp_hash{STAGE} }))[0];
     201            }
     202            else {
     203                exit_with_failure(21,"Too many STAGEs specified");
     204            }
     205            if (scalar(keys(%{ $temp_hash{FILTER} })) == 1) {
     206                $filter = (keys(%{ $temp_hash{FILTER} }))[0];
     207            }
     208            else {
     209                exit_with_failure(21,"Too many FILTERs specified");
     210            }
     211            if (scalar(keys(%{ $temp_hash{'MJD-OBS'} })) == 1) {
     212                $mjd = (keys(%{ $temp_hash{'MJD-OBS'} }))[0];
     213            }
     214            else {
     215                exit_with_failure(21,"Too many MJD-OBS specified");
     216            }
     217        }
     218
     219        # Set up a rowList with default values
     220        my @rowList;
     221        for (my $i = 0; $i <= $#{ $query{$fpa_id}{STAGE} }; $i++) {
     222            $rowList[$i]->{CENTER_X} = $query{$fpa_id}{RA1_DEG}[$i];
     223            $rowList[$i]->{CENTER_Y} = $query{$fpa_id}{DEC1_DEG}[$i];
     224            $rowList[$i]->{ID} = $query{$fpa_id}{ROWNUM}[$i];
     225            $rowList[$i]->{COORD_MASK} = 0;
     226            # Set default values
     227            $query{$fpa_id}{BAD_COMPONENT}[$i] = 1;
     228            $query{$fpa_id}{IMAGE}[$i] = 'no_image';
     229            $query{$fpa_id}{MASK}[$i] = 'no_mask';
     230            $query{$fpa_id}{WEIGHT}[$i] = 'no_weight';
     231            $query{$fpa_id}{PSF}[$i] = 'no_psf';
     232
     233            $query{$fpa_id}{STAGE_ID}[$i] = 'no_id';
     234            $query{$fpa_id}{IMAGE_DB}[$i] = 'no_imdb';
     235            $query{$fpa_id}{NEED_MAGIC}[$i] = 'no_magic';
     236            $query{$fpa_id}{MAGICKED}[$i] = 'no_magic';
     237            $query{$fpa_id}{CATALOG}[$i] = 'no_catalog';
     238            $query{$fpa_id}{COMPONENT_ID}[$i] = 'no_component';
     239            $query{$fpa_id}{CLASS_ID}[$i] = 'no_class';
     240
     241            $query{$fpa_id}{STATE}[$i] = 'no_state';
     242            $query{$fpa_id}{DATA_STATE}[$i] = 'no_dstate';
     243            $query{$fpa_id}{FAULT}[$i] = 'no_fault';
     244            $query{$fpa_id}{BURNTOOL_STATE}[$i] = 'no_btstate';
     245        }
     246
     247        # Determine the query style for this fpa_id
     248        if ($fpa_id =~ /o.*g.*o/) {
     249            $query_style = 'byexp';
     250        }
     251        elsif ($fpa_id =~ /\d+/) {
     252            $query_style = 'byid';
     253        }
     254        elsif ($fpa_id eq 'Not_Set') {
     255            # no matching file was found skip
     256            next;
     257        }
     258        else {
     259            exit_with_failure(21,"Parse error in request file");
     260        }
     261        # Set common request components
     262        my $option_mask |= 1;
     263        $option_mask |= $PSTAMP_SELECT_IMAGE;
     264        $option_mask |= $PSTAMP_SELECT_MASK;
     265        $option_mask |= $PSTAMP_SELECT_VARIANCE;
     266        $option_mask |= $PSTAMP_SELECT_PSF;
     267
     268        # magic is dead
     269        my $need_magic = 0;
     270        if ($stage eq 'stack') {
     271            $need_magic = 0;
     272        }
     273        my $mjd_min = int $mjd;
     274        my $mjd_max = $mjd + 1;
     275       
     276        my $req_filter;
     277        if ($filter ne 'Not_Set') {
     278            $req_filter = $filter . '%';
     279        }
     280       
     281        # Call the PStamp code to find the images that contain the target on the given MJD in the specified filter.
     282        my $pstamp_images_ref = locate_images($ipprc,$imagedb,
     283                                          \@rowList,
     284                                          $query_style,$stage,
     285                                          $fpa_id,undef,undef,
     286                                          $option_mask,$need_magic,
     287                                          $mjd_min,$mjd_max,$req_filter,undef,$verbose); 
     288       
     289        foreach my $this_image_ref (@{ $pstamp_images_ref }) {
     290
     291            # loop over the rows that this component matched
     292            foreach my $valid_index (@{ $this_image_ref->{row_index} }) {
     293                $query{$fpa_id}{IMAGE}[$valid_index] = $this_image_ref->{image};
     294                $query{$fpa_id}{MASK}[$valid_index] = $this_image_ref->{mask};
     295                $query{$fpa_id}{WEIGHT}[$valid_index] = $this_image_ref->{weight};
     296                $query{$fpa_id}{PSF}[$valid_index] = $this_image_ref->{psf};
     297                $query{$fpa_id}{STAGE_ID}[$valid_index] = $this_image_ref->{stage_id};
     298                $query{$fpa_id}{IMAGE_DB}[$valid_index] = $this_image_ref->{imagedb};
     299                $query{$fpa_id}{NEED_MAGIC}[$valid_index] = $need_magic;
     300                $query{$fpa_id}{BAD_COMPONENT}[$valid_index] = 0;
     301               
     302                if (exists($this_image_ref->{astrom})) {
     303                    $query{$fpa_id}{CATALOG}[$valid_index] = $this_image_ref->{astrom};
     304                }
     305                else {
     306                    $query{$fpa_id}{CATALOG}[$valid_index] = $this_image_ref->{cmf};
     307                }
     308                if (exists($this_image_ref->{class_id})) {
     309                    $query{$fpa_id}{COMPONENT_ID}[$valid_index] = $this_image_ref->{class_id};
     310                    $query{$fpa_id}{CLASS_ID}[$valid_index] = $this_image_ref->{class_id};
     311                   
     312                }
     313                else {
     314                    $query{$fpa_id}{COMPONENT_ID}[$valid_index] = $this_image_ref->{skycell_id};
     315                    $query{$fpa_id}{CLASS_ID}[$valid_index] = 'fpa';
     316                }
     317               
     318                $query{$fpa_id}{STATE}[$valid_index] = $this_image_ref->{state};
     319                if (exists($this_image_ref->{data_state})) {
     320                    $query{$fpa_id}{DATA_STATE}[$valid_index] = $this_image_ref->{data_state};
     321                }
     322                else {
     323                    $query{$fpa_id}{DATA_STATE}[$valid_index] = $this_image_ref->{state};
     324                }
     325                $query{$fpa_id}{FAULT}[$valid_index] = 0;
     326                    $query{$fpa_id}{MAGICKED}[$valid_index] = $this_image_ref->{magicked};
     327                if ($stage eq 'chip') {
     328                    $query{$fpa_id}{BURNTOOL_STATE}[$valid_index] = $this_image_ref->{burntool_state};
     329                }
     330               
     331                # Determine if the data exists.
     332                if (($query{$fpa_id}{STATE}[$valid_index] eq 'goto_purged') or
     333                    ($query{$fpa_id}{DATA_STATE}[$valid_index] eq 'purged') or
     334                    ($query{$fpa_id}{STATE}[$valid_index] eq 'drop') or
     335                    ($query{$fpa_id}{STATE}[$valid_index] eq 'error_cleaned') or
     336                    ($query{$fpa_id}{STATE}[$valid_index] eq 'goto_scrubbed') or
     337                    ($query{$fpa_id}{DATA_STATE}[$valid_index] eq 'scrubbed')) {
     338                   
     339                    # image is gone and it's not coming back
     340                    $query{$fpa_id}{FAULT}[$valid_index] = $PSTAMP_GONE;
     341                }
     342                elsif ($need_magic and ($query{$fpa_id}{MAGICKED}[$valid_index] eq 0)) {
     343                    $query{$fpa_id}{FAULT}[$valid_index] = $PSTAMP_NOT_DESTREAKED;
     344                }
     345                elsif (($query{$fpa_id}{DATA_STATE}[$valid_index] ne 'full')) {               
     346                    $query{$fpa_id}{FAULT}[$valid_index] = $PSTAMP_NOT_AVAILABLE;
     347                   
     348                    # updating stacks isn't implemented
     349                    if (($stage eq 'stack')) {
     350                        $query{$fpa_id}{FAULT}[$valid_index] = $PSTAMP_NOT_IMPLEMENTED;
     351                    }
     352                    # updating old burntool data isn't implemented
     353                    elsif ($stage eq 'chip') {
     354                        if ($query{$fpa_id}{BURNTOOL_STATE}[$valid_index] and
     355                            (abs($query{$fpa_id}{BURNTOOL_STATE}[$valid_index]) < 14)) {
     356                            $query{$fpa_id}{FAULT}[$valid_index] = $PSTAMP_NOT_IMPLEMENTED;
     357                            }
     358                    }
     359                } # End determining error faults.
     360            }
     361        }
     362    }
     363}
     364
     365my %update_request;
     366my %rows_for_component;
     367my %faulted_rows;
     368
     369# now build a hash using image name as key which contains the list of rows for that image
     370foreach my $fpa_id (keys %query) {
     371    for (my $i = 0; $i <= $#{ $query{$fpa_id}{ROWNUM} }; $i++) {
     372        if ($query{$fpa_id}{BAD_COMPONENT}[$i] == 0) {
     373            push @{ $rows_for_component{$fpa_id}{$query{$fpa_id}{IMAGE}[$i]} }, $i;
     374        } else {
     375            $faulted_rows{$query{$fpa_id}{ROWNUM}[$i]} = $query{$fpa_id}{FAULT}[$i];
     376        }
     377    }
     378}
     379
     380#
     381# Finally build the parameter lists and queue jobs for each component
     382#
     383
     384my $job_num = 1;
     385foreach my $fpa_id (keys %rows_for_component) {
     386    foreach my $component (keys % {$rows_for_component{$fpa_id} } ) {
     387        my $i = $rows_for_component{$fpa_id}{$component}[0];
     388        my $dep_id = 0;
     389        my $data_state = $query{$fpa_id}{DATA_STATE}[$i];
     390        if ($data_state ne 'full') {
     391            # Queue this data to be updated
     392            my $stage = $query{$fpa_id}{STAGE}[$i];
     393            my $stage_id = $query{$fpa_id}{STAGE_ID}[$i];
     394            my $component = $query{$fpa_id}{COMPONENT_ID}[$i];
     395            print "Need to UPDATE $stage $stage_id $component from $data_state\n";
     396            $dep_id = queue_update_run($req_id, $outdir, $label, $data_state, $stage, $stage_id, $component, 0, $imagedb);
     397        }
     398
     399        my $outputBase = "$outdir/$job_num" . "_";
     400
     401        # params_file is an mdc description containing the parameters for the job
     402        my $params_file = $outputBase . "params.mdc";
     403
     404        open PARAMS, ">$params_file" or my_die("failed to create $params_file", $PS_EXIT_UNKNOWN_ERROR);
     405
     406        # first the job params
     407        print PARAMS "dqueryJobParams METADATA\n";
     408        foreach my $key (keys %{ $query{$fpa_id} } ) {
     409            my $value = $query{$fpa_id}{$key}[0];
     410            print PARAMS "\t$key\t\tSTR\t$value\n";
     411        }
     412        print PARAMS "END\n\n";
     413
     414        # then one structure with the coordinates for each row
     415        print PARAMS "dqueryCoord MULTI\n";
     416
     417        my @coordkeys = qw(ROWNUM RA1_DEG DEC1_DEG RA2_DEG DEC2_DEG MAG);
     418        foreach my $row ( @{ $rows_for_component{$fpa_id}{$component} }) {
     419            print PARAMS "\ndqueryCoord METADATA\n";
     420            foreach my $key (@coordkeys) {
     421                my $value = $query{$fpa_id}{$key}[$row];
     422                print PARAMS "\t$key\t\tSTR\t$value\n";
     423            }
     424            print PARAMS "END\n";
     425        }
     426        close(PARAMS);
     427
     428        # use first rownum for this component as the rownum for the job
     429        my $rownum = $query{$fpa_id}{ROWNUM}[$rows_for_component{$fpa_id}{$component}[0]];
     430
     431        my $command = "$pstamptool -addjob -req_id $req_id -outputBase $outputBase -job_type detect_query -state run -rownum $rownum";
     432        $command .= " -dep_id $dep_id" if $dep_id;
     433        unless ($no_update) {
     434            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     435                run(command => $command, verbose => $verbose);
     436            if ($success) {
     437                my $job_id = join "", @$stdout_buf;
     438                print "   Queued job: $job_id\n";
     439            } else {
     440                my $exit_status = $error_code >> 8;
     441                $exit_status = $PS_EXIT_UNKNOWN_ERROR unless $exit_status;
     442                my_die("Unable to perform $command error code: $error_code", $exit_status);
     443            }
     444        } else {
     445            print STDERR "skipping $command\n";
     446        }
     447        $job_num++;
     448    }
     449}
     450
     451foreach my $rownum (keys %faulted_rows ) {
     452    my $outputBase = "$outdir/$job_num" . "_";
     453    my $fault = $faulted_rows{$rownum};
     454    $fault = $PSTAMP_NO_IMAGE_MATCH if ($fault eq 'no_fault');
     455    print STDERR "insert faulted job for $rownum: $fault\n";
     456    my $command = "$pstamptool -addjob -req_id $req_id -outputBase $outputBase -job_type detect_query -state stop -rownum $rownum -fault $fault";
     457    unless ($no_update) {
     458        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     459            run(command => $command, verbose => $verbose);
     460        if ($success) {
     461            my $job_id = join "", @$stdout_buf;
     462            print "   Queued job: $job_id\n";
     463        } else {
     464            my $exit_status = $error_code >> 8;
     465            $exit_status = $PS_EXIT_UNKNOWN_ERROR unless $exit_status;
     466            my_die("Unable to perform $command error code: $error_code", $exit_status);
     467        }
    155468    } else {
    156         warn("Unable to perform $command error code: $error_code");
    157         $result = $error_code >> 8;
    158     }
    159 }
    160 elsif ($fault == $PSTAMP_NOT_AVAILABLE) {
    161     # Failed to run correctly, which means that we need to queue a job and flag data for updating.
    162     # Get the dependency id for the data we're requesting be updated.
    163     open(UPDATE_REQUEST,"$outdir/update_request.dat") || my_die ("Update request indicated, but unable to find actual request!", $PS_EXIT_PROG_ERROR);
    164     while (<UPDATE_REQUEST>) {
    165         my $data_to_update = $_;
    166         chomp($data_to_update);
    167         my $dep_id = queue_update_run($req_id,$job_id,$outdir,$label,$data_to_update);
    168 
    169         # Link this request to a job and link that job to any dependency
    170         my $command = "$pstamptool -addjob -req_id $req_id -outputBase $outdir";
    171         $command .= " -job_type detect_query -state run -fault 0";
    172         $command .= " -rownum 1";
    173         $command .= " -dep_id $dep_id" if $dep_id;
    174 
    175         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    176             run(command => $command, verbose => $verbose);
    177 
    178         if ($success) {
    179             $job_id = join "", @$stdout_buf;
    180             chomp $job_id;
    181             if ($job_id && -e $response_file) {
    182                 # We shouldn't have a response file at this stage.
    183                 rename $response_file, "$outdir/response${job_id}.fits";
    184             }
    185             $result = 0;
    186         } else {
    187             warn("Unable to perform $command error code: $error_code");
    188             $result = $error_code >> 8;
    189         }
    190     }
    191     close(UPDATE_REQUEST);
    192 }
    193 else {
    194     my_die ("Parse fault!!", $PS_EXIT_UNKNOWN_ERROR);
    195 }
    196 
    197 # This does not set the request state to stop.  That will happen with the request_finish.pl script,
    198 # which will notice that we've inserted the stopped job and decide we're finished. Easy enough.
     469        print STDERR "skipping $command\n";
     470    }
     471    $job_num++;
     472}
     473
    199474{
    200475    my $command = "$pstamptool -updatereq -req_id $req_id -set_name $req_name -set_outProduct $product";
    201     $command .= " -set_fault $result" if $result;
    202 
    203     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    204         run(command => $command, verbose => $verbose);
    205     unless ($success) {
     476    # $command .= " -set_fault $result" if $result;
     477
     478    unless ($no_update) {
     479        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     480            run(command => $command, verbose => $verbose);
     481        unless ($success) {
    206482        my_die("$command failed",$PS_EXIT_UNKNOWN_ERROR);
    207     }
    208 }
    209  
     483        }
     484    } else {
     485        print STDERR "Skipping $command\n";
     486    }
     487}
     488
    210489exit 0;
    211490
    212 
    213 # If we have to queue an update run, do so and create a new dependent
    214491sub queue_update_run {
    215     my ($req_id, $job_id, $outdir, $label, $data_to_update) = @_;
    216 
    217     my ($state, $stage, $stage_id, $component, $need_magic, $imagedb) = split /\s+/, $data_to_update;
     492    my ($req_id, $outdir, $label, $state, $stage, $stage_id, $component, $need_magic, $imagedb) = @_;
     493
     494#    my ($state, $stage, $stage_id, $component, $need_magic, $imagedb) = split /\s+/, $data_to_update;
    218495   
    219496    if (($state ne 'cleaned') and ($state ne 'update') and ($state ne 'goto_cleaned')) {
     
    248525    return($dep_id);
    249526}
    250        
    251    
     527
    252528
    253529sub my_die {
    254     my $msg = shift;
    255     my $fault = shift;
    256 
    257     carp $msg;
    258 
    259     # we don't fault the request here pstamp_parser_run.pl handles that if necessary
    260     exit $fault;
    261 }
     530    my $message = shift;
     531    my $exit_code = shift;
     532
     533    $exit_code = $PS_EXIT_PROG_ERROR unless defined $exit_code;
     534
     535    carp("$message");
     536    exit($exit_code);
     537}
     538
     539sub exit_with_failure {
     540    my $status = shift;
     541    my $message = shift;
     542    my_die($message, $status);
     543}
     544
Note: See TracChangeset for help on using the changeset viewer.