IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 29, 2010, 3:55:49 PM (16 years ago)
Author:
eugene
Message:

update merges from trunk

Location:
branches/eam_branches/20100225
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20100225

  • branches/eam_branches/20100225/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm

    r27022 r27517  
    1616                    locate_images
    1717                    resolve_project
     18                    getCamRunByCamID
    1819                    );
    1920our %EXPORT_TAGS = (standard => [@EXPORT_OK]);
    2021
     22
     23use PS::IPP::PStamp::RequestFile qw( :standard );
    2124
    2225use IPC::Cmd 0.36 qw( can_run run );
     
    2427use PS::IPP::Config qw( :standard );
    2528use Carp;
     29
     30# caches of camProcessedExp objects.
     31# Key is $exp_id
     32my %camRunByExpIDCache;
     33# Key is $cam_id
     34my %camRunByCamIDCache;
    2635
    2736### my @images = locate_images($image_db, $req_type, $img_type, $id, $component,
     
    3645    my $tess_id  = shift;
    3746    my $component= shift;   # class_id or skycell_id
    38     my $inverse  = shift;
     47    my $option_mask  = shift;
    3948    my $need_magic = shift;
    4049    my $x        = shift;
     
    4352    my $mjd_max  = shift;
    4453    my $filter   = shift;
    45     my $data_group    = shift;
     54    my $data_group = shift;
    4655    my $verbose  = shift;
    4756
    48 
    4957    # we die in response to bad data in request files
     58    # The wrapper script is responsible for updating the database
    5059    die "Unknown req_type: $req_type"
    5160        if ($req_type ne "byid") and
     
    5564           ($req_type ne "byskycell");
    5665
     66
    5767    my $dateobs_begin;
    5868    my $dateobs_end;
     
    6575    if (isnull($tess_id)) {
    6676        $tess_id = undef;
     77    }
     78    if (isnull($data_group)) {
     79        $data_group = undef;
     80    }
     81    if (isnull($filter)) {
     82        $filter = undef;
     83    }
     84
     85    if ($req_type eq "bycoord") {
     86        my $results = lookup_bycoord($ipprc, $image_db, $img_type, $tess_id, $component, $need_magic, $x, $y, $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
     87        return $results;
    6788    }
    6889
     
    7293        # in one place
    7394        $req_type = "bydiff";
    74         my $results = lookup_diff($ipprc, $image_db, $id, $component, 1, $inverse, $img_type, $verbose);
     95        my $results = lookup_diff($ipprc, $image_db, $id, $component, 1, $option_mask, $img_type, $verbose);
    7596        return $results;
    7697    }
    7798
    7899    if ($req_type eq "bydiff") {
    79         my $results = lookup_diff($ipprc, $image_db, $id, $component, 0, $inverse, $img_type, $verbose);
     100        my $results = lookup_diff($ipprc, $image_db, $id, $component, 0, $option_mask, $img_type, $verbose);
    80101        if (!$results) {
    81102            return undef;
     
    113134            return undef;
    114135        }
    115 
    116     } elsif ($req_type eq "bycoord") {
    117 
    118         if ($img_type eq "stack") {
    119             print STDERR "Error bycoord lookup not implemented for stage stack yet\n";
    120             return undef;
    121         }
    122136    } elsif ($req_type eq "byskycell") {
    123137        if (($img_type eq "raw") or ($img_type eq "chip")) {
     
    131145    }
    132146
    133     my $results = lookup($ipprc, $image_db, $req_type, $img_type, $id, $tess_id, $component, $need_magic, $x, $y, $dateobs_begin, $dateobs_end, $filter, $data_group, $verbose);
     147    my $results = lookup($ipprc, $image_db, $req_type, $img_type, $id, $tess_id, $component, $need_magic,
     148        $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
    134149
    135150    return $results;
     
    146161    my $component= shift;
    147162    my $need_magic = shift;
    148     my $x        = shift;
    149     my $y        = shift;
    150163    my $dateobs_begin = shift;
    151164    my $dateobs_end   = shift;
    152165    my $filter = shift;
    153166    my $data_group = shift;
     167    my $option_mask = shift;
    154168    my $verbose  = shift;
     169
     170    my $inverse = $option_mask & $PSTAMP_SELECT_INVERSE;
     171    my $unconvolved = $option_mask & $PSTAMP_SELECT_UNCONV;
    155172
    156173    my $missing_tools;
     
    238255        $component_args = " -skycell_id $skycell_id" if $skycell_id;
    239256
    240         $image_name  = "PPSTACK.OUTPUT";
    241         $mask_name   = "PPSTACK.OUTPUT.MASK";
    242         $weight_name = "PPSTACK.OUTPUT.VARIANCE";
     257        if (!$unconvolved) {
     258            $image_name  = "PPSTACK.OUTPUT";
     259            $mask_name   = "PPSTACK.OUTPUT.MASK";
     260            $weight_name = "PPSTACK.OUTPUT.VARIANCE";
     261        } else {
     262            $image_name  = "PPSTACK.UNCONV";
     263            $mask_name   = "PPSTACK.UNCONV.MASK";
     264            $weight_name = "PPSTACK.UNCONV.VARIANCE";
     265        }
    243266        # this is wrong but gets the right answer. Need to figure out how to find the
    244267        # rule properly
     
    267290        die "tess_id and component are required for byskycell" if !$tess_id or ! $skycell_id;
    268291        $command .= " -tess_id $tess_id -skycell_id $skycell_id";
    269     } elsif ($req_type eq "bycoord") {
    270         $command .= " -radius 3.0 -ra $x -decl $y";
    271         if ($img_type ne "raw") {
    272             $command .= " -tess_id $tess_id" if $tess_id;
    273             $command .= " -skycell_id $skycell_id" if $skycell_id;
    274         }
    275292    } else {
    276293        die "Unknown req_type supplied: $req_type";
     
    351368        } elsif ($img_type eq "diff") {
    352369            $stage_id = $image->{diff_id};
     370            if ($inverse && $image->{bothways}) {
     371                $image_name  = "PPSUB.INVERSE";
     372                $mask_name   = "PPSUB.INVERSE.MASK";
     373                $weight_name = "PPSUB.INVERSE.VARIANCE";
     374                $cmf_name    = "PPSUB.INVERSE.SOURCES";
     375            }
    353376        } elsif ($img_type eq "stack") {
    354377            $stage_id = $image->{stack_id};
     
    379402    my $skycell_id = shift;
    380403    my $byid     = shift;
    381     my $inverse  = shift;
     404    my $option_mask  = shift;
    382405    my $img_type = shift;
    383406    my $verbose = shift;
     407
     408    my $inverse = $option_mask & $PSTAMP_SELECT_INVERSE;
    384409
    385410    my $missing_tools;
     
    436461        # so we interpret the requested image in that way
    437462
    438         my $stack_id = $image->{stack2};
    439463        # XXX difftool currently returns max long long for null
    440         # this line is ready if we switch the code to return zero for null
    441         if ($stack_id and ($stack_id != 9223372036854775807)) {
     464        # these checks are ready if we switch the code to return zero for null
     465        my $stack1 = $image->{stack1};
     466        if (($stack1 == 0) or ($stack1 == 9223372036854775807)) {
     467            $stack1 = undef
     468        }
     469        my $stack2 = $image->{stack2};
     470        if (($stack2 == 0) or ($stack2 == 9223372036854775807)) {
     471            $stack2 = undef
     472        }
     473        my $stack_id;
     474        if ($stack1 and $stack2) {
     475            # we have a stack - stack diff (well it might be stack - warp....)
     476            # but at any rate we only handle image type diff and stack
     477            if (($img_type ne "diff") and ($img_type ne "stack")) {
     478                print STDERR "lookup_diff: cannot lookup IMG_TYPE $img_type bydiff from a stack stack diff run\n";
     479                next;
     480            }
     481            # stack-stack diff
     482            # XXX: define another flag for this
     483            if (! $inverse) {
     484                $stack_id = $stack1;
     485            } else {
     486                $stack_id = $stack2;
     487            }
    442488            $image->{stack_id} = $stack_id;
    443         }
    444 
    445         my ($warp_id, $exp_id, $exp_name, $chip_id, $cam_id);
    446         if ($inverse and !$image->{bothways}) {
    447             print STDERR "Inverse images requested for diffRun that is not bothways. Ignoring.\n";
    448             $inverse = 0;
    449         }
    450         if ($inverse) {
    451             $warp_id =  $image->{warp2};
    452             $exp_id = $image->{exp_id_2};
    453             $exp_name = $image->{exp_name_2};
    454             $chip_id = $image->{chip_id_2};
    455             $cam_id = $image->{cam_id_2};
    456489        } else {
    457             $warp_id =  $image->{warp1};
    458             $exp_id = $image->{exp_id_1};
    459             $exp_name = $image->{exp_name_1};
    460             $chip_id = $image->{chip_id_1};
    461             $cam_id = $image->{cam_id_1};
    462         }
    463         # XXX difftool currently returns max long long for null
    464         # this line is ready if we switch the code to return zero for null
    465         if ($warp_id and ($warp_id != 9223372036854775807)) {
    466             $image->{warp_id} = $warp_id;
    467             $image->{exp_id} = $exp_id;
    468             $image->{exp_name} = $exp_name;
    469             $image->{chip_id} = $chip_id;
    470             $image->{cam_id} = $cam_id;
    471         } else {
    472             print STDERR "unexpected result warp_id not defined\n";
    473             next;
    474         }
     490            my ($warp_id, $exp_id, $exp_name, $chip_id, $cam_id);
     491            if ($inverse and !$image->{bothways}) {
     492                print STDERR "Inverse images requested for diffRun that is not bothways. Ignoring inverse.\n";
     493                $inverse = 0;
     494            }
     495            if ($inverse) {
     496                $warp_id =  $image->{warp2};
     497                $exp_id = $image->{exp_id_2};
     498                $exp_name = $image->{exp_name_2};
     499                $chip_id = $image->{chip_id_2};
     500                $cam_id = $image->{cam_id_2};
     501            } else {
     502                $warp_id =  $image->{warp1};
     503                $exp_id = $image->{exp_id_1};
     504                $exp_name = $image->{exp_name_1};
     505                $chip_id = $image->{chip_id_1};
     506                $cam_id = $image->{cam_id_1};
     507            }
     508            # XXX difftool currently returns max long long for null
     509            # this line is ready if we switch the code to return zero for null
     510            if ($warp_id and ($warp_id != 9223372036854775807)) {
     511                $image->{warp_id} = $warp_id;
     512                $image->{exp_id} = $exp_id;
     513                $image->{exp_name} = $exp_name;
     514                $image->{chip_id} = $chip_id;
     515                $image->{cam_id} = $cam_id;
     516            }
     517        }
     518
     519        # XXX: If difftool doesn't return a camera insert it here
     520        if (!$image->{camera}) {
     521            $image->{camera} = "GPC1";
     522            # lie about the magicked status since stack stack diffs don't require destreaking
     523            # we can remove this once diff_mode gets included
     524            $image->{magicked} = 42;
     525        }
     526
    475527        if ($img_type eq "diff") {
    476528            # the $image is going to be returned directly in this case so we need to duplicate
     
    482534                $image->{mask}   = $ipprc->filename($filerule_base . ".MASK", $image->{path_base});
    483535                $image->{weight} = $ipprc->filename($filerule_base . ".VARIANCE", $image->{path_base});
     536                $image->{cmf}    = $ipprc->filename($filerule_base . ".SOURCES", $image->{path_base});
     537                $image->{psf}    = $ipprc->filename("PSPHOT.PSF.SKY.SAVE", $image->{path_base});
     538                $image->{stage_id} = $image->{diff_id};
     539                $image->{stage}    = "diff";
     540                $image->{image_db} = $image_db;
     541                $image->{component} = $image->{skycell_id};
    484542            } else {
    485                 # XXX this will only happen if the minuend is not a warp
    486                 print STDERR "WARNING: cannot resolve camera so cannot get weight and mask images\n";
     543                # XXX this will only happen if the minuend is not a warp. See hack above
     544                print STDERR "WARNING: cannot resolve camera so cannot get resolve file rules\n";
    487545                next;
    488546            }
     
    494552
    495553sub lookup_bycoord {
    496     my $ipprc    = shift;
    497     my $image_db = shift;
    498     my $x        = shift;
    499     my $y        = shift;
     554    my $ipprc      = shift;
     555    my $image_db   = shift;
     556    my $img_type   = shift;
     557    my $tess_id    = shift;
     558    my $component  = shift;
     559    my $need_magic = shift;
     560    my $ra         = shift;
     561    my $dec        = shift;
    500562    my $dateobs_begin  = shift;
    501     my $dateobs_end  = shift;
    502     my $filter   = shift;
    503     my $verbose  = shift;
     563    my $dateobs_end    = shift;
     564    my $filter     = shift;
     565    my $data_group = shift;
     566    my $option_mask = shift;
     567    my $verbose    = shift;
    504568
    505569    my $missing_tools;
    506570    my $regtool = can_run('regtool') or (warn "Can't find regtool" and $missing_tools = 1);
     571    my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
     572    my $dvoImagesAtCoords = can_run('dvoImagesAtCoords') or (warn "Can't find dvoImagesAtCoords" and $missing_tools = 1);
     573    my $whichimage = can_run('whichimage') or (warn "Can't find whichimage" and $missing_tools = 1);
    507574    if ($missing_tools) {
    508575        warn("Can't find required tools.");
     
    510577    }
    511578
    512     # XXX TODO:
    513     # Full lookups by coordinate require looking at the DVO database. However,
    514     # dateobs and filter which is all that MOPS has asked for.
    515 
    516     my $args;
    517     if (!isnull($dateobs_begin)) {
    518         $args .= " -dateobs_begin $dateobs_begin";
    519     }
    520     if (!isnull($dateobs_end)) {
    521         $args .= " -dateobs_end $dateobs_end";
    522     }
    523     if (!isnull($filter)) {
    524         $args .= " -filter $filter";
    525     }
    526 
    527     if (!$args) {
    528         # avoid returning every exposure in the DB
    529         print STDERR "no query arguments provided for bycoord\n";
     579    my $results = ();
     580    if (($img_type eq "raw") or ($img_type eq "chip")) {
     581
     582        my $runs = lookup_runs_by_camid_and_coords($ipprc, $image_db, $img_type,
     583            $ra, $dec, $need_magic, $dateobs_begin, $dateobs_end, $filter, $data_group, $verbose);
     584
     585        foreach my $run (@$runs) {
     586            next if $component and ($run->{component} ne $component);
     587            my $these_results = lookup($ipprc, $image_db, "byid", $img_type, $run->{id},
     588                $tess_id, $run->{component}, $need_magic,
     589                $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
     590
     591            next if !$these_results;
     592            push @$results, @$these_results;
     593        }
     594       
     595    } else {
     596        # this should be checked elsewhere
     597        die "unexpected image type $img_type" if ($img_type ne "warp")
     598                                              and ($img_type ne "stack") and ($img_type ne "diff");
     599
     600        my $skycells = lookup_skycell_by_coords($ipprc, $tess_id, $component, $ra, $dec, $verbose);
     601
     602        foreach my $skycell (@$skycells) {
     603            my $these_results = lookup($ipprc, $image_db, "byskycell", $img_type, undef,
     604                $skycell->{tess_id}, $skycell->{component}, $need_magic,
     605                $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $verbose);
     606
     607            next if !$these_results;
     608            push @$results, @$these_results;
     609        }
     610    }
     611
     612    return $results;
     613}
     614
     615# lookup_runs_by_camid_and_coords
     616# given an ra, dec, and optionally other paramters, find camera runs for exposures
     617# that are within some distance of the coordinates
     618sub lookup_runs_by_camid_and_coords {
     619    my $ipprc      = shift;
     620    my $image_db   = shift;
     621    my $img_type   = shift;
     622    my $ra         = shift;
     623    my $dec        = shift;
     624    my $need_magic = shift;
     625    my $dateobs_begin  = shift;
     626    my $dateobs_end = shift;
     627    my $filter     = shift;
     628    my $data_group = shift;
     629    my $verbose    = shift;
     630
     631    my $camruns;
     632    my $missing_tools;
     633    my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
     634    my $dvoImagesAtCoords = can_run('dvoImagesAtCoords') or (warn "Can't find dvoImagesAtCoords" and $missing_tools = 1);
     635    if ($missing_tools) {
     636        warn("Can't find required tools.");
     637        exit ($PS_EXIT_CONFIG_ERROR);
     638    }
     639
     640    {
     641        my $command = "$camtool -dbname $image_db -processedexp";
     642           $command .= " -ra $ra -decl $dec -radius 1.6";
     643           $command .= " -dateobs_begin $dateobs_begin" if $dateobs_begin;
     644           $command .= " -dateobs_end   $dateobs_end"   if $dateobs_end  ;
     645           $command .= " -filter $filter" if $filter;
     646           # NOTE: we are applying the data_group to the camera run.
     647           # If we're looking for chip stage images there is no guarentee that
     648           # chipRun.data_group = camRun.data_group. In practice this is almost
     649           # always the case. If this turns out to be a problem we can defer
     650           # the data_group test to when we look up the chipProcessedImfiles
     651           $command .= " -data_group $data_group" if $data_group;
     652           $command .= " -destreaked" if $need_magic;
     653        # run the tool and parse the output
     654        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     655                    run(command => $command, verbose => $verbose);
     656        unless ($success) {
     657            print STDERR @$stderr_buf;
     658            return undef;
     659        }
     660        my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
     661
     662        my $output = join "", @$stdout_buf;
     663        if (!$output) {
     664            print STDERR "no output returned from $command\n" if $verbose;
     665            return undef;
     666        }
     667        $camruns = parse_md_fast($mdcParser, $output);
     668    }
     669    if (!$camruns) {
    530670        return undef;
    531671    }
    532 
    533     # XXX TODO: This query doesn't work Something appears to be out of
    534     # sync about the times I'm passing and what regtool and or the DB expect
    535     # the query I used in the C version of locateimages used
    536     # WHERE dateobs >= FROM_UNIXTIME(%ld) AND dateobs <= FROM_UNIXTIME(%ld + exp_time)
    537 
    538     my $command = "$regtool -processedexp -dbname $image_db $args";
    539 
    540     # run the tool and parse the output
    541     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    542                 run(command => $command, verbose => $verbose);
    543     unless ($success) {
    544         # not sure if we should die here
    545         print STDERR @$stderr_buf;
    546         return undef;
    547     }
    548     my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    549 
    550     my $output = join "", @$stdout_buf;
    551     if (!$output) {
    552         print STDERR "no output returned from $command\n" if $verbose;
    553         return undef;
    554     }
    555     my $images = parse_md_fast($mdcParser, $output) or die ("Unable to parse metadata config doc");
    556 
    557     return $images;
     672    my $runs;
     673    foreach my $camRun (@$camruns) {
     674        my $cam_id = $camRun->{cam_id};
     675
     676        updateCamRunCache($camRun);
     677
     678        next if $camRun->{quality};
     679        next if $camRun->{fault};
     680        # XXX Use file rule
     681        my $astrom = $camRun->{path_base} . ".smf";
     682        my $astrom_resolved = $ipprc->file_resolve($astrom);
     683        next if !$astrom_resolved;
     684
     685        my $command = "$dvoImagesAtCoords -astrom $astrom_resolved $ra $dec";
     686        # run the tool and parse the output
     687        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     688                    run(command => $command, verbose => $verbose);
     689        unless ($success) {
     690            my $result_code = $error_code >> 8;
     691            if ($result_code == $PSTAMP_NO_OVERLAP) {
     692                print STDERR "no overlap for $astrom\n" if $verbose;
     693                next;
     694            }
     695            # unexpected result code
     696            die "unexpected result code: $result_code from $command\n";
     697        }
     698
     699        my $output = join "", @$stdout_buf;
     700        if (!$output) {
     701            # this shouldn't happen when dvoImagesAtCoord exits with zero status
     702            die "no output returned from $command\n";
     703        }
     704
     705        # dvoImagesAtCoords should return a single line
     706        # rownum ra dec class_id
     707        my @lines = split "\n", $output;
     708        my $n = scalar @lines;
     709        if ($n != 1) {
     710            print STDERR "unexpected number of lines returned by dvoImagesAtCoords: $n\n";
     711
     712            # XXX: There is a bug in dvo where each component is listed twice
     713            # When that gets fixed remove the conditional and just die
     714            die "unexpected number lines returned by dvoImagesAtCoords: $n"
     715                if ($n != 2) or ($lines[0] ne $lines[1]);
     716        }
     717
     718        my (undef, $ra_out, $dec_out, $class_id) = split " ", $lines[0];
     719        if (!$class_id) {
     720            die "unexpected output from dvoImagesAtCoords: $lines[0]";
     721        }
     722        # build the hash to return
     723        my $run = {
     724            exp_id    => $camRun->{exp_id},
     725            exp_name  => $camRun->{exp_name},
     726            chip_id   => $camRun->{chip_id},
     727            cam_id    => $camRun->{cam_id},
     728            astrom    => $astrom_resolved,
     729            class_id  => $class_id,
     730            component => $class_id
     731        };
     732        if ($img_type eq "chip") {
     733            $run->{id} = $run->{chip_id};
     734        } else {
     735            $run->{id} = $run->{exp_id};
     736        }
     737        push @$runs, $run;
     738    }
     739
     740    return $runs;
     741}
     742
     743sub lookup_skycell_by_coords {
     744    my $ipprc      = shift;
     745    my $requested_tess_id    = shift;
     746    my $requested_skycell  = shift;
     747    my $ra         = shift;
     748    my $dec        = shift;
     749    my $verbose    = shift;
     750
     751    my $missing_tools;
     752    my $whichimage = can_run('whichimage') or (warn "Can't find whichimage" and $missing_tools = 1);
     753    if ($missing_tools) {
     754        warn("Can't find required tools.");
     755        exit ($PS_EXIT_CONFIG_ERROR);
     756    }
     757
     758    $requested_tess_id = "" if isnull($requested_tess_id);
     759    $requested_skycell = "" if isnull($requested_skycell);
     760
     761    my @lines;
     762    {
     763        my $command = "$whichimage $ra $dec";
     764        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     765                    run(command => $command, verbose => $verbose);
     766        unless ($success) {
     767            print STDERR @$stderr_buf;
     768            return undef;
     769        }
     770
     771        my $output = join "", @$stdout_buf;
     772        if (!$output) {
     773            print STDERR "no output returned from $command\n" if $verbose;
     774            return undef;
     775        }
     776        @lines = split "\n", $output;
     777        if (!scalar @lines) {
     778            # can this happen? if $output is not null?
     779            print STDERR "no output returned from $command\n" if $verbose;
     780            return undef;
     781        }
     782    }
     783    my $runs;
     784    foreach my $line (@lines) {
     785        my ($ra_out, $dec_out, $tess_id, $skycell_id) = split " ", $line;
     786        die "unexpected output from whichimage" if !$tess_id or !$skycell_id;
     787
     788        if ($requested_tess_id) {
     789            next if ($requested_tess_id ne $tess_id);
     790        } else {
     791            # skip these obsolete tesselations unless they were explicitly asked for
     792            # should I do this?
     793            next if $tess_id eq "FIXNS";
     794            next if $tess_id eq "ALLSKY";
     795        }
     796        next if $requested_skycell and ($skycell_id ne $requested_skycell);
     797
     798        # build the hash to return
     799        my $run = {
     800            tess_id   => $tess_id,
     801            component => $skycell_id,
     802        };
     803        push @$runs, $run;
     804    }
     805
     806    return $runs;
    558807}
    559808
     
    571820    my $verbose = shift;
    572821
     822    my $mdcParser;
     823
    573824    my $exp_id = $image->{exp_id};
    574825    if (($exp_id eq $last_exp_id) and $lastAstromFile) {
     
    587838    }
    588839
    589     my $command = "$camtool -dbname $image_db -processedexp -exp_id $exp_id";
    590     # run the tool and parse the output
    591     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    592                 run(command => $command, verbose => $verbose);
    593     unless ($success) {
    594         print STDERR @$stderr_buf;
    595         return undef;
    596     }
    597 
    598     my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    599 
    600     my $output = join "", @$stdout_buf;
    601     if (!$output) {
    602         print STDERR "no output returned from $command\n" if $verbose;
    603         return undef;
    604     }
    605     my $camruns = parse_md_fast($mdcParser, $output);
    606     if (!$camruns) {
    607         return undef;
    608     }
    609 
    610     # If there are multiple cam runs for this exposure, take the last completed one
    611     # on the assumption that it has the best astrometry.
    612     my $camdata;
    613     while ($camdata = pop @$camruns) {
    614         last if (($camdata->{quality} eq 0) and ($camdata->{fault} eq 0));
    615     }
    616     if (!$camdata) {
    617         # no cam runs for this exposure id therefore best astrometry is whatever is in the header
    618         return undef;
    619     }
    620     my $camRoot = $camdata->{path_base};
     840    my $camRun = getCamRunByExpID($exp_id);
     841    if (!$camRun) {
     842        my $command = "$camtool -dbname $image_db -processedexp -exp_id $exp_id";
     843        # run the tool and parse the output
     844        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     845                    run(command => $command, verbose => $verbose);
     846        unless ($success) {
     847            print STDERR @$stderr_buf;
     848            return undef;
     849        }
     850
     851        $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
     852
     853        my $output = join "", @$stdout_buf;
     854        if (!$output) {
     855            print STDERR "no output returned from $command\n" if $verbose;
     856            return undef;
     857        }
     858        my $camruns = parse_md_fast($mdcParser, $output);
     859        if (!$camruns) {
     860            return undef;
     861        }
     862
     863        # If there are multiple cam runs for this exposure, take the last completed one with good quality
     864        # on the assumption that it has the best astrometry.
     865        while ($camRun = pop @$camruns) {
     866            last if (($camRun->{quality} eq 0) and ($camRun->{fault} eq 0));
     867        }
     868        # XXX: this looks like a bug at least if ASTROM.SOURCE eq PSASTRO.OUTPUT
     869        if (!$camRun) {
     870            # no cam runs for this exposure id therefore best astrometry is whatever is in the header
     871            return undef;
     872        }
     873        updateCamRunCache($camRun);
     874    }
     875    my $camRoot = $camRun->{path_base};
    621876    my $camera = $image->{camera};
    622877    my $astromSource = $astromSources{$camera};
    623878    if (!$astromSource) {
    624        my $command = "$ppConfigDump -camera $camera -dump-recipe PSWARP -";
    625         my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    626             run(command => $command, verbose => $verbose);
    627         unless ($success) {
    628             $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    629             die("Unable to perform ppConfigDump: $error_code");
    630         }
    631         my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
    632             die("Unable to parse metadata config doc");
    633         $astromSource = metadataLookupStr($metadata, 'ASTROM.SOURCE');
     879        if ($camera eq "GPC1") {
     880            # CHEATER !
     881            $astromSource = "PSASTRO.OUTPUT";
     882        } else {
     883            my $command = "$ppConfigDump -camera $camera -dump-recipe PSWARP -";
     884            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     885                run(command => $command, verbose => $verbose);
     886            unless ($success) {
     887                $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     888                die("Unable to perform ppConfigDump: $error_code");
     889            }
     890            $mdcParser = PS::IPP::Metadata::Config->new if !$mdcParser; # Parser for metadata config files
     891            my $metadata = $mdcParser->parse(join "", @$stdout_buf) or
     892                die("Unable to parse metadata config doc");
     893            $astromSource = metadataLookupStr($metadata, 'ASTROM.SOURCE');
     894        }
    634895        $astromSources{$camera} = $astromSource;
    635896    }
    636897
     898    # XXX: Is this code correct if ASTROM.SOURCE ne "PSASTRO.OUTPUT"
    637899    my $astromFile = $ipprc->filename($astromSource, $camRoot);
    638900    if ($astromFile) {
     
    686948    my $val = shift;
    687949
    688     return (!defined($val) or (lc($val) eq "null"));
     950    return (!defined($val) or ($val eq "") or (lc($val) eq "null"));
    689951}
    690952
     
    7461008    return $last_project;
    7471009}
     1010
     1011sub updateCamRunCache {
     1012    my $camRun = shift;
     1013    die "updateCamRunCache: camRun is nil" if !$camRun;
     1014
     1015    my $exp_id = $camRun->{exp_id};
     1016    die "updateCamRunCache: exp_id is nil" if !$exp_id;
     1017
     1018    my $cam_id = $camRun->{cam_id};
     1019    die "updateCamRunCache: cam_id is nil" if !$cam_id;
     1020
     1021    $camRunByCamIDCache{$cam_id} = $camRun;
     1022
     1023    # if this camRun is newer than previous update the cache.
     1024    # This assumes that the newest has the "best" astrometry
     1025    my $previous = $camRunByExpIDCache{$exp_id};
     1026    if ($previous) {
     1027        my $previous_cam_id = $previous->{cam_id};
     1028        return if $cam_id < $previous_cam_id;
     1029    }
     1030
     1031    $camRunByExpIDCache{$exp_id} = $camRun;
     1032}
     1033
     1034sub getCamRunByExpID {
     1035    my $exp_id = shift;
     1036    die "getCamRun: exp_id is nil" if !$exp_id;
     1037
     1038    return $camRunByExpIDCache{$exp_id};
     1039}
     1040sub getCamRunByCamID {
     1041    my $cam_id = shift;
     1042    die "getCamRun: cam_id is nil" if !$cam_id;
     1043
     1044    return $camRunByCamIDCache{$cam_id};
     1045}
    74810461;
Note: See TracChangeset for help on using the changeset viewer.