IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 28, 2015, 8:28:32 PM (11 years ago)
Author:
eugene
Message:

merge changes from trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20150112/pstamp/scripts/pstampparse.pl

    r37838 r38052  
    1818use POSIX;
    1919use Time::HiRes qw(gettimeofday);
     20use Time::Local;
    2021
    2122my $verbose;
     
    117118my_die("REQ_NAME not found in $request_file_name\n", $PS_EXIT_PROG_ERROR)  if (!$req_name);
    118119my_die("wrong EXTVER $extver found in $request_file_name\n", $PS_EXIT_PROG_ERROR) if ($extver ne "1" and $extver ne "2");
     120
     121if ($extver eq "1") {
     122    print STDERR "WARNING WARNING WARNING\n";
     123    print STDERR "Very soon version 1 postage stamp requests will be rejected. Please update your request tables to version 2 format.\n";
     124    print STDERR "Note that EMAIL will not be optional\n";
     125
     126} elsif (!$email) {
     127    my_die("ERROR: Required parameter EMAIL not found in request file header.\n", $PSTAMP_INVALID_REQUEST)
     128}
     129   
    119130
    120131if ($extver >= 2) {
     
    260271my $num_jobs = 0;
    261272
     273my $accessLevel = getUsersAccessLevel($email, $label);
     274if ($accessLevel < 0) {
     275    print STDERR "Data access forbidden.\n";
     276    insertFakeJobForRow(undef, 0, $PSTAMP_NOT_AUTHORIZED);
     277    exit 0;
     278}
     279
    262280foreach my $row (@$rows) {
    263281
     
    271289    $row->{job_num} = 0;
    272290    $row->{error_code} = 0;
     291    $row->{accessLevel} = $accessLevel;
    273292
    274293    $num_jobs += processRow($action, $row);
     
    537556}
    538557
    539 sub processRows {
    540     my $action = shift;
    541     my $rowList = shift;
    542     my $num_jobs = 0;
    543 
    544     if ($action eq 'LIST') {
    545         # LIST is not allowed by caller. Can't get here.
    546         return list_targets($rowList);
    547     }
    548 
    549     # all rows in the list are compatible
    550     my $row = $rowList->[0];
    551 
    552     my $project  = $row->{PROJECT};
    553 
    554     # note: resolve_project avoids running pstamptool every time by remembering the
    555     # last project resolved
    556     my $proj_hash = resolve_project($ipprc, $project, $dbname, $dbserver);
    557     if (!$proj_hash) {
    558         foreach my $r (@$rowList) {
    559             insertFakeJobForRow($r, 1, $PSTAMP_UNKNOWN_PRODUCT);
    560             $num_jobs++;
    561         }
    562         return $num_jobs;
    563     }
    564     my $req_type  = $row->{REQ_TYPE};
    565     my $stage     = $row->{IMG_TYPE};
    566     my $id        = $row->{ID};
    567     my $component = $row->{COMPONENT};
    568     my $tess_id   = $row->{TESS_ID};
    569 
    570     my $filter    = $row->{REQFILT};
    571     my $mjd_min   = $row->{MJD_MIN};
    572     my $mjd_max   = $row->{MJD_MAX};
    573     my $data_group = $row->{DATA_GROUP};
    574 
    575     my $rownum     = $row->{ROWNUM};
    576     my $job_type   = $row->{JOB_TYPE};
    577     my $option_mask= $row->{OPTION_MASK};
    578    
    579     my $image_db   = $proj_hash->{dbname};
    580     my $camera     = $proj_hash->{camera};
    581     my $need_magic = $proj_hash->{need_magic};
    582     # Since user can get unmagicked data "by coordinate" requests can go back in time
    583     # to dredge unusable data from the "dark days"...
    584     if ($req_type eq 'bycoord' and $mjd_min eq 0) {
    585             # ... so unless the user sets mjd_min clamp it to 2009-04-01
    586             # XXX: This value should live in the pstampProject table not be hardcoded here
    587             $mjd_min = 54922;
    588     }
    589 
    590     $need_magic = 0 if $stage eq 'stack';
    591 
    592     # XXX: magic is dead
    593     $need_magic = 0;
    594 
    595     my $numRows = scalar @$rowList;
    596 
    597     if (($req_type eq 'byskycell') or ($req_type eq 'bycoord')) {
    598         # avoid error from print below if $id isn't needed
    599         $id = "" if !$id;
    600     }
    601     print "Collected $numRows rows beginning with row $rownum. $req_type $stage $id $tess_id $component\n";
    602    
    603     # Call PS::IPP::PStamp::Job locate_images subroutine to get the images for this
    604     # request specification. An array reference is returned.
    605     my $start_locate = gettimeofday();
    606 
    607     # XXX: perhaps we should get rid of most of this argument list.
    608     # Now that we are passing down compatible rows all of the
    609     # information required is contained there
    610 
    611     my $imageList = locate_images($ipprc, $image_db, $rowList, $req_type, $stage, $id, $tess_id, $component,
    612                 $option_mask, $need_magic, $mjd_min, $mjd_max, $filter, $data_group, $verbose);
    613 
    614     my $dtime_locate = gettimeofday() - $start_locate;
    615     print "Time to locate_images for row $rownum $dtime_locate\n";
    616 
    617     # handle this
    618     # XXX: what did I mean by that comment?
    619     $row->{need_magic} = $need_magic;
    620 
    621     $num_jobs += queueJobs($action, $rowList, $imageList);
    622 
    623     # if a row slipped through with no jobs add a faulted one
    624     foreach my $row (@$rowList) {
    625         if ($row->{job_num} == 0) {
    626             print "row $row->{ROWNUM} produced no jobs\n";
    627             print STDERR "row $row->{ROWNUM} produced no jobs\n";
    628             my $error_code = $row->{error_code};
    629             $error_code =  $PSTAMP_NO_IMAGE_MATCH if !$error_code;
    630             insertFakeJobForRow($row, ++$row->{job_num}, $error_code);
    631         }
    632     }
    633 
    634     return $num_jobs;
    635 }
    636 
    637558sub queueJobForImage
    638559{
     
    648569
    649570    $option_mask |= $PSTAMP_NO_WAIT_FOR_UPDATE if $no_updates_allowed;
     571
     572    # determine if there is a cut off date for data access for this user
     573    my $mjd_max;
     574    my $adjustedDateCuts = adjustDateCuts($row, \$mjd_max);
    650575
    651576    my $roi_string;
     
    692617
    693618    my $imagefile = $image->{image};
    694     if (($stage ne "stack") and ($need_magic and !$image->{magicked})) {
    695         # XXX: should we add a faulted job so the client can know what happened if no images come back?
    696         # The test for destreaked is made in locate_images now so this code never runs. This leads to no feedback
    697         # to users, but speeds up processing significantly
    698         print STDERR "skipping non-magicked image $imagefile\n" if $verbose;
    699 
    700         # for now assume yes.
    701 
    702         insertFakeJobForRow($row, $job_num, $PSTAMP_NOT_DESTREAKED);
    703         return 1;
    704     } elsif ($stage eq "stack") {
     619
     620    if ($stage eq "stack") {
    705621        # unconvolved stack images weren't available prior to some point in time.
    706622        # XXX: handle this more correctly by examining the stack run's config dump file.
     
    713629            insertFakeJobForRow($row, $job_num, $PSTAMP_NOT_AVAILABLE);
    714630            return 1;
     631        }
     632    } else {
     633        if ($need_magic and !$image->{magicked}) {
     634            # XXX: should we add a faulted job so the client can know what happened if no images come back?
     635            # The test for destreaked is made in locate_images now so this code never runs. This leads to no feedback
     636            # to users, but speeds up processing significantly
     637            print STDERR "skipping non-magicked image $imagefile\n" if $verbose;
     638
     639            # for now assume yes.
     640
     641            insertFakeJobForRow($row, $job_num, $PSTAMP_NOT_DESTREAKED);
     642            return 1;
     643        }
     644
     645        if ($adjustedDateCuts) {
     646            my $mjd = 0;
     647            my $exp_id;
     648            if ($stage eq 'diff') {
     649                # diff stage doesn't have a dateobs so we need to hack it.
     650                # Extract dateobs from the exp_name_1
     651                if ($row->{PROJECT} eq 'gpc1') {
     652                    my $exp_name = $image->{exp_name_1};
     653                    if ($exp_name ne 'NULL') {
     654                        $mjd = 50000 + (substr($exp_name, 1, 4));
     655                        $exp_id = $image->{exp_id_1};
     656                    } else {
     657                        # it's stack-stack diff just let it go with mjd = 0
     658                        # XXX: deal with this
     659                    }
     660                }
     661            } else {
     662                $exp_id = $image->{exp_id};
     663                $mjd = dateobsToMJD($image->{dateobs});
     664            }
     665
     666            if ($mjd > $mjd_max) {
     667                print STDERR "User is not authorized for stamps from this exposure.\n";
     668                insertFakeJobForRow($row, $job_num, $PSTAMP_NOT_AUTHORIZED, $exp_id);
     669                return 1;
     670            }
    715671        }
    716672    }
     
    943899    my $job_num = shift;
    944900    my $fault = shift;
     901    my $exp_id = shift;
    945902
    946903    my ($job_type, $rownum);
     
    965922    my $command = "$pstamptool -addjob  -req_id $req_id -job_type $job_type"
    966923                        . " -rownum $rownum -state stop -fault $fault";
     924    $command .= " -exp_id $exp_id" if $exp_id and $exp_id ne 'NULL';
    967925
    968926    if (!$no_update) {
     
    12201178}
    12211179
     1180# Prototype function for setting access level.
     1181# XXX: Implement something more flexible using the database.
     1182sub getUsersAccessLevel {
     1183    my $email = shift;
     1184    my $label = shift;
     1185
     1186    my $level = -1;
     1187
     1188    if (!$email) {
     1189        # No email provided. Eventually this will be forbidden.
     1190        # For now in order to temporarily continue to support the version 1 request format
     1191        # set the access rights based on the assigned label.
     1192        if ($label eq 'IFA' or $label eq 'QUB' or $label =~ 'MOPS') {
     1193            $level = 0;
     1194        } else {
     1195            # access rights PS1 data only
     1196            $level = 1;
     1197        }
     1198    } else {
     1199        # we've got an email check the domain
     1200        my ($user, $domain) = split '@', $email;
     1201        if ($domain) {
     1202            $domain = lc($domain);
     1203            if ($domain eq 'ifa.hawaii.edu' or $domain eq 'qub.ac.uk') {
     1204                $level = 0;
     1205            } else {
     1206                # XXX check for special users
     1207                # access rights PS1 data only
     1208                $level = 1;
     1209            }
     1210        } else {
     1211            print STDERR "Error: $email is not a valid email address\n";
     1212        }
     1213    }
     1214
     1215    return $level;
     1216}
     1217
     1218
     1219# determine the latest exposure date that a user may have access to
     1220# based on the user's accessLevel.
     1221sub adjustDateCuts {
     1222    my $row = shift;
     1223    my $r_mjd_max = shift;
     1224    my $adjusted = 0;
     1225
     1226    my $MJD_PS1_BEGIN = 54922;    # 2009-04-01
     1227    my $MJD_PS1_END   = 56838;    # 2014-06-30      XXX get the actual date
     1228
     1229    if ($row->{accessLevel} == 1) {
     1230        # User has access to PS1 data only
     1231        my $mjd_max_user = $row->{MJD_MAX};
     1232        if (!$mjd_max_user or $mjd_max_user > $MJD_PS1_END) {
     1233            $$r_mjd_max = $MJD_PS1_END;
     1234            $adjusted = 1;
     1235        }
     1236    }
     1237    return $adjusted;
     1238}
     1239
     1240sub dateobsToMJD {
     1241    my $dateobs = shift;
     1242
     1243    my ($date, $time) = split "T", $dateobs;
     1244
     1245    my ($year, $mon, $day) = split "-", $date;
     1246
     1247    my ($hr, $min, $sec) = split ":",   $time;
     1248    my $fraction = $sec - int($sec);
     1249    $sec = int($sec);
     1250
     1251    my $ticks = timegm($sec, $min, $hr, $day, $mon-1, $year-1900);
     1252    $ticks += $fraction;
     1253
     1254    # I'm not allowing for leap seconds here but for our purposes here that is ok
     1255    my $mjd = 40587.0 + ($ticks / 86400.);
     1256    return $mjd;
     1257}
     1258
    12221259sub my_die
    12231260{
Note: See TracChangeset for help on using the changeset viewer.