IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 18, 2010, 5:06:01 PM (16 years ago)
Author:
watersc1
Message:

Almost working version of the mask-stats/software revision code.

Almost.

Location:
branches/czw_branch/20100427
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/czw_branch/20100427

  • branches/czw_branch/20100427/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm

    r27784 r28017  
    1717                    resolve_project
    1818                    getCamRunByCamID
     19                    parse_md_fast
    1920                    );
    2021our %EXPORT_TAGS = (standard => [@EXPORT_OK]);
     
    2728use PS::IPP::Config qw( :standard );
    2829use Carp;
    29 use DateTime;
    3030use File::Temp qw(tempfile);
    3131use File::Basename;
    3232use POSIX;
     33use Time::HiRes qw(gettimeofday);
    3334
    3435my $dvo_verbose = 0;
     
    169170
    170171# The subroutine lookup handles byexp, byid, and byskycell lookups including lookups that are
    171 # triggered by a bydiff request
    172 
    173 sub lookup
    174 {
     172# triggered by a bydiff or bycoord request
     173
     174sub lookup {
    175175    my $ipprc    = shift;
    176176    my $rowList    = shift;
     
    206206    my $class_id;
    207207    my $skycell_id;
    208 
    209     if (isnull($component)) {
    210         $component = undef;
    211     }
     208    my $default_error = $PSTAMP_NO_IMAGE_MATCH;
    212209
    213210    # note $magic_arg may be cleared below depending on $req_type
     
    220217    my $choose_components = 0;
    221218    if ($stage eq "raw") {
    222         $class_id = $component;
    223219        $command = "$regtool -processedimfile -dbname $imagedb";
    224220        # XXX: for now restrict lookups to type object
     
    226222        $command .= " -exp_type object";
    227223        $id_opt = $use_imfile_id ? "-raw_imfile_id" : "-exp_id";
    228         $component_args = " -class_id $class_id" if $component;
     224
     225        if ($component and $component ne 'all') {
     226            $class_id = $component;
     227            $component_args = " -class_id $class_id";
     228        }
    229229        $want_astrom = 1;
    230230        $set_class_id = 1;
    231231    } elsif ($stage eq "chip") {
    232         $class_id = $component;
    233232        # if the request is such that it will yield a single image per "run" or the
    234233        # center is specified in pixel coordinates
    235234        # use chiptool -processsedimfile. Otherwise use chiptool -listrun and then
    236235        # choose the chips containing the center pixel by calling selectComponets() below
    237         if ($class_id or $use_imfile_id or !$skycenter) {
     236        if ($component or $use_imfile_id or !$skycenter) {
    238237            $command = "$chiptool -processedimfile -dbname $imagedb";
    239             $component_args = " -class_id $class_id" if $class_id;
     238            if ($component and $component ne 'all') {
     239                $class_id = $component;
     240                $component_args = " -class_id $class_id";
     241            }
    240242        } else {
    241243            $command = "$chiptool -listrun -pstamp_order -dbname $imagedb";
     
    246248        # With the mdc file it has all of the information that it needs.
    247249        $image_name   = "PPIMAGE.CHIP";
    248         $mask_name    = "PPIMAGE.CHIP.MASK";
     250        # $mask_name    = "PPIMAGE.CHIP.MASK";
     251        # XXX: really need to handle this properly!
     252        $mask_name    = "PSASTRO.OUTPUT.MASK";
    249253        $weight_name  = "PPIMAGE.CHIP.VARIANCE";
    250254        $cmf_name     = "PSPHOT.OUTPUT";
     
    255259        $set_class_id = 1;
    256260    } elsif ($stage eq "warp") {
    257         $skycell_id = $component;
    258         if ($skycell_id or $use_imfile_id or !$skycenter) {
     261        if ($component or $use_imfile_id or !$skycenter) {
    259262            $command = "$warptool -warped -dbname $imagedb";
    260             $component_args = " -skycell_id $skycell_id" if $skycell_id;
     263            if ($component and $component ne 'all') {
     264                $skycell_id = $component;
     265                $component_args = " -skycell_id $skycell_id";
     266            }
    261267        } else {
    262268            $command = "$warptool -listrun -pstamp_order -dbname $imagedb";
     
    271277        $base_name    = "path_base"; # name of the field for the warptool output
    272278    } elsif ($stage eq "diff") {
    273         $skycell_id = $component;
    274         if ($skycell_id or $use_imfile_id or !$skycenter) {
     279        if ($component or $use_imfile_id or !$skycenter) {
    275280            $command = "$difftool -diffskyfile -dbname $imagedb";
    276             $component_args = " -skycell_id $skycell_id" if $skycell_id;
     281            if ($component and $component ne 'all') {
     282                $skycell_id = $component;
     283                $component_args = " -skycell_id $skycell_id";
     284            }
    277285        } else {
    278286            $command = "$difftool -listrun -pstamp_order -dbname $imagedb";
     
    284292        $weight_name = "PPSUB.OUTPUT.VARIANCE";
    285293        $cmf_name    = "PPSUB.OUTPUT.SOURCES";
    286         $psf_name     = "PSPHOT.PSF.SKY.SAVE";
     294        $psf_name    = "PSPHOT.PSF.SKY.SAVE";
    287295        $base_name   = "path_base";
    288296    } elsif ($stage eq "stack") {
     
    361369        $images = selectComponents($ipprc, $imagedb, $req_type, $stage, $rowList, $images, $verbose);
    362370    } else {
    363         # put each of the rows into all of the the images
     371        # put index for each of the rows into all of the the images
    364372        setRowRefs($rowList, $images);
    365373    }
     
    390398            $ipprc->define_camera($camera);
    391399        }
     400        # ok it's a keeper
    392401        my $out = $image;
    393402
    394403        # if uri is nil this will get overridded below
    395404        # (we do this here for raw stage)
    396         $out->{image}  = $image->{uri};
     405        $out->{image}  = $out->{uri};
    397406
    398407        if ($set_class_id) {
    399             $class_id = $image->{class_id};
     408            $class_id = $out->{class_id};
    400409            $out->{component} = $class_id;
    401410        } else {
    402             $out->{component} = $image->{skycell_id};
     411            $out->{component} = $out->{skycell_id};
    403412        }
    404413        my $stage_id;
    405414        if ($stage eq "raw") {
    406             $stage_id = $image->{exp_id};
     415            $stage_id = $out->{exp_id};
    407416        } elsif ($stage eq "chip") {
    408             $stage_id = $image->{chip_id};
     417            $stage_id = $out->{chip_id};
    409418        } elsif ($stage eq "warp") {
    410             $stage_id = $image->{warp_id};
     419            $stage_id = $out->{warp_id};
    411420        } elsif ($stage eq "diff") {
    412             $stage_id = $image->{diff_id};
    413             if ($inverse && $image->{bothways}) {
     421            $stage_id = $out->{diff_id};
     422            if ($inverse && $out->{bothways}) {
    414423                $image_name  = "PPSUB.INVERSE";
    415424                $mask_name   = "PPSUB.INVERSE.MASK";
     
    418427            }
    419428        } elsif ($stage eq "stack") {
    420             $stage_id = $image->{stack_id};
     429            $stage_id = $out->{stack_id};
    421430        }
    422431        $out->{stage_id} = $stage_id;
     
    424433        $out->{imagedb} = $imagedb;
    425434
     435        my $mask_base;
     436        if ($want_astrom) {
     437            if (! defined $out->{astrom}) {
     438                if (! find_astrometry($ipprc, $imagedb, $out, $verbose)) {
     439                    print STDERR "failed to find astrometry for $stage $stage_id\n";
     440                    next;
     441                }
     442            }
     443            # XXX: do this right by looking at the recipe
     444            $mask_base = $out->{cam_path_base};
     445        } else {
     446            $mask_base = $base;
     447        }
     448
    426449        if ($base) {
    427450            $out->{image}  = $ipprc->filename($image_name,  $base, $class_id) if $image_name;
    428             $out->{mask}   = $ipprc->filename($mask_name,   $base, $class_id) if $mask_name;
     451            $out->{mask}   = $ipprc->filename($mask_name,   $mask_base, $class_id) if $mask_name;
    429452            $out->{weight} = $ipprc->filename($weight_name, $base, $class_id) if $weight_name;
    430             $out->{cmf}    = $ipprc->filename($cmf_name, $base, $class_id) if $cmf_name;
    431             $out->{psf}    = $ipprc->filename($psf_name, $base, $class_id) if $psf_name;
    432             $out->{backmdl}= $ipprc->filename($backmdl_name, $base, $class_id) if $backmdl_name;
    433         }
    434         if ($want_astrom and ! defined $out->{astrom}) {
    435             $out->{astrom} = find_astrometry($ipprc, $imagedb, $image, $verbose);
     453            $out->{cmf}    = $ipprc->filename($cmf_name,    $base, $class_id) if $cmf_name;
     454            $out->{psf}    = $ipprc->filename($psf_name,    $base, $class_id) if $psf_name;
     455            $out->{backmdl}= $ipprc->filename($backmdl_name,$base, $class_id) if $backmdl_name;
    436456        }
    437457
     
    455475    my $inverse = $option_mask & $PSTAMP_SELECT_INVERSE;
    456476
    457     my $command = "$difftool -diffskyfile -dbname $imagedb";
     477    my $command = "$difftool -dbname $imagedb";
    458478   
     479    my $listrun = 0;
    459480    if ($byid) {
    460         $command .= " -diff_id $id";
     481        if ($skycell_id) {
     482            $command .= " -diffskyfile -diff_id $id -skycell_id $skycell_id";
     483        } else {
     484            $command .= " -listrun -diff_id $id";
     485            $listrun = 1;
     486        }
    461487    } else {
    462         $command .= " -diff_skyfile_id $id";
    463     }
    464     $command .= " -skycell_id $skycell_id" if $skycell_id;
    465 
    466     my $images = runToolAndParse($command, $verbose);
    467 
    468     my $n = @$images;
    469     if (!$byid && ($n > 1)) {
     488        $command .= " -diffskyfile -diff_skyfile_id $id";
     489    }
     490
     491    my $output = runToolAndParse($command, $verbose);
     492
     493    my $n = $output ? scalar @$output : 0;
     494    if (!$listrun && ($n > 1)) {
    470495        die ("difftool returned an unexpected number of diffskyfiles: $n");
    471496    } elsif ($n == 0) {
    472497        return undef;
     498    }
     499
     500    my $images;
     501    if ($listrun) {
     502        $images = selectComponents($ipprc, $imagedb, 'byid', 'diff', $rowList, $output, $verbose);
     503    } else {
     504        $images = $output;
    473505    }
    474506
     
    501533            if (($img_type ne "diff") and ($img_type ne "stack")) {
    502534                print STDERR "lookup_diff: cannot lookup IMG_TYPE $img_type bydiff from a stack stack diff run\n";
    503                 next;
     535                setErrorCodes($rowList, $PSTAMP_INVALID_REQUEST);
     536                # all images will be the same so we can stop
     537                last;
    504538            }
    505539            # stack-stack diff
     
    569603            } else {
    570604                # XXX this will only happen if the minuend is not a warp. See hack above
    571                 print STDERR "WARNING: cannot resolve camera so cannot get resolve file rules\n";
     605                print STDERR "WARNING: cannot resolve camera so cannot find file rules\n";
    572606                next;
    573607            }
     
    691725        next if !$astrom_resolved;
    692726
    693         my $start_dvo = DateTime->now->mjd;
     727        my $start_dvo = gettimeofday();
    694728        my $command = "$dvoImagesAtCoords -astrom $astrom_resolved $ra $dec";
    695729        # run the tool and parse the output
    696730        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    697731                    run(command => $command, verbose => $dvo_verbose);
    698         my $dtime_dvo = (DateTime->now->mjd - $start_dvo)* 86400.;
     732        my $dtime_dvo = gettimeofday() - $start_dvo;
    699733#        print "Time to run dvoImagesAtCoords: $dtime_dvo\n";
    700734        unless ($success) {
     
    720754        my $n = scalar @lines;
    721755        if ($n != 1) {
    722             print STDERR "unexpected number of lines returned by dvoImagesAtCoords: $n\n";
    723 
    724756            # XXX: There is a bug in dvo where each component is listed twice
    725757            # When that gets fixed remove the conditional and just die
    726             die "unexpected number lines returned by dvoImagesAtCoords: $n"
    727                 if ($n != 2) or ($lines[0] ne $lines[1]);
     758            if ($n ne 2 or $lines[0] ne $lines[1]) {
     759                print STDERR "unexpected number of lines returned by dvoImagesAtCoords: $n\n";
     760                print STDERR "OUTPUT:\n$output\n";
     761                # actually this seems to happen sometimes. Probably due to a problem with
     762                # astrometry. Just skip this camRun
     763                print STDERR "skipping: $astrom\n";
     764                next;
     765            }
    728766        }
    729767
     
    741779            chip_id   => $camRun->{chip_id},
    742780            cam_id    => $camRun->{cam_id},
    743             astrom    => $astrom_resolved,
     781            astrom    => $astrom_resolved,  # XXX: is astrom used?
    744782            class_id  => $class_id,
    745783            component => $class_id
     
    821859# cache of results of ppConfigDump
    822860my %astromSources;
    823 my $last_exp_id = 0;
    824 my $lastAstromFile;
    825861
    826862# find the astrometry file for a given exposure
    827863# return undef if no completed camRun exists
     864# XXX Right now this probably only works cameras where the astrometry is done at the camera stage
     865# What other possibilities are there for ASTROM.SOURCE besides PSASTRO.OUTPUT?
     866
    828867sub find_astrometry {
    829868    my $ipprc = shift;
     
    835874
    836875    my $exp_id = $image->{exp_id};
    837     if (($exp_id eq $last_exp_id) and $lastAstromFile) {
    838         # running camtool 60 times is really expensive when the answer is the same every time
    839         return $lastAstromFile;
    840     }
    841     $last_exp_id = 0;
    842     $lastAstromFile = undef;
    843876
    844877    my $camRun = getCamRunByExpID($exp_id);
     
    850883        unless ($success) {
    851884            print STDERR @$stderr_buf;
    852             return undef;
     885            return 0;
    853886        }
    854887
     
    858891        if (!$output) {
    859892            print STDERR "no output returned from $command\n" if $verbose;
    860             return undef;
     893            return 0;
    861894        }
    862895        my $camruns = parse_md_fast($mdcParser, $output);
    863896        if (!$camruns) {
    864             return undef;
     897            return 0;
    865898        }
    866899
    867900        # If there are multiple cam runs for this exposure, take the last completed one with good quality
    868901        # on the assumption that it has the best astrometry.
    869         foreach $camRun (@$camruns) {
    870             last if (($camRun->{state} eq 'full') and ($camRun->{quality} eq 0) and ($camRun->{fault} eq 0));
     902        foreach my $cr (@$camruns) {
     903            if (($cr->{state} eq 'full') and ($cr->{quality} eq 0) and ($cr->{fault} eq 0)) {
     904                $camRun = $cr;
     905                last;
     906            }
    871907        }
    872908        # XXX: this looks like a bug at least if ASTROM.SOURCE eq PSASTRO.OUTPUT
    873909        if (!$camRun) {
    874910            # no cam runs for this exposure id therefore best astrometry is whatever is in the header
    875             return undef;
     911            return 0;
    876912        }
    877913        updateCamRunCache($camRun);
     
    902938    # XXX: Is this code correct if ASTROM.SOURCE ne "PSASTRO.OUTPUT"
    903939    my $astromFile = $ipprc->filename($astromSource, $camRoot);
    904     if ($astromFile) {
    905         $lastAstromFile = $astromFile;
    906         $last_exp_id = $exp_id;
    907     }
    908 
    909     return $astromFile;
     940    if (!$astromFile) {
     941        print STDERR "failed to find astrometry file from $astromSource $camRoot\n";
     942        return 0;
     943    }
     944
     945    $image->{astrom} = $astromFile;
     946    $image->{cam_path_base} = $camRoot;
     947
     948    return 1;
    910949}
    911950
     
    9731012    }
    9741013
    975     if ($project_name eq $last_project) {
     1014    if ($last_project and ($project_name eq ($last_project->{name}))) {
    9761015        return $last_project;
    9771016    }
     
    10491088        print $pointsList "$npoints $row->{CENTER_X} $row->{CENTER_Y}\n";
    10501089        $npoints++;
     1090        # this gets overwitten if an overlapping image is found
     1091        $row->{error_code} = $PSTAMP_NO_OVERLAP;
    10511092    }
    10521093    close $pointsList;
     
    10931134                }
    10941135                push @$ref, $ptnum;
     1136                my $row = $rowList->[$ptnum];
     1137                # this row found a match
     1138                $row->{error_code} = 0;
    10951139            }
    10961140            if ($verbose) {
     
    11241168                my $images = runToolAndParse($command, $verbose);
    11251169                if (!defined $images) {
    1126                     # XXX: need to get the set error_code to $PSTAMP_NO_OVERLAP
    11271170                    print "No components containing coordinates found for ${stage}_id $stage_id\n";
    11281171                    foreach my $row (@$rowList) {
     1172                        # XXX: This doesn't seem like the correct error code?
    11291173                        $row->{error_code} = $PSTAMP_NO_OVERLAP;
    11301174                    }
     
    12211265
    12221266    print "Running $command\n" if !$verbose;
    1223     my $start_tool = DateTime->now->mjd;
     1267    my $start_tool = gettimeofday();
    12241268    # run the command and parse the output
    12251269
     
    12321276    }
    12331277
    1234     my $now = DateTime->now->mjd;
    1235     my $dtime_tool = (DateTime->now->mjd - $start_tool) * 86400.;
     1278    my $now = gettimeofday();
     1279    my $dtime_tool = $now - $start_tool;
    12361280    print "Time to run $program: $dtime_tool\n";
    12371281
     
    12411285    }
    12421286
    1243     my $start_parse = DateTime->now->mjd;
     1287    my $start_parse = gettimeofday();
    12441288
    12451289    my $mdcParser = PS::IPP::Metadata::Config->new;
     
    12471291        or die ("Unable to parse metadata config doc");
    12481292
    1249     my $dtime_parse = (DateTime->now->mjd - $start_parse) * 86400.;
     1293    my $dtime_parse = gettimeofday() - $start_parse;
    12501294    print "Time to parse results from $program: $dtime_parse\n";
    12511295
     
    12871331    }
    12881332}
     1333# set error_code for an array of rows to a given value
     1334sub setErrorCodes {
     1335    my $rowList = shift;
     1336    my $code = shift;
     1337    foreach my $row (@$rowList) {
     1338        $row->{error_code} = $code;
     1339    }
     1340}
    12891341
    12901342sub my_die
Note: See TracChangeset for help on using the changeset viewer.