IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:45:22 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simmosaic_branches
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/simmosaic_branches

  • branches/simmosaic_branches/pstamp/scripts/pstamp_finish.pl

    r24831 r27839  
    88use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
    99use Pod::Usage qw( pod2usage );
     10use Carp;
    1011
    1112use Time::Local;
     
    4748
    4849my $ipprc = PS::IPP::Config->new(); # IPP Configuration
    49 
    5050if ($redirect_output) {
    5151    my $logDest = "$out_dir/psfinish.$req_id.log";
     
    6363my $pstamp_results = can_run('pstamp_results_file.pl')
    6464                            or (warn "Can't find pstamp_results_file.pl" and $missing_tools = 1);
     65my $pstampdump = can_run('pstampdump') or (warn "Can't find pstampdump" and $missing_tools = 1);
    6566if ($missing_tools) {
    6667    warn("Can't find required tools.");
     
    6869}
    6970
     71if ($product eq "NULL") {
     72    stop_request_and_exit($req_id, $PS_EXIT_PROG_ERROR);
     73}
    7074
    7175my $outputDataStoreRoot = metadataLookupStr($ipprc->{_siteConfig}, 'DATA_STORE_ROOT');
     
    7882    my $fileset = $req_name;
    7983
    80 
    81     # Here we invoke the assumption that the output for the request is placed in the
    82     # fileset directory directly
    83 #    my $out_dir = "$outputDataStoreRoot/$product/$fileset";
    84 
    85     # now we are assuming that the output directory is the dirname of the request file
    86     # XXX: put this in the database
    87 
    8884    print STDERR "product: $product  REQ_NAME: $req_name $out_dir\n" if $verbose;
    8985
     
    9490        if (!mkdir $out_dir) {
    9591            print STDERR "cannot create output directory $out_dir";
    96             stop_request($req_id, $PS_EXIT_UNKNOWN_ERROR);
     92            stop_request_and_exit($req_id, $PS_EXIT_UNKNOWN_ERROR);
    9793        }
    9894
     
    10298        # request
    10399        print STDERR "output directory $out_dir exists but is not a directory";
    104         stop_request($req_id, $PS_EXIT_UNKNOWN_ERROR);
     100        stop_request_and_exit($req_id, $PS_EXIT_UNKNOWN_ERROR);
    105101    }
    106102
    107103    if (! -e $req_file ) {
    108104        print STDERR "request file $req_file is missing\n";
    109         stop_request($req_id, $PS_EXIT_CONFIG_ERROR);
     105        stop_request_and_exit($req_id, $PS_EXIT_CONFIG_ERROR);
    110106    }
    111107
     
    117113        # point
    118114        print STDERR "failed to read request_file $req_file" ;
    119         stop_request($req_id, $PS_EXIT_CONFIG_ERROR);
     115        stop_request_and_exit($req_id, $PS_EXIT_CONFIG_ERROR);
    120116    }
    121117
     
    128124    my ($rlf, $reglist_name) = tempfile ("$out_dir/reglist.XXXX", UNLINK => !$save_temps);
    129125    print $rlf "results.fits|||table|\n";
     126    print $rlf "results.mdc|||text|\n";
    130127
    131128    my $err_file = "parse_error.txt";
     
    154151                print STDERR "Request $req_id produced no jobs.\n"
    155152            }
    156             # No Jobs.
    157             # XXXX Ouch. We need results for each rownum (each request specification) in the request file
    158             # including those that produced no jobs.
    159             # for now add an entry for rownum 1 and a phony error code.
    160             # we've included parse_error.txt to the fileset if it exists
    161             #
    162             my $rownum = 0;
    163             my $fault = 42; # get a real error code
    164             print $tdf "$rownum|$fault|0|0|";
    165             print $tdf "0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|\n";
     153            # This should not happen. A fake job should be queued
     154            stop_request_and_exit($req_id, $PS_EXIT_PROG_ERROR);
    166155        } else {
    167156            my $metadata = $mdcParser->parse($output) or die("Unable to parse metdata config doc");
     
    173162    }
    174163
     164    my $exp_info;
     165    my $last_exp_id = -1;
    175166    foreach my $job (@jobs) {
    176167        my $job_id = $job->{job_id};
     
    180171        my $exp_id = $job->{exp_id};
    181172
     173
     174        if (($fault eq $PSTAMP_DUP_REQUEST) and ($req_name eq "NULL")) {
     175            # this request had a duplicate request name. We can't put the results
     176            # on the data store since the product name is already used
     177            # maybe we could be more clever about this and choose a fileset name
     178            stop_request_and_exit($req_id, $fault);
     179        }
    182180        my ($row, $req_info, $project) = get_request_info($rows, $rownum);
    183         my $proj_hash = resolve_project($ipprc, $project, $dbname);
     181
     182        my $proj_hash = resolve_project($ipprc, $project, $dbname, $dbserver);
    184183        my $image_db = $proj_hash->{dbname};
    185 
    186         # get the metadata for the exposure (if any i.e. stack)
    187         # returns an appropriate string if !$exp_id
    188         my $exp_info = get_exposure_info($image_db, $exp_id);
    189 
    190         if (($job_type eq "stamp") || ($job_type eq "get_image")) {
     184        if (!$image_db) {
     185            carp("failed to find imagedb for project: $project");
     186            if (!$fault) {
     187                stop_request_and_exit($req_id, $PS_EXIT_CONFIG_ERROR);
     188            }
     189        }
     190
     191        my $job_params = get_job_parameters($job);
     192        my $stage = "";
     193        if ($job_params) {
     194            $stage = $job_params->{stage};
     195        }
     196
     197        if ($stage ne 'stack') {
     198            # get the metadata for the exposure (if any i.e. stack)
     199            # returns an appropriate string if !$exp_id
     200            $exp_info = get_exposure_info($image_db, $exp_id);
     201            $last_exp_id = $exp_id;
     202        } else {
     203            my $filter = $job_params->{filter};
     204            $filter = "0" if !$filter;
     205            $exp_info = "0|0|0|$filter|0|0";
     206            $last_exp_id = -1;
     207        }
     208
     209        if (($job_type eq "stamp") || ($job_type eq "get_image") || ($job_type eq "none")) {
    191210            my $jreglist = "$out_dir/reglist$job_id";
    192211            if (open JRL, "<$jreglist") {;
     
    203222
    204223                    # ra_deg and dec_deg are the coordinates of center of the stamp
    205                     # XXX: parse the stamp header to find it
    206                     print $tdf "0.0|0.0|";
     224                    # first assume that the image is compressed and check the first extension.
     225                    # If not found check the PHU. If that doesn't work just set them to zero.
     226                    # XXX do this more cleanly
     227                    my (undef, $ra_deg, $dec_deg) = split " ", `echo $out_dir/$img_name | fields -x 0 RA_DEG DEC_DEG`;
     228                    if (!defined $ra_deg) {
     229                        (undef, $ra_deg, $dec_deg) = split " ", `echo $out_dir/$img_name | fields RA_DEG DEC_DEG`;
     230                    }
     231                    $ra_deg = 0.0 if (!$ra_deg);
     232                    $dec_deg = 0.0 if (!$dec_deg);
     233                    print $tdf "$ra_deg|$dec_deg|";
    207234
    208235                    print $tdf "$exp_info|";
     
    217244                print $tdf "$exp_info|";
    218245                print $tdf "$req_info|";
     246                print $tdf "\n";
    219247            }
    220248        } else {
     
    236264            print STDERR "Unable to perform $command error code: $error_code\n";
    237265            $request_fault = $error_code >> 8;
     266        } else {
     267            # dump a textual representation
     268            my $command = "$pstampdump $out_dir/results.fits > $out_dir/results.mdc";
     269            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     270                run(command => $command, verbose => $verbose);
     271            unless ($success) {
     272                print STDERR "Unable to perform $command error code: $error_code\n";
     273                $request_fault = $error_code >> 8;
     274            }
    238275        }
    239276    }
     
    241278        # register the fileset
    242279        my $command = "$dsreg --list $reglist_name --add $fileset --product $product --type PSRESULTS";
    243         $command .= " --link --datapath $out_dir";
     280        $command .= " --link --datapath $out_dir --ps0 $req_id";
    244281        $command .= " --dbname $dbname" if $dbname;
    245282
     
    267304}
    268305
    269 sub stop_request {
     306sub stop_request_and_exit {
    270307    my $req_id = shift;
    271308    my $fault  = shift;
     
    287324    my $rownum = shift;
    288325
     326    if ($rownum eq 0) {
     327        my $dummy_rowinfo = "0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|0|";
     328        return (undef, $dummy_rowinfo, "none");
     329    }
    289330    my $row = $rows->{$rownum};
     331
     332
     333    # these may be set to null during processing
     334    my $component = $row->{COMPONENT};
     335    $component = "null" if !$component;
     336    my $tess_id = $row->{TESS_ID};
     337    $tess_id = "null" if !$tess_id;
     338    my $comment = $row->{COMMENT};
     339    $comment = "null" if !$comment;
     340    my $data_group = $row->{DATA_GROUP};
     341    if (!defined $data_group) {
     342        # XXX: backwards compatibility hook. Remove "soon".
     343        $data_group = $row->{LABEL};
     344    }
     345    $data_group = "null" if !$data_group;
    290346
    291347    # This is ugly, error prone and hard to change.
    292348    # Create a results file module and provide a list of the names (we have the data in the columns)
    293349    my $rowinfo = "$row->{PROJECT}|$row->{JOB_TYPE}|$row->{REQ_TYPE}|$row->{IMG_TYPE}|";
    294     $rowinfo   .= "$row->{ID}|$row->{CLASS_ID}|$row->{OPTION_MASK}|$row->{MJD_MIN}|$row->{MJD_MAX}|";
     350    $rowinfo   .= "$row->{ID}|$tess_id|$component|$data_group|$row->{OPTION_MASK}|$row->{MJD_MIN}|$row->{MJD_MAX}|";
    295351    $rowinfo   .= "$row->{REQFILT}|$row->{COORD_MASK}|$row->{CENTER_X}|$row->{CENTER_Y}|";
    296     $rowinfo   .= "$row->{WIDTH}|$row->{HEIGHT}";
     352    $rowinfo   .= "$row->{WIDTH}|$row->{HEIGHT}|";
     353    $rowinfo   .= $comment;
    297354
    298355    return ($row, $rowinfo, $row->{PROJECT});
     356}
     357
     358sub get_job_parameters {
     359    my $job = shift;
     360    if (!$job->{outputBase}) {
     361        print "get_job_parameters: $job->{job_id} has no outputBase\n";
     362        return undef;
     363    }
     364    my $params_file = $job->{outputBase} . '.mdc';
     365    if (! -e $params_file ) {
     366        print "get_job_parameters: $job->{job_id} has no parameters file\n";
     367        return undef;
     368    }
     369    open IN, "<$params_file" or die "unable to open $params_file";
     370    my $data = join "", (<IN>);
     371    close IN;
     372    if (! $data ) {
     373        print "get_job_parameters: parameters file is empty\n";
     374        return undef;
     375    }
     376    my $metadata = $mdcParser->parse($data) or die("Unable to parse metdata config doc");
     377
     378    my $results = parse_md_list($metadata);
     379    if (scalar @$results != 1) {
     380        print STDERR "get_job_params: failed to parse_md_list\n";
     381        return undef;
     382    }
     383    return $results->[0];
    299384}
    300385
     
    303388    my $exp_id = shift;
    304389
    305     if (!$exp_id) {
     390    if (!$exp_id or !$image_db) {
    306391        # no exposure id just return zeros
    307392        # XXX: we could put a value in for filter, but we don't have a good place to find it
     
    347432    my $ra_deg   = $exp->{ra} * RADIANS_TO_DEGREES;
    348433    my $decl_deg = $exp->{decl} * RADIANS_TO_DEGREES;
    349     my $mjd_obs = dateobs_to_mjd($exp->{dateobs}, $exp->{exp_time});
     434    my $mjd_obs = dateobs_to_mjd($exp->{dateobs});
    350435
    351436    my $info = "$mjd_obs|$ra_deg|$decl_deg|$exp->{filter}|$exp->{exp_time}|$exp->{exp_name}";
     
    356441sub dateobs_to_mjd {
    357442    my $dateobs = shift;
    358     my $exp_time = shift;
    359443
    360444    # dateobs is in format: 1970-01-01T00:00:00
     
    366450    my $ticks = timegm($sec, $min, $hr, $day, $mon-1, $year-1900);
    367451
    368     $ticks += $exp_time / 2.0;
    369 
    370452    return 40587.0 + ($ticks/86400.);
    371453}
Note: See TracChangeset for help on using the changeset viewer.