IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 28806


Ignore:
Timestamp:
Jul 30, 2010, 12:41:45 PM (16 years ago)
Author:
watersc1
Message:

Final edits on new detectability code.

Location:
trunk/pstamp/scripts
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/pstamp/scripts/detect_query_create

    r28043 r28806  
    1717     $query_id,
    1818     $nostage,
     19     $version,
    1920     );
    2021
     
    2223           'input|i=s'    => \$input,
    2324           'output|o=s'   => \$output,
    24            'query_id|q=s'  => \$query_id,
     25           'query_id|q=s' => \$query_id,
     26           'version|v=s'  => \$version,
    2527           'nostage'      => \$nostage,
    2628) or pod2usage( 2 );
     
    3032           -exitval => 3) unless defined $input and defined $output;
    3133
    32 # The header kewords
     34# Read what we've been given
     35my $in;
     36if ($input eq '-') {
     37    $in = \*STDIN;
     38} else {
     39    open $in, "<$input" or die "cannot open $input for reading";
     40}
     41my %colData;
     42my %headerData;
     43my $numRows = read_data_for_table($in,'\s+', \%colData, \%headerData);
     44if (!$numRows) {
     45    print STDERR "no data in $input\n";
     46    exit 1;
     47}
     48
     49# The header keywords
    3350my $header = [
    3451        { name =>  'QUERY_ID',
     
    6279                    value => undef
    6380        },
    64        { name =>  'STAGE',
     81        { name =>  'STAGE',
    6582                   writetype => TSTRING,
    6683                   comment => 'processing stage to examine',
    6784                   value => undef
    68        }
     85        }
    6986];
    70 unless(defined($nostage)) {
    71     push @{ $header },        { name =>  'STAGE',
    72                                 writetype => TSTRING,
    73                                 comment => 'processing stage to examine',
    74                                 value => undef
    75     };
     87
     88# Validate header.
     89
     90if (defined($query_id)) {
     91    $headerData{QUERY_ID} = $query_id;
     92}
     93unless (exists($headerData{QUERY_ID})) {
     94    die "No QUERY_ID specified for header.";
     95}
     96
     97unless (exists($headerData{EXTVER})) {
     98    die "No EXTVER specified for header.";
     99}
     100if ($headerData{EXTVER} == 1) {
     101    unless (exists($headerData{STAGE})) {
     102        warn "No STAGE value specified in header. Assuming default value of 'diff'";
     103        $headerData{STAGE} = 'diff';
     104    }
     105    foreach my $entry_ref (@{ $header }) {
     106        my $name = $entry_ref->{name};
     107        unless (exists($headerData{$name})) {
     108            die "Required header value $name not specified (try EXTVER=2?).";
     109        }
     110        $entry_ref->{value} = $headerData{$name};
     111    }
     112}   
     113elsif ($headerData{EXTVER} == 2) {
     114    unless (exists($headerData{STAGE})) {
     115        warn "No STAGE value specified in header. Assuming default value of 'diff'";
     116        $headerData{STAGE} = 'diff';
     117    }
     118    my $tmp_header;
     119    foreach my $entry_ref (@{ $header }) {
     120        my $name = $entry_ref->{name};
     121        if (exists($headerData{$name})) {
     122            $entry_ref->{value} = $headerData{$name};
     123            push @{ $tmp_header }, $entry_ref;
     124        }
     125    }
     126    $header = $tmp_header;
     127}
     128else {
     129    die "Unknown EXTVER = $headerData{EXTVER}.";
    76130}
    77131
     
    91145        { name => 'MAG',      type => 'D',   writetype => TDOUBLE },
    92146];
    93 
    94 my $in;
    95 if ($input eq '-') {
    96     $in = \*STDIN;
    97 } else {
    98     open $in, "<$input" or die "cannot open $input for reading";
    99 }
    100 
    101 my @colData;
    102 foreach (@$columns) {
    103     push @colData, [];
    104 }
    105 
    106 
    107 my $numRows = read_data_for_table($in,'\s+', \@colData, $header);
    108 if (!$numRows) {
    109     print STDERR "no data in $input\n";
    110     exit 1;
    111 }
    112 
    113 # overwrite the QUERY_ID value from the input file with the command
    114 # line argument
    115 
    116 if ($query_id) {
    117     $header->[0]->{value} = $query_id;
    118 }
    119 
    120 my $status = make_fits_table($output, EXTNAME, $numRows, \@colData, $columns, $header);
     147my $columns_v2 = [
     148    { name => 'FPA_ID', type => '20A', writetype => TSTRING },
     149    { name => 'MJD-OBS', type => 'D', writetype => TDOUBLE },
     150    { name => 'FILTER', type => '20A', writetype => TSTRING },
     151    { name => 'OBSCODE', type => '20A', writetype => TSTRING },
     152    { name => 'STAGE', type => '20A', writetype => TSTRING }
     153    ];
     154# Validate the data.
     155if ($headerData{EXTVER} == 1) {
     156    foreach my $entry_ref (@{ $columns }) {
     157        my $name = $entry_ref->{name};
     158        unless (exists($colData{$name})) {
     159            die "Required data column $name not found (try EXTVER=2?).";
     160        }
     161    }
     162}
     163if ($headerData{EXTVER} == 2) {
     164    my $tmp_columns;
     165    foreach my $entry_ref (@{ $columns }) {
     166        my $name = $entry_ref->{name};
     167        if (exists($colData{$name})) {
     168            push @{ $tmp_columns }, $entry_ref;
     169        }
     170    }
     171    foreach my $entry_ref (@{ $columns_v2 }) {
     172        my $name = $entry_ref->{name};
     173        if (exists($colData{$name})) {
     174            push @{ $tmp_columns }, $entry_ref;
     175        }
     176    }
     177    $columns = $tmp_columns;
     178}           
     179
     180# Construct the array of arrays
     181my @colDataAoA = ();
     182foreach my $col_def (@{ $columns }) {
     183    my $name = $col_def->{name};
     184    push @colDataAoA, $colData{$name};
     185}
     186
     187# foreach (@$columns) {
     188#     push @colData, [];
     189# }
     190
     191
     192
     193my $status = make_fits_table($output, EXTNAME, $numRows, \@colDataAoA, $columns, $header);
    121194
    122195exit $status;
     
    230303
    231304    my $line_num = 0;
    232 
    233     # read data for header if any data is expected
    234     if ($header) {
    235         my $nhead = @$header;
    236         while (my $line = <$in>) {
    237             $line_num++;
    238             chomp $line;
    239             next if !$line;             # skip blank lines
    240             next if ($line =~ /^#/);    # skip comment lines
    241             my @vals = split /$sep/, $line;
    242             my $nvals = @vals;
    243             die "number of header columns in input $nvals does not equal expected number of header words $nhead"
    244                     if (@vals != @$header);
    245 
    246             for (my $i=0; $i < @$header; $i++) {
    247                 $header->[$i]->{value} = $vals[$i];
    248             }
    249 
    250             last; # only one header line
    251         }
    252     }
    253 
    254     my $row_num = 0;
    255     my $ncols = @$colData;
    256     while (my $line = <$in>) {
    257         chomp $line;
    258         $line_num++;
    259         next if !$line;             # skip blank lines
    260         next if ($line =~ /^#/);    # skip comment lines
    261 
    262         my @vals = split /$sep/, $line;
    263         my $nvals = @vals;
    264         die "number of columns $nvals in input does not equal expected number of header "
    265                 . " words $ncols on line $line_num" if ($nvals != $ncols);
    266 
    267         for (my $col = 0; $col < @$colData; $col++) {
    268             $colData->[$col]->[$row_num] = $vals[$col];
    269         }
    270         $row_num++;
    271     }
    272 
     305    my @keywords = ();
     306    my $row_num;
     307    while (<$in>) {
     308        chomp;
     309        if ($line_num == 0) {
     310            # Parse header information keywords
     311            $_ =~ s/#//g;
     312            @keywords = split /\s+/;
     313            if ($keywords[0] eq '') {
     314                shift(@keywords);
     315            }
     316        }
     317        elsif ($line_num == 1) {
     318            # Parse header information values
     319            my @values = split /\s+/;
     320            if ($#values != $#keywords) {
     321                die "Number of header columns in input does not equal expected number of header words";
     322            }
     323            for (my $i = 0; $i <= $#values; $i++) {
     324                $header->{$keywords[$i]} = $values[$i];
     325            }           
     326        }
     327        elsif ($line_num == 2) {
     328            # Parse table information keywords, dumping old keywords
     329            $_ =~ s/#//g;
     330            @keywords = split /\s+/;
     331            if ($keywords[0] eq '') {
     332                shift(@keywords);
     333            }
     334        }
     335        else {
     336            # Parse table information values
     337            unless ($_ =~ /^#/) {
     338                my @values = split /\s+/;
     339                if ($#values != $#keywords) {
     340                    die "Number of header columns in input does not equal expected number of header words";
     341                }
     342                for (my $i = 0; $i <= $#values; $i++) {
     343                    push @{ $colData{$keywords[$i]} }, $values[$i];
     344                    $row_num = $#{ $colData{$keywords[$i] } } + 1;
     345                }               
     346            }
     347        }
     348        $line_num++;
     349    }
    273350    # we return the number of rows read
    274351    return $row_num;
  • trunk/pstamp/scripts/detect_query_read

    r28777 r28806  
    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 -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
  • trunk/pstamp/scripts/detectability_respond.pl

    r28777 r28806  
    108108# Parse input request file using detect_query_read (as it's already written).
    109109#
    110     my $dqr_command = "$detect_query_read --input $request_file";
     110    my $dqr_command = "$detect_query_read --dbname $imagedb --input $request_file";
    111111    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    112112        run(command => $dqr_command, verbose => $verbose);
Note: See TracChangeset for help on using the changeset viewer.