IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 30, 2013, 4:55:55 PM (13 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/eam_branches/ipp-20130711
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20130711

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

    r35625 r36075  
    3434use POSIX;
    3535use Time::HiRes qw(gettimeofday);
     36use IO::Handle;
    3637
    3738my $dvo_verbose = 0;
     
    176177        $need_magic, $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $mjd_min, $mjd_max,
    177178        0, 0,   # fwhm cuts are not applied here
    178         undef, undef, undef, $verbose);
     179        undef, 0, # no cam run information
     180        undef, undef, $verbose);
    179181
    180182    return $results;
     
    243245    if (isnull($filter)) {
    244246        $filter = undef;
     247    }
     248
     249    if ($stage eq 'stack_summary') {
     250        # stack_summary jobs are so different from others that we lookup in a specialized function
     251        return lookup_stack_summary($ipprc, $row, $imagedb, $tess_id, $component, $filter, $data_group, $option_mask, $mjd_min, $mjd_max, $verbose);
    245252    }
    246253
     
    322329        0, $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $mjd_min, $mjd_max,
    323330        getValOrZero($row->{FWHM_MIN}), getValOrZero($row->{FWHM_MAX}),
    324         undef, $release_name, $survey,
     331        undef, 0, $release_name, $survey,
    325332        $verbose);
    326333
     
    351358    my $fwhm_max = shift;
    352359    my $selectedAstrom = shift;
     360    my $selected_cam_id = shift;
    353361    my $release_name = shift;
    354362    my $survey = shift;
     
    602610        # The image selectors are such that multiple runs my have be returned for the same exposure.
    603611        # Return only the latest one.
    604         $images = filterRuns($stage, $need_magic, $images, $inverse, $verbose);
     612        $images = filterRuns($stage, $choose_components, $need_magic, $images, $inverse, $verbose);
    605613    }
    606614
     
    699707                $out->{astrom} = $selectedAstrom;
    700708                $out->{cam_path_base} = $selectedAstrom;
     709                $out->{cam_id} = $selected_cam_id if $selected_cam_id;
    701710                if ($selectedAstrom =~ /\.smf$/) {
    702711                    $out->{cam_path_base} =~ s/\.smf$//;
     
    731740
    732741    return $output;
     742}
     743
     744sub lookup_stack_summary {
     745    my ($ipprc, $row, $imagedb, $tess_id, $component, $filter, $data_group, $option_mask, $mjd_min, $mjd_max, $verbose) = @_;
     746
     747    $row->{error_code} = $PSTAMP_NO_IMAGE_MATCH;
     748
     749    $component = '' if $component eq 'all';
     750
     751    my $req_type = $row->{REQ_TYPE};
     752    my $results;
     753    if ($req_type eq 'byid') {
     754        my $sass_id = $row->{ID};
     755        my $command = "$stacktool -dbname $imagedb -summary -sass_id $sass_id";
     756        $results = runToolAndParse($command, $verbose);
     757    } else {
     758        my ($release_name, $survey, $default_tess_id) = get_release_info($row);
     759        if (!$tess_id and $default_tess_id) {
     760            $tess_id = $default_tess_id;
     761        }
     762
     763        my $skycells;
     764        if ($req_type eq 'bycoord') {
     765            my $ra = $row->{CENTER_X};
     766            my $dec = $row->{CENTER_Y};
     767            $skycells = lookup_skycell_by_coords($ipprc, $tess_id, $component, $ra, $dec, $verbose);
     768        } elsif ($req_type eq 'byskycell') {
     769            if (!defined $tess_id or !defined $component) {
     770                print STDERR "Error: TESS_ID and  COMPONENT are required for REQ_TYPE byskycell\n";
     771                $row->{error_code} = $PSTAMP_INVALID_REQUEST;
     772            }
     773            my $skycell = {
     774                tess_id    => $tess_id,
     775                component => $component
     776            };
     777            push @$skycells, $skycell
     778        } else {
     779            print STDERR "Error: $req_type is not a valid REQ_TYPE for IMG_TYPE stack_summary\n";
     780            $row->{error_code} = $PSTAMP_INVALID_REQUEST;
     781            return undef;
     782        }
     783
     784        if ($skycells) {
     785            # We have a list of skycells. Find matching projection cells.
     786            # and then matching rows in stackSummary
     787
     788            my %projection_cells_found;
     789            foreach my $skycell (@$skycells) {
     790                my $tess_id = $skycell->{tess_id};
     791                my $skycell_id = $skycell->{component};
     792
     793                my $projection_cell = skycell_id_to_projection_cell($skycell_id);
     794
     795                # only need to include a projection cell once
     796                next if $projection_cells_found{$projection_cell};
     797
     798                $projection_cells_found{$projection_cell} = 1;
     799
     800                # print "$tess_id $skycell_id $projection_cell\n";
     801
     802                my $command;
     803                if ($release_name or $survey) {
     804                    $command = "$releasetool -dbname $imagedb";
     805                    if ($release_name) {
     806                        $command .= " -release_name $release_name";
     807                    } else {
     808                        $command .= " -priority_order";
     809                    }
     810                    $command .= " -surveyName $survey";
     811                } else {
     812                    $command = "$stacktool -dbname $imagedb";
     813                }
     814                $command .= " -summary -tess_id $tess_id";
     815                $command .= " -projection_cell $projection_cell";
     816
     817                $command .= " -filter $filter" if $filter;
     818                $command .= " -data_group $data_group" if $data_group;
     819
     820                my $these_results = runToolAndParse($command, $verbose);
     821                push @$results, @$these_results if $these_results;
     822            }
     823        }
     824    }
     825
     826    if ($results) {
     827        foreach my $summary (@$results) {
     828            my $path_base = $summary->{path_base};
     829            my $projection_cell = $summary->{projection_cell};
     830
     831            # we need to flesh out the hashes returned with values that the parser expects
     832            $summary->{stage} = 'stack_summary';
     833            $summary->{component} = $projection_cell;
     834            $summary->{stage_id} = $summary->{sass_id};
     835            $summary->{path_base} = "$path_base.$projection_cell";
     836
     837            # The stack_summary stage doesn't follow the usual ipp conventions for file rules
     838            # and path_base. We leave it up to the program that runs the job to handle this.
     839            # However, the parser uses the image parameter to set the job's outputBase (by removing the .fits)
     840            # so we need to set it. Just use the new path_base.
     841            $summary->{image} = $summary->{path_base};
     842            $summary->{state} = 'full';
     843            $summary->{data_state} = 'full';
     844            $summary->{fault} = 0;
     845            $summary->{imagedb} = $imagedb;
     846            $summary->{row_index} = [];
     847            push @{$summary->{row_index}}, 0;
     848
     849            # XXX: Are there any other required parameters?
     850            # TODO: create a function to validate the components returned
     851            # and make sure that any required elements are there.
     852        }
     853    }
     854
     855    return $results;
    733856}
    734857
     
    9461069                    $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $mjd_min, $mjd_max,
    9471070                    $fwhm_min, $fwhm_max,
    948                     $chip->{astrom}, undef, undef,
     1071                    $chip->{astrom}, $chip->{cam_id}, undef, undef,
    9491072                    $verbose);
    9501073
     
    9701093                    $dateobs_begin, $dateobs_end, $filter, $data_group, $option_mask, $mjd_min, $mjd_max,
    9711094                    $fwhm_min, $fwhm_max,
    972                     undef, $release_name, $survey,
     1095                    undef, 0, $release_name, $survey,
    9731096                    $verbose);
    9741097
     
    11661289
    11671290    my @lines;
    1168     {
     1291    my $looked_up_fast;
     1292    if ($requested_tess_id) {
     1293        # try using the last method first
     1294        $looked_up_fast = lookup_skycells_fast($ipprc, \@lines, $requested_tess_id, $ra, $dec, $verbose);
     1295    }
     1296   
     1297    if (!$looked_up_fast) {
    11691298        my $command = "$whichimage $ra $dec";
    11701299        $command .= " --tess_id $requested_tess_id" if $requested_tess_id;
     
    13021431    $image->{astrom} = $astromFile;
    13031432    $image->{cam_path_base} = $camRoot;
     1433    $image->{cam_id} = $camRun->{cam_id};
    13041434
    13051435    return 1;
     
    16491779sub filterRuns {
    16501780    my $stage      = shift;
     1781    my $drop_duplicate_runs = shift;
    16511782    my $need_magic = shift;
    16521783    my $inputs     = shift;
     
    16941825        }
    16951826
    1696         $printed = 1 if (($exp_id == $last_exp_id) and ($run_id == $last_run_id));
     1827        # $printed = 1 if (($exp_id == $last_exp_id) and ($run_id == $last_run_id));
    16971828
    16981829        # skip if we need magicked run and this one has never been magicked
     
    17021833        }
    17031834
    1704         if (($exp_id == $last_exp_id) and ($run_id != $last_run_id)) {
     1835        if (($exp_id == $last_exp_id) and ($drop_duplicate_runs || ($run_id != $last_run_id))) {
    17051836            print "Skipping duplicate ${stage}Run $run_id for $exp_id\n" if !$printed;
    17061837            next;
     
    18732004    return $diff_mode;
    18742005}
     2006
     2007sub setErrorCodesForRows {
     2008    my $rowList = shift;
     2009    my $error_code = shift;
     2010
     2011    foreach my $row (@$rowList) {
     2012        $row->{error_code} = $error_code;
     2013    }
     2014
     2015}
     2016
     2017# Convert from skycell_id to projection_cell
     2018# This code is not particularly elegant but I think that it matches the SQL used
     2019# to create stackAssocations does.
     2020
     2021sub skycell_id_to_projection_cell {
     2022    my $skycell_id = shift;
     2023
     2024    # split skycell_id into '.' separated components
     2025    my @strs = split '\.', $skycell_id;
     2026
     2027    my $num = scalar @strs;
     2028
     2029    # projection_cell is 'skycell_id' plus all .num values except the last one
     2030    my $projection_cell = $strs[0];
     2031    for (my $i=1; $i < $num - 1; $i++) {
     2032        $projection_cell .= ".$strs[$i]";
     2033    }
     2034
     2035    return $projection_cell;
     2036}
     2037
     2038# lookup_skycells_fast()
     2039# Lookup using a "skycell server" process which is implemented by running dvoImagesAtCoords in "server" mode.
     2040# Repeated calls to this function will reuse an existing process. This avoids having to loading the
     2041# tessellation over and over again for each skycell lookup.
     2042# When this parse process exists, the pipes are closed which causes the skycell server to exit.
     2043
     2044my $scs_tess_id;    # tess_id for existing scs (skycell server) (if any)
     2045my $scs_out;        # reference filehandle for sending data to the scs
     2046my $scs_in;         # filehandle for response from scs
     2047
     2048sub lookup_skycells_fast {
     2049    my ($ipprc, $results, $tess_id, $ra, $dec, $verbose) = @_;
     2050
     2051    # see if we need to start the scs (skycell server)
     2052
     2053    if (!$scs_tess_id or ($scs_tess_id ne $tess_id)) {
     2054        if ($scs_tess_id) {
     2055            # tess_id has changed, close down existing scs
     2056            close $scs_out;
     2057            $scs_out = undef;
     2058            close $scs_in;
     2059            $scs_in = undef;
     2060            $scs_tess_id = undef;
     2061        }
     2062
     2063        # convert tess_id to a directory name
     2064        my $tess_dir = $ipprc->tessellation_catdir( $tess_id );
     2065        unless ($tess_dir) {
     2066            print STDERR "Unrecognized tess_id: $tess_id\n";
     2067            return 0;
     2068        }
     2069
     2070        # convert tess_dir to an absolute directory name
     2071        my $tess_dir_resolved = $ipprc->convert_filename_absolute( $tess_dir );
     2072        unless ($tess_dir_resolved and -d $tess_dir_resolved) {
     2073            print STDERR "failed to resolve tessellation directory for $tess_id\n";
     2074            return 0;
     2075        }
     2076
     2077        # start an scs
     2078
     2079        # create pipes for communicating with the server
     2080        pipe $scs_in, SERVER_WRITER;
     2081        pipe SERVER_READER, $scs_out;
     2082
     2083        # set our output to be unbuffered
     2084        $scs_out->autoflush(1);
     2085
     2086        # FORK
     2087        my $scs_pid = fork();
     2088
     2089        if (!$scs_pid) {
     2090            unless (defined $scs_pid) {
     2091                # This is still in parent process. No joy.
     2092                print STDERR "fork of skycell server failed: $!";
     2093                return 0;
     2094            }
     2095
     2096            # This code is running in the child process - the skycell server
     2097            # Close file handles for the parent's ends of the pipes
     2098            close $scs_in;
     2099            close $scs_out;
     2100            # close stdio filehandles. we are about to redirect them
     2101            # (STDERR stays open pointing to the parser log)
     2102            close STDIN;
     2103            close STDOUT;
     2104
     2105            # redirect our stdio file handles to the proper end of the pipes
     2106            open (STDIN,  "<&SERVER_READER")   or die "\nSCS: failed to redirect stdin";
     2107            open (STDOUT, ">>&SERVER_WRITER")  or die "\nSCS: failed to redirect stdout";
     2108
     2109            # All set. Now exec dvoImagesAtCoords
     2110
     2111            my $command = "$dvoImagesAtCoords -D CATDIR $tess_dir_resolved -coords -";
     2112
     2113            print STDERR "SCS: execing $command\n";
     2114
     2115            unless(exec $command) {
     2116                # note parent will notice that we are gone due to the pipes getting broken
     2117                # when this child dies.
     2118                die "SCS: failed to exec $command";
     2119            }
     2120        }
     2121
     2122        # This code is running in the parent process (the parser).
     2123        # We have succesfully launched the scs process.
     2124        # Close file handles for the scs' end of the pipes.
     2125        close SERVER_WRITER;
     2126        close SERVER_READER;
     2127
     2128        # remember the tess_id
     2129        $scs_tess_id = $tess_id;
     2130    }
     2131
     2132    # send coordinates to the skycell server and wait for the results
     2133    # (which come nearly instantly which is the point of doing this)
     2134
     2135    # If pipes to the skycell server die, don't abort.
     2136    # Our reads and writes detect errors and act sensibly.
     2137    local $SIG{PIPE} = 'IGNORE';
     2138
     2139    # write the coordinates to the pipe
     2140    my $write_ok = print $scs_out "1 $ra $dec\n";
     2141    if (!$write_ok) {
     2142        # server process probably didn't start properly or died. Fail.
     2143        # Caller will attempt to use the conventional method.
     2144        print STDERR "write to skycell server failed. Error is $!\n";
     2145        return 0;
     2146    }
     2147
     2148    my $very_verbose = 0;
     2149    print STDERR "  sent coordinates to skycell server\n" if $very_verbose;
     2150
     2151    my $received_done = 0;
     2152
     2153    # Wait for response. If pipe breaks the read returns with no data.
     2154    while (my $line = <$scs_in>) {
     2155        chomp $line;
     2156        if ($line eq 'DONE') {
     2157            # No more results for these coordinates.
     2158            $received_done = 1;
     2159            print STDERR "    received DONE\n" if $very_verbose;
     2160            last;
     2161        }
     2162        print STDERR "    received $line\n" if $very_verbose;
     2163
     2164        # The caller expects the output to be in the format used by whichimage
     2165        # which omits the point number and includes the tess_id.
     2166        my ($ptnum, $raout, $decout, $skycell_id) = split " ", $line;
     2167
     2168        my $out = "$raout $decout $tess_id $skycell_id";
     2169
     2170        # print the result to the log
     2171        print "$out\n" if $verbose;
     2172
     2173        push @$results, $out;
     2174    }
     2175
     2176    print STDERR "Out of wait for responses loop\n" if $very_verbose;
     2177
     2178    if (!$received_done) {
     2179        # something has gone wrong.
     2180        print STDERR "Read loop exited without receiving DONE.\n";
     2181
     2182        die "BAILING out" if $very_verbose;
     2183
     2184        # forget about the existing server instance
     2185        $scs_tess_id = undef;
     2186        close $scs_out;
     2187        close $scs_in;
     2188
     2189        # drop any results received so far
     2190        @$results = ();
     2191        return 0;
     2192    }
     2193
     2194    return 1;
     2195}
     2196
    18752197       
    18762198
Note: See TracChangeset for help on using the changeset viewer.