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_create

    r28043 r28980  
    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;
Note: See TracChangeset for help on using the changeset viewer.