IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 30, 2009, 5:20:29 PM (17 years ago)
Author:
watersc1
Message:

Finished up my edits to the detrend cleanup, and some changes to my
copy of burntool and the pslib astrometry. Detrend cleanup has not
been tested yet. That's up next.

Location:
branches/czw_branch/cleanup
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/czw_branch/cleanup

  • branches/czw_branch/cleanup/pstamp/scripts/pstampparse.pl

    r23267 r24951  
    1313use PS::IPP::PStamp::RequestFile qw( :standard );
    1414use PS::IPP::PStamp::Job qw( :standard );
     15use File::Temp qw(tempfile);
    1516
    1617my $verbose;
    1718my $dbname;
     19my $dbserver;
    1820my $req_id;
    1921my $request_file_name;
     
    2123my $out_dir;
    2224my $product;
     25my $save_temps;
     26my $no_update;
    2327
    2428GetOptions(
     
    2933    'mode=s'    =>  \$mode,
    3034    'dbname=s'  =>  \$dbname,
     35    'dbserver=s'=>  \$dbserver,
    3136    'verbose'   =>  \$verbose,
     37    'save-temps'=>  \$save_temps,
     38    'no-update' =>  \$no_update,
    3239);
    3340
     
    5461my $pstamptool  = can_run('pstamptool')  or (warn "Can't find pstamptool"  and $missing_tools = 1);
    5562my $pstampdump  = can_run('pstampdump') or (warn "Can't find pstampdump" and $missing_tools = 1);
     63my $dvoImagesAtCoords  = can_run('dvoImagesAtCoords') or (warn "Can't find dvoImagesAtCoords" and $missing_tools = 1);
    5664my $fields  = can_run('fields') or (warn "Can't find fields" and $missing_tools = 1);
    5765
     
    6068    exit ($PS_EXIT_CONFIG_ERROR);
    6169}
     70
     71$no_update = 1 if $mode eq "list_job";
    6272
    6373my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
     
    8595die "wrong EXTVER $extver found in $request_file_name" if ($extver ne "1");
    8696
    87 if ($req_id) {
     97if ($req_id and !$no_update) {
    8898    my $command = "$pstamptool -updatereq -req_id $req_id  -name $req_name";
    8999    $command .= " -outProduct $product";
    90100    $command .= " -dbname $dbname" if $dbname;
     101    $command .= " -dbserver $dbserver" if $dbserver;
    91102    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    92103        run(command => $command, verbose => $verbose);
     
    114125}
    115126
     127my @rowList;
    116128my $num_jobs = 0;
     129my $imageList;
     130my $stage;
     131my $need_magic;
    117132foreach my $row (@$rows) {
    118     my $rownum   = $row->{ROWNUM};
    119     my $job_type = $row->{JOB_TYPE};
    120     my $project  = $row->{PROJECT};
    121     my $req_type = $row->{REQ_TYPE};
    122     my $img_type = $row->{IMG_TYPE};
    123     my $id       = $row->{ID};
    124     my $class_id = $row->{CLASS_ID};
    125     my $filter   = $row->{REQFILT};
    126     my $mjd_min = $row->{MJD_MIN};
    127     my $mjd_max = $row->{MJD_MAX};
    128     my $x = $row->{CENTER_X};
    129     my $y = $row->{CENTER_Y};
    130     my $w = $row->{WIDTH};
    131     my $h = $row->{HEIGHT};
    132     my $coord_mask = $row->{COORD_MASK};
    133     my $option_mask= $row->{OPTION_MASK};
    134 
    135     $class_id = "" if ($class_id eq "null" or $class_id eq "all");
    136    
    137133    # XXX: TODO: sanity check all parameters
    138134
     
    141137    # add a job with the proper fault code. If there is a db or config error we should probably just
    142138    # trash the request.
     139
     140    my $rownum   = $row->{ROWNUM};
     141    my $job_type = $row->{JOB_TYPE};
     142   
     143    # parameters that select the images of interest
     144    my $project  = $row->{PROJECT};
     145    my $req_type = $row->{REQ_TYPE};
     146    $stage = $row->{IMG_TYPE};
     147    my $id       = $row->{ID};
     148    my $class_id = $row->{CLASS_ID};
     149    my $component = $row->{COMPONENT};
     150    my $filter   = $row->{REQFILT};
     151    my $mjd_min = $row->{MJD_MIN};
     152    my $mjd_max = $row->{MJD_MAX};
     153    my $option_mask= $row->{OPTION_MASK};
     154
     155    die "region of interest is required to make postage stamps"
     156        if (($job_type eq "stamp") and ! validROI($row));
     157
     158    my $skycenter = $row->{skycenter} = ! ($row->{COORD_MASK} & $PSTAMP_CENTER_IN_PIXELS);
     159    $class_id = "" if (defined($class_id) and ($class_id eq "null" or $class_id eq "all"));
     160    $component = "" if (defined($component) and ($component eq "null" or $component eq "all"));
     161   
    143162    die "job_type is list_uri but mode is $mode" if ($job_type eq "list_uri") and ($mode ne "list_uri");
    144163
    145     my $proj_hash = resolve_project($ipprc, $project, $dbname);
     164
     165    # note: resolve_project remembers the last project returned so avoids running
     166    # pstamptool every time
     167    my $proj_hash = resolve_project($ipprc, $project, $dbname, $dbserver);
    146168    die "project $project not found\n" unless $proj_hash;
    147169
    148     my $image_db = $proj_hash->{dbname};
    149     my $camera = $proj_hash->{camera};
    150     my $need_magic = $proj_hash->{need_magic};
    151 
    152     my $roi_string;
    153     # XXX we're depending on other code to insure valid values for roi components
    154     # this is checked in and ppstamp but I should check here so that we don't get that far,
    155     # but not today
    156     if ($x && ($x ne "null") && $y && ($y ne "null") && $w && ($w ne "null") && $h && ($h ne "null")) {
    157         if ($coord_mask & $PSTAMP_CENTER_IN_PIXELS) {
    158             $roi_string = "-pixcenter $x $y";
     170    my $image_db   = $proj_hash->{dbname};
     171    my $camera     = $proj_hash->{camera};
     172    $need_magic = $proj_hash->{need_magic};
     173
     174    # collect rows with the same images of interest in a list so that they
     175    # can be processed optimially
     176    if (@rowList) {
     177        my $firstRow = $rowList[0];
     178        if ($skycenter and same_images_of_interest($row, $firstRow)) {
     179            push @rowList, $row;
     180            # On to the next row
     181            next;
    159182        } else {
    160             $roi_string = "-skycenter $x $y";
    161         }
    162         if ($coord_mask & $PSTAMP_RANGE_IN_PIXELS) {
    163             $roi_string .= " -pixrange $w $h";
    164         } else {
    165             $roi_string .= " -arcrange $w $h";
    166         }
    167     }
    168 
    169     die "region of interest is required to make postage stamps"
    170         if (($job_type eq "stamp") && !defined($roi_string));
    171 
    172     # find the uris for this request
     183            # Process the jobs for this set of rows
     184            $num_jobs += makeJobs($mode, $stage, $need_magic, \@rowList, $imageList);
     185            @rowList = ();
     186        }
     187    }
    173188
    174189    if ($req_type eq "bycoord") {
    175         die "region of interest is required for request type bycoord" if !defined($roi_string) ;
    176         die "center must be specified in sky coordintes for bycoord"
    177             if ($coord_mask & $PSTAMP_CENTER_IN_PIXELS);
    178     }
    179 
    180     # Call PS::IPP::PStamp::Job's locate_images routine to get the parameters for this request specification
    181     my $images = locate_images($ipprc, $image_db, $req_type, $img_type, $id, $class_id,
    182             $x, $y, $mjd_min, $mjd_max, $filter, $verbose);
    183 
    184     if (!$images) {
     190        die "center must be specified in sky coordintes for bycoord" if ( ! $skycenter);
     191        die "lookup bycoord is not yet implemented";
     192    } else {
     193
     194        # Call PS::IPP::PStamp::Job's locate_images routine to get the parameters for this
     195        # request specification. An array reference is returned
     196        my ($x, $y);
     197        $imageList = locate_images($ipprc, $image_db, $req_type, $stage, $id, $class_id, $skycenter,
     198                $x, $y, $mjd_min, $mjd_max, $filter, $verbose);
     199    }
     200
     201    if (!$imageList or !@$imageList) {
    185202        print STDERR "no matching images found for row $rownum\n";
    186203        next;
    187204    }
    188     if ($mode eq "list_uri") {
    189         foreach my $image (@$images) {
    190             print "$image->{image}\n";
    191         }
    192     } elsif ($job_type eq "get_image") {
    193         # XXX TODO: Get rid of this block and use the same code as the stamp jobs
    194         # XXX This doesn't work to get the mask and weight images
    195         my $listfile = "$out_dir/filelist";
    196 
    197 
    198         # map our img_type to the Data Store file types.
    199         my %filelist_img_types = ( "raw" => "chip",
    200                                    "chip" => "chipproc",
    201                                    "warp" => "warp",
    202                                    "stack"=>"stack",
    203                                    "diff" => "diff");
    204 
    205         my $filelist_img_type = $filelist_img_types{$img_type};
    206 
    207         open LISTFILE, ">$listfile"
    208             or die "failed to open file list: $listfile while parsing get_image request $req_id";
    209 
    210         foreach my $image (@$images) {
    211             my $class_id = $image->{class_id} ? $image->{class_id} : "";
    212             print LISTFILE "$image->{image}|$filelist_img_type|$class_id|\n";
    213         }
    214         close LISTFILE;
    215         $num_jobs++;
    216         my $command = "$pstamptool -addjob -req_id $req_id -job_type get_image"
    217                     . " -uri $listfile -outputBase $out_dir -rownum $rownum";
    218         $command .= " -dbname $dbname" if $dbname;
    219         if ($mode eq "list_job") {
    220             print "$command\n";
    221         } else {
    222             my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    223                 run(command => $command, verbose => $verbose);
    224             unless ($success) {
    225                 print STDERR @$stderr_buf;
    226                 # XXX TODO: now what? Should we mark the error state for the request?
    227                 die "failed to queue job for request $req_id: rownum: $rownum";
    228             }
    229         }
    230     } else {
    231         # sequence number for the the job for a request spec. Not to be confused with job_id
    232         my $job_num = 0;
    233 
    234         # XXX TODO: for raw and chip level images and class_id "null" if there are multiple images
    235         # use -list instead of -file
    236         foreach my $image (@$images) {
    237             my $uri = $image->{image};
    238             my $exp_id = $image->{exp_id};
    239            
    240             my $args = $roi_string ? $roi_string : "";
    241             $args .= " -class_id $class_id" if $class_id;
    242 
    243             $job_num++;
    244 
    245             my $output_base = "$out_dir/${rownum}_${job_num}";
    246 
    247             # add astrometry file for raw and chip images if one is available
    248             if (($img_type eq "chip") || ($img_type eq "raw")) {
    249                 $args .= " -astrom $image->{astrom}" if $image->{astrom};
    250             }
    251 
    252             if (($option_mask & $PSTAMP_SELECT_MASK) &&  $image->{mask} ) {
    253                 $args .= " -mask $image->{mask}";
    254             }
    255             if (($option_mask & $PSTAMP_SELECT_WEIGHT) and $image->{weight} ) {
    256                 $args .= " -weight $image->{weight}";
    257             }
    258 
    259             # XXX sounds like magic will be handled outside of the postage stamp server
    260             #if ($need_magic) {
    261             #    die "no magic mask available" if ! $image->{magic_mask};
    262             #    $args .= " -magic_mask $image->{magic_mask}"
    263             #}
    264 
    265             # XXX: TODO: here is where we need to check whether or not the source inputs still exist
    266             # and if not, queue a regeneration job and set the job state appropriately.
    267 
    268             my $state = "run";
    269 
    270             $num_jobs++;
    271             my $command = "$pstamptool -addjob -req_id $req_id -job_type $job_type"
    272                 . " -uri $uri -outputBase $output_base -args '$args' -rownum $rownum"
    273                 . " -state $state";
    274             $command .= " -exp_id $exp_id" if $exp_id;
    275             $command .= " -dbname $dbname" if $dbname;
    276 
    277             if ($mode eq "list_job") {
    278                 # this is a debugging mode, just print the pstamptool that would have run
    279                 # this is sort of like the mode -noupdate that some other tools support
    280                 print "$command\n";
    281             } else {
    282                 # mode eq "queue_job"
    283                 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    284                     run(command => $command, verbose => $verbose);
    285                 unless ($success) {
    286                     print STDERR @$stderr_buf;
    287                     # XXX TODO: now what? Should we mark the error state for the request?
    288                     # should we keep going for other uris? If so how do we report that some
    289                     # of the work that the request wanted isn't going to get done
    290                     die "failed to queue job for request $req_id";
    291                 }
    292             }
    293         }
    294     }
     205    push @rowList, $row;
     206}
     207
     208if (@rowList) {
     209    $num_jobs += makeJobs($mode, $stage, $need_magic, \@rowList, $imageList);
    295210}
    296211
     
    306221    exit 0;
    307222}
     223
     224
     225sub makeJobsForRow
     226{
     227    my $row = shift;
     228    my $imageList = shift;
     229    my $have_skycells = shift;
     230    my $need_magic = shift;
     231
     232    # loop over images
     233    my $job_num = 0;
     234    foreach my $image (@$imageList) {
     235        my $component;
     236        my $class_id;   # get rid of this use componet
     237        if ($have_skycells) {
     238            $component = $image->{skycell_id};
     239        } else {
     240            $component = $image->{class_id};
     241            $class_id = $component;
     242        }
     243
     244        # skip this component if it is not in the list for this row
     245        next if !$row->{components}->{$component};
     246
     247        my $rownum = $row->{ROWNUM};
     248
     249        my $roi_string;
     250
     251        # XXX we're depending on other code to insure valid values for roi components
     252        # this is checked in and ppstamp but I should check here so that we don't get that far,
     253        # but not today
     254        my $x = $row->{CENTER_X};
     255        my $y = $row->{CENTER_Y};
     256        my $w = $row->{WIDTH};
     257        my $h = $row->{HEIGHT};
     258        my $coord_mask = $row->{COORD_MASK};
     259        if ($x && ($x ne "null") && $y && ($y ne "null") && $w && ($w ne "null") && $h && ($h ne "null")) {
     260            if ($coord_mask & $PSTAMP_CENTER_IN_PIXELS) {
     261                $roi_string = "-pixcenter $x $y";
     262            } else {
     263                $roi_string = "-skycenter $x $y";
     264            }
     265            if ($coord_mask & $PSTAMP_RANGE_IN_PIXELS) {
     266                $roi_string .= " -pixrange $w $h";
     267            } else {
     268                $roi_string .= " -arcrange $w $h";
     269            }
     270        }
     271
     272        my $uri = $image->{image};
     273        if (($stage ne "stack") and ($need_magic and !$image->{magicked})) {
     274            print STDERR "skippping non-magicked image $uri\n" if $verbose;
     275            next;
     276        }
     277        my $exp_id = $image->{exp_id};
     278           
     279        my $args = $roi_string ? $roi_string : "";
     280        $args .= " -class_id $class_id" if $class_id;
     281
     282        $job_num++;
     283
     284        my $output_base = "$out_dir/${rownum}_${job_num}";
     285
     286        # add astrometry file for raw and chip images if one is available
     287        if (($stage eq "chip") || ($stage eq "raw")) {
     288            $args .= " -astrom $image->{astrom}" if $image->{astrom};
     289        }
     290
     291        if (($row->{OPTION_MASK} & $PSTAMP_SELECT_MASK) &&  $image->{mask} ) {
     292            $args .= " -mask $image->{mask}";
     293        }
     294        if (($row->{OPTION_MASK} & $PSTAMP_SELECT_WEIGHT) and $image->{weight} ) {
     295            $args .= " -weight $image->{weight}";
     296        }
     297
     298        # XXX: TODO: here is where we need to check whether or not the source inputs still exist
     299        # and if not, queue an update job and set the job state appropriately.
     300
     301        my $newState = "run";
     302
     303        $num_jobs++;
     304        my $command = "$pstamptool -addjob -req_id $req_id -job_type $row->{JOB_TYPE}"
     305            . " -uri $uri -outputBase $output_base -args '$args' -rownum $rownum"
     306            . " -state $newState";
     307        $command .= " -exp_id $exp_id" if $exp_id;
     308        $command .= " -dbname $dbname" if $dbname;
     309        $command .= " -dbserver $dbserver" if $dbserver;
     310
     311        if ($mode eq "list_job") {
     312            # this is a debugging mode, just print the pstamptool that would have run
     313            # this is sort of like the mode -noupdate that some other tools support
     314            print "$command\n";
     315        } else {
     316            # mode eq "queue_job"
     317            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     318                run(command => $command, verbose => $verbose);
     319            unless ($success) {
     320                print STDERR @$stderr_buf;
     321                # XXX TODO: now what? Should we mark the error state for the request?
     322                # should we keep going for other uris? If so how do we report that some
     323                # of the work that the request wanted isn't going to get done
     324                die "failed to queue job for request $req_id";
     325            }
     326        }
     327    }
     328    return $num_jobs;
     329}
     330
     331sub makeJobs
     332{
     333    my $mode = shift;
     334    my $stage = shift;
     335    my $need_magic = shift;
     336    my $rowList = shift;
     337    my $imageList = shift;
     338
     339    my $firstRow = $rowList[0];
     340    my $job_type = $firstRow->{JOB_TYPE};
     341
     342    my $num_jobs = 0;
     343
     344    if ($mode eq "list_uri") {
     345        foreach my $image (@$imageList) {
     346            print "$image->{image}\n";
     347        }
     348    } elsif ($job_type eq "get_image") {
     349        die "get_image jobs not implemented yet";
     350    } else {
     351        my $thisRun;
     352
     353        my ($pointsList, $pointsListName) = tempfile ("/tmp/pointsList.XXXX", UNLINK => !$save_temps);
     354        foreach my $row (@$rowList) {
     355            print $pointsList "$row->{ROWNUM} $row->{CENTER_X} $row->{CENTER_Y}\n";
     356            $row->{components} = {};
     357        }
     358        close $pointsList;
     359
     360        my $have_skycells;
     361        if (($stage eq "raw") or ($stage eq "chip")) {
     362            $have_skycells = 0;
     363        } else {
     364            $have_skycells = 1;
     365        }
     366       
     367        my $tess_dir_abs;
     368        my $last_tess_id = "";
     369        while ($thisRun = getOneRun($stage, $imageList)) {
     370            {
     371                my $command = "$dvoImagesAtCoords $pointsListName";
     372                if ($have_skycells) {
     373                    my $tess_id = $thisRun->[0]->{tess_id};
     374                    if ($tess_id ne $last_tess_id) {
     375                        $tess_dir_abs = $ipprc->tessellation_catdir( $tess_id );
     376                        $tess_dir_abs = $ipprc->convert_filename_absolute( $tess_dir_abs );
     377                        $last_tess_id = $tess_id;
     378                    }
     379                    $command .= " -D CATDIR $tess_dir_abs";
     380                } else {
     381                    my $astrom = $thisRun->[0]->{astrom};
     382                    die "no astrometry file found" if !$astrom;
     383                    $command .= " -astrom $astrom";
     384                }
     385                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     386                    run(command => $command, verbose => $verbose);
     387                unless ($success) {
     388                    print STDERR @$stderr_buf;
     389                    my $rc = $error_code >> 8;
     390                    die "dvoImagesAtCoords failed: $rc";
     391                }
     392                # now we have a list of row numbers and components
     393                # eventually we might want to multiple stamp requests for the same image
     394                # into the same ppstamp job but not yet. For now we will queue a new
     395                my @lines = split "\n", join "", @$stdout_buf;
     396                foreach my $line (@lines) {
     397                    # parse the line, ignoring the ra and dec
     398                    my ($rownum, undef, undef, $component) = split " ", $line;
     399
     400                    # I guess since we need this function we should be useing a hash for rowList
     401                    my $row = findRow($rownum, $rowList);
     402                    $row->{components}->{$component} = 1;
     403                }
     404            }
     405           
     406
     407                # XXX TODO: for raw and chip level images and class_id "null" if there are multiple images
     408                # use -list instead of -file
     409            foreach my $row (@$rowList) {
     410                $num_jobs += makeJobsForRow($row, $thisRun, $have_skycells, $need_magic);
     411            }
     412        }
     413    }
     414    return $num_jobs;
     415}
     416
     417sub get_run_id
     418{
     419    my $stage = shift;
     420    my $image = shift;
     421
     422    if ($stage eq "raw") {
     423        return $image->{exp_id};
     424    } elsif ($stage eq "chip") {
     425        return $image->{chip_id};
     426    } elsif ($stage eq "warp") {
     427        return $image->{warp_id};
     428    } elsif ($stage eq "stack") {
     429        return $image->{stack_id};
     430    } elsif ($stage eq "diff") {
     431        return $image->{diff_id};
     432    } else {
     433        die "unenexpected stage: $stage found";
     434    }
     435}
     436
     437# extract components from the imageList that have the same run id and return the list
     438sub getOneRun {
     439    my $stage = shift;
     440    my $imageList = shift;
     441
     442    # return if array is empty
     443    return undef if ! @$imageList;
     444
     445    my $last_run_id = 0;
     446    my @runList;
     447    while ($imageList->[0]) {
     448        my $run_id = get_run_id($stage, $imageList->[0]);
     449
     450        last if ($last_run_id and ($run_id ne $last_run_id));
     451
     452        my $image = shift @$imageList;
     453        push @runList, $image;
     454        $last_run_id = $run_id;
     455    }
     456    return \@runList;
     457}
     458
     459sub same_images_of_interest {
     460    my $r1 = shift;
     461    my $r2 = shift;
     462
     463    return 0 if ($r1->{project} ne $r2->{project});
     464    return 0 if ($r1->{job_type} ne $r2->{job_type});
     465    return 0 if ($r1->{req_type} ne $r2->{req_type});
     466    return 0 if ($r1->{img_type} ne $r2->{img_type});
     467    return 0 if ($r1->{id} ne $r2->{id});
     468#    XXX: turn this on when we change the request file format to change class_id into component
     469#    return false if ($r1->{component} ne $r2->{component});
     470
     471    return 1;
     472}
     473
     474sub validROI
     475{
     476    my $row = shift;
     477    return 0 if !defined $row->{CENTER_X};
     478    return 0 if !defined $row->{CENTER_Y};
     479    return 0 if !defined $row->{WIDTH};
     480    return 0 if !defined $row->{HEIGHT};
     481
     482    return 1;
     483}
     484
     485sub findRow
     486{
     487    my $rownum = shift;
     488    my $rowList = shift;
     489
     490    foreach my $row (@$rowList) {
     491        return $row if $row->{ROWNUM} eq $rownum;
     492    }
     493    return undef;
     494}
Note: See TracChangeset for help on using the changeset viewer.