Changeset 28806 for trunk/pstamp/scripts/detect_query_create
- Timestamp:
- Jul 30, 2010, 12:41:45 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/pstamp/scripts/detect_query_create (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pstamp/scripts/detect_query_create
r28043 r28806 17 17 $query_id, 18 18 $nostage, 19 $version, 19 20 ); 20 21 … … 22 23 'input|i=s' => \$input, 23 24 'output|o=s' => \$output, 24 'query_id|q=s' => \$query_id, 25 'query_id|q=s' => \$query_id, 26 'version|v=s' => \$version, 25 27 'nostage' => \$nostage, 26 28 ) or pod2usage( 2 ); … … 30 32 -exitval => 3) unless defined $input and defined $output; 31 33 32 # The header kewords 34 # Read what we've been given 35 my $in; 36 if ($input eq '-') { 37 $in = \*STDIN; 38 } else { 39 open $in, "<$input" or die "cannot open $input for reading"; 40 } 41 my %colData; 42 my %headerData; 43 my $numRows = read_data_for_table($in,'\s+', \%colData, \%headerData); 44 if (!$numRows) { 45 print STDERR "no data in $input\n"; 46 exit 1; 47 } 48 49 # The header keywords 33 50 my $header = [ 34 51 { name => 'QUERY_ID', … … 62 79 value => undef 63 80 }, 64 { name => 'STAGE',81 { name => 'STAGE', 65 82 writetype => TSTRING, 66 83 comment => 'processing stage to examine', 67 84 value => undef 68 }85 } 69 86 ]; 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 90 if (defined($query_id)) { 91 $headerData{QUERY_ID} = $query_id; 92 } 93 unless (exists($headerData{QUERY_ID})) { 94 die "No QUERY_ID specified for header."; 95 } 96 97 unless (exists($headerData{EXTVER})) { 98 die "No EXTVER specified for header."; 99 } 100 if ($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 } 113 elsif ($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 } 128 else { 129 die "Unknown EXTVER = $headerData{EXTVER}."; 76 130 } 77 131 … … 91 145 { name => 'MAG', type => 'D', writetype => TDOUBLE }, 92 146 ]; 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); 147 my $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. 155 if ($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 } 163 if ($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 181 my @colDataAoA = (); 182 foreach 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 193 my $status = make_fits_table($output, EXTNAME, $numRows, \@colDataAoA, $columns, $header); 121 194 122 195 exit $status; … … 230 303 231 304 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 } 273 350 # we return the number of rows read 274 351 return $row_num;
Note:
See TracChangeset
for help on using the changeset viewer.
