Astro/Catalog/IO/VOTable.pm

filter: Statementstmt 93.9% 31 / 33 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod n/a totaltotal 95.4% 42 / 44 CC 1 SCAR 0.3
CC
1
Cov
94%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Astro/Catalog/IO/LCOGTFITSTable.pm ↑ Summary Astro/Catalog/Star.pm »
1 package Astro::Catalog::IO::VOTable;
2  
3 =head1 NAME
4  
5 Astro::Catalog::IO::VOTable - VOTable Input/Output format
6  
7 =head1 SYNOPSIS
8  
9 $catalog = Astro::Catalog::IO::VOTable->_read_catalog(\@lines);
10 \@lines = Astro::Catalog::IO::VOTable->_write_catalog($catalog);
11 Astro::Catalog::IO::VOTable->_default_file();
12  
13 =head1 DESCRIPTION
14  
15 Performs simple IO, reading or writing a VOTable.
16  
17 =cut
18  
19 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 ▶use Astro::Catalog;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Astro::Catalog::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 1 ▶use Astro::Coords;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use Astro::Flux;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use Astro::FluxColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use Astro::Fluxes;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 0 ▶use Astro::VO::VOTable::Document;
Subroutine BEGIN: called CC=1 SCAR=2.6
32  
33 use base qw/Astro::Catalog::IO::ASCII/;
34  
35 use Data::Dumper;
36  
37 our $VERSION = '4.38';
38  
39 =begin __PRIVATE_METHODS__
40  
41 =head1 Private methods
42  
43 These methods are for internal use only and are called from the
44 Astro::Catalog module. It is not expected that anyone would want to
45 call them from outside that module.
46  
47 =over 4
48  
49 =item B<_read_catalog>
50  
51 Parses a reference to an array containing a simply formatted catalog
52  
53 $catalog = Astro::Catalog::IO::VOTable->_read_catalog(\@lines);
54  
55 =cut
56  
57 sub _read_catalog {
58 croak('Usage: _read_catalog(\@lines)') unless scalar(@_) >= 1;
59 my $class = shift;
60 my $arg = shift;
61 my @lines = @{$arg};
62  
63 # create an Astro::Catalog object;
64 my $catalog = new Astro::Catalog();
65  
66 # make the array a string
67 my $string = "";
68 foreach my $i (0 ... $#lines) {
69 $string = $string . $lines[$i] . "\n";
70 }
71  
72 # create a VOTable object from the string.
73 my $doc = Astro::VO::VOTable::Document->new_from_string($string);
74  
75 # Get the VOTABLE element.
76 my $votable = ($doc->get_VOTABLE())[0];
77  
78 # Get the RESOURCE element.
79 my $resource = ($votable->get_RESOURCE())[0];
80  
81 # Get the DESCRIPTION element and its contents.
82 my $description = ($resource->get_DESCRIPTION())[0];
83  
84 # Get the DEFINITIONS element and its contents.
85 my $definitions = ( $votable->get_DEFINITIONS())[0];
86  
87 my ($equinox, $epoch);
88 if (defined $definitions) {
89 # Get the coordinate system (COOSYS) and its contents.
90 my $coosys = ( $definitions->get_COOSYS())[0];
91  
92 # ...and the equinox and epoch and system. I LOVE VOTABLE.
93 $equinox = $coosys->get_equinox();
94 $epoch = $coosys->get_epoch();
95 my $system = $coosys->get_system();
96 if( $system =~ /fk4/i ) {
97 $equinox = "B" . $equinox;
98 }
99 else {
100 $equinox = "J" . $equinox;
101 }
102 }
103 else {
104 # Assume J2000 by default?
105 $equinox = 'J2000';
106 $epoch = '2000.0';
107 }
108  
109 # Get the TABLE element.
110 my $table = ($resource->get_TABLE())[0];
111  
112 # Get the FIELD elements.
113 my (@field_names, @field_ucds, @field_datatypes, @field_units, @field_sizes);
114  
115 foreach my $field ($table->get_FIELD()) {
116 push @field_names, $field->get_name();
117 push @field_ucds, $field->get_ucd();
118 push @field_datatypes, $field->get_datatype();
119 push @field_units, $field->get_unit();
120 push @field_sizes, $field->get_arraysize();
121 }
122  
123 # Get the DATA element.
124 my $data = ($table->get_DATA())[0];
125  
126 # Get the TABLEDATA element.
127 my $tabledata = ($data->get_TABLEDATA())[0];
128  
129 # loop round UCDs and try and figure out what everthing is so
130 # we can stuff the table contents into the relevant places
131 my %contents;
132 my $units = ['sexagesimal', 'sexagesimal'];
133 foreach my $i (0 ... $#field_ucds) {
134 $contents{"id"} = $i if $field_ucds[$i] =~ "ID_MAIN"
135 or $field_ucds[$i] =~ /META.ID.*MAIN/i;
136 if ($field_ucds[$i] =~ /POS.EQ.RA.*MAIN/i) {
137 $contents{"ra"} = $i;
138 $units->[0] = 'degrees' if $field_units[$i] =~ /^deg/i;
139 }
140 if ($field_ucds[$i] =~ /POS.EQ.DEC.*MAIN/i) {
141 $contents{"dec"} = $i;
142 $units->[1] = 'degrees' if $field_units[$i] =~ /^deg/i;
143 }
144 $contents{"quality"} = $i if $field_ucds[$i] =~ "CODE_QUALITY";
145 if ($field_ucds[$i] =~ "PHOT_") {
146 $contents{$field_ucds[$i]} = $i;
147 }
148 $contents{"parallax"} = $i if $field_ucds[$i] =~ "POS_EQ_PLX_FACTOR";
149 $contents{"pm_dec"} = $i if $field_ucds[$i] =~ "POS_EQ_PMDEC";
150 $contents{"pm_ra"} = $i if $field_ucds[$i] =~ "POS_EQ_PMRA";
151 }
152  
153 # loop over each row in the TABLEDATA (ie each star)
154 foreach my $j (0 ... $tabledata->get_num_rows()-1) {
155 # grab a row
156 my @row = $tabledata->get_row($j);
157  
158 # loop around the contents and grab the magnitudes and colours
159 my (@fluxes, @colours);
160 foreach my $key (keys %contents) {
161 # drop through unless we have a magntiude
162 next unless $key =~ "PHOT";
163  
164 my $identifier = $key;
165 $identifier =~ s/^PHOT_[A-Z]+_//;
166  
167 # okay we either have a magnitude or a colour, why did I ever
168 # make these two different things? Maybe I should re-engineer
169 # the Astro::Catalog::Item so that it hides the difference in
170 # some sort of meta API for both? Oh God this is so yuck...
171  
172 # colours
173 if ($identifier =~ /^(\w+)-(\w+)$/) { # non-greedy
174 # we might have a colour, who knows?
175 my $color = new Astro::FluxColor(
176 upper => new Astro::WaveBand(Filter => $1),
177 lower => new Astro::WaveBand(Filter => $2),
178 quantity => $row[$contents{$key}]);
179 unshift @colours, $color; # I don't understand why I have
180 # to unshift here rather than
181 # push, this is oddly disturbing
182 }
183 else {
184 # we might have a magnitude, who knows?
185 my $flux = new Astro::Flux(
186 $row[$contents{$key}], 'mag', $identifier);
187 push @fluxes, $flux;
188 }
189 }
190 my $fluxes = new Astro::Fluxes(@fluxes, @colours);
191  
192 # Set defaults for the proper motions and parallax.
193 my $pm_dec = ((exists $contents{"pm_dec"}) && (defined $contents{"pm_dec"})
194 ? $row[$contents{"pm_dec"}]
195 : undef);
196 my $pm_ra = ((exists $contents{"pm_ra"}) && (defined $contents{"pm_ra"})
197 ? $row[$contents{"pm_ra"}]
198 : undef);
199 my @pm;
200 if (! defined($pm_dec) && ! defined($pm_ra)) {
201 @pm = ();
202 }
203 else {
204 @pm = ($pm_ra, $pm_dec);
205 }
206 my $parallax = ((exists $contents{"parallax"}) && (defined $contents{"parallax"})
207 ? $row[$contents{"parallax"}]
208 : undef);
209  
210 # Create an Astro::Coords object for the star.
211 my $coords = new Astro::Coords(
212 ra => $row[$contents{"ra"}],
213 dec => $row[$contents{"dec"}],
214 type => $equinox,
215 epoch => $epoch,
216 pm => \@pm,
217 parallax => $parallax,
218 units => $units,
219 );
220  
221 # create a star
222 my $star = new Astro::Catalog::Item(
223 id => $row[$contents{"id"}],
224 coords => $coords,
225 #magnitudes => \%mags,
226 #colours => \%colours,
227 fluxes => $fluxes,
228 quality => $row[$contents{"quality"}]);
229  
230 # push the star onto the catalog
231 $catalog->pushstar( $star );
232 }
233  
234 # return the catalog
235 $catalog->origin( 'IO::VOTable' );
236 return $catalog;
237  
238 }
239  
240 =item B<_write_catalog>
241  
242 Will write the catalog object to an simple output format
243  
244 $lines = Astro::Catalog::IO::VOTable->_write_catalog($catalog);
245  
246 where $catalog is an Astro::Catalog object.
247  
248 =cut
249  
250 sub _write_catalog {
251 croak('Usage: _write_catalog($catalog)') unless scalar(@_) >= 1;
252 my $class = shift;
253 my $catalog = shift;
254  
255 # real list of filters and colours in the catalog
256 my @mags = $catalog->starbyindex(0)->what_filters();
257 my @cols = $catalog->starbyindex(0)->what_colours();
258  
259 # number of stars in catalog
260 my $number = $catalog->sizeof();
261  
262 # number of filters & colours
263 my $num_mags = $catalog->starbyindex(0)->what_filters();
264 my $num_cols = $catalog->starbyindex(0)->what_colours();
265  
266 # reference to the $self->{STARS} array in Astro::Catalog
267 my $stars = $catalog->stars();
268  
269 # generate the field headers
270 my (@field_names, @field_ucds, @field_datatypes, @field_units, @field_sizes);
271  
272 # field names
273 push @field_names, "Identifier";
274 push @field_names, "RA";
275 push @field_names, "Dec";
276 foreach my $i (0 .. $#mags) {
277 push @field_names, $mags[$i] . " Magnitude";
278 push @field_names, $mags[$i] . " Error";
279 }
280 foreach my $i (0 .. $#cols) {
281 push @field_names, $cols[$i] . " Colour";
282 push @field_names, $cols[$i] . " Error";
283 }
284 push @field_names, "Quality";
285  
286  
287 # field ucds
288 push @field_ucds, "ID_MAIN";
289 push @field_ucds, "POS_EQ_RA_MAIN";
290 push @field_ucds, "POS_EQ_DEC_MAIN";
291 foreach my $i (0 .. $#mags) {
292 push @field_ucds, "PHOT_MAG_" . $mags[$i];
293 push @field_ucds, "CODE_ERROR";
294 }
295 foreach my $i (0 .. $#cols) {
296 push @field_ucds, "PHOT_CI_" . $cols[$i];
297 push @field_ucds, "CODE_ERROR";
298 }
299 push @field_ucds, "CODE_QUALITY";
300  
301  
302 # field datatypes
303 push @field_datatypes, "char";
304 push @field_datatypes, "char";
305 push @field_datatypes, "char";
306 foreach my $i (0 .. $#mags) {
307 push @field_datatypes, "double";
308 push @field_datatypes, "double";
309 }
310 foreach my $i (0 .. $#cols) {
311 push @field_datatypes, "double";
312 push @field_datatypes, "double";
313 }
314 push @field_datatypes, "int";
315  
316  
317 # field units
318 push @field_units, "";
319 push @field_units, '"h:m:s.ss"';
320 push @field_units, '"d:m:s.ss"';
321 foreach my $i (0 .. $#mags) {
322 push @field_units, "mag";
323 push @field_units, "mag";
324 }
325 foreach my $i (0 .. $#cols) {
326 push @field_units, "mag";
327 push @field_units, "mag";
328 }
329 push @field_units, "";
330  
331  
332 # array size
333 push @field_sizes, "*";
334 push @field_sizes, "*";
335 push @field_sizes, "*";
336  
337 # generate the data table
338 my @data;
339  
340 foreach my $star (0 .. $#$stars) {
341 my @row;
342  
343 # Check to see if we should be writing out the proper motions
344 # and parallax.
345 my $coords = ${$stars}[$star]->coords;
346  
347 my $name = $coords->name;
348 my $type = $coords->type;
349 unless ($coords->type eq 'RADEC') {
350 warnings::warnif "Coordinate of type '$type' for target '$name' not currently supported\n";
351 next;
352 }
353  
354 if (scalar $coords->pm) {
355 push @field_names, "RA Proper Motion";
356 push @field_names, "Dec Proper Motion";
357 push @field_ucds, "POS_EQ_PMRA";
358 push @field_ucds, "POS_EQ_PMDEC";
359 push @field_datatypes, "double";
360 push @field_datatypes, "double";
361 push @field_units, "arcsec/yr";
362 push @field_units, "arcsec/yr";
363 }
364 if (defined $coords->parallax) {
365 push @field_names, "Parallax";
366 push @field_ucds, "POS_EQ_PLX_FACTOR";
367 push @field_datatypes, "double";
368 push @field_units, "arcsec";
369 }
370  
371 # id
372 if ( defined ${$stars}[$star]->id() ) {
373 push @row, ${$stars}[$star]->id();
374 }
375 else {
376 push @row, $star;
377 }
378  
379 # ra & dec -- we want these in J2000.
380 push @row, $coords->ra2000(format => 's');
381 push @row, $coords->dec2000(format => 's');
382  
383 # magnitudes
384 foreach my $i (0 .. $#mags) {
385 if (defined ${$stars}[$star]->get_magnitude($mags[$i])) {
386 push @row, ${$stars}[$star]->get_magnitude($mags[$i]);
387 }
388 else {
389 push @row, "0.000";
390 }
391 if (defined ${$stars}[$star]->get_errors($mags[$i])) {
392 push @row, ${$stars}[$star]->get_errors($mags[$i]);
393 }
394 else {
395 push @row, "0.000";
396 }
397 }
398  
399 # colours
400 foreach my $i (0 .. $#cols) {
401 if (defined ${$stars}[$star]->get_colour($cols[$i])) {
402 push @row, ${$stars}[$star]->get_colour($cols[$i]);
403 }
404 else {
405 push @row, "0.000";
406 }
407 if (defined ${$stars}[$star]->get_colourerr($cols[$i])) {
408 push @row, ${$stars}[$star]->get_colourerr($cols[$i]);
409 }
410 else {
411 push @row, "0.000";
412 }
413 }
414  
415 # quality
416 if (defined ${$stars}[$star]->quality()) {
417 push @row, ${$stars}[$star]->quality();
418 }
419 else {
420 push @row, "0";
421 }
422  
423 # Proper motions and parallax
424 if (defined ${$stars}[$star]->coords) {
425 my $coords = ${$stars}[$star]->coords;
426 my @pm = $coords->pm;
427 push @row, $pm[0];
428 push @row, $pm[1];
429 push @row, $coords->parallax;
430 }
431  
432 # push a reference to the row into the data
433 push @data, \@row;
434 }
435  
436 # Create the VOTABLE document.
437 my $doc = new Astro::VO::VOTable::Document();
438  
439 # Get the VOTABLE element.
440 my $votable = ($doc->get_VOTABLE)[0];
441  
442 # Create the DESCRIPTION element and its contents, and add it to the VOTABLE
443 my $description = new Astro::VO::VOTable::DESCRIPTION();
444 $description->set('Created using Astro::Catalog::IO::VOTable');
445 $votable->set_DESCRIPTION($description);
446  
447 # Create a DEFINITION element and its contents and add it to the VOTABLE
448 my $definitions = new Astro::VO::VOTable::DEFINITIONS();
449 my $coosys = new Astro::VO::VOTable::COOSYS();
450 $coosys->set_ID("J2000");
451 $coosys->set_equinox(2000.0);
452 $coosys->set_epoch(2000.0);
453 $coosys->set_system('eq_FK5');
454 $definitions->set_COOSYS( $coosys );
455 $votable->set_DEFINITIONS( $definitions );
456  
457 # Create the RESOURCE element and add it to the VOTABLE.
458 my $resource = new Astro::VO::VOTable::RESOURCE();
459 $votable->set_RESOURCE($resource);
460  
461 # Create the LINK element and its contents, and add it to the VOTABLE
462 my $link = new Astro::VO::VOTable::LINK();
463 $link->set_title('eSTAR Project');
464 $link->set_href('http://www.estar.org.uk/');
465 $link->set_content_role('doc');
466 $resource->set_LINK($link);
467  
468 # Create the TABLE element and add it to the RESOURCE.
469 my $table = new Astro::VO::VOTable::TABLE();
470 $resource->set_TABLE($table);
471  
472 # Create and add the FIELD elements to the TABLE.
473 my ($i);
474 my ($field);
475 for ($i = 0; $i < @field_names; $i++) {
476 $field = new Astro::VO::VOTable::FIELD();
477 $field->set_name($field_names[$i]);
478 $field->set_ucd($field_ucds[$i]);
479 $field->set_datatype($field_datatypes[$i]);
480 $field->set_unit($field_units[$i]);
481 $field->set_arraysize($field_sizes[$i]) if defined $field_sizes[$i];
482 $table->append_FIELD($field);
483 }
484  
485 # Create and append the DATA element.
486 my $data = new Astro::VO::VOTable::DATA();
487 $table->set_DATA($data);
488  
489 # Create and append the TABLEDATA element.
490 my $tabledata = new Astro::VO::VOTable::TABLEDATA();
491 $data->set_TABLEDATA($tabledata);
492  
493 # Create and append each TR element, and each TD element.
494 my ($tr, $td);
495 my ($j);
496 for ($i = 0; $i < @data; $i++) {
497 $tr = new Astro::VO::VOTable::TR();
498 for ($j = 0; $j < @field_names; $j++) {
499 $td = new Astro::VO::VOTable::TD();
500 $td->set($data[$i][$j]);
501 $tr->append_TD($td);
502 }
503 $tabledata->append_TR($tr);
504 }
505  
506 # Print the finished document.
507 my $output_string = $doc->toString(1);
508 my @output = split "\n", $output_string;
509  
510 return \@output;
511 }
512  
513 =item B<_default_file>
514  
515 If Astro::Catalog is created with a Format but no Filename or other data
516 source it checked this routine to see whether there is a default file
517 that should be read. This is mainly for Astro::Catalo::IO::JCMT and the
518 JAC, but might prive useful elsewhere.
519  
520 =cut
521  
522 sub _default_file {
523 # returns an empty list
524 return;
525 }
526  
527 1;
528  
529 __END__
« Astro/Catalog/IO/LCOGTFITSTable.pm ↑ Summary Astro/Catalog/Star.pm »