IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 19, 2010, 6:50:13 PM (16 years ago)
Author:
eugene
Message:

updates from trunk

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20100621/pstamp/scripts/detect_query_read

    r28794 r28980  
    2020use Math::Trig;
    2121use Data::Dumper;
     22use IPC::Cmd 0.36 qw( can_run run );
    2223
    2324use constant EXTNAME => 'MOPS_DETECTABILITY_QUERY'; # Extension name for table
     
    3132     $output,                   # Name of output table
    3233     $save_temps,               # Save temporary files?
     34     $dbname,                   # needed to do camtool lookups.
    3335     );
     36
     37my $regtool = can_run('regtool') or (die "Can't find regtool");
     38my $camtool = can_run('camtool') or (die "Can't find regtool");
     39
    3440
    3541GetOptions(
    3642           'input|i=s'    => \$input,
    3743           'output|o=s'   => \$output,
     44           'dbname=s'     => \$dbname,
    3845           'nolabel|l'    => \$no_print_label,
    3946           'noheader|h'   => \$no_print_header,
     
    6572
    6673# The keywords found in the header
    67 # my $header = {
    68 #         'EXTVER'   => { name => 'EXTVER',
    69 #                      writetype => TSTRING,
    70 #                      comment => 'Extension version',
    71 #                      value => undef
    72 #                       },
    73 #         'EXTNAME'  => { name => 'EXTNAME',
    74 #                      writetype => TSTRING,
    75 #                      comment => 'name of this binary table extension',
    76 #                      value => undef
    77 #                     },
    78 #         'QUERY_ID' => { name => 'QUERY_ID',
    79 #                         writetype => TSTRING,
    80 #                         comment => 'MOPS Query ID for this batch query',
    81 #                         value => undef
    82 #                       },
    83 #         'FPA_ID'   => { name => 'FPA_ID',
    84 #                         writetype => TSTRING,
    85 #                         comment => 'orginal FPA_ID used at ingest',
    86 #                         value => undef
    87 #                       },
    88 #         'MJD_OBS'  => { name => 'MJD-OBS',
    89 #                         writetype => TDOUBLE,
    90 #                         comment => 'starting time of the exposure, MJD',
    91 #                         value => undef
    92 #                       },
    93 #         'FILTER'   => { name => 'FILTER',
    94 #                         writetype => TSTRING,
    95 #                         comment => 'effective filter use for the exposure',
    96 #                         value => undef
    97 #                       },
    98 #         'OBSCODE'  => { name => 'OBSCODE',
    99 #                         writetype => TSTRING,
    100 #                         comment => 'site identifier (MPC observatory code)',
    101 #                         value => undef
    102 #                       },
    103 #       'STAGE'    => {
    104 #                       name => 'STAGE',
    105 #                       writetype => TSTRING,
    106 #                       comment => 'processing stage to examine',
    107 #                       value => undef
    108 #                     }
    109 # };
    11074
    11175my $parse_error = 0;
    112 # Parse the header to determine what we expect to find.
     76# Parse the header to determine what we have to work with.
     77# Clean up empty space and remove unneeded single quotes.
    11378foreach my $header_key (keys %{ $inHeader }) {
    11479    $inHeader->{$header_key} =~ s/\s+//g;
    11580    $inHeader->{$header_key} =~ s/\'//g;
    11681}
    117 my ($EXTVER,$headerEXTNAME,$QUERY_ID,$FPA_ID,$MJD_OBS,$FILTER,$OBSCODE,$STAGE) =
    118     ($inHeader->{EXTVER},$inHeader->{EXTNAME},$inHeader->{QUERY_ID},$inHeader->{FPA_ID},
    119      $inHeader->{'MJD-OBS'},$inHeader->{FILTER},$inHeader->{OBSCODE},$inHeader->{STAGE});
     82my ($EXTVER,$headerEXTNAME,$QUERY_ID,
     83    $FPA_ID,$MJD_OBS,$FILTER,
     84    $OBSCODE,$STAGE) =
     85    ($inHeader->{EXTVER},$inHeader->{EXTNAME},$inHeader->{QUERY_ID},
     86     $inHeader->{FPA_ID},$inHeader->{'MJD-OBS'},$inHeader->{FILTER},
     87     $inHeader->{OBSCODE},$inHeader->{STAGE});
     88
    12089unless(defined($EXTVER) && defined($headerEXTNAME) &&
    12190       (($EXTVER == 1)||($EXTVER == 2)) &&
    122        ($headerEXTNAME eq EXTNAME)) {
     91       ($headerEXTNAME eq EXTNAME)&&
     92       (defined($QUERY_ID))) {
    12393    $parse_error = 1;
    12494}
     
    143113        # apparent magnitude
    144114        { name => 'MAG',      type => 'D',   writetype => TDOUBLE, version => 1 },
    145     # v2 query_id: MOPS query ID for this batch query
     115    # v2 query_id: needs to be here.
    146116    { name => 'QUERY_ID', type => '20A', writetype => TSTRING, version => 2, default => $QUERY_ID },
    147117    # v2 fpa_id: original FPA_ID used at ingest
     
    172142
    173143    if ($col->{version} > $EXTVER) {
    174         @{ $colData{$col->{name}} } = map { $col->{default} } (0 .. $numRows);
     144        @{ $colData{$col->{name}} } = map { $col->{default} } (0 .. $numRows - 1);
    175145        next;
    176146    }
    177     $inFits->get_colnum(0, $col->{name}, $col_num, $status) and check_fitsio($status);
    178     $inFits->get_coltype($col_num, $col_type, undef, undef, $status) and check_fitsio($status);
    179     $inFits->read_col($col_type, $col_num, 1, 1, $numRows, 0, $col_data, undef, $status)
    180                                                                     and check_fitsio($status);
    181     $colData{$col->{name}} = $col_data;
    182     if ($col->{name} eq 'MJD-OBS') {
    183         print @{ $col_data } . "\n";
     147    else {
     148        if (defined($col->{default})) {
     149            @{ $colData{$col->{name}} } = map { $col->{default} } (0 .. $numRows - 1);
     150            next;
     151        }       
     152    }   
     153
     154    $inFits->get_colnum(0, $col->{name}, $col_num, $status);
     155    if ($status == 0) {
     156        $inFits->get_coltype($col_num, $col_type, undef, undef, $status) and check_fitsio($status);
     157        $inFits->read_col($col_type, $col_num, 1, 1, $numRows, 0, $col_data, undef, $status)
     158            and check_fitsio($status);
     159        $colData{$col->{name}} = $col_data;
     160        if ($col->{name} eq 'QUERY_ID') {
     161            print @{ $col_data } . "\n";
     162        }
     163
     164    }
     165    elsif ($status == 219) {
     166        @{ $colData{$col->{name}} } = map { "Not Set" } (0 .. $numRows - 1);
     167        $status = 0;
     168    }
     169}
     170
     171# Set things that aren't set.
     172for (my $i = 0; $i < $numRows; $i++) {
     173# Simple stuff first.
     174    my %known_filters = ();
     175    if ($colData{STAGE}[$i] eq "Not Set") {
     176        $colData{STAGE}[$i] = 'diff';
     177    }
     178    if ($colData{OBSCODE}[$i] eq "Not Set") {
     179        $colData{OBSCODE}[$i] = 566;
     180    }
     181# Define filter and MJD from FPA_ID
     182    if (($colData{FILTER}[$i] eq "Not Set")&&($colData{FPA_ID}[$i] ne "Not Set")) {
     183        if (exists($known_filters{$colData{FPA_ID}[$i]})) {
     184            $colData{FILTER}[$i] = $known_filters{$colData{FPA_ID}[$i]};
     185        }
     186        else {
     187            my $cmd = "$regtool -processedexp -dbname $dbname -exp_name $colData{FPA_ID}[$i]";
     188            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     189                run(command => $cmd, verbose => 0);
     190            unless ($success) {
     191                # This is a problem, because I'm not sure how we handle a failure to read something.
     192                # We need to return a $PSTAMP_INVALID_REQUEST, I think, but if we can't read it,
     193                # we can't send that response back.
     194                die("Unable to perform $cmd error code: $error_code");
     195            }
     196            foreach my $entry (split /\n/, (join "", @$full_buf)) {
     197                $entry =~ s/^\s+//;
     198                my @line = split /\s+/, $entry;
     199                if (scalar(@line) != 3) {
     200                    next;
     201                }
     202                my ($key,$type,$value) = @line;
     203#               print "$entry => $key / $type / $value \n";
     204                if ($key =~ /filter/i) {
     205                    $value =~ s/.00000//;
     206                    $colData{FILTER}[$i] = $value;
     207                    $known_filters{$colData{FPA_ID}[$i]} = $value;
     208                }
     209            }
     210        }
     211    }
     212    if (($colData{'MJD-OBS'}[$i] eq "Not Set")&&($colData{FPA_ID}[$i] ne "Not Set")) {
     213        # HACK!
     214        my $mjd = $colData{FPA_ID}[$i];
     215        $mjd =~ s/o(....)g.*/$1/;
     216        $mjd += 50000;
     217        $colData{'MJD-OBS'}[$i] = $mjd;
     218    }
     219    if (($colData{FPA_ID}[$i] eq "Not Set")&&(($colData{'FILTER'}[$i] ne "Not Set")&&
     220                                              ($colData{'MJD-OBS'}[$i] ne "Not Set"))) {
     221        my $dateobs_begin = mjd_to_dateobs($colData{'MJD-OBS'}[$i]);
     222        my $dateobs_end = mjd_to_dateobs($colData{'MJD-OBS'}[$i] + 1);
     223        my $ra = $colData{'RA1_DEG'}[$i];
     224        my $dec = $colData{'DEC1_DEG'}[$i];
     225        my $filter = $colData{'FILTER'}[$i] . ".00000";
     226        my $cmd = "$camtool -processedexp -dbname $dbname -filter $filter ";
     227        $cmd .= " -dateobs_begin $dateobs_begin -dateobs_end $dateobs_end ";
     228        $cmd .= " -ra $ra -decl $dec -radius 1.5 -limit 1";
     229        my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     230            run(command => $cmd, verbose => 0);
     231        unless ($success) {
     232            # This is a problem, because I'm not sure how we handle a failure to read something.
     233            # We need to return a $PSTAMP_INVALID_REQUEST, I think, but if we can't read it,
     234            # we can't send that response back.
     235            die("Unable to perform $cmd error code: $error_code");
     236        }
     237        foreach my $entry (split /\n/, (join "", @$full_buf)) {
     238            $entry =~ s/^\s+//;
     239            my @line = split /\s+/, $entry;
     240            if (scalar(@line) != 3) {
     241                next;
     242            }
     243            my ($key,$type,$value) = @line;
     244#               print "$entry => $key / $type / $value \n";
     245            if ($key =~ /exp_name/i) {
     246                $colData{FPA_ID}[$i] = $value;
     247            }
     248        }
    184249    }
    185250}
     
    189254foreach my $colName (@unique_fields) {
    190255    my %counter = ();
     256    my $i = 0;
    191257    foreach my $row (@{ $colData{$colName} }) {
    192258        $counter{$row} = 1;
     
    232298        my $msg;                # Message to output
    233299        Astro::FITS::CFITSIO::fits_get_errstatus( $status , $msg );
    234         die "CFITSIO error: $msg\n";
     300        die "CFITSIO error: $status => $msg\n";
    235301    }
    236302}
     
    252318}
    253319
     320# Stolen from PStamp/Job.pm.  Thanks, Bill.
     321sub mjd_to_dateobs {
     322    my $mjd = shift;
     323
     324    my $ticks = ($mjd - 40587.0) * 86400;
     325
     326    my ($sec, $min, $hr, $day, $mon, $year) = gmtime($ticks);
     327
     328    return sprintf "'%4d-%02d-%02dT%02d:%02d:%02dZ'", $year+1900, $mon+1, $day, $hr, $min, $sec;
     329}
     330
    254331
    255332
Note: See TracChangeset for help on using the changeset viewer.