IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 15, 2009, 4:02:42 PM (17 years ago)
Author:
eugene
Message:

updates from trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20090715/pstamp/scripts/pstampparse.pl

    r25022 r25406  
    1414use PS::IPP::PStamp::Job qw( :standard );
    1515use File::Temp qw(tempfile);
     16use File::Basename qw(basename);
     17use Carp;
    1618
    1719my $verbose;
     
    9193# make sure the file contains what we are expecting
    9294
    93 die "$request_file_name is not a PS1_PS_REQEST" if $extname ne "PS1_PS_REQUEST";
    94 die "REQ_NAME not found in $request_file_name"  if (!$req_name);
    95 die "wrong EXTVER $extver found in $request_file_name" if ($extver ne "1");
    96 
     95# we shouldn't get here if this is the case so just die. No need to notify the client
     96my_die("$request_file_name is not a PS1_PS_REQEST", $PS_EXIT_PROG_ERROR) if $extname ne "PS1_PS_REQUEST";
     97my_die("REQ_NAME not found in $request_file_name", $PS_EXIT_PROG_ERROR)  if (!$req_name);
     98my_die("wrong EXTVER $extver found in $request_file_name", $PS_EXIT_PROG_ERROR) if ($extver ne "1");
     99
     100# check for duplicate request name
    97101if ($req_id and !$no_update) {
     102    my $command = "$pstamptool -listreq  -name $req_name -not_req_id $req_id";
     103    $command .= " -dbname $dbname" if $dbname;
     104    $command .= " -dbserver $dbserver" if $dbserver;
     105    # no verbose so that error message about request not found doesn't appear in parse_error.txt
     106    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     107        run(command => $command, verbose => 0);
     108    my $exitStatus = $error_code >> 8;
     109    if ($success) {
     110        # -listreq succeeded duplicate request name
     111        insertFakeJobForRow(undef, 0, $PSTAMP_DUP_REQUEST);
     112        exit 0;
     113    }
     114}
     115
     116if ($req_id and !$no_update) {
     117    # update the database with the request name. This will be used as the
     118    # the output data store's product name
    98119    my $command = "$pstamptool -updatereq -req_id $req_id  -name $req_name";
    99120    $command .= " -outProduct $product";
     
    103124        run(command => $command, verbose => $verbose);
    104125    unless ($success) {
    105         die "$command failed";
     126        my_die("$command failed", $PS_EXIT_UNKNOWN_ERROR);
    106127    }
    107128}
     
    120141    }
    121142    my $table =  $mdcParser->parse(join "", @$stdout_buf) or
    122         die("Unable to parse metdata config doc");
     143        my_die("Unable to parse metdata config doc", $PS_EXIT_UNKNOWN_ERROR);
    123144
    124145    $rows = parse_md_list($table);
     
    143164    # parameters that select the images of interest
    144165    my $project  = $row->{PROJECT};
     166
     167    # note: resolve_project avoids running pstamptool every time by remembering the
     168    # last project resolved
     169    my $proj_hash = resolve_project($ipprc, $project, $dbname, $dbserver);
     170    if (!$proj_hash) {
     171        print STDERR "project $project not found\n"  if $verbose;
     172        insertFakeJobForRow($row, 1, $PSTAMP_UNKNOWN_PRODUCT);
     173        $num_jobs++;
     174        next;
     175    }
    145176    my $req_type = $row->{REQ_TYPE};
    146177    $stage = $row->{IMG_TYPE};
     
    153184
    154185    my $option_mask= $row->{OPTION_MASK};
    155 
    156     die "valid region of interest is required to make postage stamps"
    157         if (($job_type eq "stamp") and ! validROI($row));
     186    my $inverse = ($option_mask & $PSTAMP_SELECT_INVERSE) ? 1 : 0;
     187    $row->{inverse} = $inverse;
    158188
    159189    my $skycenter = $row->{skycenter} = ! ($row->{COORD_MASK} & $PSTAMP_CENTER_IN_PIXELS);
    160     $component = "" if (defined($component) and ($component eq "null" or $component eq "all"));
     190
     191    my $search_component = (!defined($component) or ($component eq "null")) ? "" : $component;
    161192   
    162     die "job_type is list_uri but mode is $mode" if ($job_type eq "list_uri") and ($mode ne "list_uri");
    163 
    164 
    165     # note: resolve_project avoids running pstamptool every time by remembering the
    166     # last project resolved
    167     my $proj_hash = resolve_project($ipprc, $project, $dbname, $dbserver);
    168     die "project $project not found\n" unless $proj_hash;
     193    if (!$skycenter and !$search_component) {
     194        print STDERR "COMPONENT must be specified for pixel coordinate ROI\n" if $verbose;
     195        insertFakeJobForRow($row, 1, $PSTAMP_INVALID_REQUEST);
     196        $num_jobs++;
     197        next;
     198    }
     199
     200    $search_component = "" if $search_component eq "all";
     201
     202    if (($job_type eq "stamp") and ! validROI($row)) {
     203        print STDERR "invalid ROI for row $rownum\n" if $verbose;
     204        insertFakeJobForRow($row, 1, $PSTAMP_INVALID_REQUEST);
     205        $num_jobs++;
     206        next;
     207    }
     208
     209   
     210    # $mode list_uri is a debugging mode (it may used by the http interface)
     211    # if this happens just croak
     212    my_die("job_type is list_uri but mode is $mode", $PS_EXIT_PROG_ERROR) if ($job_type eq "list_uri") and ($mode ne "list_uri");
     213
    169214
    170215    my $image_db   = $proj_hash->{dbname};
     
    172217    $need_magic = $proj_hash->{need_magic};
    173218
     219    # Temporary hack so that MOPS can get at non-magicked data
     220    if ($product eq "mops-pstamp-results") {
     221        $need_magic = 0;
     222    }
     223
    174224    # collect rows with the same images of interest in a list so that they
    175     # can be processed optimially
     225    # can be looked up together
    176226    if (@rowList) {
    177227        my $firstRow = $rowList[0];
    178         # XXX: the collecting might work with !$skycenter but I need to think about it
    179         if ($skycenter and same_images_of_interest($row, $firstRow)) {
     228        # note order of these parameters matters
     229        if (same_images_of_interest($firstRow, $row)) {
     230
     231            # add this row to the list and move on
    180232            push @rowList, $row;
    181             # On to the next row
     233
    182234            next;
     235
    183236        } else {
    184             # queue the jobs for this set of rows
     237            # this row has different selectors
     238            # queue the jobs for the ones we've collected
    185239            $num_jobs += queueJobs($mode, \@rowList, $imageList);
    186240            @rowList = ();
     
    188242    }
    189243
     244    # look up images for the current row
    190245    if ($req_type eq "bycoord") {
    191         die "center must be specified in sky coordintes for bycoord" if ( ! $skycenter);
    192         die "lookup bycoord is not yet implemented";
     246        if (!$skycenter) {
     247            print STDERR "center must be specified in sky coordintes for bycoord" if $verbose;
     248            insertFakeJobForRow($row, 1, $PSTAMP_INVALID_REQUEST);
     249            $num_jobs++;
     250            next;
     251        }
     252        print STDERR "lookup bycoord is not yet implemented" if $verbose;
     253        insertFakeJobForRow($row, 1, $PSTAMP_NOT_IMPLEMENTED);
     254        $num_jobs++;
     255        next;
    193256    } else {
    194 
    195         # Call PS::IPP::PStamp::Job's locate_images routine to get the parameters for this
    196         # request specification. An array reference is returned
     257        # Call PS::IPP::PStamp::Job locate_images subroutine to get the images for this
     258        # request specification. An array reference is returned.
    197259        my ($x, $y);
    198         $imageList = locate_images($ipprc, $image_db, $req_type, $stage, $id, $component, $skycenter,
    199                 $x, $y, $mjd_min, $mjd_max, $filter, $verbose);
    200     }
    201 
    202     if (!$imageList or !@$imageList) {
    203         print STDERR "no matching images found for row $rownum\n";
    204         next;
    205     }
    206     $row->{need_magic} = $need_magic;
    207     push @rowList, $row;
     260
     261        $imageList = locate_images($ipprc, $image_db, $req_type, $stage, $id, $search_component,
     262                $inverse, $skycenter, $x, $y, $mjd_min, $mjd_max, $filter, $verbose);
     263
     264        if (!$imageList or !@$imageList) {
     265            print STDERR "no matching images found for row $rownum\n" if $verbose;
     266            # note in this case queueJobs inserts the fake job for these rows
     267        }
     268        $row->{need_magic} = $need_magic;
     269        push @rowList, $row;
     270    }
    208271}
    209272
     
    212275}
    213276
    214 if ($mode eq "queue_jobs") {
    215     if ($num_jobs) {
    216         exit 0;
    217     } else {
    218         # no jobs queued return error to get the request stopped
    219         # TODO: need to define meaningful error codes
    220         exit $PS_EXIT_UNKNOWN_ERROR;
    221     }
    222 } else {
    223     exit 0;
    224 }
     277if (($mode eq "queue_jobs") and ($num_jobs eq 0)) {
     278    print STDERR "no jobs created for $req_name\n" if $verbose;
     279    insertFakeJobForRow(undef, 0, $PSTAMP_UNKNOWN_ERROR);
     280}
     281
     282# PAU
     283
     284exit 0;
    225285
    226286
     
    232292    my $have_skycells = shift;
    233293    my $need_magic = shift;
     294
     295    my $num_jobs = 0;
     296    my $rownum = $row->{ROWNUM};
     297    my $components = $row->{components};
     298
     299    my $roi_string;
     300
     301    # note values were insured to be numbers in validROI()
     302    my $x = $row->{CENTER_X};
     303    my $y = $row->{CENTER_Y};
     304    my $w = $row->{WIDTH};
     305    my $h = $row->{HEIGHT};
     306    my $coord_mask = $row->{COORD_MASK};
     307    if ($x && ($x ne "null") && $y && ($y ne "null") && $w && ($w ne "null") && $h && ($h ne "null")) {
     308        if ($coord_mask & $PSTAMP_CENTER_IN_PIXELS) {
     309            $roi_string = "-pixcenter $x $y";
     310        } else {
     311            $roi_string = "-skycenter $x $y";
     312        }
     313        if ($coord_mask & $PSTAMP_RANGE_IN_PIXELS) {
     314            $roi_string .= " -pixrange $w $h";
     315        } else {
     316            $roi_string .= " -arcrange $w $h";
     317        }
     318    }
    234319
    235320    # loop over images
     
    244329
    245330        # skip this component if it is not in the list for this row
    246         next if !$row->{components}->{$component};
    247 
    248         my $rownum = $row->{ROWNUM};
    249 
    250         my $roi_string;
    251 
    252         # note values were insured to be numbers in validROI()
    253         my $x = $row->{CENTER_X};
    254         my $y = $row->{CENTER_Y};
    255         my $w = $row->{WIDTH};
    256         my $h = $row->{HEIGHT};
    257         my $coord_mask = $row->{COORD_MASK};
    258         if ($x && ($x ne "null") && $y && ($y ne "null") && $w && ($w ne "null") && $h && ($h ne "null")) {
    259             if ($coord_mask & $PSTAMP_CENTER_IN_PIXELS) {
    260                 $roi_string = "-pixcenter $x $y";
    261             } else {
    262                 $roi_string = "-skycenter $x $y";
    263             }
    264             if ($coord_mask & $PSTAMP_RANGE_IN_PIXELS) {
    265                 $roi_string .= " -pixrange $w $h";
    266             } else {
    267                 $roi_string .= " -arcrange $w $h";
    268             }
    269         }
     331        next if ! $components->{$component};
     332
     333        $job_num++;
    270334
    271335        my $imagefile = $image->{image};
    272336        if (($stage ne "stack") and ($need_magic and !$image->{magicked})) {
    273             # XXX: should we add a faulted job so the client can know what happened?
    274             print STDERR "skippping non-magicked image $imagefile\n" if $verbose;
     337            # XXX: should we add a faulted job so the client can know what happened if no images come back?
     338            print STDERR "skipping non-magicked image $imagefile\n" if $verbose;
     339
     340            # for now assume yes.
     341
     342            insertFakeJobForRow($row, $job_num, $PSTAMP_NOT_DESTREAKED);
     343            $num_jobs++;
     344
    275345            next;
    276346        }
     
    282352        }
    283353
    284         $job_num++;
    285 
    286354        # add astrometry file for raw and chip images if one is available
    287355        if (($stage eq "chip") || ($stage eq "raw")) {
     
    298366        }
    299367
    300         my $output_base = "$out_dir/${rownum}_${job_num}";
     368        my $base = basename($image->{path_base});
     369        # XXX: TODO use filerule for this. I don't have a camera defined here
     370        if (($stage eq 'chip') and ($image->{camera} eq 'GPC1')) {
     371            $base = "${base}.${component}";
     372        }
     373
     374        my $output_base = "$out_dir/${rownum}_${job_num}_${base}";
    301375        my $argslist = "${output_base}.args";
    302376
    303377        # copy the argument list to a file
    304         open ARGSLIST, ">$argslist" or die "failed to open $argslist";
     378        open ARGSLIST, ">$argslist" or my_die("failed to open $argslist", $PS_EXIT_UNKNOWN_ERROR);
    305379        print ARGSLIST "$args\n";
    306         close ARGSLIST or die "failed to close $argslist";
    307 
    308         # XXX: TODO: here is where we need to check whether or not the source inputs still exist
    309         # and if not, queue an update job and set the job state appropriately.
     380        close ARGSLIST or my_die("failed to close $argslist", $PS_EXIT_UNKNOWN_ERROR);
    310381
    311382        my $newState = "run";
     383        my $fault = 0;
     384
     385        if (($stage ne 'stack') and ($stage ne 'raw')) {
     386            if (($image->{state} eq 'goto_purged') or ($image->{data_state} eq 'purged')) {
     387                $newState = 'stop';
     388                $fault = $PSTAMP_GONE;
     389            } elsif (($image->{data_state} ne 'full') or ($image->{state} ne 'full' )) {
     390                # XXX here is where we need to queue an update job
     391                # for now just say that the image is not available
     392                $newState = 'stop';
     393                $fault = $PSTAMP_NOT_AVAILABLE;
     394            }
     395        }
    312396
    313397        $num_jobs++;
    314398        my $command = "$pstamptool -addjob  -req_id $req_id -job_type $row->{JOB_TYPE}"
    315399                        . " -outputBase $output_base -rownum $rownum -state $newState";
     400        $command .= " -fault $fault" if $fault;
    316401        $command .= " -exp_id $exp_id" if $exp_id;
    317402        $command .= " -dbname $dbname" if $dbname;
     
    331416                # should we keep going for other uris? If so how do we report that some
    332417                # of the work that the request wanted isn't going to get done
    333                 die "failed to queue job for request $req_id";
     418                my_die("failed to queue job for request $req_id", $PS_EXIT_UNKNOWN_ERROR);
    334419            }
    335420        } else {
     
    337422        }
    338423    }
     424    if ( $num_jobs == 0 ) {
     425        print STDERR "no jobs for row $rownum\n" if $verbose;
     426        insertFakeJobForRow($row, 1, $PSTAMP_NO_OVERLAP);
     427        $num_jobs = 1;
     428    }
    339429    return $num_jobs;
    340430}
    341431
     432# queue jobs for a collection of request specifications that have the same Images of Interest
    342433sub queueJobs
    343434{
     
    358449        }
    359450    } elsif ($job_type eq "get_image") {
    360         die "get_image jobs not implemented yet";
     451        print STDERR "get_image jobs not implemented yet" if $verbose;
     452        foreach my $row (@$rowList) {
     453            insertFakeJobForRow($row, 1, $PSTAMP_NOT_IMPLEMENTED);
     454            $num_jobs++;
     455        }
    361456    } else {
    362         my $thisRun;
    363 
    364         my ($pointsList, $pointsListName) = tempfile ("/tmp/pointsList.XXXX", UNLINK => !$save_temps);
    365         foreach my $row (@$rowList) {
    366             print $pointsList "$row->{ROWNUM} $row->{CENTER_X} $row->{CENTER_Y}\n";
    367             $row->{components} = {};
    368         }
    369         close $pointsList;
     457        if (!$imageList or (scalar @$imageList eq 0)) {
     458            # we didn't find any images for this set of rows. Insert a fake job to carry
     459            # the status back to the requestor
     460            foreach my $row (@$rowList) {
     461                insertFakeJobForRow($row, 1, $PSTAMP_NO_IMAGE_MATCH);
     462                $num_jobs++;
     463            }
     464            return $num_jobs;
     465        }
    370466
    371467        my $have_skycells;
     
    376472        }
    377473       
     474        my $thisRun;
     475
     476        my $npoints = 0;
     477        my ($pointsList, $pointsListName);
     478        if (scalar @$imageList > 1) {
     479            ($pointsList, $pointsListName) = tempfile ("/tmp/pointsList.XXXX", UNLINK => !$save_temps);
     480            foreach my $row (@$rowList) {
     481                $row->{components} = {};
     482                if ($row->{skycenter}) {
     483                    print $pointsList "$row->{ROWNUM} $row->{CENTER_X} $row->{CENTER_Y}\n";
     484                    $npoints++;
     485                } else {
     486                    # this row's center is in pixel coordinates add all images to the component list for this row
     487                    foreach my $i (@$imageList) {
     488                        my $component = $have_skycells ? $i->{skycell_id} : $i->{class_id};
     489                        my_die( "image found with no value for component", $PS_EXIT_UNKNOWN_ERROR)
     490                                if !$component;
     491                        $row->{components}->{$component} = 1;
     492                    }
     493                }
     494            }
     495            close $pointsList;
     496        } else {
     497            # only one image. Avoid the expense of dvoImagesAtCoords.
     498            # queue the job and let ppstamp figure out if the center is valid for the image
     499            # the resulting result code will be the same.
     500            foreach my $row (@$rowList) {
     501                $row->{components} = {};
     502                my $i = $imageList->[0];
     503                my $component = $have_skycells ? $i->{skycell_id} : $i->{class_id};
     504                my_die( "image found with no value for component", $PS_EXIT_UNKNOWN_ERROR) if !$component;
     505                $row->{components}->{$component} = 1;
     506            }
     507        }
     508
    378509        my $tess_dir_abs;
    379510        my $last_tess_id = "";
    380511        while ($thisRun = getOneRun($stage, $imageList)) {
    381             {
     512            if ($npoints) {
     513                my_die( "pointsListName is not defined", $PS_EXIT_PROG_ERROR) if !$pointsListName;
     514                # we collected a set of sky coordinates above.
     515                # filter the images so that only those that contain the centers of the ROIs are processed
    382516                my $command = "$dvoImagesAtCoords $pointsListName";
    383517                if ($have_skycells) {
     
    391525                } else {
    392526                    my $astrom = $thisRun->[0]->{astrom};
    393                     die "no astrometry file found" if !$astrom;
     527                    my_die( "no astrometry file found", $PS_EXIT_UNKNOWN_ERROR) if !$astrom;
    394528                    my $astrom_resolved = $ipprc->file_resolve($astrom);
    395529                    $command .= " -astrom $astrom_resolved";
     
    400534                    print STDERR @$stderr_buf;
    401535                    my $rc = $error_code >> 8;
    402                     die "dvoImagesAtCoords failed: $rc";
     536                    my_die( "dvoImagesAtCoords failed: $rc", $PS_EXIT_UNKNOWN_ERROR);
    403537                }
    404538                # now we have a list of row numbers and components
     
    410544                    my ($rownum, undef, undef, $component) = split " ", $line;
    411545
    412                     # I guess since we need this function we should be useing a hash for rowList
     546                    # I guess since we need this function we should be using a hash for rowList
    413547                    my $row = findRow($rownum, $rowList);
    414548                    $row->{components}->{$component} = 1;
     
    422556    }
    423557    return $num_jobs;
     558}
     559sub insertFakeJobForRow
     560{
     561    my $row = shift;
     562    my $job_num = shift;
     563    my $fault = shift;
     564
     565    my ($job_type, $rownum);
     566    if ($row) {
     567        $job_type = $row->{JOB_TYPE};
     568        $rownum = $row->{ROWNUM};
     569    } else {
     570        $job_type = "none";
     571        $rownum = 0;
     572    }
     573
     574    my $command = "$pstamptool -addjob  -req_id $req_id -job_type $job_type"
     575                        . " -rownum $rownum -state stop -fault $fault";
     576    $command .= " -dbname $dbname" if $dbname;
     577    $command .= " -dbserver $dbserver" if $dbserver;
     578
     579    if ($mode eq "list_job") {
     580        # this is a debugging mode, just print the pstamptool that would have run
     581        # this is sort of like the mode -noupdate that some other tools support
     582        print "$command\n";
     583    } elsif (!$no_update) {
     584        # mode eq "queue_job"
     585        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     586            run(command => $command, verbose => $verbose);
     587        unless ($success) {
     588            print STDERR @$stderr_buf;
     589            # XXX TODO: now what? Should we mark the error state for the request?
     590            # should we keep going for other uris? If so how do we report that some
     591            # of the work that the request wanted isn't going to get done
     592            my_die("failed to queue job for request $req_id", $PS_EXIT_UNKNOWN_ERROR);
     593        }
     594    } else {
     595        print "skipping command: $command\n";
     596    }
    424597}
    425598
     
    440613        return $image->{diff_id};
    441614    } else {
    442         die "unenexpected stage: $stage found";
     615        my_die("unenexpected stage: $stage found", $PS_EXIT_PROG_ERROR);
    443616    }
    444617}
     
    476649    return 0 if ($r1->{IMG_TYPE} ne $r2->{IMG_TYPE});
    477650    return 0 if ($r1->{ID} ne $r2->{ID});
     651    return 0 if ($r1->{inverse} ne $r2->{inverse});
    478652
    479653    if (defined($r1->{COMPONENT})) {
    480654        return 0 if !defined $r2->{COMPONENT} or ($r1->{COMPONENT} ne $r2->{COMPONENT});
    481655    } elsif (defined($r2->{COMPONENT})) {
     656        # if first row has no component all of the images will be retrieved, so
     657        # the fact that this row has a component is ok. Fall through to return 1
     658        # XXX Nope this doesn't work. It is consistent with the other logic in some way
     659        # that i haven't fully thought through
    482660        return 0;
    483661    }
     
    514692    return undef;
    515693}
     694
     695sub my_die
     696{
     697    my $msg = shift;
     698    my $fault = shift;
     699
     700    carp $msg;
     701
     702    # we don't fault the request here pstamp_parser_run.pl handles that if necessary
     703
     704    return $fault;
     705}
Note: See TracChangeset for help on using the changeset viewer.